2019-12-01 11:00:53 +01:00
|
|
|
local path = minetest.get_modpath"vector_extras"
|
2014-03-08 18:09:09 +01:00
|
|
|
|
2015-04-25 09:54:07 +02:00
|
|
|
local funcs = {}
|
|
|
|
|
|
|
|
function funcs.pos_to_string(pos)
|
2015-05-12 21:29:05 +02:00
|
|
|
return "("..pos.x.."|"..pos.y.."|"..pos.z..")"
|
2014-03-08 18:09:09 +01:00
|
|
|
end
|
|
|
|
|
2014-02-09 11:43:15 +01:00
|
|
|
local r_corr = 0.25 --remove a bit more nodes (if shooting diagonal) to let it look like a hole (sth like antialiasing)
|
|
|
|
|
|
|
|
-- this doesn't need to be calculated every time
|
|
|
|
local f_1 = 0.5-r_corr
|
|
|
|
local f_2 = 0.5+r_corr
|
|
|
|
|
|
|
|
--returns information about the direction
|
|
|
|
local function get_used_dir(dir)
|
|
|
|
local abs_dir = {x=math.abs(dir.x), y=math.abs(dir.y), z=math.abs(dir.z)}
|
|
|
|
local dir_max = math.max(abs_dir.x, abs_dir.y, abs_dir.z)
|
|
|
|
if dir_max == abs_dir.x then
|
|
|
|
local tab = {"x", {x=1, y=dir.y/dir.x, z=dir.z/dir.x}}
|
|
|
|
if dir.x >= 0 then
|
|
|
|
tab[3] = "+"
|
|
|
|
end
|
|
|
|
return tab
|
|
|
|
end
|
|
|
|
if dir_max == abs_dir.y then
|
|
|
|
local tab = {"y", {x=dir.x/dir.y, y=1, z=dir.z/dir.y}}
|
|
|
|
if dir.y >= 0 then
|
|
|
|
tab[3] = "+"
|
|
|
|
end
|
|
|
|
return tab
|
|
|
|
end
|
|
|
|
local tab = {"z", {x=dir.x/dir.z, y=dir.y/dir.z, z=1}}
|
|
|
|
if dir.z >= 0 then
|
|
|
|
tab[3] = "+"
|
|
|
|
end
|
|
|
|
return tab
|
|
|
|
end
|
|
|
|
|
|
|
|
local function node_tab(z, d)
|
|
|
|
local n1 = math.floor(z*d+f_1)
|
|
|
|
local n2 = math.floor(z*d+f_2)
|
|
|
|
if n1 == n2 then
|
|
|
|
return {n1}
|
|
|
|
end
|
|
|
|
return {n1, n2}
|
|
|
|
end
|
|
|
|
|
|
|
|
local function return_line(pos, dir, range) --range ~= length
|
|
|
|
local tab = {}
|
|
|
|
local num = 1
|
|
|
|
local t_dir = get_used_dir(dir)
|
|
|
|
local dir_typ = t_dir[1]
|
|
|
|
if t_dir[3] == "+" then
|
|
|
|
f_tab = {0, range, 1}
|
|
|
|
else
|
|
|
|
f_tab = {0, -range, -1}
|
|
|
|
end
|
|
|
|
local d_ch = t_dir[2]
|
|
|
|
if dir_typ == "x" then
|
|
|
|
for d = f_tab[1],f_tab[2],f_tab[3] do
|
|
|
|
local x = d
|
|
|
|
local ytab = node_tab(d_ch.y, d)
|
|
|
|
local ztab = node_tab(d_ch.z, d)
|
|
|
|
for _,y in ipairs(ytab) do
|
|
|
|
for _,z in ipairs(ztab) do
|
|
|
|
tab[num] = {x=pos.x+x, y=pos.y+y, z=pos.z+z}
|
|
|
|
num = num+1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif dir_typ == "y" then
|
|
|
|
for d = f_tab[1],f_tab[2],f_tab[3] do
|
|
|
|
local xtab = node_tab(d_ch.x, d)
|
|
|
|
local y = d
|
|
|
|
local ztab = node_tab(d_ch.z, d)
|
|
|
|
for _,x in ipairs(xtab) do
|
|
|
|
for _,z in ipairs(ztab) do
|
|
|
|
tab[num] = {x=pos.x+x, y=pos.y+y, z=pos.z+z}
|
|
|
|
num = num+1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
for d = f_tab[1],f_tab[2],f_tab[3] do
|
|
|
|
local xtab = node_tab(d_ch.x, d)
|
|
|
|
local ytab = node_tab(d_ch.y, d)
|
|
|
|
local z = d
|
|
|
|
for _,x in ipairs(xtab) do
|
|
|
|
for _,y in ipairs(ytab) do
|
|
|
|
tab[num] = {x=pos.x+x, y=pos.y+y, z=pos.z+z}
|
|
|
|
num = num+1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return tab
|
|
|
|
end
|
|
|
|
|
2016-04-23 11:09:10 +02:00
|
|
|
function funcs.rayIter(pos, dir)
|
|
|
|
-- make a table of possible movements
|
|
|
|
local step = {}
|
|
|
|
for i in pairs(pos) do
|
|
|
|
local v = math.sign(dir[i])
|
|
|
|
if v ~= 0 then
|
|
|
|
step[i] = v
|
2014-02-10 18:26:49 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-23 11:09:10 +02:00
|
|
|
local p
|
|
|
|
return function()
|
|
|
|
if not p then
|
|
|
|
-- avoid skipping the first position
|
|
|
|
p = vector.round(pos)
|
|
|
|
return vector.new(p)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- find the position which has the smallest distance to the line
|
|
|
|
local choose = {}
|
|
|
|
local choosefit = vector.new()
|
|
|
|
for i in pairs(step) do
|
|
|
|
choose[i] = vector.new(p)
|
|
|
|
choose[i][i] = choose[i][i] + step[i]
|
|
|
|
choosefit[i] = vector.scalar(vector.normalize(vector.subtract(choose[i], pos)), dir)
|
2014-02-09 12:43:46 +01:00
|
|
|
end
|
2016-04-23 11:09:10 +02:00
|
|
|
p = choose[vector.get_max_coord(choosefit)]
|
|
|
|
|
|
|
|
return vector.new(p)
|
2014-02-09 12:43:46 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-23 11:09:10 +02:00
|
|
|
function funcs.fine_line(pos, dir, range)
|
2014-02-09 12:43:46 +01:00
|
|
|
if not range then --dir = pos2
|
2016-04-23 11:09:10 +02:00
|
|
|
dir, range = vector.direction(pos, dir), vector.distance(pos, dir)
|
2014-02-09 12:43:46 +01:00
|
|
|
end
|
2016-04-23 11:09:10 +02:00
|
|
|
local result,n = {},1
|
|
|
|
for p in vector.rayIter(pos, dir) do
|
|
|
|
if vector.distance(p, pos) > range then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
result[n] = p
|
|
|
|
n = n+1
|
|
|
|
end
|
|
|
|
return result
|
2014-02-09 12:43:46 +01:00
|
|
|
end
|
|
|
|
|
2015-04-25 09:54:07 +02:00
|
|
|
function funcs.line(pos, dir, range, alt)
|
2014-02-09 11:43:15 +01:00
|
|
|
--assert_vector(pos)
|
2014-09-14 17:15:00 +02:00
|
|
|
if alt then
|
|
|
|
if not range then --dir = pos2
|
|
|
|
dir, range = vector.direction(pos, dir), vector.distance(pos, dir)
|
|
|
|
end
|
|
|
|
return return_line(pos, dir, range)
|
|
|
|
end
|
2016-04-23 11:09:10 +02:00
|
|
|
if range then
|
2014-09-14 17:15:00 +02:00
|
|
|
dir = vector.round(vector.multiply(dir, range))
|
2016-04-23 11:09:10 +02:00
|
|
|
else --dir = pos2
|
2014-09-14 17:15:00 +02:00
|
|
|
dir = vector.subtract(dir, pos)
|
|
|
|
end
|
|
|
|
local line,n = {},1
|
|
|
|
for _,i in ipairs(vector.threeline(dir.x, dir.y, dir.z)) do
|
|
|
|
line[n] = {x=pos.x+i[1], y=pos.y+i[2], z=pos.z+i[3]}
|
|
|
|
n = n+1
|
2014-02-09 11:43:15 +01:00
|
|
|
end
|
2014-09-14 17:15:00 +02:00
|
|
|
return line
|
2014-02-09 11:43:15 +01:00
|
|
|
end
|
|
|
|
|
2014-07-27 14:50:03 +02:00
|
|
|
local twolines = {}
|
2015-04-25 09:54:07 +02:00
|
|
|
function funcs.twoline(x, y)
|
2014-07-27 14:50:03 +02:00
|
|
|
local pstr = x.." "..y
|
|
|
|
local line = twolines[pstr]
|
|
|
|
if line then
|
|
|
|
return line
|
|
|
|
end
|
2014-09-14 17:15:00 +02:00
|
|
|
line = {}
|
|
|
|
local n = 1
|
2014-07-27 14:50:03 +02:00
|
|
|
local dirx = 1
|
|
|
|
if x < 0 then
|
|
|
|
dirx = -dirx
|
|
|
|
end
|
|
|
|
local ymin, ymax = 0, y
|
|
|
|
if y < 0 then
|
|
|
|
ymin, ymax = ymax, ymin
|
|
|
|
end
|
2014-08-08 13:39:58 +02:00
|
|
|
local m = y/x --y/0 works too
|
2014-07-27 14:50:03 +02:00
|
|
|
local dir = 1
|
|
|
|
if m < 0 then
|
|
|
|
dir = -dir
|
|
|
|
end
|
|
|
|
for i = 0,x,dirx do
|
|
|
|
local p1 = math.max(math.min(math.floor((i-0.5)*m+0.5), ymax), ymin)
|
|
|
|
local p2 = math.max(math.min(math.floor((i+0.5)*m+0.5), ymax), ymin)
|
|
|
|
for j = p1,p2,dir do
|
|
|
|
line[n] = {i, j}
|
|
|
|
n = n+1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
twolines[pstr] = line
|
|
|
|
return line
|
|
|
|
end
|
|
|
|
|
2014-09-14 17:15:00 +02:00
|
|
|
local threelines = {}
|
2015-04-25 09:54:07 +02:00
|
|
|
function funcs.threeline(x, y, z)
|
2014-09-14 17:15:00 +02:00
|
|
|
local pstr = x.." "..y.." "..z
|
|
|
|
local line = threelines[pstr]
|
|
|
|
if line then
|
|
|
|
return line
|
|
|
|
end
|
|
|
|
if x ~= math.floor(x) then
|
2017-11-26 11:22:24 +01:00
|
|
|
minetest.log("error", "[vector_extras] INFO: The position used for " ..
|
|
|
|
"vector.threeline isn't round.")
|
2014-09-14 17:15:00 +02:00
|
|
|
end
|
|
|
|
local two_line = vector.twoline(x, y)
|
|
|
|
line = {}
|
|
|
|
local n = 1
|
|
|
|
local zmin, zmax = 0, z
|
|
|
|
if z < 0 then
|
|
|
|
zmin, zmax = zmax, zmin
|
|
|
|
end
|
|
|
|
local m = z/math.hypot(x, y)
|
|
|
|
local dir = 1
|
|
|
|
if m < 0 then
|
|
|
|
dir = -dir
|
|
|
|
end
|
|
|
|
for _,i in ipairs(two_line) do
|
|
|
|
local px, py = unpack(i)
|
|
|
|
local ph = math.hypot(px, py)
|
|
|
|
local z1 = math.max(math.min(math.floor((ph-0.5)*m+0.5), zmax), zmin)
|
|
|
|
local z2 = math.max(math.min(math.floor((ph+0.5)*m+0.5), zmax), zmin)
|
|
|
|
for pz = z1,z2,dir do
|
|
|
|
line[n] = {px, py, pz}
|
|
|
|
n = n+1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
threelines[pstr] = line
|
|
|
|
return line
|
|
|
|
end
|
|
|
|
|
2017-01-25 09:43:00 +01:00
|
|
|
function funcs.sort_positions(ps, preferred_coords)
|
2015-04-16 20:42:21 +02:00
|
|
|
preferred_coords = preferred_coords or {"z", "y", "x"}
|
|
|
|
local a,b,c = unpack(preferred_coords)
|
|
|
|
local function ps_sorting(p1, p2)
|
|
|
|
if p1[a] == p2[a] then
|
|
|
|
if p1[b] == p2[a] then
|
|
|
|
if p1[c] < p2[c] then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
elseif p1[b] < p2[b] then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
elseif p1[a] < p2[a] then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(ps, ps_sorting)
|
|
|
|
end
|
|
|
|
|
2015-04-25 09:54:07 +02:00
|
|
|
function funcs.scalar(v1, v2)
|
2015-04-16 20:42:21 +02:00
|
|
|
return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z
|
|
|
|
end
|
|
|
|
|
2015-04-25 09:54:07 +02:00
|
|
|
function funcs.cross(v1, v2)
|
2015-04-16 20:42:21 +02:00
|
|
|
return {
|
|
|
|
x = v1.y*v2.z - v1.z*v2.y,
|
|
|
|
y = v1.z*v2.x - v1.x*v2.z,
|
|
|
|
z = v1.x*v2.y - v1.y*v2.x
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-11-26 11:22:24 +01:00
|
|
|
-- Tschebyschew norm
|
|
|
|
function funcs.maxnorm(v)
|
|
|
|
return math.max(math.max(math.abs(v.x), math.abs(v.y)), math.abs(v.z))
|
|
|
|
end
|
|
|
|
|
|
|
|
function funcs.sumnorm(v)
|
|
|
|
return math.abs(v.x) + math.abs(v.y) + math.abs(v.z)
|
|
|
|
end
|
|
|
|
|
|
|
|
function funcs.pnorm(v, p)
|
|
|
|
return (math.abs(v.x)^p + math.abs(v.y)^p + math.abs(v.z)^p)^(1 / p)
|
|
|
|
end
|
|
|
|
|
2015-04-25 09:54:07 +02:00
|
|
|
--not optimized
|
2015-04-16 20:42:21 +02:00
|
|
|
--local areas = {}
|
2015-04-25 09:54:07 +02:00
|
|
|
function funcs.plane(ps)
|
|
|
|
-- sort positions and imagine the first one (A) as vector.zero
|
2017-01-25 09:43:00 +01:00
|
|
|
ps = vector.sort_positions(ps)
|
2015-04-16 20:42:21 +02:00
|
|
|
local pos = ps[1]
|
|
|
|
local B = vector.subtract(ps[2], pos)
|
|
|
|
local C = vector.subtract(ps[3], pos)
|
|
|
|
|
|
|
|
-- get the positions for the fors
|
|
|
|
local cube_p1 = {x=0, y=0, z=0}
|
|
|
|
local cube_p2 = {x=0, y=0, z=0}
|
|
|
|
for i in pairs(cube_p1) do
|
|
|
|
cube_p1[i] = math.min(B[i], C[i], 0)
|
|
|
|
cube_p2[i] = math.max(B[i], C[i], 0)
|
|
|
|
end
|
2015-04-25 09:54:07 +02:00
|
|
|
cube_p1 = vector.apply(cube_p1, math.floor)
|
|
|
|
cube_p2 = vector.apply(cube_p2, math.ceil)
|
2015-04-16 20:42:21 +02:00
|
|
|
|
2015-04-25 09:54:07 +02:00
|
|
|
local vn = vector.normalize(vector.cross(B, C))
|
2015-04-16 20:42:21 +02:00
|
|
|
|
2015-04-25 08:35:23 +02:00
|
|
|
local nAB = vector.normalize(B)
|
|
|
|
local nAC = vector.normalize(C)
|
2015-04-25 09:54:07 +02:00
|
|
|
local angle_BAC = math.acos(vector.scalar(nAB, nAC))
|
2015-04-16 20:42:21 +02:00
|
|
|
|
2015-04-25 08:35:23 +02:00
|
|
|
local nBA = vector.multiply(nAB, -1)
|
|
|
|
local nBC = vector.normalize(vector.subtract(C, B))
|
2015-04-25 09:54:07 +02:00
|
|
|
local angle_ABC = math.acos(vector.scalar(nBA, nBC))
|
2015-04-16 20:42:21 +02:00
|
|
|
|
|
|
|
for z = cube_p1.z, cube_p2.z do
|
|
|
|
for y = cube_p1.y, cube_p2.y do
|
|
|
|
for x = cube_p1.x, cube_p2.x do
|
|
|
|
local p = {x=x, y=y, z=z}
|
2015-04-25 09:54:07 +02:00
|
|
|
local n = -vector.scalar(p, vn)/vector.scalar(vn, vn)
|
|
|
|
if math.abs(n) <= 0.5 then
|
|
|
|
local ep = vector.add(p, vector.multiply(vn, n))
|
2015-04-25 08:28:18 +02:00
|
|
|
local nep = vector.normalize(ep)
|
2015-04-25 09:54:07 +02:00
|
|
|
local angle_BAep = math.acos(vector.scalar(nAB, nep))
|
|
|
|
local angle_CAep = math.acos(vector.scalar(nAC, nep))
|
2015-04-25 08:35:23 +02:00
|
|
|
local angldif = angle_BAC - (angle_BAep+angle_CAep)
|
2015-04-25 09:54:07 +02:00
|
|
|
if math.abs(angldif) < 0.001 then
|
2015-04-25 08:28:18 +02:00
|
|
|
ep = vector.subtract(ep, B)
|
|
|
|
nep = vector.normalize(ep)
|
2015-04-25 09:54:07 +02:00
|
|
|
local angle_ABep = math.acos(vector.scalar(nBA, nep))
|
|
|
|
local angle_CBep = math.acos(vector.scalar(nBC, nep))
|
2015-04-25 08:35:23 +02:00
|
|
|
local angldif = angle_ABC - (angle_ABep+angle_CBep)
|
2015-04-25 09:54:07 +02:00
|
|
|
if math.abs(angldif) < 0.001 then
|
|
|
|
table.insert(ps, vector.add(pos, p))
|
2015-04-25 08:28:18 +02:00
|
|
|
end
|
2015-04-16 20:42:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-04-25 09:54:07 +02:00
|
|
|
return ps
|
2015-04-16 20:42:21 +02:00
|
|
|
end
|
|
|
|
|
2015-04-25 09:54:07 +02:00
|
|
|
function funcs.straightdelay(s, v, a)
|
2014-02-09 11:43:15 +01:00
|
|
|
if not a then
|
|
|
|
return s/v
|
|
|
|
end
|
|
|
|
return (math.sqrt(v*v+2*a*s)-v)/a
|
|
|
|
end
|
2014-02-22 22:40:17 +01:00
|
|
|
|
2015-08-27 20:44:45 +02:00
|
|
|
vector.zero = vector.new()
|
2014-03-08 18:09:09 +01:00
|
|
|
|
2015-04-25 09:54:07 +02:00
|
|
|
function funcs.sun_dir(time)
|
2014-08-08 13:39:58 +02:00
|
|
|
if not time then
|
|
|
|
time = minetest.get_timeofday()
|
|
|
|
end
|
2014-03-08 18:09:09 +01:00
|
|
|
local t = (time-0.5)*5/6+0.5 --the sun rises at 5 o'clock, not at 6
|
2014-02-22 22:40:17 +01:00
|
|
|
if t < 0.25
|
|
|
|
or t > 0.75 then
|
|
|
|
return
|
|
|
|
end
|
2014-03-08 18:14:41 +01:00
|
|
|
local tmp = math.cos(math.pi*(2*t-0.5))
|
|
|
|
return {x=tmp, y=math.sqrt(1-tmp*tmp), z=0}
|
2014-02-22 22:40:17 +01:00
|
|
|
end
|
2014-02-24 19:44:02 +01:00
|
|
|
|
2015-04-25 09:54:07 +02:00
|
|
|
function funcs.inside(pos, minp, maxp)
|
2015-03-07 16:02:28 +01:00
|
|
|
for _,i in pairs({"x", "y", "z"}) do
|
2014-02-24 19:44:02 +01:00
|
|
|
if pos[i] < minp[i]
|
|
|
|
or pos[i] > maxp[i] then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2015-04-25 09:54:07 +02:00
|
|
|
function funcs.minmax(p1, p2)
|
2015-04-16 20:42:21 +02:00
|
|
|
local p1 = vector.new(p1)
|
2014-02-24 19:44:02 +01:00
|
|
|
local p2 = vector.new(p2)
|
|
|
|
for _,i in ipairs({"x", "y", "z"}) do
|
|
|
|
if p1[i] > p2[i] then
|
|
|
|
p1[i], p2[i] = p2[i], p1[i]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return p1, p2
|
|
|
|
end
|
|
|
|
|
2015-04-25 09:54:07 +02:00
|
|
|
function funcs.move(p1, p2, s)
|
2014-02-24 19:44:02 +01:00
|
|
|
return vector.round(
|
|
|
|
vector.add(
|
|
|
|
vector.multiply(
|
|
|
|
vector.direction(
|
|
|
|
p1,
|
|
|
|
p2
|
|
|
|
),
|
|
|
|
s
|
|
|
|
),
|
|
|
|
p1
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
2014-03-03 15:16:12 +01:00
|
|
|
|
2015-08-27 20:44:45 +02:00
|
|
|
function funcs.from_number(i)
|
|
|
|
return {x=i, y=i, z=i}
|
|
|
|
end
|
|
|
|
|
2019-12-01 11:00:53 +01:00
|
|
|
local adammil_fill = dofile(path .. "/adammil_flood_fill.lua")
|
|
|
|
function funcs.search_2d(go_test, x0, y0, allow_revisit, give_map)
|
|
|
|
marked_places = adammil_fill(go_test, x0, y0, allow_revisit)
|
|
|
|
if give_map then
|
|
|
|
return marked_places
|
|
|
|
end
|
|
|
|
local l = {}
|
|
|
|
for vi in pairs(marked_places) do
|
|
|
|
local x = (vi % 65536) - 32768
|
|
|
|
local y = (math.floor(x / 65536) % 65536) - 32768
|
|
|
|
l[#l+1] = {x, y}
|
|
|
|
end
|
|
|
|
return l
|
|
|
|
end
|
|
|
|
|
2014-03-03 15:16:12 +01:00
|
|
|
local explosion_tables = {}
|
2015-04-25 09:54:07 +02:00
|
|
|
function funcs.explosion_table(r)
|
2014-03-03 15:16:12 +01:00
|
|
|
local table = explosion_tables[r]
|
|
|
|
if table then
|
|
|
|
return table
|
|
|
|
end
|
|
|
|
|
2017-11-26 11:22:24 +01:00
|
|
|
--~ local t1 = os.clock()
|
2014-03-03 15:16:12 +01:00
|
|
|
local tab, n = {}, 1
|
|
|
|
|
|
|
|
local tmp = r*r + r
|
|
|
|
for x=-r,r do
|
|
|
|
for y=-r,r do
|
|
|
|
for z=-r,r do
|
2015-05-29 12:09:25 +02:00
|
|
|
local rc = x*x+y*y+z*z
|
2014-03-03 15:16:12 +01:00
|
|
|
if rc <= tmp then
|
|
|
|
local np={x=x, y=y, z=z}
|
|
|
|
if math.floor(math.sqrt(rc) +0.5) > r-1 then
|
|
|
|
tab[n] = {np, true}
|
|
|
|
else
|
|
|
|
tab[n] = {np}
|
|
|
|
end
|
|
|
|
n = n+1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
explosion_tables[r] = tab
|
2017-11-26 11:22:24 +01:00
|
|
|
--~ minetest.log("info", string.format("[vector_extras] table created after ca. %.2fs", os.clock() - t1))
|
2014-03-03 15:16:12 +01:00
|
|
|
return tab
|
|
|
|
end
|
|
|
|
|
2015-08-27 20:44:45 +02:00
|
|
|
local default_nparams = {
|
|
|
|
offset = 0,
|
|
|
|
scale = 1,
|
|
|
|
seed = 1337,
|
|
|
|
octaves = 6,
|
|
|
|
persist = 0.6
|
|
|
|
}
|
2015-08-27 21:23:11 +02:00
|
|
|
function funcs.explosion_perlin(rmin, rmax, nparams)
|
2015-08-27 20:44:45 +02:00
|
|
|
local t1 = os.clock()
|
|
|
|
|
|
|
|
local r = math.ceil(rmax)
|
|
|
|
nparams = nparams or {}
|
|
|
|
for i,v in pairs(default_nparams) do
|
|
|
|
nparams[i] = nparams[i] or v
|
|
|
|
end
|
|
|
|
nparams.spread = nparams.spread or vector.from_number(r*5)
|
|
|
|
|
2015-08-27 21:23:11 +02:00
|
|
|
local pos = {x=math.random(-30000, 30000), y=math.random(-30000, 30000), z=math.random(-30000, 30000)}
|
2015-08-27 20:44:45 +02:00
|
|
|
local map = minetest.get_perlin_map(nparams, vector.from_number(r+r+1)):get3dMap_flat(pos)
|
|
|
|
|
|
|
|
local id = 1
|
|
|
|
|
|
|
|
local bare_maxdist = rmax*rmax
|
|
|
|
local bare_mindist = rmin*rmin
|
|
|
|
|
|
|
|
local mindist = math.sqrt(bare_mindist)
|
2015-08-27 21:23:11 +02:00
|
|
|
local dist_diff = math.sqrt(bare_maxdist)-mindist
|
2015-08-27 20:44:45 +02:00
|
|
|
mindist = mindist/dist_diff
|
|
|
|
|
|
|
|
local pval_min, pval_max
|
|
|
|
|
|
|
|
local tab, n = {}, 1
|
|
|
|
for z=-r,r do
|
|
|
|
local bare_dist = z*z
|
|
|
|
for y=-r,r do
|
|
|
|
local bare_dist = bare_dist+y*y
|
|
|
|
for x=-r,r do
|
|
|
|
local bare_dist = bare_dist+x*x
|
|
|
|
local add = bare_dist < bare_mindist
|
|
|
|
local pval, distdiv
|
|
|
|
if not add
|
|
|
|
and bare_dist <= bare_maxdist then
|
|
|
|
distdiv = math.sqrt(bare_dist)/dist_diff-mindist
|
|
|
|
pval = math.abs(map[id]) -- strange perlin values…
|
|
|
|
if not pval_min then
|
|
|
|
pval_min = pval
|
|
|
|
pval_max = pval
|
|
|
|
else
|
|
|
|
pval_min = math.min(pval, pval_min)
|
|
|
|
pval_max = math.max(pval, pval_max)
|
|
|
|
end
|
|
|
|
add = true--distdiv < 1-math.abs(map[id])
|
|
|
|
end
|
|
|
|
|
|
|
|
if add then
|
2015-08-27 21:23:11 +02:00
|
|
|
tab[n] = {{x=x, y=y, z=z}, pval, distdiv}
|
2015-08-27 20:44:45 +02:00
|
|
|
n = n+1
|
|
|
|
end
|
|
|
|
id = id+1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-06 12:04:27 +01:00
|
|
|
map = nil
|
|
|
|
collectgarbage()
|
|
|
|
|
2015-08-27 20:44:45 +02:00
|
|
|
-- change strange values
|
|
|
|
local pval_diff = pval_max - pval_min
|
|
|
|
pval_min = pval_min/pval_diff
|
|
|
|
|
|
|
|
for n,i in pairs(tab) do
|
|
|
|
if i[2] then
|
2015-08-30 15:51:56 +02:00
|
|
|
local new_pval = math.abs(i[2]/pval_diff - pval_min)
|
|
|
|
if i[3]+0.33 < new_pval then
|
2015-08-27 20:44:45 +02:00
|
|
|
tab[n] = {i[1]}
|
2015-08-30 15:51:56 +02:00
|
|
|
elseif i[3] < new_pval then
|
|
|
|
tab[n] = {i[1], true}
|
2015-08-27 20:44:45 +02:00
|
|
|
else
|
|
|
|
tab[n] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.log("info", string.format("[vector_extras] table created after ca. %.2fs", os.clock() - t1))
|
|
|
|
return tab
|
|
|
|
end
|
|
|
|
|
2014-03-03 15:16:12 +01:00
|
|
|
local circle_tables = {}
|
2015-04-25 09:54:07 +02:00
|
|
|
function funcs.circle(r)
|
2014-03-03 15:16:12 +01:00
|
|
|
local table = circle_tables[r]
|
|
|
|
if table then
|
|
|
|
return table
|
|
|
|
end
|
|
|
|
|
|
|
|
local t1 = os.clock()
|
|
|
|
local tab, n = {}, 1
|
|
|
|
|
|
|
|
for i = -r, r do
|
|
|
|
for j = -r, r do
|
|
|
|
if math.floor(math.sqrt(i*i+j*j)+0.5) == r then
|
|
|
|
tab[n] = {x=i, y=0, z=j}
|
|
|
|
n = n+1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
circle_tables[r] = tab
|
2014-12-28 22:01:45 +01:00
|
|
|
minetest.log("info", string.format("[vector_extras] table created after ca. %.2fs", os.clock() - t1))
|
2014-03-03 15:16:12 +01:00
|
|
|
return tab
|
|
|
|
end
|
|
|
|
|
|
|
|
local ring_tables = {}
|
2015-04-25 09:54:07 +02:00
|
|
|
function funcs.ring(r)
|
2014-03-03 15:16:12 +01:00
|
|
|
local table = ring_tables[r]
|
|
|
|
if table then
|
|
|
|
return table
|
|
|
|
end
|
|
|
|
|
|
|
|
local t1 = os.clock()
|
|
|
|
local tab, n = {}, 1
|
|
|
|
|
|
|
|
local tmp = r*r
|
|
|
|
local p = {x=math.floor(r+0.5), z=0}
|
|
|
|
while p.x > 0 do
|
|
|
|
tab[n] = p
|
|
|
|
n = n+1
|
|
|
|
local p1, p2 = {x=p.x-1, z=p.z}, {x=p.x, z=p.z+1}
|
|
|
|
local dif1 = math.abs(tmp-p1.x*p1.x-p1.z*p1.z)
|
|
|
|
local dif2 = math.abs(tmp-p2.x*p2.x-p2.z*p2.z)
|
|
|
|
if dif1 <= dif2 then
|
|
|
|
p = p1
|
|
|
|
else
|
|
|
|
p = p2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-19 17:52:01 +02:00
|
|
|
local tab2 = {}
|
|
|
|
n = 1
|
2014-03-03 15:16:12 +01:00
|
|
|
for _,i in ipairs(tab) do
|
|
|
|
for _,j in ipairs({
|
|
|
|
{i.x, i.z},
|
|
|
|
{-i.z, i.x},
|
|
|
|
{-i.x, -i.z},
|
|
|
|
{i.z, -i.x},
|
|
|
|
}) do
|
|
|
|
tab2[n] = {x=j[1], y=0, z=j[2]}
|
|
|
|
n = n+1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ring_tables[r] = tab2
|
2014-12-28 22:01:45 +01:00
|
|
|
minetest.log("info", string.format("[vector_extras] table created after ca. %.2fs", os.clock() - t1))
|
2014-03-03 15:16:12 +01:00
|
|
|
return tab2
|
|
|
|
end
|
2014-03-05 21:02:25 +01:00
|
|
|
|
2017-06-11 08:58:13 +02:00
|
|
|
--~ posy(t) = att + bt + c
|
|
|
|
--~ vely(t) = 2at + b
|
|
|
|
--~ accy(t) = 2a
|
|
|
|
|
|
|
|
--~ a = -0.5gravity
|
|
|
|
--~ vely(0) = b = vel.y
|
|
|
|
--~ posy(0) = c = pos.y
|
|
|
|
|
2017-06-18 13:47:54 +02:00
|
|
|
--~ posy(t) = -0.5 * gravity * t * t + vel.y * t + pos.y
|
2017-06-11 08:58:13 +02:00
|
|
|
--~ vely(t) = -gravity*t + vel.y
|
|
|
|
|
|
|
|
--~ Scheitel:
|
|
|
|
--~ vely(t) = 0 = -gravity*t + vel.y
|
|
|
|
--~ t = vel.y / gravity
|
|
|
|
|
|
|
|
--~ 45°
|
2017-06-18 13:47:54 +02:00
|
|
|
--~ vely(t)^2 = velx(t)^2 + velz(t)^2
|
|
|
|
--~ (-gravity*t + vel.y)^2 = vel.x * vel.x + vel.z * vel.z
|
|
|
|
--~ gravity^2 * t^2 + vel.y^2 - -2*gravity*t*vel.y = vel.x * vel.x + vel.z * vel.z
|
|
|
|
--~ gravity^2 * t^2 - 2*gravity*vel.y * t + (vel.y^2 - vel.x^2 - vel.z^2) = 0
|
|
|
|
--~ t = (2*gravity*vel.y .. rt((2*gravity*vel.y)^2 - 4*gravity^2*(vel.y^2 - vel.x^2 - vel.z^2))) / (2*gravity^2)
|
|
|
|
--~ t = (2*gravity*vel.y .. rt(4*gravity^2*vel.y^2 - 4*gravity^2*(vel.y^2) + 4*gravity^2*(vel.x^2 + vel.z^2))) / (2*gravity^2)
|
|
|
|
--~ t = (2*gravity*vel.y .. 2*gravity*rt(vel.x^2 + vel.z^2)) / (2*gravity^2)
|
|
|
|
--~ t = (vel.y .. rt(vel.x^2 + vel.z^2)) / gravity
|
|
|
|
--~ t1 = (vel.y - math.sqrt(vel.x * vel.x + vel.z * vel.z)) / gravity
|
|
|
|
--~ t2 = (vel.y + math.sqrt(vel.x * vel.x + vel.z * vel.z)) / gravity
|
|
|
|
|
|
|
|
--~ yswitch = posy(t1) (= posy(t2)) //links und rechts gleich
|
|
|
|
--~ yswitch = -0.5 * gravity * ((vel.y + math.sqrt(vel.x * vel.x + vel.z * vel.z)) / gravity)^2 + vel.y * ((vel.y + math.sqrt(vel.x * vel.x + vel.z * vel.z)) / gravity) + pos.y
|
|
|
|
--~ yswitch = -0.5 * gravity * (vel.y + math.sqrt(vel.x * vel.x + vel.z * vel.z))^2 / gravity^2 + vel.y * ((vel.y + math.sqrt(vel.x * vel.x + vel.z * vel.z)) / gravity) + pos.y
|
|
|
|
--~ yswitch = -0.5 * (vel.y^2 + 2*vel.y*math.sqrt(vel.x * vel.x + vel.z * vel.z) + vel.x^2 + vel.z^2) / gravity + ((vel.y^2 + vel.y*math.sqrt(vel.x * vel.x + vel.z * vel.z)) / gravity) + pos.y
|
|
|
|
--~ yswitch = (-0.5 * (vel.y^2 + 2*vel.y*math.sqrt(vel.x * vel.x + vel.z * vel.z) + vel.x^2 + vel.z^2) + ((vel.y^2 + vel.y*math.sqrt(vel.x * vel.x + vel.z * vel.z)))) / gravity + pos.y
|
|
|
|
--~ yswitch = (-0.5 * vel.y^2 - vel.y*math.sqrt(vel.x * vel.x + vel.z * vel.z) - 0.5 * vel.x^2 - 0.5 * vel.z^2 + vel.y^2 + vel.y*math.sqrt(vel.x * vel.x + vel.z * vel.z)) / gravity + pos.y
|
|
|
|
--~ yswitch = (-0.5 * vel.y^2 - 0.5 * vel.x^2 - 0.5 * vel.z^2 + vel.y^2) / gravity + pos.y
|
|
|
|
--~ yswitch = (0.5 * vel.y^2 - 0.5 * vel.x^2 - 0.5 * vel.z^2) / gravity + pos.y
|
|
|
|
--~ yswitch = -0.5 * (vel.x * vel.x + vel.z * vel.z - vel.y * vel.y) / gravity + pos.y
|
|
|
|
|
|
|
|
|
|
|
|
--~ 45° Zeitpunkte kleineres beim Aufstieg, größeres beim Fall
|
|
|
|
--~ (-gravity*t + vel.y)^2 = vel.x * vel.x + vel.z * vel.z
|
|
|
|
--~ -gravity*t + vel.y = ..math.sqrt(vel.x * vel.x + vel.z * vel.z)
|
|
|
|
--~ t = (..math.sqrt(vel.x * vel.x + vel.z * vel.z) + vel.y) / gravity
|
|
|
|
--~ t_raise = (-math.sqrt(vel.x * vel.x + vel.z * vel.z) + vel.y) / gravity
|
|
|
|
--~ t_fall = (math.sqrt(vel.x * vel.x + vel.z * vel.z) + vel.y) / gravity
|
|
|
|
|
|
|
|
--~ posy nach t umstellen
|
|
|
|
--~ y = -0.5 * gravity * t * t + vel.y * t + pos.y
|
|
|
|
--~ 0 = -0.5 * gravity * t * t + vel.y * t + pos.y - y
|
|
|
|
--~ t = (-vel.y .. math.sqrt(vel.y^2 + 2 * gravity * (pos.y - y))) / (-gravity)
|
|
|
|
--~ t = (vel.y .. math.sqrt(vel.y^2 + 2 * gravity * (pos.y - y))) / gravity
|
|
|
|
--~ t_up = (vel.y - math.sqrt(vel.y^2 + 2 * gravity * (pos.y - y))) / gravity
|
|
|
|
--~ t_down = (vel.y + math.sqrt(vel.y^2 + 2 * gravity * (pos.y - y))) / gravity
|
2017-06-11 08:58:13 +02:00
|
|
|
|
|
|
|
--~ posx(t) = vel.x * t + pos.x
|
|
|
|
--~ posz(t) = vel.z * t + pos.z
|
|
|
|
|
|
|
|
--~ posx nach t umstellen
|
|
|
|
--~ posx - pos.x = vel.x * t
|
|
|
|
--~ t = (posx - pos.x) / vel.x
|
|
|
|
|
|
|
|
|
2017-06-19 17:52:01 +02:00
|
|
|
local function get_parabola_points(pos, vel, gravity, waypoints, max_pointcount,
|
|
|
|
time)
|
2017-06-11 08:58:13 +02:00
|
|
|
local pointcount = 0
|
|
|
|
|
|
|
|
-- the height of the 45° angle point
|
2017-06-19 17:52:01 +02:00
|
|
|
local yswitch = -0.5 * (vel.x^2 + vel.z^2 - vel.y^2)
|
2017-06-18 13:47:54 +02:00
|
|
|
/ gravity + pos.y
|
2017-06-11 08:58:13 +02:00
|
|
|
|
2017-06-18 13:47:54 +02:00
|
|
|
-- the times of the 45° angle point
|
2017-06-19 17:52:01 +02:00
|
|
|
local i = math.sqrt(vel.x^2 + vel.z^2)
|
|
|
|
local t_raise_end = (-i + vel.y) / gravity
|
|
|
|
local t_fall_start = (i + vel.y) / gravity
|
2017-06-11 08:58:13 +02:00
|
|
|
if t_fall_start > 0 then
|
|
|
|
-- the right 45° angle point wasn't passed yet
|
|
|
|
if t_raise_end > 0 then
|
|
|
|
-- put points from before the 45° angle
|
|
|
|
for y = math.ceil(pos.y), math.floor(yswitch +.5) do
|
2017-06-18 13:47:54 +02:00
|
|
|
local t = (vel.y -
|
|
|
|
math.sqrt(vel.y^2 + 2 * gravity * (pos.y - y))) / gravity
|
2017-06-19 17:52:01 +02:00
|
|
|
if t > time then
|
|
|
|
return
|
|
|
|
end
|
2017-06-11 08:58:13 +02:00
|
|
|
local p = {
|
|
|
|
x = math.floor(vel.x * t + pos.x +.5),
|
|
|
|
y = y,
|
|
|
|
z = math.floor(vel.z * t + pos.z +.5),
|
|
|
|
}
|
|
|
|
pointcount = pointcount+1
|
|
|
|
waypoints[pointcount] = {p, t}
|
|
|
|
if pointcount == max_pointcount then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- smaller and bigger horizonzal pivot
|
|
|
|
local shp, bhp
|
|
|
|
if math.abs(vel.x) > math.abs(vel.z) then
|
|
|
|
shp = "z"
|
|
|
|
bhp = "x"
|
|
|
|
else
|
|
|
|
shp = "x"
|
|
|
|
bhp = "z"
|
|
|
|
end
|
|
|
|
-- put points between the 45° angles
|
|
|
|
local cstart, cdir
|
|
|
|
local cend = math.floor(vel[bhp] * t_fall_start + pos[bhp] +.5)
|
|
|
|
if vel[bhp] > 0 then
|
|
|
|
cstart = math.floor(math.max(pos[bhp],
|
|
|
|
vel[bhp] * t_raise_end + pos[bhp]) +.5)
|
|
|
|
cdir = 1
|
|
|
|
else
|
|
|
|
cstart = math.floor(math.min(pos[bhp],
|
|
|
|
vel[bhp] * t_raise_end + pos[bhp]) +.5)
|
|
|
|
cdir = -1
|
|
|
|
end
|
|
|
|
for i = cstart, cend, cdir do
|
|
|
|
local t = (i - pos[bhp]) / vel[bhp]
|
2017-06-19 17:52:01 +02:00
|
|
|
if t > time then
|
|
|
|
return
|
|
|
|
end
|
2017-06-11 08:58:13 +02:00
|
|
|
local p = {
|
|
|
|
[bhp] = i,
|
|
|
|
y = math.floor(-0.5 * gravity * t * t + vel.y * t + pos.y +.5),
|
|
|
|
[shp] = math.floor(vel[shp] * t + pos[shp] +.5),
|
|
|
|
}
|
|
|
|
pointcount = pointcount+1
|
|
|
|
waypoints[pointcount] = {p, t}
|
|
|
|
if pointcount == max_pointcount then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- put points from after the 45° angle
|
2017-06-18 13:47:54 +02:00
|
|
|
local y = yswitch
|
|
|
|
if vel.y < 0
|
|
|
|
and pos.y < yswitch then
|
|
|
|
y = pos.y
|
|
|
|
end
|
2017-06-19 17:52:01 +02:00
|
|
|
y = math.floor(y +.5)
|
2017-06-11 08:58:13 +02:00
|
|
|
while pointcount < max_pointcount do
|
2017-06-18 13:47:54 +02:00
|
|
|
local t = (vel.y +
|
|
|
|
math.sqrt(vel.y^2 + 2 * gravity * (pos.y - y))) / gravity
|
2017-06-19 17:52:01 +02:00
|
|
|
if t > time then
|
|
|
|
return
|
|
|
|
end
|
2017-06-11 08:58:13 +02:00
|
|
|
local p = {
|
|
|
|
x = math.floor(vel.x * t + pos.x +.5),
|
|
|
|
y = y,
|
2017-06-11 09:32:04 +02:00
|
|
|
z = math.floor(vel.z * t + pos.z +.5),
|
2017-06-11 08:58:13 +02:00
|
|
|
}
|
|
|
|
pointcount = pointcount+1
|
|
|
|
waypoints[pointcount] = {p, t}
|
|
|
|
y = y-1
|
|
|
|
end
|
|
|
|
end
|
2017-06-18 13:47:54 +02:00
|
|
|
--[[
|
|
|
|
minetest.override_item("default:axe_wood", {
|
|
|
|
on_use = function(_, player)
|
|
|
|
local dir = player:get_look_dir()
|
|
|
|
local pos = player:getpos()
|
|
|
|
local grav = 0.03
|
2017-06-19 17:52:01 +02:00
|
|
|
local ps = vector.throw_parabola(pos, dir, grav, 80)
|
2017-06-18 13:47:54 +02:00
|
|
|
for i = 1,#ps do
|
2017-06-19 17:52:01 +02:00
|
|
|
minetest.set_node(ps[i], {name="default:stone"})
|
2017-06-18 13:47:54 +02:00
|
|
|
end
|
|
|
|
--~ for t = 0,50,3 do
|
|
|
|
--~ local p = {
|
|
|
|
--~ x = dir.x * t + pos.x,
|
|
|
|
--~ y = -0.5*grav*t*t + dir.y*t + pos.y,
|
|
|
|
--~ z = dir.z * t + pos.z
|
|
|
|
--~ }
|
|
|
|
--~ minetest.set_node(p, {name="default:sandstone"})
|
2017-06-11 09:32:04 +02:00
|
|
|
--~ end
|
2017-06-18 13:47:54 +02:00
|
|
|
end,
|
|
|
|
})--]]
|
2017-06-11 08:58:13 +02:00
|
|
|
|
2017-06-19 17:52:01 +02:00
|
|
|
function funcs.throw_parabola(pos, vel, gravity, point_count, time)
|
2017-06-11 08:58:13 +02:00
|
|
|
local waypoints = {}
|
2017-06-19 17:52:01 +02:00
|
|
|
get_parabola_points(pos, vel, gravity, waypoints, point_count,
|
|
|
|
time or math.huge)
|
2017-06-11 08:58:13 +02:00
|
|
|
local ps = {}
|
2017-06-19 17:52:01 +02:00
|
|
|
local ptscnt = #waypoints
|
|
|
|
local i = 1
|
|
|
|
while i < ptscnt do
|
|
|
|
local p,t = unpack(waypoints[i])
|
|
|
|
i = i+1
|
|
|
|
local p2,t2 = unpack(waypoints[i])
|
|
|
|
ps[#ps+1] = p
|
|
|
|
local dist = vector.distance(p, p2)
|
|
|
|
if dist < 1.1 then
|
|
|
|
if dist < 0.9 then
|
|
|
|
-- same position
|
|
|
|
i = i+1
|
|
|
|
end
|
|
|
|
-- touching
|
|
|
|
elseif dist < 1.7 then
|
|
|
|
-- common edge
|
|
|
|
-- get a list of possible positions between
|
|
|
|
local diff = vector.subtract(p2, p)
|
|
|
|
local possible_positions = {}
|
|
|
|
for i,v in pairs(diff) do
|
|
|
|
if v ~= 0 then
|
|
|
|
local p = vector.new(p)
|
|
|
|
p[i] = p[i] + v
|
|
|
|
possible_positions[#possible_positions+1] = p
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- test which one fits best
|
|
|
|
t = 0.5 * (t + t2)
|
|
|
|
local near_p = {
|
|
|
|
x = vel.x * t + pos.x,
|
|
|
|
y = -0.5 * gravity * t * t + vel.y * t + pos.y,
|
|
|
|
z = vel.z * t + pos.z,
|
|
|
|
}
|
|
|
|
local d = math.huge
|
|
|
|
for i = 1,2 do
|
|
|
|
local pos = possible_positions[i]
|
|
|
|
local dist = vector.distance(pos, near_p)
|
|
|
|
if dist < d then
|
|
|
|
p = pos
|
|
|
|
d = dist
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- add it
|
|
|
|
ps[#ps+1] = p
|
|
|
|
elseif dist < 1.8 then
|
|
|
|
-- common vertex
|
|
|
|
for k = 1,2 do
|
|
|
|
-- get a list of possible positions between
|
|
|
|
local diff = vector.subtract(p2, p)
|
|
|
|
local possible_positions = {}
|
|
|
|
for i,v in pairs(diff) do
|
|
|
|
if v ~= 0 then
|
|
|
|
local p = vector.new(p)
|
|
|
|
p[i] = p[i] + v
|
|
|
|
possible_positions[#possible_positions+1] = p
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- test which one fits best
|
|
|
|
t = k / 3 * (t + t2)
|
|
|
|
local near_p = {
|
|
|
|
x = vel.x * t + pos.x,
|
|
|
|
y = -0.5 * gravity * t * t + vel.y * t + pos.y,
|
|
|
|
z = vel.z * t + pos.z,
|
|
|
|
}
|
|
|
|
local d = math.huge
|
|
|
|
assert(#possible_positions == 4-k, "how, number positions?")
|
|
|
|
for i = 1,4-k do
|
|
|
|
local pos = possible_positions[i]
|
|
|
|
local dist = vector.distance(pos, near_p)
|
|
|
|
if dist < d then
|
|
|
|
p = pos
|
|
|
|
d = dist
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- add it
|
|
|
|
ps[#ps+1] = p
|
|
|
|
end
|
|
|
|
else
|
|
|
|
minetest.log("warning", "[vector_extras] A gap: " .. dist)
|
|
|
|
--~ error("A gap, it's a gap!: " .. dist)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if i == ptscnt then
|
|
|
|
ps[#ps+1] = waypoints[i]
|
2017-06-11 08:58:13 +02:00
|
|
|
end
|
|
|
|
return ps
|
|
|
|
end
|
|
|
|
|
2015-04-25 09:54:07 +02:00
|
|
|
function funcs.chunkcorner(pos)
|
2014-03-05 21:02:25 +01:00
|
|
|
return {x=pos.x-pos.x%16, y=pos.y-pos.y%16, z=pos.z-pos.z%16}
|
|
|
|
end
|
|
|
|
|
2015-06-25 02:39:03 +02:00
|
|
|
function funcs.point_distance_minmax(p1, p2)
|
|
|
|
local p1 = vector.new(p1)
|
|
|
|
local p2 = vector.new(p2)
|
|
|
|
local min, max, vmin, vmax, num
|
|
|
|
for _,i in ipairs({"x", "y", "z"}) do
|
|
|
|
num = math.abs(p1[i] - p2[i])
|
|
|
|
if not vmin or num < vmin then
|
|
|
|
vmin = num
|
|
|
|
min = i
|
|
|
|
end
|
|
|
|
if not vmax or num > vmax then
|
|
|
|
vmax = num
|
|
|
|
max = i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return min, max
|
|
|
|
end
|
|
|
|
|
|
|
|
function funcs.collision(p1, p2)
|
2017-06-19 17:52:01 +02:00
|
|
|
local clear, node_pos, collision_pos, max, dmax, dcmax, pt
|
2015-06-25 02:39:03 +02:00
|
|
|
clear, node_pos = minetest.line_of_sight(p1, p2)
|
|
|
|
if clear then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
collision_pos = {}
|
|
|
|
min, max = funcs.point_distance_minmax(node_pos, p2)
|
|
|
|
if node_pos[max] > p2[max] then
|
|
|
|
collision_pos[max] = node_pos[max] - 0.5
|
|
|
|
else
|
|
|
|
collision_pos[max] = node_pos[max] + 0.5
|
|
|
|
end
|
|
|
|
dmax = p2[max] - node_pos[max]
|
|
|
|
dcmax = p2[max] - collision_pos[max]
|
|
|
|
pt = dcmax/dmax
|
|
|
|
|
|
|
|
for _,i in ipairs({"x", "y", "z"}) do
|
|
|
|
collision_pos[i] = p2[i] - (p2[i] - node_pos[i]) * pt
|
|
|
|
end
|
|
|
|
return true, collision_pos, node_pos
|
|
|
|
end
|
|
|
|
|
2015-08-30 15:51:56 +02:00
|
|
|
function funcs.update_minp_maxp(minp, maxp, pos)
|
|
|
|
for _,i in pairs({"z", "y", "x"}) do
|
|
|
|
minp[i] = math.min(minp[i], pos[i])
|
|
|
|
maxp[i] = math.max(maxp[i], pos[i])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function funcs.quickadd(pos, z,y,x)
|
|
|
|
if z then
|
|
|
|
pos.z = pos.z+z
|
|
|
|
end
|
|
|
|
if y then
|
|
|
|
pos.y = pos.y+y
|
|
|
|
end
|
|
|
|
if x then
|
|
|
|
pos.x = pos.x+x
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function funcs.unpack(pos)
|
|
|
|
return pos.z, pos.y, pos.x
|
|
|
|
end
|
|
|
|
|
2016-04-23 11:09:10 +02:00
|
|
|
function funcs.get_max_coord(vec)
|
|
|
|
if vec.x < vec.y then
|
|
|
|
if vec.y < vec.z then
|
|
|
|
return "z"
|
|
|
|
end
|
|
|
|
return "y"
|
|
|
|
end
|
|
|
|
if vec.x < vec.z then
|
|
|
|
return "z"
|
|
|
|
end
|
|
|
|
return "x"
|
|
|
|
end
|
|
|
|
|
2016-01-31 12:17:38 +01:00
|
|
|
function funcs.get_max_coords(pos)
|
|
|
|
if pos.x < pos.y then
|
|
|
|
if pos.y < pos.z then
|
|
|
|
return "z", "y", "x"
|
|
|
|
end
|
|
|
|
if pos.x < pos.z then
|
|
|
|
return "y", "z", "x"
|
|
|
|
end
|
|
|
|
return "y", "x", "z"
|
|
|
|
end
|
|
|
|
if pos.x < pos.z then
|
|
|
|
return "z", "x", "y"
|
|
|
|
end
|
|
|
|
if pos.y < pos.z then
|
|
|
|
return "x", "z", "y"
|
|
|
|
end
|
|
|
|
return "x", "y", "z"
|
|
|
|
end
|
|
|
|
|
2016-05-27 17:20:57 +02:00
|
|
|
function funcs.serialize(vec)
|
|
|
|
return "{x=" .. vec.x .. ",y=" .. vec.y .. ",z=" .. vec.z .. "}"
|
|
|
|
end
|
|
|
|
|
2015-08-30 15:51:56 +02:00
|
|
|
|
2017-11-26 11:22:24 +01:00
|
|
|
vector_extras_functions = funcs
|
|
|
|
|
2017-12-25 20:52:09 +01:00
|
|
|
dofile(path .. "/legacy.lua")
|
2016-02-18 18:46:22 +01:00
|
|
|
--dofile(minetest.get_modpath("vector_extras").."/vector_meta.lua")
|
2014-03-08 18:09:09 +01:00
|
|
|
|
2017-11-26 11:22:24 +01:00
|
|
|
vector_extras_functions = nil
|
|
|
|
|
|
|
|
|
2015-04-25 09:54:07 +02:00
|
|
|
for name,func in pairs(funcs) do
|
2016-02-18 18:46:22 +01:00
|
|
|
if vector[name] then
|
2017-11-26 11:22:24 +01:00
|
|
|
minetest.log("error", "[vector_extras] vector."..name..
|
|
|
|
" already exists.")
|
2016-02-18 18:46:22 +01:00
|
|
|
else
|
|
|
|
vector[name] = func
|
|
|
|
end
|
2015-04-25 09:54:07 +02:00
|
|
|
end
|