Debian Codegraph @

src/cdogs/door.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (C) 1995 Ronny Wester
5 Copyright (C) 2003 Jeremy Chin
6 Copyright (C) 2003-2007 Lucas Martin-King
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 This file incorporates work covered by the following copyright and
23 permission notice:
24
25 Copyright (c) 2013-2015, 2018-2022 Cong Xu
26 All rights reserved.
27
28 Redistribution and use in source and binary forms, with or without
29 modification, are permitted provided that the following conditions are met:
30
31 Redistributions of source code must retain the above copyright notice, this
32 list of conditions and the following disclaimer.
33 Redistributions in binary form must reproduce the above copyright notice,
34 this list of conditions and the following disclaimer in the documentation
35 and/or other materials provided with the distribution.
36
37 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
41 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 POSSIBILITY OF SUCH DAMAGE.
48 */
49 #include "door.h"
50
51 #include "aheasing/easing.h"
52 #include "draw/draw.h"
53 #include "gamedata.h"
54 #include "log.h"
55 #include "net_util.h"
56 #include "texture.h"
57
58 static DoorType GetDoorType(
59 const bool isHorizontal, const int i, const int count)
60 {
61 if (isHorizontal)
62 {
63 if (count == 1)
64 {
65 return DOORTYPE_H;
66 }
67 else if (i == 0)
68 {
69 return DOORTYPE_LEFT;
70 }
71 else if (i == count - 1)
72 {
73 return DOORTYPE_RIGHT;
74 }
75 else
76 {
77 return DOORTYPE_HMID;
78 }
79 }
80 else
81 {
82 if (count == 1)
83 {
84 return DOORTYPE_V;
85 }
86 else if (i == 0)
87 {
88 return DOORTYPE_TOP;
89 }
90 else if (i == count - 1)
91 {
92 return DOORTYPE_BOTTOM;
93 }
94 else
95 {
96 return DOORTYPE_VMID;
97 }
98 }
99 }
100 static bool DoorTypeIsHorizontal(const DoorType type)
101 {
102 return type == DOORTYPE_H || type == DOORTYPE_LEFT ||
103 type == DOORTYPE_HMID || type == DOORTYPE_RIGHT;
104 }
105
106 static bool DoorGroupIsHorizontal(const MapBuilder *mb, const struct vec2i v);
107 static void DoorGetClassName(
108 char *buf, const TileClass *door, const char *key, const DoorType dType);
109 static int GetDoorCountInGroup(
110 const MapBuilder *mb, const struct vec2i v, const bool isHorizontal);
111 static TWatch *CreateCloseDoorWatch(
112 MapBuilder *mb, const struct vec2i v, const bool isHorizontal,
113 const int doorGroupCount);
114 static Trigger *CreateOpenDoorTrigger(
115 MapBuilder *mb, const struct vec2i v, const bool isHorizontal,
116 const int doorGroupCount, const int keyFlags);
117 struct vec2i MapAddDoorGroup(
118 MapBuilder *mb, const struct vec2i v, const int keyFlags)
119 {
120 const TileClass *door = MapBuilderGetTile(mb, v);
121 const bool isHorizontal = DoorGroupIsHorizontal(mb, v);
122 const int doorGroupCount = GetDoorCountInGroup(mb, v, isHorizontal);
123 const struct vec2i dv = svec2i(isHorizontal ? 1 : 0, isHorizontal ? 0 : 1);
124 const struct vec2i dAside = svec2i(dv.y, dv.x);
125
126 const char *doorKey;
127 switch (keyFlags)
128 {
129 case FLAGS_KEYCARD_RED:
130 doorKey = "red";
131 break;
132 case FLAGS_KEYCARD_BLUE:
133 doorKey = "blue";
134 break;
135 case FLAGS_KEYCARD_GREEN:
136 doorKey = "green";
137 break;
138 case FLAGS_KEYCARD_YELLOW:
139 doorKey = "yellow";
140 break;
141 default:
142 doorKey = "normal";
143 break;
144 }
145
146 // set up the door pics
147 for (int i = 0; i < doorGroupCount; i++)
148 {
149 char doorClassName[CDOGS_FILENAME_MAX];
150 const DoorType type = GetDoorType(isHorizontal, i, doorGroupCount);
151 DoorGetClassName(doorClassName, door, doorKey, type);
152 const TileClass *doorClass = StrTileClass(mb->Map->TileClasses, doorClassName);
153 DoorGetClassName(doorClassName, door, "open", type);
154 const TileClass *doorClassOpen = StrTileClass(mb->Map->TileClasses, doorClassName);
155 const struct vec2i vI = svec2i_add(v, svec2i_scale(dv, (float)i));
156 Tile *tile = MapGetTile(mb->Map, vI);
157 tile->Door.Class = doorClass;
158 DoorStateInit(&tile->Door, false);
159 tile->Door.IsHorizontal = isHorizontal;
160 tile->Class = doorClassOpen;
161 if (isHorizontal)
162 {
163 const struct vec2i vB = svec2i_add(vI, dAside);
164 Tile *tileB = MapGetTile(mb->Map, vB);
165 if (TileCanWalk(tileB))
166 {
167 if (tileB->Class->Type == TILE_CLASS_FLOOR)
168 {
169 // Change the tile below to shadow, cast by this door
170 tileB->Class = TileClassesGetMaskedTile(
171 mb->Map->TileClasses,
172 tileB->Class, tileB->Class->Style, "shadow",
173 tileB->Class->Mask, tileB->Class->MaskAlt);
174 }
175 }
176 }
177 else if (i == 0)
178 {
179 // special door cavity picture
180 DoorGetClassName(doorClassName, door, "wall", type);
181 tile->Door.Class2 = StrTileClass(mb->Map->TileClasses, doorClassName);
182 }
183 }
184
185 TWatch *w = CreateCloseDoorWatch(mb, v, isHorizontal, doorGroupCount);
186 Trigger *t =
187 CreateOpenDoorTrigger(mb, v, isHorizontal, doorGroupCount, keyFlags);
188 // Connect trigger and watch up
189 Action *a = TriggerAddAction(t);
190 a->Type = ACTION_ACTIVATEWATCH;
191 a->u.index = w->index;
192 a = WatchAddAction(w);
193 a->Type = ACTION_SETTRIGGER;
194 a->u.index = t->id;
195
196 // Set tiles on and besides doors free
197 for (int i = 0; i < doorGroupCount; i++)
198 {
199 const struct vec2i vI = svec2i_add(v, svec2i_scale(dv, (float)i));
200 MapBuilderSetLeaveFree(mb, vI, true);
201 const struct vec2i vI1 = svec2i_add(vI, dAside);
202 MapBuilderSetLeaveFree(mb, vI1, true);
203 const struct vec2i vI2 = svec2i_subtract(vI, dAside);
204 MapBuilderSetLeaveFree(mb, vI2, true);
205 }
206
207 return isHorizontal ? svec2i(doorGroupCount, 1)
208 : svec2i(1, doorGroupCount);
209 }
210 static bool DoorGroupIsHorizontal(const MapBuilder *mb, const struct vec2i v)
211 {
212 const TileClass *tileLeftType =
213 MapBuilderGetTile(mb, svec2i(v.x - 1, v.y));
214 const TileClass *tileRightType =
215 MapBuilderGetTile(mb, svec2i(v.x + 1, v.y));
216 const TileClass *tileAboveType =
217 MapBuilderGetTile(mb, svec2i(v.x, v.y - 1));
218 const TileClass *tileBelowType =
219 MapBuilderGetTile(mb, svec2i(v.x, v.y + 1));
220 const bool tileLeftIsDoor =
221 tileLeftType != NULL && tileLeftType->Type == TILE_CLASS_DOOR;
222 const bool tileRightIsDoor =
223 tileRightType != NULL && tileRightType->Type == TILE_CLASS_DOOR;
224 const bool tileAboveIsDoor =
225 tileAboveType != NULL && tileAboveType->Type == TILE_CLASS_DOOR;
226 const bool tileBelowIsDoor =
227 tileBelowType != NULL && tileBelowType->Type == TILE_CLASS_DOOR;
228 const bool tileLeftCanWalk = tileLeftType != NULL && tileLeftType->canWalk;
229 const bool tileRightCanWalk =
230 tileRightType != NULL && tileRightType->canWalk;
231 const bool tileAboveCanWalk =
232 tileAboveType != NULL && tileAboveType->canWalk;
233 const bool tileBelowCanWalk =
234 tileBelowType != NULL && tileBelowType->canWalk;
235 // If door is free all around, follow doors
236 if (tileAboveCanWalk && tileBelowCanWalk && tileLeftCanWalk &&
237 tileRightCanWalk)
238 {
239 if (tileLeftIsDoor && tileRightIsDoor)
240 {
241 return true;
242 }
243 if (tileAboveIsDoor && tileBelowIsDoor)
244 {
245 return false;
246 }
247 return tileLeftIsDoor || tileRightIsDoor;
248 }
249 // If door is free both above/below or both left/right
250 if (tileAboveCanWalk && tileBelowCanWalk)
251 {
252 return true;
253 }
254 if (tileLeftCanWalk && tileRightCanWalk)
255 {
256 return false;
257 }
258 // If door is free only one of above/below/left/right
259 if (tileAboveCanWalk || tileBelowCanWalk)
260 {
261 return true;
262 }
263 // If there are doors, don't follow them - door layers
264 if (tileAboveIsDoor || tileBelowIsDoor)
265 {
266 return true;
267 }
268 return false;
269 }
270
271 // Count the number of doors that are in the same group as this door
272 // Only check to the right/below
273 static int GetDoorCountInGroup(
274 const MapBuilder *mb, const struct vec2i v, const bool isHorizontal)
275 {
276 const struct vec2i dv = svec2i(isHorizontal ? 1 : 0, isHorizontal ? 0 : 1);
277 int count = 0;
278 for (struct vec2i vi = v;
279 MapIsTileIn(mb->Map, vi) &&
280 MapBuilderGetTile(mb, vi)->Type == TILE_CLASS_DOOR;
281 vi = svec2i_add(vi, dv))
282 {
283 count++;
284 }
285 return count;
286 }
287 // Create the watch responsible for closing the door
288 static TWatch *CreateCloseDoorWatch(
289 MapBuilder *mb, const struct vec2i v, const bool isHorizontal,
290 const int doorGroupCount)
291 {
292 TWatch *w = WatchNew();
293 const struct vec2i dv = svec2i(isHorizontal ? 1 : 0, isHorizontal ? 0 : 1);
294 const struct vec2i dAside = svec2i(dv.y, dv.x);
295
296 // The conditions are that the tile above, at and below the doors are empty
297 for (int i = 0; i < doorGroupCount; i++)
298 {
299 const struct vec2i vI = svec2i_add(v, svec2i_scale(dv, (float)i));
300
301 for (int j = -1; j <= 1; j++)
302 {
303 WatchAddCondition(
304 w, CONDITION_TILECLEAR, gCampaign.Setting.DoorOpenTicks,
305 svec2i_add(vI, svec2i_scale(dAside, (float)j)));
306 }
307 }
308
309 // Now the actions of the watch once it's triggered
310 Action *a;
311
312 // Deactivate itself
313 a = WatchAddAction(w);
314 a->Type = ACTION_DEACTIVATEWATCH;
315 a->u.index = w->index;
316 // play close sound at the center of the door group
317 a = WatchAddAction(w);
318 a->Type = ACTION_EVENT;
319 a->u.Event = GameEventNew(GAME_EVENT_SOUND_AT);
320 strcpy(a->u.Event.u.SoundAt.Sound, "door_close");
321 a->u.Event.u.SoundAt.Pos = Vec2ToNet(Vec2CenterOfTile(
322 svec2i_add(v, svec2i_scale(dv, (float)doorGroupCount / 2))));
323
324 // Close doors
325 for (int i = 0; i < doorGroupCount; i++)
326 {
327 const struct vec2i vI = svec2i_add(v, svec2i_scale(dv, (float)i));
328
329 a = WatchAddAction(w);
330 a->Type = ACTION_EVENT;
331 a->u.Event = GameEventNew(GAME_EVENT_DOOR_TOGGLE);
332 a->u.Event.u.DoorToggle.IsOpen = false;
333 a->u.Event.u.DoorToggle.Pos = Vec2i2Net(vI);
334 }
335
336 // Add shadows below doors
337 if (isHorizontal)
338 {
339 for (int i = 0; i < doorGroupCount; i++)
340 {
341 const struct vec2i vI = svec2i_add(v, svec2i_scale(dv, (float)i));
342 const struct vec2i vI2 = svec2i(vI.x + dAside.x, vI.y + dAside.y);
343 const TileClass *t = MapBuilderGetTile(mb, vI2);
344 if (t->Type == TILE_CLASS_FLOOR)
345 {
346 a = WatchAddAction(w);
347 a->Type = ACTION_EVENT;
348 a->u.Event = GameEventNew(GAME_EVENT_TILE_SET);
349 a->u.Event.u.TileSet.Pos = Vec2i2Net(vI2);
350 TileClassGetName(
351 a->u.Event.u.TileSet.ClassName, t, t->Style, "shadow",
352 t->Mask, t->MaskAlt);
353 }
354 }
355 }
356
357 return w;
358 }
359 static void TileAddTrigger(Tile *t, Trigger *tr);
360 static Trigger *CreateOpenDoorTrigger(
361 MapBuilder *mb, const struct vec2i v, const bool isHorizontal,
362 const int doorGroupCount, const int keyFlags)
363 {
364 // All tiles on either side of the door group use the same trigger
365 const struct vec2i dv = svec2i(isHorizontal ? 1 : 0, isHorizontal ? 0 : 1);
366 const struct vec2i dAside = svec2i(dv.y, dv.x);
367 Trigger *t = MapNewTrigger(mb->Map);
368 t->flags = keyFlags;
369
370 // Deactivate itself
371 Action *a;
372 a = TriggerAddAction(t);
373 a->Type = ACTION_CLEARTRIGGER;
374 a->u.index = t->id;
375
376 // Open doors
377 for (int i = 0; i < doorGroupCount; i++)
378 {
379 const struct vec2i vI = svec2i_add(v, svec2i_scale(dv, (float)i));
380 a = TriggerAddAction(t);
381 a->Type = ACTION_EVENT;
382 a->u.Event = GameEventNew(GAME_EVENT_DOOR_TOGGLE);
383 a->u.Event.u.DoorToggle.IsOpen = true;
384 a->u.Event.u.DoorToggle.Pos = Vec2i2Net(vI);
385 }
386
387 // Change tiles below the doors
388 if (isHorizontal)
389 {
390 for (int i = 0; i < doorGroupCount; i++)
391 {
392 const struct vec2i vI = svec2i_add(v, svec2i_scale(dv, (float)i));
393 const struct vec2i vIAside = svec2i_add(vI, dAside);
394 const TileClass *tc = MapBuilderGetTile(mb, vIAside);
395 if (tc->Type == TILE_CLASS_FLOOR)
396 {
397 a = TriggerAddAction(t);
398 // Remove shadows below doors
399 a->Type = ACTION_EVENT;
400 a->u.Event = GameEventNew(GAME_EVENT_TILE_SET);
401 a->u.Event.u.TileSet.Pos = Vec2i2Net(vIAside);
402 TileClassGetName(
403 a->u.Event.u.TileSet.ClassName, tc, tc->Style, "normal",
404 tc->Mask, tc->MaskAlt);
405 }
406 }
407 }
408
409 // Now place the two triggers on the tiles along either side of the doors
410 for (int i = 0; i < doorGroupCount; i++)
411 {
412 const struct vec2i vI = svec2i_add(v, svec2i_scale(dv, (float)i));
413 const struct vec2i vIA = svec2i_subtract(vI, dAside);
414 TileAddTrigger(MapGetTile(mb->Map, vIA), t);
415 const struct vec2i vIB = svec2i_add(vI, dAside);
416 TileAddTrigger(MapGetTile(mb->Map, vIB), t);
417 }
418
419 /// play sound at the center of the door group
420 a = TriggerAddAction(t);
421 a->Type = ACTION_EVENT;
422 a->u.Event = GameEventNew(GAME_EVENT_SOUND_AT);
423 strcpy(a->u.Event.u.SoundAt.Sound, "door");
424 a->u.Event.u.SoundAt.Pos = Vec2ToNet(Vec2CenterOfTile(
425 svec2i_add(v, svec2i_scale(dv, (float)doorGroupCount / 2))));
426
427 return t;
428 }
429 static void TileAddTrigger(Tile *t, Trigger *tr)
430 {
431 if (t == NULL)
432 return;
433 CArrayPushBack(&t->triggers, &tr);
434 }
435
436 // Get the tile class of a door; if it doesn't exist create it
437 // style: office/dungeon/blast/alien, or custom
438 // key: normal/yellow/green/blue/red/wall/open
439 static void DoorGetTypeName(char *buf, const char *key, const DoorType type)
440 {
441 const char *typeStr = "";
442 if (strcmp(key, "wall") == 0)
443 {
444 // no change
445 }
446 else if (strcmp(key, "open") == 0)
447 {
448 typeStr = DoorTypeIsHorizontal(type) ? "_h" : "_v";
449 }
450 else
451 {
452 switch (type)
453 {
454 case DOORTYPE_H:
455 typeStr = "_h";
456 break;
457 case DOORTYPE_LEFT:
458 typeStr = "_left";
459 break;
460 case DOORTYPE_HMID:
461 typeStr = "_hmid";
462 break;
463 case DOORTYPE_RIGHT:
464 typeStr = "_right";
465 break;
466 case DOORTYPE_V:
467 typeStr = "_v";
468 break;
469 case DOORTYPE_TOP:
470 typeStr = "_top";
471 break;
472 case DOORTYPE_VMID:
473 typeStr = "_vmid";
474 break;
475 case DOORTYPE_BOTTOM:
476 typeStr = "_bottom";
477 break;
478 default:
479 CASSERT(false, "unknown doortype");
480 break;
481 }
482 }
483 sprintf(buf, "%s%s", key, typeStr);
484 }
485 static void DoorGetClassName(
486 char *buf, const TileClass *door, const char *key, const DoorType dType)
487 {
488 char type[256];
489 DoorGetTypeName(type, key, dType);
490 const color_t mask = strcmp(key, "normal") == 0 ? door->Mask : colorWhite;
491 const color_t maskAlt =
492 strcmp(key, "normal") == 0 ? door->MaskAlt : colorWhite;
493 // If the key is "wall", it doesn't include orientation
494 TileClassGetName(buf, door, door->Style, type, mask, maskAlt);
495 }
496 void DoorAddClass(
497 map_t c, PicManager *pm, const TileClass *base, const char *key,
498 const DoorType type)
499 {
500 char buf[CDOGS_FILENAME_MAX];
501 DoorGetTypeName(buf, key, type);
502 const color_t mask = strcmp(key, "normal") == 0 ? base->Mask : colorWhite;
503 const color_t maskAlt =
504 strcmp(key, "normal") == 0 ? base->MaskAlt : colorWhite;
505 PicManagerGenerateMaskedStylePic(
506 pm, "door", base->Style, buf, mask, maskAlt, true);
507 TileClass *t =
508 TileClassesAdd(c, pm, base, base->Style, buf, mask, maskAlt);
509 CASSERT(t != NULL, "cannot add door class");
510 const bool isOpenOrWallCavity =
511 strcmp(key, "open") == 0 || strcmp(key, "wall") == 0;
512 t->isOpaque = !isOpenOrWallCavity;
513 t->canWalk = isOpenOrWallCavity;
514 t->shootable = !isOpenOrWallCavity;
515 }
516
517 #define DOORSTYLE_COUNT 4
518 const char *IntDoorStyle(const int i)
519 {
520 static const char *doorStyles[] = {"office", "dungeon", "blast", "alien"};
521 // fix bugs with old campaigns
522 return doorStyles[abs(i) % DOORSTYLE_COUNT];
523 }
524
525 static void DoorDrawPic(
526 const Pic *pic, const struct vec2i pos, const bool crop, const int dy,
527 const int bottom, const color_t mask);
528 void DoorDraw(const DoorState *d, const struct vec2i pos, const color_t mask)
529 {
530 if (d->Class2)
531 {
532 DoorDrawPic(d->Class2->Pic, pos, false, 0, 0, mask);
533 }
534 if (d->Class && d->Class->Pic && (!d->IsOpen || d->Count > 0))
535 {
536 // Drawing doors
537 // Doors may be offset; vertical doors are drawn centered
538 // horizontal doors are bottom aligned
539 const int bottom =
540 d->IsHorizontal ? TILE_HEIGHT * 2 - d->Class->Pic->size.y : 0;
541 const int doorHCropped = d->Class->Pic->size.y - bottom;
542 const double fdy =
543 d->IsOpen
544 ? CircularEaseOut(
545 (DOORSTATE_COUNT - d->Count) / (double)DOORSTATE_COUNT)
546 : CircularEaseIn(d->Count / (double)DOORSTATE_COUNT);
547 const int dy = (int)(fdy * doorHCropped);
548 DoorDrawPic(d->Class->Pic, pos, d->IsHorizontal, dy, bottom, mask);
549 }
550 }
551 static void DoorDrawPic(
552 const Pic *pic, const struct vec2i pos, const bool crop, const int dy,
553 const int bottom, const color_t mask)
554 {
555 if (!pic)
556 {
557 return;
558 }
559 struct vec2i offset =
560 svec2i((TILE_WIDTH - pic->size.x) / 2, WALL_OFFSET_Y + dy);
561 if (pic->size.y > 16)
562 {
563 offset.y += TILE_HEIGHT - (pic->size.y % TILE_HEIGHT);
564 }
565 Rect2i src = Rect2iNew(
566 svec2i_zero(),
567 svec2i(pic->size.x, pic->size.y - (crop ? dy + bottom : 0)));
568 Rect2i dest = Rect2iNew(svec2i_add(pos, offset), src.Size);
569 TextureRender(
570 pic->Tex, gGraphicsDevice.gameWindow.renderer, src, dest, mask, 0.0,
571 SDL_FLIP_NONE);
572 }