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.38 by root, Sun May 4 14:12:38 2008 UTC vs.
Revision 1.43 by root, Mon Sep 29 09:04:51 2008 UTC

48bc_random (int x) 48bc_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
53static object *
54gen_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*/
72static int
73keyplace (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 (true);
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 (true);
149 return 1;
150 }
151 }
152
153 if (the_keymaster)
154 the_keymaster->head_ ()->insert (the_key);
155 else
156 {
157 the_key->x = kx;
158 the_key->y = ky;
159 insert_ob_in_map (the_key, map, NULL, 0);
160 }
161
162 return 1;
163}
164
53/* returns true if square x,y has P_NO_PASS set, which is true for walls 165/* returns true if square x,y has P_NO_PASS set, which is true for walls
54 * and doors but not monsters. 166 * and doors but not monsters.
55 * This function is not map tile aware. 167 * This function is not map tile aware.
56 */ 168 */
57int 169int
219 331
220 /* first, find a place to put the chest. */ 332 /* first, find a place to put the chest. */
221 int i = find_first_free_spot (the_chest, map, x, y); // this call uses the main rng 333 int i = find_first_free_spot (the_chest, map, x, y); // this call uses the main rng
222 if (i == -1) 334 if (i == -1)
223 { 335 {
224 the_chest->destroy (); 336 the_chest->destroy (true);
225 return NULL; 337 return NULL;
226 } 338 }
227 339
228 int xl = x + freearr_x[i]; 340 int xl = x + freearr_x[i];
229 int yl = y + freearr_y[i]; 341 int yl = y + freearr_y[i];
286 /* set the chest lock code, and call the keyplacer routine with 398 /* set the chest lock code, and call the keyplacer routine with
287 the lockcode. It's not worth bothering to lock the chest if 399 the lockcode. It's not worth bothering to lock the chest if
288 there's only 1 treasure.... */ 400 there's only 1 treasure.... */
289 if ((treasureoptions & KEYREQUIRED) && n_treasures > 1) 401 if ((treasureoptions & KEYREQUIRED) && n_treasures > 1)
290 { 402 {
291 char keybuf[1024]; 403 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); 404 keyplace (map, x, y, the_chest->slaying, PASS_DOORS, 1, RP);
296 } 405 }
297 406
298 /* actually place the chest. */ 407 /* actually place the chest. */
299 the_chest->x = xl; 408 the_chest->x = xl;
300 the_chest->y = yl; 409 the_chest->y = yl;
329 } 438 }
330 } 439 }
331 return NULL; 440 return NULL;
332} 441}
333 442
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*/
346int
347keyplace (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
440/* both find_monster_in_room routines need to have access to this. */ 443/* both find_monster_in_room routines need to have access to this. */
441 444
442object *theMonsterToFind; 445object *theMonsterToFind;
443 446
444/* a recursive routine which will return a monster, eventually,if there is one. 447/* a recursive routine which will return a monster, eventually,if there is one.
666} 669}
667 670
668void 671void
669remove_monsters (int x, int y, maptile *map) 672remove_monsters (int x, int y, maptile *map)
670{ 673{
671 object *tmp;
672
673 for (tmp = GET_MAP_OB (map, x, y); tmp; tmp = tmp->above) 674 for (object *tmp = GET_MAP_OB (map, x, y); tmp; )
674 if (QUERY_FLAG (tmp, FLAG_ALIVE))
675 { 675 {
676 if (tmp->head) 676 object *next = tmp->above;
677 tmp = tmp->head; 677
678 tmp->remove (); 678 if (tmp->flag [FLAG_ALIVE])
679 tmp->destroy (); 679 tmp->head_ ()->destroy (true);
680 tmp = GET_MAP_OB (map, x, y); 680
681 if (tmp == NULL) 681 tmp = next;
682 break;
683 }; 682 }
684} 683}
685 684
686/* surrounds the point x,y by doors, so as to enclose something, like 685/* surrounds the point x,y by doors, so as to enclose something, like
687 a chest. It only goes as far as the 8 squares surrounding, and 686 a chest. It only goes as far as the 8 squares surrounding, and
688 it'll remove any monsters it finds.*/ 687 it'll remove any monsters it finds.*/
817 816
818 /* lock the doors and hide the keys. */ 817 /* lock the doors and hide the keys. */
819 818
820 if (opts & DOORED) 819 if (opts & DOORED)
821 { 820 {
822 for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++) 821 for (i = 0, door = doorlist[0]; doorlist[i]; i++)
823 { 822 {
824 object *new_door = get_archetype ("locked_door1"); 823 object *new_door = get_archetype (shstr_locked_door1);
825 char keybuf[1024];
826 824
827 door = doorlist[i]; 825 door = doorlist[i];
828 new_door->face = door->face; 826 new_door->face = door->face;
829 new_door->x = door->x; 827 new_door->x = door->x;
830 new_door->y = door->y; 828 new_door->y = door->y;
831 door->remove ();
832 door->destroy (); 829 door->destroy (true);
833 doorlist[i] = new_door; 830 doorlist[i] = new_door;
834 insert_ob_in_map (new_door, map, NULL, 0); 831 insert_ob_in_map (new_door, map, NULL, 0);
835 sprintf (keybuf, "%d", rmg_rndm (1000000000)); 832 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); 833 keyplace (map, new_door->x, new_door->y, new_door->slaying, NO_PASS_DOORS, 2, RP);
838 } 834 }
839 } 835 }
840 836
841 /* change the faces of the doors and surrounding walls to hide them. */ 837 /* change the faces of the doors and surrounding walls to hide them. */
842 if (opts & HIDDEN) 838 if (opts & HIDDEN)
854 retrofit_joined_wall (map, door->x, door->y - 1, 0, RP); 850 retrofit_joined_wall (map, door->x, door->y - 1, 0, RP);
855 retrofit_joined_wall (map, door->x, door->y + 1, 0, RP); 851 retrofit_joined_wall (map, door->x, door->y + 1, 0, RP);
856 852
857 door->face = wallface->face; 853 door->face = wallface->face;
858 854
859 if (!QUERY_FLAG (wallface, FLAG_REMOVED))
860 wallface->remove ();
861
862 wallface->destroy (); 855 wallface->destroy (true);
863 } 856 }
864 } 857 }
865 } 858 }
866} 859}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines