From 71bb18cd767a867252792d0f1c15b3b52197681e Mon Sep 17 00:00:00 2001 From: AFCMS Date: Tue, 16 Mar 2021 11:07:17 +0100 Subject: [PATCH] walkover optimisation part1 --- mods/CORE/walkover/init.lua | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/mods/CORE/walkover/init.lua b/mods/CORE/walkover/init.lua index 6bbd505d2..a4f657706 100644 --- a/mods/CORE/walkover/init.lua +++ b/mods/CORE/walkover/init.lua @@ -1,4 +1,9 @@ -- register extra flavours of a base nodedef + +local get_connected_players = minetest.get_connected_players +local get_node = minetest.get_node +local vector_add = vector.add + walkover = {} walkover.registered_globals = {} @@ -6,24 +11,37 @@ function walkover.register_global(func) table.insert(walkover.registered_globals, func) end +local on_walk = {} +local registered_globals = {} + +minetest.register_on_mods_loaded(function() + for name,def in pairs(minetest.registered_nodes) do + if def.on_walk_over then + on_walk[name] = def.on_walk_over + end + end + for _,func in ipairs(walkover.registered_globals) do --cache registered globals + table.insert(registered_globals, value) + end +end) + local timer = 0 minetest.register_globalstep(function(dtime) timer = timer + dtime; if timer >= 0.3 then - for _,player in pairs(minetest.get_connected_players()) do + for _,player in ipairs(get_connected_players()) do local pp = player:get_pos() pp.y = math.ceil(pp.y) - local loc = vector.add(pp, {x=0,y=-1,z=0}) + local loc = vector_add(pp, {x=0,y=-1,z=0}) if loc ~= nil then - local nodeiamon = minetest.get_node(loc) + local nodeiamon = get_node(loc) if nodeiamon ~= nil then - local def = minetest.registered_nodes[nodeiamon.name] - if def ~= nil and def.on_walk_over ~= nil then - def.on_walk_over(loc, nodeiamon, player) + if on_walk[nodeiamon.name] then + on_walk[nodeiamon.name](loc, nodeiamon, player) end - for _, func in ipairs(walkover.registered_globals) do + for _,func in ipairs(registered_globals) do func(loc, nodeiamon, player) end end