added yl_speak_up.input_do_trade_simple, store and delete of a trade and store information dialog
This commit is contained in:
parent
2a9f772e10
commit
3c79ad9491
158
trade_simple.lua
158
trade_simple.lua
@ -6,7 +6,84 @@ yl_speak_up.trade_fail_fs = "size[6,2]"..
|
||||
"button_exit[2,1.5;1,0.9;exit;Exit]"
|
||||
|
||||
|
||||
-- possible inputs:
|
||||
-- fields.edit_trade_simple go on to showing the add_trade_simple formspec
|
||||
-- fields.abort_trade_simple, ESC depends on context
|
||||
-- if in edit mode: go back to edit options dialog
|
||||
-- if traded at least once: go on to the target dialog
|
||||
-- if not traded: go back to the original dialog
|
||||
yl_speak_up.input_do_trade_simple = function(player, formname, fields)
|
||||
if(not(player)) then
|
||||
return 0
|
||||
end
|
||||
local pname = player:get_player_name()
|
||||
|
||||
-- which trade are we talking about?
|
||||
local trade = yl_speak_up.trade[pname]
|
||||
|
||||
-- can the player edit this trade?
|
||||
if(fields.edit_trade_simple
|
||||
and (yl_speak_up.edit_mode[pname] == yl_speak_up.speak_to[pname].n_id
|
||||
and (yl_speak_up.speak_to[pname].n_id))) then
|
||||
-- force edit mode for this trade
|
||||
trade.edit_trade = true
|
||||
end
|
||||
|
||||
local trade_inv = minetest.get_inventory({type="detached", name="yl_speak_up_player_"..pname})
|
||||
local player_inv = player:get_inventory()
|
||||
-- give the items from the pay slot back
|
||||
local pay = trade_inv:get_stack("pay", 1)
|
||||
if( player_inv:room_for_item("main", pay)) then
|
||||
player_inv:add_item("main", pay)
|
||||
trade_inv:set_stack("pay", 1, "")
|
||||
end
|
||||
-- clear the buy slot as well
|
||||
trade_inv:set_stack("buy", 1, "")
|
||||
|
||||
-- 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))
|
||||
return
|
||||
end
|
||||
|
||||
-- go back to the main dialog
|
||||
if(fields.abort_trade_simple or fields.quit) then
|
||||
-- if in edit mode: go back to the edit options dialog
|
||||
if(trade.edit_trade) then
|
||||
minetest.show_formspec(pname, "yl_speak_up:edit_option_dialog",
|
||||
yl_speak_up.get_fs_edit_option_dialog(player, n_id, d_id, trade.o_id))
|
||||
return
|
||||
end
|
||||
|
||||
local d_id = yl_speak_up.speak_to[pname].d_id
|
||||
local n_id = yl_speak_up.speak_to[pname].n_id
|
||||
-- if at least one successful trade has happened: show the target dialog
|
||||
-- (else show the current dialog again)
|
||||
if(trade.trade_done > 0) then
|
||||
d_id = trade.target_dialog
|
||||
end
|
||||
-- show either the current or the target dialog
|
||||
minetest.show_formspec(pname, "yl_speak_up:talk",
|
||||
yl_speak_up.get_fs_talkdialog(player, n_id, d_id))
|
||||
return
|
||||
end
|
||||
|
||||
-- show this formspec again
|
||||
minetest.show_formspec(pname, "yl_speak_up:do_trade_simple",
|
||||
yl_speak_up.get_fs_trade_simple(player))
|
||||
end
|
||||
|
||||
|
||||
-- the player wants to add a simple trade; handle formspec input
|
||||
-- possible inputs:
|
||||
-- fields.back_from_error_msg show this formspec here again
|
||||
-- fields.store_trade_simple store this trade as a result and
|
||||
-- go on to showing the do_trade_simple formspec
|
||||
-- fields.delete_trade_simple delete this trade
|
||||
-- go back to edit options dialog
|
||||
-- abort_trade_simple, ESC go back to edit options dialog
|
||||
-- The rest is inventory item movement.
|
||||
yl_speak_up.input_add_trade_simple = function(player, formname, fields)
|
||||
if(not(player)) then
|
||||
return 0
|
||||
@ -19,7 +96,7 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields)
|
||||
end
|
||||
|
||||
-- we return from showing an error message (the player may not have noticed
|
||||
-- a chat message while viewing a formspec)
|
||||
-- a chat message while viewing a formspec; thus, we showed a formspec message)
|
||||
if(fields.back_from_error_msg) then
|
||||
minetest.show_formspec(pname, "yl_speak_up:add_trade_simple",
|
||||
yl_speak_up.get_fs_trade_simple(player))
|
||||
@ -31,6 +108,7 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields)
|
||||
|
||||
local d_id = yl_speak_up.speak_to[pname].d_id
|
||||
local n_id = yl_speak_up.speak_to[pname].n_id
|
||||
local o_id = trade.o_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})
|
||||
@ -56,9 +134,43 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields)
|
||||
elseif(pay:get_name() == buy:get_name()) then
|
||||
error_msg = "Selling *and* buying the same item\nat the same time makes no sense."
|
||||
else
|
||||
-- TODO: *can* we store the trade?
|
||||
-- TODO no trade stored for that option yet?
|
||||
error_msg = "So far so good!"
|
||||
-- get the necessary dialog data
|
||||
local dialog = yl_speak_up.speak_to[pname].dialog
|
||||
-- does this option have a trade result already?
|
||||
local r_id = yl_speak_up.get_result_id_by_type(dialog, d_id, o_id, "trade")
|
||||
-- no trade stored for that option yet? -> create new result
|
||||
if(not(r_id)) then
|
||||
r_id = yl_speak_up.add_new_result(dialog, d_id, o_id)
|
||||
end
|
||||
-- construct the text information for r_value
|
||||
local trade_text = "npc_gives: \""..
|
||||
buy:get_name().." "..tostring(buy:get_count())..
|
||||
"\" player_gives: \""..
|
||||
pay:get_name().." "..tostring(pay:get_count())..
|
||||
"\""
|
||||
|
||||
-- store the new result
|
||||
dialog.n_dialogs[d_id].d_options[o_id].o_results[r_id] = {
|
||||
r_id = r_id,
|
||||
r_type = "trade",
|
||||
r_value = trade_text,
|
||||
-- additional data for easier parsing of the trade
|
||||
r_player_gives = pay:get_name().." "..tostring(pay:get_count()),
|
||||
r_npc_gives = buy:get_name().." "..tostring(buy:get_count()),
|
||||
r_trade_type = "trade_simple",
|
||||
}
|
||||
-- update temporal trade information
|
||||
trade.trade_type = "trade_simple"
|
||||
trade.player_gives = pay:get_name().." "..tostring(pay:get_count())
|
||||
trade.npc_gives = buy:get_name().." "..tostring(buy:get_count())
|
||||
-- finished editing
|
||||
trade.edit_trade = nil
|
||||
-- record this as a change
|
||||
table.insert(yl_speak_up.npc_was_changed[ n_id ],
|
||||
"Dialog "..d_id..": Trade "..tostring(r_id).." added to option "..
|
||||
tostring(o_id)..".")
|
||||
-- do not return yet - the items still need to be given back!
|
||||
error_msg = nil
|
||||
end
|
||||
-- show error message (that leads back to this formspec)
|
||||
if(error_msg) then
|
||||
@ -68,6 +180,21 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields)
|
||||
"button[2,1.5;1,0.9;back_from_error_msg;Back]")
|
||||
return
|
||||
end
|
||||
|
||||
-- we need a way of deleting trades as well
|
||||
elseif(fields.delete_trade_simple) then
|
||||
-- delete this result (if it exists)
|
||||
if(trade.r_id) then
|
||||
-- get the necessary dialog data
|
||||
local dialog = yl_speak_up.speak_to[pname].dialog
|
||||
-- record this as a change
|
||||
table.insert(yl_speak_up.npc_was_changed[ n_id ],
|
||||
"Dialog "..d_id..": Trade "..tostring(r_id).." deleted from option "..
|
||||
tostring(o_id)..".")
|
||||
-- delete the trade type result
|
||||
dialog.n_dialogs[d_id].d_options[trade.o_id].o_results[trade.r_id] = nil
|
||||
-- do not return yet - the items still need to be given back!
|
||||
end
|
||||
end
|
||||
|
||||
-- give the items back to the player (he took them from his inventory and had no
|
||||
@ -82,10 +209,19 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields)
|
||||
trade_inv:set_stack("setup", 2, "")
|
||||
end
|
||||
|
||||
-- TODO: or show the trade we just stored directly?
|
||||
-- ..and go back to the edit options formspec
|
||||
minetest.show_formspec(pname, "yl_speak_up:edit_option_dialog",
|
||||
yl_speak_up.get_fs_edit_option_dialog(player, n_id, d_id, trade.o_id))
|
||||
if(fields.store_trade_simple) then
|
||||
-- tell the player that the new trade has been added
|
||||
minetest.show_formspec(pname, "yl_speak_up:do_trade_simple",
|
||||
"size[6,2]"..
|
||||
"label[0.2,0.5;The new trade has been configured successfully.]"..
|
||||
"button[1.5,1.5;2,0.9;trade_simple_stored;Show trade]")
|
||||
else
|
||||
-- we are no longer trading
|
||||
yl_speak_up.trade[pname] = nil
|
||||
-- ..else go back to the edit options formspec
|
||||
minetest.show_formspec(pname, "yl_speak_up:edit_option_dialog",
|
||||
yl_speak_up.get_fs_edit_option_dialog(player, n_id, d_id, o_id))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@ -170,7 +306,7 @@ end
|
||||
-- simple trade: one item(stack) for another
|
||||
-- handles configuration of new trades and showing the formspec for trades;
|
||||
-- checks if payment and buying is possible
|
||||
yl_speak_up.get_fs_trade_simple = function(player, player_gives, npc_gives)
|
||||
yl_speak_up.get_fs_trade_simple = function(player)
|
||||
if(not(player)) then
|
||||
return yl_speak_up.trade_fail_fs
|
||||
end
|
||||
@ -189,7 +325,7 @@ yl_speak_up.get_fs_trade_simple = function(player, player_gives, npc_gives)
|
||||
"list[current_player;main;0.2,5.08;8,3;8]"
|
||||
|
||||
-- configuration of a new trade happens here
|
||||
if(not(trade.player_gives) or not(trade.npc_gives)) then
|
||||
if(not(trade.player_gives) or not(trade.npc_gives) or trade.edit_trade) then
|
||||
-- is this player allowed to edit the NPC and his trades? If not abort.
|
||||
if(not(yl_speak_up.may_edit_npc(player, trade.n_id))) then
|
||||
return formspec..
|
||||
@ -206,10 +342,12 @@ yl_speak_up.get_fs_trade_simple = function(player, player_gives, npc_gives)
|
||||
"label[1.5,2.8;Put items in the two slots and click on \"Store trade\".]"..
|
||||
"label[1.5,3.2;You will get your items back when storing the trade.]"..
|
||||
"button[0.2,1.6;1.0,0.9;abort_trade_simple;Abort]"..
|
||||
"button[0.2,2.6;1.0,0.9;delete_trade_simple;Delete]"..
|
||||
"button[6.2,1.6;2.0,0.9;store_trade_simple;Store trade]"..
|
||||
"tooltip[store_trade_simple;Click here to store this as a new trade. Your "..
|
||||
"items will be returned to you and the trade will be shown the way "..
|
||||
"the customer can see it.]"..
|
||||
"tooltip[delete_trade_simple;Delete this trade.]"..
|
||||
"tooltip[abort_trade_simple;Abort setting up this new trade.]"
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user