diff --git a/.tests/Vector3/add.test.lua b/.tests/Vector3/add.test.lua index 9eb1c6b..de2bae4 100644 --- a/.tests/Vector3/add.test.lua +++ b/.tests/Vector3/add.test.lua @@ -14,89 +14,74 @@ describe("Vector3.add", function() local a = Vector3.new(3, 4, 5) local b = Vector3.new(1, 1, 1) assert.are.same( - a:add(b), - Vector3.new(4, 5, 6) + Vector3.new(4, 5, 6), + a:add(b) ) end) it("should support the add operator", function() local a = Vector3.new(3, 4, 5) local b = Vector3.new(1, 1, 1) assert.are.same( - a + b, - Vector3.new(4, 5, 6) + Vector3.new(4, 5, 6), + a + b ) end) it("should work with floats", function() local a = Vector3.new(3.5, 4.5, 5.5) local b = Vector3.new(1.1, 1.1, 1.1) assert.are.same( - a + b, - Vector3.new(4.6, 5.6, 6.6) + Vector3.new(4.6, 5.6, 6.6), + a + b ) end) it("should work with scalar a", function() local a = 2 local b = Vector3.new(6, 7, 8) assert.are.same( - a + b, - Vector3.new(8, 9, 10) + Vector3.new(8, 9, 10), + a + b ) end) it("should work with scalar b", function() local a = Vector3.new(6, 7, 8) local b = 2 assert.are.same( - a + b, - Vector3.new(8, 9, 10) + Vector3.new(8, 9, 10), + a + b ) end) it("should handle negative b", function() local a = Vector3.new(3, 4, 5) local b = Vector3.new(-1, -1, -1) assert.are.same( - a + b, - Vector3.new(2, 3, 4) + Vector3.new(2, 3, 4), + a + b ) end) it("should handle negative a", function() local a = Vector3.new(-3, -4, -5) local b = Vector3.new(1, 1, 1) assert.are.same( - a + b, - Vector3.new(-2, -3, -4) + Vector3.new(-2, -3, -4), + a + b ) end) it("should handle negative a and b", function() local a = Vector3.new(-3, -4, -5) local b = Vector3.new(-1, -1, -1) assert.are.same( - a + b, - Vector3.new(-4, -5, -6) + Vector3.new(-4, -5, -6), + a + b ) end) - it("should throw an error on invalid x", function() - assert.has.errors(function() - Vector3.new("cheese", 4, 5) - end) - end) - it("should throw an error on invalid y", function() - assert.has.errors(function() - Vector3.new(4, "cheese", 5) - end) - end) - it("should throw an error on invalid z", function() - assert.has.errors(function() - Vector3.new(66, 2, "cheese") - end) - end) it("should return a new Vector3 instance", function() local a = Vector3.new(3, 4, 5) local b = Vector3.new(1, 1, 1) local result = a + b assert.are.same( - result, - Vector3.new(4, 5, 6) + Vector3.new(4, 5, 6), + result ) assert.are_not.equal(result, a) assert.are_not.equal(result, b) diff --git a/.tests/Vector3/area.test.lua b/.tests/Vector3/area.test.lua index 076c180..d593fd4 100644 --- a/.tests/Vector3/area.test.lua +++ b/.tests/Vector3/area.test.lua @@ -13,22 +13,22 @@ describe("Vector3.area", function() it("should work with a positive vector", function() local a = Vector3.new(3, 3, 3) assert.are.equal( - a:area(), - 27 + 27, + a:area() ) end) it("should work with a negative vector", function() local a = Vector3.new(-4, -4, -4) assert.are.equal( - a:area(), - -64 + -64, + a:area() ) end) it("should work with a mixed vector", function() local a = Vector3.new(-3, 3, -3) assert.are.equal( - a:area(), - 27 + 27, + a:area() ) end) end) diff --git a/.tests/Vector3/ceil.test.lua b/.tests/Vector3/ceil.test.lua index 9c2ce99..16ddd7e 100644 --- a/.tests/Vector3/ceil.test.lua +++ b/.tests/Vector3/ceil.test.lua @@ -13,15 +13,15 @@ describe("Vector3.ceil", function() it("should ceil a positive vector", function() local a = Vector3.new(3.1, 4.2, 5.8) assert.are.same( - a:ceil(), - Vector3.new(4, 5, 6) + Vector3.new(4, 5, 6), + a:ceil() ) end) it("should ceil a negative vector", function() local a = Vector3.new(-3.1, -4.2, -5.3) assert.are.same( - a:ceil(), - Vector3.new(-3, -4, -5) + Vector3.new(-3, -4, -5), + a:ceil() ) end) it("should work with integers", function() @@ -36,8 +36,8 @@ describe("Vector3.ceil", function() local result = a:ceil() assert.are.same( - result, - Vector3.new(4, 5, 6) + Vector3.new(4, 5, 6), + result ) assert.are_not.equal(result, a) end) diff --git a/.tests/Vector3/divide.test.lua b/.tests/Vector3/divide.test.lua index 6095e69..18c728f 100644 --- a/.tests/Vector3/divide.test.lua +++ b/.tests/Vector3/divide.test.lua @@ -5,64 +5,64 @@ describe("Vector3.divide", function() local a = Vector3.new(30, 40, 50) local b = Vector3.new(2, 2, 2) assert.are.same( - a:divide(b), - Vector3.new(15, 20, 25) + Vector3.new(15, 20, 25), + a:divide(b) ) end) it("should work with the div alias", function() local a = Vector3.new(30, 40, 50) local b = Vector3.new(2, 2, 2) assert.are.same( - a:div(b), - Vector3.new(15, 20, 25) + Vector3.new(15, 20, 25), + a:div(b) ) end) it("should work with scalar a", function() local a = 2 local b = Vector3.new(12, 14, 16) assert.are.same( - a / b, - Vector3.new(6, 7, 8) + Vector3.new(6, 7, 8), + a / b ) end) it("should work with scalar b", function() local a = Vector3.new(6, 8, 10) local b = 2 assert.are.same( - a / b, - Vector3.new(3, 4, 5) + Vector3.new(3, 4, 5), + a / b ) end) it("should support the divide operator", function() local a = Vector3.new(10, 12, 14) local b = Vector3.new(2, 3, 2) assert.are.same( - a / b, - Vector3.new(5, 4, 7) + Vector3.new(5, 4, 7), + a / b ) end) it("should handle negative b", function() local a = Vector3.new(30, 40, 50) local b = Vector3.new(-2, -2, -2) assert.are.same( - a / b, - Vector3.new(-15, -20, -25) + Vector3.new(-15, -20, -25), + a / b ) end) it("should handle negative a", function() local a = Vector3.new(-30, -40, -50) local b = Vector3.new(2, 4, 2) assert.are.same( - a / b, - Vector3.new(-15, -10, -25) + Vector3.new(-15, -10, -25), + a / b ) end) it("should handle negative a and b", function() local a = Vector3.new(-30, -40, -50) local b = Vector3.new(-2, -2, -2) assert.are.same( - a / b, - Vector3.new(15, 20, 25) + Vector3.new(15, 20, 25), + a / b ) end) it("should return a new Vector3 instance", function() @@ -71,8 +71,8 @@ describe("Vector3.divide", function() local result = a / b assert.are.same( - result, - Vector3.new(3, 4, 5) + Vector3.new(3, 4, 5), + result ) assert.are_not.equal(result, a) assert.are_not.equal(result, b) diff --git a/.tests/Vector3/floor.test.lua b/.tests/Vector3/floor.test.lua index 61f4c7c..4226475 100644 --- a/.tests/Vector3/floor.test.lua +++ b/.tests/Vector3/floor.test.lua @@ -13,22 +13,22 @@ describe("Vector3.floor", function() it("should floor a positive vector", function() local a = Vector3.new(3.1, 4.75, 5.9) assert.are.same( - a:floor(), - Vector3.new(3, 4, 5) + Vector3.new(3, 4, 5), + a:floor() ) end) it("should floor a negative vector", function() local a = Vector3.new(-3.1, -4.2, -5.3) assert.are.same( - a:floor(), - Vector3.new(-4, -5, -6) + Vector3.new(-4, -5, -6), + a:floor() ) end) it("should work with integers", function() local a = Vector3.new(3, 4, 5) assert.are.same( - a:floor(), - Vector3.new(3, 4, 5) + Vector3.new(3, 4, 5), + a:floor() ) end) it("should return a new Vector3 instance", function() @@ -36,8 +36,8 @@ describe("Vector3.floor", function() local result = a:floor() assert.are.same( - result, - Vector3.new(3, 4, 5) + Vector3.new(3, 4, 5), + result ) assert.are_not.equal(result, a) end) diff --git a/.tests/Vector3/multiply.test.lua b/.tests/Vector3/multiply.test.lua index d1a39d3..f133135 100644 --- a/.tests/Vector3/multiply.test.lua +++ b/.tests/Vector3/multiply.test.lua @@ -5,64 +5,64 @@ describe("Vector3.multiply", function() local a = Vector3.new(3, 4, 5) local b = Vector3.new(2, 2, 2) assert.are.same( - a:multiply(b), - Vector3.new(6, 8, 10) + Vector3.new(6, 8, 10), + a:multiply(b) ) end) it("should work with the mul alias", function() local a = Vector3.new(3, 4, 5) local b = Vector3.new(2, 2, 2) assert.are.same( - a:mul(b), - Vector3.new(6, 8, 10) + Vector3.new(6, 8, 10), + a:mul(b) ) end) it("should work with scalar a", function() local a = 2 local b = Vector3.new(6, 7, 8) assert.are.same( - a * b, - Vector3.new(12, 14, 16) + Vector3.new(12, 14, 16), + a * b ) end) it("should work with scalar b", function() local a = Vector3.new(6, 7, 8) local b = 2 assert.are.same( - a * b, - Vector3.new(12, 14, 16) + Vector3.new(12, 14, 16), + a * b ) end) it("should support the multiply operator", function() local a = Vector3.new(3, 4, 5) local b = Vector3.new(2, 2, 2) assert.are.same( - a * b, - Vector3.new(6, 8, 10) + Vector3.new(6, 8, 10), + a * b ) end) it("should handle negative b", function() local a = Vector3.new(3, 4, 5) local b = Vector3.new(-1, -1, -1) assert.are.same( - a * b, - Vector3.new(-3, -4, -5) + Vector3.new(-3, -4, -5), + a * b ) end) it("should handle negative a", function() local a = Vector3.new(-3, -4, -5) local b = Vector3.new(2, 2, 2) assert.are.same( - a * b, - Vector3.new(-6, -8, -10) + Vector3.new(-6, -8, -10), + a * b ) end) it("should handle negative a and b", function() local a = Vector3.new(-3, -4, -5) local b = Vector3.new(-2, -2, -2) assert.are.same( - a * b, - Vector3.new(6, 8, 10) + Vector3.new(6, 8, 10), + a * b ) end) it("should return a new Vector3 instance", function() @@ -71,8 +71,8 @@ describe("Vector3.multiply", function() local result = a * b assert.are.same( - result, - Vector3.new(9, 12, 15) + Vector3.new(9, 12, 15), + result ) assert.are_not.equal(result, a) assert.are_not.equal(result, b) diff --git a/.tests/Vector3/new.test.lua b/.tests/Vector3/new.test.lua index 91c044f..b74e5b1 100644 --- a/.tests/Vector3/new.test.lua +++ b/.tests/Vector3/new.test.lua @@ -3,8 +3,8 @@ local Vector3 = require("worldeditadditions.utils.vector3") describe("Vector3.add", function() it("should create a new Vector3", function() assert.are.same( - Vector3.new(3, 4, 5), - { x = 3, y = 4, z = 5 } + { x = 3, y = 4, z = 5 }, + Vector3.new(3, 4, 5) ) end) it("should throw an error on invalid x", function() diff --git a/.tests/Vector3/round.test.lua b/.tests/Vector3/round.test.lua index e997dae..9b673af 100644 --- a/.tests/Vector3/round.test.lua +++ b/.tests/Vector3/round.test.lua @@ -13,22 +13,22 @@ describe("Vector3.round", function() it("should round a positive vector", function() local a = Vector3.new(3.1, 4.75, 5.9) assert.are.same( - a:round(), - Vector3.new(3, 5, 6) + Vector3.new(3, 5, 6), + a:round() ) end) it("should round a negative vector", function() local a = Vector3.new(-3.1, -4.2, -5.3) assert.are.same( - a:round(), - Vector3.new(-3, -4, -5) + Vector3.new(-3, -4, -5), + a:round() ) end) it("should work with integers", function() local a = Vector3.new(3, 4, 5) assert.are.same( - a:round(), - Vector3.new(3, 4, 5) + Vector3.new(3, 4, 5), + a:round() ) end) it("should return a new Vector3 instance", function() @@ -36,8 +36,8 @@ describe("Vector3.round", function() local result = a:round() assert.are.same( - result, - Vector3.new(3, 5, 6) + Vector3.new(3, 5, 6), + result ) assert.are_not.equal(result, a) end) diff --git a/.tests/Vector3/snap_to.test.lua b/.tests/Vector3/snap_to.test.lua new file mode 100644 index 0000000..27028c4 --- /dev/null +++ b/.tests/Vector3/snap_to.test.lua @@ -0,0 +1,45 @@ +local Vector3 = require("worldeditadditions.utils.vector3") + +local function format_map(map) + local result = {} + for key, value in pairs(map) do + table.insert(result, key.."\t"..tostring(value)) + end + return table.concat(result, "\n") +end + + +describe("Vector3.snap_to", function() + it("should snap_to a positive vector", function() + local a = Vector3.new(3.1, 4.75, 5.9) + print("DEBUG "..tostring(a:snap_to(10))) + assert.are.same( + a:snap_to(10), + Vector3.new(0, 0, 10) + ) + end) + it("should snap_to a negative vector", function() + local a = Vector3.new(-2.5, -4.2, -5.3) + assert.are.same( + a:snap_to(6), + Vector3.new(0, -6, -6) + ) + end) + it("should work with integers", function() + local a = Vector3.new(3, 4, 5) + assert.are.same( + a:snap_to(3), + Vector3.new(3, 3, 6) + ) + end) + it("should return a new Vector3 instance", function() + local a = Vector3.new(3.1, 4.7, 5.99999) + + local result = a:snap_to(3) + assert.are.same( + result, + Vector3.new(3, 6, 6) + ) + assert.are_not.equal(result, a) + end) +end) diff --git a/.tests/Vector3/subtract.test.lua b/.tests/Vector3/subtract.test.lua index 58de460..c447c71 100644 --- a/.tests/Vector3/subtract.test.lua +++ b/.tests/Vector3/subtract.test.lua @@ -5,64 +5,64 @@ describe("Vector3.subtract", function() local a = Vector3.new(3, 4, 5) local b = Vector3.new(1, 1, 1) assert.are.same( - a:subtract(b), - Vector3.new(2, 3, 4) + Vector3.new(2, 3, 4), + a:subtract(b) ) end) it("should work with the sub alias", function() local a = Vector3.new(3, 4, 5) local b = Vector3.new(1, 1, 1) assert.are.same( - a:sub(b), - Vector3.new(2, 3, 4) + Vector3.new(2, 3, 4), + a:sub(b) ) end) it("should work with scalar a", function() local a = 2 local b = Vector3.new(6, 7, 8) assert.are.same( - a - b, - Vector3.new(4, 5, 6) + Vector3.new(4, 5, 6), + a - b ) end) it("should work with scalar b", function() local a = Vector3.new(6, 7, 8) local b = 2 assert.are.same( - a - b, - Vector3.new(4, 5, 6) + Vector3.new(4, 5, 6), + a - b ) end) it("should support the subtract operator", function() local a = Vector3.new(3, 4, 5) local b = Vector3.new(1, 1, 1) assert.are.same( - a - b, - Vector3.new(2, 3, 4) + Vector3.new(2, 3, 4), + a - b ) end) it("should handle negative b", function() local a = Vector3.new(3, 4, 5) local b = Vector3.new(-1, -1, -1) assert.are.same( - a - b, - Vector3.new(4, 5, 6) + Vector3.new(4, 5, 6), + a - b ) end) it("should handle negative a", function() local a = Vector3.new(-3, -4, -5) local b = Vector3.new(1, 1, 1) assert.are.same( - a - b, - Vector3.new(-4, -5, -6) + Vector3.new(-4, -5, -6), + a - b ) end) it("should handle negative a and b", function() local a = Vector3.new(-3, -4, -5) local b = Vector3.new(-1, -1, -1) assert.are.same( - a - b, - Vector3.new(-2, -3, -4) + Vector3.new(-2, -3, -4), + a - b ) end) it("should return a new Vector3 instance", function() @@ -71,8 +71,8 @@ describe("Vector3.subtract", function() local result = a - b assert.are.same( - result, - Vector3.new(2, 3, 4) + Vector3.new(2, 3, 4), + result ) assert.are_not.equal(result, a) assert.are_not.equal(result, b) diff --git a/.tests/Vector3/tostring.test.lua b/.tests/Vector3/tostring.test.lua index d8d3b0c..e46884a 100644 --- a/.tests/Vector3/tostring.test.lua +++ b/.tests/Vector3/tostring.test.lua @@ -13,36 +13,36 @@ describe("Vector3.__tostring", function() it("should stringify a Vector3", function() local a = Vector3.new(3, 4, 5) assert.are.same( - a:__tostring(), - "(3, 4, 5)" + "(3, 4, 5)", + a:__tostring() ) end) it("should implicitly stringify a Vector3", function() local a = Vector3.new(3, 4, 5) assert.are.same( - tostring(a), - "(3, 4, 5)" + "(3, 4, 5)", + tostring(a) ) end) it("should implicitly stringify another Vector3", function() local a = Vector3.new(55, 77, 22) assert.are.same( - tostring(a), - "(55, 77, 22)" + "(55, 77, 22)", + tostring(a) ) end) it("should handle negative numbers", function() local a = Vector3.new(-1, -2, -3) assert.are.same( - tostring(a), - "(-1, -2, -3)" + "(-1, -2, -3)", + tostring(a) ) end) it("should handle a mix of positive and negative numbers", function() local a = Vector3.new(-7, 2, -99) assert.are.same( - tostring(a), - "(-7, 2, -99)" + "(-7, 2, -99)", + tostring(a) ) end) end) diff --git a/worldeditadditions/utils/vector3.lua b/worldeditadditions/utils/vector3.lua index e57defa..239e59b 100644 --- a/worldeditadditions/utils/vector3.lua +++ b/worldeditadditions/utils/vector3.lua @@ -116,6 +116,16 @@ function Vector3.round(a) return Vector3.new(math.floor(a.x+0.5), math.floor(a.y+0.5), math.floor(a.z+0.5)) end + +--- Snaps this Vector3 to an imaginary square grid with the specified sized +-- squares. +-- @param a Vector3 The vector to operate on. +-- @param number grid_size The size of the squares on the imaginary grid to which to snap. +-- @returns Vector3 A new Vector3 instance snapped to an imaginary grid of the specified size. +function Vector3.snap_to(a, grid_size) + return a:round(a / grid_size) * grid_size +end + --- Returns the area of this vector. -- In other words, multiplies all the components together and returns a scalar value. -- @param a Vector3 The vector to return the area of.