use specified prefix for all knots in ink export

This commit is contained in:
Sokomine 2024-12-26 18:15:23 +01:00
parent a7f5f3c8b0
commit 293df54dac

View File

@ -13,9 +13,9 @@ local use_d_name = true
-- in order to be able to deal with multiple NPC in ink, we use the NPC id n_id
-- plus the dialog id d_id as a name prefix; o_id, a_id and r_id are appended
-- as needed
yl_speak_up.export_to_ink.print_knot_name = function(lines, knot_name)
yl_speak_up.export_to_ink.print_knot_name = function(lines, knot_name, use_prefix)
table.insert(lines, "\n\n=== ")
table.insert(lines, tostring(knot_name or "ERROR"))
table.insert(lines, use_prefix..tostring(knot_name or "ERROR"))
table.insert(lines, " ===")
end
@ -55,7 +55,7 @@ end
-- choices are a bit complicated as they may contain alternate_text that is to be
-- displayed instead (in yl_speak_up) and before (in ink) shwoing the target dialog text;
-- also, the divert_to target dialog may need to be rewritten
yl_speak_up.export_to_ink.print_choice = function(lines, choice_text, n_id, start_dialog,
yl_speak_up.export_to_ink.print_choice = function(lines, choice_text, use_prefix, start_dialog,
alternate_text, divert_to, only_once, label,
precondition_list, effect_list,
dialog_names)
@ -111,19 +111,18 @@ yl_speak_up.export_to_ink.print_choice = function(lines, choice_text, n_id, star
table.insert(lines, "\n")
end
-- actually go to the dialog this option leads to
table.insert(lines, " -> ")
table.insert(lines, " -> "..use_prefix)
if(not(start_dialog) or start_dialog == "") then
start_dialog = "d_1"
end
if(not(divert_to) or divert_to == "") then
-- go back to the start dialog (the start dialog may have been changed)
divert_to = tostring(start_dialog)
elseif(divert_to == "d_end" or divert_to == tostring(n_id).."_d_end") then
elseif(divert_to == "d_end" or divert_to == use_prefix.."d_end") then
-- go back to choosing between talking to NPC and end
divert_to = tostring(n_id).."_d_end"
elseif(string.sub(divert_to, 1, 2) ~= "n_") then
-- make sure it is prefixed with the n_id
divert_to = tostring(n_id).."_"..tostring(divert_to)
divert_to = "d_end"
else
divert_to = tostring(divert_to)
end
if(dialog_names and dialog_names[divert_to]) then
divert_to = dialog_names[divert_to]
@ -134,12 +133,12 @@ end
-- this prints the dialog as a knot - but without choices (those are added to the lines table later)
-- d: dialog
yl_speak_up.export_to_ink.print_dialog_knot = function(lines, n_id, d_id, d)
local knot_name = tostring(n_id).."_"..tostring(d_id)
yl_speak_up.export_to_ink.print_dialog_knot = function(lines, use_prefix, d_id, d)
local knot_name = tostring(d_id)
if(use_d_name) then
knot_name = (d.d_name or knot_name)
end
ink_export.print_knot_name(lines, knot_name)
ink_export.print_knot_name(lines, knot_name, use_prefix)
-- many characters at the start of a line have a special meaning;
-- hopefully they will not be obstrusive later on;
@ -159,26 +158,26 @@ end
-- a knot for each action
-- Parameter:
-- a action
yl_speak_up.export_to_ink.print_action_knot = function(lines, n_id, d_id, o_id, start_dialog,
yl_speak_up.export_to_ink.print_action_knot = function(lines, use_prefix, d_id, o_id, start_dialog,
a, alternate_text_on_success, next_target, dialog_names)
local knot_name = tostring(n_id).."_"..tostring(d_id).."_"..tostring(o_id).."_"..tostring(a.a_id)
ink_export.print_knot_name(lines, knot_name)
local knot_name = tostring(d_id).."_"..tostring(o_id).."_"..tostring(a.a_id)
ink_export.print_knot_name(lines, knot_name, use_prefix)
table.insert(lines, "\n:action: ")
table.insert(lines, a.a_id)
table.insert(lines, " ")
table.insert(lines, yl_speak_up.show_action(a))
ink_export.print_choice(lines, "Action was successful", n_id, start_dialog,
ink_export.print_choice(lines, "Action was successful", use_prefix, start_dialog,
alternate_text_on_success, next_target, false, nil,
nil, nil, dialog_names)
ink_export.print_choice(lines, "Action failed", n_id, start_dialog,
ink_export.print_choice(lines, "Action failed", use_prefix, start_dialog,
a.alternate_text, a.a_on_failure, false, nil,
nil, nil, dialog_names)
ink_export.print_choice(lines, "Back", n_id, start_dialog,
nil, tostring(n_id).."_"..tostring(d_id), false, nil,
ink_export.print_choice(lines, "Back", use_prefix, start_dialog,
nil, tostring(d_id), false, nil,
nil, nil, dialog_names)
return knot_name
end
@ -190,11 +189,11 @@ end
-- Parameter:
-- r effect/result
-- r_prev previous effect
yl_speak_up.export_to_ink.print_effect_knot = function(lines, n_id, d_id, o_id, start_dialog,
yl_speak_up.export_to_ink.print_effect_knot = function(lines, use_prefix, d_id, o_id, start_dialog,
r, r_prev, alternate_text_on_success, next_target,
dialog_names)
local knot_name = tostring(n_id).."_"..tostring(d_id).."_"..tostring(o_id).."_"..tostring(r.r_id)
ink_export.print_knot_name(lines, knot_name)
local knot_name = tostring(d_id).."_"..tostring(o_id).."_"..tostring(r.r_id)
ink_export.print_knot_name(lines, knot_name, use_prefix)
table.insert(lines, "\n:effect: ")
table.insert(lines, r.r_id)
@ -208,11 +207,11 @@ yl_speak_up.export_to_ink.print_effect_knot = function(lines, n_id, d_id, o_id,
-- show text of the *previous effect* - because that is the one which may have failed:
table.insert(lines, yl_speak_up.show_effect(r_prev))
ink_export.print_choice(lines, "Effect was successful", n_id, start_dialog,
ink_export.print_choice(lines, "Effect was successful", use_prefix, start_dialog,
alternate_text_on_success, next_target, false, nil,
nil, nil, dialog_names)
ink_export.print_choice(lines, "Effect failed", n_id, start_dialog,
ink_export.print_choice(lines, "Effect failed", use_prefix, start_dialog,
r.alternate_text, r.r_value, false, nil,
nil, nil, dialog_names)
@ -221,7 +220,7 @@ end
-- which variables are used by this NPC?
yl_speak_up.export_to_ink.print_variables_used = function(lines, dialog, n_id, pname)
yl_speak_up.export_to_ink.print_variables_used = function(lines, dialog)
if(not(dialog) or not(dialog.n_dialogs)) then
return
end
@ -306,7 +305,7 @@ local var_with_operator = function(liste, var_name, op, var_cmp_value, vars_used
-- "quest_step_done", "quest_step_not_done"
end
yl_speak_up.export_to_ink.translate_precondition_list = function(dialog, preconditions, vars_used, n_id,
yl_speak_up.export_to_ink.translate_precondition_list = function(dialog, preconditions, vars_used, use_prefix,
dialog_names)
-- collect preconditions that may work in ink
local liste = {}
@ -320,9 +319,9 @@ yl_speak_up.export_to_ink.translate_precondition_list = function(dialog, precond
var_with_operator(liste, p.p_value, p.p_operator, p.p_var_cmp_value, vars_used)
elseif(p and p.p_type and p.p_type == "evaluate" and p.p_value == "counted_visits_to_option") then
-- simulate the visit counter that ink has in yl_speak_up
local tmp_var_name = n_id.."_"..p.p_param1
local tmp_var_name = use_prefix..p.p_param1
if(dialog_names[tmp_var_name]) then
tmp_var_name = dialog_names[tmp_var_name].."."..tostring(p.p_param2)
tmp_var_name = use_prefix..dialog_names[tmp_var_name].."."..tostring(p.p_param2)
else
tmp_var_name = tmp_var_name.. "_"..tostring(p.p_param2)
end
@ -360,7 +359,7 @@ local set_var_to_value = function(liste, var_name_full, op, val, vars_used)
end
end
yl_speak_up.export_to_ink.translate_effect_list = function(dialog, effects, vars_used, n_id)
yl_speak_up.export_to_ink.translate_effect_list = function(dialog, effects, vars_used)
-- collect effects that may work in ink
local liste = {}
-- variables may be set in effects
@ -388,21 +387,27 @@ yl_speak_up.export_to_ink_language = function(dialog, n_id)
and dialog.n_dialogs[start_dialog].d_name) then
start_dialog = dialog.n_dialogs[start_dialog].d_name
else
start_dialog = tostring(n_id).."_"..tostring(start_dialog)
start_dialog = tostring(start_dialog)
end
-- prefix all dialog names with this;
-- advantage: several NPC dialog exports can be combined into one inc game
-- where the player can talk to diffrent NPC (which can have the
-- same dialog names without conflict thanks to the prefix)
use_prefix = tostring(n_id).."_"
-- go to the main loop whenever the player ends the conversation with the NPC;
-- this allows to create an additional dialog in INK where the player can then
-- decide to talk to multiple NPC - or to continue his conversation with the
-- same NPC
local main_loop = tostring(n_id).."_d_end"
local main_loop = use_prefix.."d_end"
local tmp = {"-> ", main_loop,
"\n=== ", main_loop, " ===",
"\nWhat do you wish to do?",
"\n+ Talk to ", tostring(dialog.n_npc), " -> ", tostring(start_dialog),
"\n+ Talk to ", tostring(dialog.n_npc), " -> ", use_prefix..tostring(start_dialog),
"\n+ End -> END"}
local vars_used = ink_export.print_variables_used(tmp, dialog, n_id, pname)
local vars_used = ink_export.print_variables_used(tmp, dialog)
local sorted_d_list = yl_speak_up.get_dialog_list_for_export(dialog)
-- d_got_item may contain alternate texts - so it is of intrest here
@ -419,7 +424,7 @@ yl_speak_up.export_to_ink_language = function(dialog, n_id)
local dialog_names = {}
for i, d_id in ipairs(sorted_d_list) do
if(use_d_name) then
local n = tostring(n_id).."_"..tostring(d_id)
local n = tostring(d_id)
local d = dialog.n_dialogs[d_id]
dialog_names[n] = (d.d_name or n)
end
@ -430,7 +435,7 @@ yl_speak_up.export_to_ink_language = function(dialog, n_id)
local tmp2 = {}
local d = dialog.n_dialogs[d_id]
-- print the dialog knot, but without choices (those we add in the following loop)
local this_knot_name = ink_export.print_dialog_knot(tmp, n_id, d_id, d)
local this_knot_name = ink_export.print_dialog_knot(tmp, use_prefix, d_id, d)
-- iterate over all options
local sorted_o_list = yl_speak_up.get_sorted_options(dialog.n_dialogs[d_id].d_options or {}, "o_sort")
@ -447,7 +452,7 @@ yl_speak_up.export_to_ink_language = function(dialog, n_id)
for k, r_id in ipairs(sorted_e_list) do
local r = o_data.o_results[r_id]
if(r and r.r_type and r.r_type == "dialog") then
target_dialog = tostring(n_id).."_"..tostring(r.r_value)
target_dialog = tostring(r.r_value)
alternate_text_on_success = r.alternate_text or ""
end
end
@ -468,7 +473,7 @@ yl_speak_up.export_to_ink_language = function(dialog, n_id)
-- whatever dialog comes previously - the dialog, an action, or
-- another on_failure dialog - needs to lead to this dialog
target_dialog = ink_export.print_effect_knot(tmp2,
n_id, d_id, o_id, start_dialog,
use_prefix, d_id, o_id, start_dialog,
r, r_prev,
alternate_text_on_success, target_dialog,
dialog_names)
@ -489,7 +494,7 @@ yl_speak_up.export_to_ink_language = function(dialog, n_id)
local a = o_data.actions[a_id]
target_dialog = ink_export.print_action_knot(tmp2,
n_id, d_id, o_id, start_dialog,
use_prefix, d_id, o_id, start_dialog,
a,
alternate_text_on_success, target_dialog, dialog_names)
-- has been dealt with
@ -498,21 +503,21 @@ yl_speak_up.export_to_ink_language = function(dialog, n_id)
-- which preconditions can be translated to ink?
local p_list = ink_export.translate_precondition_list(dialog, o_data.o_prerequisites,
vars_used, n_id, dialog_names)
vars_used, use_prefix, dialog_names)
local e_list = ink_export.translate_effect_list(dialog, o_data.o_results,
vars_used, n_id)
vars_used)
-- what remains is to print the option/choice itself
ink_export.print_choice(tmp,
-- TODO: deal with when_prerequisites_not_met
o_data.o_text_when_prerequisites_met, n_id, start_dialog,
o_data.o_text_when_prerequisites_met, use_prefix, start_dialog,
alternate_text_on_success, target_dialog,
o_data.o_visit_only_once,
o_id, p_list, e_list, dialog_names)
end -- dealt with the option
-- add way to end talking to the NPC
ink_export.print_choice(tmp, "Farewell!", n_id, start_dialog,
nil, tostring(n_id).."_d_end", false, nil, dialog_names)
ink_export.print_choice(tmp, "Farewell!", use_prefix, start_dialog,
nil, "d_end", false, nil, dialog_names)
-- add the knots for actions and effects for this dialog and all its options:
for _, line in ipairs(tmp2) do