respect trade limits

This commit is contained in:
Sokomine 2022-09-06 22:26:45 +02:00
parent 3ddec81b1c
commit 1eb272ecb2

View File

@ -618,6 +618,7 @@ yl_speak_up.get_fs_trade_simple = function(player, trade_id)
local n_id = yl_speak_up.speak_to[pname].n_id
local dialog = yl_speak_up.speak_to[pname].dialog
yl_speak_up.setup_trade_limits(dialog)
trade = {
-- we start with the simple trade
trade_type = "trade_simple",
@ -636,6 +637,15 @@ yl_speak_up.get_fs_trade_simple = function(player, trade_id)
trade.npc_gives = dialog.trades[ trade_id ].buy[1]
trade.trade_is_trade_list = not(dialog.trades[ trade_id ].d_id)
yl_speak_up.speak_to[pname].trade_id = trade_id
-- copy the limits
local stack = ItemStack(trade.npc_gives)
trade.npc_gives_name = stack:get_name()
trade.npc_gives_amount = stack:get_count()
trade.min_storage = dialog.trades.limits.sell_if_more[ trade.npc_gives_name ]
stack = ItemStack(trade.player_gives)
trade.player_gives_name = stack:get_name()
trade.player_gives_amount = stack:get_count()
trade.max_storage = dialog.trades.limits.buy_if_less[ trade.player_gives_name ]
else
trade.edit_trade = true
end
@ -719,6 +729,17 @@ yl_speak_up.get_fs_trade_simple = function(player, trade_id)
local trade_possible_msg = "Status of trade: Unknown."
local can_trade = false
-- find out how much the npc has stoerd
local stock_pay = 0
local stock_buy = 0
-- only count the inv if there actually are any mins or max
if(trade.min_storage or trade.max_storage) then
local n_id = yl_speak_up.speak_to[pname].n_id
local counted_npc_inv = {}
counted_npc_inv = yl_speak_up.count_npc_inv(n_id)
stock_pay = counted_npc_inv[trade.player_gives_name] or 0
stock_buy = counted_npc_inv[trade.npc_gives_name] or 0
end
-- can the NPC provide his part?
if(not(npc_inv:contains_item("npc_main", trade.npc_gives))) then
trade_possible_msg = "Sorry. "..minetest.formspec_escape(trade.npc_name)..
@ -727,6 +748,20 @@ yl_speak_up.get_fs_trade_simple = function(player, trade_id)
elseif(not(npc_inv:room_for_item("npc_main", trade.player_gives))) then
trade_possible_msg = "Sorry. "..minetest.formspec_escape(trade.npc_name)..
" ran out of inventory space.\nThere is no room to store your payment!"
-- trade limit: is enough left after the player buys the item?
elseif(trade.min_storage and trade.min_storage > stock_buy - trade.npc_gives_amount) then
trade_possible_msg = "Sorry. "..minetest.formspec_escape(trade.npc_name)..
" currently does not want to\nsell that much."..
" Current stock: "..tostring(stock_buy)..
" (min: "..tostring(trade.min_storage)..
"). Perhaps later?"
-- trade limit: make sure the bought amount does not exceed the desired maximum
elseif(trade.max_storage and trade.max_storage < stock_pay + trade.player_gives_amount) then
trade_possible_msg = "Sorry. "..minetest.formspec_escape(trade.npc_name)..
" currently does not want to\nbuy that much."..
" Current stock: "..tostring(stock_pay)..
" (max: "..tostring(trade.max_storage)..
"). Perhaps later?"
-- can the player pay?
elseif(not(player_inv:contains_item("main", trade.player_gives))) then
-- both slots will remain empty