From 9d5dace4fb078cdb49cf851a8e5e19b579864feb Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sun, 25 Aug 2019 12:37:36 +0200 Subject: [PATCH] Guarantee 1 chest with treasures --- init.lua | 10 ++++++++-- room.lua | 10 +++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/init.lua b/init.lua index e4a06dd..587fdcd 100644 --- a/init.lua +++ b/init.lua @@ -40,12 +40,16 @@ else table.insert(chest_stuff, {name="farming:apple", max = 3}) end -function tsm_pyramids.fill_chest(pos, stype, flood_sand) +function tsm_pyramids.fill_chest(pos, stype, flood_sand, treasure_chance) local sand = "default:sand" + if not treasure_chance then + treasure_chance = 100 + end if stype == "desert_sandstone" or stype == "desert_stone" then sand = "default:desert_sand" end local n = minetest.get_node(pos) + local treasure_added = false if n and n.name and n.name == "default:chest" then local meta = minetest.get_meta(pos) local inv = meta:get_inventory() @@ -56,13 +60,14 @@ function tsm_pyramids.fill_chest(pos, stype, flood_sand) table.insert(stacks, {name=sand, count = math.random(1,32)}) end -- Add treasures - if math.random(1,10) >= 7 then + if math.random(1,100) <= treasure_chance then if minetest.get_modpath("treasurer") ~= nil then stacks = treasurer.select_random_treasures(3,7,9,{"minetool", "food", "crafting_component"}) else for i=0,2,1 do local stuff = chest_stuff[math.random(1,#chest_stuff)] table.insert(stacks, {name=stuff.name, count = math.random(1,stuff.max)}) + treasure_added = true end end end @@ -72,6 +77,7 @@ function tsm_pyramids.fill_chest(pos, stype, flood_sand) end end end + return treasure_added end local function add_spawner(pos, mummy_offset) diff --git a/room.lua b/room.lua index db5c0d5..a9991d9 100644 --- a/room.lua +++ b/room.lua @@ -1054,12 +1054,20 @@ function tsm_pyramids.make_room(pos, stype, room_id, rotations) if #chests > 0 then -- Make at least 8 attempts to fill chests local filled = 0 + local chests_with_treasure = 0 while filled < 8 do for c=1, #chests do - tsm_pyramids.fill_chest(chests[c], stype, sanded) + local has_treasure = tsm_pyramids.fill_chest(chests[c], stype, sanded, 30) + if has_treasure then + chests_with_treasure = chests_with_treasure + 1 + end filled = filled + 1 end end + -- If no chests were filled with treasure so far, fill a random chest guaranteed + if chests_with_treasure == 0 then + tsm_pyramids.fill_chests(chests[math.random(1, #chests)], stype, sanded, 100) + end end if room.traps and math.random(1,4) then tsm_pyramids.make_traps(pos, stype, rotations, layout)