Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize move_entities_globalstep_part1 #134

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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