From 6a0d3180a70c09c2fba6a5ee37733d86dfb385f3 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sun, 11 Jun 2017 21:39:33 +0100
Subject: [PATCH] [client] Fix chunk update piping & display logic

---
 Nibriboard/ClientFiles/.tern-project     |  3 +--
 Nibriboard/ClientFiles/Chunk.js          | 11 +++++-----
 Nibriboard/ClientFiles/ChunkCache.js     | 26 ++++++++++++++++++------
 Nibriboard/ClientFiles/ChunkReference.js | 10 +++++++++
 4 files changed, 37 insertions(+), 13 deletions(-)

diff --git a/Nibriboard/ClientFiles/.tern-project b/Nibriboard/ClientFiles/.tern-project
index aa40b83..ebe6414 100644
--- a/Nibriboard/ClientFiles/.tern-project
+++ b/Nibriboard/ClientFiles/.tern-project
@@ -8,7 +8,6 @@
     "Utilities/*.js"
   ],
   "dontLoad": [
-    "node_module/**",
     "NibriClient.js"
   ],
   "plugins": {
@@ -27,4 +26,4 @@
     },
     "commonjs": {}
   }
-}
\ No newline at end of file
+}
diff --git a/Nibriboard/ClientFiles/Chunk.js b/Nibriboard/ClientFiles/Chunk.js
index f39c300..175bf30 100644
--- a/Nibriboard/ClientFiles/Chunk.js
+++ b/Nibriboard/ClientFiles/Chunk.js
@@ -9,7 +9,8 @@ class Chunk
 {
 	/**
 	 * Creates a new chunk.
-	 * @param  {ChunkReference} inChunkRef The location of the new chunk.
+	 * @param 	{ChunkReference}	inChunkRef	The location of the new chunk.
+	 * @param	{number}			inSize		The size of this chunk.
 	 */
 	constructor(inChunkRef, inSize)
 	{
@@ -45,14 +46,14 @@ class Chunk
 		{
 			context.beginPath();
 			context.moveTo(
-				line.Points[0].x - this.chunkRef.x,
-				line.Points[0].y - this.chunkRef.y
+				line.Points[0].x - this.chunkRef.inPlaneSpace(this.size).x,
+				line.Points[0].y - this.chunkRef.inPlaneSpace(this.size).y
 			);
 			for(let i = 1; i < line.Points.length; i++)
 			{
 				context.lineTo(
-					line.Points[i].x - this.chunkRef.x,
-					line.Points[i].y - this.chunkRef.y
+					line.Points[i].x - this.chunkRef.inPlaneSpace(this.size).x,
+					line.Points[i].y - this.chunkRef.inPlaneSpace(this.size).y
 				);
 			}
 			
diff --git a/Nibriboard/ClientFiles/ChunkCache.js b/Nibriboard/ClientFiles/ChunkCache.js
index 9db2045..eda33dd 100644
--- a/Nibriboard/ClientFiles/ChunkCache.js
+++ b/Nibriboard/ClientFiles/ChunkCache.js
@@ -3,6 +3,7 @@
 import ChunkReference from './ChunkReference';
 import Chunk from './Chunk';
 import Rectangle from './Utilities/Rectangle';
+import Vector from './Utilities/Vector';
 
 class ChunkCache
 {
@@ -16,14 +17,19 @@ class ChunkCache
 	
 	/**
 	 * Adds the given chunk to the chunk cache.
-	 * @param {Chunk} chunkData The chunk to add to the cache.
+	 * @param	{Chunk}	chunkData	The chunk to add to the cache.
+	 * @returns	{bool}	Whether we actually updated an existing chunk in the	
+	 * 					cache instead of adding a new one.
 	 */
 	add(chunkData, override = false)
 	{
-		if(!override && this.cache.contains(chunkData.chunkRef.toString()))
+		var hasChunk = this.cache.has(chunkData.chunkRef.toString());
+		if(!override && hasChunk)
 			throw new Error("Error: We already have a chunk at that location stored.");
 		
 		this.cache.set(chunkData.chunkRef.toString(), chunkData);
+		
+		return !hasChunk;
 	}
 	
 	/**
@@ -56,7 +62,9 @@ class ChunkCache
 			{
 				let cChunk = new ChunkReference(
 					this.boardWindow.currentPlaneName,
-					cx, cy
+					// Translate from plane space to chunk space when creating
+					// a _chunk_ reference
+					cx / chunkSize, cy / chunkSize
 				);
 				let chunk = this.cache.get(cChunk.toString());
 				if(typeof chunk != "undefined")
@@ -76,10 +84,16 @@ class ChunkCache
 				chunkData.Location.Y
 			);
 			
-			let newChunk = new Chunk(newChunkRef, chunkData.Size);
-			newChunk.lines = newChunk.lines.concat(chunkData.lines);
+			console.info(`Chunk Update @ ${newChunkRef}`)
 			
-			this.add(newChunk);
+			let newChunk = new Chunk(newChunkRef, chunkData.Size);
+			let newLines = chunkData.lines.map((line) => {
+				line.Points = line.Points.map((raw) => new Vector(raw.X, raw.Y));
+				return line;
+			});
+			newChunk.lines = newChunk.lines.concat(newLines);
+			
+			this.add(newChunk, true);
 		}
 	}
 }
diff --git a/Nibriboard/ClientFiles/ChunkReference.js b/Nibriboard/ClientFiles/ChunkReference.js
index fb1f44b..504828e 100644
--- a/Nibriboard/ClientFiles/ChunkReference.js
+++ b/Nibriboard/ClientFiles/ChunkReference.js
@@ -10,6 +10,16 @@ class ChunkReference extends Vector
 		this.planeName = inPlaneName;
 	}
 	
+	/**
+	 * Returns a plain Vector of this chunk reference in plane space.
+	 * @param  {number} chunkSize The size of the chunk this ChunkReference refers to.
+	 * @return {Vector}           This ChunkReference in plane space.
+	 */
+	inPlaneSpace(chunkSize)
+	{
+		return this.clone().multiply(chunkSize);
+	}
+	
 	toString()
 	{
 		return `ChunkReference: (${this.x}, ${this.y}, ${this.planeName})`;
