Crossfire Server, Trunk  R21041
map.c
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2014 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 
19 #include "global.h"
20 
21 #include <ctype.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 
28 #ifndef WIN32 /* ---win32 exclude header */
29 #include <unistd.h>
30 #endif /* win32 */
31 
32 #include "sproto.h"
33 #include "loader.h"
34 #include "output_file.h"
35 #include "path.h"
36 
37 static void free_all_objects(mapstruct *m);
38 
44 const char *const map_layer_name[MAP_LAYERS] = {
45  "floor", "no_pick", "no_pick", "item", "item",
46  "item", "living", "living", "fly", "fly"
47 };
48 
50 typedef struct Map_Layer_Info {
54 
62  { MAP_LAYER_FLOOR, 1 },
64  { MAP_LAYER_ITEM3, 1 }, { MAP_LAYER_ITEM3, 1 }, { MAP_LAYER_ITEM3, 1 },
65  { MAP_LAYER_LIVING2, 1 }, { MAP_LAYER_LIVING2, 1 },
66  { MAP_LAYER_FLY2, 1 }, { MAP_LAYER_FLY2, 1 }
67 };
68 
77 mapstruct *has_been_loaded(const char *name) {
78  mapstruct *map;
79 
80  if (!name || !*name)
81  return NULL;
82 
83  for (map = first_map; map; map = map->next)
84  if (!strcmp(name, map->path))
85  break;
86  return (map);
87 }
88 
102 char *create_pathname(const char *name, char *buf, size_t size) {
103  /* Why? having extra / doesn't confuse unix anyplace? Dependancies
104  * someplace else in the code? msw 2-17-97
105  */
106  if (*name == '/')
107  snprintf(buf, size, "%s/%s%s", settings.datadir, settings.mapdir, name);
108  else
109  snprintf(buf, size, "%s/%s/%s", settings.datadir, settings.mapdir, name);
110  return buf;
111 }
112 
123 void create_overlay_pathname(const char *name, char *buf, size_t size) {
124  /* Why? having extra / doesn't confuse unix anyplace? Dependancies
125  * someplace else in the code? msw 2-17-97
126  */
127  if (*name == '/')
128  snprintf(buf, size, "%s/%s%s", settings.localdir, settings.mapdir, name);
129  else
130  snprintf(buf, size, "%s/%s/%s", settings.localdir, settings.mapdir, name);
131 }
132 
143 void create_template_pathname(const char *name, char *buf, size_t size) {
144  /* Why? having extra / doesn't confuse unix anyplace? Dependancies
145  * someplace else in the code? msw 2-17-97
146  */
147  if (*name == '/')
148  snprintf(buf, size, "%s/%s%s", settings.localdir, settings.templatedir, name);
149  else
150  snprintf(buf, size, "%s/%s/%s", settings.localdir, settings.templatedir, name);
151 }
152 
166 static void create_items_path(const char *s, char *buf, size_t size) {
167  char *t;
168 
169  if (*s == '/')
170  s++;
171 
172  snprintf(buf, size, "%s/%s/", settings.localdir, settings.uniquedir);
173  t = buf+strlen(buf);
174  snprintf(t, buf+size-t, "%s", s);
175 
176  while (*t != '\0') {
177  if (*t == '/')
178  *t = '@';
179  t++;
180  }
181 }
182 
201 int check_path(const char *name, int prepend_dir) {
202  char buf[MAX_BUF];
203 #ifndef WIN32
204  struct stat statbuf;
205  int mode = 0;
206 #endif
207 
208  if (prepend_dir)
209  create_pathname(name, buf, MAX_BUF);
210  else
211  snprintf(buf, sizeof(buf), "%s", name);
212 #ifdef WIN32 /* ***win32: check this sucker in windows style. */
213  return(_access(buf, 0));
214 #else
215 
216  if (stat(buf, &statbuf) != 0)
217  return -1;
218 
219  if (!S_ISREG(statbuf.st_mode))
220  return (-1);
221 
222  if (((statbuf.st_mode&S_IRGRP) && getegid() == statbuf.st_gid)
223  || ((statbuf.st_mode&S_IRUSR) && geteuid() == statbuf.st_uid)
224  || (statbuf.st_mode&S_IROTH))
225  mode |= 4;
226 
227  if ((statbuf.st_mode&S_IWGRP && getegid() == statbuf.st_gid)
228  || (statbuf.st_mode&S_IWUSR && geteuid() == statbuf.st_uid)
229  || (statbuf.st_mode&S_IWOTH))
230  mode |= 2;
231 
232  return (mode);
233 #endif
234 }
235 
245 void dump_map(const mapstruct *m) {
246  LOG(llevError, "Map %s status: %d.\n", m->path, m->in_memory);
247  LOG(llevError, "Size: %dx%d Start: %d,%d\n", MAP_WIDTH(m), MAP_HEIGHT(m), MAP_ENTER_X(m), MAP_ENTER_Y(m));
248 
249  if (m->msg != NULL)
250  LOG(llevError, "Message:\n%s", m->msg);
251 
252  if (m->maplore != NULL)
253  LOG(llevError, "Lore:\n%s", m->maplore);
254 
255  if (m->tmpname != NULL)
256  LOG(llevError, "Tmpname: %s\n", m->tmpname);
257 
258  LOG(llevError, "Difficulty: %d\n", m->difficulty);
259  LOG(llevError, "Darkness: %d\n", m->darkness);
260 }
261 
268 void dump_all_maps(void) {
269  mapstruct *m;
270 
271  for (m = first_map; m != NULL; m = m->next) {
272  dump_map(m);
273  }
274 }
275 
300 int get_map_flags(mapstruct *oldmap, mapstruct **newmap, int16_t x, int16_t y, int16_t *nx, int16_t *ny) {
301  int retval = 0;
302  mapstruct *mp;
303 
304  /*
305  * Since x and y are copies of the original values, we can directly
306  * mess with them here.
307  */
308  mp = get_map_from_coord(oldmap, &x, &y);
309  if (!mp)
310  return P_OUT_OF_MAP;
311  if (mp != oldmap)
312  retval |= P_NEW_MAP;
313  if (newmap)
314  *newmap = mp;
315  if (nx)
316  *nx = x;
317  if (ny)
318  *ny = y;
319  retval |= mp->spaces[x+mp->width*y].flags;
320  return retval;
321 }
322 
344 int blocked_link(object *ob, mapstruct *m, int sx, int sy) {
345  object *tmp_head;
346  int mflags, blocked;
347 
348  /* Make sure the coordinates are valid - they should be, as caller should
349  * have already checked this.
350  */
351  if (OUT_OF_REAL_MAP(m, sx, sy)) {
352  LOG(llevError, "blocked_link: Passed map, x, y coordinates outside of map\n");
353  return 1;
354  }
355 
356  /* special hack for transports: if it's a transport with a move_type of 0, it can do on the space anyway */
357  if (ob->type == TRANSPORT && ob->move_type == 0)
358  return 0;
359 
360  /* Save some cycles - instead of calling get_map_flags(), just get the value
361  * directly.
362  */
363  mflags = m->spaces[sx+m->width*sy].flags;
364 
365  blocked = GET_MAP_MOVE_BLOCK(m, sx, sy);
366 
367  /* If space is currently not blocked by anything, no need to
368  * go further. Not true for players - all sorts of special
369  * things we need to do for players.
370  */
371  if (ob->type != PLAYER && !(mflags&P_IS_ALIVE) && (blocked == 0))
372  return 0;
373 
374  /* if there isn't anytyhing alive on this space, and this space isn't
375  * otherwise blocked, we can return now. Only if there is a living
376  * creature do we need to investigate if it is part of this creature
377  * or another. Likewise, only if something is blocking us do we
378  * need to investigate if there is a special circumstance that would
379  * let the player through (inventory checkers for example)
380  */
381  if (!(mflags&P_IS_ALIVE) && !OB_TYPE_MOVE_BLOCK(ob, blocked))
382  return 0;
383 
384  ob = HEAD(ob);
385 
386  /* We basically go through the stack of objects, and if there is
387  * some other object that has NO_PASS or FLAG_ALIVE set, return
388  * true. If we get through the entire stack, that must mean
389  * ob is blocking it, so return 0.
390  */
391  FOR_MAP_PREPARE(m, sx, sy, tmp) {
392  /* Never block part of self. */
393  tmp_head = HEAD(tmp);
394  if (tmp_head == ob)
395  continue;
396  /* This must be before the checks below. Code for inventory checkers. */
397  if (tmp->type == CHECK_INV && OB_MOVE_BLOCK(ob, tmp)) {
398  /* If last_sp is set, the player/monster needs an object,
399  * so we check for it. If they don't have it, they can't
400  * pass through this space.
401  */
402  if (tmp->last_sp) {
403  if (check_inv_recursive(ob, tmp) == NULL) {
404  if (tmp->msg) {
405  /* Optionally display the reason why one cannot move
406  * there. Note: emitting a message from this function
407  * is not very elegant. Ideally, this should be done
408  * somewhere in server/player.c, but this is difficult
409  * for objects of type CHECK_INV that are not alive.
410  */
413  tmp->msg);
414  }
415  return 1;
416  }
417  } else {
418  /* In this case, the player must not have the object -
419  * if they do, they can't pass through.
420  */
421  if (check_inv_recursive(ob, tmp) != NULL) {
422  if (tmp->msg) {
425  tmp->msg);
426  }
427  return 1;
428  }
429  }
430  } /* if check_inv */
431  else {
432  /* Broke apart a big nasty if into several here to make
433  * this more readable. first check - if the space blocks
434  * movement, can't move here.
435  * second - if a monster, can't move there, unless it is a
436  * hidden dm
437  */
438  if (OB_MOVE_BLOCK(ob, tmp))
439  return 1;
440  if (QUERY_FLAG(tmp, FLAG_ALIVE)
441  && tmp->head != ob
442  && tmp != ob
443  && tmp->type != DOOR
444  && !(QUERY_FLAG(tmp, FLAG_WIZ) && tmp->contr->hidden))
445  return 1;
446  }
447  } FOR_MAP_FINISH();
448  return 0;
449 }
450 
487 int ob_blocked(const object *ob, mapstruct *m, int16_t x, int16_t y) {
488  archetype *tmp;
489  int flag;
490  mapstruct *m1;
491  int16_t sx, sy;
492  const object *part;
493 
494  if (ob == NULL) {
495  flag = get_map_flags(m, &m1, x, y, &sx, &sy);
496  if (flag&P_OUT_OF_MAP)
497  return P_OUT_OF_MAP;
498 
499  /* don't have object, so don't know what types would block */
500  return(GET_MAP_MOVE_BLOCK(m1, sx, sy));
501  }
502 
503  for (tmp = ob->arch, part = ob; tmp != NULL; tmp = tmp->more, part = part->more) {
504  flag = get_map_flags(m, &m1, x+tmp->clone.x, y+tmp->clone.y, &sx, &sy);
505 
506  if (flag&P_OUT_OF_MAP)
507  return P_OUT_OF_MAP;
508  if (flag&P_IS_ALIVE)
509  return P_IS_ALIVE;
510 
511  /* object_find_first_free_spot() calls this function. However, often
512  * ob doesn't have any move type (when used to place exits)
513  * so the AND operation in OB_TYPE_MOVE_BLOCK doesn't work.
514  */
515 
516  if (ob->move_type == 0 && GET_MAP_MOVE_BLOCK(m1, sx, sy) != MOVE_ALL)
517  continue;
518 
519  /* A transport without move_type for a part should go through everything for that part. */
520  if (ob->type == TRANSPORT && part->move_type == 0)
521  continue;
522 
523  /* Note it is intentional that we check ob - the movement type of the
524  * head of the object should correspond for the entire object.
525  */
526  if (OB_TYPE_MOVE_BLOCK(ob, GET_MAP_MOVE_BLOCK(m1, sx, sy)))
527  return AB_NO_PASS;
528  }
529  return 0;
530 }
531 
542 void fix_container(object *container) {
543  object *tmp = container->inv;
544 
545  container->inv = NULL;
547  if (tmp->inv)
548  fix_container(tmp);
549  (void)object_insert_in_ob(tmp, container);
551  /* object_sum_weight will go through and calculate what all the containers are
552  * carrying.
553  */
554  object_sum_weight(container);
555 }
556 
567 static void fix_container_multipart(object *container) {
568  FOR_INV_PREPARE(container, tmp) {
569  archetype *at;
570  object *op, *last;
571 
572  if (tmp->inv)
574  /* already multipart, or non-multipart arch - don't do anything more */
575  for (at = tmp->arch->more, last = tmp; at != NULL; at = at->more, last = op) {
576  /* FIXME: We can't reuse object_fix_multipart() since that only
577  * works for items directly on maps. Maybe factor out common code?
578  */
579  op = arch_to_object(at);
580  op->head = tmp;
581  op->env = tmp->env;
582  last->more = op;
583  if (tmp->name != op->name) {
584  if (op->name)
585  free_string(op->name);
586  op->name = add_string(tmp->name);
587  }
588  if (tmp->title != op->title) {
589  if (op->title)
590  free_string(op->title);
591  op->title = add_string(tmp->title);
592  }
594  }
595  } FOR_INV_FINISH();
596 }
597 
609  int x, y;
610 
611  for (x = 0; x < MAP_WIDTH(m); x++)
612  for (y = 0; y < MAP_HEIGHT(m); y++)
613  FOR_MAP_PREPARE(m, x, y, tmp) {
614  if (tmp->inv)
616 
617  /* already multipart - don't do anything more */
618  if (tmp->head || tmp->more)
619  continue;
620 
622  } FOR_MAP_FINISH(); /* for objects on this space */
623 }
624 
636 static void load_objects(mapstruct *m, FILE *fp, int mapflags) {
637  int i, j, bufstate = LO_NEWFILE;
638  int unique;
639  object *op, *prev = NULL, *last_more = NULL;
640 
641  op = object_new();
642  op->map = m; /* To handle buttons correctly */
643 
644  while ((i = load_object(fp, op, bufstate, mapflags))) {
645  /* Since the loading of the map header does not load an object
646  * anymore, we need to pass LO_NEWFILE for the first object loaded,
647  * and then switch to LO_REPEAT for faster loading.
648  */
649  bufstate = LO_REPEAT;
650 
651  /* if the archetype for the object is null, means that we
652  * got an invalid object. Don't do anything with it - the game
653  * or editor will not be able to do anything with it either.
654  */
655  if (op->arch == NULL) {
656  LOG(llevDebug, "Discarding object without arch: %s\n", op->name ? op->name : "(null)");
657  continue;
658  }
659 
660  /*
661  * You can NOT have players on a map being loaded.
662  * Trying to use such a type leads to crashes everywhere as op->contr is NULL.
663  */
664  if (op->type == PLAYER) {
665  LOG(llevError, "Discarding invalid item with type PLAYER in map %s\n", m->path);
666  continue;
667  }
668 
669  /* don't use out_of_map because we don't want to consider tiling properties, we're loading a single map */
670  if (op->x < 0 || op->y < 0 || op->x >= MAP_WIDTH(m) || op->y >= MAP_HEIGHT(m)) {
671  LOG(llevError, " object %s not on valid map position %s:%d:%d\n", op->name ? op->name : "(null)", m->path, op->x, op->y);
672  if (op->x < 0) {
673  op->x = 0;
674  } else if (op->x >= MAP_WIDTH(m)) {
675  op->x = MAP_WIDTH(m) - 1;
676  }
677  if (op->y < 0) {
678  op->y = 0;
679  } else if (op->y >= MAP_HEIGHT(m)) {
680  op->y = MAP_HEIGHT(m) - 1;
681  }
682  }
683 
684  switch (i) {
685  case LL_NORMAL:
686  /* if we are loading an overlay, put the floors on the bottom */
688  && mapflags&MAP_OVERLAY)
690  else
692 
693  if (op->inv)
694  object_sum_weight(op);
695 
696  prev = op,
697  last_more = op;
698  break;
699 
700  case LL_MORE:
702  op->head = prev,
703  last_more->more = op,
704  last_more = op;
705  break;
706  }
707  if (mapflags&MAP_STYLE) {
709  }
710  op = object_new();
711  op->map = m;
712  }
713  for (i = 0; i < m->width; i++) {
714  for (j = 0; j < m->height; j++) {
715  unique = 0;
716  /* check for unique items, or unique squares */
717  FOR_MAP_PREPARE(m, i, j, otmp) {
718  if (QUERY_FLAG(otmp, FLAG_UNIQUE))
719  unique = 1;
720  if (!(mapflags&(MAP_OVERLAY|MAP_PLAYER_UNIQUE) || unique))
722  } FOR_MAP_FINISH();
723  }
724  }
727 }
728 
746 static int save_objects(mapstruct *m, FILE *fp, FILE *fp2, int flag) {
747  int i, j = 0, unique = 0, res = 0;
748 
749  /* first pass - save one-part objects */
750  for (i = 0; i < MAP_WIDTH(m); i++)
751  for (j = 0; j < MAP_HEIGHT(m); j++) {
752  unique = 0;
753  FOR_MAP_PREPARE(m, i, j, op) {
755  unique = 1;
756 
757  if (op->type == PLAYER) {
758  LOG(llevDebug, "Player on map that is being saved\n");
759  continue;
760  }
761 
762  if (op->head || object_get_owner(op) != NULL)
763  continue;
764 
765  if (unique || QUERY_FLAG(op, FLAG_UNIQUE))
767  else
768  if (flag == 0
769  || (flag == SAVE_FLAG_NO_REMOVE && (!QUERY_FLAG(op, FLAG_OBJ_ORIGINAL) && !QUERY_FLAG(op, FLAG_UNPAID))))
771 
772  if (res != 0)
773  return res;
774  } FOR_MAP_FINISH(); /* for this space */
775  } /* for this j */
776 
777  return 0;
778 }
779 
792  mapstruct *map = (mapstruct *)calloc(1, sizeof(mapstruct));
793  /* mapstruct *mp;*/
794 
795  if (map == NULL)
797  /*
798  * Nothing in the code appears to require we add new maps to the end of the list.
799  * Why not just add them to the front instead? Its faster.
800  *
801  * SilverNexus 2016-05-18
802  *
803  for (mp = first_map; mp != NULL && mp->next != NULL; mp = mp->next)
804  ;
805  if (mp == NULL)
806  first_map = map;
807  else
808  mp->next = map;
809  */
810  map->next = first_map;
811  first_map = map;
812 
813  map->in_memory = MAP_SWAPPED;
814  /* The maps used to pick up default x and y values from the
815  * map archetype. Mimic that behaviour.
816  */
817  MAP_WIDTH(map) = 16;
818  MAP_HEIGHT(map) = 16;
819  MAP_RESET_TIMEOUT(map) = 0;
820  MAP_TIMEOUT(map) = 300;
821  MAP_ENTER_X(map) = 0;
822  MAP_ENTER_Y(map) = 0;
823  map->last_reset_time = 0;
824  return map;
825 }
826 
829  return (uint32_t)m->width * (uint32_t)m->height;
830 }
831 
842 static void allocate_map(mapstruct *m) {
844  /* Log this condition and free the storage. We could I suppose
845  * realloc, but if the caller is presuming the data will be intact,
846  * that is their poor assumption.
847  */
848  if (m->spaces) {
849  LOG(llevError, "allocate_map called with already allocated map (%s)\n", m->path);
850  free(m->spaces);
851  }
852 
853  m->spaces = calloc(map_size(m), sizeof(MapSpace));
854 
855  if (m->spaces == NULL)
857 }
858 
872 mapstruct *get_empty_map(int sizex, int sizey) {
873  mapstruct *m = get_linked_map();
874  m->width = sizex;
875  m->height = sizey;
876  m->in_memory = MAP_SWAPPED;
877  allocate_map(m);
878  return m;
879 }
880 
894 static shopitems *parse_shop_string(const char *input_string, const mapstruct *map) {
895  char *shop_string, *p, *q, *next_semicolon, *next_colon;
896  shopitems *items = NULL;
897  int i = 0, number_of_entries = 0;
898  const typedata *current_type;
899 
900  shop_string = strdup_local(input_string);
901  p = shop_string;
902  LOG(llevDebug, "parsing %s\n", input_string);
903  /* first we'll count the entries, we'll need that for allocating the array shortly */
904  while (p) {
905  p = strchr(p, ';');
906  number_of_entries++;
907  if (p)
908  p++;
909  }
910  p = shop_string;
911  strip_endline(p);
912  items = CALLOC(number_of_entries+1, sizeof(shopitems));
913  /*
914  * The memset would always set at least one byte to zero,
915  * so a failed calloc would have segfaulted the program.
916  * Instead, check for a null and fail more gracefully.
917  */
918  if (!items)
920  /*
921  * calloc() already sets each byte to zero already
922  *
923  memset(items, 0, (sizeof(shopitems)*number_of_entries+1));
924  */
925  for (i = 0; i < number_of_entries; i++) {
926  if (!p) {
927  LOG(llevError, "parse_shop_string: I seem to have run out of string, that shouldn't happen.\n");
928  break;
929  }
930  next_semicolon = strchr(p, ';');
931  next_colon = strchr(p, ':');
932  /* if there is a stregth specified, figure out what it is, we'll need it soon. */
933  if (next_colon && (!next_semicolon || next_colon < next_semicolon))
934  items[i].strength = atoi(strchr(p, ':')+1);
935 
936  if (isdigit(*p) || *p == '*') {
937  items[i].typenum = *p == '*' ? -1 : atoi(p);
938  current_type = get_typedata(items[i].typenum);
939  if (current_type) {
940  items[i].name = current_type->name;
941  items[i].name_pl = current_type->name_pl;
942  }
943  } else { /*we have a named type, let's figure out what it is */
944  q = strpbrk(p, ";:");
945  if (q)
946  *q = '\0';
947 
948  current_type = get_typedata_by_name(p);
949  if (current_type) {
950  items[i].name = current_type->name;
951  items[i].typenum = current_type->number;
952  items[i].name_pl = current_type->name_pl;
953  } else {
954  /* oh uh, something's wrong, let's free up this one, and try
955  * the next entry while we're at it, better print a warning */
956  LOG(llevError, "invalid type %s defined in shopitems for %s in string %s\n", p, map->name, input_string);
957  }
958  }
959  items[i].index = number_of_entries;
960  if (next_semicolon)
961  p = ++next_semicolon;
962  else
963  p = NULL;
964  }
965  free(shop_string);
966  return items;
967 }
968 
980 static void print_shop_string(mapstruct *m, char *output_string, int size) {
981  int i;
982  char tmp[MAX_BUF];
983 
984  output_string[0] = '\0';
985  for (i = 0; i < m->shopitems[0].index; i++) {
986  if (m->shopitems[i].typenum != -1) {
987  if (m->shopitems[i].strength) {
988  snprintf(tmp, sizeof(tmp), "%s:%d;", m->shopitems[i].name, m->shopitems[i].strength);
989  } else
990  snprintf(tmp, sizeof(tmp), "%s;", m->shopitems[i].name);
991  } else {
992  if (m->shopitems[i].strength) {
993  snprintf(tmp, sizeof(tmp), "*:%d;", m->shopitems[i].strength);
994  } else
995  snprintf(tmp, sizeof(tmp), "*;");
996  }
997  snprintf(output_string+strlen(output_string), size-strlen(output_string), "%s", tmp);
998  }
999 
1000  /* erase final ; else parsing back will lead to issues */
1001  if (strlen(output_string) > 0) {
1002  output_string[strlen(output_string) - 1] = '\0';
1003  }
1004 }
1005 
1025 static int load_map_header(FILE *fp, mapstruct *m) {
1026  char buf[HUGE_BUF], *key = NULL, *value;
1027 
1028  m->width = m->height = 0;
1029  while (fgets(buf, sizeof(buf), fp) != NULL) {
1030  char *p;
1031 
1032  p = strchr(buf, '\n');
1033  if (p == NULL) {
1034  LOG(llevError, "Error loading map header - did not find a newline - perhaps file is truncated? Buf=%s\n", buf);
1035  return 1;
1036  }
1037  *p = '\0';
1038 
1039  key = buf;
1040  while (isspace(*key))
1041  key++;
1042  if (*key == 0)
1043  continue; /* empty line */
1044  value = strchr(key, ' ');
1045  if (value) {
1046  *value = 0;
1047  value++;
1048  while (isspace(*value)) {
1049  value++;
1050  if (*value == '\0') {
1051  /* Nothing but spaces. */
1052  value = NULL;
1053  break;
1054  }
1055  }
1056  }
1057 
1058  /* key is the field name, value is what it should be set
1059  * to. We've already done the work to null terminate key,
1060  * and strip off any leading spaces for both of these.
1061  * We have not touched the newline at the end of the line -
1062  * these are needed for some values. the end pointer
1063  * points to the first of the newlines.
1064  * value could be NULL! It would be easy enough to just point
1065  * this to "" to prevent cores, but that would let more errors slide
1066  * through.
1067  *
1068  * First check for entries that do not use the value parameter, then
1069  * validate that value is given and check for the remaining entries
1070  * that use the parameter.
1071  */
1072 
1073  if (!strcmp(key, "msg")) {
1074  char msgbuf[HUGE_BUF];
1075  int msgpos = 0;
1076 
1077  while (fgets(buf, sizeof(buf), fp) != NULL) {
1078  if (!strcmp(buf, "endmsg\n"))
1079  break;
1080  else {
1081  snprintf(msgbuf+msgpos, sizeof(msgbuf)-msgpos, "%s", buf);
1082  msgpos += strlen(buf);
1083  }
1084  }
1085  /* There are lots of maps that have empty messages (eg, msg/endmsg
1086  * with nothing between). There is no reason in those cases to
1087  * keep the empty message. Also, msgbuf contains garbage data
1088  * when msgpos is zero, so copying it results in crashes
1089  */
1090  if (msgpos != 0) {
1091  /* When loading eg an overlay, message is already set, so free() current one. */
1092  free(m->msg);
1093  m->msg = strdup_local(msgbuf);
1094  }
1095  } else if (!strcmp(key, "maplore")) {
1096  char maplorebuf[HUGE_BUF];
1097  size_t maplorepos = 0;
1098 
1099  while (fgets(buf, HUGE_BUF-1, fp) != NULL) {
1100  if (!strcmp(buf, "endmaplore\n"))
1101  break;
1102  else {
1103  if (maplorepos >= sizeof(maplorebuf)) {
1104  LOG(llevError, "Map lore exceeds buffer length\n");
1105  return 1;
1106  }
1107  snprintf(maplorebuf+maplorepos, sizeof(maplorebuf)-maplorepos, "%s", buf);
1108  maplorepos += strlen(buf);
1109  }
1110  }
1111  if (maplorepos != 0)
1112  m->maplore = strdup_local(maplorebuf);
1113  } else if (!strcmp(key, "end")) {
1114  break;
1115  } else if (value == NULL) {
1116  LOG(llevError, "Got '%s' line without parameter in map header\n", key);
1117  } else if (!strcmp(key, "arch")) {
1118  /* This is an oddity, but not something we care about much. */
1119  if (strcmp(value, "map")) {
1120  LOG(llevError, "load_map_header: expected 'arch map': check line endings?\n");
1121  return 1;
1122  }
1123  } else if (!strcmp(key, "name")) {
1124  /* When loading eg an overlay, the name is already set, so free() current one. */
1125  free(m->name);
1126  m->name = strdup_local(value);
1127  /* first strcmp value on these are old names supported
1128  * for compatibility reasons. The new values (second) are
1129  * what really should be used.
1130  */
1131  } else if (!strcmp(key, "enter_x")) {
1132  m->enter_x = atoi(value);
1133  } else if (!strcmp(key, "enter_y")) {
1134  m->enter_y = atoi(value);
1135  } else if (!strcmp(key, "width")) {
1136  m->width = atoi(value);
1137  } else if (!strcmp(key, "height")) {
1138  m->height = atoi(value);
1139  } else if (!strcmp(key, "reset_timeout")) {
1140  m->reset_timeout = atoi(value);
1141  } else if (!strcmp(key, "swap_time")) {
1142  m->timeout = atoi(value);
1143  } else if (!strcmp(key, "difficulty")) {
1144  m->difficulty = atoi(value);
1145  } else if (!strcmp(key, "darkness")) {
1146  m->darkness = atoi(value);
1147  } else if (!strcmp(key, "fixed_resettime")) {
1148  m->fixed_resettime = atoi(value);
1149  } else if (!strcmp(key, "unique")) {
1150  m->unique = atoi(value);
1151  } else if (!strcmp(key, "template")) {
1152  m->is_template = atoi(value);
1153  } else if (!strcmp(key, "region")) {
1154  m->region = get_region_by_name(value);
1155  } else if (!strcmp(key, "shopitems")) {
1156  m->shopitems = parse_shop_string(value, m);
1157  } else if (!strcmp(key, "shopgreed")) {
1158  m->shopgreed = atof(value);
1159  } else if (!strcmp(key, "shopmin")) {
1160  m->shopmin = atol(value);
1161  } else if (!strcmp(key, "shopmax")) {
1162  m->shopmax = atol(value);
1163  } else if (!strcmp(key, "shoprace")) {
1164  m->shoprace = strdup_local(value);
1165  } else if (!strcmp(key, "outdoor")) {
1166  m->outdoor = atoi(value);
1167  } else if (!strcmp(key, "nosmooth")) {
1168  m->nosmooth = atoi(value);
1169  } else if (!strcmp(key, "first_load")) {
1170  m->last_reset_time = atoi(value);
1171  } else if (!strncmp(key, "tile_path_", 10)) {
1172  int tile = atoi(key+10);
1173 
1174  if (tile < 1 || tile > 4) {
1175  LOG(llevError, "load_map_header: tile location %d out of bounds (%s)\n", tile, m->path);
1176  } else {
1177  char path[HUGE_BUF];
1178 
1179  if (m->tile_path[tile-1]) {
1180  LOG(llevError, "load_map_header: tile location %d duplicated (%s)\n", tile, m->path);
1181  free(m->tile_path[tile-1]);
1182  m->tile_path[tile-1] = NULL;
1183  }
1184 
1185  if (check_path(value, 1) != -1) {
1186  /* The unadorned path works. */
1187  snprintf(path, sizeof(path), "%s", value);
1188  } else {
1189  /* Try again; it could be a relative exit. */
1190  path_combine_and_normalize(m->path, value, path, sizeof(path));
1191 
1192  if (check_path(path, 1) == -1) {
1193  LOG(llevError, "get_map_header: Bad tile path %s %s\n", m->path, value);
1194  path[0] = '\0';
1195  }
1196  }
1197 
1198  if (*path != '\0') {
1199  /* Use the normalized value. */
1200  m->tile_path[tile-1] = strdup_local(path);
1201  }
1202  } /* end if tile direction (in)valid */
1203  } else if (!strcmp(key, "background_music")) {
1204  m->background_music = strdup_local(value);
1205  } else {
1206  LOG(llevError, "Got unknown value in map header: %s %s\n", key, value);
1207  }
1208  }
1209  if ((m->width == 0) || (m->height == 0)) {
1210  LOG(llevError, "Map width or height not specified\n");
1211  return 1;
1212  }
1213  if (!key || strcmp(key, "end")) {
1214  LOG(llevError, "Got premature eof on map header!\n");
1215  return 1;
1216  }
1217  return 0;
1218 }
1219 
1235 mapstruct *mapfile_load(const char *map, int flags) {
1236  FILE *fp;
1237  mapstruct *m;
1238  char pathname[MAX_BUF];
1239 
1240  if (flags&MAP_PLAYER_UNIQUE)
1241  snprintf(pathname, sizeof(pathname), "%s", map);
1242  else if (flags&MAP_OVERLAY)
1243  create_overlay_pathname(map, pathname, MAX_BUF);
1244  else
1245  create_pathname(map, pathname, MAX_BUF);
1246 
1247  if ((fp = fopen(pathname, "r")) == NULL) {
1248  LOG((flags&MAP_PLAYER_UNIQUE) ? llevDebug : llevError,
1249  "Can't open %s: %s\n", pathname, strerror(errno));
1250  return (NULL);
1251  }
1252 
1253  m = get_linked_map();
1254 
1255  safe_strncpy(m->path, map, HUGE_BUF);
1256  if (load_map_header(fp, m)) {
1257  LOG(llevError, "Error loading map header for %s, flags=%d\n", map, flags);
1258  delete_map(m);
1259  fclose(fp);
1260  return NULL;
1261  }
1262 
1263  allocate_map(m);
1264 
1265  m->in_memory = MAP_LOADING;
1266  load_objects(m, fp, flags & MAP_STYLE);
1267  fclose(fp);
1268  m->in_memory = MAP_IN_MEMORY;
1269  if (!MAP_DIFFICULTY(m))
1271  set_map_reset_time(m);
1272 
1273  /* In case other objects press some buttons down */
1274  update_buttons(m);
1275 
1276  /* Handle for map load event */
1278 
1279  if (!(flags & MAP_STYLE))
1280  apply_auto_fix(m); /* Chests which open as default */
1281 
1282  return (m);
1283 }
1284 
1294  FILE *fp;
1295 
1296  if (!m->tmpname) {
1297  LOG(llevError, "No temporary filename for map %s\n", m->path);
1298  return 1;
1299  }
1300 
1301  if ((fp = fopen(m->tmpname, "r")) == NULL) {
1302  LOG(llevError, "Cannot open %s: %s\n", m->tmpname, strerror(errno));
1303  return 2;
1304  }
1305 
1306  if (load_map_header(fp, m)) {
1307  LOG(llevError, "Error loading map header for %s (%s)\n", m->path, m->tmpname);
1308  fclose(fp);
1309  return 3;
1310  }
1311  allocate_map(m);
1312 
1313  m->in_memory = MAP_LOADING;
1314  load_objects(m, fp, 0);
1315  fclose(fp);
1316  m->in_memory = MAP_IN_MEMORY;
1317  return 0;
1318 }
1319 
1329 static int load_overlay_map(const char *filename, mapstruct *m) {
1330  FILE *fp;
1331  char pathname[MAX_BUF];
1332 
1333  create_overlay_pathname(filename, pathname, MAX_BUF);
1334 
1335  if ((fp = fopen(pathname, "r")) == NULL) {
1336  /* nothing bad to not having an overlay */
1337  return 0;
1338  }
1339 
1340  if (load_map_header(fp, m)) {
1341  LOG(llevError, "Error loading map header for overlay %s (%s)\n", m->path, pathname);
1342  fclose(fp);
1343  return 1;
1344  }
1345  /*allocate_map(m);*/
1346 
1347  m->in_memory = MAP_LOADING;
1348  load_objects(m, fp, MAP_OVERLAY);
1349  fclose(fp);
1350  m->in_memory = MAP_IN_MEMORY;
1351  return 0;
1352 }
1353 
1354 /******************************************************************************
1355  * This is the start of unique map handling code
1356  *****************************************************************************/
1357 
1365  int i, j, unique = 0;
1366 
1367  for (i = 0; i < MAP_WIDTH(m); i++)
1368  for (j = 0; j < MAP_HEIGHT(m); j++) {
1369  unique = 0;
1370  FOR_MAP_PREPARE(m, i, j, op) {
1372  unique = 1;
1373  if (op->head == NULL && (QUERY_FLAG(op, FLAG_UNIQUE) || unique)) {
1374  clean_object(op);
1375  if (QUERY_FLAG(op, FLAG_IS_LINKED))
1376  remove_button_link(op);
1377  object_remove(op);
1379  }
1380  } FOR_MAP_FINISH();
1381  }
1382 }
1383 
1390  FILE *fp;
1391  int count;
1392  char firstname[MAX_BUF], name[MAX_BUF];
1393 
1394  create_items_path(m->path, name, MAX_BUF);
1395  for (count = 0; count < 10; count++) {
1396  snprintf(firstname, sizeof(firstname), "%s.v%02d", name, count);
1397  if (!access(firstname, R_OK))
1398  break;
1399  }
1400  /* If we get here, we did not find any map */
1401  if (count == 10)
1402  return;
1403 
1404  if ((fp = fopen(firstname, "r")) == NULL) {
1405  /* There is no expectation that every map will have unique items, but this
1406  * is debug output, so leave it in.
1407  */
1408  LOG(llevDebug, "Can't open unique items file for %s\n", name);
1409  return;
1410  }
1411 
1412  m->in_memory = MAP_LOADING;
1413  if (m->tmpname == NULL) /* if we have loaded unique items from */
1414  delete_unique_items(m); /* original map before, don't duplicate them */
1415  load_object(fp, NULL, LO_NOREAD, 0);
1416  load_objects(m, fp, 0);
1417  fclose(fp);
1418  m->in_memory = MAP_IN_MEMORY;
1419 }
1420 
1421 static bool in_localdir(const char path[static 1]) {
1422  return strstr(path, settings.localdir) == path;
1423 }
1424 
1440 int save_map(mapstruct *m, int flag) {
1441 #define TEMP_EXT ".savefile"
1442  FILE *fp, *fp2;
1443  OutputFile of, of2;
1444  char filename[MAX_BUF], buf[MAX_BUF], shop[MAX_BUF], final[MAX_BUF];
1445  int i, res;
1446 
1447  if (flag && !*m->path) {
1448  LOG(llevError, "Tried to save map without path.\n");
1449  return SAVE_ERROR_NO_PATH;
1450  }
1451 
1452  if (flag != SAVE_MODE_NORMAL || (m->unique) || (m->is_template)) {
1453  if (!m->unique && !m->is_template) { /* flag is set */
1454  if (flag == SAVE_MODE_OVERLAY)
1455  create_overlay_pathname(m->path, filename, MAX_BUF);
1456  else
1457  create_pathname(m->path, filename, MAX_BUF);
1458  } else {
1459  if (!in_localdir(m->path)) {
1460  LOG(llevError,
1461  "Cannot save unique map '%s' outside of LOCALDIR. Check "
1462  "that all exits to '%s' have FLAG_UNIQUE set correctly.\n",
1463  m->path, m->path);
1464  return SAVE_ERROR_UCREATION;
1465  }
1466  snprintf(filename, sizeof(filename), "%s", m->path);
1467  }
1468 
1469  make_path_to_file(filename);
1470  } else {
1471  if (!m->tmpname)
1472  m->tmpname = tempnam(settings.tmpdir, NULL);
1473  snprintf(filename, sizeof(filename), "%s", m->tmpname);
1474  }
1475  LOG(llevDebug, "Saving map %s\n", m->path);
1476  m->in_memory = MAP_SAVING;
1477 
1478  snprintf(final, sizeof(final), "%s", filename);
1479  snprintf(filename, sizeof(filename), "%s%s", final, TEMP_EXT);
1480  fp = of_open(&of, filename);
1481  if (fp == NULL)
1482  return SAVE_ERROR_RCREATION;
1483 
1484  /* legacy */
1485  fprintf(fp, "arch map\n");
1486  if (m->name)
1487  fprintf(fp, "name %s\n", m->name);
1488  if (!flag)
1489  fprintf(fp, "swap_time %d\n", m->swap_time);
1490  if (m->reset_timeout)
1491  fprintf(fp, "reset_timeout %u\n", m->reset_timeout);
1492  if (m->fixed_resettime)
1493  fprintf(fp, "fixed_resettime %d\n", m->fixed_resettime);
1494  /* we unfortunately have no idea if this is a value the creator set
1495  * or a difficulty value we generated when the map was first loaded
1496  */
1497  if (m->difficulty)
1498  fprintf(fp, "difficulty %d\n", m->difficulty);
1499  if (m->region)
1500  fprintf(fp, "region %s\n", m->region->name);
1501  if (m->shopitems) {
1502  print_shop_string(m, shop, sizeof(shop));
1503  fprintf(fp, "shopitems %s\n", shop);
1504  }
1505  if (m->shopgreed)
1506  fprintf(fp, "shopgreed %f\n", m->shopgreed);
1507  if (m->shopmin)
1508  fprintf(fp, "shopmin %"FMT64U"\n", m->shopmin);
1509  if (m->shopmax)
1510  fprintf(fp, "shopmax %"FMT64U"\n", m->shopmax);
1511  if (m->shoprace)
1512  fprintf(fp, "shoprace %s\n", m->shoprace);
1513  if (m->darkness)
1514  fprintf(fp, "darkness %d\n", m->darkness);
1515  if (m->width)
1516  fprintf(fp, "width %d\n", m->width);
1517  if (m->height)
1518  fprintf(fp, "height %d\n", m->height);
1519  if (m->enter_x)
1520  fprintf(fp, "enter_x %d\n", m->enter_x);
1521  if (m->enter_y)
1522  fprintf(fp, "enter_y %d\n", m->enter_y);
1523  if (m->msg)
1524  fprintf(fp, "msg\n%sendmsg\n", m->msg);
1525  if (m->maplore)
1526  fprintf(fp, "maplore\n%sendmaplore\n", m->maplore);
1527  if (m->unique)
1528  fprintf(fp, "unique %d\n", m->unique);
1529  if (m->is_template)
1530  fprintf(fp, "template %d\n", m->is_template);
1531  if (m->outdoor)
1532  fprintf(fp, "outdoor %d\n", m->outdoor);
1533  if (m->nosmooth)
1534  fprintf(fp, "nosmooth %d\n", m->nosmooth);
1535  if (m->last_reset_time)
1536  fprintf(fp, "first_load %ld\n", m->last_reset_time);
1537  if (m->background_music)
1538  fprintf(fp, "background_music %s\n", m->background_music);
1539 
1540  /* Save any tiling information, except on overlays */
1541  if (flag != SAVE_MODE_OVERLAY)
1542  for (i = 0; i < 4; i++)
1543  if (m->tile_path[i])
1544  fprintf(fp, "tile_path_%d %s\n", i+1, m->tile_path[i]);
1545 
1546  fprintf(fp, "end\n");
1547 
1548  /* In the game save unique items in the different file, but
1549  * in the editor save them to the normal map file.
1550  * If unique map, save files in the proper destination (set by
1551  * player)
1552  */
1553  if ((flag == SAVE_MODE_NORMAL || flag == SAVE_MODE_OVERLAY) && !m->unique && !m->is_template) {
1554  char name[MAX_BUF], final_unique[MAX_BUF];
1555 
1556  create_items_path(m->path, name, MAX_BUF);
1557  snprintf(final_unique, sizeof(final_unique), "%s.v00", name);
1558  snprintf(buf, sizeof(buf), "%s%s", final_unique, TEMP_EXT);
1559  fp2 = of_open(&of2, buf);
1560  if (fp2 == NULL) {
1561  of_cancel(&of);
1562  return SAVE_ERROR_UCREATION;
1563  }
1564  if (flag == SAVE_MODE_OVERLAY) {
1565  /* SO_FLAG_NO_REMOVE is non destructive save, so map is still valid. */
1566  res = save_objects(m, fp, fp2, SAVE_FLAG_NO_REMOVE);
1567  if (res < 0) {
1568  LOG(llevError, "Save error during object save: %d\n", res);
1569  of_cancel(&of);
1570  of_cancel(&of2);
1571  return res;
1572  }
1573  m->in_memory = MAP_IN_MEMORY;
1574  } else {
1575  res = save_objects(m, fp, fp2, 0);
1576  if (res < 0) {
1577  LOG(llevError, "Save error during object save: %d\n", res);
1578  of_cancel(&of);
1579  of_cancel(&of2);
1580  return res;
1581  }
1582  free_all_objects(m);
1583  }
1584  if (ftell(fp2) == 0) {
1585  of_cancel(&of2);
1586  unlink(buf);
1587  /* If there are no unique items left on the map, we need to
1588  * unlink the original unique map so that the unique
1589  * items don't show up again.
1590  */
1591  unlink(final_unique);
1592  } else {
1593  if (!of_close(&of2)) {
1594  of_cancel(&of);
1595  return SAVE_ERROR_WRITE;
1596  }
1597  unlink(final_unique); /* failure isn't too bad, maybe the file doesn't exist. */
1598  if (rename(buf, final_unique) == -1) {
1599  LOG(llevError, "Couldn't rename unique file %s to %s\n", buf, final_unique);
1600  of_cancel(&of);
1601  return SAVE_ERROR_URENAME;
1602  }
1603 
1604  if (chmod(final_unique, SAVE_MODE) != 0) {
1605  LOG(llevError, "Could not set permissions on '%s'\n",
1606  final_unique);
1607  }
1608  }
1609  } else { /* save same file when not playing, like in editor */
1610  res = save_objects(m, fp, fp, 0);
1611  if (res < 0) {
1612  LOG(llevError, "Save error during object save: %d\n", res);
1613  of_cancel(&of);
1614  return res;
1615  }
1616  free_all_objects(m);
1617  }
1618 
1619  if (!of_close(&of))
1620  return SAVE_ERROR_CLOSE;
1621  unlink(final); /* failure isn't too bad, maybe the file doesn't exist. */
1622  if (rename(filename, final) == -1) {
1623  LOG(llevError, "Couldn't rename regular file %s to %s\n", filename, final);
1624  return SAVE_ERROR_RRENAME;
1625  }
1626 
1627  if (chmod(final, SAVE_MODE) != 0) {
1628  LOG(llevError, "Could not set permissions on '%s'\n", final);
1629  }
1630 
1631  return SAVE_ERROR_OK;
1632 }
1633 
1643 void clean_object(object *op) {
1644  FOR_INV_PREPARE(op, tmp) {
1645  clean_object(tmp);
1646  if (QUERY_FLAG(tmp, FLAG_IS_LINKED))
1647  remove_button_link(tmp);
1648  object_remove(tmp);
1650  } FOR_INV_FINISH();
1651 }
1652 
1659 static void free_all_objects(mapstruct *m) {
1660  int i, j;
1661  object *op;
1662 
1663  for (i = 0; i < MAP_WIDTH(m); i++)
1664  for (j = 0; j < MAP_HEIGHT(m); j++) {
1665  object *previous_obj = NULL;
1666 
1667  while ((op = GET_MAP_OB(m, i, j)) != NULL) {
1668  if (op == previous_obj) {
1669  LOG(llevDebug, "free_all_objects: Link error, bailing out.\n");
1670  break;
1671  }
1672  previous_obj = op;
1673  op = HEAD(op);
1674 
1675  /* If the map isn't in memory, object_free_drop_inventory() will remove and
1676  * free objects in op's inventory. So let it do the job.
1677  */
1678  if (m->in_memory == MAP_IN_MEMORY)
1679  clean_object(op);
1680  object_remove(op);
1682  }
1683  }
1684 #ifdef MANY_CORES
1685  /* I see periodic cores on metalforge where a map has been swapped out, but apparantly
1686  * an item on that map was not saved - look for that condition and die as appropriate -
1687  * this leaves more of the map data intact for better debugging.
1688  */
1689  for (op = objects; op != NULL; op = op->next) {
1690  if (!QUERY_FLAG(op, FLAG_REMOVED) && op->map == m) {
1691  LOG(llevDebug, "free_all_objects: object %s still on map after it should have been freed\n", op->name);
1692  abort();
1693  }
1694  }
1695 #endif
1696 }
1697 
1707  int i;
1708 
1709  if (!m->in_memory) {
1710  LOG(llevError, "Trying to free freed map.\n");
1711  return;
1712  }
1713 
1714  /* Handle for plugin map unload event. */
1716 
1717  if (m->spaces)
1718  free_all_objects(m);
1719  if (m->name)
1720  FREE_AND_CLEAR(m->name);
1721  if (m->spaces)
1722  FREE_AND_CLEAR(m->spaces);
1723  if (m->msg)
1724  FREE_AND_CLEAR(m->msg);
1725  if (m->maplore)
1726  FREE_AND_CLEAR(m->maplore);
1727  if (m->shopitems)
1729  if (m->shoprace)
1731  if (m->background_music)
1733  if (m->buttons)
1735  m->buttons = NULL;
1736  for (i = 0; i < 4; i++) {
1737  if (m->tile_path[i])
1738  FREE_AND_CLEAR(m->tile_path[i]);
1739  m->tile_map[i] = NULL;
1740  }
1741  m->in_memory = MAP_SWAPPED;
1742 }
1743 
1754  mapstruct *tmp, *last;
1755  int i;
1756 
1757  if (!m)
1758  return;
1759  if (m->in_memory == MAP_IN_MEMORY) {
1760  /* change to MAP_SAVING, even though we are not,
1761  * so that object_remove() doesn't do as much work.
1762  */
1763  m->in_memory = MAP_SAVING;
1764  free_map(m);
1765  }
1766  /* move this out of free_map, since tmpname can still be needed if
1767  * the map is swapped out.
1768  */
1769  free(m->tmpname);
1770  m->tmpname = NULL;
1771  last = NULL;
1772  /* We need to look through all the maps and see if any maps
1773  * are pointing at this one for tiling information. Since
1774  * tiling can be assymetric, we just can not look to see which
1775  * maps this map tiles with and clears those.
1776  */
1777  for (tmp = first_map; tmp != NULL; tmp = tmp->next) {
1778  if (tmp->next == m)
1779  last = tmp;
1780 
1781  /* This should hopefully get unrolled on a decent compiler */
1782  for (i = 0; i < 4; i++)
1783  if (tmp->tile_map[i] == m)
1784  tmp->tile_map[i] = NULL;
1785  }
1786 
1787  /* If last is null, then this should be the first map in the list */
1788  if (!last) {
1789  if (m == first_map)
1790  first_map = m->next;
1791  else
1792  /* m->path is a static char, so should hopefully still have
1793  * some useful data in it.
1794  */
1795  LOG(llevError, "delete_map: Unable to find map %s in list\n", m->path);
1796  } else
1797  last->next = m->next;
1798 
1799  free(m);
1800 }
1801 
1815 mapstruct *ready_map_name(const char *name, int flags) {
1816  mapstruct *m;
1817 
1818  if (!name)
1819  return (NULL);
1820 
1821  /* Have we been at this level before? */
1822  m = has_been_loaded(name);
1823 
1824  /* Map is good to go, so just return it */
1825  if (m && (m->in_memory == MAP_LOADING || m->in_memory == MAP_IN_MEMORY)) {
1826  return m;
1827  }
1828 
1829  /* unique maps always get loaded from their original location, and never
1830  * a temp location. Likewise, if map_flush is set, or we have never loaded
1831  * this map, load it now. I removed the reset checking from here -
1832  * it seems the probability of a player trying to enter a map that should
1833  * reset but hasn't yet is quite low, and removing that makes this function
1834  * a bit cleaner (and players probably shouldn't rely on exact timing for
1835  * resets in any case - if they really care, they should use the 'maps command.
1836  */
1837  if ((flags&(MAP_FLUSH|MAP_PLAYER_UNIQUE)) || !m) {
1838  /* first visit or time to reset */
1839  if (m) {
1840  clean_tmp_map(m); /* Doesn't make much difference */
1841  delete_map(m);
1842  }
1843 
1844  m = mapfile_load(name, (flags&MAP_PLAYER_UNIQUE));
1845  if (m == NULL) return NULL;
1846 
1847  /* If a player unique map, no extra unique object file to load.
1848  * if from the editor, likewise.
1849  */
1850  if (!(flags&(MAP_FLUSH|MAP_PLAYER_UNIQUE)))
1852 
1853  if (!(flags&(MAP_FLUSH|MAP_PLAYER_UNIQUE|MAP_OVERLAY))) {
1854  if (load_overlay_map(name, m) != 0) {
1855  delete_map(m);
1856  m = mapfile_load(name, 0);
1857  if (m == NULL) {
1858  /* Really, this map is bad :( */
1859  return NULL;
1860  }
1861  }
1862  }
1863  } else {
1864  /* If in this loop, we found a temporary map, so load it up. */
1865 
1866  if (load_temporary_map(m) != 0) {
1867  /*
1868  * There was a failure loading the temporary map, fall back to original one.
1869  * load_temporary_map() already logged the error.
1870  */
1871  delete_map(m);
1872  m = mapfile_load(name, 0);
1873  if (m == NULL) {
1874  /* Really, this map is bad :( */
1875  return NULL;
1876  }
1877  }
1879 
1880  clean_tmp_map(m);
1881  m->in_memory = MAP_IN_MEMORY;
1882  /* tempnam() on sun systems (probably others) uses malloc
1883  * to allocated space for the string. Free it here.
1884  * In some cases, load_temporary_map above won't find the
1885  * temporary map, and so has reloaded a new map. If that
1886  * is the case, tmpname is now null
1887  */
1888  free(m->tmpname);
1889  m->tmpname = NULL;
1890  /* It's going to be saved anew anyway */
1891  }
1892 
1893  /* Below here is stuff common to both first time loaded maps and
1894  * temp maps.
1895  */
1896 
1897  decay_objects(m); /* start the decay */
1898 
1899  if (m->outdoor)
1900  set_darkness_map(m);
1901 
1902  if (!(flags&(MAP_FLUSH))) {
1903  if (m->last_reset_time == 0) {
1904  m->last_reset_time = seconds();
1905  }
1906  }
1907  return m;
1908 }
1909 
1926  archetype *at;
1927  int x, y;
1928  int diff = 0;
1929  int i;
1930  int64_t exp_pr_sq, total_exp = 0;
1931 
1932  if (MAP_DIFFICULTY(m)) {
1933  return MAP_DIFFICULTY(m);
1934  }
1935 
1936  for (x = 0; x < MAP_WIDTH(m); x++)
1937  for (y = 0; y < MAP_HEIGHT(m); y++)
1938  FOR_MAP_PREPARE(m, x, y, op) {
1939  if (QUERY_FLAG(op, FLAG_MONSTER))
1940  total_exp += op->stats.exp;
1941  if (QUERY_FLAG(op, FLAG_GENERATOR)) {
1942  total_exp += op->stats.exp;
1944  if (at != NULL)
1945  total_exp += at->clone.stats.exp*8;
1946  }
1947  } FOR_MAP_FINISH();
1948 
1949  exp_pr_sq = ((double)1000*total_exp)/(MAP_WIDTH(m)*MAP_HEIGHT(m)+1);
1950  diff = 20;
1951  for (i = 1; i < 20; i++)
1952  if (exp_pr_sq <= level_exp(i, 1.0)) {
1953  diff = i;
1954  break;
1955  }
1956 
1957  return diff;
1958 }
1959 
1967  if (m->tmpname == NULL)
1968  return;
1969  (void)unlink(m->tmpname);
1970 }
1971 
1975 void free_all_maps(void) {
1976  int real_maps = 0;
1977 
1978  while (first_map) {
1979  /* I think some of the callers above before it gets here set this to be
1980  * saving, but we still want to free this data
1981  */
1982  if (first_map->in_memory == MAP_SAVING)
1985  real_maps++;
1986  }
1987  LOG(llevDebug, "free_all_maps: Freed %d maps\n", real_maps);
1988 }
1989 
2007 int change_map_light(mapstruct *m, int change) {
2008  int new_level = m->darkness+change;
2009 
2010  /* Nothing to do */
2011  if (!change
2012  || (new_level <= 0 && m->darkness == 0)
2013  || (new_level >= MAX_DARKNESS && m->darkness >= MAX_DARKNESS)) {
2014  return 0;
2015  }
2016 
2017  /* inform all players on the map */
2018  if (change > 0)
2019  ext_info_map(NDI_BLACK, m, MSG_TYPE_MISC, MSG_SUBTYPE_NONE, "It becomes darker.");
2020  else
2021  ext_info_map(NDI_BLACK, m, MSG_TYPE_MISC, MSG_SUBTYPE_NONE, "It becomes brighter.");
2022 
2023  /* Do extra checking. since m->darkness is a unsigned value,
2024  * we need to be extra careful about negative values.
2025  * In general, the checks below are only needed if change
2026  * is not +/-1
2027  */
2028  if (new_level < 0)
2029  m->darkness = 0;
2030  else if (new_level >= MAX_DARKNESS)
2031  m->darkness = MAX_DARKNESS;
2032  else
2033  m->darkness = new_level;
2034 
2035  /* All clients need to get re-updated for the change */
2036  update_all_map_los(m);
2037  return 1;
2038 }
2039 
2062 static inline void add_face_layer(int low_layer, int high_layer, object *ob, object *layers[], int honor_visibility) {
2063  int l, l1;
2064  object *tmp;
2065 
2066  for (l = low_layer; l <= high_layer; l++) {
2067  if (!layers[l]) {
2068  /* found an empty spot. now, we want to make sure
2069  * highest visibility at top, etc.
2070  */
2071  layers[l] = ob;
2072  if (!honor_visibility)
2073  return;
2074 
2075  /* This is basically a mini bubble sort. Only swap
2076  * position if the lower face has greater (not equal)
2077  * visibility - map stacking is secondary consideration here.
2078  */
2079  for (l1 = (l-1); l1 >= low_layer; l1--) {
2080  if (layers[l1]->face->visibility > layers[l1+1]->face->visibility) {
2081  tmp = layers[l1+1];
2082  layers[l1+1] = layers[l1];
2083  layers[l1] = tmp;
2084  }
2085  }
2086  /* Nothing more to do - face inserted */
2087  return;
2088  }
2089  }
2090  /* If we get here, all the layers have an object..
2091  */
2092  if (!honor_visibility) {
2093  /* Basically, in this case, it is pure stacking logic, so
2094  * new object goes on the top.
2095  */
2096  for (l = low_layer; l < high_layer; l++)
2097  layers[l] = layers[l+1];
2098  layers[high_layer] = ob;
2099  /* If this object doesn't have higher visibility than
2100  * the lowest object, no reason to go further.
2101  */
2102  } else if (ob->face->visibility >= layers[low_layer]->face->visibility) {
2103  /*
2104  * Start at the top (highest visibility) layer and work down.
2105  * once this face exceed that of the layer, push down those
2106  * other layers, and then replace the layer with our object.
2107  */
2108  for (l = high_layer; l >= low_layer; l--) {
2109  if (ob->face->visibility >= layers[l]->face->visibility) {
2110  for (l1 = low_layer; l1 < l; l1++)
2111  layers[l1] = layers[l1+1];
2112  layers[l] = ob;
2113  break;
2114  }
2115  }
2116  }
2117 }
2118 
2131 void update_position(mapstruct *m, int x, int y) {
2132  object *player = NULL;
2133  uint8_t flags = 0, oldflags, light = 0;
2134  object *layers[MAP_LAYERS];
2135 
2136  MoveType move_block = 0, move_slow = 0, move_on = 0, move_off = 0, move_allow = 0;
2137 
2138  oldflags = GET_MAP_FLAGS(m, x, y);
2139  if (!(oldflags&P_NEED_UPDATE)) {
2140  LOG(llevDebug, "update_position called with P_NEED_UPDATE not set: %s (%d, %d)\n", m->path, x, y);
2141  return;
2142  }
2143 
2144  memset(layers, 0, MAP_LAYERS*sizeof(object *));
2145 
2146  FOR_MAP_PREPARE(m, x, y, tmp) {
2147  /* DMs just don't do anything when hidden, including no light. */
2148  if (QUERY_FLAG(tmp, FLAG_WIZ) && tmp->contr->hidden)
2149  continue;
2150 
2151  if (tmp->type == PLAYER)
2152  player = tmp;
2153 
2154  /* This could be made additive I guess (two lights better than
2155  * one). But if so, it shouldn't be a simple additive - 2
2156  * light bulbs do not illuminate twice as far as once since
2157  * it is a dissipation factor that is squared (or is it cubed?)
2158  */
2159  if (tmp->glow_radius > light)
2160  light = tmp->glow_radius;
2161 
2162  /* if this object is visible and not a blank face,
2163  * update the objects that show how this space
2164  * looks.
2165  */
2166  if (!tmp->invisible && tmp->face != blank_face) {
2167  if (tmp->map_layer) {
2168  add_face_layer(tmp->map_layer, map_layer_info[tmp->map_layer].high_layer,
2169  tmp, layers, map_layer_info[tmp->map_layer].honor_visibility);
2170  } else if (tmp->move_type&MOVE_FLYING) {
2171  add_face_layer(MAP_LAYER_FLY1, MAP_LAYER_FLY2, tmp, layers, 1);
2172  } else if ((tmp->type == PLAYER || QUERY_FLAG(tmp, FLAG_MONSTER))) {
2174  } else if (QUERY_FLAG(tmp, FLAG_IS_FLOOR)) {
2175  layers[MAP_LAYER_FLOOR] = tmp;
2176  /* floors hide everything else */
2177  memset(layers+1, 0, (MAP_LAYERS-1)*sizeof(object *));
2178  /* Check for FLAG_SEE_ANYWHERE is removed - objects
2179  * with that flag should just have a high visibility
2180  * set - we shouldn't need special code here.
2181  */
2182  } else if (QUERY_FLAG(tmp, FLAG_NO_PICK)) {
2184  } else {
2186  }
2187  }
2188  if (tmp == tmp->above) {
2189  LOG(llevError, "Error in structure of map\n");
2190  exit(-1);
2191  }
2192 
2193  move_slow |= tmp->move_slow;
2194  move_block |= tmp->move_block;
2195  move_on |= tmp->move_on;
2196  move_off |= tmp->move_off;
2197  move_allow |= tmp->move_allow;
2198 
2199  if (QUERY_FLAG(tmp, FLAG_ALIVE))
2200  flags |= P_IS_ALIVE;
2201  if (QUERY_FLAG(tmp, FLAG_NO_MAGIC))
2202  flags |= P_NO_MAGIC;
2203  if (QUERY_FLAG(tmp, FLAG_DAMNED))
2204  flags |= P_NO_CLERIC;
2205 
2206  if (QUERY_FLAG(tmp, FLAG_BLOCKSVIEW))
2207  flags |= P_BLOCKSVIEW;
2208  } FOR_MAP_FINISH(); /* for stack of objects */
2209 
2210  if (player)
2211  flags |= P_PLAYER;
2212 
2213  /* we don't want to rely on this function to have accurate flags, but
2214  * since we're already doing the work, we calculate them here.
2215  * if they don't match, logic is broken someplace.
2216  */
2217  if (((oldflags&~(P_NEED_UPDATE|P_NO_ERROR)) != flags)
2218  && (!(oldflags&P_NO_ERROR))) {
2219  LOG(llevDebug, "update_position: updated flags do not match old flags: %s (x=%d,y=%d) %x != %x\n",
2220  m->path, x, y, (oldflags&~P_NEED_UPDATE), flags);
2221  }
2222 
2223  SET_MAP_FLAGS(m, x, y, flags);
2224  SET_MAP_MOVE_BLOCK(m, x, y, move_block&~move_allow);
2225  SET_MAP_MOVE_ON(m, x, y, move_on);
2226  SET_MAP_MOVE_OFF(m, x, y, move_off);
2227  SET_MAP_MOVE_SLOW(m, x, y, move_slow);
2228  SET_MAP_LIGHT(m, x, y, light);
2229 
2230  /* Note that player may be NULL here, which is fine - if no player, need
2231  * to clear any value that may be set.
2232  */
2233  SET_MAP_PLAYER(m, x, y, player);
2234 
2235  /* Note it is intentional we copy everything, including NULL values. */
2236  memcpy(GET_MAP_FACE_OBJS(m, x, y), layers, sizeof(object *)*MAP_LAYERS);
2237 }
2238 
2246  int timeout;
2247 
2248  timeout = MAP_RESET_TIMEOUT(map);
2249  if (timeout <= 0)
2250  timeout = MAP_DEFAULTRESET;
2251  if (timeout >= MAP_MAXRESET)
2252  timeout = MAP_MAXRESET;
2253  MAP_WHEN_RESET(map) = seconds()+timeout;
2254 }
2255 
2270 static mapstruct *load_and_link_tiled_map(mapstruct *orig_map, int tile_num) {
2271  int dest_tile = (tile_num+2)%4;
2272  char path[HUGE_BUF];
2273 
2274  path_combine_and_normalize(orig_map->path, orig_map->tile_path[tile_num], path, sizeof(path));
2275 
2276  orig_map->tile_map[tile_num] = ready_map_name(path, 0);
2277  if (orig_map->tile_map[tile_num] == NULL) {
2278  return NULL;
2279  }
2280 
2281  /* need to do a strcmp here as the orig_map->path is not a shared string */
2282  if (orig_map->tile_map[tile_num]->tile_path[dest_tile]
2283  && !strcmp(orig_map->tile_map[tile_num]->tile_path[dest_tile], orig_map->path))
2284  orig_map->tile_map[tile_num]->tile_map[dest_tile] = orig_map;
2285 
2286  return orig_map->tile_map[tile_num];
2287 }
2288 
2306 int out_of_map(mapstruct *m, int x, int y) {
2307 
2308  /* Simple case - coordinates are within this local
2309  * map.
2310  */
2311  if (x >= 0 && x < MAP_WIDTH(m) && y >= 0 && y < MAP_HEIGHT(m))
2312  return 0;
2313 
2314  if (x < 0) {
2315  if (!m->tile_path[3])
2316  return 1;
2317  if (!m->tile_map[3] || m->tile_map[3]->in_memory != MAP_IN_MEMORY) {
2319  /* Verify the tile map loaded correctly */
2320  if (!m->tile_map[3])
2321  return 0;
2322  }
2323  return (out_of_map(m->tile_map[3], x+MAP_WIDTH(m->tile_map[3]), y));
2324  }
2325  if (x >= MAP_WIDTH(m)) {
2326  if (!m->tile_path[1])
2327  return 1;
2328  if (!m->tile_map[1] || m->tile_map[1]->in_memory != MAP_IN_MEMORY) {
2330  /* Verify the tile map loaded correctly */
2331  if (!m->tile_map[1])
2332  return 0;
2333  }
2334  return (out_of_map(m->tile_map[1], x-MAP_WIDTH(m), y));
2335  }
2336  if (y < 0) {
2337  if (!m->tile_path[0])
2338  return 1;
2339  if (!m->tile_map[0] || m->tile_map[0]->in_memory != MAP_IN_MEMORY) {
2341  /* Verify the tile map loaded correctly */
2342  if (!m->tile_map[0])
2343  return 0;
2344  }
2345  return (out_of_map(m->tile_map[0], x, y+MAP_HEIGHT(m->tile_map[0])));
2346  }
2347  if (y >= MAP_HEIGHT(m)) {
2348  if (!m->tile_path[2])
2349  return 1;
2350  if (!m->tile_map[2] || m->tile_map[2]->in_memory != MAP_IN_MEMORY) {
2352  /* Verify the tile map loaded correctly */
2353  if (!m->tile_map[2])
2354  return 0;
2355  }
2356  return (out_of_map(m->tile_map[2], x, y-MAP_HEIGHT(m)));
2357  }
2358  return 1;
2359 }
2360 
2381 
2382  /* Simple case - coordinates are within this local
2383  * map.
2384  */
2385 
2386  if (*x >= 0 && *x < MAP_WIDTH(m) && *y >= 0 && *y < MAP_HEIGHT(m))
2387  return m;
2388 
2389  do /* With the first case there, we can assume we are out of the map if we get here */
2390  {
2391  // Figure out what map should be in the direction we are off the map, and then
2392  // load that map and look again.
2393  if (*x < 0) {
2394  if (!m->tile_path[3])
2395  return NULL;
2396  if (!m->tile_map[3] || m->tile_map[3]->in_memory != MAP_IN_MEMORY){
2398  /* Make sure we loaded properly. */
2399  if (!m->tile_map[3])
2400  return NULL;
2401  }
2402  *x += MAP_WIDTH(m->tile_map[3]);
2403  m = m->tile_map[3];
2404  }
2405  else if (*x >= MAP_WIDTH(m)) {
2406  if (!m->tile_path[1])
2407  return NULL;
2408  if (!m->tile_map[1] || m->tile_map[1]->in_memory != MAP_IN_MEMORY){
2410  /* Make sure we loaded properly. */
2411  if (!m->tile_map[1])
2412  return NULL;
2413  }
2414  *x -= MAP_WIDTH(m);
2415  m = m->tile_map[1];
2416  }
2417  // It is possible that x and y be considered separate compare groups,
2418  // But using an else-if here retains the old behavior that recursion produced.
2419  else if (*y < 0) {
2420  if (!m->tile_path[0])
2421  return NULL;
2422  if (!m->tile_map[0] || m->tile_map[0]->in_memory != MAP_IN_MEMORY){
2424  /* Make sure we loaded properly. */
2425  if (!m->tile_map[0])
2426  return NULL;
2427  }
2428  *y += MAP_HEIGHT(m->tile_map[0]);
2429  m = m->tile_map[0];
2430  }
2431  else if (*y >= MAP_HEIGHT(m)) {
2432  if (!m->tile_path[2])
2433  return NULL;
2434  if (!m->tile_map[2] || m->tile_map[2]->in_memory != MAP_IN_MEMORY){
2436  /* Make sure we loaded properly. */
2437  if (!m->tile_map[2])
2438  return NULL;
2439  }
2440  *y -= MAP_HEIGHT(m);
2441  m = m->tile_map[2];
2442  }
2443  // The check here is if our single tile is in the map.
2444  // That is exactly what the OUT_OF_MAP macro does.
2445  } while (OUT_OF_REAL_MAP(m, *x, *y));
2446  return m; /* We have found our map */
2447 }
2448 
2462 static int adjacent_map(const mapstruct *map1, const mapstruct *map2, int *dx, int *dy) {
2463  if (!map1 || !map2)
2464  return 0;
2465 
2466  if (map1 == map2) {
2467  *dx = 0;
2468  *dy = 0;
2469  } else if (map1->tile_map[0] == map2) { /* up */
2470  *dx = 0;
2471  *dy = -MAP_HEIGHT(map2);
2472  } else if (map1->tile_map[1] == map2) { /* right */
2473  *dx = MAP_WIDTH(map1);
2474  *dy = 0;
2475  } else if (map1->tile_map[2] == map2) { /* down */
2476  *dx = 0;
2477  *dy = MAP_HEIGHT(map1);
2478  } else if (map1->tile_map[3] == map2) { /* left */
2479  *dx = -MAP_WIDTH(map2);
2480  *dy = 0;
2481  } else if (map1->tile_map[0] && map1->tile_map[0]->tile_map[1] == map2) { /* up right */
2482  *dx = MAP_WIDTH(map1->tile_map[0]);
2483  *dy = -MAP_HEIGHT(map1->tile_map[0]);
2484  } else if (map1->tile_map[0] && map1->tile_map[0]->tile_map[3] == map2) { /* up left */
2485  *dx = -MAP_WIDTH(map2);
2486  *dy = -MAP_HEIGHT(map1->tile_map[0]);
2487  } else if (map1->tile_map[1] && map1->tile_map[1]->tile_map[0] == map2) { /* right up */
2488  *dx = MAP_WIDTH(map1);
2489  *dy = -MAP_HEIGHT(map2);
2490  } else if (map1->tile_map[1] && map1->tile_map[1]->tile_map[2] == map2) { /* right down */
2491  *dx = MAP_WIDTH(map1);
2492  *dy = MAP_HEIGHT(map1->tile_map[1]);
2493  } else if (map1->tile_map[2] && map1->tile_map[2]->tile_map[1] == map2) { /* down right */
2494  *dx = MAP_WIDTH(map1->tile_map[2]);
2495  *dy = MAP_HEIGHT(map1);
2496  } else if (map1->tile_map[2] && map1->tile_map[2]->tile_map[3] == map2) { /* down left */
2497  *dx = -MAP_WIDTH(map2);
2498  *dy = MAP_HEIGHT(map1);
2499  } else if (map1->tile_map[3] && map1->tile_map[3]->tile_map[0] == map2) { /* left up */
2500  *dx = -MAP_WIDTH(map1->tile_map[3]);
2501  *dy = -MAP_HEIGHT(map2);
2502  } else if (map1->tile_map[3] && map1->tile_map[3]->tile_map[2] == map2) { /* left down */
2503  *dx = -MAP_WIDTH(map1->tile_map[3]);
2504  *dy = MAP_HEIGHT(map1->tile_map[3]);
2505  } else { /* not "adjacent" enough */
2506  return 0;
2507  }
2508 
2509  return 1;
2510 }
2511 
2539 int get_rangevector(object *op1, const object *op2, rv_vector *retval, int flags) {
2540  if (!adjacent_map(op1->map, op2->map, &retval->distance_x, &retval->distance_y)) {
2541  /* be conservative and fill in _some_ data */
2542  retval->distance = 100000;
2543  retval->distance_x = 32767;
2544  retval->distance_y = 32767;
2545  retval->direction = 0;
2546  retval->part = NULL;
2547  return 0;
2548  } else {
2549  object *best;
2550 
2551  retval->distance_x += op2->x-op1->x;
2552  retval->distance_y += op2->y-op1->y;
2553 
2554  best = op1;
2555  /* If this is multipart, find the closest part now */
2556  if (!(flags&0x1) && op1->more) {
2557  object *tmp;
2558  int best_distance = retval->distance_x*retval->distance_x+
2559  retval->distance_y*retval->distance_y, tmpi;
2560 
2561  /* we just take the offset of the piece to head to figure
2562  * distance instead of doing all that work above again
2563  * since the distance fields we set above are positive in the
2564  * same axis as is used for multipart objects, the simply arithmetic
2565  * below works.
2566  */
2567  for (tmp = op1->more; tmp != NULL; tmp = tmp->more) {
2568  tmpi = (op1->x-tmp->x+retval->distance_x)*(op1->x-tmp->x+retval->distance_x)+
2569  (op1->y-tmp->y+retval->distance_y)*(op1->y-tmp->y+retval->distance_y);
2570  if (tmpi < best_distance) {
2571  best_distance = tmpi;
2572  best = tmp;
2573  }
2574  }
2575  if (best != op1) {
2576  retval->distance_x += op1->x-best->x;
2577  retval->distance_y += op1->y-best->y;
2578  }
2579  }
2580  retval->part = best;
2581  retval->distance = isqrt(retval->distance_x*retval->distance_x+retval->distance_y*retval->distance_y);
2582  retval->direction = find_dir_2(-retval->distance_x, -retval->distance_y);
2583  return 1;
2584  }
2585 }
2586 
2612 int get_rangevector_from_mapcoord(const mapstruct *m, int x, int y, const object *op2, rv_vector *retval, int flags) {
2613  if (!adjacent_map(m, op2->map, &retval->distance_x, &retval->distance_y)) {
2614  /* be conservative and fill in _some_ data */
2615  retval->distance = 100000;
2616  retval->distance_x = 32767;
2617  retval->distance_y = 32767;
2618  retval->direction = 0;
2619  retval->part = NULL;
2620  return 0;
2621  } else {
2622  retval->distance_x += op2->x-x;
2623  retval->distance_y += op2->y-y;
2624 
2625  retval->part = NULL;
2626  retval->distance = isqrt(retval->distance_x*retval->distance_x+retval->distance_y*retval->distance_y);
2627  retval->direction = find_dir_2(-retval->distance_x, -retval->distance_y);
2628  return 1;
2629  }
2630 }
2631 
2650 int on_same_map(const object *op1, const object *op2) {
2651  int dx, dy;
2652 
2653  return adjacent_map(op1->map, op2->map, &dx, &dy);
2654 }
2655 
2673 object *map_find_by_flag(mapstruct *map, int x, int y, int flag) {
2674  object *tmp;
2675 
2676  for (tmp = GET_MAP_OB(map, x, y); tmp != NULL; tmp = tmp->above) {
2677  object *head;
2678 
2679  head = HEAD(tmp);
2680  if (QUERY_FLAG(head, flag))
2681  return head;
2682  }
2683  return NULL;
2684 }
2685 
2691  char base[HUGE_BUF], path[HUGE_BUF];
2692  int count;
2693 
2694  if (map->unique) {
2695  /* Unique maps have their full path already set. */
2696  unlink(map->path);
2697  return;
2698  }
2699 
2700  create_items_path(map->path, base, sizeof(base));
2701 
2702  for (count = 0; count < 10; count++) {
2703  snprintf(path, sizeof(path), "%s.v%02d", base, count);
2704  unlink(path);
2705  }
2706 }
2707 
2713 const char *map_get_path(const object *item) {
2714  if (item->map != NULL) {
2715  if (strlen(item->map->path) > 0) {
2716  return item->map->path;
2717  }
2718 
2719  return item->map->name ? item->map->name : "(empty path and name)";
2720  }
2721 
2722  if (item->env != NULL)
2723  return map_get_path(item->env);
2724 
2725  return "(no map and no env!)";
2726 }
#define FLAG_NO_MAGIC
Definition: define.h:276
char path[HUGE_BUF]
Definition: map.h:365
int find_dir_2(int x, int y)
Definition: object.c:3615
void free_all_maps(void)
Definition: map.c:1975
#define MSG_TYPE_MISC
Definition: newclient.h:389
Definition: player.h:92
double shopgreed
Definition: map.h:358
#define FLAG_DAMNED
Definition: define.h:318
mapstruct * mapfile_load(const char *map, int flags)
Definition: map.c:1235
#define FLAG_IS_FLOOR
Definition: define.h:303
#define FLAG_UNPAID
Definition: define.h:236
MapSpace * spaces
Definition: map.h:355
static void allocate_map(mapstruct *m)
Definition: map.c:842
#define MAP_STYLE
Definition: map.h:98
#define FLAG_IS_LINKED
Definition: define.h:316
MoveType move_type
Definition: object.h:424
#define S_IRGRP
Definition: win32.h:88
mapstruct * get_linked_map(void)
Definition: map.c:791
Definition: map.h:380
int64_t level_exp(int level, double expmul)
Definition: living.c:1848
#define P_PLAYER
Definition: map.h:236
#define SAVE_ERROR_OK
Definition: map.h:143
static mapstruct * load_and_link_tiled_map(mapstruct *orig_map, int tile_num)
Definition: map.c:2270
void of_cancel(OutputFile *of)
Definition: output_file.c:89
#define P_NEED_UPDATE
Definition: map.h:239
#define MAP_FLUSH
Definition: map.h:96
mapstruct * get_empty_map(int sizex, int sizey)
Definition: map.c:872
#define SET_FLAG(xyz, p)
Definition: define.h:223
int32_t swap_time
Definition: map.h:342
region * get_region_by_name(const char *region_name)
Definition: region.c:46
unsigned char uint8_t
Definition: win32.h:161
int get_rangevector_from_mapcoord(const mapstruct *m, int x, int y, const object *op2, rv_vector *retval, int flags)
Definition: map.c:2612
void make_path_to_file(const char *filename)
Definition: porting.c:312
object * part
Definition: map.h:385
const char * name_pl
Definition: define.h:92
#define strdup_local
Definition: compat.h:25
static int save_objects(mapstruct *m, FILE *fp, FILE *fp2, int flag)
Definition: map.c:746
uint32_t map_size(mapstruct *m)
Definition: map.c:828
#define MAP_LAYER_ITEM1
Definition: map.h:43
#define P_NO_MAGIC
Definition: map.h:227
uint32_t nosmooth
Definition: map.h:339
static const Map_Layer_Info map_layer_info[MAP_LAYERS]
Definition: map.c:61
void fatal(enum fatal_error err)
Definition: utils.c:597
struct mapdef * tile_map[4]
Definition: map.h:364
char * create_pathname(const char *name, char *buf, size_t size)
Definition: map.c:102
uint32_t in_memory
Definition: map.h:345
mapstruct * ready_map_name(const char *name, int flags)
Definition: map.c:1815
unsigned char MoveType
Definition: define.h:432
#define LO_REPEAT
Definition: loader.h:15
#define MAP_ENTER_X(m)
Definition: map.h:85
void free_string(sstring str)
Definition: shstr.c:280
#define MAP_DEFAULTRESET
Definition: config.h:428
signed long object_sum_weight(object *op)
Definition: object.c:342
static shopitems * parse_shop_string(const char *input_string, const mapstruct *map)
Definition: map.c:894
#define FMT64U
Definition: compat.h:13
#define HUGE_BUF
Definition: define.h:37
void object_fix_multipart(object *tmp)
Definition: object.c:4582
#define MAP_MAXRESET
Definition: config.h:426
mapstruct * get_map_from_coord(mapstruct *m, int16_t *x, int16_t *y)
Definition: map.c:2380
#define S_IWOTH
Definition: win32.h:76
#define SET_MAP_LIGHT(M, X, Y, L)
Definition: map.h:167
uint32_t is_template
Definition: map.h:338
#define MAP_HEIGHT(m)
Definition: map.h:80
object clone
Definition: object.h:470
int calculate_difficulty(mapstruct *m)
Definition: map.c:1925
#define MAX_DARKNESS
Definition: define.h:470
int isqrt(int n)
Definition: utils.c:586
struct Map_Layer_Info Map_Layer_Info
void decay_objects(mapstruct *m)
Definition: utils.c:175
int64_t exp
Definition: living.h:47
#define MAP_LAYER_FLOOR
Definition: map.h:40
struct obj * above
Definition: object.h:288
const char * name
Definition: map.h:306
char * maplore
Definition: map.h:362
#define OUT_OF_REAL_MAP(M, X, Y)
Definition: map.h:217
#define SAVE_MODE_NORMAL
Definition: map.h:120
const typedata * get_typedata_by_name(const char *name)
Definition: item.c:355
#define NDI_BLACK
Definition: newclient.h:221
void strip_endline(char *buf)
Definition: utils.c:341
void free_map(mapstruct *m)
Definition: map.c:1706
#define safe_strncpy
Definition: compat.h:23
static int load_temporary_map(mapstruct *m)
Definition: map.c:1293
int distance_y
Definition: map.h:383
void draw_ext_info(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *message)
Definition: main.c:310
Definition: object.h:465
const char * map_get_path(const object *item)
Definition: map.c:2713
#define FOR_OB_AND_BELOW_FINISH()
Definition: define.h:791
int change_map_light(mapstruct *m, int change)
Definition: map.c:2007
#define FLAG_REMOVED
Definition: define.h:232
#define MAP_WHEN_RESET(m)
Definition: map.h:62
int typenum
Definition: map.h:308
#define CALLOC(x, y)
Definition: compat.h:27
#define MAP_LAYER_LIVING2
Definition: map.h:47
#define SAVE_ERROR_WRITE
Definition: map.h:146
#define NDI_NAVY
Definition: newclient.h:223
const typedata * get_typedata(int itemtype)
Definition: item.c:335
uint16_t height
Definition: map.h:347
#define MOVE_ALL
Definition: define.h:413
char * name
Definition: map.h:328
#define SAVE_FLAG_SAVE_UNPAID
Definition: map.h:110
#define MAP_IN_MEMORY
Definition: map.h:130
void object_free_drop_inventory(object *ob)
Definition: object.c:1389
#define MAP_LAYER_NO_PICK1
Definition: map.h:41
const char * title
Definition: object.h:317
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
static int load_map_header(FILE *fp, mapstruct *m)
Definition: map.c:1025
char * name
Definition: map.h:278
#define FLAG_ALIVE
Definition: define.h:230
#define S_IROTH
Definition: win32.h:85
#define LL_NORMAL
Definition: loader.h:12
object * object_new(void)
Definition: object.c:1068
#define MAP_OVERLAY
Definition: map.h:99
#define FLAG_OBJ_ORIGINAL
Definition: define.h:365
#define MAP_SWAPPED
Definition: map.h:131
int index
Definition: map.h:311
object * object_insert_in_ob(object *op, object *where)
Definition: object.c:2705
#define INS_ON_TOP
Definition: object.h:569
int blocked_link(object *ob, mapstruct *m, int sx, int sy)
Definition: map.c:344
signed short int16_t
Definition: win32.h:160
#define LO_NEWFILE
Definition: loader.h:17
int16_t enter_x
Definition: map.h:348
uint32_t unique
Definition: map.h:337
const char * tmpdir
Definition: global.h:251
#define LL_MORE
Definition: loader.h:11
uint8_t honor_visibility
Definition: map.c:52
int8_t strength
Definition: map.h:309
struct mapdef * map
Definition: object.h:297
#define SET_MAP_MOVE_SLOW(M, X, Y, C)
Definition: map.h:199
#define snprintf
Definition: win32.h:46
uint64_t shopmin
Definition: map.h:359
int ob_blocked(const object *ob, mapstruct *m, int16_t x, int16_t y)
Definition: map.c:487
static void link_multipart_objects(mapstruct *m)
Definition: map.c:608
#define MAP_TIMEOUT(m)
Definition: map.h:66
void dump_all_maps(void)
Definition: map.c:268
char * path_combine_and_normalize(const char *src, const char *dst, char *path, size_t size)
Definition: path.c:172
#define MOVE_FLYING
Definition: define.h:410
#define FOR_INV_FINISH()
Definition: define.h:714
char * msg
Definition: map.h:361
void object_remove_from_active_list(object *op)
Definition: object.c:1215
void update_position(mapstruct *m, int x, int y)
Definition: map.c:2131
#define S_IWGRP
Definition: win32.h:79
#define MAP_LAYERS
Definition: map.h:32
#define R_OK
Definition: win32.h:64
#define SET_MAP_PLAYER(M, X, Y, C)
Definition: map.h:169
int of_close(OutputFile *of)
Definition: output_file.c:61
object * objects
Definition: object.c:60
void remove_button_link(object *op)
Definition: button.c:698
#define FLAG_BLOCKSVIEW
Definition: define.h:269
const char * name
Definition: object.h:311
long seconds(void)
Definition: time.c:344
struct obj * env
Definition: object.h:293
uint8_t visibility
Definition: face.h:17
int distance_x
Definition: map.h:382
int save_object(FILE *fp, object *op, int flag)
Definition: object.c:5268
struct archt * more
Definition: object.h:469
int execute_global_event(int eventcode,...)
Definition: main.c:369
char * tile_path[4]
Definition: map.h:363
#define INS_NO_WALK_ON
Definition: object.h:568
#define MAP_LAYER_ITEM3
Definition: map.h:45
uint32_t fixed_resettime
Definition: map.h:335
#define MAP_ENTER_Y(m)
Definition: map.h:87
#define OB_TYPE_MOVE_BLOCK(ob1, type)
Definition: define.h:447
#define FLAG_OVERLAY_FLOOR
Definition: define.h:255
#define P_OUT_OF_MAP
Definition: map.h:251
#define SAVE_ERROR_NO_PATH
Definition: map.h:147
#define GET_MAP_MOVE_BLOCK(M, X, Y)
Definition: map.h:192
uint8_t darkness
Definition: map.h:346
const char * templatedir
Definition: global.h:250
#define MAP_DIFFICULTY(m)
Definition: map.h:65
#define SAVE_ERROR_URENAME
Definition: map.h:148
static void delete_unique_items(mapstruct *m)
Definition: map.c:1364
#define MAP_SAVING
Definition: map.h:133
void update_buttons(mapstruct *m)
Definition: button.c:232
#define SET_MAP_MOVE_OFF(M, X, Y, C)
Definition: map.h:209
long last_reset_time
Definition: map.h:366
int on_same_map(const object *op1, const object *op2)
Definition: map.c:2650
#define QUERY_FLAG(xyz, p)
Definition: define.h:225
#define CLEAR_FLAG(xyz, p)
Definition: define.h:224
#define HEAD(op)
Definition: object.h:592
#define INS_ABOVE_FLOOR_ONLY
Definition: object.h:567
#define EVENT_MAPLOAD
Definition: plugin.h:99
#define FLAG_WIZ
Definition: define.h:231
#define SAVE_ERROR_CLOSE
Definition: map.h:149
void dump_map(const mapstruct *m)
Definition: map.c:245
#define MAX_BUF
Definition: define.h:35
#define MSG_TYPE_ATTACK
Definition: newclient.h:385
Definition: object.h:126
oblinkpt * buttons
Definition: map.h:354
int16_t enter_y
Definition: map.h:348
#define GENERATE_TYPE(xyz)
Definition: define.h:452
int16_t x
Definition: object.h:326
#define EVENT_MAPUNLOAD
Definition: plugin.h:98
const char *const map_layer_name[MAP_LAYERS]
Definition: map.c:44
#define MAP_LAYER_FLY1
Definition: map.h:48
uint16_t difficulty
Definition: map.h:343
signed __int64 int64_t
Definition: win32.h:168
char * tempnam(const char *dir, const char *pfx)
Definition: porting.c:52
static const flag_definition flags[]
#define FOR_MAP_FINISH()
Definition: define.h:767
struct obj * next
Definition: object.h:277
const char * uniquedir
Definition: global.h:249
#define MAP_LAYER_NO_PICK2
Definition: map.h:42
static int adjacent_map(const mapstruct *map1, const mapstruct *map2, int *dx, int *dy)
Definition: map.c:2462
char * tmpname
Definition: map.h:327
#define SAVE_FLAG_NO_REMOVE
Definition: map.h:111
int save_map(mapstruct *m, int flag)
Definition: map.c:1440
uint8_t high_layer
Definition: map.c:51
#define SAVE_ERROR_UCREATION
Definition: map.h:145
int32_t timeout
Definition: map.h:341
#define GET_MAP_FLAGS(M, X, Y)
Definition: map.h:161
unsigned int uint32_t
Definition: win32.h:162
Definition: object.h:107
const char * datadir
Definition: global.h:242
object * check_inv_recursive(object *op, const object *trig)
Definition: button.c:786
static void free_all_objects(mapstruct *m)
Definition: map.c:1659
#define SET_MAP_MOVE_ON(M, X, Y, C)
Definition: map.h:204
void set_map_reset_time(mapstruct *map)
Definition: map.c:2245
#define SET_MAP_FLAGS(M, X, Y, C)
Definition: map.h:163
#define P_NEW_MAP
Definition: map.h:252
#define FLAG_GENERATOR
Definition: define.h:248
#define MAP_LAYER_LIVING1
Definition: map.h:46
uint32_t outdoor
Definition: map.h:340
#define INS_NO_MERGE
Definition: object.h:566
static void load_unique_objects(mapstruct *m)
Definition: map.c:1389
Definition: map.h:258
const char * name
Definition: define.h:91
void update_all_map_los(mapstruct *map)
Definition: los.c:504
object * map_find_by_flag(mapstruct *map, int x, int y, int flag)
Definition: map.c:2673
New_Face * blank_face
Definition: image.c:39
#define MAP_PLAYER_UNIQUE
Definition: map.h:97
const char * localdir
Definition: global.h:243
archetype * get_archetype_by_type_subtype(int type, int subtype)
Definition: arch.c:136
const char * name_pl
Definition: map.h:307
living stats
Definition: object.h:368
int load_object(FILE *fp, object *op, int bufstate, int map_flags)
mapstruct * has_been_loaded(const char *name)
Definition: map.c:77
struct archt * arch
Definition: object.h:412
#define AB_NO_PASS
Definition: map.h:235
#define MAP_WIDTH(m)
Definition: map.h:78
#define unlink(__a)
Definition: win32.h:56
const char * mapdir
Definition: global.h:245
uint32_t reset_timeout
Definition: map.h:333
static void print_shop_string(mapstruct *m, char *output_string, int size)
Definition: map.c:980
uint64_t shopmax
Definition: map.h:360
uint16_t width
Definition: map.h:347
unsigned int distance
Definition: map.h:381
uint8_t type
Definition: object.h:338
struct Settings settings
Definition: init.c:40
static void load_objects(mapstruct *m, FILE *fp, int mapflags)
Definition: map.c:636
FILE * of_open(OutputFile *of, const char *fname)
Definition: output_file.c:30
void fix_container(object *container)
Definition: map.c:542
int direction
Definition: map.h:384
#define SAVE_MODE
Definition: config.h:607
Definition: map.h:305
#define TEMP_EXT
int out_of_map(mapstruct *m, int x, int y)
Definition: map.c:2306
void delete_map(mapstruct *m)
Definition: map.c:1753
void apply_auto_fix(mapstruct *m)
Definition: main.c:261
#define MAP_RESET_TIMEOUT(m)
Definition: map.h:64
#define MAP_LOADING
Definition: map.h:132
#define FOR_OB_AND_BELOW_PREPARE(op_)
Definition: define.h:787
#define GET_MAP_FACE_OBJS(M, X, Y)
Definition: map.h:189
static void create_items_path(const char *s, char *buf, size_t size)
Definition: map.c:166
sstring add_string(const char *str)
Definition: shstr.c:124
#define GET_MAP_OB(M, X, Y)
Definition: map.h:172
int check_path(const char *name, int prepend_dir)
Definition: map.c:201
int8_t glow_radius
Definition: object.h:364
#define FLAG_MONSTER
Definition: define.h:245
static void fix_container_multipart(object *container)
Definition: map.c:567
int get_map_flags(mapstruct *oldmap, mapstruct **newmap, int16_t x, int16_t y, int16_t *nx, int16_t *ny)
Definition: map.c:300
struct obj * inv
Definition: object.h:290
#define NDI_UNIQUE
Definition: newclient.h:245
#define OB_MOVE_BLOCK(ob1, ob2)
Definition: define.h:438
struct obj * head
Definition: object.h:296
#define SAVE_ERROR_RRENAME
Definition: map.h:150
#define MSG_SUBTYPE_NONE
Definition: newclient.h:398
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
uint8_t flags
Definition: map.h:262
#define SAVE_ERROR_RCREATION
Definition: map.h:144
#define LO_NOREAD
Definition: loader.h:18
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Definition: define.h:760
struct shopitem * shopitems
Definition: map.h:356
#define S_IRUSR
Definition: win32.h:91
#define S_IWUSR
Definition: win32.h:82
#define MSG_TYPE_ATTACK_NOKEY
Definition: newclient.h:612
struct mapdef * next
Definition: map.h:326
#define P_BLOCKSVIEW
Definition: map.h:226
#define P_NO_ERROR
Definition: map.h:240
void set_darkness_map(mapstruct *m)
Definition: main.c:348
int number
Definition: define.h:90
char * shoprace
Definition: map.h:357
#define MAP_LAYER_FLY2
Definition: map.h:49
void create_overlay_pathname(const char *name, char *buf, size_t size)
Definition: map.c:123
Definition: map.h:325
char * background_music
Definition: map.h:367
#define P_IS_ALIVE
Definition: map.h:237
const New_Face * face
Definition: object.h:332
#define FREE_AND_CLEAR(xyz)
Definition: global.h:201
#define FLAG_UNIQUE
Definition: define.h:288
#define FLAG_NO_PICK
Definition: define.h:239
#define SET_MAP_MOVE_BLOCK(M, X, Y, C)
Definition: map.h:194
static int load_overlay_map(const char *filename, mapstruct *m)
Definition: map.c:1329
int get_rangevector(object *op1, const object *op2, rv_vector *retval, int flags)
Definition: map.c:2539
void ext_info_map(int color, const mapstruct *map, uint8_t type, uint8_t subtype, const char *str1)
Definition: main.c:325
#define S_ISREG(x)
Definition: win32.h:70
void create_template_pathname(const char *name, char *buf, size_t size)
Definition: map.c:143
void map_remove_unique_files(const mapstruct *map)
Definition: map.c:2690
struct regiondef * region
Definition: map.h:329
void clean_tmp_map(mapstruct *m)
Definition: map.c:1966
EXTERN mapstruct * first_map
Definition: global.h:118
struct obj * more
Definition: object.h:295
object * arch_to_object(archetype *at)
Definition: arch.c:569
void clean_object(object *op)
Definition: map.c:1643
object * object_get_owner(object *op)
Definition: object.c:590
#define SAVE_MODE_OVERLAY
Definition: map.h:122
#define P_NO_CLERIC
Definition: map.h:238
#define FOR_INV_PREPARE(op_, it_)
Definition: define.h:707
void object_remove(object *op)
Definition: object.c:1669
static bool in_localdir(const char path[static 1])
Definition: map.c:1421
static void add_face_layer(int low_layer, int high_layer, object *ob, object *layers[], int honor_visibility)
Definition: map.c:2062
#define INS_MAP_LOAD
Definition: object.h:571