Crossfire Server, Trunk  R213250
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  unsigned int count = 0;
740 
741  PROFILE_BEGIN();
742  /* first pass - save one-part objects */
743  for (i = 0; i < MAP_WIDTH(m); i++)
744  for (j = 0; j < MAP_HEIGHT(m); j++) {
745  unique = 0;
746  FOR_MAP_PREPARE(m, i, j, op) {
748  unique = 1;
749 
750  if (op->type == PLAYER) {
751  LOG(llevDebug, "Player on map that is being saved\n");
752  continue;
753  }
754 
755  if (op->head || object_get_owner(op) != NULL)
756  continue;
757 
758  if (unique || QUERY_FLAG(op, FLAG_UNIQUE)) {
760  count++ ;
761  } else if (flag == 0
762  || (flag == SAVE_FLAG_NO_REMOVE && (!QUERY_FLAG(op, FLAG_OBJ_ORIGINAL) && !QUERY_FLAG(op, FLAG_UNPAID)))) {
764  count++;
765  }
766 
767  if (res != 0)
768  return res;
769  } FOR_MAP_FINISH(); /* for this space */
770  } /* for this j */
771 
772  PROFILE_END(diff,
773  LOG(llevDebug, "save_objects on %s took %ld us (%u objects = %f us each)\n", m->path, diff, count, (double)diff/count));
774  return 0;
775 }
776 
789  mapstruct *map = (mapstruct *)calloc(1, sizeof(mapstruct));
790  /* mapstruct *mp;*/
791 
792  if (map == NULL)
794  /*
795  * Nothing in the code appears to require we add new maps to the end of the list.
796  * Why not just add them to the front instead? Its faster.
797  *
798  * SilverNexus 2016-05-18
799  *
800  for (mp = first_map; mp != NULL && mp->next != NULL; mp = mp->next)
801  ;
802  if (mp == NULL)
803  first_map = map;
804  else
805  mp->next = map;
806  */
807  map->next = first_map;
808  first_map = map;
809 
810  map->in_memory = MAP_SWAPPED;
811  /* The maps used to pick up default x and y values from the
812  * map archetype. Mimic that behaviour.
813  */
814  MAP_WIDTH(map) = 16;
815  MAP_HEIGHT(map) = 16;
816  MAP_RESET_TIMEOUT(map) = 0;
817  MAP_TIMEOUT(map) = 300;
818  MAP_ENTER_X(map) = 0;
819  MAP_ENTER_Y(map) = 0;
820  map->last_reset_time = 0;
821  return map;
822 }
823 
826  return (uint32_t)m->width * (uint32_t)m->height;
827 }
828 
839 static void allocate_map(mapstruct *m) {
841  /* Log this condition and free the storage. We could I suppose
842  * realloc, but if the caller is presuming the data will be intact,
843  * that is their poor assumption.
844  */
845  if (m->spaces) {
846  LOG(llevError, "allocate_map called with already allocated map (%s)\n", m->path);
847  free(m->spaces);
848  }
849 
850  m->spaces = calloc(map_size(m), sizeof(MapSpace));
851 
852  if (m->spaces == NULL)
854 }
855 
869 mapstruct *get_empty_map(int sizex, int sizey) {
870  mapstruct *m = get_linked_map();
871  m->width = sizex;
872  m->height = sizey;
873  m->in_memory = MAP_SWAPPED;
874  allocate_map(m);
875  return m;
876 }
877 
891 static shopitems *parse_shop_string(const char *input_string, const mapstruct *map) {
892  char *shop_string, *p, *q, *next_semicolon, *next_colon;
893  shopitems *items = NULL;
894  int i = 0, number_of_entries = 0;
895  const typedata *current_type;
896 
897  shop_string = strdup_local(input_string);
898  p = shop_string;
899  LOG(llevDebug, "parsing %s\n", input_string);
900  /* first we'll count the entries, we'll need that for allocating the array shortly */
901  while (p) {
902  p = strchr(p, ';');
903  number_of_entries++;
904  if (p)
905  p++;
906  }
907  p = shop_string;
908  strip_endline(p);
909  items = CALLOC(number_of_entries+1, sizeof(shopitems));
910  /*
911  * The memset would always set at least one byte to zero,
912  * so a failed calloc would have segfaulted the program.
913  * Instead, check for a null and fail more gracefully.
914  */
915  if (!items)
917  /*
918  * calloc() already sets each byte to zero already
919  *
920  memset(items, 0, (sizeof(shopitems)*number_of_entries+1));
921  */
922  for (i = 0; i < number_of_entries; i++) {
923  if (!p) {
924  LOG(llevError, "parse_shop_string: I seem to have run out of string, that shouldn't happen.\n");
925  break;
926  }
927  next_semicolon = strchr(p, ';');
928  next_colon = strchr(p, ':');
929  /* if there is a stregth specified, figure out what it is, we'll need it soon. */
930  if (next_colon && (!next_semicolon || next_colon < next_semicolon))
931  items[i].strength = atoi(strchr(p, ':')+1);
932 
933  if (isdigit(*p) || *p == '*') {
934  items[i].typenum = *p == '*' ? -1 : atoi(p);
935  current_type = get_typedata(items[i].typenum);
936  if (current_type) {
937  items[i].name = current_type->name;
938  items[i].name_pl = current_type->name_pl;
939  }
940  } else { /*we have a named type, let's figure out what it is */
941  q = strpbrk(p, ";:");
942  if (q)
943  *q = '\0';
944 
945  current_type = get_typedata_by_name(p);
946  if (current_type) {
947  items[i].name = current_type->name;
948  items[i].typenum = current_type->number;
949  items[i].name_pl = current_type->name_pl;
950  } else {
951  /* oh uh, something's wrong, let's free up this one, and try
952  * the next entry while we're at it, better print a warning */
953  LOG(llevError, "invalid type %s defined in shopitems for %s in string %s\n", p, map->name, input_string);
954  }
955  }
956  items[i].index = number_of_entries;
957  if (next_semicolon)
958  p = ++next_semicolon;
959  else
960  p = NULL;
961  }
962  free(shop_string);
963  return items;
964 }
965 
977 static void print_shop_string(mapstruct *m, char *output_string, int size) {
978  int i;
979  char tmp[MAX_BUF];
980 
981  output_string[0] = '\0';
982  for (i = 0; i < m->shopitems[0].index; i++) {
983  if (m->shopitems[i].typenum != -1) {
984  if (m->shopitems[i].strength) {
985  snprintf(tmp, sizeof(tmp), "%s:%d;", m->shopitems[i].name, m->shopitems[i].strength);
986  } else
987  snprintf(tmp, sizeof(tmp), "%s;", m->shopitems[i].name);
988  } else {
989  if (m->shopitems[i].strength) {
990  snprintf(tmp, sizeof(tmp), "*:%d;", m->shopitems[i].strength);
991  } else
992  snprintf(tmp, sizeof(tmp), "*;");
993  }
994  snprintf(output_string+strlen(output_string), size-strlen(output_string), "%s", tmp);
995  }
996 
997  /* erase final ; else parsing back will lead to issues */
998  if (strlen(output_string) > 0) {
999  output_string[strlen(output_string) - 1] = '\0';
1000  }
1001 }
1002 
1022 static int load_map_header(FILE *fp, mapstruct *m) {
1023  char buf[HUGE_BUF], *key = NULL, *value;
1024 
1025  m->width = m->height = 0;
1026  while (fgets(buf, sizeof(buf), fp) != NULL) {
1027  char *p;
1028 
1029  p = strchr(buf, '\n');
1030  if (p == NULL) {
1031  LOG(llevError, "Error loading map header - did not find a newline - perhaps file is truncated? Buf=%s\n", buf);
1032  return 1;
1033  }
1034  *p = '\0';
1035 
1036  key = buf;
1037  while (isspace(*key))
1038  key++;
1039  if (*key == 0)
1040  continue; /* empty line */
1041  value = strchr(key, ' ');
1042  if (value) {
1043  *value = 0;
1044  value++;
1045  while (isspace(*value)) {
1046  value++;
1047  if (*value == '\0') {
1048  /* Nothing but spaces. */
1049  value = NULL;
1050  break;
1051  }
1052  }
1053  }
1054 
1055  /* key is the field name, value is what it should be set
1056  * to. We've already done the work to null terminate key,
1057  * and strip off any leading spaces for both of these.
1058  * We have not touched the newline at the end of the line -
1059  * these are needed for some values. the end pointer
1060  * points to the first of the newlines.
1061  * value could be NULL! It would be easy enough to just point
1062  * this to "" to prevent cores, but that would let more errors slide
1063  * through.
1064  *
1065  * First check for entries that do not use the value parameter, then
1066  * validate that value is given and check for the remaining entries
1067  * that use the parameter.
1068  */
1069 
1070  if (!strcmp(key, "msg")) {
1071  char msgbuf[HUGE_BUF];
1072  int msgpos = 0;
1073 
1074  while (fgets(buf, sizeof(buf), fp) != NULL) {
1075  if (!strcmp(buf, "endmsg\n"))
1076  break;
1077  else {
1078  snprintf(msgbuf+msgpos, sizeof(msgbuf)-msgpos, "%s", buf);
1079  msgpos += strlen(buf);
1080  }
1081  }
1082  /* There are lots of maps that have empty messages (eg, msg/endmsg
1083  * with nothing between). There is no reason in those cases to
1084  * keep the empty message. Also, msgbuf contains garbage data
1085  * when msgpos is zero, so copying it results in crashes
1086  */
1087  if (msgpos != 0) {
1088  /* When loading eg an overlay, message is already set, so free() current one. */
1089  free(m->msg);
1090  m->msg = strdup_local(msgbuf);
1091  }
1092  } else if (!strcmp(key, "maplore")) {
1093  char maplorebuf[HUGE_BUF];
1094  size_t maplorepos = 0;
1095 
1096  while (fgets(buf, HUGE_BUF-1, fp) != NULL) {
1097  if (!strcmp(buf, "endmaplore\n"))
1098  break;
1099  else {
1100  if (maplorepos >= sizeof(maplorebuf)) {
1101  LOG(llevError, "Map lore exceeds buffer length\n");
1102  return 1;
1103  }
1104  snprintf(maplorebuf+maplorepos, sizeof(maplorebuf)-maplorepos, "%s", buf);
1105  maplorepos += strlen(buf);
1106  }
1107  }
1108  if (maplorepos != 0)
1109  m->maplore = strdup_local(maplorebuf);
1110  } else if (!strcmp(key, "end")) {
1111  break;
1112  } else if (value == NULL) {
1113  LOG(llevError, "Got '%s' line without parameter in map header\n", key);
1114  } else if (!strcmp(key, "arch")) {
1115  /* This is an oddity, but not something we care about much. */
1116  if (strcmp(value, "map")) {
1117  LOG(llevError, "load_map_header: expected 'arch map': check line endings?\n");
1118  return 1;
1119  }
1120  } else if (!strcmp(key, "name")) {
1121  /* When loading eg an overlay, the name is already set, so free() current one. */
1122  free(m->name);
1123  m->name = strdup_local(value);
1124  /* first strcmp value on these are old names supported
1125  * for compatibility reasons. The new values (second) are
1126  * what really should be used.
1127  */
1128  } else if (!strcmp(key, "enter_x")) {
1129  m->enter_x = atoi(value);
1130  } else if (!strcmp(key, "enter_y")) {
1131  m->enter_y = atoi(value);
1132  } else if (!strcmp(key, "width")) {
1133  m->width = atoi(value);
1134  } else if (!strcmp(key, "height")) {
1135  m->height = atoi(value);
1136  } else if (!strcmp(key, "reset_timeout")) {
1137  m->reset_timeout = atoi(value);
1138  } else if (!strcmp(key, "swap_time")) {
1139  m->timeout = atoi(value);
1140  } else if (!strcmp(key, "difficulty")) {
1141  m->difficulty = atoi(value);
1142  } else if (!strcmp(key, "darkness")) {
1143  m->darkness = atoi(value);
1144  } else if (!strcmp(key, "fixed_resettime")) {
1145  m->fixed_resettime = atoi(value);
1146  } else if (!strcmp(key, "unique")) {
1147  m->unique = atoi(value);
1148  } else if (!strcmp(key, "template")) {
1149  m->is_template = atoi(value);
1150  } else if (!strcmp(key, "region")) {
1151  m->region = get_region_by_name(value);
1152  } else if (!strcmp(key, "shopitems")) {
1153  m->shopitems = parse_shop_string(value, m);
1154  } else if (!strcmp(key, "shopgreed")) {
1155  m->shopgreed = atof(value);
1156  } else if (!strcmp(key, "shopmin")) {
1157  m->shopmin = atol(value);
1158  } else if (!strcmp(key, "shopmax")) {
1159  m->shopmax = atol(value);
1160  } else if (!strcmp(key, "shoprace")) {
1161  m->shoprace = strdup_local(value);
1162  } else if (!strcmp(key, "outdoor")) {
1163  m->outdoor = atoi(value);
1164  } else if (!strcmp(key, "nosmooth")) {
1165  m->nosmooth = atoi(value);
1166  } else if (!strcmp(key, "first_load")) {
1167  m->last_reset_time = atoi(value);
1168  } else if (!strncmp(key, "tile_path_", 10)) {
1169  int tile = atoi(key+10);
1170 
1171  if (tile < 1 || tile > 4) {
1172  LOG(llevError, "load_map_header: tile location %d out of bounds (%s)\n", tile, m->path);
1173  } else {
1174  char path[HUGE_BUF];
1175 
1176  if (m->tile_path[tile-1]) {
1177  LOG(llevError, "load_map_header: tile location %d duplicated (%s)\n", tile, m->path);
1178  free(m->tile_path[tile-1]);
1179  m->tile_path[tile-1] = NULL;
1180  }
1181 
1182  if (check_path(value, 1) != -1) {
1183  /* The unadorned path works. */
1184  strlcpy(path, value, sizeof(path));
1185  } else {
1186  /* Try again; it could be a relative exit. */
1187  path_combine_and_normalize(m->path, value, path, sizeof(path));
1188 
1189  if (check_path(path, 1) == -1) {
1190  LOG(llevError, "get_map_header: Bad tile path %s %s\n", m->path, value);
1191  path[0] = '\0';
1192  }
1193  }
1194 
1195  if (*path != '\0') {
1196  /* Use the normalized value. */
1197  m->tile_path[tile-1] = strdup_local(path);
1198  }
1199  } /* end if tile direction (in)valid */
1200  } else if (!strcmp(key, "background_music")) {
1201  m->background_music = strdup_local(value);
1202  } else {
1203  LOG(llevError, "Got unknown value in map header: %s %s\n", key, value);
1204  }
1205  }
1206  if ((m->width == 0) || (m->height == 0)) {
1207  LOG(llevError, "Map width or height not specified\n");
1208  return 1;
1209  }
1210  if (!key || strcmp(key, "end")) {
1211  LOG(llevError, "Got premature eof on map header!\n");
1212  return 1;
1213  }
1214  return 0;
1215 }
1216 
1232 mapstruct *mapfile_load(const char *map, int flags) {
1233  FILE *fp;
1234  mapstruct *m;
1235  char pathname[MAX_BUF];
1236 
1237  PROFILE_BEGIN();
1238 
1239  if (flags&MAP_PLAYER_UNIQUE)
1240  strlcpy(pathname, map, sizeof(pathname));
1241  else if (flags&MAP_OVERLAY)
1242  create_overlay_pathname(map, pathname, MAX_BUF);
1243  else
1244  create_pathname(map, pathname, MAX_BUF);
1245 
1246  if ((fp = fopen(pathname, "r")) == NULL) {
1247  LOG((flags&MAP_PLAYER_UNIQUE) ? llevDebug : llevError,
1248  "Can't open %s: %s\n", pathname, strerror(errno));
1249  return (NULL);
1250  }
1251 
1252  m = get_linked_map();
1253 
1254  safe_strncpy(m->path, map, HUGE_BUF);
1255  if (load_map_header(fp, m)) {
1256  LOG(llevError, "Error loading map header for %s, flags=%d\n", map, flags);
1257  delete_map(m);
1258  fclose(fp);
1259  return NULL;
1260  }
1261 
1262  allocate_map(m);
1263 
1264  m->in_memory = MAP_LOADING;
1265  load_objects(m, fp, flags & MAP_STYLE);
1266  fclose(fp);
1267  m->in_memory = MAP_IN_MEMORY;
1268  if (!MAP_DIFFICULTY(m))
1270  set_map_reset_time(m);
1271 
1272  /* In case other objects press some buttons down */
1273  update_buttons(m);
1274 
1275  /* Handle for map load event */
1277 
1278  if (!(flags & MAP_STYLE))
1279  apply_auto_fix(m); /* Chests which open as default */
1280 
1281  PROFILE_END(diff,
1282  LOG(llevDebug, "mapfile_load on %s" " took %ld us\n", map, diff));
1283 
1284  return (m);
1285 }
1286 
1296  FILE *fp;
1297 
1298  if (!m->tmpname) {
1299  LOG(llevError, "No temporary filename for map %s\n", m->path);
1300  return 1;
1301  }
1302 
1303  if ((fp = fopen(m->tmpname, "r")) == NULL) {
1304  LOG(llevError, "Cannot open %s: %s\n", m->tmpname, strerror(errno));
1305  return 2;
1306  }
1307 
1308  if (load_map_header(fp, m)) {
1309  LOG(llevError, "Error loading map header for %s (%s)\n", m->path, m->tmpname);
1310  fclose(fp);
1311  return 3;
1312  }
1313  allocate_map(m);
1314 
1315  m->in_memory = MAP_LOADING;
1316  load_objects(m, fp, 0);
1317  fclose(fp);
1318  m->in_memory = MAP_IN_MEMORY;
1319  return 0;
1320 }
1321 
1331 static int load_overlay_map(const char *filename, mapstruct *m) {
1332  FILE *fp;
1333  char pathname[MAX_BUF];
1334 
1335  create_overlay_pathname(filename, pathname, MAX_BUF);
1336 
1337  if ((fp = fopen(pathname, "r")) == NULL) {
1338  /* nothing bad to not having an overlay */
1339  return 0;
1340  }
1341 
1342  if (load_map_header(fp, m)) {
1343  LOG(llevError, "Error loading map header for overlay %s (%s)\n", m->path, pathname);
1344  fclose(fp);
1345  return 1;
1346  }
1347  /*allocate_map(m);*/
1348 
1349  m->in_memory = MAP_LOADING;
1350  load_objects(m, fp, MAP_OVERLAY);
1351  fclose(fp);
1352  m->in_memory = MAP_IN_MEMORY;
1353  return 0;
1354 }
1355 
1356 /******************************************************************************
1357  * This is the start of unique map handling code
1358  *****************************************************************************/
1359 
1367  int i, j, unique = 0;
1368 
1369  for (i = 0; i < MAP_WIDTH(m); i++)
1370  for (j = 0; j < MAP_HEIGHT(m); j++) {
1371  unique = 0;
1372  FOR_MAP_PREPARE(m, i, j, op) {
1374  unique = 1;
1375  if (op->head == NULL && (QUERY_FLAG(op, FLAG_UNIQUE) || unique)) {
1376  clean_object(op);
1377  if (QUERY_FLAG(op, FLAG_IS_LINKED))
1378  remove_button_link(op);
1379  object_remove(op);
1381  }
1382  } FOR_MAP_FINISH();
1383  }
1384 }
1385 
1392  FILE *fp;
1393  int count;
1394  char firstname[MAX_BUF], name[MAX_BUF];
1395 
1396  create_items_path(m->path, name, MAX_BUF);
1397  for (count = 0; count < 10; count++) {
1398  snprintf(firstname, sizeof(firstname), "%s.v%02d", name, count);
1399  if (!access(firstname, R_OK))
1400  break;
1401  }
1402  /* If we get here, we did not find any map */
1403  if (count == 10)
1404  return;
1405 
1406  if ((fp = fopen(firstname, "r")) == NULL) {
1407  /* There is no expectation that every map will have unique items, but this
1408  * is debug output, so leave it in.
1409  */
1410  LOG(llevDebug, "Can't open unique items file for %s\n", name);
1411  return;
1412  }
1413 
1414  m->in_memory = MAP_LOADING;
1415  if (m->tmpname == NULL) /* if we have loaded unique items from */
1416  delete_unique_items(m); /* original map before, don't duplicate them */
1417  load_object(fp, NULL, LO_NOREAD, 0);
1418  load_objects(m, fp, 0);
1419  fclose(fp);
1420  m->in_memory = MAP_IN_MEMORY;
1421 }
1422 
1423 static bool in_localdir(const char path[static 1]) {
1424  return strstr(path, settings.localdir) == path;
1425 }
1426 
1442 int save_map(mapstruct *m, int flag) {
1443 #define TEMP_EXT ".savefile"
1444  FILE *fp, *fp2;
1445  OutputFile of, of2;
1446  char filename[MAX_BUF], buf[MAX_BUF], shop[MAX_BUF], final[MAX_BUF];
1447  int i, res;
1448 
1449  if (flag && !*m->path) {
1450  LOG(llevError, "Tried to save map without path.\n");
1451  return SAVE_ERROR_NO_PATH;
1452  }
1453 
1454  PROFILE_BEGIN();
1455 
1456  if (flag != SAVE_MODE_NORMAL || (m->unique) || (m->is_template)) {
1457  if (!m->unique && !m->is_template) { /* flag is set */
1458  if (flag == SAVE_MODE_OVERLAY)
1459  create_overlay_pathname(m->path, filename, MAX_BUF);
1460  else
1461  create_pathname(m->path, filename, MAX_BUF);
1462  } else {
1463  if (!in_localdir(m->path)) {
1464  LOG(llevError,
1465  "Cannot save unique map '%s' outside of LOCALDIR. Check "
1466  "that all exits to '%s' have FLAG_UNIQUE set correctly.\n",
1467  m->path, m->path);
1468  return SAVE_ERROR_UCREATION;
1469  }
1470  strlcpy(filename, m->path, sizeof(filename));
1471  }
1472 
1473  make_path_to_file(filename);
1474  } else {
1475  if (!m->tmpname)
1476  m->tmpname = tempnam(settings.tmpdir, NULL);
1477  strlcpy(filename, m->tmpname, sizeof(filename));
1478  }
1479  m->in_memory = MAP_SAVING;
1480 
1481  strlcpy(final, filename, sizeof(final));
1482  snprintf(filename, sizeof(filename), "%s%s", final, TEMP_EXT);
1483  fp = of_open(&of, filename);
1484  if (fp == NULL)
1485  return SAVE_ERROR_RCREATION;
1486 
1487  /* legacy */
1488  fprintf(fp, "arch map\n");
1489  if (m->name)
1490  fprintf(fp, "name %s\n", m->name);
1491  if (!flag)
1492  fprintf(fp, "swap_time %d\n", m->swap_time);
1493  if (m->reset_timeout)
1494  fprintf(fp, "reset_timeout %u\n", m->reset_timeout);
1495  if (m->fixed_resettime)
1496  fprintf(fp, "fixed_resettime %d\n", m->fixed_resettime);
1497  /* we unfortunately have no idea if this is a value the creator set
1498  * or a difficulty value we generated when the map was first loaded
1499  */
1500  if (m->difficulty)
1501  fprintf(fp, "difficulty %d\n", m->difficulty);
1502  if (m->region)
1503  fprintf(fp, "region %s\n", m->region->name);
1504  if (m->shopitems) {
1505  print_shop_string(m, shop, sizeof(shop));
1506  fprintf(fp, "shopitems %s\n", shop);
1507  }
1508  if (m->shopgreed)
1509  fprintf(fp, "shopgreed %f\n", m->shopgreed);
1510  if (m->shopmin)
1511  fprintf(fp, "shopmin %"FMT64U"\n", m->shopmin);
1512  if (m->shopmax)
1513  fprintf(fp, "shopmax %"FMT64U"\n", m->shopmax);
1514  if (m->shoprace)
1515  fprintf(fp, "shoprace %s\n", m->shoprace);
1516  if (m->darkness)
1517  fprintf(fp, "darkness %d\n", m->darkness);
1518  if (m->width)
1519  fprintf(fp, "width %d\n", m->width);
1520  if (m->height)
1521  fprintf(fp, "height %d\n", m->height);
1522  if (m->enter_x)
1523  fprintf(fp, "enter_x %d\n", m->enter_x);
1524  if (m->enter_y)
1525  fprintf(fp, "enter_y %d\n", m->enter_y);
1526  if (m->msg)
1527  fprintf(fp, "msg\n%sendmsg\n", m->msg);
1528  if (m->maplore)
1529  fprintf(fp, "maplore\n%sendmaplore\n", m->maplore);
1530  if (m->unique)
1531  fprintf(fp, "unique %d\n", m->unique);
1532  if (m->is_template)
1533  fprintf(fp, "template %d\n", m->is_template);
1534  if (m->outdoor)
1535  fprintf(fp, "outdoor %d\n", m->outdoor);
1536  if (m->nosmooth)
1537  fprintf(fp, "nosmooth %d\n", m->nosmooth);
1538  if (m->last_reset_time)
1539  fprintf(fp, "first_load %ld\n", m->last_reset_time);
1540  if (m->background_music)
1541  fprintf(fp, "background_music %s\n", m->background_music);
1542 
1543  /* Save any tiling information, except on overlays */
1544  if (flag != SAVE_MODE_OVERLAY)
1545  for (i = 0; i < 4; i++)
1546  if (m->tile_path[i])
1547  fprintf(fp, "tile_path_%d %s\n", i+1, m->tile_path[i]);
1548 
1549  fprintf(fp, "end\n");
1550 
1551  /* In the game save unique items in the different file, but
1552  * in the editor save them to the normal map file.
1553  * If unique map, save files in the proper destination (set by
1554  * player)
1555  */
1556  if ((flag == SAVE_MODE_NORMAL || flag == SAVE_MODE_OVERLAY) && !m->unique && !m->is_template) {
1557  char name[MAX_BUF], final_unique[MAX_BUF];
1558 
1559  create_items_path(m->path, name, MAX_BUF);
1560  snprintf(final_unique, sizeof(final_unique), "%s.v00", name);
1561  snprintf(buf, sizeof(buf), "%s%s", final_unique, TEMP_EXT);
1562  fp2 = of_open(&of2, buf);
1563  if (fp2 == NULL) {
1564  of_cancel(&of);
1565  return SAVE_ERROR_UCREATION;
1566  }
1567  if (flag == SAVE_MODE_OVERLAY) {
1568  /* SO_FLAG_NO_REMOVE is non destructive save, so map is still valid. */
1569  res = save_objects(m, fp, fp2, SAVE_FLAG_NO_REMOVE);
1570  if (res < 0) {
1571  LOG(llevError, "Save error during object save: %d\n", res);
1572  of_cancel(&of);
1573  of_cancel(&of2);
1574  return res;
1575  }
1576  m->in_memory = MAP_IN_MEMORY;
1577  } else {
1578  res = save_objects(m, fp, fp2, 0);
1579  if (res < 0) {
1580  LOG(llevError, "Save error during object save: %d\n", res);
1581  of_cancel(&of);
1582  of_cancel(&of2);
1583  return res;
1584  }
1585  free_all_objects(m);
1586  }
1587  if (ftell(fp2) == 0) {
1588  of_cancel(&of2);
1589  unlink(buf);
1590  /* If there are no unique items left on the map, we need to
1591  * unlink the original unique map so that the unique
1592  * items don't show up again.
1593  */
1594  unlink(final_unique);
1595  } else {
1596  if (!of_close(&of2)) {
1597  of_cancel(&of);
1598  return SAVE_ERROR_WRITE;
1599  }
1600  unlink(final_unique); /* failure isn't too bad, maybe the file doesn't exist. */
1601  if (rename(buf, final_unique) == -1) {
1602  LOG(llevError, "Couldn't rename unique file %s to %s\n", buf, final_unique);
1603  of_cancel(&of);
1604  return SAVE_ERROR_URENAME;
1605  }
1606 
1607  if (chmod(final_unique, SAVE_MODE) != 0) {
1608  LOG(llevError, "Could not set permissions on '%s'\n",
1609  final_unique);
1610  }
1611  }
1612  } else { /* save same file when not playing, like in editor */
1613  res = save_objects(m, fp, fp, 0);
1614  if (res < 0) {
1615  LOG(llevError, "Save error during object save: %d\n", res);
1616  of_cancel(&of);
1617  return res;
1618  }
1619  free_all_objects(m);
1620  }
1621 
1622  if (!of_close(&of))
1623  return SAVE_ERROR_CLOSE;
1624  unlink(final); /* failure isn't too bad, maybe the file doesn't exist. */
1625  if (rename(filename, final) == -1) {
1626  LOG(llevError, "Couldn't rename regular file %s to %s\n", filename, final);
1627  return SAVE_ERROR_RRENAME;
1628  }
1629 
1630  if (chmod(final, SAVE_MODE) != 0) {
1631  LOG(llevError, "Could not set permissions on '%s'\n", final);
1632  }
1633 
1634  PROFILE_END(diff,
1635  LOG(llevDebug, "save_map on %s" " took %ld us\n", m->path, diff));
1636 
1637  return SAVE_ERROR_OK;
1638 }
1639 
1649 void clean_object(object *op) {
1650  FOR_INV_PREPARE(op, tmp) {
1651  clean_object(tmp);
1652  if (QUERY_FLAG(tmp, FLAG_IS_LINKED))
1653  remove_button_link(tmp);
1654  object_remove(tmp);
1656  } FOR_INV_FINISH();
1657 }
1658 
1665 static void free_all_objects(mapstruct *m) {
1666  int i, j;
1667  object *op;
1668 
1669  for (i = 0; i < MAP_WIDTH(m); i++)
1670  for (j = 0; j < MAP_HEIGHT(m); j++) {
1671  object *previous_obj = NULL;
1672 
1673  while ((op = GET_MAP_OB(m, i, j)) != NULL) {
1674  if (op == previous_obj) {
1675  LOG(llevDebug, "free_all_objects: Link error, bailing out.\n");
1676  break;
1677  }
1678  previous_obj = op;
1679  op = HEAD(op);
1680 
1681  /* If the map isn't in memory, object_free_drop_inventory() will remove and
1682  * free objects in op's inventory. So let it do the job.
1683  */
1684  if (m->in_memory == MAP_IN_MEMORY)
1685  clean_object(op);
1686  object_remove(op);
1688  }
1689  }
1690 #ifdef MANY_CORES
1691  /* I see periodic cores on metalforge where a map has been swapped out, but apparantly
1692  * an item on that map was not saved - look for that condition and die as appropriate -
1693  * this leaves more of the map data intact for better debugging.
1694  */
1695  for (op = objects; op != NULL; op = op->next) {
1696  if (!QUERY_FLAG(op, FLAG_REMOVED) && op->map == m) {
1697  LOG(llevDebug, "free_all_objects: object %s still on map after it should have been freed\n", op->name);
1698  abort();
1699  }
1700  }
1701 #endif
1702 }
1703 
1713  int i;
1714 
1715  if (!m->in_memory) {
1716  LOG(llevError, "Trying to free freed map.\n");
1717  return;
1718  }
1719 
1720  /* Handle for plugin map unload event. */
1722 
1723  if (m->spaces)
1724  free_all_objects(m);
1725  if (m->name)
1726  FREE_AND_CLEAR(m->name);
1727  if (m->spaces)
1728  FREE_AND_CLEAR(m->spaces);
1729  if (m->msg)
1730  FREE_AND_CLEAR(m->msg);
1731  if (m->maplore)
1732  FREE_AND_CLEAR(m->maplore);
1733  if (m->shopitems)
1735  if (m->shoprace)
1737  if (m->background_music)
1739  if (m->buttons)
1741  m->buttons = NULL;
1742  for (i = 0; i < 4; i++) {
1743  if (m->tile_path[i])
1744  FREE_AND_CLEAR(m->tile_path[i]);
1745  m->tile_map[i] = NULL;
1746  }
1747  m->in_memory = MAP_SWAPPED;
1748 }
1749 
1760  mapstruct *tmp, *last;
1761  int i;
1762 
1763  if (!m)
1764  return;
1765  if (m->in_memory == MAP_IN_MEMORY) {
1766  /* change to MAP_SAVING, even though we are not,
1767  * so that object_remove() doesn't do as much work.
1768  */
1769  m->in_memory = MAP_SAVING;
1770  free_map(m);
1771  }
1772  /* move this out of free_map, since tmpname can still be needed if
1773  * the map is swapped out.
1774  */
1775  free(m->tmpname);
1776  m->tmpname = NULL;
1777  last = NULL;
1778  /* We need to look through all the maps and see if any maps
1779  * are pointing at this one for tiling information. Since
1780  * tiling can be assymetric, we just can not look to see which
1781  * maps this map tiles with and clears those.
1782  */
1783  for (tmp = first_map; tmp != NULL; tmp = tmp->next) {
1784  if (tmp->next == m)
1785  last = tmp;
1786 
1787  /* This should hopefully get unrolled on a decent compiler */
1788  for (i = 0; i < 4; i++)
1789  if (tmp->tile_map[i] == m)
1790  tmp->tile_map[i] = NULL;
1791  }
1792 
1793  /* If last is null, then this should be the first map in the list */
1794  if (!last) {
1795  if (m == first_map)
1796  first_map = m->next;
1797  else
1798  /* m->path is a static char, so should hopefully still have
1799  * some useful data in it.
1800  */
1801  LOG(llevError, "delete_map: Unable to find map %s in list\n", m->path);
1802  } else
1803  last->next = m->next;
1804 
1805  free(m);
1806 }
1807 
1821 mapstruct *ready_map_name(const char *name, int flags) {
1822  mapstruct *m;
1823 
1824  if (!name)
1825  return (NULL);
1826 
1827  /* Have we been at this level before? */
1828  m = has_been_loaded(name);
1829 
1830  /* Map is good to go, so just return it */
1831  if (m && (m->in_memory == MAP_LOADING || m->in_memory == MAP_IN_MEMORY)) {
1832  return m;
1833  }
1834 
1835  /* unique maps always get loaded from their original location, and never
1836  * a temp location. Likewise, if map_flush is set, or we have never loaded
1837  * this map, load it now. I removed the reset checking from here -
1838  * it seems the probability of a player trying to enter a map that should
1839  * reset but hasn't yet is quite low, and removing that makes this function
1840  * a bit cleaner (and players probably shouldn't rely on exact timing for
1841  * resets in any case - if they really care, they should use the 'maps command.
1842  */
1843  if ((flags&(MAP_FLUSH|MAP_PLAYER_UNIQUE)) || !m) {
1844  /* first visit or time to reset */
1845  if (m) {
1846  clean_tmp_map(m); /* Doesn't make much difference */
1847  delete_map(m);
1848  }
1849 
1850  m = mapfile_load(name, (flags&MAP_PLAYER_UNIQUE));
1851  if (m == NULL) return NULL;
1852 
1853  /* If a player unique map, no extra unique object file to load.
1854  * if from the editor, likewise.
1855  */
1856  if (!(flags&(MAP_FLUSH|MAP_PLAYER_UNIQUE)))
1858 
1859  if (!(flags&(MAP_FLUSH|MAP_PLAYER_UNIQUE|MAP_OVERLAY))) {
1860  if (load_overlay_map(name, m) != 0) {
1861  delete_map(m);
1862  m = mapfile_load(name, 0);
1863  if (m == NULL) {
1864  /* Really, this map is bad :( */
1865  return NULL;
1866  }
1867  }
1868  }
1869  } else {
1870  /* If in this loop, we found a temporary map, so load it up. */
1871 
1872  if (load_temporary_map(m) != 0) {
1873  /*
1874  * There was a failure loading the temporary map, fall back to original one.
1875  * load_temporary_map() already logged the error.
1876  */
1877  delete_map(m);
1878  m = mapfile_load(name, 0);
1879  if (m == NULL) {
1880  /* Really, this map is bad :( */
1881  return NULL;
1882  }
1883  }
1885 
1886  clean_tmp_map(m);
1887  m->in_memory = MAP_IN_MEMORY;
1888  /* tempnam() on sun systems (probably others) uses malloc
1889  * to allocated space for the string. Free it here.
1890  * In some cases, load_temporary_map above won't find the
1891  * temporary map, and so has reloaded a new map. If that
1892  * is the case, tmpname is now null
1893  */
1894  free(m->tmpname);
1895  m->tmpname = NULL;
1896  /* It's going to be saved anew anyway */
1897  }
1898 
1899  /* Below here is stuff common to both first time loaded maps and
1900  * temp maps.
1901  */
1902 
1903  decay_objects(m); /* start the decay */
1904 
1905  if (m->outdoor)
1906  set_darkness_map(m);
1907 
1908  if (!(flags&(MAP_FLUSH))) {
1909  if (m->last_reset_time == 0) {
1910  m->last_reset_time = seconds();
1911  }
1912  }
1913  return m;
1914 }
1915 
1932  archetype *at;
1933  int x, y;
1934  int diff = 0;
1935  int i;
1936  int64_t exp_pr_sq, total_exp = 0;
1937 
1938  if (MAP_DIFFICULTY(m)) {
1939  return MAP_DIFFICULTY(m);
1940  }
1941 
1942  for (x = 0; x < MAP_WIDTH(m); x++)
1943  for (y = 0; y < MAP_HEIGHT(m); y++)
1944  FOR_MAP_PREPARE(m, x, y, op) {
1945  if (QUERY_FLAG(op, FLAG_MONSTER))
1946  total_exp += op->stats.exp;
1947  if (QUERY_FLAG(op, FLAG_GENERATOR)) {
1948  total_exp += op->stats.exp;
1950  if (at != NULL)
1951  total_exp += at->clone.stats.exp*8;
1952  }
1953  } FOR_MAP_FINISH();
1954 
1955  exp_pr_sq = ((double)1000*total_exp)/(MAP_WIDTH(m)*MAP_HEIGHT(m)+1);
1956  diff = 20;
1957  for (i = 1; i < 20; i++)
1958  if (exp_pr_sq <= level_exp(i, 1.0)) {
1959  diff = i;
1960  break;
1961  }
1962 
1963  return diff;
1964 }
1965 
1973  if (m->tmpname == NULL)
1974  return;
1975  (void)unlink(m->tmpname);
1976 }
1977 
1981 void free_all_maps(void) {
1982  int real_maps = 0;
1983 
1984  while (first_map) {
1985  /* I think some of the callers above before it gets here set this to be
1986  * saving, but we still want to free this data
1987  */
1988  if (first_map->in_memory == MAP_SAVING)
1991  real_maps++;
1992  }
1993  LOG(llevDebug, "free_all_maps: Freed %d maps\n", real_maps);
1994 }
1995 
2013 int change_map_light(mapstruct *m, int change) {
2014  int new_level = m->darkness+change;
2015 
2016  /* Nothing to do */
2017  if (!change
2018  || (new_level <= 0 && m->darkness == 0)
2019  || (new_level >= MAX_DARKNESS && m->darkness >= MAX_DARKNESS)) {
2020  return 0;
2021  }
2022 
2023  /* inform all players on the map */
2024  if (change > 0)
2025  ext_info_map(NDI_BLACK, m, MSG_TYPE_MISC, MSG_SUBTYPE_NONE, "It becomes darker.");
2026  else
2027  ext_info_map(NDI_BLACK, m, MSG_TYPE_MISC, MSG_SUBTYPE_NONE, "It becomes brighter.");
2028 
2029  /* Do extra checking. since m->darkness is a unsigned value,
2030  * we need to be extra careful about negative values.
2031  * In general, the checks below are only needed if change
2032  * is not +/-1
2033  */
2034  if (new_level < 0)
2035  m->darkness = 0;
2036  else if (new_level >= MAX_DARKNESS)
2037  m->darkness = MAX_DARKNESS;
2038  else
2039  m->darkness = new_level;
2040 
2041  /* All clients need to get re-updated for the change */
2042  update_all_map_los(m);
2043  return 1;
2044 }
2045 
2068 static inline void add_face_layer(int low_layer, int high_layer, object *ob, object *layers[], int honor_visibility) {
2069  int l, l1;
2070  object *tmp;
2071 
2072  for (l = low_layer; l <= high_layer; l++) {
2073  if (!layers[l]) {
2074  /* found an empty spot. now, we want to make sure
2075  * highest visibility at top, etc.
2076  */
2077  layers[l] = ob;
2078  if (!honor_visibility)
2079  return;
2080 
2081  /* This is basically a mini bubble sort. Only swap
2082  * position if the lower face has greater (not equal)
2083  * visibility - map stacking is secondary consideration here.
2084  */
2085  for (l1 = (l-1); l1 >= low_layer; l1--) {
2086  if (layers[l1]->face->visibility > layers[l1+1]->face->visibility) {
2087  tmp = layers[l1+1];
2088  layers[l1+1] = layers[l1];
2089  layers[l1] = tmp;
2090  }
2091  }
2092  /* Nothing more to do - face inserted */
2093  return;
2094  }
2095  }
2096  /* If we get here, all the layers have an object..
2097  */
2098  if (!honor_visibility) {
2099  /* Basically, in this case, it is pure stacking logic, so
2100  * new object goes on the top.
2101  */
2102  for (l = low_layer; l < high_layer; l++)
2103  layers[l] = layers[l+1];
2104  layers[high_layer] = ob;
2105  /* If this object doesn't have higher visibility than
2106  * the lowest object, no reason to go further.
2107  */
2108  } else if (ob->face->visibility >= layers[low_layer]->face->visibility) {
2109  /*
2110  * Start at the top (highest visibility) layer and work down.
2111  * once this face exceed that of the layer, push down those
2112  * other layers, and then replace the layer with our object.
2113  */
2114  for (l = high_layer; l >= low_layer; l--) {
2115  if (ob->face->visibility >= layers[l]->face->visibility) {
2116  for (l1 = low_layer; l1 < l; l1++)
2117  layers[l1] = layers[l1+1];
2118  layers[l] = ob;
2119  break;
2120  }
2121  }
2122  }
2123 }
2124 
2137 void update_position(mapstruct *m, int x, int y) {
2138  object *player = NULL;
2139  uint8_t flags = 0, oldflags, light = 0;
2140  object *layers[MAP_LAYERS];
2141 
2142  MoveType move_block = 0, move_slow = 0, move_on = 0, move_off = 0, move_allow = 0;
2143 
2144  oldflags = GET_MAP_FLAGS(m, x, y);
2145  if (!(oldflags&P_NEED_UPDATE)) {
2146  LOG(llevDebug, "update_position called with P_NEED_UPDATE not set: %s (%d, %d)\n", m->path, x, y);
2147  return;
2148  }
2149 
2150  memset(layers, 0, MAP_LAYERS*sizeof(object *));
2151 
2152  FOR_MAP_PREPARE(m, x, y, tmp) {
2153  /* DMs just don't do anything when hidden, including no light. */
2154  if (QUERY_FLAG(tmp, FLAG_WIZ) && tmp->contr->hidden)
2155  continue;
2156 
2157  if (tmp->type == PLAYER)
2158  player = tmp;
2159 
2160  /* This could be made additive I guess (two lights better than
2161  * one). But if so, it shouldn't be a simple additive - 2
2162  * light bulbs do not illuminate twice as far as once since
2163  * it is a dissipation factor that is squared (or is it cubed?)
2164  */
2165  if (tmp->glow_radius > light)
2166  light = tmp->glow_radius;
2167 
2168  /* if this object is visible and not a blank face,
2169  * update the objects that show how this space
2170  * looks.
2171  */
2172  if (!tmp->invisible && tmp->face != blank_face) {
2173  if (tmp->map_layer) {
2174  add_face_layer(tmp->map_layer, map_layer_info[tmp->map_layer].high_layer,
2175  tmp, layers, map_layer_info[tmp->map_layer].honor_visibility);
2176  } else if (tmp->move_type&MOVE_FLYING) {
2177  add_face_layer(MAP_LAYER_FLY1, MAP_LAYER_FLY2, tmp, layers, 1);
2178  } else if ((tmp->type == PLAYER || QUERY_FLAG(tmp, FLAG_MONSTER))) {
2180  } else if (QUERY_FLAG(tmp, FLAG_IS_FLOOR)) {
2181  layers[MAP_LAYER_FLOOR] = tmp;
2182  /* floors hide everything else */
2183  memset(layers+1, 0, (MAP_LAYERS-1)*sizeof(object *));
2184  /* Check for FLAG_SEE_ANYWHERE is removed - objects
2185  * with that flag should just have a high visibility
2186  * set - we shouldn't need special code here.
2187  */
2188  } else if (QUERY_FLAG(tmp, FLAG_NO_PICK)) {
2190  } else {
2192  }
2193  }
2194  if (tmp == tmp->above) {
2195  LOG(llevError, "Error in structure of map\n");
2196  exit(-1);
2197  }
2198 
2199  move_slow |= tmp->move_slow;
2200  move_block |= tmp->move_block;
2201  move_on |= tmp->move_on;
2202  move_off |= tmp->move_off;
2203  move_allow |= tmp->move_allow;
2204 
2205  if (QUERY_FLAG(tmp, FLAG_ALIVE))
2206  flags |= P_IS_ALIVE;
2207  if (QUERY_FLAG(tmp, FLAG_NO_MAGIC))
2208  flags |= P_NO_MAGIC;
2209  if (QUERY_FLAG(tmp, FLAG_DAMNED))
2210  flags |= P_NO_CLERIC;
2211 
2212  if (QUERY_FLAG(tmp, FLAG_BLOCKSVIEW))
2213  flags |= P_BLOCKSVIEW;
2214  } FOR_MAP_FINISH(); /* for stack of objects */
2215 
2216  if (player)
2217  flags |= P_PLAYER;
2218 
2219  /* we don't want to rely on this function to have accurate flags, but
2220  * since we're already doing the work, we calculate them here.
2221  * if they don't match, logic is broken someplace.
2222  */
2223  if (((oldflags&~(P_NEED_UPDATE|P_NO_ERROR)) != flags)
2224  && (!(oldflags&P_NO_ERROR))) {
2225  LOG(llevDebug, "update_position: updated flags do not match old flags: %s (x=%d,y=%d) %x != %x\n",
2226  m->path, x, y, (oldflags&~P_NEED_UPDATE), flags);
2227  }
2228 
2229  SET_MAP_FLAGS(m, x, y, flags);
2230  SET_MAP_MOVE_BLOCK(m, x, y, move_block&~move_allow);
2231  SET_MAP_MOVE_ON(m, x, y, move_on);
2232  SET_MAP_MOVE_OFF(m, x, y, move_off);
2233  SET_MAP_MOVE_SLOW(m, x, y, move_slow);
2234  SET_MAP_LIGHT(m, x, y, light);
2235 
2236  /* Note that player may be NULL here, which is fine - if no player, need
2237  * to clear any value that may be set.
2238  */
2239  SET_MAP_PLAYER(m, x, y, player);
2240 
2241  /* Note it is intentional we copy everything, including NULL values. */
2242  memcpy(GET_MAP_FACE_OBJS(m, x, y), layers, sizeof(object *)*MAP_LAYERS);
2243 }
2244 
2252  int timeout;
2253 
2254  timeout = MAP_RESET_TIMEOUT(map);
2255  if (timeout <= 0)
2256  timeout = MAP_DEFAULTRESET;
2257  if (timeout >= MAP_MAXRESET)
2258  timeout = MAP_MAXRESET;
2259  MAP_RESET_TIMEOUT(map) = timeout;
2260  MAP_WHEN_RESET(map) = seconds()+timeout;
2261 }
2262 
2277 static mapstruct *load_and_link_tiled_map(mapstruct *orig_map, int tile_num) {
2278  int dest_tile = (tile_num+2)%4;
2279  char path[HUGE_BUF];
2280 
2281  path_combine_and_normalize(orig_map->path, orig_map->tile_path[tile_num], path, sizeof(path));
2282 
2283  orig_map->tile_map[tile_num] = ready_map_name(path, 0);
2284  if (orig_map->tile_map[tile_num] == NULL) {
2285  return NULL;
2286  }
2287 
2288  /* need to do a strcmp here as the orig_map->path is not a shared string */
2289  if (orig_map->tile_map[tile_num]->tile_path[dest_tile]
2290  && !strcmp(orig_map->tile_map[tile_num]->tile_path[dest_tile], orig_map->path))
2291  orig_map->tile_map[tile_num]->tile_map[dest_tile] = orig_map;
2292 
2293  return orig_map->tile_map[tile_num];
2294 }
2295 
2313 int out_of_map(mapstruct *m, int x, int y) {
2314 
2315  /* Simple case - coordinates are within this local
2316  * map.
2317  */
2318  if (x >= 0 && x < MAP_WIDTH(m) && y >= 0 && y < MAP_HEIGHT(m))
2319  return 0;
2320 
2321  if (x < 0) {
2322  if (!m->tile_path[3])
2323  return 1;
2324  if (!m->tile_map[3] || m->tile_map[3]->in_memory != MAP_IN_MEMORY) {
2326  /* Verify the tile map loaded correctly */
2327  if (!m->tile_map[3])
2328  return 0;
2329  }
2330  return (out_of_map(m->tile_map[3], x+MAP_WIDTH(m->tile_map[3]), y));
2331  }
2332  if (x >= MAP_WIDTH(m)) {
2333  if (!m->tile_path[1])
2334  return 1;
2335  if (!m->tile_map[1] || m->tile_map[1]->in_memory != MAP_IN_MEMORY) {
2337  /* Verify the tile map loaded correctly */
2338  if (!m->tile_map[1])
2339  return 0;
2340  }
2341  return (out_of_map(m->tile_map[1], x-MAP_WIDTH(m), y));
2342  }
2343  if (y < 0) {
2344  if (!m->tile_path[0])
2345  return 1;
2346  if (!m->tile_map[0] || m->tile_map[0]->in_memory != MAP_IN_MEMORY) {
2348  /* Verify the tile map loaded correctly */
2349  if (!m->tile_map[0])
2350  return 0;
2351  }
2352  return (out_of_map(m->tile_map[0], x, y+MAP_HEIGHT(m->tile_map[0])));
2353  }
2354  if (y >= MAP_HEIGHT(m)) {
2355  if (!m->tile_path[2])
2356  return 1;
2357  if (!m->tile_map[2] || m->tile_map[2]->in_memory != MAP_IN_MEMORY) {
2359  /* Verify the tile map loaded correctly */
2360  if (!m->tile_map[2])
2361  return 0;
2362  }
2363  return (out_of_map(m->tile_map[2], x, y-MAP_HEIGHT(m)));
2364  }
2365  return 1;
2366 }
2367 
2388 
2389  /* Simple case - coordinates are within this local
2390  * map.
2391  */
2392 
2393  if (*x >= 0 && *x < MAP_WIDTH(m) && *y >= 0 && *y < MAP_HEIGHT(m))
2394  return m;
2395 
2396  do /* With the first case there, we can assume we are out of the map if we get here */
2397  {
2398  // Figure out what map should be in the direction we are off the map, and then
2399  // load that map and look again.
2400  if (*x < 0) {
2401  if (!m->tile_path[3])
2402  return NULL;
2403  if (!m->tile_map[3] || m->tile_map[3]->in_memory != MAP_IN_MEMORY){
2405  /* Make sure we loaded properly. */
2406  if (!m->tile_map[3])
2407  return NULL;
2408  }
2409  *x += MAP_WIDTH(m->tile_map[3]);
2410  m = m->tile_map[3];
2411  }
2412  else if (*x >= MAP_WIDTH(m)) {
2413  if (!m->tile_path[1])
2414  return NULL;
2415  if (!m->tile_map[1] || m->tile_map[1]->in_memory != MAP_IN_MEMORY){
2417  /* Make sure we loaded properly. */
2418  if (!m->tile_map[1])
2419  return NULL;
2420  }
2421  *x -= MAP_WIDTH(m);
2422  m = m->tile_map[1];
2423  }
2424  // It is possible that x and y be considered separate compare groups,
2425  // But using an else-if here retains the old behavior that recursion produced.
2426  else if (*y < 0) {
2427  if (!m->tile_path[0])
2428  return NULL;
2429  if (!m->tile_map[0] || m->tile_map[0]->in_memory != MAP_IN_MEMORY){
2431  /* Make sure we loaded properly. */
2432  if (!m->tile_map[0])
2433  return NULL;
2434  }
2435  *y += MAP_HEIGHT(m->tile_map[0]);
2436  m = m->tile_map[0];
2437  }
2438  else if (*y >= MAP_HEIGHT(m)) {
2439  if (!m->tile_path[2])
2440  return NULL;
2441  if (!m->tile_map[2] || m->tile_map[2]->in_memory != MAP_IN_MEMORY){
2443  /* Make sure we loaded properly. */
2444  if (!m->tile_map[2])
2445  return NULL;
2446  }
2447  *y -= MAP_HEIGHT(m);
2448  m = m->tile_map[2];
2449  }
2450  // The check here is if our single tile is in the map.
2451  // That is exactly what the OUT_OF_MAP macro does.
2452  } while (OUT_OF_REAL_MAP(m, *x, *y));
2453  return m; /* We have found our map */
2454 }
2455 
2469 static int adjacent_map(const mapstruct *map1, const mapstruct *map2, int *dx, int *dy) {
2470  if (!map1 || !map2)
2471  return 0;
2472 
2473  if (map1 == map2) {
2474  *dx = 0;
2475  *dy = 0;
2476  } else if (map1->tile_map[0] == map2) { /* up */
2477  *dx = 0;
2478  *dy = -MAP_HEIGHT(map2);
2479  } else if (map1->tile_map[1] == map2) { /* right */
2480  *dx = MAP_WIDTH(map1);
2481  *dy = 0;
2482  } else if (map1->tile_map[2] == map2) { /* down */
2483  *dx = 0;
2484  *dy = MAP_HEIGHT(map1);
2485  } else if (map1->tile_map[3] == map2) { /* left */
2486  *dx = -MAP_WIDTH(map2);
2487  *dy = 0;
2488  } else if (map1->tile_map[0] && map1->tile_map[0]->tile_map[1] == map2) { /* up right */
2489  *dx = MAP_WIDTH(map1->tile_map[0]);
2490  *dy = -MAP_HEIGHT(map1->tile_map[0]);
2491  } else if (map1->tile_map[0] && map1->tile_map[0]->tile_map[3] == map2) { /* up left */
2492  *dx = -MAP_WIDTH(map2);
2493  *dy = -MAP_HEIGHT(map1->tile_map[0]);
2494  } else if (map1->tile_map[1] && map1->tile_map[1]->tile_map[0] == map2) { /* right up */
2495  *dx = MAP_WIDTH(map1);
2496  *dy = -MAP_HEIGHT(map2);
2497  } else if (map1->tile_map[1] && map1->tile_map[1]->tile_map[2] == map2) { /* right down */
2498  *dx = MAP_WIDTH(map1);
2499  *dy = MAP_HEIGHT(map1->tile_map[1]);
2500  } else if (map1->tile_map[2] && map1->tile_map[2]->tile_map[1] == map2) { /* down right */
2501  *dx = MAP_WIDTH(map1->tile_map[2]);
2502  *dy = MAP_HEIGHT(map1);
2503  } else if (map1->tile_map[2] && map1->tile_map[2]->tile_map[3] == map2) { /* down left */
2504  *dx = -MAP_WIDTH(map2);
2505  *dy = MAP_HEIGHT(map1);
2506  } else if (map1->tile_map[3] && map1->tile_map[3]->tile_map[0] == map2) { /* left up */
2507  *dx = -MAP_WIDTH(map1->tile_map[3]);
2508  *dy = -MAP_HEIGHT(map2);
2509  } else if (map1->tile_map[3] && map1->tile_map[3]->tile_map[2] == map2) { /* left down */
2510  *dx = -MAP_WIDTH(map1->tile_map[3]);
2511  *dy = MAP_HEIGHT(map1->tile_map[3]);
2512  } else { /* not "adjacent" enough */
2513  return 0;
2514  }
2515 
2516  return 1;
2517 }
2518 
2546 int get_rangevector(object *op1, const object *op2, rv_vector *retval, int flags) {
2547  if (!adjacent_map(op1->map, op2->map, &retval->distance_x, &retval->distance_y)) {
2548  /* be conservative and fill in _some_ data */
2549  retval->distance = 100000;
2550  retval->distance_x = 32767;
2551  retval->distance_y = 32767;
2552  retval->direction = 0;
2553  retval->part = NULL;
2554  return 0;
2555  } else {
2556  object *best;
2557 
2558  retval->distance_x += op2->x-op1->x;
2559  retval->distance_y += op2->y-op1->y;
2560 
2561  best = op1;
2562  /* If this is multipart, find the closest part now */
2563  if (!(flags&0x1) && op1->more) {
2564  object *tmp;
2565  int best_distance = retval->distance_x*retval->distance_x+
2566  retval->distance_y*retval->distance_y, tmpi;
2567 
2568  /* we just take the offset of the piece to head to figure
2569  * distance instead of doing all that work above again
2570  * since the distance fields we set above are positive in the
2571  * same axis as is used for multipart objects, the simply arithmetic
2572  * below works.
2573  */
2574  for (tmp = op1->more; tmp != NULL; tmp = tmp->more) {
2575  tmpi = (op1->x-tmp->x+retval->distance_x)*(op1->x-tmp->x+retval->distance_x)+
2576  (op1->y-tmp->y+retval->distance_y)*(op1->y-tmp->y+retval->distance_y);
2577  if (tmpi < best_distance) {
2578  best_distance = tmpi;
2579  best = tmp;
2580  }
2581  }
2582  if (best != op1) {
2583  retval->distance_x += op1->x-best->x;
2584  retval->distance_y += op1->y-best->y;
2585  }
2586  }
2587  retval->part = best;
2588  retval->distance = isqrt(retval->distance_x*retval->distance_x+retval->distance_y*retval->distance_y);
2589  retval->direction = find_dir_2(-retval->distance_x, -retval->distance_y);
2590  return 1;
2591  }
2592 }
2593 
2619 int get_rangevector_from_mapcoord(const mapstruct *m, int x, int y, const object *op2, rv_vector *retval, int flags) {
2620  (void)flags; // unused [avoid compiler warning]
2621  if (!adjacent_map(m, op2->map, &retval->distance_x, &retval->distance_y)) {
2622  /* be conservative and fill in _some_ data */
2623  retval->distance = 100000;
2624  retval->distance_x = 32767;
2625  retval->distance_y = 32767;
2626  retval->direction = 0;
2627  retval->part = NULL;
2628  return 0;
2629  } else {
2630  retval->distance_x += op2->x-x;
2631  retval->distance_y += op2->y-y;
2632 
2633  retval->part = NULL;
2634  retval->distance = isqrt(retval->distance_x*retval->distance_x+retval->distance_y*retval->distance_y);
2635  retval->direction = find_dir_2(-retval->distance_x, -retval->distance_y);
2636  return 1;
2637  }
2638 }
2639 
2658 int on_same_map(const object *op1, const object *op2) {
2659  int dx, dy;
2660 
2661  return adjacent_map(op1->map, op2->map, &dx, &dy);
2662 }
2663 
2681 object *map_find_by_flag(mapstruct *map, int x, int y, int flag) {
2682  object *tmp;
2683 
2684  for (tmp = GET_MAP_OB(map, x, y); tmp != NULL; tmp = tmp->above) {
2685  object *head;
2686 
2687  head = HEAD(tmp);
2688  if (QUERY_FLAG(head, flag))
2689  return head;
2690  }
2691  return NULL;
2692 }
2693 
2699  char base[HUGE_BUF], path[HUGE_BUF];
2700  int count;
2701 
2702  if (map->unique) {
2703  /* Unique maps have their full path already set. */
2704  unlink(map->path);
2705  return;
2706  }
2707 
2708  create_items_path(map->path, base, sizeof(base));
2709 
2710  for (count = 0; count < 10; count++) {
2711  snprintf(path, sizeof(path), "%s.v%02d", base, count);
2712  unlink(path);
2713  }
2714 }
2715 
2721 const char *map_get_path(const object *item) {
2722  if (item->map != NULL) {
2723  if (strlen(item->map->path) > 0) {
2724  return item->map->path;
2725  }
2726 
2727  return item->map->name ? item->map->name : "(empty path and name)";
2728  }
2729 
2730  if (item->env != NULL)
2731  return map_get_path(item->env);
2732 
2733  return "(no map and no env!)";
2734 }
#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:3616
void free_all_maps(void)
Definition: map.c:1981
#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:1232
#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:839
#define MAP_STYLE
Definition: map.h:98
#define FLAG_IS_LINKED
Definition: define.h:316
MoveType move_type
Definition: object.h:426
#define S_IRGRP
Definition: win32.h:88
mapstruct * get_linked_map(void)
Definition: map.c:788
Definition: map.h:380
int64_t level_exp(int level, double expmul)
Definition: living.c:1868
#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:2277
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:869
#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:2619
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:825
#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:1821
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:891
#define FMT64U
Definition: compat.h:13
#define HUGE_BUF
Definition: define.h:37
void object_fix_multipart(object *tmp)
Definition: object.c:4583
#define MAP_MAXRESET
Definition: config.h:424
mapstruct * get_map_from_coord(mapstruct *m, int16_t *x, int16_t *y)
Definition: map.c:2387
#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:472
int calculate_difficulty(mapstruct *m)
Definition: map.c:1931
#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:1712
#define safe_strncpy
Definition: compat.h:23
static int load_temporary_map(mapstruct *m)
Definition: map.c:1295
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:311
Definition: object.h:467
const char * map_get_path(const object *item)
Definition: map.c:2721
int change_map_light(mapstruct *m, int change)
Definition: map.c:2013
#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:1933
static int load_map_header(FILE *fp, mapstruct *m)
Definition: map.c:1022
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:571
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:257
#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:2137
#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:5270
struct archt * more
Definition: object.h:471
int execute_global_event(int eventcode,...)
Definition: main.c:370
char * tile_path[4]
Definition: map.h:363
#define INS_NO_WALK_ON
Definition: object.h:570
#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:256
#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:1366
#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:2658
#define QUERY_FLAG(xyz, p)
Definition: define.h:225
#define CLEAR_FLAG(xyz, p)
Definition: define.h:224
#define HEAD(op)
Definition: object.h:594
#define INS_ABOVE_FLOOR_ONLY
Definition: object.h:569
#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:255
#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:2469
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:1442
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:248
object * check_inv_recursive(object *op, const object *trig)
Definition: button.c:786
static void free_all_objects(mapstruct *m)
Definition: map.c:1665
#define SET_MAP_MOVE_ON(M, X, Y, C)
Definition: map.h:204
void set_map_reset_time(mapstruct *map)
Definition: map.c:2251
#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:568
static void load_unique_objects(mapstruct *m)
Definition: map.c:1391
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:2681
New_Face * blank_face
Definition: image.c:39
#define MAP_PLAYER_UNIQUE
Definition: map.h:97
const char * localdir
Definition: global.h:249
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:369
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:414
#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:251
uint32_t reset_timeout
Definition: map.h:333
static void print_shop_string(mapstruct *m, char *output_string, int size)
Definition: map.c:977
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:339
struct Settings settings
Definition: init.c:39
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:2313
void delete_map(mapstruct *m)
Definition: map.c:1759
void apply_auto_fix(mapstruct *m)
Definition: main.c:262
#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:365
#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:349
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:1331
int get_rangevector(object *op1, const object *op2, rv_vector *retval, int flags)
Definition: map.c:2546
void ext_info_map(int color, const mapstruct *map, uint8_t type, uint8_t subtype, const char *str1)
Definition: main.c:326
#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:2698
struct regiondef * region
Definition: map.h:329
void clean_tmp_map(mapstruct *m)
Definition: map.c:1972
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:1649
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:1666
static bool in_localdir(const char path[static 1])
Definition: map.c:1423
static void add_face_layer(int low_layer, int high_layer, object *ob, object *layers[], int honor_visibility)
Definition: map.c:2068
#define INS_MAP_LOAD
Definition: object.h:573