From b21608d2617109b6ca95d1b8fb5f207957500666 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Fri, 28 Apr 2017 22:25:55 +0100
Subject: [PATCH] [server] Fix line splitting logic.... except it broke again
 :-(

---
 Nibriboard/RippleSpace/DrawnLine.cs | 15 ++++++++++++---
 Nibriboard/RippleSpace/Plane.cs     | 20 +++++++++++++++-----
 Nibriboard/RippleSpace/Reference.cs |  1 +
 3 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/Nibriboard/RippleSpace/DrawnLine.cs b/Nibriboard/RippleSpace/DrawnLine.cs
index 31c5059..93595ae 100644
--- a/Nibriboard/RippleSpace/DrawnLine.cs
+++ b/Nibriboard/RippleSpace/DrawnLine.cs
@@ -41,7 +41,7 @@ namespace Nibriboard.RippleSpace
 				ChunkReference containingChunk = Points[0].ContainingChunk;
 				foreach(LocationReference point in Points)
 				{
-					if (point.ContainingChunk != containingChunk)
+					if (!point.ContainingChunk.Equals(containingChunk))
 						return true;
 				}
 				return false;
@@ -67,7 +67,11 @@ namespace Nibriboard.RippleSpace
 			LineId = inLineId;
 		}
 
-		public List<DrawnLine> SplitOnChunks(int chunkSize)
+		/// <summary>
+		/// Splits this line into a list of lines that don't cross chunk boundaries.
+		/// </summary>
+		/// <returns>A list of lines, that, when stitched together, will produce this line.</returns>
+		public List<DrawnLine> SplitOnChunks()
 		{
 			List<DrawnLine> results = new List<DrawnLine>();
 
@@ -82,7 +86,7 @@ namespace Nibriboard.RippleSpace
 			ChunkReference currentChunk = null;
 			foreach(LocationReference point in Points)
 			{
-				if(currentChunk != null && point.ContainingChunk != currentChunk)
+				if(currentChunk != null && !point.ContainingChunk.Equals(currentChunk))
 				{
 					// We're heading into a new chunk! Split the line up here.
 					// TODO: Add connecting lines to each DrawnLine instance to prevent gaps
@@ -91,8 +95,13 @@ namespace Nibriboard.RippleSpace
 				}
 
 				nextLine.Points.Add(point);
+				if(!point.ContainingChunk.Equals(currentChunk))
+					currentChunk = point.ContainingChunk;
 			}
 
+			if(nextLine.Points.Count > 0)
+				results.Add(nextLine);
+
 			return results;
 		}
 	}
diff --git a/Nibriboard/RippleSpace/Plane.cs b/Nibriboard/RippleSpace/Plane.cs
index 85d1d36..c3658dc 100644
--- a/Nibriboard/RippleSpace/Plane.cs
+++ b/Nibriboard/RippleSpace/Plane.cs
@@ -119,7 +119,11 @@ namespace Nibriboard.RippleSpace
 			// Uh-oh! The chunk isn't loaded at moment. Load it quick & then
 			// return it fast.
 			string chunkFilePath = Path.Combine(StorageDirectory, chunkLocation.AsFilename());
-			Chunk loadedChunk = await Chunk.FromFile(this, chunkFilePath);
+			Chunk loadedChunk;
+			if(File.Exists(chunkFilePath)) // If the chunk exists on disk, load it
+				loadedChunk = await Chunk.FromFile(this, chunkFilePath);
+			else // Ooooh! It's a _new_, never-before-seen one! Create a brand new chunk :D
+				loadedChunk = new Chunk(this, ChunkSize, chunkLocation);
 			loadedChunk.OnChunkUpdate += handleChunkUpdate;
 			loadedChunkspace.Add(chunkLocation, loadedChunk);
 
@@ -128,15 +132,21 @@ namespace Nibriboard.RippleSpace
 
 		public async Task AddLine(DrawnLine newLine)
 		{
-			List<DrawnLine> chunkedLine;
+			List<DrawnLine> chunkedLineParts;
 			// Split the line up into chunked pieces if neccessary
 			if(newLine.SpansMultipleChunks)
-				chunkedLine = newLine.SplitOnChunks(ChunkSize);
+				chunkedLineParts = newLine.SplitOnChunks();
 			else
-				chunkedLine = new List<DrawnLine>() { newLine };
+				chunkedLineParts = new List<DrawnLine>() { newLine };
+
+			foreach(DrawnLine linePart in chunkedLineParts)
+			{
+				if(linePart.Points.Count == 0)
+					Log.WriteLine("[Plane/{0}] Warning: A line part has no points in it O.o", Name);
+			}
 
 			// Add each segment to the appropriate chunk
-			foreach(DrawnLine newLineSegment in chunkedLine)
+			foreach(DrawnLine newLineSegment in chunkedLineParts)
 			{
 				Chunk containingChunk = await FetchChunk(newLineSegment.ContainingChunk);
 				containingChunk.Add(newLineSegment);
diff --git a/Nibriboard/RippleSpace/Reference.cs b/Nibriboard/RippleSpace/Reference.cs
index 5ea865e..e467d87 100644
--- a/Nibriboard/RippleSpace/Reference.cs
+++ b/Nibriboard/RippleSpace/Reference.cs
@@ -15,6 +15,7 @@ namespace Nibriboard.RippleSpace
 		public Reference(Plane inPlane, int inX, int inY)
 		{
 			Plane = inPlane;
+			X = inX; Y = inY;
 		}
 
 		public override string ToString()
