Debian Codegraph @

src/cdogs/map_interior.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2021 Cong Xu
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "map_interior.h"
29
30 #include "algorithms.h"
31 #include "log.h"
32 #include "map_build.h"
33
34 #define CORRIDOR_LEVEL_DIFF_BLOCK 1
35
36 // Which side of the critical path this area is on
37 // This is important for key placement, as the start is placed on the left,
38 // and exit on the right.
39 typedef enum
40 {
41 CRIT_PATH_NONE,
42 CRIT_PATH_LEFT,
43 CRIT_PATH_RIGHT,
44 } CriticalPath;
45
46 typedef struct
47 {
48 Rect2i r;
49 int level;
50 int parent;
51 int child1;
52 int child2;
53 bool horizontal;
54 bool isCorridor;
55 CriticalPath criticalPath;
56 } BSPArea;
57
58 static BSPArea BSPAreaRoot(const struct vec2i size)
59 {
60 BSPArea b;
61 memset(&b, 0, sizeof b);
62 b.r = Rect2iNew(svec2i_zero(), size);
63 b.parent = -1;
64 b.child1 = -1;
65 b.child2 = -1;
66 return b;
67 }
68 static bool BSPAreaIsLeaf(const BSPArea *b)
69 {
70 return b->child1 == -1 && b->child2 == -1;
71 }
72
73 typedef struct
74 {
75 CArray a;
76 size_t dim;
77 } Adjacency;
78
79 static Adjacency AdjacencyNew(const size_t dim)
80 {
81 Adjacency am;
82 CArrayInitFillZero(&am.a, sizeof(bool), dim * dim);
83 am.dim = dim;
84 return am;
85 }
86 static void AdjacencyTerminate(Adjacency *am)
87 {
88 CArrayTerminate(&am->a);
89 }
90 static void AdjacencyConnect(Adjacency *am, const int i, const int j)
91 {
92 CArraySet(&am->a, i + j * am->dim, &gTrue);
93 CArraySet(&am->a, j + i * am->dim, &gTrue);
94 }
95 static bool AdjacencyIsConnected(const Adjacency *am, const int i, const int j)
96 {
97 const bool *isAdjacent = CArrayGet(&am->a, i + j * am->dim);
98 return *isAdjacent;
99 }
100 static bool AdjacencyHasConnections(const Adjacency *am, const int i)
101 {
102 for (int j = 0; j < (int)am->dim; j++)
103 {
104 if (AdjacencyIsConnected(am, i, j))
105 {
106 return true;
107 }
108 }
109 return false;
110 }
111 static void AdjacencyDisconnectAll(Adjacency *am, const int i)
112 {
113 for (int j = 0; j < (int)am->dim; j++)
114 {
115 CArraySet(&am->a, i + j * am->dim, &gFalse);
116 CArraySet(&am->a, j + i * am->dim, &gFalse);
117 }
118 }
119
120 static void SplitAreas(MapBuilder *mb, CArray *areas);
121 static void SplitLeafRooms(MapBuilder *mb, CArray *areas);
122 static void FillRooms(MapBuilder *mb, const CArray *areas);
123 static Adjacency SetupAdjacencyMatrix(const CArray *areas);
124 static void AddDoorsToClosestCorridors(
125 MapBuilder *mb, CArray *areas, Adjacency *am);
126 static void ConnectUnconnectedRooms(
127 MapBuilder *mb, CArray *areas, Adjacency *am);
128 static void FindAndMarkCriticalPath(
129 MapBuilder *mb, const CArray *areas, const int missionIndex);
130 static void FillCorridors(MapBuilder *mb, const CArray *areas);
131 static CArray CalcDistanceToCriticalPath(
132 const CArray *areas, const Adjacency *am);
133 static void PlaceKeys(
134 MapBuilder *mb, const CArray *areas, const Adjacency *am,
135 CArray *dCriticalPath);
136 static void AddPillars(
137 MapBuilder *mb, const CArray *areas, Adjacency *am,
138 const CArray *dCriticalPath);
139 static void AddRoomWalls(MapBuilder *mb, const CArray *areas);
140 void MapInteriorLoad(MapBuilder *mb, const int missionIndex)
141 {
142 // TODO: multiple tile types
143 MissionSetupTileClasses(
144 mb->Map, &gPicManager, &mb->mission->u.Interior.TileClasses);
145
146 CArray areas;
147 CArrayInit(&areas, sizeof(BSPArea));
148 BSPArea a = BSPAreaRoot(mb->Map->Size);
149
150 // Split the map for a number of iterations, choosing alternating axis and
151 // random location
152 CArrayPushBack(&areas, &a);
153 SplitAreas(mb, &areas);
154
155 // Try to split leaf rooms into more rooms, by longest axis
156 SplitLeafRooms(mb, &areas);
157
158 FillRooms(mb, &areas);
159
160 if (areas.size > 1)
161 {
162 Adjacency am = SetupAdjacencyMatrix(&areas);
163
164 // Add doors leading to the closest corridor in the hierarchy
165 AddDoorsToClosestCorridors(mb, &areas, &am);
166
167 // For every room, connect it to a random shallower room
168 // Keep going until all rooms are connected
169 ConnectUnconnectedRooms(mb, &areas, &am);
170
171 // Find deepest leaf going down both branches; place start/end
172 // This represents longest/critical path
173 FindAndMarkCriticalPath(mb, &areas, missionIndex);
174
175 FillCorridors(mb, &areas);
176
177 CArray dCriticalPath = CalcDistanceToCriticalPath(&areas, &am);
178
179 if (mb->mission->u.Interior.Doors.Enabled)
180 {
181 // For each locked street (street on critical path), place a key in
182 // a non-critical leaf Do so by following a child away from the
183 // critical path
184 PlaceKeys(mb, &areas, &am, &dCriticalPath);
185 }
186
187 // Fill random leaf rooms
188 AddPillars(mb, &areas, &am, &dCriticalPath);
189
190 // Add rooms after placing keys to avoid overlaps
191 AddRoomWalls(mb, &areas);
192
193 CArrayTerminate(&dCriticalPath);
194 AdjacencyTerminate(&am);
195 }
196
197 CArrayTerminate(&areas);
198 }
199
200 static bool BSPAreaTrySplit(
201 const CArray *areas, const bool horizontal, const int idx,
202 const int minSize, BSPArea *r1, BSPArea *r2);
203 static void SplitAreas(MapBuilder *mb, CArray *areas)
204 {
205 const int hcount = RAND_INT(0, 2);
206
207 // Need to allow at least one split
208 const int minSize =
209 MIN(mb->mission->u.Interior.Rooms.Min +
210 mb->mission->u.Interior.CorridorWidth / 2,
211 MIN(mb->Map->Size.x, mb->Map->Size.y));
212 CA_FOREACH(BSPArea, a, *areas)
213 // Alternate splitting direction per level
214 const bool horizontal = ((hcount + a->level) % 2) == 1;
215 BSPArea a1 = BSPAreaRoot(svec2i_zero());
216 BSPArea a2 = BSPAreaRoot(svec2i_zero());
217 if (BSPAreaTrySplit(areas, horizontal, _ca_index, minSize, &a1, &a2))
218 {
219 // Resize rooms to allow space for street
220 for (int i = 0; i < mb->mission->u.Interior.CorridorWidth; i++)
221 {
222 if (horizontal)
223 {
224 if ((i % 2) == 0)
225 {
226 a1.r.Size.x--;
227 }
228 else
229 {
230 a2.r.Pos.x++;
231 a2.r.Size.x--;
232 }
233 }
234 else
235 {
236 if ((i % 2) == 0)
237 {
238 a1.r.Size.y--;
239 }
240 else
241 {
242 a2.r.Pos.y++;
243 a2.r.Size.y--;
244 }
245 }
246 }
247 // Replace current area with a corridor
248 a->isCorridor = true;
249 if (horizontal)
250 {
251 a->r = Rect2iNew(
252 svec2i(a1.r.Pos.x + a1.r.Size.x, a1.r.Pos.y),
253 svec2i(mb->mission->u.Interior.CorridorWidth, a1.r.Size.y));
254 }
255 else
256 {
257 a->r = Rect2iNew(
258 svec2i(a1.r.Pos.x, a1.r.Pos.y + a1.r.Size.y),
259 svec2i(a1.r.Size.x, mb->mission->u.Interior.CorridorWidth));
260 }
261 a->horizontal = !horizontal;
262
263 // Add children
264 a->child1 = (int)areas->size;
265 a->child2 = (int)areas->size + 1;
266 CArrayPushBack(areas, &a1);
267 CArrayPushBack(areas, &a2);
268 }
269 CA_FOREACH_END()
270 }
271 static bool BSPAreaTrySplit(
272 const CArray *areas, const bool horizontal, const int idx,
273 const int minSize, BSPArea *a1, BSPArea *a2)
274 {
275 const BSPArea *area = CArrayGet(areas, idx);
276 const int r = horizontal ? area->r.Size.x - minSize * 2
277 : area->r.Size.y - minSize * 2;
278 if (r < 0)
279 {
280 // Too small; can't split
281 return false;
282 }
283 a1->level = a2->level = area->level + 1;
284 a1->parent = a2->parent = idx;
285 a1->horizontal = a2->horizontal = horizontal;
286 if (horizontal)
287 {
288 // Left/right children
289 const int x = RAND_INT(0, r) + minSize;
290 a1->r = Rect2iNew(area->r.Pos, svec2i(x, area->r.Size.y));
291 a2->r = Rect2iNew(
292 svec2i(area->r.Pos.x + x, area->r.Pos.y),
293 svec2i(area->r.Size.x - x, area->r.Size.y));
294 }
295 else
296 {
297 // Top/bottom children
298 const int y = RAND_INT(0, r) + minSize;
299 a1->r = Rect2iNew(area->r.Pos, svec2i(area->r.Size.x, y));
300 a2->r = Rect2iNew(
301 svec2i(area->r.Pos.x, area->r.Pos.y + y),
302 svec2i(area->r.Size.x, area->r.Size.y - y));
303 }
304 return true;
305 }
306
307 static void SplitLeafRooms(MapBuilder *mb, CArray *areas)
308 {
309 CA_FOREACH(BSPArea, a, *areas)
310 if (a->isCorridor)
311 {
312 continue;
313 }
314 // Don't split if we're under max size
315 if (a->r.Size.x <= mb->mission->u.Interior.Rooms.Max &&
316 a->r.Size.y <= mb->mission->u.Interior.Rooms.Max)
317 {
318 continue;
319 }
320 const bool horizontal = a->r.Size.x > a->r.Size.y;
321 BSPArea a1 = BSPAreaRoot(svec2i_zero());
322 BSPArea a2 = BSPAreaRoot(svec2i_zero());
323 if (BSPAreaTrySplit(
324 areas, horizontal, _ca_index, mb->mission->u.Interior.Rooms.Min,
325 &a1, &a2))
326 {
327 // Resize rooms so they share a splitting wall
328 if (a1.horizontal)
329 {
330 a1.r.Size.x++;
331 }
332 else
333 {
334 a1.r.Size.y++;
335 }
336
337 // Add children
338 a->child1 = (int)areas->size;
339 CArrayPushBack(areas, &a1);
340 a->child2 = (int)areas->size;
341 CArrayPushBack(areas, &a2);
342 }
343 CA_FOREACH_END()
344 }
345
346 static void FillRooms(MapBuilder *mb, const CArray *areas)
347 {
348 CA_FOREACH(BSPArea, a, *areas)
349 // Skip non-leaves
350 if (!BSPAreaIsLeaf(a))
351 {
352 continue;
353 }
354 MapMakeRoom(
355 mb, a->r.Pos, a->r.Size, true,
356 &mb->mission->u.Interior.TileClasses.Wall,
357 &mb->mission->u.Interior.TileClasses.Room, false);
358 CA_FOREACH_END()
359 }
360
361 static Adjacency SetupAdjacencyMatrix(const CArray *areas)
362 {
363 Adjacency am = AdjacencyNew(areas->size);
364 CA_FOREACH(const BSPArea, a, *areas)
365 if (!a->isCorridor || a->parent == -1)
366 {
367 continue;
368 }
369 AdjacencyConnect(&am, _ca_index, a->parent);
370 CA_FOREACH_END()
371 return am;
372 }
373
374 static void AddDoorsToClosestCorridors(
375 MapBuilder *mb, CArray *areas, Adjacency *am)
376 {
377 CA_FOREACH(BSPArea, a, *areas)
378 // Skip non-leaves
379 if (!BSPAreaIsLeaf(a))
380 {
381 continue;
382 }
383 // Add doors leading to corridors
384 const BSPArea *corridor = a;
385 int corridorI = _ca_index;
386 for (; !corridor->isCorridor && corridor->parent >= 0;)
387 {
388 corridorI = corridor->parent;
389 corridor = CArrayGet(areas, corridorI);
390 }
391 // Four walls/directions
392 for (int i = 0; i < 4; i++)
393 {
394 struct vec2i doorPos =
395 svec2i(a->r.Pos.x + a->r.Size.x / 2, a->r.Pos.y + a->r.Size.y / 2);
396 struct vec2i outsideDoor = doorPos;
397 switch (i)
398 {
399 case 0:
400 // left
401 doorPos.x = a->r.Pos.x;
402 outsideDoor = svec2i(doorPos.x - 1, doorPos.y);
403 break;
404 case 1:
405 // right
406 doorPos.x = a->r.Pos.x + a->r.Size.x - 1;
407 outsideDoor = svec2i(doorPos.x + 1, doorPos.y);
408 break;
409 case 2:
410 // top
411 doorPos.y = a->r.Pos.y;
412 outsideDoor = svec2i(doorPos.x, doorPos.y - 1);
413 break;
414 case 3:
415 // bottom
416 doorPos.y = a->r.Pos.y + a->r.Size.y - 1;
417 outsideDoor = svec2i(doorPos.x, doorPos.y + 1);
418 break;
419 default:
420 CASSERT(false, "unexpected");
421 break;
422 }
423 if (Rect2iIsInside(corridor->r, outsideDoor))
424 {
425 bool doors[4];
426 memset(&doors, 0, sizeof doors);
427 doors[i] = true;
428 MapPlaceDoors(
429 mb, a->r, mb->mission->u.Interior.Doors.Enabled, doors,
430 mb->mission->u.Interior.Doors.Min,
431 mb->mission->u.Interior.Doors.Max, 0,
432 mb->mission->u.Interior.Doors.RandomPos,
433 &mb->mission->u.Interior.TileClasses.Door,
434 &mb->mission->u.Interior.TileClasses.Room);
435 AdjacencyConnect(am, _ca_index, corridorI);
436 // Change parentage
437 a->parent = corridorI;
438 break;
439 }
440 }
441 CA_FOREACH_END()
442 }
443
444 static bool TryConnectRooms(
445 MapBuilder *mb, CArray *areas, Adjacency *am, BSPArea *a1, const int aIdx);
446 static void ConnectUnconnectedRooms(
447 MapBuilder *mb, CArray *areas, Adjacency *am)
448 {
449 int numUnconnected = 0;
450 do
451 {
452 numUnconnected = 0;
453 CA_FOREACH(BSPArea, a, *areas)
454 if (AdjacencyHasConnections(am, _ca_index) || a->isCorridor ||
455 !BSPAreaIsLeaf(a))
456 {
457 continue;
458 }
459 if (!TryConnectRooms(mb, areas, am, a, _ca_index))
460 {
461 numUnconnected++;
462 }
463 CA_FOREACH_END()
464 } while (numUnconnected > 0);
465 }
466 static bool RectIsAdjacent(
467 const Rect2i r1, const Rect2i r2, const int overlapSize);
468 static bool TryConnectRooms(
469 MapBuilder *mb, CArray *areas, Adjacency *am, BSPArea *a1, const int aIdx)
470 {
471 CA_FOREACH(BSPArea, a2, *areas)
472 // Only connect to a room that is also connected
473 if (!BSPAreaIsLeaf(a2) || a1 == a2 ||
474 !AdjacencyHasConnections(am, _ca_index))
475 {
476 continue;
477 }
478 // Shrink rectangles by 1 to determine
479 // overlap
480 Rect2i r1 = a1->r;
481 r1.Size.x--;
482 r1.Size.y--;
483 Rect2i r2 = a2->r;
484 r2.Size.x--;
485 r2.Size.y--;
486 const int overlapSize = 1;
487 if (!RectIsAdjacent(r1, r2, overlapSize))
488 {
489 continue;
490 }
491 // Rooms are adjacent; add a door in the adjacent area
492 bool doors[4];
493 memset(&doors, 0, sizeof doors);
494 if (a1->r.Pos.x > a2->r.Pos.x)
495 {
496 // left
497 doors[0] = true;
498 }
499 else if (a1->r.Pos.x < a2->r.Pos.x)
500 {
501 // right
502 doors[1] = true;
503 }
504 else if (a1->r.Pos.y > a2->r.Pos.y)
505 {
506 // top
507 doors[2] = true;
508 }
509 else
510 {
511 // botttom
512 doors[3] = true;
513 }
514 MapPlaceDoors(
515 mb, a1->r, mb->mission->u.Interior.Doors.Enabled, doors,
516 mb->mission->u.Interior.Doors.Min, mb->mission->u.Interior.Doors.Max,
517 0, mb->mission->u.Interior.Doors.RandomPos,
518 &mb->mission->u.Interior.TileClasses.Door,
519 &mb->mission->u.Interior.TileClasses.Room);
520 AdjacencyConnect(am, aIdx, _ca_index);
521 // Change parentage
522 a1->parent = _ca_index;
523 return true;
524 CA_FOREACH_END()
525 return false;
526 }
527 static bool RectIsAdjacent(
528 const Rect2i r1, const Rect2i r2, const int overlapSize)
529 {
530 // If left/right edges adjacent
531 if (r1.Pos.x - (r2.Pos.x + r2.Size.x) == 0 ||
532 r2.Pos.x - (r1.Pos.x + r1.Size.x) == 0)
533 {
534 return r1.Pos.y + overlapSize < r2.Pos.y + r2.Size.y &&
535 r2.Pos.y + overlapSize < r1.Pos.y + r1.Size.y;
536 }
537 if (r1.Pos.y - (r2.Pos.y + r2.Size.y) == 0 ||
538 r2.Pos.y - (r1.Pos.y + r1.Size.y) == 0)
539 {
540 return r1.Pos.x + overlapSize < r2.Pos.x + r2.Size.x &&
541 r2.Pos.x + overlapSize < r1.Pos.x + r1.Size.x;
542 }
543 return false;
544 }
545
546 static int FindDeepestRoomFrom(const CArray *areas, const int idx);
547 static void MarkParentCorridors(
548 const CArray *areas, const int idx, const CriticalPath cp);
549 static void FindAndMarkCriticalPath(
550 MapBuilder *mb, const CArray *areas, const int missionIndex)
551 {
552 const BSPArea *root = CArrayGet(areas, 0);
553 const int deepestRoom1 = FindDeepestRoomFrom(areas, root->child1);
554 const BSPArea *a1 = CArrayGet(areas, deepestRoom1);
555 mb->Map->start =
556 svec2i_add(a1->r.Pos, svec2i_divide(a1->r.Size, svec2i(2, 2)));
557 const int deepestRoom2 = FindDeepestRoomFrom(areas, root->child2);
558 const BSPArea *a2 = CArrayGet(areas, deepestRoom2);
559 if (mb->mission->u.Interior.ExitEnabled)
560 {
561 Exit exit;
562 exit.Mission = missionIndex + 1;
563 exit.Hidden = false;
564 exit.R.Pos = svec2i_add(a2->r.Pos, svec2i_one());
565 exit.R.Size = svec2i_subtract(a2->r.Size, svec2i(3, 3));
566 CArrayPushBack(&mb->Map->exits, &exit);
567 }
568 MarkParentCorridors(areas, deepestRoom1, CRIT_PATH_LEFT);
569 MarkParentCorridors(areas, deepestRoom2, CRIT_PATH_RIGHT);
570 }
571 static int FindDeepestRoomFrom(const CArray *areas, const int idx)
572 {
573 if (idx == -1)
574 {
575 return 0;
576 }
577 CArray pathStack;
578 CArrayInit(&pathStack, sizeof(int));
579 CArrayPushBack(&pathStack, &idx);
580 int deepestChild = -1;
581 int maxDepth = 0;
582 while (pathStack.size > 0)
583 {
584 const int i = *(int *)CArrayGet(&pathStack, pathStack.size - 1);
585 const BSPArea *a = CArrayGet(areas, i);
586 CArrayPopBack(&pathStack);
587 if (BSPAreaIsLeaf(a))
588 {
589 if (a->level > maxDepth)
590 {
591 maxDepth = a->level;
592 deepestChild = i;
593 }
594 }
595 if (a->child1 >= 0)
596 {
597 CArrayPushBack(&pathStack, &a->child1);
598 }
599 if (a->child2 >= 0)
600 {
601 CArrayPushBack(&pathStack, &a->child2);
602 }
603 }
604 CArrayTerminate(&pathStack);
605 return deepestChild;
606 }
607 static void MarkParentCorridors(
608 const CArray *areas, const int idx, const CriticalPath cp)
609 {
610 for (int i = idx; i >= 0;)
611 {
612 BSPArea *corridor = CArrayGet(areas, i);
613 corridor->criticalPath = cp;
614 i = corridor->parent;
615 }
616 }
617
618 static void CapCorridor(
619 MapBuilder *mb, const CArray *areas, const BSPArea *a,
620 const struct vec2i end, const struct vec2i dAcross,
621 const struct vec2i dAlong);
622 static struct vec2i CorridorDAlong(const BSPArea *a);
623 static struct vec2i CorridorDAcross(const BSPArea *a);
624 static void FillCorridors(MapBuilder *mb, const CArray *areas)
625 {
626 CA_FOREACH(const BSPArea, a, *areas)
627 // Iterate in reverse order so we cap child corridors first,
628 // to correctly shrink corridor ends
629 a = CArrayGet(areas, areas->size - _ca_index - 1);
630 if (!a->isCorridor)
631 {
632 continue;
633 }
634 RECT_FOREACH(a->r)
635 MapBuilderSetTile(mb, _v, &mb->mission->u.Interior.TileClasses.Floor);
636 RECT_FOREACH_END()
637 // Check ends of corridors - cap or place door
638 const struct vec2i end1 = a->r.Pos;
639 const struct vec2i end2 =
640 svec2i_add(end1, svec2i_subtract(a->r.Size, svec2i_one()));
641 CapCorridor(mb, areas, a, end1, CorridorDAcross(a), CorridorDAlong(a));
642 CapCorridor(
643 mb, areas, a, end2, svec2i_scale(CorridorDAcross(a), -1),
644 svec2i_scale(CorridorDAlong(a), -1));
645 CA_FOREACH_END()
646 }
647 static void CapCorridor(
648 MapBuilder *mb, const CArray *areas, const BSPArea *a,
649 const struct vec2i end, const struct vec2i dAcross,
650 const struct vec2i dAlong)
651 {
652 // Check ends of corridor - if outside map, or next to much older corridor,
653 // block off with wall
654 const struct vec2i outside = svec2i_subtract(end, dAlong);
655 const TileClass *capTile = &mb->mission->u.Interior.TileClasses.Floor;
656 if (!MapIsTileIn(mb->Map, outside))
657 {
658 capTile = &mb->mission->u.Interior.TileClasses.Wall;
659 }
660 else
661 {
662 CA_FOREACH(const BSPArea, a2, *areas)
663 if (Rect2iIsInside(a2->r, outside))
664 {
665 if (a->level - a2->level > CORRIDOR_LEVEL_DIFF_BLOCK)
666 {
667 capTile = &mb->mission->u.Interior.TileClasses.Wall;
668 }
669 else if (mb->mission->u.Interior.Doors.Enabled)
670 {
671 capTile = &mb->mission->u.Interior.TileClasses.Door;
672 }
673 break;
674 }
675 CA_FOREACH_END()
676 }
677 for (int j = 0;; j++)
678 {
679 const struct vec2i endJ =
680 svec2i_add(end, svec2i_multiply(dAlong, svec2i(j, j)));
681 // Keep filling the end of the corridor unless there's
682 // a door in the way
683 if (MapBuilderGetTile(
684 mb, svec2i_add(endJ, svec2i_multiply(dAcross, svec2i(-1, -1))))
685 ->Type != TILE_CLASS_WALL ||
686 MapBuilderGetTile(
687 mb, svec2i_add(
688 endJ, svec2i_multiply(
689 dAcross,
690 svec2i(
691 mb->mission->u.Interior.CorridorWidth,
692 mb->mission->u.Interior.CorridorWidth))))
693 ->Type != TILE_CLASS_WALL)
694 {
695 break;
696 }
697 for (int i = 0; i < mb->mission->u.Interior.CorridorWidth; i++)
698 {
699 MapBuilderSetTile(
700 mb, svec2i_add(endJ, svec2i_multiply(dAcross, svec2i(i, i))),
701 capTile);
702 }
703 if (capTile->Type != TILE_CLASS_WALL)
704 {
705 break;
706 }
707 }
708 }
709 static struct vec2i CorridorDAlong(const BSPArea *a)
710 {
711 // Unit vector going along the corridor
712 return a->horizontal ? svec2i(1, 0) : svec2i(0, 1);
713 }
714 static struct vec2i CorridorDAcross(const BSPArea *a)
715 {
716 // Unit vector going across the corridor
717 return a->horizontal ? svec2i(0, 1) : svec2i(1, 0);
718 }
719
720 static int FindAdjacentCriticalPath(
721 const CArray *areas, const Adjacency *am, const CArray *dCriticalPath,
722 const int idx);
723 static CArray CalcDistanceToCriticalPath(
724 const CArray *areas, const Adjacency *am)
725 {
726 CArray dCriticalPath;
727 CArrayInitFillZero(&dCriticalPath, sizeof(int), areas->size);
728 CA_FOREACH(const BSPArea, a, *areas)
729 if (a->criticalPath != CRIT_PATH_NONE)
730 {
731 const int d = 1;
732 CArraySet(&dCriticalPath, _ca_index, &d);
733 }
734 CA_FOREACH_END()
735 int newConnections;
736 do
737 {
738 newConnections = 0;
739 CA_FOREACH(const BSPArea, a, *areas)
740 if (*(int *)CArrayGet(&dCriticalPath, _ca_index) > 0)
741 {
742 continue;
743 }
744 if (!a->isCorridor && !BSPAreaIsLeaf(a))
745 {
746 continue;
747 }
748 const int d =
749 FindAdjacentCriticalPath(areas, am, &dCriticalPath, _ca_index);
750 if (d > 0)
751 {
752 CArraySet(&dCriticalPath, _ca_index, &d);
753 newConnections++;
754 }
755 CA_FOREACH_END()
756 } while (newConnections > 0);
757 return dCriticalPath;
758 }
759 static int FindAdjacentCriticalPath(
760 const CArray *areas, const Adjacency *am, const CArray *dCriticalPath,
761 const int idx)
762 {
763 for (int i = 0; i < (int)areas->size; i++)
764 {
765 const int *d = CArrayGet(dCriticalPath, i);
766 if (*d == 0)
767 {
768 continue;
769 }
770 if (AdjacencyIsConnected(am, idx, i))
771 {
772 return *d + 1;
773 }
774 }
775 return 0;
776 }
777
778 static CArray FindRoomsFurthestFromCriticalPath(
779 const CArray *areas, const Adjacency *am, const CArray *dCriticalPath,
780 const int fromIdx);
781 static void AddLockedRooms(
782 MapBuilder *mb, const CArray *areas, const CArray *rooms,
783 const uint16_t accessMask);
784 static void PlaceKey(
785 MapBuilder *mb, const CArray *areas, const Adjacency *am,
786 CArray *dCriticalPath, const int idx, const int keyIndex,
787 const CriticalPath cp);
788 static void PlaceKeys(
789 MapBuilder *mb, const CArray *areas, const Adjacency *am,
790 CArray *dCriticalPath)
791 {
792 CArray lockedRoomCandidates;
793 CArrayInit(&lockedRoomCandidates, sizeof(int));
794 CA_FOREACH(const BSPArea, a, *areas)
795 if (!a->isCorridor || a->criticalPath == CRIT_PATH_NONE || _ca_index == 0)
796 {
797 continue;
798 }
799
800 // If we're on the right critical path, we need to lock the child corridor
801 // that is on the critical path instead
802 if (a->criticalPath == CRIT_PATH_RIGHT)
803 {
804 const BSPArea *child1 = CArrayGet(areas, a->child1);
805 const BSPArea *child2 = CArrayGet(areas, a->child2);
806 a = child1->criticalPath != CRIT_PATH_NONE ? child1 : child2;
807 if (!a->isCorridor)
808 {
809 continue;
810 }
811 }
812 CArrayPushBack(&lockedRoomCandidates, &_ca_index);
813 CA_FOREACH_END()
814
815 // Randomly choose rooms to lock
816 while (lockedRoomCandidates.size > KEY_COUNT)
817 {
818 CArrayDelete(
819 &lockedRoomCandidates, RAND_INT(0, lockedRoomCandidates.size));
820 }
821
822 CA_FOREACH(const int, idx, lockedRoomCandidates)
823 const BSPArea *a = CArrayGet(areas, *idx);
824 if (a->criticalPath == CRIT_PATH_RIGHT)
825 {
826 const BSPArea *child1 = CArrayGet(areas, a->child1);
827 const BSPArea *child2 = CArrayGet(areas, a->child2);
828 a = child1->criticalPath != CRIT_PATH_NONE ? child1 : child2;
829 }
830 // Lock corridor
831 const uint16_t accessMask = GetAccessMask(_ca_index);
832 const struct vec2i end1 = a->r.Pos;
833 const struct vec2i end2 =
834 svec2i_add(end1, svec2i_subtract(a->r.Size, svec2i_one()));
835 for (int i = 0; i < mb->mission->u.Interior.CorridorWidth; i++)
836 {
837 if (MapBuilderGetTile(mb, end1)->Type != TILE_CLASS_WALL)
838 {
839 MapSetRoomAccessMask(
840 mb,
841 Rect2iNew(
842 svec2i_add(
843 end1,
844 svec2i_multiply(CorridorDAcross(a), svec2i(i, i))),
845 svec2i_one()),
846 accessMask);
847 }
848 if (MapBuilderGetTile(mb, end2)->Type != TILE_CLASS_WALL)
849 {
850 MapSetRoomAccessMask(
851 mb,
852 Rect2iNew(
853 svec2i_add(
854 end2,
855 svec2i_multiply(CorridorDAcross(a), svec2i(-i, -i))),
856 svec2i_one()),
857 accessMask);
858 }
859 }
860
861 // Place key in a child room before the locked corridor, but far away
862 CArray furthestChildren =
863 FindRoomsFurthestFromCriticalPath(areas, am, dCriticalPath, *idx);
864 CArrayShuffle(&furthestChildren);
865 CASSERT(furthestChildren.size > 0, "Cannot find child for locked street");
866
867 const int child = *(int *)CArrayGet(&furthestChildren, 0);
868 PlaceKey(mb, areas, am, dCriticalPath, child, _ca_index, a->criticalPath);
869
870 // If there are more children, mark some of them as locked rooms
871 // So that special objective items can be placed there
872 AddLockedRooms(mb, areas, &furthestChildren, accessMask);
873
874 CArrayTerminate(&furthestChildren);
875 CA_FOREACH_END()
876 }
877 static CArray FindRoomsFurthestFromCriticalPath(
878 const CArray *areas, const Adjacency *am, const CArray *dCriticalPath,
879 const int fromIdx)
880 {
881 CArray furthest;
882 CArrayInit(&furthest, sizeof(int));
883 CArrayPushBack(&furthest, &fromIdx);
884 CA_FOREACH(const int, idxp, furthest)
885 const int idx = *idxp;
886 bool hasChildren = false;
887 for (int i = 0; i < (int)areas->size; i++)
888 {
889 if (i == idx || !AdjacencyIsConnected(am, idx, i) ||
890 *(int *)CArrayGet(dCriticalPath, i) <=
891 *(int *)CArrayGet(dCriticalPath, idx))
892 {
893 continue;
894 }
895 CArrayPushBack(&furthest, &i);
896 hasChildren = true;
897 }
898 const BSPArea *a = CArrayGet(areas, idx);
899 if (hasChildren || a->criticalPath != CRIT_PATH_NONE)
900 {
901 CArrayDelete(&furthest, _ca_index);
902 _ca_index--;
903 }
904 CA_FOREACH_END()
905 return furthest;
906 }
907 static void AddLockedRooms(
908 MapBuilder *mb, const CArray *areas, const CArray *rooms,
909 const uint16_t accessMask)
910 {
911 CA_FOREACH(const int, idx, *rooms)
912 if (_ca_index == 0)
913 {
914 continue;
915 }
916 if (RAND_BOOL())
917 {
918 const BSPArea *room = CArrayGet(areas, *idx);
919 MapSetRoomAccessMask(
920 mb,
921 Rect2iNew(
922 svec2i_add(room->r.Pos, svec2i_one()),
923 svec2i_subtract(room->r.Size, svec2i(2, 2))),
924 accessMask);
925 }
926 CA_FOREACH_END()
927 }
928 static void PlaceKey(
929 MapBuilder *mb, const CArray *areas, const Adjacency *am,
930 CArray *dCriticalPath, const int idx, const int keyIndex,
931 const CriticalPath cp)
932 {
933 BSPArea *room = CArrayGet(areas, idx);
934 const struct vec2i keyPos =
935 svec2i_add(room->r.Pos, svec2i_divide(room->r.Size, svec2i(2, 2)));
936 MapPlaceKey(mb, keyPos, keyIndex);
937 // Prevent items being placed over the key
938 MapBuilderSetLeaveFree(mb, keyPos, true);
939 // Add room to critical path to avoid room walls here
940 room->criticalPath = cp;
941 int dNext = *(int *)CArrayGet(dCriticalPath, idx);
942 int d = 1;
943 CArraySet(dCriticalPath, idx, &d);
944 // Path find until we're back on the critical path, marking the path
945 // as critical too
946 int idxNext = idx;
947 for (dNext--; dNext > 1; dNext--)
948 {
949 CA_FOREACH(BSPArea, a, *areas)
950 if (AdjacencyIsConnected(am, _ca_index, idxNext) &&
951 *(int *)CArrayGet(dCriticalPath, _ca_index) == dNext)
952 {
953 idxNext = _ca_index;
954 a->criticalPath = cp;
955 CArraySet(dCriticalPath, idxNext, &d);
956 break;
957 }
958 CA_FOREACH_END()
959 }
960 }
961 static bool AllTilesAroundUnwalkable(
962 const MapBuilder *mb, const struct vec2i v)
963 {
964 RECT_FOREACH(Rect2iNew(svec2i_subtract(v, svec2i_one()), svec2i(3, 3)))
965 const TileClass *tc = MapBuilderGetTile(mb, _v);
966 if (tc != NULL && tc->canWalk)
967 {
968 return false;
969 }
970 RECT_FOREACH_END()
971 return true;
972 }
973 static void AddPillars(
974 MapBuilder *mb, const CArray *areas, Adjacency *am,
975 const CArray *dCriticalPath)
976 {
977 // Repeatedly add pillars because more candidates can be found
978 // after placing some
979 for (int count = 0; count < mb->mission->u.Interior.Pillars.Count;)
980 {
981 // Find all the children furthest away from the critical path
982 CArray allChildren;
983 memset(&allChildren, 0, sizeof allChildren);
984 CA_FOREACH(const int, d, *dCriticalPath)
985 if (*d == 1)
986 {
987 // Is on critical path; find furthest children
988 CArray furthestChildren = FindRoomsFurthestFromCriticalPath(
989 areas, am, dCriticalPath, _ca_index);
990 CArrayConcat(&allChildren, &furthestChildren);
991 CArrayTerminate(&furthestChildren);
992 // Sort and remove duplicates
993 qsort(
994 allChildren.data, allChildren.size, allChildren.elemSize,
995 CompareIntsDesc);
996 CArrayUnique(&allChildren, IntsEqual);
997 }
998 CA_FOREACH_END()
999 if (allChildren.size == 0)
1000 {
1001 break;
1002 }
1003 CArrayShuffle(&allChildren);
1004 CA_FOREACH(const int, idx, allChildren)
1005 if (count == mb->mission->u.Interior.Pillars.Count)
1006 {
1007 break;
1008 }
1009 const BSPArea *a = CArrayGet(areas, *idx);
1010 MapFillRect(
1011 mb, a->r, &mb->mission->u.Interior.TileClasses.Wall,
1012 &gTileNothing);
1013 MapSetRoomAccessMask(mb, a->r, 0);
1014 AdjacencyDisconnectAll(am, *idx);
1015 count++;
1016 CA_FOREACH_END()
1017 CArrayTerminate(&allChildren);
1018 }
1019
1020 // Clean up orphaned walls (walls with no surrounding walkable tiles)
1021 RECT_FOREACH(Rect2iNew(svec2i_zero(), mb->Map->Size))
1022 if (AllTilesAroundUnwalkable(mb, _v))
1023 {
1024 MapBuilderSetTile(mb, _v, &gTileNothing);
1025 }
1026 RECT_FOREACH_END()
1027 }
1028
1029 static void AddRoomWalls(MapBuilder *mb, const CArray *areas)
1030 {
1031 CA_FOREACH(const BSPArea, a, *areas)
1032 // Skip non-leaves
1033 if (!BSPAreaIsLeaf(a))
1034 {
1035 continue;
1036 }
1037
1038 if (a->criticalPath == CRIT_PATH_NONE)
1039 {
1040 MapMakeRoomWalls(
1041 mb, mb->mission->u.Interior.Rooms,
1042 &mb->mission->u.Interior.TileClasses.Wall, a->r);
1043 }
1044 CA_FOREACH_END()
1045 }