Skip to content

Commit

Permalink
add mapblock calc function / fix entity spawning in inactive areas
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Mar 29, 2021
1 parent 733f4fb commit 895d9e1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
6 changes: 1 addition & 5 deletions blocks/send_mapblock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ minetest.register_node("epic:send_mapblock", {
on_enter = function(pos, meta, player, ctx)
local rel_pos = minetest.string_to_pos(meta:get_string("pos"))
local target_pos = epic.to_absolute_pos(pos, rel_pos)
local block_pos = {
x = math.floor(target_pos.x / 16),
y = math.floor(target_pos.y / 16),
z = math.floor(target_pos.z / 16)
}
local block_pos = epic.get_mapblock_pos(target_pos)
player:send_mapblock(block_pos)
ctx.next()
end
Expand Down
31 changes: 20 additions & 11 deletions blocks/spawn_mob.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,28 @@ local function do_spawn(pos, meta)
local target_pos = epic.to_absolute_pos(pos, minetest.string_to_pos(meta:get_string("pos")))
local mobname = meta:get_string("mobname")

local objs = minetest.get_objects_inside_radius(target_pos, 9)
local count = 0
for _ in ipairs(objs) do
count = count + 1
end
if mobname and mobname ~= "" then
local mapblock_pos = epic.get_mapblock_pos(target_pos)
-- start a transient forceload
-- entities can only operate in active mapblocks
minetest.forceload_block(mapblock_pos, true)

-- spawn mob and free forceload in the next server step
minetest.after(0.1, function()
local objs = minetest.get_objects_inside_radius(target_pos, 9)
local count = 0
for _ in ipairs(objs) do
count = count + 1
end

if count > 16 then
-- too many objects
return
end
if count < 16 then
-- only spawn new entities if not too much objects in that area already
minetest.add_entity(target_pos, mobname)
end

if mobname and mobname ~= "" then
minetest.add_entity(target_pos, mobname)
-- release forceload
minetest.forceload_free_block(mapblock_pos, true)
end)
end
end

Expand Down
8 changes: 8 additions & 0 deletions common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,11 @@ epic.execute_epic = function(player, main_pos, name)
epic.run_hook("on_execute_epic", { player, main_pos, state })

end

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

0 comments on commit 895d9e1

Please sign in to comment.