Crossfire Server, Trunk  R21189
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 #define PROFILE_BEGIN(expr) { \
38  struct timespec _begin, _end; \
39  clock_gettime(CLOCK_MONOTONIC, &_begin); \
40  expr;
41 
42 #define PROFILE_END(var, expr) \
43  clock_gettime(CLOCK_MONOTONIC, &_end); \
44  long var = timespec_diff(&_end, &_begin); \
45  expr; }
46 
47 static void free_all_objects(mapstruct *m);
48 
54 const char *const map_layer_name[MAP_LAYERS] = {
55  "floor", "no_pick", "no_pick", "item", "item",
56  "item", "living", "living", "fly", "fly"
57 };
58 
60 typedef struct Map_Layer_Info {
64 
72  { MAP_LAYER_FLOOR, 1 },
74  { MAP_LAYER_ITEM3, 1 }, { MAP_LAYER_ITEM3, 1 }, { MAP_LAYER_ITEM3, 1 },
75  { MAP_LAYER_LIVING2, 1 }, { MAP_LAYER_LIVING2, 1 },
76  { MAP_LAYER_FLY2, 1 }, { MAP_LAYER_FLY2, 1 }
77 };
78 
87 mapstruct *has_been_loaded(const char *name) {
88  mapstruct *map;
89 
90  if (!name || !*name)
91  return NULL;
92 
93  for (map = first_map; map; map = map->next)
94  if (!strcmp(name, map->path))
95  break;
96  return (map);
97 }
98 
112 char *create_pathname(const char *name, char *buf, size_t size) {
113  /* Why? having extra / doesn't confuse unix anyplace? Dependancies
114  * someplace else in the code? msw 2-17-97
115  */
116  if (*name == '/')
117  snprintf(buf, size, "%s/%s%s", settings.datadir, settings.mapdir, name);
118  else
119  snprintf(buf, size, "%s/%s/%s", settings.datadir, settings.mapdir, name);
120  return buf;
121 }
122 
133 void create_overlay_pathname(const char *name, char *buf, size_t size) {
134  /* Why? having extra / doesn't confuse unix anyplace? Dependancies
135  * someplace else in the code? msw 2-17-97
136  */
137  if (*name == '/')
138  snprintf(buf, size, "%s/%s%s", settings.localdir, settings.mapdir, name);
139  else
140  snprintf(buf, size, "%s/%s/%s", settings.localdir, settings.mapdir, name);
141 }
142 
153 void create_template_pathname(const char *name, char *buf, size_t size) {
154  /* Why? having extra / doesn't confuse unix anyplace? Dependancies
155  * someplace else in the code? msw 2-17-97
156  */
157  if (*name == '/')
158  snprintf(buf, size, "%s/%s%s", settings.localdir, settings.templatedir, name);
159  else
160  snprintf(buf, size, "%s/%s/%s", settings.localdir, settings.templatedir, name);
161 }
162 
176 static void create_items_path(const char *s, char *buf, size_t size) {
177  char *t;
178 
179  if (*s == '/')
180  s++;
181 
182  snprintf(buf, size, "%s/%s/", settings.localdir, settings.uniquedir);
183  t = buf+strlen(buf);
184  snprintf(t, buf+size-t, "%s", s);
185 
186  while (*t != '\0') {
187  if (*t == '/')
188  *t = '@';
189  t++;
190  }
191 }
192 
211 int check_path(const char *name, int prepend_dir) {
212  char buf[MAX_BUF];
213 #ifndef WIN32
214  struct stat statbuf;
215  int mode = 0;
216 #endif
217 
218  if (prepend_dir)
219  create_pathname(name, buf, MAX_BUF);
220  else
221  strlcpy(buf, name, sizeof(buf));
222 #ifdef WIN32 /* ***win32: check this sucker in windows style. */
223  return(_access(buf, 0));
224 #else
225 
226  if (stat(buf, &statbuf) != 0)
227  return -1;
228 
229  if (!S_ISREG(statbuf.st_mode))
230  return (-1);
231 
232  if (((statbuf.st_mode&S_IRGRP) && getegid() == statbuf.st_gid)
233  || ((statbuf.st_mode&S_IRUSR) && geteuid() == statbuf.st_uid)
234  || (statbuf.st_mode&S_IROTH))
235  mode |= 4;
236 
237  if ((statbuf.st_mode&S_IWGRP && getegid() == statbuf.st_gid)
238  || (statbuf.st_mode&S_IWUSR && geteuid() == statbuf.st_uid)
239  || (statbuf.st_mode&S_IWOTH))
240  mode |= 2;
241 
242  return (mode);
243 #endif
244 }
245 
255 void dump_map(const mapstruct *m) {
256  LOG(llevError, "Map %s status: %d.\n", m->path, m->in_memory);
257  LOG(llevError, "Size: %dx%d Start: %d,%d\n", MAP_WIDTH(m), MAP_HEIGHT(m), MAP_ENTER_X(m), MAP_ENTER_Y(m));
258 
259  if (m->msg != NULL)
260  LOG(llevError, "Message:\n%s", m->msg);
261 
262  if (m->maplore != NULL)
263  LOG(llevError, "Lore:\n%s", m->maplore);
264 
265  if (m->tmpname != NULL)
266  LOG(llevError, "Tmpname: %s\n", m->tmpname);
267 
268  LOG(llevError, "Difficulty: %d\n", m->difficulty);
269  LOG(llevError, "Darkness: %d\n", m->darkness);
270 }
271 
278 void dump_all_maps(void) {
279  mapstruct *m;
280 
281  for (m = first_map; m != NULL; m = m->next) {
282  dump_map(m);
283  }
284 }
285 
310 int get_map_flags(mapstruct *oldmap, mapstruct **newmap, int16_t x, int16_t y, int16_t *nx, int16_t *ny) {
311  int retval = 0;
312  mapstruct *mp;
313 
314  /*
315  * Since x and y are copies of the original values, we can directly
316  * mess with them here.
317  */
318  mp = get_map_from_coord(oldmap, &x, &y);
319  if (!mp)
320  return P_OUT_OF_MAP;
321  if (mp != oldmap)
322  retval |= P_NEW_MAP;
323  if (newmap)
324  *newmap = mp;
325  if (nx)
326  *nx = x;
327  if (ny)
328  *ny = y;
329  retval |= mp->spaces[x+mp->width*y].flags;
330  return retval;
331 }
332 
354 int blocked_link(object *ob, mapstruct *m, int sx, int sy) {
355  object *tmp_head;
356  int mflags, blocked;
357 
358  /* Make sure the coordinates are valid - they should be, as caller should
359  * have already checked this.
360  */
361  if (OUT_OF_REAL_MAP(m, sx, sy)) {
362  LOG(llevError, "blocked_link: Passed map, x, y coordinates outside of map\n");
363  return 1;
364  }
365 
366  /* special hack for transports: if it's a transport with a move_type of 0, it can do on the space anyway */
367  if (ob->type == TRANSPORT && ob->move_type == 0)
368  return 0;
369 
370  /* Save some cycles - instead of calling get_map_flags(), just get the value
371  * directly.
372  */
373  mflags = m->spaces[sx+m->width*sy].flags;
374 
375  blocked = GET_MAP_MOVE_BLOCK(m, sx, sy);
376 
377  /* If space is currently not blocked by anything, no need to
378  * go further. Not true for players - all sorts of special
379  * things we need to do for players.
380  */
381  if (ob->type != PLAYER && !(mflags&P_IS_ALIVE) && (blocked == 0))
382  return 0;
383 
384  /* if there isn't anytyhing alive on this space, and this space isn't
385  * otherwise blocked, we can return now. Only if there is a living
386  * creature do we need to investigate if it is part of this creature
387  * or another. Likewise, only if something is blocking us do we
388  * need to investigate if there is a special circumstance that would
389  * let the player through (inventory checkers for example)
390  */
391  if (!(mflags&P_IS_ALIVE) && !OB_TYPE_MOVE_BLOCK(ob, blocked))
392  return 0;
393 
394  ob = HEAD(ob);
395 
396  /* We basically go through the stack of objects, and if there is
397  * some other object that has NO_PASS or FLAG_ALIVE set, return
398  * true. If we get through the entire stack, that must mean
399  * ob is blocking it, so return 0.
400  */
401  FOR_MAP_PREPARE(m, sx, sy, tmp) {
402  /* Never block part of self. */
403  tmp_head = HEAD(tmp);
404  if (tmp_head == ob)
405  continue;
406  /* This must be before the checks below. Code for inventory checkers. */
407  if (tmp->type == CHECK_INV && OB_MOVE_BLOCK(ob, tmp)) {
408  /* If last_sp is set, the player/monster needs an object,
409  * so we check for it. If they don't have it, they can't
410  * pass through this space.
411  */
412  if (tmp->last_sp) {
413  if (check_inv_recursive(ob, tmp) == NULL) {
414  if (tmp->msg) {
415  /* Optionally display the reason why one cannot move
416  * there. Note: emitting a message from this function
417  * is not very elegant. Ideally, this should be done
418  * somewhere in server/player.c, but this is difficult
419  * for objects of type CHECK_INV that are not alive.
420  */
423  tmp->msg);
424  }
425  return 1;
426  }
427  } else {
428  /* In this case, the player must not have the object -
429  * if they do, they can't pass through.
430  */
431  if (check_inv_recursive(ob, tmp) != NULL) {
432  if (tmp->msg) {
435  tmp->msg);
436  }
437  return 1;
438  }
439  }
440  } /* if check_inv */
441  else {
442  /* Broke apart a big nasty if into several here to make
443  * this more readable. first check - if the space blocks
444  * movement, can't move here.
445  * second - if a monster, can't move there, unless it is a
446  * hidden dm
447  */
448  if (OB_MOVE_BLOCK(ob, tmp))
449  return 1;
450  if (QUERY_FLAG(tmp, FLAG_ALIVE)
451  && tmp->head != ob
452  && tmp != ob
453  && tmp->type != DOOR
454  && !(QUERY_FLAG(tmp, FLAG_WIZ) && tmp->contr->hidden))
455  return 1;
456  }
457  } FOR_MAP_FINISH();
458  return 0;
459 }
460 
497 int ob_blocked(const object *ob, mapstruct *m, int16_t x, int16_t y) {
498  archetype *tmp;
499  int flag;
500  mapstruct *m1;
501  int16_t sx, sy;
502  const object *part;
503 
504  if (ob == NULL) {
505  flag = get_map_flags(m, &m1, x, y, &sx, &sy);
506  if (flag&P_OUT_OF_MAP)
507  return P_OUT_OF_MAP;
508 
509  /* don't have object, so don't know what types would block */
510  return(GET_MAP_MOVE_BLOCK(m1, sx, sy));
511  }
512 
513  for (tmp = ob->arch, part = ob; tmp != NULL; tmp = tmp->more, part = part->more) {
514  flag = get_map_flags(m, &m1, x+tmp->clone.x, y+tmp->clone.y, &sx, &sy);
515 
516  if (flag&P_OUT_OF_MAP)
517  return P_OUT_OF_MAP;
518  if (flag&P_IS_ALIVE)
519  return P_IS_ALIVE;
520 
521  /* object_find_first_free_spot() calls this function. However, often
522  * ob doesn't have any move type (when used to place exits)
523  * so the AND operation in OB_TYPE_MOVE_BLOCK doesn't work.
524  */
525 
526  if (ob->move_type == 0 && GET_MAP_MOVE_BLOCK(m1, sx, sy) != MOVE_ALL)
527  continue;
528 
529  /* A transport without move_type for a part should go through everything for that part. */
530  if (ob->type == TRANSPORT && part->move_type == 0)
531  continue;
532 
533  /* Note it is intentional that we check ob - the movement type of the
534  * head of the object should correspond for the entire object.
535  */
536  if (OB_TYPE_MOVE_BLOCK(ob, GET_MAP_MOVE_BLOCK(m1, sx, sy)))
537  return AB_NO_PASS;
538  }
539  return 0;
540 }
541 
552 static void fix_container_multipart(object *container) {
553  FOR_INV_PREPARE(container, tmp) {
554  archetype *at;
555  object *op, *last;
556 
557  if (tmp->inv)
559  /* already multipart, or non-multipart arch - don't do anything more */
560  for (at = tmp->arch->more, last = tmp; at != NULL; at = at->more, last = op) {
561  /* FIXME: We can't reuse object_fix_multipart() since that only
562  * works for items directly on maps. Maybe factor out common code?
563  */
564  op = arch_to_object(at);
565  op->head = tmp;
566  op->env = tmp->env;
567  last->more = op;
568  if (tmp->name != op->name) {
569  if (op->name)
570  free_string(op->name);
571  op->name = add_string(tmp->name);
572  }
573  if (tmp->title != op->title) {
574  if (op->title)
575  free_string(op->title);
576  op->title = add_string(tmp->title);
577  }
579  }
580  } FOR_INV_FINISH();
581 }
582 
594  int x, y;
595 
596  for (x = 0; x < MAP_WIDTH(m); x++)
597  for (y = 0; y < MAP_HEIGHT(m); y++)
598  FOR_MAP_PREPARE(m, x, y, tmp) {
599  if (tmp->inv)
601 
602  /* already multipart - don't do anything more */
603  if (tmp->head || tmp->more)
604  continue;
605 
607  } FOR_MAP_FINISH(); /* for objects on this space */
608 }
609 
621 static void load_objects(mapstruct *m, FILE *fp, int mapflags) {
622  int i, j, bufstate = LO_NEWFILE;
623  int unique;
624  object *op, *prev = NULL, *last_more = NULL;
625 
626  op = object_new();
627  op->map = m; /* To handle buttons correctly */
628 
629  PROFILE_BEGIN(long cum_body_time = 0;);
630  while ((i = load_object(fp, op, bufstate, mapflags))) {
631  /* Since the loading of the map header does not load an object
632  * anymore, we need to pass LO_NEWFILE for the first object loaded,
633  * and then switch to LO_REPEAT for faster loading.
634  */
635  bufstate = LO_REPEAT;
636 
637  /* if the archetype for the object is null, means that we
638  * got an invalid object. Don't do anything with it - the game
639  * or editor will not be able to do anything with it either.
640  */
641  if (op->arch == NULL) {
642  LOG(llevDebug, "Discarding object without arch: %s\n", op->name ? op->name : "(null)");
643  continue;
644  }
645 
646  /*
647  * You can NOT have players on a map being loaded.
648  * Trying to use such a type leads to crashes everywhere as op->contr is NULL.
649  */
650  if (op->type == PLAYER) {
651  LOG(llevError, "Discarding invalid item with type PLAYER in map %s\n", m->path);
652  continue;
653  }
654 
655  /* don't use out_of_map because we don't want to consider tiling properties, we're loading a single map */
656  if (op->x < 0 || op->y < 0 || op->x >= MAP_WIDTH(m) || op->y >= MAP_HEIGHT(m)) {
657  LOG(llevError, " object %s not on valid map position %s:%d:%d\n", op->name ? op->name : "(null)", m->path, op->x, op->y);
658  if (op->x < 0) {
659  op->x = 0;
660  } else if (op->x >= MAP_WIDTH(m)) {
661  op->x = MAP_WIDTH(m) - 1;
662  }
663  if (op->y < 0) {
664  op->y = 0;
665  } else if (op->y >= MAP_HEIGHT(m)) {
666  op->y = MAP_HEIGHT(m) - 1;
667  }
668  }
669 
670  PROFILE_BEGIN();
671  switch (i) {
672  case LL_NORMAL:
673  /* if we are loading an overlay, put the floors on the bottom */
675  && mapflags&MAP_OVERLAY)
677  else
679 
680  if (op->inv)
681  object_sum_weight(op);
682 
683  prev = op,
684  last_more = op;
685  break;
686 
687  case LL_MORE:
689  op->head = prev,
690  last_more->more = op,
691  last_more = op;
692  break;
693  }
694  if (mapflags&MAP_STYLE) {
696  }
697  PROFILE_END(diff, cum_body_time += diff);
698  op = object_new();
699  op->map = m;
700  }
701  PROFILE_END(diff, LOG(llevDebug,
702  "load_objects: while loop took %ld, body took %ld\n",
703  diff, cum_body_time));
704  for (i = 0; i < m->width; i++) {
705  for (j = 0; j < m->height; j++) {
706  unique = 0;
707  /* check for unique items, or unique squares */
708  FOR_MAP_PREPARE(m, i, j, otmp) {
709  if (QUERY_FLAG(otmp, FLAG_UNIQUE))
710  unique = 1;
711  if (!(mapflags&(MAP_OVERLAY|MAP_PLAYER_UNIQUE) || unique))
713  } FOR_MAP_FINISH();
714  }
715  }
718 }
719 
737 static int save_objects(mapstruct *m, FILE *fp, FILE *fp2, int flag) {
738  int i, j = 0, unique = 0, res = 0;
739 
740  /* first pass - save one-part objects */
741  for (i = 0; i < MAP_WIDTH(m); i++)
742  for (j = 0; j < MAP_HEIGHT(m); j++) {
743  unique = 0;
744  FOR_MAP_PREPARE(m, i, j, op) {
746  unique = 1;
747 
748  if (op->type == PLAYER) {
749  LOG(llevDebug, "Player on map that is being saved\n");
750  continue;
751  }
752 
753  if (op->head || object_get_owner(op) != NULL)
754  continue;
755 
756  if (unique || QUERY_FLAG(op, FLAG_UNIQUE))
758  else
759  if (flag == 0
760  || (flag == SAVE_FLAG_NO_REMOVE && (!QUERY_FLAG(op, FLAG_OBJ_ORIGINAL) && !QUERY_FLAG(op, FLAG_UNPAID))))
762 
763  if (res != 0)
764  return res;
765  } FOR_MAP_FINISH(); /* for this space */
766  } /* for this j */
767 
768  return 0;
769 }
770 
783  mapstruct *map = (mapstruct *)calloc(1, sizeof(mapstruct));
784  /* mapstruct *mp;*/
785 
786  if (map == NULL)
788  /*
789  * Nothing in the code appears to require we add new maps to the end of the list.
790  * Why not just add them to the front instead? Its faster.
791  *
792  * SilverNexus 2016-05-18
793  *
794  for (mp = first_map; mp != NULL && mp->next != NULL; mp = mp->next)
795  ;
796  if (mp == NULL)
797  first_map = map;
798  else
799  mp->next = map;
800  */
801  map->next = first_map;
802  first_map = map;
803 
804  map->in_memory = MAP_SWAPPED;
805  /* The maps used to pick up default x and y values from the
806  * map archetype. Mimic that behaviour.
807  */
808  MAP_WIDTH(map) = 16;
809  MAP_HEIGHT(map) = 16;
810  MAP_RESET_TIMEOUT(map) = 0;
811  MAP_TIMEOUT(map) = 300;
812  MAP_ENTER_X(map) = 0;
813  MAP_ENTER_Y(map) = 0;
814  map->last_reset_time = 0;
815  return map;
816 }
817 
820  return (uint32_t)m->width * (uint32_t)m->height;
821 }
822 
833 static void allocate_map(mapstruct *m) {
835  /* Log this condition and free the storage. We could I suppose
836  * realloc, but if the caller is presuming the data will be intact,
837  * that is their poor assumption.
838  */
839  if (m->spaces) {
840  LOG(llevError, "allocate_map called with already allocated map (%s)\n", m->path);
841  free(m->spaces);
842  }
843 
844  m->spaces = calloc(map_size(m), sizeof(MapSpace));
845 
846  if (m->spaces == NULL)
848 }
849 
863 mapstruct *get_empty_map(int sizex, int sizey) {
864  mapstruct *m = get_linked_map();
865  m->width = sizex;
866  m->height = sizey;
867  m->in_memory = MAP_SWAPPED;
868  allocate_map(m);
869  return m;
870 }
871 
885 static shopitems *parse_shop_string(const char *input_string, const mapstruct *map) {
886  char *shop_string, *p, *q, *next_semicolon, *next_colon;
887  shopitems *items = NULL;
888  int i = 0, number_of_entries = 0;
889  const typedata *current_type;
890 
891  shop_string = strdup_local(input_string);
892  p = shop_string;
893  LOG(llevDebug, "parsing %s\n", input_string);
894  /* first we'll count the entries, we'll need that for allocating the array shortly */
895  while (p) {
896  p = strchr(p, ';');
897  number_of_entries++;
898  if (p)
899  p++;
900  }
901  p = shop_string;
902  strip_endline(p);
903  items = CALLOC(number_of_entries+1, sizeof(shopitems));
904  /*
905  * The memset would always set at least one byte to zero,
906  * so a failed calloc would have segfaulted the program.
907  * Instead, check for a null and fail more gracefully.
908  */
909  if (!items)
911  /*
912  * calloc() already sets each byte to zero already
913  *
914  memset(items, 0, (sizeof(shopitems)*number_of_entries+1));
915  */
916  for (i = 0; i < number_of_entries; i++) {
917  if (!p) {
918  LOG(llevError, "parse_shop_string: I seem to have run out of string, that shouldn't happen.\n");
919  break;
920  }
921  next_semicolon = strchr(p, ';');
922  next_colon = strchr(p, ':');
923  /* if there is a stregth specified, figure out what it is, we'll need it soon. */
924  if (next_colon && (!next_semicolon || next_colon < next_semicolon))
925  items[i].strength = atoi(strchr(p, ':')+1);
926 
927  if (isdigit(*p) || *p == '*') {
928  items[i].typenum = *p == '*' ? -1 : atoi(p);
929  current_type = get_typedata(items[i].typenum);
930  if (current_type) {
931  items[i].name = current_type->name;
932  items[i].name_pl = current_type->name_pl;
933  }
934  } else { /*we have a named type, let's figure out what it is */
935  q = strpbrk(p, ";:");
936  if (q)
937  *q = '\0';
938 
939  current_type = get_typedata_by_name(p);
940  if (current_type) {
941  items[i].name = current_type->name;
942  items[i].typenum = current_type->number;
943  items[i].name_pl = current_type->name_pl;
944  } else {
945  /* oh uh, something's wrong, let's free up this one, and try
946  * the next entry while we're at it, better print a warning */
947  LOG(llevError, "invalid type %s defined in shopitems for %s in string %s\n", p, map->name, input_string);
948  }
949  }
950  items[i].index = number_of_entries;
951  if (next_semicolon)
952  p = ++next_semicolon;
953  else
954  p = NULL;
955  }
956  free(shop_string);
957  return items;
958 }
959 
971 static void print_shop_string(mapstruct *m, char *output_string, int size) {
972  int i;
973  char tmp[MAX_BUF];
974 
975  output_string[0] = '\0';
976  for (i = 0; i < m->shopitems[0].index; i++) {
977  if (m->shopitems[i].typenum != -1) {
978  if (m->shopitems[i].strength) {
979  snprintf(tmp, sizeof(tmp), "%s:%d;", m->shopitems[i].name, m->shopitems[i].strength);
980  } else
981  snprintf(tmp, sizeof(tmp), "%s;", m->shopitems[i].name);
982  } else {
983  if (m->shopitems[i].strength) {
984  snprintf(tmp, sizeof(tmp), "*:%d;", m->shopitems[i].strength);
985  } else
986  snprintf(tmp, sizeof(tmp), "*;");
987  }
988  snprintf(output_string+strlen(output_string), size-strlen(output_string), "%s", tmp);
989  }
990 
991  /* erase final ; else parsing back will lead to issues */
992  if (strlen(output_string) > 0) {
993  output_string[strlen(output_string) - 1] = '\0';
994  }
995 }
996 
1016 static int load_map_header(FILE *fp, mapstruct *m) {
1017  char buf[HUGE_BUF], *key = NULL, *value;
1018 
1019  m->width = m->height = 0;
1020  while (fgets(buf, sizeof(buf), fp) != NULL) {
1021  char *p;
1022 
1023  p = strchr(buf, '\n');
1024  if (p == NULL) {
1025  LOG(llevError, "Error loading map header - did not find a newline - perhaps file is truncated? Buf=%s\n", buf);
1026  return 1;
1027  }
1028  *p = '\0';
1029 
1030  key = buf;
1031  while (isspace(*key))
1032  key++;
1033  if (*key == 0)
1034  continue; /* empty line */
1035  value = strchr(key, ' ');
1036  if (value) {
1037  *value = 0;
1038  value++;
1039  while (isspace(*value)) {
1040  value++;
1041  if (*value == '\0') {
1042  /* Nothing but spaces. */
1043  value = NULL;
1044  break;
1045  }
1046  }
1047  }
1048 
1049  /* key is the field name, value is what it should be set
1050  * to. We've already done the work to null terminate key,
1051  * and strip off any leading spaces for both of these.
1052  * We have not touched the newline at the end of the line -
1053  * these are needed for some values. the end pointer
1054  * points to the first of the newlines.
1055  * value could be NULL! It would be easy enough to just point
1056  * this to "" to prevent cores, but that would let more errors slide
1057  * through.
1058  *
1059  * First check for entries that do not use the value parameter, then
1060  * validate that value is given and check for the remaining entries
1061  * that use the parameter.
1062  */
1063 
1064  if (!strcmp(key, "msg")) {
1065  char msgbuf[HUGE_BUF];
1066  int msgpos = 0;
1067 
1068  while (fgets(buf, sizeof(buf), fp) != NULL) {
1069  if (!strcmp(buf, "endmsg\n"))
1070  break;
1071  else {
1072  snprintf(msgbuf+msgpos, sizeof(msgbuf)-msgpos, "%s", buf);
1073  msgpos += strlen(buf);
1074  }
1075  }
1076  /* There are lots of maps that have empty messages (eg, msg/endmsg
1077  * with nothing between). There is no reason in those cases to
1078  * keep the empty message. Also, msgbuf contains garbage data
1079  * when msgpos is zero, so copying it results in crashes
1080  */
1081  if (msgpos != 0) {
1082  /* When loading eg an overlay, message is already set, so free() current one. */
1083  free(m->msg);
1084  m->msg = strdup_local(msgbuf);
1085  }
1086  } else if (!strcmp(key, "maplore")) {
1087  char maplorebuf[HUGE_BUF];
1088  size_t maplorepos = 0;
1089 
1090  while (fgets(buf, HUGE_BUF-1, fp) != NULL) {
1091  if (!strcmp(buf, "endmaplore\n"))
1092  break;
1093  else {
1094  if (maplorepos >= sizeof(maplorebuf)) {
1095  LOG(llevError, "Map lore exceeds buffer length\n");
1096  return 1;
1097  }
1098  snprintf(maplorebuf+maplorepos, sizeof(maplorebuf)-maplorepos, "%s", buf);
1099  maplorepos += strlen(buf);
1100  }
1101  }
1102  if (maplorepos != 0)
1103  m->maplore = strdup_local(maplorebuf);
1104  } else if (!strcmp(key, "end")) {
1105  break;
1106  } else if (value == NULL) {
1107  LOG(llevError, "Got '%s' line without parameter in map header\n", key);
1108  } else if (!strcmp(key, "arch")) {
1109  /* This is an oddity, but not something we care about much. */
1110  if (strcmp(value, "map")) {
1111  LOG(llevError, "load_map_header: expected 'arch map': check line endings?\n");
1112  return 1;
1113  }
1114  } else if (!strcmp(key, "name")) {
1115  /* When loading eg an overlay, the name is already set, so free() current one. */
1116  free(m->name);
1117  m->name = strdup_local(value);
1118  /* first strcmp value on these are old names supported
1119  * for compatibility reasons. The new values (second) are
1120  * what really should be used.
1121  */
1122  } else if (!strcmp(key, "enter_x")) {
1123  m->enter_x = atoi(value);
1124  } else if (!strcmp(key, "enter_y")) {
1125  m->enter_y = atoi(value);
1126  } else if (!strcmp(key, "width")) {
1127  m->width = atoi(value);
1128  } else if (!strcmp(key, "height")) {
1129  m->height = atoi(value);
1130  } else if (!strcmp(key, "reset_timeout")) {
1131  m->reset_timeout = atoi(value);
1132  } else if (!strcmp(key, "swap_time")) {
1133  m->timeout = atoi(value);
1134  } else if (!strcmp(key, "difficulty")) {
1135  m->difficulty = atoi(value);
1136  } else if (!strcmp(key, "darkness")) {
1137  m->darkness = atoi(value);
1138  } else if (!strcmp(key, "fixed_resettime")) {
1139  m->fixed_resettime = atoi(value);
1140  } else if (!strcmp(key, "unique")) {
1141  m->unique = atoi(value);
1142  } else if (!strcmp(key, "template")) {
1143  m->is_template = atoi(value);
1144  } else if (!strcmp(key, "region")) {
1145  m->region = get_region_by_name(value);
1146  } else if (!strcmp(key, "shopitems")) {
1147  m->shopitems = parse_shop_string(value, m);
1148  } else if (!strcmp(key, "shopgreed")) {
1149  m->shopgreed = atof(value);
1150  } else if (!strcmp(key, "shopmin")) {
1151  m->shopmin = atol(value);
1152  } else if (!strcmp(key, "shopmax")) {
1153  m->shopmax = atol(value);
1154  } else if (!strcmp(key, "shoprace")) {
1155  m->shoprace = strdup_local(value);
1156  } else if (!strcmp(key, "outdoor")) {
1157  m->outdoor = atoi(value);
1158  } else if (!strcmp(key, "nosmooth")) {
1159  m->nosmooth = atoi(value);
1160  } else if (!strcmp(key, "first_load")) {
1161  m->last_reset_time = atoi(value);
1162  } else if (!strncmp(key, "tile_path_", 10)) {
1163  int tile = atoi(key+10);
1164 
1165  if (tile < 1 || tile > 4) {
1166  LOG(llevError, "load_map_header: tile location %d out of bounds (%s)\n", tile, m->path);
1167  } else {
1168  char path[HUGE_BUF];
1169 
1170  if (m->tile_path[tile-1]) {
1171  LOG(llevError, "load_map_header: tile location %d duplicated (%s)\n", tile, m->path);
1172  free(m->tile_path[tile-1]);
1173  m->tile_path[tile-1] = NULL;
1174  }
1175 
1176  if (check_path(value, 1) != -1) {
1177  /* The unadorned path works. */
1178  strlcpy(path, value, sizeof(path));
1179  } else {
1180  /* Try again; it could be a relative exit. */
1181  path_combine_and_normalize(m->path, value, path, sizeof(path));
1182 
1183  if (check_path(path, 1) == -1) {
1184  LOG(llevError, "get_map_header: Bad tile path %s %s\n", m->path, value);
1185  path[0] = '\0';
1186  }
1187  }
1188 
1189  if (*path != '\0') {
1190  /* Use the normalized value. */
1191  m->tile_path[tile-1] = strdup_local(path);
1192  }
1193  } /* end if tile direction (in)valid */
1194  } else if (!strcmp(key, "background_music")) {
1195  m->background_music = strdup_local(value);
1196  } else {
1197  LOG(llevError, "Got unknown value in map header: %s %s\n", key, value);
1198  }
1199  }
1200  if ((m->width == 0) || (m->height == 0)) {
1201  LOG(llevError, "Map width or height not specified\n");
1202  return 1;
1203  }
1204  if (!key || strcmp(key, "end")) {
1205  LOG(llevError, "Got premature eof on map header!\n");
1206  return 1;
1207  }
1208  return 0;
1209 }
1210 
1226 mapstruct *mapfile_load(const char *map, int flags) {
1227  FILE *fp;
1228  mapstruct *m;
1229  char pathname[MAX_BUF];
1230 
1231  PROFILE_BEGIN();
1232 
1233  if (flags&MAP_PLAYER_UNIQUE)
1234  strlcpy(pathname, map, sizeof(pathname));
1235  else if (flags&MAP_OVERLAY)
1236  create_overlay_pathname(map, pathname, MAX_BUF);
1237  else
1238  create_pathname(map, pathname, MAX_BUF);
1239 
1240  if ((fp = fopen(pathname, "r")) == NULL) {
1241  LOG((flags&MAP_PLAYER_UNIQUE) ? llevDebug : llevError,
1242  "Can't open %s: %s\n", pathname, strerror(errno));
1243  return (NULL);
1244  }
1245 
1246  m = get_linked_map();
1247 
1248  safe_strncpy(m->path, map, HUGE_BUF);
1249  if (load_map_header(fp, m)) {
1250  LOG(llevError, "Error loading map header for %s, flags=%d\n", map, flags);
1251  delete_map(m);
1252  fclose(fp);
1253  return NULL;
1254  }
1255 
1256  allocate_map(m);
1257 
1258  m->in_memory = MAP_LOADING;
1259  load_objects(m, fp, flags & MAP_STYLE);
1260  fclose(fp);
1261  m->in_memory = MAP_IN_MEMORY;
1262  if (!MAP_DIFFICULTY(m))
1264  set_map_reset_time(m);
1265 
1266  /* In case other objects press some buttons down */
1267  update_buttons(m);
1268 
1269  /* Handle for map load event */
1271 
1272  if (!(flags & MAP_STYLE))
1273  apply_auto_fix(m); /* Chests which open as default */
1274 
1275  PROFILE_END(diff,
1276  LOG(llevDebug, "mapfile_load on %s" " took %ld us\n", map, diff));
1277 
1278  return (m);
1279 }
1280 
1290  FILE *fp;
1291 
1292  if (!m->tmpname) {
1293  LOG(llevError, "No temporary filename for map %s\n", m->path);
1294  return 1;
1295  }
1296 
1297  if ((fp = fopen(m->tmpname, "r")) == NULL) {
1298  LOG(llevError, "Cannot open %s: %s\n", m->tmpname, strerror(errno));
1299  return 2;
1300  }
1301 
1302  if (load_map_header(fp, m)) {
1303  LOG(llevError, "Error loading map header for %s (%s)\n", m->path, m->tmpname);
1304  fclose(fp);
1305  return 3;
1306  }
1307  allocate_map(m);
1308 
1309  m->in_memory = MAP_LOADING;
1310  load_objects(m, fp, 0);
1311  fclose(fp);
1312  m->in_memory = MAP_IN_MEMORY;
1313  return 0;
1314 }
1315 
1325 static int load_overlay_map(const char *filename, mapstruct *m) {
1326  FILE *fp;
1327  char pathname[MAX_BUF];
1328 
1329  create_overlay_pathname(filename, pathname, MAX_BUF);
1330 
1331  if ((fp = fopen(pathname, "r")) == NULL) {
1332  /* nothing bad to not having an overlay */
1333  return 0;
1334  }
1335 
1336  if (load_map_header(fp, m)) {
1337  LOG(llevError, "Error loading map header for overlay %s (%s)\n", m->path, pathname);
1338  fclose(fp);
1339  return 1;
1340  }
1341  /*allocate_map(m);*/
1342 
1343  m->in_memory = MAP_LOADING;
1344  load_objects(m, fp, MAP_OVERLAY);
1345  fclose(fp);
1346  m->in_memory = MAP_IN_MEMORY;
1347  return 0;
1348 }
1349 
1350 /******************************************************************************
1351  * This is the start of unique map handling code
1352  *****************************************************************************/
1353 
1361  int i, j, unique = 0;
1362 
1363  for (i = 0; i < MAP_WIDTH(m); i++)
1364  for (j = 0; j < MAP_HEIGHT(m); j++) {
1365  unique = 0;
1366  FOR_MAP_PREPARE(m, i, j, op) {
1368  unique = 1;
1369  if (op->head == NULL && (QUERY_FLAG(op, FLAG_UNIQUE) || unique)) {
1370  clean_object(op);
1371  if (QUERY_FLAG(op, FLAG_IS_LINKED))
1372  remove_button_link(op);
1373  object_remove(op);
1375  }
1376  } FOR_MAP_FINISH();
1377  }
1378 }
1379 
1386  FILE *fp;
1387  int count;
1388  char firstname[MAX_BUF], name[MAX_BUF];
1389 
1390  create_items_path(m->path, name, MAX_BUF);
1391  for (count = 0; count < 10; count++) {
1392  snprintf(firstname, sizeof(firstname), "%s.v%02d", name, count);
1393  if (!access(firstname, R_OK))
1394  break;
1395  }
1396  /* If we get here, we did not find any map */
1397  if (count == 10)
1398  return;
1399 
1400  if ((fp = fopen(firstname, "r")) == NULL) {
1401  /* There is no expectation that every map will have unique items, but this
1402  * is debug output, so leave it in.
1403  */
1404  LOG(llevDebug, "Can't open unique items file for %s\n", name);
1405  return;
1406  }
1407 
1408  m->in_memory = MAP_LOADING;
1409  if (m->tmpname == NULL) /* if we have loaded unique items from */
1410  delete_unique_items(m); /* original map before, don't duplicate them */
1411  load_object(fp, NULL, LO_NOREAD, 0);
1412  load_objects(m, fp, 0);
1413  fclose(fp);
1414  m->in_memory = MAP_IN_MEMORY;
1415 }
1416 
1417 static bool in_localdir(const char path[static 1]) {
1418  return strstr(path, settings.localdir) == path;
1419 }
1420 
1436 int save_map(mapstruct *m, int flag) {
1437 #define TEMP_EXT ".savefile"
1438  FILE *fp, *fp2;
1439  OutputFile of, of2;
1440  char filename[MAX_BUF], buf[MAX_BUF], shop[MAX_BUF], final[MAX_BUF];
1441  int i, res;
1442 
1443  if (flag && !*m->path) {
1444  LOG(llevError, "Tried to save map without path.\n");
1445  return SAVE_ERROR_NO_PATH;
1446  }
1447 
1448  PROFILE_BEGIN();
1449 
1450  if (flag != SAVE_MODE_NORMAL || (m->unique) || (m->is_template)) {
1451  if (!m->unique && !m->is_template) { /* flag is set */
1452  if (flag == SAVE_MODE_OVERLAY)
1453  create_overlay_pathname(m->path, filename, MAX_BUF);
1454  else
1455  create_pathname(m->path, filename, MAX_BUF);
1456  } else {
1457  if (!in_localdir(m->path)) {
1458  LOG(llevError,
1459  "Cannot save unique map '%s' outside of LOCALDIR. Check "
1460  "that all exits to '%s' have FLAG_UNIQUE set correctly.\n",
1461  m->path, m->path);
1462  return SAVE_ERROR_UCREATION;
1463  }
1464  strlcpy(filename, m->path, sizeof(filename));
1465  }
1466 
1467  make_path_to_file(filename);
1468  } else {
1469  if (!m->tmpname)
1470  m->tmpname = tempnam(settings.tmpdir, NULL);
1471  strlcpy(filename, m->tmpname, sizeof(filename));
1472  }
1473  m->in_memory = MAP_SAVING;
1474 
1475  strlcpy(final, filename, sizeof(final));
1476  snprintf(filename, sizeof(filename), "%s%s", final, TEMP_EXT);
1477  fp = of_open(&of, filename);
1478  if (fp == NULL)
1479  return SAVE_ERROR_RCREATION;
1480 
1481  /* legacy */
1482  fprintf(fp, "arch map\n");
1483  if (m->name)
1484  fprintf(fp, "name %s\n", m->name);
1485  if (!flag)
1486  fprintf(fp, "swap_time %d\n", m->swap_time);
1487  if (m->reset_timeout)
1488  fprintf(fp, "reset_timeout %u\n", m->reset_timeout);
1489  if (m->fixed_resettime)
1490  fprintf(fp, "fixed_resettime %d\n", m->fixed_resettime);
1491  /* we unfortunately have no idea if this is a value the creator set
1492  * or a difficulty value we generated when the map was first loaded
1493  */
1494  if (m->difficulty)
1495  fprintf(fp, "difficulty %d\n", m->difficulty);
1496  if (m->region)
1497  fprintf(fp, "region %s\n", m->region->name);
1498  if (m->shopitems) {
1499  print_shop_string(m, shop, sizeof(shop));
1500  fprintf(fp, "shopitems %s\n", shop);
1501  }
1502  if (m->shopgreed)
1503  fprintf(fp, "shopgreed %f\n", m->shopgreed);
1504  if (m->shopmin)
1505  fprintf(fp, "shopmin %"FMT64U"\n", m->shopmin);
1506  if (m->shopmax)
1507  fprintf(fp, "shopmax %"FMT64U"\n", m->shopmax);
1508  if (m->shoprace)
1509  fprintf(fp, "shoprace %s\n", m->shoprace);
1510  if (m->darkness)
1511  fprintf(fp, "darkness %d\n", m->darkness);
1512  if (m->width)
1513  fprintf(fp, "width %d\n", m->width);
1514  if (m->height)
1515  fprintf(fp, "height %d\n", m->height);
1516  if (m->enter_x)
1517  fprintf(fp, "enter_x %d\n", m->enter_x);
1518  if (m->enter_y)
1519  fprintf(fp, "enter_y %d\n", m->enter_y);
1520  if (m->msg)
1521  fprintf(fp, "msg\n%sendmsg\n", m->msg);
1522  if (m->maplore)
1523  fprintf(fp, "maplore\n%sendmaplore\n", m->maplore);
1524  if (m->unique)
1525  fprintf(fp, "unique %d\n", m->unique);
1526  if (m->is_template)
1527  fprintf(fp, "template %d\n", m->is_template);
1528  if (m->outdoor)
1529  fprintf(fp, "outdoor %d\n", m->outdoor);
1530  if (m->nosmooth)
1531  fprintf(fp, "nosmooth %d\n", m->nosmooth);
1532  if (m->last_reset_time)
1533  fprintf(fp, "first_load %ld\n", m->last_reset_time);
1534  if (m->background_music)
1535  fprintf(fp, "background_music %s\n", m->background_music);
1536 
1537  /* Save any tiling information, except on overlays */
1538  if (flag != SAVE_MODE_OVERLAY)
1539  for (i = 0; i < 4; i++)
1540  if (m->tile_path[i])
1541  fprintf(fp, "tile_path_%d %s\n", i+1, m->tile_path[i]);
1542 
1543  fprintf(fp, "end\n");
1544 
1545  /* In the game save unique items in the different file, but
1546  * in the editor save them to the normal map file.
1547  * If unique map, save files in the proper destination (set by
1548  * player)
1549  */
1550  if ((flag == SAVE_MODE_NORMAL || flag == SAVE_MODE_OVERLAY) && !m->unique && !m->is_template) {
1551  char name[MAX_BUF], final_unique[MAX_BUF];
1552 
1553  create_items_path(m->path, name, MAX_BUF);
1554  snprintf(final_unique, sizeof(final_unique), "%s.v00", name);
1555  snprintf(buf, sizeof(buf), "%s%s", final_unique, TEMP_EXT);
1556  fp2 = of_open(&of2, buf);
1557  if (fp2 == NULL) {
1558  of_cancel(&of);
1559  return SAVE_ERROR_UCREATION;
1560  }
1561  if (flag == SAVE_MODE_OVERLAY) {
1562  /* SO_FLAG_NO_REMOVE is non destructive save, so map is still valid. */
1563  res = save_objects(m, fp, fp2, SAVE_FLAG_NO_REMOVE);
1564  if (res < 0) {
1565  LOG(llevError, "Save error during object save: %d\n", res);
1566  of_cancel(&of);
1567  of_cancel(&of2);
1568  return res;
1569  }
1570  m->in_memory = MAP_IN_MEMORY;
1571  } else {
1572  res = save_objects(m, fp, fp2, 0);
1573  if (res < 0) {
1574  LOG(llevError, "Save error during object save: %d\n", res);
1575  of_cancel(&of);
1576  of_cancel(&of2);
1577  return res;
1578  }
1579  free_all_objects(m);
1580  }
1581  if (ftell(fp2) == 0) {
1582  of_cancel(&of2);
1583  unlink(buf);
1584  /* If there are no unique items left on the map, we need to
1585  * unlink the original unique map so that the unique
1586  * items don't show up again.
1587  */
1588  unlink(final_unique);
1589  } else {
1590  if (!of_close(&of2)) {
1591  of_cancel(&of);
1592  return SAVE_ERROR_WRITE;
1593  }
1594  unlink(final_unique); /* failure isn't too bad, maybe the file doesn't exist. */
1595  if (rename(buf, final_unique) == -1) {
1596  LOG(llevError, "Couldn't rename unique file %s to %s\n", buf, final_unique);
1597  of_cancel(&of);
1598  return SAVE_ERROR_URENAME;
1599  }
1600 
1601  if (chmod(final_unique, SAVE_MODE) != 0) {
1602  LOG(llevError, "Could not set permissions on '%s'\n",
1603  final_unique);
1604  }
1605  }
1606  } else { /* save same file when not playing, like in editor */
1607  res = save_objects(m, fp, fp, 0);
1608  if (res < 0) {
1609  LOG(llevError, "Save error during object save: %d\n", res);
1610  of_cancel(&of);
1611  return res;
1612  }
1613  free_all_objects(m);
1614  }
1615 
1616  if (!of_close(&of))
1617  return SAVE_ERROR_CLOSE;
1618  unlink(final); /* failure isn't too bad, maybe the file doesn't exist. */
1619  if (rename(filename, final) == -1) {
1620  LOG(llevError, "Couldn't rename regular file %s to %s\n", filename, final);
1621  return SAVE_ERROR_RRENAME;
1622  }
1623 
1624  if (chmod(final, SAVE_MODE) != 0) {
1625  LOG(llevError, "Could not set permissions on '%s'\n", final);
1626  }
1627 
1628  PROFILE_END(diff,
1629  LOG(llevDebug, "save_map on %s" " took %ld us\n", m->path, diff));
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:3620
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:1226
#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:833
#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:782
Definition: map.h:380
int64_t level_exp(int level, double expmul)
Definition: living.c:1847
#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:863
#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:737
uint32_t map_size(mapstruct *m)
Definition: map.c:819
#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:71
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:112
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:426
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:885
#define FMT64U
Definition: compat.h:13
#define HUGE_BUF
Definition: define.h:37
void object_fix_multipart(object *tmp)
Definition: object.c:4587
#define MAP_MAXRESET
Definition: config.h:424
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:1289
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
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 PROFILE_BEGIN(expr)
Definition: map.c:37
#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:1016
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
#define INS_ON_TOP
Definition: object.h:569
int blocked_link(object *ob, mapstruct *m, int sx, int sy)
Definition: map.c:354
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:62
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:497
static void link_multipart_objects(mapstruct *m)
Definition: map.c:593
#define MAP_TIMEOUT(m)
Definition: map.h:66
#define PROFILE_END(var, expr)
Definition: map.c:42
void dump_all_maps(void)
Definition: map.c:278
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:5274
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:1360
#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:255
#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:54
#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:1436
uint8_t high_layer
Definition: map.c:61
#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:1385
Definition: map.h:258
const char * name
Definition: define.h:91
void update_all_map_los(mapstruct *map)
Definition: los.c:508
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:87
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:971
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:621
FILE * of_open(OutputFile *of, const char *fname)
Definition: output_file.c:30
int direction
Definition: map.h:384
#define SAVE_MODE
Definition: config.h:566
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 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:176
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:211
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:552
int get_map_flags(mapstruct *oldmap, mapstruct **newmap, int16_t x, int16_t y, int16_t *nx, int16_t *ny)
Definition: map.c:310
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:133
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:1325
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:153
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:571
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
size_t strlcpy(char *dst, const char *src, size_t size)
Definition: porting.c:366
#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:1417
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