split fs_trade_limit.lua into fs/ and api/ part

This commit is contained in:
Sokomine 2023-12-10 04:56:51 +01:00
parent eb6d75c8fc
commit 259a79e359
3 changed files with 81 additions and 83 deletions

79
api/api_trade.lua Normal file
View File

@ -0,0 +1,79 @@
-----------------------------------------------------------------------------
-- limits for trading: maximum and minimum stock to keep
-----------------------------------------------------------------------------
-- sometimes players may not want the NPC to sell *all* of their stock,
-- or not let the NPC buy endless amounts of something when only a limited
-- amount is needed
-----------------------------------------------------------------------------
-- helper function: make sure all necessary entries in the trades table exist
yl_speak_up.setup_trade_limits = function(dialog)
if(not(dialog)) then
dialog = {}
end
if(not(dialog.trades)) then
dialog.trades = {}
end
if(not(dialog.trades.limits)) then
dialog.trades.limits = {}
end
if(not(dialog.trades.limits.sell_if_more)) then
dialog.trades.limits.sell_if_more = {}
end
if(not(dialog.trades.limits.buy_if_less)) then
dialog.trades.limits.buy_if_less = {}
end
return dialog
end
-- helper function: count how many items the NPC has in his inventory
-- empty stacks are counted under the key "";
-- for other items, the amount of items of each type is counted
yl_speak_up.count_npc_inv = function(n_id)
if(not(n_id)) then
return {}
end
-- the NPC's inventory
local npc_inv = minetest.get_inventory({type="detached", name="yl_speak_up_npc_"..tostring(n_id)})
if(not(npc_inv)) then
return {}
end
local anz = npc_inv:get_size('npc_main')
local stored = {}
for i=1, anz do
local stack = npc_inv:get_stack('npc_main', i )
local name = stack:get_name()
local count = stack:get_count()
-- count empty stacks
if(name=="") then
count = 1
end
-- count how much of each item is there
if(not(stored[ name ])) then
stored[ name ] = count
else
stored[ name ] = stored[ name ] + count
end
end
return stored
end
-- helper function: update the items table so that it reflects a limitation
-- items is a table (list) with these entries:
-- [1] 0 in stock;
-- [2] sell if more than 0;
-- [3] buy if less than 10000;
-- [4] item is part of a trade offer
yl_speak_up.insert_trade_item_limitation = function( items, k, i, v )
if( i<1 or i>4) then
return;
end
if( not( items[ k ] )) then
-- 0 in stock; sell if more than 0; buy if less than 10000; item is part of a trade offer
items[ k ] = { 0, 0, 10000, false, #items }
end
items[ k ][ i ] = v
end

View File

@ -1,85 +1,3 @@
-----------------------------------------------------------------------------
-- limits for trading: maximum and minimum stock to keep
-----------------------------------------------------------------------------
-- sometimes players may not want the NPC to sell *all* of their stock,
-- or not let the NPC buy endless amounts of something when only a limited
-- amount is needed
-----------------------------------------------------------------------------
-- helper function: make sure all necessary entries in the trades table exist
yl_speak_up.setup_trade_limits = function(dialog)
if(not(dialog)) then
dialog = {}
end
if(not(dialog.trades)) then
dialog.trades = {}
end
if(not(dialog.trades.limits)) then
dialog.trades.limits = {}
end
if(not(dialog.trades.limits.sell_if_more)) then
dialog.trades.limits.sell_if_more = {}
end
if(not(dialog.trades.limits.buy_if_less)) then
dialog.trades.limits.buy_if_less = {}
end
return dialog
end
-- helper function: count how many items the NPC has in his inventory
-- empty stacks are counted under the key "";
-- for other items, the amount of items of each type is counted
yl_speak_up.count_npc_inv = function(n_id)
if(not(n_id)) then
return {}
end
-- the NPC's inventory
local npc_inv = minetest.get_inventory({type="detached", name="yl_speak_up_npc_"..tostring(n_id)})
if(not(npc_inv)) then
return {}
end
local anz = npc_inv:get_size('npc_main')
local stored = {}
for i=1, anz do
local stack = npc_inv:get_stack('npc_main', i )
local name = stack:get_name()
local count = stack:get_count()
-- count empty stacks
if(name=="") then
count = 1
end
-- count how much of each item is there
if(not(stored[ name ])) then
stored[ name ] = count
else
stored[ name ] = stored[ name ] + count
end
end
return stored
end
-- helper function: update the items table so that it reflects a limitation
-- items is a table (list) with these entries:
-- [1] 0 in stock;
-- [2] sell if more than 0;
-- [3] buy if less than 10000;
-- [4] item is part of a trade offer
yl_speak_up.insert_trade_item_limitation = function( items, k, i, v )
if( i<1 or i>4) then
return;
end
if( not( items[ k ] )) then
-- 0 in stock; sell if more than 0; buy if less than 10000; item is part of a trade offer
items[ k ] = { 0, 0, 10000, false, #items }
end
items[ k ][ i ] = v
end
-- handle input to the formspec
yl_speak_up.input_trade_limit = function(player, formname, fields)
local pname = player:get_player_name()

View File

@ -206,7 +206,8 @@ yl_speak_up.reload = function(modpath, log_entry)
-- inventory management, trading and handling of quest items:
dofile(modpath .. "inventory.lua")
-- limit how much the NPC shall buy and sell
dofile(modpath .. "fs_trade_limit.lua")
dofile(modpath .. "api/api_trade.lua")
dofile(modpath .. "fs/fs_trade_limit.lua")
dofile(modpath .. "fs_edit_trade_limit.lua")
-- trade one item(stack) against one other item(stack)
dofile(modpath .. "trade_simple.lua")