show in trade list how much stock is available

This commit is contained in:
Sokomine 2022-09-10 20:12:08 +02:00
parent da99bd3f95
commit 99c5d60dc2

View File

@ -76,15 +76,6 @@ yl_speak_up.get_fs_trade_list = function(player, show_dialog_option_trades)
"button_exit[2,1.5;1,0.9;exit;Exit]"
end
-- make sure the NPC has that table defined
if(not(dialog.trades)) then
dialog.trades = {}
end
yl_speak_up.load_npc_inventory(n_id)
local npc_inv = minetest.get_inventory({type="detached", name="yl_speak_up_npc_"..tostring(n_id)})
if(not(yl_speak_up.may_edit_npc(player, n_id))) then
-- do not show trades attached to dialog options for players who cannot edit the NPC
show_dialog_option_trades = false
@ -106,12 +97,18 @@ yl_speak_up.get_fs_trade_list = function(player, show_dialog_option_trades)
local col = 0
local anz_trades = 0
-- make sure all fields (especially the buy and trade limits) are initialized properly
yl_speak_up.setup_trade_limits(dialog)
-- the order in which the trades appear shall not change each time;
-- but lua cannot sort the keys of a table by itself...
-- this function can be found in fs_trade_via_button.lua
local sorted_trades = yl_speak_up.get_sorted_trade_id_list(dialog, show_dialog_option_trades)
yl_speak_up.speak_to[pname].trade_id_list = sorted_trades
-- how much stock does the NPC have?
local counted_npc_inv = yl_speak_up.count_npc_inv(n_id)
for i, k in ipairs(sorted_trades) do
local v = dialog.trades[ k ]
-- needs both to be negated because show_dialog_option_trades will most of the time be nil
@ -120,20 +117,32 @@ yl_speak_up.get_fs_trade_list = function(player, show_dialog_option_trades)
and v.pay and v.pay[1] and v.pay[1] ~= "" and v.buy and v.buy[1] and v.buy[1] ~= "") then
local pay_stack = ItemStack(v.pay[1])
local buy_stack = ItemStack(v.buy[1])
local pay_stack_name = pay_stack:get_name()
local buy_stack_name = buy_stack:get_name()
-- do not show trades with nonexistant items
if( not(minetest.registered_items[ pay_stack:get_name() ])
or not(minetest.registered_items[ buy_stack:get_name() ])) then
if( not(minetest.registered_items[ pay_stack_name ])
or not(minetest.registered_items[ buy_stack_name ])) then
break
end
anz_trades = anz_trades + 1
local kstr = tostring(minetest.formspec_escape(k))
local sold_out = not(npc_inv:contains_item("npc_main", buy_stack))
-- how many times can the NPC do this particular trade?
local amount_available = yl_speak_up.get_trade_amount_available(
(counted_npc_inv[ buy_stack_name ] or 0),
(counted_npc_inv[ pay_stack_name ] or 0),
buy_stack, pay_stack,
dialog.trades.limits.sell_if_more[ buy_stack_name ],
dialog.trades.limits.buy_if_less[ pay_stack_name ])
local sold_out = false
local color = "#a37e45" --"#777777"
if(sold_out) then
if(amount_available < 1) then
sold_out = true
color = "#663333"
-- still needs to contain kstr so that each button has a diffrent text
-- kstr = "sold out "..kstr
-- else -- if admin shop
-- -- indicate that the shop will never run out of stock with a special color
-- color = "#999999"
end
table.insert(formspec,
"container["..tostring(0.5+(col*4.5))..","..
@ -152,10 +161,12 @@ yl_speak_up.get_fs_trade_list = function(player, show_dialog_option_trades)
table.insert(formspec, "button[0,1.0;4.3,1.0;"..kstr.."__;Sold out]"..
"\ncontainer_end[]")
else
-- how often can this trade be done?
table.insert(formspec, "label[0,0.8;Stock: "..tostring(amount_available).."x]")
-- show the price label only when the offer is in stock
table.insert(formspec, "label[0,1.9;->]"..
"label[0,4.4;Price:]\ncontainer_end[]")
"label[0,4.4;Price:]"..
"\ncontainer_end[]")
end
col = col + 1
if(col >= yl_speak_up.trade_max_cols) then