/* event.c * * Event and event queue functions. * * Copyright (C) 2002 Andy Goth * Matt Harang * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, * USA. */ #include #include "spite.h" /* Returns a new event constructed according to given parameters. */ event_t* event_new(event_type_t type, player_id_t player, card_t card_pre, card_t card_post, pile_name_t source, pile_name_t dest, char* text, void* data) { event_t* e = xmalloc(sizeof(event_t)); e->type = type; e->player = player; e->card_pre = card_pre; e->card_post = card_post; e->source = source; e->dest = dest; e->text = text; e->data = data; return e; } /* Deallocates an event. */ void event_del(event_t* e) { free(e->data); free(e); } /* Add an event to the queue, growing the queue if necessary. */ void event_queue_push(event_queue_t* q, event_t* e) { if (event_queue_num_events(q) == q->capacity) { int i = q->tail, j = 0; event_t** e = xmalloc(sizeof(event_queue_t*) * q->capacity * 2); while (j < q->num_events) { e[j++] = q->events[i++]; if (i == q->capacity) i = 0; } q->events = e; q->head = q->num_events; q->tail = 0; q->capacity *= 2; } q->events[q->head++] = e; if (q->head == q->capacity) q->head = 0; q->num_events++; } /* Returns the next event on the queue. */ event_t* event_queue_pop(event_queue_t* q) { event_t* e; assert(event_queue_num_events(q) > 0); e = q->events[q->tail++]; if (q->tail == q->capacity) q->tail = 0; return e; } /* Returns the number of events remaining in the queue. */ int event_queue_num_events(const event_queue_t* q) { return q->num_events; } /* Returns a new empty event queue. */ event_queue_t* event_queue_new(void) { event_queue_t* q = xmalloc(sizeof(event_queue_t)); q->capacity = 8; q->events = xmalloc(sizeof(event_t*) * q->capacity); q->num_events = 0; q->head = 0; q->tail = 0; return q; } /* Deallocates an event queue. */ void event_queue_del(event_queue_t* q) { free(q->events); free(q); } /* EOF */