Ripped a part utility folder and shoved it into a lib. Changed a lot of Utility prefixes to the modules name. This contains breaking changes. Scripts that use 0.2.0 will need to update to match the new format
This commit is contained in:
gononono64 committed 2025-02-05 17:42:50 -06:00
1 parent a08c22afed
commit 84480f8939
36 files changed
+2523 -97

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
local Actions = {}
Action = {}
if not IsDuplicityVersion() then goto client end
function Action.Fire(id, players, ...)
local action = Actions[id]
if not action then return end
if type(players) == "table" then
for _, player in ipairs(players) do
TriggerClientEvent(GetCurrentResourceName() .. "client:Action", tonumber(player), id, ...)
end
return
end
TriggerClientEvent(GetCurrentResourceName() .. "client:Action", tonumber(players or -1), id, ...)
end
if IsDuplicityVersion() then return end
::client::
function Action.Create(id, action)
assert(type(id) == "string", "id must be a string")
assert(type(action) == "function", "action must be a function")
Actions[id] = action
end
function Action.Remove(id)
Actions[id] = nil
end
function Action.Get(id)
return Actions[id]
end
function Action.GetAll()
return Actions
end
RegisterNetEvent(GetCurrentResourceName() .. "client:Action", function(id, ...)
local action = Actions[id]
if not action then return end
action(...)
end)
+146
View File
@@ -0,0 +1,146 @@
Callbacks = {}
cbData = {}
Callback = Callback or {}
local resource = GetCurrentResourceName() or 'unknown'
local clientToServerName = resource .. ':server:CS:TriggerCallback'
local clientToServerBackToClientName = resource .. ':client:CSC:TriggerCallback'
local serverToClientName = resource .. ':client:SC:TriggerCallback'
local serverToClientBackToServerName = resource .. ':server:SCS:TriggerCallback'
exports('Callback', Callback)
if not IsDuplicityVersion() then goto client end
-- -- -- -- -- -- -- -- -- --
-- ▄▀▀ ██▀ █▀▄ █ █ ██▀ █▀▄
-- ▄█▀ █▄▄ █▀▄ ▀▄▀ █▄▄ █▀▄
-- -- -- -- -- -- -- -- -- --
-- client to server rebound
function Callback.Register(name, callback, all)
Callback[name] = {
callback = callback,
toAll = all
}
end
RegisterNetEvent(clientToServerName, function(name, ...)
local src = source
local data = Callback[name]
if not data then return end
local func = data.callback
if not func then return end
local toAll = data.toAll and -1 or src
local cb = function(...)
TriggerClientEvent(clientToServerBackToClientName, toAll, name, ...)
end
func(src, cb, ...)
end)
-----------------------------
-- server to client rebound
function Callback.Trigger(name, src, func, ...)
if src == nil then src = -1 end
local p = promise.new()
cbData[name] = {cb = func, p = p}
if type(src) == 'table' then
for i, d in pairs(src) do
TriggerClientEvent(serverToClientName, tonumber(d), name, ...)
end
return Citizen.Await(p)
end
TriggerClientEvent(serverToClientName, tonumber(src), name, ...)
return Citizen.Await(p)
end
RegisterNetEvent(serverToClientBackToServerName, function(name, ...)
print('Rebound')
local data = cbData[name]
if not data then return end
local func = data.cb
if not func then return end
data.p:resolve(func(...) or true)
cbData[name] = nil
end)
-----------------------------
exports('RegisterCallback', Callback.Register)
exports('TriggerCallback', Callback.Trigger)
-- -- -- -- -- -- -- -- -- --
-- ▄▀▀ █ █ ██▀ █▄ █ ▀█▀
-- ▀▄▄ █▄▄ █ █▄▄ █ ▀█ █
-- -- -- -- -- -- -- -- -- --
if IsDuplicityVersion() then return Callback end
::client::
cbRebounds = {}
function Callback.Register(name, callback, all)
Callback[name] = {
callback = callback,
}
end
function Callback.RegisterRebound(name, callback)
cbRebounds[name] = callback
end
-- client to server rebound
function Callback.Trigger(name, func, ...)
local p = promise.new()
TriggerServerEvent(clientToServerName, name, ...)
cbData[name] = {cb = func, p = p}
CreateThread(function()
Wait(5000)
if cbData[name] then
cbData[name].p:resolve()
cbData[name] = nil
end
end)
print('Triggered')
return Citizen.Await(p)
end
RegisterNetEvent(clientToServerBackToClientName, function(name, ...)
local clientRebound = cbRebounds[name]
if clientRebound then
clientRebound(...)
end
local data = cbData[name]
if not data then return end
local func = data.cb
if not func then return end
data.p:resolve(func(...) or true)
cbData[name] = nil
end)
-----------------------------
-- server to client rebound
RegisterNetEvent(serverToClientName, function(name, ...)
local data = Callback[name]
if not data then return end
local func = data.callback
if not func then return end
local cb = function(...)
TriggerServerEvent(serverToClientBackToServerName, name, ...)
end
func(cb, ...)
end)
-----------------------------
exports('RegisterCallback', Callback.Register)
exports('RegisterRebound', Callback.RegisterRebound)
exports('TriggerCallback', Callback.Trigger)
return Callback
+44
View File
@@ -0,0 +1,44 @@
Ids = Ids or {}
-- for unique ids
Ids.CreateUniqueId = function(tbl, len, pattern) -- both optional
tbl = tbl or {} -- table to check uniqueness. Ids to check against must be the key to the tables value
len = len or 8
local id = ""
for i = 1, len do
local char = ""
if pattern then
char = pattern:sub(math.random(1, #pattern), math.random(1, #pattern))
else
char = math.random(1, 2) == 1 and string.char(math.random(65, 90)) or math.random(0, 9) -- CAP letter and number
end
id = id .. char
end
if tbl[id] then
return Ids.CreateUniqueId(tbl, len, pattern)
end
return id
end
Ids.RandomUpper = function(tbl, len)
return Ids.CreateUniqueId(tbl, len, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
end
Ids.RandomLower = function(tbl, len)
return Ids.CreateUniqueId(tbl, len, "abcdefghijklmnopqrstuvwxyz")
end
Ids.RandomString = function(tbl, len)
return Ids.CreateUniqueId(tbl, len, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
end
Ids.RandomNumber = function(tbl, len)
return Ids.CreateUniqueId(tbl, len, "0123456789")
end
Ids.Random = function(tbl, len)
return Ids.CreateUniqueId(tbl, len)
end
exports("Ids", Ids)
return Ids
+446
View File
@@ -0,0 +1,446 @@
LA = LA or {}
--https://easings.net
LA.Lerp = function(a, b, t)
return a + (b - a) * t
end
LA.LerpVector = function(a, b, t)
return vector3(LA.Lerp(a.x, b.x, t), LA.Lerp(a.y, b.y, t), LA.Lerp(a.z, b.z, t))
end
LA.EaseInSine = function(t)
return 1 - math.cos((t * math.pi) / 2)
end
LA.EaseOutSine = function(t)
return math.sin((t * math.pi) / 2)
end
LA.EaseInOutSine = function(t)
return -(math.cos(math.pi * t) - 1) / 2
end
LA.EaseInCubic = function(t)
return t ^ 3
end
LA.EaseOutCubic = function(t)
return 1 - (1 - t) ^ 3
end
LA.EaseInOutCubic = function(t)
return t < 0.5 and 4 * t ^ 3 or 1 - ((-2 * t + 2) ^ 3) / 2
end
LA.EaseInQuint = function(t)
return t ^ 5
end
LA.EaseOutQuint = function(t)
return 1 - (1 - t) ^ 5
end
LA.EaseInOutQuint = function(t)
return t < 0.5 and 16 * t ^ 5 or 1 - ((-2 * t + 2) ^ 5) / 2
end
LA.EaseInCirc = function(t)
return 1 - math.sqrt(1 - t ^ 2)
end
LA.EaseOutCirc = function(t)
return math.sqrt(1 - (t - 1) ^ 2)
end
LA.EaseInOutCirc = function(t)
return t < 0.5 and (1 - math.sqrt(1 - (2 * t) ^ 2)) / 2 or (math.sqrt(1 - (-2 * t + 2) ^ 2) + 1) / 2
end
LA.EaseInElastic = function(t)
return t == 0 and 0 or t == 1 and 1 or -2 ^ (10 * t - 10) * math.sin((t * 10 - 10.75) * (2 * math.pi) / 3)
end
LA.EaseOutElastic = function(t)
return t == 0 and 0 or t == 1 and 1 or 2 ^ (-10 * t) * math.sin((t * 10 - 0.75) * (2 * math.pi) / 3) + 1
end
LA.EaseInOutElastic = function(t)
return t == 0 and 0 or t == 1 and 1 or t < 0.5 and -(2 ^ (20 * t - 10) * math.sin((20 * t - 11.125) * (2 * math.pi) / 4.5)) / 2 or (2 ^ (-20 * t + 10) * math.sin((20 * t - 11.125) * (2 * math.pi) / 4.5)) / 2 + 1
end
LA.EaseInQuad = function(t)
return t ^ 2
end
LA.EaseOutQuad = function(t)
return 1 - (1 - t) ^ 2
end
LA.EaseInOutQuad = function(t)
return t < 0.5 and 2 * t ^ 2 or 1 - (-2 * t + 2) ^ 2 / 2
end
LA.EaseInQuart = function(t)
return t ^ 4
end
LA.EaseOutQuart = function(t)
return 1 - (1 - t) ^ 4
end
LA.EaseInOutQuart = function(t)
return t < 0.5 and 8 * t ^ 4 or 1 - ((-2 * t + 2) ^ 4) / 2
end
LA.EaseInExpo = function(t)
return t == 0 and 0 or 2 ^ (10 * t - 10)
end
LA.EaseOutExpo = function(t)
return t == 1 and 1 or 1 - 2 ^ (-10 * t)
end
LA.EaseInOutExpo = function(t)
return t == 0 and 0 or t == 1 and 1 or t < 0.5 and 2 ^ (20 * t - 10) / 2 or (2 - 2 ^ (-20 * t + 10)) / 2
end
LA.EaseInBack = function(t)
return 2.70158 * t ^ 3 - 1.70158 * t ^ 2
end
LA.EaseOutBack = function(t)
return 1 + 2.70158 * (t - 1) ^ 3 + 1.70158 * (t - 1) ^ 2
end
LA.EaseInOutBack = function(t)
return t < 0.5 and (2 * t) ^ 2 * ((1.70158 + 1) * 2 * t - 1.70158) / 2 or ((2 * t - 2) ^ 2 * ((1.70158 + 1) * (t * 2 - 2) + 1.70158) + 2) / 2
end
LA.EaseInBounce = function(t)
print(1 - LA.EaseOutBounce(1 - t))
return 1 - LA.EaseOutBounce(1 - t)
end
LA.EaseOutBounce = function(t)
if t < 1 / 2.75 then
return 7.5625 * t ^ 2
elseif t < 2 / 2.75 then
return 7.5625 * (t - 1.5 / 2.75) ^ 2 + 0.75
elseif t < 2.5 / 2.75 then
return 7.5625 * (t - 2.25 / 2.75) ^ 2 + 0.9375
else
return 7.5625 * (t - 2.625 / 2.75) ^ 2 + 0.984375
end
end
LA.EaseInOutBounce = function(t)
return t < 0.5 and (1 - LA.EaseOutBounce(1 - 2 * t)) / 2 or (1 + LA.EaseOutBounce(2 * t - 1)) / 2
end
LA.EaseIn = function(t, easingType)
easingType = string.lower(easingType)
if easingType == "linear" then
return t
elseif easingType == "sine" then
return LA.EaseInSine(t)
elseif easingType == "cubic" then
return LA.EaseInCubic(t)
elseif easingType == "quint" then
return LA.EaseInQuint(t)
elseif easingType == "circ" then
return LA.EaseInCirc(t)
elseif easingType == "elastic" then
return LA.EaseInElastic(t)
elseif easingType == "quad" then
return LA.EaseInQuad(t)
elseif easingType == "quart" then
return LA.EaseInQuart(t)
elseif easingType == "expo" then
return LA.EaseInExpo(t)
elseif easingType == "back" then
return LA.EaseInBack(t)
elseif easingType == "bounce" then
return LA.EaseInBounce(t)
end
end
LA.EaseOut = function(t, easingType)
easingType = string.lower(easingType)
if easingType == "linear" then
return t
elseif easingType == "sine" then
return LA.EaseOutSine(t)
elseif easingType == "cubic" then
return LA.EaseOutCubic(t)
elseif easingType == "quint" then
return LA.EaseOutQuint(t)
elseif easingType == "circ" then
return LA.EaseOutCirc(t)
elseif easingType == "elastic" then
return LA.EaseOutElastic(t)
elseif easingType == "quad" then
return LA.EaseOutQuad(t)
elseif easingType == "quart" then
return LA.EaseOutQuart(t)
elseif easingType == "expo" then
return LA.EaseOutExpo(t)
elseif easingType == "back" then
return LA.EaseOutBack(t)
elseif easingType == "bounce" then
return LA.EaseOutBounce(t)
end
end
LA.EaseInOut = function(t, easingType)
easingType = string.lower(easingType)
if easingType == "linear" then
return t
elseif easingType == "sine" then
return LA.EaseInOutSine(t)
elseif easingType == "cubic" then
return LA.EaseInOutCubic(t)
elseif easingType == "quint" then
return LA.EaseInOutQuint(t)
elseif easingType == "circ" then
return LA.EaseInOutCirc(t)
elseif easingType == "elastic" then
return LA.EaseInOutElastic(t)
elseif easingType == "quad" then
return LA.EaseInOutQuad(t)
elseif easingType == "quart" then
return LA.EaseInOutQuart(t)
elseif easingType == "expo" then
return LA.EaseInOutExpo(t)
elseif easingType == "back" then
return LA.EaseInOutBack(t)
elseif easingType == "bounce" then
return LA.EaseInOutBounce(t)
end
end
LA.EaseInVector = function(a, b, t, easingType)
local tEase = LA.EaseIn(t, easingType)
local x, y, z = a.x, a.y, a.z
local x2, y2, z2 = b.x, b.y, b.z
local x3, y3, z3 = x2 - x, y2 - y, z2 - z
local x4, y4, z4 = x3 * tEase, y3 * tEase, z3 * tEase
local x5, y5, z5 = x + x4, y + y4, z + z4
return vector3(x5, y5, z5)
end
LA.EaseOutVector = function(a, b, t, easingType)
local tEase = LA.EaseOut(t, easingType)
local x, y, z = a.x, a.y, a.z
local x2, y2, z2 = b.x, b.y, b.z
local x3, y3, z3 = x2 - x, y2 - y, z2 - z
local x4, y4, z4 = x3 * tEase, y3 * tEase, z3 * tEase
local x5, y5, z5 = x + x4, y + y4, z + z4
return vector3(x5, y5, z5)
end
LA.EaseInOutVector = function(a, b, t, easingType)
local tEase = LA.EaseInOut(t, easingType)
local x, y, z = a.x, a.y, a.z
local x2, y2, z2 = b.x, b.y, b.z
local x3, y3, z3 = x2 - x, y2 - y, z2 - z
local x4, y4, z4 = x3 * tEase, y3 * tEase, z3 * tEase
local x5, y5, z5 = x + x4, y + y4, z + z4
return vector3(x5, y5, z5)
end
LA.EaseVector = function(inout, a, b, t, easingType)
inout = string.lower(inout)
if inout == "in" then
return LA.EaseInVector(a, b, t, easingType)
elseif inout == "out" then
return LA.EaseOutVector(a, b, t, easingType)
elseif inout == "inout" then
return LA.EaseInOutVector(a, b, t, easingType)
end
Assert(false, "Invalid type")
end
LA.EaseOnAxis = function(inout, a, b, t, easingType, axis)
axis = axis or vector3(0, 0, 0)
local x, y, z = a.x, a.y, a.z
local increment = 0
if inout == "in" then
increment = LA.EaseIn(t, easingType)
elseif inout == "out" then
increment = LA.EaseOut(t, easingType)
elseif inout == "inout" then
increment = LA.EaseInOut(t, easingType)
end
return axis * increment
end
LA.TestCheck = function(point1, point2)
local dx = point1.x - point2.x
local dy = point1.y - point2.y
local dz = point1.z - point2.z
return math.sqrt(dx*dx + dy*dy + dz*dz)
end
LA.BoxZoneCheck = function(point, lower, upper)
local x1, y1, z1 = lower.x, lower.y, lower.z
local x2, y2, z2 = upper.x, upper.y, upper.z
return point.x > x1 and point.x < x2 and point.y > y1 and point.y < y2 and point.z > z1 and point.z < z2
end
LA.DistanceCheck = function(pointA, pointB)
return #(pointA - pointB) < 0.5
end
LA.Chance = function(chance)
Assert(chance, "Chance must be passed")
Assert(type(chance) == "number", "Chance must be a number")
Assert(chance >= 0 and chance <= 100, "Chance must be between 0 and 100")
return math.random(1, 100) <= chance
end
LA.Vector4To3 = function(vector4)
Assert(vector4, "Vector4 must be passed")
Assert(type(vector4) == "vector4", "Vector4 must be a vector4")
return vector3(vector4.x, vector4.y, vector4.z), vector4.w
end
LA.Dot = function(vectorA, vectorB)
return vectorA.x * vectorB.x + vectorA.y * vectorB.y + vectorA.z * vectorB.z
end
LA.Length = function(vector)
return math.sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z)
end
LA.Normalize = function(vector)
local length = LA.Length(vector)
return vector3(vector.x / length, vector.y / length, vector.z / length)
end
LA.Create2DRotationMatrix = function(angle) -- angle in radians
local c = math.cos(angle)
local s = math.sin(angle)
return {
c, -s, 0, 0,
s, c, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
}
end
LA.Create3DAxisRotationMatrix = function(vector)
local yaw = vector.z
local pitch = vector.x
local roll = vector.y
local cy = math.cos(yaw)
local sy = math.sin(yaw)
local cp = math.cos(pitch)
local sp = math.sin(pitch)
local cr = math.cos(roll)
local sr = math.sin(roll)
local matrix = {
vector3(cp * cy, cp * sy, -sp),
vector3(cy * sp * sr - cr * sy, sy * sp * sr + cr * cy, cp * sr),
vector3(cy * sp * cr + sr * sy, cr * cy * sp - sr * sy, cp * cr)
}
return matrix
end
LA.Circle = function(t, radius, center)
local x = radius * math.cos(t) + center.x
local y = radius * math.sin(t) + center.y
return vector3(x, y, center.z)
end
LA.Clamp = function(value, min, max)
return math.min(math.max(value, min), max)
end
LA.CrossProduct = function(a, b)
local x = a.y * b.z - a.z * b.y
local y = a.z * b.x - a.x * b.z
local z = a.x * b.y - a.y * b.x
return vector3(x, y, z)
end
LA.CreateTranslateMatrix = function(x, y, z)
return {
1, 0, 0, x,
0, 1, 0, y,
0, 0, 1, z,
0, 0, 0, 1,
}
end
LA.MultiplyMatrix = function(m1, m2)
local result = {}
for i = 1, 4 do
for j = 1, 4 do
local sum = 0
for k = 1, 4 do
sum = sum + m1[(i - 1) * 4 + k] * m2[(k - 1) * 4 + j]
end
result[(i - 1) * 4 + j] = sum
end
end
return result
end
LA.MultiplyMatrixByVector = function(m, v)
local result = {}
for i = 1, 4 do
local sum = 0
for j = 1, 4 do
sum = sum + m[(i - 1) * 4 + j] * v[j]
end
result[i] = sum
end
return result
end
exports('LA', LA)
return LA
-- local easingTypes = {
-- "linear",
-- "sine",
-- "cubic",
-- "quint",
-- "circ",
-- "elastic",
-- "quad",
-- "quart",
-- "expo",
-- "back",
-- "bounce"
-- }
--local inout = {
-- "in",
-- "out",
-- "inout"
--}
-- LA.LerpAngle = function(a, b, t)
-- local num = math.abs(b - a) % 360
-- local num2 = 360 - num
-- if num < num2 then
-- return a + num * t
-- else
-- return a - num2 * t
-- end
-- end
-- LA.LerpVectorAngle = function(a, b, t)
-- local x, y, z = a.x, a.y, a.z
-- local x2, y2, z2 = b.x, b.y, b.z
-- local x3, y3, z3 = LA.LerpAngle(x, x2, t), LA.LerpAngle(y, y2, t), LA.LerpAngle(z, z2, t)
-- return vector3(x3, y3, z3)
-- end
-- return LA
+50
View File
@@ -0,0 +1,50 @@
Math = {}
function Math.Clamp(value, min, max)
return math.min(math.max(value, min), max)
end
function Math.Remap(value, min, max, newMin, newMax)
return newMin + (value - min) / (max - min) * (newMax - newMin)
end
function Math.PointInRadius(radius)
local angle = math.rad(math.random(0, 360))
return vector2(radius * math.cos(angle), radius * math.sin(angle))
end
function Math.Normalize(value, min, max)
if max == min then return 0 end -- Avoid division by zero
return (value - min) / (max - min)
end
function Math.Normalize2D(x, y)
if type(x) == "vector2" then
x, y = x.x, x.y
end
local length = math.sqrt(x*x + y*y)
return length ~= 0 and vector2(x / length, y / length) or vector2(0, 0)
end
function Math.Normalize3D(x, y, z)
if type(x) == "vector3" then
x, y, z = x.x, x.y, x.z
end
local length = math.sqrt(x*x + y*y + z*z)
return length ~= 0 and vector3(x / length, y / length, z / length) or vector3(0, 0, 0)
end
function Math.Normalize4D(x, y, z, w)
if type(x) == "vector4" then
x, y, z, w = x.x, x.y, x.z, x.w
end
local length = math.sqrt(x*x + y*y + z*z + w*w)
return length ~= 0 and vector4(x / length, y / length, z / length, w / length) or vector4(0, 0, 0, 0)
end
function Math.DirectionToTarget(fromV3, toV3)
return Math.Normalize3D(toV3.x - fromV3.x, toV3.y - fromV3.y, toV3.z - fromV3.z)
end
exports('Math', Math)
return Math
+110
View File
@@ -0,0 +1,110 @@
local Perlin = {}
-- Permutation table for random gradients
local perm = {}
local grad3 = {
{1,1,0}, {-1,1,0}, {1,-1,0}, {-1,-1,0},
{1,0,1}, {-1,0,1}, {1,0,-1}, {-1,0,-1},
{0,1,1}, {0,-1,1}, {0,1,-1}, {0,-1,-1}
}
-- Initialize permutation table
local function ShufflePermutation()
local p = {}
for i = 0, 255 do
p[i] = i
end
for i = 255, 1, -1 do
local j = math.random(i + 1) - 1
p[i], p[j] = p[j], p[i]
end
for i = 0, 255 do
perm[i] = p[i]
perm[i + 256] = p[i] -- Repeat for wrapping
end
end
ShufflePermutation() -- Randomize on load
-- Dot product helper
local function Dot(g, x, y, z)
return g[1] * x + g[2] * y + (z and g[3] * z or 0)
end
-- Fade function (smootherstep)
local function Fade(t)
return t * t * t * (t * (t * 6 - 15) + 10)
end
-- Linear interpolation
local function Lerp(a, b, t)
return a + (b - a) * t
end
-- **Perlin Noise 1D**
function Perlin.Noise1D(x)
local X = math.floor(x) & 255
x = x - math.floor(x)
local u = Fade(x)
local a = perm[X]
local b = perm[X + 1]
return Lerp(a, b, u) * (2 / 255) - 1
end
-- **Perlin Noise 2D**
function Perlin.Noise2D(x, y)
local X = math.floor(x) & 255
local Y = math.floor(y) & 255
x, y = x - math.floor(x), y - math.floor(y)
local u, v = Fade(x), Fade(y)
local aa = perm[X] + Y
local ab = perm[X] + Y + 1
local ba = perm[X + 1] + Y
local bb = perm[X + 1] + Y + 1
return Lerp(
Lerp(Dot(grad3[perm[aa] % 12 + 1], x, y), Dot(grad3[perm[ba] % 12 + 1], x - 1, y), u),
Lerp(Dot(grad3[perm[ab] % 12 + 1], x, y - 1), Dot(grad3[perm[bb] % 12 + 1], x - 1, y - 1), u),
v
)
end
-- **Perlin Noise 3D**
function Perlin.Noise3D(x, y, z)
local X = math.floor(x) & 255
local Y = math.floor(y) & 255
local Z = math.floor(z) & 255
x, y, z = x - math.floor(x), y - math.floor(y), z - math.floor(z)
local u, v, w = Fade(x), Fade(y), Fade(z)
local aaa = perm[X] + Y + Z
local aba = perm[X] + Y + Z + 1
local aab = perm[X] + Y + 1 + Z
local abb = perm[X] + Y + 1 + Z + 1
local baa = perm[X + 1] + Y + Z
local bba = perm[X + 1] + Y + Z + 1
local bab = perm[X + 1] + Y + 1 + Z
local bbb = perm[X + 1] + Y + 1 + Z + 1
return Lerp(
Lerp(
Lerp(Dot(grad3[perm[aaa] % 12 + 1], x, y, z), Dot(grad3[perm[baa] % 12 + 1], x - 1, y, z), u),
Lerp(Dot(grad3[perm[aab] % 12 + 1], x, y - 1, z), Dot(grad3[perm[bab] % 12 + 1], x - 1, y - 1, z), u),
v
),
Lerp(
Lerp(Dot(grad3[perm[aba] % 12 + 1], x, y, z - 1), Dot(grad3[perm[bba] % 12 + 1], x - 1, y, z - 1), u),
Lerp(Dot(grad3[perm[abb] % 12 + 1], x, y - 1, z - 1), Dot(grad3[perm[bbb] % 12 + 1], x - 1, y - 1, z - 1), u),
v
),
w
)
end
exports('Perlin', Perlin)
return Perlin
+25
View File
@@ -0,0 +1,25 @@
Prints = Prints or {}
local function printMessage(level, color, message)
if type(message) == 'table' then
message = json.encode(message)
end
print(color .. '[' .. level .. ']:', message)
end
Prints.Info = function(message)
printMessage('INFO', '^5', message)
end
Prints.Warn = function(message)
printMessage('WARN', '^3', message)
end
Prints.Error = function(message)
printMessage('ERROR', '^1', message)
end
Prints.Debug = function(message)
printMessage('DEBUG', '^2', message)
end
return Prints
+469
View File
@@ -0,0 +1,469 @@
-- -- -- -- -- -- -- -- -- --
-- ▄▀▀ █▄█ ▄▀▄ █▀▄ ██▀ █▀▄ --
-- ▄█▀ █ █ █▀█ █▀▄ █▄▄ █▄▀ --
-- -- -- -- -- -- -- -- -- --
local Entities = {}
local isServer = IsDuplicityVersion()
local registeredFunctions = {}
ReboundEntities = {}
--- Global Add function which fire when an entity is registered
--- @param func function = function(entityData: table)
--- @return number = ID
function ReboundEntities.AddFunction(func)
assert(func, "Func is nil")
table.insert(registeredFunctions, func)
return #registeredFunctions
end
--- Remove a registered function
--- @param id number
function ReboundEntities.RemoveFunction(id)
assert(id, "ID is nil")
assert(registeredFunctions[id], "ID not found")
registeredFunctions[id] = false
end
--- Internal: Fire all registered functions
--- @vararg any
local function FireFunctions(...)
for k, v in pairs(registeredFunctions) do
if v then
v(...)
end
end
end
--- Convert a table to an entityData object
--- @param entityData table = {model: string, position: vector3, id: string || nil}
--- @return table = {model: string, position: vector3, id: string, entity: number, isServer: boolean || nil}
function ReboundEntities.Register(entityData)
assert(entityData, "Entity data is nil")
local position = entityData.position
assert(position, "Position is nil")
local id = entityData.id or Ids.CreateUniqueId(Entities)
entityData.isServer = isServer
entityData.id = tostring(id)
local entityData = setmetatable(entityData, {
__tostring = function()
return entityData.id
end
})
Entities[id] = entityData
FireFunctions(entityData)
return entityData
end
-- Internal
local function Unregister(id)
Entities[id] = nil
end
function ReboundEntities.GetSyncData(entityData, key)
return entityData[key]
end
--- Get all entityDatas registered
--- @return table = {entityData: table}
function ReboundEntities.GetAll()
return Entities
end
--- Get an entityData by its id
--- @param id string
--- @return table = {model: string, position: vector3, id: string, entity: number, isServer: boolean}
function ReboundEntities.GetById(id)
id = tostring(id)
return Entities[id]
end
--- Get all entities by their model
--- @param model string
--- @return table = {entityData: table}
function ReboundEntities.GetByModel(model)
local entities = {}
for k, v in pairs(Entities) do
if v.model == model then
table.insert(entities, v)
end
end
return entities
end
--- Get the closest entityData to a position
--- @param pos vector3
--- @return table = {model: string, position: vector3, id: string, entity: number, isServer: boolean}
function ReboundEntities.GetClosest(pos)
local closest = nil
local closestDist = 9999
for k, v in pairs(Entities) do
local dist = #(pos - v.pos)
if dist < closestDist then
closest = v
closestDist = dist
end
end
return closest
end
--- Get all entities within a radius of a position
--- @param pos vector3
--- @param radius number
--- @return table = {entityData: table}
function ReboundEntities.GetWithinRadius(pos, radius)
local entities = {}
for k, v in pairs(Entities) do
local dist = #(pos - v.pos)
if dist < radius then
table.insert(entities, v)
end
end
return entities
end
function ReboundEntities.SetOnSyncKeyChange(entityData, cb)
print("SetOnSyncKeyChange", entityData, cb)
local id = entityData.id or entityData
local data = ReboundEntities.GetById(id)
assert(data, "Entity not found")
data.onSyncKeyChange = cb
end
exports('ReboundEntities', ReboundEntities)
if not isServer then goto client end
-- -- -- -- -- -- -- -- -- --
-- ▄▀▀ ██▀ █▀▄ █ █ ██▀ █▀▄ --
-- ▄█▀ █▄▄ █▀▄ ▀▄▀ █▄▄ █▀▄ --
-- -- -- -- -- -- -- -- -- --
--- Create a rebound entity
--- @param entityData object = {model: string, position: vector3, rotation: vector3 || nil}
--- @param src number = Who to send to. Defaults: -1
--- @return table = {model: string, position: vector3, rotation: vector3, id: string, entity: number, isServer: boolean}
function ReboundEntities.Create(entityData, src)
local entity = ReboundEntities.Register(entityData)
assert(entity, "Failed to register entity")
assert(entity.position, "Entity position is nil")
entity.rotation = entity.rotation or vector3(0, 0, 0)
src = src and tonumber(src) or -1
TriggerClientEvent(GetCurrentResourceName() ..":client:CreateReboundEntity", src, entity)
return entity
end
function ReboundEntities.SetCheckRestricted(entityData, cb)
assert(type(cb) == "function", "Check restricted is not a function")
entityData.restricted = cb
end
function ReboundEntities.CheckRestricted(src, entityData)
src = tonumber(src)
if not entityData.restricted then return false end
if src == -1 then return true end
return entityData.restricted(src, entityData) or false
end
function ReboundEntities.Refresh(src)
src= tonumber(src)
local list = ReboundEntities.GetAccessibleList(src)
TriggerClientEvent(GetCurrentResourceName() ..":client:CreateReboundEntities", src, list)
end
--- Delete a rebound entity
--- @param id string = The entity id: string || entityData: object
--- @return boolean
function ReboundEntities.Delete(id)
Unregister(id)
TriggerClientEvent(GetCurrentResourceName() ..":client:DeleteReboundEntity", -1, id)
end
-- Create multiple rebound entities
--- @param entityDatas table = {entityData: object}
--- @param src number = Who to send to. Defaults: -1
--- @return table = {entityData: object}
function ReboundEntities.CreateMultiple(entityDatas, src, restricted)
src = src and tonumber(src) or -1
local _entityDatas = {}
for k, v in pairs(entityDatas) do
local entity = ReboundEntities.Register(v)
assert(entity, "Failed to register entity")
assert(entity.position, "Entity position is nil")
entity.rotation = entity.rotation or vector3(0, 0, 0)
if restricted then
ReboundEntities.SetCheckRestricted(entity, restricted)
end
if not ReboundEntities.CheckRestricted(src, entity) then
_entityDatas[entity.id] = entity
end
end
TriggerClientEvent(GetCurrentResourceName() ..":client:CreateReboundEntities", src, _entityDatas)
return _entityDatas
end
--- Sets data to be synced to clients for a rebound entity
--- @param entityData object = {model: string, position: vector3, rotation: vector3, id: string, entity: number, isServer: boolean}
--- @param key string
--- @param value any
function ReboundEntities.SetSyncData(entityData, key, value)
entityData[key] = value
if entityData.onSyncKeyChange then
entityData.onSyncKeyChange(entityData, key, value)
end
TriggerClientEvent(GetCurrentResourceName() ..":client:SetReboundSyncData", -1, entityData.id, key, value)
end
function ReboundEntities.GetAccessibleList(src)
src = tonumber(src)
local list = {}
for k, data in pairs(ReboundEntities.GetAll()) do
if not ReboundEntities.CheckRestricted(src, data) then
list[data.id] = data
end
end
return list
end
RegisterNetEvent("playerJoining", function(src, _)
ReboundEntities.Refresh(src)
end)
AddEventHandler('onResourceStart', function(resource)
if resource == GetCurrentResourceName() then
Wait(1000)
local players = GetPlayers()
for k, src in pairs(players) do
ReboundEntities.Refresh(src)
end
end
end)
::client::
if isServer then return ReboundEntities end
-- -- -- -- -- -- -- -- -- --
-- ▄▀▀ █ █ ██▀ █▄ █ ▀█▀ --
-- ▀▄▄ █▄▄ █ █▄▄ █ ▀█ █ --
-- -- -- -- -- -- -- -- -- --
--- Loads a model into memory
--- @param model : Model name <string> || Model hash <number>
--- @return modelHash : number
function ReboundEntities.LoadModel(model)
assert(model, "Model is nil")
model = type(model) == "number" and model or GetHashKey(model)
local timeout = 100
RequestModel(model)
while not HasModelLoaded(model) and timeout > 0 do
timeout = timeout - 1
Wait(100)
end
assert(timeout > 0, string.format("Failed to load model %s", model))
return model
end
--- Spawns an entity
--- @param entityData : table = {model: string, position: vector3, rotation: vector3 || nil, onSpawn: function || nil}
--- @return entity : number
function ReboundEntities.Spawn(entityData)
local model = entityData.model
local position = entityData.position
assert(position, "Position is nil")
local rotation = entityData.rotation or vector3(0, 0, 0)
local entity = nil
if model then
model = ReboundEntities.LoadModel(model)
entity = CreateObject(model, position.x, position.y, position.z, false, false, false)
SetEntityRotation(entity, rotation.x, rotation.y, rotation.z)
end
local onSpawn = entityData.onSpawn
print(onSpawn)
if onSpawn then
onSpawn(entityData, entity)
end
-- dont forget to save the entity to the entityData table in the spawn loop
return entity, true
end
--- Set the onSpawn callback for an entity. Triggered after the entity is created.
--- @param id : string || entityData : object
--- @param cb : function(entityData: object, entity: number)
--- @return success : boolean
function ReboundEntities.SetOnSpawn(id, cb)
id = tostring(id)
local entity = ReboundEntities.GetById(id)
assert(entity, "Entity not found")
entity.onSpawn = cb
return true
end
--- Despawns an entity
--- @param entityData : entity || table = {entity: number, onDespawn: function || nil}
function ReboundEntities.Despawn(entityData)
assert(entityData, "Entity data is nil")
local entity = entityData.entity
local onDespawn = entityData.onDespawn
if onDespawn and type(onDespawn) == "function" then
onDespawn(entityData, entity)
end
if entity and DoesEntityExist(entity) then
DeleteEntity(entity)
end
return nil, nil
end
--- Set the onDespawn callback for an entity. Triggered before the entity is deleted.
--- @param id : string || entityData : object
--- @param cb : function(entityData: object, entity: number)
--- @return success : boolean
function ReboundEntities.SetOnDespawn(id, cb)
id = tostring(id)
local entity = ReboundEntities.GetById(id)
assert(entity, "Entity not found")
entity.onDespawn = cb
return true
end
local spawnLoopRunning = false
--- Internal function to spawn entities in a loop
--- @param distanceToCheck : number || nil
--- @param waitTime : number || nil
function ReboundEntities.SpawnLoop(distanceToCheck, waitTime)
if spawnLoopRunning then return end
spawnLoopRunning = true
distanceToCheck = distanceToCheck or 50
waitTime = waitTime or 2500
CreateThread(function()
while spawnLoopRunning do
for k, v in pairs(Entities) do
if not v.restricted then
local pos = GetEntityCoords(PlayerPedId())
local dist = #(pos - v.position)
if dist <= distanceToCheck and (not v.entity and not v.inRange) then
v.entity, v.inRange = ReboundEntities.Spawn(v)
elseif dist > distanceToCheck and (v.entity or v.inRange) then
v.entity, v.inRange = ReboundEntities.Despawn(v)
end
elseif v.entity then
v.entity = ReboundEntities.Despawn(v)
end
end
Wait(waitTime)
end
end)
end
--- Get an entity by its entity number
--- @param entity : number
--- @return entityData : table
function ReboundEntities.GetByEntity(entity)
for k, v in pairs(Entities) do
if v.entity == entity then
return v
end
end
end
--- Create a client side only, rebound entity
--- @param entityData : table = {model: string, position: vector3, rotation: vector3 || nil}
--- @return entity : table
function ReboundEntities.CreateClient(entityData)
local entity = ReboundEntities.Register(entityData)
if not entity then return end
ReboundEntities.SpawnLoop()
return entity
end
--- Delete a client side only, rebound entity
--- @param id : string || entityData : table
--- @return success : boolean
function ReboundEntities.DeleteClient(id)
id = tostring(id)
local entityData = ReboundEntities.GetById(id)
local isServer = entityData.isServer
assert(not isServer, "Cannot delete server entity from client")
local entity = entityData.entity
if entity then
ReboundEntities.Despawn(entityData)
end
Unregister(id)
return true
end
--- Create multiple client only, rebound entities
--- @param entityDatas : table = {entityData: table = {model: string, position: vector3, rotation: vector3 || nil}}
--- @return _entityDatas : objects
function ReboundEntities.CreateMultipleClient(entityDatas)
local _entityDatas = {}
for k, v in pairs(entityDatas) do
local entityData = ReboundEntities.CreateClient(v)
entityDatas[entityData.id] = entityData
end
return _entityDatas
end
--- Delete multiple client only, rebound entities
--- @param ids : table = {string || entityData: table}
function ReboundEntities.DeleteMultipleClient(ids)
for k, v in pairs(ids) do
ReboundEntities.DeleteClient(v)
end
end
-- -- -- -- internal -- -- -- --
local function DeleteFromServer(id)
id = tostring(id)
local entityData = ReboundEntities.GetById(id)
if not entityData then return end
entityData.isServer = nil
ReboundEntities.DeleteClient(id)
end
local function DeleteMultipleFromServer(ids)
for k, v in pairs(ids) do
DeleteFromServer(v)
end
end
-- -- -- -- -- -- -- -- -- --
RegisterNetEvent(GetCurrentResourceName() ..":client:CreateReboundEntity", function(entityData)
ReboundEntities.CreateClient(entityData)
end)
RegisterNetEvent(GetCurrentResourceName() ..":client:DeleteReboundEntity", function(id)
DeleteFromServer(id)
end)
RegisterNetEvent(GetCurrentResourceName() ..":client:CreateReboundEntities", function(entityDatas)
ReboundEntities.CreateMultipleClient(entityDatas)
end)
RegisterNetEvent(GetCurrentResourceName() ..":client:DeleteReboundEntities", function(ids)
DeleteMultipleFromServer(ids)
end)
RegisterNetEvent(GetCurrentResourceName() ..":client:SetReboundSyncData", function(id, key, value)
local entityData = ReboundEntities.GetById(id)
if not entityData then return end
entityData[key] = value
if entityData.onSyncKeyChange then
entityData.onSyncKeyChange(entityData, key, value)
end
end)
AddEventHandler('onResourceStop', function(resource)
if resource == GetCurrentResourceName() then
spawnLoopRunning = false
for k, v in pairs(Entities) do
if v.entity then
DeleteEntity(tonumber(v.entity))
end
end
end
end)
return ReboundEntities
+35
View File
@@ -0,0 +1,35 @@
local ReboundEntities = ReboundEntities or Require("shared/rebound_entities.lua")
local Actions = Actions or Require("shared/actions.lua")
local LA = LA or Require("shared/la.lua")
REA = {}
if not IsDuplicityVersion() then goto client end
if IsDuplicityVersion() then return end
::client::
local ActionsInProgress = {}
Action.Create("LerpToPosition", function(reboundId, start, position, duration, shouldRepeat, overrideStartTime)
local re = ReboundEntities.Get(reboundId)
assert(re, "Rebound entity not found")
local entity = re and re.entity
if not entity then return end
local startTime = GetGameTimer()
local endTime = overrideStartTime or startTime + duration
ActionsInProgress[reboundId] = {reboundId, start, position, duration, shouldRepeat, startTime}
local t = 0
CreateThread(function()
while shouldRepeat or t < 1 do
t = (GetGameTimer() - startTime) / duration
local lerpPosition = LA.LerpVector(start, position, t)
SetEntityCoords(entity, lerpPosition)
Wait(0)
end
end)
end)
+109
View File
@@ -0,0 +1,109 @@
Table = {}
Table.CheckPopulated = function(tbl)
if #tbl == 0 then
for _, _ in pairs(tbl) do
return true
end
return false
end
return true
end
Table.DeepClone = function(tbl, out, omit)
if type(tbl) ~= "table" then return tbl end
local new = out or {}
omit = omit or {}
for key, data in pairs(tbl) do
if not omit[key] then
if type(data) == "table" then
new[key] = Table.DeepClone(data)
else
new[key] = data
end
end
end
return new
end
Table.TableContains = function(tbl, search, nested)
for _, v in pairs(tbl) do
if nested and type(v) == "table" then
return Table.TableContains(v, search)
elseif v == search then
return true, v
end
end
return false
end
Table.TableContainsKey = function(tbl, search)
for k, _ in pairs(tbl) do
if k == search then
return true, k
end
end
return false
end
Table.GetClosest = function(coords, tbl)
local closestPoint = nil
local dist = math.huge
for k, v in pairs(tbl) do
local c = v.coords
local d = c and #(coords - c)
if d < dist then
dist = d
closestPoint = v
end
end
return closestPoint
end
Table.FindFirstUnoccupiedSlot = function(tbl)
local occupiedSlots = {}
for _, v in pairs(tbl) do
occupiedSlots[v.slot] = true
end
for i = 1, BridgeServerConfig.MaxInventorySlots do
if not occupiedSlots[i] then
return i
end
end
return nil
end
Table.Append = function(tbl1, tbl2)
for _, v in pairs(tbl2) do
table.insert(tbl1, v)
end
return tbl1
end
Table.Split = function(tbl, size)
local new1 = {}
local new2 = {}
size = size or math.floor(#tbl / 2)
if size > #tbl then
assert(false, "Size is greater than the length of the table.")
end
for i = 1, size do
table.insert(new1, tbl[i])
end
for i = size + 1, #tbl do
table.insert(new2, tbl[i])
end
return new1, new2
end
Table.Shuffle = function(tbl)
for i = #tbl, 2, -1 do
local j = math.random(i)
tbl[i], tbl[j] = tbl[j], tbl[i]
end
return tbl
end
exports("Table", Table)
return Table