ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/rogue_layout.C
(Generate patch)

Comparing deliantra/server/random_maps/rogue_layout.C (file contents):
Revision 1.10 by root, Sun May 4 14:12:37 2008 UTC vs.
Revision 1.16 by root, Sun Jul 4 22:12:26 2010 UTC

1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) Crossfire Development Team (restored, original file without copyright notice)
6 *
7 * Deliantra is free software: you can redistribute it and/or modify it under
8 * the terms of the Affero GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the Affero GNU General Public License
18 * and the GNU General Public License along with this program. If not, see
19 * <http://www.gnu.org/licenses/>.
20 *
21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */
1 23
2/* generate a rogue/nethack-like layout */ 24/* generate a rogue/nethack-like maze */
3#include <global.h> 25#include <global.h>
4#include <random_map.h> 26#include <random_map.h>
5#include <math.h> 27#include <rproto.h>
6 28
7typedef struct 29typedef struct
8{ 30{
9 int x; 31 int x;
10 int y; /* coordinates of room centers */ 32 int y; /* coordinates of room centers */
16 int rtype; /* circle or rectangular */ 38 int rtype; /* circle or rectangular */
17} Room; 39} Room;
18 40
19static int roguelike_place_room (Room *rooms, int xsize, int ysize, int nrooms); 41static int roguelike_place_room (Room *rooms, int xsize, int ysize, int nrooms);
20static void roguelike_make_rooms (Room *rooms, char **maze, int options); 42static void roguelike_make_rooms (Room *rooms, char **maze, int options);
21static void roguelike_link_rooms (Room *rooms, char **maze, int xsize, int ysize); 43static void roguelike_link_rooms (Room *rooms, layout &maze);
22 44
23int 45int
24surround_check (char **layout, int i, int j, int Xsize, int Ysize) 46surround_check (layout &maze, int i, int j)
25{ 47{
26 /* 1 = wall to left, 48 /* 1 = wall to left,
27 2 = wall to right, 49 2 = wall to right,
28 4 = wall above 50 4 = wall above
29 8 = wall below */ 51 8 = wall below */
30 int surround_index = 0; 52 int surround_index = 0;
31 53
32 if ((i > 0) && (layout[i - 1][j] != 0 && layout[i - 1][j] != '.')) surround_index |= 1; 54 if ((i > 0) && (maze[i - 1][j] != 0 && maze[i - 1][j] != '.')) surround_index |= 1;
33 if ((i < Xsize - 1) && (layout[i + 1][j] != 0 && layout[i + 1][j] != '.')) surround_index |= 2; 55 if ((i < maze.w - 1) && (maze[i + 1][j] != 0 && maze[i + 1][j] != '.')) surround_index |= 2;
34 if ((j > 0) && (layout[i][j - 1] != 0 && layout[i][j - 1] != '.')) surround_index |= 4; 56 if ((j > 0) && (maze[i][j - 1] != 0 && maze[i][j - 1] != '.')) surround_index |= 4;
35 if ((j < Ysize - 1) && (layout[i][j + 1] != 0 && layout[i][j + 1] != '.')) surround_index |= 8; 57 if ((j < maze.h - 1) && (maze[i][j + 1] != 0 && maze[i][j + 1] != '.')) surround_index |= 8;
36 58
37 return surround_index; 59 return surround_index;
38} 60}
39 61
40/* actually make the layout: we work by a reduction process: 62/* actually make the maze: we work by a reduction process:
41 * first we make everything a wall, then we remove areas to make rooms 63 * first we make everything a wall, then we remove areas to make rooms
42 */ 64 */
43void 65void
44roguelike_layout_gen (Layout maze, int options) 66roguelike_layout_gen (layout &maze, int options)
45{ 67{
46 int i, j; 68 int i, j;
47 Room *walk; 69 Room *walk;
48 int nrooms = 0; 70 int nrooms = 0;
49 int tries = 0; 71 int tries = 0;
50 72
51 int xsize = maze->w; 73 int xsize = maze.w;
52 int ysize = maze->h; 74 int ysize = maze.h;
53 75
54 /* minimum room size is basically 5x5: if xsize/ysize is 76 /* minimum room size is basically 5x5: if xsize/ysize is
55 less than 3x that then hollow things out, stick in 77 less than 3x that then hollow things out, stick in
56 a stairsup and stairs down, and exit */ 78 a stairsup and stairs down, and exit */
57 if (xsize < 11 || ysize < 11) 79 if (xsize < 11 || ysize < 11)
58 { 80 {
59 maze->clear (); 81 maze.clear ();
60 maze->border (); 82 maze.border ();
61 83
62 maze[(xsize - 1) / 2][(ysize - 1) / 2 ] = '>'; 84 maze[(xsize - 1) / 2][(ysize - 1) / 2 ] = '>';
63 maze[(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<'; 85 maze[(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<';
64 86
65 return; 87 return;
66 } 88 }
67 89
68 maze->clear ('#'); 90 maze.fill ('#');
69 91
70 /* decide on the number of rooms */ 92 /* decide on the number of rooms */
71 nrooms = rmg_rndm (10) + 6; 93 nrooms = rmg_rndm (10) + 6;
72 Room *rooms = salloc0<Room> (nrooms + 1); 94 Room *rooms = salloc0<Room> (nrooms + 1);
73 95
82 i++; 104 i++;
83 } 105 }
84 106
85 if (i == 0) /* no can do! */ 107 if (i == 0) /* no can do! */
86 { 108 {
87 maze->clear (); 109 maze.clear ();
88 maze->border (); 110 maze.border ();
89 111
90 maze [(xsize - 1) / 2][(ysize - 1) / 2 ] = '>'; 112 maze [(xsize - 1) / 2][(ysize - 1) / 2 ] = '>';
91 maze [(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<'; 113 maze [(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<';
92 114
93 sfree (rooms, nrooms + 1); 115 sfree (rooms, nrooms + 1);
95 } 117 }
96 118
97 /* erase the areas occupied by the rooms */ 119 /* erase the areas occupied by the rooms */
98 roguelike_make_rooms (rooms, maze, options); 120 roguelike_make_rooms (rooms, maze, options);
99 121
100 roguelike_link_rooms (rooms, maze, xsize, ysize); 122 roguelike_link_rooms (rooms, maze);
101 123
102 /* put in the stairs */ 124 /* put in the stairs */
103 125
104 maze[rooms->x][rooms->y] = '<'; 126 maze[rooms->x][rooms->y] = '<';
105 127
132 if (maze[i][j] == '.') 154 if (maze[i][j] == '.')
133 maze[i][j] = 0; 155 maze[i][j] = 0;
134 156
135 if (maze[i][j] == 'D') 157 if (maze[i][j] == 'D')
136 { /* remove bad door. */ 158 { /* remove bad door. */
137 int si = surround_check (maze, i, j, xsize, ysize); 159 int si = surround_check (maze, i, j);
138 160
139 if (si != 3 && si != 12) 161 if (si != 3 && si != 12)
140 { 162 {
141 maze[i][j] = 0; 163 maze[i][j] = 0;
142 /* back up and recheck any nearby doors */ 164 /* back up and recheck any nearby doors */
252 maze[i][j] = '.'; 274 maze[i][j] = '.';
253 } 275 }
254} 276}
255 277
256static void 278static void
257roguelike_link_rooms (Room *rooms, char **maze, int xsize, int ysize) 279roguelike_link_rooms (Room *rooms, layout &maze)
258{ 280{
259 Room *walk; 281 Room *walk;
260 int i, j; 282 int i, j;
261 283
262 /* link each room to the previous room */ 284 /* link each room to the previous room */
301 maze[i - 1][j] = 'D'; 323 maze[i - 1][j] = 'D';
302 } 324 }
303 else if (maze[i][j] != 'D' && maze[i][j] != '.') 325 else if (maze[i][j] != 'D' && maze[i][j] != '.')
304 maze[i][j] = 0; 326 maze[i][j] = 0;
305 } 327 }
328
306 j = MIN (y1, y2); 329 j = min (y1, y2);
330
307 if (maze[i][j] == '.') 331 if (maze[i][j] == '.')
308 in_wall = 0; 332 in_wall = 0;
333
309 if (maze[i][j] == 0 || maze[i][j] == '#') 334 if (maze[i][j] == 0 || maze[i][j] == '#')
310 in_wall = 1; 335 in_wall = 1;
336
311 for ( /* j set already */ ; j < MAX (y1, y2); j++) 337 for ( /* j set already */ ; j < max (y1, y2); j++)
312 { 338 {
313 if (in_wall == 0 && maze[i][j] == '#') 339 if (in_wall == 0 && maze[i][j] == '#')
314 { 340 {
315 in_wall = 1; 341 in_wall = 1;
316 maze[i][j] = 'D'; 342 maze[i][j] = 'D';
354 } 380 }
355 else if (maze[i][j] != 'D' && maze[i][j] != '.') 381 else if (maze[i][j] != 'D' && maze[i][j] != '.')
356 maze[i][j] = 0; 382 maze[i][j] = 0;
357 } 383 }
358 384
359 i = MIN (x1, x2); 385 i = min (x1, x2);
386
360 if (maze[i][j] == '.') 387 if (maze[i][j] == '.')
361 in_wall = 0; 388 in_wall = 0;
389
362 if (maze[i][j] == 0 || maze[i][j] == '#') 390 if (maze[i][j] == 0 || maze[i][j] == '#')
363 in_wall = 1; 391 in_wall = 1;
392
364 for ( /* i set already */ ; i < MAX (x1, x2); i++) 393 for ( /* i set already */ ; i < max (x1, x2); i++)
365 { 394 {
366 if (in_wall == 0 && maze[i][j] == '#') 395 if (in_wall == 0 && maze[i][j] == '#')
367 { 396 {
368 in_wall = 1; 397 in_wall = 1;
369 maze[i][j] = 'D'; 398 maze[i][j] = 'D';

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines