From 577ae68d6d3475258d95962cf7edc01800e05ec2 Mon Sep 17 00:00:00 2001
From: MascaraSnake <jonassauer27@gmail.com>
Date: Tue, 4 Jan 2022 21:25:34 +0100
Subject: [PATCH] Make sector friction a floating-point field

---
 extras/conf/udb/Includes/SRB222_misc.cfg |  4 ++--
 src/lua_maplib.c                         |  2 +-
 src/p_setup.c                            | 20 ++++++++++++++++++--
 src/p_spec.c                             | 12 ++----------
 src/r_defs.h                             |  2 +-
 5 files changed, 24 insertions(+), 16 deletions(-)

diff --git a/extras/conf/udb/Includes/SRB222_misc.cfg b/extras/conf/udb/Includes/SRB222_misc.cfg
index bc284a3f9..e8da74e99 100644
--- a/extras/conf/udb/Includes/SRB222_misc.cfg
+++ b/extras/conf/udb/Includes/SRB222_misc.cfg
@@ -250,8 +250,8 @@ universalfields
 
 		friction
 		{
-			type = 0;
-			default = 0;
+			type = 1;
+			default = 0.90625;
 		}
 
 		triggertag
diff --git a/src/lua_maplib.c b/src/lua_maplib.c
index 4bbd826ef..41ed0bbdc 100644
--- a/src/lua_maplib.c
+++ b/src/lua_maplib.c
@@ -679,7 +679,7 @@ static int sector_get(lua_State *L)
 		lua_pushinteger(L, (UINT8)sector->triggerer);
 		return 1;
 	case sector_friction: // friction
-		lua_pushinteger(L, sector->friction);
+		lua_pushfixed(L, sector->friction);
 		return 1;
 	case sector_gravity: // gravity
 		lua_pushfixed(L, sector->gravity);
diff --git a/src/p_setup.c b/src/p_setup.c
index 5d8bc9cc0..2e6b89ce1 100644
--- a/src/p_setup.c
+++ b/src/p_setup.c
@@ -1054,6 +1054,8 @@ static void P_LoadSectors(UINT8 *data)
 		ss->triggertag = 0;
 		ss->triggerer = TO_PLAYER;
 
+		ss->friction = ORIG_FRICTION;
+
 		P_InitializeSector(ss);
 	}
 }
@@ -1720,7 +1722,7 @@ static void ParseTextmapSectorParameter(UINT32 i, char *param, char *val)
 	else if (fastcmp(param, "ropehang") && fastcmp("true", val))
 		sectors[i].specialflags |= SSF_ROPEHANG;
 	else if (fastcmp(param, "friction"))
-		sectors[i].friction = atol(val);
+		sectors[i].friction = FLOAT_TO_FIXED(atof(val));
 	else if (fastcmp(param, "gravity"))
 		sectors[i].gravity = FLOAT_TO_FIXED(atof(val));
 	else if (fastcmp(param, "damagetype"))
@@ -2034,6 +2036,8 @@ static void P_LoadTextmap(void)
 		sc->triggertag = 0;
 		sc->triggerer = TO_PLAYER;
 
+		sc->friction = ORIG_FRICTION;
+
 		textmap_colormap.used = false;
 		textmap_colormap.lightcolor = 0;
 		textmap_colormap.lightalpha = 25;
@@ -4921,8 +4925,20 @@ static void P_ConvertBinaryMap(void)
 		case 540: //Floor friction
 		{
 			INT32 s;
+			fixed_t strength; // friction value of sector
+			fixed_t friction; // friction value to be applied during movement
+
+			strength = sides[lines[i].sidenum[0]].textureoffset >> FRACBITS;
+			if (strength > 0) // sludge
+				strength = strength*2; // otherwise, the maximum sludginess value is +967...
+
+			// The following might seem odd. At the time of movement,
+			// the move distance is multiplied by 'friction/0x10000', so a
+			// higher friction value actually means 'less friction'.
+			friction = ORIG_FRICTION - (0x1EB8*strength)/0x80; // ORIG_FRICTION is 0xE800
+
 			TAG_ITER_SECTORS(tag, s)
-				sectors[s].friction = sides[lines[i].sidenum[0]].textureoffset >> FRACBITS;
+				sectors[s].friction = friction;
 			break;
 		}
 		case 541: //Wind
diff --git a/src/p_spec.c b/src/p_spec.c
index 73a095fde..4d6b83414 100644
--- a/src/p_spec.c
+++ b/src/p_spec.c
@@ -8337,23 +8337,15 @@ static void P_SpawnFriction(void)
 	size_t i;
 	sector_t *s = sectors;
 
-	fixed_t strength; // friction value of sector
 	fixed_t friction; // friction value to be applied during movement
 	INT32 movefactor; // applied to each player move to simulate inertia
 
 	for (i = 0; i < numsectors; i++, s++)
 	{
-		if (!s->friction)
+		if (s->friction == ORIG_FRICTION)
 			continue;
 
-		strength = s->friction;
-		if (strength > 0) // sludge
-			strength = strength*2; // otherwise, the maximum sludginess value is +967...
-
-		// The following might seem odd. At the time of movement,
-		// the move distance is multiplied by 'friction/0x10000', so a
-		// higher friction value actually means 'less friction'.
-		friction = ORIG_FRICTION - (0x1EB8*strength)/0x80; // ORIG_FRICTION is 0xE800
+		friction = s->friction;
 
 		if (friction > FRACUNIT)
 			friction = FRACUNIT;
diff --git a/src/r_defs.h b/src/r_defs.h
index c56abdbfd..ce3ffaad9 100644
--- a/src/r_defs.h
+++ b/src/r_defs.h
@@ -428,7 +428,7 @@ typedef struct sector_s
 	mtag_t triggertag; // tag to call upon triggering
 	UINT8 triggerer; // who can trigger?
 
-	INT32 friction;
+	fixed_t friction;
 
 	// Sprite culling feature
 	struct line_s *cullheight;
-- 
GitLab