Debian Codegraph @

src/cdogs/tile_class.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2018-2022 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 "tile_class.h"
29
30 #include "door.h"
31 #include "json_utils.h"
32 #include "log.h"
33 #include "pics.h"
34 #include "sys_config.h"
35
36
37 TileClass gTileFloor = {
38 "tile", NULL, NULL, NULL, { 255, 255, 255, 255 }, { 255, 255, 255, 255 },
39 true, false, false, false, TILE_CLASS_FLOOR, NULL,
40 };
41 TileClass gTileRoom = {
42 "tile", NULL, NULL, NULL, { 255, 255, 255, 255 }, { 255, 255, 255, 255 },
43 true, false, false, true, TILE_CLASS_FLOOR, NULL,
44 };
45 TileClass gTileWall = {
46 "wall", NULL, NULL, NULL, { 255, 255, 255, 255 }, { 255, 255, 255, 255 },
47 false, true, true, false, TILE_CLASS_WALL, NULL,
48 };
49 TileClass gTileNothing = {
50 NULL, NULL, NULL, NULL, { 255, 255, 255, 255 }, { 255, 255, 255, 255 },
51 false, false, false, false, TILE_CLASS_NOTHING, NULL,
52 };
53 TileClass gTileExit = {
54 "exits", NULL, NULL, NULL, { 255, 255, 255, 255 }, { 255, 255, 255, 255 },
55 true, false, false, false, TILE_CLASS_FLOOR, NULL,
56 };
57 TileClass gTileDoor = {
58 "door", NULL, NULL, NULL, { 255, 255, 255, 255 }, { 255, 255, 255, 255 },
59 false, true, true, true, TILE_CLASS_DOOR, NULL,
60 };
61
62 const char *TileClassTypeStr(const TileClassType t)
63 {
64 switch (t)
65 {
66 T2S(TILE_CLASS_FLOOR, "Floor");
67 T2S(TILE_CLASS_WALL, "Wall");
68 T2S(TILE_CLASS_DOOR, "Door");
69 default:
70 return "";
71 }
72 }
73 TileClassType StrTileClassType(const char *s)
74 {
75 S2T(TILE_CLASS_FLOOR, "Floor");
76 S2T(TILE_CLASS_WALL, "Wall");
77 S2T(TILE_CLASS_DOOR, "Door");
78 S2T(TILE_CLASS_NOTHING, "");
79 CASSERT(false, "unknown tile class type");
80 return TILE_CLASS_NOTHING;
81 }
82
83 map_t TileClassesNew(void)
84 {
85 return hashmap_new();
86 }
87 void TileClassesTerminate(map_t c)
88 {
89 hashmap_destroy(c, TileClassDestroy);
90 }
91 void TileClassDestroy(any_t data)
92 {
93 TileClass *tc = data;
94 TileClassTerminate(tc);
95 CFREE(tc);
96 }
97 void TileClassTerminate(TileClass *tc)
98 {
99 if (tc->Type == TILE_CLASS_NOTHING)
100 {
101 return;
102 }
103 CFREE(tc->Name);
104 CFREE(tc->Style);
105 CFREE(tc->StyleType);
106 CFREE(tc->DamageBullet);
107 }
108
109 void TileClassLoadJSON(TileClass *tc, json_t *node)
110 {
111 memset(tc, 0, sizeof *tc);
112 LoadStr(&tc->Name, node, "Name");
113 JSON_UTILS_LOAD_ENUM(tc->Type, node, "Type", StrTileClassType);
114 LoadStr(&tc->Style, node, "Style");
115 LoadColor(&tc->Mask, node, "Mask");
116 LoadColor(&tc->MaskAlt, node, "MaskAlt");
117 LoadBool(&tc->canWalk, node, "CanWalk");
118 LoadBool(&tc->isOpaque, node, "IsOpaque");
119 LoadBool(&tc->shootable, node, "Shootable");
120 LoadBool(&tc->IsRoom, node, "IsRoom");
121 LoadStr(&tc->DamageBullet, node, "DamageBullet");
122 }
123
124 json_t *TileClassSaveJSON(const TileClass *tc)
125 {
126 json_t *itemNode = json_new_object();
127 AddStringPair(itemNode, "Name", tc->Name);
128 AddStringPair(itemNode, "Type", TileClassTypeStr(tc->Type));
129 AddStringPair(itemNode, "Style", tc->Style);
130 AddColorPair(itemNode, "Mask", tc->Mask);
131 AddColorPair(itemNode, "MaskAlt", tc->MaskAlt);
132 AddBoolPair(itemNode, "CanWalk", tc->canWalk);
133 AddBoolPair(itemNode, "IsOpaque", tc->isOpaque);
134 AddBoolPair(itemNode, "Shootable", tc->shootable);
135 AddBoolPair(itemNode, "IsRoom", tc->IsRoom);
136 AddStringPair(itemNode, "DamageBullet", tc->DamageBullet);
137 return itemNode;
138 }
139
140 const char *TileClassBaseStyleType(const TileClassType type)
141 {
142 switch (type)
143 {
144 case TILE_CLASS_FLOOR:
145 return "normal";
146 case TILE_CLASS_WALL:
147 return "o";
148 case TILE_CLASS_DOOR:
149 return "normal_h";
150 default:
151 return "";
152 }
153 }
154
155 void TileClassCopy(TileClass *dst, const TileClass *src)
156 {
157 memcpy(dst, src, sizeof *dst);
158 if (src->Name) CSTRDUP(dst->Name, src->Name);
159 if (src->Style) CSTRDUP(dst->Style, src->Style);
160 if (src->StyleType) CSTRDUP(dst->StyleType, TileClassBaseStyleType(src->Type));
161 if (src->DamageBullet) CSTRDUP(dst->DamageBullet, src->DamageBullet);
162 }
163 const TileClass *StrTileClass(map_t c, const char *name)
164 {
165 if (name == NULL || strlen(name) == 0)
166 {
167 return &gTileNothing;
168 }
169 LOG(LM_MAIN, LL_TRACE, "get tile class %s", name);
170 TileClass *t;
171 const int error = hashmap_get(c, name, (any_t *)&t);
172 if (error == MAP_OK)
173 {
174 return t;
175 }
176 LOG(LM_MAIN, LL_ERROR, "failed to get tile class %s: %d",
177 name, error);
178 return &gTileNothing;
179 }
180
181 void TileClassInit(
182 TileClass *t, PicManager *pm, const TileClass *base,
183 const char *style, const char *type,
184 const color_t mask, const color_t maskAlt)
185 {
186 memcpy(t, base, sizeof *t);
187 if (base->Name) CSTRDUP(t->Name, base->Name);
188 if (style) CSTRDUP(t->Style, style);
189 if (type && strlen(type))
190 {
191 CSTRDUP(t->StyleType, type);
192 }
193 if (base->DamageBullet) CSTRDUP(t->DamageBullet, base->DamageBullet);
194 t->Mask = mask;
195 t->MaskAlt = maskAlt;
196 TileClassReloadPic(t, pm);
197 }
198 void TileClassInitDefault(
199 TileClass *t, PicManager *pm, const TileClass *base,
200 const char *forceStyle, const color_t *forceMask)
201 {
202 const char *style = IntFloorStyle(0);
203 color_t mask = colorBattleshipGrey;
204 color_t maskAlt = colorOfficeGreen;
205 switch (base->Type)
206 {
207 case TILE_CLASS_FLOOR:
208 break;
209 case TILE_CLASS_WALL:
210 style = IntWallStyle(0);
211 mask = colorGravel;
212 break;
213 case TILE_CLASS_DOOR:
214 style = IntDoorStyle(0);
215 mask = colorWhite;
216 break;
217 case TILE_CLASS_NOTHING:
218 break;
219 default:
220 CASSERT(false, "unknown tile class");
221 break;
222 }
223 if (forceStyle != NULL)
224 {
225 style = forceStyle;
226 }
227 if (forceMask != NULL)
228 {
229 mask = *forceMask;
230 }
231 TileClassInit(
232 t, pm, base, style, TileClassBaseStyleType(base->Type),
233 mask, maskAlt);
234 }
235 void TileClassReloadPic(TileClass *t, PicManager *pm)
236 {
237 if (t->Name != NULL && strlen(t->Name) != 0)
238 {
239 // Generate the pic in case it doesn't exist
240 PicManagerGenerateMaskedStylePic(
241 pm, t->Name, t->Style, t->StyleType, t->Mask, t->MaskAlt, false);
242 t->Pic = TileClassGetPic(pm, t);
243 CASSERT(t->Pic != NULL, "cannot find tile pic");
244 }
245 }
246 const TileClass *TileClassesGetMaskedTile(
247 map_t c, const TileClass *baseClass, const char *style, const char *type,
248 const color_t mask, const color_t maskAlt)
249 {
250 char buf[256];
251 TileClassGetName(buf, baseClass, style, type, mask, maskAlt);
252 return StrTileClass(c, buf);
253 }
254 TileClass *TileClassesAdd(
255 map_t c, PicManager *pm, const TileClass *baseClass,
256 const char *style, const char *type,
257 const color_t mask, const color_t maskAlt)
258 {
259 TileClass *t;
260 CMALLOC(t, sizeof *t);
261 TileClassInit(t, pm, baseClass, style, type, mask, maskAlt);
262
263 char buf[CDOGS_PATH_MAX];
264 TileClassGetName(buf, t, style, type, mask, maskAlt);
265 const int error = hashmap_put(c, buf, t);
266 if (error != MAP_OK)
267 {
268 LOG(LM_MAIN, LL_ERROR, "failed to add tile class %s: %d", buf, error);
269 TileClassDestroy(t);
270 return NULL;
271 }
272 LOG(LM_MAIN, LL_DEBUG, "add tile class %s", buf);
273 return t;
274 }
275 void TileClassGetName(
276 char *buf, const TileClass *base, const char *style, const char *type,
277 const color_t mask, const color_t maskAlt)
278 {
279 char maskName[COLOR_STR_BUF];
280 ColorStr(maskName, mask);
281 char maskAltName[COLOR_STR_BUF];
282 ColorStr(maskAltName, maskAlt);
283 sprintf(
284 buf, "%s%s/%s/%s/%s/%s",
285 TileClassTypeStr(base->Type), base->IsRoom ? "room" : "",
286 style, type, maskName, maskAltName);
287 }
288 void TileClassGetBaseName(char *buf, const TileClass *tc)
289 {
290 TileClassGetName(
291 buf, tc, tc->Style, TileClassBaseStyleType(tc->Type),
292 tc->Mask, tc->MaskAlt);
293 }
294 const Pic *TileClassGetPic(const PicManager *pm, const TileClass *tc)
295 {
296 char buf[CDOGS_PATH_MAX];
297 char maskName[COLOR_STR_BUF];
298 ColorStr(maskName, tc->Mask);
299 char maskAltName[COLOR_STR_BUF];
300 ColorStr(maskAltName, tc->MaskAlt);
301 sprintf(
302 buf, "%s/%s/%s/%s/%s",
303 tc->Name, tc->Style, tc->StyleType, maskName, maskAltName);
304 const Pic *pic = PicManagerGetPic(pm, buf);
305 return pic;
306 }
307
308 const TileClass *TileClassesGetExit(
309 map_t c, PicManager *pm, const char *style, const bool isShadow)
310 {
311 char buf[256];
312 const char *type = isShadow ? "shadow" : "normal";
313 TileClassGetName(buf, &gTileExit, style, type, colorWhite, colorWhite);
314 const TileClass *t = StrTileClass(c, buf);
315 if (t != &gTileNothing)
316 {
317 return t;
318 }
319
320 // tile class not found; create it
321 PicManagerGenerateMaskedStylePic(
322 pm, "exits", style, type, colorWhite, colorWhite, true);
323 return TileClassesAdd(
324 c, pm, &gTileExit, style, type, colorWhite, colorWhite);
325 }