yl_speak_up/export_to_ink.lua
2024-02-23 03:23:46 +01:00

146 lines
5.4 KiB
Lua

-- helper functions for export_to_ink_language:
-- d: dialog
yl_speak_up.export_to_ink_dialog_knot = function(lines, d, n_d_id)
table.insert(lines, "\n\n=== ")
table.insert(lines, n_d_id)
table.insert(lines, " ===")
-- many characters at the start of a line have a special meaning;
-- hopefully they will not be obstrusive later on;
-- TODO: in order to be on the safe side: add a ":" in front of each line?
local t = d.d_text or ""
-- t = string.gsub(t, "\n([:>=])", "\n %1")
table.insert(lines, "\n")
table.insert(lines, t)
table.insert(lines, "\n")
end
-- a: action
yl_speak_up.export_to_ink_action_knot = function(lines, a, n_d_id, o_id, next_target)
table.insert(lines, "\n\n=== ")
table.insert(lines, n_d_id.."_"..tostring(o_id).."_"..tostring(a.a_id))
table.insert(lines, " ===")
table.insert(lines, "\n:action:")
table.insert(lines, yl_speak_up.show_action(a))
table.insert(lines, "\n+ [Action was successful] -> ")
table.insert(lines, next_target)
table.insert(lines, "\n+ [Action failed] -> ")
table.insert(lines, tostring(a.a_on_failure or next_target or "ERROR"))
table.insert(lines, "\n+ [Back] -> ")
table.insert(lines, n_d_id)
end
-- r: effect/result
-- r_prev: previous effect
yl_speak_up.export_to_ink_effect_knot = function(lines, r, n_d_id, o_id, next_target, r_prev)
table.insert(lines, "\n\n=== ")
table.insert(lines, n_d_id.."_"..tostring(o_id).."_"..tostring(r.r_id))
table.insert(lines, " ===")
table.insert(lines, "\n:effect:")
-- 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))
table.insert(lines, "\n+ [Effect was successful] -> ")
table.insert(lines, next_target)
table.insert(lines, "\n+ [Effect failed] -> ")
table.insert(lines, tostring(r.r_value or "ERROR"))
end
yl_speak_up.export_to_ink_language = function(dialog, n_id)
local main = tostring(n_id).."_main"
local tmp = {"-> ", main,
"\n=== ", main, " ===\n",
"\nWhat do you wish to do?",
"\n+ Talk to ", tostring(dialog.n_npc), " -> ", tostring(n_id).."_d_1",
"\n+ End -> END"}
local sorted_d_list = yl_speak_up.sort_keys(dialog.n_dialogs or {}, true)
for i, d_id in ipairs(sorted_d_list) do
-- store the knots for actions and effects here:
local tmp2 = {}
local d = dialog.n_dialogs[d_id]
local n_d_id = tostring(n_id).."_"..tostring(d_id)
yl_speak_up.export_to_ink_dialog_knot(tmp, d, n_d_id)
local sorted_o_list = yl_speak_up.get_sorted_options(dialog.n_dialogs[d_id].d_options or {}, "o_sort")
for j, o_id in ipairs(sorted_o_list) do
local o_data = d.d_options[o_id]
local target_action = nil
local target_effect = nil
local target_dialog = nil
local sorted_a_list = yl_speak_up.sort_keys(o_data.actions or {})
local sorted_e_list = yl_speak_up.sort_keys(o_data.o_results or {})
-- is there an action? That takes precedence.
if(#sorted_a_list > 0) then
-- this action dialog knot needs to be created:
target_action = n_d_id.."_"..tostring(o_id).."_"..tostring(sorted_a_list[1])
end
-- is there an effect/result of type on_failure?
local effect_list = {}
for k, r_id in ipairs(sorted_e_list) do
local r = o_data.o_results[r_id]
-- checking for previous effect failed?
if(not(target_effect)
and k > 1 and r and r.r_type and r.r_type == "on_failure") then
-- this effect dialog knot needs to be created:
target_effect = n_d_id.."_"..tostring(o_id).."_"..tostring(r_id)
elseif(target_effect
and k > 1 and r and r.r_type and r.r_type == "on_failure") then
-- collect all effects that need their own knots
table.insert(effect_list, target_effect)
-- normal target dialog?
elseif(not(target_dialog)
and r and r.r_type and r.r_type == "dialog") then
target_dialog = tostring(n_id).."_"..tostring(r.r_value)
end
end
table.insert(effect_list, target_dialog)
local target = (target_action or target_effect or target_dialog or "d_1")
table.insert(tmp, "+ [")
table.insert(tmp, o_data.o_text_when_prerequisites_met)
table.insert(tmp, "] -> ")
table.insert(tmp, target)
table.insert(tmp, "\n")
-- add the actions
local next_target = "ERROR"
for k, a_id in ipairs(sorted_a_list) do
local a = o_data.actions[a_id]
if(k < #sorted_a_list) then
next_target = n_d_id.."_"..tostring(o_id).."_"..tostring(sorted_a_list[k+1])
elseif(target_effect) then
next_target = target_effect
elseif(target_dialog) then
next_target = target_dialog
else
next_target = "d_1"
end
yl_speak_up.export_to_ink_action_knot(tmp2, a, n_d_id, o_id, next_target)
end
-- add the effects
for k, r_id in ipairs(sorted_e_list) do
local r = o_data.o_results[r_id]
-- checking for previous effect failed?
if(not(target_effect)
and k > 1 and r and r.r_type and r.r_type == "on_failure") then
-- this effect dialog knot needs to be created:
if(effect_list > 0) then
next_target = effect_list[1]
table.remove(effect_list, 1)
else
next_target = "ERROR"
end
yl_speak_up.export_to_ink_effect_knot(tmp2, r, n_d_id, o_id, next_target, o_data.o_results[sorted_e_list[k-1]])
end
end
end
table.insert(tmp, "\n")
-- add way to end talking to the NPC
table.insert(tmp, "\n+ Farewell! -> "..tostring(n_id).."_main")
-- add the knots for actions and effects for this dialog and all its options:
for _, line in ipairs(tmp2) do
table.insert(tmp, line)
end
end
return table.concat(tmp, "")
end