From 1eb272ecb291aa8f2009da272fbe6b8b3989d02a Mon Sep 17 00:00:00 2001 From: Sokomine Date: Tue, 6 Sep 2022 22:26:45 +0200 Subject: [PATCH] respect trade limits --- trade_simple.lua | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/trade_simple.lua b/trade_simple.lua index 6e3c499..5f1d8f6 100644 --- a/trade_simple.lua +++ b/trade_simple.lua @@ -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