Village: Fix entities spawning on same pos

This commit is contained in:
Wuzzy 2019-09-05 17:55:14 +02:00
parent 482d856233
commit 6f761e2323
2 changed files with 12 additions and 3 deletions

View File

@ -161,6 +161,8 @@ end
function util.choice_element(tab, pr)
-- return a random element of the given table
-- 2nd return value is index of chosen element
-- Returns nil if table is empty
local choices = {}
@ -170,11 +172,13 @@ function util.choice_element(tab, pr)
if #choices <= 0 then return end
local rnd
if pr then
return choices[pr:next(1, #choices)]
rnd = pr:next(1, #choices)
else
return choices[math.random(1, #choices)]
rnd = math.random(1, #choices)
end
return choices[rnd], rnd
end
-- util.split function taken from a StackOverflow answer.

View File

@ -435,10 +435,15 @@ function village.spawn_chunk(pos, orient, replace, pr, chunktype, nofill)
if #ent_spawns > 0 then
for ent, amt in pairs(chunkdef.entities) 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
spawn.y = spawn.y + 1.6
minetest.add_entity(spawn, ent)
-- Prevent spawning on same tile
table.remove(ent_spawns, index)
end
end
end