From 6b9565604d42a63926697a719caf6d1def90c33a Mon Sep 17 00:00:00 2001
From: biwa <6475593+biwa@users.noreply.github.com>
Date: Fri, 9 Apr 2021 19:48:38 +0200
Subject: [PATCH] Sector Edit Form UDMF: fixed a bug where resetting a slope
 when the sectors doesn't have a slope resulted in a nonsensical sector height

---
 Source/Core/Windows/SectorEditFormUDMF.cs | 52 ++++++++++++++++-------
 1 file changed, 36 insertions(+), 16 deletions(-)

diff --git a/Source/Core/Windows/SectorEditFormUDMF.cs b/Source/Core/Windows/SectorEditFormUDMF.cs
index 1005790d1..101a243c6 100755
--- a/Source/Core/Windows/SectorEditFormUDMF.cs
+++ b/Source/Core/Windows/SectorEditFormUDMF.cs
@@ -1854,15 +1854,25 @@ namespace CodeImp.DoomBuilder.Windows
 		{
 			MakeUndo(); //mxd
 			Sector fs = General.GetByIndex(sectors, 0);
-			Plane p = new Plane(fs.CeilSlope, fs.CeilSlopeOffset);
-			fs.UpdateBBox();
-			ceilingslopecontrol.SetOffset((int)Math.Round(p.GetZ(fs.BBox.X + fs.BBox.Width / 2, fs.BBox.Y + fs.BBox.Height / 2)), true);
-			
-			foreach(Sector s in sectors) 
+
+			if (fs.CeilSlope.IsNormalized())
 			{
-				s.UpdateBBox();
-				p = new Plane(s.CeilSlope, s.CeilSlopeOffset);
-				s.CeilHeight = (int)Math.Round(p.GetZ(s.BBox.X + s.BBox.Width / 2, s.BBox.Y + s.BBox.Height / 2));
+				fs.UpdateBBox();
+				Plane p = new Plane(fs.CeilSlope, fs.CeilSlopeOffset);
+				ceilingslopecontrol.SetOffset((int)Math.Round(p.GetZ(fs.BBox.X + fs.BBox.Width / 2, fs.BBox.Y + fs.BBox.Height / 2)), true);
+			}
+			else
+				ceilingslopecontrol.SetOffset(fs.CeilHeight, true);
+
+			foreach (Sector s in sectors) 
+			{
+				if (s.CeilSlope.IsNormalized())
+				{
+					s.UpdateBBox();
+					Plane p = new Plane(s.CeilSlope, s.CeilSlopeOffset);
+					s.CeilHeight = (int)Math.Round(p.GetZ(s.BBox.X + s.BBox.Width / 2, s.BBox.Y + s.BBox.Height / 2));
+				}
+
 				s.CeilSlope = new Vector3D();
 				s.CeilSlopeOffset = double.NaN;
 				s.UpdateNeeded = true;
@@ -1879,15 +1889,25 @@ namespace CodeImp.DoomBuilder.Windows
 		{
 			MakeUndo(); //mxd
 			Sector fs = General.GetByIndex(sectors, 0);
-			Plane p = new Plane(fs.FloorSlope, fs.FloorSlopeOffset);
-			fs.UpdateBBox();
-			floorslopecontrol.SetOffset((int)Math.Round(p.GetZ(fs.BBox.X + fs.BBox.Width / 2, fs.BBox.Y + fs.BBox.Height / 2)), true);
-			
-			foreach(Sector s in sectors) 
+
+			if (fs.FloorSlope.IsNormalized())
 			{
-				s.UpdateBBox();
-				p = new Plane(s.FloorSlope, s.FloorSlopeOffset);
-				s.FloorHeight = (int)Math.Round(p.GetZ(s.BBox.X + s.BBox.Width / 2, s.BBox.Y + s.BBox.Height / 2));
+				fs.UpdateBBox();
+				Plane p = new Plane(fs.FloorSlope, fs.FloorSlopeOffset);
+				floorslopecontrol.SetOffset((int)Math.Round(p.GetZ(fs.BBox.X + fs.BBox.Width / 2, fs.BBox.Y + fs.BBox.Height / 2)), true);
+			}
+			else
+				floorslopecontrol.SetOffset(fs.FloorHeight, true);
+
+			foreach (Sector s in sectors) 
+			{
+				if (s.FloorSlope.IsNormalized())
+				{
+					s.UpdateBBox();
+					Plane p = new Plane(s.FloorSlope, s.FloorSlopeOffset);
+					s.FloorHeight = (int)Math.Round(p.GetZ(s.BBox.X + s.BBox.Width / 2, s.BBox.Y + s.BBox.Height / 2));
+				}
+
 				s.FloorSlope = new Vector3D();
 				s.FloorSlopeOffset = double.NaN;
 				s.UpdateNeeded = true;
-- 
GitLab