From d6a86c37b2c0ecd9e3256ae7c57fa0043302d44d Mon Sep 17 00:00:00 2001
From: Nev3r <apophycens@gmail.com>
Date: Mon, 23 Dec 2019 16:36:26 +0100
Subject: [PATCH 1/3] P_PrepareRawThings() is now where it was supposed to be
 at; moved save and flat & texture caching related functions to the end of
 P_LoadMapData().

---
 src/p_setup.c | 46 +++++++++++++++++++++++-----------------------
 1 file changed, 23 insertions(+), 23 deletions(-)

diff --git a/src/p_setup.c b/src/p_setup.c
index 6e6c1a72b..7ac713044 100644
--- a/src/p_setup.c
+++ b/src/p_setup.c
@@ -485,6 +485,7 @@ static inline void P_LoadRawSubsectors(void *data)
 
 size_t numlevelflats;
 levelflat_t *levelflats;
+levelflat_t *foundflats;
 
 //SoM: Other files want this info.
 size_t P_PrecacheLevelFlats(void)
@@ -648,7 +649,6 @@ static void P_LoadRawSectors(UINT8 *data)
 {
 	mapsector_t *ms = (mapsector_t *)data;
 	sector_t *ss = sectors;
-	levelflat_t *foundflats;
 	size_t i;
 
 	// Allocate a big chunk of memory as big as our MAXLEVELFLATS limit.
@@ -725,16 +725,6 @@ static void P_LoadRawSectors(UINT8 *data)
 		ss->lineoutLength = -1.0l;
 #endif // ----- end special tricks -----
 	}
-
-	// set the sky flat num
-	skyflatnum = P_AddLevelFlat(SKYFLATNAME, foundflats);
-
-	// copy table for global usage
-	levelflats = M_Memcpy(Z_Calloc(numlevelflats * sizeof (*levelflats), PU_LEVEL, NULL), foundflats, numlevelflats * sizeof (levelflat_t));
-	free(foundflats);
-
-	// search for animated flats and set up
-	P_SetupLevelFlatAnims();
 }
 
 //
@@ -1455,7 +1445,6 @@ static void P_LoadRawSideDefs2(void *data)
 				break;
 		}
 	}
-	R_ClearTextureNumCache(true);
 }
 
 static boolean LineInBlock(fixed_t cx1, fixed_t cy1, fixed_t cx2, fixed_t cy2, fixed_t bx1, fixed_t by1)
@@ -2007,7 +1996,29 @@ static void P_LoadMapData(const virtres_t* virt)
 		P_LoadRawLineDefs(virtlinedefs->data);
 		P_SetupLines();
 		P_LoadRawSideDefs2(virtsidedefs->data);
+		P_PrepareRawThings(virtthings->data);
 	}
+
+	R_ClearTextureNumCache(true);
+
+	// set the sky flat num
+	skyflatnum = P_AddLevelFlat(SKYFLATNAME, foundflats);
+
+	// copy table for global usage
+	levelflats = M_Memcpy(Z_Calloc(numlevelflats * sizeof (*levelflats), PU_LEVEL, NULL), foundflats, numlevelflats * sizeof (levelflat_t));
+	free(foundflats);
+
+	// search for animated flats and set up
+	P_SetupLevelFlatAnims();
+
+	// Copy relevant map data for NetArchive purposes.
+	spawnsectors = Z_Calloc(numsectors * sizeof (*sectors), PU_LEVEL, NULL);
+	spawnlines = Z_Calloc(numlines * sizeof (*lines), PU_LEVEL, NULL);
+	spawnsides = Z_Calloc(numsides * sizeof (*sides), PU_LEVEL, NULL);
+
+	memcpy(spawnsectors, sectors, numsectors * sizeof (*sectors));
+	memcpy(spawnlines, lines, numlines * sizeof (*lines));
+	memcpy(spawnsides, sides, numsides * sizeof (*sides));
 }
 
 #if 0
@@ -2774,17 +2785,6 @@ boolean P_SetupLevel(boolean skipprecip)
 		P_LoadLineDefs2();
 		P_GroupLines();
 
-		// Copy relevant map data for NetArchive purposes.
-		spawnsectors = Z_Calloc(numsectors * sizeof (*sectors), PU_LEVEL, NULL);
-		spawnlines = Z_Calloc(numlines * sizeof (*lines), PU_LEVEL, NULL);
-		spawnsides = Z_Calloc(numsides * sizeof (*sides), PU_LEVEL, NULL);
-
-		memcpy(spawnsectors, sectors, numsectors * sizeof (*sectors));
-		memcpy(spawnlines, lines, numlines * sizeof (*lines));
-		memcpy(spawnsides, sides, numsides * sizeof (*sides));
-
-		P_PrepareRawThings(vres_Find(virt, "THINGS")->data);
-
 		P_MakeMapMD5(virt, &mapmd5);
 
 		vres_Free(virt);

From 9a9f72f59899143f1b0483c7d828b5b260de8151 Mon Sep 17 00:00:00 2001
From: Nev3r <apophycens@gmail.com>
Date: Mon, 23 Dec 2019 16:37:44 +0100
Subject: [PATCH 2/3] Don't reload mapthings in P_LoadThingsOnly() as they
 should no longer be modified past the mapload procedure.

---
 src/p_setup.c | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/src/p_setup.c b/src/p_setup.c
index 7ac713044..f5d4c26e2 100644
--- a/src/p_setup.c
+++ b/src/p_setup.c
@@ -2196,8 +2196,6 @@ void P_LoadThingsOnly(void)
 	// Search through all the thinkers.
 	thinker_t *think;
 	INT32 i, viewid = -1, centerid = -1; // for skyboxes
-	virtres_t* virt = vres_GetMap(lastloadedmaplumpnum);
-	virtlump_t* vth = vres_Find(virt, "THINGS");
 
 	// check if these are any of the normal viewpoint/centerpoint mobjs in the level or not
 	if (skyboxmo[0] || skyboxmo[1])
@@ -2218,11 +2216,8 @@ void P_LoadThingsOnly(void)
 
 	P_LevelInitStuff();
 
-	P_PrepareRawThings(vth->data);
 	P_LoadThings(true);
 
-	vres_Free(virt);
-
 	// restore skybox viewpoint/centerpoint if necessary, set them to defaults if we can't do that
 	skyboxmo[0] = skyboxviewpnts[(viewid >= 0) ? viewid : 0];
 	skyboxmo[1] = skyboxcenterpnts[(centerid >= 0) ? centerid : 0];

From c4a017ddf01e6c529df0369bb28e66d9aa0cdf20 Mon Sep 17 00:00:00 2001
From: Nev3r <apophycens@gmail.com>
Date: Wed, 25 Dec 2019 12:05:40 +0100
Subject: [PATCH 3/3] Move flat caching code to the map data load function.

---
 src/p_setup.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/src/p_setup.c b/src/p_setup.c
index f5d4c26e2..3ae17cc23 100644
--- a/src/p_setup.c
+++ b/src/p_setup.c
@@ -651,14 +651,6 @@ static void P_LoadRawSectors(UINT8 *data)
 	sector_t *ss = sectors;
 	size_t i;
 
-	// Allocate a big chunk of memory as big as our MAXLEVELFLATS limit.
-	//Fab : FIXME: allocate for whatever number of flats - 512 different flats per level should be plenty
-	foundflats = calloc(MAXLEVELFLATS, sizeof (*foundflats));
-	if (foundflats == NULL)
-		I_Error("Ran out of memory while loading sectors\n");
-
-	numlevelflats = 0;
-
 	// For each counted sector, copy the sector raw data from our cache pointer ms, to the global table pointer ss.
 	for (i = 0; i < numsectors; i++, ss++, ms++)
 	{
@@ -1982,6 +1974,14 @@ static void P_LoadMapData(const virtres_t* virt)
 	lines     = Z_Calloc(numlines * sizeof (*lines), PU_LEVEL, NULL);
 	mapthings = Z_Calloc(nummapthings * sizeof (*mapthings), PU_LEVEL, NULL);
 
+	// Allocate a big chunk of memory as big as our MAXLEVELFLATS limit.
+	//Fab : FIXME: allocate for whatever number of flats - 512 different flats per level should be plenty
+	foundflats = calloc(MAXLEVELFLATS, sizeof (*foundflats));
+	if (foundflats == NULL)
+		I_Error("Ran out of memory while loading sectors\n");
+
+	numlevelflats = 0;
+
 #ifdef UDMF
 	if (textmap)
 	{