Debian Codegraph @

src/cdogs/game_events.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4
5 Copyright (c) 2013-2020, 2022 Cong Xu
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
13 Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29 #include "game_events.h"
30
31 #include <string.h>
32
33 #include "actors.h"
34 #include "net_client.h"
35 #include "net_server.h"
36 #include "pickup.h"
37 #include "utils.h"
38
39 CArray gGameEvents;
40
41 void GameEventsInit(CArray *store)
42 {
43 CArrayInit(store, sizeof(GameEvent));
44 }
45 void GameEventsTerminate(CArray *store)
46 {
47 CArrayTerminate(store);
48 }
49
50 // Array indexed by GameEvent
51 static GameEventEntry sGameEventEntries[] = {
52 {GAME_EVENT_NONE, false, false, false, false, NULL},
53
54 {GAME_EVENT_CLIENT_CONNECT, false, false, false, false, NULL},
55 {GAME_EVENT_CLIENT_ID, false, false, false, false, NClientId_fields},
56 {GAME_EVENT_CAMPAIGN_DEF, false, false, false, false, NCampaignDef_fields},
57 {GAME_EVENT_PLAYER_DATA, true, false, true, false, NPlayerData_fields},
58 {GAME_EVENT_PLAYER_REMOVE, true, false, true, false, NPlayerRemove_fields},
59 {GAME_EVENT_TILE_SET, true, false, true, true, NTileSet_fields},
60
61 {GAME_EVENT_THING_DAMAGE, true, false, true, true, NThingDamage_fields},
62 {GAME_EVENT_MAP_OBJECT_ADD, true, false, true, true, NMapObjectAdd_fields},
63 {GAME_EVENT_MAP_OBJECT_REMOVE, true, false, true, true,
64 NMapObjectRemove_fields},
65 {GAME_EVENT_CLIENT_READY, false, false, false, false, NULL},
66 {GAME_EVENT_NET_GAME_START, false, false, false, false, NULL},
67
68 {GAME_EVENT_CONFIG, true, false, true, false, NConfig_fields},
69 {GAME_EVENT_SCORE, true, true, true, true, NScore_fields},
70 {GAME_EVENT_SOUND_AT, true, false, true, true, NSound_fields},
71 {GAME_EVENT_SCREEN_SHAKE, false, false, true, true, NULL},
72 {GAME_EVENT_SET_MESSAGE, false, false, true, true, NULL},
73
74 {GAME_EVENT_GAME_START, true, false, true, true, NULL},
75 {GAME_EVENT_GAME_BEGIN, true, false, true, true, NGameBegin_fields},
76
77 {GAME_EVENT_ACTOR_ADD, true, false, true, true, NActorAdd_fields},
78 {GAME_EVENT_ACTOR_MOVE, true, true, true, true, NActorMove_fields},
79 {GAME_EVENT_ACTOR_STATE, true, true, true, true, NActorState_fields},
80 {GAME_EVENT_ACTOR_DIR, true, true, true, true, NActorDir_fields},
81 {GAME_EVENT_ACTOR_SLIDE, true, true, true, true, NActorSlide_fields},
82 {GAME_EVENT_ACTOR_IMPULSE, true, false, true, true, NActorImpulse_fields},
83 {GAME_EVENT_ACTOR_SWITCH_GUN, true, true, true, true,
84 NActorSwitchGun_fields},
85 {GAME_EVENT_ACTOR_PICKUP_ALL, false, true, true, true,
86 NActorPickupAll_fields},
87 {GAME_EVENT_ACTOR_REPLACE_GUN, true, false, true, true,
88 NActorReplaceGun_fields},
89 {GAME_EVENT_ACTOR_HEAL, true, false, true, true, NActorHeal_fields},
90 {GAME_EVENT_ACTOR_ADD_AMMO, true, false, true, true, NActorAddAmmo_fields},
91 {GAME_EVENT_ACTOR_USE_AMMO, true, true, true, true, NActorUseAmmo_fields},
92 {GAME_EVENT_ACTOR_DIE, true, false, true, true, NActorDie_fields},
93 {GAME_EVENT_PLAYER_ADD_LIVES, true, false, true, true, NPlayerAddLives_fields},
94 {GAME_EVENT_ACTOR_MELEE, true, true, true, true, NActorMelee_fields},
95 {GAME_EVENT_ACTOR_PILOT, true, true, true, true, NActorPilot_fields},
96
97 {GAME_EVENT_ADD_PICKUP, true, false, true, true, NAddPickup_fields},
98 {GAME_EVENT_REMOVE_PICKUP, true, false, true, true, NRemovePickup_fields},
99
100 {GAME_EVENT_BULLET_BOUNCE, true, false, true, true, NBulletBounce_fields},
101 {GAME_EVENT_REMOVE_BULLET, true, false, true, true, NRemoveBullet_fields},
102 {GAME_EVENT_PARTICLE_REMOVE, false, false, true, true, NULL},
103 {GAME_EVENT_GUN_FIRE, true, true, true, true, NGunFire_fields},
104 {GAME_EVENT_GUN_RELOAD, true, true, true, true, NGunReload_fields},
105 {GAME_EVENT_GUN_STATE, true, true, true, true, NGunState_fields},
106 {GAME_EVENT_ADD_BULLET, true, false, true, true, NAddBullet_fields},
107 {GAME_EVENT_ADD_PARTICLE, false, false, true, true, NULL},
108 {GAME_EVENT_TRIGGER, true, false, true, true, NTrigger_fields},
109 {GAME_EVENT_EXPLORE_TILES, true, false, true, true, NExploreTiles_fields},
110 {GAME_EVENT_RESCUE_CHARACTER, true, false, true, true,
111 NRescueCharacter_fields},
112 {GAME_EVENT_OBJECTIVE_UPDATE, true, false, true, true,
113 NObjectiveUpdate_fields},
114 {GAME_EVENT_ADD_KEYS, true, false, true, true, NAddKeys_fields},
115 {GAME_EVENT_DOOR_TOGGLE, true, false, true, true, NDoorToggle_fields},
116
117 {GAME_EVENT_MISSION_COMPLETE, true, false, true, true,
118 NMissionComplete_fields},
119
120 {GAME_EVENT_MISSION_INCOMPLETE, true, false, true, true, NULL},
121 {GAME_EVENT_MISSION_PICKUP, true, false, true, true, NULL},
122 {GAME_EVENT_MISSION_END, true, false, true, true, NMissionEnd_fields}};
123 GameEventEntry GameEventGetEntry(const GameEventType e)
124 {
125 return sGameEventEntries[(int)e];
126 }
127
128 void GameEventsEnqueue(CArray *store, GameEvent e)
129 {
130 if (store->elemSize == 0)
131 {
132 return;
133 }
134 // If we're the server, broadcast any events that clients need
135 // If we're the client, pass along to server, but only if it's for a local
136 // player Otherwise we'd ping-pong the same updates from the server
137 const GameEventEntry gee = sGameEventEntries[e.Type];
138 if (gee.Broadcast)
139 {
140 NetServerSendMsg(&gNetServer, NET_SERVER_BCAST, gee.Type, &e.u);
141 }
142 if (gee.Submit)
143 {
144 int actorUID = -1;
145 bool actorIsLocal = false;
146 switch (e.Type)
147 {
148 case GAME_EVENT_ACTOR_MOVE:
149 actorUID = e.u.ActorMove.UID;
150 break;
151 case GAME_EVENT_ACTOR_STATE:
152 actorUID = e.u.ActorState.UID;
153 break;
154 case GAME_EVENT_ACTOR_DIR:
155 actorUID = e.u.ActorDir.UID;
156 break;
157 case GAME_EVENT_ACTOR_SLIDE:
158 actorUID = e.u.ActorSlide.UID;
159 break;
160 case GAME_EVENT_ACTOR_SWITCH_GUN:
161 actorUID = e.u.ActorSwitchGun.UID;
162 break;
163 case GAME_EVENT_ACTOR_PICKUP_ALL:
164 actorUID = e.u.ActorPickupAll.UID;
165 break;
166 case GAME_EVENT_ACTOR_USE_AMMO:
167 actorUID = e.u.UseAmmo.UID;
168 break;
169 case GAME_EVENT_ACTOR_MELEE:
170 actorUID = e.u.Melee.UID;
171 break;
172 case GAME_EVENT_ACTOR_PILOT:
173 actorUID = e.u.Pilot.UID;
174 break;
175 case GAME_EVENT_GUN_FIRE:
176 if (e.u.GunFire.IsGun)
177 {
178 actorUID = e.u.GunFire.ActorUID;
179 }
180 break;
181 case GAME_EVENT_GUN_RELOAD:
182 actorIsLocal = PlayerIsLocal(e.u.GunReload.PlayerUID);
183 break;
184 case GAME_EVENT_GUN_STATE:
185 actorUID = e.u.GunState.ActorUID;
186 break;
187 default:
188 break;
189 }
190 if (actorUID >= 0)
191 {
192 actorIsLocal = ActorIsLocalPlayer(actorUID);
193 }
194 if (actorIsLocal)
195 {
196 NetClientSendMsg(&gNetClient, gee.Type, &e.u);
197 }
198 }
199
200 CArrayPushBack(store, &e);
201 }
202 static bool EventComplete(const void *elem);
203 void GameEventsClear(CArray *store)
204 {
205 CArrayRemoveIf(store, EventComplete);
206 }
207 static bool EventComplete(const void *elem)
208 {
209 return ((const GameEvent *)elem)->Delay < 0;
210 }
211
212 GameEvent GameEventNew(GameEventType type)
213 {
214 GameEvent e;
215 memset(&e, 0, sizeof e);
216 e.Type = type;
217 switch (type)
218 {
219 case GAME_EVENT_MAP_OBJECT_REMOVE:
220 e.u.MapObjectRemove.ActorUID = -1;
221 break;
222 case GAME_EVENT_ADD_PICKUP:
223 e.u.AddPickup.UID = PickupsGetNextUID();
224 e.u.AddPickup.SpawnerUID = -1;
225 e.u.AddPickup.has_Pos = true;
226 break;
227 case GAME_EVENT_ACTOR_ADD:
228 e.u.ActorAdd.PilotUID = -1;
229 e.u.ActorAdd.VehicleUID = -1;
230 e.u.ActorAdd.PlayerUID = -1;
231 e.u.ActorAdd.Direction = rand() % DIRECTION_COUNT;
232 e.u.ActorAdd.has_Pos = true;
233 break;
234 case GAME_EVENT_ACTOR_ADD_AMMO:
235 e.u.AddAmmo.has_Ammo = true;
236 break;
237 case GAME_EVENT_ACTOR_IMPULSE:
238 e.u.ActorImpulse.has_Vel = true;
239 e.u.ActorImpulse.has_Pos = true;
240 break;
241 case GAME_EVENT_ACTOR_MOVE:
242 e.u.ActorMove.has_Pos = true;
243 e.u.ActorMove.has_MoveVel = true;
244 break;
245 case GAME_EVENT_ACTOR_SLIDE:
246 e.u.ActorSlide.has_Vel = true;
247 break;
248 case GAME_EVENT_ACTOR_USE_AMMO:
249 e.u.UseAmmo.has_Ammo = true;
250 break;
251 case GAME_EVENT_ACTOR_DIE:
252 e.u.ActorDie.UID = -1;
253 break;
254 case GAME_EVENT_PLAYER_ADD_LIVES:
255 e.u.PlayerAddLives.UID = -1;
256 break;
257 case GAME_EVENT_ADD_BULLET:
258 e.u.AddBullet.ActorUID = -1;
259 e.u.AddBullet.has_MuzzlePos = true;
260 break;
261 case GAME_EVENT_ADD_KEYS:
262 e.u.AddKeys.has_Pos = true;
263 break;
264 case GAME_EVENT_BULLET_BOUNCE:
265 e.u.BulletBounce.has_Vel = true;
266 e.u.BulletBounce.has_Pos = true;
267 e.u.BulletBounce.has_BouncePos = true;
268 break;
269 case GAME_EVENT_GUN_FIRE:
270 e.u.GunFire.has_MuzzlePos = true;
271 break;
272 case GAME_EVENT_GUN_RELOAD:
273 e.u.GunReload.has_Pos = true;
274 break;
275 case GAME_EVENT_MAP_OBJECT_ADD:
276 e.u.MapObjectAdd.has_Pos = true;
277 e.u.MapObjectAdd.has_Mask = true;
278 break;
279 case GAME_EVENT_SOUND_AT:
280 e.u.SoundAt.has_Pos = true;
281 break;
282 case GAME_EVENT_THING_DAMAGE:
283 e.u.ThingDamage.has_Vel = true;
284 break;
285 case GAME_EVENT_TILE_SET:
286 e.u.TileSet.has_Pos = true;
287 break;
288 case GAME_EVENT_TRIGGER:
289 e.u.TriggerEvent.has_Tile = true;
290 break;
291 case GAME_EVENT_PLAYER_DATA:
292 e.u.PlayerData.has_Colors = true;
293 e.u.PlayerData.has_Stats = true;
294 e.u.PlayerData.has_Totals = true;
295 break;
296 default:
297 break;
298 }
299 return e;
300 }
301 GameEvent GameEventNewActorAdd(const struct vec2 pos, const Character *c, const PlayerData *p)
302 {
303 GameEvent e = GameEventNew(GAME_EVENT_ACTOR_ADD);
304 e.u.ActorAdd.Pos = Vec2ToNet(pos);
305 e.u.ActorAdd.UID = ActorsGetNextUID();
306 if (!c->Class->Vehicle)
307 {
308 e.u.ActorAdd.PilotUID = e.u.ActorAdd.UID;
309 }
310 e.u.ActorAdd.Health = CharacterGetStartingHealth(c, c->Class->Vehicle ? NULL : p);
311 return e;
312 }