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.39 by root, Thu May 8 11:39:24 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
85 if (door_flag == PASS_DOORS)
86 {
87 int tries = 0;
88
89 the_keymaster = 0;
90 while (tries < 15 && !the_keymaster)
91 {
92 i = rmg_rndm (RP->Xsize - 2) + 1;
93 j = rmg_rndm (RP->Ysize - 2) + 1;
94 tries++;
95 the_keymaster = find_closest_monster (map, i, j, RP);
96 }
97
98 /* if we don't find a good keymaster, drop the key on the ground. */
99 if (!the_keymaster)
100 {
101 int freeindex;
102
103 freeindex = -1;
104 for (tries = 0; tries < 15 && freeindex == -1; tries++)
105 {
106 kx = rmg_rndm (RP->Xsize - 2) + 1;
107 ky = rmg_rndm (RP->Ysize - 2) + 1;
108 freeindex = find_free_spot (gen_key (keycode), map, kx, ky, 1, SIZEOFFREE1 + 1);
109 }
110
111 // can freeindex ever be < 0?
112 if (freeindex >= 0)
113 {
114 kx += freearr_x [freeindex];
115 ky += freearr_y [freeindex];
116 }
117 }
118 }
119 else
120 { /* NO_PASS_DOORS --we have to work harder. */
121 /* don't try to keyplace if we're sitting on a blocked square and
122 NO_PASS_DOORS is set. */
123 if (n_keys == 1)
124 {
125 if (wall_blocked (map, x, y))
126 return 0;
127
128 the_keymaster = find_monster_in_room (map, x, y, RP);
129 if (!the_keymaster) /* if fail, find a spot to drop the key. */
130 find_spot_in_room (map, x, y, &kx, &ky, RP);
131 }
132 else
133 {
134 int sum = 0; /* count how many keys we actually place */
135
136 /* I'm lazy, so just try to place in all 4 directions. */
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 sum += keyplace (map, x - 1, y, keycode, NO_PASS_DOORS, 1, RP);
140 sum += keyplace (map, x, y - 1, keycode, NO_PASS_DOORS, 1, RP);
141
142 if (sum < 2) /* we might have made a disconnected map-place more keys. */
143 { /* diagonally this time. */
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 keyplace (map, x - 1, y + 1, keycode, NO_PASS_DOORS, 1, RP);
147 keyplace (map, x - 1, y - 1, keycode, NO_PASS_DOORS, 1, RP);
148 }
149
150 return 1;
151 }
152 }
153
154 object *the_key = gen_key (keycode);
155
156 if (the_keymaster)
157 the_keymaster->head_ ()->insert (the_key);
158 else
159 {
160 the_key->x = kx;
161 the_key->y = ky;
162 insert_ob_in_map (the_key, map, NULL, 0);
163 }
164
165 return 1;
166}
167
53/* returns true if square x,y has P_NO_PASS set, which is true for walls 168/* returns true if square x,y has P_NO_PASS set, which is true for walls
54 * and doors but not monsters. 169 * and doors but not monsters.
55 * This function is not map tile aware. 170 * This function is not map tile aware.
56 */ 171 */
57int 172int
286 /* set the chest lock code, and call the keyplacer routine with 401 /* set the chest lock code, and call the keyplacer routine with
287 the lockcode. It's not worth bothering to lock the chest if 402 the lockcode. It's not worth bothering to lock the chest if
288 there's only 1 treasure.... */ 403 there's only 1 treasure.... */
289 if ((treasureoptions & KEYREQUIRED) && n_treasures > 1) 404 if ((treasureoptions & KEYREQUIRED) && n_treasures > 1)
290 { 405 {
291 char keybuf[1024]; 406 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); 407 keyplace (map, x, y, the_chest->slaying, PASS_DOORS, 1, RP);
296 } 408 }
297 409
298 /* actually place the chest. */ 410 /* actually place the chest. */
299 the_chest->x = xl; 411 the_chest->x = xl;
300 the_chest->y = yl; 412 the_chest->y = yl;
328 return the_monster; 440 return the_monster;
329 } 441 }
330 } 442 }
331 return NULL; 443 return NULL;
332} 444}
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 445
440/* both find_monster_in_room routines need to have access to this. */ 446/* both find_monster_in_room routines need to have access to this. */
441 447
442object *theMonsterToFind; 448object *theMonsterToFind;
443 449
820 if (opts & DOORED) 826 if (opts & DOORED)
821 { 827 {
822 for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++) 828 for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++)
823 { 829 {
824 object *new_door = get_archetype ("locked_door1"); 830 object *new_door = get_archetype ("locked_door1");
825 char keybuf[1024];
826 831
827 door = doorlist[i]; 832 door = doorlist[i];
828 new_door->face = door->face; 833 new_door->face = door->face;
829 new_door->x = door->x; 834 new_door->x = door->x;
830 new_door->y = door->y; 835 new_door->y = door->y;
831 door->remove (); 836 door->remove ();
832 door->destroy (); 837 door->destroy ();
833 doorlist[i] = new_door; 838 doorlist[i] = new_door;
834 insert_ob_in_map (new_door, map, NULL, 0); 839 insert_ob_in_map (new_door, map, NULL, 0);
835 sprintf (keybuf, "%d", rmg_rndm (1000000000)); 840 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); 841 keyplace (map, new_door->x, new_door->y, new_door->slaying, NO_PASS_DOORS, 2, RP);
838 } 842 }
839 } 843 }
840 844
841 /* change the faces of the doors and surrounding walls to hide them. */ 845 /* change the faces of the doors and surrounding walls to hide them. */
842 if (opts & HIDDEN) 846 if (opts & HIDDEN)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines