Debian Codegraph @

src/cdogs/map_build.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-2014, 2017-2024 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_build.h"
50
51 #include "actors.h"
52 #include "collision/collision.h"
53 #include "door.h"
54 #include "log.h"
55 #include "map_cave.h"
56 #include "map_classic.h"
57 #include "map_interior.h"
58 #include "map_static.h"
59 #include "net_util.h"
60 #include "objs.h"
61
62 #define COLLECTABLE_W 4
63 #define COLLECTABLE_H 3
64 #define EXIT_WIDTH 8
65 #define EXIT_HEIGHT 8
66
67 static void MapSetupTilesAndWalls(MapBuilder *mb);
68 static void MapSetupDoors(MapBuilder *mb);
69 static void MapAddDrains(MapBuilder *mb);
70 static void MapGenerateRandomExitArea(Map *map, const int mission);
71 void MapBuild(
72 Map *m, const Mission *mission, const bool loadDynamic,
73 const int missionIndex, const GameMode mode,
74 const CharacterStore *characters)
75 {
76 MapBuilder mb;
77 MapBuilderInit(&mb, m, mission, mode, characters);
78 MapInit(mb.Map, mb.mission->Size);
79
80 switch (mb.mission->Type)
81 {
82 case MAPTYPE_CLASSIC:
83 MapClassicLoad(&mb);
84 break;
85 case MAPTYPE_STATIC:
86 MapStaticLoad(&mb);
87 break;
88 case MAPTYPE_CAVE:
89 MapCaveLoad(&mb);
90 break;
91 case MAPTYPE_INTERIOR:
92 MapInteriorLoad(&mb, missionIndex);
93 break;
94 default:
95 CASSERT(false, "unknown map type");
96 break;
97 }
98 CArrayCopy(&mb.Map->access, &mb.access);
99
100 MapSetupTilesAndWalls(&mb);
101 MapSetupDoors(&mb);
102 MapPrintDebug(mb.Map);
103
104 // Set exit now since we have set up all the tiles
105 switch (mb.mission->Type)
106 {
107 case MAPTYPE_CLASSIC:
108 MapAddDrains(&mb);
109 if (HasExit(gCampaign.Entry.Mode) && mb.mission->u.Classic.ExitEnabled)
110 {
111 MapGenerateRandomExitArea(mb.Map, missionIndex);
112 }
113 break;
114 case MAPTYPE_STATIC:
115 break;
116 case MAPTYPE_CAVE:
117 if (HasExit(gCampaign.Entry.Mode) && mb.mission->u.Cave.ExitEnabled)
118 {
119 MapGenerateRandomExitArea(mb.Map, missionIndex);
120 }
121 break;
122 case MAPTYPE_INTERIOR:
123 MapAddDrains(&mb);
124 break;
125 default:
126 CASSERT(false, "unknown map type");
127 break;
128 }
129
130 // Count total number of reachable tiles, for explored %
131 mb.Map->NumExplorableTiles = 0;
132 struct vec2i v;
133 for (v.y = 0; v.y < mb.Map->Size.y; v.y++)
134 {
135 for (v.x = 0; v.x < mb.Map->Size.x; v.x++)
136 {
137 if (TileCanWalk(MapGetTile(mb.Map, v)))
138 {
139 mb.Map->NumExplorableTiles++;
140 }
141 }
142 }
143
144 if (loadDynamic)
145 {
146 MapLoadDynamic(&mb);
147 ActorsPilotVehicles();
148 }
149 MapBuilderTerminate(&mb);
150 }
151 void SetupWallTileClasses(Map *m, PicManager *pm, const TileClass *base)
152 {
153 for (int i = 0; i < WALL_TYPE_COUNT; i++)
154 {
155 PicManagerGenerateMaskedStylePic(
156 pm, "wall", base->Style, IntWallType(i), base->Mask, base->MaskAlt,
157 false);
158 TileClassesAdd(
159 m->TileClasses, pm, base, base->Style, IntWallType(i), base->Mask,
160 base->MaskAlt);
161 }
162 }
163 void SetupFloorTileClasses(Map *m, PicManager *pm, const TileClass *base)
164 {
165 for (int i = 0; i < FLOOR_TYPES; i++)
166 {
167 PicManagerGenerateMaskedStylePic(
168 pm, "tile", base->Style, IntTileType(i), base->Mask, base->MaskAlt,
169 false);
170 TileClassesAdd(
171 m->TileClasses, pm, base, base->Style, IntTileType(i), base->Mask,
172 base->MaskAlt);
173 }
174 }
175 void SetupDoorTileClasses(Map *m, PicManager *pm, const TileClass *base)
176 {
177 const char *doorStyles[] = {"yellow", "green", "blue", "red",
178 "open", "wall", "normal"};
179 for (int i = 0; i < (int)(sizeof doorStyles / sizeof doorStyles[0]); i++)
180 {
181 for (DoorType type = DOORTYPE_H; type < DOORTYPE_COUNT; type++)
182 {
183 DoorAddClass(m->TileClasses, pm, base, doorStyles[i], type);
184 }
185 }
186 }
187 static bool MapBuilderIsDoor(const MapBuilder *mb, const struct vec2i v);
188 static int MapGetAccessFlags(const MapBuilder *mb, const struct vec2i v);
189 static void MapSetupDoors(MapBuilder *mb)
190 {
191 // Mark doors as set up as we go
192 CArray doorsSetup;
193 CArrayInitFillZero(
194 &doorsSetup, sizeof(bool), mb->Map->Size.x * mb->Map->Size.y);
195
196 RECT_FOREACH(Rect2iNew(svec2i_zero(), mb->Map->Size))
197 // Check if this door tile hasn't been set up yet
198 const int idx = _v.x + _v.y * mb->Map->Size.x;
199 if (MapBuilderIsDoor(mb, _v) && !*(bool *)CArrayGet(&doorsSetup, idx))
200 {
201 const struct vec2i groupSize =
202 MapAddDoorGroup(mb, _v, MapGetAccessFlags(mb, _v));
203 for (int dx = 0; dx < groupSize.x; dx++)
204 {
205 for (int dy = 0; dy < groupSize.y; dy++)
206 {
207 const int idx2 = _v.x + dx + (_v.y + dy) * mb->Map->Size.x;
208 CArraySet(&doorsSetup, idx2, &gTrue);
209 }
210 }
211 }
212 RECT_FOREACH_END()
213
214 CArrayTerminate(&doorsSetup);
215 }
216 static bool MapBuilderIsDoor(const MapBuilder *mb, const struct vec2i v)
217 {
218 const TileClass *t = MapBuilderGetTile(mb, v);
219 return t != NULL && t->Type == TILE_CLASS_DOOR;
220 }
221 static int MapGetAccessFlags(const MapBuilder *mb, const struct vec2i v)
222 {
223 int flags = 0;
224 flags = MAX(flags, AccessCodeToFlags(MapBuildGetAccess(mb, v)));
225 flags = MAX(
226 flags, AccessCodeToFlags(MapBuildGetAccess(mb, svec2i(v.x - 1, v.y))));
227 flags = MAX(
228 flags, AccessCodeToFlags(MapBuildGetAccess(mb, svec2i(v.x + 1, v.y))));
229 flags = MAX(
230 flags, AccessCodeToFlags(MapBuildGetAccess(mb, svec2i(v.x, v.y - 1))));
231 flags = MAX(
232 flags, AccessCodeToFlags(MapBuildGetAccess(mb, svec2i(v.x, v.y + 1))));
233 return flags;
234 }
235
236 void MapBuilderInit(
237 MapBuilder *mb, Map *m, const Mission *mission, const GameMode mode,
238 const CharacterStore *characters)
239 {
240 memset(mb, 0, sizeof *mb);
241 mb->Map = m;
242 mb->mission = mission;
243 mb->mode = mode;
244 mb->characters = characters;
245
246 const int mapSize = mission->Size.x * mission->Size.y;
247 CArrayInitFillZero(&mb->access, sizeof(uint16_t), mapSize);
248 CArrayInitFill(&mb->tiles, sizeof(TileClass), mapSize, &gTileNothing);
249 CArrayInitFillZero(&mb->leaveFree, sizeof(bool), mapSize);
250 }
251 void MapBuilderTerminate(MapBuilder *mb)
252 {
253 CArrayTerminate(&mb->access);
254 CArrayTerminate(&mb->tiles);
255 CArrayTerminate(&mb->leaveFree);
256 }
257
258 uint16_t MapBuildGetAccess(const MapBuilder *mb, const struct vec2i pos)
259 {
260 if (!MapIsTileIn(mb->Map, pos))
261 {
262 return 0;
263 }
264 return *(uint16_t *)CArrayGet(
265 &mb->access, pos.y * mb->Map->Size.x + pos.x);
266 }
267 void MapBuildSetAccess(MapBuilder *mb, struct vec2i pos, const uint16_t v)
268 {
269 *(uint16_t *)CArrayGet(&mb->access, pos.y * mb->Map->Size.x + pos.x) = v;
270 }
271 const TileClass *MapBuilderGetTile(
272 const MapBuilder *mb, const struct vec2i pos)
273 {
274 if (!MapIsTileIn(mb->Map, pos))
275 {
276 return NULL;
277 }
278 return CArrayGet(&mb->tiles, pos.y * mb->Map->Size.x + pos.x);
279 }
280 void MapBuilderSetTile(MapBuilder *mb, struct vec2i pos, const TileClass *t)
281 {
282 if (t != NULL)
283 {
284 *(TileClass *)CArrayGet(&mb->tiles, pos.y * mb->Map->Size.x + pos.x) =
285 *t;
286 }
287 }
288
289 static bool IsTileOKStrict(
290 const MapObject *obj, const Tile *tile, const Tile *tileAbove,
291 const Tile *tileBelow, const bool isLeaveFree, const int numWallsAdjacent,
292 const int numWallsAround);
293 static int MapGetNumWallsAdjacentTile(const Map *map, const struct vec2i v);
294 static int MapGetNumWallsAroundTile(const Map *map, const struct vec2i v);
295 bool MapTryPlaceOneObject(
296 MapBuilder *mb, const struct vec2i v, const MapObject *mo,
297 const int extraFlags, const bool isStrictMode)
298 {
299 // Don't place ammo spawners if ammo is disabled
300 if (!gCampaign.Setting.Ammo &&
301 mo->Type == MAP_OBJECT_TYPE_PICKUP_SPAWNER &&
302 PickupClassHasAmmoEffect(mo->u.PickupClass))
303 {
304 return false;
305 }
306 const Tile *t = MapGetTile(mb->Map, v);
307 const Tile *tAbove = MapGetTile(mb->Map, svec2i(v.x, v.y - 1));
308 const Tile *tBelow = MapGetTile(mb->Map, svec2i(v.x, v.y + 1));
309 if (isStrictMode &&
310 !IsTileOKStrict(
311 mo, t, tAbove, tBelow, MapBuilderIsLeaveFree(mb, v),
312 MapGetNumWallsAdjacentTile(mb->Map, v),
313 MapGetNumWallsAroundTile(mb->Map, v)))
314 {
315 return false;
316 }
317 else if (!MapObjectIsTileOK(mo, t, tAbove))
318 {
319 return false;
320 }
321
322 if (mo->Flags & (1 << PLACEMENT_FREE_IN_FRONT))
323 {
324 MapBuilderSetLeaveFree(mb, svec2i(v.x, v.y + 1), true);
325 }
326
327 NMapObjectAdd amo = NMapObjectAdd_init_default;
328 amo.UID = ObjsGetNextUID();
329 strcpy(amo.MapObjectClass, mo->Name);
330 amo.has_Pos = true;
331 amo.Pos = Vec2ToNet(MapObjectGetPlacementPos(mo, v));
332 amo.ThingFlags = MapObjectGetFlags(mo) | extraFlags;
333 amo.Health = mo->Health;
334 ObjAdd(amo);
335 return true;
336 }
337 static bool IsTileOKStrict(
338 const MapObject *obj, const Tile *tile, const Tile *tileAbove,
339 const Tile *tileBelow, const bool isLeaveFree, const int numWallsAdjacent,
340 const int numWallsAround)
341 {
342 if (!MapObjectIsTileOK(obj, tile, tileAbove))
343 {
344 return false;
345 }
346 if (isLeaveFree)
347 {
348 return false;
349 }
350
351 if (obj->Flags & (1 << PLACEMENT_OUTSIDE) && tile->Class->IsRoom)
352 {
353 return false;
354 }
355 if ((obj->Flags & (1 << PLACEMENT_INSIDE)) && !tile->Class->IsRoom)
356 {
357 return false;
358 }
359 if ((obj->Flags & (1 << PLACEMENT_NO_WALLS)) && numWallsAround != 0)
360 {
361 return false;
362 }
363 if ((obj->Flags & (1 << PLACEMENT_ONE_WALL)) && numWallsAdjacent != 1)
364 {
365 return false;
366 }
367 if ((obj->Flags & (1 << PLACEMENT_ONE_OR_MORE_WALLS)) &&
368 numWallsAdjacent < 1)
369 {
370 return false;
371 }
372 if ((obj->Flags & (1 << PLACEMENT_FREE_IN_FRONT)) &&
373 !TileIsClear(tileBelow))
374 {
375 return false;
376 }
377
378 return true;
379 }
380 // Adjacent means to the left, right, above or below
381 static int MapGetNumWallsAdjacentTile(const Map *map, const struct vec2i v)
382 {
383 int count = 0;
384 if (v.x > 0 && v.y > 0 && v.x < map->Size.x - 1 && v.y < map->Size.y - 1)
385 {
386 if (!TileCanWalk(MapGetTile(map, svec2i(v.x - 1, v.y))))
387 {
388 count++;
389 }
390 if (!TileCanWalk(MapGetTile(map, svec2i(v.x + 1, v.y))))
391 {
392 count++;
393 }
394 if (!TileCanWalk(MapGetTile(map, svec2i(v.x, v.y - 1))))
395 {
396 count++;
397 }
398 if (!TileCanWalk(MapGetTile(map, svec2i(v.x, v.y + 1))))
399 {
400 count++;
401 }
402 }
403 return count;
404 }
405 // Around means the 8 tiles surrounding the tile
406 static int MapGetNumWallsAroundTile(const Map *map, const struct vec2i v)
407 {
408 int count = MapGetNumWallsAdjacentTile(map, v);
409 if (v.x > 0 && v.y > 0 && v.x < map->Size.x - 1 && v.y < map->Size.y - 1)
410 {
411 // Having checked the adjacencies, check the diagonals
412 if (!TileCanWalk(MapGetTile(map, svec2i(v.x - 1, v.y - 1))))
413 {
414 count++;
415 }
416 if (!TileCanWalk(MapGetTile(map, svec2i(v.x + 1, v.y + 1))))
417 {
418 count++;
419 }
420 if (!TileCanWalk(MapGetTile(map, svec2i(v.x + 1, v.y - 1))))
421 {
422 count++;
423 }
424 if (!TileCanWalk(MapGetTile(map, svec2i(v.x - 1, v.y + 1))))
425 {
426 count++;
427 }
428 }
429 return count;
430 }
431
432 static void AddObjectives(MapBuilder *mb);
433 static void AddKeys(MapBuilder *mb);
434 void MapLoadDynamic(MapBuilder *mb)
435 {
436 if (mb->mission->Type == MAPTYPE_STATIC)
437 {
438 MapStaticLoadDynamic(mb);
439 }
440
441 CA_FOREACH(const MapObjectDensity, mod, mb->mission->MapObjectDensities)
442 for (int j = 0;
443 j < (mod->Density * mb->Map->Size.x * mb->Map->Size.y) / 1000; j++)
444 {
445 MapTryPlaceOneObject(mb, MapGetRandomTile(mb->Map), mod->M, 0, true);
446 }
447 CA_FOREACH_END()
448
449 CA_FOREACH(const PickupCount, pc, mb->mission->PickupCounts)
450 for (int j = 0; j < pc->Count; j++)
451 {
452 for (int i = 0; i < 1000; i++)
453 {
454 const struct vec2 v = MapGetRandomPos(mb->Map);
455 const struct vec2i size = svec2i(COLLECTABLE_W, COLLECTABLE_H);
456 if (!IsCollisionWithWall(v, size))
457 {
458 MapPlacePickup(pc->P, v, 0);
459 break;
460 }
461 }
462 }
463 CA_FOREACH_END()
464
465 if (HasObjectives(gCampaign.Entry.Mode))
466 {
467 AddObjectives(mb);
468 }
469
470 if (AreKeysAllowed(gCampaign.Entry.Mode))
471 {
472 AddKeys(mb);
473 }
474 }
475 static bool MapTryPlaceBlowup(
476 MapBuilder *mb, const int objective, const bool strict);
477 static int MapTryPlaceCollectible(MapBuilder *mb, const int objective);
478 static void AddObjectives(MapBuilder *mb)
479 {
480 // Try to add the objectives
481 // If we are unable to place them all, make sure to reduce the totals
482 // in case we create missions that are impossible to complete
483 CA_FOREACH(Objective, o, mb->mission->Objectives)
484 if (o->Type != OBJECTIVE_COLLECT && o->Type != OBJECTIVE_DESTROY)
485 {
486 continue;
487 }
488 if (o->Type == OBJECTIVE_COLLECT)
489 {
490 for (int i = o->placed; i < o->Count; i++)
491 {
492 if (MapTryPlaceCollectible(mb, _ca_index))
493 {
494 o->placed++;
495 }
496 }
497 }
498 else if (o->Type == OBJECTIVE_DESTROY)
499 {
500 // Try using strict rule when placing objective, at least for a bit
501 int strictCounter = 1000;
502 for (int i = o->placed; i < o->Count; i++)
503 {
504 if (MapTryPlaceBlowup(mb, _ca_index, strictCounter > 0))
505 {
506 o->placed++;
507 }
508 if (strictCounter > 0)
509 {
510 strictCounter--;
511 }
512 }
513 }
514 o->Count = o->placed;
515 if (o->Count < o->Required)
516 {
517 o->Required = o->Count;
518 }
519 CA_FOREACH_END()
520 }
521 static int MapTryPlaceCollectible(MapBuilder *mb, const int objective)
522 {
523 const Objective *o = CArrayGet(&mb->mission->Objectives, objective);
524 const bool hasLockedRooms =
525 (o->Flags & OBJECTIVE_HIACCESS) && MapHasLockedRooms(mb->Map);
526 const bool noaccess = o->Flags & OBJECTIVE_NOACCESS;
527 int i = (noaccess || hasLockedRooms) ? 1000 : 100;
528
529 while (i)
530 {
531 const struct vec2 v = MapGetRandomPos(mb->Map);
532 const struct vec2i size = svec2i(COLLECTABLE_W, COLLECTABLE_H);
533 if (!IsCollisionWithWall(v, size))
534 {
535 if ((!hasLockedRooms || MapPosIsInLockedRoom(mb->Map, v)) &&
536 (!noaccess || !MapPosIsInLockedRoom(mb->Map, v)))
537 {
538 MapPlaceCollectible(mb->mission, objective, v);
539 return 1;
540 }
541 }
542 i--;
543 }
544 return 0;
545 }
546 static void MapPlaceCard(
547 MapBuilder *mb, const int keyIndex, const int mapAccess);
548 static void AddKeys(MapBuilder *mb)
549 {
550 if (mb->Map->keyAccessCount >= 5)
551 {
552 MapPlaceCard(mb, 3, MAP_ACCESS_BLUE);
553 }
554 if (mb->Map->keyAccessCount >= 4)
555 {
556 MapPlaceCard(mb, 2, MAP_ACCESS_GREEN);
557 }
558 if (mb->Map->keyAccessCount >= 3)
559 {
560 MapPlaceCard(mb, 1, MAP_ACCESS_YELLOW);
561 }
562 if (mb->Map->keyAccessCount >= 2)
563 {
564 MapPlaceCard(mb, 0, 0);
565 }
566 }
567 static void MapPlaceCard(
568 MapBuilder *mb, const int keyIndex, const int mapAccess)
569 {
570 for (;;)
571 {
572 const struct vec2i v = MapGetRandomTile(mb->Map);
573 const Tile *t = MapGetTile(mb->Map, v);
574 if (t->Class->IsRoom && TileIsClear(t) && TileCanWalk(t) &&
575 MapBuildGetAccess(mb, v) == mapAccess &&
576 // Ensure keys are visible, not hidden behind walls
577 TileIsClear(MapGetTile(mb->Map, svec2i(v.x, v.y + 1))))
578 {
579 MapPlaceKey(mb, v, keyIndex);
580 return;
581 }
582 }
583 }
584 typedef struct
585 {
586 const Objective *o;
587 int objective;
588 bool strict;
589 } TryPlaceOneBlowupData;
590 static bool TryPlaceOneBlowup(
591 MapBuilder *mb, const struct vec2i tilePos, void *data);
592 static bool MapTryPlaceBlowup(
593 MapBuilder *mb, const int objective, const bool strict)
594 {
595 TryPlaceOneBlowupData data;
596 data.o = CArrayGet(&mb->mission->Objectives, objective);
597 const PlacementAccessFlags paFlags =
598 ObjectiveGetPlacementAccessFlags(data.o);
599 data.objective = objective;
600 data.strict = strict;
601 return MapPlaceRandomTile(mb, paFlags, TryPlaceOneBlowup, &data);
602 }
603 static bool TryPlaceOneBlowup(
604 MapBuilder *mb, const struct vec2i tilePos, void *data)
605 {
606 const TryPlaceOneBlowupData *pData = data;
607 return MapTryPlaceDestroyObject(
608 mb, mb->mission, pData->objective, tilePos, pData->strict);
609 }
610
611 void MapBuilderSetLeaveFree(
612 MapBuilder *mb, const struct vec2i tile, const bool value)
613 {
614 if (!MapIsTileIn(mb->Map, tile))
615 return;
616 CArraySet(&mb->leaveFree, tile.y * mb->Map->Size.x + tile.x, &value);
617 }
618 bool MapBuilderIsLeaveFree(const MapBuilder *mb, const struct vec2i tile)
619 {
620 if (!MapIsTileIn(mb->Map, tile))
621 return false;
622 return *(bool *)CArrayGet(
623 &mb->leaveFree, tile.y * mb->Map->Size.x + tile.x);
624 }
625
626 static void MapSetupTile(MapBuilder *mb, const struct vec2i pos);
627
628 void MapBuildTile(
629 MapBuilder *mb, const struct vec2i pos, const TileClass *tile)
630 {
631 // Load tiles from +2 perimeter
632 RECT_FOREACH(Rect2iNew(svec2i_subtract(pos, svec2i(2, 2)), svec2i(5, 5)))
633 MapStaticLoadTile(mb, _v);
634 RECT_FOREACH_END()
635 // Update the tile as well, plus neighbours as they may be affected
636 // by shadows etc. especially walls
637 MapBuilderSetTile(mb, pos, tile);
638 MapSetupTile(mb, pos);
639 RECT_FOREACH(Rect2iNew(svec2i_subtract(pos, svec2i(1, 1)), svec2i(3, 3)))
640 MapSetupTile(mb, _v);
641 RECT_FOREACH_END()
642 CArrayCopy(&mb->Map->access, &mb->access);
643 }
644
645 static bool MapTileIsNormalFloor(const MapBuilder *mb, const struct vec2i pos)
646 {
647 // Normal floor tiles can be replaced randomly with
648 // special floor tiles such as drainage
649 const TileClass *tile = MapBuilderGetTile(mb, pos);
650 if (tile->Type != TILE_CLASS_FLOOR || tile->IsRoom)
651 {
652 return false;
653 }
654 const TileClass *tAbove = MapBuilderGetTile(mb, svec2i(pos.x, pos.y - 1));
655 if (!tAbove || tAbove->Type != TILE_CLASS_FLOOR || tAbove->IsRoom)
656 {
657 return false;
658 }
659 return true;
660 }
661
662 static void MapSetupTilesAndWalls(MapBuilder *mb)
663 {
664 RECT_FOREACH(Rect2iNew(svec2i_zero(), mb->Map->Size))
665 MapSetupTile(mb, _v);
666 RECT_FOREACH_END()
667
668 if (mb->mission->Type != MAPTYPE_STATIC ||
669 mb->mission->u.Static.AltFloorsEnabled)
670 {
671 // Randomly change normal floor tiles to alternative floor tiles
672 for (int i = 0; i < mb->Map->Size.x * mb->Map->Size.y / 22; i++)
673 {
674 const struct vec2i pos = MapGetRandomTile(mb->Map);
675 if (MapTileIsNormalFloor(mb, pos))
676 {
677 Tile *t = MapGetTile(mb->Map, pos);
678 t->Class = TileClassesGetMaskedTile(
679 mb->Map->TileClasses, t->Class, t->Class->Style, "alt1",
680 t->Class->Mask, t->Class->MaskAlt);
681 }
682 }
683 for (int i = 0; i < mb->Map->Size.x * mb->Map->Size.y / 16; i++)
684 {
685 const struct vec2i pos = MapGetRandomTile(mb->Map);
686 if (MapTileIsNormalFloor(mb, pos))
687 {
688 Tile *t = MapGetTile(mb->Map, pos);
689 t->Class = TileClassesGetMaskedTile(
690 mb->Map->TileClasses, t->Class, t->Class->Style, "alt2",
691 t->Class->Mask, t->Class->MaskAlt);
692 }
693 }
694 }
695 }
696 static const char *MapGetWallPic(const MapBuilder *m, const struct vec2i pos);
697 // Set tile properties for a map tile
698 static void MapSetupTile(MapBuilder *mb, const struct vec2i pos)
699 {
700 if (!MapIsTileIn(mb->Map, pos))
701 return;
702 const Tile *tAbove = MapGetTile(mb->Map, svec2i(pos.x, pos.y - 1));
703 const bool canSeeTileAbove = !(tAbove != NULL && TileIsOpaque(tAbove));
704 Tile *t = MapGetTile(mb->Map, pos);
705 if (!t)
706 {
707 return;
708 }
709 const TileClass *tc = MapBuilderGetTile(mb, pos);
710 if (tc->Type == TILE_CLASS_FLOOR)
711 {
712 t->Class = TileClassesGetMaskedTile(
713 mb->Map->TileClasses, tc, tc->Style,
714 canSeeTileAbove ? "normal" : "shadow", tc->Mask, tc->MaskAlt);
715 }
716 else if (tc->Type == TILE_CLASS_WALL)
717 {
718 t->Class = TileClassesGetMaskedTile(
719 mb->Map->TileClasses, tc, tc->Style, MapGetWallPic(mb, pos),
720 tc->Mask, tc->MaskAlt);
721 }
722 else if (tc->Type == TILE_CLASS_DOOR)
723 {
724 t->Class = TileClassesGetMaskedTile(
725 mb->Map->TileClasses, tc, tc->Style, "normal_h", tc->Mask,
726 tc->MaskAlt);
727 }
728 else if (tc->Type == TILE_CLASS_NOTHING)
729 {
730 t->Class = &gTileNothing;
731 }
732 else
733 {
734 CASSERT(false, "cannot setup tile");
735 t->Class = &gTileNothing;
736 }
737 }
738 static bool W(const MapBuilder *mb, const int x, const int y);
739 static const char *MapGetWallPic(const MapBuilder *m, const struct vec2i pos)
740 {
741 const int x = pos.x;
742 const int y = pos.y;
743 if (W(m, x - 1, y) && W(m, x + 1, y) && W(m, x, y + 1) && W(m, x, y - 1))
744 {
745 return "x";
746 }
747 if (W(m, x - 1, y) && W(m, x + 1, y) && W(m, x, y + 1))
748 {
749 return "nt";
750 }
751 if (W(m, x - 1, y) && W(m, x + 1, y) && W(m, x, y - 1))
752 {
753 return "st";
754 }
755 if (W(m, x - 1, y) && W(m, x, y + 1) && W(m, x, y - 1))
756 {
757 return "et";
758 }
759 if (W(m, x + 1, y) && W(m, x, y + 1) && W(m, x, y - 1))
760 {
761 return "wt";
762 }
763 if (W(m, x + 1, y) && W(m, x, y + 1))
764 {
765 return "nw";
766 }
767 if (W(m, x + 1, y) && W(m, x, y - 1))
768 {
769 return "sw";
770 }
771 if (W(m, x - 1, y) && W(m, x, y + 1))
772 {
773 return "ne";
774 }
775 if (W(m, x - 1, y) && W(m, x, y - 1))
776 {
777 return "se";
778 }
779 if (W(m, x - 1, y) && W(m, x + 1, y))
780 {
781 return "h";
782 }
783 if (W(m, x, y + 1) && W(m, x, y - 1))
784 {
785 return "v";
786 }
787 if (W(m, x, y + 1))
788 {
789 return "n";
790 }
791 if (W(m, x, y - 1))
792 {
793 return "s";
794 }
795 if (W(m, x + 1, y))
796 {
797 return "w";
798 }
799 if (W(m, x - 1, y))
800 {
801 return "e";
802 }
803 return "o";
804 }
805 static bool W(const MapBuilder *mb, const int x, const int y)
806 {
807 const struct vec2i v = svec2i(x, y);
808 if (!MapIsTileIn(mb->Map, v))
809 return false;
810 const TileClass *tc = MapBuilderGetTile(mb, v);
811 return tc->Type == TILE_CLASS_WALL;
812 }
813
814 static bool MapIsValidStartForWall(
815 const MapBuilder *mb, const struct vec2i pos, const bool isRoom,
816 const int pad)
817 {
818 if (!MapIsTileIn(mb->Map, pos) || pos.x == 0 || pos.y == 0 ||
819 pos.x == mb->Map->Size.x - 1 || pos.y == mb->Map->Size.y - 1)
820 {
821 return false;
822 }
823 struct vec2i d;
824 for (d.x = pos.x - pad; d.x <= pos.x + pad; d.x++)
825 {
826 for (d.y = pos.y - pad; d.y <= pos.y + pad; d.y++)
827 {
828 const TileClass *t = MapBuilderGetTile(mb, d);
829 if (t == NULL || t->Type != TILE_CLASS_FLOOR ||
830 t->IsRoom != isRoom)
831 {
832 return false;
833 }
834 }
835 }
836 return true;
837 }
838
839 struct vec2i MapGetRoomSize(const RoomParams r, const int doorMin)
840 {
841 // Work out dimensions of room
842 // make sure room is large enough to accommodate doors
843 const int roomMin = MAX(r.Min, doorMin + 2);
844 const int roomMax = MAX(r.Max, doorMin + 2);
845 return svec2i(RAND_INT(roomMin, roomMax), RAND_INT(roomMin, roomMax));
846 }
847
848 static bool MapBuilderGetIsRoom(const MapBuilder *mb, const struct vec2i pos);
849 void MapMakeRoom(
850 MapBuilder *mb, const struct vec2i pos, const struct vec2i size,
851 const bool walls, const TileClass *wall, const TileClass *room,
852 const bool removeInterRoomWalls)
853 {
854 struct vec2i v;
855 // Set the perimeter walls and interior
856 // If the tile is a room interior already, do not turn it into a wall
857 // This is due to overlapping rooms
858 for (v.y = pos.y; v.y < pos.y + size.y; v.y++)
859 {
860 for (v.x = pos.x; v.x < pos.x + size.x; v.x++)
861 {
862 if (v.y == pos.y || v.y == pos.y + size.y - 1 || v.x == pos.x ||
863 v.x == pos.x + size.x - 1)
864 {
865 const TileClass *t = MapBuilderGetTile(mb, v);
866 if (walls && t != NULL && !t->IsRoom)
867 {
868 MapBuilderSetTile(mb, v, wall);
869 }
870 }
871 else
872 {
873 MapBuilderSetTile(mb, v, room);
874 }
875 }
876 }
877 if (removeInterRoomWalls)
878 {
879
880 // Check perimeter again; if there are walls where both sides contain
881 // rooms, remove the wall as the rooms have merged
882 for (v.y = pos.y; v.y < pos.y + size.y; v.y++)
883 {
884 for (v.x = pos.x; v.x < pos.x + size.x; v.x++)
885 {
886 if (v.y == pos.y || v.y == pos.y + size.y - 1 ||
887 v.x == pos.x || v.x == pos.x + size.x - 1)
888 {
889 if ((MapBuilderGetIsRoom(mb, svec2i(v.x + 1, v.y)) &&
890 MapBuilderGetIsRoom(mb, svec2i(v.x - 1, v.y))) ||
891 (MapBuilderGetIsRoom(mb, svec2i(v.x, v.y + 1)) &&
892 MapBuilderGetIsRoom(mb, svec2i(v.x, v.y - 1))))
893 {
894 MapBuilderSetTile(mb, v, room);
895 }
896 }
897 }
898 }
899 }
900 }
901 static bool MapBuilderGetIsRoom(const MapBuilder *mb, const struct vec2i pos)
902 {
903 const TileClass *t = MapBuilderGetTile(mb, pos);
904 return t != NULL && t->Type == TILE_CLASS_FLOOR && t->IsRoom;
905 }
906
907 void MapMakeRoomWalls(
908 MapBuilder *mb, const RoomParams r, const TileClass *wall,
909 const Rect2i room)
910 {
911 int count = 0;
912 for (int i = 0; i < 100 && count < r.Walls; i++)
913 {
914 if (!MapTryBuildWall(
915 mb, true, MAX(r.WallPad, 1), r.WallLength, wall, room))
916 {
917 continue;
918 }
919 count++;
920 }
921 }
922
923 static void MapGrowWall(
924 MapBuilder *mb, struct vec2i pos, const bool isRoom, const int pad,
925 const int d, int length, const TileClass *wall);
926 bool MapTryBuildWall(
927 MapBuilder *mb, const bool isRoom, const int pad, const int wallLength,
928 const TileClass *wall, const Rect2i r)
929 {
930 const struct vec2i v =
931 Rect2iIsZero(r)
932 ? MapGetRandomTile(mb->Map)
933 : svec2i_add(r.Pos, svec2i(rand() % r.Size.x, rand() % r.Size.y));
934 if (MapIsValidStartForWall(mb, v, isRoom, pad))
935 {
936 MapBuilderSetTile(mb, v, wall);
937 MapGrowWall(mb, v, isRoom, pad, rand() & 3, wallLength, wall);
938 return true;
939 }
940 return false;
941 }
942 static bool MapGrowWallCheck(
943 const MapBuilder *mb, const struct vec2i v, const bool isRoom);
944 static void MapGrowWall(
945 MapBuilder *mb, struct vec2i pos, const bool isRoom, const int pad,
946 const int d, int length, const TileClass *wall)
947 {
948 int l;
949 struct vec2i v;
950
951 if (length <= 0)
952 return;
953
954 switch (d)
955 {
956 case 0:
957 if (pos.y < 2 + pad)
958 {
959 return;
960 }
961 // Check tiles above
962 // xxxxx
963 // xxx
964 // o
965 for (v.y = pos.y - 2; v.y > pos.y - 2 - pad; v.y--)
966 {
967 int level = v.y - (pos.y - 2);
968 for (v.x = pos.x - 1 - level; v.x <= pos.x + 1 + level; v.x++)
969 {
970 if (!MapGrowWallCheck(mb, v, isRoom))
971 return;
972 }
973 }
974 pos.y--;
975 break;
976 case 1:
977 // Check tiles to the right
978 // x
979 // xx
980 // oxx
981 // xx
982 // x
983 for (v.x = pos.x + 2; v.x < pos.x + 2 + pad; v.x++)
984 {
985 int level = v.x - (pos.x + 2);
986 for (v.y = pos.y - 1 - level; v.y <= pos.y + 1 + level; v.y++)
987 {
988 if (!MapGrowWallCheck(mb, v, isRoom))
989 return;
990 }
991 }
992 pos.x++;
993 break;
994 case 2:
995 // Check tiles below
996 // o
997 // xxx
998 // xxxxx
999 for (v.y = pos.y + 2; v.y < pos.y + 2 + pad; v.y++)
1000 {
1001 int level = v.y - (pos.y + 2);
1002 for (v.x = pos.x - 1 - level; v.x <= pos.x + 1 + level; v.x++)
1003 {
1004 if (!MapGrowWallCheck(mb, v, isRoom))
1005 return;
1006 }
1007 }
1008 pos.y++;
1009 break;
1010 case 4:
1011 if (pos.x < 2 + pad)
1012 {
1013 return;
1014 }
1015 // Check tiles to the left
1016 // x
1017 // xx
1018 // xxo
1019 // xx
1020 // x
1021 for (v.x = pos.x - 2; v.x > pos.x - 2 - pad; v.x--)
1022 {
1023 int level = v.x - (pos.x - 2);
1024 for (v.y = pos.y - 1 - level; v.y <= pos.y + 1 + level; v.y++)
1025 {
1026 if (!MapGrowWallCheck(mb, v, isRoom))
1027 return;
1028 }
1029 }
1030 pos.x--;
1031 break;
1032 }
1033 MapBuilderSetTile(mb, pos, wall);
1034 length--;
1035 if (length > 0 && (rand() & 3) == 0)
1036 {
1037 // Randomly try to grow the wall in a different direction
1038 l = rand() % length;
1039 MapGrowWall(mb, pos, isRoom, pad, rand() & 3, l, wall);
1040 length -= l;
1041 }
1042 // Keep growing wall in same direction
1043 MapGrowWall(mb, pos, isRoom, pad, d, length, wall);
1044 }
1045 static bool MapGrowWallCheck(
1046 const MapBuilder *mb, const struct vec2i v, const bool isRoom)
1047 {
1048 if (!MapIsTileIn(mb->Map, v))
1049 return true;
1050 const TileClass *t = MapBuilderGetTile(mb, v);
1051 if (t == NULL || t->Type != TILE_CLASS_FLOOR || t->IsRoom != isRoom)
1052 {
1053 return false;
1054 }
1055 return true;
1056 }
1057
1058 void MapSetRoomAccessMask(
1059 MapBuilder *mb, const Rect2i r, const uint16_t accessMask)
1060 {
1061 RECT_FOREACH(r)
1062 MapBuildSetAccess(mb, _v, accessMask);
1063 RECT_FOREACH_END()
1064 }
1065
1066 static void AddOverlapRooms(
1067 MapBuilder *mb, const Rect2i room, CArray *overlapRooms, CArray *rooms,
1068 const uint16_t accessMask);
1069 void MapSetRoomAccessMaskOverlap(
1070 MapBuilder *mb, CArray *rooms, const uint16_t accessMask)
1071 {
1072 CArray overlapRooms;
1073 CArrayInit(&overlapRooms, sizeof(Rect2i));
1074 const Rect2i room = *(const Rect2i *)CArrayGet(rooms, 0);
1075 CArrayPushBack(&overlapRooms, &room);
1076 CA_FOREACH(const Rect2i, r, overlapRooms)
1077 AddOverlapRooms(mb, *r, &overlapRooms, rooms, accessMask);
1078 CA_FOREACH_END()
1079 }
1080 static void AddOverlapRooms(
1081 MapBuilder *mb, const Rect2i room, CArray *overlapRooms, CArray *rooms,
1082 const uint16_t accessMask)
1083 {
1084 // Find all rooms that overlap with a room, and move it to the overlap
1085 // rooms array, setting access mask as we go
1086 CA_FOREACH(const Rect2i, r, *rooms)
1087 if (Rect2iOverlap(room, *r))
1088 {
1089 LOG(LM_MAP, LL_TRACE,
1090 "Room overlap {%d, %d (%dx%d)} {%d, %d (%dx%d)} access(%d)",
1091 room.Pos.x, room.Pos.y, room.Size.x, room.Size.y, r->Pos.x,
1092 r->Pos.y, r->Size.x, r->Size.y, accessMask);
1093 MapSetRoomAccessMask(mb, *r, accessMask);
1094 CArrayPushBack(overlapRooms, r);
1095 CArrayDelete(rooms, _ca_index);
1096 _ca_index--;
1097 }
1098 CA_FOREACH_END()
1099 }
1100
1101 static void PlaceDoors(
1102 MapBuilder *mb, const int doorSize, const int roomDim,
1103 const struct vec2i doorStart, const struct vec2i d, const bool randomPos,
1104 const TileClass *tile);
1105 void MapPlaceDoors(
1106 MapBuilder *mb, const Rect2i r, const bool hasDoors, const bool doors[4],
1107 const int doorMin, const int doorMax, const uint16_t accessMask,
1108 const bool randomPos, const TileClass *door, const TileClass *floor)
1109 {
1110 const TileClass *tile = hasDoors ? door : floor;
1111
1112 MapSetRoomAccessMask(mb, r, accessMask);
1113
1114 // Set the doors
1115 for (int i = 0; i < 4; i++)
1116 {
1117 if (!doors[i])
1118 {
1119 continue;
1120 }
1121 const int doorSize =
1122 doorMax > doorMin ? RAND_INT(doorMin, doorMax) : doorMin;
1123 int roomDim;
1124 struct vec2i d;
1125 struct vec2i doorStart;
1126 switch (i)
1127 {
1128 case 0:
1129 // left
1130 roomDim = r.Size.y;
1131 d = svec2i(1, 0);
1132 doorStart = r.Pos;
1133 break;
1134 case 1:
1135 // right
1136 roomDim = r.Size.y;
1137 d = svec2i(1, 0);
1138 doorStart = svec2i(r.Pos.x + r.Size.x - 1, r.Pos.y);
1139 break;
1140 case 2:
1141 // top
1142 roomDim = r.Size.x;
1143 d = svec2i(0, 1);
1144 doorStart = r.Pos;
1145 break;
1146 case 3:
1147 // bottom:
1148 roomDim = r.Size.x;
1149 d = svec2i(0, 1);
1150 doorStart = svec2i(r.Pos.x, r.Pos.y + r.Size.y - 1);
1151 break;
1152 default:
1153 CASSERT(false, "unexpected side index");
1154 return;
1155 }
1156 PlaceDoors(mb, doorSize, roomDim, doorStart, d, randomPos, tile);
1157 }
1158 }
1159 static bool TryPlaceDoorTile(
1160 MapBuilder *mb, const struct vec2i v, const struct vec2i d,
1161 const TileClass *tile);
1162 static void PlaceDoors(
1163 MapBuilder *mb, const int doorSize, const int roomDim,
1164 const struct vec2i doorStart, const struct vec2i d, const bool randomPos,
1165 const TileClass *tile)
1166 {
1167 const struct vec2i dAcross = svec2i_subtract(svec2i_one(), d);
1168 const int size = MIN(doorSize, roomDim - 2);
1169 struct vec2i start = doorStart;
1170 if (randomPos)
1171 {
1172 start = svec2i_add(
1173 start,
1174 svec2i_scale(dAcross, (float)RAND_INT(1, roomDim - size - 1)));
1175 }
1176 else
1177 {
1178 start = svec2i_add(
1179 start, svec2i_scale(dAcross, (float)((roomDim - size) / 2)));
1180 }
1181 for (int i = 0; i < size; i++)
1182 {
1183 const struct vec2i v =
1184 svec2i_add(start, svec2i_scale(dAcross, (float)i));
1185 if (!TryPlaceDoorTile(mb, v, d, tile))
1186 {
1187 break;
1188 }
1189 }
1190 }
1191 static bool TryPlaceDoorTile(
1192 MapBuilder *mb, const struct vec2i v, const struct vec2i d,
1193 const TileClass *tile)
1194 {
1195 if (MapBuilderGetTile(mb, v)->Type == TILE_CLASS_WALL &&
1196 MapBuilderGetTile(mb, svec2i_add(v, d))->Type != TILE_CLASS_WALL &&
1197 MapBuilderGetTile(mb, svec2i_subtract(v, d))->Type != TILE_CLASS_WALL)
1198 {
1199 MapBuilderSetTile(mb, v, tile);
1200 return true;
1201 }
1202 return false;
1203 }
1204
1205 bool MapIsAreaInside(
1206 const Map *map, const struct vec2i pos, const struct vec2i size)
1207 {
1208 return pos.x >= 0 && pos.y >= 0 && pos.x + size.x < map->Size.x &&
1209 pos.y + size.y < map->Size.y;
1210 }
1211
1212 bool MapBuilderIsAreaFunc(
1213 const MapBuilder *mb, const struct vec2i pos, const struct vec2i size,
1214 bool (*func)(const MapBuilder *, const struct vec2i))
1215 {
1216 if (!MapIsAreaInside(mb->Map, pos, size))
1217 {
1218 return false;
1219 }
1220 RECT_FOREACH(Rect2iNew(pos, size))
1221 if (!func(mb, _v))
1222 return false;
1223 RECT_FOREACH_END()
1224 return true;
1225 }
1226
1227 static bool IsClear(const MapBuilder *mb, const struct vec2i pos)
1228 {
1229 return MapBuilderGetTile(mb, pos)->Type == TILE_CLASS_FLOOR;
1230 }
1231 bool MapIsAreaClear(
1232 const MapBuilder *mb, const struct vec2i pos, const struct vec2i size)
1233 {
1234 return MapBuilderIsAreaFunc(mb, pos, size, IsClear);
1235 }
1236 static bool AreaHasRoomAndFloor(const MapBuilder *mb, const struct vec2i pos)
1237 {
1238 // Find whether a wall tile is part of a room perimeter
1239 // The surrounding tiles must have normal floor and room tiles
1240 // to be a perimeter
1241 bool hasRoom = false;
1242 bool hasFloor = false;
1243 RECT_FOREACH(Rect2iNew(svec2i_subtract(pos, svec2i_one()), svec2i(3, 3)))
1244 const TileClass *t = MapBuilderGetTile(mb, _v);
1245 if (t == NULL || t->Type != TILE_CLASS_FLOOR)
1246 continue;
1247 if (t->IsRoom)
1248 hasRoom = true;
1249 else
1250 hasFloor = true;
1251 RECT_FOREACH_END()
1252 return hasRoom && hasFloor;
1253 }
1254 static bool IsClearOrRoom(const MapBuilder *mb, const struct vec2i pos)
1255 {
1256 const TileClass *tile = MapBuilderGetTile(mb, pos);
1257 if (tile->Type == TILE_CLASS_FLOOR)
1258 return true;
1259 // Check if this wall is part of a room
1260 if (tile->Type != TILE_CLASS_WALL)
1261 return false;
1262 return AreaHasRoomAndFloor(mb, pos);
1263 }
1264 bool MapIsAreaClearOrRoom(
1265 const MapBuilder *mb, const struct vec2i pos, const struct vec2i size)
1266 {
1267 return MapBuilderIsAreaFunc(mb, pos, size, IsClearOrRoom);
1268 }
1269 static bool IsClearOrWall(const MapBuilder *mb, const struct vec2i pos)
1270 {
1271 const TileClass *tile = MapBuilderGetTile(mb, pos);
1272 if (tile->Type == TILE_CLASS_FLOOR && !tile->IsRoom)
1273 return true;
1274 // Check if this wall is part of a room
1275 if (tile->Type != TILE_CLASS_WALL)
1276 return false;
1277 return AreaHasRoomAndFloor(mb, pos);
1278 }
1279 bool MapIsAreaClearOrWall(
1280 const MapBuilder *mb, struct vec2i pos, struct vec2i size)
1281 {
1282 return MapBuilderIsAreaFunc(mb, pos, size, IsClearOrWall);
1283 }
1284 // Find the size of the passage created by the overlap of two rooms
1285 // To find whether an overlap is valid,
1286 // collect the perimeter walls that overlap
1287 // Two possible cases where there is a valid overlap:
1288 // - if there are exactly two overlapping perimeter walls
1289 // i.e.
1290 // X
1291 // room 2 XXXXXX
1292 // X X <-- all tiles between either belong to room 1 or room 2
1293 // XXXXXX
1294 // X room 1
1295 //
1296 // - if the collection of overlapping tiles are contiguous
1297 // i.e.
1298 // X room 1 X
1299 // XXXXXXXXXXXXXXX
1300 // X room 2 X
1301 //
1302 // In both cases, the overlap is valid if all tiles in between are room or
1303 // perimeter tiles. The size of the passage is given by the largest difference
1304 // in the x or y coordinates between the first and last intersection tiles,
1305 // minus 1
1306 bool MapGetRoomOverlapSize(
1307 const MapBuilder *mb, const Rect2i r, uint16_t *overlapAccess)
1308 {
1309 int numOverlaps = 0;
1310 struct vec2i overlapMin = svec2i_zero();
1311 struct vec2i overlapMax = svec2i_zero();
1312
1313 // Find perimeter tiles that overlap
1314 RECT_FOREACH(r)
1315 // only check perimeter
1316 if (_v.x != r.Pos.x && _v.x != r.Pos.x + r.Size.x - 1 && _v.y != r.Pos.y &&
1317 _v.y != r.Pos.y + r.Size.y - 1)
1318 {
1319 continue;
1320 }
1321 if (!MapIsTileIn(mb->Map, _v))
1322 {
1323 continue;
1324 }
1325 const TileClass *tile = MapBuilderGetTile(mb, _v);
1326 // Check if this wall is part of a room
1327 if (tile->Type != TILE_CLASS_WALL || !AreaHasRoomAndFloor(mb, _v))
1328 {
1329 continue;
1330 }
1331 // Get the access level of the room
1332 struct vec2i v2;
1333 for (v2.y = _v.y - 1; v2.y <= _v.y + 1; v2.y++)
1334 {
1335 for (v2.x = _v.x - 1; v2.x <= _v.x + 1; v2.x++)
1336 {
1337 const TileClass *t = MapBuilderGetTile(mb, v2);
1338 if (t != NULL && t->IsRoom)
1339 {
1340 if (overlapAccess != NULL)
1341 {
1342 *overlapAccess |= MapBuildGetAccess(mb, v2);
1343 }
1344 }
1345 }
1346 }
1347 if (numOverlaps == 0)
1348 {
1349 overlapMin = overlapMax = _v;
1350 }
1351 else
1352 {
1353 overlapMin = svec2i_min(overlapMin, _v);
1354 overlapMax = svec2i_max(overlapMax, _v);
1355 }
1356 numOverlaps++;
1357 RECT_FOREACH_END()
1358 if (numOverlaps < 2)
1359 {
1360 return 0;
1361 }
1362
1363 // Now check that all tiles between the first and last tiles are room or
1364 // perimeter tiles
1365 struct vec2i v;
1366 for (v.y = overlapMin.y; v.y <= overlapMax.y; v.y++)
1367 {
1368 for (v.x = overlapMin.x; v.x <= overlapMax.x; v.x++)
1369 {
1370 const TileClass *tile = MapBuilderGetTile(mb, v);
1371 if (tile->Type == TILE_CLASS_WALL)
1372 {
1373 // Check if this wall is part of a room
1374 if (!AreaHasRoomAndFloor(mb, v))
1375 {
1376 return 0;
1377 }
1378 }
1379 else if (!tile->IsRoom)
1380 {
1381 // invalid tile type
1382 return 0;
1383 }
1384 }
1385 }
1386
1387 return MAX(overlapMax.x - overlapMin.x, overlapMax.y - overlapMin.y) - 1;
1388 }
1389 // Check that this area does not overlap two or more "walls"
1390 bool MapIsLessThanTwoWallOverlaps(
1391 const MapBuilder *mb, struct vec2i pos, struct vec2i size)
1392 {
1393 if (!MapIsTileIn(mb->Map, pos))
1394 {
1395 return false;
1396 }
1397
1398 struct vec2i v;
1399 int numOverlaps = 0;
1400 struct vec2i overlapMin = svec2i_zero();
1401 struct vec2i overlapMax = svec2i_zero();
1402 for (v.y = pos.y; v.y < pos.y + size.y; v.y++)
1403 {
1404 for (v.x = pos.x; v.x < pos.x + size.x; v.x++)
1405 {
1406 // only check perimeter
1407 if (v.x != pos.x && v.x != pos.x + size.x - 1 && v.y != pos.y &&
1408 v.y != pos.y + size.y - 1)
1409 {
1410 continue;
1411 }
1412 if (MapBuilderGetTile(mb, v)->Type != TILE_CLASS_WALL)
1413 {
1414 continue;
1415 }
1416 // Check if this wall is part of a room
1417 if (!AreaHasRoomAndFloor(mb, v))
1418 {
1419 if (numOverlaps == 0)
1420 {
1421 overlapMin = overlapMax = v;
1422 }
1423 else
1424 {
1425 overlapMin = svec2i_min(overlapMin, v);
1426 overlapMax = svec2i_max(overlapMax, v);
1427 }
1428 numOverlaps++;
1429 }
1430 }
1431 }
1432 if (numOverlaps < 2)
1433 {
1434 return true;
1435 }
1436
1437 // Now check that all tiles between the first and last tiles are
1438 // pillar tiles
1439 for (v.y = overlapMin.y; v.y <= overlapMax.y; v.y++)
1440 {
1441 for (v.x = overlapMin.x; v.x <= overlapMax.x; v.x++)
1442 {
1443 if (MapBuilderGetTile(mb, v)->Type != TILE_CLASS_WALL)
1444 {
1445 // invalid tile type
1446 return false;
1447 }
1448 // Check if this wall is not part of a room
1449 if (AreaHasRoomAndFloor(mb, v))
1450 {
1451 return false;
1452 }
1453 }
1454 }
1455
1456 return true;
1457 }
1458
1459 void MapFillRect(
1460 MapBuilder *mb, const Rect2i r, const TileClass *edge,
1461 const TileClass *fill)
1462 {
1463 RECT_FOREACH(r)
1464 const TileClass *tc = Rect2iIsAtEdge(r, _v) ? edge : fill;
1465 MapBuilderSetTile(mb, _v, tc);
1466 RECT_FOREACH_END()
1467 }
1468
1469 uint16_t GenerateAccessMask(int *accessLevel)
1470 {
1471 uint16_t accessMask = 0;
1472 switch (rand() % 20)
1473 {
1474 case 0:
1475 if (*accessLevel >= 4)
1476 {
1477 accessMask = MAP_ACCESS_RED;
1478 *accessLevel = 5;
1479 }
1480 break;
1481 case 1:
1482 case 2:
1483 if (*accessLevel >= 3)
1484 {
1485 accessMask = MAP_ACCESS_BLUE;
1486 if (*accessLevel < 4)
1487 {
1488 *accessLevel = 4;
1489 }
1490 }
1491 break;
1492 case 3:
1493 case 4:
1494 case 5:
1495 if (*accessLevel >= 2)
1496 {
1497 accessMask = MAP_ACCESS_GREEN;
1498 if (*accessLevel < 3)
1499 {
1500 *accessLevel = 3;
1501 }
1502 }
1503 break;
1504 case 6:
1505 case 7:
1506 case 8:
1507 case 9:
1508 if (*accessLevel >= 1)
1509 {
1510 accessMask = MAP_ACCESS_YELLOW;
1511 if (*accessLevel < 2)
1512 {
1513 *accessLevel = 2;
1514 }
1515 }
1516 break;
1517 }
1518 if (*accessLevel < 1)
1519 {
1520 *accessLevel = 1;
1521 }
1522 return accessMask;
1523 }
1524
1525 static void MapGenerateRandomExitArea(Map *map, const int mission)
1526 {
1527 const Tile *t = NULL;
1528 Exit exit;
1529 exit.Mission = mission + 1;
1530 exit.Hidden = false;
1531 for (int i = 0; i < 10000 && (t == NULL || !TileCanWalk(t)); i++)
1532 {
1533 exit.R.Size.x = MIN(map->Size.x - 2, EXIT_WIDTH + 1);
1534 exit.R.Pos.x = (rand() % (abs(map->Size.x) - exit.R.Size.x));
1535 exit.R.Size.y = MIN(map->Size.y - 2, EXIT_HEIGHT + 1);
1536 exit.R.Pos.y = (rand() % (abs(map->Size.y) - exit.R.Size.y));
1537 // Check that the exit area is walkable
1538 t = MapGetTile(map, Rect2iCenter(exit.R));
1539 }
1540 CArrayPushBack(&map->exits, &exit);
1541 }
1542
1543 static void MapAddDrains(MapBuilder *mb)
1544 {
1545 // Randomly add drainage tiles for classic map type;
1546 // For other map types drains are regular map objects
1547 const MapObject *drain = StrMapObject("drain0");
1548 if (drain == NULL)
1549 {
1550 return;
1551 }
1552 for (int i = 0; i < mb->Map->Size.x * mb->Map->Size.y / 45; i++)
1553 {
1554 // Make sure drain tiles aren't next to each other
1555 struct vec2i v = MapGetRandomTile(mb->Map);
1556 v.x &= 0xFFFFFE;
1557 v.y &= 0xFFFFFE;
1558 if (MapTileIsNormalFloor(mb, v))
1559 {
1560 MapTryPlaceOneObject(mb, v, drain, 0, false);
1561 }
1562 }
1563 }