From 02df8d2d463e6d226fa8528493991abd97c118bb Mon Sep 17 00:00:00 2001 From: Hybrid Dog Date: Sun, 26 Nov 2017 11:22:24 +0100 Subject: [PATCH] Add new functions and remove deprecated/inefficient --- init.lua | 118 ++++++++++++++++------------------------------------- legacy.lua | 88 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 83 deletions(-) create mode 100644 legacy.lua diff --git a/init.lua b/init.lua index 203fac6..4d63f9d 100644 --- a/init.lua +++ b/init.lua @@ -1,4 +1,4 @@ -local load_time_start = os.clock() +local load_time_start = minetest.get_us_time() local funcs = {} @@ -207,7 +207,8 @@ function funcs.threeline(x, y, z) return line end if x ~= math.floor(x) then - minetest.log("error", "[vector_extras] INFO: The position used for vector.threeline isn't round.") + minetest.log("error", "[vector_extras] INFO: The position used for " .. + "vector.threeline isn't round.") end local two_line = vector.twoline(x, y) line = {} @@ -266,6 +267,19 @@ function funcs.cross(v1, v2) } end +-- 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 + --not optimized --local areas = {} function funcs.plane(ps) @@ -392,7 +406,7 @@ function funcs.explosion_table(r) return table end - local t1 = os.clock() + --~ local t1 = os.clock() local tab, n = {}, 1 local tmp = r*r + r @@ -413,7 +427,7 @@ function funcs.explosion_table(r) end end explosion_tables[r] = tab - minetest.log("info", string.format("[vector_extras] table created after ca. %.2fs", os.clock() - t1)) + --~ minetest.log("info", string.format("[vector_extras] table created after ca. %.2fs", os.clock() - t1)) return tab end @@ -885,83 +899,6 @@ function funcs.collision(p1, p2) return true, collision_pos, node_pos end -function funcs.get_data_from_pos(tab, z,y,x) - 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) - 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) - 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) - 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) - 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 - function funcs.update_minp_maxp(minp, maxp, pos) for _,i in pairs({"z", "y", "x"}) do minp[i] = math.min(minp[i], pos[i]) @@ -1022,14 +959,29 @@ function funcs.serialize(vec) end +vector_extras_functions = funcs + +local path = minetest.get_modpath"vector_extras" +dofile(path .. "/vector_meta.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.") + minetest.log("error", "[vector_extras] vector."..name.. + " already exists.") else vector[name] = func end end -minetest.log("info", string.format("[vector_extras] loaded after ca. %.2fs", os.clock() - load_time_start)) + +local time = (minetest.get_us_time() - load_time_start) / 1000000 +local msg = "[vector_extras] loaded after ca. " .. time .. " seconds." +if time > 0.01 then + print(msg) +else + minetest.log("info", msg) +end diff --git a/legacy.lua b/legacy.lua new file mode 100644 index 0000000..8f530d9 --- /dev/null +++ b/legacy.lua @@ -0,0 +1,88 @@ +local funcs = vector_extras_functions + +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