--- deliantra/server/common/map.C 2006/10/08 16:51:50 1.33 +++ deliantra/server/common/map.C 2006/12/18 04:07:30 1.44 @@ -26,13 +26,10 @@ #include #include -#ifndef WIN32 /* ---win32 exclude header */ -# include -#endif /* win32 */ +#include #include "path.h" - /* * Returns the maptile which has a name matching the given argument. * return NULL if no match is found. @@ -159,19 +156,14 @@ { char buf[MAX_BUF]; -#ifndef WIN32 char *endbuf; struct stat statbuf; int mode = 0; -#endif if (prepend_dir) strcpy (buf, create_pathname (name)); else strcpy (buf, name); -#ifdef WIN32 /* ***win32: check this sucker in windows style. */ - return (_access (buf, 0)); -#else /* old method (strchr(buf, '\0')) seemd very odd to me - * this method should be equivalant and is clearer. @@ -194,7 +186,6 @@ mode |= 2; return (mode); -#endif } /* @@ -542,7 +533,7 @@ int unique; object *op, *prev = NULL, *last_more = NULL, *otmp; - op = get_object (); + op = object::create (); op->map = m; /* To handle buttons correctly */ while ((i = load_object (fp, op, mapflags))) @@ -560,29 +551,29 @@ switch (i) { - case LL_NORMAL: - /* if we are loading an overlay, put the floors on the bottom */ - if ((QUERY_FLAG (op, FLAG_IS_FLOOR) || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR)) && mapflags & MAP_OVERLAY) - insert_ob_in_map (op, m, op, INS_NO_MERGE | INS_NO_WALK_ON | INS_ABOVE_FLOOR_ONLY | INS_MAP_LOAD); - else - insert_ob_in_map (op, m, op, INS_NO_MERGE | INS_NO_WALK_ON | INS_ON_TOP | INS_MAP_LOAD); - - if (op->inv) - sum_weight (op); - - prev = op, last_more = op; - break; - - case LL_MORE: - insert_ob_in_map (op, m, op, INS_NO_MERGE | INS_NO_WALK_ON | INS_ABOVE_FLOOR_ONLY); - op->head = prev, last_more->more = op, last_more = op; - break; + case LL_NORMAL: + /* if we are loading an overlay, put the floors on the bottom */ + if ((QUERY_FLAG (op, FLAG_IS_FLOOR) || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR)) && mapflags & MAP_OVERLAY) + insert_ob_in_map (op, m, op, INS_NO_MERGE | INS_NO_WALK_ON | INS_ABOVE_FLOOR_ONLY | INS_MAP_LOAD); + else + insert_ob_in_map (op, m, op, INS_NO_MERGE | INS_NO_WALK_ON | INS_ON_TOP | INS_MAP_LOAD); + + if (op->inv) + sum_weight (op); + + prev = op, last_more = op; + break; + + case LL_MORE: + insert_ob_in_map (op, m, op, INS_NO_MERGE | INS_NO_WALK_ON | INS_ABOVE_FLOOR_ONLY); + op->head = prev, last_more->more = op, last_more = op; + break; } if (mapflags & MAP_STYLE) remove_from_active_list (op); - op = get_object (); + op = object::create (); op->map = m; } @@ -596,13 +587,14 @@ { if (QUERY_FLAG (otmp, FLAG_UNIQUE) || QUERY_FLAG (otmp, FLAG_OBJ_SAVE_ON_OVL)) unique = 1; + if (!(mapflags & (MAP_OVERLAY | MAP_PLAYER_UNIQUE) || unique)) SET_FLAG (otmp, FLAG_OBJ_ORIGINAL); } } } - free_object (op); + op->destroy (); link_multipart_objects (m); } @@ -645,37 +637,40 @@ } /* for this j */ } +maptile::maptile () +{ + in_memory = MAP_SWAPPED; + /* The maps used to pick up default x and y values from the + * map archetype. Mimic that behaviour. + */ + MAP_WIDTH (this) = 16; + MAP_HEIGHT (this) = 16; + MAP_RESET_TIMEOUT (this) = 0; + MAP_TIMEOUT (this) = 300; + MAP_ENTER_X (this) = 0; + MAP_ENTER_Y (this) = 0; + /*set part to -1 indicating conversion to weather map not yet done */ + MAP_WORLDPARTX (this) = -1; + MAP_WORLDPARTY (this) = -1; +} + /* * Allocates, initialises, and returns a pointer to a maptile. * Modified to no longer take a path option which was not being * used anyways. MSW 2001-07-01 */ - maptile * get_linked_map (void) { - maptile *map = new maptile; - maptile *mp; + maptile *mp, *map = new maptile; + + for (mp = first_map; mp && mp->next; mp = mp->next); - for (mp = first_map; mp != NULL && mp->next != NULL; mp = mp->next); if (mp == NULL) first_map = map; else mp->next = map; - map->in_memory = MAP_SWAPPED; - /* The maps used to pick up default x and y values from the - * map archetype. Mimic that behaviour. - */ - MAP_WIDTH (map) = 16; - MAP_HEIGHT (map) = 16; - MAP_RESET_TIMEOUT (map) = 0; - MAP_TIMEOUT (map) = 300; - MAP_ENTER_X (map) = 0; - MAP_ENTER_Y (map) = 0; - /*set part to -1 indicating conversion to weather map not yet done */ - MAP_WORLDPARTX (map) = -1; - MAP_WORLDPARTY (map) = -1; return map; } @@ -684,24 +679,25 @@ * This basically allocates the dynamic array of spaces for the * map. */ - void -allocate_map (maptile *m) +maptile::allocate () { - m->in_memory = MAP_IN_MEMORY; + in_memory = MAP_IN_MEMORY; + /* Log this condition and free the storage. We could I suppose * realloc, but if the caller is presuming the data will be intact, * that is their poor assumption. */ - if (m->spaces) + if (spaces) { - LOG (llevError, "allocate_map called with already allocated map (%s)\n", m->path); - free (m->spaces); + LOG (llevError, "allocate_map called with already allocated map (%s)\n", path); + free (spaces); } - m->spaces = (MapSpace *) calloc (1, MAP_WIDTH (m) * MAP_HEIGHT (m) * sizeof (MapSpace)); + spaces = (MapSpace *) + calloc (1, width * height * sizeof (MapSpace)); - if (m->spaces == NULL) + if (!spaces) fatal (OUT_OF_MEMORY); } @@ -716,7 +712,8 @@ m->width = sizex; m->height = sizey; m->in_memory = MAP_SWAPPED; - allocate_map (m); + m->allocate (); + return m; } @@ -734,7 +731,7 @@ int i = 0, number_of_entries = 0; const typedata *current_type; - shop_string = strdup_local (input_string); + shop_string = strdup (input_string); p = shop_string; /* first we'll count the entries, we'll need that for allocating the array shortly */ while (p) @@ -853,31 +850,34 @@ int msgpos = 0; int maplorepos = 0; - while (fgets (buf, HUGE_BUF - 1, fp) != NULL) + while (fgets (buf, HUGE_BUF, fp) != NULL) { buf[HUGE_BUF - 1] = 0; key = buf; + while (isspace (*key)) key++; + if (*key == 0) continue; /* empty line */ + value = strchr (key, ' '); + if (!value) { - end = strchr (key, '\n'); - if (end != NULL) - { - *end = 0; - } + if ((end = strchr (key, '\n'))) + *end = 0; } else { *value = 0; value++; end = strchr (value, '\n'); + while (isspace (*value)) { value++; + if (*value == '\0' || value == end) { /* Nothing but spaces. */ @@ -910,7 +910,7 @@ if (!strcmp (key, "msg")) { - while (fgets (buf, HUGE_BUF - 1, fp) != NULL) + while (fgets (buf, HUGE_BUF, fp) != NULL) { if (!strcmp (buf, "endmsg\n")) break; @@ -927,11 +927,11 @@ * when msgpos is zero, so copying it results in crashes */ if (msgpos != 0) - m->msg = strdup_local (msgbuf); + m->msg = strdup (msgbuf); } else if (!strcmp (key, "maplore")) { - while (fgets (buf, HUGE_BUF - 1, fp) != NULL) + while (fgets (buf, HUGE_BUF, fp) != NULL) { if (!strcmp (buf, "endmaplore\n")) break; @@ -943,7 +943,7 @@ } } if (maplorepos != 0) - m->maplore = strdup_local (maplorebuf); + m->maplore = strdup (maplorebuf); } else if (!strcmp (key, "end")) { @@ -962,122 +962,72 @@ else if (!strcmp (key, "name")) { *end = 0; - m->name = strdup_local (value); + m->name = strdup (value); } /* first strcmp value on these are old names supported * for compatibility reasons. The new values (second) are * what really should be used. */ else if (!strcmp (key, "oid")) - { - fp.get (m, atoi (value)); - } + fp.get (m, atoi (value)); else if (!strcmp (key, "attach")) - { - m->attach = value; - } + m->attach = value; else if (!strcmp (key, "hp") || !strcmp (key, "enter_x")) - { - m->enter_x = atoi (value); - } + m->enter_x = atoi (value); else if (!strcmp (key, "sp") || !strcmp (key, "enter_y")) - { - m->enter_y = atoi (value); - } + m->enter_y = atoi (value); else if (!strcmp (key, "x") || !strcmp (key, "width")) - { - m->width = atoi (value); - } + m->width = atoi (value); else if (!strcmp (key, "y") || !strcmp (key, "height")) - { - m->height = atoi (value); - } + m->height = atoi (value); else if (!strcmp (key, "weight") || !strcmp (key, "reset_timeout")) - { - m->reset_timeout = atoi (value); - } + m->reset_timeout = atoi (value); else if (!strcmp (key, "value") || !strcmp (key, "swap_time")) - { - m->timeout = atoi (value); - } + m->timeout = atoi (value); else if (!strcmp (key, "level") || !strcmp (key, "difficulty")) - { - m->difficulty = atoi (value); - } + m->difficulty = clamp (atoi (value), 1, settings.max_level); else if (!strcmp (key, "invisible") || !strcmp (key, "darkness")) - { - m->darkness = atoi (value); - } + m->darkness = atoi (value); else if (!strcmp (key, "stand_still") || !strcmp (key, "fixed_resettime")) - { - m->fixed_resettime = atoi (value); - } + m->fixed_resettime = atoi (value); else if (!strcmp (key, "unique")) - { - m->unique = atoi (value); - } + m->unique = atoi (value); else if (!strcmp (key, "template")) - { - m->templatemap = atoi (value); - } + m->templatemap = atoi (value); else if (!strcmp (key, "region")) - { - m->region = get_region_by_name (value); - } + m->region = get_region_by_name (value); else if (!strcmp (key, "shopitems")) { *end = 0; m->shopitems = parse_shop_string (value); } else if (!strcmp (key, "shopgreed")) - { - m->shopgreed = atof (value); - } + m->shopgreed = atof (value); else if (!strcmp (key, "shopmin")) - { - m->shopmin = atol (value); - } + m->shopmin = atol (value); else if (!strcmp (key, "shopmax")) - { - m->shopmax = atol (value); - } + m->shopmax = atol (value); else if (!strcmp (key, "shoprace")) { *end = 0; - m->shoprace = strdup_local (value); + m->shoprace = strdup (value); } else if (!strcmp (key, "outdoor")) - { - m->outdoor = atoi (value); - } + m->outdoor = atoi (value); else if (!strcmp (key, "temp")) - { - m->temp = atoi (value); - } + m->temp = atoi (value); else if (!strcmp (key, "pressure")) - { - m->pressure = atoi (value); - } + m->pressure = atoi (value); else if (!strcmp (key, "humid")) - { - m->humid = atoi (value); - } + m->humid = atoi (value); else if (!strcmp (key, "windspeed")) - { - m->windspeed = atoi (value); - } + m->windspeed = atoi (value); else if (!strcmp (key, "winddir")) - { - m->winddir = atoi (value); - } + m->winddir = atoi (value); else if (!strcmp (key, "sky")) - { - m->sky = atoi (value); - } + m->sky = atoi (value); else if (!strcmp (key, "nosmooth")) - { - m->nosmooth = atoi (value); - } + m->nosmooth = atoi (value); else if (!strncmp (key, "tile_path_", 10)) { int tile = atoi (key + 10); @@ -1120,25 +1070,25 @@ if (editor) { /* Use the value as in the file. */ - m->tile_path[tile - 1] = strdup_local (value); + m->tile_path[tile - 1] = strdup (value); } else if (path != NULL) { /* Use the normalized value. */ - m->tile_path[tile - 1] = strdup_local (path); + m->tile_path[tile - 1] = strdup (path); } } /* end if tile direction (in)valid */ } else - { - LOG (llevError, "Got unknown value in map header: %s %s\n", key, value); - } + LOG (llevError, "Got unknown value in map header: %s %s\n", key, value); } + if (!key || strcmp (key, "end")) { LOG (llevError, "Got premature eof on map header!\n"); return 1; } + return 0; } @@ -1184,7 +1134,7 @@ return NULL; } - allocate_map (m); + m->allocate (); m->in_memory = MAP_LOADING; load_objects (m, thawer, flags & (MAP_BLOCK | MAP_STYLE)); @@ -1240,7 +1190,8 @@ m = load_original_map (m->path, 0); return NULL; } - allocate_map (m); + + m->allocate (); m->in_memory = MAP_LOADING; load_objects (m, thawer, 0); @@ -1275,7 +1226,7 @@ m = load_original_map (m->path, 0); return NULL; } - /*allocate_map(m); */ + /*m->allocate ();*/ m->in_memory = MAP_LOADING; load_objects (m, thawer, MAP_OVERLAY); @@ -1299,18 +1250,22 @@ for (j = 0; j < MAP_HEIGHT (m); j++) { unique = 0; + for (op = get_map_ob (m, i, j); op; op = next) { next = op->above; + if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) unique = 1; + if (op->head == NULL && (QUERY_FLAG (op, FLAG_UNIQUE) || unique)) { clean_object (op); + if (QUERY_FLAG (op, FLAG_IS_LINKED)) remove_button_link (op); - remove_ob (op); - free_object (op); + + op->destroy (); } } } @@ -1389,7 +1344,7 @@ else { if (!m->tmpname) - m->tmpname = tempnam_local (settings.tmpdir, NULL); + m->tmpname = tempnam (settings.tmpdir, NULL); strcpy (filename, m->tmpname); } @@ -1423,17 +1378,10 @@ } if (m->shopgreed) fprintf (freezer, "shopgreed %f\n", m->shopgreed); -#ifndef WIN32 if (m->shopmin) fprintf (freezer, "shopmin %llu\n", m->shopmin); if (m->shopmax) fprintf (freezer, "shopmax %llu\n", m->shopmax); -#else - if (m->shopmin) - fprintf (freezer, "shopmin %I64u\n", m->shopmin); - if (m->shopmax) - fprintf (freezer, "shopmax %I64u\n", m->shopmax); -#endif if (m->shoprace) fprintf (freezer, "shoprace %s\n", m->shoprace); if (m->darkness) @@ -1522,11 +1470,12 @@ for (tmp = op->inv; tmp; tmp = next) { next = tmp->below; + clean_object (tmp); if (QUERY_FLAG (tmp, FLAG_IS_LINKED)) remove_button_link (tmp); - remove_ob (tmp); - free_object (tmp); + + tmp->destroy (); } } @@ -1552,7 +1501,9 @@ LOG (llevDebug, "free_all_objects: Link error, bailing out.\n"); break; } + previous_obj = op; + if (op->head != NULL) op = op->head; @@ -1561,8 +1512,8 @@ */ if (m->in_memory == MAP_IN_MEMORY) clean_object (op); - remove_ob (op); - free_object (op); + + op->destroy (); } } } @@ -1582,6 +1533,10 @@ LOG (llevError, "Trying to free freed map.\n"); return; } + + // TODO: use new/delete +#define FREE_AND_CLEAR(p) { free (p); p = NULL; } + if (flag && m->spaces) free_all_objects (m); if (m->name) @@ -1592,21 +1547,29 @@ FREE_AND_CLEAR (m->msg); if (m->maplore) FREE_AND_CLEAR (m->maplore); - if (m->shopitems) - delete[]m->shopitems; + + delete [] m->shopitems; m->shopitems = 0; + if (m->shoprace) FREE_AND_CLEAR (m->shoprace); + if (m->buttons) free_objectlinkpt (m->buttons); + m->buttons = NULL; + for (i = 0; i < 4; i++) { if (m->tile_path[i]) FREE_AND_CLEAR (m->tile_path[i]); m->tile_map[i] = NULL; } + m->in_memory = MAP_SWAPPED; + +#undef FREE_AND_CLEAR + } /* @@ -1677,8 +1640,6 @@ delete m; } - - /* * Makes sure the given map is loaded and swapped in. * name is path name of the map. @@ -1690,7 +1651,6 @@ * * Returns a pointer to the given map. */ - maptile * ready_map_name (const char *name, int flags) { @@ -2146,7 +2106,7 @@ timeout = MAP_DEFAULTRESET; if (timeout >= MAP_MAXRESET) timeout = MAP_MAXRESET; - MAP_WHEN_RESET (map) = seconds () + timeout; + MAP_WHEN_RESET (map) = time (0) + timeout; } /* this updates the orig_map->tile_map[tile_num] value after loading