From 2e70e19defc4df6779d37111643dc027a7e7f071 Mon Sep 17 00:00:00 2001 From: Denneisk <20892685+Denneisk@users.noreply.github.com> Date: Thu, 23 Nov 2023 13:03:54 -0500 Subject: [PATCH 1/2] Vector cleanup --- .../gmod_wire_expression2/core/vector.lua | 56 +++++++------------ lua/wire/client/e2descriptions.lua | 1 + 2 files changed, 20 insertions(+), 37 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/core/vector.lua b/lua/entities/gmod_wire_expression2/core/vector.lua index e726bfcec7..a861e84cd4 100644 --- a/lua/entities/gmod_wire_expression2/core/vector.lua +++ b/lua/entities/gmod_wire_expression2/core/vector.lua @@ -13,26 +13,7 @@ local asin = math.asin local rad2deg = 180 / pi local deg2rad = pi / 180 --- Remove this when a later, mandatory update is made. --- These were added in the August (08) 9 (09) 2023 (23) update -if VERSION < 230809 then - function math.CubicBezier(frac, p0, p1, p2, p3) - local frac2 = frac * frac - local inv = 1 - frac - local inv2 = inv * inv - - return inv2 * inv * p0 + 3 * inv2 * frac * p1 + 3 * inv * frac2 * p2 + frac2 * frac * p3 - end - - function math.QuadraticBezier(frac, p0, p1, p2) - local frac2 = frac * frac - local inv = 1 - frac - local inv2 = inv * inv - - return inv2 * p0 + 2 * inv * frac * p1 + frac2 * p2 - end -end - +local LerpVector = LerpVector local quadraticBezier = math.QuadraticBezier local cubicBezier = math.CubicBezier @@ -215,9 +196,10 @@ end __e2setcost(5) --- Returns a random vector with its components between and -e2function vector randvec( normal min, normal max) - local range = max-min - return Vector(min+random()*range, min+random()*range, min+random()*range) +e2function vector randvec(number min, number max) + local v = Vector() + v:Random(min, max) + return v end --- Returns a random vector between and @@ -432,16 +414,12 @@ end __e2setcost(10) --- min/max based on vector length - returns shortest/longest vector -e2function vector min(vector rv1, vector rv2) - local length1 = ( rv1[1] * rv1[1] + rv1[2] * rv1[2] + rv1[3] * rv1[3] ) ^ 0.5 - local length2 = ( rv2[1] * rv2[1] + rv2[2] * rv2[2] + rv2[3] * rv2[3] ) ^ 0.5 - if length1 < length2 then return rv1 else return rv2 end +e2function vector min(vector vec1, vector vec2) + return vec1:LengthSqr() < vec2:LengthSqr() and vec1 or vec2 end -e2function vector max(vector rv1, vector rv2) - local length1 = ( rv1[1] * rv1[1] + rv1[2] * rv1[2] + rv1[3] * rv1[3] ) ^ 0.5 - local length2 = ( rv2[1] * rv2[1] + rv2[2] * rv2[2] + rv2[3] * rv2[3] ) ^ 0.5 - if length1 > length2 then return rv1 else return rv2 end +e2function vector max(vector vec1, vector vec2) +return vec1:LengthSqr() > vec2:LengthSqr() and vec1 or vec2 end --- component-wise min/max @@ -498,9 +476,13 @@ e2function vector clamp(vector value, vector min, vector max) return Vector(x, y, z) end ---- Mix two vectors by a given proportion (between 0 and 1) -e2function vector mix(vector vec1, vector vec2, ratio) - return vec1 * ratio + vec2 * (1 - ratio) +e2function vector lerp(vector from, vector to, fraction) + return LerpVector(fraction, from, to) +end + +[deprecated = "Use lerp instead"] +e2function vector mix(vector to, vector from, fraction) + return LerpVector(fraction, from, to) end e2function vector bezier(vector startVec, vector tangent, vector endVec, ratio) @@ -543,11 +525,11 @@ end __e2setcost(3) e2function angle vector:toAngle() - return Vector(this[1], this[2], this[3]):Angle() + return this:Angle() end e2function angle vector:toAngle(vector up) - return Vector(this[1], this[2], this[3]):AngleEx(up) + return this:AngleEx(up) end -------------------------------------------------------------------------------- @@ -654,7 +636,7 @@ end -------------------------------------------------------------------------------- -- Credits to Wizard of Ass for bearing(v,a,v) and elevation(v,a,v) -local ANG_ZERO = Angle(0, 0, 0) +local ANG_ZERO = angle_zero e2function number bearing(vector originpos, angle originangle, vector pos) pos = WorldToLocal(pos, ANG_ZERO, originpos, originangle) return rad2deg * -atan2(pos.y, pos.x) diff --git a/lua/wire/client/e2descriptions.lua b/lua/wire/client/e2descriptions.lua index 7ac68bd83c..d6043722a5 100644 --- a/lua/wire/client/e2descriptions.lua +++ b/lua/wire/client/e2descriptions.lua @@ -512,6 +512,7 @@ E2Helper.Descriptions["length(xv4:)"] = "Gets the length of the vector" E2Helper.Descriptions["length2(xv2:)"] = "Gets the squared length of the vector" E2Helper.Descriptions["length2(v:)"] = "Gets the squared length of the vector" E2Helper.Descriptions["length2(xv4:)"] = "Gets the squared length of the vector" +E2Helper.Descriptions["lerp(vvn)"] = "Performs linear interpolation. Returns a new value between 'from' and 'to', based on a 0-1 percentage ('fraction')" E2Helper.Descriptions["distance(xv2:xv2)"] = "Gets the distance between 2D vectors" E2Helper.Descriptions["distance(v:v)"] = "Gets the distance between vectors" E2Helper.Descriptions["distance(xv4:xv4)"] = "Gets the distance between 4D vectors" From bdcee2c5ee897600e22c3963bb4d689396b67bd8 Mon Sep 17 00:00:00 2001 From: Denneisk <20892685+Denneisk@users.noreply.github.com> Date: Thu, 23 Nov 2023 14:07:16 -0500 Subject: [PATCH 2/2] Fix indentation --- lua/entities/gmod_wire_expression2/core/vector.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/entities/gmod_wire_expression2/core/vector.lua b/lua/entities/gmod_wire_expression2/core/vector.lua index a861e84cd4..06a05f2e56 100644 --- a/lua/entities/gmod_wire_expression2/core/vector.lua +++ b/lua/entities/gmod_wire_expression2/core/vector.lua @@ -419,7 +419,7 @@ e2function vector min(vector vec1, vector vec2) end e2function vector max(vector vec1, vector vec2) -return vec1:LengthSqr() > vec2:LengthSqr() and vec1 or vec2 + return vec1:LengthSqr() > vec2:LengthSqr() and vec1 or vec2 end --- component-wise min/max