mirror of
https://git.minetest.land/VoxeLibre/VoxeLibre.git
synced 2024-11-04 15:21:06 +01:00
Refactor spawn_group() so that each spawn point can only be used once and protection is respected
This commit is contained in:
parent
133ea93259
commit
5b29aac866
1 changed files with 28 additions and 11 deletions
|
@ -778,24 +778,41 @@ local function build_state_for_position(pos, parent_state)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function spawn_group(p, mob, spawn_on, amount_to_spawn, parent_state)
|
local function spawn_group(p, mob, spawn_on, amount_to_spawn, parent_state)
|
||||||
local nn= find_nodes_in_area_under_air(vector.offset(p,-5,-3,-5),vector.offset(p,5,3,5),spawn_on)
|
-- Find possible spawn locations and shuffle the list
|
||||||
local o
|
local nn = find_nodes_in_area_under_air(vector.offset(p,-5,-3,-5), vector.offset(p,5,3,5), spawn_on)
|
||||||
table.shuffle(nn)
|
|
||||||
if not nn or #nn < 1 then
|
if not nn or #nn < 1 then
|
||||||
nn = {}
|
nn = {}
|
||||||
table.insert(nn,p)
|
table.insert(nn,p)
|
||||||
end
|
end
|
||||||
|
table.shuffle(nn)
|
||||||
|
--minetest.log("Spawn point list: "..dump(nn))
|
||||||
|
|
||||||
for i = 1, amount_to_spawn do
|
-- Use the first amount_to_spawn positions to spawn mobs. If a spawn position is protected,
|
||||||
local sp = vector.offset(nn[math.random(#nn)],0,1,0)
|
-- it is removed from the list and not counted against the spawn amount. Only one mob will
|
||||||
local state, node = build_state_for_position(sp, parent_state)
|
-- spawn in a given spot.
|
||||||
|
local o
|
||||||
|
while amount_to_spawn > 0 and #nn > 0 do
|
||||||
|
-- Select the next spawn position
|
||||||
|
local sp = vector.offset(nn[#nn],0,1,0)
|
||||||
|
nn[#nn] = nil
|
||||||
|
|
||||||
if spawn_check(sp, state, node, mob) then
|
-- Spawning prohibited in protected areas
|
||||||
if mob.type_of_spawning == "water" then
|
if not (spawn_protected and minetest.is_protected(sp, "")) then
|
||||||
sp = get_water_spawn(sp)
|
local state, node = build_state_for_position(sp, parent_state)
|
||||||
|
|
||||||
|
if spawn_check(sp, state, node, mob) then
|
||||||
|
if mob.type_of_spawning == "water" then
|
||||||
|
sp = get_water_spawn(sp)
|
||||||
|
end
|
||||||
|
|
||||||
|
--minetest.log("Using spawn point "..vector.to_string(sp))
|
||||||
|
|
||||||
|
o = mcl_mobs.spawn(sp,mob.name)
|
||||||
|
if o then
|
||||||
|
amount_to_spawn = amount_to_spawn - 1
|
||||||
|
dbg_spawn_succ = dbg_spawn_succ + 1
|
||||||
|
end
|
||||||
end
|
end
|
||||||
o = mcl_mobs.spawn(sp,mob.name)
|
|
||||||
if o then dbg_spawn_succ = dbg_spawn_succ + 1 end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return o
|
return o
|
||||||
|
|
Loading…
Reference in a new issue