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.12 by root, Sat Nov 7 18:32:45 2009 UTC vs.
Revision 1.17 by root, Mon Jul 5 00:07:21 2010 UTC

19 * <http://www.gnu.org/licenses/>. 19 * <http://www.gnu.org/licenses/>.
20 * 20 *
21 * The authors can be reached via e-mail to <support@deliantra.net> 21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 22 */
23 23
24/* generate a rogue/nethack-like layout */ 24/* generate a rogue/nethack-like maze */
25#include <global.h> 25#include <global.h>
26#include <random_map.h> 26#include <random_map.h>
27#include <rproto.h> 27#include <rproto.h>
28 28
29typedef struct 29typedef struct
38 int rtype; /* circle or rectangular */ 38 int rtype; /* circle or rectangular */
39} Room; 39} Room;
40 40
41static 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);
42static void roguelike_make_rooms (Room *rooms, char **maze, int options); 42static void roguelike_make_rooms (Room *rooms, char **maze, int options);
43static void roguelike_link_rooms (Room *rooms, char **maze, int xsize, int ysize); 43static void roguelike_link_rooms (Room *rooms, layout &maze);
44 44
45int 45int
46surround_check (char **layout, int i, int j, int Xsize, int Ysize) 46surround_check (layout &maze, int i, int j)
47{ 47{
48 /* 1 = wall to left, 48 /* 1 = wall to left,
49 2 = wall to right, 49 2 = wall to right,
50 4 = wall above 50 4 = wall above
51 8 = wall below */ 51 8 = wall below */
52 int surround_index = 0; 52 int surround_index = 0;
53 53
54 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;
55 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;
56 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;
57 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;
58 58
59 return surround_index; 59 return surround_index;
60} 60}
61 61
62/* actually make the layout: we work by a reduction process: 62/* actually make the maze: we work by a reduction process:
63 * 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
64 */ 64 */
65void 65void
66roguelike_layout_gen (Layout maze, int options) 66roguelike_layout_gen (layout &maze, int options)
67{ 67{
68 int i, j; 68 int i, j;
69 Room *walk; 69 Room *walk;
70 int nrooms = 0; 70 int nrooms = 0;
71 int tries = 0; 71 int tries = 0;
72 72
73 int xsize = maze->w; 73 int xsize = maze.w;
74 int ysize = maze->h; 74 int ysize = maze.h;
75 75
76 /* minimum room size is basically 5x5: if xsize/ysize is 76 /* minimum room size is basically 5x5: if xsize/ysize is
77 less than 3x that then hollow things out, stick in 77 less than 3x that then hollow things out, stick in
78 a stairsup and stairs down, and exit */ 78 a stairsup and stairs down, and exit */
79 if (xsize < 11 || ysize < 11) 79 if (xsize < 11 || ysize < 11)
80 { 80 {
81 maze->clear (); 81 maze.clear ();
82 maze->border (); 82 maze.border ();
83 83
84 maze[(xsize - 1) / 2][(ysize - 1) / 2 ] = '>'; 84 maze[(xsize - 1) / 2][(ysize - 1) / 2 ] = '>';
85 maze[(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<'; 85 maze[(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<';
86 86
87 return; 87 return;
88 } 88 }
89 89
90 maze->clear ('#'); 90 maze.fill ('#');
91 91
92 /* decide on the number of rooms */ 92 /* decide on the number of rooms */
93 nrooms = rmg_rndm (10) + 6; 93 nrooms = rmg_rndm (10) + 6;
94 Room *rooms = salloc0<Room> (nrooms + 1); 94 Room *rooms = salloc0<Room> (nrooms + 1);
95 95
104 i++; 104 i++;
105 } 105 }
106 106
107 if (i == 0) /* no can do! */ 107 if (i == 0) /* no can do! */
108 { 108 {
109 maze->clear (); 109 maze.clear ();
110 maze->border (); 110 maze.border ();
111 111
112 maze [(xsize - 1) / 2][(ysize - 1) / 2 ] = '>'; 112 maze [(xsize - 1) / 2][(ysize - 1) / 2 ] = '>';
113 maze [(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<'; 113 maze [(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<';
114 114
115 sfree (rooms, nrooms + 1); 115 sfree (rooms, nrooms + 1);
117 } 117 }
118 118
119 /* erase the areas occupied by the rooms */ 119 /* erase the areas occupied by the rooms */
120 roguelike_make_rooms (rooms, maze, options); 120 roguelike_make_rooms (rooms, maze, options);
121 121
122 roguelike_link_rooms (rooms, maze, xsize, ysize); 122 roguelike_link_rooms (rooms, maze);
123 123
124 /* put in the stairs */ 124 /* put in the stairs */
125 125
126 maze[rooms->x][rooms->y] = '<'; 126 maze[rooms->x][rooms->y] = '<';
127 127
154 if (maze[i][j] == '.') 154 if (maze[i][j] == '.')
155 maze[i][j] = 0; 155 maze[i][j] = 0;
156 156
157 if (maze[i][j] == 'D') 157 if (maze[i][j] == 'D')
158 { /* remove bad door. */ 158 { /* remove bad door. */
159 int si = surround_check (maze, i, j, xsize, ysize); 159 int si = surround_check (maze, i, j);
160 160
161 if (si != 3 && si != 12) 161 if (si != 3 && si != 12)
162 { 162 {
163 maze[i][j] = 0; 163 maze[i][j] = 0;
164 /* back up and recheck any nearby doors */ 164 /* back up and recheck any nearby doors */
190 ty = rmg_rndm (ysize); 190 ty = rmg_rndm (ysize);
191 191
192 /* generate a distribution of sizes centered about basesize */ 192 /* generate a distribution of sizes centered about basesize */
193 sx = rmg_rndm (x_basesize) + rmg_rndm (x_basesize) + rmg_rndm (x_basesize); 193 sx = rmg_rndm (x_basesize) + rmg_rndm (x_basesize) + rmg_rndm (x_basesize);
194 sy = rmg_rndm (y_basesize) + rmg_rndm (y_basesize) + rmg_rndm (y_basesize); 194 sy = rmg_rndm (y_basesize) + rmg_rndm (y_basesize) + rmg_rndm (y_basesize);
195 sy = (int) (sy * .5); /* renormalize */ 195 sy >>= 1; /* renormalize */
196 196
197 /* find the corners */ 197 /* find the corners */
198 ax = tx - sx / 2; 198 ax = tx - sx / 2;
199 zx = tx + sx / 2 + sx % 2; 199 zx = tx + sx / 2 + sx % 2;
200 200
220 } 220 }
221 221
222 /* if we've got here, presumably the room is OK. */ 222 /* if we've got here, presumably the room is OK. */
223 223
224 /* get a pointer to the first free room */ 224 /* get a pointer to the first free room */
225 for (walk = rooms; walk->x != 0; walk++) 225 for (walk = rooms; walk->x; walk++)
226 ; 226 ;
227 227
228 walk->x = tx; 228 walk->x = tx;
229 walk->y = ty; 229 walk->y = ty;
230 walk->sx = sx; 230 walk->sx = sx;
231 walk->sy = sy; 231 walk->sy = sy;
232 walk->ax = ax; 232 walk->ax = ax;
233 walk->ay = ay; 233 walk->ay = ay;
234 walk->zx = zx; 234 walk->zx = zx;
240/* write all the rooms into the maze */ 240/* write all the rooms into the maze */
241static void 241static void
242roguelike_make_rooms (Room *rooms, char **maze, int options) 242roguelike_make_rooms (Room *rooms, char **maze, int options)
243{ 243{
244 int making_circle = 0; 244 int making_circle = 0;
245 int i, j;
246 int R; 245 int R;
247 Room *walk; 246 Room *walk;
248 247
249 for (walk = rooms; walk->x != 0; walk++) 248 for (walk = rooms; walk->x; walk++)
250 { 249 {
251 /* first decide what shape to make */ 250 /* first decide what shape to make */
252 switch (options) 251 switch (options)
253 { 252 {
254 case 1: 253 case 1:
266 R = walk->sx / 2; 265 R = walk->sx / 2;
267 else 266 else
268 R = walk->sy / 2; 267 R = walk->sy / 2;
269 268
270 /* enscribe a rectangle */ 269 /* enscribe a rectangle */
271 for (i = walk->ax; i < walk->zx; i++) 270 for (int i = walk->ax; i < walk->zx; i++)
272 for (j = walk->ay; j < walk->zy; j++) 271 for (int j = walk->ay; j < walk->zy; j++)
273 if (!making_circle || ((int) (0.5 + hypot (walk->x - i, walk->y - j))) <= R) 272 if (!making_circle || ((int) (0.5 + hypot (walk->x - i, walk->y - j))) <= R)
274 maze[i][j] = '.'; 273 maze[i][j] = '.';
275 } 274 }
276} 275}
277 276
278static void 277static void
279roguelike_link_rooms (Room *rooms, char **maze, int xsize, int ysize) 278roguelike_link_rooms (Room *rooms, layout &maze)
280{ 279{
281 Room *walk; 280 Room *walk;
282 int i, j; 281 int i, j;
283 282
284 /* link each room to the previous room */ 283 /* link each room to the previous room */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines