Debian Codegraph @

src/cdogs/actor_pickup.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2014-2015, 2017-2020, 2022-2025 Cong Xu
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "actor_pickup.h"
29
30 #include "campaigns.h"
31 #include "gamedata.h"
32 #include "net_util.h"
33
34 static bool TreatAsGunPickup(const PickupEffect *pe, const TActor *a);
35 static bool TryPickupAmmo(TActor *a, const Pickup *p, const PickupEffect *pe);
36 static bool TryPickupGun(
37 TActor *a, const PickupEffect *pe, const bool pickupAll,
38 const char **sound);
39 void PickupPickup(TActor *a, Pickup *p, const bool pickupAll)
40 {
41 if (p->PickedUp)
42 return;
43 CASSERT(a->PlayerUID >= 0, "NPCs cannot pickup");
44 // can always pickup effect-less pickups
45 bool canPickup = p->class->Effects.size == 0;
46 const char *sound = p->class->Sound;
47 GameEvent e;
48
49 CA_FOREACH(const PickupEffect, pe, p->class->Effects)
50 canPickup = PickupApplyEffect(a, p, pe, pickupAll, &sound) || canPickup;
51 CA_FOREACH_END()
52
53 if (canPickup)
54 {
55 if (sound != NULL)
56 {
57 e = GameEventNew(GAME_EVENT_SOUND_AT);
58 strcpy(e.u.SoundAt.Sound, sound);
59 e.u.SoundAt.Pos = Vec2ToNet(a->thing.Pos);
60 GameEventsEnqueue(&gGameEvents, e);
61 }
62 e = GameEventNew(GAME_EVENT_REMOVE_PICKUP);
63 e.u.RemovePickup.UID = p->UID;
64 e.u.RemovePickup.SpawnerUID = p->SpawnerUID;
65 GameEventsEnqueue(&gGameEvents, e);
66 // Prevent multiple pickups by marking
67 p->PickedUp = true;
68 a->PickupAll = false;
69 }
70 }
71 bool PickupApplyEffect(
72 TActor *a, const Pickup *p, const PickupEffect *pe, const bool force,
73 const char **sound)
74 {
75 bool canPickup = false;
76 GameEvent e;
77 switch (pe->Type)
78 {
79 case PICKUP_JEWEL: {
80 canPickup = true;
81 e = GameEventNew(GAME_EVENT_SCORE);
82 e.u.Score.PlayerUID = a->PlayerUID;
83 e.u.Score.Score = pe->u.Score;
84 GameEventsEnqueue(&gGameEvents, e);
85
86 e = GameEventNew(GAME_EVENT_ADD_PARTICLE);
87 e.u.AddParticle.Class =
88 StrParticleClass(&gParticleClasses, "score_text");
89 e.u.AddParticle.ActorUID = a->uid;
90 e.u.AddParticle.Pos = p->thing.Pos;
91 e.u.AddParticle.DZ = 3;
92 if (gCampaign.Setting.Ammo)
93 {
94 sprintf(e.u.AddParticle.Text, "$%d", pe->u.Score);
95 }
96 else
97 {
98 sprintf(e.u.AddParticle.Text, "+%d", pe->u.Score);
99 }
100 GameEventsEnqueue(&gGameEvents, e);
101
102 UpdateMissionObjective(
103 &gMission, p->thing.flags, OBJECTIVE_COLLECT, 1);
104 }
105 break;
106
107 case PICKUP_HEALTH:
108 // Don't pick up unless taken damage
109 if (a->health < ActorGetMaxHeal(a, pe->u.Heal.ExceedMax))
110 {
111 canPickup = true;
112 e = GameEventNew(GAME_EVENT_ACTOR_HEAL);
113 e.u.Heal.UID = a->uid;
114 e.u.Heal.PlayerUID = a->PlayerUID;
115 e.u.Heal.Amount = pe->u.Heal.Amount;
116 e.u.Heal.ExceedMax = pe->u.Heal.ExceedMax;
117 e.u.Heal.IsRandomSpawned = p->IsRandomSpawned;
118 GameEventsEnqueue(&gGameEvents, e);
119 }
120 break;
121
122 case PICKUP_AMMO: // fallthrough
123 case PICKUP_GUN:
124 if (TreatAsGunPickup(pe, a))
125 {
126 canPickup = TryPickupGun(a, pe, force, sound) || canPickup;
127 }
128 else
129 {
130 canPickup = TryPickupAmmo(a, p, pe) || canPickup;
131 }
132 break;
133
134 case PICKUP_KEYCARD: {
135 canPickup = true;
136 e = GameEventNew(GAME_EVENT_ADD_KEYS);
137 e.u.AddKeys.KeyFlags = pe->u.Keys;
138 e.u.AddKeys.Pos = Vec2ToNet(a->thing.Pos);
139 GameEventsEnqueue(&gGameEvents, e);
140 if (*sound == NULL)
141 {
142 *sound = "key";
143 }
144 }
145 break;
146
147 case PICKUP_SHOW_MAP: {
148 canPickup = true;
149 e = GameEventNew(GAME_EVENT_EXPLORE_TILES);
150 e.u.ExploreTiles.Runs_count = 1;
151 e.u.ExploreTiles.Runs[0].Run = gMap.Size.x * gMap.Size.y;
152 GameEventsEnqueue(&gGameEvents, e);
153 }
154 break;
155
156 case PICKUP_LIVES: {
157 canPickup = true;
158 e = GameEventNew(GAME_EVENT_PLAYER_ADD_LIVES);
159 e.u.PlayerAddLives.UID = a->PlayerUID;
160 e.u.PlayerAddLives.Lives = pe->u.Lives;
161 GameEventsEnqueue(&gGameEvents, e);
162 }
163 break;
164
165 case PICKUP_SOUND:
166 canPickup = true;
167 *sound = pe->u.Sound;
168 break;
169
170 case PICKUP_MENU:
171 canPickup = force;
172 if (force)
173 {
174 a->pickupMenu.pickup = p;
175 a->pickupMenu.effect = pe;
176 a->pickupMenu.index = 0;
177 }
178 break;
179
180 default:
181 CASSERT(false, "unexpected pickup type");
182 break;
183 }
184 return canPickup;
185 }
186
187 static bool TreatAsGunPickup(const PickupEffect *pe, const TActor *a)
188 {
189 // Grenades can also be gun pickups; treat as gun pickup if the player
190 // doesn't have its ammo
191 switch (pe->Type)
192 {
193 case PICKUP_AMMO:
194 if (!ActorUsesAmmo(a, pe->u.Ammo.Id))
195 {
196 const Ammo *ammo = AmmoGetById(&gAmmo, pe->u.Ammo.Id);
197 if (ammo->DefaultGun)
198 {
199 return true;
200 }
201 }
202 return false;
203 case PICKUP_GUN: {
204 const WeaponClass *wc = IdWeaponClass(pe->u.GunId);
205 return wc->Type != GUNTYPE_GRENADE ||
206 !ActorUsesAmmo(a, wc->u.Normal.AmmoId);
207 }
208 default:
209 CASSERT(false, "unexpected pickup type");
210 return false;
211 }
212 }
213
214 static bool TryPickupAmmo(TActor *a, const Pickup *p, const PickupEffect *pe)
215 {
216 // Don't pickup if not using ammo
217 if (!gCampaign.Setting.Ammo)
218 {
219 return false;
220 }
221 // Don't pickup if ammo full
222 const int ammoId = pe->Type == PICKUP_AMMO
223 ? (int)pe->u.Ammo.Id
224 : IdWeaponClass(pe->u.GunId)->u.Normal.AmmoId;
225 const Ammo *ammo = AmmoGetById(&gAmmo, ammoId);
226 const int amount =
227 pe->Type == PICKUP_AMMO ? (int)pe->u.Ammo.Amount : ammo->Amount;
228 const int current = *(int *)CArrayGet(&a->ammo, ammoId);
229 if (ammo->Max > 0 && current >= ammo->Max)
230 {
231 return false;
232 }
233
234 // Take ammo
235 GameEvent e = GameEventNew(GAME_EVENT_ACTOR_ADD_AMMO);
236 e.u.AddAmmo.UID = a->uid;
237 e.u.AddAmmo.PlayerUID = a->PlayerUID;
238 e.u.AddAmmo.Ammo.Id = ammoId;
239 e.u.AddAmmo.Ammo.Amount = amount;
240 e.u.AddAmmo.IsRandomSpawned = p->IsRandomSpawned;
241 // Note: receiving end will prevent ammo from exceeding max
242 GameEventsEnqueue(&gGameEvents, e);
243 return true;
244 }
245 static bool TryPickupGun(
246 TActor *a, const PickupEffect *pe, const bool pickupAll,
247 const char **sound)
248 {
249 // Guns can only be picked up manually
250 if (!pickupAll)
251 {
252 return false;
253 }
254
255 // When picking up a gun, the actor always ends up with it equipped, but:
256 // - If the player already has the gun:
257 // - Switch to the same gun and drop the same gun
258 // - If the player doesn't have the gun:
259 // - If the player has an empty slot, pickup the gun into that slot
260 // - If the player doesn't have an empty slot, replace the current gun,
261 // dropping it in the process
262
263 const WeaponClass *wc =
264 pe->Type == PICKUP_GUN
265 ? IdWeaponClass(pe->u.GunId)
266 : StrWeaponClass(AmmoGetById(&gAmmo, pe->u.Ammo.Id)->DefaultGun);
267
268 ActorPickupGun(a, wc);
269
270 // If the player has less ammo than the default amount,
271 // replenish up to this amount
272 // TODO: support multi gun
273 const int ammoId = WC_BARREL_ATTR(*wc, AmmoId, 0);
274 if (ammoId >= 0)
275 {
276 const Ammo *ammo = AmmoGetById(&gAmmo, ammoId);
277 const int ammoDeficit = ammo->Amount * AMMO_STARTING_MULTIPLE -
278 *(int *)CArrayGet(&a->ammo, ammoId);
279 if (ammoDeficit > 0)
280 {
281 GameEvent e = GameEventNew(GAME_EVENT_ACTOR_ADD_AMMO);
282 e.u.AddAmmo.UID = a->uid;
283 e.u.AddAmmo.PlayerUID = a->PlayerUID;
284 e.u.AddAmmo.Ammo.Id = ammoId;
285 e.u.AddAmmo.Ammo.Amount = ammoDeficit;
286 e.u.AddAmmo.IsRandomSpawned = false;
287 GameEventsEnqueue(&gGameEvents, e);
288
289 // Also play an ammo pickup sound
290 *sound = ammo->Sound;
291 }
292 }
293
294 return true;
295 }
296
297 bool PickupIsManual(const TActor *a, const Pickup *p)
298 {
299 if (p->PickedUp)
300 return false;
301 CA_FOREACH(const PickupEffect, pe, p->class->Effects)
302 switch (pe->Type)
303 {
304 case PICKUP_GUN:
305 return true;
306 case PICKUP_AMMO: {
307 const Ammo *ammo = AmmoGetById(&gAmmo, pe->u.Ammo.Id);
308 if (ammo->DefaultGun != NULL && !ActorUsesAmmo(a, pe->u.Ammo.Id))
309 {
310 return true;
311 }
312 }
313 break;
314 case PICKUP_MENU:
315 return true;
316 default:
317 break;
318 }
319 CA_FOREACH_END()
320 return false;
321 }