From 5850004d161fee4ea0c6aa29a5ee5fd7abac25f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Fri, 14 Feb 2025 00:43:32 +0300 Subject: [PATCH] add some functions to util --- data/scripts/util.lua | 68 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 data/scripts/util.lua diff --git a/data/scripts/util.lua b/data/scripts/util.lua new file mode 100644 index 0000000..71f61d6 --- /dev/null +++ b/data/scripts/util.lua @@ -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 \ No newline at end of file