Skip to content

Commit

Permalink
Optimize move_entities_globalstep_part1 (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
Emojigit authored Aug 24, 2024
1 parent e9a9bd7 commit 171faec
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions luaentity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,36 +67,41 @@ end
minetest.register_on_shutdown(write_entities)
luaentity.entities_index = 0

local function get_blockpos(pos)
return {x = math.floor(pos.x / 16),
y = math.floor(pos.y / 16),
z = math.floor(pos.z / 16)}
end

local move_entities_globalstep_part1
local is_active

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

local function get_blockpos(pos)
return {x = math.floor(pos.x / 16),
y = math.floor(pos.y / 16),
z = math.floor(pos.z / 16)}
end

move_entities_globalstep_part1 = function(dtime)
local active_block_range = tonumber(minetest.settings:get("active_block_range")) or 2
local new_active_blocks = {}
for key in pairs(active_blocks) do
active_blocks[key] = nil
end
for _, player in ipairs(minetest.get_connected_players()) do
local blockpos = get_blockpos(player:get_pos())
local minp = vector.subtract(blockpos, active_block_range)
local maxp = vector.add(blockpos, active_block_range)

for x = minp.x, maxp.x do
for y = minp.y, maxp.y do
for z = minp.z, maxp.z do
local pos = {x = x, y = y, z = z}
new_active_blocks[minetest.hash_node_position(pos)] = pos
end
end
local minpx = blockpos.x - active_block_range
local minpy = blockpos.y - active_block_range
local minpz = blockpos.z - active_block_range
local maxpx = blockpos.x + active_block_range
local maxpy = blockpos.y + active_block_range
local maxpz = blockpos.z + active_block_range

for x = minpx, maxpx do
for y = minpy, maxpy do
for z = minpz, maxpz do
local pos = {x = x, y = y, z = z}
active_blocks[minetest.hash_node_position(pos)] = true
end
end
end
end
active_blocks = new_active_blocks
-- todo: callbacks on block load/unload
end

Expand Down

0 comments on commit 171faec

Please sign in to comment.