--- deliantra/server/common/map.C 2006/12/25 11:25:49 1.47 +++ deliantra/server/common/map.C 2006/12/27 13:13:46 1.54 @@ -21,7 +21,6 @@ The authors can be reached via e-mail at */ - #include #include @@ -34,18 +33,17 @@ * 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) { - maptile *map; - if (!name || !*name) return 0; - for (map = first_map; map; map = map->next) + + for_all_maps (map) if (!strcmp (name, map->path)) - break; - return (map); + return map; + + return 0; } /* @@ -198,7 +196,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); @@ -222,9 +220,7 @@ void dump_all_maps (void) { - maptile *m; - - for (m = first_map; m; m = m->next) + for_all_maps (m) dump_map (m); } @@ -477,7 +473,6 @@ * they are saved). We do have to look for the old maps that did save * the more sections and not re-add sections for them. */ - static void link_multipart_objects (maptile *m) { @@ -485,8 +480,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; @@ -602,31 +597,29 @@ * in order to do map tiling properly. */ void -save_objects (maptile *m, object_freezer & fp, object_freezer & fp2, int flag) +save_objects (maptile *m, object_freezer &fp, object_freezer &fp2, int flag) { int i, j = 0, unique = 0; 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_UNIQUE] && op->flag [FLAG_IS_FLOOR]) unique = 1; - if (op->type == PLAYER) - continue; - - if (op->head || op->owner) + if (!op->can_map_save ()) continue; - if (unique || QUERY_FLAG (op, FLAG_UNIQUE)) - 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); + if (unique || op->flag [FLAG_UNIQUE]) + save_object (fp2, op, 1); + else if (flag == 0 || (flag == 2 && (!op->flag [FLAG_OBJ_ORIGINAL] && !op->flag [FLAG_UNPAID]))) + save_object (fp, op, 1); } } } @@ -634,18 +627,40 @@ 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; + width = 16; + height = 16; + reset_timeout = 0; + timeout = 300; + enter_x = 0; + enter_y = 0; /*set part to -1 indicating conversion to weather map not yet done */ - MAP_WORLDPARTX (this) = -1; - MAP_WORLDPARTY (this) = -1; + worldpartx = -1; + worldparty = -1; +} + +void +maptile::link () +{ + next = first_map; + first_map = this; +} + +void +maptile::unlink () +{ + if (first_map == this) + first_map = next; + else + for_all_maps (m) + if (m->next = this) + { + m->next = next; + break; + } } /* @@ -656,15 +671,8 @@ maptile * get_linked_map (void) { - maptile *mp, *map = new maptile; - - for (mp = first_map; mp && mp->next; mp = mp->next); - - if (mp == NULL) - first_map = map; - else - mp->next = map; - + maptile *map = new maptile; + map->link (); return map; } @@ -685,14 +693,10 @@ if (spaces) { LOG (llevError, "allocate_map called with already allocated map (%s)\n", path); - free (spaces); + sfree (spaces, size ()); } - spaces = (mapspace *) - calloc (1, width * height * sizeof (mapspace)); - - if (!spaces) - fatal (OUT_OF_MEMORY); + spaces = salloc0 (size ()); } /* Create and returns a map of the specific size. Used @@ -735,6 +739,7 @@ if (p) p++; } + p = shop_string; strip_endline (p); items = new shopitems[number_of_entries + 1]; @@ -745,6 +750,7 @@ LOG (llevError, "parse_shop_string: I seem to have run out of string, that shouldn't happen.\n"); break; } + next_semicolon = strchr (p, ';'); next_colon = strchr (p, ':'); /* if there is a stregth specified, figure out what it is, we'll need it soon. */ @@ -781,12 +787,14 @@ LOG (llevError, "invalid type %s defined in shopitems in string %s\n", p, input_string); } } + items[i].index = number_of_entries; if (next_semicolon) p = ++next_semicolon; else p = NULL; } + free (shop_string); return items; } @@ -805,21 +813,18 @@ if (m->shopitems[i].typenum) { if (m->shopitems[i].strength) - { - sprintf (tmp, "%s:%d;", m->shopitems[i].name, m->shopitems[i].strength); - } + sprintf (tmp, "%s:%d;", m->shopitems[i].name, m->shopitems[i].strength); else sprintf (tmp, "%s;", m->shopitems[i].name); } else { if (m->shopitems[i].strength) - { - sprintf (tmp, "*:%d;", m->shopitems[i].strength); - } + sprintf (tmp, "*:%d;", m->shopitems[i].strength); else sprintf (tmp, "*"); } + strcat (output_string, tmp); } } @@ -1097,7 +1102,6 @@ * MAP_STYLE: style map - don't add active objects, don't add to server * managed map list. */ - maptile * load_original_map (const char *filename, int flags) { @@ -1134,8 +1138,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); @@ -1146,7 +1150,6 @@ * Return the map object we load into (this can change from the passed * option if we can't find the original map) */ - static maptile * load_temporary_map (maptile *m) { @@ -1200,7 +1203,6 @@ * Return the map object we load into (this can change from the passed * option if we can't find the original map) */ - maptile * load_overlay_map (const char *filename, maptile *m) { @@ -1240,8 +1242,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; @@ -1261,7 +1263,6 @@ } } - /* * Loads unique objects from file(s) into the map which is in memory * m is the map to load unique items into. @@ -1290,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 @@ -1304,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) { @@ -1455,12 +1455,12 @@ if (!m->spaces) return; - for (int i = 0; i < MAP_WIDTH (m); i++) - for (int j = 0; j < MAP_HEIGHT (m); j++) + for (int i = 0; i < m->width; i++) + for (int j = 0; j < m->height; j++) { mapspace &ms = m->at (i, j); - while (object *op = ms.bottom) + while (object *op = ms.bot) { if (op->head) op = op->head; @@ -1478,53 +1478,36 @@ void free_map (maptile *m, int flag) { - if (m->in_memory != MAP_IN_MEMORY) + if (!m->in_memory) //TODO: makes no sense to me? 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; + sfree (m->spaces, m->size ()), m->spaces = 0; - if (m->shoprace) - FREE_AND_CLEAR (m->shoprace); + free (m->name), m->name = 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 (int i = 0; i < 4; i++) - { - if (m->tile_path[i]) - FREE_AND_CLEAR (m->tile_path[i]); - - m->tile_map[i] = 0; - } + free (m->tile_path[i]), m->tile_path[i] = 0; m->in_memory = MAP_SWAPPED; - -#undef FREE_AND_CLEAR } maptile::~maptile () { - free_map (this, 1); - free (tmpname); + assert (destroyed ()); } void @@ -1532,7 +1515,10 @@ { attachable::do_destroy (); - free_all_objects (this); + unlink (); + + free_map (this, 1); + free (tmpname), tmpname = 0; /* We need to look through all the maps and see if any maps * are pointing at this one for tiling information. Since @@ -1540,20 +1526,10 @@ * maps this map tiles with and clears those. */ //TODO: non-euclidean-tiling MUST GO - for (maptile *m = first_map; m; m = m->next) + for_all_maps (m) for (int i = 0; i < 4; i++) if (m->tile_map[i] == this) m->tile_map[i] = 0; - - if (first_map == this) - first_map = next; - else - for (maptile *m = first_map; m; m = m->next) - if (m->next = this) - { - m->next = next; - break; - } } //TODO: must go @@ -1611,10 +1587,8 @@ else LOG (llevDebug, "Trying to load map %s.\n", create_pathname (name)); - //eval_pv ("$x = Event::time", 1);//D if (!(m = load_original_map (name, (flags & MAP_PLAYER_UNIQUE)))) return (NULL); - //eval_pv ("warn \"LOAD \", Event::time - $x", 1);//D fix_auto_apply (m); /* Chests which open as default */ @@ -1694,14 +1668,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)) @@ -1760,6 +1734,7 @@ delete_map (first_map); real_maps++; } + LOG (llevDebug, "free_all_maps: Freed %d maps\n", real_maps); } @@ -1828,7 +1803,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 @@ -1988,12 +1963,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 @@ -2046,10 +2021,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; @@ -2057,7 +2032,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) @@ -2068,10 +2043,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; @@ -2079,7 +2054,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 @@ -2107,11 +2082,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; @@ -2119,7 +2094,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)); } @@ -2131,11 +2106,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; @@ -2143,7 +2118,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)); } @@ -2173,63 +2148,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 @@ -2364,3 +2339,13 @@ return adjacent_map (op1->map, op2->map, &dx, &dy); } + +object * +maptile::insert (object *op, int x, int y, object *originator, int flags) +{ + if (!op->flag [FLAG_REMOVED]) + op->remove (); + + return insert_ob_in_map_at (op, this, originator, flags, x, y); +} +