diff --git a/.luacheckrc b/.luacheckrc index ef68d8d..be685ba 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -1,5 +1,11 @@ read_globals = { -- Defined by Minetest - "minetest", "PseudoRandom", "VoxelArea", "string", "dump", "math" + "minetest", "PseudoRandom", "VoxelArea", "string", "dump", "math", + vector = { + fields = { + "add", "cross", "direction", "distance", "dot", "multiply", + "new", "normalize", "round", "subtract", + "from_number", "get_max_coord", "twoline", "threeline", "rayIter" + } + } } -globals = {"vector", "vector_extras_functions"} diff --git a/doc.md b/doc.md index c154a24..e3684ce 100644 --- a/doc.md +++ b/doc.md @@ -134,21 +134,3 @@ See e.g. `minetest.line_of_sight`. * If `time` is omitted, it uses the current time. * This function does not yet support the moon; at night it simply returns `nil`. - - -## Helpers which I don't recommend to use now - -* `vector.pos_to_string(pos)`: returns a string - * It is similar to `minetest.pos_to_string`; it uses a different format: - `"("..pos.x.."|"..pos.y.."|"..pos.z..")"` -* `vector.zero` - * The zero vector `{x=0, y=0, z=0}` -* `vector.quickadd(pos, [z],[y],[x])` - * Adds values to the vector components in-place - - -## Deprecated helpers - -* `vector.plane` - * should be removed soon; it should have done the same as vector.triangle - diff --git a/init.lua b/init.lua index 14563d1..bcd4a9c 100644 --- a/init.lua +++ b/init.lua @@ -2,10 +2,6 @@ local path = minetest.get_modpath"vector_extras" local funcs = {} -function funcs.pos_to_string(pos) - return "("..pos.x.."|"..pos.y.."|"..pos.z..")" -end - local r_corr = 0.25 --remove a bit more nodes (if shooting diagonal) to let it -- look like a hole (sth like antialiasing) @@ -270,63 +266,6 @@ function funcs.pnorm(v, p) return (math.abs(v.x)^p + math.abs(v.y)^p + math.abs(v.z)^p)^(1 / p) end ---not optimized ---local areas = {} -function funcs.plane(ps) - -- sort positions and imagine the first one (A) as vector.zero - vector.sort_positions(ps) - 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 - cube_p1 = vector.apply(cube_p1, math.floor) - cube_p2 = vector.apply(cube_p2, math.ceil) - - local vn = vector.normalize(vector.cross(B, C)) - - local nAB = vector.normalize(B) - local nAC = vector.normalize(C) - local angle_BAC = math.acos(vector.dot(nAB, nAC)) - - local nBA = vector.multiply(nAB, -1) - local nBC = vector.normalize(vector.subtract(C, B)) - local angle_ABC = math.acos(vector.dot(nBA, nBC)) - - 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} - local n = -vector.dot(p, vn)/vector.dot(vn, vn) - if math.abs(n) <= 0.5 then - local ep = vector.add(p, vector.multiply(vn, n)) - local nep = vector.normalize(ep) - local angle_BAep = math.acos(vector.dot(nAB, nep)) - local angle_CAep = math.acos(vector.dot(nAC, nep)) - local angldif = angle_BAC - (angle_BAep+angle_CAep) - if math.abs(angldif) < 0.001 then - ep = vector.subtract(ep, B) - nep = vector.normalize(ep) - local angle_ABep = math.acos(vector.dot(nBA, nep)) - local angle_CBep = math.acos(vector.dot(nBC, nep)) - angldif = angle_ABC - (angle_ABep+angle_CBep) - if math.abs(angldif) < 0.001 then - table.insert(ps, vector.add(pos, p)) - end - end - end - end - end - end - return ps -end - function funcs.straightdelay(s, v, a) if not a then return s/v @@ -334,17 +273,6 @@ function funcs.straightdelay(s, v, a) return (math.sqrt(v*v+2*a*s)-v)/a end --- override vector.zero --- builtin used not to have the vector.zero function. to keep compatibility, --- vector.zero has to be a 0-vector and vector.zero() has to return a 0-vector --- => we make a callable 0-vector table -if not vector.zero then - vector.zero = {x = 0, y = 0, z = 0} -else - local old_zero = vector.zero - vector.zero = setmetatable({x = 0, y = 0, z = 0}, {__call = old_zero}) -end - function funcs.sun_dir(time) if not time then time = minetest.get_timeofday() @@ -901,18 +829,6 @@ function funcs.update_minp_maxp(minp, maxp, pos) 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 @@ -1021,20 +937,12 @@ function funcs.triangle(pos1, pos2, pos3) return points, n, barycentric_coords end - -vector_extras_functions = funcs - -dofile(path .. "/legacy.lua") ---dofile(minetest.get_modpath("vector_extras").."/vector_meta.lua") - -vector_extras_functions = nil - - for name,func in pairs(funcs) do if vector[name] then minetest.log("error", "[vector_extras] vector."..name.. " already exists.") else + -- luacheck: globals vector vector[name] = func end end diff --git a/legacy.lua b/legacy.lua deleted file mode 100644 index 39dd7c0..0000000 --- a/legacy.lua +++ /dev/null @@ -1,94 +0,0 @@ -local funcs = vector_extras_functions - -function funcs.scalar(v1, v2) - minetest.log("deprecated", "[vector_extras] vector.scalar is " .. - "deprecated, use vector.dot instead.") - return vector.dot(v1, v2) -end - -function funcs.get_data_from_pos(tab, z,y,x) - minetest.log("deprecated", "[vector_extras] get_data_from_pos is " .. - "deprecated, use the minetest pos hash function instead.") - local data = tab[z] - if data then - data = data[y] - if data then - return data[x] - end - end -end - -function funcs.set_data_to_pos(tab, z,y,x, data) - minetest.log("deprecated", "[vector_extras] set_data_to_pos is " .. - "deprecated, use the minetest pos hash function instead.") - if tab[z] then - if tab[z][y] then - tab[z][y][x] = data - return - end - tab[z][y] = {[x] = data} - return - end - tab[z] = {[y] = {[x] = data}} -end - -function funcs.set_data_to_pos_optional(tab, z,y,x, data) - minetest.log("deprecated", "[vector_extras] set_data_to_pos_optional is " .. - "deprecated, use the minetest pos hash function instead.") - if vector.get_data_from_pos(tab, z,y,x) ~= nil then - return - end - funcs.set_data_to_pos(tab, z,y,x, data) -end - -function funcs.remove_data_from_pos(tab, z,y,x) - minetest.log("deprecated", "[vector_extras] remove_data_from_pos is " .. - "deprecated, use the minetest pos hash function instead.") - if vector.get_data_from_pos(tab, z,y,x) == nil then - return - end - tab[z][y][x] = nil - if not next(tab[z][y]) then - tab[z][y] = nil - end - if not next(tab[z]) then - tab[z] = nil - end -end - -function funcs.get_data_pos_table(tab) - minetest.log("deprecated", "[vector_extras] get_data_pos_table likely " .. - "is deprecated, use the minetest pos hash function instead.") - local t,n = {},1 - local minz, miny, minx, maxz, maxy, maxx - for z,yxs in pairs(tab) do - if not minz then - minz = z - maxz = z - else - minz = math.min(minz, z) - maxz = math.max(maxz, z) - end - for y,xs in pairs(yxs) do - if not miny then - miny = y - maxy = y - else - miny = math.min(miny, y) - maxy = math.max(maxy, y) - end - for x,v in pairs(xs) do - if not minx then - minx = x - maxx = x - else - minx = math.min(minx, x) - maxx = math.max(maxx, x) - end - t[n] = {z,y,x, v} - n = n+1 - end - end - end - return t, {x=minx, y=miny, z=minz}, {x=maxx, y=maxy, z=maxz}, n-1 -end diff --git a/vector_meta.lua b/vector_meta.lua deleted file mode 100644 index 8a65006..0000000 --- a/vector_meta.lua +++ /dev/null @@ -1,183 +0,0 @@ -vector.meta = vector.meta or {} -vector.meta.nodes = {} - -vector.meta.nodes_file = { - load = function() - local nodesfile = io.open(minetest.get_worldpath()..'/vector_nodes.txt', "r") - if nodesfile then - local contents = nodesfile:read('*all') - io.close(nodesfile) - if contents ~= nil then - local lines = string.split(contents, "\n") - for _,entry in ipairs(lines) do - local name, px, py, pz, meta = unpack(string.split(entry, "°")) - vector.meta.set_node({x=px, y=py, z=pz}, name, meta) - end - end - end - end, - save = function() --WRITE CHANGES TO FILE - local output = '' - for x,ys in pairs(vector.meta.nodes) do - for y,zs in pairs(ys) do - for z,names in pairs(zs) do - for name,meta in pairs(names) do - output = name.."°"..x.."°"..y.."°"..z.."°"..dump(meta).."\n" - end - end - end - end - local f = io.open(minetest.get_worldpath()..'/vector_nodes.txt', "w") - f:write(output) - io.close(f) - end -} - -local function table_empty(tab) --looks if it's an empty table - if next(tab) == nil then - return true - end - return false -end - -function vector.meta.nodes_info() --returns an info string of the node table - local tmp = "[vector] "..dump(vector.meta.nodes).."\n[vector]:\n" - for x,a in pairs(vector.meta.nodes) do - for y,b in pairs(a) do - for z,c in pairs(b) do - for name,meta in pairs(c) do - tmp = tmp..">\t"..name.." "..x.." "..y.." "..z.." "..dump(meta).."\n" - end - end - end - end - return tmp -end - -function vector.meta.clean_node_table() --replaces {} with nil - local again = true - while again do - again = false - for x,ys in pairs(vector.meta.nodes) do - if table_empty(ys) then - vector.meta.nodes[x] = nil - again = true - else - for y,zs in pairs(ys) do - if table_empty(zs) then - vector.meta.nodes[x][y] = nil - again = true - else - for z,names in pairs(zs) do - if table_empty(names) then - vector.meta.nodes[x][y][z] = nil - again = true - else - for name,meta in pairs(names) do - if table_empty(meta) - or meta == "" then - vector.meta.nodes[x][y][z][name] = nil - again = true - end - end - end - end - end - end - end - end - end -end - -function vector.meta.complete_node_table(pos, name) --neccesary because tab[1] wouldn't work if tab is not a table - local tmp = vector.meta.nodes[pos.x] - if not tmp then - vector.meta.nodes[pos.x] = {} - end - tmp = vector.meta.nodes[pos.x][pos.y] - if not tmp then - vector.meta.nodes[pos.x][pos.y] = {} - end - tmp = vector.meta.nodes[pos.x][pos.y][pos.z] - if not tmp then - vector.meta.nodes[pos.x][pos.y][pos.z] = {} - end - tmp = vector.meta.nodes[pos.x][pos.y][pos.z][name] - if not tmp then - vector.meta.nodes[pos.x][pos.y][pos.z][name] = {} - end -end - -function vector.meta.get_node(pos, name) - if not pos then - return false - end - local tmp = vector.meta.nodes[pos.x] - if not tmp - or table_empty(tmp) then - return false - end - tmp = vector.meta.nodes[pos.x][pos.y] - if not tmp - or table_empty(tmp) then - return false - end - tmp = vector.meta.nodes[pos.x][pos.y][pos.z] - if not tmp - or table_empty(tmp) then - return false - end - - -- if name isn't mentioned, just look if there's a node - if not name then - return true - end - - tmp = vector.meta.nodes[pos.x][pos.y][pos.z][name] - if not tmp - or table_empty(tmp) then - return false - end - return tmp -end - -function vector.meta.remove_node(pos) - if not pos then - return false - end - if vector.meta.get_node(pos) then - vector.meta.nodes[pos.x][pos.y][pos.z] = nil - local xarr = vector.meta.nodes[pos.x] - if table_empty(xarr[pos.y]) then - vector.meta.nodes[pos.x][pos.y] = nil - end - if table_empty(xarr) then - vector.meta.nodes[pos.x] = nil - end - else - print("[vector_extras] Warning: The node at "..vector.pos_to_string(pos).." wasn't stored in vector.meta.nodes.") - end -end - -function vector.meta.set_node(pos, name, meta) - if not (name or pos) then - return false - end - vector.meta.complete_node_table(pos, name) - meta = meta or true - vector.meta.nodes[pos.x][pos.y][pos.z][name] = meta -end - -minetest.register_chatcommand('cleanvectormetatable',{ - description = 'Tidy up it.', - params = "", - privs = {}, - func = function(name) - vector.meta.clean_node_table() - local tmp = vector.meta.nodes_info() - minetest.chat_send_player(name, tmp) - print("[vector_extras] "..tmp) - end -}) - -vector.meta.nodes_file.load()