--- deliantra/server/common/map.C 2006/12/20 11:36:38 1.46 +++ deliantra/server/common/map.C 2006/12/25 15:28:35 1.50 @@ -34,7 +34,6 @@ * 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) { @@ -42,9 +41,11 @@ if (!name || !*name) return 0; + for (map = first_map; map; map = map->next) if (!strcmp (name, map->path)) break; + return (map); } @@ -198,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); @@ -411,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); @@ -423,18 +424,21 @@ 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))) + if (OB_TYPE_MOVE_BLOCK (ob, ms.move_block)) return P_NO_PASS; } @@ -482,8 +486,8 @@ object *tmp, *op, *last, *above; archetype *at; - for (x = 0; x < MAP_WIDTH (m); x++) - for (y = 0; y < MAP_HEIGHT (m); y++) + 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; @@ -544,7 +548,6 @@ continue; } - switch (i) { case LL_NORMAL: @@ -606,13 +609,13 @@ 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) @@ -621,9 +624,9 @@ 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); } } @@ -635,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; } /* @@ -1123,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 (); @@ -1132,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); @@ -1216,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 ();*/ @@ -1238,8 +1241,8 @@ 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; @@ -1252,11 +1255,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 (); } } @@ -1292,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 @@ -1306,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) { @@ -1448,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 < 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); - + op->destroy_inv (false); op->destroy (); } } @@ -1514,122 +1475,81 @@ * 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; - 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; } 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); - 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; + free_objectlinkpt (m->buttons), m->buttons = 0; - 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; + free (m->tile_path[i]), m->tile_path[i] = 0; + + 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; - - if (!m) - return; + attachable::do_destroy (); - m->clear (); + free_all_objects (this); - 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 (); } /* @@ -1646,19 +1566,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 @@ -1670,7 +1586,6 @@ */ if ((flags & (MAP_FLUSH | MAP_PLAYER_UNIQUE)) || !m) { - /* first visit or time to reset */ if (m) { @@ -1767,14 +1682,14 @@ 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 (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)) @@ -1901,7 +1816,7 @@ top_obj = 0; floor_obj = 0; - for (tmp = bottom; 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 @@ -2061,12 +1976,12 @@ { 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) = time (0) + timeout; + map->reset_time = time (0) + timeout; } /* this updates the orig_map->tile_map[tile_num] value after loading @@ -2119,10 +2034,10 @@ 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)); + 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; @@ -2130,7 +2045,7 @@ 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)); + return (out_of_map (m->tile_map[1], x - m->width, y)); } if (y < 0) @@ -2141,10 +2056,10 @@ 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]))); + 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; @@ -2152,7 +2067,7 @@ 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))); + return (out_of_map (m->tile_map[2], x, y - m->height)); } /* Simple case - coordinates are within this local @@ -2180,11 +2095,11 @@ 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 0; @@ -2192,7 +2107,7 @@ 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)); } @@ -2204,11 +2119,11 @@ 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 0; @@ -2216,7 +2131,7 @@ 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)); } @@ -2246,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