Debian Codegraph @

src/cdogs/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-2024 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 "pickup.h"
29
30 #include "json_utils.h"
31 #include "map.h"
32 #include "net_util.h"
33
34 CArray gPickups;
35 static unsigned int sPickupUIDs;
36 #define PICKUP_SIZE svec2i(8, 8)
37
38 void PickupsInit(void)
39 {
40 CArrayInit(&gPickups, sizeof(Pickup));
41 CArrayReserve(&gPickups, 128);
42 sPickupUIDs = 0;
43 }
44 void PickupsTerminate(void)
45 {
46 CA_FOREACH(const Pickup, p, gPickups)
47 if (p->isInUse)
48 {
49 PickupDestroy(p->UID);
50 }
51 CA_FOREACH_END()
52 CArrayTerminate(&gPickups);
53 }
54 int PickupsGetNextUID(void)
55 {
56 return sPickupUIDs++;
57 }
58 static void PickupDraw(
59 GraphicsDevice *g, const int id, const struct vec2i pos);
60 void PickupAdd(const NAddPickup ap)
61 {
62 // Check if existing pickup
63 Pickup *p = PickupGetByUID(ap.UID);
64 if (p != NULL && p->isInUse)
65 {
66 PickupDestroy(ap.UID);
67 }
68 // Find an empty slot in pickup list
69 p = NULL;
70 int i;
71 for (i = 0; i < (int)gPickups.size; i++)
72 {
73 Pickup *pu = CArrayGet(&gPickups, i);
74 if (!pu->isInUse)
75 {
76 p = pu;
77 break;
78 }
79 }
80 if (p == NULL)
81 {
82 Pickup pu;
83 memset(&pu, 0, sizeof pu);
84 CArrayPushBack(&gPickups, &pu);
85 i = (int)gPickups.size - 1;
86 p = CArrayGet(&gPickups, i);
87 }
88 memset(p, 0, sizeof *p);
89 p->UID = ap.UID;
90 p->class = StrPickupClass(ap.PickupClass);
91 ThingInit(&p->thing, i, KIND_PICKUP, PICKUP_SIZE, ap.ThingFlags);
92 p->thing.CPic = p->class->Pic;
93 p->thing.CPicFunc = PickupDraw;
94 MapTryMoveThing(&gMap, &p->thing, NetToVec2(ap.Pos));
95 p->IsRandomSpawned = ap.IsRandomSpawned;
96 p->PickedUp = false;
97 p->SpawnerUID = ap.SpawnerUID;
98 p->isInUse = true;
99 }
100 void PickupAddGun(const WeaponClass *w, const struct vec2 pos)
101 {
102 if (!w->CanDrop)
103 {
104 return;
105 }
106 GameEvent e = GameEventNew(GAME_EVENT_ADD_PICKUP);
107 sprintf(e.u.AddPickup.PickupClass, "gun_%s", w->name);
108 e.u.AddPickup.Pos = Vec2ToNet(pos);
109 GameEventsEnqueue(&gGameEvents, e);
110 }
111 void PickupDestroy(const int uid)
112 {
113 Pickup *p = PickupGetByUID(uid);
114 CASSERT(p->isInUse, "Destroying not-in-use pickup");
115 MapRemoveThing(&gMap, &p->thing);
116 p->isInUse = false;
117 }
118
119 void PickupsUpdate(CArray *pickups, const int ticks)
120 {
121 CA_FOREACH(Pickup, p, *pickups)
122 if (!p->isInUse)
123 {
124 continue;
125 }
126 ThingUpdate(&p->thing, ticks);
127 CA_FOREACH_END()
128 }
129
130 static void PickupDraw(GraphicsDevice *g, const int id, const struct vec2i pos)
131 {
132 const Pickup *p = CArrayGet(&gPickups, id);
133 CASSERT(p->isInUse, "Cannot draw non-existent pickup");
134 CPicDrawContext c = CPicDrawContextNew();
135 const Pic *pic = CPicGetPic(&p->thing.CPic, c.Dir);
136 if (pic != NULL)
137 {
138 c.Offset = svec2i_scale_divide(CPicGetSize(&p->class->Pic), -2);
139 }
140 CPicDraw(g, &p->thing.CPic, pos, &c);
141 }
142
143 Pickup *PickupGetByUID(const int uid)
144 {
145 CA_FOREACH(Pickup, p, gPickups)
146 if (p->UID == uid)
147 {
148 return p;
149 }
150 CA_FOREACH_END()
151 return NULL;
152 }