… | |
… | |
45 | /* a macro to get a strongly centered random distribution, |
45 | /* a macro to get a strongly centered random distribution, |
46 | from 0 to x, centered at x/2 */ |
46 | from 0 to x, centered at x/2 */ |
47 | static int |
47 | static int |
48 | bc_random (int x) |
48 | bc_random (int x) |
49 | { |
49 | { |
50 | return (rndm (x) + rndm (x) + rndm (x)) / 3; |
50 | return (rmg_rndm (x) + rmg_rndm (x) + rmg_rndm (x)) / 3; |
51 | } |
51 | } |
52 | |
52 | |
53 | /* returns true if square x,y has P_NO_PASS set, which is true for walls |
53 | /* returns true if square x,y has P_NO_PASS set, which is true for walls |
54 | * and doors but not monsters. |
54 | * and doors but not monsters. |
55 | * This function is not map tile aware. |
55 | * This function is not map tile aware. |
… | |
… | |
82 | if (treasure_style) |
82 | if (treasure_style) |
83 | if (!strcmp (treasure_style, "none")) |
83 | if (!strcmp (treasure_style, "none")) |
84 | return; |
84 | return; |
85 | |
85 | |
86 | if (treasureoptions <= 0) |
86 | if (treasureoptions <= 0) |
87 | treasureoptions = rndm (2 * LAST_OPTION); |
87 | treasureoptions = rmg_rndm (2 * LAST_OPTION); |
88 | |
88 | |
89 | /* filter out the mutually exclusive options */ |
89 | /* filter out the mutually exclusive options */ |
90 | if ((treasureoptions & RICH) && (treasureoptions & SPARSE)) |
90 | if ((treasureoptions & RICH) && (treasureoptions & SPARSE)) |
91 | { |
91 | { |
92 | if (rndm (2)) |
92 | if (rmg_rndm (2)) |
93 | treasureoptions -= 1; |
93 | treasureoptions -= 1; |
94 | else |
94 | else |
95 | treasureoptions -= 2; |
95 | treasureoptions -= 2; |
96 | } |
96 | } |
97 | |
97 | |
… | |
… | |
157 | } |
157 | } |
158 | } |
158 | } |
159 | } |
159 | } |
160 | break; |
160 | break; |
161 | } |
161 | } |
|
|
162 | |
162 | default: |
163 | default: |
163 | { |
164 | { |
164 | int i, j, tries; |
165 | int i, j, tries; |
165 | object *chest; |
166 | object *chest; |
166 | object **doorlist; |
167 | object **doorlist; |
167 | |
168 | |
168 | i = j = -1; |
169 | i = j = -1; |
169 | tries = 0; |
170 | tries = 0; |
170 | while (i == -1 && tries < 100) |
171 | while (i == -1 && tries < 100) |
171 | { |
172 | { |
172 | i = rndm (RP->Xsize - 2) + 1; |
173 | i = rmg_rndm (RP->Xsize - 2) + 1; |
173 | j = rndm (RP->Ysize - 2) + 1; |
174 | j = rmg_rndm (RP->Ysize - 2) + 1; |
174 | find_enclosed_spot (map, &i, &j, RP); |
175 | find_enclosed_spot (map, &i, &j, RP); |
175 | |
176 | |
176 | if (wall_blocked (map, i, j)) |
177 | if (wall_blocked (map, i, j)) |
177 | i = -1; |
178 | i = -1; |
178 | |
179 | |
… | |
… | |
199 | { /* DIFFUSE treasure layout */ |
200 | { /* DIFFUSE treasure layout */ |
200 | int ti, i, j; |
201 | int ti, i, j; |
201 | |
202 | |
202 | for (ti = 0; ti < num_treasures; ti++) |
203 | for (ti = 0; ti < num_treasures; ti++) |
203 | { |
204 | { |
204 | i = rndm (RP->Xsize - 2) + 1; |
205 | i = rmg_rndm (RP->Xsize - 2) + 1; |
205 | j = rndm (RP->Ysize - 2) + 1; |
206 | j = rmg_rndm (RP->Ysize - 2) + 1; |
206 | place_chest (treasureoptions, i, j, map, style_map, 1, RP); |
207 | place_chest (treasureoptions, i, j, map, style_map, 1, RP); |
207 | } |
208 | } |
208 | } |
209 | } |
209 | } |
210 | } |
210 | |
211 | |
… | |
… | |
212 | determined (may be null, or may be a treasure list from lib/treasures, |
213 | determined (may be null, or may be a treasure list from lib/treasures, |
213 | if the global variable "treasurestyle" is set to that treasure list's name */ |
214 | if the global variable "treasurestyle" is set to that treasure list's name */ |
214 | object * |
215 | object * |
215 | place_chest (int treasureoptions, int x, int y, maptile *map, maptile *style_map, int n_treasures, random_map_params *RP) |
216 | place_chest (int treasureoptions, int x, int y, maptile *map, maptile *style_map, int n_treasures, random_map_params *RP) |
216 | { |
217 | { |
217 | object *the_chest; |
|
|
218 | int i, xl, yl; |
|
|
219 | |
|
|
220 | the_chest = get_archetype ("chest"); /* was "chest_2" */ |
218 | object *the_chest = archetype::get (shstr_chest); /* was "chest_2" */ |
221 | |
219 | |
222 | /* first, find a place to put the chest. */ |
220 | /* first, find a place to put the chest. */ |
223 | i = find_first_free_spot (the_chest, map, x, y); |
221 | int i = find_first_free_spot (the_chest, map, x, y); // this call uses the main rng |
224 | if (i == -1) |
222 | if (i == -1) |
225 | { |
223 | { |
226 | the_chest->destroy (); |
224 | the_chest->destroy (); |
227 | return NULL; |
225 | return NULL; |
228 | } |
226 | } |
229 | |
227 | |
230 | xl = x + freearr_x[i]; |
228 | int xl = x + freearr_x[i]; |
231 | yl = y + freearr_y[i]; |
229 | int yl = y + freearr_y[i]; |
232 | |
230 | |
233 | /* if the placement is blocked, return a fail. */ |
231 | /* if the placement is blocked, return a fail. */ |
234 | if (wall_blocked (map, xl, yl)) |
232 | if (wall_blocked (map, xl, yl)) |
235 | return 0; |
233 | return 0; |
236 | |
234 | |
… | |
… | |
243 | treasurelist *tlist = find_treasurelist (RP->treasurestyle); |
241 | treasurelist *tlist = find_treasurelist (RP->treasurestyle); |
244 | |
242 | |
245 | if (tlist != NULL) |
243 | if (tlist != NULL) |
246 | for (ti = 0; ti < n_treasures; ti++) |
244 | for (ti = 0; ti < n_treasures; ti++) |
247 | { /* use the treasure list */ |
245 | { /* use the treasure list */ |
248 | object *new_treasure = style_map->pick_random_object (); |
246 | object *new_treasure = style_map->pick_random_object (rmg_rndm); |
249 | |
247 | |
250 | insert_ob_in_ob (arch_to_object (new_treasure->arch), the_chest); |
248 | insert_ob_in_ob (arch_to_object (new_treasure->arch), the_chest); |
251 | } |
249 | } |
252 | else |
250 | else |
253 | { /* use the style map */ |
251 | { /* use the style map */ |
… | |
… | |
264 | |
262 | |
265 | /* stick a trap in the chest if required */ |
263 | /* stick a trap in the chest if required */ |
266 | if (treasureoptions & TRAPPED) |
264 | if (treasureoptions & TRAPPED) |
267 | { |
265 | { |
268 | maptile *trap_map = find_style ("/styles/trapstyles", "traps", -1); |
266 | maptile *trap_map = find_style ("/styles/trapstyles", "traps", -1); |
269 | object *the_trap; |
|
|
270 | |
267 | |
271 | if (trap_map) |
268 | if (trap_map) |
272 | { |
269 | { |
273 | the_trap = trap_map->pick_random_object (); |
270 | object *the_trap = trap_map->pick_random_object (rmg_rndm); |
|
|
271 | |
274 | the_trap->stats.Cha = 10 + RP->difficulty; |
272 | the_trap->stats.Cha = 10 + RP->difficulty; |
275 | the_trap->level = bc_random ((3 * RP->difficulty) / 2); |
273 | the_trap->level = bc_random ((3 * RP->difficulty) / 2); |
|
|
274 | |
276 | if (the_trap) |
275 | if (the_trap) |
277 | { |
276 | { |
278 | object *new_trap; |
277 | object *new_trap = the_trap->arch->instance ();//TODO: why not clone? |
279 | |
278 | |
280 | new_trap = arch_to_object (the_trap->arch); |
|
|
281 | new_trap->copy_to (the_trap); |
|
|
282 | new_trap->x = x; |
279 | new_trap->x = x; |
283 | new_trap->y = y; |
280 | new_trap->y = y; |
284 | insert_ob_in_ob (new_trap, the_chest); |
281 | insert_ob_in_ob (new_trap, the_chest); |
285 | } |
282 | } |
286 | } |
283 | } |
… | |
… | |
291 | there's only 1 treasure.... */ |
288 | there's only 1 treasure.... */ |
292 | if ((treasureoptions & KEYREQUIRED) && n_treasures > 1) |
289 | if ((treasureoptions & KEYREQUIRED) && n_treasures > 1) |
293 | { |
290 | { |
294 | char keybuf[1024]; |
291 | char keybuf[1024]; |
295 | |
292 | |
296 | sprintf (keybuf, "%d", rndm (1000000000)); |
293 | sprintf (keybuf, "%d", rmg_rndm (1000000000)); |
297 | the_chest->slaying = keybuf; |
294 | the_chest->slaying = keybuf; |
298 | keyplace (map, x, y, keybuf, PASS_DOORS, 1, RP); |
295 | keyplace (map, x, y, keybuf, PASS_DOORS, 1, RP); |
299 | } |
296 | } |
300 | |
297 | |
301 | /* actually place the chest. */ |
298 | /* actually place the chest. */ |
… | |
… | |
350 | keyplace (maptile *map, int x, int y, char *keycode, int door_flag, int n_keys, random_map_params *RP) |
347 | keyplace (maptile *map, int x, int y, char *keycode, int door_flag, int n_keys, random_map_params *RP) |
351 | { |
348 | { |
352 | int i, j; |
349 | int i, j; |
353 | int kx = 0, ky = 0; |
350 | int kx = 0, ky = 0; |
354 | object *the_keymaster; /* the monster that gets the key. */ |
351 | object *the_keymaster; /* the monster that gets the key. */ |
355 | object *the_key; |
|
|
356 | |
352 | |
357 | /* get a key and set its keycode */ |
353 | /* get a key and set its keycode */ |
358 | the_key = get_archetype ("key2"); |
354 | object *the_key = archetype::get (shstr_key2); |
359 | the_key->slaying = keycode; |
355 | the_key->slaying = keycode; |
360 | |
356 | |
361 | if (door_flag == PASS_DOORS) |
357 | if (door_flag == PASS_DOORS) |
362 | { |
358 | { |
363 | int tries = 0; |
359 | int tries = 0; |
364 | |
360 | |
365 | the_keymaster = 0; |
361 | the_keymaster = 0; |
366 | while (tries < 15 && !the_keymaster) |
362 | while (tries < 15 && !the_keymaster) |
367 | { |
363 | { |
368 | i = rndm (RP->Xsize - 2) + 1; |
364 | i = rmg_rndm (RP->Xsize - 2) + 1; |
369 | j = rndm (RP->Ysize - 2) + 1; |
365 | j = rmg_rndm (RP->Ysize - 2) + 1; |
370 | tries++; |
366 | tries++; |
371 | the_keymaster = find_closest_monster (map, i, j, RP); |
367 | the_keymaster = find_closest_monster (map, i, j, RP); |
372 | } |
368 | } |
373 | |
369 | |
374 | /* if we don't find a good keymaster, drop the key on the ground. */ |
370 | /* if we don't find a good keymaster, drop the key on the ground. */ |
… | |
… | |
377 | int freeindex; |
373 | int freeindex; |
378 | |
374 | |
379 | freeindex = -1; |
375 | freeindex = -1; |
380 | for (tries = 0; tries < 15 && freeindex == -1; tries++) |
376 | for (tries = 0; tries < 15 && freeindex == -1; tries++) |
381 | { |
377 | { |
382 | kx = rndm (RP->Xsize - 2) + 1; |
378 | kx = rmg_rndm (RP->Xsize - 2) + 1; |
383 | ky = rndm (RP->Ysize - 2) + 1; |
379 | ky = rmg_rndm (RP->Ysize - 2) + 1; |
384 | freeindex = find_free_spot (the_key, map, kx, ky, 1, SIZEOFFREE1 + 1); |
380 | freeindex = find_free_spot (the_key, map, kx, ky, 1, SIZEOFFREE1 + 1); |
385 | } |
381 | } |
386 | |
382 | |
387 | // can freeindex ever be < 0? |
383 | // can freeindex ever be < 0? |
388 | if (freeindex >= 0) |
384 | if (freeindex >= 0) |
… | |
… | |
480 | return theMonsterToFind; |
476 | return theMonsterToFind; |
481 | } |
477 | } |
482 | } |
478 | } |
483 | |
479 | |
484 | /* now search all the 8 squares around recursively for a monster,in random order */ |
480 | /* now search all the 8 squares around recursively for a monster,in random order */ |
485 | for (i = rndm (8), j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) |
481 | for (i = rmg_rndm (8), j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) |
486 | { |
482 | { |
487 | theMonsterToFind = find_monster_in_room_recursive (layout, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); |
483 | theMonsterToFind = find_monster_in_room_recursive (layout, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); |
488 | if (theMonsterToFind != NULL) |
484 | if (theMonsterToFind != NULL) |
489 | return theMonsterToFind; |
485 | return theMonsterToFind; |
490 | } |
486 | } |
|
|
487 | |
491 | return theMonsterToFind; |
488 | return theMonsterToFind; |
492 | } |
489 | } |
493 | |
|
|
494 | |
490 | |
495 | /* sets up some data structures: the _recursive form does the |
491 | /* sets up some data structures: the _recursive form does the |
496 | real work. */ |
492 | real work. */ |
497 | |
|
|
498 | object * |
493 | object * |
499 | find_monster_in_room (maptile *map, int x, int y, random_map_params *RP) |
494 | find_monster_in_room (maptile *map, int x, int y, random_map_params *RP) |
500 | { |
495 | { |
501 | char **layout2; |
496 | Layout layout2 (RP); |
502 | int i, j; |
497 | |
|
|
498 | layout2->clear (); |
|
|
499 | |
|
|
500 | /* allocate and copy the layout, converting C to 0. */ |
|
|
501 | for (int i = 0; i < layout2->w; i++) |
|
|
502 | for (int j = 0; j < layout2->h; j++) |
|
|
503 | if (wall_blocked (map, i, j)) |
|
|
504 | layout2[i][j] = '#'; |
503 | |
505 | |
504 | theMonsterToFind = 0; |
506 | theMonsterToFind = 0; |
505 | layout2 = (char **) calloc (sizeof (char *), RP->Xsize); |
|
|
506 | /* allocate and copy the layout, converting C to 0. */ |
|
|
507 | for (i = 0; i < RP->Xsize; i++) |
|
|
508 | { |
|
|
509 | layout2[i] = (char *) calloc (sizeof (char), RP->Ysize); |
|
|
510 | for (j = 0; j < RP->Ysize; j++) |
|
|
511 | if (wall_blocked (map, i, j)) |
|
|
512 | layout2[i][j] = '#'; |
|
|
513 | } |
|
|
514 | |
|
|
515 | theMonsterToFind = find_monster_in_room_recursive (layout2, map, x, y, RP); |
507 | theMonsterToFind = find_monster_in_room_recursive (layout2, map, x, y, RP); |
516 | |
508 | |
517 | /* deallocate the temp. layout */ |
509 | layout2.free (); |
518 | for (i = 0; i < RP->Xsize; i++) |
|
|
519 | free (layout2[i]); |
|
|
520 | |
|
|
521 | free (layout2); |
|
|
522 | |
510 | |
523 | return theMonsterToFind; |
511 | return theMonsterToFind; |
524 | } |
512 | } |
525 | |
513 | |
526 | /* a datastructure needed by find_spot_in_room and find_spot_in_room_recursive */ |
514 | /* a datastructure needed by find_spot_in_room and find_spot_in_room_recursive */ |
… | |
… | |
551 | room_free_spots_x[number_of_free_spots_in_room] = x; |
539 | room_free_spots_x[number_of_free_spots_in_room] = x; |
552 | room_free_spots_y[number_of_free_spots_in_room] = y; |
540 | room_free_spots_y[number_of_free_spots_in_room] = y; |
553 | number_of_free_spots_in_room++; |
541 | number_of_free_spots_in_room++; |
554 | |
542 | |
555 | /* now search all the 8 squares around recursively for free spots,in random order */ |
543 | /* now search all the 8 squares around recursively for free spots,in random order */ |
556 | for (i = rndm (8), j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) |
544 | for (i = rmg_rndm (8), j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) |
557 | find_spot_in_room_recursive (layout, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); |
545 | find_spot_in_room_recursive (layout, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); |
558 | |
546 | |
559 | } |
547 | } |
560 | |
548 | |
561 | /* find a random non-blocked spot in this room to drop a key. */ |
549 | /* find a random non-blocked spot in this room to drop a key. */ |
… | |
… | |
582 | /* setup num_free_spots and room_free_spots */ |
570 | /* setup num_free_spots and room_free_spots */ |
583 | find_spot_in_room_recursive (layout2, x, y, RP); |
571 | find_spot_in_room_recursive (layout2, x, y, RP); |
584 | |
572 | |
585 | if (number_of_free_spots_in_room > 0) |
573 | if (number_of_free_spots_in_room > 0) |
586 | { |
574 | { |
587 | i = rndm (number_of_free_spots_in_room); |
575 | i = rmg_rndm (number_of_free_spots_in_room); |
588 | *kx = room_free_spots_x[i]; |
576 | *kx = room_free_spots_x[i]; |
589 | *ky = room_free_spots_y[i]; |
577 | *ky = room_free_spots_y[i]; |
590 | } |
578 | } |
591 | |
579 | |
592 | /* deallocate the temp. layout */ |
580 | /* deallocate the temp. layout */ |
… | |
… | |
660 | *cy = ly; |
648 | *cy = ly; |
661 | return; |
649 | return; |
662 | } |
650 | } |
663 | } |
651 | } |
664 | /* give up and return the closest free spot. */ |
652 | /* give up and return the closest free spot. */ |
665 | i = find_free_spot (archetype::find ("chest"), map, x, y, 1, SIZEOFFREE1 + 1); |
653 | i = find_free_spot (archetype::find (shstr_chest), map, x, y, 1, SIZEOFFREE1 + 1); |
666 | |
654 | |
667 | if (i != -1) |
655 | if (i != -1) |
668 | { |
656 | { |
669 | *cx = x + freearr_x[i]; |
657 | *cx = x + freearr_x[i]; |
670 | *cy = y + freearr_y[i]; |
658 | *cy = y + freearr_y[i]; |
… | |
… | |
787 | else |
775 | else |
788 | { |
776 | { |
789 | layout[x][y] = 1; |
777 | layout[x][y] = 1; |
790 | |
778 | |
791 | /* 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 */ |
792 | for (i = rndm (8), j = 0; j < 8 && !theMonsterToFind; i++, j++) |
780 | for (i = rmg_rndm (8), j = 0; j < 8 && !theMonsterToFind; i++, j++) |
793 | find_doors_in_room_recursive (layout, map, |
781 | find_doors_in_room_recursive (layout, map, |
794 | x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], |
782 | x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], |
795 | doorlist, ndoors, RP); |
783 | doorlist, ndoors, RP); |
796 | } |
784 | } |
797 | } |
785 | } |
… | |
… | |
803 | int i, j; |
791 | int i, j; |
804 | int ndoors = 0; |
792 | int ndoors = 0; |
805 | |
793 | |
806 | object **doorlist = (object **)calloc (sizeof (int), 1024); |
794 | object **doorlist = (object **)calloc (sizeof (int), 1024); |
807 | |
795 | |
808 | MazeData layout2 (RP->Xsize, RP->Ysize); |
796 | LayoutData layout2 (RP->Xsize, RP->Ysize); |
|
|
797 | layout2.clear (); |
809 | |
798 | |
810 | /* allocate and copy the layout, converting C to 0. */ |
799 | /* allocate and copy the layout, converting C to 0. */ |
811 | for (i = 0; i < RP->Xsize; i++) |
800 | for (i = 0; i < RP->Xsize; i++) |
812 | for (j = 0; j < RP->Ysize; j++) |
801 | for (j = 0; j < RP->Ysize; j++) |
813 | if (wall_blocked (map, i, j)) |
802 | layout2[i][j] = wall_blocked (map, i, j) ? '#' : 0; |
814 | layout2[i][j] = '#'; |
|
|
815 | |
803 | |
816 | /* setup num_free_spots and room_free_spots */ |
804 | /* setup num_free_spots and room_free_spots */ |
817 | find_doors_in_room_recursive (layout2, map, x, y, doorlist, &ndoors, RP); |
805 | find_doors_in_room_recursive (layout2, map, x, y, doorlist, &ndoors, RP); |
818 | |
806 | |
819 | return doorlist; |
807 | return doorlist; |
… | |
… | |
842 | new_door->y = door->y; |
830 | new_door->y = door->y; |
843 | door->remove (); |
831 | door->remove (); |
844 | door->destroy (); |
832 | door->destroy (); |
845 | doorlist[i] = new_door; |
833 | doorlist[i] = new_door; |
846 | insert_ob_in_map (new_door, map, NULL, 0); |
834 | insert_ob_in_map (new_door, map, NULL, 0); |
847 | sprintf (keybuf, "%d", rndm (1000000000)); |
835 | sprintf (keybuf, "%d", rmg_rndm (1000000000)); |
848 | new_door->slaying = keybuf; |
836 | new_door->slaying = keybuf; |
849 | keyplace (map, new_door->x, new_door->y, keybuf, NO_PASS_DOORS, 2, RP); |
837 | keyplace (map, new_door->x, new_door->y, keybuf, NO_PASS_DOORS, 2, RP); |
850 | } |
838 | } |
851 | } |
839 | } |
852 | |
840 | |
… | |
… | |
863 | { |
851 | { |
864 | retrofit_joined_wall (map, door->x - 1, door->y, 0, RP); |
852 | retrofit_joined_wall (map, door->x - 1, door->y, 0, RP); |
865 | retrofit_joined_wall (map, door->x + 1, door->y, 0, RP); |
853 | retrofit_joined_wall (map, door->x + 1, door->y, 0, RP); |
866 | retrofit_joined_wall (map, door->x, door->y - 1, 0, RP); |
854 | retrofit_joined_wall (map, door->x, door->y - 1, 0, RP); |
867 | retrofit_joined_wall (map, door->x, door->y + 1, 0, RP); |
855 | retrofit_joined_wall (map, door->x, door->y + 1, 0, RP); |
|
|
856 | |
868 | door->face = wallface->face; |
857 | door->face = wallface->face; |
|
|
858 | |
869 | if (!QUERY_FLAG (wallface, FLAG_REMOVED)) |
859 | if (!QUERY_FLAG (wallface, FLAG_REMOVED)) |
870 | wallface->remove (); |
860 | wallface->remove (); |
|
|
861 | |
871 | wallface->destroy (); |
862 | wallface->destroy (); |
872 | } |
863 | } |
873 | } |
864 | } |
874 | } |
865 | } |
875 | } |
866 | } |