mirror of
				https://gitea.your-land.de/Sokomine/yl_speak_up.git
				synced 2025-11-04 06:13:08 +01:00 
			
		
		
		
	added limited export to ink language
This commit is contained in:
		
							parent
							
								
									3eab65d2ee
								
							
						
					
					
						commit
						5c7ffe3739
					
				
							
								
								
									
										145
									
								
								export_to_ink.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										145
									
								
								export_to_ink.lua
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,145 @@
 | 
			
		||||
-- 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
 | 
			
		||||
@ -1,5 +1,3 @@
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
yl_speak_up.input_export = function(player, formname, fields)
 | 
			
		||||
	if(fields and fields.back) then
 | 
			
		||||
		return yl_speak_up.show_fs(player, "talk")
 | 
			
		||||
@ -7,6 +5,8 @@ yl_speak_up.input_export = function(player, formname, fields)
 | 
			
		||||
		return yl_speak_up.show_fs(player, "export", "show_readable")
 | 
			
		||||
	elseif(fields and (fields.back_to_export or fields.show_export)) then
 | 
			
		||||
		return yl_speak_up.show_fs(player, "export", "show_export")
 | 
			
		||||
	elseif(fields and fields.show_ink_export) then
 | 
			
		||||
		return yl_speak_up.show_fs(player, "export", "show_ink_export")
 | 
			
		||||
	elseif(fields and fields.show_simple_dialogs) then
 | 
			
		||||
		return yl_speak_up.show_fs(player, "export", "show_simple_dialogs")
 | 
			
		||||
	elseif(fields and (fields.import or fields.back_from_error_msg)) then
 | 
			
		||||
@ -136,14 +136,17 @@ yl_speak_up.get_fs_export = function(player, param)
 | 
			
		||||
	end
 | 
			
		||||
	local content = ""
 | 
			
		||||
	local explanation = ""
 | 
			
		||||
	local b1 = "button[0.2,17.2;6.0,0.9;show_readable;Human readable format]"..
 | 
			
		||||
	local b1 = "button[0.2,17.2;4.0,0.9;show_readable;Human readable]"..
 | 
			
		||||
			"tooltip[show_readable;Shows the raw dialog data format formatted diffrently so\n"..
 | 
			
		||||
				"that it is easier to read.]"
 | 
			
		||||
	local b2 = "button[7.0,17.2;6.0,0.9;show_export;Export in .json format]"..
 | 
			
		||||
	local b2 = "button[5.0,17.2;4.0,0.9;show_export;Export in .json]"..
 | 
			
		||||
			"tooltip[show_export;Shows the raw dialog data format (.json).]"
 | 
			
		||||
	local b3 = "button[13.8,17.2;6.0,0.9;show_simple_dialogs;Show simple dialogs format]"..
 | 
			
		||||
	local b3 = "button[9.8,17.2;4.0,0.9;show_simple_dialogs;Simple dialogs]"..
 | 
			
		||||
			"tooltip[show_simple_dialogs;Show the dialog data structure in the format used by\n"..
 | 
			
		||||
				"the mod \"simple_dialogs\".]"
 | 
			
		||||
	local b4 = "button[14.6,17.2;4.0,0.9;show_ink_export;Ink language]"..
 | 
			
		||||
			"tooltip[show_ink_export;Export the dialog data structure to the\n"..
 | 
			
		||||
				"Ink markup language.]"
 | 
			
		||||
	-- include self.yl_speak_up (contains skin, animation, properties and the like)
 | 
			
		||||
	local obj = yl_speak_up.speak_to[pname].obj
 | 
			
		||||
	if(obj) then
 | 
			
		||||
@ -165,8 +168,14 @@ yl_speak_up.get_fs_export = function(player, param)
 | 
			
		||||
		explanation = "This is like the raw dialog data format - except that it's written a bit "..
 | 
			
		||||
			"diffrent so that it is easier to read."
 | 
			
		||||
		content = minetest.write_json(dialog, true)
 | 
			
		||||
	elseif(param and param == "show_ink_export") then
 | 
			
		||||
		b4 = "label[14.6,17.6;Ink lanugage]"
 | 
			
		||||
		-- TODO
 | 
			
		||||
		explanation = "This is the format used by the \"Ink\" scripting language. "..
 | 
			
		||||
			"TODO: The export is not complete yet."
 | 
			
		||||
		content = yl_speak_up.export_to_ink_language(dialog, n_id)
 | 
			
		||||
	elseif(param and param == "show_simple_dialogs") then
 | 
			
		||||
		b3 = "label[13.8,17.6;This is simple dialogs format]"
 | 
			
		||||
		b3 = "label[9.8,17.6;Simple dialogs format]"
 | 
			
		||||
		explanation = "This is the format used by the \"simple_dialogs\" mod. "..
 | 
			
		||||
			"It does not cover preconditions, actions, effects and other specialities of "..
 | 
			
		||||
			"this mod here. It only covers the raw dialogs. If a dialog line starts with "..
 | 
			
		||||
@ -209,7 +218,7 @@ yl_speak_up.get_fs_export = function(player, param)
 | 
			
		||||
		end
 | 
			
		||||
		content = table.concat(tmp, "")
 | 
			
		||||
	else
 | 
			
		||||
		b2 = "label[7.0,17.6;This is export in .json format]"
 | 
			
		||||
		b2 = "label[5.0,17.6;This is export in .json format]"
 | 
			
		||||
		explanation = "Mark the text in the above window with your mouse and paste it into "..
 | 
			
		||||
				"a local file on your disk. Save it as n_<id>.json (i.e. n_123.json) "..
 | 
			
		||||
				"in the folder containing your dialog data. Then the NPCs in your "..
 | 
			
		||||
@ -224,7 +233,7 @@ yl_speak_up.get_fs_export = function(player, param)
 | 
			
		||||
		"button[15.4,0.2;2.0,0.9;import;Import]",
 | 
			
		||||
		"tooltip[import;WARNING: This is highly experimental and requires the \"privs\" priv.\n"..
 | 
			
		||||
			"Use in singleplayer on a new NPC - but not on a live server!]",
 | 
			
		||||
		b1, b2, b3,
 | 
			
		||||
		b1, b2, b3, b4,
 | 
			
		||||
		-- background color for the textarea below
 | 
			
		||||
		"box[0.2,2;19.6,15;#AAAAAA]",
 | 
			
		||||
		-- read-only
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user