Divergent/mods/Western Goods/gamedata/scripts/modxml_zzz_rand_text_dialog...

131 lines
6.1 KiB
Plaintext

---==================================================================================================================---
--- ---
--- Original Author(s) : NLTP_ASHES ---
--- Edited : N/A ---
--- Date : 12/11/2023 ---
--- License : Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) ---
--- ---
--- Script used to add a new valid node to the dialog system. ---
--- ---
--- With this script, you can define a node like : ---
--- <rand_text>st_something_</rand_text> ---
--- ---
--- And then, you can define a couple of translation strings like : ---
--- - st_something_0 ---
--- - st_something_1 ---
--- - st_something_2 ---
--- ---
--- At runtime, thanks to DXML, this script make it so it uses one of the 3 translation strings at random. ---
--- ---
---==================================================================================================================---
-- ---------------------------------------------------------------------------------------------------------------------
-- Core Functions
-- ---------------------------------------------------------------------------------------------------------------------
function on_xml_read()
RegisterScriptCallback("on_xml_read", function(xml_file_name, xml_obj)
-- Only execute on gamedata\configs\gameplay\*.xml
if not string.find(xml_file_name, [[gameplay\]]) then
return
end
-- Query the file to get the node(s) to modify
local query_res = xml_obj:query("rand_text")
-- Return if no results were found
if is_empty(query_res) then
return
end
-- Replace rand_text with script_text
for _,elem in pairs(query_res) do
local value = xml_obj:getText(elem)
local phrase_elem = elem.parent
local new_value = [[<script_text>modxml_zzz_rand_text_dialog.]]..value..[[</script_text>]]
xml_obj:removeElement(elem)
xml_obj:insertFromXMLString(new_value, phrase_elem, #phrase_elem.kids)
this[value] = function()
local translations = this.collect_translations(value, true)
return translations and translations[math.random(#translations)] or "No translation"
end
end
end)
end
function trigger_dxml()
local file_list = str_explode(ini_sys:r_string("dialogs", "files"),",")
local xml = CScriptXmlInit()
for _,file_name in pairs(file_list) do
xml:ParseDirFile([[gameplay]],file_name..".xml")
end
xml = nil
end
-- ---------------------------------------------------------------------------------------------------------------------
-- Callbacks registration
-- ---------------------------------------------------------------------------------------------------------------------
--- Function used to register callbacks.
--- @return nil
function on_game_start()
RegisterScriptCallback("on_game_load", trigger_dxml)
end
-- ---------------------------------------------------------------------------------------------------------------------
-- Utils Functions
-- ---------------------------------------------------------------------------------------------------------------------
local cache_translated = {}
local cache_untranslated = {}
--- Function used to collect all translation string with the pattern of 'st'.
--- If is_translated is true, the table return will contained already translated strings.
--- @author unknown (from S.T.A.L.K.E.R. Anomaly's files)
--- @param st string
--- @param is_translated boolean
--- @return table
function collect_translations(st, is_translated)
-- read strings with specific pattern (ends with increased number) -> return them translated in a table
-- no need to scan for identical results
if is_translated and cache_translated[st] then
return cache_translated[st]
elseif cache_untranslated[st] then
return cache_untranslated[st]
end
local string_count = 1
local tr_t = {}
while true do
local tr_s = game.translate_string(st .. string_count)
if (tr_s == st .. string_count) then
break
else
if is_translated then
tr_t[#tr_t + 1] = tr_s
else
tr_t[#tr_t + 1] = st .. tostring(string_count)
end
end
string_count = string_count + 1
end
if (#tr_t == 0) then
if (not string.find(st,"st_msg")) then
printf("~[WG] WARNING | Random Text Tag | collect_translations - strings of (%s) don't exist!", st)
end
return false
end
if is_translated then
cache_translated[st] = tr_t
else
cache_untranslated[st] = tr_t
end
return tr_t
end