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.7 by root, Sat Sep 16 22:24:13 2006 UTC vs.
Revision 1.19 by root, Wed Jan 17 12:36:31 2007 UTC

1/* 1/*
2<<<<<<< treasure.C
2 CrossFire, A Multiplayer game for X-windows 3 * CrossFire, A Multiplayer game
3 4 *
5 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
4 Copyright (C) 2001 Mark Wedel & Crossfire Development Team 6 * Copyright (C) 2001 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen 7 * Copyright (C) 1992 Frank Tore Johansen
6 8 *
7 This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or 11 * the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version. 12 * (at your option) any later version.
11 13 *
12 This program is distributed in the hope that it will be useful, 14 * This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 17 * GNU General Public License for more details.
16 18 *
17 You should have received a copy of the GNU General Public License 19 * You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software 20 * along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 22 *
21 The authors can be reached via e-mail at <crossfire@schmorp.de> 23 * The authors can be reached via e-mail at <crossfire@schmorp.de>
22*/ 24 */
25=======
26 * CrossFire, A Multiplayer game for X-windows
27 *
28 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
29 * Copyright (C) 2001 Mark Wedel & Crossfire Development Team
30 * Copyright (C) 1992 Frank Tore Johansen
31 *
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
36 *
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
41 *
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
45 *
46 * The authors can be reached via e-mail at <crossfire@schmorp.de>
47 */
48>>>>>>> 1.18
23 49
24/* placing treasure in maps, where appropriate. */ 50/* placing treasure in maps, where appropriate. */
25
26
27 51
28#include <global.h> 52#include <global.h>
29#include <random_map.h> 53#include <random_map.h>
30#include <rproto.h> 54#include <rproto.h>
31 55
47 71
48/* returns true if square x,y has P_NO_PASS set, which is true for walls 72/* returns true if square x,y has P_NO_PASS set, which is true for walls
49 * and doors but not monsters. 73 * and doors but not monsters.
50 * This function is not map tile aware. 74 * This function is not map tile aware.
51 */ 75 */
52
53int 76int
54wall_blocked (maptile *m, int x, int y) 77wall_blocked (maptile *m, int x, int y)
55{ 78{
56 int r;
57
58 if (OUT_OF_REAL_MAP (m, x, y)) 79 if (OUT_OF_REAL_MAP (m, x, y))
59 return 1; 80 return 1;
81
60 r = GET_MAP_MOVE_BLOCK (m, x, y) & ~MOVE_BLOCK_DEFAULT; 82 int r = GET_MAP_MOVE_BLOCK (m, x, y) & ~MOVE_BLOCK_DEFAULT;
61 return r; 83 return r;
62} 84}
63 85
64/* place treasures in the map, given the 86/* place treasures in the map, given the
65map, (required) 87map, (required)
66layout, (required) 88layout, (required)
67treasure style (may be empty or NULL, or "none" to cause no treasure.) 89treasure style (may be empty or NULL, or "none" to cause no treasure.)
68treasureoptions (may be 0 for random choices or positive) 90treasureoptions (may be 0 for random choices or positive)
69*/ 91*/
70
71void 92void
72place_treasure (maptile *map, char **layout, char *treasure_style, int treasureoptions, RMParms * RP) 93place_treasure (maptile *map, char **layout, char *treasure_style, int treasureoptions, random_map_params *RP)
73{ 94{
74 char styledirname[256]; 95 char styledirname[1024];
75 char stylefilepath[256]; 96 char stylefilepath[1024];
76 maptile *style_map = 0; 97 maptile *style_map = 0;
77 int num_treasures; 98 int num_treasures;
78 99
79 /* bail out if treasure isn't wanted. */ 100 /* bail out if treasure isn't wanted. */
80 if (treasure_style) 101 if (treasure_style)
113 { 134 {
114 135
115 /* map_layout_style global, and is previously set */ 136 /* map_layout_style global, and is previously set */
116 switch (RP->map_layout_style) 137 switch (RP->map_layout_style)
117 { 138 {
118 case ONION_LAYOUT: 139 case LAYOUT_ONION:
119 case SPIRAL_LAYOUT: 140 case LAYOUT_SPIRAL:
120 case SQUARE_SPIRAL_LAYOUT: 141 case LAYOUT_SQUARE_SPIRAL:
121 { 142 {
122 int i, j; 143 int i, j;
123 144
124 /* search the onion for C's or '>', and put treasure there. */ 145 /* search the onion for C's or '>', and put treasure there. */
125 for (i = 0; i < RP->Xsize; i++) 146 for (i = 0; i < RP->Xsize; i++)
126 { 147 {
127 for (j = 0; j < RP->Ysize; j++) 148 for (j = 0; j < RP->Ysize; j++)
128 { 149 {
129 if (layout[i][j] == 'C' || layout[i][j] == '>') 150 if (layout[i][j] == 'C' || layout[i][j] == '>')
130 { 151 {
131 int tdiv = RP->symmetry_used; 152 int tdiv = RP->symmetry_used;
132 object **doorlist; 153 object **doorlist;
133 object *chest; 154 object *chest;
134 155
135 if (tdiv == 3) 156 if (tdiv == 3)
136 tdiv = 2; /* this symmetry uses a divisor of 2 */ 157 tdiv = 2; /* this symmetry uses a divisor of 2 */
137 /* don't put a chest on an exit. */ 158 /* don't put a chest on an exit. */
138 chest = place_chest (treasureoptions, i, j, map, style_map, num_treasures / tdiv, RP); 159 chest = place_chest (treasureoptions, i, j, map, style_map, num_treasures / tdiv, RP);
139 if (!chest) 160 if (!chest)
140 continue; /* if no chest was placed NEXT */ 161 continue; /* if no chest was placed NEXT */
141 if (treasureoptions & (DOORED | HIDDEN)) 162 if (treasureoptions & (DOORED | HIDDEN))
142 { 163 {
143 doorlist = find_doors_in_room (map, i, j, RP); 164 doorlist = find_doors_in_room (map, i, j, RP);
144 lock_and_hide_doors (doorlist, map, treasureoptions, RP); 165 lock_and_hide_doors (doorlist, map, treasureoptions, RP);
145 free (doorlist); 166 free (doorlist);
146 } 167 }
147 } 168 }
148 } 169 }
149 } 170 }
150 break; 171 break;
151 } 172 }
152 default: 173 default:
153 { 174 {
154 int i, j, tries; 175 int i, j, tries;
155 object *chest; 176 object *chest;
156 object **doorlist; 177 object **doorlist;
157 178
158 i = j = -1; 179 i = j = -1;
159 tries = 0; 180 tries = 0;
160 while (i == -1 && tries < 100) 181 while (i == -1 && tries < 100)
161 { 182 {
162 i = RANDOM () % (RP->Xsize - 2) + 1; 183 i = RANDOM () % (RP->Xsize - 2) + 1;
163 j = RANDOM () % (RP->Ysize - 2) + 1; 184 j = RANDOM () % (RP->Ysize - 2) + 1;
164 find_enclosed_spot (map, &i, &j, RP); 185 find_enclosed_spot (map, &i, &j, RP);
165 if (wall_blocked (map, i, j)) 186 if (wall_blocked (map, i, j))
166 i = -1; 187 i = -1;
167 tries++; 188 tries++;
168 } 189 }
169 chest = place_chest (treasureoptions, i, j, map, style_map, num_treasures, RP); 190 chest = place_chest (treasureoptions, i, j, map, style_map, num_treasures, RP);
170 if (!chest) 191 if (!chest)
171 return; 192 return;
172 i = chest->x; 193 i = chest->x;
173 j = chest->y; 194 j = chest->y;
174 if (treasureoptions & (DOORED | HIDDEN)) 195 if (treasureoptions & (DOORED | HIDDEN))
175 { 196 {
176 doorlist = surround_by_doors (map, layout, i, j, treasureoptions); 197 doorlist = surround_by_doors (map, layout, i, j, treasureoptions);
177 lock_and_hide_doors (doorlist, map, treasureoptions, RP); 198 lock_and_hide_doors (doorlist, map, treasureoptions, RP);
178 free (doorlist); 199 free (doorlist);
179 } 200 }
180 } 201 }
181 } 202 }
182 } 203 }
183 else 204 else
184 { /* DIFFUSE treasure layout */ 205 { /* DIFFUSE treasure layout */
185 int ti, i, j; 206 int ti, i, j;
191 place_chest (treasureoptions, i, j, map, style_map, 1, RP); 212 place_chest (treasureoptions, i, j, map, style_map, 1, RP);
192 } 213 }
193 } 214 }
194} 215}
195 216
196
197
198/* put a chest into the map, near x and y, with the treasure style 217/* put a chest into the map, near x and y, with the treasure style
199 determined (may be null, or may be a treasure list from lib/treasures, 218 determined (may be null, or may be a treasure list from lib/treasures,
200 if the global variable "treasurestyle" is set to that treasure list's name */ 219 if the global variable "treasurestyle" is set to that treasure list's name */
201 220
202object * 221object *
203place_chest (int treasureoptions, int x, int y, maptile *map, maptile *style_map, int n_treasures, RMParms * RP) 222place_chest (int treasureoptions, int x, int y, maptile *map, maptile *style_map, int n_treasures, random_map_params *RP)
204{ 223{
205 object *the_chest; 224 object *the_chest;
206 int i, xl, yl; 225 int i, xl, yl;
207 226
208 the_chest = get_archetype ("chest"); /* was "chest_2" */ 227 the_chest = get_archetype ("chest"); /* was "chest_2" */
209 228
210 /* first, find a place to put the chest. */ 229 /* first, find a place to put the chest. */
211 i = find_first_free_spot (the_chest, map, x, y); 230 i = find_first_free_spot (the_chest, map, x, y);
212 if (i == -1) 231 if (i == -1)
213 { 232 {
214 free_object (the_chest); 233 the_chest->destroy ();
215 return NULL; 234 return NULL;
216 } 235 }
236
217 xl = x + freearr_x[i]; 237 xl = x + freearr_x[i];
218 yl = y + freearr_y[i]; 238 yl = y + freearr_y[i];
219 239
220 /* if the placement is blocked, return a fail. */ 240 /* if the placement is blocked, return a fail. */
221 if (wall_blocked (map, xl, yl)) 241 if (wall_blocked (map, xl, yl))
222 return 0; 242 return 0;
223
224 243
225 /* put the treasures in the chest. */ 244 /* put the treasures in the chest. */
226 /* if(style_map) { */ 245 /* if(style_map) { */
227#if 0 /* don't use treasure style maps for now! */ 246#if 0 /* don't use treasure style maps for now! */
228 int ti; 247 int ti;
241 { /* use the style map */ 260 { /* use the style map */
242 the_chest->randomitems = tlist; 261 the_chest->randomitems = tlist;
243 the_chest->stats.hp = n_treasures; 262 the_chest->stats.hp = n_treasures;
244 } 263 }
245#endif 264#endif
246 else
247 { /* neither style_map no treasure list given */ 265 { /* neither style_map no treasure list given */
248 treasurelist *tlist = find_treasurelist ("chest"); 266 treasurelist *tlist = find_treasurelist ("chest");
249 267
250 the_chest->randomitems = tlist; 268 the_chest->randomitems = tlist;
251 the_chest->stats.hp = n_treasures; 269 the_chest->stats.hp = n_treasures;
265 if (the_trap) 283 if (the_trap)
266 { 284 {
267 object *new_trap; 285 object *new_trap;
268 286
269 new_trap = arch_to_object (the_trap->arch); 287 new_trap = arch_to_object (the_trap->arch);
270 copy_object (new_trap, the_trap); 288 new_trap->copy_to (the_trap);
271 new_trap->x = x; 289 new_trap->x = x;
272 new_trap->y = y; 290 new_trap->y = y;
273 insert_ob_in_ob (new_trap, the_chest); 291 insert_ob_in_ob (new_trap, the_chest);
274 } 292 }
275 } 293 }
279 the lockcode. It's not worth bothering to lock the chest if 297 the lockcode. It's not worth bothering to lock the chest if
280 there's only 1 treasure.... */ 298 there's only 1 treasure.... */
281 299
282 if ((treasureoptions & KEYREQUIRED) && n_treasures > 1) 300 if ((treasureoptions & KEYREQUIRED) && n_treasures > 1)
283 { 301 {
284 char keybuf[256]; 302 char keybuf[1024];
285 303
286 sprintf (keybuf, "%d", (int) RANDOM ()); 304 sprintf (keybuf, "%d", (int) RANDOM ());
287 the_chest->slaying = keybuf; 305 the_chest->slaying = keybuf;
288 keyplace (map, x, y, keybuf, PASS_DOORS, 1, RP); 306 keyplace (map, x, y, keybuf, PASS_DOORS, 1, RP);
289 } 307 }
297 315
298 316
299/* finds the closest monster and returns him, regardless of doors 317/* finds the closest monster and returns him, regardless of doors
300 or walls */ 318 or walls */
301object * 319object *
302find_closest_monster (maptile *map, int x, int y, RMParms * RP) 320find_closest_monster (maptile *map, int x, int y, random_map_params *RP)
303{ 321{
304 int i; 322 int i;
305 323
306 for (i = 0; i < SIZEOFFREE; i++) 324 for (i = 0; i < SIZEOFFREE; i++)
307 { 325 {
312 /* boundscheck */ 330 /* boundscheck */
313 if (lx >= 0 && ly >= 0 && lx < RP->Xsize && ly < RP->Ysize) 331 if (lx >= 0 && ly >= 0 && lx < RP->Xsize && ly < RP->Ysize)
314 /* don't bother searching this square unless the map says life exists. */ 332 /* don't bother searching this square unless the map says life exists. */
315 if (GET_MAP_FLAGS (map, lx, ly) & P_IS_ALIVE) 333 if (GET_MAP_FLAGS (map, lx, ly) & P_IS_ALIVE)
316 { 334 {
317 object *the_monster = get_map_ob (map, lx, ly); 335 object *the_monster = GET_MAP_OB (map, lx, ly);
318 336
319 for (; the_monster != NULL && (!QUERY_FLAG (the_monster, FLAG_MONSTER)); the_monster = the_monster->above); 337 for (; the_monster != NULL && (!QUERY_FLAG (the_monster, FLAG_MONSTER)); the_monster = the_monster->above);
320 if (the_monster && QUERY_FLAG (the_monster, FLAG_MONSTER)) 338 if (the_monster && QUERY_FLAG (the_monster, FLAG_MONSTER))
321 return the_monster; 339 return the_monster;
322 } 340 }
334 it will place 2-4 keys regardless of what nkeys is provided nkeys > 1. 352 it will place 2-4 keys regardless of what nkeys is provided nkeys > 1.
335 353
336 The idea is that you call keyplace on x,y where a door is, and it'll make 354 The idea is that you call keyplace on x,y where a door is, and it'll make
337 sure a key is placed on both sides of the door. 355 sure a key is placed on both sides of the door.
338*/ 356*/
339
340int 357int
341keyplace (maptile *map, int x, int y, char *keycode, int door_flag, int n_keys, RMParms * RP) 358keyplace (maptile *map, int x, int y, char *keycode, int door_flag, int n_keys, random_map_params *RP)
342{ 359{
343 int i, j; 360 int i, j;
344 int kx, ky; 361 int kx = 0, ky = 0;
345 object *the_keymaster; /* the monster that gets the key. */ 362 object *the_keymaster; /* the monster that gets the key. */
346 object *the_key; 363 object *the_key;
347 364
348 /* get a key and set its keycode */ 365 /* get a key and set its keycode */
349 the_key = get_archetype ("key2"); 366 the_key = get_archetype ("key2");
351 368
352 if (door_flag == PASS_DOORS) 369 if (door_flag == PASS_DOORS)
353 { 370 {
354 int tries = 0; 371 int tries = 0;
355 372
356 the_keymaster = NULL; 373 the_keymaster = 0;
357 while (tries < 15 && the_keymaster == NULL) 374 while (tries < 15 && !the_keymaster)
358 { 375 {
359 i = (RANDOM () % (RP->Xsize - 2)) + 1; 376 i = (RANDOM () % (RP->Xsize - 2)) + 1;
360 j = (RANDOM () % (RP->Ysize - 2)) + 1; 377 j = (RANDOM () % (RP->Ysize - 2)) + 1;
361 tries++; 378 tries++;
362 the_keymaster = find_closest_monster (map, i, j, RP); 379 the_keymaster = find_closest_monster (map, i, j, RP);
363 } 380 }
381
364 /* if we don't find a good keymaster, drop the key on the ground. */ 382 /* if we don't find a good keymaster, drop the key on the ground. */
365 if (the_keymaster == NULL) 383 if (!the_keymaster)
366 { 384 {
367 int freeindex; 385 int freeindex;
368 386
369 freeindex = -1; 387 freeindex = -1;
370 for (tries = 0; tries < 15 && freeindex == -1; tries++) 388 for (tries = 0; tries < 15 && freeindex == -1; tries++)
371 { 389 {
372 kx = (RANDOM () % (RP->Xsize - 2)) + 1; 390 kx = (RANDOM () % (RP->Xsize - 2)) + 1;
373 ky = (RANDOM () % (RP->Ysize - 2)) + 1; 391 ky = (RANDOM () % (RP->Ysize - 2)) + 1;
374 freeindex = find_first_free_spot (the_key, map, kx, ky); 392 freeindex = find_free_spot (the_key, map, kx, ky, 1, SIZEOFFREE1 + 1);
375 } 393 }
394
395 // can freeindex ever be < 0?
376 if (freeindex != -1) 396 if (freeindex >= 0)
377 { 397 {
378 kx += freearr_x[freeindex]; 398 kx += freearr_x [freeindex];
379 ky += freearr_y[freeindex]; 399 ky += freearr_y [freeindex];
380 } 400 }
381 } 401 }
382 } 402 }
383 else 403 else
384 { /* NO_PASS_DOORS --we have to work harder. */ 404 { /* NO_PASS_DOORS --we have to work harder. */
386 NO_PASS_DOORS is set. */ 406 NO_PASS_DOORS is set. */
387 if (n_keys == 1) 407 if (n_keys == 1)
388 { 408 {
389 if (wall_blocked (map, x, y)) 409 if (wall_blocked (map, x, y))
390 return 0; 410 return 0;
411
391 the_keymaster = find_monster_in_room (map, x, y, RP); 412 the_keymaster = find_monster_in_room (map, x, y, RP);
392 if (the_keymaster == NULL) /* if fail, find a spot to drop the key. */ 413 if (!the_keymaster) /* if fail, find a spot to drop the key. */
393 find_spot_in_room (map, x, y, &kx, &ky, RP); 414 find_spot_in_room (map, x, y, &kx, &ky, RP);
394 } 415 }
395 else 416 else
396 { 417 {
397 int sum = 0; /* count how many keys we actually place */ 418 int sum = 0; /* count how many keys we actually place */
399 /* I'm lazy, so just try to place in all 4 directions. */ 420 /* I'm lazy, so just try to place in all 4 directions. */
400 sum += keyplace (map, x + 1, y, keycode, NO_PASS_DOORS, 1, RP); 421 sum += keyplace (map, x + 1, y, keycode, NO_PASS_DOORS, 1, RP);
401 sum += keyplace (map, x, y + 1, keycode, NO_PASS_DOORS, 1, RP); 422 sum += keyplace (map, x, y + 1, keycode, NO_PASS_DOORS, 1, RP);
402 sum += keyplace (map, x - 1, y, keycode, NO_PASS_DOORS, 1, RP); 423 sum += keyplace (map, x - 1, y, keycode, NO_PASS_DOORS, 1, RP);
403 sum += keyplace (map, x, y - 1, keycode, NO_PASS_DOORS, 1, RP); 424 sum += keyplace (map, x, y - 1, keycode, NO_PASS_DOORS, 1, RP);
425
404 if (sum < 2) /* we might have made a disconnected map-place more keys. */ 426 if (sum < 2) /* we might have made a disconnected map-place more keys. */
405 { /* diagnoally this time. */ 427 { /* diagonally this time. */
406 keyplace (map, x + 1, y + 1, keycode, NO_PASS_DOORS, 1, RP); 428 keyplace (map, x + 1, y + 1, keycode, NO_PASS_DOORS, 1, RP);
407 keyplace (map, x + 1, y - 1, keycode, NO_PASS_DOORS, 1, RP); 429 keyplace (map, x + 1, y - 1, keycode, NO_PASS_DOORS, 1, RP);
408 keyplace (map, x - 1, y + 1, keycode, NO_PASS_DOORS, 1, RP); 430 keyplace (map, x - 1, y + 1, keycode, NO_PASS_DOORS, 1, RP);
409 keyplace (map, x - 1, y - 1, keycode, NO_PASS_DOORS, 1, RP); 431 keyplace (map, x - 1, y - 1, keycode, NO_PASS_DOORS, 1, RP);
410 } 432 }
433
411 return 1; 434 return 1;
412 } 435 }
413 } 436 }
414 437
415 if (the_keymaster == NULL) 438 if (!the_keymaster)
416 { 439 {
417 the_key->x = kx; 440 the_key->x = kx;
418 the_key->y = ky; 441 the_key->y = ky;
419 insert_ob_in_map (the_key, map, NULL, 0); 442 insert_ob_in_map (the_key, map, NULL, 0);
420 return 1; 443 return 1;
432 455
433/* a recursive routine which will return a monster, eventually,if there is one. 456/* a recursive routine which will return a monster, eventually,if there is one.
434 it does a check-off on the layout, converting 0's to 1's */ 457 it does a check-off on the layout, converting 0's to 1's */
435 458
436object * 459object *
437find_monster_in_room_recursive (char **layout, maptile *map, int x, int y, RMParms * RP) 460find_monster_in_room_recursive (char **layout, maptile *map, int x, int y, random_map_params *RP)
438{ 461{
439 int i, j; 462 int i, j;
440 463
441 /* if we've found a monster already, leave */ 464 /* if we've found a monster already, leave */
442 if (theMonsterToFind != NULL) 465 if (theMonsterToFind != NULL)
453 /* check the current square for a monster. If there is one, 476 /* check the current square for a monster. If there is one,
454 set theMonsterToFind and return it. */ 477 set theMonsterToFind and return it. */
455 layout[x][y] = 1; 478 layout[x][y] = 1;
456 if (GET_MAP_FLAGS (map, x, y) & P_IS_ALIVE) 479 if (GET_MAP_FLAGS (map, x, y) & P_IS_ALIVE)
457 { 480 {
458 object *the_monster = get_map_ob (map, x, y); 481 object *the_monster = GET_MAP_OB (map, x, y);
459 482
460 /* check off this point */ 483 /* check off this point */
461 for (; the_monster != NULL && (!QUERY_FLAG (the_monster, FLAG_ALIVE)); the_monster = the_monster->above); 484 for (; the_monster != NULL && (!QUERY_FLAG (the_monster, FLAG_ALIVE)); the_monster = the_monster->above);
462 if (the_monster && QUERY_FLAG (the_monster, FLAG_ALIVE)) 485 if (the_monster && QUERY_FLAG (the_monster, FLAG_ALIVE))
463 { 486 {
479 502
480/* sets up some data structures: the _recursive form does the 503/* sets up some data structures: the _recursive form does the
481 real work. */ 504 real work. */
482 505
483object * 506object *
484find_monster_in_room (maptile *map, int x, int y, RMParms * RP) 507find_monster_in_room (maptile *map, int x, int y, random_map_params *RP)
485{ 508{
486 char **layout2; 509 char **layout2;
487 int i, j; 510 int i, j;
488 511
489 theMonsterToFind = 0; 512 theMonsterToFind = 0;
508 free (layout2); 531 free (layout2);
509 532
510 return theMonsterToFind; 533 return theMonsterToFind;
511} 534}
512 535
513
514
515
516/* a datastructure needed by find_spot_in_room and find_spot_in_room_recursive */ 536/* a datastructure needed by find_spot_in_room and find_spot_in_room_recursive */
517int *room_free_spots_x; 537int *room_free_spots_x;
518int *room_free_spots_y; 538int *room_free_spots_y;
519int number_of_free_spots_in_room; 539int number_of_free_spots_in_room;
520 540
521/* the workhorse routine, which finds the free spots in a room: 541/* the workhorse routine, which finds the free spots in a room:
522a datastructure of free points is set up, and a position chosen from 542a datastructure of free points is set up, and a position chosen from
523that datastructure. */ 543that datastructure. */
524
525void 544void
526find_spot_in_room_recursive (char **layout, int x, int y, RMParms * RP) 545find_spot_in_room_recursive (char **layout, int x, int y, random_map_params *RP)
527{ 546{
528 int i, j; 547 int i, j;
529 548
530 /* bounds check x and y */ 549 /* bounds check x and y */
531 if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize)) 550 if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize))
540 /* check off this point */ 559 /* check off this point */
541 layout[x][y] = 1; 560 layout[x][y] = 1;
542 room_free_spots_x[number_of_free_spots_in_room] = x; 561 room_free_spots_x[number_of_free_spots_in_room] = x;
543 room_free_spots_y[number_of_free_spots_in_room] = y; 562 room_free_spots_y[number_of_free_spots_in_room] = y;
544 number_of_free_spots_in_room++; 563 number_of_free_spots_in_room++;
564
545 /* now search all the 8 squares around recursively for free spots,in random order */ 565 /* now search all the 8 squares around recursively for free spots,in random order */
546 for (i = RANDOM () % 8, j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) 566 for (i = RANDOM () % 8, j = 0; j < 8 && theMonsterToFind == NULL; i++, j++)
547 {
548 find_spot_in_room_recursive (layout, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); 567 find_spot_in_room_recursive (layout, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP);
549 }
550 568
551} 569}
552 570
553/* find a random non-blocked spot in this room to drop a key. */ 571/* find a random non-blocked spot in this room to drop a key. */
554void 572void
555find_spot_in_room (maptile *map, int x, int y, int *kx, int *ky, RMParms * RP) 573find_spot_in_room (maptile *map, int x, int y, int *kx, int *ky, random_map_params *RP)
556{ 574{
557 char **layout2; 575 char **layout2;
558 int i, j; 576 int i, j;
559 577
560 number_of_free_spots_in_room = 0; 578 number_of_free_spots_in_room = 0;
565 /* allocate and copy the layout, converting C to 0. */ 583 /* allocate and copy the layout, converting C to 0. */
566 for (i = 0; i < RP->Xsize; i++) 584 for (i = 0; i < RP->Xsize; i++)
567 { 585 {
568 layout2[i] = (char *) calloc (sizeof (char), RP->Ysize); 586 layout2[i] = (char *) calloc (sizeof (char), RP->Ysize);
569 for (j = 0; j < RP->Ysize; j++) 587 for (j = 0; j < RP->Ysize; j++)
570 {
571 if (wall_blocked (map, i, j)) 588 if (wall_blocked (map, i, j))
572 layout2[i][j] = '#'; 589 layout2[i][j] = '#';
573 }
574 } 590 }
575 591
576 /* setup num_free_spots and room_free_spots */ 592 /* setup num_free_spots and room_free_spots */
577 find_spot_in_room_recursive (layout2, x, y, RP); 593 find_spot_in_room_recursive (layout2, x, y, RP);
578 594
583 *ky = room_free_spots_y[i]; 599 *ky = room_free_spots_y[i];
584 } 600 }
585 601
586 /* deallocate the temp. layout */ 602 /* deallocate the temp. layout */
587 for (i = 0; i < RP->Xsize; i++) 603 for (i = 0; i < RP->Xsize; i++)
588 {
589 free (layout2[i]); 604 free (layout2[i]);
590 } 605
591 free (layout2); 606 free (layout2);
592 free (room_free_spots_x); 607 free (room_free_spots_x);
593 free (room_free_spots_y); 608 free (room_free_spots_y);
594} 609}
595 610
596 611
597/* searches the map for a spot with walls around it. The more 612/* searches the map for a spot with walls around it. The more
598 walls the better, but it'll settle for 1 wall, or even 0, but 613 walls the better, but it'll settle for 1 wall, or even 0, but
599 it'll return 0 if no FREE spots are found.*/ 614 it'll return 0 if no FREE spots are found.*/
600
601void 615void
602find_enclosed_spot (maptile *map, int *cx, int *cy, RMParms * RP) 616find_enclosed_spot (maptile *map, int *cx, int *cy, random_map_params *RP)
603{ 617{
604 int x, y; 618 int x, y;
605 int i; 619 int i;
606 620
607 x = *cx; 621 x = *cx;
656 *cy = ly; 670 *cy = ly;
657 return; 671 return;
658 } 672 }
659 } 673 }
660 /* give up and return the closest free spot. */ 674 /* give up and return the closest free spot. */
661 i = find_first_free_spot (&archetype::find ("chest")->clone, map, x, y); 675 i = find_free_spot (&archetype::find ("chest")->clone, map, x, y, 1, SIZEOFFREE1 + 1);
662 if (i != -1 && i <= SIZEOFFREE1) 676
677 if (i != -1)
663 { 678 {
664 *cx = x + freearr_x[i]; 679 *cx = x + freearr_x[i];
665 *cy = y + freearr_y[i]; 680 *cy = y + freearr_y[i];
666 return; 681 }
682 else
667 } 683 {
668 /* indicate failure */ 684 /* indicate failure */
685 *cx = -1;
669 *cx = *cy = -1; 686 *cy = -1;
687 }
670} 688}
671 689
672 690
673void 691void
674remove_monsters (int x, int y, maptile *map) 692remove_monsters (int x, int y, maptile *map)
675{ 693{
676 object *tmp; 694 object *tmp;
677 695
678 for (tmp = get_map_ob (map, x, y); tmp != NULL; tmp = tmp->above) 696 for (tmp = GET_MAP_OB (map, x, y); tmp != NULL; tmp = tmp->above)
679 if (QUERY_FLAG (tmp, FLAG_ALIVE)) 697 if (QUERY_FLAG (tmp, FLAG_ALIVE))
680 { 698 {
681 if (tmp->head) 699 if (tmp->head)
682 tmp = tmp->head; 700 tmp = tmp->head;
683 remove_ob (tmp); 701 tmp->remove ();
684 free_object (tmp); 702 tmp->destroy ();
685 tmp = get_map_ob (map, x, y); 703 tmp = GET_MAP_OB (map, x, y);
686 if (tmp == NULL) 704 if (tmp == NULL)
687 break; 705 break;
688 }; 706 };
689} 707}
690 708
739object * 757object *
740door_in_square (maptile *map, int x, int y) 758door_in_square (maptile *map, int x, int y)
741{ 759{
742 object *tmp; 760 object *tmp;
743 761
744 for (tmp = get_map_ob (map, x, y); tmp != NULL; tmp = tmp->above) 762 for (tmp = GET_MAP_OB (map, x, y); tmp != NULL; tmp = tmp->above)
745 if (tmp->type == DOOR || tmp->type == LOCKED_DOOR) 763 if (tmp->type == DOOR || tmp->type == LOCKED_DOOR)
746 return tmp; 764 return tmp;
747 return NULL; 765 return NULL;
748} 766}
749 767
750 768
751/* the workhorse routine, which finds the doors in a room */ 769/* the workhorse routine, which finds the doors in a room */
752void 770void
753find_doors_in_room_recursive (char **layout, maptile *map, int x, int y, object **doorlist, int *ndoors, RMParms * RP) 771find_doors_in_room_recursive (char **layout, maptile *map, int x, int y, object **doorlist, int *ndoors, random_map_params *RP)
754{ 772{
755 int i, j; 773 int i, j;
756 object *door; 774 object *door;
757 775
758 /* bounds check x and y */ 776 /* bounds check x and y */
766 /* check off this point */ 784 /* check off this point */
767 if (layout[x][y] == '#') 785 if (layout[x][y] == '#')
768 { /* there could be a door here */ 786 { /* there could be a door here */
769 layout[x][y] = 1; 787 layout[x][y] = 1;
770 door = door_in_square (map, x, y); 788 door = door_in_square (map, x, y);
771 if (door != NULL) 789 if (door)
772 { 790 {
773 doorlist[*ndoors] = door; 791 doorlist[*ndoors] = door;
774 if (*ndoors > 254) /* eek! out of memory */ 792 if (*ndoors > 1022) /* eek! out of memory */
775 { 793 {
776 LOG (llevError, "find_doors_in_room_recursive:Too many doors for memory allocated!\n"); 794 LOG (llevError, "find_doors_in_room_recursive:Too many doors for memory allocated!\n");
777 return; 795 return;
778 } 796 }
797
779 *ndoors = *ndoors + 1; 798 *ndoors = *ndoors + 1;
780 } 799 }
781 } 800 }
782 else 801 else
783 { 802 {
784 layout[x][y] = 1; 803 layout[x][y] = 1;
785 /* now search all the 8 squares around recursively for free spots,in random order */ 804 /* now search all the 8 squares around recursively for free spots,in random order */
786 for (i = RANDOM () % 8, j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) 805 for (i = RANDOM () % 8, j = 0; j < 8 && theMonsterToFind == NULL; i++, j++)
787 {
788 find_doors_in_room_recursive (layout, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], doorlist, ndoors, RP); 806 find_doors_in_room_recursive (layout, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], doorlist, ndoors, RP);
789 }
790 } 807 }
791} 808}
792 809
793/* find a random non-blocked spot in this room to drop a key. */ 810/* find a random non-blocked spot in this room to drop a key. */
794object ** 811object **
795find_doors_in_room (maptile *map, int x, int y, RMParms * RP) 812find_doors_in_room (maptile *map, int x, int y, random_map_params *RP)
796{ 813{
797 char **layout2; 814 char **layout2;
798 object **doorlist; 815 object **doorlist;
799 int i, j; 816 int i, j;
800 int ndoors = 0; 817 int ndoors = 0;
801 818
802 doorlist = (object **) calloc (sizeof (int), 256); 819 doorlist = (object **) calloc (sizeof (int), 1024);
803
804 820
805 layout2 = (char **) calloc (sizeof (char *), RP->Xsize); 821 layout2 = (char **) calloc (sizeof (char *), RP->Xsize);
806 /* allocate and copy the layout, converting C to 0. */ 822 /* allocate and copy the layout, converting C to 0. */
807 for (i = 0; i < RP->Xsize; i++) 823 for (i = 0; i < RP->Xsize; i++)
808 { 824 {
817 /* setup num_free_spots and room_free_spots */ 833 /* setup num_free_spots and room_free_spots */
818 find_doors_in_room_recursive (layout2, map, x, y, doorlist, &ndoors, RP); 834 find_doors_in_room_recursive (layout2, map, x, y, doorlist, &ndoors, RP);
819 835
820 /* deallocate the temp. layout */ 836 /* deallocate the temp. layout */
821 for (i = 0; i < RP->Xsize; i++) 837 for (i = 0; i < RP->Xsize; i++)
822 {
823 free (layout2[i]); 838 free (layout2[i]);
824 } 839
825 free (layout2); 840 free (layout2);
826 return doorlist; 841 return doorlist;
827} 842}
828 843
829 844
830 845
831/* locks and/or hides all the doors in doorlist, or does nothing if 846/* locks and/or hides all the doors in doorlist, or does nothing if
832 opts doesn't say to lock/hide doors. */ 847 opts doesn't say to lock/hide doors. */
833 848
834void 849void
835lock_and_hide_doors (object **doorlist, maptile *map, int opts, RMParms * RP) 850lock_and_hide_doors (object **doorlist, maptile *map, int opts, random_map_params *RP)
836{ 851{
837 object *door; 852 object *door;
838 int i; 853 int i;
839 854
840 /* lock the doors and hide the keys. */ 855 /* lock the doors and hide the keys. */
842 if (opts & DOORED) 857 if (opts & DOORED)
843 { 858 {
844 for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++) 859 for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++)
845 { 860 {
846 object *new_door = get_archetype ("locked_door1"); 861 object *new_door = get_archetype ("locked_door1");
847 char keybuf[256]; 862 char keybuf[1024];
848 863
849 door = doorlist[i]; 864 door = doorlist[i];
850 new_door->face = door->face; 865 new_door->face = door->face;
851 new_door->x = door->x; 866 new_door->x = door->x;
852 new_door->y = door->y; 867 new_door->y = door->y;
853 remove_ob (door); 868 door->remove ();
854 free_object (door); 869 door->destroy ();
855 doorlist[i] = new_door; 870 doorlist[i] = new_door;
856 insert_ob_in_map (new_door, map, NULL, 0); 871 insert_ob_in_map (new_door, map, NULL, 0);
857 sprintf (keybuf, "%d", (int) RANDOM ()); 872 sprintf (keybuf, "%d", (int) RANDOM ());
858 new_door->slaying = keybuf; 873 new_door->slaying = keybuf;
859 keyplace (map, new_door->x, new_door->y, keybuf, NO_PASS_DOORS, 2, RP); 874 keyplace (map, new_door->x, new_door->y, keybuf, NO_PASS_DOORS, 2, RP);
875 retrofit_joined_wall (map, door->x + 1, door->y, 0, RP); 890 retrofit_joined_wall (map, door->x + 1, door->y, 0, RP);
876 retrofit_joined_wall (map, door->x, door->y - 1, 0, RP); 891 retrofit_joined_wall (map, door->x, door->y - 1, 0, RP);
877 retrofit_joined_wall (map, door->x, door->y + 1, 0, RP); 892 retrofit_joined_wall (map, door->x, door->y + 1, 0, RP);
878 door->face = wallface->face; 893 door->face = wallface->face;
879 if (!QUERY_FLAG (wallface, FLAG_REMOVED)) 894 if (!QUERY_FLAG (wallface, FLAG_REMOVED))
880 remove_ob (wallface); 895 wallface->remove ();
881 free_object (wallface); 896 wallface->destroy ();
882 } 897 }
883 } 898 }
884 } 899 }
885} 900}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines