Debian Codegraph @

src/cdogs/map_cave.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2016-2019, 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_cave.h"
29
30 #include "algorithms.h"
31 #include "log.h"
32 #include "map_build.h"
33
34 static void CaveRep(MapBuilder *mb, const int r1, const int r2);
35 static void LinkDisconnectedAreas(MapBuilder *mb);
36 static void FixCorridors(MapBuilder *mb, const int corridorWidth);
37 static void PlaceSquares(MapBuilder *mb, const int squares);
38 static void PlaceRooms(MapBuilder *mb);
39 void MapCaveLoad(MapBuilder *mb)
40 {
41 // TODO: multiple tile types
42 MissionSetupTileClasses(mb->Map, &gPicManager, &mb->mission->u.Cave.TileClasses);
43
44 // Randomly set a percentage of the tiles as walls
45 for (int i = 0; i < mb->mission->u.Cave.FillPercent * mb->Map->Size.x *
46 mb->Map->Size.y / 100;
47 i++)
48 {
49 const struct vec2i pos =
50 svec2i(i % mb->Map->Size.x, i / mb->Map->Size.x);
51 MapBuilderSetTile(mb, pos, &mb->mission->u.Cave.TileClasses.Wall);
52 }
53 // Shuffle
54 CArrayShuffle(&mb->tiles);
55 // Repetitions
56 for (int i = 0; i < mb->mission->u.Cave.Repeat; i++)
57 {
58 CaveRep(mb, mb->mission->u.Cave.R1, mb->mission->u.Cave.R2);
59 }
60
61 LinkDisconnectedAreas(mb);
62
63 FixCorridors(mb, mb->mission->u.Cave.CorridorWidth);
64
65 PlaceSquares(mb, mb->mission->u.Cave.Squares);
66
67 PlaceRooms(mb);
68 }
69
70 // Perform one generation of cellular automata
71 // If the number of walls within 1 distance is at least R1, OR
72 // if the number of walls within 2 distance is at most R2, then the tile
73 // becomes a wall; otherwise it is a floor
74 static int CountWallsAround(
75 const MapBuilder *mb, const struct vec2i pos, const int d);
76 static void CaveRep(MapBuilder *mb, const int r1, const int r2)
77 {
78 CArray buf;
79 CArrayInitFill(
80 &buf, mb->tiles.elemSize, mb->tiles.size,
81 &mb->mission->u.Cave.TileClasses.Floor);
82 RECT_FOREACH(Rect2iNew(svec2i_zero(), mb->Map->Size))
83 TileClass *tile = CArrayGet(&buf, _i);
84 if (CountWallsAround(mb, _v, 1) >= r1 || CountWallsAround(mb, _v, 2) <= r2)
85 {
86 *tile = mb->mission->u.Cave.TileClasses.Wall;
87 }
88 else
89 {
90 *tile = mb->mission->u.Cave.TileClasses.Floor;
91 }
92 RECT_FOREACH_END()
93 RECT_FOREACH(Rect2iNew(svec2i_zero(), mb->Map->Size))
94 MapBuilderSetTile(mb, _v, CArrayGet(&buf, _i));
95 RECT_FOREACH_END()
96 CArrayTerminate(&buf);
97 }
98 static int CountWallsAround(
99 const MapBuilder *mb, const struct vec2i pos, const int d)
100 {
101 int c = 0;
102 RECT_FOREACH(Rect2iNew(
103 svec2i_subtract(pos, svec2i(d, d)), svec2i(2 * d + 1, 2 * d + 1)))
104 // Also count edge of maps
105 if (!MapIsTileIn(mb->Map, _v) ||
106 MapBuilderGetTile(mb, _v)->Type == TILE_CLASS_WALL)
107 {
108 c++;
109 }
110 RECT_FOREACH_END()
111 return c;
112 }
113
114 static void MapFloodFill(
115 CArray *fl, const struct vec2i size, const int idx, const int elem);
116 static void AddCorridor(
117 MapBuilder *mb, const struct vec2i v1, const struct vec2i v2,
118 const struct vec2i dInit, const TileClass *tile);
119 static void LinkDisconnectedAreas(MapBuilder *mb)
120 {
121 // Use flood fill to identify disconnected areas
122 CArray fl;
123 CArrayInitFillZero(&fl, sizeof(int), mb->tiles.size);
124 // First copy across the wall tiles (as -1)
125 RECT_FOREACH(Rect2iNew(svec2i_zero(), mb->Map->Size))
126 const TileClass *tile = MapBuilderGetTile(mb, _v);
127 if (tile->Type == TILE_CLASS_WALL)
128 {
129 *(int *)CArrayGet(&fl, _i) = -1;
130 }
131 RECT_FOREACH_END()
132 // Then perform flood fill conditionally on the flood layer
133 int idx = 1;
134 CA_FOREACH(int, i, fl)
135 if (*i == 0)
136 {
137 MapFloodFill(&fl, mb->Map->Size, _ca_index, idx);
138 idx++;
139 }
140 CA_FOREACH_END()
141
142 const int numAreas = idx - 1;
143 // Connect the disconnected areas, first to second, second to third etc.
144 // Select random tile from each area, using index shuffle
145 CArray areaTiles;
146 CArrayInit(&areaTiles, sizeof(int));
147 CA_FOREACH(int, i, fl)
148 UNUSED(i);
149 CArrayPushBack(&areaTiles, &_ca_index);
150 CA_FOREACH_END()
151 CArrayShuffle(&areaTiles);
152 CArray areaStarts;
153 CArrayInitFillZero(&areaStarts, sizeof(int), numAreas);
154 CA_FOREACH(int, areaIdx, areaTiles)
155 const int tile = *(int *)CArrayGet(&fl, *areaIdx) - 1;
156 if (tile >= 0 && tile < numAreas)
157 {
158 int *areaStart = CArrayGet(&areaStarts, tile);
159 if (*areaStart == 0)
160 {
161 *areaStart = *areaIdx;
162 }
163 }
164 CA_FOREACH_END()
165 CArrayTerminate(&fl);
166 CArrayTerminate(&areaTiles);
167 // Connect consecutive pairs of tiles
168 for (int i = 0; i < (int)areaStarts.size - 1; i++)
169 {
170 const int *a1 = CArrayGet(&areaStarts, i);
171 const int *a2 = CArrayGet(&areaStarts, i + 1);
172 const struct vec2i v1 =
173 svec2i(*a1 % mb->Map->Size.x, *a1 / mb->Map->Size.x);
174 const struct vec2i v2 =
175 svec2i(*a2 % mb->Map->Size.x, *a2 / mb->Map->Size.x);
176 const struct vec2i delta = svec2i(abs(v1.x - v2.x), abs(v1.y - v2.y));
177 const int dx = delta.x > delta.y ? 1 : 0;
178 const int dy = 1 - dx;
179 AddCorridor(
180 mb, v1, v2, svec2i(dx, dy),
181 &mb->mission->u.Cave.TileClasses.Floor);
182 }
183 CArrayTerminate(&areaStarts);
184 }
185
186 static void MapFloodFill(
187 CArray *fl, const struct vec2i size, const int idx, const int elem)
188 {
189 CArray indices;
190 CArrayInit(&indices, sizeof(int));
191 CArrayPushBack(&indices, &idx);
192 const int floodTile = *(int *)CArrayGet(fl, idx);
193 *(int *)CArrayGet(fl, idx) = elem;
194 CA_FOREACH(int, i, indices)
195 const int x = *i % size.x;
196 const int y = *i / size.x;
197 int next;
198 // top
199 next = (y - 1) * size.x + x;
200 if (y > 0 && *(int *)CArrayGet(fl, next) == floodTile)
201 {
202 CArrayPushBack(&indices, &next);
203 *(int *)CArrayGet(fl, next) = elem;
204 }
205 // bottom
206 next = (y + 1) * size.x + x;
207 if (y < size.y - 1 && *(int *)CArrayGet(fl, next) == floodTile)
208 {
209 CArrayPushBack(&indices, &next);
210 *(int *)CArrayGet(fl, next) = elem;
211 }
212 // left
213 next = y * size.x + x - 1;
214 if (x > 0 && *(int *)CArrayGet(fl, next) == floodTile)
215 {
216 CArrayPushBack(&indices, &next);
217 *(int *)CArrayGet(fl, next) = elem;
218 }
219 // right
220 next = y * size.x + x + 1;
221 if (x < size.x - 1 && *(int *)CArrayGet(fl, next) == floodTile)
222 {
223 CArrayPushBack(&indices, &next);
224 *(int *)CArrayGet(fl, next) = elem;
225 }
226 CA_FOREACH_END()
227 CArrayTerminate(&indices);
228 }
229
230 // Add an S-shaped corridor from one point to another, filling it with a
231 // certain tile value. The corridor starts in a specific direction d, then
232 // makes a turn in the middle, then turns back to the original direction.
233 static void AddCorridor(
234 MapBuilder *mb, const struct vec2i v1, const struct vec2i v2,
235 const struct vec2i dInit, const TileClass *tile)
236 {
237 struct vec2i dAlt;
238 // Location of the turn
239 struct vec2i half;
240 struct vec2i start = v1;
241 struct vec2i end = v2;
242 struct vec2i d = dInit;
243 if (d.x > 0)
244 {
245 // horizontal
246 d = svec2i(1, 0);
247 if (start.x > end.x)
248 {
249 // Swap
250 const struct vec2i tmp = start;
251 start = end;
252 end = tmp;
253 }
254 dAlt = svec2i(0, 1);
255 half = svec2i((end.x - start.x) / 2 + start.x, end.y + 1);
256 if (end.y < start.y)
257 {
258 dAlt.y = -1;
259 half.y = end.y - 1;
260 }
261 }
262 else
263 {
264 // vertical
265 d = svec2i(0, 1);
266 if (start.y > end.y)
267 {
268 // Swap
269 const struct vec2i tmp = start;
270 start = end;
271 end = tmp;
272 }
273 dAlt = svec2i(1, 0);
274 half = svec2i(end.x + 1, (end.y - start.y) / 2 + start.y);
275 if (end.x < start.x)
276 {
277 dAlt.x = -1;
278 half.x = end.x - 1;
279 }
280 }
281 // Initial direction
282 struct vec2i v = start;
283 for (; v.x != half.x && v.y != half.y; v = svec2i_add(v, d))
284 {
285 MapBuilderSetTile(mb, v, tile);
286 }
287 // Turn
288 for (; v.x != end.x && v.y != end.y; v = svec2i_add(v, dAlt))
289 {
290 MapBuilderSetTile(mb, v, tile);
291 }
292 // Finish
293 for (; v.x != end.x || v.y != end.y; v = svec2i_add(v, d))
294 {
295 MapBuilderSetTile(mb, v, tile);
296 }
297 MapBuilderSetTile(mb, v, tile);
298 }
299
300 static bool CheckCorridorsAroundTile(
301 const MapBuilder *mb, const int corridorWidth, const struct vec2i v);
302 // Make sure corridors are wide enough
303 static void FixCorridors(MapBuilder *mb, const int corridorWidth)
304 {
305 // Find all instances where a corridor is too narrow
306 // Do this by drawing a line from each wall tile to its surrounding square
307 // area, and that this line of tiles that each wall tile is either
308 // - blocked by a neighbouring wall tile, or
309 // - only consists of floor tiles
310 // If not, then replace that wall with a floor tile
311 struct vec2i v;
312 for (v.y = corridorWidth; v.y < mb->Map->Size.y - corridorWidth; v.y++)
313 {
314 for (v.x = corridorWidth; v.x < mb->Map->Size.x - corridorWidth; v.x++)
315 {
316 const TileClass *tile = MapBuilderGetTile(mb, v);
317 // Make sure that the tile is a wall
318 if (tile->Type != TILE_CLASS_WALL)
319 {
320 continue;
321 }
322 if (!CheckCorridorsAroundTile(mb, corridorWidth, v))
323 {
324 // Corridor checks failed; replace with a floor tile
325 MapBuilderSetTile(
326 mb, v, &mb->mission->u.Cave.TileClasses.Floor);
327 }
328 }
329 }
330 }
331 typedef struct
332 {
333 const MapBuilder *M;
334 int Counter;
335 bool IsFirstWall;
336 bool AreAllFloors;
337 } FixCorridorOnTileData;
338 static void FixCorridorOnTile(void *data, struct vec2i v);
339 static bool CheckCorridorsAroundTile(
340 const MapBuilder *mb, const int corridorWidth, const struct vec2i v)
341 {
342 AlgoLineDrawData data;
343 data.Draw = FixCorridorOnTile;
344 FixCorridorOnTileData onTileData;
345 onTileData.M = mb;
346 data.data = &onTileData;
347 struct vec2i v1;
348 for (v1.y = v.y - corridorWidth; v1.y <= v.y + corridorWidth; v1.y++)
349 {
350 for (v1.x = v.x - corridorWidth; v1.x <= v.x + corridorWidth; v1.x++)
351 {
352 // only draw out to edges
353 if (v1.y != v.y - corridorWidth && v1.y != v.y + corridorWidth &&
354 v1.x != v.x - corridorWidth && v1.x != v.x + corridorWidth)
355 {
356 continue;
357 }
358 onTileData.Counter = 0;
359 onTileData.IsFirstWall = false;
360 onTileData.AreAllFloors = true;
361 BresenhamLineDraw(v, v1, &data);
362 if (!onTileData.IsFirstWall && !onTileData.AreAllFloors)
363 {
364 return false;
365 }
366 }
367 }
368 return true;
369 }
370 static void FixCorridorOnTile(void *data, struct vec2i v)
371 {
372 FixCorridorOnTileData *onTileData = data;
373 if (onTileData->Counter == 1)
374 {
375 // This is the first tile after the starting tile
376 // If this tile is a wall, result is good, and we don't care about
377 // the rest of the tiles anymore
378 onTileData->IsFirstWall =
379 MapBuilderGetTile(onTileData->M, v)->Type == TILE_CLASS_WALL;
380 }
381 if (onTileData->Counter > 0)
382 {
383 if (MapBuilderGetTile(onTileData->M, v)->Type != TILE_CLASS_FLOOR)
384 {
385 onTileData->AreAllFloors = false;
386 }
387 }
388 onTileData->Counter++;
389 }
390
391 static bool MapIsAreaClearForCaveSquare(
392 const MapBuilder *mb, const struct vec2i pos, const struct vec2i size);
393 static void PlaceSquares(MapBuilder *mb, const int squares)
394 {
395 // Place empty square areas on the map
396 // This can only be done if at least one tile in the square is a floor type
397 int count = 0;
398 for (int i = 0; i < 1000 && count < squares; i++)
399 {
400 const struct vec2i v = MapGetRandomTile(mb->Map);
401 const struct vec2i size = svec2i(rand() % 9 + 8, rand() % 9 + 8);
402 if (!MapIsAreaClearForCaveSquare(mb, v, size))
403 {
404 continue;
405 }
406 MapFillRect(
407 mb, Rect2iNew(v, size), &mb->mission->u.Cave.TileClasses.Floor, &mb->mission->u.Cave.TileClasses.Floor);
408 count++;
409 }
410 }
411 static bool MapIsAreaClearForCaveSquare(
412 const MapBuilder *mb, const struct vec2i pos, const struct vec2i size)
413 {
414 if (!MapIsAreaInside(mb->Map, pos, size))
415 {
416 return false;
417 }
418
419 // For area to be clear, it must have:
420 // - At least one floor tile
421 // - No square tiles
422 struct vec2i v;
423 bool hasFloor = false;
424 for (v.y = pos.y; v.y < pos.y + size.y; v.y++)
425 {
426 for (v.x = pos.x; v.x < pos.x + size.x; v.x++)
427 {
428 const TileClass *tile = MapBuilderGetTile(mb, v);
429 switch (tile->Type)
430 {
431 case TILE_CLASS_FLOOR:
432 hasFloor = true;
433 break;
434 case TILE_CLASS_WALL:
435 break;
436 default:
437 // Any other tile type is disallowed
438 return false;
439 }
440 }
441 }
442 return hasFloor;
443 }
444
445 static bool MapIsAreaClearForCaveRoom(const MapBuilder *mb, const Rect2i room);
446 static void MapBuildRoom(MapBuilder *mb, const Rect2i room);
447 static void PlaceRooms(MapBuilder *mb)
448 {
449 CArray rooms; // of Rect2i
450 CArrayInit(&rooms, sizeof(Rect2i));
451 for (int i = 0;
452 i < 1000 && (int)rooms.size < mb->mission->u.Cave.Rooms.Count; i++)
453 {
454 Rect2i room;
455 room.Pos = MapGetRandomTile(mb->Map);
456 room.Size = MapGetRoomSize(mb->mission->u.Cave.Rooms, 1);
457 if (!MapIsAreaClearForCaveRoom(mb, room))
458 {
459 continue;
460 }
461 MapBuildRoom(mb, room);
462 CArrayPushBack(&rooms, &room);
463 LOG(LM_MAP, LL_TRACE, "Room %d, %d (%dx%d)", room.Pos.x, room.Pos.y,
464 room.Size.x, room.Size.y);
465 }
466 // Set keys for rooms
467 if (AreKeysAllowed(mb->mode) &&
468 mb->mission->u.Cave.DoorsEnabled)
469 {
470 while (rooms.size > 0)
471 {
472 // generate an access level for this room
473 const uint16_t accessMask =
474 GenerateAccessMask(&mb->Map->keyAccessCount);
475 MapSetRoomAccessMaskOverlap(mb, &rooms, accessMask);
476 }
477 }
478 CArrayTerminate(&rooms);
479 }
480
481 static bool CaveRoomInsideOk(const MapBuilder *mb, const struct vec2i v);
482 static bool CaveRoomOutsideOk(const MapBuilder *mb, const struct vec2i v);
483 static bool MapIsAreaClearForCaveRoom(const MapBuilder *mb, const Rect2i room)
484 {
485 if (!MapIsAreaInside(mb->Map, room.Pos, room.Size))
486 {
487 return false;
488 }
489
490 // For area to be clear, it must have:
491 // - Area has at least one floor tile
492 // - Can only contain floor, wall or room tiles
493 // - Not be entirely surrounded by walls
494 bool hasFloor = false;
495 bool hasFloorAroundEdge = false;
496 bool isOverlapRoom = false;
497 RECT_FOREACH(room)
498 const TileClass *tile = MapBuilderGetTile(mb, _v);
499 if (tile->Type == TILE_CLASS_FLOOR && !tile->IsRoom)
500 {
501 hasFloor = true;
502 if (Rect2iIsAtEdge(room, _v))
503 {
504 hasFloorAroundEdge = true;
505 }
506 }
507 else if (tile->IsRoom || tile->Type == TILE_CLASS_DOOR)
508 {
509 isOverlapRoom = true;
510 }
511 else if (tile->Type != TILE_CLASS_WALL)
512 {
513 // Any other tile type is disallowed
514 return false;
515 }
516 RECT_FOREACH_END()
517 if (!hasFloor || !hasFloorAroundEdge)
518 {
519 return false;
520 }
521
522 // For edges:
523 // - Either the edge is a wall, or
524 // - Edge is floor/room, and outside is floor/room/square
525 // This is to prevent rooms from cutting off areas of the map
526 RECT_FOREACH(room)
527 if (!Rect2iIsAtEdge(room, _v))
528 {
529 continue;
530 }
531 const bool isTop = _v.y == room.Pos.y;
532 const bool isBottom = _v.y == room.Pos.y + room.Size.y - 1;
533 const bool isLeft = _v.x == room.Pos.x;
534 const bool isRight = _v.x == room.Pos.x + room.Size.x - 1;
535 const struct vec2i outside = svec2i(
536 isLeft ? room.Pos.x - 1 : (isRight ? room.Pos.x + room.Size.x : _v.x),
537 isTop ? room.Pos.y - 1 : (isBottom ? room.Pos.y + room.Size.y : _v.y));
538 const struct vec2i outsideX =
539 svec2i((isLeft || isRight) ? outside.x : _v.x, _v.y);
540 const struct vec2i outsideY =
541 svec2i(_v.x, (isTop || isBottom) ? outside.y : _v.y);
542 const TileClass *tile = MapBuilderGetTile(mb, _v);
543 switch (tile->Type)
544 {
545 case TILE_CLASS_DOOR:
546 case TILE_CLASS_WALL: // fallthrough
547 // Note: also need to check outside to see if we overlap
548 // but just along the edge
549 if (CaveRoomInsideOk(mb, outside) || CaveRoomInsideOk(mb, outsideX) ||
550 CaveRoomInsideOk(mb, outsideY))
551 {
552 isOverlapRoom = true;
553 }
554 break;
555 case TILE_CLASS_FLOOR:
556 // Check outside tiles
557 if (!CaveRoomOutsideOk(mb, outside) ||
558 !CaveRoomOutsideOk(mb, outsideX) ||
559 !CaveRoomOutsideOk(mb, outsideY))
560 {
561 return false;
562 }
563 break;
564 default:
565 CASSERT(false, "unexpected tile type");
566 return false;
567 }
568 RECT_FOREACH_END()
569
570 // Check if room overlaps with another room and the overlap is valid
571 if (isOverlapRoom)
572 {
573 if (!mb->mission->u.Cave.Rooms.Overlap)
574 {
575 // Overlapping disabled
576 return false;
577 }
578 // Now check if the overlapping rooms will create a passage
579 // large enough
580 const int roomOverlapSize = MapGetRoomOverlapSize(mb, room, NULL);
581 if (roomOverlapSize < mb->mission->u.Cave.CorridorWidth)
582 {
583 return false;
584 }
585 }
586
587 return true;
588 }
589 static bool CaveRoomInsideOk(const MapBuilder *mb, const struct vec2i v)
590 {
591 const TileClass *t = MapBuilderGetTile(mb, v);
592 return t != NULL && t->IsRoom;
593 }
594 static bool CaveRoomOutsideOk(const MapBuilder *mb, const struct vec2i v)
595 {
596 const TileClass *t = MapBuilderGetTile(mb, v);
597 return t != NULL && t->Type == TILE_CLASS_FLOOR;
598 }
599
600 static void MapBuildRoom(MapBuilder *mb, const Rect2i room)
601 {
602 // For edges, any tile that is next to a floor must be turned into
603 // a door, unless it was a corner - then it must be a wall
604 // This is to prevent generating inaccessible rooms, or blocking off
605 // sections of the map
606 // TODO: if this is a locked room, can still cause the level to be
607 // blocked off
608 RECT_FOREACH(room)
609 if (!Rect2iIsAtEdge(room, _v))
610 {
611 continue;
612 }
613
614 const bool isTop = _v.y == room.Pos.y;
615 const bool isBottom = _v.y == room.Pos.y + room.Size.y - 1;
616 const bool isLeft = _v.x == room.Pos.x;
617 const bool isRight = _v.x == room.Pos.x + room.Size.x - 1;
618 const bool leftOrRightEdge = isLeft || isRight;
619 const bool topOrBottomEdge = isTop || isBottom;
620 if (leftOrRightEdge && topOrBottomEdge)
621 {
622 // corner
623 MapBuilderSetTile(mb, _v, &mb->mission->u.Cave.TileClasses.Wall);
624 }
625 else
626 {
627 const struct vec2i outside = svec2i(
628 isLeft ? room.Pos.x - 1
629 : (isRight ? room.Pos.x + room.Size.x : _v.x),
630 isTop ? room.Pos.y - 1
631 : (isBottom ? room.Pos.y + room.Size.y : _v.y));
632 const struct vec2i outsideX =
633 svec2i((isLeft || isRight) ? outside.x : _v.x, _v.y);
634 const struct vec2i outsideY =
635 svec2i(_v.x, (isTop || isBottom) ? outside.y : _v.y);
636 const bool atEdgeOfMap = _v.y == 0 || _v.y == mb->Map->Size.y - 1 ||
637 _v.x == 0 || _v.x == mb->Map->Size.x - 1;
638 const TileClass *tile = MapBuilderGetTile(mb, _v);
639 if (tile->Type == TILE_CLASS_DOOR)
640 {
641 if (!CaveRoomOutsideOk(mb, outside) &&
642 !CaveRoomOutsideOk(mb, outsideX) &&
643 !CaveRoomOutsideOk(mb, outsideY))
644 {
645 // This door would become a corner
646 MapBuilderSetTile(
647 mb, _v, &mb->mission->u.Cave.TileClasses.Wall);
648 }
649 }
650 else
651 {
652 // Check outside tiles
653 if (!atEdgeOfMap &&
654 (svec2i_is_equal(outside, _v) ||
655 CaveRoomOutsideOk(mb, outside)) &&
656 (svec2i_is_equal(outsideX, _v) ||
657 CaveRoomOutsideOk(mb, outsideX)) &&
658 (svec2i_is_equal(outsideY, _v) ||
659 CaveRoomOutsideOk(mb, outsideY)))
660 {
661 MapBuilderSetTile(
662 mb, _v,
663 mb->mission->u.Cave.DoorsEnabled
664 ? &mb->mission->u.Cave.TileClasses.Door
665 : &mb->mission->u.Cave.TileClasses.Room);
666 }
667 else
668 {
669 MapBuilderSetTile(
670 mb, _v, &mb->mission->u.Cave.TileClasses.Wall);
671 }
672 }
673 }
674 RECT_FOREACH_END()
675
676 MapMakeRoom(
677 mb, room.Pos, room.Size, false, &mb->mission->u.Cave.TileClasses.Wall,
678 &mb->mission->u.Cave.TileClasses.Room, true);
679
680 MapMakeRoomWalls(
681 mb, mb->mission->u.Cave.Rooms, &mb->mission->u.Cave.TileClasses.Wall, room);
682 }