Debian Codegraph @

src/cdogs/map_classic.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, 2018-2019, 2021 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_classic.h"
50
51 #include "campaigns.h"
52 #include "map_build.h"
53
54 static int MapTryBuildSquare(MapBuilder *mb);
55 static bool MapIsAreaClearForClassicRoom(
56 const MapBuilder *mb, const struct vec2i pos, const struct vec2i size,
57 const int pad, bool *isOverlapRoom, uint16_t *overlapAccess);
58 static void MapBuildRoom(
59 MapBuilder *mb, const struct vec2i pos, const struct vec2i size,
60 const int doorMin, const int doorMax, const bool hasKeys,
61 const bool isOverlapRoom, const uint16_t overlapAccess);
62 static bool MapTryBuildPillar(MapBuilder *mb, const int pad);
63 void MapClassicLoad(MapBuilder *mb)
64 {
65 // The classic random map generator randomly attempts to place
66 // a configured number of features on the map, in order:
67 // 1. "Squares", almost-square areas of empty floor
68 // 2. Rooms, rectangles of "room" squares surrounded by walls,
69 // some of which have doors
70 // 3. "Pillars", or rectangles of solid wall
71 // 4. Walls, single-thickness walls that sometimes bend and
72 // split
73 // All features are placed by randomly picking a position in
74 // the map and checking to see if it's possible to place the
75 // feature at that point, and repeating this process until
76 // either all features are placed or too many attempts have
77 // been done.
78 // Sometimes it's impossible to place features, either because
79 // they overlap with other incompatible features, or it may
80 // create inaccessible areas on the map.
81
82 // TODO: multiple tile types
83 MissionSetupTileClasses(mb->Map, &gPicManager, &mb->mission->u.Classic.TileClasses);
84
85 MapFillRect(
86 mb, Rect2iNew(svec2i_zero(), mb->mission->Size),
87 &mb->mission->u.Classic.TileClasses.Wall, &mb->mission->u.Classic.TileClasses.Floor);
88
89 const int pad = MAX(mb->mission->u.Classic.CorridorWidth, 1);
90
91 // place squares
92 int count = 0;
93 int i = 0;
94 while (i < 1000 && count < mb->mission->u.Classic.Squares)
95 {
96 if (MapTryBuildSquare(mb))
97 {
98 count++;
99 }
100 i++;
101 }
102
103 // place rooms
104 count = 0;
105 for (i = 0; i < 1000 && count < mb->mission->u.Classic.Rooms.Count; i++)
106 {
107 const struct vec2i v = MapGetRandomTile(mb->Map);
108 const int doorMin = CLAMP(mb->mission->u.Classic.Doors.Min, 1, 6);
109 const int doorMax =
110 CLAMP(mb->mission->u.Classic.Doors.Max, doorMin, 6);
111 const struct vec2i size =
112 MapGetRoomSize(mb->mission->u.Classic.Rooms, doorMin);
113 bool isOverlapRoom;
114 uint16_t overlapAccess;
115 if (!MapIsAreaClearForClassicRoom(
116 mb, v, size, pad, &isOverlapRoom, &overlapAccess))
117 {
118 continue;
119 }
120 MapBuildRoom(
121 mb, v, size, doorMin, doorMax,
122 AreKeysAllowed(mb->mode), isOverlapRoom,
123 overlapAccess);
124 count++;
125 }
126
127 // place pillars
128 count = 0;
129 i = 0;
130 while (i < 1000 && count < mb->mission->u.Classic.Pillars.Count)
131 {
132 if (MapTryBuildPillar(mb, pad))
133 {
134 count++;
135 }
136 i++;
137 }
138
139 // place walls
140 count = 0;
141 i = 0;
142 while (i < 1000 && count < mb->mission->u.Classic.Walls)
143 {
144 if (MapTryBuildWall(
145 mb, false, pad, mb->mission->u.Classic.WallLength,
146 &mb->mission->u.Classic.TileClasses.Wall, Rect2iZero()))
147 {
148 count++;
149 }
150 i++;
151 }
152 }
153
154 static int MapTryBuildSquare(MapBuilder *mb)
155 {
156 const struct vec2i v = MapGetRandomTile(mb->Map);
157 struct vec2i size = svec2i(rand() % 9 + 8, rand() % 9 + 8);
158 if (MapIsAreaClear(mb, v, size))
159 {
160 MapFillRect(
161 mb, Rect2iNew(v, size), &mb->mission->u.Cave.TileClasses.Floor, &mb->mission->u.Cave.TileClasses.Floor);
162 return 1;
163 }
164 return 0;
165 }
166
167 static bool MapIsAreaClearForClassicRoom(
168 const MapBuilder *mb, const struct vec2i pos, const struct vec2i size,
169 const int pad, bool *isOverlapRoom, uint16_t *overlapAccess)
170 {
171 struct vec2i clearPos = svec2i(pos.x - pad, pos.y - pad);
172 struct vec2i clearSize = svec2i(size.x + 2 * pad, size.y + 2 * pad);
173 bool isEdgeRoom = false;
174 *isOverlapRoom = false;
175 *overlapAccess = 0;
176
177 if (mb->mission->u.Classic.Rooms.Edge)
178 {
179 // Check if room is at edge; if so only check if clear inside edge
180 if (pos.x == 0 || pos.x == 1)
181 {
182 const int dx = 1 - clearPos.x;
183 clearPos.x += dx;
184 clearSize.x -= dx;
185 isEdgeRoom = true;
186 }
187 else if (
188 pos.x + size.x == mb->Map->Size.x - 2 ||
189 pos.x + size.x == mb->Map->Size.x - 1)
190 {
191 clearSize.x = mb->Map->Size.x - 1 - pos.x;
192 isEdgeRoom = true;
193 }
194 if (pos.y == 0 || pos.y == 1)
195 {
196 const int dy = 1 - clearPos.y;
197 clearPos.y += dy;
198 clearSize.y -= dy;
199 isEdgeRoom = true;
200 }
201 else if (
202 pos.y + size.y == mb->Map->Size.y - 2 ||
203 pos.y + size.y == mb->Map->Size.y - 1)
204 {
205 clearSize.y = mb->Map->Size.y - 1 - pos.y;
206 isEdgeRoom = true;
207 }
208 }
209 bool isClear = MapIsAreaClear(mb, clearPos, clearSize);
210 // Don't let rooms be both edge rooms and overlap rooms
211 // Otherwise dead pockets will be created
212 if (!isClear && !isEdgeRoom)
213 {
214 // If room overlap is enabled, check if it overlaps with a room
215 const bool isOverlap = mb->mission->u.Classic.Rooms.Overlap &&
216 MapIsAreaClearOrRoom(mb, clearPos, clearSize);
217 // Now check if the overlapping rooms will create a passage
218 // large enough
219 const int roomOverlapSize =
220 MapGetRoomOverlapSize(mb, Rect2iNew(pos, size), overlapAccess);
221 isClear = isOverlap &&
222 roomOverlapSize >= mb->mission->u.Classic.CorridorWidth;
223 *isOverlapRoom = true;
224 }
225 return isClear;
226 }
227
228 static void MapFindAvailableDoors(
229 const MapBuilder *mb, const struct vec2i pos, const struct vec2i size,
230 const int doorMin, bool doors[4]);
231
232 static void MapBuildRoom(
233 MapBuilder *mb, const struct vec2i pos, const struct vec2i size,
234 const int doorMin, const int doorMax, const bool hasKeys,
235 const bool isOverlapRoom, const uint16_t overlapAccess)
236 {
237 int doormask = rand() % 15 + 1;
238 bool doors[4];
239 int doorsUnplaced = 0;
240 int i;
241 uint16_t accessMask = 0;
242
243 MapMakeRoom(
244 mb, pos, size, true, &mb->mission->u.Classic.TileClasses.Wall,
245 &mb->mission->u.Classic.TileClasses.Room, true);
246 // Check which walls we can place doors
247 // If we cannot place doors, remember this and try to place them
248 // on other walls
249 // We cannot place doors on: the perimeter, and on overlaps
250 MapFindAvailableDoors(mb, pos, size, doorMin, doors);
251 // Try to place doors according to the random mask
252 // If we cannot place a door, remember this and try to place it
253 // on other doors
254 for (i = 0; i < 4; i++)
255 {
256 if ((doormask & (1 << i)) && !doors[i])
257 {
258 doorsUnplaced++;
259 }
260 }
261 for (i = 0; i < 4; i++)
262 {
263 if (!(doormask & (1 << i)))
264 {
265 if (doorsUnplaced == 0)
266 {
267 // If we don't need to place any doors,
268 // set this door to unavailable
269 doors[i] = false;
270 }
271 else if (doors[i])
272 {
273 // Otherwise, if it's possible to place a door here
274 // and we need to place doors, do so
275 doorsUnplaced--;
276 }
277 }
278 }
279
280 // Work out what access level (i.e. key) this room has
281 if (hasKeys && mb->mission->u.Classic.Doors.Enabled)
282 {
283 // If this room has overlapped another room, use the same
284 // access level as that room
285 if (isOverlapRoom)
286 {
287 accessMask = overlapAccess;
288 }
289 else
290 {
291 // Otherwise, generate an access level for this room
292 accessMask = GenerateAccessMask(&mb->Map->keyAccessCount);
293 }
294 }
295
296 const Rect2i r = Rect2iNew(pos, size);
297 MapPlaceDoors(
298 mb, r, mb->mission->u.Classic.Doors.Enabled, doors,
299 doorMin, doorMax, accessMask,
300 mb->mission->u.Classic.Doors.RandomPos,
301 &mb->mission->u.Classic.TileClasses.Door,
302 &mb->mission->u.Classic.TileClasses.Floor);
303
304 MapMakeRoomWalls(
305 mb, mb->mission->u.Classic.Rooms,
306 &mb->mission->u.Classic.TileClasses.Wall, r);
307 }
308
309 static bool MapTryBuildPillar(MapBuilder *mb, const int pad)
310 {
311 const int pillarMin = mb->mission->u.Classic.Pillars.Min;
312 const int pillarMax = mb->mission->u.Classic.Pillars.Max;
313 struct vec2i size = svec2i(
314 rand() % (pillarMax - pillarMin + 1) + pillarMin,
315 rand() % (pillarMax - pillarMin + 1) + pillarMin);
316 const struct vec2i pos = MapGetRandomTile(mb->Map);
317 struct vec2i clearPos = svec2i(pos.x - pad, pos.y - pad);
318 struct vec2i clearSize = svec2i(size.x + 2 * pad, size.y + 2 * pad);
319 int isEdge = 0;
320 int isClear = 0;
321
322 // Check if pillar is at edge; if so only check if clear inside edge
323 if (pos.x == 0 || pos.x == 1)
324 {
325 int dx = 1 - clearPos.x;
326 clearPos.x += dx;
327 clearSize.x -= dx;
328 isEdge = 1;
329 }
330 else if (
331 pos.x + size.x == mb->Map->Size.x - 2 ||
332 pos.x + size.x == mb->Map->Size.x - 1)
333 {
334 clearSize.x = mb->Map->Size.x - 1 - pos.x;
335 isEdge = 1;
336 }
337 if (pos.y == 0 || pos.y == 1)
338 {
339 int dy = 1 - clearPos.y;
340 clearPos.y += dy;
341 clearSize.y -= dy;
342 isEdge = 1;
343 }
344 else if (
345 pos.y + size.y == mb->Map->Size.y - 2 ||
346 pos.y + size.y == mb->Map->Size.y - 1)
347 {
348 clearSize.y = mb->Map->Size.y - 1 - pos.y;
349 isEdge = 1;
350 }
351
352 // Only place pillars if the area is totally clear,
353 // or if the pillar only overlaps one of the edge or another
354 // non-room wall
355 // This is to prevent dead pockets
356 isClear = MapIsAreaClear(mb, clearPos, clearSize);
357 if (!isClear && !isEdge && MapIsAreaClearOrWall(mb, clearPos, clearSize))
358 {
359 // Also check that the pillar does not overlap two pillars
360 isClear = MapIsLessThanTwoWallOverlaps(mb, clearPos, clearSize);
361 }
362 if (isClear)
363 {
364 MapFillRect(
365 mb, Rect2iNew(pos, size),
366 &mb->mission->u.Classic.TileClasses.Wall, &gTileNothing);
367 return true;
368 }
369 return false;
370 }
371
372 // Find the maximum door size for a wall
373 static int FindWallRun(
374 const MapBuilder *mb, const struct vec2i mid, const struct vec2i d,
375 const int len);
376 static void MapFindAvailableDoors(
377 const MapBuilder *mb, const struct vec2i pos, const struct vec2i size,
378 const int doorMin, bool doors[4])
379 {
380 for (int i = 0; i < 4; i++)
381 {
382 doors[i] = true;
383 }
384 // left
385 if (pos.x <= 1)
386 {
387 doors[0] = false;
388 }
389 else if (
390 FindWallRun(
391 mb, svec2i(pos.x, pos.y + 1), svec2i(0, 1), size.y - 2) <
392 doorMin)
393 {
394 doors[0] = false;
395 }
396 // right
397 if (pos.x + size.x >= mb->Map->Size.x - 2)
398 {
399 doors[1] = false;
400 }
401 else if (
402 FindWallRun(
403 mb, svec2i(pos.x + size.x - 1, pos.y + 1), svec2i(0, 1),
404 size.y - 2) < doorMin)
405 {
406 doors[1] = false;
407 }
408 // top
409 if (pos.y <= 1)
410 {
411 doors[2] = false;
412 }
413 else if (
414 FindWallRun(
415 mb, svec2i(pos.x + 1, pos.y), svec2i(1, 0), size.x - 2) <
416 doorMin)
417 {
418 doors[2] = false;
419 }
420 // bottom
421 if (pos.y >= mb->Map->Size.y - 2)
422 {
423 doors[3] = false;
424 }
425 else if (
426 FindWallRun(
427 mb, svec2i(pos.x + 1, pos.y + size.y - 1), svec2i(1, 0),
428 size.x - 2) < doorMin)
429 {
430 doors[3] = false;
431 }
432 }
433 static int FindWallRun(
434 const MapBuilder *mb, const struct vec2i mid, const struct vec2i d,
435 const int len)
436 {
437 int maxRun = 0;
438 int run = 0;
439 for (int i = 0; i < len; i++, run++)
440 {
441 // Check if this is a wall so we can add a door here
442 // Also check if the two tiles aside are not walls
443
444 // Note: we must look for runs
445 const struct vec2i v = svec2i_add(mid, svec2i_scale(d, (float)i));
446
447 if (MapBuilderGetTile(mb, v)->Type != TILE_CLASS_WALL ||
448 MapBuilderGetTile(mb, svec2i(v.x + d.y, v.y + d.x))->Type ==
449 TILE_CLASS_WALL ||
450 MapBuilderGetTile(mb, svec2i(v.x - d.y, v.y - d.x))->Type ==
451 TILE_CLASS_WALL)
452 {
453 if (maxRun < run)
454 {
455 maxRun = run;
456 }
457 run = 0;
458 }
459 }
460 return MAX(run, maxRun);
461 }