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