Make mcl_loot/get_random_slots() deterministic

This commit is contained in:
kay27 2021-02-22 21:58:35 +04:00
parent eb62db441b
commit 47db5c5917
5 changed files with 9 additions and 9 deletions

View file

@ -111,14 +111,14 @@ end
Returns a table of length `max_slot` and all natural numbers between 1 and `max_slot` Returns a table of length `max_slot` and all natural numbers between 1 and `max_slot`
in a random order. in a random order.
]] ]]
local function get_random_slots(max_slot) local function get_random_slots(max_slot, pr)
local slots = {} local slots = {}
for s=1, max_slot do for s=1, max_slot do
slots[s] = s slots[s] = s
end end
local slots_out = {} local slots_out = {}
while #slots > 0 do while #slots > 0 do
local r = math.random(1, #slots) local r = pr and pr:next(1, #slots) or math.random(1, #slots)
table.insert(slots_out, slots[r]) table.insert(slots_out, slots[r])
table.remove(slots, r) table.remove(slots, r)
end end
@ -135,9 +135,9 @@ Items will be added from start of the table to end.
If the inventory already has occupied slots, or is If the inventory already has occupied slots, or is
too small, placement of some items might fail. too small, placement of some items might fail.
]] ]]
function mcl_loot.fill_inventory(inv, listname, items) function mcl_loot.fill_inventory(inv, listname, items, pr)
local size = inv:get_size(listname) local size = inv:get_size(listname)
local slots = get_random_slots(size) local slots = get_random_slots(size, pr)
local leftovers = {} local leftovers = {}
-- 1st pass: Add items into random slots -- 1st pass: Add items into random slots
for i=1, math.min(#items, size) do for i=1, math.min(#items, size) do

View file

@ -311,7 +311,7 @@ local function ecb_spawn_dungeon(blockpos, action, calls_remaining, param)
minetest.set_node(pos, {name="mcl_chests:chest", param2=facedir}) minetest.set_node(pos, {name="mcl_chests:chest", param2=facedir})
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
mcl_loot.fill_inventory(meta:get_inventory(), "main", mcl_loot.get_multi_loot(loottable, pr)) mcl_loot.fill_inventory(meta:get_inventory(), "main", mcl_loot.get_multi_loot(loottable, pr), pr)
end end
-- Mob spawners are placed seperately, too -- Mob spawners are placed seperately, too

View file

@ -244,7 +244,7 @@ local function igloo_placement_callback(p1, p2, size, orientation, pr)
init_node_construct(chest_pos) init_node_construct(chest_pos)
local meta = minetest.get_meta(chest_pos) local meta = minetest.get_meta(chest_pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
mcl_loot.fill_inventory(inv, "main", lootitems) mcl_loot.fill_inventory(inv, "main", lootitems, pr)
end end
mcl_structures.generate_igloo_basement = function(pos, orientation, pr) mcl_structures.generate_igloo_basement = function(pos, orientation, pr)
@ -463,7 +463,7 @@ local function temple_placement_callback(p1, p2, size, rotation, pr)
init_node_construct(chests[c]) init_node_construct(chests[c])
local meta = minetest.get_meta(chests[c]) local meta = minetest.get_meta(chests[c])
local inv = meta:get_inventory() local inv = meta:get_inventory()
mcl_loot.fill_inventory(inv, "main", lootitems) mcl_loot.fill_inventory(inv, "main", lootitems, pr)
end end
-- Initialize pressure plates and randomly remove up to 5 plates -- Initialize pressure plates and randomly remove up to 5 plates

View file

@ -178,7 +178,7 @@ function settlements.fill_chest(pos, pr)
end end
local items = get_treasures(pr) local items = get_treasures(pr)
mcl_loot.fill_inventory(inv, "main", items) mcl_loot.fill_inventory(inv, "main", items, pr)
end end
------------------------------------------------------------------------------- -------------------------------------------------------------------------------

View file

@ -380,7 +380,7 @@ local function PlaceChest(pos, param2)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
local items = tsm_railcorridors.get_treasures(pr) local items = tsm_railcorridors.get_treasures(pr)
mcl_loot.fill_inventory(inv, "main", items) mcl_loot.fill_inventory(inv, "main", items, pr)
end end
end end