diff --git a/mods/CORE/mcl_util/init.lua b/mods/CORE/mcl_util/init.lua index 15f935a41..6143bfb41 100644 --- a/mods/CORE/mcl_util/init.lua +++ b/mods/CORE/mcl_util/init.lua @@ -48,16 +48,18 @@ function table.pairs_by_keys(t, f) return iter end -function table.pull_random_items(table) +-- Removes one element randomly selected from the array section of the table and +-- returns it, or nil if there are no elements in the array section of the table +function table.remove_random_element(table) local count = #table - return function() - local idx = math.random(count) - local res = table[idx] - table[idx] = table[count] - table[count] = nil - count = count - 1 - return res - end + if count == 0 then return nil end + + local idx = math.random(count) + local res = table[idx] + table[idx] = table[count] + table[count] = nil + count = count - 1 + return res end local LOGGING_ON = minetest.settings:get_bool("mcl_logging_default", false) diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index 8a2e099cf..d5ebe8788 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -574,7 +574,7 @@ local function has_room(self, pos) nodes = nodes, })) ]] - if n == ( dx * dy * dz ) then + if n == dx * dy * dz then return true end @@ -582,7 +582,7 @@ local function has_room(self, pos) if not minetest.get_node_boxes then return false end -- Check if it's possible for a sub-node space check to succeed - local needed_in_bottom_section = (dx * dz * ( dy - 1)) + local needed_in_bottom_section = dx * ( dy - 1) * dz if n < needed_in_bottom_section then return false end -- Make sure the entire volume except for the top level is free before checking the top layer diff --git a/mods/ENTITIES/mobs_mc/villager.lua b/mods/ENTITIES/mobs_mc/villager.lua index e80518e48..5ea20c707 100644 --- a/mods/ENTITIES/mobs_mc/villager.lua +++ b/mods/ENTITIES/mobs_mc/villager.lua @@ -1053,7 +1053,8 @@ local function summon_golem(self) local p1 = vector.offset(pos, -10, -10, -10) local p2 = vector.offset(pos, 10, 10, 10) local nn = minetest.find_nodes_in_area_under_air(p1, p2,{"group:solid","group:water"}) - for n in table.pull_random_items(nn) do + while #nn > 0 do + local n = table.remove_random_element(nn) n.y = n.y + 1 local summon = mcl_mobs.spawn(n, "mobs_mc:iron_golem") diff --git a/mods/ITEMS/mcl_mobspawners/init.lua b/mods/ITEMS/mcl_mobspawners/init.lua index 80f57faa8..5c934a819 100644 --- a/mods/ITEMS/mcl_mobspawners/init.lua +++ b/mods/ITEMS/mcl_mobspawners/init.lua @@ -230,7 +230,8 @@ local function spawn_mobs(pos, elapsed) local mlig = meta:get_int("MinLight") local xlig = meta:get_int("MaxLight") - for pos2 in table.pull_random_items(air) do + while #air > 0 do + local pos2 = table.remove_random_element(air) -- only if light levels are within range local lig = minetest.get_node_light(pos2) or 0 if lig >= mlig and lig <= xlig then