Debian Codegraph @

src/cdogs/map.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-2023 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 "map.h"
50
51 #include <assert.h>
52 #include <stdlib.h>
53 #include <string.h>
54
55 #include "actors.h"
56 #include "algorithms.h"
57 #include "ammo.h"
58 #include "collision/collision.h"
59 #include "config.h"
60 #include "door.h"
61 #include "game_events.h"
62 #include "gamedata.h"
63 #include "log.h"
64 #include "los.h"
65 #include "map_build.h"
66 #include "map_cave.h"
67 #include "map_classic.h"
68 #include "map_static.h"
69 #include "mission.h"
70 #include "net_util.h"
71 #include "objs.h"
72 #include "pic_manager.h"
73 #include "pickup.h"
74 #include "sounds.h"
75 #include "utils.h"
76
77 Map gMap;
78
79 const char *IMapTypeStr(IMapType t)
80 {
81 switch (t)
82 {
83 T2S(MAP_FLOOR, "Floor");
84 T2S(MAP_WALL, "Wall");
85 T2S(MAP_DOOR, "Door");
86 T2S(MAP_ROOM, "Room");
87 T2S(MAP_NOTHING, "Nothing");
88 T2S(MAP_SQUARE, "Square");
89 default:
90 return "";
91 }
92 }
93 IMapType StrIMapType(const char *s)
94 {
95 S2T(MAP_FLOOR, "Floor");
96 S2T(MAP_WALL, "Wall");
97 S2T(MAP_DOOR, "Door");
98 S2T(MAP_ROOM, "Room");
99 S2T(MAP_NOTHING, "Nothing");
100 S2T(MAP_SQUARE, "Square");
101 return MAP_FLOOR;
102 }
103
104 uint16_t GetAccessMask(const int k)
105 {
106 if (k == -1)
107 {
108 return 0;
109 }
110 return MAP_ACCESS_YELLOW << k;
111 }
112
113 Tile *MapGetTile(const Map *map, const struct vec2i pos)
114 {
115 if (!MapIsTileIn(map, pos))
116 {
117 return NULL;
118 }
119 return CArrayGet(&map->Tiles, pos.y * map->Size.x + pos.x);
120 }
121
122 bool MapIsTileIn(const Map *map, const struct vec2i pos)
123 {
124 // Check that the tile pos is within the interior of the map
125 return Rect2iIsInside(Rect2iNew(svec2i_zero(), map->Size), pos);
126 }
127 static bool MapIsPosIn(const Map *map, const struct vec2 pos)
128 {
129 // Check that the pos is within the interior of the map
130 return pos.x >= 0 && pos.y >= 0 && MapIsTileIn(map, Vec2ToTile(pos));
131 }
132
133 // If thing is in the exit-index exit
134 // If exit = -1 then check any exit
135 // Returns the exit index or -1 if not in any exit
136 int MapIsTileInExit(const Map *map, const Thing *ti, const int exit)
137 {
138 if (exit < 0)
139 {
140 for (int i = 0; i < (int)map->exits.size; i++)
141 {
142 if (MapIsTileInExit(map, ti, i) != -1)
143 {
144 return i;
145 }
146 }
147 return -1;
148 }
149 const struct vec2i tilePos = Vec2ToTile(ti->Pos);
150 const Exit *e = CArrayGet(&map->exits, exit);
151 // Outer edge is also in exit
152 const Rect2i r = Rect2iNew(e->R.Pos, svec2i_add(e->R.Size, svec2i_one()));
153 return Rect2iIsInside(r, tilePos) ? exit : -1;
154 }
155
156 static Tile *MapGetTileOfItem(Map *map, Thing *t)
157 {
158 const struct vec2i pos = Vec2ToTile(t->Pos);
159 return MapGetTile(map, pos);
160 }
161
162 static void AddItemToTile(Thing *t, Tile *tile);
163 bool MapTryMoveThing(Map *map, Thing *t, const struct vec2 pos)
164 {
165 // Check if we can move to new position
166 if (!MapIsPosIn(map, pos))
167 {
168 return false;
169 }
170 t->LastPos = t->Pos;
171 // When first initialised, position is -1
172 const bool doRemove = t->Pos.x >= 0 && t->Pos.y >= 0;
173 const struct vec2i t1 = Vec2ToTile(t->Pos);
174 const struct vec2i t2 = Vec2ToTile(pos);
175 // If we'll be in the same tile, do nothing
176 if (svec2i_is_equal(t1, t2) && doRemove)
177 {
178 t->Pos = pos;
179 return true;
180 }
181 // Moving; remove from old tile...
182 if (doRemove)
183 {
184 MapRemoveThing(map, t);
185 }
186 // ...move and add to new tile
187 t->Pos = pos;
188 AddItemToTile(t, MapGetTile(map, t2));
189 return true;
190 }
191 static void AddItemToTile(Thing *t, Tile *tile)
192 {
193 ThingId tid;
194 tid.Id = t->id;
195 tid.Kind = t->kind;
196 CASSERT(tid.Id >= 0, "invalid ThingId");
197 CASSERT(tid.Kind >= 0 && tid.Kind <= KIND_PICKUP, "unknown thing kind");
198 CArrayPushBack(&tile->things, &tid);
199 }
200
201 void MapRemoveThing(Map *map, Thing *t)
202 {
203 if (!MapIsPosIn(map, t->Pos))
204 {
205 return;
206 }
207 Tile *tile = MapGetTileOfItem(map, t);
208 CA_FOREACH(ThingId, tid, tile->things)
209 if (tid->Id == t->id && tid->Kind == t->kind)
210 {
211 CArrayDelete(&tile->things, _ca_index);
212 return;
213 }
214 CA_FOREACH_END()
215 CASSERT(false, "Did not find element to delete");
216 }
217
218 struct vec2i MapGetRandomTile(const Map *map)
219 {
220 return svec2i(rand() % map->Size.x, rand() % map->Size.y);
221 }
222
223 struct vec2 MapGetRandomPos(const Map *map)
224 {
225 for (;;)
226 {
227 const struct vec2 pos = svec2(
228 RAND_FLOAT(0, map->Size.x * TILE_WIDTH),
229 RAND_FLOAT(0, map->Size.y * TILE_HEIGHT));
230 // RAND_FLOAT can sometimes produce the max size
231 if (pos.x < map->Size.x * TILE_WIDTH &&
232 pos.y < map->Size.y * TILE_HEIGHT)
233 {
234 return pos;
235 }
236 }
237 }
238
239 static void MapChangeFloor(
240 Map *map, const struct vec2i pos, const TileClass *normal,
241 const TileClass *shadow)
242 {
243 const Tile *tAbove = MapGetTile(map, svec2i(pos.x, pos.y - 1));
244 const int canSeeTileAbove = !(pos.y > 0 && TileIsOpaque(tAbove));
245 Tile *t = MapGetTile(map, pos);
246 if (t == NULL || t->Class->Type != TILE_CLASS_FLOOR)
247 {
248 return;
249 }
250 if (!canSeeTileAbove)
251 {
252 t->Class = shadow;
253 }
254 else
255 {
256 t->Class = normal;
257 }
258 }
259
260 bool MapHasExits(const Map *m)
261 {
262 return m->exits.size > 0 && !IsPVP(gCampaign.Entry.Mode);
263 }
264
265 // Change the perimeter of tiles around the exit area
266 void MapShowExitArea(Map *map, const int i)
267 {
268 const Exit *exit = CArrayGet(&map->exits, i);
269 const int left = exit->R.Pos.x;
270 const int right = left + exit->R.Size.x;
271 const int top = exit->R.Pos.y;
272 const int bottom = top + exit->R.Size.y;
273
274 const TileClass *exitClass = TileClassesGetExit(
275 map->TileClasses, &gPicManager, gMission.missionData->ExitStyle,
276 false);
277 const TileClass *exitShadowClass = TileClassesGetExit(
278 map->TileClasses, &gPicManager, gMission.missionData->ExitStyle, true);
279
280 struct vec2i v;
281 v.y = top;
282 for (v.x = left; v.x <= right; v.x++)
283 {
284 MapChangeFloor(map, v, exitClass, exitShadowClass);
285 }
286 v.y = bottom;
287 for (v.x = left; v.x <= right; v.x++)
288 {
289 MapChangeFloor(map, v, exitClass, exitShadowClass);
290 }
291 v.x = left;
292 for (v.y = top + 1; v.y < bottom; v.y++)
293 {
294 MapChangeFloor(map, v, exitClass, exitShadowClass);
295 }
296 v.x = right;
297 for (v.y = top + 1; v.y < bottom; v.y++)
298 {
299 MapChangeFloor(map, v, exitClass, exitShadowClass);
300 }
301 }
302
303 struct vec2 MapGetExitPos(const Map *m, const int i)
304 {
305 const Exit *exit = CArrayGet(&m->exits, i);
306 return svec2_assign_vec2i(Vec2iCenterOfTile(Rect2iCenter(exit->R)));
307 }
308
309 bool MapHasLockedRooms(const Map *map)
310 {
311 CA_FOREACH(const uint16_t, a, map->access)
312 if (*a != 0)
313 {
314 return true;
315 }
316 CA_FOREACH_END()
317 return false;
318 }
319
320 uint16_t MapGetAccessLevel(const Map *map, const struct vec2i pos)
321 {
322 if (!MapIsTileIn(map, pos))
323 {
324 return 0;
325 }
326 const uint16_t t =
327 *(uint16_t *)CArrayGet(&map->access, pos.y * map->Size.x + pos.x);
328 return AccessCodeToFlags(t);
329 }
330
331 static bool MapTileIsInLockedRoom(const Map *map, const struct vec2i tilePos)
332 {
333 return MapGetAccessLevel(map, tilePos) != 0;
334 }
335
336 bool MapPosIsInLockedRoom(const Map *map, const struct vec2 pos)
337 {
338 const struct vec2i tilePos = Vec2ToTile(pos);
339 return MapTileIsInLockedRoom(map, tilePos);
340 }
341
342 bool MapTryPlaceDestroyObject(
343 MapBuilder *mb, const Mission *m, const int objective, const struct vec2i pos, const bool strict)
344 {
345 const Objective *o = CArrayGet(&m->Objectives, objective);
346 // Pick a random map object out of the available ones
347 const int i = RAND_INT(0, (int)o->u.MapObjects.size - 1);
348 const MapObject *mo = *(const MapObject **)CArrayGet(&o->u.MapObjects, i);
349 return MapTryPlaceOneObject(mb, pos, mo, ObjectiveToThing(objective), strict);
350 }
351
352 void MapPlacePickup(
353 const PickupClass *p, const struct vec2 pos, const int flags)
354 {
355 GameEvent e = GameEventNew(GAME_EVENT_ADD_PICKUP);
356 strcpy(e.u.AddPickup.PickupClass, p->Name);
357 e.u.AddPickup.ThingFlags = flags;
358 e.u.AddPickup.Pos = Vec2ToNet(pos);
359 GameEventsEnqueue(&gGameEvents, e);
360 }
361
362 void MapPlaceCollectible(
363 const Mission *m, const int objective, const struct vec2 pos)
364 {
365 const Objective *o = CArrayGet(&m->Objectives, objective);
366 // Pick a random pickup out of the available ones
367 const int i = RAND_INT(0, (int)o->u.Pickups.size - 1);
368 const PickupClass *p = *(const PickupClass **)CArrayGet(&o->u.Pickups, i);
369 MapPlacePickup(p, pos, ObjectiveToThing(objective));
370 }
371
372 struct vec2 MapGenerateFreePosition(Map *map, const struct vec2i size)
373 {
374 for (int i = 0; i < 100; i++)
375 {
376 const struct vec2 v = MapGetRandomPos(map);
377 if (!IsCollisionWithWall(v, size))
378 {
379 return v;
380 }
381 }
382 return svec2_zero();
383 }
384
385 void MapPlaceKey(
386 MapBuilder *mb, const struct vec2i tilePos, const int keyIndex)
387 {
388 GameEvent e = GameEventNew(GAME_EVENT_ADD_PICKUP);
389 strcpy(
390 e.u.AddPickup.PickupClass,
391 KeyPickupClass(mb->mission->KeyStyle, keyIndex)->Name);
392 e.u.AddPickup.Pos = Vec2ToNet(Vec2CenterOfTile(tilePos));
393 GameEventsEnqueue(&gGameEvents, e);
394 }
395
396 static int GetPlacementRetries(
397 const Map *map, const PlacementAccessFlags paFlags, bool *locked,
398 bool *unlocked)
399 {
400 // Try more times if we need to place in a locked room or unlocked place
401 *locked = paFlags == PLACEMENT_ACCESS_LOCKED && MapHasLockedRooms(map);
402 *unlocked = paFlags == PLACEMENT_ACCESS_NOT_LOCKED;
403 return (*locked || *unlocked) ? 1000 : 100;
404 }
405
406 bool MapPlaceRandomTile(
407 MapBuilder *mb, const PlacementAccessFlags paFlags,
408 bool (*tryPlaceFunc)(MapBuilder *, const struct vec2i, void *), void *data)
409 {
410 // Try a bunch of times to place something on a random tile
411 bool locked, unlocked;
412 const int retries =
413 GetPlacementRetries(mb->Map, paFlags, &locked, &unlocked);
414 for (int i = 0; i < retries; i++)
415 {
416 const struct vec2i tilePos = MapGetRandomTile(mb->Map);
417 const bool isInLocked = MapTileIsInLockedRoom(mb->Map, tilePos);
418 if ((!locked || isInLocked) && (!unlocked || !isInLocked))
419 {
420 if (tryPlaceFunc(mb, tilePos, data))
421 {
422 return true;
423 }
424 }
425 }
426 return false;
427 }
428 bool MapPlaceRandomPos(
429 const Map *map, const PlacementAccessFlags paFlags,
430 bool (*tryPlaceFunc)(const Map *, const struct vec2, void *), void *data)
431 {
432 // Try a bunch of times to place something at a random location
433 bool locked, unlocked;
434 const int retries = GetPlacementRetries(map, paFlags, &locked, &unlocked);
435 for (int i = 0; i < retries; i++)
436 {
437 const struct vec2 v = MapGetRandomPos(map);
438 const bool isInLocked = MapPosIsInLockedRoom(map, v);
439 if ((!locked || isInLocked) && (!unlocked || !isInLocked))
440 {
441 if (tryPlaceFunc(map, v, data))
442 {
443 return true;
444 }
445 }
446 }
447 return false;
448 }
449
450 // TODO: use enum instead of flag for map access
451 uint16_t AccessCodeToFlags(const uint16_t code)
452 {
453 if (code & MAP_ACCESS_RED)
454 return FLAGS_KEYCARD_RED;
455 if (code & MAP_ACCESS_BLUE)
456 return FLAGS_KEYCARD_BLUE;
457 if (code & MAP_ACCESS_GREEN)
458 return FLAGS_KEYCARD_GREEN;
459 if (code & MAP_ACCESS_YELLOW)
460 return FLAGS_KEYCARD_YELLOW;
461 return 0;
462 }
463
464 // Need to check the flags around the door tile because it's the
465 // triggers that contain the right flags
466 // TODO: refactor door
467 int MapGetDoorKeycardFlag(Map *map, struct vec2i pos)
468 {
469 int l = MapGetAccessLevel(map, pos);
470 if (l)
471 return l;
472 l = MapGetAccessLevel(map, svec2i(pos.x - 1, pos.y));
473 if (l)
474 return l;
475 l = MapGetAccessLevel(map, svec2i(pos.x + 1, pos.y));
476 if (l)
477 return l;
478 l = MapGetAccessLevel(map, svec2i(pos.x, pos.y - 1));
479 if (l)
480 return l;
481 return MapGetAccessLevel(map, svec2i(pos.x, pos.y + 1));
482 }
483
484 void MapTerminate(Map *map)
485 {
486 CA_FOREACH(Trigger *, t, map->triggers)
487 TriggerTerminate(*t);
488 CA_FOREACH_END()
489 CArrayTerminate(&map->triggers);
490 CArrayTerminate(&map->exits);
491 struct vec2i v;
492 for (v.y = 0; v.y < map->Size.y; v.y++)
493 {
494 for (v.x = 0; v.x < map->Size.x; v.x++)
495 {
496 Tile *t = MapGetTile(map, v);
497 TileDestroy(t);
498 }
499 }
500 CArrayTerminate(&map->Tiles);
501 TileClassesTerminate(map->TileClasses);
502 LOSTerminate(&map->LOS);
503 CArrayTerminate(&map->access);
504 PathCacheTerminate(&gPathCache);
505 }
506
507 void MapInit(Map *map, const struct vec2i size)
508 {
509 MapTerminate(map);
510
511 // Init map
512 memset(map, 0, sizeof *map);
513 map->TileClasses = TileClassesNew();
514 CArrayInit(&map->Tiles, sizeof(Tile));
515 map->Size = size;
516 LOSInit(map);
517 CArrayInitFillZero(&map->access, sizeof(uint16_t), size.x * size.y);
518 CArrayInit(&map->triggers, sizeof(Trigger *));
519 CArrayInit(&map->exits, sizeof(Exit));
520 PathCacheInit(&gPathCache, map);
521
522 struct vec2i v;
523 for (v.y = 0; v.y < map->Size.y; v.y++)
524 {
525 for (v.x = 0; v.x < map->Size.x; v.x++)
526 {
527 Tile t;
528 TileInit(&t);
529 CArrayPushBack(&map->Tiles, &t);
530 }
531 }
532 }
533
534 void MapPrintDebug(const Map *m)
535 {
536 if (LogModuleGetLevel(LM_MAP) > LL_TRACE)
537 {
538 return;
539 }
540 char *buf;
541 CCALLOC(buf, m->Size.x + 1);
542 char *bufP = buf;
543 struct vec2i v;
544 for (v.y = 0; v.y < m->Size.y; v.y++)
545 {
546 for (v.x = 0; v.x < m->Size.x; v.x++)
547 {
548 const TileClass *t = MapGetTile(m, v)->Class;
549 switch (t->Type)
550 {
551 case TILE_CLASS_FLOOR:
552 *bufP++ = t->IsRoom ? '-' : '.';
553 break;
554 case TILE_CLASS_WALL:
555 *bufP++ = '#';
556 break;
557 case TILE_CLASS_DOOR:
558 *bufP++ = '+';
559 break;
560 case TILE_CLASS_NOTHING:
561 *bufP++ = ' ';
562 break;
563 default:
564 *bufP++ = '?';
565 break;
566 }
567 }
568 LOG(LM_MAP, LL_TRACE, buf);
569 *buf = '\0';
570 bufP = buf;
571 }
572 LOG_FLUSH();
573 CFREE(buf);
574 }
575
576 bool MapIsPosOKForPlayer(
577 const Map *map, const struct vec2 pos, const bool allowAllTiles)
578 {
579 if (!MapIsTileAreaClear(map, pos, svec2i(ACTOR_W, ACTOR_H)))
580 {
581 return false;
582 }
583 // Don't put players in locked rooms
584 if (MapPosIsInLockedRoom(map, pos))
585 {
586 return false;
587 }
588 const struct vec2i tilePos = Vec2ToTile(pos);
589 const Tile *tile = MapGetTile(map, tilePos);
590 if (tile->Class->Type == TILE_CLASS_FLOOR)
591 {
592 return true;
593 }
594 else if (allowAllTiles)
595 {
596 return TileCanWalk(tile);
597 }
598 return false;
599 }
600
601 // Check if the target position is completely clear
602 // This includes collisions that make the target illegal, such as walls
603 // But it also includes item collisions, whether or not the collisions
604 // are legal, e.g. item pickups, friendly collisions
605 bool MapIsTileAreaClear(
606 const Map *map, const struct vec2 pos, const struct vec2i size)
607 {
608 // Wall collision
609 if (IsCollisionWithWall(pos, size))
610 {
611 return false;
612 }
613
614 // Item collision
615 const struct vec2i tv = Vec2ToTile(pos);
616 struct vec2i dv;
617 // Check collisions with all other items on this tile, in all 8 directions
618 for (dv.y = -1; dv.y <= 1; dv.y++)
619 {
620 for (dv.x = -1; dv.x <= 1; dv.x++)
621 {
622 const struct vec2i dtv = svec2i_add(tv, dv);
623 if (!MapIsTileIn(map, dtv))
624 {
625 continue;
626 }
627 const CArray *tileThings = &MapGetTile(map, dtv)->things;
628 if (tileThings == NULL)
629 {
630 continue;
631 }
632 for (int i = 0; i < (int)tileThings->size; i++)
633 {
634 const Thing *ti = ThingIdGetThing(CArrayGet(tileThings, i));
635 if (AABBOverlap(pos, ti->Pos, size, ti->size))
636 {
637 if (ti->kind == KIND_OBJECT)
638 {
639 const TObject *tobj = CArrayGet(&gObjs, ti->id);
640 if (tobj->Health <= 0)
641 {
642 continue;
643 }
644 }
645 return false;
646 }
647 }
648 }
649 }
650
651 return true;
652 }
653
654 void MapMarkAsVisited(Map *map, struct vec2i pos)
655 {
656 Tile *t = MapGetTile(map, pos);
657 if (!t->isVisited && TileCanWalk(t))
658 {
659 map->tilesSeen++;
660 }
661 t->isVisited = true;
662 }
663
664 void MapMarkAllAsVisited(Map *map)
665 {
666 struct vec2i pos;
667 for (pos.y = 0; pos.y < map->Size.y; pos.y++)
668 {
669 for (pos.x = 0; pos.x < map->Size.x; pos.x++)
670 {
671 MapMarkAsVisited(map, pos);
672 }
673 }
674 }
675
676 int MapGetExploredPercentage(Map *map)
677 {
678 return (100 * map->tilesSeen) / map->NumExplorableTiles;
679 }
680
681 void MapUpdate(Map *map)
682 {
683 CA_FOREACH(Tile, t, map->Tiles)
684 TileUpdate(t);
685 CA_FOREACH_END()
686 }
687
688 struct vec2i MapSearchTileAround(
689 Map *map, struct vec2i start, TileSelectFunc func)
690 {
691 if (func(map, start))
692 {
693 return start;
694 }
695 // Search using an expanding box pattern around the goal
696 for (int radius = 1; radius < MAX(map->Size.x, map->Size.y); radius++)
697 {
698 struct vec2i tile;
699 for (tile.x = start.x - radius; tile.x <= start.x + radius; tile.x++)
700 {
701 if (tile.x < 0)
702 continue;
703 if (tile.x >= map->Size.x)
704 break;
705 for (tile.y = start.y - radius; tile.y <= start.y + radius;
706 tile.y++)
707 {
708 if (tile.y < 0)
709 continue;
710 if (tile.y >= map->Size.y)
711 break;
712 // Check box; don't check inside
713 if (tile.x != start.x - radius && tile.x != start.x + radius &&
714 tile.y != start.y - radius && tile.y != start.y + radius)
715 {
716 continue;
717 }
718 if (func(map, tile))
719 {
720 return tile;
721 }
722 }
723 }
724 }
725 // Should never reach this point; something is very wrong
726 CASSERT(false, "failed to find tile around tile");
727 return svec2i_zero();
728 }
729 bool MapTileIsUnexplored(Map *map, struct vec2i tile)
730 {
731 const Tile *t = MapGetTile(map, tile);
732 return !t->isVisited && TileCanWalk(t);
733 }
734
735 // Only creates the trigger, but does not place it
736 Trigger *MapNewTrigger(Map *map)
737 {
738 Trigger *t = TriggerNew();
739 CArrayPushBack(&map->triggers, &t);
740 t->id = map->triggerId++;
741 return t;
742 }