Hendricks266

Hendricks266's Map Tool

Sep 14th, 2013
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.57 KB | None | 0 0
  1. // Hendricks266's Map Tool
  2. // by Hendricks266
  3. // special thanks to Daedolon
  4. // License: ISC License
  5.  
  6. /*
  7. action commands:
  8.  
  9. check - loads and potentially outputs the data
  10. -d - full dump to separate bin files
  11. -t - test dump to a full map file
  12. -h - uses decimal instead of hex
  13. -l - outputs to a log file
  14.  
  15. combine - searches for "check -d" bins and combines to a map file, but does not overwrite
  16.  
  17. */
  18.  
  19. // needed for precise types like int8_t and uint16_t
  20. #ifdef _MSC_VER // MSVC sucks.
  21. # include "stdint.h"
  22. #else
  23. # include <stdint.h>
  24. #endif
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29.  
  30. #ifdef _MSC_VER // MSVC sucks.
  31. # define MALLOC_STRUCTS
  32. #endif
  33.  
  34. #if defined MALLOC_STRUCTS && !defined __cplusplus
  35. # error C++ is required for the malloc structs mode.
  36. #endif
  37.  
  38. // borrowed from compat.h
  39.  
  40. #if __GNUC__ >= 4
  41. # define CLAMP_DECL static inline __attribute__((warn_unused_result))
  42. #else
  43. # define CLAMP_DECL static inline
  44. #endif
  45.  
  46. // Clamp <in> to [<min>..<max>]. The case in <= min is handled first.
  47. CLAMP_DECL int32_t clamp(int32_t in, int32_t min, int32_t max)
  48. {
  49.     return in <= min ? min : (in >= max ? max : in);
  50. }
  51.  
  52. #define BMAX_PATH 256
  53.  
  54. #if defined(_MSC_VER)
  55. # define strcasecmp _stricmp
  56. # define strncasecmp _strnicmp
  57. #elseif defined(__QNX__)
  58. # define strcasecmp stricmp
  59. # define strncasecmp strnicmp
  60. #endif
  61.  
  62. // borrowed from build.h
  63.  
  64. #define MAXSECTORSV8 4096
  65. #define MAXWALLSV8 16384
  66. #define MAXSPRITESV8 16384
  67.  
  68. #define MAXSECTORSV7 1024
  69. #define MAXWALLSV7 8192
  70. #define MAXSPRITESV7 4096
  71.  
  72. #define MAXSECTORS MAXSECTORSV8
  73. #define MAXWALLS MAXWALLSV8
  74. #define MAXSPRITES MAXSPRITESV8
  75.  
  76. typedef struct
  77. {
  78.     int16_t wallptr, wallnum;
  79.     int32_t ceilingz, floorz;
  80.     uint16_t ceilingstat, floorstat;
  81.     int16_t ceilingpicnum, ceilingheinum;
  82.     int8_t ceilingshade;
  83.     uint8_t ceilingpal, ceilingxpanning, ceilingypanning;
  84.     int16_t floorpicnum, floorheinum;
  85.     int8_t floorshade;
  86.     uint8_t floorpal, floorxpanning, floorypanning;
  87.     uint8_t visibility, filler;
  88.     uint16_t lotag, hitag;
  89.     int16_t extra;
  90. } sector_t;
  91.  
  92. typedef struct
  93. {
  94.     int32_t x, y;
  95.     int16_t point2, nextwall, nextsector;
  96.     uint16_t cstat;
  97.     int16_t picnum, overpicnum;
  98.     int8_t shade;
  99.     uint8_t pal, xrepeat, yrepeat, xpanning, ypanning;
  100.     uint16_t lotag, hitag;
  101.     int16_t extra;
  102. } wall_t;
  103.  
  104. typedef struct
  105. {
  106.     int32_t x, y, z;
  107.     uint16_t cstat;
  108.     int16_t picnum;
  109.     int8_t shade;
  110.     uint8_t pal, clipdist, filler;
  111.     uint8_t xrepeat, yrepeat;
  112.     int8_t xoffset, yoffset;
  113.     int16_t sectnum, statnum;
  114.     int16_t ang, owner, xvel, yvel, zvel;
  115.     uint16_t lotag, hitag;
  116.     int16_t extra;
  117. } sprite_t;
  118.  
  119. // brand new
  120.  
  121. #ifdef MALLOC_STRUCTS
  122. struct BUILD_Map
  123. #else
  124. typedef struct
  125. #endif
  126. {
  127.         /* Map version */
  128.         uint8_t        mapVersion[4];    // MAP version
  129.  
  130.         /* Player data */
  131.         uint8_t        playerX[4];       // Player X position
  132.         uint8_t        playerY[4];       // Player Y position
  133.         uint8_t        playerZ[4];       // Player Z position
  134.  
  135.         uint8_t        playerAng[2];     // Player angle
  136.         uint8_t        playerSector[2];  // Player sector
  137.  
  138.         /* Actual data */
  139.         uint8_t        numSectors[2];    // Number of sectors
  140. #ifndef MALLOC_STRUCTS
  141.         sector_t       sectors[MAXSECTORS];
  142. #endif
  143.         uint8_t        numWalls[2];      // Number of walls
  144. #ifndef MALLOC_STRUCTS
  145.         wall_t         walls[MAXWALLS];
  146. #endif
  147.         uint8_t        numSprites[2];    // Number of sprites
  148. #ifndef MALLOC_STRUCTS
  149.         sprite_t       sprites[MAXSPRITES];
  150. #endif
  151.  
  152. #ifdef MALLOC_STRUCTS
  153.         sector_t *getsectors(void) { return sectors; }
  154.         wall_t *getwalls(void) { return walls; }
  155.         sprite_t *getsprites(void) { return sprites; }
  156.  
  157.         sector_t *setsectors(sector_t *newsectors);
  158.         wall_t *setwalls(wall_t *newwalls);
  159.         sprite_t *setsprites(sprite_t *newsprites);
  160.  
  161.         BUILD_Map(void);
  162.         ~BUILD_Map(void);
  163.  
  164.     private:
  165.         // make these private so we can enforce that no memory leaks
  166.         sector_t       *sectors;
  167.         wall_t         *walls;
  168.         sprite_t       *sprites;
  169. };
  170. #else
  171. } BUILD_Map;
  172. #endif
  173.  
  174. #ifdef MALLOC_STRUCTS
  175. sector_t* BUILD_Map::setsectors(sector_t *newsectors)
  176. {
  177.     if (sectors != NULL && sectors != newsectors)
  178.         free(sectors);
  179.  
  180.     sectors = newsectors;
  181.  
  182.     return sectors;
  183. }
  184. wall_t* BUILD_Map::setwalls(wall_t *newwalls)
  185. {
  186.     if (walls != NULL && walls != newwalls)
  187.         free(walls);
  188.  
  189.     walls = newwalls;
  190.  
  191.     return walls;
  192. }
  193. sprite_t* BUILD_Map::setsprites(sprite_t *newsprites)
  194. {
  195.     if (sprites != NULL && sprites != newsprites)
  196.         free(sprites);
  197.  
  198.     sprites = newsprites;
  199.  
  200.     return sprites;
  201. }
  202. BUILD_Map::BUILD_Map(void)
  203. {
  204.     sectors = NULL;
  205.     walls = NULL;
  206.     sprites = NULL;
  207. }
  208. BUILD_Map::~BUILD_Map(void)
  209. {
  210.     if (sectors != NULL)
  211.         free(sectors);
  212.     if (walls != NULL)
  213.         free(walls);
  214.     if (sprites != NULL)
  215.         free(sprites);
  216.  
  217.     sectors = NULL;
  218.     walls = NULL;
  219.     sprites = NULL;
  220. }
  221. #endif
  222.  
  223. uint32_t ByteArrayValue(uint8_t array[], size_t num)
  224. {
  225.     uint32_t value = 0;
  226.     size_t max = clamp(num, 0, 4);
  227.  
  228.     // ENDIANNESS COMPATIBILITY WARNING!
  229.  
  230.     size_t i;
  231.  
  232.     for (i = 0; i < max; ++i)
  233.         value |= array[i] << (i<<3); // ((max - (i + 1))<<3)
  234.  
  235.     return value;
  236. }
  237.  
  238. // These are pretty dumb wrapper functions, but they show how to interpret the return values.
  239. size_t readBytes(void * ptr, const size_t size, FILE * stream)
  240. {
  241.     const size_t result = fread(ptr, sizeof(char), size, stream);
  242.  
  243.     if (result != size)
  244.         fputs("fread error\n", stderr);
  245.  
  246.     return result;
  247. }
  248. size_t writeBytes(void * ptr, const size_t size, FILE * stream)
  249. {
  250.     const size_t result = fwrite(ptr, sizeof(char), size, stream);
  251.  
  252.     if (result != size)
  253.         fputs("fwrite error\n", stderr);
  254.  
  255.     return result;
  256. }
  257. FILE * fopenChild(const char *parent, const char *child, const char *type)
  258. {
  259.     FILE *dump;
  260.     char namebuf[BMAX_PATH];
  261.  
  262.     strncpy(namebuf, parent, BMAX_PATH - 16);
  263.     strcat(namebuf, child);
  264.  
  265.     dump = fopen ( namebuf, type);
  266.  
  267.     if (dump == NULL)
  268.         fprintf(stderr, "fopen \"%s\" error (%s)\n", type, namebuf);
  269.  
  270.     return dump;
  271. }
  272.  
  273. // This is a workaround for the below section.
  274. // It's a lengthy explanation involving "const void*".
  275. size_t fwrite_derp ( void * ptr, size_t size, size_t count, FILE * stream )
  276. {
  277.     return fwrite ( ptr, size, count, stream );
  278. }
  279.  
  280. // Fun with function pointers!
  281.  
  282. 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)
  283. {
  284.     FILE *mapfile;
  285.  
  286.     mapfile = fopenChild(mapname, ".header.bin", type);
  287.     ioBytes(map->mapVersion, sizeof(map->mapVersion), mapfile);
  288.     ioBytes(map->playerX, sizeof(map->playerX), mapfile);
  289.     ioBytes(map->playerY, sizeof(map->playerY), mapfile);
  290.     ioBytes(map->playerZ, sizeof(map->playerZ), mapfile);
  291.     ioBytes(map->playerAng, sizeof(map->playerAng), mapfile);
  292.     ioBytes(map->playerSector, sizeof(map->playerSector), mapfile);
  293.     fclose(mapfile);
  294.  
  295.     mapfile = fopenChild(mapname, ".numsectors.bin", type);
  296.     ioBytes(map->numSectors, sizeof(map->numSectors), mapfile);
  297.     fclose(mapfile);
  298.  
  299.     mapfile = fopenChild(mapname, ".sectors.bin", type);
  300. #ifdef MALLOC_STRUCTS
  301.     fio(map->getsectors(), sizeof(sector_t), ByteArrayValue(map->numSectors, sizeof(map->numSectors)), mapfile);
  302. #else
  303.     fio(map->sectors, sizeof(sector_t), ByteArrayValue(map->numSectors, sizeof(map->numSectors)), mapfile);
  304. #endif
  305.     fclose(mapfile);
  306.  
  307.     mapfile = fopenChild(mapname, ".numwalls.bin", type);
  308.     ioBytes(map->numWalls, sizeof(map->numWalls), mapfile);
  309.     fclose(mapfile);
  310.  
  311.     mapfile = fopenChild(mapname, ".walls.bin", type);
  312. #ifdef MALLOC_STRUCTS
  313.     fio(map->getwalls(), sizeof(wall_t), ByteArrayValue(map->numWalls, sizeof(map->numWalls)), mapfile);
  314. #else
  315.     fio(map->walls, sizeof(wall_t), ByteArrayValue(map->numWalls, sizeof(map->numWalls)), mapfile);
  316. #endif
  317.     fclose(mapfile);
  318.  
  319.     mapfile = fopenChild(mapname, ".numsprites.bin", type);
  320.     ioBytes(map->numSprites, sizeof(map->numSprites), mapfile);
  321.     fclose(mapfile);
  322.  
  323.     mapfile = fopenChild(mapname, ".sprites.bin", type);
  324. #ifdef MALLOC_STRUCTS
  325.     fio(map->getsprites(), sizeof(sprite_t), ByteArrayValue(map->numSprites, sizeof(map->numSprites)), mapfile);
  326. #else
  327.     fio(map->sprites, sizeof(sprite_t), ByteArrayValue(map->numSprites, sizeof(map->numSprites)), mapfile);
  328. #endif
  329.     fclose(mapfile);
  330. }
  331.  
  332. void mapFull(BUILD_Map *map, FILE *mapfile, size_t (*fio) ( void *, size_t, size_t, FILE * ), size_t (*ioBytes) ( void *, const size_t, FILE * ))
  333. {
  334.     ioBytes(map->mapVersion, sizeof(map->mapVersion), mapfile);
  335.     ioBytes(map->playerX, sizeof(map->playerX), mapfile);
  336.     ioBytes(map->playerY, sizeof(map->playerY), mapfile);
  337.     ioBytes(map->playerZ, sizeof(map->playerZ), mapfile);
  338.     ioBytes(map->playerAng, sizeof(map->playerAng), mapfile);
  339.     ioBytes(map->playerSector, sizeof(map->playerSector), mapfile);
  340.  
  341.     ioBytes(map->numSectors, sizeof(map->numSectors), mapfile);
  342. #ifdef MALLOC_STRUCTS
  343.     fio(map->getsectors(), sizeof(sector_t), ByteArrayValue(map->numSectors, sizeof(map->numSectors)), mapfile);
  344. #else
  345.     fio(map->sectors, sizeof(sector_t), ByteArrayValue(map->numSectors, sizeof(map->numSectors)), mapfile);
  346. #endif
  347.     ioBytes(map->numWalls, sizeof(map->numWalls), mapfile);
  348. #ifdef MALLOC_STRUCTS
  349.     fio(map->getwalls(), sizeof(wall_t), ByteArrayValue(map->numWalls, sizeof(map->numWalls)), mapfile);
  350. #else
  351.     fio(map->walls, sizeof(wall_t), ByteArrayValue(map->numWalls, sizeof(map->numWalls)), mapfile);
  352. #endif
  353.     ioBytes(map->numSprites, sizeof(map->numSprites), mapfile);
  354. #ifdef MALLOC_STRUCTS
  355.     fio(map->getsprites(), sizeof(sprite_t), ByteArrayValue(map->numSprites, sizeof(map->numSprites)), mapfile);
  356. #else
  357.     fio(map->sprites, sizeof(sprite_t), ByteArrayValue(map->numSprites, sizeof(map->numSprites)), mapfile);
  358. #endif
  359. }
  360.  
  361. int main(int argc, char *argv[])
  362. {
  363.     if (argc < 3)
  364.     {
  365.         fputs("Not enough command line arguments.\n", stderr);
  366.         return 1;
  367.     }
  368.  
  369.     if (strcasecmp(argv[1], "check") == 0)
  370.     {
  371.         char *filename = NULL;
  372.         int j;
  373.  
  374.         // should be bools
  375.         int8_t dump = 0;
  376.         int8_t test = 0;
  377.         int8_t decimal = 0;
  378.         int8_t log = 0;
  379.  
  380.         FILE *mapfile, *textoutput = stdout;
  381.         BUILD_Map map;
  382.         size_t i;
  383. #ifdef MALLOC_STRUCTS
  384.         sector_t *sectors;
  385.         wall_t *walls;
  386.         sprite_t *sprites;
  387. #endif
  388.  
  389.         for (j = 2; j < argc; ++j)
  390.         {
  391.             if (strcmp(argv[j], "-d") == 0)
  392.                 dump = 1;
  393.             else if (strcmp(argv[j], "-t") == 0)
  394.                 test = 1;
  395.             else if (strcmp(argv[j], "-h") == 0)
  396.                 decimal = 1;
  397.             else if (strcmp(argv[j], "-l") == 0)
  398.                 log = 1;
  399.             else
  400.                 filename = argv[j];
  401.         }
  402.  
  403.         if (filename == NULL)
  404.         {
  405.             fputs("No map file specified.\n", stderr);
  406.             return 3;
  407.         }
  408.  
  409.         // input
  410.         mapfile = fopenChild(filename, "", "rb");
  411.         mapFull(&map, mapfile, fread, readBytes);
  412.         fclose(mapfile);
  413.  
  414.         // print data
  415.         if (log)
  416.             textoutput = fopenChild(filename, ".log", "w");
  417.  
  418.         fprintf(textoutput, "Map: %s\n", filename);
  419.         fprintf(textoutput, "Map Format   Player X     Player Y     Player Z     Player Ang   Player Sector\n");
  420.         if (decimal)
  421.             fprintf(textoutput, "%011u  %011i  %011i  %011i  %05u        %05u",
  422.                 ByteArrayValue(map.mapVersion, 4), ByteArrayValue(map.playerX, 4), ByteArrayValue(map.playerY, 4), ByteArrayValue(map.playerZ, 4),
  423.                 ByteArrayValue(map.playerAng, 2), ByteArrayValue(map.playerSector, 2));
  424.         else
  425.         {
  426.             for (i = 0; i < sizeof(map.mapVersion); ++i)
  427.                 fprintf(textoutput, "%02X ", map.mapVersion[i]);
  428.             fprintf(textoutput, " ");
  429.             for (i = 0; i < sizeof(map.playerX); ++i)
  430.                 fprintf(textoutput, "%02X ", map.playerX[i]);
  431.             fprintf(textoutput, " ");
  432.             for (i = 0; i < sizeof(map.playerY); ++i)
  433.                 fprintf(textoutput, "%02X ", map.playerY[i]);
  434.             fprintf(textoutput, " ");
  435.             for (i = 0; i < sizeof(map.playerZ); ++i)
  436.                 fprintf(textoutput, "%02X ", map.playerZ[i]);
  437.             fprintf(textoutput, " ");
  438.             for (i = 0; i < sizeof(map.playerAng); ++i)
  439.                 fprintf(textoutput, "%02X ", map.playerAng[i]);
  440.             fprintf(textoutput, "       ");
  441.             for (i = 0; i < sizeof(map.playerSector); ++i)
  442.                 fprintf(textoutput, "%02X ", map.playerSector[i]);
  443.         }
  444.         fprintf(textoutput, "\nNumSectors   Sectors      NumWalls     Walls        NumSprites   Sprites\n");
  445.         if (decimal)
  446.             fprintf(textoutput, "%05u        [%07u]    %05u        [%07u]    %05u        [%07u]",
  447.                 ByteArrayValue(map.numSectors, sizeof(map.numSectors)), ByteArrayValue(map.numSectors, sizeof(map.numSectors)) * sizeof(sector_t),
  448.                 ByteArrayValue(map.numWalls, sizeof(map.numWalls)), ByteArrayValue(map.numWalls, sizeof(map.numWalls)) * sizeof(wall_t),
  449.                 ByteArrayValue(map.numSprites, sizeof(map.numSprites)), ByteArrayValue(map.numSprites, sizeof(map.numSprites)) * sizeof(sprite_t));
  450.         else
  451.         {
  452.             for (i = 0; i < sizeof(map.numSectors); ++i)
  453.                 fprintf(textoutput, "%02X ", map.numSectors[i]);
  454.             fprintf(textoutput, "       [%05X]      ", ByteArrayValue(map.numSectors, sizeof(map.numSectors)) * sizeof(sector_t));
  455.             for (i = 0; i < sizeof(map.numWalls); ++i)
  456.                 fprintf(textoutput, "%02X ", map.numWalls[i]);
  457.             fprintf(textoutput, "       [%05X]      ", ByteArrayValue(map.numWalls, sizeof(map.numWalls)) * sizeof(wall_t));
  458.             for (i = 0; i < sizeof(map.numSprites); ++i)
  459.                 fprintf(textoutput, "%02X ", map.numSprites[i]);
  460.             fprintf(textoutput, "       [%05X]      ", ByteArrayValue(map.numSprites, sizeof(map.numSprites)) * sizeof(sprite_t));
  461.         }
  462.         fprintf(textoutput, "\n");
  463.  
  464.         if (log)
  465.             fclose(textoutput);
  466.  
  467.         if (dump)
  468.             mapPieces(&map, filename, fwrite_derp, writeBytes, "wb");
  469.  
  470.         if (test)
  471.         {
  472.             FILE *test = fopenChild(filename, ".test.map", "wb");
  473.             mapFull(&map, test, fwrite_derp, writeBytes);
  474.             fclose(test);
  475.         }
  476.     }
  477.     else if (strcasecmp(argv[1], "combine") == 0)
  478.     {
  479.         char *filename = NULL;
  480.         int j;
  481.  
  482.         FILE *mapfile;
  483.         BUILD_Map map;
  484.  
  485.         for (j = 2; j < argc; ++j)
  486.             filename = argv[j];
  487.  
  488.         if (filename == NULL)
  489.         {
  490.             fputs("No map file specified.\n", stderr);
  491.             return 3;
  492.         }
  493.  
  494.         mapfile = fopen(filename, "rb");
  495.         if (mapfile != NULL)
  496.         {
  497.             fputs("Map file already exists. Not overwriting.\n", stderr);
  498.             return 4;
  499.         }
  500.  
  501.         // input
  502.         mapPieces(&map, filename, fread, readBytes, "rb");
  503.  
  504.         // output
  505.         mapfile = fopenChild(filename, "", "wb");
  506.         mapFull(&map, mapfile, fwrite_derp, writeBytes);
  507.         fclose(mapfile);
  508.     }
  509.     else
  510.     {
  511.         fputs("No valid command given.\n", stderr);
  512.         return 2;
  513.     }
  514.  
  515.     return 0;
  516. }
Advertisement
Add Comment
Please, Sign In to add comment