From 0eb23cd4a646fe9322706645aa2ec28776ca9099 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Tue, 19 Feb 2019 19:36:35 +0100 Subject: [PATCH] Add support for get_treasures --- gameconfig.lua | 8 ++++++++ init.lua | 19 +++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/gameconfig.lua b/gameconfig.lua index f072aaa..8a8b0db 100644 --- a/gameconfig.lua +++ b/gameconfig.lua @@ -145,3 +145,11 @@ function tsm_railcorridors.get_default_treasure(pr) end end +-- Alternative treasure fallback function. This function is called +-- to return ALL treasures of a single chest at all. It must return +-- a table of items. If this function is defined, get_default_treasure +-- is ignored. +-- Syntax: +-- tsm_railcorridors.get_treasures = function(pr) +-- Not used by default. +tsm_railcorridors.get_treasures = nil diff --git a/init.lua b/init.lua index 0752b9b..87da398 100644 --- a/init.lua +++ b/init.lua @@ -3,6 +3,8 @@ tsm_railcorridors = {} -- Load node names dofile(minetest.get_modpath(minetest.get_current_modname()).."/gameconfig.lua") +local treasurer_supported = minetest.get_modpath("treasurer") ~= nil + -- Settings local setting @@ -374,10 +376,8 @@ local function Platform(p, radius, node, node2) end end - --- Random chest items -local function rci() - if(minetest.get_modpath("treasurer") ~= nil) then +local function RandomChestItem() + if treasurer_supported then local treasures if pr_treasures:next(0,100) < 3 then treasures = treasurer.select_random_treasures(1,2,4) @@ -418,8 +418,15 @@ local function PlaceChest(pos, param2) if SetNodeIfCanBuild(pos, {name=tsm_railcorridors.nodes.chest, param2=param2}) then local meta = minetest.get_meta(pos) local inv = meta:get_inventory() - for i=1, inv:get_size("main") do - inv:set_stack("main", i, ItemStack(rci())) + if not treasurer_supported and tsm_railcorridors.get_treasures then + local items = tsm_railcorridors.get_treasures(pr) + for i=1, math.min(#items, inv:get_size("main")) do + inv:set_stack("main", i, ItemStack(items[i])) + end + else + for i=1, inv:get_size("main") do + inv:set_stack("main", i, ItemStack(RandomChestItem())) + end end end end