Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Hendricks266's Map Tool
- // by Hendricks266
- // special thanks to Daedolon
- // License: ISC License
- /*
- action commands:
- check - loads and potentially outputs the data
- -d - full dump to separate bin files
- -t - test dump to a full map file
- -h - uses decimal instead of hex
- -l - outputs to a log file
- combine - searches for "check -d" bins and combines to a map file, but does not overwrite
- */
- // needed for precise types like int8_t and uint16_t
- #ifdef _MSC_VER // MSVC sucks.
- # include "stdint.h"
- #else
- # include <stdint.h>
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #ifdef _MSC_VER // MSVC sucks.
- # define MALLOC_STRUCTS
- #endif
- #if defined MALLOC_STRUCTS && !defined __cplusplus
- # error C++ is required for the malloc structs mode.
- #endif
- // borrowed from compat.h
- #if __GNUC__ >= 4
- # define CLAMP_DECL static inline __attribute__((warn_unused_result))
- #else
- # define CLAMP_DECL static inline
- #endif
- // Clamp <in> to [<min>..<max>]. The case in <= min is handled first.
- CLAMP_DECL int32_t clamp(int32_t in, int32_t min, int32_t max)
- {
- return in <= min ? min : (in >= max ? max : in);
- }
- #define BMAX_PATH 256
- #if defined(_MSC_VER)
- # define strcasecmp _stricmp
- # define strncasecmp _strnicmp
- #elseif defined(__QNX__)
- # define strcasecmp stricmp
- # define strncasecmp strnicmp
- #endif
- // borrowed from build.h
- #define MAXSECTORSV8 4096
- #define MAXWALLSV8 16384
- #define MAXSPRITESV8 16384
- #define MAXSECTORSV7 1024
- #define MAXWALLSV7 8192
- #define MAXSPRITESV7 4096
- #define MAXSECTORS MAXSECTORSV8
- #define MAXWALLS MAXWALLSV8
- #define MAXSPRITES MAXSPRITESV8
- typedef struct
- {
- int16_t wallptr, wallnum;
- int32_t ceilingz, floorz;
- uint16_t ceilingstat, floorstat;
- int16_t ceilingpicnum, ceilingheinum;
- int8_t ceilingshade;
- uint8_t ceilingpal, ceilingxpanning, ceilingypanning;
- int16_t floorpicnum, floorheinum;
- int8_t floorshade;
- uint8_t floorpal, floorxpanning, floorypanning;
- uint8_t visibility, filler;
- uint16_t lotag, hitag;
- int16_t extra;
- } sector_t;
- typedef struct
- {
- int32_t x, y;
- int16_t point2, nextwall, nextsector;
- uint16_t cstat;
- int16_t picnum, overpicnum;
- int8_t shade;
- uint8_t pal, xrepeat, yrepeat, xpanning, ypanning;
- uint16_t lotag, hitag;
- int16_t extra;
- } wall_t;
- typedef struct
- {
- int32_t x, y, z;
- uint16_t cstat;
- int16_t picnum;
- int8_t shade;
- uint8_t pal, clipdist, filler;
- uint8_t xrepeat, yrepeat;
- int8_t xoffset, yoffset;
- int16_t sectnum, statnum;
- int16_t ang, owner, xvel, yvel, zvel;
- uint16_t lotag, hitag;
- int16_t extra;
- } sprite_t;
- // brand new
- #ifdef MALLOC_STRUCTS
- struct BUILD_Map
- #else
- typedef struct
- #endif
- {
- /* Map version */
- uint8_t mapVersion[4]; // MAP version
- /* Player data */
- uint8_t playerX[4]; // Player X position
- uint8_t playerY[4]; // Player Y position
- uint8_t playerZ[4]; // Player Z position
- uint8_t playerAng[2]; // Player angle
- uint8_t playerSector[2]; // Player sector
- /* Actual data */
- uint8_t numSectors[2]; // Number of sectors
- #ifndef MALLOC_STRUCTS
- sector_t sectors[MAXSECTORS];
- #endif
- uint8_t numWalls[2]; // Number of walls
- #ifndef MALLOC_STRUCTS
- wall_t walls[MAXWALLS];
- #endif
- uint8_t numSprites[2]; // Number of sprites
- #ifndef MALLOC_STRUCTS
- sprite_t sprites[MAXSPRITES];
- #endif
- #ifdef MALLOC_STRUCTS
- sector_t *getsectors(void) { return sectors; }
- wall_t *getwalls(void) { return walls; }
- sprite_t *getsprites(void) { return sprites; }
- sector_t *setsectors(sector_t *newsectors);
- wall_t *setwalls(wall_t *newwalls);
- sprite_t *setsprites(sprite_t *newsprites);
- BUILD_Map(void);
- ~BUILD_Map(void);
- private:
- // make these private so we can enforce that no memory leaks
- sector_t *sectors;
- wall_t *walls;
- sprite_t *sprites;
- };
- #else
- } BUILD_Map;
- #endif
- #ifdef MALLOC_STRUCTS
- sector_t* BUILD_Map::setsectors(sector_t *newsectors)
- {
- if (sectors != NULL && sectors != newsectors)
- free(sectors);
- sectors = newsectors;
- return sectors;
- }
- wall_t* BUILD_Map::setwalls(wall_t *newwalls)
- {
- if (walls != NULL && walls != newwalls)
- free(walls);
- walls = newwalls;
- return walls;
- }
- sprite_t* BUILD_Map::setsprites(sprite_t *newsprites)
- {
- if (sprites != NULL && sprites != newsprites)
- free(sprites);
- sprites = newsprites;
- return sprites;
- }
- BUILD_Map::BUILD_Map(void)
- {
- sectors = NULL;
- walls = NULL;
- sprites = NULL;
- }
- BUILD_Map::~BUILD_Map(void)
- {
- if (sectors != NULL)
- free(sectors);
- if (walls != NULL)
- free(walls);
- if (sprites != NULL)
- free(sprites);
- sectors = NULL;
- walls = NULL;
- sprites = NULL;
- }
- #endif
- uint32_t ByteArrayValue(uint8_t array[], size_t num)
- {
- uint32_t value = 0;
- size_t max = clamp(num, 0, 4);
- // ENDIANNESS COMPATIBILITY WARNING!
- size_t i;
- for (i = 0; i < max; ++i)
- value |= array[i] << (i<<3); // ((max - (i + 1))<<3)
- return value;
- }
- // These are pretty dumb wrapper functions, but they show how to interpret the return values.
- size_t readBytes(void * ptr, const size_t size, FILE * stream)
- {
- const size_t result = fread(ptr, sizeof(char), size, stream);
- if (result != size)
- fputs("fread error\n", stderr);
- return result;
- }
- size_t writeBytes(void * ptr, const size_t size, FILE * stream)
- {
- const size_t result = fwrite(ptr, sizeof(char), size, stream);
- if (result != size)
- fputs("fwrite error\n", stderr);
- return result;
- }
- FILE * fopenChild(const char *parent, const char *child, const char *type)
- {
- FILE *dump;
- char namebuf[BMAX_PATH];
- strncpy(namebuf, parent, BMAX_PATH - 16);
- strcat(namebuf, child);
- dump = fopen ( namebuf, type);
- if (dump == NULL)
- fprintf(stderr, "fopen \"%s\" error (%s)\n", type, namebuf);
- return dump;
- }
- // This is a workaround for the below section.
- // It's a lengthy explanation involving "const void*".
- size_t fwrite_derp ( void * ptr, size_t size, size_t count, FILE * stream )
- {
- return fwrite ( ptr, size, count, stream );
- }
- // Fun with function pointers!
- void mapPieces(BUILD_Map *map, const char *mapname, size_t (*fio) ( void *, size_t, size_t, FILE * ), size_t (*ioBytes) ( void *, const size_t, FILE * ), const char *type)
- {
- FILE *mapfile;
- mapfile = fopenChild(mapname, ".header.bin", type);
- ioBytes(map->mapVersion, sizeof(map->mapVersion), mapfile);
- ioBytes(map->playerX, sizeof(map->playerX), mapfile);
- ioBytes(map->playerY, sizeof(map->playerY), mapfile);
- ioBytes(map->playerZ, sizeof(map->playerZ), mapfile);
- ioBytes(map->playerAng, sizeof(map->playerAng), mapfile);
- ioBytes(map->playerSector, sizeof(map->playerSector), mapfile);
- fclose(mapfile);
- mapfile = fopenChild(mapname, ".numsectors.bin", type);
- ioBytes(map->numSectors, sizeof(map->numSectors), mapfile);
- fclose(mapfile);
- mapfile = fopenChild(mapname, ".sectors.bin", type);
- #ifdef MALLOC_STRUCTS
- fio(map->getsectors(), sizeof(sector_t), ByteArrayValue(map->numSectors, sizeof(map->numSectors)), mapfile);
- #else
- fio(map->sectors, sizeof(sector_t), ByteArrayValue(map->numSectors, sizeof(map->numSectors)), mapfile);
- #endif
- fclose(mapfile);
- mapfile = fopenChild(mapname, ".numwalls.bin", type);
- ioBytes(map->numWalls, sizeof(map->numWalls), mapfile);
- fclose(mapfile);
- mapfile = fopenChild(mapname, ".walls.bin", type);
- #ifdef MALLOC_STRUCTS
- fio(map->getwalls(), sizeof(wall_t), ByteArrayValue(map->numWalls, sizeof(map->numWalls)), mapfile);
- #else
- fio(map->walls, sizeof(wall_t), ByteArrayValue(map->numWalls, sizeof(map->numWalls)), mapfile);
- #endif
- fclose(mapfile);
- mapfile = fopenChild(mapname, ".numsprites.bin", type);
- ioBytes(map->numSprites, sizeof(map->numSprites), mapfile);
- fclose(mapfile);
- mapfile = fopenChild(mapname, ".sprites.bin", type);
- #ifdef MALLOC_STRUCTS
- fio(map->getsprites(), sizeof(sprite_t), ByteArrayValue(map->numSprites, sizeof(map->numSprites)), mapfile);
- #else
- fio(map->sprites, sizeof(sprite_t), ByteArrayValue(map->numSprites, sizeof(map->numSprites)), mapfile);
- #endif
- fclose(mapfile);
- }
- void mapFull(BUILD_Map *map, FILE *mapfile, size_t (*fio) ( void *, size_t, size_t, FILE * ), size_t (*ioBytes) ( void *, const size_t, FILE * ))
- {
- ioBytes(map->mapVersion, sizeof(map->mapVersion), mapfile);
- ioBytes(map->playerX, sizeof(map->playerX), mapfile);
- ioBytes(map->playerY, sizeof(map->playerY), mapfile);
- ioBytes(map->playerZ, sizeof(map->playerZ), mapfile);
- ioBytes(map->playerAng, sizeof(map->playerAng), mapfile);
- ioBytes(map->playerSector, sizeof(map->playerSector), mapfile);
- ioBytes(map->numSectors, sizeof(map->numSectors), mapfile);
- #ifdef MALLOC_STRUCTS
- fio(map->getsectors(), sizeof(sector_t), ByteArrayValue(map->numSectors, sizeof(map->numSectors)), mapfile);
- #else
- fio(map->sectors, sizeof(sector_t), ByteArrayValue(map->numSectors, sizeof(map->numSectors)), mapfile);
- #endif
- ioBytes(map->numWalls, sizeof(map->numWalls), mapfile);
- #ifdef MALLOC_STRUCTS
- fio(map->getwalls(), sizeof(wall_t), ByteArrayValue(map->numWalls, sizeof(map->numWalls)), mapfile);
- #else
- fio(map->walls, sizeof(wall_t), ByteArrayValue(map->numWalls, sizeof(map->numWalls)), mapfile);
- #endif
- ioBytes(map->numSprites, sizeof(map->numSprites), mapfile);
- #ifdef MALLOC_STRUCTS
- fio(map->getsprites(), sizeof(sprite_t), ByteArrayValue(map->numSprites, sizeof(map->numSprites)), mapfile);
- #else
- fio(map->sprites, sizeof(sprite_t), ByteArrayValue(map->numSprites, sizeof(map->numSprites)), mapfile);
- #endif
- }
- int main(int argc, char *argv[])
- {
- if (argc < 3)
- {
- fputs("Not enough command line arguments.\n", stderr);
- return 1;
- }
- if (strcasecmp(argv[1], "check") == 0)
- {
- char *filename = NULL;
- int j;
- // should be bools
- int8_t dump = 0;
- int8_t test = 0;
- int8_t decimal = 0;
- int8_t log = 0;
- FILE *mapfile, *textoutput = stdout;
- BUILD_Map map;
- size_t i;
- #ifdef MALLOC_STRUCTS
- sector_t *sectors;
- wall_t *walls;
- sprite_t *sprites;
- #endif
- for (j = 2; j < argc; ++j)
- {
- if (strcmp(argv[j], "-d") == 0)
- dump = 1;
- else if (strcmp(argv[j], "-t") == 0)
- test = 1;
- else if (strcmp(argv[j], "-h") == 0)
- decimal = 1;
- else if (strcmp(argv[j], "-l") == 0)
- log = 1;
- else
- filename = argv[j];
- }
- if (filename == NULL)
- {
- fputs("No map file specified.\n", stderr);
- return 3;
- }
- // input
- mapfile = fopenChild(filename, "", "rb");
- mapFull(&map, mapfile, fread, readBytes);
- fclose(mapfile);
- // print data
- if (log)
- textoutput = fopenChild(filename, ".log", "w");
- fprintf(textoutput, "Map: %s\n", filename);
- fprintf(textoutput, "Map Format Player X Player Y Player Z Player Ang Player Sector\n");
- if (decimal)
- fprintf(textoutput, "%011u %011i %011i %011i %05u %05u",
- ByteArrayValue(map.mapVersion, 4), ByteArrayValue(map.playerX, 4), ByteArrayValue(map.playerY, 4), ByteArrayValue(map.playerZ, 4),
- ByteArrayValue(map.playerAng, 2), ByteArrayValue(map.playerSector, 2));
- else
- {
- for (i = 0; i < sizeof(map.mapVersion); ++i)
- fprintf(textoutput, "%02X ", map.mapVersion[i]);
- fprintf(textoutput, " ");
- for (i = 0; i < sizeof(map.playerX); ++i)
- fprintf(textoutput, "%02X ", map.playerX[i]);
- fprintf(textoutput, " ");
- for (i = 0; i < sizeof(map.playerY); ++i)
- fprintf(textoutput, "%02X ", map.playerY[i]);
- fprintf(textoutput, " ");
- for (i = 0; i < sizeof(map.playerZ); ++i)
- fprintf(textoutput, "%02X ", map.playerZ[i]);
- fprintf(textoutput, " ");
- for (i = 0; i < sizeof(map.playerAng); ++i)
- fprintf(textoutput, "%02X ", map.playerAng[i]);
- fprintf(textoutput, " ");
- for (i = 0; i < sizeof(map.playerSector); ++i)
- fprintf(textoutput, "%02X ", map.playerSector[i]);
- }
- fprintf(textoutput, "\nNumSectors Sectors NumWalls Walls NumSprites Sprites\n");
- if (decimal)
- fprintf(textoutput, "%05u [%07u] %05u [%07u] %05u [%07u]",
- ByteArrayValue(map.numSectors, sizeof(map.numSectors)), ByteArrayValue(map.numSectors, sizeof(map.numSectors)) * sizeof(sector_t),
- ByteArrayValue(map.numWalls, sizeof(map.numWalls)), ByteArrayValue(map.numWalls, sizeof(map.numWalls)) * sizeof(wall_t),
- ByteArrayValue(map.numSprites, sizeof(map.numSprites)), ByteArrayValue(map.numSprites, sizeof(map.numSprites)) * sizeof(sprite_t));
- else
- {
- for (i = 0; i < sizeof(map.numSectors); ++i)
- fprintf(textoutput, "%02X ", map.numSectors[i]);
- fprintf(textoutput, " [%05X] ", ByteArrayValue(map.numSectors, sizeof(map.numSectors)) * sizeof(sector_t));
- for (i = 0; i < sizeof(map.numWalls); ++i)
- fprintf(textoutput, "%02X ", map.numWalls[i]);
- fprintf(textoutput, " [%05X] ", ByteArrayValue(map.numWalls, sizeof(map.numWalls)) * sizeof(wall_t));
- for (i = 0; i < sizeof(map.numSprites); ++i)
- fprintf(textoutput, "%02X ", map.numSprites[i]);
- fprintf(textoutput, " [%05X] ", ByteArrayValue(map.numSprites, sizeof(map.numSprites)) * sizeof(sprite_t));
- }
- fprintf(textoutput, "\n");
- if (log)
- fclose(textoutput);
- if (dump)
- mapPieces(&map, filename, fwrite_derp, writeBytes, "wb");
- if (test)
- {
- FILE *test = fopenChild(filename, ".test.map", "wb");
- mapFull(&map, test, fwrite_derp, writeBytes);
- fclose(test);
- }
- }
- else if (strcasecmp(argv[1], "combine") == 0)
- {
- char *filename = NULL;
- int j;
- FILE *mapfile;
- BUILD_Map map;
- for (j = 2; j < argc; ++j)
- filename = argv[j];
- if (filename == NULL)
- {
- fputs("No map file specified.\n", stderr);
- return 3;
- }
- mapfile = fopen(filename, "rb");
- if (mapfile != NULL)
- {
- fputs("Map file already exists. Not overwriting.\n", stderr);
- return 4;
- }
- // input
- mapPieces(&map, filename, fread, readBytes, "rb");
- // output
- mapfile = fopenChild(filename, "", "wb");
- mapFull(&map, mapfile, fwrite_derp, writeBytes);
- fclose(mapfile);
- }
- else
- {
- fputs("No valid command given.\n", stderr);
- return 2;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment