actually apply alternate_text texts

This commit is contained in:
Sokomine 2021-07-01 23:14:33 +02:00
parent 3d4141e786
commit 577ea356aa
4 changed files with 19 additions and 9 deletions

View File

@ -182,7 +182,8 @@ yl_speak_up.execute_next_action = function(player, a_id, result_of_a_id)
yl_speak_up.speak_to[pname].o_id = nil yl_speak_up.speak_to[pname].o_id = nil
yl_speak_up.speak_to[pname].a_id = nil yl_speak_up.speak_to[pname].a_id = nil
yl_speak_up.show_fs(player, "talk", {n_id = n_id, yl_speak_up.show_fs(player, "talk", {n_id = n_id,
d_id = this_action.a_on_failure}) d_id = this_action.a_on_failure,
alternate_text = this_action.alternate_text})
return return
else else
yl_speak_up.debug_msg(player, n_id, o_id, "Action ".. yl_speak_up.debug_msg(player, n_id, o_id, "Action "..
@ -211,7 +212,8 @@ yl_speak_up.execute_next_action = function(player, a_id, result_of_a_id)
"Doing effects/results now.") "Doing effects/results now.")
-- execute all effects/results -- execute all effects/results
local effects = dialog.n_dialogs[d_id].d_options[o_id].o_results local effects = dialog.n_dialogs[d_id].d_options[o_id].o_results
local target_dialog = yl_speak_up.execute_all_relevant_effects(player, effects, o_id, true) local res = yl_speak_up.execute_all_relevant_effects(player, effects, o_id, true)
local target_dialog = res.next_dialog
yl_speak_up.speak_to[pname].o_id = nil yl_speak_up.speak_to[pname].o_id = nil
yl_speak_up.speak_to[pname].a_id = nil yl_speak_up.speak_to[pname].a_id = nil
if(not(target_dialog) if(not(target_dialog)
@ -220,7 +222,8 @@ yl_speak_up.execute_next_action = function(player, a_id, result_of_a_id)
target_dialog = d_id target_dialog = d_id
end end
-- the function above returns a target dialog; show that to the player -- the function above returns a target dialog; show that to the player
yl_speak_up.show_fs(player, "talk", {n_id = n_id, d_id = target_dialog}) yl_speak_up.show_fs(player, "talk", {n_id = n_id, d_id = target_dialog,
alternate_text = res.alternate_text})
end end

View File

@ -230,7 +230,7 @@ yl_speak_up.execute_all_relevant_effects = function(player, effects, o_id, actio
if(not(effects)) then if(not(effects)) then
yl_speak_up.debug_msg(player, n_id, o_id, "No effects given.") yl_speak_up.debug_msg(player, n_id, o_id, "No effects given.")
-- no effects? Then...return to the start dialog -- no effects? Then...return to the start dialog
return "" return {next_dialog = "", alternate_text = nil}
end end
local edit_mode = (yl_speak_up.edit_mode[pname] == n_id) local edit_mode = (yl_speak_up.edit_mode[pname] == n_id)
-- Important: the list of effects is *sorted* here. The order remains constant! -- Important: the list of effects is *sorted* here. The order remains constant!
@ -244,6 +244,8 @@ yl_speak_up.execute_all_relevant_effects = function(player, effects, o_id, actio
else else
yl_speak_up.debug_msg(player, n_id, o_id, "Not executing effects because in edit mode.") yl_speak_up.debug_msg(player, n_id, o_id, "Not executing effects because in edit mode.")
end end
-- failed actions may set an alternate text
local alternate_text = nil
local last_result = action_was_successful local last_result = action_was_successful
local res = true local res = true
for i, k in ipairs(sorted_key_list) do for i, k in ipairs(sorted_key_list) do
@ -258,6 +260,7 @@ yl_speak_up.execute_all_relevant_effects = function(player, effects, o_id, actio
if(not(res)) then if(not(res)) then
yl_speak_up.debug_msg(player, n_id, o_id, tostring(r.r_id).. yl_speak_up.debug_msg(player, n_id, o_id, tostring(r.r_id)..
" -> Effect failed to execute.") " -> Effect failed to execute.")
alternate_text = r.alternate_text
end end
else else
-- in edit mode: assume that the effect was successful -- in edit mode: assume that the effect was successful
@ -266,19 +269,20 @@ yl_speak_up.execute_all_relevant_effects = function(player, effects, o_id, actio
-- "dialog" gives us the normal target_dialog -- "dialog" gives us the normal target_dialog
if(r.r_type and r.r_type == "dialog") then if(r.r_type and r.r_type == "dialog") then
target_dialog = r.r_value target_dialog = r.r_value
alternate_text = r.alternate_text
-- "on_failure" gives an alternate target dialog if the action -- "on_failure" gives an alternate target dialog if the action
-- or last effect failed -- or last effect failed
elseif(r.r_type and r.r_type == "on_failure" and r.r_value and not(last_result)) then elseif(r.r_type and r.r_type == "on_failure" and r.r_value and not(last_result)) then
yl_speak_up.debug_msg(player, n_id, o_id, "Aborted executing effects at ".. yl_speak_up.debug_msg(player, n_id, o_id, "Aborted executing effects at "..
tostring(r.r_id)..". New target dialog: "..tostring(r.r_value)..".") tostring(r.r_id)..". New target dialog: "..tostring(r.r_value)..".")
-- we also stop execution here -- we also stop execution here
return r.r_value return {next_dialog = r.r_value, alternate_text = r.alternate_text}
end end
last_result = res last_result = res
end end
-- all preconditions are true -- all preconditions are true
yl_speak_up.debug_msg(player, n_id, o_id, "Finished executing effects.") yl_speak_up.debug_msg(player, n_id, o_id, "Finished executing effects.")
return target_dialog return {next_dialog = target_dialog, alternate_text = alternate_text}
end end

View File

@ -725,7 +725,7 @@ end
-- talk -- talk
yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id) yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text)
local pname = player:get_player_name() local pname = player:get_player_name()
local dialog = yl_speak_up.speak_to[pname].dialog local dialog = yl_speak_up.speak_to[pname].dialog
local context_d_id = yl_speak_up.speak_to[pname].d_id local context_d_id = yl_speak_up.speak_to[pname].d_id
@ -922,9 +922,12 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id)
table.insert(formspec, minetest.formspec_escape(active_dialog.d_text)) table.insert(formspec, minetest.formspec_escape(active_dialog.d_text))
table.insert(formspec, "]") table.insert(formspec, "]")
else else
if(alternate_text) then
alternate_text = string.gsub(alternate_text, "%$TEXT%$", active_dialog.d_text)
end
-- replace $NPC_NAME$ etc. -- replace $NPC_NAME$ etc.
local t = minetest.formspec_escape(yl_speak_up.replace_vars_in_text( local t = minetest.formspec_escape(yl_speak_up.replace_vars_in_text(
active_dialog.d_text, dialog, pname)) (alternate_text or active_dialog.d_text), dialog, pname))
table.insert(formspec, "hypertext[0.2,5;19.6,17.8;d_text;<normal>") table.insert(formspec, "hypertext[0.2,5;19.6,17.8;d_text;<normal>")
table.insert(formspec, t .. "\n</normal>") table.insert(formspec, t .. "\n</normal>")
table.insert(formspec, "]") table.insert(formspec, "]")

View File

@ -211,7 +211,7 @@ yl_speak_up.show_fs = function(player, fs_name, param)
param = {} param = {}
end end
minetest.show_formspec(pname, "yl_speak_up:talk", minetest.show_formspec(pname, "yl_speak_up:talk",
yl_speak_up.get_fs_talkdialog(player, param.n_id, param.d_id)) yl_speak_up.get_fs_talkdialog(player, param.n_id, param.d_id, param.alternate_text))
elseif(fs_name == "fashion") then elseif(fs_name == "fashion") then
minetest.show_formspec(pname, "yl_speak_up:fashion", minetest.show_formspec(pname, "yl_speak_up:fashion",