diff --git a/api/api_trade.lua b/api/api_trade.lua index 9f0e5d1..37fca5c 100644 --- a/api/api_trade.lua +++ b/api/api_trade.lua @@ -77,3 +77,61 @@ yl_speak_up.insert_trade_item_limitation = function( items, k, i, v ) end items[ k ][ i ] = v end + + +-- helper function; returns how often a trade can be done +-- stock_buy how much of the buy stack does the NPC have in storage? +-- stock_pay how much of the price stack does the NPC have in storage? +-- buy_stack stack containing the item the NPC sells +-- pay_stack stack containing the price for said item +-- min_storage how many items of the buy stack items shall the NPC keep? +-- max_storage how many items of the pay stack items can the NPC accept? +-- used in fs_trade_via_buy_button.lua and fs_trade_list.lua +yl_speak_up.get_trade_amount_available = function(stock_buy, stock_pay, buy_stack, pay_stack, min_storage, max_storage) + local stock = 0 + -- the NPC shall not sell more than this + if(min_storage and min_storage > 0) then + stock_buy = math.max(0, stock_buy - min_storage) + end + stock = math.floor(stock_buy / buy_stack:get_count()) + -- the NPC shall not buy more than this + if(max_storage and max_storage < 10000) then + stock_pay = math.min(max_storage - stock_pay, 10000) + stock = math.min(stock, math.floor(stock_pay / pay_stack:get_count())) + end + return stock +end + + + +-- helper function; also used by fs_trade_list.lua +yl_speak_up.get_sorted_trade_id_list = function(dialog, show_dialog_option_trades) + -- make sure all fields exist + yl_speak_up.setup_trade_limits(dialog) + local keys = {} + if(show_dialog_option_trades) then + for k, v in pairs(dialog.trades) do + if(k ~= "limits" and k ~= "" and v.d_id) then + table.insert(keys, k) + end + end + else + for k, v in pairs(dialog.trades) do + if(k ~= "limits" and k ~= "") then + -- structure of the indices: sell name amount for name amount + local parts = string.split(k, " ") + if(parts and #parts == 6 and parts[4] == "for" + and v.pay and v.pay[1] ~= "" and v.pay[1] == parts[5].." "..parts[6] + and v.buy and v.buy[1] ~= "" and v.buy[1] == parts[2].." "..parts[3] + and minetest.registered_items[parts[5]] + and minetest.registered_items[parts[2]] + and tonumber(parts[6]) > 0 + and tonumber(parts[3]) > 0) then + table.insert(keys, k) + end + end + end + end + table.sort(keys) + return keys +end diff --git a/fs_edit_trade_limit.lua b/fs/fs_edit_trade_limit.lua similarity index 100% rename from fs_edit_trade_limit.lua rename to fs/fs_edit_trade_limit.lua diff --git a/fs_player_offers_item.lua b/fs/fs_player_offers_item.lua similarity index 100% rename from fs_player_offers_item.lua rename to fs/fs_player_offers_item.lua diff --git a/fs_trade_via_buy_button.lua b/fs/fs_trade_via_buy_button.lua similarity index 87% rename from fs_trade_via_buy_button.lua rename to fs/fs_trade_via_buy_button.lua index e476576..42e5dd9 100644 --- a/fs_trade_via_buy_button.lua +++ b/fs/fs_trade_via_buy_button.lua @@ -12,63 +12,6 @@ yl_speak_up.get_trade_item_desc = function(item) end --- helper function; returns how often a trade can be done --- stock_buy how much of the buy stack does the NPC have in storage? --- stock_pay how much of the price stack does the NPC have in storage? --- buy_stack stack containing the item the NPC sells --- pay_stack stack containing the price for said item --- min_storage how many items of the buy stack items shall the NPC keep? --- max_storage how many items of the pay stack items can the NPC accept? -yl_speak_up.get_trade_amount_available = function(stock_buy, stock_pay, buy_stack, pay_stack, min_storage, max_storage) - local stock = 0 - -- the NPC shall not sell more than this - if(min_storage and min_storage > 0) then - stock_buy = math.max(0, stock_buy - min_storage) - end - stock = math.floor(stock_buy / buy_stack:get_count()) - -- the NPC shall not buy more than this - if(max_storage and max_storage < 10000) then - stock_pay = math.min(max_storage - stock_pay, 10000) - stock = math.min(stock, math.floor(stock_pay / pay_stack:get_count())) - end - return stock -end - - - --- helper function; also used by fs_trade_list.lua -yl_speak_up.get_sorted_trade_id_list = function(dialog, show_dialog_option_trades) - -- make sure all fields exist - yl_speak_up.setup_trade_limits(dialog) - local keys = {} - if(show_dialog_option_trades) then - for k, v in pairs(dialog.trades) do - if(k ~= "limits" and k ~= "" and v.d_id) then - table.insert(keys, k) - end - end - else - for k, v in pairs(dialog.trades) do - if(k ~= "limits" and k ~= "") then - -- structure of the indices: sell name amount for name amount - local parts = string.split(k, " ") - if(parts and #parts == 6 and parts[4] == "for" - and v.pay and v.pay[1] ~= "" and v.pay[1] == parts[5].." "..parts[6] - and v.buy and v.buy[1] ~= "" and v.buy[1] == parts[2].." "..parts[3] - and minetest.registered_items[parts[5]] - and minetest.registered_items[parts[2]] - and tonumber(parts[6]) > 0 - and tonumber(parts[3]) > 0) then - table.insert(keys, k) - end - end - end - end - table.sort(keys) - return keys -end - - yl_speak_up.input_trade_via_buy_button = function(player, formname, fields) local pname = player:get_player_name() diff --git a/init.lua b/init.lua index 62dec0c..242537c 100644 --- a/init.lua +++ b/init.lua @@ -212,7 +212,7 @@ yl_speak_up.reload = function(modpath, log_entry) -- trade one item(stack) against one other item(stack) dofile(modpath .. "trade_simple.lua") -- just click on a button to buy items from the trade list - dofile(modpath .. "fs_trade_via_buy_button.lua") + dofile(modpath .. "fs/fs_trade_via_buy_button.lua") -- easily accessible list of all trades the NPC offers dofile(modpath .. "fs/fs_trade_list.lua") -- as the name says: list which npc acesses a variable how and in which context