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