From 6d543937ae2e7321e2101373313bb46dd077cc15 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Fri, 15 Dec 2017 20:47:45 +0000 Subject: [PATCH] [server] Upgrade LocationReference to double to avoid awkward integer math problems in negative space --- Nibriboard/Client/NibriClient.cs | 6 +++--- Nibriboard/ClientFiles/Pencil.js | 6 ++---- Nibriboard/Program.cs | 7 +++++++ Nibriboard/RippleSpace/ChunkReference.cs | 2 +- Nibriboard/RippleSpace/LocationReference.cs | 8 ++++---- Nibriboard/RippleSpace/Reference.cs | 12 ++++++------ Nibriboard/Utilities/ChunkTools.cs | 4 ++-- Nibriboard/Utilities/LineSimplifier.cs | 6 +++--- Nibriboard/Utilities/Rectangle.cs | 18 +++++++++--------- Nibriboard/Utilities/Vector2.cs | 12 ++++++------ 10 files changed, 43 insertions(+), 38 deletions(-) diff --git a/Nibriboard/Client/NibriClient.cs b/Nibriboard/Client/NibriClient.cs index b18de53..f442166 100644 --- a/Nibriboard/Client/NibriClient.cs +++ b/Nibriboard/Client/NibriClient.cs @@ -343,8 +343,8 @@ namespace Nibriboard.Client List initialChunks = new List(); ChunkReference currentChunkRef = new ChunkReference( CurrentPlane, - CurrentViewPort.X / CurrentPlane.ChunkSize, - CurrentViewPort.Y / CurrentPlane.ChunkSize + (int)Math.Floor(CurrentViewPort.X / CurrentPlane.ChunkSize), + (int)Math.Floor(CurrentViewPort.Y / CurrentPlane.ChunkSize) ); while(CanSee(currentChunkRef)) { @@ -354,7 +354,7 @@ namespace Nibriboard.Client currentChunkRef = currentChunkRef.Clone() as ChunkReference; currentChunkRef.X++; } - currentChunkRef.X = CurrentViewPort.X; + currentChunkRef.X = (int)Math.Floor(CurrentViewPort.X / CurrentPlane.ChunkSize); currentChunkRef.Y++; } diff --git a/Nibriboard/ClientFiles/Pencil.js b/Nibriboard/ClientFiles/Pencil.js index 1d62c00..d993a6d 100644 --- a/Nibriboard/ClientFiles/Pencil.js +++ b/Nibriboard/ClientFiles/Pencil.js @@ -119,11 +119,9 @@ class Pencil if(!this.pencilDown) return; // Don't draw anything if the left mouse button isn't down - // The server only supports ints atm, so we have to round here :-( - // TODO: Lift this limit var nextPoint = new Vector( - Math.floor((event.clientX / this.boardWindow.viewport.zoomLevel) + this.boardWindow.viewport.x), - Math.floor((event.clientY / this.boardWindow.viewport.zoomLevel) + this.boardWindow.viewport.y) + (event.clientX / this.boardWindow.viewport.zoomLevel) + this.boardWindow.viewport.x, + (event.clientY / this.boardWindow.viewport.zoomLevel) + this.boardWindow.viewport.y ); this.unsentSegments.push(nextPoint); this.currentLineSegments.push(nextPoint); diff --git a/Nibriboard/Program.cs b/Nibriboard/Program.cs index 8127e6a..9bfc6de 100644 --- a/Nibriboard/Program.cs +++ b/Nibriboard/Program.cs @@ -1,6 +1,7 @@ using System; using System.Reflection; using System.Threading.Tasks; +using Nibriboard.RippleSpace; using SBRL.Utilities; namespace Nibriboard @@ -9,6 +10,12 @@ namespace Nibriboard { public static void Main(string[] args) { + Plane testPlane = new Plane(new PlaneInfo("test-plane", 512), "."); + LocationReference testReference = new LocationReference(testPlane, -300, -250); + Console.WriteLine(testReference); + Console.WriteLine(testReference.ContainingChunk); + return; + string packedRippleSpaceFile = "./default.ripplespace.zip"; for(int i = 0; i < args.Length; i++) diff --git a/Nibriboard/RippleSpace/ChunkReference.cs b/Nibriboard/RippleSpace/ChunkReference.cs index 5a38596..8044cd6 100644 --- a/Nibriboard/RippleSpace/ChunkReference.cs +++ b/Nibriboard/RippleSpace/ChunkReference.cs @@ -13,7 +13,7 @@ namespace Nibriboard.RippleSpace /// and obtained (A /// is returned). /// - public class ChunkReference : Reference + public class ChunkReference : Reference { public ChunkReference(Plane inPlane, int inX, int inY) : base(inPlane, inX, inY) { diff --git a/Nibriboard/RippleSpace/LocationReference.cs b/Nibriboard/RippleSpace/LocationReference.cs index 20b187b..d712fa1 100644 --- a/Nibriboard/RippleSpace/LocationReference.cs +++ b/Nibriboard/RippleSpace/LocationReference.cs @@ -7,7 +7,7 @@ namespace Nibriboard.RippleSpace /// /// Represents a location in absolute plane-space. /// - public class LocationReference : Reference + public class LocationReference : Reference { /// /// The chunk that this location reference fall inside. @@ -19,12 +19,12 @@ namespace Nibriboard.RippleSpace return new ChunkReference( Plane, - X / Plane.ChunkSize, - Y / Plane.ChunkSize + (int)Math.Floor(X / Plane.ChunkSize), + (int)Math.Floor(Y / Plane.ChunkSize) ); } } - public LocationReference(Plane inPlane, int inX, int inY) : base(inPlane, inX, inY) + public LocationReference(Plane inPlane, double inX, double inY) : base(inPlane, inX, inY) { } diff --git a/Nibriboard/RippleSpace/Reference.cs b/Nibriboard/RippleSpace/Reference.cs index e752f7f..75db4d6 100644 --- a/Nibriboard/RippleSpace/Reference.cs +++ b/Nibriboard/RippleSpace/Reference.cs @@ -8,7 +8,7 @@ namespace Nibriboard.RippleSpace /// An abstract class representing a coordinate reference to a location. /// [JsonObject(MemberSerialization.OptIn)] - public abstract class Reference : ICloneable + public abstract class Reference : ICloneable { public Plane Plane { get; set; } @@ -27,14 +27,14 @@ namespace Nibriboard.RippleSpace /// The x position of this reference. /// [JsonProperty] - public int X { get; set; } + public T X { get; set; } /// /// The y position of this reference. /// [JsonProperty] - public int Y { get; set; } + public T Y { get; set; } - public Reference(Plane inPlane, int inX, int inY) + public Reference(Plane inPlane, T inX, T inY) { Plane = inPlane; X = inX; Y = inY; @@ -43,8 +43,8 @@ namespace Nibriboard.RippleSpace /// Creates a new blank . /// Don't use this yourself! This is only for Newtonsoft.Json to use when deserialising references. /// - public Reference() - { + public Reference() { + } diff --git a/Nibriboard/Utilities/ChunkTools.cs b/Nibriboard/Utilities/ChunkTools.cs index cace6ea..0935bb0 100644 --- a/Nibriboard/Utilities/ChunkTools.cs +++ b/Nibriboard/Utilities/ChunkTools.cs @@ -26,8 +26,8 @@ namespace SBRL.Utilities { result.Add(new ChunkReference( plane, - currentLocation.X / plane.ChunkSize, - currentLocation.Y / plane.ChunkSize + (int)Math.Floor(currentLocation.X / plane.ChunkSize), + (int)Math.Floor(currentLocation.Y / plane.ChunkSize) )); currentLocation.X += plane.ChunkSize; diff --git a/Nibriboard/Utilities/LineSimplifier.cs b/Nibriboard/Utilities/LineSimplifier.cs index ba9d5d6..4cfae5a 100644 --- a/Nibriboard/Utilities/LineSimplifier.cs +++ b/Nibriboard/Utilities/LineSimplifier.cs @@ -16,12 +16,12 @@ namespace Nibriboard.Utilities while(true) { - float smallestArea = float.MaxValue; + double smallestArea = float.MaxValue; int smallestAreaI = 1; for(int i = 1; i < points.Count - 1; i++) { - float nextArea = TriangleArea(points[i - 1], points[i], points[i + 1]); + double nextArea = TriangleArea(points[i - 1], points[i], points[i + 1]); if(nextArea < smallestArea) { smallestArea = nextArea; smallestAreaI = i; @@ -39,7 +39,7 @@ namespace Nibriboard.Utilities return points; } - public static float TriangleArea(LocationReference a, LocationReference b, LocationReference c) + public static double TriangleArea(LocationReference a, LocationReference b, LocationReference c) { return Math.Abs( ( diff --git a/Nibriboard/Utilities/Rectangle.cs b/Nibriboard/Utilities/Rectangle.cs index 998d05d..6329ee6 100644 --- a/Nibriboard/Utilities/Rectangle.cs +++ b/Nibriboard/Utilities/Rectangle.cs @@ -25,20 +25,20 @@ namespace SBRL.Utilities /// /// The X coordinate of the rectangle. /// - public int X { get; set; } + public double X { get; set; } /// /// The Ycoordinateof the rectangle. /// /// The y. - public int Y { get; set; } + public double Y { get; set; } /// /// The width of the rectangle. /// - public int Width { get; set; } + public double Width { get; set; } /// /// The height of the rectangle. /// - public int Height { get; set; } + public double Height { get; set; } #endregion #region Corners @@ -85,7 +85,7 @@ namespace SBRL.Utilities /// The Y coordinate of the top of the rectangle. /// [JsonIgnore] - public int Top { + public double Top { get { return Y; } @@ -97,7 +97,7 @@ namespace SBRL.Utilities /// The Y coordinate of the bottom of the rectangle. /// [JsonIgnore] - public int Bottom { + public double Bottom { get { return Y + Height; } @@ -109,7 +109,7 @@ namespace SBRL.Utilities /// The X coordinate of the left side of the rectangle. /// [JsonIgnore] - public int Left { + public double Left { get { return X; } @@ -121,7 +121,7 @@ namespace SBRL.Utilities /// The X coordinate of the right side of the rectangle. /// [JsonIgnore] - public int Right { + public double Right { get { return X + Width; } @@ -131,7 +131,7 @@ namespace SBRL.Utilities } #endregion - public Rectangle(int x, int y, int width, int height) + public Rectangle(double x, double y, double width, double height) { X = x; Y = y; diff --git a/Nibriboard/Utilities/Vector2.cs b/Nibriboard/Utilities/Vector2.cs index ba18f68..e7f1579 100644 --- a/Nibriboard/Utilities/Vector2.cs +++ b/Nibriboard/Utilities/Vector2.cs @@ -3,7 +3,7 @@ namespace SBRL.Utilities { /// - /// Represents a single point in 2D space. + /// Represents a single podouble in 2D space. /// May also be used to represent a direction with a magnitude. /// /// v0.1 @@ -21,14 +21,14 @@ namespace SBRL.Utilities /// /// The X coordinate. /// - public int X { get; set; } + public double X { get; set; } /// /// The Y coordinate. /// - public int Y { get; set; } + public double Y { get; set; } - public Vector2(int x, int y) + public Vector2(double x, double y) { X = x; Y = y; @@ -49,14 +49,14 @@ namespace SBRL.Utilities Y - b.X ); } - public Vector2 Divide(int b) + public Vector2 Divide(double b) { return new Vector2( X / b, Y / b ); } - public Vector2 Multiply(int b) + public Vector2 Multiply(double b) { return new Vector2( X * b,