From 735553a937163f8b6c21a8dd83cfd812bf191142 Mon Sep 17 00:00:00 2001 From: Sokomine Date: Sun, 30 May 2021 19:14:48 +0200 Subject: [PATCH] always give items back when storing a trade; changed trade_id for trade list for better sorting; fixed bug with trade.trade_is_trade_list for storing temporal trade list --- trade_simple.lua | 86 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 27 deletions(-) diff --git a/trade_simple.lua b/trade_simple.lua index fd4b567..36f9052 100644 --- a/trade_simple.lua +++ b/trade_simple.lua @@ -30,6 +30,14 @@ yl_speak_up.input_do_trade_simple = function(player, formname, fields) return end + -- a new trade has been stored - show it + if(fields.trade_simple_stored) then + minetest.show_formspec(pname, "yl_speak_up:do_trade_simple", + yl_speak_up.get_fs_trade_simple(player, + yl_speak_up.speak_to[pname].trade_id)) + return + end + -- delete a trade; this can be done here only if.. -- * it is a trade from the trade list (not an effect of a dialog option) -- * the player has the necessary privs @@ -79,7 +87,7 @@ yl_speak_up.input_do_trade_simple = function(player, formname, fields) -- show the edit trade formspec if(fields.edit_trade_simple) then minetest.show_formspec(pname, "yl_speak_up:add_trade_simple", - yl_speak_up.get_fs_trade_simple(player)) + yl_speak_up.get_fs_add_trade_simple(player)) return end @@ -168,6 +176,23 @@ yl_speak_up.get_fs_add_trade_simple = function(player, trade_id) end +-- when closing the yl_speak_up.get_fs_add_trade_simple formspec: +-- give the items back to the player (he took them from his inventory and +-- had no real chance to put them elsewhere - so there really ought to be +-- room enough) +yl_speak_up.add_trade_simple_return_items = function(player, trade_inv, pay, buy) + local player_inv = player:get_inventory() + if( pay and player_inv:room_for_item("main", pay)) then + player_inv:add_item("main", pay) + trade_inv:set_stack("setup", 1, "") + end + if( buy and player_inv:room_for_item("main", buy)) then + player_inv:add_item("main", buy) + trade_inv:set_stack("setup", 2, "") + end +end + + -- the player wants to add a simple trade; handle formspec input -- possible inputs: -- fields.back_from_error_msg show this formspec here again @@ -194,11 +219,21 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields) -- which trade are we talking about? local trade_id = yl_speak_up.speak_to[pname].trade_id + -- this also contains the inventory list "setup" where the player placed the items + local trade_inv = minetest.get_inventory({type="detached", name="yl_speak_up_player_"..pname}) + + -- fields.abort_trade_simple can be ignored as it is similar to ESC + + local pay = trade_inv:get_stack("setup", 1) + local buy = trade_inv:get_stack("setup", 2) + -- clicking on abort here when adding a new trade via the trade list -- goes back to the trade list (does not require special privs) if(fields.abort_trade_simple and trade_id == "new") then -- we are no longer doing a particular trade yl_speak_up.speak_to[pname].trade_id = nil + -- return the items (setting up the trade was aborted) + yl_speak_up.add_trade_simple_return_items(player, trade_inv, pay, buy) -- ..else go back to the edit options formspec minetest.show_formspec(pname, "yl_speak_up:trade_list", yl_speak_up.get_fs_trade_list(player)) @@ -218,17 +253,11 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields) -- exception: when adding a new trade via the trade list -- (that is allowed without having to be in edit mode) and not(trade_id == "new" and yl_speak_up.may_edit_npc(player, n_id))) then + -- return the items (setting up the trade was aborted) + yl_speak_up.add_trade_simple_return_items(player, trade_inv, pay, buy) return end - -- this also contains the inventory list "setup" where the player placed the items - local trade_inv = minetest.get_inventory({type="detached", name="yl_speak_up_player_"..pname}) - - -- fields.abort_trade_simple can be ignored as it is similar to ESC - - local pay = trade_inv:get_stack("setup", 1) - local buy = trade_inv:get_stack("setup", 2) - -- store the new trade if(fields.store_trade_simple) then local error_msg = "" @@ -253,12 +282,18 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields) local bs = buy:get_name().." "..tostring(buy:get_count()) local r_id = "?" + if(not(dialog.trades)) then + dialog.trades = {} + end -- is this a trade attached to the trade list? -- or do we have to create a new trade ID? if(trade_id == "new") then -- if the player adds the same trade again, the ID is reused; other -- than that, the ID is uniq - trade_id = "trade "..ps.." for "..bs + -- (the ID is formed so that we can later easily sort the offers by + -- the name of the buy stack - which is more helpful for the player + -- than sorting by the pay stack) + trade_id = "sell "..bs.." for "..ps -- log the change yl_speak_up.log_change(pname, n_id, "Trade: Added offer "..tostring(trade_id)..".") @@ -312,20 +347,12 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields) -- do not return yet - the items still need to be given back! end - -- give the items back to the player (he took them from his inventory and had no - -- real chance to put them elsewhere - so there really ought to be room enough) - local player_inv = player:get_inventory() - if( player_inv:room_for_item("main", pay)) then - player_inv:add_item("main", pay) - trade_inv:set_stack("setup", 1, "") - end - if( player_inv:room_for_item("main", buy)) then - player_inv:add_item("main", buy) - trade_inv:set_stack("setup", 2, "") - end + -- return the items after successful setup + yl_speak_up.add_trade_simple_return_items(player, trade_inv, pay, buy) local dialog = yl_speak_up.speak_to[pname].dialog if(dialog.trades[ trade_id ] and dialog.trades[ trade_id ].d_id) then + yl_speak_up.speak_to[pname].trade_id = trade_id -- tell the player that the new trade has been added minetest.show_formspec(pname, "yl_speak_up:do_trade_simple", "size[6,2]".. @@ -338,7 +365,6 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields) -- ..else go back to the edit options formspec minetest.show_formspec(pname, "yl_speak_up:trade_list", yl_speak_up.get_fs_trade_list(player)) - return else -- we are no longer trading yl_speak_up.speak_to[pname].trade_id = nil @@ -464,6 +490,7 @@ yl_speak_up.get_fs_trade_simple = function(player, trade_id) if(dialog.trades[ trade_id ]) then trade.player_gives = dialog.trades[ trade_id ].pay[1] trade.npc_gives = dialog.trades[ trade_id ].buy[1] + trade.trade_is_trade_list = not(dialog.trades[ trade_id ].d_id) else trade.edit_trade = true end @@ -504,16 +531,24 @@ yl_speak_up.get_fs_trade_simple = function(player, trade_id) "button[0.2,1.6;1.0,0.9;edit_trade_simple;Edit]".. "tooltip[edit_trade_simple;Edit this trade. You can do so only ".. "if you can edit the NPC as such (i.e. own it).]" + -- TODO offer button to show the edit options dialog if suitable +-- formspec = formspec.. +-- "button[0.2,0.0;2.0,0.9;back_to_trade_list;Back to list]".. +-- "tooltip[back_to_trade_list;Click here once you've traded enough with this ".. +-- "NPC and want to get back to the trade list.]" + -- for trades in trade list: allow delete (new trades can easily be added) -- allow delete for trades in trade list even if not in edit mode -- (entering edit mode for that would be too much work) elseif(trade.trade_is_trade_list) then - -- TODO: make this button work formspec = formspec.. "button[0.2,1.6;1.2,0.9;delete_trade_simple;Delete]".. "tooltip[delete_trade_simple;Delete this trade. You can do so only ".. "if you can edit the NPC as such (i.e. own it).]" end + -- TODO: just for debugging + formspec = formspec.. + "label[0.0,-0.3;"..tostring(trade_id).." trade_is_trade_list: "..tostring(trade.trade_is_trade_list).."]" end local trade_possible_msg = "Status of trade: Unknown." @@ -575,12 +610,9 @@ yl_speak_up.get_fs_trade_simple = function(player, trade_id) "NPC and want to get back to talking.]" else -- go back to the trade list - -- TODO: make this button work - formspec = formspec.. - "button[0.2,0.0;2.0,0.9;back_to_trade_list;Back to list]".. + formspec = formspec.. "button[0.2,0.0;2.0,0.9;back_to_trade_list;Back to list]".. "tooltip[back_to_trade_list;Click here once you've traded enough with this ".. "NPC and want to get back to the trade list.]" - -- TODO: offer button to show the edit options dialog if suitable end return formspec..