Logikraft/utils.lua

82 lines
1.9 KiB
Lua

function table.indexof(arr,x)
for i,v in ipairs(arr)
do
if v == x
then return i
end
end
end
function table.arrperm(arr1,arr2)
out = {}
for i,v in ipairs(arr1)
do
table.insert(out,table.indexof(arr2,v))
end
return out
end
function table.invert(arr)
out = {}
for k,v in pairs(arr)
do
out[v] = k
end
return out
end
function table.getkey1(arr,x)
for k,v in pairs(arr)
do
if table.equals1(x,v)
then return k
end
end
end
function string.startsWith(str,ex)
return string.sub(str,1,string.len(ex)) == ex
end
function string.without(str,ex)
return string.sub(str,string.len(ex)+1,-1)
end
function table.equals1(arr1,arr2)
for i,v in ipairs(arr1)
do
if arr2[i] ~= v
then return false
end
end
return #arr1 == #arr2
end
local function nodeboxDistance(nb,p)
return
(((nb[1] < p[1]) and (p[1] - nb[1])) or ((p[1] < nb[4]) and (nb[4] - p[1])) or 0) +
(((nb[2] < p[2]) and (p[2] - nb[2])) or ((p[2] < nb[5]) and (nb[5] - p[2])) or 0) +
(((nb[3] < p[3]) and (p[3] - nb[3])) or ((p[3] < nb[6]) and (nb[6] - p[3])) or 0)
end
function circuits.nearestNodeboxIndex(nodeboxes,point)
local minindex=-1
local minvalue = 9999
for i,nb in ipairs(nodeboxes)
do
local d = nodeboxDistance(nb,point)
if d < minvalue
then
minvalue = d
minindex = i
end
end
return minindex
end
function circuits.lookingAtNbIndex(user,under)
local range=5
local eye = vector.add(user:get_pos(),{x=0,y=1.625,z=0})
local objective = vector.add(eye, vector.multiply(user:get_look_dir(),range))
local ray = minetest.raycast(eye,objective,false,false)
for p in ray
do
if p.under.x==under.x and p.under.y==under.y and p.under.z==under.z
then return p.box_id
end
end
return 0
end