Debian Codegraph @

src/cdogs/map_static.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2014, 2016, 2018-2021 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 "map_static.h"
29
30 #include "actors.h"
31 #include "campaigns.h"
32 #include "events.h"
33 #include "game_events.h"
34 #include "gamedata.h"
35 #include "handle_game_events.h"
36 #include "log.h"
37 #include "map_build.h"
38 #include "net_util.h"
39
40 static int AddTileClass(any_t data, any_t item);
41 void MapStaticLoad(MapBuilder *mb)
42 {
43 // Tile classes
44 if (hashmap_iterate(
45 mb->mission->u.Static.TileClasses, AddTileClass, mb->Map) != MAP_OK)
46 {
47 CASSERT(false, "failed to add static tile classes");
48 }
49
50 // Tiles
51 const Rect2i r = Rect2iNew(svec2i_zero(), mb->Map->Size);
52 RECT_FOREACH(r)
53 MapStaticLoadTile(mb, _v);
54 RECT_FOREACH_END()
55
56 // Start/exit areas
57 mb->Map->start = mb->mission->u.Static.Start;
58 CArrayCopy(&mb->Map->exits, &mb->mission->u.Static.Exits);
59 }
60 static int AddTileClass(any_t data, any_t item)
61 {
62 Map *m = data;
63 TileClass *t = item;
64 // Attach base style to tile class for convenience in editors etc
65 CSTRDUP(t->StyleType, TileClassBaseStyleType(t->Type));
66 TileClassesAdd(
67 m->TileClasses, &gPicManager, t, t->Style, t->StyleType, t->Mask,
68 t->MaskAlt);
69 switch (t->Type)
70 {
71 case TILE_CLASS_DOOR:
72 SetupDoorTileClasses(m, &gPicManager, t);
73 break;
74 case TILE_CLASS_WALL:
75 SetupWallTileClasses(m, &gPicManager, t);
76 break;
77 case TILE_CLASS_FLOOR:
78 SetupFloorTileClasses(m, &gPicManager, t);
79 break;
80 default:
81 break;
82 }
83 return MAP_OK;
84 }
85
86 void MapStaticLoadTile(MapBuilder *mb, const struct vec2i v)
87 {
88 if (!MapIsTileIn(mb->Map, v))
89 return;
90 const int idx = v.y * mb->Map->Size.x + v.x;
91 uint16_t tileAccess =
92 *(uint16_t *)CArrayGet(&mb->mission->u.Static.Access, idx);
93 if (!AreKeysAllowed(mb->mode))
94 {
95 tileAccess = 0;
96 }
97 const TileClass *tc =
98 MissionStaticGetTileClass(&mb->mission->u.Static, mb->Map->Size, v);
99 MapBuilderSetTile(mb, v, tc);
100 MapBuildSetAccess(mb, v, tileAccess);
101 }
102
103 static void AddCharacters(const MapBuilder *mb, const CArray *characters);
104 static void AddObjectives(MapBuilder *mb, const CArray *objectives);
105 static void AddKeys(MapBuilder *mb, const CArray *keys);
106 static void AddPickups(const CArray *pickups);
107 void MapStaticLoadDynamic(MapBuilder *mb)
108 {
109 // Map objects
110 CA_FOREACH(const MapObjectPositions, mop, mb->mission->u.Static.Items)
111 for (int j = 0; j < (int)mop->Positions.size; j++)
112 {
113 const struct vec2i *pos = CArrayGet(&mop->Positions, j);
114 MapTryPlaceOneObject(mb, *pos, mop->M, 0, false);
115 }
116 CA_FOREACH_END()
117
118 if (ModeHasNPCs(mb->mode))
119 {
120 AddCharacters(mb, &mb->mission->u.Static.Characters);
121 }
122
123 if (HasObjectives(mb->mode))
124 {
125 AddObjectives(mb, &mb->mission->u.Static.Objectives);
126 }
127
128 if (AreKeysAllowed(mb->mode))
129 {
130 AddKeys(mb, &mb->mission->u.Static.Keys);
131 }
132
133 AddPickups(&mb->mission->u.Static.Pickups);
134
135 // Process the events to place dynamic objects
136 HandleGameEvents(&gGameEvents, NULL, NULL, NULL, NULL);
137 }
138 static void AddCharacter(const MapBuilder *mb, const CharacterPlaces *cps);
139 static void AddCharacters(const MapBuilder *mb, const CArray *characters)
140 {
141 CA_FOREACH(const CharacterPlaces, cps, *characters)
142 AddCharacter(mb, cps);
143 CA_FOREACH_END()
144 }
145 static void AddCharacter(const MapBuilder *mb, const CharacterPlaces *cps)
146 {
147 CASSERT(mb->characters != NULL, "cannot add characters");
148 const Character *c = CArrayGet(
149 &mb->characters->OtherChars, cps->Index);
150 CA_FOREACH(const CharacterPlace, cp, cps->Places)
151 GameEvent e = GameEventNewActorAdd(Vec2CenterOfTile(cp->Pos), c, NULL);
152 e.u.ActorAdd.CharId = cps->Index;
153 e.u.ActorAdd.Direction = cp->Dir;
154 GameEventsEnqueue(&gGameEvents, e);
155 CA_FOREACH_END()
156 }
157 static void AddObjective(MapBuilder *mb, const ObjectivePositions *op);
158 static void AddObjectives(MapBuilder *mb, const CArray *objectives)
159 {
160 CA_FOREACH(const ObjectivePositions, op, *objectives)
161 AddObjective(mb, op);
162 CA_FOREACH_END()
163 }
164 static void AddObjective(MapBuilder *mb, const ObjectivePositions *op)
165 {
166 if (op->Index >= (int)mb->mission->Objectives.size)
167 {
168 LOG(LM_MAP, LL_ERROR, "cannot add objective; objective #%d missing",
169 op->Index);
170 return;
171 }
172 Objective *o = CArrayGet(&mb->mission->Objectives, op->Index);
173 CA_FOREACH(const PositionIndex, pi, op->PositionIndices)
174 if (!MapIsTileIn(mb->Map, pi->Position))
175 {
176 LOG(LM_MAP, LL_ERROR, "objective outside map");
177 continue;
178 }
179
180 const struct vec2 pos = Vec2CenterOfTile(pi->Position);
181 switch (o->Type)
182 {
183 case OBJECTIVE_KILL: {
184 CASSERT(mb->characters != NULL, "cannot add kill objective");
185 const int charId = CharacterStoreGetSpecialId(mb->characters, pi->Index);
186 const Character *c = CArrayGet(
187 &mb->characters->OtherChars, charId);
188 GameEvent e = GameEventNewActorAdd(pos, c, NULL);
189 e.u.ActorAdd.CharId = charId;
190 e.u.ActorAdd.ThingFlags = ObjectiveToThing(op->Index);
191 GameEventsEnqueue(&gGameEvents, e);
192 }
193 break;
194 case OBJECTIVE_COLLECT:
195 MapPlaceCollectible(mb->mission, op->Index, pos);
196 break;
197 case OBJECTIVE_DESTROY:
198 MapTryPlaceDestroyObject(mb, mb->mission, op->Index, pi->Position, false);
199 break;
200 case OBJECTIVE_RESCUE: {
201 CASSERT(mb->characters != NULL, "cannot add rescue objective");
202 const int charId = CharacterStoreGetPrisonerId(
203 mb->characters, pi->Index);
204 const Character *c = CArrayGet(
205 &mb->characters->OtherChars, charId);
206 GameEvent e = GameEventNewActorAdd(pos, c, NULL);
207 e.u.ActorAdd.CharId = charId;
208 e.u.ActorAdd.ThingFlags = ObjectiveToThing(op->Index);
209 GameEventsEnqueue(&gGameEvents, e);
210 }
211 break;
212 default:
213 // do nothing
214 break;
215 }
216 o->placed++;
217 CA_FOREACH_END()
218 }
219 static void AddKey(MapBuilder *mb, const KeyPositions *kp);
220 static void AddKeys(MapBuilder *mb, const CArray *keys)
221 {
222 CA_FOREACH(const KeyPositions, kp, *keys)
223 AddKey(mb, kp);
224 CA_FOREACH_END()
225 }
226 static void AddKey(MapBuilder *mb, const KeyPositions *kp)
227 {
228 CA_FOREACH(const struct vec2i, pos, kp->Positions)
229 MapPlaceKey(mb, *pos, kp->Index);
230 CA_FOREACH_END()
231 }
232 static void AddPickup(const PickupPositions *pp);
233 static void AddPickups(const CArray *pickups)
234 {
235 CA_FOREACH(const PickupPositions, pp, *pickups)
236 AddPickup(pp);
237 CA_FOREACH_END()
238 }
239 static void AddPickup(const PickupPositions *pp)
240 {
241 CA_FOREACH(const struct vec2i, pos, pp->Positions)
242 GameEvent e = GameEventNew(GAME_EVENT_ADD_PICKUP);
243 e.u.AddPickup.Pos = Vec2ToNet(Vec2CenterOfTile(*pos));
244 strcpy(e.u.AddPickup.PickupClass, pp->P->Name);
245 GameEventsEnqueue(&gGameEvents, e);
246 CA_FOREACH_END()
247 }