… | |
… | |
48 | bc_random (int x) |
48 | bc_random (int x) |
49 | { |
49 | { |
50 | return (rmg_rndm (x) + rmg_rndm (x) + rmg_rndm (x)) / 3; |
50 | return (rmg_rndm (x) + rmg_rndm (x) + rmg_rndm (x)) / 3; |
51 | } |
51 | } |
52 | |
52 | |
|
|
53 | static object * |
|
|
54 | gen_key (const shstr &keycode) |
|
|
55 | { |
|
|
56 | /* get a key and set its keycode */ |
|
|
57 | object *key = archetype::get (shstr_key_random_map); |
|
|
58 | key->slaying = keycode; |
|
|
59 | return key; |
|
|
60 | } |
|
|
61 | |
|
|
62 | /* places keys in the map, preferably in something alive. |
|
|
63 | keycode is the key's code, |
|
|
64 | door_flag is either PASS_DOORS or NO_PASS_DOORS. |
|
|
65 | NO_PASS_DOORS won't cross doors or walls to keyplace, PASS_DOORS will. |
|
|
66 | if n_keys is 1, it will place 1 key. if n_keys >1, it will place 2-4 keys: |
|
|
67 | it will place 2-4 keys regardless of what nkeys is provided nkeys > 1. |
|
|
68 | |
|
|
69 | The idea is that you call keyplace on x,y where a door is, and it'll make |
|
|
70 | sure a key is placed on both sides of the door. |
|
|
71 | */ |
|
|
72 | static int |
|
|
73 | keyplace (maptile *map, int x, int y, const shstr &keycode, int door_flag, int n_keys, random_map_params *RP) |
|
|
74 | { |
|
|
75 | int i, j; |
|
|
76 | int kx = 0, ky = 0; |
|
|
77 | object *the_keymaster; /* the monster that gets the key. */ |
|
|
78 | object *the_key = gen_key (keycode); |
|
|
79 | |
|
|
80 | if (door_flag == PASS_DOORS) |
|
|
81 | { |
|
|
82 | int tries = 0; |
|
|
83 | |
|
|
84 | the_keymaster = 0; |
|
|
85 | while (tries < 15 && !the_keymaster) |
|
|
86 | { |
|
|
87 | i = rmg_rndm (RP->Xsize - 2) + 1; |
|
|
88 | j = rmg_rndm (RP->Ysize - 2) + 1; |
|
|
89 | tries++; |
|
|
90 | the_keymaster = find_closest_monster (map, i, j, RP); |
|
|
91 | } |
|
|
92 | |
|
|
93 | /* if we don't find a good keymaster, drop the key on the ground. */ |
|
|
94 | if (!the_keymaster) |
|
|
95 | { |
|
|
96 | int freeindex; |
|
|
97 | |
|
|
98 | freeindex = -1; |
|
|
99 | for (tries = 0; tries < 15 && freeindex == -1; tries++) |
|
|
100 | { |
|
|
101 | kx = rmg_rndm (RP->Xsize - 2) + 1; |
|
|
102 | ky = rmg_rndm (RP->Ysize - 2) + 1; |
|
|
103 | freeindex = find_free_spot (the_key, map, kx, ky, 1, SIZEOFFREE1 + 1); |
|
|
104 | } |
|
|
105 | |
|
|
106 | // can freeindex ever be < 0? |
|
|
107 | if (freeindex >= 0) |
|
|
108 | { |
|
|
109 | kx += freearr_x [freeindex]; |
|
|
110 | ky += freearr_y [freeindex]; |
|
|
111 | } |
|
|
112 | } |
|
|
113 | } |
|
|
114 | else |
|
|
115 | { /* NO_PASS_DOORS --we have to work harder. */ |
|
|
116 | /* don't try to keyplace if we're sitting on a blocked square and |
|
|
117 | NO_PASS_DOORS is set. */ |
|
|
118 | if (n_keys == 1) |
|
|
119 | { |
|
|
120 | if (wall_blocked (map, x, y)) |
|
|
121 | { |
|
|
122 | the_key->destroy (); |
|
|
123 | return 0; |
|
|
124 | } |
|
|
125 | |
|
|
126 | the_keymaster = find_monster_in_room (map, x, y, RP); |
|
|
127 | if (!the_keymaster) /* if fail, find a spot to drop the key. */ |
|
|
128 | find_spot_in_room (map, x, y, &kx, &ky, RP); |
|
|
129 | } |
|
|
130 | else |
|
|
131 | { |
|
|
132 | int sum = 0; /* count how many keys we actually place */ |
|
|
133 | |
|
|
134 | /* I'm lazy, so just try to place in all 4 directions. */ |
|
|
135 | sum += keyplace (map, x + 1, y, keycode, NO_PASS_DOORS, 1, RP); |
|
|
136 | sum += keyplace (map, x, y + 1, keycode, NO_PASS_DOORS, 1, RP); |
|
|
137 | sum += keyplace (map, x - 1, y, keycode, NO_PASS_DOORS, 1, RP); |
|
|
138 | sum += keyplace (map, x, y - 1, keycode, NO_PASS_DOORS, 1, RP); |
|
|
139 | |
|
|
140 | if (sum < 2) /* we might have made a disconnected map-place more keys. */ |
|
|
141 | { /* diagonally this time. */ |
|
|
142 | keyplace (map, x + 1, y + 1, keycode, NO_PASS_DOORS, 1, RP); |
|
|
143 | keyplace (map, x + 1, y - 1, keycode, NO_PASS_DOORS, 1, RP); |
|
|
144 | keyplace (map, x - 1, y + 1, keycode, NO_PASS_DOORS, 1, RP); |
|
|
145 | keyplace (map, x - 1, y - 1, keycode, NO_PASS_DOORS, 1, RP); |
|
|
146 | } |
|
|
147 | |
|
|
148 | the_key->destroy (); |
|
|
149 | return 1; |
|
|
150 | } |
|
|
151 | } |
|
|
152 | |
|
|
153 | LOG(llevError, "key %d,%d %p\n", the_keymaster ? the_keymaster->x : x, the_keymaster ? the_keymaster->y : y, the_keymaster+0);//D |
|
|
154 | if (the_keymaster) |
|
|
155 | the_keymaster->head_ ()->insert (the_key); |
|
|
156 | else |
|
|
157 | { |
|
|
158 | the_key->x = kx; |
|
|
159 | the_key->y = ky; |
|
|
160 | insert_ob_in_map (the_key, map, NULL, 0); |
|
|
161 | } |
|
|
162 | |
|
|
163 | return 1; |
|
|
164 | } |
|
|
165 | |
53 | /* returns true if square x,y has P_NO_PASS set, which is true for walls |
166 | /* returns true if square x,y has P_NO_PASS set, which is true for walls |
54 | * and doors but not monsters. |
167 | * and doors but not monsters. |
55 | * This function is not map tile aware. |
168 | * This function is not map tile aware. |
56 | */ |
169 | */ |
57 | int |
170 | int |
… | |
… | |
286 | /* set the chest lock code, and call the keyplacer routine with |
399 | /* set the chest lock code, and call the keyplacer routine with |
287 | the lockcode. It's not worth bothering to lock the chest if |
400 | the lockcode. It's not worth bothering to lock the chest if |
288 | there's only 1 treasure.... */ |
401 | there's only 1 treasure.... */ |
289 | if ((treasureoptions & KEYREQUIRED) && n_treasures > 1) |
402 | if ((treasureoptions & KEYREQUIRED) && n_treasures > 1) |
290 | { |
403 | { |
291 | char keybuf[1024]; |
404 | the_chest->slaying = format ("RMG-%d-%d", (int)rmg_rndm (1000000000), (int)rmg_rndm (1000000000)); |
292 | |
|
|
293 | sprintf (keybuf, "%d", rmg_rndm (1000000000)); |
|
|
294 | the_chest->slaying = keybuf; |
|
|
295 | keyplace (map, x, y, keybuf, PASS_DOORS, 1, RP); |
405 | keyplace (map, x, y, the_chest->slaying, PASS_DOORS, 1, RP); |
296 | } |
406 | } |
297 | |
407 | |
298 | /* actually place the chest. */ |
408 | /* actually place the chest. */ |
299 | the_chest->x = xl; |
409 | the_chest->x = xl; |
300 | the_chest->y = yl; |
410 | the_chest->y = yl; |
… | |
… | |
328 | return the_monster; |
438 | return the_monster; |
329 | } |
439 | } |
330 | } |
440 | } |
331 | return NULL; |
441 | return NULL; |
332 | } |
442 | } |
333 | |
|
|
334 | |
|
|
335 | |
|
|
336 | /* places keys in the map, preferably in something alive. |
|
|
337 | keycode is the key's code, |
|
|
338 | door_flag is either PASS_DOORS or NO_PASS_DOORS. |
|
|
339 | NO_PASS_DOORS won't cross doors or walls to keyplace, PASS_DOORS will. |
|
|
340 | if n_keys is 1, it will place 1 key. if n_keys >1, it will place 2-4 keys: |
|
|
341 | it will place 2-4 keys regardless of what nkeys is provided nkeys > 1. |
|
|
342 | |
|
|
343 | The idea is that you call keyplace on x,y where a door is, and it'll make |
|
|
344 | sure a key is placed on both sides of the door. |
|
|
345 | */ |
|
|
346 | int |
|
|
347 | keyplace (maptile *map, int x, int y, char *keycode, int door_flag, int n_keys, random_map_params *RP) |
|
|
348 | { |
|
|
349 | int i, j; |
|
|
350 | int kx = 0, ky = 0; |
|
|
351 | object *the_keymaster; /* the monster that gets the key. */ |
|
|
352 | |
|
|
353 | /* get a key and set its keycode */ |
|
|
354 | object *the_key = archetype::get (shstr_key2); |
|
|
355 | the_key->slaying = keycode; |
|
|
356 | |
|
|
357 | if (door_flag == PASS_DOORS) |
|
|
358 | { |
|
|
359 | int tries = 0; |
|
|
360 | |
|
|
361 | the_keymaster = 0; |
|
|
362 | while (tries < 15 && !the_keymaster) |
|
|
363 | { |
|
|
364 | i = rmg_rndm (RP->Xsize - 2) + 1; |
|
|
365 | j = rmg_rndm (RP->Ysize - 2) + 1; |
|
|
366 | tries++; |
|
|
367 | the_keymaster = find_closest_monster (map, i, j, RP); |
|
|
368 | } |
|
|
369 | |
|
|
370 | /* if we don't find a good keymaster, drop the key on the ground. */ |
|
|
371 | if (!the_keymaster) |
|
|
372 | { |
|
|
373 | int freeindex; |
|
|
374 | |
|
|
375 | freeindex = -1; |
|
|
376 | for (tries = 0; tries < 15 && freeindex == -1; tries++) |
|
|
377 | { |
|
|
378 | kx = rmg_rndm (RP->Xsize - 2) + 1; |
|
|
379 | ky = rmg_rndm (RP->Ysize - 2) + 1; |
|
|
380 | freeindex = find_free_spot (the_key, map, kx, ky, 1, SIZEOFFREE1 + 1); |
|
|
381 | } |
|
|
382 | |
|
|
383 | // can freeindex ever be < 0? |
|
|
384 | if (freeindex >= 0) |
|
|
385 | { |
|
|
386 | kx += freearr_x [freeindex]; |
|
|
387 | ky += freearr_y [freeindex]; |
|
|
388 | } |
|
|
389 | } |
|
|
390 | } |
|
|
391 | else |
|
|
392 | { /* NO_PASS_DOORS --we have to work harder. */ |
|
|
393 | /* don't try to keyplace if we're sitting on a blocked square and |
|
|
394 | NO_PASS_DOORS is set. */ |
|
|
395 | if (n_keys == 1) |
|
|
396 | { |
|
|
397 | if (wall_blocked (map, x, y)) |
|
|
398 | return 0; |
|
|
399 | |
|
|
400 | the_keymaster = find_monster_in_room (map, x, y, RP); |
|
|
401 | if (!the_keymaster) /* if fail, find a spot to drop the key. */ |
|
|
402 | find_spot_in_room (map, x, y, &kx, &ky, RP); |
|
|
403 | } |
|
|
404 | else |
|
|
405 | { |
|
|
406 | int sum = 0; /* count how many keys we actually place */ |
|
|
407 | |
|
|
408 | /* I'm lazy, so just try to place in all 4 directions. */ |
|
|
409 | sum += keyplace (map, x + 1, y, keycode, NO_PASS_DOORS, 1, RP); |
|
|
410 | sum += keyplace (map, x, y + 1, keycode, NO_PASS_DOORS, 1, RP); |
|
|
411 | sum += keyplace (map, x - 1, y, keycode, NO_PASS_DOORS, 1, RP); |
|
|
412 | sum += keyplace (map, x, y - 1, keycode, NO_PASS_DOORS, 1, RP); |
|
|
413 | |
|
|
414 | if (sum < 2) /* we might have made a disconnected map-place more keys. */ |
|
|
415 | { /* diagonally this time. */ |
|
|
416 | keyplace (map, x + 1, y + 1, keycode, NO_PASS_DOORS, 1, RP); |
|
|
417 | keyplace (map, x + 1, y - 1, keycode, NO_PASS_DOORS, 1, RP); |
|
|
418 | keyplace (map, x - 1, y + 1, keycode, NO_PASS_DOORS, 1, RP); |
|
|
419 | keyplace (map, x - 1, y - 1, keycode, NO_PASS_DOORS, 1, RP); |
|
|
420 | } |
|
|
421 | |
|
|
422 | return 1; |
|
|
423 | } |
|
|
424 | } |
|
|
425 | |
|
|
426 | if (!the_keymaster) |
|
|
427 | { |
|
|
428 | the_key->x = kx; |
|
|
429 | the_key->y = ky; |
|
|
430 | insert_ob_in_map (the_key, map, NULL, 0); |
|
|
431 | return 1; |
|
|
432 | } |
|
|
433 | |
|
|
434 | insert_ob_in_ob (the_key, the_keymaster->head_ ()); |
|
|
435 | return 1; |
|
|
436 | } |
|
|
437 | |
|
|
438 | |
|
|
439 | |
443 | |
440 | /* both find_monster_in_room routines need to have access to this. */ |
444 | /* both find_monster_in_room routines need to have access to this. */ |
441 | |
445 | |
442 | object *theMonsterToFind; |
446 | object *theMonsterToFind; |
443 | |
447 | |
… | |
… | |
819 | |
823 | |
820 | if (opts & DOORED) |
824 | if (opts & DOORED) |
821 | { |
825 | { |
822 | for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++) |
826 | for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++) |
823 | { |
827 | { |
824 | object *new_door = get_archetype ("locked_door1"); |
828 | object *new_door = get_archetype (shstr_locked_door1); |
825 | char keybuf[1024]; |
|
|
826 | |
829 | |
827 | door = doorlist[i]; |
830 | door = doorlist[i]; |
828 | new_door->face = door->face; |
831 | new_door->face = door->face; |
829 | new_door->x = door->x; |
832 | new_door->x = door->x; |
830 | new_door->y = door->y; |
833 | new_door->y = door->y; |
831 | door->remove (); |
834 | door->remove (); |
832 | door->destroy (); |
835 | door->destroy (); |
833 | doorlist[i] = new_door; |
836 | doorlist[i] = new_door; |
834 | insert_ob_in_map (new_door, map, NULL, 0); |
837 | insert_ob_in_map (new_door, map, NULL, 0); |
835 | sprintf (keybuf, "%d", rmg_rndm (1000000000)); |
838 | new_door->slaying = format ("RMG-%d-%d", (int)rmg_rndm (1000000000), (int)rmg_rndm (1000000000)); |
836 | new_door->slaying = keybuf; |
|
|
837 | keyplace (map, new_door->x, new_door->y, keybuf, NO_PASS_DOORS, 2, RP); |
839 | keyplace (map, new_door->x, new_door->y, new_door->slaying, NO_PASS_DOORS, 2, RP); |
838 | } |
840 | } |
839 | } |
841 | } |
840 | |
842 | |
841 | /* change the faces of the doors and surrounding walls to hide them. */ |
843 | /* change the faces of the doors and surrounding walls to hide them. */ |
842 | if (opts & HIDDEN) |
844 | if (opts & HIDDEN) |