mapinfo: Add function to deduce a given map's tileset from the terrain type info.

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@6767 4a71c877-e1ca-e34f-864e-861f7616d084
master
Per Inge Mathisen 2009-03-02 19:40:31 +00:00
parent a348f2683b
commit a31fe8a6ec
4 changed files with 73 additions and 4 deletions

View File

@ -199,6 +199,7 @@ int main(int argc, char **argv)
MADD("TileTypes {");
MADD(" NumTiles 128"); // ??? FIXME - read from ttypes file
MADD(" Tiles {");
// The first value of 2 is not written into the Deliverance (Warzone binary) format for some reason.
switch (tileset)
{
case TILESET_ARIZONA:

View File

@ -37,6 +37,13 @@ int main(int argc, char **argv)
map = mapLoad(filename);
if (map)
{
char tilesetName[PATH_MAX];
if (map->tileset == TILESET_ARIZONA) { strcpy(tilesetName, "Arizona"); }
else if (map->tileset == TILESET_URBAN) { strcpy(tilesetName, "Urban"); }
else if (map->tileset == TILESET_ROCKIES) { strcpy(tilesetName, "Rockies"); }
else { strcpy(tilesetName, "(unknown)"); }
printf("Loaded map: %s\n", filename);
printf("\tMap version: %d\n", (int)map->version);
printf("\tWidth: %d\n", (int)map->width);
@ -46,6 +53,7 @@ int main(int argc, char **argv)
printf("\tScroll limits: (%d, %d, %d, %d)\n",
(int)map->scrollMinX, (int)map->scrollMinY, (int)map->scrollMaxX, (int)map->scrollMaxY);
printf("\tLevel name: %s\n", map->levelName);
printf("\tTileset: %s\n", tilesetName);
}
mapFree(map);

View File

@ -10,8 +10,11 @@
void mapFree(GAMEMAP *map)
{
free(map->mGateways);
free(map->mMapTiles);
if (map)
{
free(map->mGateways);
free(map->mMapTiles);
}
free(map);
}
@ -20,10 +23,11 @@ GAMEMAP *mapLoad(char *filename)
{
char path[PATH_MAX];
GAMEMAP *map = malloc(sizeof(*map));
uint32_t gwVersion, i, j, gameVersion, gameTime, gameType, featVersion;
uint32_t gwVersion, i, j, gameVersion, gameTime, gameType, featVersion, terrainVersion;
char aFileType[4];
bool littleEndian = true;
PHYSFS_file *fp = NULL;
uint16_t terrainSignature[3];
// this cries out for a class based design
#define readU8(v) ( littleEndian ? PHYSFS_readULE8(fp, v) : PHYSFS_readUBE8(fp, v) )
@ -180,5 +184,53 @@ GAMEMAP *mapLoad(char *filename)
}
PHYSFS_close(fp);
/* === Load terrain data === */
littleEndian = true;
strcpy(path, filename);
strcat(path, "/ttypes.ttp");
fp = PHYSFS_openRead(path);
if (!fp)
{
debug(LOG_ERROR, "Terrain type file %s not found", path);
return NULL;
}
else if (PHYSFS_read(fp, aFileType, 4, 1) != 1
|| aFileType[0] != 't'
|| aFileType[1] != 't'
|| aFileType[2] != 'y'
|| aFileType[3] != 'p'
|| !readU32(&terrainVersion)
|| !readU32(&map->numTerrainTypes))
{
debug(LOG_ERROR, "Bad features header in %s", path);
return NULL;
}
if (!readU16(&terrainSignature[0]) || !readU16(&terrainSignature[1]) || !readU16(&terrainSignature[2]))
{
debug(LOG_ERROR, "Could not read terrain signature from %s", path);
return NULL;
}
if (terrainSignature[0] == 1 && terrainSignature[1] == 0 && terrainSignature[2] == 2)
{
map->tileset = TILESET_ARIZONA;
}
else if (terrainSignature[0] == 2 && terrainSignature[1] == 2 && terrainSignature[2] == 2)
{
map->tileset = TILESET_URBAN;
}
else if (terrainSignature[0] == 0 && terrainSignature[1] == 0 && terrainSignature[2] == 2)
{
map->tileset = TILESET_ROCKIES;
}
else
{
debug(LOG_ERROR, "Unknown terrain signature in %s: %lu %lu %lu", path,
terrainSignature[0], terrainSignature[1], terrainSignature[2]);
return NULL;
}
PHYSFS_close(fp);
return map;
}

View File

@ -27,6 +27,13 @@
#define MAX_LEVEL_SIZE 20
typedef enum _tileset_type
{
TILESET_ARIZONA = 0,
TILESET_URBAN = 1,
TILESET_ROCKIES = 2
} TILESET;
typedef struct _gateway
{
uint8_t x1, y1, x2, y2;
@ -45,12 +52,13 @@ typedef struct _maptile_type
typedef struct _mapfile_type
{
uint32_t height, width, version, numGateways, numFeatures;
uint32_t height, width, version, numGateways, numFeatures, numTerrainTypes;
int32_t scrollMinX;
int32_t scrollMinY;
uint32_t scrollMaxX;
uint32_t scrollMaxY;
char levelName[MAX_LEVEL_SIZE];
TILESET tileset;
// private members - don't touch! :-)
GATEWAY *mGateways;