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.11 by root, Sat Dec 30 18:45:28 2006 UTC vs.
Revision 1.21 by root, Thu Jan 18 19:42:10 2007 UTC

1/* 1/*
2 CrossFire, A Multiplayer game for X-windows 2 * CrossFire, A Multiplayer game
3 3 *
4 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
4 Copyright (C) 2001 Mark Wedel & Crossfire Development Team 5 * Copyright (C) 2001 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (C) 1992 Frank Tore Johansen
6 7 *
7 This program is free software; you can redistribute it and/or modify 8 * 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 9 * 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 10 * the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version. 11 * (at your option) any later version.
11 12 *
12 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,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 16 * GNU General Public License for more details.
16 17 *
17 You should have received a copy of the GNU General Public License 18 * You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software 19 * along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 21 *
21 The authors can be reached via e-mail at <crossfire@schmorp.de> 22 * The authors can be reached via e-mail at <crossfire@schmorp.de>
22*/ 23 */
23 24
24/* placing treasure in maps, where appropriate. */ 25/* placing treasure in maps, where appropriate. */
25
26
27 26
28#include <global.h> 27#include <global.h>
29#include <random_map.h> 28#include <random_map.h>
30#include <rproto.h> 29#include <rproto.h>
31 30
47 46
48/* returns true if square x,y has P_NO_PASS set, which is true for walls 47/* returns true if square x,y has P_NO_PASS set, which is true for walls
49 * and doors but not monsters. 48 * and doors but not monsters.
50 * This function is not map tile aware. 49 * This function is not map tile aware.
51 */ 50 */
52
53int 51int
54wall_blocked (maptile *m, int x, int y) 52wall_blocked (maptile *m, int x, int y)
55{ 53{
56 int r;
57
58 if (OUT_OF_REAL_MAP (m, x, y)) 54 if (OUT_OF_REAL_MAP (m, x, y))
59 return 1; 55 return 1;
56
60 r = GET_MAP_MOVE_BLOCK (m, x, y) & ~MOVE_BLOCK_DEFAULT; 57 int r = GET_MAP_MOVE_BLOCK (m, x, y) & ~MOVE_BLOCK_DEFAULT;
61 return r; 58 return r;
62} 59}
63 60
64/* place treasures in the map, given the 61/* place treasures in the map, given the
65map, (required) 62map, (required)
66layout, (required) 63layout, (required)
67treasure style (may be empty or NULL, or "none" to cause no treasure.) 64treasure style (may be empty or NULL, or "none" to cause no treasure.)
68treasureoptions (may be 0 for random choices or positive) 65treasureoptions (may be 0 for random choices or positive)
69*/ 66*/
70
71void 67void
72place_treasure (maptile *map, char **layout, char *treasure_style, int treasureoptions, random_map_params * RP) 68place_treasure (maptile *map, char **layout, char *treasure_style, int treasureoptions, random_map_params *RP)
73{ 69{
74 char styledirname[256]; 70 char styledirname[1024];
75 char stylefilepath[256]; 71 char stylefilepath[1024];
76 maptile *style_map = 0; 72 maptile *style_map = 0;
77 int num_treasures; 73 int num_treasures;
78 74
79 /* bail out if treasure isn't wanted. */ 75 /* bail out if treasure isn't wanted. */
80 if (treasure_style) 76 if (treasure_style)
84 treasureoptions = RANDOM () % (2 * LAST_OPTION); 80 treasureoptions = RANDOM () % (2 * LAST_OPTION);
85 81
86 /* filter out the mutually exclusive options */ 82 /* filter out the mutually exclusive options */
87 if ((treasureoptions & RICH) && (treasureoptions & SPARSE)) 83 if ((treasureoptions & RICH) && (treasureoptions & SPARSE))
88 { 84 {
89 if (RANDOM () % 2) 85 if (rndm (2))
90 treasureoptions -= 1; 86 treasureoptions -= 1;
91 else 87 else
92 treasureoptions -= 2; 88 treasureoptions -= 2;
93 } 89 }
94 90
113 { 109 {
114 110
115 /* map_layout_style global, and is previously set */ 111 /* map_layout_style global, and is previously set */
116 switch (RP->map_layout_style) 112 switch (RP->map_layout_style)
117 { 113 {
118 case LAYOUT_ONION: 114 case LAYOUT_ONION:
119 case LAYOUT_SPIRAL: 115 case LAYOUT_SPIRAL:
120 case LAYOUT_SQUARE_SPIRAL: 116 case LAYOUT_SQUARE_SPIRAL:
121 { 117 {
122 int i, j; 118 int i, j;
123 119
124 /* search the onion for C's or '>', and put treasure there. */ 120 /* search the onion for C's or '>', and put treasure there. */
125 for (i = 0; i < RP->Xsize; i++) 121 for (i = 0; i < RP->Xsize; i++)
126 { 122 {
127 for (j = 0; j < RP->Ysize; j++) 123 for (j = 0; j < RP->Ysize; j++)
128 { 124 {
129 if (layout[i][j] == 'C' || layout[i][j] == '>') 125 if (layout[i][j] == 'C' || layout[i][j] == '>')
130 { 126 {
131 int tdiv = RP->symmetry_used; 127 int tdiv = RP->symmetry_used;
132 object **doorlist; 128 object **doorlist;
133 object *chest; 129 object *chest;
134 130
135 if (tdiv == 3) 131 if (tdiv == 3)
136 tdiv = 2; /* this symmetry uses a divisor of 2 */ 132 tdiv = 2; /* this symmetry uses a divisor of 2 */
137 /* don't put a chest on an exit. */ 133 /* don't put a chest on an exit. */
138 chest = place_chest (treasureoptions, i, j, map, style_map, num_treasures / tdiv, RP); 134 chest = place_chest (treasureoptions, i, j, map, style_map, num_treasures / tdiv, RP);
139 if (!chest) 135 if (!chest)
140 continue; /* if no chest was placed NEXT */ 136 continue; /* if no chest was placed NEXT */
141 if (treasureoptions & (DOORED | HIDDEN)) 137 if (treasureoptions & (DOORED | HIDDEN))
142 { 138 {
143 doorlist = find_doors_in_room (map, i, j, RP); 139 doorlist = find_doors_in_room (map, i, j, RP);
144 lock_and_hide_doors (doorlist, map, treasureoptions, RP); 140 lock_and_hide_doors (doorlist, map, treasureoptions, RP);
145 free (doorlist); 141 free (doorlist);
146 } 142 }
147 } 143 }
148 } 144 }
149 } 145 }
150 break; 146 break;
151 } 147 }
152 default: 148 default:
153 { 149 {
154 int i, j, tries; 150 int i, j, tries;
155 object *chest; 151 object *chest;
156 object **doorlist; 152 object **doorlist;
157 153
158 i = j = -1; 154 i = j = -1;
159 tries = 0; 155 tries = 0;
160 while (i == -1 && tries < 100) 156 while (i == -1 && tries < 100)
161 { 157 {
162 i = RANDOM () % (RP->Xsize - 2) + 1; 158 i = RANDOM () % (RP->Xsize - 2) + 1;
163 j = RANDOM () % (RP->Ysize - 2) + 1; 159 j = RANDOM () % (RP->Ysize - 2) + 1;
164 find_enclosed_spot (map, &i, &j, RP); 160 find_enclosed_spot (map, &i, &j, RP);
165 if (wall_blocked (map, i, j)) 161 if (wall_blocked (map, i, j))
166 i = -1; 162 i = -1;
167 tries++; 163 tries++;
168 } 164 }
169 chest = place_chest (treasureoptions, i, j, map, style_map, num_treasures, RP); 165 chest = place_chest (treasureoptions, i, j, map, style_map, num_treasures, RP);
170 if (!chest) 166 if (!chest)
171 return; 167 return;
172 i = chest->x; 168 i = chest->x;
173 j = chest->y; 169 j = chest->y;
174 if (treasureoptions & (DOORED | HIDDEN)) 170 if (treasureoptions & (DOORED | HIDDEN))
175 { 171 {
176 doorlist = surround_by_doors (map, layout, i, j, treasureoptions); 172 doorlist = surround_by_doors (map, layout, i, j, treasureoptions);
177 lock_and_hide_doors (doorlist, map, treasureoptions, RP); 173 lock_and_hide_doors (doorlist, map, treasureoptions, RP);
178 free (doorlist); 174 free (doorlist);
179 } 175 }
180 } 176 }
181 } 177 }
182 } 178 }
183 else 179 else
184 { /* DIFFUSE treasure layout */ 180 { /* DIFFUSE treasure layout */
185 int ti, i, j; 181 int ti, i, j;
191 place_chest (treasureoptions, i, j, map, style_map, 1, RP); 187 place_chest (treasureoptions, i, j, map, style_map, 1, RP);
192 } 188 }
193 } 189 }
194} 190}
195 191
196
197
198/* put a chest into the map, near x and y, with the treasure style 192/* 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, 193 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 */ 194 if the global variable "treasurestyle" is set to that treasure list's name */
201 195
202object * 196object *
203place_chest (int treasureoptions, int x, int y, maptile *map, maptile *style_map, int n_treasures, random_map_params * RP) 197place_chest (int treasureoptions, int x, int y, maptile *map, maptile *style_map, int n_treasures, random_map_params *RP)
204{ 198{
205 object *the_chest; 199 object *the_chest;
206 int i, xl, yl; 200 int i, xl, yl;
207 201
208 the_chest = get_archetype ("chest"); /* was "chest_2" */ 202 the_chest = get_archetype ("chest"); /* was "chest_2" */
212 if (i == -1) 206 if (i == -1)
213 { 207 {
214 the_chest->destroy (); 208 the_chest->destroy ();
215 return NULL; 209 return NULL;
216 } 210 }
211
217 xl = x + freearr_x[i]; 212 xl = x + freearr_x[i];
218 yl = y + freearr_y[i]; 213 yl = y + freearr_y[i];
219 214
220 /* if the placement is blocked, return a fail. */ 215 /* if the placement is blocked, return a fail. */
221 if (wall_blocked (map, xl, yl)) 216 if (wall_blocked (map, xl, yl))
222 return 0; 217 return 0;
223
224 218
225 /* put the treasures in the chest. */ 219 /* put the treasures in the chest. */
226 /* if(style_map) { */ 220 /* if(style_map) { */
227#if 0 /* don't use treasure style maps for now! */ 221#if 0 /* don't use treasure style maps for now! */
228 int ti; 222 int ti;
241 { /* use the style map */ 235 { /* use the style map */
242 the_chest->randomitems = tlist; 236 the_chest->randomitems = tlist;
243 the_chest->stats.hp = n_treasures; 237 the_chest->stats.hp = n_treasures;
244 } 238 }
245#endif 239#endif
246 else
247 { /* neither style_map no treasure list given */ 240 { /* neither style_map no treasure list given */
248 treasurelist *tlist = find_treasurelist ("chest"); 241 treasurelist *tlist = find_treasurelist ("chest");
249 242
250 the_chest->randomitems = tlist; 243 the_chest->randomitems = tlist;
251 the_chest->stats.hp = n_treasures; 244 the_chest->stats.hp = n_treasures;
279 the lockcode. It's not worth bothering to lock the chest if 272 the lockcode. It's not worth bothering to lock the chest if
280 there's only 1 treasure.... */ 273 there's only 1 treasure.... */
281 274
282 if ((treasureoptions & KEYREQUIRED) && n_treasures > 1) 275 if ((treasureoptions & KEYREQUIRED) && n_treasures > 1)
283 { 276 {
284 char keybuf[256]; 277 char keybuf[1024];
285 278
286 sprintf (keybuf, "%d", (int) RANDOM ()); 279 sprintf (keybuf, "%d", (int) RANDOM ());
287 the_chest->slaying = keybuf; 280 the_chest->slaying = keybuf;
288 keyplace (map, x, y, keybuf, PASS_DOORS, 1, RP); 281 keyplace (map, x, y, keybuf, PASS_DOORS, 1, RP);
289 } 282 }
297 290
298 291
299/* finds the closest monster and returns him, regardless of doors 292/* finds the closest monster and returns him, regardless of doors
300 or walls */ 293 or walls */
301object * 294object *
302find_closest_monster (maptile *map, int x, int y, random_map_params * RP) 295find_closest_monster (maptile *map, int x, int y, random_map_params *RP)
303{ 296{
304 int i; 297 int i;
305 298
306 for (i = 0; i < SIZEOFFREE; i++) 299 for (i = 0; i < SIZEOFFREE; i++)
307 { 300 {
334 it will place 2-4 keys regardless of what nkeys is provided nkeys > 1. 327 it will place 2-4 keys regardless of what nkeys is provided nkeys > 1.
335 328
336 The idea is that you call keyplace on x,y where a door is, and it'll make 329 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. 330 sure a key is placed on both sides of the door.
338*/ 331*/
339
340int 332int
341keyplace (maptile *map, int x, int y, char *keycode, int door_flag, int n_keys, random_map_params * RP) 333keyplace (maptile *map, int x, int y, char *keycode, int door_flag, int n_keys, random_map_params *RP)
342{ 334{
343 int i, j; 335 int i, j;
344 int kx, ky; 336 int kx = 0, ky = 0;
345 object *the_keymaster; /* the monster that gets the key. */ 337 object *the_keymaster; /* the monster that gets the key. */
346 object *the_key; 338 object *the_key;
347 339
348 /* get a key and set its keycode */ 340 /* get a key and set its keycode */
349 the_key = get_archetype ("key2"); 341 the_key = get_archetype ("key2");
351 343
352 if (door_flag == PASS_DOORS) 344 if (door_flag == PASS_DOORS)
353 { 345 {
354 int tries = 0; 346 int tries = 0;
355 347
356 the_keymaster = NULL; 348 the_keymaster = 0;
357 while (tries < 15 && the_keymaster == NULL) 349 while (tries < 15 && !the_keymaster)
358 { 350 {
359 i = (RANDOM () % (RP->Xsize - 2)) + 1; 351 i = (RANDOM () % (RP->Xsize - 2)) + 1;
360 j = (RANDOM () % (RP->Ysize - 2)) + 1; 352 j = (RANDOM () % (RP->Ysize - 2)) + 1;
361 tries++; 353 tries++;
362 the_keymaster = find_closest_monster (map, i, j, RP); 354 the_keymaster = find_closest_monster (map, i, j, RP);
363 } 355 }
356
364 /* if we don't find a good keymaster, drop the key on the ground. */ 357 /* if we don't find a good keymaster, drop the key on the ground. */
365 if (the_keymaster == NULL) 358 if (!the_keymaster)
366 { 359 {
367 int freeindex; 360 int freeindex;
368 361
369 freeindex = -1; 362 freeindex = -1;
370 for (tries = 0; tries < 15 && freeindex == -1; tries++) 363 for (tries = 0; tries < 15 && freeindex == -1; tries++)
371 { 364 {
372 kx = (RANDOM () % (RP->Xsize - 2)) + 1; 365 kx = (RANDOM () % (RP->Xsize - 2)) + 1;
373 ky = (RANDOM () % (RP->Ysize - 2)) + 1; 366 ky = (RANDOM () % (RP->Ysize - 2)) + 1;
374 freeindex = find_first_free_spot (the_key, map, kx, ky); 367 freeindex = find_free_spot (the_key, map, kx, ky, 1, SIZEOFFREE1 + 1);
375 } 368 }
369
370 // can freeindex ever be < 0?
376 if (freeindex != -1) 371 if (freeindex >= 0)
377 { 372 {
378 kx += freearr_x[freeindex]; 373 kx += freearr_x [freeindex];
379 ky += freearr_y[freeindex]; 374 ky += freearr_y [freeindex];
380 } 375 }
381 } 376 }
382 } 377 }
383 else 378 else
384 { /* NO_PASS_DOORS --we have to work harder. */ 379 { /* NO_PASS_DOORS --we have to work harder. */
386 NO_PASS_DOORS is set. */ 381 NO_PASS_DOORS is set. */
387 if (n_keys == 1) 382 if (n_keys == 1)
388 { 383 {
389 if (wall_blocked (map, x, y)) 384 if (wall_blocked (map, x, y))
390 return 0; 385 return 0;
386
391 the_keymaster = find_monster_in_room (map, x, y, RP); 387 the_keymaster = find_monster_in_room (map, x, y, RP);
392 if (the_keymaster == NULL) /* if fail, find a spot to drop the key. */ 388 if (!the_keymaster) /* if fail, find a spot to drop the key. */
393 find_spot_in_room (map, x, y, &kx, &ky, RP); 389 find_spot_in_room (map, x, y, &kx, &ky, RP);
394 } 390 }
395 else 391 else
396 { 392 {
397 int sum = 0; /* count how many keys we actually place */ 393 int sum = 0; /* count how many keys we actually place */
399 /* I'm lazy, so just try to place in all 4 directions. */ 395 /* 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); 396 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); 397 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); 398 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); 399 sum += keyplace (map, x, y - 1, keycode, NO_PASS_DOORS, 1, RP);
400
404 if (sum < 2) /* we might have made a disconnected map-place more keys. */ 401 if (sum < 2) /* we might have made a disconnected map-place more keys. */
405 { /* diagnoally this time. */ 402 { /* diagonally this time. */
406 keyplace (map, x + 1, y + 1, keycode, NO_PASS_DOORS, 1, RP); 403 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); 404 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); 405 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); 406 keyplace (map, x - 1, y - 1, keycode, NO_PASS_DOORS, 1, RP);
410 } 407 }
408
411 return 1; 409 return 1;
412 } 410 }
413 } 411 }
414 412
415 if (the_keymaster == NULL) 413 if (!the_keymaster)
416 { 414 {
417 the_key->x = kx; 415 the_key->x = kx;
418 the_key->y = ky; 416 the_key->y = ky;
419 insert_ob_in_map (the_key, map, NULL, 0); 417 insert_ob_in_map (the_key, map, NULL, 0);
420 return 1; 418 return 1;
432 430
433/* a recursive routine which will return a monster, eventually,if there is one. 431/* 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 */ 432 it does a check-off on the layout, converting 0's to 1's */
435 433
436object * 434object *
437find_monster_in_room_recursive (char **layout, maptile *map, int x, int y, random_map_params * RP) 435find_monster_in_room_recursive (char **layout, maptile *map, int x, int y, random_map_params *RP)
438{ 436{
439 int i, j; 437 int i, j;
440 438
441 /* if we've found a monster already, leave */ 439 /* if we've found a monster already, leave */
442 if (theMonsterToFind != NULL) 440 if (theMonsterToFind != NULL)
465 return theMonsterToFind; 463 return theMonsterToFind;
466 } 464 }
467 } 465 }
468 466
469 /* now search all the 8 squares around recursively for a monster,in random order */ 467 /* now search all the 8 squares around recursively for a monster,in random order */
470 for (i = RANDOM () % 8, j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) 468 for (i = rndm (8), j = 0; j < 8 && theMonsterToFind == NULL; i++, j++)
471 { 469 {
472 theMonsterToFind = find_monster_in_room_recursive (layout, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); 470 theMonsterToFind = find_monster_in_room_recursive (layout, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP);
473 if (theMonsterToFind != NULL) 471 if (theMonsterToFind != NULL)
474 return theMonsterToFind; 472 return theMonsterToFind;
475 } 473 }
479 477
480/* sets up some data structures: the _recursive form does the 478/* sets up some data structures: the _recursive form does the
481 real work. */ 479 real work. */
482 480
483object * 481object *
484find_monster_in_room (maptile *map, int x, int y, random_map_params * RP) 482find_monster_in_room (maptile *map, int x, int y, random_map_params *RP)
485{ 483{
486 char **layout2; 484 char **layout2;
487 int i, j; 485 int i, j;
488 486
489 theMonsterToFind = 0; 487 theMonsterToFind = 0;
508 free (layout2); 506 free (layout2);
509 507
510 return theMonsterToFind; 508 return theMonsterToFind;
511} 509}
512 510
513
514
515
516/* a datastructure needed by find_spot_in_room and find_spot_in_room_recursive */ 511/* a datastructure needed by find_spot_in_room and find_spot_in_room_recursive */
517int *room_free_spots_x; 512int *room_free_spots_x;
518int *room_free_spots_y; 513int *room_free_spots_y;
519int number_of_free_spots_in_room; 514int number_of_free_spots_in_room;
520 515
521/* the workhorse routine, which finds the free spots in a room: 516/* the workhorse routine, which finds the free spots in a room:
522a datastructure of free points is set up, and a position chosen from 517a datastructure of free points is set up, and a position chosen from
523that datastructure. */ 518that datastructure. */
524
525void 519void
526find_spot_in_room_recursive (char **layout, int x, int y, random_map_params * RP) 520find_spot_in_room_recursive (char **layout, int x, int y, random_map_params *RP)
527{ 521{
528 int i, j; 522 int i, j;
529 523
530 /* bounds check x and y */ 524 /* bounds check x and y */
531 if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize)) 525 if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize))
540 /* check off this point */ 534 /* check off this point */
541 layout[x][y] = 1; 535 layout[x][y] = 1;
542 room_free_spots_x[number_of_free_spots_in_room] = x; 536 room_free_spots_x[number_of_free_spots_in_room] = x;
543 room_free_spots_y[number_of_free_spots_in_room] = y; 537 room_free_spots_y[number_of_free_spots_in_room] = y;
544 number_of_free_spots_in_room++; 538 number_of_free_spots_in_room++;
539
545 /* now search all the 8 squares around recursively for free spots,in random order */ 540 /* 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++) 541 for (i = rndm (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); 542 find_spot_in_room_recursive (layout, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP);
549 }
550 543
551} 544}
552 545
553/* find a random non-blocked spot in this room to drop a key. */ 546/* find a random non-blocked spot in this room to drop a key. */
554void 547void
555find_spot_in_room (maptile *map, int x, int y, int *kx, int *ky, random_map_params * RP) 548find_spot_in_room (maptile *map, int x, int y, int *kx, int *ky, random_map_params *RP)
556{ 549{
557 char **layout2; 550 char **layout2;
558 int i, j; 551 int i, j;
559 552
560 number_of_free_spots_in_room = 0; 553 number_of_free_spots_in_room = 0;
565 /* allocate and copy the layout, converting C to 0. */ 558 /* allocate and copy the layout, converting C to 0. */
566 for (i = 0; i < RP->Xsize; i++) 559 for (i = 0; i < RP->Xsize; i++)
567 { 560 {
568 layout2[i] = (char *) calloc (sizeof (char), RP->Ysize); 561 layout2[i] = (char *) calloc (sizeof (char), RP->Ysize);
569 for (j = 0; j < RP->Ysize; j++) 562 for (j = 0; j < RP->Ysize; j++)
570 {
571 if (wall_blocked (map, i, j)) 563 if (wall_blocked (map, i, j))
572 layout2[i][j] = '#'; 564 layout2[i][j] = '#';
573 }
574 } 565 }
575 566
576 /* setup num_free_spots and room_free_spots */ 567 /* setup num_free_spots and room_free_spots */
577 find_spot_in_room_recursive (layout2, x, y, RP); 568 find_spot_in_room_recursive (layout2, x, y, RP);
578 569
583 *ky = room_free_spots_y[i]; 574 *ky = room_free_spots_y[i];
584 } 575 }
585 576
586 /* deallocate the temp. layout */ 577 /* deallocate the temp. layout */
587 for (i = 0; i < RP->Xsize; i++) 578 for (i = 0; i < RP->Xsize; i++)
588 {
589 free (layout2[i]); 579 free (layout2[i]);
590 } 580
591 free (layout2); 581 free (layout2);
592 free (room_free_spots_x); 582 free (room_free_spots_x);
593 free (room_free_spots_y); 583 free (room_free_spots_y);
594} 584}
595 585
596 586
597/* searches the map for a spot with walls around it. The more 587/* 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 588 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.*/ 589 it'll return 0 if no FREE spots are found.*/
600
601void 590void
602find_enclosed_spot (maptile *map, int *cx, int *cy, random_map_params * RP) 591find_enclosed_spot (maptile *map, int *cx, int *cy, random_map_params *RP)
603{ 592{
604 int x, y; 593 int x, y;
605 int i; 594 int i;
606 595
607 x = *cx; 596 x = *cx;
656 *cy = ly; 645 *cy = ly;
657 return; 646 return;
658 } 647 }
659 } 648 }
660 /* give up and return the closest free spot. */ 649 /* give up and return the closest free spot. */
661 i = find_first_free_spot (&archetype::find ("chest")->clone, map, x, y); 650 i = find_free_spot (&archetype::find ("chest")->clone, map, x, y, 1, SIZEOFFREE1 + 1);
662 if (i != -1 && i <= SIZEOFFREE1) 651
652 if (i != -1)
663 { 653 {
664 *cx = x + freearr_x[i]; 654 *cx = x + freearr_x[i];
665 *cy = y + freearr_y[i]; 655 *cy = y + freearr_y[i];
666 return; 656 }
657 else
667 } 658 {
668 /* indicate failure */ 659 /* indicate failure */
660 *cx = -1;
669 *cx = *cy = -1; 661 *cy = -1;
662 }
670} 663}
671 664
672 665
673void 666void
674remove_monsters (int x, int y, maptile *map) 667remove_monsters (int x, int y, maptile *map)
748} 741}
749 742
750 743
751/* the workhorse routine, which finds the doors in a room */ 744/* the workhorse routine, which finds the doors in a room */
752void 745void
753find_doors_in_room_recursive (char **layout, maptile *map, int x, int y, object **doorlist, int *ndoors, random_map_params * RP) 746find_doors_in_room_recursive (char **layout, maptile *map, int x, int y, object **doorlist, int *ndoors, random_map_params *RP)
754{ 747{
755 int i, j; 748 int i, j;
756 object *door; 749 object *door;
757 750
758 /* bounds check x and y */ 751 /* bounds check x and y */
766 /* check off this point */ 759 /* check off this point */
767 if (layout[x][y] == '#') 760 if (layout[x][y] == '#')
768 { /* there could be a door here */ 761 { /* there could be a door here */
769 layout[x][y] = 1; 762 layout[x][y] = 1;
770 door = door_in_square (map, x, y); 763 door = door_in_square (map, x, y);
771 if (door != NULL) 764 if (door)
772 { 765 {
773 doorlist[*ndoors] = door; 766 doorlist[*ndoors] = door;
774 if (*ndoors > 254) /* eek! out of memory */ 767 if (*ndoors > 1022) /* eek! out of memory */
775 { 768 {
776 LOG (llevError, "find_doors_in_room_recursive:Too many doors for memory allocated!\n"); 769 LOG (llevError, "find_doors_in_room_recursive:Too many doors for memory allocated!\n");
777 return; 770 return;
778 } 771 }
772
779 *ndoors = *ndoors + 1; 773 *ndoors = *ndoors + 1;
780 } 774 }
781 } 775 }
782 else 776 else
783 { 777 {
784 layout[x][y] = 1; 778 layout[x][y] = 1;
785 /* now search all the 8 squares around recursively for free spots,in random order */ 779 /* 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++) 780 for (i = rndm (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); 781 find_doors_in_room_recursive (layout, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], doorlist, ndoors, RP);
789 }
790 } 782 }
791} 783}
792 784
793/* find a random non-blocked spot in this room to drop a key. */ 785/* find a random non-blocked spot in this room to drop a key. */
794object ** 786object **
795find_doors_in_room (maptile *map, int x, int y, random_map_params * RP) 787find_doors_in_room (maptile *map, int x, int y, random_map_params *RP)
796{ 788{
797 char **layout2; 789 char **layout2;
798 object **doorlist; 790 object **doorlist;
799 int i, j; 791 int i, j;
800 int ndoors = 0; 792 int ndoors = 0;
801 793
802 doorlist = (object **) calloc (sizeof (int), 256); 794 doorlist = (object **) calloc (sizeof (int), 1024);
803
804 795
805 layout2 = (char **) calloc (sizeof (char *), RP->Xsize); 796 layout2 = (char **) calloc (sizeof (char *), RP->Xsize);
806 /* allocate and copy the layout, converting C to 0. */ 797 /* allocate and copy the layout, converting C to 0. */
807 for (i = 0; i < RP->Xsize; i++) 798 for (i = 0; i < RP->Xsize; i++)
808 { 799 {
817 /* setup num_free_spots and room_free_spots */ 808 /* setup num_free_spots and room_free_spots */
818 find_doors_in_room_recursive (layout2, map, x, y, doorlist, &ndoors, RP); 809 find_doors_in_room_recursive (layout2, map, x, y, doorlist, &ndoors, RP);
819 810
820 /* deallocate the temp. layout */ 811 /* deallocate the temp. layout */
821 for (i = 0; i < RP->Xsize; i++) 812 for (i = 0; i < RP->Xsize; i++)
822 {
823 free (layout2[i]); 813 free (layout2[i]);
824 } 814
825 free (layout2); 815 free (layout2);
826 return doorlist; 816 return doorlist;
827} 817}
828 818
829 819
830 820
831/* locks and/or hides all the doors in doorlist, or does nothing if 821/* locks and/or hides all the doors in doorlist, or does nothing if
832 opts doesn't say to lock/hide doors. */ 822 opts doesn't say to lock/hide doors. */
833 823
834void 824void
835lock_and_hide_doors (object **doorlist, maptile *map, int opts, random_map_params * RP) 825lock_and_hide_doors (object **doorlist, maptile *map, int opts, random_map_params *RP)
836{ 826{
837 object *door; 827 object *door;
838 int i; 828 int i;
839 829
840 /* lock the doors and hide the keys. */ 830 /* lock the doors and hide the keys. */
842 if (opts & DOORED) 832 if (opts & DOORED)
843 { 833 {
844 for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++) 834 for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++)
845 { 835 {
846 object *new_door = get_archetype ("locked_door1"); 836 object *new_door = get_archetype ("locked_door1");
847 char keybuf[256]; 837 char keybuf[1024];
848 838
849 door = doorlist[i]; 839 door = doorlist[i];
850 new_door->face = door->face; 840 new_door->face = door->face;
851 new_door->x = door->x; 841 new_door->x = door->x;
852 new_door->y = door->y; 842 new_door->y = door->y;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines