Crossfire Server, Trunk  R20513
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, "loading map and got a non 'arch map' line(%s %s)?\n", key, value);
1123  } else if (!strcmp(key, "name")) {
1124  /* When loading eg an overlay, the name is already set, so free() current one. */
1125  free(m->name);
1126  m->name = strdup_local(value);
1127  /* first strcmp value on these are old names supported
1128  * for compatibility reasons. The new values (second) are
1129  * what really should be used.
1130  */
1131  } else if (!strcmp(key, "enter_x")) {
1132  m->enter_x = atoi(value);
1133  } else if (!strcmp(key, "enter_y")) {
1134  m->enter_y = atoi(value);
1135  } else if (!strcmp(key, "width")) {
1136  m->width = atoi(value);
1137  } else if (!strcmp(key, "height")) {
1138  m->height = atoi(value);
1139  } else if (!strcmp(key, "reset_timeout")) {
1140  m->reset_timeout = atoi(value);
1141  } else if (!strcmp(key, "swap_time")) {
1142  m->timeout = atoi(value);
1143  } else if (!strcmp(key, "difficulty")) {
1144  m->difficulty = atoi(value);
1145  } else if (!strcmp(key, "darkness")) {
1146  m->darkness = atoi(value);
1147  } else if (!strcmp(key, "fixed_resettime")) {
1148  m->fixed_resettime = atoi(value);
1149  } else if (!strcmp(key, "unique")) {
1150  m->unique = atoi(value);
1151  } else if (!strcmp(key, "template")) {
1152  m->is_template = atoi(value);
1153  } else if (!strcmp(key, "region")) {
1154  m->region = get_region_by_name(value);
1155  } else if (!strcmp(key, "shopitems")) {
1156  m->shopitems = parse_shop_string(value, m);
1157  } else if (!strcmp(key, "shopgreed")) {
1158  m->shopgreed = atof(value);
1159  } else if (!strcmp(key, "shopmin")) {
1160  m->shopmin = atol(value);
1161  } else if (!strcmp(key, "shopmax")) {
1162  m->shopmax = atol(value);
1163  } else if (!strcmp(key, "shoprace")) {
1164  m->shoprace = strdup_local(value);
1165  } else if (!strcmp(key, "outdoor")) {
1166  m->outdoor = atoi(value);
1167  } else if (!strcmp(key, "nosmooth")) {
1168  m->nosmooth = atoi(value);
1169  } else if (!strcmp(key, "first_load")) {
1170  m->last_reset_time = atoi(value);
1171  } else if (!strncmp(key, "tile_path_", 10)) {
1172  int tile = atoi(key+10);
1173 
1174  if (tile < 1 || tile > 4) {
1175  LOG(llevError, "load_map_header: tile location %d out of bounds (%s)\n", tile, m->path);
1176  } else {
1177  char path[HUGE_BUF];
1178 
1179  if (m->tile_path[tile-1]) {
1180  LOG(llevError, "load_map_header: tile location %d duplicated (%s)\n", tile, m->path);
1181  free(m->tile_path[tile-1]);
1182  m->tile_path[tile-1] = NULL;
1183  }
1184 
1185  if (check_path(value, 1) != -1) {
1186  /* The unadorned path works. */
1187  snprintf(path, sizeof(path), "%s", value);
1188  } else {
1189  /* Try again; it could be a relative exit. */
1190  path_combine_and_normalize(m->path, value, path, sizeof(path));
1191 
1192  if (check_path(path, 1) == -1) {
1193  LOG(llevError, "get_map_header: Bad tile path %s %s\n", m->path, value);
1194  path[0] = '\0';
1195  }
1196  }
1197 
1198  if (*path != '\0') {
1199  /* Use the normalized value. */
1200  m->tile_path[tile-1] = strdup_local(path);
1201  }
1202  } /* end if tile direction (in)valid */
1203  } else if (!strcmp(key, "background_music")) {
1204  m->background_music = strdup_local(value);
1205  } else {
1206  LOG(llevError, "Got unknown value in map header: %s %s\n", key, value);
1207  }
1208  }
1209  if ((m->width == 0) || (m->height == 0)) {
1210  LOG(llevError, "Map width or height not specified\n");
1211  return 1;
1212  }
1213  if (!key || strcmp(key, "end")) {
1214  LOG(llevError, "Got premature eof on map header!\n");
1215  return 1;
1216  }
1217  return 0;
1218 }
1219 
1235 mapstruct *mapfile_load(const char *map, int flags) {
1236  FILE *fp;
1237  mapstruct *m;
1238  char pathname[MAX_BUF];
1239 
1240  if (flags&MAP_PLAYER_UNIQUE)
1241  snprintf(pathname, sizeof(pathname), "%s", map);
1242  else if (flags&MAP_OVERLAY)
1243  create_overlay_pathname(map, pathname, MAX_BUF);
1244  else
1245  create_pathname(map, pathname, MAX_BUF);
1246 
1247  if ((fp = fopen(pathname, "r")) == NULL) {
1248  LOG((flags&MAP_PLAYER_UNIQUE) ? llevDebug : llevError,
1249  "Can't open %s: %s\n", pathname, strerror(errno));
1250  return (NULL);
1251  }
1252 
1253  m = get_linked_map();
1254 
1255  safe_strncpy(m->path, map, HUGE_BUF);
1256  if (load_map_header(fp, m)) {
1257  LOG(llevError, "Error loading map header for %s, flags=%d\n", map, flags);
1258  delete_map(m);
1259  fclose(fp);
1260  return NULL;
1261  }
1262 
1263  allocate_map(m);
1264 
1265  m->in_memory = MAP_LOADING;
1266  load_objects(m, fp, flags & MAP_STYLE);
1267  fclose(fp);
1268  m->in_memory = MAP_IN_MEMORY;
1269  if (!MAP_DIFFICULTY(m))
1271  set_map_reset_time(m);
1272 
1273  /* In case other objects press some buttons down */
1274  update_buttons(m);
1275 
1276  /* Handle for map load event */
1278 
1279  if (!(flags & MAP_STYLE))
1280  apply_auto_fix(m); /* Chests which open as default */
1281 
1282  return (m);
1283 }
1284 
1294  FILE *fp;
1295 
1296  if (!m->tmpname) {
1297  LOG(llevError, "No temporary filename for map %s\n", m->path);
1298  return 1;
1299  }
1300 
1301  if ((fp = fopen(m->tmpname, "r")) == NULL) {
1302  LOG(llevError, "Cannot open %s: %s\n", m->tmpname, strerror(errno));
1303  return 2;
1304  }
1305 
1306  if (load_map_header(fp, m)) {
1307  LOG(llevError, "Error loading map header for %s (%s)\n", m->path, m->tmpname);
1308  fclose(fp);
1309  return 3;
1310  }
1311  allocate_map(m);
1312 
1313  m->in_memory = MAP_LOADING;
1314  load_objects(m, fp, 0);
1315  fclose(fp);
1316  m->in_memory = MAP_IN_MEMORY;
1317  return 0;
1318 }
1319 
1329 static int load_overlay_map(const char *filename, mapstruct *m) {
1330  FILE *fp;
1331  char pathname[MAX_BUF];
1332 
1333  create_overlay_pathname(filename, pathname, MAX_BUF);
1334 
1335  if ((fp = fopen(pathname, "r")) == NULL) {
1336  /* nothing bad to not having an overlay */
1337  return 0;
1338  }
1339 
1340  if (load_map_header(fp, m)) {
1341  LOG(llevError, "Error loading map header for overlay %s (%s)\n", m->path, pathname);
1342  fclose(fp);
1343  return 1;
1344  }
1345  /*allocate_map(m);*/
1346 
1347  m->in_memory = MAP_LOADING;
1348  load_objects(m, fp, MAP_OVERLAY);
1349  fclose(fp);
1350  m->in_memory = MAP_IN_MEMORY;
1351  return 0;
1352 }
1353 
1354 /******************************************************************************
1355  * This is the start of unique map handling code
1356  *****************************************************************************/
1357 
1365  int i, j, unique = 0;
1366 
1367  for (i = 0; i < MAP_WIDTH(m); i++)
1368  for (j = 0; j < MAP_HEIGHT(m); j++) {
1369  unique = 0;
1370  FOR_MAP_PREPARE(m, i, j, op) {
1372  unique = 1;
1373  if (op->head == NULL && (QUERY_FLAG(op, FLAG_UNIQUE) || unique)) {
1374  clean_object(op);
1375  if (QUERY_FLAG(op, FLAG_IS_LINKED))
1376  remove_button_link(op);
1377  object_remove(op);
1379  }
1380  } FOR_MAP_FINISH();
1381  }
1382 }
1383 
1390  FILE *fp;
1391  int count;
1392  char firstname[MAX_BUF], name[MAX_BUF];
1393 
1394  create_items_path(m->path, name, MAX_BUF);
1395  for (count = 0; count < 10; count++) {
1396  snprintf(firstname, sizeof(firstname), "%s.v%02d", name, count);
1397  if (!access(firstname, R_OK))
1398  break;
1399  }
1400  /* If we get here, we did not find any map */
1401  if (count == 10)
1402  return;
1403 
1404  if ((fp = fopen(firstname, "r")) == NULL) {
1405  /* There is no expectation that every map will have unique items, but this
1406  * is debug output, so leave it in.
1407  */
1408  LOG(llevDebug, "Can't open unique items file for %s\n", name);
1409  return;
1410  }
1411 
1412  m->in_memory = MAP_LOADING;
1413  if (m->tmpname == NULL) /* if we have loaded unique items from */
1414  delete_unique_items(m); /* original map before, don't duplicate them */
1415  load_object(fp, NULL, LO_NOREAD, 0);
1416  load_objects(m, fp, 0);
1417  fclose(fp);
1418  m->in_memory = MAP_IN_MEMORY;
1419 }
1420 
1436 int save_map(mapstruct *m, int flag) {
1437 #define TEMP_EXT ".savefile"
1438  FILE *fp, *fp2;
1439  OutputFile of, of2;
1440  char filename[MAX_BUF], buf[MAX_BUF], shop[MAX_BUF], final[MAX_BUF];
1441  int i, res;
1442 
1443  if (flag && !*m->path) {
1444  LOG(llevError, "Tried to save map without path.\n");
1445  return SAVE_ERROR_NO_PATH;
1446  }
1447 
1448  if (flag != SAVE_MODE_NORMAL || (m->unique) || (m->is_template)) {
1449  if (!m->unique && !m->is_template) { /* flag is set */
1450  if (flag == SAVE_MODE_OVERLAY)
1451  create_overlay_pathname(m->path, filename, MAX_BUF);
1452  else
1453  create_pathname(m->path, filename, MAX_BUF);
1454  } else
1455  snprintf(filename, sizeof(filename), "%s", m->path);
1456 
1457  make_path_to_file(filename);
1458  } else {
1459  if (!m->tmpname)
1460  m->tmpname = tempnam(settings.tmpdir, NULL);
1461  snprintf(filename, sizeof(filename), "%s", m->tmpname);
1462  }
1463  LOG(llevDebug, "Saving map %s\n", m->path);
1464  m->in_memory = MAP_SAVING;
1465 
1466  snprintf(final, sizeof(final), "%s", filename);
1467  snprintf(filename, sizeof(filename), "%s%s", final, TEMP_EXT);
1468  fp = of_open(&of, filename);
1469  if (fp == NULL)
1470  return SAVE_ERROR_RCREATION;
1471 
1472  /* legacy */
1473  fprintf(fp, "arch map\n");
1474  if (m->name)
1475  fprintf(fp, "name %s\n", m->name);
1476  if (!flag)
1477  fprintf(fp, "swap_time %d\n", m->swap_time);
1478  if (m->reset_timeout)
1479  fprintf(fp, "reset_timeout %u\n", m->reset_timeout);
1480  if (m->fixed_resettime)
1481  fprintf(fp, "fixed_resettime %d\n", m->fixed_resettime);
1482  /* we unfortunately have no idea if this is a value the creator set
1483  * or a difficulty value we generated when the map was first loaded
1484  */
1485  if (m->difficulty)
1486  fprintf(fp, "difficulty %d\n", m->difficulty);
1487  if (m->region)
1488  fprintf(fp, "region %s\n", m->region->name);
1489  if (m->shopitems) {
1490  print_shop_string(m, shop, sizeof(shop));
1491  fprintf(fp, "shopitems %s\n", shop);
1492  }
1493  if (m->shopgreed)
1494  fprintf(fp, "shopgreed %f\n", m->shopgreed);
1495  if (m->shopmin)
1496  fprintf(fp, "shopmin %"FMT64U"\n", m->shopmin);
1497  if (m->shopmax)
1498  fprintf(fp, "shopmax %"FMT64U"\n", m->shopmax);
1499  if (m->shoprace)
1500  fprintf(fp, "shoprace %s\n", m->shoprace);
1501  if (m->darkness)
1502  fprintf(fp, "darkness %d\n", m->darkness);
1503  if (m->width)
1504  fprintf(fp, "width %d\n", m->width);
1505  if (m->height)
1506  fprintf(fp, "height %d\n", m->height);
1507  if (m->enter_x)
1508  fprintf(fp, "enter_x %d\n", m->enter_x);
1509  if (m->enter_y)
1510  fprintf(fp, "enter_y %d\n", m->enter_y);
1511  if (m->msg)
1512  fprintf(fp, "msg\n%sendmsg\n", m->msg);
1513  if (m->maplore)
1514  fprintf(fp, "maplore\n%sendmaplore\n", m->maplore);
1515  if (m->unique)
1516  fprintf(fp, "unique %d\n", m->unique);
1517  if (m->is_template)
1518  fprintf(fp, "template %d\n", m->is_template);
1519  if (m->outdoor)
1520  fprintf(fp, "outdoor %d\n", m->outdoor);
1521  if (m->nosmooth)
1522  fprintf(fp, "nosmooth %d\n", m->nosmooth);
1523  if (m->last_reset_time)
1524  fprintf(fp, "first_load %ld\n", m->last_reset_time);
1525  if (m->background_music)
1526  fprintf(fp, "background_music %s\n", m->background_music);
1527 
1528  /* Save any tiling information, except on overlays */
1529  if (flag != SAVE_MODE_OVERLAY)
1530  for (i = 0; i < 4; i++)
1531  if (m->tile_path[i])
1532  fprintf(fp, "tile_path_%d %s\n", i+1, m->tile_path[i]);
1533 
1534  fprintf(fp, "end\n");
1535 
1536  /* In the game save unique items in the different file, but
1537  * in the editor save them to the normal map file.
1538  * If unique map, save files in the proper destination (set by
1539  * player)
1540  */
1541  if ((flag == SAVE_MODE_NORMAL || flag == SAVE_MODE_OVERLAY) && !m->unique && !m->is_template) {
1542  char name[MAX_BUF], final_unique[MAX_BUF];
1543 
1544  create_items_path(m->path, name, MAX_BUF);
1545  snprintf(final_unique, sizeof(final_unique), "%s.v00", name);
1546  snprintf(buf, sizeof(buf), "%s%s", final_unique, TEMP_EXT);
1547  fp2 = of_open(&of2, buf);
1548  if (fp2 == NULL) {
1549  of_cancel(&of);
1550  return SAVE_ERROR_UCREATION;
1551  }
1552  if (flag == SAVE_MODE_OVERLAY) {
1553  /* SO_FLAG_NO_REMOVE is non destructive save, so map is still valid. */
1554  res = save_objects(m, fp, fp2, SAVE_FLAG_NO_REMOVE);
1555  if (res < 0) {
1556  LOG(llevError, "Save error during object save: %d\n", res);
1557  of_cancel(&of);
1558  of_cancel(&of2);
1559  return res;
1560  }
1561  m->in_memory = MAP_IN_MEMORY;
1562  } else {
1563  res = save_objects(m, fp, fp2, 0);
1564  if (res < 0) {
1565  LOG(llevError, "Save error during object save: %d\n", res);
1566  of_cancel(&of);
1567  of_cancel(&of2);
1568  return res;
1569  }
1570  free_all_objects(m);
1571  }
1572  if (ftell(fp2) == 0) {
1573  of_cancel(&of2);
1574  unlink(buf);
1575  /* If there are no unique items left on the map, we need to
1576  * unlink the original unique map so that the unique
1577  * items don't show up again.
1578  */
1579  unlink(final_unique);
1580  } else {
1581  if (!of_close(&of2)) {
1582  of_cancel(&of);
1583  return SAVE_ERROR_WRITE;
1584  }
1585  unlink(final_unique); /* failure isn't too bad, maybe the file doesn't exist. */
1586  if (rename(buf, final_unique) == -1) {
1587  LOG(llevError, "Couldn't rename unique file %s to %s\n", buf, final_unique);
1588  of_cancel(&of);
1589  return SAVE_ERROR_URENAME;
1590  }
1591 
1592  if (chmod(final_unique, SAVE_MODE) != 0) {
1593  LOG(llevError, "Could not set permissions on '%s'\n",
1594  final_unique);
1595  }
1596  }
1597  } else { /* save same file when not playing, like in editor */
1598  res = save_objects(m, fp, fp, 0);
1599  if (res < 0) {
1600  LOG(llevError, "Save error during object save: %d\n", res);
1601  of_cancel(&of);
1602  return res;
1603  }
1604  free_all_objects(m);
1605  }
1606 
1607  if (!of_close(&of))
1608  return SAVE_ERROR_CLOSE;
1609  unlink(final); /* failure isn't too bad, maybe the file doesn't exist. */
1610  if (rename(filename, final) == -1) {
1611  LOG(llevError, "Couldn't rename regular file %s to %s\n", filename, final);
1612  return SAVE_ERROR_RRENAME;
1613  }
1614 
1615  if (chmod(final, SAVE_MODE) != 0) {
1616  LOG(llevError, "Could not set permissions on '%s'\n", final);
1617  }
1618 
1619  return SAVE_ERROR_OK;
1620 }
1621 
1631 void clean_object(object *op) {
1632  FOR_INV_PREPARE(op, tmp) {
1633  clean_object(tmp);
1634  if (QUERY_FLAG(tmp, FLAG_IS_LINKED))
1635  remove_button_link(tmp);
1636  object_remove(tmp);
1638  } FOR_INV_FINISH();
1639 }
1640 
1647 static void free_all_objects(mapstruct *m) {
1648  int i, j;
1649  object *op;
1650 
1651  for (i = 0; i < MAP_WIDTH(m); i++)
1652  for (j = 0; j < MAP_HEIGHT(m); j++) {
1653  object *previous_obj = NULL;
1654 
1655  while ((op = GET_MAP_OB(m, i, j)) != NULL) {
1656  if (op == previous_obj) {
1657  LOG(llevDebug, "free_all_objects: Link error, bailing out.\n");
1658  break;
1659  }
1660  previous_obj = op;
1661  op = HEAD(op);
1662 
1663  /* If the map isn't in memory, object_free_drop_inventory() will remove and
1664  * free objects in op's inventory. So let it do the job.
1665  */
1666  if (m->in_memory == MAP_IN_MEMORY)
1667  clean_object(op);
1668  object_remove(op);
1670  }
1671  }
1672 #ifdef MANY_CORES
1673  /* I see periodic cores on metalforge where a map has been swapped out, but apparantly
1674  * an item on that map was not saved - look for that condition and die as appropriate -
1675  * this leaves more of the map data intact for better debugging.
1676  */
1677  for (op = objects; op != NULL; op = op->next) {
1678  if (!QUERY_FLAG(op, FLAG_REMOVED) && op->map == m) {
1679  LOG(llevDebug, "free_all_objects: object %s still on map after it should have been freed\n", op->name);
1680  abort();
1681  }
1682  }
1683 #endif
1684 }
1685 
1695  int i;
1696 
1697  if (!m->in_memory) {
1698  LOG(llevError, "Trying to free freed map.\n");
1699  return;
1700  }
1701 
1702  /* Handle for plugin map unload event. */
1704 
1705  if (m->spaces)
1706  free_all_objects(m);
1707  if (m->name)
1708  FREE_AND_CLEAR(m->name);
1709  if (m->spaces)
1710  FREE_AND_CLEAR(m->spaces);
1711  if (m->msg)
1712  FREE_AND_CLEAR(m->msg);
1713  if (m->maplore)
1714  FREE_AND_CLEAR(m->maplore);
1715  if (m->shopitems)
1717  if (m->shoprace)
1719  if (m->background_music)
1721  if (m->buttons)
1723  m->buttons = NULL;
1724  for (i = 0; i < 4; i++) {
1725  if (m->tile_path[i])
1726  FREE_AND_CLEAR(m->tile_path[i]);
1727  m->tile_map[i] = NULL;
1728  }
1729  m->in_memory = MAP_SWAPPED;
1730 }
1731 
1742  mapstruct *tmp, *last;
1743  int i;
1744 
1745  if (!m)
1746  return;
1747  if (m->in_memory == MAP_IN_MEMORY) {
1748  /* change to MAP_SAVING, even though we are not,
1749  * so that object_remove() doesn't do as much work.
1750  */
1751  m->in_memory = MAP_SAVING;
1752  free_map(m);
1753  }
1754  /* move this out of free_map, since tmpname can still be needed if
1755  * the map is swapped out.
1756  */
1757  free(m->tmpname);
1758  m->tmpname = NULL;
1759  last = NULL;
1760  /* We need to look through all the maps and see if any maps
1761  * are pointing at this one for tiling information. Since
1762  * tiling can be assymetric, we just can not look to see which
1763  * maps this map tiles with and clears those.
1764  */
1765  for (tmp = first_map; tmp != NULL; tmp = tmp->next) {
1766  if (tmp->next == m)
1767  last = tmp;
1768 
1769  /* This should hopefully get unrolled on a decent compiler */
1770  for (i = 0; i < 4; i++)
1771  if (tmp->tile_map[i] == m)
1772  tmp->tile_map[i] = NULL;
1773  }
1774 
1775  /* If last is null, then this should be the first map in the list */
1776  if (!last) {
1777  if (m == first_map)
1778  first_map = m->next;
1779  else
1780  /* m->path is a static char, so should hopefully still have
1781  * some useful data in it.
1782  */
1783  LOG(llevError, "delete_map: Unable to find map %s in list\n", m->path);
1784  } else
1785  last->next = m->next;
1786 
1787  free(m);
1788 }
1789 
1803 mapstruct *ready_map_name(const char *name, int flags) {
1804  mapstruct *m;
1805 
1806  if (!name)
1807  return (NULL);
1808 
1809  /* Have we been at this level before? */
1810  m = has_been_loaded(name);
1811 
1812  /* Map is good to go, so just return it */
1813  if (m && (m->in_memory == MAP_LOADING || m->in_memory == MAP_IN_MEMORY)) {
1814  return m;
1815  }
1816 
1817  /* unique maps always get loaded from their original location, and never
1818  * a temp location. Likewise, if map_flush is set, or we have never loaded
1819  * this map, load it now. I removed the reset checking from here -
1820  * it seems the probability of a player trying to enter a map that should
1821  * reset but hasn't yet is quite low, and removing that makes this function
1822  * a bit cleaner (and players probably shouldn't rely on exact timing for
1823  * resets in any case - if they really care, they should use the 'maps command.
1824  */
1825  if ((flags&(MAP_FLUSH|MAP_PLAYER_UNIQUE)) || !m) {
1826  /* first visit or time to reset */
1827  if (m) {
1828  clean_tmp_map(m); /* Doesn't make much difference */
1829  delete_map(m);
1830  }
1831 
1832  m = mapfile_load(name, (flags&MAP_PLAYER_UNIQUE));
1833  if (m == NULL) return NULL;
1834 
1835  /* If a player unique map, no extra unique object file to load.
1836  * if from the editor, likewise.
1837  */
1838  if (!(flags&(MAP_FLUSH|MAP_PLAYER_UNIQUE)))
1840 
1841  if (!(flags&(MAP_FLUSH|MAP_PLAYER_UNIQUE|MAP_OVERLAY))) {
1842  if (load_overlay_map(name, m) != 0) {
1843  delete_map(m);
1844  m = mapfile_load(name, 0);
1845  if (m == NULL) {
1846  /* Really, this map is bad :( */
1847  return NULL;
1848  }
1849  }
1850  }
1851  } else {
1852  /* If in this loop, we found a temporary map, so load it up. */
1853 
1854  if (load_temporary_map(m) != 0) {
1855  /*
1856  * There was a failure loading the temporary map, fall back to original one.
1857  * load_temporary_map() already logged the error.
1858  */
1859  delete_map(m);
1860  m = mapfile_load(name, 0);
1861  if (m == NULL) {
1862  /* Really, this map is bad :( */
1863  return NULL;
1864  }
1865  }
1867 
1868  clean_tmp_map(m);
1869  m->in_memory = MAP_IN_MEMORY;
1870  /* tempnam() on sun systems (probably others) uses malloc
1871  * to allocated space for the string. Free it here.
1872  * In some cases, load_temporary_map above won't find the
1873  * temporary map, and so has reloaded a new map. If that
1874  * is the case, tmpname is now null
1875  */
1876  free(m->tmpname);
1877  m->tmpname = NULL;
1878  /* It's going to be saved anew anyway */
1879  }
1880 
1881  /* Below here is stuff common to both first time loaded maps and
1882  * temp maps.
1883  */
1884 
1885  decay_objects(m); /* start the decay */
1886 
1887  if (m->outdoor)
1888  set_darkness_map(m);
1889 
1890  if (!(flags&(MAP_FLUSH))) {
1891  if (m->last_reset_time == 0) {
1892  m->last_reset_time = seconds();
1893  }
1894  }
1895  return m;
1896 }
1897 
1914  archetype *at;
1915  int x, y;
1916  int diff = 0;
1917  int i;
1918  int64_t exp_pr_sq, total_exp = 0;
1919 
1920  if (MAP_DIFFICULTY(m)) {
1921  return MAP_DIFFICULTY(m);
1922  }
1923 
1924  for (x = 0; x < MAP_WIDTH(m); x++)
1925  for (y = 0; y < MAP_HEIGHT(m); y++)
1926  FOR_MAP_PREPARE(m, x, y, op) {
1927  if (QUERY_FLAG(op, FLAG_MONSTER))
1928  total_exp += op->stats.exp;
1929  if (QUERY_FLAG(op, FLAG_GENERATOR)) {
1930  total_exp += op->stats.exp;
1932  if (at != NULL)
1933  total_exp += at->clone.stats.exp*8;
1934  }
1935  } FOR_MAP_FINISH();
1936 
1937  exp_pr_sq = ((double)1000*total_exp)/(MAP_WIDTH(m)*MAP_HEIGHT(m)+1);
1938  diff = 20;
1939  for (i = 1; i < 20; i++)
1940  if (exp_pr_sq <= level_exp(i, 1.0)) {
1941  diff = i;
1942  break;
1943  }
1944 
1945  return diff;
1946 }
1947 
1955  if (m->tmpname == NULL)
1956  return;
1957  (void)unlink(m->tmpname);
1958 }
1959 
1963 void free_all_maps(void) {
1964  int real_maps = 0;
1965 
1966  while (first_map) {
1967  /* I think some of the callers above before it gets here set this to be
1968  * saving, but we still want to free this data
1969  */
1970  if (first_map->in_memory == MAP_SAVING)
1973  real_maps++;
1974  }
1975  LOG(llevDebug, "free_all_maps: Freed %d maps\n", real_maps);
1976 }
1977 
1995 int change_map_light(mapstruct *m, int change) {
1996  int new_level = m->darkness+change;
1997 
1998  /* Nothing to do */
1999  if (!change
2000  || (new_level <= 0 && m->darkness == 0)
2001  || (new_level >= MAX_DARKNESS && m->darkness >= MAX_DARKNESS)) {
2002  return 0;
2003  }
2004 
2005  /* inform all players on the map */
2006  if (change > 0)
2007  ext_info_map(NDI_BLACK, m, MSG_TYPE_MISC, MSG_SUBTYPE_NONE, "It becomes darker.");
2008  else
2009  ext_info_map(NDI_BLACK, m, MSG_TYPE_MISC, MSG_SUBTYPE_NONE, "It becomes brighter.");
2010 
2011  /* Do extra checking. since m->darkness is a unsigned value,
2012  * we need to be extra careful about negative values.
2013  * In general, the checks below are only needed if change
2014  * is not +/-1
2015  */
2016  if (new_level < 0)
2017  m->darkness = 0;
2018  else if (new_level >= MAX_DARKNESS)
2019  m->darkness = MAX_DARKNESS;
2020  else
2021  m->darkness = new_level;
2022 
2023  /* All clients need to get re-updated for the change */
2024  update_all_map_los(m);
2025  return 1;
2026 }
2027 
2050 static inline void add_face_layer(int low_layer, int high_layer, object *ob, object *layers[], int honor_visibility) {
2051  int l, l1;
2052  object *tmp;
2053 
2054  for (l = low_layer; l <= high_layer; l++) {
2055  if (!layers[l]) {
2056  /* found an empty spot. now, we want to make sure
2057  * highest visibility at top, etc.
2058  */
2059  layers[l] = ob;
2060  if (!honor_visibility)
2061  return;
2062 
2063  /* This is basically a mini bubble sort. Only swap
2064  * position if the lower face has greater (not equal)
2065  * visibility - map stacking is secondary consideration here.
2066  */
2067  for (l1 = (l-1); l1 >= low_layer; l1--) {
2068  if (layers[l1]->face->visibility > layers[l1+1]->face->visibility) {
2069  tmp = layers[l1+1];
2070  layers[l1+1] = layers[l1];
2071  layers[l1] = tmp;
2072  }
2073  }
2074  /* Nothing more to do - face inserted */
2075  return;
2076  }
2077  }
2078  /* If we get here, all the layers have an object..
2079  */
2080  if (!honor_visibility) {
2081  /* Basically, in this case, it is pure stacking logic, so
2082  * new object goes on the top.
2083  */
2084  for (l = low_layer; l < high_layer; l++)
2085  layers[l] = layers[l+1];
2086  layers[high_layer] = ob;
2087  /* If this object doesn't have higher visibility than
2088  * the lowest object, no reason to go further.
2089  */
2090  } else if (ob->face->visibility >= layers[low_layer]->face->visibility) {
2091  /*
2092  * Start at the top (highest visibility) layer and work down.
2093  * once this face exceed that of the layer, push down those
2094  * other layers, and then replace the layer with our object.
2095  */
2096  for (l = high_layer; l >= low_layer; l--) {
2097  if (ob->face->visibility >= layers[l]->face->visibility) {
2098  for (l1 = low_layer; l1 < l; l1++)
2099  layers[l1] = layers[l1+1];
2100  layers[l] = ob;
2101  break;
2102  }
2103  }
2104  }
2105 }
2106 
2119 void update_position(mapstruct *m, int x, int y) {
2120  object *player = NULL;
2121  uint8_t flags = 0, oldflags, light = 0;
2122  object *layers[MAP_LAYERS];
2123 
2124  MoveType move_block = 0, move_slow = 0, move_on = 0, move_off = 0, move_allow = 0;
2125 
2126  oldflags = GET_MAP_FLAGS(m, x, y);
2127  if (!(oldflags&P_NEED_UPDATE)) {
2128  LOG(llevDebug, "update_position called with P_NEED_UPDATE not set: %s (%d, %d)\n", m->path, x, y);
2129  return;
2130  }
2131 
2132  memset(layers, 0, MAP_LAYERS*sizeof(object *));
2133 
2134  FOR_MAP_PREPARE(m, x, y, tmp) {
2135  /* DMs just don't do anything when hidden, including no light. */
2136  if (QUERY_FLAG(tmp, FLAG_WIZ) && tmp->contr->hidden)
2137  continue;
2138 
2139  if (tmp->type == PLAYER)
2140  player = tmp;
2141 
2142  /* This could be made additive I guess (two lights better than
2143  * one). But if so, it shouldn't be a simple additive - 2
2144  * light bulbs do not illuminate twice as far as once since
2145  * it is a dissipation factor that is squared (or is it cubed?)
2146  */
2147  if (tmp->glow_radius > light)
2148  light = tmp->glow_radius;
2149 
2150  /* if this object is visible and not a blank face,
2151  * update the objects that show how this space
2152  * looks.
2153  */
2154  if (!tmp->invisible && tmp->face != blank_face) {
2155  if (tmp->map_layer) {
2156  add_face_layer(tmp->map_layer, map_layer_info[tmp->map_layer].high_layer,
2157  tmp, layers, map_layer_info[tmp->map_layer].honor_visibility);
2158  } else if (tmp->move_type&MOVE_FLYING) {
2159  add_face_layer(MAP_LAYER_FLY1, MAP_LAYER_FLY2, tmp, layers, 1);
2160  } else if ((tmp->type == PLAYER || QUERY_FLAG(tmp, FLAG_MONSTER))) {
2162  } else if (QUERY_FLAG(tmp, FLAG_IS_FLOOR)) {
2163  layers[MAP_LAYER_FLOOR] = tmp;
2164  /* floors hide everything else */
2165  memset(layers+1, 0, (MAP_LAYERS-1)*sizeof(object *));
2166  /* Check for FLAG_SEE_ANYWHERE is removed - objects
2167  * with that flag should just have a high visibility
2168  * set - we shouldn't need special code here.
2169  */
2170  } else if (QUERY_FLAG(tmp, FLAG_NO_PICK)) {
2172  } else {
2174  }
2175  }
2176  if (tmp == tmp->above) {
2177  LOG(llevError, "Error in structure of map\n");
2178  exit(-1);
2179  }
2180 
2181  move_slow |= tmp->move_slow;
2182  move_block |= tmp->move_block;
2183  move_on |= tmp->move_on;
2184  move_off |= tmp->move_off;
2185  move_allow |= tmp->move_allow;
2186 
2187  if (QUERY_FLAG(tmp, FLAG_ALIVE))
2188  flags |= P_IS_ALIVE;
2189  if (QUERY_FLAG(tmp, FLAG_NO_MAGIC))
2190  flags |= P_NO_MAGIC;
2191  if (QUERY_FLAG(tmp, FLAG_DAMNED))
2192  flags |= P_NO_CLERIC;
2193 
2194  if (QUERY_FLAG(tmp, FLAG_BLOCKSVIEW))
2195  flags |= P_BLOCKSVIEW;
2196  } FOR_MAP_FINISH(); /* for stack of objects */
2197 
2198  if (player)
2199  flags |= P_PLAYER;
2200 
2201  /* we don't want to rely on this function to have accurate flags, but
2202  * since we're already doing the work, we calculate them here.
2203  * if they don't match, logic is broken someplace.
2204  */
2205  if (((oldflags&~(P_NEED_UPDATE|P_NO_ERROR)) != flags)
2206  && (!(oldflags&P_NO_ERROR))) {
2207  LOG(llevDebug, "update_position: updated flags do not match old flags: %s (x=%d,y=%d) %x != %x\n",
2208  m->path, x, y, (oldflags&~P_NEED_UPDATE), flags);
2209  }
2210 
2211  SET_MAP_FLAGS(m, x, y, flags);
2212  SET_MAP_MOVE_BLOCK(m, x, y, move_block&~move_allow);
2213  SET_MAP_MOVE_ON(m, x, y, move_on);
2214  SET_MAP_MOVE_OFF(m, x, y, move_off);
2215  SET_MAP_MOVE_SLOW(m, x, y, move_slow);
2216  SET_MAP_LIGHT(m, x, y, light);
2217 
2218  /* Note that player may be NULL here, which is fine - if no player, need
2219  * to clear any value that may be set.
2220  */
2221  SET_MAP_PLAYER(m, x, y, player);
2222 
2223  /* Note it is intentional we copy everything, including NULL values. */
2224  memcpy(GET_MAP_FACE_OBJS(m, x, y), layers, sizeof(object *)*MAP_LAYERS);
2225 }
2226 
2234  int timeout;
2235 
2236  timeout = MAP_RESET_TIMEOUT(map);
2237  if (timeout <= 0)
2238  timeout = MAP_DEFAULTRESET;
2239  if (timeout >= MAP_MAXRESET)
2240  timeout = MAP_MAXRESET;
2241  MAP_WHEN_RESET(map) = seconds()+timeout;
2242 }
2243 
2261 static mapstruct *load_and_link_tiled_map(mapstruct *orig_map, int tile_num) {
2262  int dest_tile = (tile_num+2)%4;
2263  char path[HUGE_BUF];
2264 
2265  path_combine_and_normalize(orig_map->path, orig_map->tile_path[tile_num], path, sizeof(path));
2266 
2267  orig_map->tile_map[tile_num] = ready_map_name(path, 0);
2268 
2269  /* need to do a strcmp here as the orig_map->path is not a shared string */
2270  if (orig_map->tile_map[tile_num]->tile_path[dest_tile]
2271  && !strcmp(orig_map->tile_map[tile_num]->tile_path[dest_tile], orig_map->path))
2272  orig_map->tile_map[tile_num]->tile_map[dest_tile] = orig_map;
2273 
2274  return orig_map->tile_map[tile_num];
2275 }
2276 
2294 int out_of_map(mapstruct *m, int x, int y) {
2295 
2296  /* Simple case - coordinates are within this local
2297  * map.
2298  */
2299  if (x >= 0 && x < MAP_WIDTH(m) && y >= 0 && y < MAP_HEIGHT(m))
2300  return 0;
2301 
2302  if (x < 0) {
2303  if (!m->tile_path[3])
2304  return 1;
2305  if (!m->tile_map[3] || m->tile_map[3]->in_memory != MAP_IN_MEMORY) {
2307  /* Verify the tile map loaded correctly */
2308  if (!m->tile_map[3])
2309  return 0;
2310  }
2311  return (out_of_map(m->tile_map[3], x+MAP_WIDTH(m->tile_map[3]), y));
2312  }
2313  if (x >= MAP_WIDTH(m)) {
2314  if (!m->tile_path[1])
2315  return 1;
2316  if (!m->tile_map[1] || m->tile_map[1]->in_memory != MAP_IN_MEMORY) {
2318  /* Verify the tile map loaded correctly */
2319  if (!m->tile_map[1])
2320  return 0;
2321  }
2322  return (out_of_map(m->tile_map[1], x-MAP_WIDTH(m), y));
2323  }
2324  if (y < 0) {
2325  if (!m->tile_path[0])
2326  return 1;
2327  if (!m->tile_map[0] || m->tile_map[0]->in_memory != MAP_IN_MEMORY) {
2329  /* Verify the tile map loaded correctly */
2330  if (!m->tile_map[0])
2331  return 0;
2332  }
2333  return (out_of_map(m->tile_map[0], x, y+MAP_HEIGHT(m->tile_map[0])));
2334  }
2335  if (y >= MAP_HEIGHT(m)) {
2336  if (!m->tile_path[2])
2337  return 1;
2338  if (!m->tile_map[2] || m->tile_map[2]->in_memory != MAP_IN_MEMORY) {
2340  /* Verify the tile map loaded correctly */
2341  if (!m->tile_map[2])
2342  return 0;
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  /* Make sure we loaded properly. */
2381  if (!m->tile_map[3])
2382  return NULL;
2383  }
2384  *x += MAP_WIDTH(m->tile_map[3]);
2385  return (get_map_from_coord(m->tile_map[3], x, y));
2386  }
2387  if (*x >= MAP_WIDTH(m)) {
2388  if (!m->tile_path[1])
2389  return NULL;
2390  if (!m->tile_map[1] || m->tile_map[1]->in_memory != MAP_IN_MEMORY){
2392  /* Make sure we loaded properly. */
2393  if (!m->tile_map[1])
2394  return NULL;
2395  }
2396  *x -= MAP_WIDTH(m);
2397  return (get_map_from_coord(m->tile_map[1], x, y));
2398  }
2399  if (*y < 0) {
2400  if (!m->tile_path[0])
2401  return NULL;
2402  if (!m->tile_map[0] || m->tile_map[0]->in_memory != MAP_IN_MEMORY){
2404  /* Make sure we loaded properly. */
2405  if (!m->tile_map[0])
2406  return NULL;
2407  }
2408  *y += MAP_HEIGHT(m->tile_map[0]);
2409  return (get_map_from_coord(m->tile_map[0], x, y));
2410  }
2411  if (*y >= MAP_HEIGHT(m)) {
2412  if (!m->tile_path[2])
2413  return NULL;
2414  if (!m->tile_map[2] || m->tile_map[2]->in_memory != MAP_IN_MEMORY){
2416  /* Make sure we loaded properly. */
2417  if (!m->tile_map[2])
2418  return NULL;
2419  }
2420  *y -= MAP_HEIGHT(m);
2421  return (get_map_from_coord(m->tile_map[2], x, y));
2422  }
2423  return NULL; /* Coordinates aren't valid if we got here */
2424 }
2425 
2439 static int adjacent_map(const mapstruct *map1, const mapstruct *map2, int *dx, int *dy) {
2440  if (!map1 || !map2)
2441  return 0;
2442 
2443  if (map1 == map2) {
2444  *dx = 0;
2445  *dy = 0;
2446  } else if (map1->tile_map[0] == map2) { /* up */
2447  *dx = 0;
2448  *dy = -MAP_HEIGHT(map2);
2449  } else if (map1->tile_map[1] == map2) { /* right */
2450  *dx = MAP_WIDTH(map1);
2451  *dy = 0;
2452  } else if (map1->tile_map[2] == map2) { /* down */
2453  *dx = 0;
2454  *dy = MAP_HEIGHT(map1);
2455  } else if (map1->tile_map[3] == map2) { /* left */
2456  *dx = -MAP_WIDTH(map2);
2457  *dy = 0;
2458  } else if (map1->tile_map[0] && map1->tile_map[0]->tile_map[1] == map2) { /* up right */
2459  *dx = MAP_WIDTH(map1->tile_map[0]);
2460  *dy = -MAP_HEIGHT(map1->tile_map[0]);
2461  } else if (map1->tile_map[0] && map1->tile_map[0]->tile_map[3] == map2) { /* up left */
2462  *dx = -MAP_WIDTH(map2);
2463  *dy = -MAP_HEIGHT(map1->tile_map[0]);
2464  } else if (map1->tile_map[1] && map1->tile_map[1]->tile_map[0] == map2) { /* right up */
2465  *dx = MAP_WIDTH(map1);
2466  *dy = -MAP_HEIGHT(map2);
2467  } else if (map1->tile_map[1] && map1->tile_map[1]->tile_map[2] == map2) { /* right down */
2468  *dx = MAP_WIDTH(map1);
2469  *dy = MAP_HEIGHT(map1->tile_map[1]);
2470  } else if (map1->tile_map[2] && map1->tile_map[2]->tile_map[1] == map2) { /* down right */
2471  *dx = MAP_WIDTH(map1->tile_map[2]);
2472  *dy = MAP_HEIGHT(map1);
2473  } else if (map1->tile_map[2] && map1->tile_map[2]->tile_map[3] == map2) { /* down left */
2474  *dx = -MAP_WIDTH(map2);
2475  *dy = MAP_HEIGHT(map1);
2476  } else if (map1->tile_map[3] && map1->tile_map[3]->tile_map[0] == map2) { /* left up */
2477  *dx = -MAP_WIDTH(map1->tile_map[3]);
2478  *dy = -MAP_HEIGHT(map2);
2479  } else if (map1->tile_map[3] && map1->tile_map[3]->tile_map[2] == map2) { /* left down */
2480  *dx = -MAP_WIDTH(map1->tile_map[3]);
2481  *dy = MAP_HEIGHT(map1->tile_map[3]);
2482  } else { /* not "adjacent" enough */
2483  return 0;
2484  }
2485 
2486  return 1;
2487 }
2488 
2516 int get_rangevector(object *op1, const object *op2, rv_vector *retval, int flags) {
2517  if (!adjacent_map(op1->map, op2->map, &retval->distance_x, &retval->distance_y)) {
2518  /* be conservative and fill in _some_ data */
2519  retval->distance = 100000;
2520  retval->distance_x = 32767;
2521  retval->distance_y = 32767;
2522  retval->direction = 0;
2523  retval->part = NULL;
2524  return 0;
2525  } else {
2526  object *best;
2527 
2528  retval->distance_x += op2->x-op1->x;
2529  retval->distance_y += op2->y-op1->y;
2530 
2531  best = op1;
2532  /* If this is multipart, find the closest part now */
2533  if (!(flags&0x1) && op1->more) {
2534  object *tmp;
2535  int best_distance = retval->distance_x*retval->distance_x+
2536  retval->distance_y*retval->distance_y, tmpi;
2537 
2538  /* we just take the offset of the piece to head to figure
2539  * distance instead of doing all that work above again
2540  * since the distance fields we set above are positive in the
2541  * same axis as is used for multipart objects, the simply arithmetic
2542  * below works.
2543  */
2544  for (tmp = op1->more; tmp != NULL; tmp = tmp->more) {
2545  tmpi = (op1->x-tmp->x+retval->distance_x)*(op1->x-tmp->x+retval->distance_x)+
2546  (op1->y-tmp->y+retval->distance_y)*(op1->y-tmp->y+retval->distance_y);
2547  if (tmpi < best_distance) {
2548  best_distance = tmpi;
2549  best = tmp;
2550  }
2551  }
2552  if (best != op1) {
2553  retval->distance_x += op1->x-best->x;
2554  retval->distance_y += op1->y-best->y;
2555  }
2556  }
2557  retval->part = best;
2558  retval->distance = isqrt(retval->distance_x*retval->distance_x+retval->distance_y*retval->distance_y);
2559  retval->direction = find_dir_2(-retval->distance_x, -retval->distance_y);
2560  return 1;
2561  }
2562 }
2563 
2589 int get_rangevector_from_mapcoord(const mapstruct *m, int x, int y, const object *op2, rv_vector *retval, int flags) {
2590  if (!adjacent_map(m, op2->map, &retval->distance_x, &retval->distance_y)) {
2591  /* be conservative and fill in _some_ data */
2592  retval->distance = 100000;
2593  retval->distance_x = 32767;
2594  retval->distance_y = 32767;
2595  retval->direction = 0;
2596  retval->part = NULL;
2597  return 0;
2598  } else {
2599  retval->distance_x += op2->x-x;
2600  retval->distance_y += op2->y-y;
2601 
2602  retval->part = NULL;
2603  retval->distance = isqrt(retval->distance_x*retval->distance_x+retval->distance_y*retval->distance_y);
2604  retval->direction = find_dir_2(-retval->distance_x, -retval->distance_y);
2605  return 1;
2606  }
2607 }
2608 
2627 int on_same_map(const object *op1, const object *op2) {
2628  int dx, dy;
2629 
2630  return adjacent_map(op1->map, op2->map, &dx, &dy);
2631 }
2632 
2650 object *map_find_by_flag(mapstruct *map, int x, int y, int flag) {
2651  object *tmp;
2652 
2653  for (tmp = GET_MAP_OB(map, x, y); tmp != NULL; tmp = tmp->above) {
2654  object *head;
2655 
2656  head = HEAD(tmp);
2657  if (QUERY_FLAG(head, flag))
2658  return head;
2659  }
2660  return NULL;
2661 }
2662 
2668  char base[HUGE_BUF], path[HUGE_BUF];
2669  int count;
2670 
2671  if (map->unique) {
2672  /* Unique maps have their full path already set. */
2673  unlink(map->path);
2674  return;
2675  }
2676 
2677  create_items_path(map->path, base, sizeof(base));
2678 
2679  for (count = 0; count < 10; count++) {
2680  snprintf(path, sizeof(path), "%s.v%02d", base, count);
2681  unlink(path);
2682  }
2683 }
2684 
2690 const char *map_get_path(const object *item) {
2691  if (item->map != NULL) {
2692  if (strlen(item->map->path) > 0) {
2693  return item->map->path;
2694  }
2695 
2696  return item->map->name ? item->map->name : "(empty path and name)";
2697  }
2698 
2699  if (item->env != NULL)
2700  return map_get_path(item->env);
2701 
2702  return "(no map and no env!)";
2703 }
Error, serious thing.
Definition: logger.h:11
#define FLAG_NO_MAGIC
Spells (some) can&#39;t pass this object.
Definition: define.h:276
char path[HUGE_BUF]
Filename of the map.
Definition: map.h:365
int find_dir_2(int x, int y)
Computes a direction which you should travel to move of x and y.
Definition: object.c:3600
void free_all_maps(void)
Frees all allocated maps.
Definition: map.c:1963
#define MSG_TYPE_MISC
Messages that don&#39;t go elsewhere.
Definition: newclient.h:389
One player.
Definition: player.h:92
double shopgreed
How much our shopkeeper overcharges.
Definition: map.h:358
#define FLAG_DAMNED
The object is very cursed.
Definition: define.h:318
mapstruct * mapfile_load(const char *map, int flags)
Opens the file "filename" and reads information about the map from the given file, and stores it in a newly allocated mapstruct.
Definition: map.c:1235
#define FLAG_IS_FLOOR
Can&#39;t see what&#39;s underneath this object.
Definition: define.h:303
#define FLAG_UNPAID
Object hasn&#39;t been paid for yet.
Definition: define.h:236
MapSpace * spaces
Array of spaces on this map.
Definition: map.h:355
static void allocate_map(mapstruct *m)
This basically allocates the dynamic array of spaces for the map.
Definition: map.c:844
#define MAP_STYLE
Active objects shouldn&#39;t be put on active list.
Definition: map.h:98
#define FLAG_IS_LINKED
The object is linked with other objects.
Definition: define.h:316
MoveType move_type
Type of movement this object uses.
Definition: object.h:424
see doc/Developers/objects
Definition: object.h:108
#define S_IRGRP
Definition: win32.h:88
mapstruct * get_linked_map(void)
Allocates, initialises, and returns a pointer to a mapstruct.
Definition: map.c:793
This is used by get_rangevector to determine where the other creature is.
Definition: map.h:380
int64_t level_exp(int level, double expmul)
Returns how much experience is needed for a player to become the given level.
Definition: living.c:1822
#define P_PLAYER
There is a player on this space.
Definition: map.h:236
#define SAVE_ERROR_OK
No error.
Definition: map.h:143
static mapstruct * load_and_link_tiled_map(mapstruct *orig_map, int tile_num)
This updates the orig_map->tile_map[tile_num] value after loading the map.
Definition: map.c:2261
void of_cancel(OutputFile *of)
Cancels a save process.
Definition: output_file.c:89
#define P_NEED_UPDATE
This space is out of date.
Definition: map.h:239
#define MAP_FLUSH
Always load map from the map directory, and don&#39;t do unique items or the like.
Definition: map.h:96
mapstruct * get_empty_map(int sizex, int sizey)
Creates and returns a map of the specific size.
Definition: map.c:874
#define SET_FLAG(xyz, p)
Definition: define.h:223
int32_t swap_time
When it reaches 0, the map will be swapped out.
Definition: map.h:342
region * get_region_by_name(const char *region_name)
Gets a region by 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)
This is basically the same as get_rangevector() above, but instead of the first parameter being an ob...
Definition: map.c:2589
void make_path_to_file(const char *filename)
Checks if any directories in the given path doesn&#39;t exist, and creates if necessary.
Definition: porting.c:312
object * part
Part we found.
Definition: map.h:385
const char * name_pl
Plural name.
Definition: define.h:92
#define strdup_local
Definition: compat.h:25
static int save_objects(mapstruct *m, FILE *fp, FILE *fp2, int flag)
This saves all the objects on the map in a non destructive fashion.
Definition: map.c:748
uint32_t map_size(mapstruct *m)
Calculate map size without intermediate sign extension.
Definition: map.c:830
#define MAP_LAYER_ITEM1
Items that can be picked up.
Definition: map.h:43
#define P_NO_MAGIC
Spells (some) can&#39;t pass this object.
Definition: map.h:227
uint32_t nosmooth
If set the content of this map has smoothlevel=0 forced.
Definition: map.h:339
static const Map_Layer_Info map_layer_info[MAP_LAYERS]
the ob->map_layer holds the low layer.
Definition: map.c:63
void fatal(enum fatal_error err)
fatal() is meant to be called whenever a fatal signal is intercepted.
Definition: utils.c:596
struct mapdef * tile_map[4]
Adjoining maps.
Definition: map.h:364
char * create_pathname(const char *name, char *buf, size_t size)
Get the full path to a map file.
Definition: map.c:104
uint32_t in_memory
Combination of IN_MEMORY_xxx flags.
Definition: map.h:345
mapstruct * ready_map_name(const char *name, int flags)
Makes sure the given map is loaded and swapped in.
Definition: map.c:1803
unsigned char MoveType
Typdef here to define type large enough to hold bitmask of all movement types.
Definition: define.h:432
#define LO_REPEAT
Definition: loader.h:15
#define MAP_ENTER_X(m)
Default X coordinate for map enter.
Definition: map.h:85
void free_string(sstring str)
This will reduce the refcount, and if it has reached 0, str will be freed.
Definition: shstr.c:280
#define MAP_DEFAULTRESET
Default time to reset.
Definition: config.h:428
signed long object_sum_weight(object *op)
object_sum_weight() is a recursive function which calculates the weight an object is carrying...
Definition: object.c:311
static shopitems * parse_shop_string(const char *input_string, const mapstruct *map)
Takes a string from a map definition and outputs a pointer to the array of shopitems corresponding to...
Definition: map.c:896
#define FMT64U
Definition: compat.h:13
#define HUGE_BUF
Used for messages - some can be quite long.
Definition: define.h:37
void object_fix_multipart(object *tmp)
Ensures specified object has its more parts correctly inserted in map.
Definition: object.c:4564
#define MAP_MAXRESET
MAP_MAXRESET is the maximum time a map can have before being reset.
Definition: config.h:426
mapstruct * get_map_from_coord(mapstruct *m, int16_t *x, int16_t *y)
This is basically the same as out_of_map above(), but instead we return NULL if no map is valid (coor...
Definition: map.c:2366
#define S_IWOTH
Definition: win32.h:76
#define SET_MAP_LIGHT(M, X, Y, L)
Sets map light.
Definition: map.h:167
uint32_t is_template
If set, this is a template map.
Definition: map.h:338
#define MAP_HEIGHT(m)
Map height.
Definition: map.h:80
object clone
An object from which to do object_copy()
Definition: object.h:470
int calculate_difficulty(mapstruct *m)
This routine is supposed to find out the difficulty of the map.
Definition: map.c:1913
#define MAX_DARKNESS
Maximum map darkness, there is no practical reason to exceed this.
Definition: define.h:470
int isqrt(int n)
Compute the square root.
Definition: utils.c:585
struct Map_Layer_Info Map_Layer_Info
Information about a layer.
void decay_objects(mapstruct *m)
Decay and destroy persihable items in a map.
Definition: utils.c:174
int64_t exp
Experience.
Definition: living.h:46
#define MAP_LAYER_FLOOR
Definition: map.h:40
struct obj * above
Pointer to the object stacked above this one.
Definition: object.h:288
const char * name
Name of the item in question, null if it is the default item.
Definition: map.h:306
char * maplore
Map lore information.
Definition: map.h:362
#define OUT_OF_REAL_MAP(M, X, Y)
Checks if a square is out of the map.
Definition: map.h:217
#define SAVE_MODE_NORMAL
No special handling.
Definition: map.h:120
const typedata * get_typedata_by_name(const char *name)
Definition: item.c:354
#define NDI_BLACK
Definition: newclient.h:221
void strip_endline(char *buf)
Removes endline from buffer (modified in place).
Definition: utils.c:340
Global type definitions and header inclusions.
Link an object type with skill needed to identify, and general name.
Definition: define.h:89
void free_map(mapstruct *m)
Frees everything allocated by the given mapstructure.
Definition: map.c:1694
#define safe_strncpy
Definition: compat.h:23
static int load_temporary_map(mapstruct *m)
Loads a map, which has been loaded earlier, from file.
Definition: map.c:1293
int distance_y
Y delta.
Definition: map.h:383
void draw_ext_info(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *message)
Sends message to player(s).
Definition: main.c:310
The archetype structure is a set of rules on how to generate and manipulate objects which point to ar...
Definition: object.h:465
const char * map_get_path(const object *item)
Return the map path on which the specified item is.
Definition: map.c:2690
#define FOR_OB_AND_BELOW_FINISH()
Finishes FOR_OB_AND_BELOW_PREPARE().
Definition: define.h:789
int change_map_light(mapstruct *m, int change)
Used to change map light level (darkness) up or down.
Definition: map.c:1995
#define FLAG_REMOVED
Object is not in any map or invenory.
Definition: define.h:232
#define MAP_WHEN_RESET(m)
This is when the map will reset.
Definition: map.h:62
int typenum
Itemtype number we need to match, -1 if it is the default price.
Definition: map.h:308
#define CALLOC(x, y)
Definition: compat.h:27
#define MAP_LAYER_LIVING2
Definition: map.h:47
#define SAVE_ERROR_WRITE
Write error.
Definition: map.h:146
#define NDI_NAVY
Definition: newclient.h:223
const typedata * get_typedata(int itemtype)
Definition: item.c:334
uint16_t height
Width and height of map.
Definition: map.h:347
#define MOVE_ALL
Mask of all movement types.
Definition: define.h:413
Information about a layer.
Definition: map.c:52
char * name
Name of map as given by its creator.
Definition: map.h:328
#define SAVE_FLAG_SAVE_UNPAID
If set, unpaid items will be saved.
Definition: map.h:110
#define MAP_IN_MEMORY
Map is fully loaded.
Definition: map.h:130
void object_free_drop_inventory(object *ob)
Frees everything allocated by an object, removes it from the list of used objects, and puts it on the list of free objects.
Definition: object.c:1368
#define MAP_LAYER_NO_PICK1
Non pickable ground objects.
Definition: map.h:41
const char * title
Of foo, etc.
Definition: object.h:317
int16_t y
Position in the map for this object.
Definition: object.h:326
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Same as object_insert_in_map() except it handle separate coordinates and do a clean job preparing mul...
Definition: object.c:1921
static int load_map_header(FILE *fp, mapstruct *m)
This loads the header information of the map.
Definition: map.c:1027
char * name
Shortend name of the region as maps refer to it.
Definition: map.h:278
#define FLAG_ALIVE
Object can fight (or be fought)
Definition: define.h:230
#define S_IROTH
Definition: win32.h:85
#define LL_NORMAL
Definition: loader.h:12
object * object_new(void)
Grabs an object from the list of unused objects, makes sure it is initialised, and returns it...
Definition: object.c:1037
#define MAP_OVERLAY
Map to load is an overlay.
Definition: map.h:99
#define FLAG_OBJ_ORIGINAL
NEVER SET THIS.
Definition: define.h:365
#define MAP_SWAPPED
Map spaces have been saved to disk.
Definition: map.h:131
int index
Being the size of the shopitems array.
Definition: map.h:311
object * object_insert_in_ob(object *op, object *where)
This function inserts the object op in the linked list inside the object environment.
Definition: object.c:2690
#define INS_ON_TOP
Always put object on top.
Definition: object.h:571
Defines for loader.l / loader.c.
int blocked_link(object *ob, mapstruct *m, int sx, int sy)
Returns true if the given coordinate is blocked except by the object passed is not blocking...
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
If set, this is a per player unique map.
Definition: map.h:337
const char * tmpdir
Directory to use for temporary files.
Definition: global.h:253
#define LL_MORE
Definition: loader.h:11
uint8_t honor_visibility
If 0 then don&#39;t reorder items, else allow.
Definition: map.c:54
int8_t strength
The degree of specialisation the shop has in this item, as a percentage from -100 to 100...
Definition: map.h:309
struct mapdef * map
Pointer to the map in which this object is present.
Definition: object.h:297
#define SET_MAP_MOVE_SLOW(M, X, Y, C)
Sets the slowing state of a square.
Definition: map.h:199
#define snprintf
Definition: win32.h:46
uint64_t shopmin
Minimum price a shop will trade for.
Definition: map.h:359
int ob_blocked(const object *ob, mapstruct *m, int16_t x, int16_t y)
Returns true if the given object can&#39;t fit in the given spot.
Definition: map.c:489
static void link_multipart_objects(mapstruct *m)
Go through all the objects on the map looking for objects whose arch says they are multipart yet acco...
Definition: map.c:610
#define MAP_TIMEOUT(m)
Definition: map.h:66
void dump_all_maps(void)
Prints out debug-information about all maps.
Definition: map.c:270
char * path_combine_and_normalize(const char *src, const char *dst, char *path, size_t size)
Combines the 2 paths.
Definition: path.c:172
#define MOVE_FLYING
Combo of fly_low and fly_high.
Definition: define.h:410
#define FOR_INV_FINISH()
Finishes FOR_INV_PREPARE().
Definition: define.h:712
char * msg
Message map creator may have left.
Definition: map.h:361
void object_remove_from_active_list(object *op)
This function removes object &#39;op&#39; from the list of active objects.
Definition: object.c:1194
void update_position(mapstruct *m, int x, int y)
This function updates various attributes about a specific space on the map (what it looks like...
Definition: map.c:2119
#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)
Closes an output file.
Definition: output_file.c:61
object * objects
Pointer to the list of used objects.
Definition: object.c:60
void remove_button_link(object *op)
Remove the object from the linked lists of buttons in the map.
Definition: button.c:698
#define FLAG_BLOCKSVIEW
Object blocks view.
Definition: define.h:269
const char * name
The name of the object, obviously...
Definition: object.h:311
long seconds(void)
Return wall clock time in seconds.
Definition: time.c:344
struct obj * env
Pointer to the object which is the environment.
Definition: object.h:293
uint8_t visibility
Definition: face.h:17
int distance_x
X delta.
Definition: map.h:382
int save_object(FILE *fp, object *op, int flag)
Dumps all variables in an object to a file.
Definition: object.c:5186
struct archt * more
Next part of a linked object.
Definition: object.h:469
int execute_global_event(int eventcode,...)
Definition: main.c:369
char * tile_path[4]
Path to adjoining maps.
Definition: map.h:363
#define INS_NO_WALK_ON
Don&#39;t call check_walk_on against the originator.
Definition: object.h:570
#define MAP_LAYER_ITEM3
Definition: map.h:45
uint32_t fixed_resettime
If true, reset time is not affected by players entering/exiting map.
Definition: map.h:335
#define MAP_ENTER_Y(m)
Default Y coordinate for map enter.
Definition: map.h:87
#define OB_TYPE_MOVE_BLOCK(ob1, type)
Basic macro to see if if ob1 can not move onto a space based on the &#39;type&#39; move_block parameter Add c...
Definition: define.h:447
#define FLAG_OVERLAY_FLOOR
Object is an overlay floor.
Definition: define.h:255
#define P_OUT_OF_MAP
This space is outside the map.
Definition: map.h:251
#define SAVE_ERROR_NO_PATH
Map had no path set.
Definition: map.h:147
#define GET_MAP_MOVE_BLOCK(M, X, Y)
Gets the blocking state of a square.
Definition: map.h:192
int nrofallocobjects
How many OBs allocated (free + used)
Definition: object.c:57
uint8_t darkness
Indicates level of darkness of map.
Definition: map.h:346
const char * templatedir
Directory for the template map.
Definition: global.h:252
#define MAP_DIFFICULTY(m)
Definition: map.h:65
#define SAVE_ERROR_URENAME
Couldn&#39;t rename unique temporary file.
Definition: map.h:148
static void delete_unique_items(mapstruct *m)
This goes through map &#39;m&#39; and removes any unique items on the map.
Definition: map.c:1364
#define MAP_SAVING
Map being saved.
Definition: map.h:133
void update_buttons(mapstruct *m)
Updates every button on the map (by calling update_button() for them).
Definition: button.c:232
#define SET_MAP_MOVE_OFF(M, X, Y, C)
Sets the move_off state of a square.
Definition: map.h:209
long last_reset_time
A timestamp of the last original map loading.
Definition: map.h:366
int on_same_map(const object *op1, const object *op2)
Checks whether 2 objects are on the same map or not.
Definition: map.c:2627
#define QUERY_FLAG(xyz, p)
Definition: define.h:225
#define CLEAR_FLAG(xyz, p)
Definition: define.h:224
#define HEAD(op)
Returns the head part of an object.
Definition: object.h:594
#define INS_ABOVE_FLOOR_ONLY
Put object immediatly above the floor.
Definition: object.h:569
#define EVENT_MAPLOAD
A map is loaded.
Definition: plugin.h:99
#define FLAG_WIZ
Object has special privilegies.
Definition: define.h:231
#define SAVE_ERROR_CLOSE
Close error for regular file.
Definition: map.h:149
void dump_map(const mapstruct *m)
Prints out debug-information about a map.
Definition: map.c:247
#define MAX_BUF
Used for all kinds of things.
Definition: define.h:35
#define MSG_TYPE_ATTACK
Attack related messages.
Definition: newclient.h:385
See Door.
Definition: object.h:126
oblinkpt * buttons
Linked list of linked lists of buttons.
Definition: map.h:354
int16_t enter_y
Enter_x and enter_y are default entrance coordinates to use for a map such that when an exit specifie...
Definition: map.h:348
#define GENERATE_TYPE(xyz)
Definition: define.h:452
int16_t x
Definition: object.h:326
#define EVENT_MAPUNLOAD
A map is freed (includes swapping out)
Definition: plugin.h:98
const char *const map_layer_name[MAP_LAYERS]
These correspond to the layer names in map.h - since some of the types can be on multiple layers...
Definition: map.c:46
#define MAP_LAYER_FLY1
Flying objects - creatures, spells.
Definition: map.h:48
Path-related function prototypes.
uint16_t difficulty
What level the player should be to play here.
Definition: map.h:343
signed __int64 int64_t
Definition: win32.h:168
char * tempnam(const char *dir, const char *pfx)
Portable implementation of tempnam(3).
Definition: porting.c:52
static const flag_definition flags[]
Flag mapping.
#define FOR_MAP_FINISH()
Finishes FOR_MAP_PREPARE().
Definition: define.h:765
struct obj * next
Pointer to the next object in the free/used list.
Definition: object.h:277
const char * uniquedir
Directory for the unique items.
Definition: global.h:251
#define MAP_LAYER_NO_PICK2
Non pickable ground objects.
Definition: map.h:42
static int adjacent_map(const mapstruct *map1, const mapstruct *map2, int *dx, int *dy)
Return whether map2 is adjacent to map1.
Definition: map.c:2439
char * tmpname
Name of temporary file.
Definition: map.h:327
#define SAVE_FLAG_NO_REMOVE
If set, objects are not removed while saving.
Definition: map.h:111
int save_map(mapstruct *m, int flag)
Saves a map to file.
Definition: map.c:1436
uint8_t high_layer
Highest layer for this group.
Definition: map.c:53
#define SAVE_ERROR_UCREATION
Couldn&#39;t create the file for unique objects.
Definition: map.h:145
int32_t timeout
Swapout is set to this.
Definition: map.h:341
#define GET_MAP_FLAGS(M, X, Y)
Gets map flags.
Definition: map.h:161
unsigned int uint32_t
Definition: win32.h:162
See Player.
Definition: object.h:107
const char * datadir
Read only data files.
Definition: global.h:244
object * check_inv_recursive(object *op, const object *trig)
Checks object and its inventory for specific item.
Definition: button.c:786
static void free_all_objects(mapstruct *m)
Remove and free all objects in the given map.
Definition: map.c:1647
#define SET_MAP_MOVE_ON(M, X, Y, C)
Sets the move_on state of a square.
Definition: map.h:204
void set_map_reset_time(mapstruct *map)
Updates the map&#39;s timeout.
Definition: map.c:2233
#define SET_MAP_FLAGS(M, X, Y, C)
Sets map flags.
Definition: map.h:163
#define P_NEW_MAP
Coordinates passed result in a new tiled map.
Definition: map.h:252
#define FLAG_GENERATOR
Will generate type ob->stats.food.
Definition: define.h:248
#define MAP_LAYER_LIVING1
Living creatures.
Definition: map.h:46
uint32_t outdoor
True if an outdoor map.
Definition: map.h:340
#define INS_NO_MERGE
Don&#39;t try to merge with other items.
Definition: object.h:568
static void load_unique_objects(mapstruct *m)
Loads unique objects from file(s) into the map which is in memory.
Definition: map.c:1389
This structure contains all information related to one map square.
Definition: map.h:258
const char * name
Object name.
Definition: define.h:91
void update_all_map_los(mapstruct *map)
update all_map_los is like update_all_los() below, but updates everyone on the map, no matter where they are.
Definition: los.c:504
object * map_find_by_flag(mapstruct *map, int x, int y, int flag)
Finds an object in a map tile by flag number.
Definition: map.c:2650
New_Face * blank_face
Following can just as easily be pointers, but it is easier to keep them like this.
Definition: image.c:39
#define MAP_PLAYER_UNIQUE
This map is player-specific.
Definition: map.h:97
const char * localdir
Read/write data files.
Definition: global.h:245
archetype * get_archetype_by_type_subtype(int type, int subtype)
Retrieves an archetype by type and subtype.
Definition: arch.c:136
const char * name_pl
Plural name.
Definition: map.h:307
living stats
Str, Con, Dex, etc.
Definition: object.h:368
int load_object(FILE *fp, object *op, int bufstate, int map_flags)
mapstruct * has_been_loaded(const char *name)
Checks whether map has been loaded.
Definition: map.c:79
struct archt * arch
Pointer to archetype.
Definition: object.h:412
#define AB_NO_PASS
Definition: map.h:235
#define MAP_WIDTH(m)
Map width.
Definition: map.h:78
Only for debugging purposes.
Definition: logger.h:13
int nroffreeobjects
How many OBs allocated and free (free)
Definition: object.c:56
#define unlink(__a)
Definition: win32.h:56
const char * mapdir
Where the map files are.
Definition: global.h:247
uint32_t reset_timeout
How many seconds must elapse before this map should be reset.
Definition: map.h:333
static void print_shop_string(mapstruct *m, char *output_string, int size)
Opposite of parse string(), this puts the string that was originally fed in to the map (or something ...
Definition: map.c:982
uint64_t shopmax
MMaximum price a shop will offer.
Definition: map.h:360
uint16_t width
Definition: map.h:347
unsigned int distance
Distance, in squares.
Definition: map.h:381
uint8_t type
PLAYER, BULLET, etc.
Definition: object.h:338
struct Settings settings
Server settings.
Definition: init.c:40
static void load_objects(mapstruct *m, FILE *fp, int mapflags)
Loads (and parses) the objects into a given map from the specified file pointer.
Definition: map.c:638
FILE * of_open(OutputFile *of, const char *fname)
Opens an output file.
Definition: output_file.c:30
void fix_container(object *container)
When the map is loaded, load_object() does not actually insert objects into inventory, but just links them.
Definition: map.c:544
int direction
General direction to the targer.
Definition: map.h:384
#define SAVE_MODE
If you have defined SAVE_PLAYER, you might want to change this, too.
Definition: config.h:616
Shop-related information for a map.
Definition: map.h:305
#define TEMP_EXT
int out_of_map(mapstruct *m, int x, int y)
this returns TRUE if the coordinates (x,y) are out of map m.
Definition: map.c:2294
Functions for creating text output files.
void delete_map(mapstruct *m)
Frees the map, including the mapstruct.
Definition: map.c:1741
void apply_auto_fix(mapstruct *m)
Go through the entire map (only the first time when an original map is loaded) and performs special a...
Definition: main.c:261
#define MAP_RESET_TIMEOUT(m)
Definition: map.h:64
#define MAP_LOADING
This map is being loaded.
Definition: map.h:132
#define FOR_OB_AND_BELOW_PREPARE(op_)
Constructs a loop iterating over an object and all objects below it in the same pile.
Definition: define.h:785
#define GET_MAP_FACE_OBJS(M, X, Y)
Returns the layers array so update_position can just copy the entire array over.
Definition: map.h:189
static void create_items_path(const char *s, char *buf, size_t size)
This makes absolute path to the itemfile where unique objects will be saved.
Definition: map.c:168
sstring add_string(const char *str)
This will add &#39;str&#39; to the hash table.
Definition: shstr.c:124
#define GET_MAP_OB(M, X, Y)
Gets the bottom object on a map.
Definition: map.h:172
int check_path(const char *name, int prepend_dir)
This function checks if a file with the given path exists.
Definition: map.c:203
int8_t glow_radius
indicates the glow radius of the object
Definition: object.h:364
#define FLAG_MONSTER
Will attack players.
Definition: define.h:245
static void fix_container_multipart(object *container)
Go through all the objects in a container (recursively) looking for objects whose arch says they are ...
Definition: map.c:569
int get_map_flags(mapstruct *oldmap, mapstruct **newmap, int16_t x, int16_t y, int16_t *nx, int16_t *ny)
This rolls up wall, blocks_magic, blocks_view, etc, all into one function that just returns a P_...
Definition: map.c:302
struct obj * inv
Pointer to the first object in the inventory.
Definition: object.h:290
#define NDI_UNIQUE
Print immediately, don&#39;t buffer.
Definition: newclient.h:245
#define OB_MOVE_BLOCK(ob1, ob2)
Basic macro to see if ob2 blocks ob1 from moving onto this space.
Definition: define.h:438
struct obj * head
Points to the main object of a large body.
Definition: object.h:296
#define SAVE_ERROR_RRENAME
Couldn&#39;t rename regular temporary file.
Definition: map.h:150
#define MSG_SUBTYPE_NONE
Definition: newclient.h:398
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
Definition: logger.c:51
uint8_t flags
Flags about this space (see the P_ values above).
Definition: map.h:262
#define SAVE_ERROR_RCREATION
Couldn&#39;t create the regular save file.
Definition: map.h:144
#define LO_NOREAD
Definition: loader.h:18
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Constructs a loop iterating over all objects of a map tile.
Definition: define.h:758
struct shopitem * shopitems
List of item-types the map&#39;s shop will trade in.
Definition: map.h:356
#define S_IRUSR
Definition: win32.h:91
#define S_IWUSR
Definition: win32.h:82
#define MSG_TYPE_ATTACK_NOKEY
Keys are like attacks, so...
Definition: newclient.h:612
struct mapdef * next
Next map, linked list.
Definition: map.h:326
#define P_BLOCKSVIEW
This spot blocks the player&#39;s view.
Definition: map.h:226
#define P_NO_ERROR
Purely temporary - if set, update_position does not complain if the flags are different.
Definition: map.h:240
void set_darkness_map(mapstruct *m)
Set the darkness level for a map, based on the time of the day.
Definition: main.c:348
int number
Type.
Definition: define.h:90
char * shoprace
The preffered race of the local shopkeeper.
Definition: map.h:357
#define MAP_LAYER_FLY2
Arrows, etc.
Definition: map.h:49
void create_overlay_pathname(const char *name, char *buf, size_t size)
Same as create_pathname(), but for the overlay maps.
Definition: map.c:125
This is a game-map.
Definition: map.h:325
char * background_music
Background music to use for this map.
Definition: map.h:367
#define P_IS_ALIVE
Something alive is on this space.
Definition: map.h:237
const New_Face * face
Face with colors.
Definition: object.h:332
#define FREE_AND_CLEAR(xyz)
Free the pointer and then set it to NULL.
Definition: global.h:203
#define FLAG_UNIQUE
Item is really unique (UNIQUE_ITEMS)
Definition: define.h:288
#define FLAG_NO_PICK
Object can&#39;t be picked up.
Definition: define.h:239
#define SET_MAP_MOVE_BLOCK(M, X, Y, C)
Sets the blocking state of a square.
Definition: map.h:194
static int load_overlay_map(const char *filename, mapstruct *m)
Loads an overlay for a map, which has been loaded earlier, from file.
Definition: map.c:1329
int get_rangevector(object *op1, const object *op2, rv_vector *retval, int flags)
From map.c This is used by get_player to determine where the other creature is.
Definition: map.c:2516
void ext_info_map(int color, const mapstruct *map, uint8_t type, uint8_t subtype, const char *str1)
Writes to everyone on the specified map.
Definition: main.c:325
#define S_ISREG(x)
Definition: win32.h:70
void create_template_pathname(const char *name, char *buf, size_t size)
same as create_pathname(), but for the template maps.
Definition: map.c:145
void map_remove_unique_files(const mapstruct *map)
Remove files containing the map&#39;s unique items.
Definition: map.c:2667
struct regiondef * region
What jurisdiction in the game world this map is ruled by points to the struct containing all the prop...
Definition: map.h:329
void clean_tmp_map(mapstruct *m)
Removse the temporary file used by the map.
Definition: map.c:1954
EXTERN mapstruct * first_map
First map.
Definition: global.h:118
struct obj * more
Pointer to the rest of a large body of objects.
Definition: object.h:295
object * arch_to_object(archetype *at)
Creates and returns a new object which is a copy of the given archetype.
Definition: arch.c:571
void clean_object(object *op)
Remove and free all objects in the inventory of the given object.
Definition: map.c:1631
object * object_get_owner(object *op)
Returns the object which this object marks as being the owner.
Definition: object.c:559
#define SAVE_MODE_OVERLAY
Map is persisted as an overlay.
Definition: map.h:122
#define P_NO_CLERIC
No clerical spells cast here.
Definition: map.h:238
#define FOR_INV_PREPARE(op_, it_)
Constructs a loop iterating over the inventory of an object.
Definition: define.h:705
void object_remove(object *op)
This function removes the object op from the linked list of objects which it is currently tied to...
Definition: object.c:1654
static void add_face_layer(int low_layer, int high_layer, object *ob, object *layers[], int honor_visibility)
This function is used for things that can have multiple layers - NO_PICK, ITEM, LIVING, FLYING.
Definition: map.c:2050
#define INS_MAP_LOAD
Disable lots of checkings.
Definition: object.h:573
b.t.
Definition: object.h:169