0
0
Fork 0

Put treasure loot into random inventory slots

This commit is contained in:
Wuzzy 2019-02-28 18:00:17 +01:00
parent 7851cee45e
commit 1daf9b7a59
4 changed files with 58 additions and 12 deletions

View File

@ -98,3 +98,57 @@ function mcl_loot.get_multi_loot(multi_loot_definitions, pr)
end end
return items return items
end end
--[[
Returns a table of length `max_slot` and all natural numbers between 1 and `max_slot`
in a random order.
]]
local function get_random_slots(max_slot)
local slots = {}
for s=1, max_slot do
slots[s] = s
end
local slots_out = {}
while #slots > 0 do
local r = math.random(1, #slots)
table.insert(slots_out, slots[r])
table.remove(slots, r)
end
for s=1, #slots_out do
print(slots_out[s])
end
return slots_out
end
--[[
Puts items in an inventory list into random slots.
* inv: InvRef
* listname: Inventory list name
* items: table of items to add
Items will be added from start of the table to end.
If the inventory already has occupied slots, or is
too small, placement of some items might fail.
]]
function mcl_loot.fill_inventory(inv, listname, items)
local size = inv:get_size(listname)
local slots = get_random_slots(size)
local leftovers = {}
-- 1st pass: Add items into random slots
for i=1, math.min(#items, size) do
local item = items[i]
local slot = slots[i]
local old_item = inv:get_stack(listname, slot)
local leftover = old_item:add_item(item)
inv:set_stack(listname, slot, old_item)
if not leftover:is_empty() then
table.insert(leftovers, item)
end
end
-- 2nd pass: If some items couldn't be added in first pass,
-- try again in a non-random fashion
for l=1, math.min(#leftovers, size) do
inv:add_item(listname, leftovers[l])
end
-- If there are still items left, tough luck!
end

View File

@ -366,9 +366,7 @@ minetest.register_on_generated(function(minp, maxp)
local meta = minetest.get_meta(cpos) local meta = minetest.get_meta(cpos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
local items = get_loot() local items = get_loot()
for i=1, math.min(#items, inv:get_size("main")) do mcl_loot.fill_inventory(inv, "main", items)
inv:set_stack("main", i, ItemStack(items[i]))
end
end end
-- Mob spawners are placed seperately, too -- Mob spawners are placed seperately, too

View File

@ -188,9 +188,7 @@ mcl_structures.generate_igloo_basement = function(pos, orientation)
local meta = minetest.get_meta(chest_pos) local meta = minetest.get_meta(chest_pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
inv:set_size("main", 9*3) inv:set_size("main", 9*3)
for i=1, #lootitems do mcl_loot.fill_inventory(inv, "main", lootitems)
inv:add_item("main", lootitems[i])
end
end end
return success return success
end end
@ -401,9 +399,7 @@ mcl_structures.generate_desert_temple = function(pos)
local meta = minetest.get_meta(chests[c]) local meta = minetest.get_meta(chests[c])
local inv = meta:get_inventory() local inv = meta:get_inventory()
inv:set_size("main", 9*3) inv:set_size("main", 9*3)
for i=1, #lootitems do mcl_loot.fill_inventory(inv, "main", lootitems)
inv:add_item("main", lootitems[i])
end
end end
-- Initialize pressure plates and randomly remove up to 5 plates -- Initialize pressure plates and randomly remove up to 5 plates

View File

@ -377,9 +377,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)
for i=1, math.min(#items, inv:get_size("main")) do mcl_loot.fill_inventory(inv, "main", items)
inv:set_stack("main", i, ItemStack(items[i]))
end
end end
end end