--- deliantra/server/common/map.C 2006/09/14 22:33:59 1.30 +++ deliantra/server/common/map.C 2006/12/12 21:39:56 1.40 @@ -26,24 +26,20 @@ #include #include -#ifndef WIN32 /* ---win32 exclude header */ -# include -#endif /* win32 */ +#include #include "path.h" -extern int nrofallocobjects, nroffreeobjects; - /* - * Returns the mapstruct which has a name matching the given argument. + * Returns the maptile which has a name matching the given argument. * return NULL if no match is found. */ -mapstruct * +maptile * has_been_loaded (const char *name) { - mapstruct *map; + maptile *map; if (!name || !*name) return 0; @@ -161,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. @@ -196,7 +187,6 @@ mode |= 2; return (mode); -#endif } /* @@ -206,7 +196,7 @@ */ void -dump_map (const mapstruct *m) +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)); @@ -233,7 +223,7 @@ void dump_all_maps (void) { - mapstruct *m; + maptile *m; for (m = first_map; m != NULL; m = m->next) { @@ -252,11 +242,11 @@ * don't expect to insert/remove anything from those spaces. */ int -get_map_flags (mapstruct *oldmap, mapstruct **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; - mapstruct *mp; + maptile *mp; if (out_of_map (oldmap, x, y)) return P_OUT_OF_MAP; @@ -277,7 +267,6 @@ return retval; } - /* * Returns true if the given coordinate is blocked except by the * object passed is not blocking. This is used with @@ -290,9 +279,8 @@ * the coordinates & map passed in should have been updated for tiling * by the caller. */ - int -blocked_link (object *ob, mapstruct *m, int sx, int sy) +blocked_link (object *ob, maptile *m, int sx, int sy) { object *tmp; int mflags, blocked; @@ -414,11 +402,11 @@ */ int -ob_blocked (const object *ob, mapstruct *m, sint16 x, sint16 y) +ob_blocked (const object *ob, maptile *m, sint16 x, sint16 y) { archetype *tmp; int flag; - mapstruct *m1; + maptile *m1; sint16 sx, sy; if (ob == NULL) @@ -493,7 +481,7 @@ */ static void -link_multipart_objects (mapstruct *m) +link_multipart_objects (maptile *m) { int x, y; object *tmp, *op, *last, *above; @@ -540,13 +528,13 @@ * mapflags is the same as we get with load_original_map */ void -load_objects (mapstruct *m, object_thawer & fp, int mapflags) +load_objects (maptile *m, object_thawer & fp, int mapflags) { int i, j; 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))) @@ -586,7 +574,7 @@ if (mapflags & MAP_STYLE) remove_from_active_list (op); - op = get_object (); + op = object::create (); op->map = m; } @@ -606,7 +594,7 @@ } } - free_object (op); + op->destroy (); link_multipart_objects (m); } @@ -616,7 +604,7 @@ * in order to do map tiling properly. */ void -save_objects (mapstruct *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; @@ -649,78 +637,83 @@ } /* 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 mapstruct. + * 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 */ - -mapstruct * +maptile * get_linked_map (void) { - mapstruct *map = new mapstruct; - mapstruct *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; } /* - * Allocates the arrays contained in a mapstruct. + * Allocates the arrays contained in a maptile. * This basically allocates the dynamic array of spaces for the * map. */ - void -allocate_map (mapstruct *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); } /* Create and returns a map of the specific size. Used * in random map code and the editor. */ -mapstruct * +maptile * get_empty_map (int sizex, int sizey) { - mapstruct *m = get_linked_map (); + maptile *m = get_linked_map (); m->width = sizex; m->height = sizey; m->in_memory = MAP_SWAPPED; - allocate_map (m); + m->allocate (); + return m; } @@ -807,7 +800,7 @@ /* opposite of parse string, this puts the string that was originally fed in to * the map (or something equivilent) into output_string. */ static void -print_shop_string (mapstruct *m, char *output_string) +print_shop_string (maptile *m, char *output_string) { int i; char tmp[MAX_BUF]; @@ -851,37 +844,40 @@ */ static int -load_map_header (object_thawer & fp, mapstruct *m) +load_map_header (object_thawer & fp, maptile *m) { char buf[HUGE_BUF], msgbuf[HUGE_BUF], maplorebuf[HUGE_BUF], *key = NULL, *value, *end; 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. */ @@ -914,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; @@ -935,7 +931,7 @@ } 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; @@ -1149,7 +1145,7 @@ /* * Opens the file "filename" and reads information about the map * from the given file, and stores it in a newly allocated - * mapstruct. A pointer to this structure is returned, or NULL on failure. + * maptile. A pointer to this structure is returned, or NULL on failure. * flags correspond to those in map.h. Main ones used are * MAP_PLAYER_UNIQUE, in which case we don't do any name changes, and * MAP_BLOCK, in which case we block on this load. This happens in all @@ -1158,10 +1154,10 @@ * managed map list. */ -mapstruct * +maptile * load_original_map (const char *filename, int flags) { - mapstruct *m; + maptile *m; char pathname[MAX_BUF]; if (flags & MAP_PLAYER_UNIQUE) @@ -1188,7 +1184,7 @@ return NULL; } - allocate_map (m); + m->allocate (); m->in_memory = MAP_LOADING; load_objects (m, thawer, flags & (MAP_BLOCK | MAP_STYLE)); @@ -1207,8 +1203,8 @@ * option if we can't find the original map) */ -static mapstruct * -load_temporary_map (mapstruct *m) +static maptile * +load_temporary_map (maptile *m) { char buf[MAX_BUF]; @@ -1244,7 +1240,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); @@ -1260,8 +1257,8 @@ * option if we can't find the original map) */ -mapstruct * -load_overlay_map (const char *filename, mapstruct *m) +maptile * +load_overlay_map (const char *filename, maptile *m) { char pathname[MAX_BUF]; @@ -1279,7 +1276,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); @@ -1294,7 +1291,7 @@ /* This goes through map 'm' and removed any unique items on the map. */ static void -delete_unique_items (mapstruct *m) +delete_unique_items (maptile *m) { int i, j, unique; object *op, *next; @@ -1303,18 +1300,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 (); } } } @@ -1326,7 +1327,7 @@ * m is the map to load unique items into. */ static void -load_unique_objects (mapstruct *m) +load_unique_objects (maptile *m) { int count; char firstname[MAX_BUF]; @@ -1359,13 +1360,13 @@ * Saves a map to file. If flag is set, it is saved into the same * file it was (originally) loaded from. Otherwise a temporary * filename will be genarated, and the file will be stored there. - * The temporary filename will be stored in the mapstructure. + * The temporary filename will be stored in the maptileure. * 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 (mapstruct *m, int flag) +new_save_map (maptile *m, int flag) { char filename[MAX_BUF], buf[MAX_BUF], shop[MAX_BUF]; int i; @@ -1427,17 +1428,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) @@ -1526,11 +1520,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 (); } } @@ -1539,7 +1534,7 @@ */ void -free_all_objects (mapstruct *m) +free_all_objects (maptile *m) { int i, j; object *op; @@ -1556,7 +1551,9 @@ LOG (llevDebug, "free_all_objects: Link error, bailing out.\n"); break; } + previous_obj = op; + if (op->head != NULL) op = op->head; @@ -1565,19 +1562,19 @@ */ if (m->in_memory == MAP_IN_MEMORY) clean_object (op); - remove_ob (op); - free_object (op); + + op->destroy (); } } } /* - * Frees everything allocated by the given mapstructure. + * Frees everything allocated by the given maptileure. * don't free tmpname - our caller is left to do that */ void -free_map (mapstruct *m, int flag) +free_map (maptile *m, int flag) { int i; @@ -1614,16 +1611,16 @@ } /* - * function: vanish mapstruct - * m : pointer to mapstruct, if NULL no action + * 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. */ void -delete_map (mapstruct *m) +delete_map (maptile *m) { - mapstruct *tmp, *last; + maptile *tmp, *last; int i; if (!m) @@ -1695,10 +1692,10 @@ * Returns a pointer to the given map. */ -mapstruct * +maptile * ready_map_name (const char *name, int flags) { - mapstruct *m; + maptile *m; if (!name) return (NULL); @@ -1810,7 +1807,7 @@ */ int -calculate_difficulty (mapstruct *m) +calculate_difficulty (maptile *m) { object *op; archetype *at; @@ -1862,7 +1859,7 @@ } void -clean_tmp_map (mapstruct *m) +clean_tmp_map (maptile *m) { if (m->tmpname == NULL) return; @@ -1897,7 +1894,7 @@ */ int -change_map_light (mapstruct *m, int change) +change_map_light (maptile *m, int change) { int new_level = m->darkness + change; @@ -1938,7 +1935,7 @@ * through, etc) */ void -update_position (mapstruct *m, int x, int y) +update_position (maptile *m, int x, int y) { object *tmp, *last = NULL; uint8 flags = 0, oldflags, light = 0, anywhere = 0; @@ -2141,7 +2138,7 @@ void -set_map_reset_time (mapstruct *map) +set_map_reset_time (maptile *map) { int timeout; @@ -2160,8 +2157,8 @@ * so that it is easier for calling functions to verify success. */ -static mapstruct * -load_and_link_tiled_map (mapstruct *orig_map, int tile_num) +static maptile * +load_and_link_tiled_map (maptile *orig_map, int tile_num) { int dest_tile = (tile_num + 2) % 4; char *path = path_combine_and_normalize (orig_map->path, orig_map->tile_path[tile_num]); @@ -2186,7 +2183,7 @@ * */ int -out_of_map (mapstruct *m, int x, int y) +out_of_map (maptile *m, int x, int y) { /* If we get passed a null map, this is obviously the @@ -2251,8 +2248,8 @@ * Using this is more efficient of calling out_of_map * and then figuring out what the real map is */ -mapstruct * -get_map_from_coord (mapstruct *m, sint16 * x, sint16 * y) +maptile * +get_map_from_coord (maptile *m, sint16 * x, sint16 * y) { if (*x < 0) @@ -2308,7 +2305,7 @@ * map1 to map2 in dx/dy. */ static int -adjacent_map (const mapstruct *map1, const mapstruct *map2, int *dx, int *dy) +adjacent_map (const maptile *map1, const maptile *map2, int *dx, int *dy) { if (!map1 || !map2) return 0; @@ -2476,7 +2473,7 @@ */ void -get_rangevector_from_mapcoord (const mapstruct *m, int x, int y, const object *op2, rv_vector * retval, int flags) +get_rangevector_from_mapcoord (const maptile *m, int x, int y, const object *op2, rv_vector * retval, int flags) { if (!adjacent_map (m, op2->map, &retval->distance_x, &retval->distance_y)) {