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.40 by root, Thu May 8 13:47:19 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_key2);
58
59 key->slaying = keycode;
60 key->stats.food = 100;
61 key->speed_left = -1.f;
62 key->flag [FLAG_IS_USED_UP] = true;
63 key->set_speed (1.f / 300.f);
64
65 return key;
66}
67
68/* places keys in the map, preferably in something alive.
69 keycode is the key's code,
70 door_flag is either PASS_DOORS or NO_PASS_DOORS.
71 NO_PASS_DOORS won't cross doors or walls to keyplace, PASS_DOORS will.
72 if n_keys is 1, it will place 1 key. if n_keys >1, it will place 2-4 keys:
73 it will place 2-4 keys regardless of what nkeys is provided nkeys > 1.
74
75 The idea is that you call keyplace on x,y where a door is, and it'll make
76 sure a key is placed on both sides of the door.
77*/
78static int
79keyplace (maptile *map, int x, int y, const shstr &keycode, int door_flag, int n_keys, random_map_params *RP)
80{
81 int i, j;
82 int kx = 0, ky = 0;
83 object *the_keymaster; /* the monster that gets the key. */
84 object *the_key = gen_key (keycode);
85
86 if (door_flag == PASS_DOORS)
87 {
88 int tries = 0;
89
90 the_keymaster = 0;
91 while (tries < 15 && !the_keymaster)
92 {
93 i = rmg_rndm (RP->Xsize - 2) + 1;
94 j = rmg_rndm (RP->Ysize - 2) + 1;
95 tries++;
96 the_keymaster = find_closest_monster (map, i, j, RP);
97 }
98
99 /* if we don't find a good keymaster, drop the key on the ground. */
100 if (!the_keymaster)
101 {
102 int freeindex;
103
104 freeindex = -1;
105 for (tries = 0; tries < 15 && freeindex == -1; tries++)
106 {
107 kx = rmg_rndm (RP->Xsize - 2) + 1;
108 ky = rmg_rndm (RP->Ysize - 2) + 1;
109 freeindex = find_free_spot (the_key, map, kx, ky, 1, SIZEOFFREE1 + 1);
110 }
111
112 // can freeindex ever be < 0?
113 if (freeindex >= 0)
114 {
115 kx += freearr_x [freeindex];
116 ky += freearr_y [freeindex];
117 }
118 }
119 }
120 else
121 { /* NO_PASS_DOORS --we have to work harder. */
122 /* don't try to keyplace if we're sitting on a blocked square and
123 NO_PASS_DOORS is set. */
124 if (n_keys == 1)
125 {
126 if (wall_blocked (map, x, y))
127 {
128 the_key->destroy ();
129 return 0;
130 }
131
132 the_keymaster = find_monster_in_room (map, x, y, RP);
133 if (!the_keymaster) /* if fail, find a spot to drop the key. */
134 find_spot_in_room (map, x, y, &kx, &ky, RP);
135 }
136 else
137 {
138 int sum = 0; /* count how many keys we actually place */
139
140 /* I'm lazy, so just try to place in all 4 directions. */
141 sum += keyplace (map, x + 1, y, keycode, NO_PASS_DOORS, 1, RP);
142 sum += keyplace (map, x, y + 1, keycode, NO_PASS_DOORS, 1, RP);
143 sum += keyplace (map, x - 1, y, keycode, NO_PASS_DOORS, 1, RP);
144 sum += keyplace (map, x, y - 1, keycode, NO_PASS_DOORS, 1, RP);
145
146 if (sum < 2) /* we might have made a disconnected map-place more keys. */
147 { /* diagonally this time. */
148 keyplace (map, x + 1, y + 1, keycode, NO_PASS_DOORS, 1, RP);
149 keyplace (map, x + 1, y - 1, keycode, NO_PASS_DOORS, 1, RP);
150 keyplace (map, x - 1, y + 1, keycode, NO_PASS_DOORS, 1, RP);
151 keyplace (map, x - 1, y - 1, keycode, NO_PASS_DOORS, 1, RP);
152 }
153
154 the_key->destroy ();
155 return 1;
156 }
157 }
158
159 if (the_keymaster)
160 the_keymaster->head_ ()->insert (the_key);
161 else
162 {
163 the_key->x = kx;
164 the_key->y = ky;
165 insert_ob_in_map (the_key, map, NULL, 0);
166 }
167
168 return 1;
169}
170
53/* returns true if square x,y has P_NO_PASS set, which is true for walls 171/* returns true if square x,y has P_NO_PASS set, which is true for walls
54 * and doors but not monsters. 172 * and doors but not monsters.
55 * This function is not map tile aware. 173 * This function is not map tile aware.
56 */ 174 */
57int 175int
286 /* set the chest lock code, and call the keyplacer routine with 404 /* set the chest lock code, and call the keyplacer routine with
287 the lockcode. It's not worth bothering to lock the chest if 405 the lockcode. It's not worth bothering to lock the chest if
288 there's only 1 treasure.... */ 406 there's only 1 treasure.... */
289 if ((treasureoptions & KEYREQUIRED) && n_treasures > 1) 407 if ((treasureoptions & KEYREQUIRED) && n_treasures > 1)
290 { 408 {
291 char keybuf[1024]; 409 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); 410 keyplace (map, x, y, the_chest->slaying, PASS_DOORS, 1, RP);
296 } 411 }
297 412
298 /* actually place the chest. */ 413 /* actually place the chest. */
299 the_chest->x = xl; 414 the_chest->x = xl;
300 the_chest->y = yl; 415 the_chest->y = yl;
328 return the_monster; 443 return the_monster;
329 } 444 }
330 } 445 }
331 return NULL; 446 return NULL;
332} 447}
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*/
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 448
440/* both find_monster_in_room routines need to have access to this. */ 449/* both find_monster_in_room routines need to have access to this. */
441 450
442object *theMonsterToFind; 451object *theMonsterToFind;
443 452
820 if (opts & DOORED) 829 if (opts & DOORED)
821 { 830 {
822 for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++) 831 for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++)
823 { 832 {
824 object *new_door = get_archetype ("locked_door1"); 833 object *new_door = get_archetype ("locked_door1");
825 char keybuf[1024];
826 834
827 door = doorlist[i]; 835 door = doorlist[i];
828 new_door->face = door->face; 836 new_door->face = door->face;
829 new_door->x = door->x; 837 new_door->x = door->x;
830 new_door->y = door->y; 838 new_door->y = door->y;
831 door->remove (); 839 door->remove ();
832 door->destroy (); 840 door->destroy ();
833 doorlist[i] = new_door; 841 doorlist[i] = new_door;
834 insert_ob_in_map (new_door, map, NULL, 0); 842 insert_ob_in_map (new_door, map, NULL, 0);
835 sprintf (keybuf, "%d", rmg_rndm (1000000000)); 843 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); 844 keyplace (map, new_door->x, new_door->y, new_door->slaying, NO_PASS_DOORS, 2, RP);
838 } 845 }
839 } 846 }
840 847
841 /* change the faces of the doors and surrounding walls to hide them. */ 848 /* change the faces of the doors and surrounding walls to hide them. */
842 if (opts & HIDDEN) 849 if (opts & HIDDEN)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines