From 700b340827cbe8da1bee2afa423d3666be4fad5f Mon Sep 17 00:00:00 2001
From: MascaraSnake <jonassauer27@gmail.com>
Date: Sun, 3 May 2020 18:33:18 +0200
Subject: [PATCH] Allow map-wide gravity to be set via level header

---
 src/dehacked.c   | 2 ++
 src/doomstat.h   | 3 ++-
 src/lua_maplib.c | 2 ++
 src/p_setup.c    | 1 +
 src/p_spec.c     | 2 +-
 5 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/dehacked.c b/src/dehacked.c
index c05a8f4441..268f9943c9 100644
--- a/src/dehacked.c
+++ b/src/dehacked.c
@@ -1867,6 +1867,8 @@ static void readlevelheader(MYFILE *f, INT32 num)
 				mapheaderinfo[num-1]->sstimer = i;
 			else if (fastcmp(word, "SPECIALSTAGESPHERES"))
 				mapheaderinfo[num-1]->ssspheres = i;
+			else if (fastcmp(word, "GRAVITY"))
+				mapheaderinfo[num-1]->gravity = FLOAT_TO_FIXED(atof(word2));
 			else
 				deh_warning("Level header %d: unknown word '%s'", num, word);
 		}
diff --git a/src/doomstat.h b/src/doomstat.h
index e6a227a422..1ec03a86cd 100644
--- a/src/doomstat.h
+++ b/src/doomstat.h
@@ -320,7 +320,8 @@ typedef struct
 	char selectheading[22]; ///< Level select heading. Allows for controllable grouping.
 	UINT16 startrings;      ///< Number of rings players start with.
 	INT32 sstimer;          ///< Timer for special stages.
-	UINT32 ssspheres;        ///< Sphere requirement in special stages.
+	UINT32 ssspheres;       ///< Sphere requirement in special stages.
+	fixed_t gravity;        ///< Map-wide gravity.
 
 	// Title card.
 	char ltzzpatch[8];      ///< Zig zag patch.
diff --git a/src/lua_maplib.c b/src/lua_maplib.c
index 1737216e37..ece42b8d3d 100644
--- a/src/lua_maplib.c
+++ b/src/lua_maplib.c
@@ -2086,6 +2086,8 @@ static int mapheaderinfo_get(lua_State *L)
 		lua_pushinteger(L, header->sstimer);
 	else if (fastcmp(field, "ssspheres"))
 		lua_pushinteger(L, header->ssspheres);
+	else if (fastcmp(field, "gravity"))
+		lua_pushfixed(L, header->gravity);
 	// TODO add support for reading numGradedMares and grades
 	else {
 		// Read custom vars now
diff --git a/src/p_setup.c b/src/p_setup.c
index 1d1826a539..b4a5f2c3c7 100644
--- a/src/p_setup.c
+++ b/src/p_setup.c
@@ -220,6 +220,7 @@ static void P_ClearSingleMapHeaderInfo(INT16 i)
 	mapheaderinfo[num]->startrings = 0;
 	mapheaderinfo[num]->sstimer = 90;
 	mapheaderinfo[num]->ssspheres = 1;
+	mapheaderinfo[num]->gravity = FRACUNIT/2;
 	mapheaderinfo[num]->keywords[0] = '\0';
 	snprintf(mapheaderinfo[num]->musname, 7, "%sM", G_BuildMapName(i));
 	mapheaderinfo[num]->musname[6] = 0;
diff --git a/src/p_spec.c b/src/p_spec.c
index fafe2cdf3b..2e5b1f44b3 100644
--- a/src/p_spec.c
+++ b/src/p_spec.c
@@ -6246,7 +6246,7 @@ static void P_RunLevelLoadExecutors(void)
 void P_InitSpecials(void)
 {
 	// Set the default gravity. Custom gravity overrides this setting.
-	gravity = FRACUNIT/2;
+	gravity = mapheaderinfo[gamemap-1]->gravity;
 
 	// Defaults in case levels don't have them set.
 	sstimer = mapheaderinfo[gamemap-1]->sstimer*TICRATE + 6;
-- 
GitLab