--- deliantra/server/common/map.C 2006/12/11 14:28:12 1.37 +++ deliantra/server/common/map.C 2006/12/25 15:28:35 1.50 @@ -26,18 +26,14 @@ #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. */ - maptile * has_been_loaded (const char *name) { @@ -45,9 +41,11 @@ if (!name || !*name) return 0; + for (map = first_map; map; map = map->next) if (!strcmp (name, map->path)) break; + return (map); } @@ -159,19 +157,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 +187,6 @@ mode |= 2; return (mode); -#endif } /* @@ -207,7 +199,7 @@ dump_map (const maptile *m) { LOG (llevError, "Map %s status: %d.\n", m->path, m->in_memory); - LOG (llevError, "Size: %dx%d Start: %d,%d\n", MAP_WIDTH (m), MAP_HEIGHT (m), MAP_ENTER_X (m), MAP_ENTER_Y (m)); + LOG (llevError, "Size: %dx%d Start: %d,%d\n", m->width, m->height, m->enter_x, m->enter_y); if (m->msg != NULL) LOG (llevError, "Message:\n%s", m->msg); @@ -233,10 +225,8 @@ { maptile *m; - for (m = first_map; m != NULL; m = m->next) - { - dump_map (m); - } + for (m = first_map; m; m = m->next) + dump_map (m); } /* This rolls up wall, blocks_magic, blocks_view, etc, all into @@ -250,29 +240,28 @@ * don't expect to insert/remove anything from those spaces. */ int -get_map_flags (maptile *oldmap, maptile **newmap, sint16 x, sint16 y, sint16 * nx, sint16 * ny) +get_map_flags (maptile *oldmap, maptile **newmap, sint16 x, sint16 y, sint16 *nx, sint16 *ny) { sint16 newx, newy; int retval = 0; maptile *mp; - if (out_of_map (oldmap, x, y)) - return P_OUT_OF_MAP; newx = x; newy = y; + mp = get_map_from_coord (oldmap, &newx, &newy); + + if (!mp) + return P_OUT_OF_MAP; + if (mp != oldmap) retval |= P_NEW_MAP; - if (newmap) - *newmap = mp; - if (nx) - *nx = newx; - if (ny) - *ny = newy; - retval |= mp->spaces[newx + mp->width * newy].flags; + if (newmap) *newmap = mp; + if (nx) *nx = newx; + if (ny) *ny = newy; - return retval; + return retval | mp->at (newx, newy).flags (); } /* @@ -305,7 +294,7 @@ /* Save some cycles - instead of calling get_map_flags(), just get the value * directly. */ - mflags = m->spaces[sx + m->width * sy].flags; + mflags = m->at (sx, sy).flags (); blocked = GET_MAP_MOVE_BLOCK (m, sx, sy); @@ -334,7 +323,7 @@ * true. If we get through the entire stack, that must mean * ob is blocking it, so return 0. */ - for (tmp = GET_MAP_OB (m, sx, sy); tmp != NULL; tmp = tmp->above) + for (tmp = GET_MAP_OB (m, sx, sy); tmp; tmp = tmp->above) { /* This must be before the checks below. Code for inventory checkers. */ @@ -408,7 +397,6 @@ * code, we need to have actual object to check its move_type * against the move_block values. */ - int ob_blocked (const object *ob, maptile *m, sint16 x, sint16 y) { @@ -424,10 +412,10 @@ return P_OUT_OF_MAP; /* don't have object, so don't know what types would block */ - return (GET_MAP_MOVE_BLOCK (m1, sx, sy)); + return m1->at (sx, sy).move_block; } - for (tmp = ob->arch; tmp != NULL; tmp = tmp->more) + for (tmp = ob->arch; tmp; tmp = tmp->more) { flag = get_map_flags (m, &m1, x + tmp->clone.x, y + tmp->clone.y, &sx, &sy); @@ -436,21 +424,24 @@ if (flag & P_IS_ALIVE) return P_IS_ALIVE; + mapspace &ms = m1->at (sx, sy); + + /* find_first_free_spot() calls this function. However, often * ob doesn't have any move type (when used to place exits) * so the AND operation in OB_TYPE_MOVE_BLOCK doesn't work. */ - if (ob->move_type == 0 && GET_MAP_MOVE_BLOCK (m1, sx, sy) != MOVE_ALL) + if (ob->move_type == 0 && ms.move_block != MOVE_ALL) continue; /* Note it is intentional that we check ob - the movement type of the * head of the object should correspond for the entire object. */ - if (OB_TYPE_MOVE_BLOCK (ob, GET_MAP_MOVE_BLOCK (m1, sx, sy))) - return AB_NO_PASS; - + if (OB_TYPE_MOVE_BLOCK (ob, ms.move_block)) + return P_NO_PASS; } + return 0; } @@ -495,9 +486,9 @@ object *tmp, *op, *last, *above; archetype *at; - for (x = 0; x < MAP_WIDTH (m); x++) - for (y = 0; y < MAP_HEIGHT (m); y++) - for (tmp = get_map_ob (m, x, y); tmp != NULL; tmp = above) + for (x = 0; x < m->width; x++) + for (y = 0; y < m->height; y++) + for (tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = above) { above = tmp->above; @@ -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))) @@ -557,32 +548,31 @@ continue; } - 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; } @@ -592,17 +582,18 @@ { unique = 0; /* check for unique items, or unique squares */ - for (otmp = get_map_ob (m, i, j); otmp; otmp = otmp->above) + for (otmp = GET_MAP_OB (m, i, j); otmp; otmp = otmp->above) { 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); } @@ -618,31 +609,27 @@ object *op; /* first pass - save one-part objects */ - for (i = 0; i < MAP_WIDTH (m); i++) - for (j = 0; j < MAP_HEIGHT (m); j++) + for (i = 0; i < m->width; i++) + for (j = 0; j < m->height; j++) { unique = 0; - for (op = get_map_ob (m, i, j); op; op = op->above) + for (op = m->at (i, j).bot; op; op = op->above) { - if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) + if (op->flag [FLAG_IS_FLOOR] && op->flag [FLAG_UNIQUE]) unique = 1; if (op->type == PLAYER) - { - LOG (llevDebug, "Player on map that is being saved\n"); - continue; - } + continue; if (op->head || op->owner) continue; - if (unique || QUERY_FLAG (op, FLAG_UNIQUE)) + if (unique || op->flag [FLAG_UNIQUE]) save_object (fp2, op, 3); - else if (flag == 0 || (flag == 2 && (!QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) && !QUERY_FLAG (op, FLAG_UNPAID)))) + else if (flag == 0 || (flag == 2 && (!op->flag [FLAG_OBJ_ORIGINAL] && !op->flag [FLAG_UNPAID]))) save_object (fp, op, 3); - - } /* for this space */ - } /* for this j */ + } + } } maptile::maptile () @@ -651,15 +638,15 @@ /* 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; + this->width = 16; + this->height = 16; + this->reset_timeout = 0; + this->timeout = 300; + this->enter_x = 0; + this->enter_y = 0; /*set part to -1 indicating conversion to weather map not yet done */ - MAP_WORLDPARTX (this) = -1; - MAP_WORLDPARTY (this) = -1; + this->worldpartx = -1; + this->worldparty = -1; } /* @@ -702,8 +689,8 @@ free (spaces); } - spaces = (MapSpace *) - calloc (1, width * height * sizeof (MapSpace)); + spaces = (mapspace *) + calloc (1, width * height * sizeof (mapspace)); if (!spaces) fatal (OUT_OF_MEMORY); @@ -739,7 +726,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) @@ -935,7 +922,7 @@ * 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")) { @@ -951,7 +938,7 @@ } } if (maplorepos != 0) - m->maplore = strdup_local (maplorebuf); + m->maplore = strdup (maplorebuf); } else if (!strcmp (key, "end")) { @@ -970,122 +957,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); @@ -1128,25 +1065,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; } @@ -1189,7 +1126,7 @@ { LOG (llevError, "Error loading map header for %s, flags=%d\n", filename, flags); delete_map (m); - return NULL; + return 0; } m->allocate (); @@ -1198,8 +1135,8 @@ load_objects (m, thawer, flags & (MAP_BLOCK | MAP_STYLE)); m->in_memory = MAP_IN_MEMORY; - if (!MAP_DIFFICULTY (m)) - MAP_DIFFICULTY (m) = calculate_difficulty (m); + if (!m->difficulty) + m->difficulty = calculate_difficulty (m); set_map_reset_time (m); m->instantiate (); return (m); @@ -1282,7 +1219,7 @@ LOG (llevError, "Error loading map header for overlay %s (%s)\n", m->path, pathname); delete_map (m); m = load_original_map (m->path, 0); - return NULL; + return 0; } /*m->allocate ();*/ @@ -1304,22 +1241,22 @@ int i, j, unique; object *op, *next; - for (i = 0; i < MAP_WIDTH (m); i++) - for (j = 0; j < MAP_HEIGHT (m); j++) + for (i = 0; i < m->width; i++) + for (j = 0; j < m->height; j++) { unique = 0; - for (op = get_map_ob (m, i, j); op; op = next) + + 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_inv (false); + op->destroy (); } } } @@ -1354,12 +1291,12 @@ m->in_memory = MAP_LOADING; if (m->tmpname == NULL) /* if we have loaded unique items from */ delete_unique_items (m); /* original map before, don't duplicate them */ + load_objects (m, thawer, 0); m->in_memory = MAP_IN_MEMORY; } - /* * Saves a map to file. If flag is set, it is saved into the same * file it was (originally) loaded from. Otherwise a temporary @@ -1368,7 +1305,6 @@ * If the map is unique, we also save to the filename in the map * (this should have been updated when first loaded) */ - int new_save_map (maptile *m, int flag) { @@ -1398,7 +1334,7 @@ else { if (!m->tmpname) - m->tmpname = tempnam_local (settings.tmpdir, NULL); + m->tmpname = tempnam (settings.tmpdir, NULL); strcpy (filename, m->tmpname); } @@ -1432,17 +1368,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) @@ -1517,61 +1446,27 @@ return 0; } - -/* - * Remove and free all objects in the inventory of the given object. - * object.c ? - */ - -void -clean_object (object *op) -{ - object *tmp, *next; - - 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); - } -} - /* * Remove and free all objects in the given map. */ - void free_all_objects (maptile *m) { - int i, j; - object *op; + if (!m->spaces) + return; - for (i = 0; i < MAP_WIDTH (m); i++) - for (j = 0; j < MAP_HEIGHT (m); j++) + for (int i = 0; i < m->width; i++) + for (int j = 0; j < m->height; j++) { - object *previous_obj = NULL; + mapspace &ms = m->at (i, j); - while ((op = GET_MAP_OB (m, i, j)) != NULL) + while (object *op = ms.bot) { - if (op == previous_obj) - { - LOG (llevDebug, "free_all_objects: Link error, bailing out.\n"); - break; - } - previous_obj = op; - if (op->head != NULL) + if (op->head) op = op->head; - /* If the map isn't in memory, free_object will remove and - * free objects in op's inventory. So let it do the job. - */ - if (m->in_memory == MAP_IN_MEMORY) - clean_object (op); - remove_ob (op); - free_object (op); + op->destroy_inv (false); + op->destroy (); } } } @@ -1580,113 +1475,82 @@ * Frees everything allocated by the given maptileure. * don't free tmpname - our caller is left to do that */ - void free_map (maptile *m, int flag) { - int i; + if (!m->in_memory) //TODO: makes no sense to me? + return; + + m->in_memory = MAP_SAVING; + + // TODO: use new/delete - if (!m->in_memory) - { - LOG (llevError, "Trying to free freed map.\n"); - return; - } if (flag && m->spaces) free_all_objects (m); - if (m->name) - FREE_AND_CLEAR (m->name); - if (m->spaces) - FREE_AND_CLEAR (m->spaces); - if (m->msg) - FREE_AND_CLEAR (m->msg); - if (m->maplore) - FREE_AND_CLEAR (m->maplore); - if (m->shopitems) - delete[]m->shopitems; - m->shopitems = 0; - if (m->shoprace) - FREE_AND_CLEAR (m->shoprace); + + free (m->name), m->name = 0; + free (m->spaces), m->spaces = 0; + free (m->msg), m->msg = 0; + free (m->maplore), m->maplore = 0; + free (m->shoprace), m->shoprace = 0; + delete [] m->shopitems, m->shopitems = 0; + if (m->buttons) - free_objectlinkpt (m->buttons); - m->buttons = NULL; - for (i = 0; i < 4; i++) + free_objectlinkpt (m->buttons), m->buttons = 0; + + for (int i = 0; i < 4; i++) { if (m->tile_path[i]) - FREE_AND_CLEAR (m->tile_path[i]); - m->tile_map[i] = NULL; + free (m->tile_path[i]), m->tile_path[i] = 0; + + m->tile_map[i] = 0; } + m->in_memory = MAP_SWAPPED; } -/* - * function: vanish maptile - * m : pointer to maptile, if NULL no action - * this deletes all the data on the map (freeing pointers) - * and then removes this map from the global linked list of maps. - */ +maptile::~maptile () +{ + free_map (this, 1); + free (tmpname); +} void -delete_map (maptile *m) +maptile::do_destroy () { - maptile *tmp, *last; - int i; + attachable::do_destroy (); - if (!m) - return; + free_all_objects (this); - m->clear (); - - if (m->in_memory == MAP_IN_MEMORY) - { - /* change to MAP_SAVING, even though we are not, - * so that remove_ob doesn't do as much work. - */ - m->in_memory = MAP_SAVING; - free_map (m, 1); - } - /* move this out of free_map, since tmpname can still be needed if - * the map is swapped out. - */ - if (m->tmpname) - { - free (m->tmpname); - m->tmpname = NULL; - } - last = NULL; /* We need to look through all the maps and see if any maps * are pointing at this one for tiling information. Since - * tiling can be assymetric, we just can not look to see which + * tiling can be asymetric, we just can not look to see which * maps this map tiles with and clears those. */ - for (tmp = first_map; tmp != NULL; tmp = tmp->next) - { - if (tmp->next == m) - last = tmp; + //TODO: non-euclidean-tiling MUST GO + for (maptile *m = first_map; m; m = m->next) + for (int i = 0; i < 4; i++) + if (m->tile_map[i] == this) + m->tile_map[i] = 0; - /* This should hopefully get unrolled on a decent compiler */ - for (i = 0; i < 4; i++) - if (tmp->tile_map[i] == m) - tmp->tile_map[i] = NULL; - } - - /* If last is null, then this should be the first map in the list */ - if (!last) - { - if (m == first_map) - first_map = m->next; - else - /* m->path is a static char, so should hopefully still have - * some useful data in it. - */ - LOG (llevError, "delete_map: Unable to find map %s in list\n", m->path); - } + if (first_map == this) + first_map = next; else - last->next = m->next; - - delete m; + for (maptile *m = first_map; m; m = m->next) + if (m->next = this) + { + m->next = next; + break; + } } - +//TODO: must go +void +delete_map (maptile *m) +{ + if (m) + m->destroy (); +} /* * Makes sure the given map is loaded and swapped in. @@ -1699,23 +1563,18 @@ * * Returns a pointer to the given map. */ - maptile * ready_map_name (const char *name, int flags) { - maptile *m; - if (!name) - return (NULL); + return 0; /* Have we been at this level before? */ - m = has_been_loaded (name); + maptile *m = has_been_loaded (name); /* Map is good to go, so just return it */ if (m && (m->in_memory == MAP_LOADING || m->in_memory == MAP_IN_MEMORY)) - { - return m; - } + return m; /* unique maps always get loaded from their original location, and never * a temp location. Likewise, if map_flush is set, or we have never loaded @@ -1727,7 +1586,6 @@ */ if ((flags & (MAP_FLUSH | MAP_PLAYER_UNIQUE)) || !m) { - /* first visit or time to reset */ if (m) { @@ -1824,15 +1682,15 @@ double avgexp = 0; sint64 total_exp = 0; - if (MAP_DIFFICULTY (m)) + if (m->difficulty) { - LOG (llevDebug, "Using stored map difficulty: %d\n", MAP_DIFFICULTY (m)); - return MAP_DIFFICULTY (m); + LOG (llevDebug, "Using stored map difficulty: %d\n", m->difficulty); + return m->difficulty; } - for (x = 0; x < MAP_WIDTH (m); x++) - for (y = 0; y < MAP_HEIGHT (m); y++) - for (op = get_map_ob (m, x, y); op != NULL; op = op->above) + for (x = 0; x < m->width; x++) + for (y = 0; y < m->height; y++) + for (op = GET_MAP_OB (m, x, y); op != NULL; op = op->above) { if (QUERY_FLAG (op, FLAG_MONSTER)) { @@ -1935,7 +1793,6 @@ return 1; } - /* * This function updates various attributes about a specific space * on the map (what it looks like, whether it blocks magic, @@ -1943,36 +1800,28 @@ * through, etc) */ void -update_position (maptile *m, int x, int y) +mapspace::update_ () { - object *tmp, *last = NULL; - uint8 flags = 0, oldflags, light = 0, anywhere = 0; + object *tmp, *last = 0; + uint8 flags = 0, light = 0, anywhere = 0; New_Face *top, *floor, *middle; object *top_obj, *floor_obj, *middle_obj; MoveType move_block = 0, move_slow = 0, move_on = 0, move_off = 0, move_allow = 0; - oldflags = GET_MAP_FLAGS (m, x, y); - if (!(oldflags & P_NEED_UPDATE)) - { - LOG (llevDebug, "update_position called with P_NEED_UPDATE not set: %s (%d, %d)\n", m->path, x, y); - return; - } - middle = blank_face; - top = blank_face; - floor = blank_face; + top = blank_face; + floor = blank_face; - middle_obj = NULL; - top_obj = NULL; - floor_obj = NULL; + middle_obj = 0; + top_obj = 0; + floor_obj = 0; - for (tmp = get_map_ob (m, x, y); tmp; last = tmp, tmp = tmp->above) + for (tmp = bot; tmp; last = tmp, tmp = tmp->above) { - /* This could be made additive I guess (two lights better than - * one). But if so, it shouldn't be a simple additive - 2 + * one). But if so, it shouldn't be a simple additive - 2 * light bulbs do not illuminate twice as far as once since - * it is a disapation factor that is squared (or is it cubed?) + * it is a dissapation factor that is cubed. */ if (tmp->glow_radius > light) light = tmp->glow_radius; @@ -2020,45 +1869,33 @@ middle_obj = tmp; } } + if (tmp == tmp->above) { LOG (llevError, "Error in structure of map\n"); exit (-1); } - move_slow |= tmp->move_slow; + move_slow |= tmp->move_slow; move_block |= tmp->move_block; - move_on |= tmp->move_on; - move_off |= tmp->move_off; + move_on |= tmp->move_on; + move_off |= tmp->move_off; move_allow |= tmp->move_allow; - if (QUERY_FLAG (tmp, FLAG_ALIVE)) - flags |= P_IS_ALIVE; - if (QUERY_FLAG (tmp, FLAG_NO_MAGIC)) - flags |= P_NO_MAGIC; - if (QUERY_FLAG (tmp, FLAG_DAMNED)) - flags |= P_NO_CLERIC; - if (tmp->type == SAFE_GROUND) - flags |= P_SAFE; - - if (QUERY_FLAG (tmp, FLAG_BLOCKSVIEW)) - flags |= P_BLOCKSVIEW; - } /* for stack of objects */ - - /* we don't want to rely on this function to have accurate flags, but - * since we're already doing the work, we calculate them here. - * if they don't match, logic is broken someplace. - */ - if (((oldflags & ~(P_NEED_UPDATE | P_NO_ERROR)) != flags) && (!(oldflags & P_NO_ERROR))) - { - LOG (llevDebug, "update_position: updated flags do not match old flags: %s (old=%d,new=%d) %x != %x\n", - m->path, x, y, (oldflags & ~P_NEED_UPDATE), flags); - } - SET_MAP_FLAGS (m, x, y, flags); - SET_MAP_MOVE_BLOCK (m, x, y, move_block & ~move_allow); - SET_MAP_MOVE_ON (m, x, y, move_on); - SET_MAP_MOVE_OFF (m, x, y, move_off); - SET_MAP_MOVE_SLOW (m, x, y, move_slow); + if (QUERY_FLAG (tmp, FLAG_BLOCKSVIEW)) flags |= P_BLOCKSVIEW; + if (QUERY_FLAG (tmp, FLAG_NO_MAGIC)) flags |= P_NO_MAGIC; + if (tmp->type == PLAYER) flags |= P_PLAYER; + if (tmp->type == SAFE_GROUND) flags |= P_SAFE; + if (QUERY_FLAG (tmp, FLAG_ALIVE)) flags |= P_IS_ALIVE; + if (QUERY_FLAG (tmp, FLAG_DAMNED)) flags |= P_NO_CLERIC; + } + + this->light = light; + this->flags_ = flags; + this->move_block = move_block & ~move_allow; + this->move_on = move_on; + this->move_off = move_off; + this->move_slow = move_slow; /* At this point, we have a floor face (if there is a floor), * and the floor is set - we are not going to touch it at @@ -2122,40 +1959,29 @@ } } } + if (middle == floor) middle = blank_face; + if (top == middle) middle = blank_face; - SET_MAP_FACE (m, x, y, top, 0); - if (top != blank_face) - SET_MAP_FACE_OBJ (m, x, y, top_obj, 0); - else - SET_MAP_FACE_OBJ (m, x, y, NULL, 0); - SET_MAP_FACE (m, x, y, middle, 1); - if (middle != blank_face) - SET_MAP_FACE_OBJ (m, x, y, middle_obj, 1); - else - SET_MAP_FACE_OBJ (m, x, y, NULL, 1); - SET_MAP_FACE (m, x, y, floor, 2); - if (floor != blank_face) - SET_MAP_FACE_OBJ (m, x, y, floor_obj, 2); - else - SET_MAP_FACE_OBJ (m, x, y, NULL, 2); - SET_MAP_LIGHT (m, x, y, light); -} + faces [0] = top; faces_obj [0] = top != blank_face ? top_obj : 0; + faces [1] = middle; faces_obj [1] = middle != blank_face ? middle_obj : 0; + faces [2] = floor; faces_obj [2] = floor != blank_face ? floor_obj : 0; +} void set_map_reset_time (maptile *map) { int timeout; - timeout = MAP_RESET_TIMEOUT (map); + timeout = map->reset_timeout; if (timeout <= 0) timeout = MAP_DEFAULTRESET; if (timeout >= MAP_MAXRESET) timeout = MAP_MAXRESET; - MAP_WHEN_RESET (map) = seconds () + timeout; + map->reset_time = time (0) + timeout; } /* this updates the orig_map->tile_map[tile_num] value after loading @@ -2193,7 +2019,6 @@ int out_of_map (maptile *m, int x, int y) { - /* If we get passed a null map, this is obviously the * case. This generally shouldn't happen, but if the * map loads fail below, it could happen. @@ -2205,41 +2030,44 @@ { if (!m->tile_path[3]) return 1; + if (!m->tile_map[3] || m->tile_map[3]->in_memory != MAP_IN_MEMORY) - { - load_and_link_tiled_map (m, 3); - } - return (out_of_map (m->tile_map[3], x + MAP_WIDTH (m->tile_map[3]), y)); + load_and_link_tiled_map (m, 3); + + return (out_of_map (m->tile_map[3], x + m->tile_map[3]->width, y)); } - if (x >= MAP_WIDTH (m)) + + if (x >= m->width) { if (!m->tile_path[1]) return 1; + if (!m->tile_map[1] || m->tile_map[1]->in_memory != MAP_IN_MEMORY) - { - load_and_link_tiled_map (m, 1); - } - return (out_of_map (m->tile_map[1], x - MAP_WIDTH (m), y)); + load_and_link_tiled_map (m, 1); + + return (out_of_map (m->tile_map[1], x - m->width, y)); } + if (y < 0) { if (!m->tile_path[0]) return 1; + if (!m->tile_map[0] || m->tile_map[0]->in_memory != MAP_IN_MEMORY) - { - load_and_link_tiled_map (m, 0); - } - return (out_of_map (m->tile_map[0], x, y + MAP_HEIGHT (m->tile_map[0]))); + load_and_link_tiled_map (m, 0); + + return (out_of_map (m->tile_map[0], x, y + m->tile_map[0]->height)); } - if (y >= MAP_HEIGHT (m)) + + if (y >= m->height) { if (!m->tile_path[2]) return 1; + if (!m->tile_map[2] || m->tile_map[2]->in_memory != MAP_IN_MEMORY) - { - load_and_link_tiled_map (m, 2); - } - return (out_of_map (m->tile_map[2], x, y - MAP_HEIGHT (m))); + load_and_link_tiled_map (m, 2); + + return (out_of_map (m->tile_map[2], x, y - m->height)); } /* Simple case - coordinates are within this local @@ -2263,41 +2091,47 @@ if (*x < 0) { if (!m->tile_path[3]) - return NULL; + return 0; if (!m->tile_map[3] || m->tile_map[3]->in_memory != MAP_IN_MEMORY) load_and_link_tiled_map (m, 3); - *x += MAP_WIDTH (m->tile_map[3]); + *x += m->tile_map[3]->width; return (get_map_from_coord (m->tile_map[3], x, y)); } - if (*x >= MAP_WIDTH (m)) + + if (*x >= m->width) { if (!m->tile_path[1]) - return NULL; + return 0; + if (!m->tile_map[1] || m->tile_map[1]->in_memory != MAP_IN_MEMORY) load_and_link_tiled_map (m, 1); - *x -= MAP_WIDTH (m); + *x -= m->width; return (get_map_from_coord (m->tile_map[1], x, y)); } + if (*y < 0) { if (!m->tile_path[0]) - return NULL; + return 0; + if (!m->tile_map[0] || m->tile_map[0]->in_memory != MAP_IN_MEMORY) load_and_link_tiled_map (m, 0); - *y += MAP_HEIGHT (m->tile_map[0]); + *y += m->tile_map[0]->height; return (get_map_from_coord (m->tile_map[0], x, y)); } - if (*y >= MAP_HEIGHT (m)) + + if (*y >= m->height) { if (!m->tile_path[2]) - return NULL; + return 0; + if (!m->tile_map[2] || m->tile_map[2]->in_memory != MAP_IN_MEMORY) load_and_link_tiled_map (m, 2); - *y -= MAP_HEIGHT (m); + *y -= m->height; return (get_map_from_coord (m->tile_map[2], x, y)); } @@ -2327,63 +2161,63 @@ else if (map1->tile_map[0] == map2) { /* up */ *dx = 0; - *dy = -MAP_HEIGHT (map2); + *dy = -map2->height; } else if (map1->tile_map[1] == map2) { /* right */ - *dx = MAP_WIDTH (map1); + *dx = map1->width; *dy = 0; } else if (map1->tile_map[2] == map2) { /* down */ *dx = 0; - *dy = MAP_HEIGHT (map1); + *dy = map1->height; } else if (map1->tile_map[3] == map2) { /* left */ - *dx = -MAP_WIDTH (map2); + *dx = -map2->width; *dy = 0; } else if (map1->tile_map[0] && map1->tile_map[0]->tile_map[1] == map2) { /* up right */ - *dx = MAP_WIDTH (map1->tile_map[0]); - *dy = -MAP_HEIGHT (map1->tile_map[0]); + *dx = map1->tile_map[0]->width; + *dy = -map1->tile_map[0]->height; } else if (map1->tile_map[0] && map1->tile_map[0]->tile_map[3] == map2) { /* up left */ - *dx = -MAP_WIDTH (map2); - *dy = -MAP_HEIGHT (map1->tile_map[0]); + *dx = -map2->width; + *dy = -map1->tile_map[0]->height; } else if (map1->tile_map[1] && map1->tile_map[1]->tile_map[0] == map2) { /* right up */ - *dx = MAP_WIDTH (map1); - *dy = -MAP_HEIGHT (map2); + *dx = map1->width; + *dy = -map2->height; } else if (map1->tile_map[1] && map1->tile_map[1]->tile_map[2] == map2) { /* right down */ - *dx = MAP_WIDTH (map1); - *dy = MAP_HEIGHT (map1->tile_map[1]); + *dx = map1->width; + *dy = map1->tile_map[1]->height; } else if (map1->tile_map[2] && map1->tile_map[2]->tile_map[1] == map2) { /* down right */ - *dx = MAP_WIDTH (map1->tile_map[2]); - *dy = MAP_HEIGHT (map1); + *dx = map1->tile_map[2]->width; + *dy = map1->height; } else if (map1->tile_map[2] && map1->tile_map[2]->tile_map[3] == map2) { /* down left */ - *dx = -MAP_WIDTH (map2); - *dy = MAP_HEIGHT (map1); + *dx = -map2->width; + *dy = map1->height; } else if (map1->tile_map[3] && map1->tile_map[3]->tile_map[0] == map2) { /* left up */ - *dx = -MAP_WIDTH (map1->tile_map[3]); - *dy = -MAP_HEIGHT (map2); + *dx = -map1->tile_map[3]->width; + *dy = -map2->height; } else if (map1->tile_map[3] && map1->tile_map[3]->tile_map[2] == map2) { /* left down */ - *dx = -MAP_WIDTH (map1->tile_map[3]); - *dy = MAP_HEIGHT (map1->tile_map[3]); + *dx = -map1->tile_map[3]->width; + *dy = map1->tile_map[3]->height; } else