From 7126aa22f414613f831ba06688b5c08f7f240356 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Tue, 16 Nov 2021 00:30:01 +0100 Subject: [PATCH] - preparations in the map loader for dynamically sized data storage. This also consolidates these parts for Blood with the other games. --- source/build/include/build.h | 1 + source/core/maploader.cpp | 56 ++++++++++++++++++++++------- source/games/blood/src/db.cpp | 4 +-- source/games/duke/src/sectors_r.cpp | 2 +- 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/source/build/include/build.h b/source/build/include/build.h index 17e42a3ae..71e84c6c5 100644 --- a/source/build/include/build.h +++ b/source/build/include/build.h @@ -377,6 +377,7 @@ int32_t engineInit(void); void engineUnInit(void); void initspritelists(void); +void allocateMapArrays(int numsprites); void ValidateSprite(spritetype& spr); void engineLoadBoard(const char *filename, int flags, vec3_t *dapos, int16_t *daang, int *dacursectnum); void loadMapBackup(const char* filename); diff --git a/source/core/maploader.cpp b/source/core/maploader.cpp index c2de9e974..64c331856 100644 --- a/source/core/maploader.cpp +++ b/source/core/maploader.cpp @@ -46,6 +46,18 @@ #include "render.h" #include "hw_sections.h" +// needed for skipping over to get the map size first. +enum +{ + sectorsize5 = 37, + sectorsize6 = 37, + sectorsize7 = 40, + wallsize5 = 35, + wallsize6 = 32, + wallsize7 = 32, +}; + + static void ReadSectorV7(FileReader& fr, sectortype& sect) { sect.wallptr = fr.ReadInt16(); @@ -375,12 +387,23 @@ static void insertAllSprites(const char* filename, const vec3_t* pos, int* curse void addBlockingPairs(); -void engineLoadBoard(const char* filename, int flags, vec3_t* pos, int16_t* ang, int* cursectnum) +// allocates global map storage. Blood will also call this. +void allocateMapArrays(int numsprites) { - inputState.ClearAllInput(); + memset(sector, 0, sizeof(*sector) * MAXSECTORS); memset(wall, 0, sizeof(*wall) * MAXWALLS); memset(sprite, 0, sizeof(*sector) * MAXSPRITES); + memset(spriteext, 0, sizeof(spriteext_t) * MAXSPRITES); + memset(spritesmooth, 0, sizeof(spritesmooth_t) * (MAXSPRITES + MAXUNIQHUDID)); + + ClearAutomap(); + Polymost::Polymost_prepare_loadboard(); +} + +void engineLoadBoard(const char* filename, int flags, vec3_t* pos, int16_t* ang, int* cursectnum) +{ + inputState.ClearAllInput(); FileReader fr = fileSystem.OpenFileReader(filename); if (!fr.isOpen()) I_Error("Unable to open map %s", filename); @@ -390,20 +413,31 @@ void engineLoadBoard(const char* filename, int flags, vec3_t* pos, int16_t* ang, I_Error("%s: Invalid map format, expcted 5-9, got %d", filename, mapversion); } - memset(spriteext, 0, sizeof(spriteext_t) * MAXSPRITES); - memset(spritesmooth, 0, sizeof(spritesmooth_t) * (MAXSPRITES + MAXUNIQHUDID)); - initspritelists(); - ClearAutomap(); - Polymost::Polymost_prepare_loadboard(); - pos->x = fr.ReadInt32(); pos->y = fr.ReadInt32(); pos->z = fr.ReadInt32(); *ang = fr.ReadInt16() & 2047; *cursectnum = fr.ReadUInt16(); + // Get the basics out before loading the data so that we can set up the global storage. numsectors = fr.ReadUInt16(); if ((unsigned)numsectors > MAXSECTORS) I_Error("%s: Invalid map, too many sectors", filename); + auto sectorpos = fr.Tell(); + fr.Seek((mapversion == 5 ? sectorsize5 : mapversion == 6 ? sectorsize6 : sectorsize7) * numsectors, FileReader::SeekCur); + numwalls = fr.ReadUInt16(); + if ((unsigned)numwalls > MAXWALLS) I_Error("%s: Invalid map, too many walls", filename); + auto wallpos = fr.Tell(); + fr.Seek((mapversion == 5 ? wallsize5 : mapversion == 6 ? wallsize6 : wallsize7)* numwalls, FileReader::SeekCur); + int numsprites = fr.ReadUInt16(); + if ((unsigned)numsprites > MAXSPRITES) I_Error("%s: Invalid map, too many sprites", filename); + auto spritepos = fr.Tell(); + + // Now that we know the map's size, set up the globals. + initspritelists(); // may not be used in Blood! + allocateMapArrays(numsprites); + + // Now load the actual data. + fr.Seek(sectorpos, FileReader::SeekSet); for (int i = 0; i < numsectors; i++) { switch (mapversion) @@ -414,8 +448,7 @@ void engineLoadBoard(const char* filename, int flags, vec3_t* pos, int16_t* ang, } } - numwalls = fr.ReadUInt16(); - if ((unsigned)numwalls > MAXWALLS) I_Error("%s: Invalid map, too many walls", filename); + fr.Seek(wallpos, FileReader::SeekSet); for (int i = 0; i < numwalls; i++) { switch (mapversion) @@ -426,8 +459,7 @@ void engineLoadBoard(const char* filename, int flags, vec3_t* pos, int16_t* ang, } } - int numsprites = fr.ReadUInt16(); - if ((unsigned)numsprites > MAXSPRITES) I_Error("%s: Invalid map, too many sprites", filename); + fr.Seek(spritepos, FileReader::SeekSet); for (int i = 0; i < numsprites; i++) { switch (mapversion) diff --git a/source/games/blood/src/db.cpp b/source/games/blood/src/db.cpp index 607a207a8..0db4ddf50 100644 --- a/source/games/blood/src/db.cpp +++ b/source/games/blood/src/db.cpp @@ -503,9 +503,6 @@ void dbLoadMap(const char *pPath, int *pX, int *pY, int *pZ, short *pAngle, int gModernMap = false; #endif - memset(sector, 0, sizeof(sector)); - memset(wall, 0, sizeof(wall)); - memset(sprite, 0, sizeof(sprite)); memset(xsprite, 0, sizeof(xsprite)); #ifdef NOONE_EXTENSIONS @@ -595,6 +592,7 @@ void dbLoadMap(const char *pPath, int *pX, int *pY, int *pZ, short *pAngle, int gMapRev = mapHeader.revision; numsectors = mapHeader.numsectors; numwalls = mapHeader.numwalls; + allocateMapArrays(mapHeader.numsprites); dbInit(); if (encrypted) { diff --git a/source/games/duke/src/sectors_r.cpp b/source/games/duke/src/sectors_r.cpp index b703b6b02..2ec61413d 100644 --- a/source/games/duke/src/sectors_r.cpp +++ b/source/games/duke/src/sectors_r.cpp @@ -979,7 +979,7 @@ static void lotsofpopcorn(DDukeActor *actor, walltype* wal, int n) void checkhitwall_r(DDukeActor* spr, walltype* wal, int x, int y, int z, int atwith) { - int j, i; + int j; int sn = -1, darkestwall; spritetype* s;