Crossfire Server, Trunk  R20513
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 styledirname[256];
95  char stylefilepath[256];
96  mapstruct *style_map = NULL;
97  int num_treasures;
98 
99  /* bail out if treasure isn't wanted. */
100  if (treasure_style)
101  if (!strcmp(treasure_style, "none")) {
102  return;
103  }
104  if (treasureoptions <= 0) {
105  treasureoptions = RANDOM()%(2*LAST_OPTION);
106  }
107 
108  /* filter out the mutually exclusive options */
109  if ((treasureoptions&RICH) && (treasureoptions&SPARSE)) {
110  if (RANDOM()%2) {
111  treasureoptions -= 1;
112  } else {
113  treasureoptions -= 2;
114  }
115  }
116 
117  /* pick the number of treasures */
118  if (treasureoptions&SPARSE) {
119  num_treasures = BC_RANDOM(RP->total_map_hp/600+RP->difficulty/2+1);
120  } else if (treasureoptions&RICH) {
121  num_treasures = BC_RANDOM(RP->total_map_hp/150+2*RP->difficulty+1);
122  } else {
123  num_treasures = BC_RANDOM(RP->total_map_hp/300+RP->difficulty+1);
124  }
125 
126  if (num_treasures <= 0) {
127  return;
128  }
129 
130  /* get the style map */
131  snprintf(styledirname, sizeof(styledirname), "%s", "/styles/treasurestyles");
132  snprintf(stylefilepath, sizeof(stylefilepath), "%s/%s", styledirname, treasure_style);
133  style_map = find_style(styledirname, treasure_style, -1);
134 
135  /* all the treasure at one spot in the map. */
136  if (treasureoptions&CONCENTRATED) {
137  /* map_layout_style global, and is previously set */
138  switch (RP->map_layout_style) {
139  case ONION_LAYOUT:
140  case SPIRAL_LAYOUT:
141  case SQUARE_SPIRAL_LAYOUT: {
142  int i, j;
143 
144  /* search the onion for C's or '>', and put treasure there. */
145  for (i = 0; i < RP->Xsize; i++) {
146  for (j = 0; j < RP->Ysize; j++) {
147  if (layout[i][j] == 'C' || layout[i][j] == '>') {
148  int tdiv = RP->symmetry_used;
149  object **doorlist;
150  object *chest;
151 
152  if (tdiv == 3) {
153  tdiv = 2; /* this symmetry uses a divisor of 2*/
154  }
155  /* don't put a chest on an exit. */
156  chest = place_chest(treasureoptions, i, j, map, style_map, num_treasures/tdiv, RP);
157  if (!chest) {
158  continue; /* if no chest was placed NEXT */
159  }
160  if (treasureoptions&(DOORED|HIDDEN)) {
161  doorlist = find_doors_in_room(map, i, j, RP);
162  lock_and_hide_doors(doorlist, map, treasureoptions, RP);
163  free(doorlist);
164  }
165  }
166  }
167  }
168  break;
169  }
170  default: {
171  int i, j, tries;
172  object *chest;
173  object **doorlist;
174 
175  i = j = -1;
176  tries = 0;
177  while (i == -1 && tries < 100) {
178  i = RANDOM()%(RP->Xsize-2)+1;
179  j = RANDOM()%(RP->Ysize-2)+1;
180  find_enclosed_spot(map, &i, &j, RP);
181  if (wall_blocked(map, i, j)) {
182  i = -1;
183  }
184  tries++;
185  }
186  chest = place_chest(treasureoptions, i, j, map, style_map, num_treasures, RP);
187  if (!chest) {
188  return;
189  }
190  i = chest->x;
191  j = chest->y;
192  if (treasureoptions&(DOORED|HIDDEN)) {
193  doorlist = surround_by_doors(map, layout, i, j, treasureoptions);
194  lock_and_hide_doors(doorlist, map, treasureoptions, RP);
195  free(doorlist);
196  }
197  }
198  }
199  } else { /* DIFFUSE treasure layout */
200  int ti, i, j;
201 
202  for (ti = 0; ti < num_treasures; ti++) {
203  i = RANDOM()%(RP->Xsize-2)+1;
204  j = RANDOM()%(RP->Ysize-2)+1;
205  place_chest(treasureoptions, i, j, map, style_map, 1, RP);
206  }
207  }
208 }
209 
233 object *place_chest(int treasureoptions, int x, int y, mapstruct *map, mapstruct *style_map, int n_treasures, RMParms *RP)
234 {
235  object *the_chest;
236  int i, xl, yl;
237  treasurelist *tlist;
238  const char *chests[] = { "chest", "chest_green", "chest_red", "chest_yellow", "chest_blue", "chest_pink" };
239 
240  the_chest = create_archetype(chests[RANDOM() % (sizeof(chests)/sizeof(*chests))]);
241  if (the_chest == NULL) {
242  return NULL;
243  }
244 
245  /* first, find a place to put the chest. */
246  i = object_find_first_free_spot(the_chest, map, x, y);
247  if (i == -1) {
248  object_free_drop_inventory(the_chest);
249  return NULL;
250  }
251  xl = x+freearr_x[i];
252  yl = y+freearr_y[i];
253 
254  /* if the placement is blocked, return a fail. */
255  if (wall_blocked(map, xl, yl)) {
256  object_free_drop_inventory(the_chest);
257  return NULL;
258  }
259 
260  tlist = find_treasurelist("chest");
261  the_chest->randomitems = tlist;
262  the_chest->stats.hp = n_treasures;
263 
264  /* stick a trap in the chest if required */
265  if (treasureoptions&TRAPPED) {
266  mapstruct *trap_map = find_style("/styles/trapstyles", "traps", -1);
267  object *the_trap;
268 
269  if (trap_map) {
270  the_trap = pick_random_object(trap_map);
271  if (the_trap) {
272  object *new_trap;
273 
274  new_trap = object_new();
275  object_copy_with_inv(the_trap, new_trap);
276  new_trap->stats.Cha = 10+RP->difficulty;
277  new_trap->level = BC_RANDOM((3*RP->difficulty)/2);
278  if (new_trap->level == 0) {
279  new_trap->level = 1;
280  }
281  new_trap->x = x;
282  new_trap->y = y;
283  object_insert_in_ob(new_trap, the_chest);
284  }
285  }
286  }
287 
288  /* set the chest lock code, and call the keyplacer routine with
289  the lockcode. It's not worth bothering to lock the chest if
290  there's only 1 treasure....*/
291  if ((treasureoptions&KEYREQUIRED) && n_treasures > 1) {
292  char keybuf[256];
293 
294  snprintf(keybuf, sizeof(keybuf), "%d", (int)RANDOM());
295  if (keyplace(map, x, y, keybuf, PASS_DOORS, 1, RP)) {
296  the_chest->slaying = add_string(keybuf);
297  }
298  }
299 
300  /* actually place the chest. */
301  object_insert_in_map_at(the_chest, map, NULL, 0, xl, yl);
302  return the_chest;
303 }
304 
319 object *find_closest_monster(mapstruct *map, int x, int y, RMParms *RP)
320 {
321  int i;
322 
323  for (i = 0; i < SIZEOFFREE; i++) {
324  int lx, ly;
325 
326  lx = x+freearr_x[i];
327  ly = y+freearr_y[i];
328  /* boundscheck */
329  if (lx >= 0 && ly >= 0 && lx < RP->Xsize && ly < RP->Ysize)
330  /* don't bother searching this square unless the map says life exists.*/
331  if (GET_MAP_FLAGS(map, lx, ly)&P_IS_ALIVE) {
332  FOR_MAP_PREPARE(map, lx, ly, the_monster)
333  if (QUERY_FLAG(the_monster, FLAG_MONSTER)) {
334  return the_monster;
335  }
336  FOR_MAP_FINISH();
337  }
338  }
339  return NULL;
340 }
341 
369 int keyplace(mapstruct *map, int x, int y, char *keycode, int door_flag, int n_keys, RMParms *RP)
370 {
371  int i, j;
372  int kx, ky;
373  object *the_keymaster; /* the monster that gets the key. */
374  object *the_key;
375  char keybuf[256];
376  const char *keys[] = { "key2", "blue_key", "brown_key", "darkgray_key", "darkgreen_key", "gray_key", "green_key", "magenta_key", "red_key", "white_key" };
377 
378  /* get a key and set its keycode */
379  the_key = create_archetype(keys[RANDOM() % (sizeof(keys)/sizeof(*keys))]);
380  the_key->slaying = add_string(keycode);
381  free_string(the_key->name);
382  snprintf(keybuf, 256, "key from level %d of %s", RP->dungeon_level-1, RP->dungeon_name[0] != '\0' ? RP->dungeon_name : "a random map");
383  the_key->name = add_string(keybuf);
384 
385  if (door_flag == PASS_DOORS) {
386  int tries = 0;
387 
388  the_keymaster = NULL;
389  while (tries < 15 && the_keymaster == NULL) {
390  i = (RANDOM()%(RP->Xsize-2))+1;
391  j = (RANDOM()%(RP->Ysize-2))+1;
392  tries++;
393  the_keymaster = find_closest_monster(map, i, j, RP);
394  }
395  /* if we don't find a good keymaster, drop the key on the ground. */
396  if (the_keymaster == NULL) {
397  int freeindex;
398 
399  freeindex = -1;
400  for (tries = 0; tries < 15 && freeindex == -1; tries++) {
401  kx = (RANDOM()%(RP->Xsize-2))+1;
402  ky = (RANDOM()%(RP->Ysize-2))+1;
403  freeindex = object_find_first_free_spot(the_key, map, kx, ky);
404  }
405  if (freeindex != -1) {
406  kx += freearr_x[freeindex];
407  ky += freearr_y[freeindex];
408  }
409  }
410  } else { /* NO_PASS_DOORS --we have to work harder.*/
411  /* don't try to keyplace if we're sitting on a blocked square and
412  * NO_PASS_DOORS is set.
413  */
414  if (n_keys == 1) {
415  if (wall_blocked(map, x, y)) {
416  return 0;
417  }
418  the_keymaster = find_monster_in_room(map, x, y, RP);
419  if (the_keymaster == NULL) /* if fail, find a spot to drop the key. */
420  if (!find_spot_in_room(map, x, y, &kx, &ky, RP)) {
421  return 0;
422  }
423  } else {
424  /* It can happen that spots around that point are all blocked, so
425  * try to look farther away if needed
426  */
427  int sum = 0; /* count how many keys we actually place */
428  int distance = 1;
429 
430  while (distance < 5) {
431  /* I'm lazy, so just try to place in all 4 directions. */
432  sum += keyplace(map, x+distance, y, keycode, NO_PASS_DOORS, 1, RP);
433  sum += keyplace(map, x, y+distance, keycode, NO_PASS_DOORS, 1, RP);
434  sum += keyplace(map, x-distance, y, keycode, NO_PASS_DOORS, 1, RP);
435  sum += keyplace(map, x, y-distance, keycode, NO_PASS_DOORS, 1, RP);
436  if (sum < 2) { /* we might have made a disconnected map-place more keys. */
437  /* diagonally this time. */
438  keyplace(map, x+distance, y+distance, keycode, NO_PASS_DOORS, 1, RP);
439  keyplace(map, x+distance, y-distance, keycode, NO_PASS_DOORS, 1, RP);
440  keyplace(map, x-distance, y+distance, keycode, NO_PASS_DOORS, 1, RP);
441  keyplace(map, x-distance, y-distance, keycode, NO_PASS_DOORS, 1, RP);
442  }
443  if (sum > 0) {
444  return 1;
445  }
446  distance++;
447  }
448  return 0;
449  }
450  }
451 
452  if (the_keymaster == NULL) {
453  object_insert_in_map_at(the_key, map, NULL, 0, kx, ky);
454  return 1;
455  }
456 
457  object_insert_in_ob(the_key, the_keymaster);
458  return 1;
459 }
460 
476 object *find_monster_in_room_recursive(char **layout, mapstruct *map, int x, int y, RMParms *RP)
477 {
478  int i, j;
479 
480  /* bounds check x and y */
481  if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize)) {
482  return NULL;
483  }
484 
485  /* if the square is blocked or searched already, leave */
486  if (layout[x][y] != 0) {
487  return NULL;
488  }
489 
490  /* check the current square for a monster. If there is one,
491  set theMonsterToFind and return it. */
492  layout[x][y] = 1;
493  if (GET_MAP_FLAGS(map, x, y)&P_IS_ALIVE) {
494  FOR_MAP_PREPARE(map, x, y, the_monster)
495  if (QUERY_FLAG(the_monster, FLAG_ALIVE)) {
496  return HEAD(the_monster);
497  }
498  FOR_MAP_FINISH();
499  }
500 
501  /* now search all the 8 squares around recursively for a monster, in random order */
502  for (i = RANDOM()%8, j = 0; j < 8; i++, j++) {
503  object *the_monster;
504 
505  the_monster = find_monster_in_room_recursive(layout, map, x+freearr_x[i%8+1], y+freearr_y[i%8+1], RP);
506  if (the_monster != NULL) {
507  return the_monster;
508  }
509  }
510  return NULL;
511 }
512 
527 object *find_monster_in_room(mapstruct *map, int x, int y, RMParms *RP)
528 {
529  char **layout2;
530  int i, j;
531  object *theMonsterToFind;
532 
533  layout2 = (char **)calloc(sizeof(char *), RP->Xsize);
534  /* allocate and copy the layout, converting C to 0. */
535  for (i = 0; i < RP->Xsize; i++) {
536  layout2[i] = (char *)calloc(sizeof(char), RP->Ysize);
537  for (j = 0; j < RP->Ysize; j++) {
538  if (wall_blocked(map, i, j)) {
539  layout2[i][j] = '#';
540  }
541  }
542  }
543  theMonsterToFind = find_monster_in_room_recursive(layout2, map, x, y, RP);
544 
545  /* deallocate the temp. layout */
546  for (i = 0; i < RP->Xsize; i++) {
547  free(layout2[i]);
548  }
549  free(layout2);
550 
551  return theMonsterToFind;
552 }
553 
555 typedef struct free_spots_struct {
560 
575 static void find_spot_in_room_recursive(char **layout, int x, int y, RMParms *RP, free_spots_struct *spots)
576 {
577  int i, j;
578 
579  /* bounds check x and y */
580  if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize)) {
581  return;
582  }
583 
584  /* if the square is blocked or searched already, leave */
585  if (layout[x][y] != 0) {
586  return;
587  }
588 
589  /* set the current square as checked, and add it to the list.
590  check off this point */
591  layout[x][y] = 1;
595  /* now search all the 8 squares around recursively for free spots, in random order */
596  for (i = RANDOM()%8, j = 0; j < 8; i++, j++) {
597  find_spot_in_room_recursive(layout, x+freearr_x[i%8+1], y+freearr_y[i%8+1], RP, spots);
598  }
599 }
600 
619 int find_spot_in_room(mapstruct *map, int x, int y, int *kx, int *ky, RMParms *RP)
620 {
621  char **layout2;
622  int i, j;
623  free_spots_struct spots;
624 
626  spots.room_free_spots_x = (int *)calloc(sizeof(int), RP->Xsize*RP->Ysize);
627  spots.room_free_spots_y = (int *)calloc(sizeof(int), RP->Xsize*RP->Ysize);
628 
629  layout2 = (char **)calloc(sizeof(char *), RP->Xsize);
630  /* allocate and copy the layout, converting C to 0. */
631  for (i = 0; i < RP->Xsize; i++) {
632  layout2[i] = (char *)calloc(sizeof(char), RP->Ysize);
633  for (j = 0; j < RP->Ysize; j++) {
634  if (wall_blocked(map, i, j)) {
635  layout2[i][j] = '#';
636  }
637  }
638  }
639 
640  /* setup num_free_spots and room_free_spots */
641  find_spot_in_room_recursive(layout2, x, y, RP, &spots);
642 
643  if (spots.number_of_free_spots_in_room > 0) {
645  *kx = spots.room_free_spots_x[i];
646  *ky = spots.room_free_spots_y[i];
647  }
648 
649  /* deallocate the temp. layout */
650  for (i = 0; i < RP->Xsize; i++) {
651  free(layout2[i]);
652  }
653  free(layout2);
654  free(spots.room_free_spots_x);
655  free(spots.room_free_spots_y);
656 
657  if (spots.number_of_free_spots_in_room > 0) {
658  return 1;
659  }
660  return 0;
661 }
662 
675 void find_enclosed_spot(mapstruct *map, int *cx, int *cy, RMParms *RP)
676 {
677  int x, y;
678  int i;
679 
680  x = *cx;
681  y = *cy;
682 
683  for (i = 0; i <= SIZEOFFREE1; i++) {
684  int lx, ly, sindex;
685  lx = x+freearr_x[i];
686  ly = y+freearr_y[i];
687  sindex = surround_flag3(map, lx, ly, RP);
688  /* if it's blocked on 3 sides, it's enclosed */
689  if (sindex == 7 || sindex == 11 || sindex == 13 || sindex == 14) {
690  *cx = lx;
691  *cy = ly;
692  return;
693  }
694  }
695 
696  /* OK, if we got here, we're obviously someplace where there's no enclosed
697  spots--try to find someplace which is 2x enclosed. */
698  for (i = 0; i <= SIZEOFFREE1; i++) {
699  int lx, ly, sindex;
700 
701  lx = x+freearr_x[i];
702  ly = y+freearr_y[i];
703  sindex = surround_flag3(map, lx, ly, RP);
704  /* if it's blocked on 3 sides, it's enclosed */
705  if (sindex == 3 || sindex == 5 || sindex == 9 || sindex == 6 || sindex == 10 || sindex == 12) {
706  *cx = lx;
707  *cy = ly;
708  return;
709  }
710  }
711 
712  /* settle for one surround point */
713  for (i = 0; i <= SIZEOFFREE1; i++) {
714  int lx, ly, sindex;
715 
716  lx = x+freearr_x[i];
717  ly = y+freearr_y[i];
718  sindex = surround_flag3(map, lx, ly, RP);
719  /* if it's blocked on 3 sides, it's enclosed */
720  if (sindex) {
721  *cx = lx;
722  *cy = ly;
723  return;
724  }
725  }
726 
727  /* give up and return the closest free spot. */
728  i = object_find_first_free_spot(&find_archetype("chest")->clone, map, x, y);
729  if (i != -1 && i <= SIZEOFFREE1) {
730  *cx = x+freearr_x[i];
731  *cy = y+freearr_y[i];
732  return;
733  }
734  /* indicate failure */
735  *cx = *cy = -1;
736 }
737 
745 void remove_monsters(int x, int y, mapstruct *map)
746 {
747  FOR_MAP_PREPARE(map, x, y, tmp) {
748  if (QUERY_FLAG(tmp, FLAG_ALIVE)) {
749  tmp = HEAD(tmp);
750  object_remove(tmp);
752  tmp = GET_MAP_OB(map, x, y);
753  if (tmp == NULL) {
754  break;
755  }
756  }
757  }
758  FOR_MAP_FINISH();
759 }
760 
779 static object **surround_by_doors(mapstruct *map, char **layout, int x, int y, int opts)
780 {
781  int i;
782  const char *doors[2];
783  object **doorlist;
784  int ndoors_made = 0;
785  doorlist = (object **)calloc(9, sizeof(object *)); /* 9 doors so we can hold termination null */
786 
787  /* this is a list we pick from, for horizontal and vertical doors */
788  if (opts&DOORED) {
789  doors[0] = "locked_door2";
790  doors[1] = "locked_door1";
791  } else {
792  doors[0] = "door_1";
793  doors[1] = "door_2";
794  }
795 
796  /* place doors in all the 8 adjacent unblocked squares. */
797  for (i = 1; i < 9; i++) {
798  int x1 = x+freearr_x[i], y1 = y+freearr_y[i];
799 
800  if (!wall_blocked(map, x1, y1)
801  || layout[x1][y1] == '>') {/* place a door */
802  object *new_door = create_archetype((freearr_x[i] == 0) ? doors[1] : doors[0]);
803  int16_t nx, ny;
804 
805  nx = x+freearr_x[i];
806  ny = y+freearr_y[i];
807  remove_monsters(nx, ny, map);
808  object_insert_in_map_at(new_door, map, NULL, 0, nx, ny);
809  doorlist[ndoors_made] = new_door;
810  ndoors_made++;
811  }
812  }
813  return doorlist;
814 }
815 
827 static object *door_in_square(mapstruct *map, int x, int y)
828 {
829  FOR_MAP_PREPARE(map, x, y, tmp)
830  if (tmp->type == DOOR || tmp->type == LOCKED_DOOR) {
831  return tmp;
832  }
833  FOR_MAP_FINISH();
834  return NULL;
835 }
836 
851 void find_doors_in_room_recursive(char **layout, mapstruct *map, int x, int y, object **doorlist, int *ndoors, RMParms *RP)
852 {
853  int i, j;
854  object *door;
855 
856  /* bounds check x and y */
857  if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize)) {
858  return;
859  }
860 
861  /* if the square is blocked or searched already, leave */
862  if (layout[x][y] == 1) {
863  return;
864  }
865 
866  /* check off this point */
867  if (layout[x][y] == '#') { /* there could be a door here */
868  layout[x][y] = 1;
869  door = door_in_square(map, x, y);
870  if (door != NULL) {
871  doorlist[*ndoors] = door;
872  if (*ndoors > 254) { /* eek! out of memory */
873  LOG(llevError, "find_doors_in_room_recursive:Too many doors for memory allocated!\n");
874  return;
875  }
876  *ndoors = *ndoors+1;
877  }
878  } else {
879  layout[x][y] = 1;
880  /* now search all the 8 squares around recursively for free spots, in random order */
881  for (i = RANDOM()%8, j = 0; j < 8; i++, j++) {
882  find_doors_in_room_recursive(layout, map, x+freearr_x[i%8+1], y+freearr_y[i%8+1], doorlist, ndoors, RP);
883  }
884  }
885 }
886 
901 object **find_doors_in_room(mapstruct *map, int x, int y, RMParms *RP)
902 {
903  char **layout2;
904  object **doorlist;
905  int i, j;
906  int ndoors = 0;
907 
908  doorlist = (object **)calloc(sizeof(*doorlist), 256);
909 
910 
911  layout2 = (char **)calloc(sizeof(char *), RP->Xsize);
912  /* allocate and copy the layout, converting C to 0. */
913  for (i = 0; i < RP->Xsize; i++) {
914  layout2[i] = (char *)calloc(sizeof(char), RP->Ysize);
915  for (j = 0; j < RP->Ysize; j++) {
916  if (wall_blocked(map, i, j)) {
917  layout2[i][j] = '#';
918  }
919  }
920  }
921 
922  /* setup num_free_spots and room_free_spots */
923  find_doors_in_room_recursive(layout2, map, x, y, doorlist, &ndoors, RP);
924 
925  /* deallocate the temp. layout */
926  for (i = 0; i < RP->Xsize; i++) {
927  free(layout2[i]);
928  }
929  free(layout2);
930  return doorlist;
931 }
932 
942 static void remove_adjacent_doors(object *door)
943 {
944  mapstruct *m = door->map;
945  int x = door->x;
946  int y = door->y;
947  int i, flags;
948 
949  for (i = 1; i <= 8; i++) {
950  flags = get_map_flags(m, NULL, x+freearr_x[i], y+freearr_y[i], NULL, NULL);
951  if (flags&P_OUT_OF_MAP) {
952  continue;
953  }
954 
955  /* Old style doors are living objects. So if P_IS_ALIVE is not
956  * set, can not be a door on this space.
957  */
958  if (flags&P_IS_ALIVE) {
959  FOR_MAP_PREPARE(m, x+freearr_x[i], y+freearr_y[i], tmp) {
960  if (tmp->type == DOOR) {
961  object_remove(tmp);
963  break;
964  }
965  }
966  FOR_MAP_FINISH();
967  }
968  }
969 }
970 
986 void lock_and_hide_doors(object **doorlist, mapstruct *map, int opts, RMParms *RP)
987 {
988  object *door;
989  int i;
990 
991  /* lock the doors and hide the keys. */
992  if (opts&DOORED) {
993  for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++) {
994  object *new_door = create_archetype("locked_door1");
995  char keybuf[256];
996 
997  door = doorlist[i];
998  new_door->face = door->face;
999  object_remove(door);
1001  doorlist[i] = new_door;
1002  object_insert_in_map_at(new_door, map, NULL, 0, door->x, door->y);
1003 
1004  snprintf(keybuf, 256, "%d", (int)RANDOM());
1005  if (keyplace(map, new_door->x, new_door->y, keybuf, NO_PASS_DOORS, 2, RP)) {
1006  new_door->slaying = add_string(keybuf);
1007  }
1008  }
1009  for (i = 0; doorlist[i] != NULL; i++) {
1010  remove_adjacent_doors(doorlist[i]);
1011  }
1012  }
1013 
1014  /* change the faces of the doors and surrounding walls to hide them. */
1015  if (opts&HIDDEN) {
1016  for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++) {
1017  object *wallface;
1018 
1019  door = doorlist[i];
1020  wallface = retrofit_joined_wall(map, door->x, door->y, 1, RP);
1021  if (wallface != NULL) {
1022  retrofit_joined_wall(map, door->x-1, door->y, 0, RP);
1023  retrofit_joined_wall(map, door->x+1, door->y, 0, RP);
1024  retrofit_joined_wall(map, door->x, door->y-1, 0, RP);
1025  retrofit_joined_wall(map, door->x, door->y+1, 0, RP);
1026  door->face = wallface->face;
1027  if (!QUERY_FLAG(wallface, FLAG_REMOVED)) {
1028  object_remove(wallface);
1029  }
1030  object_free_drop_inventory(wallface);
1031  }
1032  }
1033  }
1034 }
Error, serious thing.
Definition: logger.h:11
static void find_spot_in_room_recursive(char **layout, int x, int y, RMParms *RP, free_spots_struct *spots)
the workhorse routine, which finds the free spots in a room: a datastructure of free points is set up...
Definition: treasure.c:575
Random map parameters.
Definition: random_map.h:14
archetype * find_archetype(const char *name)
Finds, using the hashtable, which archetype matches the given name.
Definition: arch.c:695
Datastructure needed by find_spot_in_room() and find_spot_in_room_recursive()
Definition: treasure.c:555
treasureliststruct represents one logical group of items to be generated together.
Definition: treasure.h:82
object ** find_doors_in_room(mapstruct *map, int x, int y, RMParms *RP)
Gets all doors in a room.
Definition: treasure.c:901
#define ONION_LAYOUT
Definition: random_map.h:105
void find_enclosed_spot(mapstruct *map, int *cx, int *cy, RMParms *RP)
Searches the map for a spot with walls around it.
Definition: treasure.c:675
void find_doors_in_room_recursive(char **layout, mapstruct *map, int x, int y, object **doorlist, int *ndoors, RMParms *RP)
The workhorse routine, which finds the doors in a room.
Definition: treasure.c:851
void remove_monsters(int x, int y, mapstruct *map)
Remove living things on specified spot.
Definition: treasure.c:745
void lock_and_hide_doors(object **doorlist, mapstruct *map, int opts, RMParms *RP)
Locks and/or hides all the doors in doorlist, or does nothing if opts doesn&#39;t say to lock/hide doors...
Definition: treasure.c:986
void object_copy_with_inv(const object *src_ob, object *dest_ob)
copy an object with an inventory...
Definition: object.c:975
void place_treasure(mapstruct *map, char **layout, char *treasure_style, int treasureoptions, RMParms *RP)
Place treasures in the map.
Definition: treasure.c:92
treasurelist * find_treasurelist(const char *name)
Searches for the given treasurelist in the globally linked list of treasurelists which has been built...
Definition: treasure.c:292
int number_of_free_spots_in_room
Number of positions.
Definition: treasure.c:558
void free_string(sstring str)
This will reduce the refcount, and if it has reached 0, str will be freed.
Definition: shstr.c:280
struct free_spots_struct free_spots_struct
Datastructure needed by find_spot_in_room() and find_spot_in_room_recursive()
int keyplace(mapstruct *map, int x, int y, char *keycode, int door_flag, int n_keys, RMParms *RP)
Places keys in the map, preferably in something alive.
Definition: treasure.c:369
struct treasureliststruct * randomitems
Items to be generated.
Definition: object.h:385
Random map related variables.
short freearr_x[SIZEOFFREE]
X offset when searching around a spot.
Definition: object.c:65
const char * slaying
Which race to do double damage to.
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)
Find a random non-blocked spot in this room to drop a key.
Definition: treasure.c:619
#define OUT_OF_REAL_MAP(M, X, Y)
Checks if a square is out of the map.
Definition: map.h:217
Global type definitions and header inclusions.
#define KEYREQUIRED
Definition: treasure.c:37
object * pick_random_object(mapstruct *style)
Picks a random object from a style map.
Definition: style.c:286
#define RICH
Definition: treasure.c:41
#define FLAG_REMOVED
Object is not in any map or invenory.
Definition: define.h:232
int16_t hp
Hit Points.
Definition: living.h:39
short freearr_y[SIZEOFFREE]
Y offset when searching around a spot.
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)
Frees everything allocated by an object, removes it from the list of used objects, and puts it on the list of free objects.
Definition: object.c:1368
int16_t y
Position in the map for this object.
Definition: object.h:326
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Same as object_insert_in_map() except it handle separate coordinates and do a clean job preparing mul...
Definition: object.c:1921
#define CONCENTRATED
Definition: treasure.c:35
#define FLAG_ALIVE
Object can fight (or be fought)
Definition: define.h:230
int map_layout_style
Definition: random_map.h:86
#define DOORED
Definition: treasure.c:38
object * object_new(void)
Grabs an object from the list of unused objects, makes sure it is initialised, and returns it...
Definition: object.c:1037
object * create_archetype(const char *name)
Finds which archetype matches the given name, and returns a new object containing a copy of the arche...
Definition: arch.c:620
object * object_insert_in_ob(object *op, object *where)
This function inserts the object op in the linked list inside the object environment.
Definition: object.c:2690
long unsigned int total_map_hp
Total hit points of the monsters in the map, used for treasure generation.
Definition: random_map.h:88
signed short int16_t
Definition: win32.h:160
int surround_flag3(mapstruct *map, int i, int j, RMParms *RP)
Check a map for blocked spots.
Definition: wall.c:114
object * find_closest_monster(mapstruct *map, int x, int y, RMParms *RP)
finds the closest monster and returns him, regardless of doors or walls
Definition: treasure.c:319
int * room_free_spots_x
Positions.
Definition: treasure.c:556
#define LAST_OPTION
Definition: treasure.c:43
struct mapdef * map
Pointer to the map in which this object is present.
Definition: object.h:297
#define snprintf
Definition: win32.h:46
const char * name
The name of the object, obviously...
Definition: object.h:311
char dungeon_name[RM_SIZE]
If not empty, will be used in the name of the random keys.
Definition: random_map.h:67
object * retrofit_joined_wall(mapstruct *the_map, int i, int j, int insert_flag, RMParms *RP)
this takes a map, and changes an existing wall to match what&#39;s blocked around it, counting only doors...
Definition: wall.c:315
static object ** surround_by_doors(mapstruct *map, char **layout, int x, int y, int opts)
Surrounds the point x,y by doors, so as to enclose something, like a chest.
Definition: treasure.c:779
object * find_monster_in_room(mapstruct *map, int x, int y, RMParms *RP)
Find a monster in a room.
Definition: treasure.c:527
int dungeon_level
Definition: random_map.h:79
int8_t Cha
Definition: living.h:35
object * place_chest(int treasureoptions, int x, int y, mapstruct *map, mapstruct *style_map, int n_treasures, RMParms *RP)
Put a chest into the map.
Definition: treasure.c:233
#define SIZEOFFREE
Definition: define.h:154
#define P_OUT_OF_MAP
This space is outside the map.
Definition: map.h:251
#define GET_MAP_MOVE_BLOCK(M, X, Y)
Gets the blocking state of a square.
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)
Returns the head part of an object.
Definition: object.h:594
static void remove_adjacent_doors(object *door)
This removes any &#39;normal&#39; doors around the specified door.
Definition: treasure.c:942
See Locked Door.
Definition: object.h:123
int symmetry_used
Definition: random_map.h:90
See Door.
Definition: object.h:126
static object * door_in_square(mapstruct *map, int x, int y)
Returns the first door in this square, or NULL if there isn&#39;t a door.
Definition: treasure.c:827
int16_t x
Definition: object.h:326
int Ysize
Definition: random_map.h:70
int wall_blocked(mapstruct *m, int x, int y)
Returns true if square x,y has P_NO_PASS set, which is true for walls and doors but not monsters...
Definition: treasure.c:62
#define MOVE_BLOCK_DEFAULT
The normal assumption is that objects are walking/flying.
Definition: define.h:424
int object_find_first_free_spot(const object *ob, mapstruct *m, int x, int y)
object_find_first_free_spot(archetype, mapstruct, x, y) works like object_find_free_spot(), but it will search max number of squares.
Definition: object.c:3458
static const flag_definition flags[]
Flag mapping.
#define FOR_MAP_FINISH()
Finishes FOR_MAP_PREPARE().
Definition: define.h:765
int Xsize
Definition: random_map.h:69
#define GET_MAP_FLAGS(M, X, Y)
Gets map flags.
Definition: map.h:161
#define PASS_DOORS
Definition: treasure.c:47
#define RANDOM()
Definition: define.h:679
#define TRAPPED
Definition: treasure.c:39
living stats
Str, Con, Dex, etc.
Definition: object.h:368
int * room_free_spots_y
Positions.
Definition: treasure.c:557
sstring add_string(const char *str)
This will add &#39;str&#39; to the hash table.
Definition: shstr.c:124
#define BC_RANDOM(x)
Macro to get a strongly centered random distribution, from 0 to x, centered at x/2.
Definition: random_map.h:151
#define GET_MAP_OB(M, X, Y)
Gets the bottom object on a map.
Definition: map.h:172
#define FLAG_MONSTER
Will attack players.
Definition: define.h:245
int get_map_flags(mapstruct *oldmap, mapstruct **newmap, int16_t x, int16_t y, int16_t *nx, int16_t *ny)
This rolls up wall, blocks_magic, blocks_view, etc, all into one function that just returns a P_...
Definition: map.c:302
#define SIZEOFFREE1
Definition: define.h:152
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
Definition: logger.c:51
int difficulty
Definition: random_map.h:75
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Constructs a loop iterating over all objects of a map tile.
Definition: define.h:758
mapstruct * find_style(const char *dirname, const char *stylename, int difficulty)
Loads and returns the map requested.
Definition: style.c:180
This is a game-map.
Definition: map.h:325
#define P_IS_ALIVE
Something alive is on this space.
Definition: map.h:237
const New_Face * face
Face with colors.
Definition: object.h:332
int16_t level
Level of creature or object.
Definition: object.h:351
void object_remove(object *op)
This function removes the object op from the linked list of objects which it is currently tied to...
Definition: object.c:1654
object * find_monster_in_room_recursive(char **layout, mapstruct *map, int x, int y, RMParms *RP)
A recursive routine which will return a monster, eventually, if there is one.
Definition: treasure.c:476
#define HIDDEN
Definition: treasure.c:36
Definition: main.c:88