Skip to content

Commit 171faec

Browse files
authored
Optimize move_entities_globalstep_part1 (#134)
1 parent e9a9bd7 commit 171faec

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

luaentity.lua

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,36 +67,41 @@ end
6767
minetest.register_on_shutdown(write_entities)
6868
luaentity.entities_index = 0
6969

70-
local function get_blockpos(pos)
71-
return {x = math.floor(pos.x / 16),
72-
y = math.floor(pos.y / 16),
73-
z = math.floor(pos.z / 16)}
74-
end
75-
7670
local move_entities_globalstep_part1
7771
local is_active
7872

7973
if pipeworks.use_real_entities then
8074
local active_blocks = {} -- These only contain active blocks near players (i.e., not forceloaded ones)
8175

76+
local function get_blockpos(pos)
77+
return {x = math.floor(pos.x / 16),
78+
y = math.floor(pos.y / 16),
79+
z = math.floor(pos.z / 16)}
80+
end
81+
8282
move_entities_globalstep_part1 = function(dtime)
8383
local active_block_range = tonumber(minetest.settings:get("active_block_range")) or 2
84-
local new_active_blocks = {}
84+
for key in pairs(active_blocks) do
85+
active_blocks[key] = nil
86+
end
8587
for _, player in ipairs(minetest.get_connected_players()) do
8688
local blockpos = get_blockpos(player:get_pos())
87-
local minp = vector.subtract(blockpos, active_block_range)
88-
local maxp = vector.add(blockpos, active_block_range)
89-
90-
for x = minp.x, maxp.x do
91-
for y = minp.y, maxp.y do
92-
for z = minp.z, maxp.z do
93-
local pos = {x = x, y = y, z = z}
94-
new_active_blocks[minetest.hash_node_position(pos)] = pos
95-
end
96-
end
89+
local minpx = blockpos.x - active_block_range
90+
local minpy = blockpos.y - active_block_range
91+
local minpz = blockpos.z - active_block_range
92+
local maxpx = blockpos.x + active_block_range
93+
local maxpy = blockpos.y + active_block_range
94+
local maxpz = blockpos.z + active_block_range
95+
96+
for x = minpx, maxpx do
97+
for y = minpy, maxpy do
98+
for z = minpz, maxpz do
99+
local pos = {x = x, y = y, z = z}
100+
active_blocks[minetest.hash_node_position(pos)] = true
101+
end
102+
end
97103
end
98104
end
99-
active_blocks = new_active_blocks
100105
-- todo: callbacks on block load/unload
101106
end
102107

0 commit comments

Comments
 (0)