From a8ac4935a85b5a56b334497d5a6a62eaa695724e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gustaf=20Alh=C3=A4ll?= <gustaf@hanicef.me>
Date: Wed, 26 Mar 2025 22:04:23 +0100
Subject: [PATCH] Bring back precipdensity

---
 src/m_menu.c | 17 +++++-----
 src/p_mobj.c | 89 ++++++++++++++++++++++++++++++----------------------
 src/r_main.c |  4 ++-
 src/r_main.h |  2 +-
 4 files changed, 65 insertions(+), 47 deletions(-)

diff --git a/src/m_menu.c b/src/m_menu.c
index c9b14c65ae..6b7db0d6dc 100644
--- a/src/m_menu.c
+++ b/src/m_menu.c
@@ -1341,14 +1341,15 @@ static menuitem_t OP_VideoOptionsMenu[] =
 
 	{IT_HEADER, NULL, "Level", NULL, NULL, 185},
 	{IT_STRING | IT_CVAR, NULL, "Draw Distance",          NULL, &cv_drawdist,        191},
-	{IT_STRING | IT_CVAR, NULL, "Weather Draw Dist.",     NULL, &cv_drawdist_precip, 196},
-	{IT_STRING | IT_CVAR, NULL, "NiGHTS Hoop Draw Dist.", NULL, &cv_drawdist_nights, 201},
-
-	{IT_HEADER, NULL, "Diagnostic", NULL, NULL, 210},
-	{IT_STRING | IT_CVAR, NULL, "Show FPS",            NULL, &cv_fpscounter,      216},
-	{IT_STRING | IT_CVAR, NULL, "Show TPS",            NULL, &cv_tpscounter,      221},
-	{IT_STRING | IT_CVAR, NULL, "Clear Before Redraw", NULL, &cv_homremoval,      226},
-	{IT_STRING | IT_CVAR, NULL, "Show \"FOCUS LOST\"", NULL, &cv_showfocuslost,   231},
+	{IT_STRING | IT_CVAR, NULL, "Weather Density",        NULL, &cv_precipdensity,   196},
+	{IT_STRING | IT_CVAR, NULL, "Weather Draw Dist.",     NULL, &cv_drawdist_precip, 201},
+	{IT_STRING | IT_CVAR, NULL, "NiGHTS Hoop Draw Dist.", NULL, &cv_drawdist_nights, 206},
+
+	{IT_HEADER, NULL, "Diagnostic", NULL, NULL, 215},
+	{IT_STRING | IT_CVAR, NULL, "Show FPS",            NULL, &cv_fpscounter,      221},
+	{IT_STRING | IT_CVAR, NULL, "Show TPS",            NULL, &cv_tpscounter,      226},
+	{IT_STRING | IT_CVAR, NULL, "Clear Before Redraw", NULL, &cv_homremoval,      231},
+	{IT_STRING | IT_CVAR, NULL, "Show \"FOCUS LOST\"", NULL, &cv_showfocuslost,   236},
 };
 
 static menuitem_t OP_VideoModeMenu[] =
diff --git a/src/p_mobj.c b/src/p_mobj.c
index fd5d6f8924..58a9bc1db8 100644
--- a/src/p_mobj.c
+++ b/src/p_mobj.c
@@ -11368,12 +11368,24 @@ consvar_t cv_flagtime = CVAR_INIT ("flagtime", "30", NULL, CV_SAVE|CV_NETVAR|CV_
 
 void P_SpawnPrecipitation(void)
 {
-	INT32 i, mrand;
+	INT32 i, j, mrand;
 	fixed_t basex, basey, x, y, height;
 	subsector_t *precipsector = NULL;
 	precipmobj_t *rainmo = NULL;
+	thinker_t *think = thlist[THINK_PRECIP].next;
 
-	if (dedicated || !(cv_drawdist_precip.value) || curWeather == PRECIP_NONE || curWeather == PRECIP_STORM_NORAIN)
+	if (dedicated || curWeather == PRECIP_NONE || curWeather == PRECIP_STORM_NORAIN)
+		return;
+
+	// get rid of all old precip first, if any
+	while (think != NULL && think != &thlist[THINK_PRECIP])
+	{
+		thinker_t *next = think->next;
+		P_RemovePrecipMobj((precipmobj_t *)think);
+		think = next;
+	}
+
+	if (!cv_precipdensity.value || !cv_drawdist_precip.value)
 		return;
 
 	// Use the blockmap to narrow down our placing patterns
@@ -11382,49 +11394,52 @@ void P_SpawnPrecipitation(void)
 		basex = bmaporgx + (i % bmapwidth) * MAPBLOCKSIZE;
 		basey = bmaporgy + (i / bmapwidth) * MAPBLOCKSIZE;
 
-		x = basex + ((M_RandomKey(MAPBLOCKUNITS<<3)<<FRACBITS)>>3);
-		y = basey + ((M_RandomKey(MAPBLOCKUNITS<<3)<<FRACBITS)>>3);
+		for (j = 0; j < cv_precipdensity.value; j++)
+		{
+			x = basex + ((M_RandomKey(MAPBLOCKUNITS<<3)<<FRACBITS)>>3);
+			y = basey + ((M_RandomKey(MAPBLOCKUNITS<<3)<<FRACBITS)>>3);
 
-		precipsector = R_PointInSubsectorOrNull(x, y);
+			precipsector = R_PointInSubsectorOrNull(x, y);
 
-		// No sector? Stop wasting time,
-		// move on to the next entry in the blockmap
-		if (!precipsector)
-			continue;
+			// No sector? Stop wasting time,
+			// move on to the next entry in the blockmap
+			if (!precipsector)
+				continue;
 
-		// Exists, but is too small for reasonable precipitation.
-		if (!(precipsector->sector->floorheight <= precipsector->sector->ceilingheight - (32<<FRACBITS)))
-			continue;
+			// Exists, but is too small for reasonable precipitation.
+			if (!(precipsector->sector->floorheight <= precipsector->sector->ceilingheight - (32<<FRACBITS)))
+				continue;
 
-		// Don't set height yet...
-		height = precipsector->sector->ceilingheight;
+			// Don't set height yet...
+			height = precipsector->sector->ceilingheight;
 
-		if (curWeather == PRECIP_SNOW)
-		{
-			// Not in a sector with visible sky -- exception for NiGHTS.
-			if ((!(maptol & TOL_NIGHTS) && (precipsector->sector->ceilingpic != skyflatnum)) == !(precipsector->sector->flags & MSF_INVERTPRECIP))
-				continue;
+			if (curWeather == PRECIP_SNOW)
+			{
+				// Not in a sector with visible sky -- exception for NiGHTS.
+				if ((!(maptol & TOL_NIGHTS) && (precipsector->sector->ceilingpic != skyflatnum)) == !(precipsector->sector->flags & MSF_INVERTPRECIP))
+					continue;
 
-			rainmo = P_SpawnSnowMobj(x, y, height, MT_SNOWFLAKE);
-			mrand = M_RandomByte();
-			if (mrand < 64)
-				P_SetPrecipMobjState(rainmo, S_SNOW3);
-			else if (mrand < 144)
-				P_SetPrecipMobjState(rainmo, S_SNOW2);
-		}
-		else // everything else.
-		{
-			// Not in a sector with visible sky.
-			if ((precipsector->sector->ceilingpic != skyflatnum) == !(precipsector->sector->flags & MSF_INVERTPRECIP))
-				continue;
+				rainmo = P_SpawnSnowMobj(x, y, height, MT_SNOWFLAKE);
+				mrand = M_RandomByte();
+				if (mrand < 64)
+					P_SetPrecipMobjState(rainmo, S_SNOW3);
+				else if (mrand < 144)
+					P_SetPrecipMobjState(rainmo, S_SNOW2);
+			}
+			else // everything else.
+			{
+				// Not in a sector with visible sky.
+				if ((precipsector->sector->ceilingpic != skyflatnum) == !(precipsector->sector->flags & MSF_INVERTPRECIP))
+					continue;
 
-			rainmo = P_SpawnRainMobj(x, y, height, MT_RAIN);
-			if (curWeather == PRECIP_BLANK)
-				rainmo->precipflags |= PCF_INVISIBLE;
-		}
+				rainmo = P_SpawnRainMobj(x, y, height, MT_RAIN);
+				if (curWeather == PRECIP_BLANK)
+					rainmo->precipflags |= PCF_INVISIBLE;
+			}
 
-		// Randomly assign a height, now that floorz is set.
-		rainmo->z = M_RandomRange(rainmo->floorz>>FRACBITS, rainmo->ceilingz>>FRACBITS)<<FRACBITS;
+			// Randomly assign a height, now that floorz is set.
+			rainmo->z = M_RandomRange(rainmo->floorz>>FRACBITS, rainmo->ceilingz>>FRACBITS)<<FRACBITS;
+		}
 	}
 
 }
diff --git a/src/r_main.c b/src/r_main.c
index 133c1862a3..bb4be63a2d 100644
--- a/src/r_main.c
+++ b/src/r_main.c
@@ -123,7 +123,7 @@ static CV_PossibleValue_t drawdist_cons_t[] = {
 	{3072, "3072"},	{4096, "4096"},	{6144, "6144"},
 	{8192, "8192"},	{0, "Infinite"},	{0, NULL}};
 
-//static CV_PossibleValue_t precipdensity_cons_t[] = {{0, "None"}, {1, "Light"}, {2, "Moderate"}, {4, "Heavy"}, {6, "Thick"}, {8, "V.Thick"}, {0, NULL}};
+static CV_PossibleValue_t precipdensity_cons_t[] = {{0, "None"}, {1, "Light"}, {2, "Moderate"}, {4, "Heavy"}, {6, "Thick"}, {8, "V.Thick"}, {0, NULL}};
 
 static CV_PossibleValue_t drawdist_precip_cons_t[] = {
 	{256, "256"},	{512, "512"},	{768, "768"},
@@ -162,6 +162,7 @@ consvar_t cv_translucency = CVAR_INIT ("translucency", "On", "If off, all object
 consvar_t cv_drawdist = CVAR_INIT ("drawdist", "Infinite", "Draw distance for map objects", CV_SAVE, drawdist_cons_t, NULL);
 consvar_t cv_drawdist_nights = CVAR_INIT ("drawdist_nights", "2048", "Draw distance for hoops in NiGHTS mode", CV_SAVE, drawdist_cons_t, NULL);
 consvar_t cv_drawdist_precip = CVAR_INIT ("drawdist_precip", "1024", "Draw distance for rain and snow", CV_SAVE, drawdist_precip_cons_t, NULL);
+consvar_t cv_precipdensity = CVAR_INIT ("precipdensity", "Light", "Density of rain and snow, note that this can have a significant impact on performance", CV_SAVE|CV_CALL, precipdensity_cons_t, P_SpawnPrecipitation);
 consvar_t cv_fov = CVAR_INIT ("fov", "90", "Sets the view range, higher effectively means larger camera lens", CV_SAVE|CV_CALL, fov_cons_t, Fov_OnChange);
 consvar_t cv_fovadjust = CVAR_INIT ("fovadjust", "On", NULL, CV_SAVE|CV_CALL, CV_OnOff, Fov_OnChange);
 consvar_t cv_fovchange = CVAR_INIT ("fovchange", "Off", NULL, CV_SAVE, CV_OnOff, NULL);
@@ -1684,6 +1685,7 @@ void R_RegisterEngineStuff(void)
 	CV_RegisterVar(&cv_drawdist);
 	CV_RegisterVar(&cv_drawdist_nights);
 	CV_RegisterVar(&cv_drawdist_precip);
+	CV_RegisterVar(&cv_precipdensity);
 	CV_RegisterVar(&cv_fovadjust);
 	CV_RegisterVar(&cv_fovchange);
 
diff --git a/src/r_main.h b/src/r_main.h
index 9efc562416..e4d01a0164 100644
--- a/src/r_main.h
+++ b/src/r_main.h
@@ -141,7 +141,7 @@ extern consvar_t cv_flipcam, cv_flipcam2;
 
 extern consvar_t cv_shadow;
 extern consvar_t cv_translucency;
-extern consvar_t cv_drawdist, cv_drawdist_nights, cv_drawdist_precip;
+extern consvar_t cv_precipdensity, cv_drawdist, cv_drawdist_nights, cv_drawdist_precip;
 extern consvar_t cv_fov, cv_fovadjust, cv_fovchange;
 extern consvar_t cv_skybox;
 extern consvar_t cv_renderview;
-- 
GitLab