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

Comparing deliantra/server/random_maps/treasure.C (file contents):
Revision 1.20 by root, Thu Jan 18 00:06:56 2007 UTC vs.
Revision 1.58 by root, Sun Jul 4 22:12:26 2010 UTC

1/* 1/*
2 * CrossFire, A Multiplayer game 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (C) 2001 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2001 Mark Wedel & Crossfire Development Team
6 * Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (©) 1992 Frank Tore Johansen
7 * 7 *
8 * This program is free software; you can redistribute it and/or modify 8 * Deliantra is free software: you can redistribute it and/or modify it under
9 * it under the terms of the GNU General Public License as published by 9 * the terms of the Affero GNU General Public License as published by the
10 * the Free Software Foundation; either version 2 of the License, or 10 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your option) any later version. 11 * option) any later version.
12 * 12 *
13 * This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 16 * GNU General Public License for more details.
17 * 17 *
18 * You should have received a copy of the GNU General Public License 18 * You should have received a copy of the Affero GNU General Public License
19 * along with this program; if not, write to the Free Software 19 * and the GNU General Public License along with this program. If not, see
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * <http://www.gnu.org/licenses/>.
21 * 21 *
22 * The authors can be reached via e-mail at <crossfire@schmorp.de> 22 * The authors can be reached via e-mail to <support@deliantra.net>
23 */ 23 */
24 24
25/* placing treasure in maps, where appropriate. */ 25/* placing treasure in maps, where appropriate. */
26 26
27#include <global.h> 27#include <global.h>
41#define LAST_OPTION 64 /* set this to the last real option, for random */ 41#define LAST_OPTION 64 /* set this to the last real option, for random */
42 42
43#define NO_PASS_DOORS 0 43#define NO_PASS_DOORS 0
44#define PASS_DOORS 1 44#define PASS_DOORS 1
45 45
46static object *find_closest_monster (maptile *map, int x, int y);
47static object *find_monster_in_room (maptile *map, int x, int y);
48static void find_spot_in_room (maptile *map, int x, int y, int *kx, int *ky);
49static object *place_chest (int treasureoptions, int x, int y, maptile *map, maptile *style_map, int n_treasures, random_map_params *RP);
50static object **find_doors_in_room (maptile *map, int x, int y);
51static void lock_and_hide_doors (object **doorlist, maptile *map, int opts, random_map_params *RP);
52static void find_enclosed_spot (maptile *map, int *cx, int *cy);
53static object **surround_by_doors (maptile *map, char **maze, int x, int y, int opts);
54
55/* a macro to get a strongly centered random distribution,
56 from 0 to x, centered at x/2 */
57static int
58bc_random (int x)
59{
60 return (rmg_rndm (x) + rmg_rndm (x) + rmg_rndm (x)) / 3;
61}
62
63static object *
64gen_key (const shstr &keycode)
65{
66 /* get a key and set its keycode */
67 object *key = archetype::get (shstr_key_random_map);
68 key->slaying = keycode;
69 return key;
70}
71
72/* places keys in the map, preferably in something alive.
73 keycode is the key's code,
74 door_flag is either PASS_DOORS or NO_PASS_DOORS.
75 NO_PASS_DOORS won't cross doors or walls to keyplace, PASS_DOORS will.
76 if n_keys is 1, it will place 1 key. if n_keys >1, it will place 2-4 keys:
77 it will place 2-4 keys regardless of what nkeys is provided nkeys > 1.
78
79 The idea is that you call keyplace on x,y where a door is, and it'll make
80 sure a key is placed on both sides of the door.
81*/
82static int
83keyplace (maptile *map, int x, int y, const shstr &keycode, int door_flag, int n_keys)
84{
85 int i, j;
86 int kx = 0, ky = 0;
87 object *the_keymaster; /* the monster that gets the key. */
88 object *the_key = gen_key (keycode);
89
90 if (door_flag == PASS_DOORS)
91 {
92 int tries = 0;
93
94 the_keymaster = 0;
95 while (tries < 15 && !the_keymaster)
96 {
97 i = rmg_rndm (map->width - 2) + 1;
98 j = rmg_rndm (map->height - 2) + 1;
99 tries++;
100 the_keymaster = find_closest_monster (map, i, j);
101 }
102
103 /* if we don't find a good keymaster, drop the key on the ground. */
104 if (!the_keymaster)
105 {
106 int freeindex;
107
108 freeindex = -1;
109 for (tries = 0; tries < 15 && freeindex == -1; tries++)
110 {
111 kx = rmg_rndm (map->width - 2) + 1;
112 ky = rmg_rndm (map->height - 2) + 1;
113 freeindex = rmg_find_free_spot (the_key, map, kx, ky, 1, SIZEOFFREE1 + 1);
114 }
115
116 // can freeindex ever be < 0?
117 if (freeindex >= 0)
118 {
119 kx += freearr_x [freeindex];
120 ky += freearr_y [freeindex];
121 }
122 }
123 }
124 else
125 { /* NO_PASS_DOORS --we have to work harder. */
126 /* don't try to keyplace if we're sitting on a blocked square and
127 NO_PASS_DOORS is set. */
128 if (n_keys == 1)
129 {
130 if (wall_blocked (map, x, y))
131 {
132 the_key->destroy ();
133 return 0;
134 }
135
136 the_keymaster = find_monster_in_room (map, x, y);
137 if (!the_keymaster) /* if fail, find a spot to drop the key. */
138 find_spot_in_room (map, x, y, &kx, &ky);
139 }
140 else
141 {
142 int sum = 0; /* count how many keys we actually place */
143
144 /* I'm lazy, so just try to place in all 4 directions. */
145 sum += keyplace (map, x + 1, y, keycode, NO_PASS_DOORS, 1);
146 sum += keyplace (map, x, y + 1, keycode, NO_PASS_DOORS, 1);
147 sum += keyplace (map, x - 1, y, keycode, NO_PASS_DOORS, 1);
148 sum += keyplace (map, x, y - 1, keycode, NO_PASS_DOORS, 1);
149
150 if (sum < 2) /* we might have made a disconnected map-place more keys. */
151 { /* diagonally this time. */
152 keyplace (map, x + 1, y + 1, keycode, NO_PASS_DOORS, 1);
153 keyplace (map, x + 1, y - 1, keycode, NO_PASS_DOORS, 1);
154 keyplace (map, x - 1, y + 1, keycode, NO_PASS_DOORS, 1);
155 keyplace (map, x - 1, y - 1, keycode, NO_PASS_DOORS, 1);
156 }
157
158 the_key->destroy ();
159 return 1;
160 }
161 }
162
163 if (the_keymaster)
164 the_keymaster->head_ ()->insert (the_key);
165 else
166 {
167 the_key->x = kx;
168 the_key->y = ky;
169 insert_ob_in_map (the_key, map, NULL, 0);
170 }
171
172 return 1;
173}
46 174
47/* returns true if square x,y has P_NO_PASS set, which is true for walls 175/* returns true if square x,y has P_NO_PASS set, which is true for walls
48 * and doors but not monsters. 176 * and doors but not monsters.
49 * This function is not map tile aware. 177 * This function is not map tile aware.
50 */ 178 */
52wall_blocked (maptile *m, int x, int y) 180wall_blocked (maptile *m, int x, int y)
53{ 181{
54 if (OUT_OF_REAL_MAP (m, x, y)) 182 if (OUT_OF_REAL_MAP (m, x, y))
55 return 1; 183 return 1;
56 184
185 m->at (x, y).update ();
57 int r = GET_MAP_MOVE_BLOCK (m, x, y) & ~MOVE_BLOCK_DEFAULT; 186 return GET_MAP_MOVE_BLOCK (m, x, y) & MOVE_WALK;
58 return r;
59} 187}
60 188
61/* place treasures in the map, given the 189/* place treasures in the map, given the
62map, (required) 190map, (required)
63layout, (required) 191maze, (required)
64treasure style (may be empty or NULL, or "none" to cause no treasure.) 192treasure style (may be empty or NULL, or "none" to cause no treasure.)
65treasureoptions (may be 0 for random choices or positive) 193treasureoptions (may be 0 for random choices or positive)
66*/ 194*/
67void 195void
68place_treasure (maptile *map, char **layout, char *treasure_style, int treasureoptions, random_map_params *RP) 196place_treasure (maptile *map, layout &maze, const char *treasure_style, int treasureoptions, random_map_params *RP)
69{ 197{
70 char styledirname[1024];
71 char stylefilepath[1024];
72 maptile *style_map = 0;
73 int num_treasures; 198 int num_treasures;
74 199
75 /* bail out if treasure isn't wanted. */ 200 /* bail out if treasure isn't wanted. */
76 if (treasure_style) 201 if (treasure_style)
77 if (!strcmp (treasure_style, "none")) 202 if (!strcmp (treasure_style, "none"))
78 return; 203 return;
204
79 if (treasureoptions <= 0) 205 if (treasureoptions <= 0)
80 treasureoptions = RANDOM () % (2 * LAST_OPTION); 206 treasureoptions = rmg_rndm (2 * LAST_OPTION);
81 207
82 /* filter out the mutually exclusive options */ 208 /* filter out the mutually exclusive options */
83 if ((treasureoptions & RICH) && (treasureoptions & SPARSE)) 209 if ((treasureoptions & RICH) && (treasureoptions & SPARSE))
84 { 210 {
85 if (RANDOM () % 2) 211 if (rmg_rndm (2))
86 treasureoptions -= 1; 212 treasureoptions -= 1;
87 else 213 else
88 treasureoptions -= 2; 214 treasureoptions -= 2;
89 } 215 }
90 216
91 /* pick the number of treasures */ 217 /* pick the number of treasures */
92 if (treasureoptions & SPARSE) 218 if (treasureoptions & SPARSE)
93 num_treasures = BC_RANDOM (RP->total_map_hp / 600 + RP->difficulty / 2 + 1); 219 num_treasures = bc_random (RP->total_map_hp / 600 + RP->difficulty / 2 + 1);
94 else if (treasureoptions & RICH) 220 else if (treasureoptions & RICH)
95 num_treasures = BC_RANDOM (RP->total_map_hp / 150 + 2 * RP->difficulty + 1); 221 num_treasures = bc_random (RP->total_map_hp / 150 + 2 * RP->difficulty + 1);
96 else 222 else
97 num_treasures = BC_RANDOM (RP->total_map_hp / 300 + RP->difficulty + 1); 223 num_treasures = bc_random (RP->total_map_hp / 300 + RP->difficulty + 1);
98 224
99 if (num_treasures <= 0) 225 if (num_treasures <= 0)
100 return; 226 return;
101 227
102 /* get the style map */ 228 /* get the style map */
103 sprintf (styledirname, "%s", "/styles/treasurestyles"); 229 maptile *style_map = find_style ("/styles/treasurestyles", treasure_style, RP->difficulty);
104 sprintf (stylefilepath, "%s/%s", styledirname, treasure_style); 230
105 style_map = find_style (styledirname, treasure_style, -1); 231 if (!style_map)
232 {
233 LOG (llevError, "unable to load style map %s %s.\n", "/styles/treasurestyles", treasure_style);
234 return;
235 }
106 236
107 /* all the treasure at one spot in the map. */ 237 /* all the treasure at one spot in the map. */
108 if (treasureoptions & CONCENTRATED) 238 if (treasureoptions & CONCENTRATED)
109 { 239 {
110
111 /* map_layout_style global, and is previously set */ 240 /* map_layout_style global, and is previously set */
112 switch (RP->map_layout_style) 241 switch (RP->map_layout_style)
113 { 242 {
114 case LAYOUT_ONION: 243 case LAYOUT_ONION:
115 case LAYOUT_SPIRAL: 244 case LAYOUT_SPIRAL:
120 /* search the onion for C's or '>', and put treasure there. */ 249 /* search the onion for C's or '>', and put treasure there. */
121 for (i = 0; i < RP->Xsize; i++) 250 for (i = 0; i < RP->Xsize; i++)
122 { 251 {
123 for (j = 0; j < RP->Ysize; j++) 252 for (j = 0; j < RP->Ysize; j++)
124 { 253 {
125 if (layout[i][j] == 'C' || layout[i][j] == '>') 254 if (maze[i][j] == 'C' || maze[i][j] == '>')
126 { 255 {
127 int tdiv = RP->symmetry_used; 256 int tdiv = RP->symmetry_used;
128 object **doorlist;
129 object *chest; 257 object *chest;
130 258
131 if (tdiv == 3) 259 if (tdiv == 3)
132 tdiv = 2; /* this symmetry uses a divisor of 2 */ 260 tdiv = 2; /* this symmetry uses a divisor of 2 */
261
133 /* don't put a chest on an exit. */ 262 /* don't put a chest on an exit. */
134 chest = place_chest (treasureoptions, i, j, map, style_map, num_treasures / tdiv, RP); 263 chest = place_chest (treasureoptions, i, j, map, style_map, num_treasures / tdiv, RP);
264
135 if (!chest) 265 if (!chest)
136 continue; /* if no chest was placed NEXT */ 266 continue; /* if no chest was placed NEXT */
267
137 if (treasureoptions & (DOORED | HIDDEN)) 268 if (treasureoptions & (DOORED | HIDDEN))
138 { 269 {
139 doorlist = find_doors_in_room (map, i, j, RP); 270 object **doorlist = find_doors_in_room (map, i, j);
140 lock_and_hide_doors (doorlist, map, treasureoptions, RP); 271 lock_and_hide_doors (doorlist, map, treasureoptions, RP);
141 free (doorlist); 272 free (doorlist);
142 } 273 }
143 } 274 }
144 } 275 }
145 } 276 }
146 break; 277 break;
147 } 278 }
279
148 default: 280 default:
149 { 281 {
150 int i, j, tries; 282 int i, j, tries;
151 object *chest; 283 object *chest;
152 object **doorlist; 284 object **doorlist;
153 285
154 i = j = -1; 286 i = j = -1;
155 tries = 0; 287 tries = 0;
156 while (i == -1 && tries < 100) 288 while (i == -1 && tries < 100)
157 { 289 {
158 i = RANDOM () % (RP->Xsize - 2) + 1; 290 i = rmg_rndm (RP->Xsize - 2) + 1;
159 j = RANDOM () % (RP->Ysize - 2) + 1; 291 j = rmg_rndm (RP->Ysize - 2) + 1;
292
160 find_enclosed_spot (map, &i, &j, RP); 293 find_enclosed_spot (map, &i, &j);
294
161 if (wall_blocked (map, i, j)) 295 if (wall_blocked (map, i, j))
162 i = -1; 296 i = -1;
297
163 tries++; 298 tries++;
164 } 299 }
300
165 chest = place_chest (treasureoptions, i, j, map, style_map, num_treasures, RP); 301 chest = place_chest (treasureoptions, i, j, map, style_map, num_treasures, RP);
302
166 if (!chest) 303 if (!chest)
167 return; 304 return;
305
168 i = chest->x; 306 i = chest->x;
169 j = chest->y; 307 j = chest->y;
170 if (treasureoptions & (DOORED | HIDDEN)) 308 if (treasureoptions & (DOORED | HIDDEN))
171 { 309 {
172 doorlist = surround_by_doors (map, layout, i, j, treasureoptions); 310 doorlist = surround_by_doors (map, maze, i, j, treasureoptions);
173 lock_and_hide_doors (doorlist, map, treasureoptions, RP); 311 lock_and_hide_doors (doorlist, map, treasureoptions, RP);
174 free (doorlist); 312 free (doorlist);
175 } 313 }
176 } 314 }
177 } 315 }
178 } 316 }
179 else 317 else
180 { /* DIFFUSE treasure layout */ 318 { /* DIFFUSE treasure maze */
181 int ti, i, j; 319 int ti, i, j;
182 320
183 for (ti = 0; ti < num_treasures; ti++) 321 for (ti = 0; ti < num_treasures; ti++)
184 { 322 {
185 i = RANDOM () % (RP->Xsize - 2) + 1; 323 i = rmg_rndm (RP->Xsize - 2) + 1;
186 j = RANDOM () % (RP->Ysize - 2) + 1; 324 j = rmg_rndm (RP->Ysize - 2) + 1;
187 place_chest (treasureoptions, i, j, map, style_map, 1, RP); 325 place_chest (treasureoptions, i, j, map, style_map, 1, RP);
188 } 326 }
189 } 327 }
190} 328}
191 329
192/* put a chest into the map, near x and y, with the treasure style 330/* put a chest into the map, near x and y, with the treasure style
193 determined (may be null, or may be a treasure list from lib/treasures, 331 determined (may be null, or may be a treasure list from lib/treasures,
194 if the global variable "treasurestyle" is set to that treasure list's name */ 332 if the global variable "treasurestyle" is set to that treasure list's name */
195 333static object *
196object *
197place_chest (int treasureoptions, int x, int y, maptile *map, maptile *style_map, int n_treasures, random_map_params *RP) 334place_chest (int treasureoptions, int x, int y, maptile *map, maptile *style_map, int n_treasures, random_map_params *RP)
198{ 335{
199 object *the_chest;
200 int i, xl, yl;
201
202 the_chest = get_archetype ("chest"); /* was "chest_2" */ 336 object *the_chest = archetype::get (shstr_chest); /* was "chest_2" */
203 337
204 /* first, find a place to put the chest. */ 338 /* first, find a place to put the chest. */
205 i = find_first_free_spot (the_chest, map, x, y); 339 int i = find_first_free_spot (the_chest, map, x, y); // this call uses the main rng
206 if (i == -1) 340 if (i == -1)
207 { 341 {
208 the_chest->destroy (); 342 the_chest->destroy ();
209 return NULL; 343 return NULL;
210 } 344 }
211 345
212 xl = x + freearr_x[i]; 346 int xl = x + freearr_x[i];
213 yl = y + freearr_y[i]; 347 int yl = y + freearr_y[i];
214 348
215 /* if the placement is blocked, return a fail. */ 349 /* if the placement is blocked, return a fail. */
216 if (wall_blocked (map, xl, yl)) 350 if (wall_blocked (map, xl, yl))
217 return 0; 351 return 0;
218 352
225 treasurelist *tlist = find_treasurelist (RP->treasurestyle); 359 treasurelist *tlist = find_treasurelist (RP->treasurestyle);
226 360
227 if (tlist != NULL) 361 if (tlist != NULL)
228 for (ti = 0; ti < n_treasures; ti++) 362 for (ti = 0; ti < n_treasures; ti++)
229 { /* use the treasure list */ 363 { /* use the treasure list */
230 object *new_treasure = pick_random_object (style_map); 364 object *new_treasure = style_map->pick_random_object (rmg_rndm);
231 365
232 insert_ob_in_ob (arch_to_object (new_treasure->arch), the_chest); 366 insert_ob_in_ob (new_treasure->arch->instance (), the_chest);
233 } 367 }
234 else 368 else
235 { /* use the style map */ 369 { /* use the style map */
236 the_chest->randomitems = tlist; 370 the_chest->randomitems = tlist;
237 the_chest->stats.hp = n_treasures; 371 the_chest->stats.hp = n_treasures;
238 } 372 }
239#endif 373#endif
240 { /* neither style_map no treasure list given */ 374 { /* neither style_map no treasure list given */
241 treasurelist *tlist = find_treasurelist ("chest"); 375 treasurelist *tlist = treasurelist::find ("chest");
242 376
243 the_chest->randomitems = tlist; 377 the_chest->randomitems = tlist;
244 the_chest->stats.hp = n_treasures; 378 the_chest->stats.hp = n_treasures;
245 } 379 }
246 380
247 /* stick a trap in the chest if required */ 381 /* stick a trap in the chest if required */
248 if (treasureoptions & TRAPPED) 382 if (treasureoptions & TRAPPED)
249 { 383 {
250 maptile *trap_map = find_style ("/styles/trapstyles", "traps", -1); 384 maptile *trap_map = find_style ("/styles/trapstyles", "traps", RP->difficulty);
251 object *the_trap;
252 385
253 if (trap_map) 386 if (trap_map)
254 { 387 {
255 the_trap = pick_random_object (trap_map); 388 object *the_trap = trap_map->pick_random_object (rmg_rndm);
389
256 the_trap->stats.Cha = 10 + RP->difficulty; 390 the_trap->stats.Cha = 10 + RP->difficulty;
257 the_trap->level = BC_RANDOM ((3 * RP->difficulty) / 2); 391 the_trap->level = bc_random ((3 * RP->difficulty) / 2);
392
258 if (the_trap) 393 if (the_trap)
259 { 394 {
260 object *new_trap; 395 object *new_trap = the_trap->arch->instance ();//TODO: why not clone?
261 396
262 new_trap = arch_to_object (the_trap->arch);
263 new_trap->copy_to (the_trap);
264 new_trap->x = x; 397 new_trap->x = x;
265 new_trap->y = y; 398 new_trap->y = y;
266 insert_ob_in_ob (new_trap, the_chest); 399 insert_ob_in_ob (new_trap, the_chest);
267 } 400 }
268 } 401 }
269 } 402 }
270 403
271 /* set the chest lock code, and call the keyplacer routine with 404 /* set the chest lock code, and call the keyplacer routine with
272 the lockcode. It's not worth bothering to lock the chest if 405 the lockcode. It's not worth bothering to lock the chest if
273 there's only 1 treasure.... */ 406 there's only 1 treasure.... */
274
275 if ((treasureoptions & KEYREQUIRED) && n_treasures > 1) 407 if ((treasureoptions & KEYREQUIRED) && n_treasures > 1)
276 { 408 {
277 char keybuf[1024]; 409 the_chest->slaying = format ("RMG-%d-%d", (int)rmg_rndm (1000000000), (int)rmg_rndm (1000000000));
278
279 sprintf (keybuf, "%d", (int) RANDOM ());
280 the_chest->slaying = keybuf;
281 keyplace (map, x, y, keybuf, PASS_DOORS, 1, RP); 410 keyplace (map, x, y, the_chest->slaying, PASS_DOORS, 1);
282 } 411 }
283 412
284 /* actually place the chest. */ 413 /* actually place the chest. */
285 the_chest->x = xl; 414 the_chest->x = xl;
286 the_chest->y = yl; 415 the_chest->y = yl;
287 insert_ob_in_map (the_chest, map, NULL, 0); 416 insert_ob_in_map (the_chest, map, NULL, 0);
288 return the_chest; 417 return the_chest;
289} 418}
290 419
291
292/* finds the closest monster and returns him, regardless of doors 420/* finds the closest monster and returns him, regardless of doors or walls */
293 or walls */ 421static object *
294object *
295find_closest_monster (maptile *map, int x, int y, random_map_params *RP) 422find_closest_monster (maptile *map, int x, int y)
296{ 423{
297 int i; 424 int i;
298 425
299 for (i = 0; i < SIZEOFFREE; i++) 426 for (i = 0; i < SIZEOFFREE; i++)
300 { 427 {
301 int lx, ly; 428 int lx, ly;
302 429
303 lx = x + freearr_x[i]; 430 lx = x + freearr_x[i];
304 ly = y + freearr_y[i]; 431 ly = y + freearr_y[i];
305 /* boundscheck */ 432 /* boundscheck */
306 if (lx >= 0 && ly >= 0 && lx < RP->Xsize && ly < RP->Ysize) 433 if (lx >= 0 && ly >= 0 && lx < map->width && ly < map->height)
307 /* don't bother searching this square unless the map says life exists. */ 434 /* don't bother searching this square unless the map says life exists. */
308 if (GET_MAP_FLAGS (map, lx, ly) & P_IS_ALIVE) 435 if (GET_MAP_FLAGS (map, lx, ly) & P_IS_ALIVE)
309 { 436 {
310 object *the_monster = GET_MAP_OB (map, lx, ly); 437 object *the_monster = GET_MAP_OB (map, lx, ly);
311 438
312 for (; the_monster != NULL && (!QUERY_FLAG (the_monster, FLAG_MONSTER)); the_monster = the_monster->above); 439 for (; the_monster && !the_monster->flag [FLAG_MONSTER]; the_monster = the_monster->above)
440 ;
441
313 if (the_monster && QUERY_FLAG (the_monster, FLAG_MONSTER)) 442 if (the_monster && the_monster->flag [FLAG_MONSTER])
314 return the_monster; 443 return the_monster;
315 } 444 }
316 } 445 }
317 return NULL; 446 return NULL;
318} 447}
319 448
320
321
322/* places keys in the map, preferably in something alive.
323 keycode is the key's code,
324 door_flag is either PASS_DOORS or NO_PASS_DOORS.
325 NO_PASS_DOORS won't cross doors or walls to keyplace, PASS_DOORS will.
326 if n_keys is 1, it will place 1 key. if n_keys >1, it will place 2-4 keys:
327 it will place 2-4 keys regardless of what nkeys is provided nkeys > 1.
328
329 The idea is that you call keyplace on x,y where a door is, and it'll make
330 sure a key is placed on both sides of the door.
331*/
332int
333keyplace (maptile *map, int x, int y, char *keycode, int door_flag, int n_keys, random_map_params *RP)
334{
335 int i, j;
336 int kx = 0, ky = 0;
337 object *the_keymaster; /* the monster that gets the key. */
338 object *the_key;
339
340 /* get a key and set its keycode */
341 the_key = get_archetype ("key2");
342 the_key->slaying = keycode;
343
344 if (door_flag == PASS_DOORS)
345 {
346 int tries = 0;
347
348 the_keymaster = 0;
349 while (tries < 15 && !the_keymaster)
350 {
351 i = (RANDOM () % (RP->Xsize - 2)) + 1;
352 j = (RANDOM () % (RP->Ysize - 2)) + 1;
353 tries++;
354 the_keymaster = find_closest_monster (map, i, j, RP);
355 }
356
357 /* if we don't find a good keymaster, drop the key on the ground. */
358 if (!the_keymaster)
359 {
360 int freeindex;
361
362 freeindex = -1;
363 for (tries = 0; tries < 15 && freeindex == -1; tries++)
364 {
365 kx = (RANDOM () % (RP->Xsize - 2)) + 1;
366 ky = (RANDOM () % (RP->Ysize - 2)) + 1;
367 freeindex = find_free_spot (the_key, map, kx, ky, 1, SIZEOFFREE1 + 1);
368 }
369
370 // can freeindex ever be < 0?
371 if (freeindex >= 0)
372 {
373 kx += freearr_x [freeindex];
374 ky += freearr_y [freeindex];
375 }
376 }
377 }
378 else
379 { /* NO_PASS_DOORS --we have to work harder. */
380 /* don't try to keyplace if we're sitting on a blocked square and
381 NO_PASS_DOORS is set. */
382 if (n_keys == 1)
383 {
384 if (wall_blocked (map, x, y))
385 return 0;
386
387 the_keymaster = find_monster_in_room (map, x, y, RP);
388 if (!the_keymaster) /* if fail, find a spot to drop the key. */
389 find_spot_in_room (map, x, y, &kx, &ky, RP);
390 }
391 else
392 {
393 int sum = 0; /* count how many keys we actually place */
394
395 /* I'm lazy, so just try to place in all 4 directions. */
396 sum += keyplace (map, x + 1, y, keycode, NO_PASS_DOORS, 1, RP);
397 sum += keyplace (map, x, y + 1, keycode, NO_PASS_DOORS, 1, RP);
398 sum += keyplace (map, x - 1, y, keycode, NO_PASS_DOORS, 1, RP);
399 sum += keyplace (map, x, y - 1, keycode, NO_PASS_DOORS, 1, RP);
400
401 if (sum < 2) /* we might have made a disconnected map-place more keys. */
402 { /* diagonally this time. */
403 keyplace (map, x + 1, y + 1, keycode, NO_PASS_DOORS, 1, RP);
404 keyplace (map, x + 1, y - 1, keycode, NO_PASS_DOORS, 1, RP);
405 keyplace (map, x - 1, y + 1, keycode, NO_PASS_DOORS, 1, RP);
406 keyplace (map, x - 1, y - 1, keycode, NO_PASS_DOORS, 1, RP);
407 }
408
409 return 1;
410 }
411 }
412
413 if (!the_keymaster)
414 {
415 the_key->x = kx;
416 the_key->y = ky;
417 insert_ob_in_map (the_key, map, NULL, 0);
418 return 1;
419 }
420
421 insert_ob_in_ob (the_key, the_keymaster);
422 return 1;
423}
424
425
426
427/* both find_monster_in_room routines need to have access to this. */ 449/* both find_monster_in_room routines need to have access to this. */
428 450
429object *theMonsterToFind; 451static object *theMonsterToFind;
430 452
431/* a recursive routine which will return a monster, eventually,if there is one. 453/* a recursive routine which will return a monster, eventually,if there is one.
432 it does a check-off on the layout, converting 0's to 1's */ 454 it does a check-off on the maze, converting 0's to 1's */
433 455static object *
434object *
435find_monster_in_room_recursive (char **layout, maptile *map, int x, int y, random_map_params *RP) 456find_monster_in_room_recursive (layout &maze, maptile *map, int x, int y)
436{ 457{
437 int i, j; 458 int i, j;
438 459
439 /* if we've found a monster already, leave */ 460 /* if we've found a monster already, leave */
440 if (theMonsterToFind != NULL) 461 if (theMonsterToFind != NULL)
441 return theMonsterToFind; 462 return theMonsterToFind;
442 463
443 /* bounds check x and y */ 464 /* bounds check x and y */
444 if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize)) 465 if (!(x >= 0 && y >= 0 && x < maze.w && y < maze.h))
445 return theMonsterToFind; 466 return theMonsterToFind;
446 467
447 /* if the square is blocked or searched already, leave */ 468 /* if the square is blocked or searched already, leave */
448 if (layout[x][y] != 0) 469 if (maze[x][y] != 0)
449 return theMonsterToFind; /* might be NULL, that's fine. */ 470 return theMonsterToFind; /* might be NULL, that's fine. */
450 471
451 /* check the current square for a monster. If there is one, 472 /* check the current square for a monster. If there is one,
452 set theMonsterToFind and return it. */ 473 set theMonsterToFind and return it. */
453 layout[x][y] = 1; 474 maze[x][y] = 1;
454 if (GET_MAP_FLAGS (map, x, y) & P_IS_ALIVE) 475 if (GET_MAP_FLAGS (map, x, y) & P_IS_ALIVE)
455 { 476 {
456 object *the_monster = GET_MAP_OB (map, x, y); 477 object *the_monster = GET_MAP_OB (map, x, y);
457 478
458 /* check off this point */ 479 /* check off this point */
459 for (; the_monster != NULL && (!QUERY_FLAG (the_monster, FLAG_ALIVE)); the_monster = the_monster->above); 480 for (; the_monster && (!the_monster->flag [FLAG_ALIVE]); the_monster = the_monster->above);
460 if (the_monster && QUERY_FLAG (the_monster, FLAG_ALIVE)) 481 if (the_monster && the_monster->flag [FLAG_ALIVE])
461 { 482 {
462 theMonsterToFind = the_monster; 483 theMonsterToFind = the_monster;
463 return theMonsterToFind; 484 return theMonsterToFind;
464 } 485 }
465 } 486 }
466 487
467 /* now search all the 8 squares around recursively for a monster,in random order */ 488 /* now search all the 8 squares around recursively for a monster,in random order */
468 for (i = RANDOM () % 8, j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) 489 for (i = rmg_rndm (8), j = 0; j < 8 && !theMonsterToFind; i++, j++)
469 { 490 {
470 theMonsterToFind = find_monster_in_room_recursive (layout, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); 491 theMonsterToFind = find_monster_in_room_recursive (maze, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1]);
471 if (theMonsterToFind != NULL) 492 if (theMonsterToFind)
472 return theMonsterToFind; 493 return theMonsterToFind;
473 } 494 }
495
474 return theMonsterToFind; 496 return theMonsterToFind;
475} 497}
476
477 498
478/* sets up some data structures: the _recursive form does the 499/* sets up some data structures: the _recursive form does the
479 real work. */ 500 real work. */
480 501static object *
481object *
482find_monster_in_room (maptile *map, int x, int y, random_map_params *RP) 502find_monster_in_room (maptile *map, int x, int y)
483{ 503{
484 char **layout2; 504 layout layout2 (map->width, map->height);
485 int i, j; 505
506 // find walls
507 for (int i = 0; i < layout2.w; i++)
508 for (int j = 0; j < layout2.h; j++)
509 layout2[i][j] = wall_blocked (map, i, j) ? '#' : 0;
486 510
487 theMonsterToFind = 0; 511 theMonsterToFind = 0;
488 layout2 = (char **) calloc (sizeof (char *), RP->Xsize);
489 /* allocate and copy the layout, converting C to 0. */
490 for (i = 0; i < RP->Xsize; i++)
491 {
492 layout2[i] = (char *) calloc (sizeof (char), RP->Ysize);
493 for (j = 0; j < RP->Ysize; j++)
494 {
495 if (wall_blocked (map, i, j))
496 layout2[i][j] = '#';
497 }
498 }
499 theMonsterToFind = find_monster_in_room_recursive (layout2, map, x, y, RP); 512 theMonsterToFind = find_monster_in_room_recursive (layout2, map, x, y);
500
501 /* deallocate the temp. layout */
502 for (i = 0; i < RP->Xsize; i++)
503 {
504 free (layout2[i]);
505 }
506 free (layout2);
507 513
508 return theMonsterToFind; 514 return theMonsterToFind;
509} 515}
510
511/* a datastructure needed by find_spot_in_room and find_spot_in_room_recursive */
512int *room_free_spots_x;
513int *room_free_spots_y;
514int number_of_free_spots_in_room;
515 516
516/* the workhorse routine, which finds the free spots in a room: 517/* the workhorse routine, which finds the free spots in a room:
517a datastructure of free points is set up, and a position chosen from 518a datastructure of free points is set up, and a position chosen from
518that datastructure. */ 519that datastructure. */
519void 520static void
520find_spot_in_room_recursive (char **layout, int x, int y, random_map_params *RP) 521find_spot_in_room_recursive (layout &maze, fixed_stack<point> &spots, int x, int y)
521{ 522{
522 int i, j;
523
524 /* bounds check x and y */ 523 /* bounds check x and y */
525 if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize)) 524 if (!(x >= 0 && y >= 0 && x < maze.w && y < maze.h))
526 return; 525 return;
527 526
528 /* if the square is blocked or searched already, leave */ 527 /* if the square is blocked or searched already, leave */
529 if (layout[x][y] != 0) 528 if (maze[x][y] != 0)
530 return; 529 return;
531 530
532 /* set the current square as checked, and add it to the list. 531 /* set the current square as checked, and add it to the list.
533 set theMonsterToFind and return it. */ 532 set theMonsterToFind and return it. */
534 /* check off this point */ 533 /* check off this point */
535 layout[x][y] = 1; 534 maze[x][y] = 1;
536 room_free_spots_x[number_of_free_spots_in_room] = x; 535 spots.push (point (x, y));
537 room_free_spots_y[number_of_free_spots_in_room] = y;
538 number_of_free_spots_in_room++;
539 536
540 /* now search all the 8 squares around recursively for free spots,in random order */ 537 /* now search all the 8 squares around recursively for free spots,in random order */
541 for (i = RANDOM () % 8, j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) 538 for (int i = rmg_rndm (8), j = 0; j < 8 && !theMonsterToFind; i++, j++)
542 find_spot_in_room_recursive (layout, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); 539 find_spot_in_room_recursive (maze, spots, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1]);
543 540
544} 541}
545 542
546/* find a random non-blocked spot in this room to drop a key. */ 543/* find a random non-blocked spot in this room to drop a key. */
547void 544static void
548find_spot_in_room (maptile *map, int x, int y, int *kx, int *ky, random_map_params *RP) 545find_spot_in_room (maptile *map, int x, int y, int *kx, int *ky)
549{ 546{
550 char **layout2; 547 fixed_stack<point> spots (map->width * map->height);
551 int i, j;
552 548
553 number_of_free_spots_in_room = 0; 549 layout layout2 (map->width, map->height);
554 room_free_spots_x = (int *) calloc (sizeof (int), RP->Xsize * RP->Ysize);
555 room_free_spots_y = (int *) calloc (sizeof (int), RP->Xsize * RP->Ysize);
556 550
557 layout2 = (char **) calloc (sizeof (char *), RP->Xsize);
558 /* allocate and copy the layout, converting C to 0. */ 551 /* allocate and copy the maze, converting C to 0. */
559 for (i = 0; i < RP->Xsize; i++) 552 for (int i = 0; i < map->width; i++)
560 { 553 for (int j = 0; j < map->height; j++)
561 layout2[i] = (char *) calloc (sizeof (char), RP->Ysize); 554 layout2 [i][j] = wall_blocked (map, i, j) ? '#' : 0;
562 for (j = 0; j < RP->Ysize; j++)
563 if (wall_blocked (map, i, j))
564 layout2[i][j] = '#';
565 }
566 555
567 /* setup num_free_spots and room_free_spots */ 556 /* setup num_free_spots and room_free_spots */
568 find_spot_in_room_recursive (layout2, x, y, RP); 557 find_spot_in_room_recursive (layout2, spots, x, y);
569 558
570 if (number_of_free_spots_in_room > 0) 559 if (spots.size)
571 {
572 i = RANDOM () % number_of_free_spots_in_room;
573 *kx = room_free_spots_x[i];
574 *ky = room_free_spots_y[i];
575 } 560 {
561 point p = spots [rmg_rndm (spots.size)];
576 562
577 /* deallocate the temp. layout */ 563 *kx = p.x;
578 for (i = 0; i < RP->Xsize; i++) 564 *ky = p.y;
579 free (layout2[i]); 565 }
580
581 free (layout2);
582 free (room_free_spots_x);
583 free (room_free_spots_y);
584} 566}
585
586 567
587/* searches the map for a spot with walls around it. The more 568/* searches the map for a spot with walls around it. The more
588 walls the better, but it'll settle for 1 wall, or even 0, but 569 walls the better, but it'll settle for 1 wall, or even 0, but
589 it'll return 0 if no FREE spots are found.*/ 570 it'll return 0 if no FREE spots are found.*/
590void 571static void
591find_enclosed_spot (maptile *map, int *cx, int *cy, random_map_params *RP) 572find_enclosed_spot (maptile *map, int *cx, int *cy)
592{ 573{
593 int x, y; 574 int x, y;
594 int i; 575 int i;
595 576
596 x = *cx; 577 x = *cx;
600 { 581 {
601 int lx, ly, sindex; 582 int lx, ly, sindex;
602 583
603 lx = x + freearr_x[i]; 584 lx = x + freearr_x[i];
604 ly = y + freearr_y[i]; 585 ly = y + freearr_y[i];
605 sindex = surround_flag3 (map, lx, ly, RP); 586 sindex = surround_flag3 (map, lx, ly);
606 /* if it's blocked on 3 sides, it's enclosed */ 587 /* if it's blocked on 3 sides, it's enclosed */
607 if (sindex == 7 || sindex == 11 || sindex == 13 || sindex == 14) 588 if (sindex == 7 || sindex == 11 || sindex == 13 || sindex == 14)
608 { 589 {
609 *cx = lx; 590 *cx = lx;
610 *cy = ly; 591 *cy = ly;
618 { 599 {
619 int lx, ly, sindex; 600 int lx, ly, sindex;
620 601
621 lx = x + freearr_x[i]; 602 lx = x + freearr_x[i];
622 ly = y + freearr_y[i]; 603 ly = y + freearr_y[i];
623 sindex = surround_flag3 (map, lx, ly, RP); 604 sindex = surround_flag3 (map, lx, ly);
624 /* if it's blocked on 3 sides, it's enclosed */ 605 /* if it's blocked on 3 sides, it's enclosed */
625 if (sindex == 3 || sindex == 5 || sindex == 9 || sindex == 6 || sindex == 10 || sindex == 12) 606 if (sindex == 3 || sindex == 5 || sindex == 9 || sindex == 6 || sindex == 10 || sindex == 12)
626 { 607 {
627 *cx = lx; 608 *cx = lx;
628 *cy = ly; 609 *cy = ly;
635 { 616 {
636 int lx, ly, sindex; 617 int lx, ly, sindex;
637 618
638 lx = x + freearr_x[i]; 619 lx = x + freearr_x[i];
639 ly = y + freearr_y[i]; 620 ly = y + freearr_y[i];
640 sindex = surround_flag3 (map, lx, ly, RP); 621 sindex = surround_flag3 (map, lx, ly);
641 /* if it's blocked on 3 sides, it's enclosed */ 622 /* if it's blocked on 3 sides, it's enclosed */
642 if (sindex) 623 if (sindex)
643 { 624 {
644 *cx = lx; 625 *cx = lx;
645 *cy = ly; 626 *cy = ly;
646 return; 627 return;
647 } 628 }
648 } 629 }
649 /* give up and return the closest free spot. */ 630 /* give up and return the closest free spot. */
650 i = find_free_spot (&archetype::find ("chest")->clone, map, x, y, 1, SIZEOFFREE1 + 1); 631 i = rmg_find_free_spot (archetype::find (shstr_chest), map, x, y, 1, SIZEOFFREE1 + 1);
651 632
652 if (i != -1) 633 if (i != -1)
653 { 634 {
654 *cx = x + freearr_x[i]; 635 *cx = x + freearr_x[i];
655 *cy = y + freearr_y[i]; 636 *cy = y + freearr_y[i];
660 *cx = -1; 641 *cx = -1;
661 *cy = -1; 642 *cy = -1;
662 } 643 }
663} 644}
664 645
665 646static void
666void
667remove_monsters (int x, int y, maptile *map) 647remove_monsters (int x, int y, maptile *map)
668{ 648{
669 object *tmp; 649 for (object *tmp = GET_MAP_OB (map, x, y); tmp; )
670
671 for (tmp = GET_MAP_OB (map, x, y); tmp != NULL; tmp = tmp->above)
672 if (QUERY_FLAG (tmp, FLAG_ALIVE))
673 { 650 {
674 if (tmp->head) 651 object *next = tmp->above;
675 tmp = tmp->head; 652
676 tmp->remove (); 653 if (tmp->flag [FLAG_ALIVE])
677 tmp->destroy (); 654 tmp->head_ ()->destroy ();
678 tmp = GET_MAP_OB (map, x, y); 655
679 if (tmp == NULL) 656 tmp = next;
680 break;
681 }; 657 }
682} 658}
683
684 659
685/* surrounds the point x,y by doors, so as to enclose something, like 660/* surrounds the point x,y by doors, so as to enclose something, like
686 a chest. It only goes as far as the 8 squares surrounding, and 661 a chest. It only goes as far as the 8 squares surrounding, and
687 it'll remove any monsters it finds.*/ 662 it'll remove any monsters it finds.*/
688 663static object **
689object **
690surround_by_doors (maptile *map, char **layout, int x, int y, int opts) 664surround_by_doors (maptile *map, char **maze, int x, int y, int opts)
691{ 665{
692 int i; 666 int i;
693 char *doors[2]; 667 const char *doors[2];
694 object **doorlist; 668 object **doorlist;
695 int ndoors_made = 0; 669 int ndoors_made = 0;
696 doorlist = (object **) calloc (9, sizeof (object *)); /* 9 doors so we can hold termination null */ 670 doorlist = (object **) calloc (9, sizeof (object *)); /* 9 doors so we can hold termination null */
697 671
698 /* this is a list we pick from, for horizontal and vertical doors */ 672 /* this is a list we pick from, for horizontal and vertical doors */
710 /* place doors in all the 8 adjacent unblocked squares. */ 684 /* place doors in all the 8 adjacent unblocked squares. */
711 for (i = 1; i < 9; i++) 685 for (i = 1; i < 9; i++)
712 { 686 {
713 int x1 = x + freearr_x[i], y1 = y + freearr_y[i]; 687 int x1 = x + freearr_x[i], y1 = y + freearr_y[i];
714 688
715 if (!wall_blocked (map, x1, y1) || layout[x1][y1] == '>') 689 if (!wall_blocked (map, x1, y1) && maze[x1][y1] == '>')
716 { /* place a door */ 690 { /* place a door */
691 remove_monsters (x1, y1, map);
692
717 object *new_door = get_archetype ((freearr_x[i] == 0) ? doors[1] : doors[0]); 693 object *new_door = get_archetype (freearr_x[i] == 0 ? doors[1] : doors[0]);
718 694 map->insert (new_door, x1, y1);
719 new_door->x = x + freearr_x[i];
720 new_door->y = y + freearr_y[i];
721 remove_monsters (new_door->x, new_door->y, map);
722 insert_ob_in_map (new_door, map, NULL, 0);
723 doorlist[ndoors_made] = new_door; 695 doorlist[ndoors_made] = new_door;
724 ndoors_made++; 696 ndoors_made++;
725 } 697 }
726 } 698 }
699
727 return doorlist; 700 return doorlist;
728} 701}
729 702
730
731/* returns the first door in this square, or NULL if there isn't a door. */ 703/* returns the first door in this square, or NULL if there isn't a door. */
732object * 704static object *
733door_in_square (maptile *map, int x, int y) 705door_in_square (maptile *map, int x, int y)
734{ 706{
735 object *tmp;
736
737 for (tmp = GET_MAP_OB (map, x, y); tmp != NULL; tmp = tmp->above) 707 for (object *tmp = GET_MAP_OB (map, x, y); tmp; tmp = tmp->above)
738 if (tmp->type == DOOR || tmp->type == LOCKED_DOOR) 708 if (tmp->type == DOOR || tmp->type == LOCKED_DOOR)
739 return tmp; 709 return tmp;
710
740 return NULL; 711 return NULL;
741} 712}
742 713
743
744/* the workhorse routine, which finds the doors in a room */ 714/* the workhorse routine, which finds the doors in a room */
745void 715static void
746find_doors_in_room_recursive (char **layout, maptile *map, int x, int y, object **doorlist, int *ndoors, random_map_params *RP) 716find_doors_in_room_recursive (layout &maze, maptile *map, int x, int y, object **doorlist, int *ndoors)
747{ 717{
748 int i, j; 718 int i, j;
749 object *door; 719 object *door;
750 720
751 /* bounds check x and y */ 721 /* bounds check x and y */
752 if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize)) 722 if (!(x >= 0 && y >= 0 && x < maze.w && y < maze.h))
753 return; 723 return;
754 724
755 /* if the square is blocked or searched already, leave */ 725 /* if the square is blocked or searched already, leave */
756 if (layout[x][y] == 1) 726 if (maze[x][y] == 1)
757 return; 727 return;
758 728
759 /* check off this point */ 729 /* check off this point */
760 if (layout[x][y] == '#') 730 if (maze[x][y] == '#')
761 { /* there could be a door here */ 731 { /* there could be a door here */
762 layout[x][y] = 1; 732 maze[x][y] = 1;
763 door = door_in_square (map, x, y); 733 door = door_in_square (map, x, y);
764 if (door) 734 if (door)
765 { 735 {
766 doorlist[*ndoors] = door; 736 doorlist[*ndoors] = door;
737
767 if (*ndoors > 1022) /* eek! out of memory */ 738 if (*ndoors > 1022) /* eek! out of memory */
768 { 739 {
769 LOG (llevError, "find_doors_in_room_recursive:Too many doors for memory allocated!\n"); 740 LOG (llevError, "find_doors_in_room_recursive:Too many doors for memory allocated!\n");
770 return; 741 return;
771 } 742 }
773 *ndoors = *ndoors + 1; 744 *ndoors = *ndoors + 1;
774 } 745 }
775 } 746 }
776 else 747 else
777 { 748 {
778 layout[x][y] = 1; 749 maze[x][y] = 1;
750
779 /* now search all the 8 squares around recursively for free spots,in random order */ 751 /* now search all the 8 squares around recursively for free spots,in random order */
780 for (i = RANDOM () % 8, j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) 752 for (i = rmg_rndm (8), j = 0; j < 8 && !theMonsterToFind; i++, j++)
781 find_doors_in_room_recursive (layout, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], doorlist, ndoors, RP); 753 find_doors_in_room_recursive (maze, map,
754 x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1],
755 doorlist, ndoors);
782 } 756 }
783} 757}
784 758
785/* find a random non-blocked spot in this room to drop a key. */ 759/* find a random non-blocked spot in this room to drop a key. */
786object ** 760static object **
787find_doors_in_room (maptile *map, int x, int y, random_map_params *RP) 761find_doors_in_room (maptile *map, int x, int y)
788{ 762{
789 char **layout2;
790 object **doorlist;
791 int i, j; 763 int i, j;
792 int ndoors = 0; 764 int ndoors = 0;
793 765
794 doorlist = (object **) calloc (sizeof (int), 1024); 766 object **doorlist = (object **)calloc (sizeof (int), 1024);
795 767
796 layout2 = (char **) calloc (sizeof (char *), RP->Xsize); 768 layout layout2 (map->width, map->height);
769 layout2.clear ();
770
797 /* allocate and copy the layout, converting C to 0. */ 771 /* allocate and copy the maze, converting C to 0. */
798 for (i = 0; i < RP->Xsize; i++) 772 for (i = 0; i < map->width; i++)
799 {
800 layout2[i] = (char *) calloc (sizeof (char), RP->Ysize);
801 for (j = 0; j < RP->Ysize; j++) 773 for (j = 0; j < map->height; j++)
802 { 774 layout2[i][j] = wall_blocked (map, i, j) ? '#' : 0;
803 if (wall_blocked (map, i, j))
804 layout2[i][j] = '#';
805 }
806 }
807 775
808 /* setup num_free_spots and room_free_spots */ 776 /* setup num_free_spots and room_free_spots */
809 find_doors_in_room_recursive (layout2, map, x, y, doorlist, &ndoors, RP); 777 find_doors_in_room_recursive (layout2, map, x, y, doorlist, &ndoors);
810 778
811 /* deallocate the temp. layout */
812 for (i = 0; i < RP->Xsize; i++)
813 free (layout2[i]);
814
815 free (layout2);
816 return doorlist; 779 return doorlist;
817} 780}
818
819
820 781
821/* locks and/or hides all the doors in doorlist, or does nothing if 782/* locks and/or hides all the doors in doorlist, or does nothing if
822 opts doesn't say to lock/hide doors. */ 783 opts doesn't say to lock/hide doors. */
823 784static void
824void
825lock_and_hide_doors (object **doorlist, maptile *map, int opts, random_map_params *RP) 785lock_and_hide_doors (object **doorlist, maptile *map, int opts, random_map_params *RP)
826{ 786{
827 object *door; 787 object *door;
828 int i; 788 int i;
829 789
830 /* lock the doors and hide the keys. */ 790 /* lock the doors and hide the keys. */
831 791
832 if (opts & DOORED) 792 if (opts & DOORED)
833 { 793 {
834 for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++) 794 for (i = 0, door = doorlist[0]; doorlist[i]; i++)
835 { 795 {
836 object *new_door = get_archetype ("locked_door1"); 796 object *new_door = get_archetype (shstr_locked_door1);
837 char keybuf[1024];
838 797
839 door = doorlist[i]; 798 door = doorlist[i];
840 new_door->face = door->face; 799 new_door->face = door->face;
841 new_door->x = door->x; 800 new_door->x = door->x;
842 new_door->y = door->y; 801 new_door->y = door->y;
843 door->remove ();
844 door->destroy (); 802 door->destroy ();
845 doorlist[i] = new_door; 803 doorlist[i] = new_door;
846 insert_ob_in_map (new_door, map, NULL, 0); 804 insert_ob_in_map (new_door, map, NULL, 0);
847 sprintf (keybuf, "%d", (int) RANDOM ()); 805 new_door->slaying = format ("RMG-%d-%d", (int)rmg_rndm (1000000000), (int)rmg_rndm (1000000000));
848 new_door->slaying = keybuf;
849 keyplace (map, new_door->x, new_door->y, keybuf, NO_PASS_DOORS, 2, RP); 806 keyplace (map, new_door->x, new_door->y, new_door->slaying, NO_PASS_DOORS, 2);
850 } 807 }
851 } 808 }
852 809
853 /* change the faces of the doors and surrounding walls to hide them. */ 810 /* change the faces of the doors and surrounding walls to hide them. */
854 if (opts & HIDDEN) 811 if (opts & HIDDEN)
863 { 820 {
864 retrofit_joined_wall (map, door->x - 1, door->y, 0, RP); 821 retrofit_joined_wall (map, door->x - 1, door->y, 0, RP);
865 retrofit_joined_wall (map, door->x + 1, door->y, 0, RP); 822 retrofit_joined_wall (map, door->x + 1, door->y, 0, RP);
866 retrofit_joined_wall (map, door->x, door->y - 1, 0, RP); 823 retrofit_joined_wall (map, door->x, door->y - 1, 0, RP);
867 retrofit_joined_wall (map, door->x, door->y + 1, 0, RP); 824 retrofit_joined_wall (map, door->x, door->y + 1, 0, RP);
825
868 door->face = wallface->face; 826 door->face = wallface->face;
869 if (!QUERY_FLAG (wallface, FLAG_REMOVED)) 827
870 wallface->remove ();
871 wallface->destroy (); 828 wallface->destroy ();
872 } 829 }
873 } 830 }
874 } 831 }
875} 832}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines