get_fs_talkdialog no longer local; call yl_speak_up.input_do_trade_simple; added yl_speak_up.get_result_id_by_type; made show/edit trade work

This commit is contained in:
Sokomine 2021-05-23 21:41:54 +02:00
parent 0ccbd774a1
commit 6fc01ab754

View File

@ -1066,7 +1066,7 @@ end
-- talk
local function get_fs_talkdialog(player, n_id, d_id)
yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id)
local pname = player:get_player_name()
local dialog = yl_speak_up.speak_to[pname].dialog
local context_d_id = yl_speak_up.speak_to[pname].d_id
@ -1744,6 +1744,9 @@ minetest.register_on_player_receive_fields( function(player, formname, fields)
elseif formname == "yl_speak_up:inventory" then
yl_speak_up.input_inventory(player, formname, fields)
return true
elseif formname == "yl_speak_up:do_trade_simple" then
yl_speak_up.input_do_trade_simple(player, formname, fields)
return true
elseif formname == "yl_speak_up:add_trade_simple" then
yl_speak_up.input_add_trade_simple(player, formname, fields)
return true
@ -1760,7 +1763,8 @@ yl_speak_up.input_inventory = function(player, formname, fields)
-- ..save the (very probably) modified inventory
yl_speak_up.save_npc_inventory(n_id)
-- ..and go back to the normal talk formspec
minetest.show_formspec(pname, "yl_speak_up:talk", get_fs_talkdialog(player, n_id, d_id))
minetest.show_formspec(pname, "yl_speak_up:talk",
yl_speak_up.get_fs_talkdialog(player, n_id, d_id))
end
@ -2061,6 +2065,27 @@ yl_speak_up.add_new_result = function(dialog, d_id, o_id)
end
-- this is useful for result types that can exist only once per option
-- (apart from editing with the staff);
-- examples: "dialog" and "trade";
-- returns tue r_id or nil if no result of that type has been found
yl_speak_up.get_result_id_by_type = function(dialog, d_id, o_id, result_type)
if(not(dialog) or not(dialog.n_dialogs) or not(dialog.n_dialogs[d_id])
or not(dialog.n_dialogs[d_id].d_options) or not(dialog.n_dialogs[d_id].d_options[o_id])) then
return
end
local results = dialog.n_dialogs[d_id].d_options[o_id].o_results
if(not(results)) then
return
end
for k, v in pairs(results) do
if(v.r_type == result_type) then
return k
end
end
end
-- this is always called when switching to a new dialog; but only in edit_mode does
-- it show a formspec asking for change/back/discard
yl_speak_up.save_changes_and_switch_to_other_dialog = function(player, fields, target_dialog)
@ -2082,7 +2107,8 @@ yl_speak_up.save_changes_and_switch_to_other_dialog = function(player, fields, t
-- the player decided to go back and continue editing the current dialog
if(edit_mode and fields.back_to_dialog_changes) then
-- do NOT clear the list of changes; just show the old dialog again
minetest.show_formspec(pname, "yl_speak_up:talk", get_fs_talkdialog(player, n_id, d_id))
minetest.show_formspec(pname, "yl_speak_up:talk",
yl_speak_up.get_fs_talkdialog(player, n_id, d_id))
return
-- save changes and continue on to the next dialog
@ -2184,7 +2210,8 @@ yl_speak_up.save_changes_and_switch_to_other_dialog = function(player, fields, t
end
-- move on to the target dialog
yl_speak_up.speak_to[pname].d_id = target_dialog
minetest.show_formspec(pname, "yl_speak_up:talk", get_fs_talkdialog(player, n_id, target_dialog))
minetest.show_formspec(pname, "yl_speak_up:talk",
yl_speak_up.get_fs_talkdialog(player, n_id, target_dialog))
end
@ -2615,7 +2642,8 @@ yl_speak_up.input_edit_option_dialog = function(player, formname, fields)
-- back to the main dialog window?
-- (this also happens when the last option was deleted)
if(fields.show_current_dialog or fields.quit or fields.button_exit or not(d_option) or fields.del_option) then
minetest.show_formspec(pname, "yl_speak_up:talk", get_fs_talkdialog(player, n_id, d_id))
minetest.show_formspec(pname, "yl_speak_up:talk",
yl_speak_up.get_fs_talkdialog(player, n_id, d_id))
return
end
@ -2649,34 +2677,45 @@ yl_speak_up.input_edit_option_dialog = function(player, formname, fields)
yl_speak_up.get_fs_edit_option_dialog(player, n_id, d_id, o_found))
return
elseif(fields.effect_add_trade) then
yl_speak_up.trade[pname] = {
-- we start with the simple trade
trade_type = "trade_simple",
-- we will create a new trade
player_gives = nil,
npc_gives = nil,
-- can be determined from other variables, but it is easier to store it here
n_id = n_id,
npc_name = dialog.n_npc,
-- for statistics and in order to determine which dialog to show next
trade_done = 0,
-- we need to know which option this is
o_id = fields.o_id,
}
minetest.show_formspec(pname, "yl_speak_up:add_trade_simple",
yl_speak_up.get_fs_trade_simple(player))
return
elseif(fields.effect_add_trade
or fields.effect_show_trade) then
local r_id = yl_speak_up.get_result_id_by_type(dialog, d_id, fields.o_id, "trade")
local data = dialog.n_dialogs[d_id].d_options[fields.o_id].o_results[r_id]
-- which dialog shall be shown in case of a successful trade?
local target_d_id = yl_speak_up.get_result_id_by_type(dialog, d_id, fields.o_id, "dialog")
-- we are dealing with a new trade
if(not(r_id) or not(data) or not(data.r_trade_type)) then
yl_speak_up.trade[pname] = {
-- we start with the simple trade
trade_type = "trade_simple",
-- we will create a new trade
player_gives = nil,
npc_gives = nil,
-- can be determined from other variables, but it is easier to store it here
n_id = n_id,
npc_name = dialog.n_npc,
-- for statistics and in order to determine which dialog to show next
trade_done = 0,
-- we need to know which option this is
o_id = fields.o_id,
r_id = r_id,
edit_trade = true,
target_dialog = target_d_id,
}
-- show the trade config dialog
minetest.show_formspec(pname, "yl_speak_up:add_trade_simple",
yl_speak_up.get_fs_trade_simple(player))
return
end
elseif(fields.effect_show_trade) then
-- TODO: identify the right result and trade items
-- identify the right result and trade items
-- TODO: this may not be a simple trade but instead a more complex one
yl_speak_up.trade[pname] = {
-- we start with the simple trade
trade_type = "trade_simple",
-- trade_simple is a fallback
trade_type = data.r_trade_type,
-- we will create a new trade
player_gives = nil, -- TODO
npc_gives = nil, -- TODO
player_gives = data.r_player_gives,
npc_gives = data.r_npc_gives,
-- can be determined from other variables, but it is easier to store it here
n_id = n_id,
npc_name = dialog.n_npc,
@ -2684,9 +2723,13 @@ yl_speak_up.input_edit_option_dialog = function(player, formname, fields)
trade_done = 0,
-- we need to know which option this is
o_id = fields.o_id,
r_id = r_id,
target_dialog = target_d_id,
}
minetest.show_formspec(pname, "yl_speak_up:edit_option_dialog",
-- show the trade dialog
minetest.show_formspec(pname, "yl_speak_up:do_trade_simple",
yl_speak_up.get_fs_trade_simple(player))
return
end
@ -2695,7 +2738,8 @@ yl_speak_up.input_edit_option_dialog = function(player, formname, fields)
-- if ESC is pressed or anything else unpredicted happens: go back to the main dialog edit window
-- reason: don't loose any unsaved changes to the dialog
minetest.show_formspec(pname, "yl_speak_up:talk", get_fs_talkdialog(player, n_id, d_id))
minetest.show_formspec(pname, "yl_speak_up:talk",
yl_speak_up.get_fs_talkdialog(player, n_id, d_id))
end
@ -2823,7 +2867,8 @@ yl_speak_up.input_talk = function(player, formname, fields)
end
-- actually start a chat with our new npc
minetest.show_formspec(pname, "yl_speak_up:talk", get_fs_talkdialog(player, n_id, d_id))
minetest.show_formspec(pname, "yl_speak_up:talk",
yl_speak_up.get_fs_talkdialog(player, n_id, d_id))
return
end
@ -2877,7 +2922,8 @@ yl_speak_up.input_talk = function(player, formname, fields)
yl_speak_up.edit_mode[pname] = yl_speak_up.speak_to[pname].n_id
-- start a new chat - but this time in edit mode
yl_speak_up.speak_to[pname].d_id = nil
minetest.show_formspec(pname, "yl_speak_up:talk", get_fs_talkdialog(player, yl_speak_up.speak_to[pname].n_id, nil))
minetest.show_formspec(pname, "yl_speak_up:talk",
yl_speak_up.get_fs_talkdialog(player, yl_speak_up.speak_to[pname].n_id, nil))
return
-- end edit mode (does not require the priv; will only switch back to normal behaviour)
elseif fields.button_end_edit_mode then
@ -2898,7 +2944,7 @@ yl_speak_up.input_talk = function(player, formname, fields)
minetest.show_formspec(
pname,
"yl_speak_up:talk",
get_fs_talkdialog(player, yl_speak_up.speak_to[pname].n_id, yl_speak_up.speak_to[pname].d_id)
yl_speak_up.get_fs_talkdialog(player, yl_speak_up.speak_to[pname].n_id, yl_speak_up.speak_to[pname].d_id)
)
return
elseif fields.button_down then --and yl_speak_up.speak_to[pname].option_index > yl_speak_up.max_number_of_buttons then
@ -2910,7 +2956,7 @@ yl_speak_up.input_talk = function(player, formname, fields)
minetest.show_formspec(
pname,
"yl_speak_up:talk",
get_fs_talkdialog(player, yl_speak_up.speak_to[pname].n_id, yl_speak_up.speak_to[pname].d_id)
yl_speak_up.get_fs_talkdialog(player, yl_speak_up.speak_to[pname].n_id, yl_speak_up.speak_to[pname].d_id)
)
return
else
@ -2987,7 +3033,8 @@ yl_speak_up.input_talk = function(player, formname, fields)
yl_speak_up.save_changes_and_switch_to_other_dialog(player, fields, show_dialog)
-- show the same dialog again
else
minetest.show_formspec(pname, "yl_speak_up:talk", get_fs_talkdialog(player, n_id, d_id))
minetest.show_formspec(pname, "yl_speak_up:talk",
yl_speak_up.get_fs_talkdialog(player, n_id, d_id))
end
-- no option was selected - so we need to end this here
return
@ -3307,7 +3354,8 @@ function yl_speak_up.talk(self, clicker)
yl_speak_up.speak_to[pname].obj = self.object
minetest.show_formspec(pname, "yl_speak_up:talk", get_fs_talkdialog(clicker, n_id))
minetest.show_formspec(pname, "yl_speak_up:talk",
yl_speak_up.get_fs_talkdialog(clicker, n_id))
end
-- ###
@ -3559,7 +3607,8 @@ yl_speak_up.input_fashion = function(player, formname, fields)
-- is the player editing this npc? then we need to go back to the edit menu
if( yl_speak_up.edit_mode[pname]
and yl_speak_up.edit_mode[pname] == yl_speak_up.speak_to[pname].n_id) then
minetest.show_formspec(pname, "yl_speak_up:talk", get_fs_talkdialog(player, yl_speak_up.speak_to[pname].n_id, yl_speak_up.speak_to[pname].d_id))
minetest.show_formspec(pname, "yl_speak_up:talk",
yl_speak_up.get_fs_talkdialog(player, yl_speak_up.speak_to[pname].n_id, yl_speak_up.speak_to[pname].d_id))
return
end
yl_speak_up.speak_to[pname] = nil