From ea81ad80a85db6c7d832e02818b129381a968359 Mon Sep 17 00:00:00 2001
From: MaxED <j.maxed@gmail.com>
Date: Sat, 25 Jun 2016 14:09:39 +0000
Subject: [PATCH] Added, Search and Replace mode: "Find Sector Brightness"
 search mode now supports "<=", "<", ">=" and ">" prefixes. Fixed, Sound
 Environment mode: fixed several cases when single-sided lines with only back
 side could cause either logic errors or crashes. Fixed, Automap mode: fixed
 several cases when single-sided lines with only back side could cause logic
 errors. Debug: added some debug code to WAD.Insert() to investigate the issue
 when lumpoffset could become negative.

---
 Source/Core/IO/WAD.cs                         |  9 +++++
 Source/Plugins/AutomapMode/AutomapMode.cs     |  6 +--
 .../FindReplace/FindSectorBrightness.cs       | 38 ++++++++++++++++++-
 .../SoundPropagationMode/BuilderPlug.cs       |  2 +-
 .../Interface/SoundEnvironmentPanel.cs        |  2 +-
 .../SoundPropagationDomain.cs                 |  6 +--
 6 files changed, 53 insertions(+), 10 deletions(-)

diff --git a/Source/Core/IO/WAD.cs b/Source/Core/IO/WAD.cs
index 0e55ec00f..9720279dc 100644
--- a/Source/Core/IO/WAD.cs
+++ b/Source/Core/IO/WAD.cs
@@ -363,10 +363,19 @@ namespace CodeImp.DoomBuilder.IO
 			// Create the lump
 			Lump lump = new Lump(file, this, Lump.MakeFixedName(name, ENCODING), lumpsoffset, datalength);
 			lumps.Insert(position, lump);
+
+			//dbg
+			int oldlumpoffset = lumpsoffset;
 			
 			// Advance lumps table offset
 			lumpsoffset += datalength;
 
+			//dbg
+			if(lumpsoffset < 0)
+			{
+				throw new InvalidOperationException("Invalid lumpsoffset (" + lumpsoffset + ") after inserting lump \"" + name + "\" at position " + position + ", datalength=" + datalength + ". Previous lumpoffset was " + oldlumpoffset);
+			}
+
 			// Write the new headers
 			if(writeheaders) WriteHeaders();
 
diff --git a/Source/Plugins/AutomapMode/AutomapMode.cs b/Source/Plugins/AutomapMode/AutomapMode.cs
index 74472c384..69fe1f141 100644
--- a/Source/Plugins/AutomapMode/AutomapMode.cs
+++ b/Source/Plugins/AutomapMode/AutomapMode.cs
@@ -180,7 +180,7 @@ namespace CodeImp.DoomBuilder.AutomapMode
 				return ColorSecret;
 
 			if(ld.IsFlagSet(BuilderPlug.Me.HiddenFlag)) return ColorHiddenFlag;
-			if(ld.Back == null || ld.IsFlagSet(BuilderPlug.Me.SecretFlag)) return ColorSingleSided;
+			if(ld.Back == null || ld.Front == null || ld.IsFlagSet(BuilderPlug.Me.SecretFlag)) return ColorSingleSided;
 			if(ld.Front.Sector.FloorHeight != ld.Back.Sector.FloorHeight) return ColorFloorDiff;
 			if(ld.Front.Sector.CeilHeight != ld.Back.Sector.CeilHeight) return ColorCeilDiff;
 
@@ -196,8 +196,8 @@ namespace CodeImp.DoomBuilder.AutomapMode
 		{
 			if(menusform.ShowHiddenLines ^ General.Interface.CtrlState) return true;
 			if(ld.IsFlagSet(BuilderPlug.Me.HiddenFlag)) return false;
-			if(ld.Back == null || ld.IsFlagSet(BuilderPlug.Me.SecretFlag)) return true;
-			if(ld.Back != null && (ld.Front.Sector.FloorHeight != ld.Back.Sector.FloorHeight || ld.Front.Sector.CeilHeight != ld.Back.Sector.CeilHeight)) return true;
+			if(ld.Back == null || ld.Front == null || ld.IsFlagSet(BuilderPlug.Me.SecretFlag)) return true;
+			if(ld.Back != null && ld.Front != null && (ld.Front.Sector.FloorHeight != ld.Back.Sector.FloorHeight || ld.Front.Sector.CeilHeight != ld.Back.Sector.CeilHeight)) return true;
 
 			return false;
 		}
diff --git a/Source/Plugins/BuilderModes/FindReplace/FindSectorBrightness.cs b/Source/Plugins/BuilderModes/FindReplace/FindSectorBrightness.cs
index 1ba6dc71a..89f572dde 100644
--- a/Source/Plugins/BuilderModes/FindReplace/FindSectorBrightness.cs
+++ b/Source/Plugins/BuilderModes/FindReplace/FindSectorBrightness.cs
@@ -1,5 +1,6 @@
 #region ================== Namespaces
 
+using System;
 using System.Collections.Generic;
 using CodeImp.DoomBuilder.Map;
 using System.Windows.Forms;
@@ -11,6 +12,17 @@ namespace CodeImp.DoomBuilder.BuilderModes
 	[FindReplace("Sector Brightness", BrowseButton = false)]
 	internal class FindSectorBrightness : BaseFindSector
 	{
+		#region ================== Properties
+
+		//mxd. Prefixes usage
+		public override string UsageHint { get { return "Supported prefixes:" + Environment.NewLine
+													+ "\"<=\" - finds values less or equal to given value;" + Environment.NewLine
+													+ "\">=\" - finds values greater or equal to given value;" + Environment.NewLine
+													+ "\"<\" - finds values less than given value;" + Environment.NewLine
+													+ "\">\" - finds values greater than given value."; } }
+
+		#endregion
+
 		#region ================== Methods
 
 		// This is called to perform a search (and replace)
@@ -37,6 +49,14 @@ namespace CodeImp.DoomBuilder.BuilderModes
 
 			// Interpret the number given
 			int brightness;
+			string prefix = string.Empty; //mxd
+
+			//mxd. Check prefixes
+			value = value.Trim().Replace(" ", "");
+			if(value.StartsWith(">=") || value.StartsWith("<=")) prefix = value.Substring(0, 2);
+			else if(value.StartsWith(">") || value.StartsWith("<")) prefix = value.Substring(0, 1);
+			value = value.Remove(0, prefix.Length);
+
 			if(int.TryParse(value, out brightness)) 
 			{
 				// Where to search?
@@ -46,12 +66,12 @@ namespace CodeImp.DoomBuilder.BuilderModes
 				foreach(Sector s in list) 
 				{
 					// Brightness matches?
-					if(s.Brightness == brightness) 
+					if(BrightnessMatches(s.Brightness, brightness, prefix)) 
 					{
 						// Replace
 						if(replace) s.Brightness = replacebrightness;
 
-						objs.Add(new FindReplaceObject(s, "Sector " + s.Index));
+						objs.Add(new FindReplaceObject(s, "Sector " + s.Index + (!replace ? " (Brightness " + s.Brightness + ")" : "")));
 					}
 				}
 			}
@@ -66,6 +86,20 @@ namespace CodeImp.DoomBuilder.BuilderModes
 			return objs.ToArray();
 		}
 
+		//mxd
+		private static bool BrightnessMatches(int sectorbrightness, int brightness, string prefix)
+		{
+			switch(prefix)
+			{
+				case "": return sectorbrightness == brightness;
+				case "<=": return sectorbrightness <= brightness;
+				case ">=": return sectorbrightness >= brightness;
+				case "<": return sectorbrightness < brightness;
+				case ">": return sectorbrightness > brightness;
+				default: throw new NotImplementedException("Unknown prefix");
+			}
+		}
+
 		#endregion
 
 	}
diff --git a/Source/Plugins/SoundPropagationMode/BuilderPlug.cs b/Source/Plugins/SoundPropagationMode/BuilderPlug.cs
index 44d26efc1..f5989a71d 100644
--- a/Source/Plugins/SoundPropagationMode/BuilderPlug.cs
+++ b/Source/Plugins/SoundPropagationMode/BuilderPlug.cs
@@ -290,7 +290,7 @@ namespace CodeImp.DoomBuilder.SoundPropagationMode
 							continue;
 						}
 
-						if(sd.Line.Back == null) continue;
+						if(sd.Other == null) continue;
 
 						Sector oppositesector = (sd.Line.Front.Sector == sector ? sd.Line.Back.Sector : sd.Line.Front.Sector);
 
diff --git a/Source/Plugins/SoundPropagationMode/Interface/SoundEnvironmentPanel.cs b/Source/Plugins/SoundPropagationMode/Interface/SoundEnvironmentPanel.cs
index 5b78c8ee1..24bf7e03e 100644
--- a/Source/Plugins/SoundPropagationMode/Interface/SoundEnvironmentPanel.cs
+++ b/Source/Plugins/SoundPropagationMode/Interface/SoundEnvironmentPanel.cs
@@ -133,7 +133,7 @@ namespace CodeImp.DoomBuilder.SoundPropagationMode
 				linedefnode.ImageIndex = iconindex; //mxd
 				linedefnode.SelectedImageIndex = iconindex; //mxd
 
-				if(ld.Back == null)
+				if(ld.Back == null || ld.Front == null)
 				{
 					showwarning = true;
 					linedefnode.ToolTipText = "This line is single-sided, but has\nthe sound boundary flag set.";
diff --git a/Source/Plugins/SoundPropagationMode/SoundPropagationDomain.cs b/Source/Plugins/SoundPropagationMode/SoundPropagationDomain.cs
index cdf452e2f..1646d69ed 100644
--- a/Source/Plugins/SoundPropagationMode/SoundPropagationDomain.cs
+++ b/Source/Plugins/SoundPropagationMode/SoundPropagationDomain.cs
@@ -60,7 +60,7 @@ namespace CodeImp.DoomBuilder.SoundPropagationMode
 					if(blocksound) blockinglines.Add(sd.Line);
 
 					// If the line is one sided, the sound can travel nowhere, so try the next one
-					if(sd.Line.Back == null || blocksound) continue;
+					if(sd.Other == null || blocksound) continue;
 	
 					// Get the sector on the other side of the line we're checking right now
 					Sector oppositesector = sd.Other.Sector;
@@ -87,7 +87,7 @@ namespace CodeImp.DoomBuilder.SoundPropagationMode
 			{
 				// Lines that don't have a back side, or where the sound is blocked due to
 				// the sector heights on each side can be skipped
-				if(ld.Back == null || IsSoundBlockedByHeight(ld)) continue;
+				if(IsSoundBlockedByHeight(ld)) continue;
 				if(!sectors.Contains(ld.Front.Sector)) adjacentsectors.Add(ld.Front.Sector);
 				if(!sectors.Contains(ld.Back.Sector)) adjacentsectors.Add(ld.Back.Sector);
 			}
@@ -111,7 +111,7 @@ namespace CodeImp.DoomBuilder.SoundPropagationMode
 
 		private static bool IsSoundBlockedByHeight(Linedef ld)
 		{
-			if(ld.Back == null) return false;
+			if(ld.Back == null || ld.Front == null) return false;
 
 			Sector s1 = ld.Front.Sector;
 			Sector s2 = ld.Back.Sector;
-- 
GitLab