Crossfire Server, Branches 1.12  R18729
map.c
Go to the documentation of this file.
1 /*
2  * static char *rcsid_map_c =
3  * "$Id: map.c 13939 2010-09-29 19:21:57Z ryo_saeba $";
4  */
5 
6 /*
7  CrossFire, A Multiplayer game for X-windows
8 
9  Copyright (C) 2006-2008 Mark Wedel & Crossfire Development Team
10  Copyright (C) 1992 Frank Tore Johansen
11 
12  This program is free software; you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 2 of the License, or
15  (at your option) any later version.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 
26  The authors can be reached via e-mail at crossfire-devel@real-time.com
27 */
28 
34 #include <stdio.h>
35 #include <global.h>
36 #include <sproto.h>
37 
38 #include <loader.h>
39 #ifndef WIN32 /* ---win32 exclude header */
40 #include <unistd.h>
41 #endif /* win32 */
42 
43 #include "path.h"
44 
46 
47 static void free_all_objects(mapstruct *m);
48 
54 const char *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 
114 char *create_pathname(const char *name, char *buf, size_t size) {
115  /* Why? having extra / doesn't confuse unix anyplace? Dependancies
116  * someplace else in the code? msw 2-17-97
117  */
118  if (*name == '/')
119  snprintf(buf, size, "%s/%s%s", settings.datadir, settings.mapdir, name);
120  else
121  snprintf(buf, size, "%s/%s/%s", settings.datadir, settings.mapdir, name);
122  return buf;
123 }
124 
135 void create_overlay_pathname(const char *name, char *buf, size_t size) {
136  /* Why? having extra / doesn't confuse unix anyplace? Dependancies
137  * someplace else in the code? msw 2-17-97
138  */
139  if (*name == '/')
140  snprintf(buf, size, "%s/%s%s", settings.localdir, settings.mapdir, name);
141  else
142  snprintf(buf, size, "%s/%s/%s", settings.localdir, settings.mapdir, name);
143 }
144 
155 void create_template_pathname(const char *name, char *buf, size_t size) {
156  /* Why? having extra / doesn't confuse unix anyplace? Dependancies
157  * someplace else in the code? msw 2-17-97
158  */
159  if (*name == '/')
160  snprintf(buf, size, "%s/%s%s", settings.localdir, settings.templatedir, name);
161  else
162  snprintf(buf, size, "%s/%s/%s", settings.localdir, settings.templatedir, name);
163 }
164 
178 static void create_items_path(const char *s, char *buf, size_t size) {
179  char *t;
180 
181  if (*s == '/')
182  s++;
183 
184  snprintf(buf, size, "%s/%s/", settings.localdir, settings.uniquedir);
185  t = buf+strlen(buf);
186  snprintf(t, buf+size-t, "%s", s);
187 
188  while (*t != '\0') {
189  if (*t == '/')
190  *t = '@';
191  t++;
192  }
193 }
194 
216 int check_path(const char *name, int prepend_dir) {
217  char buf[MAX_BUF];
218 #ifndef WIN32
219  char *endbuf;
220  struct stat statbuf;
221  int mode = 0, i;
222 #endif
223 
224  if (prepend_dir)
225  create_pathname(name, buf, MAX_BUF);
226  else
227  snprintf(buf, sizeof(buf), "%s", name);
228 #ifdef WIN32 /* ***win32: check this sucker in windows style. */
229  return(_access(buf, 0));
230 #else
231 
232  /* old method (strchr(buf, '\0')) seemd very odd to me -
233  * this method should be equivalant and is clearer.
234  * Can not use strcat because we need to cycle through
235  * all the names.
236  */
237  endbuf = buf+strlen(buf);
238 
239  for (i = 0; i < NROF_COMPRESS_METHODS; i++) {
240  if (uncomp[i][0])
241  snprintf(endbuf, buf+sizeof(buf)-endbuf, "%s", uncomp[i][0]);
242  else
243  *endbuf = '\0';
244  if (!stat(buf, &statbuf))
245  break;
246  }
247  if (i == NROF_COMPRESS_METHODS)
248  return (-1);
249  if (!S_ISREG(statbuf.st_mode))
250  return (-1);
251 
252  if (((statbuf.st_mode&S_IRGRP) && getegid() == statbuf.st_gid)
253  || ((statbuf.st_mode&S_IRUSR) && geteuid() == statbuf.st_uid)
254  || (statbuf.st_mode&S_IROTH))
255  mode |= 4;
256 
257  if ((statbuf.st_mode&S_IWGRP && getegid() == statbuf.st_gid)
258  || (statbuf.st_mode&S_IWUSR && geteuid() == statbuf.st_uid)
259  || (statbuf.st_mode&S_IWOTH))
260  mode |= 2;
261 
262  return (mode);
263 #endif
264 }
265 
275 void dump_map(const mapstruct *m) {
276  LOG(llevError, "Map %s status: %d.\n", m->path, m->in_memory);
277  LOG(llevError, "Size: %dx%d Start: %d,%d\n", MAP_WIDTH(m), MAP_HEIGHT(m), MAP_ENTER_X(m), MAP_ENTER_Y(m));
278 
279  if (m->msg != NULL)
280  LOG(llevError, "Message:\n%s", m->msg);
281 
282  if (m->maplore != NULL)
283  LOG(llevError, "Lore:\n%s", m->maplore);
284 
285  if (m->tmpname != NULL)
286  LOG(llevError, "Tmpname: %s\n", m->tmpname);
287 
288  LOG(llevError, "Difficulty: %d\n", m->difficulty);
289  LOG(llevError, "Darkness: %d\n", m->darkness);
290 }
291 
298 void dump_all_maps(void) {
299  mapstruct *m;
300 
301  for (m = first_map; m != NULL; m = m->next) {
302  dump_map(m);
303  }
304 }
305 
330 int get_map_flags(mapstruct *oldmap, mapstruct **newmap, sint16 x, sint16 y, sint16 *nx, sint16 *ny) {
331  sint16 newx, newy;
332  int retval = 0;
333  mapstruct *mp;
334 
335  if (out_of_map(oldmap, x, y))
336  return P_OUT_OF_MAP;
337  newx = x;
338  newy = y;
339  mp = get_map_from_coord(oldmap, &newx, &newy);
340  if (mp != oldmap)
341  retval |= P_NEW_MAP;
342  if (newmap)
343  *newmap = mp;
344  if (nx)
345  *nx = newx;
346  if (ny)
347  *ny = newy;
348  retval |= mp->spaces[newx+mp->width*newy].flags;
349  return retval;
350 }
351 
373 int blocked_link(object *ob, mapstruct *m, int sx, int sy) {
374  object *tmp, *tmp_head;
375  int mflags, blocked;
376 
377  /* Make sure the coordinates are valid - they should be, as caller should
378  * have already checked this.
379  */
380  if (OUT_OF_REAL_MAP(m, sx, sy)) {
381  LOG(llevError, "blocked_link: Passed map, x, y coordinates outside of map\n");
382  return 1;
383  }
384 
385  /* special hack for transports: if it's a transport with a move_type of 0, it can do on the space anyway */
386  if (ob->type == TRANSPORT && ob->move_type == 0)
387  return 0;
388 
389  /* Save some cycles - instead of calling get_map_flags(), just get the value
390  * directly.
391  */
392  mflags = m->spaces[sx+m->width*sy].flags;
393 
394  blocked = GET_MAP_MOVE_BLOCK(m, sx, sy);
395 
396  /* If space is currently not blocked by anything, no need to
397  * go further. Not true for players - all sorts of special
398  * things we need to do for players.
399  */
400  if (ob->type != PLAYER && !(mflags&P_IS_ALIVE) && (blocked == 0))
401  return 0;
402 
403  /* if there isn't anytyhing alive on this space, and this space isn't
404  * otherwise blocked, we can return now. Only if there is a living
405  * creature do we need to investigate if it is part of this creature
406  * or another. Likewise, only if something is blocking us do we
407  * need to investigate if there is a special circumstance that would
408  * let the player through (inventory checkers for example)
409  */
410  if (!(mflags&P_IS_ALIVE) && !OB_TYPE_MOVE_BLOCK(ob, blocked))
411  return 0;
412 
413  if (ob->head != NULL)
414  ob = ob->head;
415 
416  /* We basically go through the stack of objects, and if there is
417  * some other object that has NO_PASS or FLAG_ALIVE set, return
418  * true. If we get through the entire stack, that must mean
419  * ob is blocking it, so return 0.
420  */
421  for (tmp = GET_MAP_OB(m, sx, sy); tmp != NULL; tmp = tmp->above) {
422  /* Never block part of self. */
423  if (tmp->head)
424  tmp_head = tmp->head;
425  else
426  tmp_head = tmp;
427  if (tmp_head == ob) {
428  continue;
429  /* This must be before the checks below. Code for inventory checkers. */
430  } else if (tmp->type == CHECK_INV && OB_MOVE_BLOCK(ob, tmp)) {
431  /* If last_sp is set, the player/monster needs an object,
432  * so we check for it. If they don't have it, they can't
433  * pass through this space.
434  */
435  if (tmp->last_sp) {
436  if (check_inv_recursive(ob, tmp) == NULL) {
437  if (tmp->msg) {
438  /* Optionally display the reason why one cannot move
439  * there. Note: emitting a message from this function
440  * is not very elegant. Ideally, this should be done
441  * somewhere in server/player.c, but this is difficult
442  * for objects of type CHECK_INV that are not alive.
443  */
446  tmp->msg, tmp->msg);
447  }
448  return 1;
449  }
450  else
451  continue;
452  } else {
453  /* In this case, the player must not have the object -
454  * if they do, they can't pass through.
455  */
456  if (check_inv_recursive(ob, tmp) != NULL) {
457  if (tmp->msg) {
460  tmp->msg, tmp->msg);
461  }
462  return 1;
463  }
464  else
465  continue;
466  }
467  } /* if check_inv */
468  else {
469  /* Broke apart a big nasty if into several here to make
470  * this more readable. first check - if the space blocks
471  * movement, can't move here.
472  * second - if a monster, can't move there, unless it is a
473  * hidden dm
474  */
475  if (OB_MOVE_BLOCK(ob, tmp))
476  return 1;
477  if (QUERY_FLAG(tmp, FLAG_ALIVE)
478  && tmp->head != ob
479  && tmp != ob
480  && tmp->type != DOOR
481  && !(QUERY_FLAG(tmp, FLAG_WIZ) && tmp->contr->hidden))
482  return 1;
483  }
484 
485  }
486  return 0;
487 }
488 
525 int ob_blocked(const object *ob, mapstruct *m, sint16 x, sint16 y) {
526  archetype *tmp;
527  int flag;
528  mapstruct *m1;
529  sint16 sx, sy;
530  const object *part;
531 
532  if (ob == NULL) {
533  flag = get_map_flags(m, &m1, x, y, &sx, &sy);
534  if (flag&P_OUT_OF_MAP)
535  return P_OUT_OF_MAP;
536 
537  /* don't have object, so don't know what types would block */
538  return(GET_MAP_MOVE_BLOCK(m1, sx, sy));
539  }
540 
541  for (tmp = ob->arch, part = ob; tmp != NULL; tmp = tmp->more, part = part->more) {
542  flag = get_map_flags(m, &m1, x+tmp->clone.x, y+tmp->clone.y, &sx, &sy);
543 
544  if (flag&P_OUT_OF_MAP)
545  return P_OUT_OF_MAP;
546  if (flag&P_IS_ALIVE)
547  return P_IS_ALIVE;
548 
549  /* find_first_free_spot() calls this function. However, often
550  * ob doesn't have any move type (when used to place exits)
551  * so the AND operation in OB_TYPE_MOVE_BLOCK doesn't work.
552  */
553 
554  if (ob->move_type == 0 && GET_MAP_MOVE_BLOCK(m1, sx, sy) != MOVE_ALL)
555  continue;
556 
557  /* A transport without move_type for a part should go through everything for that part. */
558  if (ob->type == TRANSPORT && part->move_type == 0)
559  continue;
560 
561  /* Note it is intentional that we check ob - the movement type of the
562  * head of the object should correspond for the entire object.
563  */
564  if (OB_TYPE_MOVE_BLOCK(ob, GET_MAP_MOVE_BLOCK(m1, sx, sy)))
565  return AB_NO_PASS;
566  }
567  return 0;
568 }
569 
580 void fix_container(object *container) {
581  object *tmp = container->inv, *next;
582 
583  container->inv = NULL;
584  while (tmp != NULL) {
585  next = tmp->below;
586  if (tmp->inv)
587  fix_container(tmp);
588  (void)insert_ob_in_ob(tmp, container);
589  tmp = next;
590  }
591  /* sum_weight will go through and calculate what all the containers are
592  * carrying.
593  */
594  sum_weight(container);
595 }
596 
607 static void fix_container_multipart(object *container) {
608  object *tmp = container->inv, *next;
609 
610  while (tmp != NULL) {
611  archetype *at;
612  object *op, *last;
613 
614  next = tmp->below;
615  if (tmp->inv)
617  /* already multipart, or non-multipart arch - don't do anything more */
618  for (at = tmp->arch->more, last = tmp; at != NULL; at = at->more, last = op) {
619  /* FIXME: We can't reuse fix_multipart_object() since that only
620  * works for items directly on maps. Maybe factor out common code?
621  */
622  op = arch_to_object(at);
623  op->head = tmp;
624  op->env = tmp->env;
625  last->more = op;
626  if (tmp->name != op->name) {
627  if (op->name)
628  free_string(op->name);
629  op->name = add_string(tmp->name);
630  }
631  if (tmp->title != op->title) {
632  if (op->title)
633  free_string(op->title);
634  op->title = add_string(tmp->title);
635  }
637  }
638  tmp = next;
639  }
640 }
641 
653  int x, y;
654  object *tmp, *above;
655 
656  for (x = 0; x < MAP_WIDTH(m); x++)
657  for (y = 0; y < MAP_HEIGHT(m); y++)
658  for (tmp = GET_MAP_OB(m, x, y); tmp != NULL; tmp = above) {
659  above = tmp->above;
660  if (tmp->inv)
662 
663  /* already multipart - don't do anything more */
664  if (tmp->head || tmp->more)
665  continue;
666 
668  } /* for objects on this space */
669 }
670 
682 static void load_objects(mapstruct *m, FILE *fp, int mapflags) {
683  int i, j, bufstate = LO_NEWFILE;
684  int unique;
685  object *op, *prev = NULL, *last_more = NULL, *otmp;
686 
687  op = get_object();
688  op->map = m; /* To handle buttons correctly */
689 
690  while ((i = load_object(fp, op, bufstate, mapflags))) {
691  /* Since the loading of the map header does not load an object
692  * anymore, we need to pass LO_NEWFILE for the first object loaded,
693  * and then switch to LO_REPEAT for faster loading.
694  */
695  bufstate = LO_REPEAT;
696 
697  /* if the archetype for the object is null, means that we
698  * got an invalid object. Don't do anything with it - the game
699  * or editor will not be able to do anything with it either.
700  */
701  if (op->arch == NULL) {
702  LOG(llevDebug, "Discarding object without arch: %s\n", op->name ? op->name : "(null)");
703  continue;
704  }
705 
706  switch (i) {
707  case LL_NORMAL:
708  /* if we are loading an overlay, put the floors on the bottom */
710  && mapflags&MAP_OVERLAY)
712  else
714 
715  if (op->inv)
716  sum_weight(op);
717 
718  prev = op,
719  last_more = op;
720  break;
721 
722  case LL_MORE:
724  op->head = prev,
725  last_more->more = op,
726  last_more = op;
727  break;
728  }
729  if (mapflags&MAP_STYLE) {
731  }
732  op = get_object();
733  op->map = m;
734  }
735  for (i = 0; i < m->width; i++) {
736  for (j = 0; j < m->height; j++) {
737  unique = 0;
738  /* check for unique items, or unique squares */
739  for (otmp = GET_MAP_OB(m, i, j); otmp; otmp = otmp->above) {
740  if (QUERY_FLAG(otmp, FLAG_UNIQUE))
741  unique = 1;
742  if (!(mapflags&(MAP_OVERLAY|MAP_PLAYER_UNIQUE) || unique))
744  }
745  }
746  }
747  free_object(op);
749 }
750 
768 static int save_objects(mapstruct *m, FILE *fp, FILE *fp2, int flag) {
769  int i, j = 0, unique = 0, res;
770  object *op, *otmp;
771 
772  /* first pass - save one-part objects */
773  for (i = 0; i < MAP_WIDTH(m); i++)
774  for (j = 0; j < MAP_HEIGHT(m); j++) {
775  unique = 0;
776  for (op = GET_MAP_OB(m, i, j); op; op = otmp) {
777  otmp = op->above;
778 
780  unique = 1;
781 
782  if (op->type == PLAYER) {
783  LOG(llevDebug, "Player on map that is being saved\n");
784  continue;
785  }
786 
787  if (op->head || op->owner)
788  continue;
789 
790  if (unique || QUERY_FLAG(op, FLAG_UNIQUE))
792  else
793  if (flag == 0
794  || (flag == SAVE_FLAG_NO_REMOVE && (!QUERY_FLAG(op, FLAG_OBJ_ORIGINAL) && !QUERY_FLAG(op, FLAG_UNPAID))))
796 
797  if (res != 0)
798  return res;
799  } /* for this space */
800  } /* for this j */
801 
802  return 0;
803 }
804 
817  mapstruct *map = (mapstruct *)calloc(1, sizeof(mapstruct));
818  mapstruct *mp;
819 
820  if (map == NULL)
822 
823  for (mp = first_map; mp != NULL && mp->next != NULL; mp = mp->next)
824  ;
825  if (mp == NULL)
826  first_map = map;
827  else
828  mp->next = map;
829 
830  map->in_memory = MAP_SWAPPED;
831  /* The maps used to pick up default x and y values from the
832  * map archetype. Mimic that behaviour.
833  */
834  MAP_WIDTH(map) = 16;
835  MAP_HEIGHT(map) = 16;
836  MAP_RESET_TIMEOUT(map) = 0;
837  MAP_TIMEOUT(map) = 300;
838  MAP_ENTER_X(map) = 0;
839  MAP_ENTER_Y(map) = 0;
840  map->last_reset_time.tv_sec = 0;
841  return map;
842 }
843 
854 static void allocate_map(mapstruct *m) {
856  /* Log this condition and free the storage. We could I suppose
857  * realloc, but if the caller is presuming the data will be intact,
858  * that is their poor assumption.
859  */
860  if (m->spaces) {
861  LOG(llevError, "allocate_map called with already allocated map (%s)\n", m->path);
862  free(m->spaces);
863  }
864 
865  m->spaces = calloc(1, MAP_WIDTH(m)*MAP_HEIGHT(m)*sizeof(MapSpace));
866 
867  if (m->spaces == NULL)
869 }
870 
884 mapstruct *get_empty_map(int sizex, int sizey) {
885  mapstruct *m = get_linked_map();
886  m->width = sizex;
887  m->height = sizey;
888  m->in_memory = MAP_SWAPPED;
889  allocate_map(m);
890  return m;
891 }
892 
904 static shopitems *parse_shop_string(const char *input_string) {
905  char *shop_string, *p, *q, *next_semicolon, *next_colon;
906  shopitems *items = NULL;
907  int i = 0, number_of_entries = 0;
908  const typedata *current_type;
909 
910  shop_string = strdup_local(input_string);
911  p = shop_string;
912  LOG(llevDebug, "parsing %s\n", input_string);
913  /* first we'll count the entries, we'll need that for allocating the array shortly */
914  while (p) {
915  p = strchr(p, ';');
916  number_of_entries++;
917  if (p)
918  p++;
919  }
920  p = shop_string;
921  strip_endline(p);
922  items = CALLOC(number_of_entries+1, sizeof(shopitems));
923  memset(items, 0, (sizeof(shopitems)*number_of_entries+1));
924  for (i = 0; i < number_of_entries; i++) {
925  if (!p) {
926  LOG(llevError, "parse_shop_string: I seem to have run out of string, that shouldn't happen.\n");
927  break;
928  }
929  next_semicolon = strchr(p, ';');
930  next_colon = strchr(p, ':');
931  /* if there is a stregth specified, figure out what it is, we'll need it soon. */
932  if (next_colon &&(!next_semicolon || next_colon < next_semicolon))
933  items[i].strength = atoi(strchr(p, ':')+1);
934 
935  if (isdigit(*p) || *p == '*') {
936  items[i].typenum = atoi(p); /* atoi returns 0 when we have an asterisk */
937  current_type = get_typedata(items[i].typenum);
938  if (current_type) {
939  items[i].name = current_type->name;
940  items[i].name_pl = current_type->name_pl;
941  }
942  } else { /*we have a named type, let's figure out what it is */
943  q = strpbrk(p, ";:");
944  if (q)
945  *q = '\0';
946 
947  current_type = get_typedata_by_name(p);
948  if (current_type) {
949  items[i].name = current_type->name;
950  items[i].typenum = current_type->number;
951  items[i].name_pl = current_type->name_pl;
952  } else { /* oh uh, something's wrong, let's free up this one, and try
953  * the next entry while we're at it, better print a warning
954  */
955  LOG(llevError, "invalid type %s defined in shopitems in string %s\n", p, input_string);
956  }
957  }
958  items[i].index = number_of_entries;
959  if (next_semicolon)
960  p = ++next_semicolon;
961  else
962  p = NULL;
963  }
964  free(shop_string);
965  return items;
966 }
967 
979 static void print_shop_string(mapstruct *m, char *output_string, int size) {
980  int i;
981  char tmp[MAX_BUF];
982 
983  output_string[0] = '\0';
984  for (i = 0; i < m->shopitems[0].index; i++) {
985  if (m->shopitems[i].typenum) {
986  if (m->shopitems[i].strength) {
987  snprintf(tmp, sizeof(tmp), "%s:%d;", m->shopitems[i].name, m->shopitems[i].strength);
988  } else
989  snprintf(tmp, sizeof(tmp), "%s;", m->shopitems[i].name);
990  } else {
991  if (m->shopitems[i].strength) {
992  snprintf(tmp, sizeof(tmp), "*:%d;", m->shopitems[i].strength);
993  } else
994  snprintf(tmp, sizeof(tmp), "*");
995  }
996  snprintf(output_string+strlen(output_string), size-strlen(output_string), "%s", tmp);
997  }
998 }
999 
1019 static int load_map_header(FILE *fp, mapstruct *m) {
1020  char buf[HUGE_BUF], *key = NULL, *value;
1021 
1022  m->width = m->height = 0;
1023  while (fgets(buf, sizeof(buf), fp) != NULL) {
1024  char *p;
1025 
1026  p = strchr(buf, '\n');
1027  if (p == NULL) {
1028  LOG(llevError, "Error loading map header - did not find a newline - perhaps file is truncated? Buf=%s\n", buf);
1029  return 1;
1030  }
1031  *p = '\0';
1032 
1033  key = buf;
1034  while (isspace(*key))
1035  key++;
1036  if (*key == 0)
1037  continue; /* empty line */
1038  value = strchr(key, ' ');
1039  if (value) {
1040  *value = 0;
1041  value++;
1042  while (isspace(*value)) {
1043  value++;
1044  if (*value == '\0') {
1045  /* Nothing but spaces. */
1046  value = NULL;
1047  break;
1048  }
1049  }
1050  }
1051 
1052  /* key is the field name, value is what it should be set
1053  * to. We've already done the work to null terminate key,
1054  * and strip off any leading spaces for both of these.
1055  * We have not touched the newline at the end of the line -
1056  * these are needed for some values. the end pointer
1057  * points to the first of the newlines.
1058  * value could be NULL! It would be easy enough to just point
1059  * this to "" to prevent cores, but that would let more errors slide
1060  * through.
1061  *
1062  * First check for entries that do not use the value parameter, then
1063  * validate that value is given and check for the remaining entries
1064  * that use the parameter.
1065  */
1066 
1067  if (!strcmp(key, "msg")) {
1068  char msgbuf[HUGE_BUF];
1069  int msgpos = 0;
1070 
1071  while (fgets(buf, sizeof(buf), fp) != NULL) {
1072  if (!strcmp(buf, "endmsg\n"))
1073  break;
1074  else {
1075  snprintf(msgbuf+msgpos, sizeof(msgbuf)-msgpos, "%s", buf);
1076  msgpos += strlen(buf);
1077  }
1078  }
1079  /* There are lots of maps that have empty messages (eg, msg/endmsg
1080  * with nothing between). There is no reason in those cases to
1081  * keep the empty message. Also, msgbuf contains garbage data
1082  * when msgpos is zero, so copying it results in crashes
1083  */
1084  if (msgpos != 0) {
1085  /* When loading eg an overlay, message is already set, so free() current one. */
1086  free(m->msg);
1087  m->msg = strdup_local(msgbuf);
1088  }
1089  } else if (!strcmp(key, "maplore")) {
1090  char maplorebuf[HUGE_BUF];
1091  int maplorepos = 0;
1092 
1093  while (fgets(buf, HUGE_BUF-1, fp) != NULL) {
1094  if (!strcmp(buf, "endmaplore\n"))
1095  break;
1096  else {
1097  snprintf(maplorebuf+maplorepos, sizeof(maplorebuf)-maplorepos, "%s", buf);
1098  maplorepos += strlen(buf);
1099  }
1100  }
1101  if (maplorepos != 0)
1102  m->maplore = strdup_local(maplorebuf);
1103  } else if (!strcmp(key, "end")) {
1104  break;
1105  } else if (value == NULL) {
1106  LOG(llevError, "Got '%s' line without parameter in map header\n", key);
1107  } else if (!strcmp(key, "arch")) {
1108  /* This is an oddity, but not something we care about much. */
1109  if (strcmp(value, "map"))
1110  LOG(llevError, "loading map and got a non 'arch map' line(%s %s)?\n", key, value);
1111  } else if (!strcmp(key, "name")) {
1112  /* When loading eg an overlay, the name is already set, so free() current one. */
1113  free(m->name);
1114  m->name = strdup_local(value);
1115  /* first strcmp value on these are old names supported
1116  * for compatibility reasons. The new values (second) are
1117  * what really should be used.
1118  */
1119  } else if (!strcmp(key, "hp") || !strcmp(key, "enter_x")) {
1120  m->enter_x = atoi(value);
1121  } else if (!strcmp(key, "sp") || !strcmp(key, "enter_y")) {
1122  m->enter_y = atoi(value);
1123  } else if (!strcmp(key, "x") || !strcmp(key, "width")) {
1124  m->width = atoi(value);
1125  } else if (!strcmp(key, "y") || !strcmp(key, "height")) {
1126  m->height = atoi(value);
1127  } else if (!strcmp(key, "weight") || !strcmp(key, "reset_timeout")) {
1128  m->reset_timeout = atoi(value);
1129  } else if (!strcmp(key, "value") || !strcmp(key, "swap_time")) {
1130  m->timeout = atoi(value);
1131  } else if (!strcmp(key, "level") || !strcmp(key, "difficulty")) {
1132  m->difficulty = atoi(value);
1133  } else if (!strcmp(key, "invisible") || !strcmp(key, "darkness")) {
1134  m->darkness = atoi(value);
1135  } else if (!strcmp(key, "stand_still") || !strcmp(key, "fixed_resettime")) {
1136  m->fixed_resettime = atoi(value);
1137  } else if (!strcmp(key, "unique")) {
1138  m->unique = atoi(value);
1139  } else if (!strcmp(key, "template")) {
1140  m->is_template = atoi(value);
1141  } else if (!strcmp(key, "region")) {
1142  m->region = get_region_by_name(value);
1143  } else if (!strcmp(key, "shopitems")) {
1144  m->shopitems = parse_shop_string(value);
1145  } else if (!strcmp(key, "shopgreed")) {
1146  m->shopgreed = atof(value);
1147  } else if (!strcmp(key, "shopmin")) {
1148  m->shopmin = atol(value);
1149  } else if (!strcmp(key, "shopmax")) {
1150  m->shopmax = atol(value);
1151  } else if (!strcmp(key, "shoprace")) {
1152  m->shoprace = strdup_local(value);
1153  } else if (!strcmp(key, "outdoor")) {
1154  m->outdoor = atoi(value);
1155  } else if (!strcmp(key, "nosmooth")) {
1156  m->nosmooth = atoi(value);
1157  } else if (!strcmp(key, "first_load")) {
1158  m->last_reset_time.tv_sec = atoi(value);
1159  } else if (!strncmp(key, "tile_path_", 10)) {
1160  int tile = atoi(key+10);
1161 
1162  if (tile < 1 || tile > 4) {
1163  LOG(llevError, "load_map_header: tile location %d out of bounds (%s)\n", tile, m->path);
1164  } else {
1165  char path[HUGE_BUF];
1166 
1167  if (m->tile_path[tile-1]) {
1168  LOG(llevError, "load_map_header: tile location %d duplicated (%s)\n", tile, m->path);
1169  free(m->tile_path[tile-1]);
1170  m->tile_path[tile-1] = NULL;
1171  }
1172 
1173  if (check_path(value, 1) != -1) {
1174  /* The unadorned path works. */
1175  snprintf(path, sizeof(path), "%s", value);
1176  } else {
1177  /* Try again; it could be a relative exit. */
1178  path_combine_and_normalize(m->path, value, path, sizeof(path));
1179 
1180  if (check_path(path, 1) == -1) {
1181  LOG(llevError, "get_map_header: Bad tile path %s %s\n", m->path, value);
1182  path[0] = '\0';
1183  }
1184  }
1185 
1186  if (*path != '\0') {
1187  /* Use the normalized value. */
1188  m->tile_path[tile-1] = strdup_local(path);
1189  }
1190  } /* end if tile direction (in)valid */
1191  } else if (!strcmp(key, "background_music")) {
1192  m->background_music = strdup_local(value);
1193  } else {
1194  LOG(llevError, "Got unknown value in map header: %s %s\n", key, value);
1195  }
1196  }
1197  if ((m->width == 0) || (m->height == 0)) {
1198  LOG(llevError, "Map width or height not specified\n");
1199  return 1;
1200  }
1201  if (!key || strcmp(key, "end")) {
1202  LOG(llevError, "Got premature eof on map header!\n");
1203  return 1;
1204  }
1205  return 0;
1206 }
1207 
1226 mapstruct *load_original_map(const char *filename, int flags) {
1227  FILE *fp;
1228  mapstruct *m;
1229  int comp;
1230  char pathname[MAX_BUF];
1231 
1232  LOG(llevDebug, "load_original_map: %s (%x)\n", filename, flags);
1233  if (flags&MAP_PLAYER_UNIQUE)
1234  snprintf(pathname, sizeof(pathname), "%s", filename);
1235  else if (flags&MAP_OVERLAY)
1236  create_overlay_pathname(filename, pathname, MAX_BUF);
1237  else
1238  create_pathname(filename, pathname, MAX_BUF);
1239 
1240  if ((fp = open_and_uncompress(pathname, 0, &comp)) == NULL) {
1241  char err[MAX_BUF];
1242 
1243  LOG((flags&MAP_PLAYER_UNIQUE) ? llevDebug : llevError, "Can't open %s: %s\n", pathname, strerror_local(errno, err, sizeof(err)));
1244  return (NULL);
1245  }
1246 
1247  m = get_linked_map();
1248 
1249  strcpy(m->path, filename);
1250  if (load_map_header(fp, m)) {
1251  LOG(llevError, "Error loading map header for %s, flags=%d\n", filename, flags);
1252  delete_map(m);
1253  return NULL;
1254  }
1255 
1256  allocate_map(m);
1257  m->compressed = comp;
1258 
1259  m->in_memory = MAP_LOADING;
1260  load_objects(m, fp, flags&(MAP_BLOCK|MAP_STYLE));
1261  close_and_delete(fp, comp);
1262  m->in_memory = MAP_IN_MEMORY;
1263  if (!MAP_DIFFICULTY(m))
1265  set_map_reset_time(m);
1266 
1267  /* In case other objects press some buttons down */
1268  update_buttons(m);
1269 
1270  /* Handle for map load event */
1272 
1273  return (m);
1274 }
1275 
1289  FILE *fp;
1290  int comp;
1291  char buf[MAX_BUF];
1292 
1293  if (!m->tmpname) {
1294  LOG(llevError, "No temporary filename for map %s\n", m->path);
1295  snprintf(buf, sizeof(buf), "%s", m->path);
1296  delete_map(m);
1297  m = load_original_map(buf, 0);
1298  if (m == NULL)
1299  return NULL;
1300  fix_auto_apply(m); /* Chests which open as default */
1301  return m;
1302  }
1303 
1304  if ((fp = open_and_uncompress(m->tmpname, 0, &comp)) == NULL) {
1305  LOG(llevError, "Cannot open %s: %s\n", m->tmpname, strerror_local(errno, buf, sizeof(buf)));
1306  snprintf(buf, sizeof(buf), "%s", m->path);
1307  delete_map(m);
1308  m = load_original_map(buf, 0);
1309  if (m == NULL)
1310  return NULL;
1311  fix_auto_apply(m); /* Chests which open as default */
1312  return m;
1313  }
1314 
1315  if (load_map_header(fp, m)) {
1316  LOG(llevError, "Error loading map header for %s (%s)\n", m->path, m->tmpname);
1317  delete_map(m);
1318  m = load_original_map(m->path, 0);
1319  return NULL;
1320  }
1321  m->compressed = comp;
1322  allocate_map(m);
1323 
1324  m->in_memory = MAP_LOADING;
1325  load_objects(m, fp, 0);
1326  close_and_delete(fp, comp);
1327  m->in_memory = MAP_IN_MEMORY;
1328  return m;
1329 }
1330 
1341 static mapstruct *load_overlay_map(const char *filename, mapstruct *m) {
1342  FILE *fp;
1343  int comp;
1344  char pathname[MAX_BUF];
1345 
1346  create_overlay_pathname(filename, pathname, MAX_BUF);
1347 
1348  if ((fp = open_and_uncompress(pathname, 0, &comp)) == NULL) {
1349  /* LOG(llevDebug, "Can't open overlay %s\n", pathname);*/
1350  return m;
1351  }
1352 
1353  if (load_map_header(fp, m)) {
1354  LOG(llevError, "Error loading map header for overlay %s (%s)\n", m->path, pathname);
1355  delete_map(m);
1356  m = load_original_map(m->path, 0);
1357  return NULL;
1358  }
1359  m->compressed = comp;
1360  /*allocate_map(m);*/
1361 
1362  m->in_memory = MAP_LOADING;
1363  load_objects(m, fp, MAP_OVERLAY);
1364  close_and_delete(fp, comp);
1365  m->in_memory = MAP_IN_MEMORY;
1366  return m;
1367 }
1368 
1369 /******************************************************************************
1370  * This is the start of unique map handling code
1371  *****************************************************************************/
1372 
1380  int i, j, unique = 0;
1381  object *op, *next;
1382 
1383  for (i = 0; i < MAP_WIDTH(m); i++)
1384  for (j = 0; j < MAP_HEIGHT(m); j++) {
1385  unique = 0;
1386  for (op = GET_MAP_OB(m, i, j); op; op = next) {
1387  next = op->above;
1389  unique = 1;
1390  if (op->head == NULL && (QUERY_FLAG(op, FLAG_UNIQUE) || unique)) {
1391  clean_object(op);
1392  if (QUERY_FLAG(op, FLAG_IS_LINKED))
1393  remove_button_link(op);
1394  remove_ob(op);
1395  free_object(op);
1396  }
1397  }
1398  }
1399 }
1400 
1407  FILE *fp;
1408  int comp, count;
1409  char firstname[MAX_BUF], name[MAX_BUF];
1410 
1411  create_items_path(m->path, name, MAX_BUF);
1412  for (count = 0; count < 10; count++) {
1413  snprintf(firstname, sizeof(firstname), "%s.v%02d", name, count);
1414  if (!access(firstname, R_OK))
1415  break;
1416  }
1417  /* If we get here, we did not find any map */
1418  if (count == 10)
1419  return;
1420 
1421  if ((fp = open_and_uncompress(firstname, 0, &comp)) == NULL) {
1422  /* There is no expectation that every map will have unique items, but this
1423  * is debug output, so leave it in.
1424  */
1425  LOG(llevDebug, "Can't open unique items file for %s\n", name);
1426  return;
1427  }
1428 
1429  m->in_memory = MAP_LOADING;
1430  if (m->tmpname == NULL) /* if we have loaded unique items from */
1431  delete_unique_items(m); /* original map before, don't duplicate them */
1432  load_object(fp, NULL, LO_NOREAD, 0);
1433  load_objects(m, fp, 0);
1434  close_and_delete(fp, comp);
1435  m->in_memory = MAP_IN_MEMORY;
1436 }
1437 
1453 int save_map(mapstruct *m, int flag) {
1454 #define TEMP_EXT ".savefile"
1455  FILE *fp, *fp2;
1456  char filename[MAX_BUF], buf[MAX_BUF], shop[MAX_BUF], final[MAX_BUF];
1457  int i, res;
1458 
1459  if (flag && !*m->path) {
1460  LOG(llevError, "Tried to save map without path.\n");
1461  return SAVE_ERROR_NO_PATH;
1462  }
1463 
1464  if (flag != SAVE_MODE_NORMAL || (m->unique) || (m->is_template)) {
1465  if (!m->unique && !m->is_template) { /* flag is set */
1466  if (flag == SAVE_MODE_OVERLAY)
1467  create_overlay_pathname(m->path, filename, MAX_BUF);
1468  else
1469  create_pathname(m->path, filename, MAX_BUF);
1470  } else
1471  snprintf(filename, sizeof(filename), "%s", m->path);
1472 
1473  /* If the compression suffix already exists on the filename, don't
1474  * put it on again. This nasty looking strcmp checks to see if the
1475  * compression suffix is at the end of the filename already.
1476  */
1477  if (m->compressed
1478  && strcmp((filename+strlen(filename)-strlen(uncomp[m->compressed][0])), uncomp[m->compressed][0]))
1479  strcat(filename, uncomp[m->compressed][0]);
1480  make_path_to_file(filename);
1481  } else {
1482  if (!m->tmpname)
1483  m->tmpname = tempnam_local(settings.tmpdir, NULL);
1484  snprintf(filename, sizeof(filename), "%s", m->tmpname);
1485  }
1486  LOG(llevDebug, "Saving map %s\n", m->path);
1487  m->in_memory = MAP_SAVING;
1488 
1489  /* Compress if it isn't a temporary save. Do compress if unique */
1490  if (m->compressed && (m->unique || m->is_template || flag != SAVE_MODE_NORMAL)) {
1491  char buf[MAX_BUF];
1492  snprintf(buf, sizeof(buf), "%s > %s%s", uncomp[m->compressed][2], filename, TEMP_EXT);
1493  snprintf(final, sizeof(final), "%s", filename);
1494  fp = popen(buf, "w");
1495  } else {
1496  snprintf(final, sizeof(final), "%s", filename);
1497  snprintf(filename, sizeof(filename), "%s%s", final, TEMP_EXT);
1498  fp = fopen(filename, "w");
1499  }
1500 
1501  if (fp == NULL) {
1502  LOG(llevError, "Cannot open regular objects file %s: %s\n", filename, strerror_local(errno, buf, sizeof(buf)));
1503  return SAVE_ERROR_RCREATION;
1504  }
1505 
1506  /* legacy */
1507  fprintf(fp, "arch map\n");
1508  if (m->name) fprintf(fp, "name %s\n", m->name);
1509  if (!flag) fprintf(fp, "swap_time %d\n", m->swap_time);
1510  if (m->reset_timeout) fprintf(fp, "reset_timeout %u\n", m->reset_timeout);
1511  if (m->fixed_resettime) fprintf(fp, "fixed_resettime %d\n", m->fixed_resettime);
1512  /* we unfortunately have no idea if this is a value the creator set
1513  * or a difficulty value we generated when the map was first loaded
1514  */
1515  if (m->difficulty) fprintf(fp, "difficulty %d\n", m->difficulty);
1516  if (m->region) fprintf(fp, "region %s\n", m->region->name);
1517  if (m->shopitems) {
1518  print_shop_string(m, shop, sizeof(shop));
1519  fprintf(fp, "shopitems %s\n", shop);
1520  }
1521  if (m->shopgreed) fprintf(fp, "shopgreed %f\n", m->shopgreed);
1522  if (m->shopmin) fprintf(fp, "shopmin %"FMT64U"\n", m->shopmin);
1523  if (m->shopmax) fprintf(fp, "shopmax %"FMT64U"\n", m->shopmax);
1524  if (m->shoprace) fprintf(fp, "shoprace %s\n", m->shoprace);
1525  if (m->darkness) fprintf(fp, "darkness %d\n", m->darkness);
1526  if (m->width) fprintf(fp, "width %d\n", m->width);
1527  if (m->height) fprintf(fp, "height %d\n", m->height);
1528  if (m->enter_x) fprintf(fp, "enter_x %d\n", m->enter_x);
1529  if (m->enter_y) fprintf(fp, "enter_y %d\n", m->enter_y);
1530  if (m->msg) fprintf(fp, "msg\n%sendmsg\n", m->msg);
1531  if (m->maplore) fprintf(fp, "maplore\n%sendmaplore\n", m->maplore);
1532  if (m->unique) fprintf(fp, "unique %d\n", m->unique);
1533  if (m->is_template) fprintf(fp, "template %d\n", m->is_template);
1534  if (m->outdoor) fprintf(fp, "outdoor %d\n", m->outdoor);
1535  if (m->nosmooth) fprintf(fp, "nosmooth %d\n", m->nosmooth);
1536  if (m->last_reset_time.tv_sec) fprintf(fp, "first_load %d\n", (int)m->last_reset_time.tv_sec);
1537  if (m->background_music) fprintf(fp, "background_music %s\n", m->background_music);
1538 
1539  /* Save any tiling information, except on overlays */
1540  if (flag != SAVE_MODE_OVERLAY)
1541  for (i = 0; i < 4; i++)
1542  if (m->tile_path[i])
1543  fprintf(fp, "tile_path_%d %s\n", i+1, m->tile_path[i]);
1544 
1545  fprintf(fp, "end\n");
1546 
1547  /* In the game save unique items in the different file, but
1548  * in the editor save them to the normal map file.
1549  * If unique map, save files in the proper destination (set by
1550  * player)
1551  */
1552  fp2 = fp; /* save unique items into fp2 */
1553  if ((flag == SAVE_MODE_NORMAL || flag == SAVE_MODE_OVERLAY) && !m->unique && !m->is_template) {
1554  char name[MAX_BUF], final_unique[MAX_BUF];
1555 
1556  create_items_path(m->path, name, MAX_BUF);
1557  snprintf(final_unique, sizeof(final_unique), "%s.v00", name);
1558  snprintf(buf, sizeof(buf), "%s%s", final_unique, TEMP_EXT);
1559  if ((fp2 = fopen(buf, "w")) == NULL) {
1560  LOG(llevError, "Can't open unique items file %s\n", buf);
1561  return SAVE_ERROR_UCREATION;
1562  }
1563  if (flag == SAVE_MODE_OVERLAY) {
1564  /* SO_FLAG_NO_REMOVE is non destructive save, so map is still valid. */
1565  res = save_objects(m, fp, fp2, SAVE_FLAG_NO_REMOVE);
1566  if (res < 0) {
1567  LOG(llevError, "Save error during object save: %d\n", res);
1568  return res;
1569  }
1570  m->in_memory = MAP_IN_MEMORY;
1571  } else {
1572  res = save_objects(m, fp, fp2, 0);
1573  if (res < 0) {
1574  LOG(llevError, "Save error during object save: %d\n", res);
1575  return res;
1576  }
1577  free_all_objects(m);
1578  }
1579  if (fp2 != NULL) {
1580  if (ftell(fp2) == 0) {
1581  fclose(fp2);
1582  unlink(buf);
1583  /* If there are no unique items left on the map, we need to
1584  * unlink the original unique map so that the unique
1585  * items don't show up again.
1586  */
1587  unlink(final_unique);
1588  } else {
1589  fflush(fp2);
1590  fclose(fp2);
1591  unlink(final_unique); /* failure isn't too bad, maybe the file doesn't exist. */
1592  if (rename(buf, final_unique) == -1) {
1593  LOG(llevError, "Couldn't rename unique file %s to %s\n", buf, final_unique);
1594  return SAVE_ERROR_URENAME;
1595  }
1596  chmod(final_unique, SAVE_MODE);
1597  }
1598  }
1599  } else { /* save same file when not playing, like in editor */
1600  res = save_objects(m, fp, fp, 0);
1601  if (res < 0) {
1602  LOG(llevError, "Save error during object save: %d\n", res);
1603  return res;
1604  }
1605  free_all_objects(m);
1606  }
1607 
1608  if (m->compressed && (m->unique || m->is_template || flag != SAVE_MODE_NORMAL)) {
1609  fflush(fp);
1610  if (pclose(fp) == -1) {
1611  LOG(llevError, "pclose error!\n");
1612  return SAVE_ERROR_CLOSE;
1613  }
1614  } else {
1615  fflush(fp);
1616  if (fclose(fp) != 0) {
1617  LOG(llevError, "fclose error!\n");
1618  return SAVE_ERROR_CLOSE;
1619  }
1620  }
1621  unlink(final); /* failure isn't too bad, maybe the file doesn't exist. */
1622  if (rename(filename, final) == -1) {
1623  LOG(llevError, "Couldn't rename regular file %s to %s\n", filename, final);
1624  return SAVE_ERROR_RRENAME;
1625  }
1626 
1627  chmod(final, SAVE_MODE);
1628  return SAVE_ERROR_OK;
1629 }
1630 
1640 void clean_object(object *op) {
1641  object *tmp, *next;
1642 
1643  for (tmp = op->inv; tmp; tmp = next) {
1644  next = tmp->below;
1645  clean_object(tmp);
1646  if (QUERY_FLAG(tmp, FLAG_IS_LINKED))
1647  remove_button_link(tmp);
1648  remove_ob(tmp);
1649  free_object(tmp);
1650  }
1651 }
1652 
1659 static void free_all_objects(mapstruct *m) {
1660  int i, j;
1661  object *op;
1662 
1663  for (i = 0; i < MAP_WIDTH(m); i++)
1664  for (j = 0; j < MAP_HEIGHT(m); j++) {
1665  object *previous_obj = NULL;
1666 
1667  while ((op = GET_MAP_OB(m, i, j)) != NULL) {
1668  if (op == previous_obj) {
1669  LOG(llevDebug, "free_all_objects: Link error, bailing out.\n");
1670  break;
1671  }
1672  previous_obj = op;
1673  if (op->head != NULL)
1674  op = op->head;
1675 
1676  /* If the map isn't in memory, free_object will remove and
1677  * free objects in op's inventory. So let it do the job.
1678  */
1679  if (m->in_memory == MAP_IN_MEMORY)
1680  clean_object(op);
1681  remove_ob(op);
1682  free_object(op);
1683  }
1684  }
1685 #ifdef MANY_CORES
1686  /* I see periodic cores on metalforge where a map has been swapped out, but apparantly
1687  * an item on that map was not saved - look for that condition and die as appropriate -
1688  * this leaves more of the map data intact for better debugging.
1689  */
1690  for (op = objects; op != NULL; op = op->next) {
1691  if (!QUERY_FLAG(op, FLAG_REMOVED) && op->map == m) {
1692  LOG(llevDebug, "free_all_objects: object %s still on map after it should have been freed\n", op->name);
1693  abort();
1694  }
1695  }
1696 #endif
1697 }
1698 
1708  int i;
1709 
1710  if (!m->in_memory) {
1711  LOG(llevError, "Trying to free freed map.\n");
1712  return;
1713  }
1714 
1715  /* Handle for plugin map unload event. */
1717 
1718  if (m->spaces) free_all_objects(m);
1719  if (m->name) FREE_AND_CLEAR(m->name);
1720  if (m->spaces) FREE_AND_CLEAR(m->spaces);
1721  if (m->msg) FREE_AND_CLEAR(m->msg);
1722  if (m->maplore) FREE_AND_CLEAR(m->maplore);
1723  if (m->shopitems) FREE_AND_CLEAR(m->shopitems);
1724  if (m->shoprace) FREE_AND_CLEAR(m->shoprace);
1726  if (m->buttons) free_objectlinkpt(m->buttons);
1727  m->buttons = NULL;
1728  for (i = 0; i < 4; i++) {
1729  if (m->tile_path[i])
1730  FREE_AND_CLEAR(m->tile_path[i]);
1731  m->tile_map[i] = NULL;
1732  }
1733  m->in_memory = MAP_SWAPPED;
1734 }
1735 
1746  mapstruct *tmp, *last;
1747  int i;
1748 
1749  if (!m)
1750  return;
1751  if (m->in_memory == MAP_IN_MEMORY) {
1752  /* change to MAP_SAVING, even though we are not,
1753  * so that remove_ob doesn't do as much work.
1754  */
1755  m->in_memory = MAP_SAVING;
1756  free_map(m);
1757  }
1758  /* move this out of free_map, since tmpname can still be needed if
1759  * the map is swapped out.
1760  */
1761  if (m->tmpname) {
1762  free(m->tmpname);
1763  m->tmpname = NULL;
1764  }
1765  last = NULL;
1766  /* We need to look through all the maps and see if any maps
1767  * are pointing at this one for tiling information. Since
1768  * tiling can be assymetric, we just can not look to see which
1769  * maps this map tiles with and clears those.
1770  */
1771  for (tmp = first_map; tmp != NULL; tmp = tmp->next) {
1772  if (tmp->next == m)
1773  last = tmp;
1774 
1775  /* This should hopefully get unrolled on a decent compiler */
1776  for (i = 0; i < 4; i++)
1777  if (tmp->tile_map[i] == m)
1778  tmp->tile_map[i] = NULL;
1779  }
1780 
1781  /* If last is null, then this should be the first map in the list */
1782  if (!last) {
1783  if (m == first_map)
1784  first_map = m->next;
1785  else
1786  /* m->path is a static char, so should hopefully still have
1787  * some useful data in it.
1788  */
1789  LOG(llevError, "delete_map: Unable to find map %s in list\n", m->path);
1790  } else
1791  last->next = m->next;
1792 
1793  free(m);
1794 }
1795 
1809 mapstruct *ready_map_name(const char *name, int flags) {
1810  mapstruct *m;
1811 
1812  if (!name)
1813  return (NULL);
1814 
1815  /* Have we been at this level before? */
1816  m = has_been_loaded(name);
1817 
1818  /* Map is good to go, so just return it */
1819  if (m && (m->in_memory == MAP_LOADING || m->in_memory == MAP_IN_MEMORY)) {
1820  return m;
1821  }
1822 
1823  /* unique maps always get loaded from their original location, and never
1824  * a temp location. Likewise, if map_flush is set, or we have never loaded
1825  * this map, load it now. I removed the reset checking from here -
1826  * it seems the probability of a player trying to enter a map that should
1827  * reset but hasn't yet is quite low, and removing that makes this function
1828  * a bit cleaner (and players probably shouldn't rely on exact timing for
1829  * resets in any case - if they really care, they should use the 'maps command.
1830  */
1831  if ((flags&(MAP_FLUSH|MAP_PLAYER_UNIQUE)) || !m) {
1832 
1833  /* first visit or time to reset */
1834  if (m) {
1835  clean_tmp_map(m); /* Doesn't make much difference */
1836  delete_map(m);
1837  }
1838 
1839  /* create and load a map */
1840  if (flags&MAP_PLAYER_UNIQUE)
1841  LOG(llevDebug, "Trying to load map %s.\n", name);
1842  else {
1843  char fullpath[MAX_BUF];
1844 
1845  create_pathname(name, fullpath, MAX_BUF);
1846  LOG(llevDebug, "Trying to load map %s.\n", fullpath);
1847  }
1848 
1849  if (!(m = load_original_map(name, (flags&MAP_PLAYER_UNIQUE))))
1850  return (NULL);
1851 
1852  fix_auto_apply(m); /* Chests which open as default */
1853 
1854  /* If a player unique map, no extra unique object file to load.
1855  * if from the editor, likewise.
1856  */
1857  if (!(flags&(MAP_FLUSH|MAP_PLAYER_UNIQUE)))
1859 
1860  if (!(flags&(MAP_FLUSH|MAP_PLAYER_UNIQUE|MAP_OVERLAY))) {
1861  m = load_overlay_map(name, m);
1862  if (m == NULL)
1863  return NULL;
1864  }
1865  } else {
1866  /* If in this loop, we found a temporary map, so load it up. */
1867 
1868  m = load_temporary_map(m);
1869  if (m == NULL)
1870  return NULL;
1872 
1873  clean_tmp_map(m);
1874  m->in_memory = MAP_IN_MEMORY;
1875  /* tempnam() on sun systems (probably others) uses malloc
1876  * to allocated space for the string. Free it here.
1877  * In some cases, load_temporary_map above won't find the
1878  * temporary map, and so has reloaded a new map. If that
1879  * is the case, tmpname is now null
1880  */
1881  if (m->tmpname)
1882  free(m->tmpname);
1883  m->tmpname = NULL;
1884  /* It's going to be saved anew anyway */
1885  }
1886 
1887  /* Below here is stuff common to both first time loaded maps and
1888  * temp maps.
1889  */
1890 
1891  decay_objects(m); /* start the decay */
1892 
1893  if (m->outdoor)
1894  set_darkness_map(m);
1895  if (!(flags&(MAP_FLUSH))) {
1896  if (m->last_reset_time.tv_sec == 0)
1897  gettimeofday(&(m->last_reset_time), NULL);
1898  }
1899  return m;
1900 }
1901 
1918  object *op;
1919  archetype *at;
1920  int x, y;
1921  int diff = 0;
1922  int i;
1923  sint64 exp_pr_sq, total_exp = 0;
1924 
1925  if (MAP_DIFFICULTY(m)) {
1926  return MAP_DIFFICULTY(m);
1927  }
1928 
1929  for (x = 0; x < MAP_WIDTH(m); x++)
1930  for (y = 0; y < MAP_HEIGHT(m); y++)
1931  for (op = GET_MAP_OB(m, x, y); op != NULL; op = op->above) {
1932  if (QUERY_FLAG(op, FLAG_MONSTER))
1933  total_exp += op->stats.exp;
1934  if (QUERY_FLAG(op, FLAG_GENERATOR)) {
1935  total_exp += op->stats.exp;
1937  if (at != NULL)
1938  total_exp += at->clone.stats.exp*8;
1939  }
1940  }
1941 
1942  exp_pr_sq = ((double)1000*total_exp)/(MAP_WIDTH(m)*MAP_HEIGHT(m)+1);
1943  diff = 20;
1944  for (i = 1; i < 20; i++)
1945  if (exp_pr_sq <= level_exp(i, 1.0)) {
1946  diff = i;
1947  break;
1948  }
1949 
1950  return diff;
1951 }
1952 
1960  if (m->tmpname == NULL)
1961  return;
1962  (void)unlink(m->tmpname);
1963 }
1964 
1968 void free_all_maps(void) {
1969  int real_maps = 0;
1970 
1971  while (first_map) {
1972  /* I think some of the callers above before it gets here set this to be
1973  * saving, but we still want to free this data
1974  */
1975  if (first_map->in_memory == MAP_SAVING)
1978  real_maps++;
1979  }
1980  LOG(llevDebug, "free_all_maps: Freed %d maps\n", real_maps);
1981 }
1982 
2000 int change_map_light(mapstruct *m, int change) {
2001  int new_level = m->darkness+change;
2002 
2003  /* Nothing to do */
2004  if (!change
2005  || (new_level <= 0 && m->darkness == 0)
2006  || (new_level >= MAX_DARKNESS && m->darkness >= MAX_DARKNESS)) {
2007  return 0;
2008  }
2009 
2010  /* inform all players on the map */
2011  if (change > 0)
2012  ext_info_map(NDI_BLACK, m, MSG_TYPE_MISC, MSG_SUBTYPE_NONE, "It becomes darker.", NULL);
2013  else
2014  ext_info_map(NDI_BLACK, m, MSG_TYPE_MISC, MSG_SUBTYPE_NONE, "It becomes brighter.", NULL);
2015 
2016  /* Do extra checking. since m->darkness is a unsigned value,
2017  * we need to be extra careful about negative values.
2018  * In general, the checks below are only needed if change
2019  * is not +/-1
2020  */
2021  if (new_level < 0)
2022  m->darkness = 0;
2023  else if (new_level >= MAX_DARKNESS)
2024  m->darkness = MAX_DARKNESS;
2025  else
2026  m->darkness = new_level;
2027 
2028  /* All clients need to get re-updated for the change */
2029  update_all_map_los(m);
2030  return 1;
2031 }
2032 
2055 static inline void add_face_layer(int low_layer, int high_layer, object *ob, object *layers[], int honor_visibility) {
2056  int l, l1;
2057  object *tmp;
2058 
2059  for (l = low_layer; l <= high_layer; l++) {
2060  if (!layers[l]) {
2061  /* found an empty spot. now, we want to make sure
2062  * highest visibility at top, etc.
2063  */
2064  layers[l] = ob;
2065  if (!honor_visibility)
2066  return;
2067 
2068  /* This is basically a mini bubble sort. Only swap
2069  * position if the lower face has greater (not equal)
2070  * visibility - map stacking is secondary consideration here.
2071  */
2072  for (l1 = (l-1); l1 >= low_layer; l1--) {
2073  if (layers[l1]->face->visibility > layers[l1+1]->face->visibility) {
2074  tmp = layers[l1+1];
2075  layers[l1+1] = layers[l1];
2076  layers[l1] = tmp;
2077  }
2078  }
2079  /* Nothing more to do - face inserted */
2080  return;
2081  }
2082  }
2083  /* If we get here, all the layers have an object..
2084  */
2085  if (!honor_visibility) {
2086  /* Basically, in this case, it is pure stacking logic, so
2087  * new object goes on the top.
2088  */
2089  for (l = low_layer; l < high_layer; l++)
2090  layers[l] = layers[l+1];
2091  layers[high_layer] = ob;
2092  /* If this object doesn't have higher visibility than
2093  * the lowest object, no reason to go further.
2094  */
2095  } else if (ob->face->visibility >= layers[low_layer]->face->visibility) {
2096  /*
2097  * Start at the top (highest visibility) layer and work down.
2098  * once this face exceed that of the layer, push down those
2099  * other layers, and then replace the layer with our object.
2100  */
2101  for (l = high_layer; l >= low_layer; l--) {
2102  if (ob->face->visibility >= layers[l]->face->visibility) {
2103  for (l1 = low_layer; l1 < l; l1++)
2104  layers[l1] = layers[l1+1];
2105  layers[l] = ob;
2106  break;
2107  }
2108  }
2109  }
2110 }
2111 
2124 void update_position(mapstruct *m, int x, int y) {
2125  object *tmp, *player = NULL;
2126  uint8 flags = 0, oldflags, light = 0;
2127  object *layers[MAP_LAYERS];
2128 
2129  MoveType move_block = 0, move_slow = 0, move_on = 0, move_off = 0, move_allow = 0;
2130 
2131  oldflags = GET_MAP_FLAGS(m, x, y);
2132  if (!(oldflags&P_NEED_UPDATE)) {
2133  LOG(llevDebug, "update_position called with P_NEED_UPDATE not set: %s (%d, %d)\n", m->path, x, y);
2134  return;
2135  }
2136 
2137  memset(layers, 0, MAP_LAYERS*sizeof(object *));
2138 
2139  for (tmp = GET_MAP_OB(m, x, y); tmp; tmp = tmp->above) {
2140 
2141  /* DMs just don't do anything when hidden, including no light. */
2142  if (QUERY_FLAG(tmp, FLAG_WIZ) && tmp->contr->hidden)
2143  continue;
2144 
2145  if (tmp->type == PLAYER)
2146  player = tmp;
2147 
2148  /* This could be made additive I guess (two lights better than
2149  * one). But if so, it shouldn't be a simple additive - 2
2150  * light bulbs do not illuminate twice as far as once since
2151  * it is a dissipation factor that is squared (or is it cubed?)
2152  */
2153  if (tmp->glow_radius > light)
2154  light = tmp->glow_radius;
2155 
2156  /* if this object is visible and not a blank face,
2157  * update the objects that show how this space
2158  * looks.
2159  */
2160  if (!tmp->invisible && tmp->face != blank_face) {
2161  if (tmp->map_layer) {
2162  add_face_layer(tmp->map_layer, map_layer_info[tmp->map_layer].high_layer,
2163  tmp, layers, map_layer_info[tmp->map_layer].honor_visibility);
2164  } else if (tmp->move_type&MOVE_FLYING) {
2165  add_face_layer(MAP_LAYER_FLY1, MAP_LAYER_FLY2, tmp, layers, 1);
2166  } else if ((tmp->type == PLAYER || QUERY_FLAG(tmp, FLAG_MONSTER))) {
2168  } else if (QUERY_FLAG(tmp, FLAG_IS_FLOOR)) {
2169  layers[MAP_LAYER_FLOOR] = tmp;
2170  /* floors hide everything else */
2171  memset(layers+1, 0, (MAP_LAYERS-1)*sizeof(object *));
2172  /* Check for FLAG_SEE_ANYWHERE is removed - objects
2173  * with that flag should just have a high visibility
2174  * set - we shouldn't need special code here.
2175  */
2176  } else if (QUERY_FLAG(tmp, FLAG_NO_PICK)) {
2178  } else {
2180  }
2181  }
2182  if (tmp == tmp->above) {
2183  LOG(llevError, "Error in structure of map\n");
2184  exit(-1);
2185  }
2186 
2187  move_slow |= tmp->move_slow;
2188  move_block |= tmp->move_block;
2189  move_on |= tmp->move_on;
2190  move_off |= tmp->move_off;
2191  move_allow |= tmp->move_allow;
2192 
2193  if (QUERY_FLAG(tmp, FLAG_ALIVE))
2194  flags |= P_IS_ALIVE;
2195  if (QUERY_FLAG(tmp, FLAG_NO_MAGIC))
2196  flags |= P_NO_MAGIC;
2197  if (QUERY_FLAG(tmp, FLAG_DAMNED))
2198  flags |= P_NO_CLERIC;
2199 
2200  if (QUERY_FLAG(tmp, FLAG_BLOCKSVIEW))
2201  flags |= P_BLOCKSVIEW;
2202  } /* for stack of objects */
2203 
2204  if (player)
2205  flags |= P_PLAYER;
2206 
2207  /* we don't want to rely on this function to have accurate flags, but
2208  * since we're already doing the work, we calculate them here.
2209  * if they don't match, logic is broken someplace.
2210  */
2211  if (((oldflags&~(P_NEED_UPDATE|P_NO_ERROR)) != flags)
2212  && (!(oldflags&P_NO_ERROR))) {
2213  LOG(llevDebug, "update_position: updated flags do not match old flags: %s (old=%d,new=%d) %x != %x\n",
2214  m->path, x, y, (oldflags&~P_NEED_UPDATE), flags);
2215  }
2216 
2217  SET_MAP_FLAGS(m, x, y, flags);
2218  SET_MAP_MOVE_BLOCK(m, x, y, move_block&~move_allow);
2219  SET_MAP_MOVE_ON(m, x, y, move_on);
2220  SET_MAP_MOVE_OFF(m, x, y, move_off);
2221  SET_MAP_MOVE_SLOW(m, x, y, move_slow);
2222  SET_MAP_LIGHT(m, x, y, light);
2223 
2224  /* Note that player may be NULL here, which is fine - if no player, need
2225  * to clear any value that may be set.
2226  */
2227  SET_MAP_PLAYER(m, x, y, player);
2228 
2229  /* Note it is intentional we copy everything, including NULL values. */
2230  memcpy(GET_MAP_FACE_OBJS(m, x, y), layers, sizeof(object *)*MAP_LAYERS);
2231 }
2232 
2240  int timeout;
2241 
2242  timeout = MAP_RESET_TIMEOUT(map);
2243  if (timeout <= 0)
2244  timeout = MAP_DEFAULTRESET;
2245  if (timeout >= MAP_MAXRESET)
2246  timeout = MAP_MAXRESET;
2247  MAP_WHEN_RESET(map) = seconds()+timeout;
2248 }
2249 
2267 static mapstruct *load_and_link_tiled_map(mapstruct *orig_map, int tile_num) {
2268  int dest_tile = (tile_num+2)%4;
2269  char path[HUGE_BUF];
2270 
2271  path_combine_and_normalize(orig_map->path, orig_map->tile_path[tile_num], path, sizeof(path));
2272 
2273  orig_map->tile_map[tile_num] = ready_map_name(path, 0);
2274 
2275  /* need to do a strcmp here as the orig_map->path is not a shared string */
2276  if (orig_map->tile_map[tile_num]->tile_path[dest_tile]
2277  && !strcmp(orig_map->tile_map[tile_num]->tile_path[dest_tile], orig_map->path))
2278  orig_map->tile_map[tile_num]->tile_map[dest_tile] = orig_map;
2279 
2280  return orig_map->tile_map[tile_num];
2281 }
2282 
2300 int out_of_map(mapstruct *m, int x, int y) {
2301  /* If we get passed a null map, this is obviously the
2302  * case. This generally shouldn't happen, but if the
2303  * map loads fail below, it could happen.
2304  */
2305  if (!m)
2306  return 0;
2307 
2308  /* Simple case - coordinates are within this local
2309  * map.
2310  */
2311  if (x >= 0 && x < MAP_WIDTH(m) && y >= 0 && y < MAP_HEIGHT(m))
2312  return 0;
2313 
2314  if (x < 0) {
2315  if (!m->tile_path[3])
2316  return 1;
2317  if (!m->tile_map[3] || m->tile_map[3]->in_memory != MAP_IN_MEMORY) {
2319  }
2320  return (out_of_map(m->tile_map[3], x+MAP_WIDTH(m->tile_map[3]), y));
2321  }
2322  if (x >= MAP_WIDTH(m)) {
2323  if (!m->tile_path[1])
2324  return 1;
2325  if (!m->tile_map[1] || m->tile_map[1]->in_memory != MAP_IN_MEMORY) {
2327  }
2328  return (out_of_map(m->tile_map[1], x-MAP_WIDTH(m), y));
2329  }
2330  if (y < 0) {
2331  if (!m->tile_path[0])
2332  return 1;
2333  if (!m->tile_map[0] || m->tile_map[0]->in_memory != MAP_IN_MEMORY) {
2335  }
2336  return (out_of_map(m->tile_map[0], x, y+MAP_HEIGHT(m->tile_map[0])));
2337  }
2338  if (y >= MAP_HEIGHT(m)) {
2339  if (!m->tile_path[2])
2340  return 1;
2341  if (!m->tile_map[2] || m->tile_map[2]->in_memory != MAP_IN_MEMORY) {
2343  }
2344  return (out_of_map(m->tile_map[2], x, y-MAP_HEIGHT(m)));
2345  }
2346  return 1;
2347 }
2348 
2367 
2368  /* Simple case - coordinates are within this local
2369  * map.
2370  */
2371 
2372  if (*x >= 0 && *x < MAP_WIDTH(m) && *y >= 0 && *y < MAP_HEIGHT(m))
2373  return m;
2374 
2375  if (*x < 0) {
2376  if (!m->tile_path[3])
2377  return NULL;
2378  if (!m->tile_map[3] || m->tile_map[3]->in_memory != MAP_IN_MEMORY)
2380 
2381  *x += MAP_WIDTH(m->tile_map[3]);
2382  return (get_map_from_coord(m->tile_map[3], x, y));
2383  }
2384  if (*x >= MAP_WIDTH(m)) {
2385  if (!m->tile_path[1])
2386  return NULL;
2387  if (!m->tile_map[1] || m->tile_map[1]->in_memory != MAP_IN_MEMORY)
2389 
2390  *x -= MAP_WIDTH(m);
2391  return (get_map_from_coord(m->tile_map[1], x, y));
2392  }
2393  if (*y < 0) {
2394  if (!m->tile_path[0])
2395  return NULL;
2396  if (!m->tile_map[0] || m->tile_map[0]->in_memory != MAP_IN_MEMORY)
2398 
2399  *y += MAP_HEIGHT(m->tile_map[0]);
2400  return (get_map_from_coord(m->tile_map[0], x, y));
2401  }
2402  if (*y >= MAP_HEIGHT(m)) {
2403  if (!m->tile_path[2])
2404  return NULL;
2405  if (!m->tile_map[2] || m->tile_map[2]->in_memory != MAP_IN_MEMORY)
2407 
2408  *y -= MAP_HEIGHT(m);
2409  return (get_map_from_coord(m->tile_map[2], x, y));
2410  }
2411  return NULL; /* Shouldn't get here */
2412 }
2413 
2427 static int adjacent_map(const mapstruct *map1, const mapstruct *map2, int *dx, int *dy) {
2428  if (!map1 || !map2)
2429  return 0;
2430 
2431  if (map1 == map2) {
2432  *dx = 0;
2433  *dy = 0;
2434  } else if (map1->tile_map[0] == map2) { /* up */
2435  *dx = 0;
2436  *dy = -MAP_HEIGHT(map2);
2437  } else if (map1->tile_map[1] == map2) { /* right */
2438  *dx = MAP_WIDTH(map1);
2439  *dy = 0;
2440  } else if (map1->tile_map[2] == map2) { /* down */
2441  *dx = 0;
2442  *dy = MAP_HEIGHT(map1);
2443  } else if (map1->tile_map[3] == map2) { /* left */
2444  *dx = -MAP_WIDTH(map2);
2445  *dy = 0;
2446  } else if (map1->tile_map[0] && map1->tile_map[0]->tile_map[1] == map2) { /* up right */
2447  *dx = MAP_WIDTH(map1->tile_map[0]);
2448  *dy = -MAP_HEIGHT(map1->tile_map[0]);
2449  } else if (map1->tile_map[0] && map1->tile_map[0]->tile_map[3] == map2) { /* up left */
2450  *dx = -MAP_WIDTH(map2);
2451  *dy = -MAP_HEIGHT(map1->tile_map[0]);
2452  } else if (map1->tile_map[1] && map1->tile_map[1]->tile_map[0] == map2) { /* right up */
2453  *dx = MAP_WIDTH(map1);
2454  *dy = -MAP_HEIGHT(map2);
2455  } else if (map1->tile_map[1] && map1->tile_map[1]->tile_map[2] == map2) { /* right down */
2456  *dx = MAP_WIDTH(map1);
2457  *dy = MAP_HEIGHT(map1->tile_map[1]);
2458  } else if (map1->tile_map[2] && map1->tile_map[2]->tile_map[1] == map2) { /* down right */
2459  *dx = MAP_WIDTH(map1->tile_map[2]);
2460  *dy = MAP_HEIGHT(map1);
2461  } else if (map1->tile_map[2] && map1->tile_map[2]->tile_map[3] == map2) { /* down left */
2462  *dx = -MAP_WIDTH(map2);
2463  *dy = MAP_HEIGHT(map1);
2464  } else if (map1->tile_map[3] && map1->tile_map[3]->tile_map[0] == map2) { /* left up */
2465  *dx = -MAP_WIDTH(map1->tile_map[3]);
2466  *dy = -MAP_HEIGHT(map2);
2467  } else if (map1->tile_map[3] && map1->tile_map[3]->tile_map[2] == map2) { /* left down */
2468  *dx = -MAP_WIDTH(map1->tile_map[3]);
2469  *dy = MAP_HEIGHT(map1->tile_map[3]);
2470  } else { /* not "adjacent" enough */
2471  return 0;
2472  }
2473 
2474  return 1;
2475 }
2476 
2504 void get_rangevector(object *op1, const object *op2, rv_vector *retval, int flags) {
2505  if (!adjacent_map(op1->map, op2->map, &retval->distance_x, &retval->distance_y)) {
2506  /* be conservative and fill in _some_ data */
2507  retval->distance = 100000;
2508  retval->distance_x = 32767;
2509  retval->distance_y = 32767;
2510  retval->direction = 0;
2511  retval->part = NULL;
2512  } else {
2513  object *best;
2514 
2515  retval->distance_x += op2->x-op1->x;
2516  retval->distance_y += op2->y-op1->y;
2517 
2518  best = op1;
2519  /* If this is multipart, find the closest part now */
2520  if (!(flags&0x1) && op1->more) {
2521  object *tmp;
2522  int best_distance = retval->distance_x*retval->distance_x+
2523  retval->distance_y*retval->distance_y, tmpi;
2524 
2525  /* we just take the offset of the piece to head to figure
2526  * distance instead of doing all that work above again
2527  * since the distance fields we set above are positive in the
2528  * same axis as is used for multipart objects, the simply arithmetic
2529  * below works.
2530  */
2531  for (tmp = op1->more; tmp != NULL; tmp = tmp->more) {
2532  tmpi = (op1->x-tmp->x+retval->distance_x)*(op1->x-tmp->x+retval->distance_x)+
2533  (op1->y-tmp->y+retval->distance_y)*(op1->y-tmp->y+retval->distance_y);
2534  if (tmpi < best_distance) {
2535  best_distance = tmpi;
2536  best = tmp;
2537  }
2538  }
2539  if (best != op1) {
2540  retval->distance_x += op1->x-best->x;
2541  retval->distance_y += op1->y-best->y;
2542  }
2543  }
2544  retval->part = best;
2545  retval->distance = isqrt(retval->distance_x*retval->distance_x+retval->distance_y*retval->distance_y);
2546  retval->direction = find_dir_2(-retval->distance_x, -retval->distance_y);
2547  }
2548 }
2549 
2573 void get_rangevector_from_mapcoord(const mapstruct *m, int x, int y, const object *op2, rv_vector *retval, int flags) {
2574  if (!adjacent_map(m, op2->map, &retval->distance_x, &retval->distance_y)) {
2575  /* be conservative and fill in _some_ data */
2576  retval->distance = 100000;
2577  retval->distance_x = 32767;
2578  retval->distance_y = 32767;
2579  retval->direction = 0;
2580  retval->part = NULL;
2581  } else {
2582  retval->distance_x += op2->x-x;
2583  retval->distance_y += op2->y-y;
2584 
2585  retval->part = NULL;
2586  retval->distance = isqrt(retval->distance_x*retval->distance_x+retval->distance_y*retval->distance_y);
2587  retval->direction = find_dir_2(-retval->distance_x, -retval->distance_y);
2588  }
2589 }
2590 
2609 int on_same_map(const object *op1, const object *op2) {
2610  int dx, dy;
2611 
2612  return adjacent_map(op1->map, op2->map, &dx, &dy);
2613 }
2614 
2620  char base[HUGE_BUF], path[HUGE_BUF];
2621  int count;
2622 
2623  if (map->unique) {
2624  /* Unique maps have their full path already set. */
2625  unlink(map->path);
2626  return;
2627  }
2628 
2629  create_items_path(map->path, base, sizeof(base));
2630 
2631  for (count = 0; count < 10; count++) {
2632  snprintf(path, sizeof(path), "%s.v%02d", base, count);
2633  unlink(path);
2634  }
2635 }
#define FLAG_NO_MAGIC
Definition: define.h:572
char path[HUGE_BUF]
Definition: map.h:384
uint8 map_layer
Definition: object.h:275
int find_dir_2(int x, int y)
Definition: object.c:3380
void free_all_maps(void)
Definition: map.c:1968
uint64 shopmax
Definition: map.h:379
int get_map_flags(mapstruct *oldmap, mapstruct **newmap, sint16 x, sint16 y, sint16 *nx, sint16 *ny)
Definition: map.c:330
Definition: player.h:146
double shopgreed
Definition: map.h:377
#define FLAG_DAMNED
Definition: define.h:614
#define FLAG_IS_FLOOR
Definition: define.h:599
#define FLAG_UNPAID
Definition: define.h:532
MapSpace * spaces
Definition: map.h:374
static void allocate_map(mapstruct *m)
Definition: map.c:854
#define CHECK_INV
Definition: define.h:206
#define MAP_STYLE
Definition: map.h:119
#define FLAG_IS_LINKED
Definition: define.h:612
MoveType move_type
Definition: object.h:277
#define S_IRGRP
Definition: win32.h:99
#define OUT_OF_MEMORY
Definition: define.h:94
MoveType move_on
Definition: object.h:280
signed short sint16
Definition: global.h:72
mapstruct * get_linked_map(void)
Definition: map.c:816
Definition: map.h:399
#define MSG_SUBTYPE_NONE
Definition: newclient.h:339
#define P_PLAYER
Definition: map.h:257
#define SAVE_ERROR_OK
Definition: map.h:164
static mapstruct * load_and_link_tiled_map(mapstruct *orig_map, int tile_num)
Definition: map.c:2267
char * tempnam_local(const char *dir, const char *pfx)
Definition: porting.c:87
#define P_NEED_UPDATE
Definition: map.h:260
#define MAP_FLUSH
Definition: map.h:116
#define CALLOC(x, y)
Definition: global.h:295
mapstruct * get_empty_map(int sizex, int sizey)
Definition: map.c:884
#define SET_FLAG(xyz, p)
Definition: define.h:510
MoveType move_allow
Definition: object.h:279
region * get_region_by_name(const char *region_name)
Definition: region.c:57
void make_path_to_file(const char *filename)
Definition: porting.c:764
object * part
Definition: map.h:404
const char * name_pl
Definition: define.h:380
static int save_objects(mapstruct *m, FILE *fp, FILE *fp2, int flag)
Definition: map.c:768
#define MAP_LAYER_ITEM1
Definition: map.h:71
#define P_NO_MAGIC
Definition: map.h:248
int gettimeofday(struct timeval *time_Info, struct timezone *timezone_Info)
Definition: win32.c:54
struct mapdef * tile_map[4]
Definition: map.h:383
void remove_from_active_list(object *op)
Definition: object.c:1070
#define DOOR
Definition: define.h:135
char * create_pathname(const char *name, char *buf, size_t size)
Definition: map.c:114
mapstruct * ready_map_name(const char *name, int flags)
Definition: map.c:1809
unsigned char MoveType
Definition: define.h:725
#define popen(__a, __b)
Definition: win32.h:70
static mapstruct * load_overlay_map(const char *filename, mapstruct *m)
Definition: map.c:1341
#define LO_REPEAT
Definition: loader.h:43
#define MAP_ENTER_X(m)
Definition: map.h:104
void free_string(sstring str)
Definition: shstr.c:272
#define MAP_DEFAULTRESET
Definition: config.h:432
#define HUGE_BUF
Definition: define.h:83
#define MAP_MAXRESET
Definition: config.h:430
#define S_IWOTH
Definition: win32.h:87
#define SET_MAP_LIGHT(M, X, Y, L)
Definition: map.h:188
#define MAP_HEIGHT(m)
Definition: map.h:99
object clone
Definition: object.h:326
sint16 invisible
Definition: object.h:211
int calculate_difficulty(mapstruct *m)
Definition: map.c:1917
#define MAX_DARKNESS
Definition: define.h:763
void draw_ext_info(int flags, int pri, const object *pl, uint8 type, uint8 subtype, const char *message, const char *oldmessage)
Definition: standalone.c:171
sint64 level_exp(int level, double expmul)
Definition: living.c:1788
struct Map_Layer_Info Map_Layer_Info
sint32 last_sp
Definition: object.h:209
static mapstruct * load_temporary_map(mapstruct *m)
Definition: map.c:1288
void decay_objects(mapstruct *m)
Definition: utils.c:187
sint64 exp
Definition: living.h:88
static shopitems * parse_shop_string(const char *input_string)
Definition: map.c:904
#define MAP_LAYER_FLOOR
Definition: map.h:68
uint32 in_memory
Definition: map.h:366
struct obj * above
Definition: object.h:146
void close_and_delete(FILE *fp, int compressed)
Definition: porting.c:748
mapstruct * load_original_map(const char *filename, int flags)
Definition: map.c:1226
const char * name
Definition: map.h:327
char * maplore
Definition: map.h:381
#define OUT_OF_REAL_MAP(M, X, Y)
Definition: map.h:238
#define SAVE_MODE_NORMAL
Definition: map.h:141
sint16 x
Definition: object.h:179
signed long sum_weight(object *op)
Definition: object.c:317
const typedata * get_typedata_by_name(const char *name)
Definition: item.c:372
#define NDI_BLACK
Definition: newclient.h:195
void strip_endline(char *buf)
Definition: utils.c:431
uint16 height
Definition: map.h:370
void free_map(mapstruct *m)
Definition: map.c:1707
int distance_y
Definition: map.h:402
Definition: object.h:321
#define PLAYER
Definition: define.h:113
int change_map_light(mapstruct *m, int change)
Definition: map.c:2000
#define FLAG_REMOVED
Definition: define.h:528
#define MAP_WHEN_RESET(m)
Definition: map.h:81
int typenum
Definition: map.h:329
sint32 timeout
Definition: map.h:362
#define MAP_LAYER_LIVING2
Definition: map.h:75
#define NDI_NAVY
Definition: newclient.h:197
const typedata * get_typedata(int itemtype)
Definition: item.c:352
#define MOVE_ALL
Definition: define.h:706
const char * name
Definition: map.h:299
char * name
Definition: map.h:349
#define SAVE_FLAG_SAVE_UNPAID
Definition: map.h:131
void fix_multipart_object(object *tmp)
Definition: object.c:4007
#define MAP_IN_MEMORY
Definition: map.h:151
#define MAP_LAYER_NO_PICK1
Definition: map.h:69
const char * title
Definition: object.h:170
uint8 flags
Definition: map.h:283
static int load_map_header(FILE *fp, mapstruct *m)
Definition: map.c:1019
void remove_ob(object *op)
Definition: object.c:1515
void get_rangevector_from_mapcoord(const mapstruct *m, int x, int y, const object *op2, rv_vector *retval, int flags)
Definition: map.c:2573
#define FLAG_ALIVE
Definition: define.h:526
void fix_auto_apply(mapstruct *m)
Definition: standalone.c:130
#define S_IROTH
Definition: win32.h:96
#define TRANSPORT
Definition: define.h:114
#define LL_NORMAL
Definition: loader.h:40
#define MAP_OVERLAY
Definition: map.h:120
#define FLAG_OBJ_ORIGINAL
Definition: define.h:661
#define MAP_SWAPPED
Definition: map.h:152
int index
Definition: map.h:332
uint8 honor_visibility
Definition: map.c:62
uint32 hidden
Definition: player.h:186
#define INS_ON_TOP
Definition: object.h:397
int blocked_link(object *ob, mapstruct *m, int sx, int sy)
Definition: map.c:373
#define LO_NEWFILE
Definition: loader.h:45
const char * tmpdir
Definition: global.h:343
#define LL_MORE
Definition: loader.h:39
struct mapdef * map
Definition: object.h:155
#define SET_MAP_MOVE_SLOW(M, X, Y, C)
Definition: map.h:220
void get_rangevector(object *op1, const object *op2, rv_vector *retval, int flags)
Definition: map.c:2504
static void link_multipart_objects(mapstruct *m)
Definition: map.c:652
#define MAP_TIMEOUT(m)
Definition: map.h:85
void dump_all_maps(void)
Definition: map.c:298
char * path_combine_and_normalize(const char *src, const char *dst, char *path, size_t size)
Definition: path.c:184
#define MOVE_FLYING
Definition: define.h:703
char * msg
Definition: map.h:380
void update_position(mapstruct *m, int x, int y)
Definition: map.c:2124
#define S_IWGRP
Definition: win32.h:90
#define MAP_LAYERS
Definition: map.h:60
#define R_OK
Definition: win32.h:75
#define SET_MAP_PLAYER(M, X, Y, C)
Definition: map.h:190
struct timeval last_reset_time
Definition: map.h:385
object * objects
Definition: object.c:70
void remove_button_link(object *op)
Definition: button.c:700
#define FLAG_BLOCKSVIEW
Definition: define.h:565
const char * name
Definition: object.h:167
long seconds(void)
Definition: time.c:417
struct obj * env
Definition: object.h:151
uint32 reset_timeout
Definition: map.h:354
int distance_x
Definition: map.h:401
struct obj * below
Definition: object.h:145
struct archt * more
Definition: object.h:325
int execute_global_event(int eventcode,...)
Definition: standalone.c:229
uint16 difficulty
Definition: map.h:364
char * tile_path[4]
Definition: map.h:382
#define INS_NO_WALK_ON
Definition: object.h:396
void fatal(int err)
Definition: glue.c:60
#define MAP_LAYER_ITEM3
Definition: map.h:73
#define MAP_ENTER_Y(m)
Definition: map.h:106
unsigned char uint8
Definition: global.h:75
#define OB_TYPE_MOVE_BLOCK(ob1, type)
Definition: define.h:740
#define FLAG_OVERLAY_FLOOR
Definition: define.h:551
#define P_OUT_OF_MAP
Definition: map.h:272
MoveType move_off
Definition: object.h:281
#define SAVE_ERROR_NO_PATH
Definition: map.h:168
mapstruct * get_map_from_coord(mapstruct *m, sint16 *x, sint16 *y)
Definition: map.c:2366
#define GET_MAP_MOVE_BLOCK(M, X, Y)
Definition: map.h:213
sint16 y
Definition: object.h:179
struct pl * contr
Definition: object.h:134
int nrofallocobjects
Definition: object.c:67
const char * templatedir
Definition: global.h:342
#define MAP_DIFFICULTY(m)
Definition: map.h:84
#define NROF_COMPRESS_METHODS
Definition: global.h:201
#define SAVE_ERROR_URENAME
Definition: map.h:169
static void delete_unique_items(mapstruct *m)
Definition: map.c:1379
uint16 enter_y
Definition: map.h:372
#define MAP_SAVING
Definition: map.h:154
void update_buttons(mapstruct *m)
Definition: button.c:241
#define SET_MAP_MOVE_OFF(M, X, Y, C)
Definition: map.h:230
sint8 strength
Definition: map.h:330
const char * map_layer_name[MAP_LAYERS]
Definition: map.c:54
int on_same_map(const object *op1, const object *op2)
Definition: map.c:2609
#define QUERY_FLAG(xyz, p)
Definition: define.h:514
#define CLEAR_FLAG(xyz, p)
Definition: define.h:512
char * strdup_local(const char *str)
Definition: porting.c:310
static Map_Layer_Info map_layer_info[MAP_LAYERS]
Definition: map.c:71
#define MSG_TYPE_MISC
Definition: newclient.h:335
#define INS_ABOVE_FLOOR_ONLY
Definition: object.h:395
#define EVENT_MAPLOAD
Definition: plugin.h:95
#define FLAG_WIZ
Definition: define.h:527
#define SAVE_ERROR_CLOSE
Definition: map.h:170
object * insert_ob_in_ob(object *op, object *where)
Definition: object.c:2510
void dump_map(const mapstruct *m)
Definition: map.c:275
#define MAX_BUF
Definition: define.h:81
oblinkpt * buttons
Definition: map.h:373
object * get_object(void)
Definition: object.c:921
#define GENERATE_TYPE(xyz)
Definition: define.h:745
uint8 compressed
Definition: map.h:367
#define EVENT_MAPUNLOAD
Definition: plugin.h:94
int save_object(FILE *fp, object *op, int flag)
object * insert_ob_in_map(object *op, mapstruct *m, object *originator, int flag)
Definition: object.c:1992
MoveType move_slow
Definition: object.h:282
#define MAP_LAYER_FLY1
Definition: map.h:76
static const flag_definition flags[]
struct obj * next
Definition: object.h:135
const char * uniquedir
Definition: global.h:341
#define MAP_LAYER_NO_PICK2
Definition: map.h:70
static int adjacent_map(const mapstruct *map1, const mapstruct *map2, int *dx, int *dy)
Definition: map.c:2427
uint8 visibility
Definition: face.h:45
char * tmpname
Definition: map.h:348
#define SAVE_FLAG_NO_REMOVE
Definition: map.h:132
int save_map(mapstruct *m, int flag)
Definition: map.c:1453
#define pclose(__a)
Definition: win32.h:71
uint8 darkness
Definition: map.h:369
uint32 unique
Definition: map.h:358
#define SAVE_ERROR_UCREATION
Definition: map.h:166
#define GET_MAP_FLAGS(M, X, Y)
Definition: map.h:182
const char * datadir
Definition: global.h:334
object * check_inv_recursive(object *op, const object *trig)
Definition: button.c:788
#define MAP_BLOCK
Definition: map.h:118
int snprintf(char *dest, int max, const char *format,...)
Definition: porting.c:498
const char * uncomp[NROF_COMPRESS_METHODS][3]
Definition: porting.c:593
static void free_all_objects(mapstruct *m)
Definition: map.c:1659
#define SET_MAP_MOVE_ON(M, X, Y, C)
Definition: map.h:225
void set_map_reset_time(mapstruct *map)
Definition: map.c:2239
uint32 is_template
Definition: map.h:359
#define SET_MAP_FLAGS(M, X, Y, C)
Definition: map.h:184
#define P_NEW_MAP
Definition: map.h:273
#define FLAG_GENERATOR
Definition: define.h:544
#define MAP_LAYER_LIVING1
Definition: map.h:74
#define INS_NO_MERGE
Definition: object.h:394
static void load_unique_objects(mapstruct *m)
Definition: map.c:1406
Definition: map.h:279
const char * name
Definition: define.h:379
struct obj * owner
Definition: object.h:228
void update_all_map_los(mapstruct *map)
Definition: los.c:516
New_Face * blank_face
Definition: image.c:66
#define MAP_PLAYER_UNIQUE
Definition: map.h:117
const char * localdir
Definition: global.h:335
archetype * get_archetype_by_type_subtype(int type, int subtype)
Definition: arch.c:149
uint16 width
Definition: map.h:370
const char * name_pl
Definition: map.h:328
living stats
Definition: object.h:219
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:263
#define AB_NO_PASS
Definition: map.h:256
#define MAP_WIDTH(m)
Definition: map.h:97
int nroffreeobjects
Definition: object.c:66
#define unlink(__a)
Definition: win32.h:67
const char * mapdir
Definition: global.h:337
static void print_shop_string(mapstruct *m, char *output_string, int size)
Definition: map.c:979
uint32 fixed_resettime
Definition: map.h:356
unsigned int distance
Definition: map.h:400
struct Settings settings
Definition: init.c:48
int isqrt(int n)
Definition: porting.c:573
static void load_objects(mapstruct *m, FILE *fp, int mapflags)
Definition: map.c:682
void fix_container(object *container)
Definition: map.c:580
int direction
Definition: map.h:403
#define SAVE_MODE
Definition: config.h:634
Definition: map.h:326
#define TEMP_EXT
int out_of_map(mapstruct *m, int x, int y)
Definition: map.c:2300
void delete_map(mapstruct *m)
Definition: map.c:1745
const char * msg
Definition: object.h:175
#define MAP_RESET_TIMEOUT(m)
Definition: map.h:83
#define MAP_LOADING
Definition: map.h:153
#define GET_MAP_FACE_OBJS(M, X, Y)
Definition: map.h:210
sint32 swap_time
Definition: map.h:363
static void create_items_path(const char *s, char *buf, size_t size)
Definition: map.c:178
sstring add_string(const char *str)
Definition: shstr.c:116
#define GET_MAP_OB(M, X, Y)
Definition: map.h:193
int check_path(const char *name, int prepend_dir)
Definition: map.c:216
sint8 glow_radius
Definition: object.h:215
#define FLAG_MONSTER
Definition: define.h:541
static void fix_container_multipart(object *container)
Definition: map.c:607
struct obj * inv
Definition: object.h:148
#define NDI_UNIQUE
Definition: newclient.h:219
#define OB_MOVE_BLOCK(ob1, ob2)
Definition: define.h:731
struct obj * head
Definition: object.h:154
#define SAVE_ERROR_RRENAME
Definition: map.h:171
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:63
#define SAVE_ERROR_RCREATION
Definition: map.h:165
#define LO_NOREAD
Definition: loader.h:46
struct shopitem * shopitems
Definition: map.h:375
MoveType move_block
Definition: object.h:278
#define S_IRUSR
Definition: win32.h:102
#define S_IWUSR
Definition: win32.h:93
struct mapdef * next
Definition: map.h:347
#define P_BLOCKSVIEW
Definition: map.h:247
#define MSG_TYPE_ATTACK_NOKEY
Definition: newclient.h:532
#define P_NO_ERROR
Definition: map.h:261
void set_darkness_map(mapstruct *m)
Definition: standalone.c:206
int number
Definition: define.h:378
char * shoprace
Definition: map.h:376
#define MAP_LAYER_FLY2
Definition: map.h:77
void free_object(object *ob)
Definition: object.c:1238
void create_overlay_pathname(const char *name, char *buf, size_t size)
Definition: map.c:135
uint64 shopmin
Definition: map.h:378
Definition: map.h:346
char * background_music
Definition: map.h:386
#define P_IS_ALIVE
Definition: map.h:258
char * strerror_local(int errnum, char *buf, size_t size)
Definition: porting.c:525
New_Face * face
Definition: object.h:183
#define FREE_AND_CLEAR(xyz)
Definition: global.h:282
#define FLAG_UNIQUE
Definition: define.h:584
#define FLAG_NO_PICK
Definition: define.h:535
#define SET_MAP_MOVE_BLOCK(M, X, Y, C)
Definition: map.h:215
#define S_ISREG(x)
Definition: win32.h:81
void create_template_pathname(const char *name, char *buf, size_t size)
Definition: map.c:155
void map_remove_unique_files(const mapstruct *map)
Definition: map.c:2619
struct regiondef * region
Definition: map.h:350
uint32 outdoor
Definition: map.h:361
void clean_tmp_map(mapstruct *m)
Definition: map.c:1959
EXTERN mapstruct * first_map
Definition: global.h:191
struct obj * more
Definition: object.h:153
object * arch_to_object(archetype *at)
Definition: arch.c:576
FILE * open_and_uncompress(const char *name, int flag, int *compressed)
Definition: porting.c:724
void clean_object(object *op)
Definition: map.c:1640
uint32 nosmooth
Definition: map.h:360
void ext_info_map(int color, const mapstruct *map, uint8 type, uint8 subtype, const char *str1, const char *str2)
Definition: standalone.c:184
#define SAVE_MODE_OVERLAY
Definition: map.h:143
#define P_NO_CLERIC
Definition: map.h:259
#define MSG_TYPE_ATTACK
Definition: newclient.h:331
uint8 type
Definition: object.h:189
static void add_face_layer(int low_layer, int high_layer, object *ob, object *layers[], int honor_visibility)
Definition: map.c:2055
uint8 high_layer
Definition: map.c:61
int ob_blocked(const object *ob, mapstruct *m, sint16 x, sint16 y)
Definition: map.c:525
#define INS_MAP_LOAD
Definition: object.h:399
uint16 enter_x
Definition: map.h:371