From 6f761e232305c5596b5b0f5ec19baeeedc1f45c6 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Thu, 5 Sep 2019 17:55:14 +0200 Subject: [PATCH] Village: Fix entities spawning on same pos --- mods/util/init.lua | 8 ++++++-- mods/village/generate.lua | 7 ++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/mods/util/init.lua b/mods/util/init.lua index 5f948d9..ab8f6ab 100644 --- a/mods/util/init.lua +++ b/mods/util/init.lua @@ -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. diff --git a/mods/village/generate.lua b/mods/village/generate.lua index d69b133..fdadf03 100644 --- a/mods/village/generate.lua +++ b/mods/village/generate.lua @@ -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