allowed nil as return value for aborting actions

This commit is contained in:
Sokomine 2021-06-29 04:51:39 +02:00
parent 01af5d1365
commit 86eaaae3ea
2 changed files with 32 additions and 11 deletions

View File

@ -68,8 +68,9 @@ yl_speak_up.get_fs_action_custom = function(player, param)
return "size[8.5,4.0]"..
"button[0.2,0.0;2.0,0.9;back_to_talk;Back to talk]"..
"button[4.75,0.0;3.0,0.9;finished_action;Stare back]"..
"tooltip[back_to_talk;Click here if you want the action to FAIL.]"..
"button[4.75,0.8;3.0,0.9;failed_action;Fail to stare back]"..
"tooltip[back_to_talk;Click here if you want to abort/cancel.]"..
"tooltip[failed_action;Click here if you want the action to FAIL.]"..
"tooltip[finished_action;Click here if you want the action to be a SUCCESS.]"..
"label[0.5,1.7;"..minetest.formspec_escape(
"["..(dialog.n_npc or "- ? -").." stares expectantly at you.]").."]"..
@ -92,6 +93,11 @@ yl_speak_up.input_fs_action_custom = function(player, formname, fields)
local pname = player:get_player_name()
local a_id = yl_speak_up.speak_to[pname].a_id
if(fields.back_to_talk) then
-- the action was aborted
yl_speak_up.execute_next_action(player, a_id, nil)
return
end
if(fields.failed_action) then
-- the action failed
yl_speak_up.execute_next_action(player, a_id, false)
return

View File

@ -147,8 +147,22 @@ yl_speak_up.execute_next_action = function(player, a_id, result_of_a_id)
local nr = 0
if(a_id) then
nr = table.indexof(sorted_key_list, a_id)
-- did the player go back?
if(nr > -1 and result_of_a_id == nil) then
-- set the current action to nil
yl_speak_up.speak_to[pname].a_id = nil
-- no option of the new dialog has been selected yet
yl_speak_up.speak_to[pname].o_id = nil
-- show the new dialog
yl_speak_up.debug_msg(player, n_id, o_id, "Action "..
tostring(a_id).." aborted. Switching back to dialog "..
tostring(d_id)..".")
yl_speak_up.speak_to[pname].o_id = nil
yl_speak_up.speak_to[pname].a_id = nil
yl_speak_up.show_fs(player, "talk", {n_id = n_id, d_id = d_id})
return
-- did the action fail?
if(nr > -1 and not(result_of_a_id)) then
elseif(nr > -1 and not(result_of_a_id)) then
local this_action = actions[ sorted_key_list[ nr ]]
-- if there is an on_failure target dialog: go there
if(this_action.a_on_failure) then
@ -282,7 +296,8 @@ yl_speak_up.action_quest_item_prepare = function(player)
local o_id = yl_speak_up.speak_to[pname].o_id
yl_speak_up.debug_msg(player, n_id, o_id, "Action "..tostring(a.a_id)..": NPC ran out of "..
tostring(a.a_value)..".")
return false
-- just go back; the player didn't do anything wrong
return nil
end
-- get the items from the NPCs inventory
local new_stack = npc_inv:remove_item("npc_main", stack)
@ -434,8 +449,8 @@ yl_speak_up.input_fs_action_npc_gives = function(player, formname, fields)
local trade_inv = minetest.get_inventory({type="detached", name="yl_speak_up_player_"..pname})
local a_id = yl_speak_up.speak_to[pname].a_id
if(fields.npc_does_not_have_item) then
-- the NPC can't supply the item - the action failed
yl_speak_up.execute_next_action(player, a_id, false)
-- the NPC can't supply the item - abort the action
yl_speak_up.execute_next_action(player, a_id, nil)
return
end
-- is the npc_gives inv empty? then all went as expected.
@ -455,7 +470,7 @@ yl_speak_up.input_fs_action_npc_gives = function(player, formname, fields)
-- and give that (hopefully) stackable stack back to the NPC
yl_speak_up.action_quest_item_take_back(player)
-- the action failed
yl_speak_up.execute_next_action(player, a_id, false)
yl_speak_up.execute_next_action(player, a_id, nil)
return
end
-- else show a message to the player that he ought to take the item
@ -518,8 +533,8 @@ yl_speak_up.input_fs_action_npc_wants = function(player, formname, fields)
local a_id = yl_speak_up.speak_to[pname].a_id
-- is the npc_wants inv empty and the player pressed the back to talk button? then the action failed.
if(trade_inv:is_empty("npc_wants") and fields.back_to_talk) then
-- the action failed
yl_speak_up.execute_next_action(player, a_id, false)
-- the action was aborted
yl_speak_up.execute_next_action(player, a_id, nil)
return
end
-- the player tried to give something; check if it is the right thing
@ -573,8 +588,8 @@ yl_speak_up.input_fs_action_text_input = function(player, formname, fields)
local a_id = yl_speak_up.speak_to[pname].a_id
local a = yl_speak_up.get_action_by_player(player)
if(fields.back_to_talk) then
-- the action failed
yl_speak_up.execute_next_action(player, a_id, false)
-- the action was aborted
yl_speak_up.execute_next_action(player, a_id, nil)
return
end
if(fields.finished_action and fields.quest_answer and fields.quest_answer ~= "") then