Crossfire Server, Trunk  R21189
treasure.c
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2013 Mark Wedel and the Crossfire Development Team
5  * Copyright (c) 1992 Frank Tore Johansen
6  *
7  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8  * welcome to redistribute it under certain conditions. For details, please
9  * see COPYING and LICENSE.
10  *
11  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
12  */
13 
14 /* placing treasure in maps, where appropriate. */
15 
21 #include "global.h"
22 
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "random_map.h"
27 #include "rproto.h"
28 
35 #define CONCENTRATED 1 /* all the treasure is at the C's for onions. */
36 #define HIDDEN 2 /* doors to treasure are hidden. */
37 #define KEYREQUIRED 4 /* chest has a key, which is placed randomly in the map. */
38 #define DOORED 8 /* treasure has doors around it. */
39 #define TRAPPED 16 /* trap dropped in same location as chest. */
40 #define SPARSE 32 /* 1/2 as much treasure as default */
41 #define RICH 64 /* 2x as much treasure as default */
42 #define FILLED 128 /* Fill/tile the entire map with treasure */
43 #define LAST_OPTION 64 /* set this to the last real option, for random */
44 
46 #define NO_PASS_DOORS 0
47 #define PASS_DOORS 1
48 
49 static object **surround_by_doors(mapstruct *map, char **layout, int x, int y, int opts);
50 
62 int wall_blocked(mapstruct *m, int x, int y)
63 {
64  int r;
65 
66  if (OUT_OF_REAL_MAP(m, x, y)) {
67  return 1;
68  }
70  return r;
71 }
72 
92 void place_treasure(mapstruct *map, char **layout, char *treasure_style, int treasureoptions, RMParms *RP)
93 {
94  char stylefilepath[256];
95  mapstruct *style_map = NULL;
96  int num_treasures;
97 
98  /* bail out if treasure isn't wanted. */
99  if (treasure_style)
100  if (!strcmp(treasure_style, "none")) {
101  return;
102  }
103  if (treasureoptions <= 0) {
104  treasureoptions = RANDOM()%(2*LAST_OPTION);
105  }
106 
107  /* filter out the mutually exclusive options */
108  if ((treasureoptions&RICH) && (treasureoptions&SPARSE)) {
109  if (RANDOM()%2) {
110  treasureoptions -= 1;
111  } else {
112  treasureoptions -= 2;
113  }
114  }
115 
116  /* pick the number of treasures */
117  if (treasureoptions&SPARSE) {
118  num_treasures = BC_RANDOM(RP->total_map_hp/600+RP->difficulty/2+1);
119  } else if (treasureoptions&RICH) {
120  num_treasures = BC_RANDOM(RP->total_map_hp/150+2*RP->difficulty+1);
121  } else {
122  num_treasures = BC_RANDOM(RP->total_map_hp/300+RP->difficulty+1);
123  }
124 
125  if (num_treasures <= 0) {
126  return;
127  }
128 
129  /* get the style map */
130  const char *styledirname = "/styles/treasurestyles";
131  snprintf(stylefilepath, sizeof(stylefilepath), "%s/%s", styledirname, treasure_style);
132  style_map = find_style(styledirname, treasure_style, -1);
133 
134  /* all the treasure at one spot in the map. */
135  if (treasureoptions&CONCENTRATED) {
136  /* map_layout_style global, and is previously set */
137  switch (RP->map_layout_style) {
138  case ONION_LAYOUT:
139  case SPIRAL_LAYOUT:
140  case SQUARE_SPIRAL_LAYOUT: {
141  int i, j;
142 
143  /* search the onion for C's or '>', and put treasure there. */
144  for (i = 0; i < RP->Xsize; i++) {
145  for (j = 0; j < RP->Ysize; j++) {
146  if (layout[i][j] == 'C' || layout[i][j] == '>') {
147  int tdiv = RP->symmetry_used;
148  object **doorlist;
149  object *chest;
150 
151  if (tdiv == 3) {
152  tdiv = 2; /* this symmetry uses a divisor of 2*/
153  }
154  /* don't put a chest on an exit. */
155  chest = place_chest(treasureoptions, i, j, map, style_map, num_treasures/tdiv, RP);
156  if (!chest) {
157  continue; /* if no chest was placed NEXT */
158  }
159  if (treasureoptions&(DOORED|HIDDEN)) {
160  doorlist = find_doors_in_room(map, i, j, RP);
161  lock_and_hide_doors(doorlist, map, treasureoptions, RP);
162  free(doorlist);
163  }
164  }
165  }
166  }
167  break;
168  }
169  default: {
170  int i, j, tries;
171  object *chest;
172  object **doorlist;
173 
174  i = j = -1;
175  tries = 0;
176  while (i == -1 && tries < 100) {
177  i = RANDOM()%(RP->Xsize-2)+1;
178  j = RANDOM()%(RP->Ysize-2)+1;
179  find_enclosed_spot(map, &i, &j, RP);
180  if (wall_blocked(map, i, j)) {
181  i = -1;
182  }
183  tries++;
184  }
185  chest = place_chest(treasureoptions, i, j, map, style_map, num_treasures, RP);
186  if (!chest) {
187  return;
188  }
189  i = chest->x;
190  j = chest->y;
191  if (treasureoptions&(DOORED|HIDDEN)) {
192  doorlist = surround_by_doors(map, layout, i, j, treasureoptions);
193  lock_and_hide_doors(doorlist, map, treasureoptions, RP);
194  free(doorlist);
195  }
196  }
197  }
198  } else { /* DIFFUSE treasure layout */
199  int ti, i, j;
200 
201  for (ti = 0; ti < num_treasures; ti++) {
202  i = RANDOM()%(RP->Xsize-2)+1;
203  j = RANDOM()%(RP->Ysize-2)+1;
204  place_chest(treasureoptions, i, j, map, style_map, 1, RP);
205  }
206  }
207 }
208 
232 object *place_chest(int treasureoptions, int x, int y, mapstruct *map, mapstruct *style_map, int n_treasures, RMParms *RP)
233 {
234  object *the_chest;
235  int i, xl, yl;
236  treasurelist *tlist;
237  const char *chests[] = { "chest", "chest_green", "chest_red", "chest_yellow", "chest_blue", "chest_pink" };
238 
239  // If the difficulty is greater than 5, then there is a chance for there to be a mimic lurking.
240  // It is a slim chance at any rate, so it shouldn't be a problem in any case.
241  if (map->difficulty > 5 && RANDOM() % 1000000 < map->difficulty*map->difficulty*map->difficulty)
242  {
243  the_chest = create_archetype("mimic");
244  }
245  else
246  {
247  the_chest = create_archetype(chests[RANDOM() % (sizeof(chests)/sizeof(*chests))]);
248  }
249  if (the_chest == NULL) {
250  return NULL;
251  }
252 
253  /* first, find a place to put the chest. */
254  i = object_find_first_free_spot(the_chest, map, x, y);
255  if (i == -1) {
256  object_free_drop_inventory(the_chest);
257  return NULL;
258  }
259  xl = x+freearr_x[i];
260  yl = y+freearr_y[i];
261 
262  /* if the placement is blocked, return a fail. */
263  if (wall_blocked(map, xl, yl)) {
264  object_free_drop_inventory(the_chest);
265  return NULL;
266  }
267 
268  // If normal, then do the old behavior.
269  if (the_chest->type == TREASURE)
270  {
271  tlist = find_treasurelist("chest");
272  the_chest->randomitems = tlist;
273  the_chest->stats.hp = n_treasures;
274  }
275  // Otherwise, do the mimic treasurelist and don't change hp
276  else if (the_chest->type == MIMIC)
277  {
278  tlist = find_treasurelist("mimic");
279  the_chest->randomitems = tlist;
280  // TODO: Make mimics on higher levels have more treasure
281  }
282  /* stick a trap in the chest if required */
283  if (treasureoptions&TRAPPED) {
284  mapstruct *trap_map = find_style("/styles/trapstyles", "traps", -1);
285  object *the_trap;
286 
287  if (trap_map) {
288  the_trap = pick_random_object(trap_map);
289  if (the_trap) {
290  object *new_trap;
291 
292  new_trap = object_new();
293  object_copy_with_inv(the_trap, new_trap);
294  new_trap->stats.Cha = 10+RP->difficulty;
295  new_trap->level = BC_RANDOM((3*RP->difficulty)/2);
296  if (new_trap->level == 0) {
297  new_trap->level = 1;
298  }
299  new_trap->x = x;
300  new_trap->y = y;
301  object_insert_in_ob(new_trap, the_chest);
302  }
303  }
304  }
305 
306  /* set the chest lock code, and call the keyplacer routine with
307  the lockcode. It's not worth bothering to lock the chest if
308  there's only 1 treasure....*/
309  if ((treasureoptions&KEYREQUIRED) && n_treasures > 1) {
310  char keybuf[256];
311 
312  snprintf(keybuf, sizeof(keybuf), "%d", (int)RANDOM());
313  if (keyplace(map, x, y, keybuf, PASS_DOORS, 1, RP)) {
314  the_chest->slaying = add_string(keybuf);
315  }
316  }
317 
318  /* actually place the chest. */
319  object_insert_in_map_at(the_chest, map, NULL, 0, xl, yl);
320  return the_chest;
321 }
322 
337 object *find_closest_monster(mapstruct *map, int x, int y, RMParms *RP)
338 {
339  int i;
340 
341  for (i = 0; i < SIZEOFFREE; i++) {
342  int lx, ly;
343 
344  lx = x+freearr_x[i];
345  ly = y+freearr_y[i];
346  /* boundscheck */
347  if (lx >= 0 && ly >= 0 && lx < RP->Xsize && ly < RP->Ysize)
348  /* don't bother searching this square unless the map says life exists.*/
349  if (GET_MAP_FLAGS(map, lx, ly)&P_IS_ALIVE) {
350  FOR_MAP_PREPARE(map, lx, ly, the_monster)
351  if (QUERY_FLAG(the_monster, FLAG_MONSTER)) {
352  return the_monster;
353  }
354  FOR_MAP_FINISH();
355  }
356  }
357  return NULL;
358 }
359 
387 int keyplace(mapstruct *map, int x, int y, char *keycode, int door_flag, int n_keys, RMParms *RP)
388 {
389  int i, j;
390  int kx, ky;
391  object *the_keymaster; /* the monster that gets the key. */
392  object *the_key;
393  char keybuf[256];
394  const char *keys[] = { "key2", "blue_key", "brown_key", "darkgray_key", "darkgreen_key", "gray_key", "green_key", "magenta_key", "red_key", "white_key" };
395 
396  /* get a key and set its keycode */
397  the_key = create_archetype(keys[RANDOM() % (sizeof(keys)/sizeof(*keys))]);
398  the_key->slaying = add_string(keycode);
399  free_string(the_key->name);
400  snprintf(keybuf, 256, "key from level %d of %s", RP->dungeon_level-1, RP->dungeon_name[0] != '\0' ? RP->dungeon_name : "a random map");
401  the_key->name = add_string(keybuf);
402 
403  if (door_flag == PASS_DOORS) {
404  int tries = 0;
405 
406  the_keymaster = NULL;
407  while (tries < 15 && the_keymaster == NULL) {
408  i = (RANDOM()%(RP->Xsize-2))+1;
409  j = (RANDOM()%(RP->Ysize-2))+1;
410  tries++;
411  the_keymaster = find_closest_monster(map, i, j, RP);
412  }
413  /* if we don't find a good keymaster, drop the key on the ground. */
414  if (the_keymaster == NULL) {
415  int freeindex;
416 
417  freeindex = -1;
418  for (tries = 0; tries < 15 && freeindex == -1; tries++) {
419  kx = (RANDOM()%(RP->Xsize-2))+1;
420  ky = (RANDOM()%(RP->Ysize-2))+1;
421  freeindex = object_find_first_free_spot(the_key, map, kx, ky);
422  }
423  if (freeindex != -1) {
424  kx += freearr_x[freeindex];
425  ky += freearr_y[freeindex];
426  }
427  }
428  } else { /* NO_PASS_DOORS --we have to work harder.*/
429  /* don't try to keyplace if we're sitting on a blocked square and
430  * NO_PASS_DOORS is set.
431  */
432  if (n_keys == 1) {
433  if (wall_blocked(map, x, y)) {
434  return 0;
435  }
436  the_keymaster = find_monster_in_room(map, x, y, RP);
437  if (the_keymaster == NULL) /* if fail, find a spot to drop the key. */
438  if (!find_spot_in_room(map, x, y, &kx, &ky, RP)) {
439  return 0;
440  }
441  } else {
442  /* It can happen that spots around that point are all blocked, so
443  * try to look farther away if needed
444  */
445  int sum = 0; /* count how many keys we actually place */
446  int distance = 1;
447 
448  while (distance < 5) {
449  /* I'm lazy, so just try to place in all 4 directions. */
450  sum += keyplace(map, x+distance, y, keycode, NO_PASS_DOORS, 1, RP);
451  sum += keyplace(map, x, y+distance, keycode, NO_PASS_DOORS, 1, RP);
452  sum += keyplace(map, x-distance, y, keycode, NO_PASS_DOORS, 1, RP);
453  sum += keyplace(map, x, y-distance, keycode, NO_PASS_DOORS, 1, RP);
454  if (sum < 2) { /* we might have made a disconnected map-place more keys. */
455  /* diagonally this time. */
456  keyplace(map, x+distance, y+distance, keycode, NO_PASS_DOORS, 1, RP);
457  keyplace(map, x+distance, y-distance, keycode, NO_PASS_DOORS, 1, RP);
458  keyplace(map, x-distance, y+distance, keycode, NO_PASS_DOORS, 1, RP);
459  keyplace(map, x-distance, y-distance, keycode, NO_PASS_DOORS, 1, RP);
460  }
461  if (sum > 0) {
462  return 1;
463  }
464  distance++;
465  }
466  return 0;
467  }
468  }
469 
470  if (the_keymaster == NULL) {
471  object_insert_in_map_at(the_key, map, NULL, 0, kx, ky);
472  return 1;
473  }
474 
475  object_insert_in_ob(the_key, the_keymaster);
476  return 1;
477 }
478 
494 object *find_monster_in_room_recursive(char **layout, mapstruct *map, int x, int y, RMParms *RP)
495 {
496  int i, j;
497 
498  /* bounds check x and y */
499  if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize)) {
500  return NULL;
501  }
502 
503  /* if the square is blocked or searched already, leave */
504  if (layout[x][y] != 0) {
505  return NULL;
506  }
507 
508  /* check the current square for a monster. If there is one,
509  set theMonsterToFind and return it. */
510  layout[x][y] = 1;
511  if (GET_MAP_FLAGS(map, x, y)&P_IS_ALIVE) {
512  FOR_MAP_PREPARE(map, x, y, the_monster)
513  if (QUERY_FLAG(the_monster, FLAG_ALIVE)) {
514  return HEAD(the_monster);
515  }
516  FOR_MAP_FINISH();
517  }
518 
519  /* now search all the 8 squares around recursively for a monster, in random order */
520  for (i = RANDOM()%8, j = 0; j < 8; i++, j++) {
521  object *the_monster;
522 
523  the_monster = find_monster_in_room_recursive(layout, map, x+freearr_x[i%8+1], y+freearr_y[i%8+1], RP);
524  if (the_monster != NULL) {
525  return the_monster;
526  }
527  }
528  return NULL;
529 }
530 
545 object *find_monster_in_room(mapstruct *map, int x, int y, RMParms *RP)
546 {
547  char **layout2;
548  int i, j;
549  object *theMonsterToFind;
550 
551  layout2 = (char **)calloc(sizeof(char *), RP->Xsize);
552  /* allocate and copy the layout, converting C to 0. */
553  for (i = 0; i < RP->Xsize; i++) {
554  layout2[i] = (char *)calloc(sizeof(char), RP->Ysize);
555  for (j = 0; j < RP->Ysize; j++) {
556  if (wall_blocked(map, i, j)) {
557  layout2[i][j] = '#';
558  }
559  }
560  }
561  theMonsterToFind = find_monster_in_room_recursive(layout2, map, x, y, RP);
562 
563  /* deallocate the temp. layout */
564  for (i = 0; i < RP->Xsize; i++) {
565  free(layout2[i]);
566  }
567  free(layout2);
568 
569  return theMonsterToFind;
570 }
571 
573 typedef struct free_spots_struct {
578 
593 static void find_spot_in_room_recursive(char **layout, int x, int y, RMParms *RP, free_spots_struct *spots)
594 {
595  int i, j;
596 
597  /* bounds check x and y */
598  if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize)) {
599  return;
600  }
601 
602  /* if the square is blocked or searched already, leave */
603  if (layout[x][y] != 0) {
604  return;
605  }
606 
607  /* set the current square as checked, and add it to the list.
608  check off this point */
609  layout[x][y] = 1;
613  /* now search all the 8 squares around recursively for free spots, in random order */
614  for (i = RANDOM()%8, j = 0; j < 8; i++, j++) {
615  find_spot_in_room_recursive(layout, x+freearr_x[i%8+1], y+freearr_y[i%8+1], RP, spots);
616  }
617 }
618 
637 int find_spot_in_room(mapstruct *map, int x, int y, int *kx, int *ky, RMParms *RP)
638 {
639  char **layout2;
640  int i, j;
641  free_spots_struct spots;
642 
644  spots.room_free_spots_x = (int *)calloc(sizeof(int), RP->Xsize*RP->Ysize);
645  spots.room_free_spots_y = (int *)calloc(sizeof(int), RP->Xsize*RP->Ysize);
646 
647  layout2 = (char **)calloc(sizeof(char *), RP->Xsize);
648  /* allocate and copy the layout, converting C to 0. */
649  for (i = 0; i < RP->Xsize; i++) {
650  layout2[i] = (char *)calloc(sizeof(char), RP->Ysize);
651  for (j = 0; j < RP->Ysize; j++) {
652  if (wall_blocked(map, i, j)) {
653  layout2[i][j] = '#';
654  }
655  }
656  }
657 
658  /* setup num_free_spots and room_free_spots */
659  find_spot_in_room_recursive(layout2, x, y, RP, &spots);
660 
661  if (spots.number_of_free_spots_in_room > 0) {
663  *kx = spots.room_free_spots_x[i];
664  *ky = spots.room_free_spots_y[i];
665  }
666 
667  /* deallocate the temp. layout */
668  for (i = 0; i < RP->Xsize; i++) {
669  free(layout2[i]);
670  }
671  free(layout2);
672  free(spots.room_free_spots_x);
673  free(spots.room_free_spots_y);
674 
675  if (spots.number_of_free_spots_in_room > 0) {
676  return 1;
677  }
678  return 0;
679 }
680 
693 void find_enclosed_spot(mapstruct *map, int *cx, int *cy, RMParms *RP)
694 {
695  int x, y;
696  int i;
697 
698  x = *cx;
699  y = *cy;
700 
701  for (i = 0; i <= SIZEOFFREE1; i++) {
702  int lx, ly, sindex;
703  lx = x+freearr_x[i];
704  ly = y+freearr_y[i];
705  sindex = surround_flag3(map, lx, ly, RP);
706  /* if it's blocked on 3 sides, it's enclosed */
707  if (sindex == 7 || sindex == 11 || sindex == 13 || sindex == 14) {
708  *cx = lx;
709  *cy = ly;
710  return;
711  }
712  }
713 
714  /* OK, if we got here, we're obviously someplace where there's no enclosed
715  spots--try to find someplace which is 2x enclosed. */
716  for (i = 0; i <= SIZEOFFREE1; i++) {
717  int lx, ly, sindex;
718 
719  lx = x+freearr_x[i];
720  ly = y+freearr_y[i];
721  sindex = surround_flag3(map, lx, ly, RP);
722  /* if it's blocked on 3 sides, it's enclosed */
723  if (sindex == 3 || sindex == 5 || sindex == 9 || sindex == 6 || sindex == 10 || sindex == 12) {
724  *cx = lx;
725  *cy = ly;
726  return;
727  }
728  }
729 
730  /* settle for one surround point */
731  for (i = 0; i <= SIZEOFFREE1; i++) {
732  int lx, ly, sindex;
733 
734  lx = x+freearr_x[i];
735  ly = y+freearr_y[i];
736  sindex = surround_flag3(map, lx, ly, RP);
737  /* if it's blocked on 3 sides, it's enclosed */
738  if (sindex) {
739  *cx = lx;
740  *cy = ly;
741  return;
742  }
743  }
744 
745  /* give up and return the closest free spot. */
746  archetype *arch = find_archetype("chest");
747  if (arch != NULL) {
748  i = object_find_first_free_spot(&arch->clone, map, x, y);
749  if (i != -1 && i <= SIZEOFFREE1) {
750  *cx = x+freearr_x[i];
751  *cy = y+freearr_y[i];
752  return;
753  }
754  }
755  /* indicate failure */
756  *cx = *cy = -1;
757 }
758 
766 void remove_monsters(int x, int y, mapstruct *map)
767 {
768  FOR_MAP_PREPARE(map, x, y, tmp) {
769  if (QUERY_FLAG(tmp, FLAG_ALIVE)) {
770  tmp = HEAD(tmp);
771  object_remove(tmp);
773  tmp = GET_MAP_OB(map, x, y);
774  if (tmp == NULL) {
775  break;
776  }
777  }
778  }
779  FOR_MAP_FINISH();
780 }
781 
800 static object **surround_by_doors(mapstruct *map, char **layout, int x, int y, int opts)
801 {
802  int i;
803  const char *doors[2];
804  object **doorlist;
805  int ndoors_made = 0;
806  doorlist = (object **)calloc(9, sizeof(object *)); /* 9 doors so we can hold termination null */
807 
808  /* this is a list we pick from, for horizontal and vertical doors */
809  if (opts&DOORED) {
810  doors[0] = "locked_door2";
811  doors[1] = "locked_door1";
812  } else {
813  doors[0] = "door_1";
814  doors[1] = "door_2";
815  }
816 
817  /* place doors in all the 8 adjacent unblocked squares. */
818  for (i = 1; i < 9; i++) {
819  int x1 = x+freearr_x[i], y1 = y+freearr_y[i];
820 
821  if (!wall_blocked(map, x1, y1)
822  || layout[x1][y1] == '>') {/* place a door */
823  object *new_door = create_archetype((freearr_x[i] == 0) ? doors[1] : doors[0]);
824  int16_t nx, ny;
825 
826  nx = x+freearr_x[i];
827  ny = y+freearr_y[i];
828  remove_monsters(nx, ny, map);
829  object_insert_in_map_at(new_door, map, NULL, 0, nx, ny);
830  doorlist[ndoors_made] = new_door;
831  ndoors_made++;
832  }
833  }
834  return doorlist;
835 }
836 
848 static object *door_in_square(mapstruct *map, int x, int y)
849 {
850  FOR_MAP_PREPARE(map, x, y, tmp)
851  if (tmp->type == DOOR || tmp->type == LOCKED_DOOR) {
852  return tmp;
853  }
854  FOR_MAP_FINISH();
855  return NULL;
856 }
857 
872 void find_doors_in_room_recursive(char **layout, mapstruct *map, int x, int y, object **doorlist, int *ndoors, RMParms *RP)
873 {
874  int i, j;
875  object *door;
876 
877  /* bounds check x and y */
878  if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize)) {
879  return;
880  }
881 
882  /* if the square is blocked or searched already, leave */
883  if (layout[x][y] == 1) {
884  return;
885  }
886 
887  /* check off this point */
888  if (layout[x][y] == '#') { /* there could be a door here */
889  layout[x][y] = 1;
890  door = door_in_square(map, x, y);
891  if (door != NULL) {
892  doorlist[*ndoors] = door;
893  if (*ndoors > 254) { /* eek! out of memory */
894  LOG(llevError, "find_doors_in_room_recursive:Too many doors for memory allocated!\n");
895  return;
896  }
897  *ndoors = *ndoors+1;
898  }
899  } else {
900  layout[x][y] = 1;
901  /* now search all the 8 squares around recursively for free spots, in random order */
902  for (i = RANDOM()%8, j = 0; j < 8; i++, j++) {
903  find_doors_in_room_recursive(layout, map, x+freearr_x[i%8+1], y+freearr_y[i%8+1], doorlist, ndoors, RP);
904  }
905  }
906 }
907 
922 object **find_doors_in_room(mapstruct *map, int x, int y, RMParms *RP)
923 {
924  char **layout2;
925  object **doorlist;
926  int i, j;
927  int ndoors = 0;
928 
929  doorlist = (object **)calloc(sizeof(*doorlist), 256);
930 
931 
932  layout2 = (char **)calloc(sizeof(char *), RP->Xsize);
933  /* allocate and copy the layout, converting C to 0. */
934  for (i = 0; i < RP->Xsize; i++) {
935  layout2[i] = (char *)calloc(sizeof(char), RP->Ysize);
936  for (j = 0; j < RP->Ysize; j++) {
937  if (wall_blocked(map, i, j)) {
938  layout2[i][j] = '#';
939  }
940  }
941  }
942 
943  /* setup num_free_spots and room_free_spots */
944  find_doors_in_room_recursive(layout2, map, x, y, doorlist, &ndoors, RP);
945 
946  /* deallocate the temp. layout */
947  for (i = 0; i < RP->Xsize; i++) {
948  free(layout2[i]);
949  }
950  free(layout2);
951  return doorlist;
952 }
953 
963 static void remove_adjacent_doors(object *door)
964 {
965  mapstruct *m = door->map;
966  int x = door->x;
967  int y = door->y;
968  int i, flags;
969 
970  for (i = 1; i <= 8; i++) {
971  flags = get_map_flags(m, NULL, x+freearr_x[i], y+freearr_y[i], NULL, NULL);
972  if (flags&P_OUT_OF_MAP) {
973  continue;
974  }
975 
976  /* Old style doors are living objects. So if P_IS_ALIVE is not
977  * set, can not be a door on this space.
978  */
979  if (flags&P_IS_ALIVE) {
980  FOR_MAP_PREPARE(m, x+freearr_x[i], y+freearr_y[i], tmp) {
981  if (tmp->type == DOOR) {
982  object_remove(tmp);
984  break;
985  }
986  }
987  FOR_MAP_FINISH();
988  }
989  }
990 }
991 
1007 void lock_and_hide_doors(object **doorlist, mapstruct *map, int opts, RMParms *RP)
1008 {
1009  object *door;
1010  int i;
1011 
1012  /* lock the doors and hide the keys. */
1013  if (opts&DOORED) {
1014  for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++) {
1015  object *new_door = create_archetype("locked_door1");
1016  char keybuf[256];
1017 
1018  door = doorlist[i];
1019  new_door->face = door->face;
1020  object_remove(door);
1022  doorlist[i] = new_door;
1023  object_insert_in_map_at(new_door, map, NULL, 0, door->x, door->y);
1024 
1025  snprintf(keybuf, 256, "%d", (int)RANDOM());
1026  if (keyplace(map, new_door->x, new_door->y, keybuf, NO_PASS_DOORS, 2, RP)) {
1027  new_door->slaying = add_string(keybuf);
1028  }
1029  }
1030  for (i = 0; doorlist[i] != NULL; i++) {
1031  remove_adjacent_doors(doorlist[i]);
1032  }
1033  }
1034 
1035  /* change the faces of the doors and surrounding walls to hide them. */
1036  if (opts&HIDDEN) {
1037  for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++) {
1038  object *wallface;
1039 
1040  door = doorlist[i];
1041  wallface = retrofit_joined_wall(map, door->x, door->y, 1, RP);
1042  if (wallface != NULL) {
1043  retrofit_joined_wall(map, door->x-1, door->y, 0, RP);
1044  retrofit_joined_wall(map, door->x+1, door->y, 0, RP);
1045  retrofit_joined_wall(map, door->x, door->y-1, 0, RP);
1046  retrofit_joined_wall(map, door->x, door->y+1, 0, RP);
1047  door->face = wallface->face;
1048  if (!QUERY_FLAG(wallface, FLAG_REMOVED)) {
1049  object_remove(wallface);
1050  }
1051  object_free_drop_inventory(wallface);
1052  }
1053  }
1054  }
1055 }
static void find_spot_in_room_recursive(char **layout, int x, int y, RMParms *RP, free_spots_struct *spots)
Definition: treasure.c:593
archetype * find_archetype(const char *name)
Definition: arch.c:695
object ** find_doors_in_room(mapstruct *map, int x, int y, RMParms *RP)
Definition: treasure.c:922
#define ONION_LAYOUT
Definition: random_map.h:105
void find_enclosed_spot(mapstruct *map, int *cx, int *cy, RMParms *RP)
Definition: treasure.c:693
void find_doors_in_room_recursive(char **layout, mapstruct *map, int x, int y, object **doorlist, int *ndoors, RMParms *RP)
Definition: treasure.c:872
void remove_monsters(int x, int y, mapstruct *map)
Definition: treasure.c:766
void lock_and_hide_doors(object **doorlist, mapstruct *map, int opts, RMParms *RP)
Definition: treasure.c:1007
void object_copy_with_inv(const object *src_ob, object *dest_ob)
Definition: object.c:1006
void place_treasure(mapstruct *map, char **layout, char *treasure_style, int treasureoptions, RMParms *RP)
Definition: treasure.c:92
treasurelist * find_treasurelist(const char *name)
Definition: treasure.c:290
int number_of_free_spots_in_room
Definition: treasure.c:576
void free_string(sstring str)
Definition: shstr.c:280
struct free_spots_struct free_spots_struct
int keyplace(mapstruct *map, int x, int y, char *keycode, int door_flag, int n_keys, RMParms *RP)
Definition: treasure.c:387
struct treasureliststruct * randomitems
Definition: object.h:385
object clone
Definition: object.h:470
short freearr_x[SIZEOFFREE]
Definition: object.c:65
const char * slaying
Definition: object.h:319
#define NO_PASS_DOORS
Definition: treasure.c:46
int find_spot_in_room(mapstruct *map, int x, int y, int *kx, int *ky, RMParms *RP)
Definition: treasure.c:637
#define OUT_OF_REAL_MAP(M, X, Y)
Definition: map.h:217
Definition: object.h:249
#define KEYREQUIRED
Definition: treasure.c:37
object * pick_random_object(mapstruct *style)
Definition: style.c:289
#define RICH
Definition: treasure.c:41
Definition: object.h:465
#define FLAG_REMOVED
Definition: define.h:232
int16_t hp
Definition: living.h:40
short freearr_y[SIZEOFFREE]
Definition: object.c:71
#define SPARSE
Definition: treasure.c:40
#define SPIRAL_LAYOUT
Definition: random_map.h:107
void object_free_drop_inventory(object *ob)
Definition: object.c:1389
int16_t y
Definition: object.h:326
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Definition: object.c:1936
#define CONCENTRATED
Definition: treasure.c:35
#define FLAG_ALIVE
Definition: define.h:230
int map_layout_style
Definition: random_map.h:86
#define DOORED
Definition: treasure.c:38
object * object_new(void)
Definition: object.c:1068
object * create_archetype(const char *name)
Definition: arch.c:620
object * object_insert_in_ob(object *op, object *where)
Definition: object.c:2710
long unsigned int total_map_hp
Definition: random_map.h:88
signed short int16_t
Definition: win32.h:160
int surround_flag3(mapstruct *map, int i, int j, RMParms *RP)
Definition: wall.c:114
object * find_closest_monster(mapstruct *map, int x, int y, RMParms *RP)
Definition: treasure.c:337
int * room_free_spots_x
Definition: treasure.c:574
#define LAST_OPTION
Definition: treasure.c:43
struct mapdef * map
Definition: object.h:297
#define snprintf
Definition: win32.h:46
const char * name
Definition: object.h:311
char dungeon_name[RM_SIZE]
Definition: random_map.h:67
object * retrofit_joined_wall(mapstruct *the_map, int i, int j, int insert_flag, RMParms *RP)
Definition: wall.c:314
static object ** surround_by_doors(mapstruct *map, char **layout, int x, int y, int opts)
Definition: treasure.c:800
object * find_monster_in_room(mapstruct *map, int x, int y, RMParms *RP)
Definition: treasure.c:545
int dungeon_level
Definition: random_map.h:79
int8_t Cha
Definition: living.h:36
object * place_chest(int treasureoptions, int x, int y, mapstruct *map, mapstruct *style_map, int n_treasures, RMParms *RP)
Definition: treasure.c:232
#define SIZEOFFREE
Definition: define.h:154
#define P_OUT_OF_MAP
Definition: map.h:251
#define GET_MAP_MOVE_BLOCK(M, X, Y)
Definition: map.h:192
#define SQUARE_SPIRAL_LAYOUT
Definition: random_map.h:110
#define QUERY_FLAG(xyz, p)
Definition: define.h:225
#define HEAD(op)
Definition: object.h:592
static void remove_adjacent_doors(object *door)
Definition: treasure.c:963
int symmetry_used
Definition: random_map.h:90
Definition: object.h:126
static object * door_in_square(mapstruct *map, int x, int y)
Definition: treasure.c:848
int16_t x
Definition: object.h:326
int Ysize
Definition: random_map.h:70
int wall_blocked(mapstruct *m, int x, int y)
Definition: treasure.c:62
#define MOVE_BLOCK_DEFAULT
Definition: define.h:424
int object_find_first_free_spot(const object *ob, mapstruct *m, int x, int y)
Definition: object.c:3478
uint16_t difficulty
Definition: map.h:343
static const flag_definition flags[]
#define FOR_MAP_FINISH()
Definition: define.h:767
int Xsize
Definition: random_map.h:69
#define GET_MAP_FLAGS(M, X, Y)
Definition: map.h:161
#define PASS_DOORS
Definition: treasure.c:47
#define RANDOM()
Definition: define.h:681
#define TRAPPED
Definition: treasure.c:39
living stats
Definition: object.h:368
uint8_t type
Definition: object.h:338
int * room_free_spots_y
Definition: treasure.c:575
sstring add_string(const char *str)
Definition: shstr.c:124
#define BC_RANDOM(x)
Definition: random_map.h:151
#define GET_MAP_OB(M, X, Y)
Definition: map.h:172
#define FLAG_MONSTER
Definition: define.h:245
int get_map_flags(mapstruct *oldmap, mapstruct **newmap, int16_t x, int16_t y, int16_t *nx, int16_t *ny)
Definition: map.c:310
#define SIZEOFFREE1
Definition: define.h:152
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
int difficulty
Definition: random_map.h:75
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Definition: define.h:760
mapstruct * find_style(const char *dirname, const char *stylename, int difficulty)
Definition: style.c:180
Definition: map.h:325
#define P_IS_ALIVE
Definition: map.h:237
const New_Face * face
Definition: object.h:332
int16_t level
Definition: object.h:351
void object_remove(object *op)
Definition: object.c:1669
object * find_monster_in_room_recursive(char **layout, mapstruct *map, int x, int y, RMParms *RP)
Definition: treasure.c:494
#define HIDDEN
Definition: treasure.c:36
Definition: main.c:88