From e4e05542ec813e287ba575d4f75da90db986dd45 Mon Sep 17 00:00:00 2001
From: biwa <6475593+biwa@users.noreply.github.com>
Date: Thu, 2 Jan 2020 01:32:55 +0100
Subject: [PATCH] Fixed a problem where negative Y scales of sidedefs would
 result in a wrong offset when auto-aligning

---
 Source/Core/Geometry/Tools.cs                        | 12 +++++++++++-
 .../BuilderModes/VisualModes/BaseVisualMode.cs       |  4 ++--
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/Source/Core/Geometry/Tools.cs b/Source/Core/Geometry/Tools.cs
index 1f3253d91..c326c5761 100755
--- a/Source/Core/Geometry/Tools.cs
+++ b/Source/Core/Geometry/Tools.cs
@@ -1937,6 +1937,9 @@ namespace CodeImp.DoomBuilder.Geometry
 			if(side.Line.IsFlagSet(General.Map.Config.UpperUnpeggedFlag) || side.Other == null || side.Other.Sector == null)
 				return offset;
 
+			// Make sure the offset doesn't go in the wrong direction
+			scaleY = Math.Abs(scaleY);
+
 			//if we don't have UpperUnpegged flag, normalize offset
 			float surfaceHeight = side.GetHighHeight() * scaleY;
 			return (float)Math.Round((fromNormalized ? offset + surfaceHeight : offset - surfaceHeight), General.Map.FormatInterface.VertexDecimals);
@@ -1947,6 +1950,9 @@ namespace CodeImp.DoomBuilder.Geometry
 		{
 			if(side.Sector == null) return offset;
 
+			// Make sure the offset doesn't go in the wrong direction
+			scaleY = Math.Abs(scaleY);
+
 			// Normalize offset
 			float surfaceHeight;
 			if(side.Other != null && side.Other.Sector != null)
@@ -1985,7 +1991,11 @@ namespace CodeImp.DoomBuilder.Geometry
 		public static float GetSidedefBottomOffsetY(Sidedef side, float offset, float scaleY, bool fromNormalized) 
 		{
 			float surfaceHeight;
-			if(side.Line.IsFlagSet(General.Map.Config.LowerUnpeggedFlag)) 
+
+			// Make sure the offset doesn't go in the wrong direction
+			scaleY = Math.Abs(scaleY);
+
+			if (side.Line.IsFlagSet(General.Map.Config.LowerUnpeggedFlag)) 
 			{
 				if(side.Other == null || side.Other.Sector == null || side.Sector.CeilTexture != General.Map.Config.SkyFlatName ||
 					side.Other.Sector.CeilTexture != General.Map.Config.SkyFlatName)
diff --git a/Source/Plugins/BuilderModes/VisualModes/BaseVisualMode.cs b/Source/Plugins/BuilderModes/VisualModes/BaseVisualMode.cs
index f26506215..8642ebe0a 100755
--- a/Source/Plugins/BuilderModes/VisualModes/BaseVisualMode.cs
+++ b/Source/Plugins/BuilderModes/VisualModes/BaseVisualMode.cs
@@ -4203,7 +4203,7 @@ namespace CodeImp.DoomBuilder.BuilderModes
 						float offset;
 
 						if (!texture.WorldPanning && !General.Map.Data.MapInfo.ForceWorldPanning)
-							offset = ((start.Sidedef.Sector.CeilHeight - j.ceilingHeight) / scaley) * j.scaleY + ystartalign - j.sidedef.OffsetY; //mxd
+							offset = ((start.Sidedef.Sector.CeilHeight - j.ceilingHeight) / scaley) * Math.Abs(j.scaleY)  + ystartalign - j.sidedef.OffsetY; //mxd
 						else
 							offset = (start.Sidedef.Sector.CeilHeight - j.ceilingHeight + ystartalign - j.sidedef.OffsetY);
 
@@ -4345,7 +4345,7 @@ namespace CodeImp.DoomBuilder.BuilderModes
 
 					if(aligny) 
 					{
-						float offset = ((start.Sidedef.Sector.CeilHeight - j.ceilingHeight) / scaley) * j.scaleY + ystartalign; //mxd
+						float offset = ((start.Sidedef.Sector.CeilHeight - j.ceilingHeight) / scaley) * Math.Abs(j.scaleY) + ystartalign; //mxd
 						offset -= j.sidedef.OffsetY; //mxd
 
 						if(matchtop)
-- 
GitLab