add some functions to util

This commit is contained in:
Lera Elvoé 2025-02-14 00:43:32 +03:00
parent 73dd382730
commit 5850004d16
Signed by: yagich
SSH Key Fingerprint: SHA256:6xjGb6uA7lAVcULa7byPEN//rQ0wPoG+UzYVMfZnbvc

68
data/scripts/util.lua Normal file
View File

@ -0,0 +1,68 @@
local Vector3 = require "types.vector3"
local util = {}
function util.printt(t)
if type(t) == 'table' then
local s = '{ '
for k,v in pairs(t) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. util.printt(v) .. ','
end
return s .. '} '
else
return tostring(t)
end
end
function util.b2n(value)
return value and 1 or 0
end
---creates a list of vertices for a draw_quad call from a plane defined by position and normal
---@param position Vector3
---@param normal Vector3
---@param size number
---@return table
function util.create_plane_quad(position, normal, size)
local axis = Vector3.UP:cross(normal)
local angle = math.acos(Vector3.UP:dot(normal))
local vlist = {
v0 = (Vector3(-0.5, 0, 0.5) * size):rotated(axis, angle) + position,
v1 = (Vector3(0.5, 0, 0.5) * size):rotated(axis, angle) + position,
v2 = (Vector3(0.5, 0, -0.5) * size):rotated(axis, angle) + position,
v3 = (Vector3(-0.5, 0, -0.5) * size):rotated(axis, angle) + position,
}
return vlist
end
---creates a shallow copy of the table t
---@param t table
---@return table
function util.shallow_copy(t)
local t2 = {}
for k, v in pairs(t) do
t2[k] = v
end
return t2
end
---returns the intersection of tables t1 and t2, optionally overwriting t1's keys with t2's
---@param t1 table
---@param t2 table
---@param overwrite boolean?
---@return table
function util.merge(t1, t2, overwrite)
local t3 = util.shallow_copy(t1)
overwrite = overwrite or true
for k, v in pairs(t2) do
if overwrite or not t3[k] then
t3[k] = v
end
end
return t3
end
return util