1 | /* |
1 | /* |
2 | <<<<<<< treasure.C |
|
|
3 | * CrossFire, A Multiplayer game |
2 | * CrossFire, A Multiplayer game |
4 | * |
3 | * |
5 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
4 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
6 | * Copyright (C) 2001 Mark Wedel & Crossfire Development Team |
5 | * Copyright (C) 2001 Mark Wedel & Crossfire Development Team |
7 | * Copyright (C) 1992 Frank Tore Johansen |
6 | * Copyright (C) 1992 Frank Tore Johansen |
… | |
… | |
20 | * along with this program; if not, write to the Free Software |
19 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
22 | * |
21 | * |
23 | * The authors can be reached via e-mail at <crossfire@schmorp.de> |
22 | * The authors can be reached via e-mail at <crossfire@schmorp.de> |
24 | */ |
23 | */ |
25 | ======= |
|
|
26 | * CrossFire, A Multiplayer game for X-windows |
|
|
27 | * |
|
|
28 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
|
|
29 | * Copyright (C) 2001 Mark Wedel & Crossfire Development Team |
|
|
30 | * Copyright (C) 1992 Frank Tore Johansen |
|
|
31 | * |
|
|
32 | * This program is free software; you can redistribute it and/or modify |
|
|
33 | * it under the terms of the GNU General Public License as published by |
|
|
34 | * the Free Software Foundation; either version 2 of the License, or |
|
|
35 | * (at your option) any later version. |
|
|
36 | * |
|
|
37 | * This program is distributed in the hope that it will be useful, |
|
|
38 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
39 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
40 | * GNU General Public License for more details. |
|
|
41 | * |
|
|
42 | * You should have received a copy of the GNU General Public License |
|
|
43 | * along with this program; if not, write to the Free Software |
|
|
44 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
|
45 | * |
|
|
46 | * The authors can be reached via e-mail at <crossfire@schmorp.de> |
|
|
47 | */ |
|
|
48 | >>>>>>> 1.18 |
|
|
49 | |
24 | |
50 | /* placing treasure in maps, where appropriate. */ |
25 | /* placing treasure in maps, where appropriate. */ |
51 | |
26 | |
52 | #include <global.h> |
27 | #include <global.h> |
53 | #include <random_map.h> |
28 | #include <random_map.h> |
… | |
… | |
66 | #define LAST_OPTION 64 /* set this to the last real option, for random */ |
41 | #define LAST_OPTION 64 /* set this to the last real option, for random */ |
67 | |
42 | |
68 | #define NO_PASS_DOORS 0 |
43 | #define NO_PASS_DOORS 0 |
69 | #define PASS_DOORS 1 |
44 | #define PASS_DOORS 1 |
70 | |
45 | |
|
|
46 | /* a macro to get a strongly centered random distribution, |
|
|
47 | from 0 to x, centered at x/2 */ |
|
|
48 | static int |
|
|
49 | bc_random (int x) |
|
|
50 | { |
|
|
51 | return (rndm (x) + rndm (x) + rndm (x)) / 3; |
|
|
52 | } |
71 | |
53 | |
72 | /* returns true if square x,y has P_NO_PASS set, which is true for walls |
54 | /* returns true if square x,y has P_NO_PASS set, which is true for walls |
73 | * and doors but not monsters. |
55 | * and doors but not monsters. |
74 | * This function is not map tile aware. |
56 | * This function is not map tile aware. |
75 | */ |
57 | */ |
… | |
… | |
99 | |
81 | |
100 | /* bail out if treasure isn't wanted. */ |
82 | /* bail out if treasure isn't wanted. */ |
101 | if (treasure_style) |
83 | if (treasure_style) |
102 | if (!strcmp (treasure_style, "none")) |
84 | if (!strcmp (treasure_style, "none")) |
103 | return; |
85 | return; |
|
|
86 | |
104 | if (treasureoptions <= 0) |
87 | if (treasureoptions <= 0) |
105 | treasureoptions = RANDOM () % (2 * LAST_OPTION); |
88 | treasureoptions = rndm (2 * LAST_OPTION); |
106 | |
89 | |
107 | /* filter out the mutually exclusive options */ |
90 | /* filter out the mutually exclusive options */ |
108 | if ((treasureoptions & RICH) && (treasureoptions & SPARSE)) |
91 | if ((treasureoptions & RICH) && (treasureoptions & SPARSE)) |
109 | { |
92 | { |
110 | if (RANDOM () % 2) |
93 | if (rndm (2)) |
111 | treasureoptions -= 1; |
94 | treasureoptions -= 1; |
112 | else |
95 | else |
113 | treasureoptions -= 2; |
96 | treasureoptions -= 2; |
114 | } |
97 | } |
115 | |
98 | |
116 | /* pick the number of treasures */ |
99 | /* pick the number of treasures */ |
117 | if (treasureoptions & SPARSE) |
100 | if (treasureoptions & SPARSE) |
118 | num_treasures = BC_RANDOM (RP->total_map_hp / 600 + RP->difficulty / 2 + 1); |
101 | num_treasures = bc_random (RP->total_map_hp / 600 + RP->difficulty / 2 + 1); |
119 | else if (treasureoptions & RICH) |
102 | else if (treasureoptions & RICH) |
120 | num_treasures = BC_RANDOM (RP->total_map_hp / 150 + 2 * RP->difficulty + 1); |
103 | num_treasures = bc_random (RP->total_map_hp / 150 + 2 * RP->difficulty + 1); |
121 | else |
104 | else |
122 | num_treasures = BC_RANDOM (RP->total_map_hp / 300 + RP->difficulty + 1); |
105 | num_treasures = bc_random (RP->total_map_hp / 300 + RP->difficulty + 1); |
123 | |
106 | |
124 | if (num_treasures <= 0) |
107 | if (num_treasures <= 0) |
125 | return; |
108 | return; |
126 | |
109 | |
127 | /* get the style map */ |
110 | /* get the style map */ |
128 | sprintf (styledirname, "%s", "/styles/treasurestyles"); |
111 | sprintf (styledirname, "%s", "/styles/treasurestyles"); |
129 | sprintf (stylefilepath, "%s/%s", styledirname, treasure_style); |
112 | sprintf (stylefilepath, "%s/%s", styledirname, treasure_style); |
130 | style_map = find_style (styledirname, treasure_style, -1); |
113 | style_map = find_style (styledirname, treasure_style, -1); |
|
|
114 | |
|
|
115 | if (!style_map) |
|
|
116 | { |
|
|
117 | LOG (llevError, "unable to load style map %s %s.\n", styledirname, treasure_style); |
|
|
118 | return; |
|
|
119 | } |
131 | |
120 | |
132 | /* all the treasure at one spot in the map. */ |
121 | /* all the treasure at one spot in the map. */ |
133 | if (treasureoptions & CONCENTRATED) |
122 | if (treasureoptions & CONCENTRATED) |
134 | { |
123 | { |
135 | |
124 | |
… | |
… | |
178 | |
167 | |
179 | i = j = -1; |
168 | i = j = -1; |
180 | tries = 0; |
169 | tries = 0; |
181 | while (i == -1 && tries < 100) |
170 | while (i == -1 && tries < 100) |
182 | { |
171 | { |
183 | i = RANDOM () % (RP->Xsize - 2) + 1; |
172 | i = rndm (RP->Xsize - 2) + 1; |
184 | j = RANDOM () % (RP->Ysize - 2) + 1; |
173 | j = rndm (RP->Ysize - 2) + 1; |
185 | find_enclosed_spot (map, &i, &j, RP); |
174 | find_enclosed_spot (map, &i, &j, RP); |
186 | if (wall_blocked (map, i, j)) |
175 | if (wall_blocked (map, i, j)) |
187 | i = -1; |
176 | i = -1; |
188 | tries++; |
177 | tries++; |
189 | } |
178 | } |
… | |
… | |
205 | { /* DIFFUSE treasure layout */ |
194 | { /* DIFFUSE treasure layout */ |
206 | int ti, i, j; |
195 | int ti, i, j; |
207 | |
196 | |
208 | for (ti = 0; ti < num_treasures; ti++) |
197 | for (ti = 0; ti < num_treasures; ti++) |
209 | { |
198 | { |
210 | i = RANDOM () % (RP->Xsize - 2) + 1; |
199 | i = rndm (RP->Xsize - 2) + 1; |
211 | j = RANDOM () % (RP->Ysize - 2) + 1; |
200 | j = rndm (RP->Ysize - 2) + 1; |
212 | place_chest (treasureoptions, i, j, map, style_map, 1, RP); |
201 | place_chest (treasureoptions, i, j, map, style_map, 1, RP); |
213 | } |
202 | } |
214 | } |
203 | } |
215 | } |
204 | } |
216 | |
205 | |
… | |
… | |
250 | treasurelist *tlist = find_treasurelist (RP->treasurestyle); |
239 | treasurelist *tlist = find_treasurelist (RP->treasurestyle); |
251 | |
240 | |
252 | if (tlist != NULL) |
241 | if (tlist != NULL) |
253 | for (ti = 0; ti < n_treasures; ti++) |
242 | for (ti = 0; ti < n_treasures; ti++) |
254 | { /* use the treasure list */ |
243 | { /* use the treasure list */ |
255 | object *new_treasure = pick_random_object (style_map); |
244 | object *new_treasure = style_map->pick_random_object (); |
256 | |
245 | |
257 | insert_ob_in_ob (arch_to_object (new_treasure->arch), the_chest); |
246 | insert_ob_in_ob (arch_to_object (new_treasure->arch), the_chest); |
258 | } |
247 | } |
259 | else |
248 | else |
260 | { /* use the style map */ |
249 | { /* use the style map */ |
… | |
… | |
275 | maptile *trap_map = find_style ("/styles/trapstyles", "traps", -1); |
264 | maptile *trap_map = find_style ("/styles/trapstyles", "traps", -1); |
276 | object *the_trap; |
265 | object *the_trap; |
277 | |
266 | |
278 | if (trap_map) |
267 | if (trap_map) |
279 | { |
268 | { |
280 | the_trap = pick_random_object (trap_map); |
269 | the_trap = trap_map->pick_random_object (); |
281 | the_trap->stats.Cha = 10 + RP->difficulty; |
270 | the_trap->stats.Cha = 10 + RP->difficulty; |
282 | the_trap->level = BC_RANDOM ((3 * RP->difficulty) / 2); |
271 | the_trap->level = bc_random ((3 * RP->difficulty) / 2); |
283 | if (the_trap) |
272 | if (the_trap) |
284 | { |
273 | { |
285 | object *new_trap; |
274 | object *new_trap; |
286 | |
275 | |
287 | new_trap = arch_to_object (the_trap->arch); |
276 | new_trap = arch_to_object (the_trap->arch); |
… | |
… | |
294 | } |
283 | } |
295 | |
284 | |
296 | /* set the chest lock code, and call the keyplacer routine with |
285 | /* set the chest lock code, and call the keyplacer routine with |
297 | the lockcode. It's not worth bothering to lock the chest if |
286 | the lockcode. It's not worth bothering to lock the chest if |
298 | there's only 1 treasure.... */ |
287 | there's only 1 treasure.... */ |
299 | |
|
|
300 | if ((treasureoptions & KEYREQUIRED) && n_treasures > 1) |
288 | if ((treasureoptions & KEYREQUIRED) && n_treasures > 1) |
301 | { |
289 | { |
302 | char keybuf[1024]; |
290 | char keybuf[1024]; |
303 | |
291 | |
304 | sprintf (keybuf, "%d", (int) RANDOM ()); |
292 | sprintf (keybuf, "%d", rndm (1000000000)); |
305 | the_chest->slaying = keybuf; |
293 | the_chest->slaying = keybuf; |
306 | keyplace (map, x, y, keybuf, PASS_DOORS, 1, RP); |
294 | keyplace (map, x, y, keybuf, PASS_DOORS, 1, RP); |
307 | } |
295 | } |
308 | |
296 | |
309 | /* actually place the chest. */ |
297 | /* actually place the chest. */ |
… | |
… | |
371 | int tries = 0; |
359 | int tries = 0; |
372 | |
360 | |
373 | the_keymaster = 0; |
361 | the_keymaster = 0; |
374 | while (tries < 15 && !the_keymaster) |
362 | while (tries < 15 && !the_keymaster) |
375 | { |
363 | { |
376 | i = (RANDOM () % (RP->Xsize - 2)) + 1; |
364 | i = rndm (RP->Xsize - 2) + 1; |
377 | j = (RANDOM () % (RP->Ysize - 2)) + 1; |
365 | j = rndm (RP->Ysize - 2) + 1; |
378 | tries++; |
366 | tries++; |
379 | the_keymaster = find_closest_monster (map, i, j, RP); |
367 | the_keymaster = find_closest_monster (map, i, j, RP); |
380 | } |
368 | } |
381 | |
369 | |
382 | /* 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. */ |
… | |
… | |
385 | int freeindex; |
373 | int freeindex; |
386 | |
374 | |
387 | freeindex = -1; |
375 | freeindex = -1; |
388 | for (tries = 0; tries < 15 && freeindex == -1; tries++) |
376 | for (tries = 0; tries < 15 && freeindex == -1; tries++) |
389 | { |
377 | { |
390 | kx = (RANDOM () % (RP->Xsize - 2)) + 1; |
378 | kx = rndm (RP->Xsize - 2) + 1; |
391 | ky = (RANDOM () % (RP->Ysize - 2)) + 1; |
379 | ky = rndm (RP->Ysize - 2) + 1; |
392 | 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); |
393 | } |
381 | } |
394 | |
382 | |
395 | // can freeindex ever be < 0? |
383 | // can freeindex ever be < 0? |
396 | if (freeindex >= 0) |
384 | if (freeindex >= 0) |
… | |
… | |
488 | return theMonsterToFind; |
476 | return theMonsterToFind; |
489 | } |
477 | } |
490 | } |
478 | } |
491 | |
479 | |
492 | /* 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 */ |
493 | for (i = RANDOM () % 8, j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) |
481 | for (i = rndm (8), j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) |
494 | { |
482 | { |
495 | 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); |
496 | if (theMonsterToFind != NULL) |
484 | if (theMonsterToFind != NULL) |
497 | return theMonsterToFind; |
485 | return theMonsterToFind; |
498 | } |
486 | } |
… | |
… | |
561 | room_free_spots_x[number_of_free_spots_in_room] = x; |
549 | room_free_spots_x[number_of_free_spots_in_room] = x; |
562 | room_free_spots_y[number_of_free_spots_in_room] = y; |
550 | room_free_spots_y[number_of_free_spots_in_room] = y; |
563 | number_of_free_spots_in_room++; |
551 | number_of_free_spots_in_room++; |
564 | |
552 | |
565 | /* now search all the 8 squares around recursively for free spots,in random order */ |
553 | /* now search all the 8 squares around recursively for free spots,in random order */ |
566 | for (i = RANDOM () % 8, j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) |
554 | for (i = rndm (8), j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) |
567 | find_spot_in_room_recursive (layout, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); |
555 | find_spot_in_room_recursive (layout, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); |
568 | |
556 | |
569 | } |
557 | } |
570 | |
558 | |
571 | /* find a random non-blocked spot in this room to drop a key. */ |
559 | /* find a random non-blocked spot in this room to drop a key. */ |
… | |
… | |
592 | /* setup num_free_spots and room_free_spots */ |
580 | /* setup num_free_spots and room_free_spots */ |
593 | find_spot_in_room_recursive (layout2, x, y, RP); |
581 | find_spot_in_room_recursive (layout2, x, y, RP); |
594 | |
582 | |
595 | if (number_of_free_spots_in_room > 0) |
583 | if (number_of_free_spots_in_room > 0) |
596 | { |
584 | { |
597 | i = RANDOM () % number_of_free_spots_in_room; |
585 | i = rndm (number_of_free_spots_in_room); |
598 | *kx = room_free_spots_x[i]; |
586 | *kx = room_free_spots_x[i]; |
599 | *ky = room_free_spots_y[i]; |
587 | *ky = room_free_spots_y[i]; |
600 | } |
588 | } |
601 | |
589 | |
602 | /* deallocate the temp. layout */ |
590 | /* deallocate the temp. layout */ |
… | |
… | |
685 | *cx = -1; |
673 | *cx = -1; |
686 | *cy = -1; |
674 | *cy = -1; |
687 | } |
675 | } |
688 | } |
676 | } |
689 | |
677 | |
690 | |
|
|
691 | void |
678 | void |
692 | remove_monsters (int x, int y, maptile *map) |
679 | remove_monsters (int x, int y, maptile *map) |
693 | { |
680 | { |
694 | object *tmp; |
681 | object *tmp; |
695 | |
682 | |
696 | for (tmp = GET_MAP_OB (map, x, y); tmp != NULL; tmp = tmp->above) |
683 | for (tmp = GET_MAP_OB (map, x, y); tmp; tmp = tmp->above) |
697 | if (QUERY_FLAG (tmp, FLAG_ALIVE)) |
684 | if (QUERY_FLAG (tmp, FLAG_ALIVE)) |
698 | { |
685 | { |
699 | if (tmp->head) |
686 | if (tmp->head) |
700 | tmp = tmp->head; |
687 | tmp = tmp->head; |
701 | tmp->remove (); |
688 | tmp->remove (); |
… | |
… | |
704 | if (tmp == NULL) |
691 | if (tmp == NULL) |
705 | break; |
692 | break; |
706 | }; |
693 | }; |
707 | } |
694 | } |
708 | |
695 | |
709 | |
|
|
710 | /* surrounds the point x,y by doors, so as to enclose something, like |
696 | /* surrounds the point x,y by doors, so as to enclose something, like |
711 | a chest. It only goes as far as the 8 squares surrounding, and |
697 | a chest. It only goes as far as the 8 squares surrounding, and |
712 | it'll remove any monsters it finds.*/ |
698 | it'll remove any monsters it finds.*/ |
713 | |
|
|
714 | object ** |
699 | object ** |
715 | surround_by_doors (maptile *map, char **layout, int x, int y, int opts) |
700 | surround_by_doors (maptile *map, char **layout, int x, int y, int opts) |
716 | { |
701 | { |
717 | int i; |
702 | int i; |
718 | char *doors[2]; |
703 | char *doors[2]; |
… | |
… | |
735 | /* place doors in all the 8 adjacent unblocked squares. */ |
720 | /* place doors in all the 8 adjacent unblocked squares. */ |
736 | for (i = 1; i < 9; i++) |
721 | for (i = 1; i < 9; i++) |
737 | { |
722 | { |
738 | int x1 = x + freearr_x[i], y1 = y + freearr_y[i]; |
723 | int x1 = x + freearr_x[i], y1 = y + freearr_y[i]; |
739 | |
724 | |
740 | if (!wall_blocked (map, x1, y1) || layout[x1][y1] == '>') |
725 | if (!wall_blocked (map, x1, y1) && layout[x1][y1] == '>') |
741 | { /* place a door */ |
726 | { /* place a door */ |
|
|
727 | remove_monsters (x1, y1, map); |
|
|
728 | |
742 | object *new_door = get_archetype ((freearr_x[i] == 0) ? doors[1] : doors[0]); |
729 | object *new_door = get_archetype (freearr_x[i] == 0 ? doors[1] : doors[0]); |
743 | |
730 | map->insert (new_door, x1, y1); |
744 | new_door->x = x + freearr_x[i]; |
|
|
745 | new_door->y = y + freearr_y[i]; |
|
|
746 | remove_monsters (new_door->x, new_door->y, map); |
|
|
747 | insert_ob_in_map (new_door, map, NULL, 0); |
|
|
748 | doorlist[ndoors_made] = new_door; |
731 | doorlist[ndoors_made] = new_door; |
749 | ndoors_made++; |
732 | ndoors_made++; |
750 | } |
733 | } |
751 | } |
734 | } |
|
|
735 | |
752 | return doorlist; |
736 | return doorlist; |
753 | } |
737 | } |
754 | |
738 | |
755 | |
739 | |
756 | /* returns the first door in this square, or NULL if there isn't a door. */ |
740 | /* returns the first door in this square, or NULL if there isn't a door. */ |
… | |
… | |
799 | } |
783 | } |
800 | } |
784 | } |
801 | else |
785 | else |
802 | { |
786 | { |
803 | layout[x][y] = 1; |
787 | layout[x][y] = 1; |
|
|
788 | |
804 | /* now search all the 8 squares around recursively for free spots,in random order */ |
789 | /* now search all the 8 squares around recursively for free spots,in random order */ |
805 | for (i = RANDOM () % 8, j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) |
790 | for (i = rndm (8), j = 0; j < 8 && !theMonsterToFind; i++, j++) |
806 | find_doors_in_room_recursive (layout, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], doorlist, ndoors, RP); |
791 | find_doors_in_room_recursive (layout, map, |
|
|
792 | x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], |
|
|
793 | doorlist, ndoors, RP); |
807 | } |
794 | } |
808 | } |
795 | } |
809 | |
796 | |
810 | /* find a random non-blocked spot in this room to drop a key. */ |
797 | /* find a random non-blocked spot in this room to drop a key. */ |
811 | object ** |
798 | object ** |
… | |
… | |
867 | new_door->y = door->y; |
854 | new_door->y = door->y; |
868 | door->remove (); |
855 | door->remove (); |
869 | door->destroy (); |
856 | door->destroy (); |
870 | doorlist[i] = new_door; |
857 | doorlist[i] = new_door; |
871 | insert_ob_in_map (new_door, map, NULL, 0); |
858 | insert_ob_in_map (new_door, map, NULL, 0); |
872 | sprintf (keybuf, "%d", (int) RANDOM ()); |
859 | sprintf (keybuf, "%d", rndm (1000000000)); |
873 | new_door->slaying = keybuf; |
860 | new_door->slaying = keybuf; |
874 | keyplace (map, new_door->x, new_door->y, keybuf, NO_PASS_DOORS, 2, RP); |
861 | keyplace (map, new_door->x, new_door->y, keybuf, NO_PASS_DOORS, 2, RP); |
875 | } |
862 | } |
876 | } |
863 | } |
877 | |
864 | |