added save function for actions of type trade

This commit is contained in:
Sokomine 2021-06-16 01:36:50 +02:00
parent 5d0083212a
commit 5f652f62ae
3 changed files with 54 additions and 25 deletions

View File

@ -95,7 +95,8 @@ yl_speak_up.show_action = function(a)
if(not(a.a_type) or a.a_type == "" or a.a_type == "none") then
return "(nothing): Nothing to do. No action."
elseif(a.a_type == "trade") then
return "trade:" -- TODO show ation text
return "NPC sells \""..table.concat(a.a_buy, ";").."\" for \""..
table.concat(a.a_pay, ";").."\"."
elseif(a.a_type == "npc_gives") then
return "The NPC gives \""..tostring(a.a_item_desc or "- default description -")..
"\" (\""..tostring(a.a_value or "- ? -").."\") "..

View File

@ -434,6 +434,42 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
end
v[ "r_value" ] = data.chat_msg_text
-- "Normal trade - one item(stack) for another item(stack).", -- 3
-- (only for actions)
elseif(data.what and id_prefix == "a_" and data.what == 3) then
-- remember which option was selected
yl_speak_up.speak_to[pname].o_id = o_id
-- do not switch target dialog (we are in edit mode)
yl_speak_up.speak_to[pname].target_d_id = nil
-- just to make sure that the trade_id is properly set...
if(not(data.trade_id)) then
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:"..formspec_input_to,
formspec = "size[9,2.5]"..
"label[0.2,0.5;Error: Missing trade ID.]"..
"button[1.5,2.0;2,0.9;back_from_error_msg;Back]"})
return
end
-- the button is called store_trade_simple instead of save_element in
-- the trade simple function(s); we want to store a trade
fields.store_trade_simple = true
local res = yl_speak_up.input_add_trade_simple(player, "", fields)
-- the above function sets:
-- dialog.trades[ trade_id ] = {pay={ps},buy={bs}, d_id = d_id, o_id = o_id}
-- store the trade as an action:
local dialog = yl_speak_up.speak_to[pname].dialog
if(res and dialog.trades and dialog.trades[ data.trade_id ]) then
v[ "a_value" ] = data.trade_id
v[ "a_pay" ] = dialog.trades[ data.trade_id ].pay
v[ "a_buy" ] = dialog.trades[ data.trade_id ].buy
v[ "a_on_failure" ] = ""
if(data.action_failure_dialog) then
local dialog = yl_speak_up.speak_to[pname].dialog
local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs)
v[ "a_on_failure" ] = sorted_dialog_list[ data.action_failure_dialog ]
end
end
-- "The NPC gives something to the player (i.e. a quest item).", -- 4
-- "The player is expected to give something to the NPC (i.e. a quest item).", -- 5
-- (only for actions)
@ -652,24 +688,6 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
yl_speak_up.speak_to[pname][ tmp_data_cache ].action_failure_dialog = nr
end
--[[
-- "Normal trade - one item(stack) for another item(stack).", -- 3
-- (only for actions)
-- TODO: this needs to be adjusted
if(data and data.what == 3 and id_prefix == "a_") then
-- remember which option was selected
yl_speak_up.speak_to[pname].o_id = o_id
-- do not switch target dialog (we are in edit mode)
yl_speak_up.speak_to[pname].target_d_id = nil
-- create a new trade for this dialog and option - with ID "<d_id> <o_id>"
yl_speak_up.show_fs(player, "add_trade_simple", tostring(d_id).." "..tostring(o_id))
return
end
-- TODO: set trade_id in the input_ function and then call
-- yl_speak_up.input_add_trade_simple(player, formname, fields)
-- TODO gets saved as: dialog.trades[ trade_id ] = {pay={ps},buy={bs}, d_id = d_id, o_id = o_id}
--]]
-- the player wants to change/edit a precondition or effect
if(not(fields.back)
and (fields.change_element or fields.select_what or fields.select_trade

View File

@ -253,10 +253,17 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields)
end
local pname = player:get_player_name()
local input_to = "add_trade_simple"
-- are we editing an action of the type trade?
if( yl_speak_up.speak_to[pname][ "tmp_action" ]
and yl_speak_up.speak_to[pname][ "tmp_action" ].what == 3) then
input_to = "edit_actions"
end
-- we return from showing an error message (the player may not have noticed
-- a chat message while viewing a formspec; thus, we showed a formspec message)
if(fields.back_from_error_msg) then
yl_speak_up.show_fs(player, "add_trade_simple")
yl_speak_up.show_fs(player, input_to)
return
end
@ -368,7 +375,7 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields)
-- show error message (that leads back to this formspec)
if(error_msg) then
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:add_trade_simple",
input_to = "yl_speak_up:"..input_to,
formspec =
"size[6,2]"..
"label[0.2,0.5;"..error_msg.."]"..
@ -404,7 +411,8 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields)
if(not(dialog.trades)) then
dialog.trades = {}
end
if(dialog.trades[ trade_id ] and dialog.trades[ trade_id ].d_id) then
if(dialog.trades[ trade_id ] and dialog.trades[ trade_id ].d_id
and input_to ~= "edit_actions") then
yl_speak_up.speak_to[pname].trade_id = trade_id
-- tell the player that the new trade has been added
yl_speak_up.show_fs(player, "msg", {
@ -422,9 +430,11 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields)
else
-- we are no longer trading
yl_speak_up.speak_to[pname].trade_id = nil
-- ..else go back to the edit options formspec
yl_speak_up.show_fs(player, "edit_option_dialog",
{n_id = n_id, d_id = d_id, o_id = o_id})
-- the trade has been stored or deleted successfully
return true
-- -- ..else go back to the edit options formspec (obsolete)
-- yl_speak_up.show_fs(player, "edit_option_dialog",
-- {n_id = n_id, d_id = d_id, o_id = o_id})
end
end