Village: Fix entities spawning on same pos
This commit is contained in:
parent
482d856233
commit
6f761e2323
@ -161,6 +161,8 @@ end
|
|||||||
|
|
||||||
function util.choice_element(tab, pr)
|
function util.choice_element(tab, pr)
|
||||||
-- return a random element of the given table
|
-- return a random element of the given table
|
||||||
|
-- 2nd return value is index of chosen element
|
||||||
|
-- Returns nil if table is empty
|
||||||
|
|
||||||
local choices = {}
|
local choices = {}
|
||||||
|
|
||||||
@ -170,11 +172,13 @@ function util.choice_element(tab, pr)
|
|||||||
|
|
||||||
if #choices <= 0 then return end
|
if #choices <= 0 then return end
|
||||||
|
|
||||||
|
local rnd
|
||||||
if pr then
|
if pr then
|
||||||
return choices[pr:next(1, #choices)]
|
rnd = pr:next(1, #choices)
|
||||||
else
|
else
|
||||||
return choices[math.random(1, #choices)]
|
rnd = math.random(1, #choices)
|
||||||
end
|
end
|
||||||
|
return choices[rnd], rnd
|
||||||
end
|
end
|
||||||
|
|
||||||
-- util.split function taken from a StackOverflow answer.
|
-- util.split function taken from a StackOverflow answer.
|
||||||
|
@ -435,10 +435,15 @@ function village.spawn_chunk(pos, orient, replace, pr, chunktype, nofill)
|
|||||||
if #ent_spawns > 0 then
|
if #ent_spawns > 0 then
|
||||||
for ent, amt in pairs(chunkdef.entities) do
|
for ent, amt in pairs(chunkdef.entities) do
|
||||||
for j = 1, pr:next(1, amt) do
|
for j = 1, pr:next(1, amt) do
|
||||||
local spawn = util.choice_element(ent_spawns, pr)
|
if #ent_spawns == 0 then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
local spawn, index = util.choice_element(ent_spawns, pr)
|
||||||
if spawn ~= nil then
|
if spawn ~= nil then
|
||||||
spawn.y = spawn.y + 1.6
|
spawn.y = spawn.y + 1.6
|
||||||
minetest.add_entity(spawn, ent)
|
minetest.add_entity(spawn, ent)
|
||||||
|
-- Prevent spawning on same tile
|
||||||
|
table.remove(ent_spawns, index)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user