--- deliantra/server/common/map.C 2006/12/18 04:07:30 1.44 +++ deliantra/server/common/map.C 2006/12/25 11:25:49 1.47 @@ -224,10 +224,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 @@ -241,29 +239,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 (); } /* @@ -296,7 +293,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); @@ -325,7 +322,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. */ @@ -399,7 +396,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) { @@ -415,10 +411,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); @@ -427,21 +423,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; } @@ -488,7 +487,7 @@ 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 (tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = above) { above = tmp->above; @@ -548,7 +547,6 @@ continue; } - switch (i) { case LL_NORMAL: @@ -583,7 +581,7 @@ { 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; @@ -614,16 +612,13 @@ for (j = 0; j < MAP_HEIGHT (m); j++) { unique = 0; - for (op = get_map_ob (m, i, j); op; op = op->above) + for (op = GET_MAP_OB (m, i, j); op; op = op->above) { if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, 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; @@ -632,9 +627,8 @@ save_object (fp2, op, 3); else if (flag == 0 || (flag == 2 && (!QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) && !QUERY_FLAG (op, FLAG_UNPAID)))) save_object (fp, op, 3); - - } /* for this space */ - } /* for this j */ + } + } } maptile::maptile () @@ -694,8 +688,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); @@ -1131,7 +1125,7 @@ { LOG (llevError, "Error loading map header for %s, flags=%d\n", filename, flags); delete_map (m); - return NULL; + return 0; } m->allocate (); @@ -1224,7 +1218,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 ();*/ @@ -1251,7 +1245,7 @@ { 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; @@ -1260,11 +1254,7 @@ if (op->head == NULL && (QUERY_FLAG (op, FLAG_UNIQUE) || unique)) { - clean_object (op); - - if (QUERY_FLAG (op, FLAG_IS_LINKED)) - remove_button_link (op); - + op->destroy_inv (false); op->destroy (); } } @@ -1456,63 +1446,26 @@ 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); - - tmp->destroy (); - } -} - /* * 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 < MAP_WIDTH (m); i++) + for (int j = 0; j < MAP_HEIGHT (m); j++) { - object *previous_obj = NULL; + mapspace &ms = m->at (i, j); - while ((op = GET_MAP_OB (m, i, j)) != NULL) + while (object *op = ms.bottom) { - 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); - + op->destroy_inv (false); op->destroy (); } } @@ -1522,17 +1475,13 @@ * 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 != MAP_IN_MEMORY) + return; - if (!m->in_memory) - { - LOG (llevError, "Trying to free freed map.\n"); - return; - } + m->in_memory = MAP_SAVING; // TODO: use new/delete #define FREE_AND_CLEAR(p) { free (p); p = NULL; } @@ -1559,85 +1508,60 @@ m->buttons = NULL; - for (i = 0; i < 4; i++) + for (int i = 0; i < 4; i++) { if (m->tile_path[i]) FREE_AND_CLEAR (m->tile_path[i]); - m->tile_map[i] = NULL; + + m->tile_map[i] = 0; } m->in_memory = MAP_SWAPPED; #undef FREE_AND_CLEAR - } -/* - * 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; - - /* 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; - } + //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; - /* 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; + for (maptile *m = first_map; m; m = m->next) + if (m->next = this) + { + m->next = next; + break; + } +} - delete m; +//TODO: must go +void +delete_map (maptile *m) +{ + if (m) + m->destroy (); } /* @@ -1654,19 +1578,15 @@ 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 @@ -1678,7 +1598,6 @@ */ if ((flags & (MAP_FLUSH | MAP_PLAYER_UNIQUE)) || !m) { - /* first visit or time to reset */ if (m) { @@ -1783,7 +1702,7 @@ 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 (op = GET_MAP_OB (m, x, y); op != NULL; op = op->above) { if (QUERY_FLAG (op, FLAG_MONSTER)) { @@ -1886,7 +1805,6 @@ return 1; } - /* * This function updates various attributes about a specific space * on the map (what it looks like, whether it blocks magic, @@ -1894,36 +1812,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 = bottom; 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; @@ -1971,45 +1881,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); + 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; } - 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); + + 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 @@ -2073,28 +1971,17 @@ } } } + 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) @@ -2144,7 +2031,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. @@ -2156,40 +2042,43 @@ { 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); - } + load_and_link_tiled_map (m, 3); + return (out_of_map (m->tile_map[3], x + MAP_WIDTH (m->tile_map[3]), y)); } + if (x >= MAP_WIDTH (m)) { 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); - } + load_and_link_tiled_map (m, 1); + return (out_of_map (m->tile_map[1], x - MAP_WIDTH (m), 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); - } + load_and_link_tiled_map (m, 0); + return (out_of_map (m->tile_map[0], x, y + MAP_HEIGHT (m->tile_map[0]))); } + if (y >= MAP_HEIGHT (m)) { 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); - } + load_and_link_tiled_map (m, 2); + return (out_of_map (m->tile_map[2], x, y - MAP_HEIGHT (m))); } @@ -2214,37 +2103,43 @@ 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]); return (get_map_from_coord (m->tile_map[3], x, y)); } + if (*x >= MAP_WIDTH (m)) { 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); 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]); return (get_map_from_coord (m->tile_map[0], x, y)); } + if (*y >= MAP_HEIGHT (m)) { 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);