forked from avose/ChromoCraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_event.c
231 lines (161 loc) · 4.95 KB
/
game_event.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "types.h"
#include "gem.h"
#include "enemy.h"
#include "util.h"
#define GAME_EVENT
#include "game_event.h"
////////////////////////////////////////////////////////////////////////////////
static void game_event_new_node (game_eventq_t *eventq);
static game_eventq_t* game_event_new_eventq();
////////////////////////////////////////////////////////////////////////////////
// Main event q
static game_eventq_t *Events;
static pthread_mutex_t EventLock=PTHREAD_MUTEX_INITIALIZER;
////////////////////////////////////////////////////////////////////////////////
// Init / internally used
////////////////////////////////////////////////////////////////////////////////
// Allocates initial event q
static void game_event_init()
{
pthread_mutex_lock(&EventLock);
// One time allocation
if( !Events ) {
Events = game_event_new_eventq();
}
pthread_mutex_unlock(&EventLock);
}
// Appends a node to the end of the game_event linked list
static void game_event_new_node(game_eventq_t *eventq)
{
game_eventq_node_t *node;
// Allocate a new eventq node
if( !(node=malloc(sizeof(game_eventq_node_t))) ) {
Error("Failed to allocate space (%u) for new eventq node.\n",sizeof(game_eventq_node_t));
}
// Setup the new node for insertion
memset(node,0,sizeof(game_eventq_node_t));
node->next = eventq;
node->last = eventq->last;
// Insert the node
eventq->last->next = node;
eventq->last = node;
}
static game_eventq_t* game_event_new_eventq()
{
game_eventq_t *eventq;
// Allocate eventq
if( !(eventq=malloc(sizeof(game_eventq_t))) ) {
Error("Failed to allocate space (%u) for new eventq.\n",sizeof(game_eventq_t));
}
memset(eventq,0,sizeof(game_eventq_t));
eventq->next = eventq;
eventq->last = eventq;
return eventq;
}
////////////////////////////////////////////////////////////////////////////////
// Used by event consumers
////////////////////////////////////////////////////////////////////////////////
game_eventq_t* game_event_get(game_eventq_t *node)
{
game_event_init();
// Set a current if none
if( !node ) {
node = Events;
}
// Return next node
pthread_mutex_lock(&EventLock);
if( node->next == Events ) {
pthread_mutex_unlock(&EventLock);
return NULL;
}
// Grab the next node
node = node->next;
pthread_mutex_unlock(&EventLock);
// Return the next node
return node;
}
void game_event_remove(game_eventq_t *node)
{
game_event_init();
pthread_mutex_lock(&EventLock);
// Do some sanity checks
if( (!node) || (node == Events) ) {
pthread_mutex_unlock(&EventLock);
return;
}
// Remove it from the queue
node->next->last = node->last;
node->last->next = node->next;
// Free node
free(node);
pthread_mutex_unlock(&EventLock);
}
////////////////////////////////////////////////////////////////////////////////
// Used by event producers
////////////////////////////////////////////////////////////////////////////////
void game_event_tower_install_gem(tower_t *tower, u32b_t ndx)
{
game_event_init();
// Fill in new node
pthread_mutex_lock(&EventLock);
game_event_new_node(Events);
Events->last->type = GAME_EVENT_TOWER_INSTALL_GEM;
Events->last->tower_install_gem.ndx = ndx;
vector3_copy(&(tower->position), &(Events->last->tower_install_gem.tpos));
pthread_mutex_unlock(&EventLock);
}
void game_event_tower_remove_gem(tower_t *tower)
{
game_event_init();
// Fill in new node
pthread_mutex_lock(&EventLock);
game_event_new_node(Events);
Events->last->type = GAME_EVENT_TOWER_REMOVE_GEM;
vector3_copy(&(tower->position), &(Events->last->tower_install_gem.tpos));
pthread_mutex_unlock(&EventLock);
}
void game_event_tower_swap_gem(tower_t *tower, u32b_t ndx)
{
game_event_init();
// Fill in new node
pthread_mutex_lock(&EventLock);
game_event_new_node(Events);
Events->last->type = GAME_EVENT_TOWER_SWAP_GEM;
Events->last->tower_install_gem.ndx = ndx;
vector3_copy(&(tower->position), &(Events->last->tower_install_gem.tpos));
pthread_mutex_unlock(&EventLock);
}
void game_event_create_gem(gem_t *gem)
{
game_event_init();
// Fill in new node
pthread_mutex_lock(&EventLock);
game_event_new_node(Events);
Events->last->type = GAME_EVENT_CREATE_GEM;
memcpy(&(Events->last->create_gem.gem), gem, sizeof(gem_t));
Events->last->create_gem.ndx = -1;
pthread_mutex_unlock(&EventLock);
}
void game_event_mix_gems(u32b_t ndx1, u32b_t ndx2)
{
game_event_init();
// Fill in new node
pthread_mutex_lock(&EventLock);
game_event_new_node(Events);
Events->last->type = GAME_EVENT_MIX_GEMS;
Events->last->mix_gems.ndx1 = ndx1;
Events->last->mix_gems.ndx2 = ndx2;
pthread_mutex_unlock(&EventLock);
}
void game_event_next_wave()
{
game_event_init();
// Fill in new node
pthread_mutex_lock(&EventLock);
game_event_new_node(Events);
Events->last->type = GAME_EVENT_NEXT_WAVE;
pthread_mutex_unlock(&EventLock);
}