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.11 by root, Sat Nov 7 18:30:05 2009 UTC vs.
Revision 1.17 by root, Mon Jul 5 00:07:21 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 */
23
1/* generate a rogue/nethack-like layout */ 24/* generate a rogue/nethack-like maze */
2#include <global.h> 25#include <global.h>
3#include <random_map.h> 26#include <random_map.h>
4#include <rproto.h> 27#include <rproto.h>
5 28
6typedef struct 29typedef struct
15 int rtype; /* circle or rectangular */ 38 int rtype; /* circle or rectangular */
16} Room; 39} Room;
17 40
18static 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);
19static void roguelike_make_rooms (Room *rooms, char **maze, int options); 42static void roguelike_make_rooms (Room *rooms, char **maze, int options);
20static void roguelike_link_rooms (Room *rooms, char **maze, int xsize, int ysize); 43static void roguelike_link_rooms (Room *rooms, layout &maze);
21 44
22int 45int
23surround_check (char **layout, int i, int j, int Xsize, int Ysize) 46surround_check (layout &maze, int i, int j)
24{ 47{
25 /* 1 = wall to left, 48 /* 1 = wall to left,
26 2 = wall to right, 49 2 = wall to right,
27 4 = wall above 50 4 = wall above
28 8 = wall below */ 51 8 = wall below */
29 int surround_index = 0; 52 int surround_index = 0;
30 53
31 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;
32 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;
33 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;
34 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;
35 58
36 return surround_index; 59 return surround_index;
37} 60}
38 61
39/* actually make the layout: we work by a reduction process: 62/* actually make the maze: we work by a reduction process:
40 * 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
41 */ 64 */
42void 65void
43roguelike_layout_gen (Layout maze, int options) 66roguelike_layout_gen (layout &maze, int options)
44{ 67{
45 int i, j; 68 int i, j;
46 Room *walk; 69 Room *walk;
47 int nrooms = 0; 70 int nrooms = 0;
48 int tries = 0; 71 int tries = 0;
49 72
50 int xsize = maze->w; 73 int xsize = maze.w;
51 int ysize = maze->h; 74 int ysize = maze.h;
52 75
53 /* minimum room size is basically 5x5: if xsize/ysize is 76 /* minimum room size is basically 5x5: if xsize/ysize is
54 less than 3x that then hollow things out, stick in 77 less than 3x that then hollow things out, stick in
55 a stairsup and stairs down, and exit */ 78 a stairsup and stairs down, and exit */
56 if (xsize < 11 || ysize < 11) 79 if (xsize < 11 || ysize < 11)
57 { 80 {
58 maze->clear (); 81 maze.clear ();
59 maze->border (); 82 maze.border ();
60 83
61 maze[(xsize - 1) / 2][(ysize - 1) / 2 ] = '>'; 84 maze[(xsize - 1) / 2][(ysize - 1) / 2 ] = '>';
62 maze[(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<'; 85 maze[(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<';
63 86
64 return; 87 return;
65 } 88 }
66 89
67 maze->clear ('#'); 90 maze.fill ('#');
68 91
69 /* decide on the number of rooms */ 92 /* decide on the number of rooms */
70 nrooms = rmg_rndm (10) + 6; 93 nrooms = rmg_rndm (10) + 6;
71 Room *rooms = salloc0<Room> (nrooms + 1); 94 Room *rooms = salloc0<Room> (nrooms + 1);
72 95
81 i++; 104 i++;
82 } 105 }
83 106
84 if (i == 0) /* no can do! */ 107 if (i == 0) /* no can do! */
85 { 108 {
86 maze->clear (); 109 maze.clear ();
87 maze->border (); 110 maze.border ();
88 111
89 maze [(xsize - 1) / 2][(ysize - 1) / 2 ] = '>'; 112 maze [(xsize - 1) / 2][(ysize - 1) / 2 ] = '>';
90 maze [(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<'; 113 maze [(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<';
91 114
92 sfree (rooms, nrooms + 1); 115 sfree (rooms, nrooms + 1);
94 } 117 }
95 118
96 /* erase the areas occupied by the rooms */ 119 /* erase the areas occupied by the rooms */
97 roguelike_make_rooms (rooms, maze, options); 120 roguelike_make_rooms (rooms, maze, options);
98 121
99 roguelike_link_rooms (rooms, maze, xsize, ysize); 122 roguelike_link_rooms (rooms, maze);
100 123
101 /* put in the stairs */ 124 /* put in the stairs */
102 125
103 maze[rooms->x][rooms->y] = '<'; 126 maze[rooms->x][rooms->y] = '<';
104 127
131 if (maze[i][j] == '.') 154 if (maze[i][j] == '.')
132 maze[i][j] = 0; 155 maze[i][j] = 0;
133 156
134 if (maze[i][j] == 'D') 157 if (maze[i][j] == 'D')
135 { /* remove bad door. */ 158 { /* remove bad door. */
136 int si = surround_check (maze, i, j, xsize, ysize); 159 int si = surround_check (maze, i, j);
137 160
138 if (si != 3 && si != 12) 161 if (si != 3 && si != 12)
139 { 162 {
140 maze[i][j] = 0; 163 maze[i][j] = 0;
141 /* back up and recheck any nearby doors */ 164 /* back up and recheck any nearby doors */
167 ty = rmg_rndm (ysize); 190 ty = rmg_rndm (ysize);
168 191
169 /* generate a distribution of sizes centered about basesize */ 192 /* generate a distribution of sizes centered about basesize */
170 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);
171 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);
172 sy = (int) (sy * .5); /* renormalize */ 195 sy >>= 1; /* renormalize */
173 196
174 /* find the corners */ 197 /* find the corners */
175 ax = tx - sx / 2; 198 ax = tx - sx / 2;
176 zx = tx + sx / 2 + sx % 2; 199 zx = tx + sx / 2 + sx % 2;
177 200
197 } 220 }
198 221
199 /* if we've got here, presumably the room is OK. */ 222 /* if we've got here, presumably the room is OK. */
200 223
201 /* get a pointer to the first free room */ 224 /* get a pointer to the first free room */
202 for (walk = rooms; walk->x != 0; walk++) 225 for (walk = rooms; walk->x; walk++)
203 ; 226 ;
204 227
205 walk->x = tx; 228 walk->x = tx;
206 walk->y = ty; 229 walk->y = ty;
207 walk->sx = sx; 230 walk->sx = sx;
208 walk->sy = sy; 231 walk->sy = sy;
209 walk->ax = ax; 232 walk->ax = ax;
210 walk->ay = ay; 233 walk->ay = ay;
211 walk->zx = zx; 234 walk->zx = zx;
217/* write all the rooms into the maze */ 240/* write all the rooms into the maze */
218static void 241static void
219roguelike_make_rooms (Room *rooms, char **maze, int options) 242roguelike_make_rooms (Room *rooms, char **maze, int options)
220{ 243{
221 int making_circle = 0; 244 int making_circle = 0;
222 int i, j;
223 int R; 245 int R;
224 Room *walk; 246 Room *walk;
225 247
226 for (walk = rooms; walk->x != 0; walk++) 248 for (walk = rooms; walk->x; walk++)
227 { 249 {
228 /* first decide what shape to make */ 250 /* first decide what shape to make */
229 switch (options) 251 switch (options)
230 { 252 {
231 case 1: 253 case 1:
243 R = walk->sx / 2; 265 R = walk->sx / 2;
244 else 266 else
245 R = walk->sy / 2; 267 R = walk->sy / 2;
246 268
247 /* enscribe a rectangle */ 269 /* enscribe a rectangle */
248 for (i = walk->ax; i < walk->zx; i++) 270 for (int i = walk->ax; i < walk->zx; i++)
249 for (j = walk->ay; j < walk->zy; j++) 271 for (int j = walk->ay; j < walk->zy; j++)
250 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)
251 maze[i][j] = '.'; 273 maze[i][j] = '.';
252 } 274 }
253} 275}
254 276
255static void 277static void
256roguelike_link_rooms (Room *rooms, char **maze, int xsize, int ysize) 278roguelike_link_rooms (Room *rooms, layout &maze)
257{ 279{
258 Room *walk; 280 Room *walk;
259 int i, j; 281 int i, j;
260 282
261 /* 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