Skip to content

Commit 5bc1fe2

Browse files
authored
Vector changes (#60)
* LuaLS fmt * Remove the pool * Remove unused parameters * isfunction -> type() == "function" * Revert "isfunction" change :C
1 parent 7bb39c8 commit 5bc1fe2

File tree

1 file changed

+39
-72
lines changed

1 file changed

+39
-72
lines changed

source/lua/scripts/VectorFFI.lua

Lines changed: 39 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
-- setting x/y/z to a string that contains a number, will error, eg. v.x = "1" won't work but works in gmod
33
-- cannot grab the metatable of the vector, nor edit it, so you cannot add custom methods to the vector
44

5-
-- Collaboration between Raphael & Srlion (https://github.com/Srlion) <3
6-
7-
local POOL_SIZE = 20000
5+
-- Collaboration between Raphael & Srlion (https://github.com/Srlion) <3
86

97
local CreateVector, isvector
108

@@ -21,26 +19,14 @@ end
2119

2220
local type = type
2321
local tonumber = tonumber
24-
local table_remove = table.remove
25-
26-
local POOL = {}
2722

2823
local function Vector(x, y, z)
2924
-- Vector() in gmod, doesn't error for invalid arguments
3025
local vec = x
31-
x, y, z = tonumber(x) or 0, tonumber(y) or 0, tonumber(z) or 0
3226
if isvector(vec) then
33-
x = vec.x
34-
y = vec.y
35-
z = vec.z
36-
end
37-
38-
local v_pool = table_remove(POOL)
39-
if v_pool then
40-
v_pool.x, v_pool.y, v_pool.z = x, y, z
41-
return v_pool
27+
return CreateVector(vec.x, vec.y, vec.z)
4228
end
43-
return CreateVector(x, y, z)
29+
return CreateVector(tonumber(x) or 0, tonumber(y) or 0, tonumber(z) or 0)
4430
end
4531
_G.Vector = Vector
4632

@@ -71,11 +57,11 @@ local function check_num(value, arg_num, is_optional)
7157
return expect(value, "number", arg_num, is_optional)
7258
end
7359

74-
local function check_vec(value, arg_num, is_optional, is_optionalType)
60+
local function check_vec(value, arg_num, is_optional)
7561
return expect(value, "Vector", arg_num, is_optional)
7662
end
7763

78-
local function check_ang(value, arg_num, is_optional, is_optionalType)
64+
local function check_ang(value, arg_num, is_optional)
7965
return expect(value, "Angle", arg_num, is_optional)
8066
end
8167

@@ -361,40 +347,40 @@ function methods:Rotate(rotation) -- This was painful.
361347
local pitch_angle = rotation[1]
362348
local yaw_angle = rotation[2]
363349
local roll_angle = rotation[3]
364-
local radPitch = math.rad(pitch_angle)
365-
local radYaw = math.rad(yaw_angle)
366-
local radRoll = math.rad(roll_angle)
367-
local sinPitch = math.sin(radPitch)
368-
local cosPitch = math.cos(radPitch)
369-
local sinYaw = math.sin(radYaw)
370-
local cosYaw = math.cos(radYaw)
371-
local sinRoll = math.sin(radRoll)
372-
local cosRoll = math.cos(radRoll)
373-
local x = self.x
374-
local y = self.y
375-
local z = self.z
376-
local temp_x = x * cosPitch + z * sinPitch
377-
local temp_z = -x * sinPitch + z * cosPitch
378-
x = temp_x
379-
z = temp_z
380-
381-
temp_x = x * cosYaw - y * sinYaw
382-
local temp_y = x * sinYaw + y * cosYaw
383-
x = temp_x
384-
y = temp_y
385-
386-
temp_y = y * cosRoll - z * sinRoll
387-
temp_z = y * sinRoll + z * cosRoll
388-
y = temp_y
389-
z = temp_z
390-
391-
self.x = x
392-
self.y = y
393-
self.z = z
350+
local radPitch = math.rad(pitch_angle)
351+
local radYaw = math.rad(yaw_angle)
352+
local radRoll = math.rad(roll_angle)
353+
local sinPitch = math.sin(radPitch)
354+
local cosPitch = math.cos(radPitch)
355+
local sinYaw = math.sin(radYaw)
356+
local cosYaw = math.cos(radYaw)
357+
local sinRoll = math.sin(radRoll)
358+
local cosRoll = math.cos(radRoll)
359+
local x = self.x
360+
local y = self.y
361+
local z = self.z
362+
local temp_x = x * cosPitch + z * sinPitch
363+
local temp_z = -x * sinPitch + z * cosPitch
364+
x = temp_x
365+
z = temp_z
366+
367+
temp_x = x * cosYaw - y * sinYaw
368+
local temp_y = x * sinYaw + y * cosYaw
369+
x = temp_x
370+
y = temp_y
371+
372+
temp_y = y * cosRoll - z * sinRoll
373+
temp_z = y * sinRoll + z * cosRoll
374+
y = temp_y
375+
z = temp_z
376+
377+
self.x = x
378+
self.y = y
379+
self.z = z
394380
end
395381

396382
function methods:ToColor()
397-
return Color( self.x * 255, self.y * 255, self.z * 255, 255 )
383+
return Color(self.x * 255, self.y * 255, self.z * 255, 255)
398384
end
399385

400386
function methods:ToTable()
@@ -404,10 +390,10 @@ end
404390
function methods:WithinAABox(boxStart, boxEnd)
405391
check_vec(boxStart, 1)
406392
check_vec(boxEnd, 2)
407-
393+
408394
return self.x >= boxStart.x and self.x <= boxEnd.x and
409-
self.y >= boxStart.y and self.y <= boxEnd.y and
410-
self.z >= boxStart.z and self.z <= boxEnd.z
395+
self.y >= boxStart.y and self.y <= boxEnd.y and
396+
self.z >= boxStart.z and self.z <= boxEnd.z
411397
end
412398

413399
---@class Vector
@@ -442,25 +428,6 @@ do
442428
end
443429

444430
debug.setblocked(isvector)
445-
446-
local function initialize_vector_pool()
447-
local function add_to_pool(v)
448-
table.insert(POOL, v)
449-
ffi.gc(v, add_to_pool)
450-
end
451-
452-
for _ = 1, POOL_SIZE do
453-
local v = CreateVector(0, 0, 0)
454-
add_to_pool(v)
455-
end
456-
end
457-
458-
if timer.Simple then
459-
-- Lot's of addons cache Vectors when they load, and main reason for the pool is for temporary vectors, so we need to avoid them using the vectors inside the pool
460-
timer.Simple(0, initialize_vector_pool)
461-
else
462-
initialize_vector_pool()
463-
end
464431
end
465432

466433
jit.markFFITypeAsGmodUserData(Vector(1, 1, 1))

0 commit comments

Comments
 (0)