forked from AirFoxTwo/FS25_FieldsOfStories
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIANeighbourDynamicConversationData.lua
More file actions
373 lines (348 loc) · 13.9 KB
/
Copy pathIANeighbourDynamicConversationData.lua
File metadata and controls
373 lines (348 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
--
-- IANeighbourDynamicConversationData: open fieldwork list + per-character dynamic conversations.
--
-- Conversations live as normal conversation.xml trees under
-- conversations/dynamic/<characterId>/<topic>/[<variant>/]
-- (same branching/options as IAConversation).
--
-- The conversation engine substitutes {placeholder} tokens in entry text and resolves
-- <segment type="variable" name="..."/> audio clips using a `variableMap` that callers attach
-- to the mission table (e.g. IANeighbour:onContractCallTimeTriggered). Keys map 1:1 to the
-- placeholder names used in conversation.xml; values are either:
-- * symbolic catalog keys (canonical IAFieldwork.JobType values for jobType /
-- vehicleNameWithArticle; "needed" or nil for extraRefillNote)
-- * raw strings already formatted for display (fieldNumber, fieldNumbers, totalHectares,
-- situationId)
--
-- Symbolic placeholders pull their UI text (text / textDe) and voice suffix from
-- data/IANeighbourDynamicConversationVoices.xml. Raw-string placeholders substitute their
-- value as-is in subtitles and look up FieldNumber-range suffixes numerically when a voice
-- segment is requested.
--
-- Mission table shape used by the dynamic conversation loader:
-- {
-- config = situation config (id, fieldwork, ...),
-- farmlandIds = array of farmland ids (multi-field offer),
-- farmlandId = first farmland id (single-field offer),
-- contractOpenFieldworkList = full per-row open fieldwork list (used by callbacks),
-- variableMap = { jobType=, vehicleNameWithArticle=, extraRefillNote=,
-- fieldNumber=, fieldNumbers=, totalHectares=, situationId= }
-- }
--
IANeighbourDynamicConversationData = {}
IANeighbourDynamicConversationData._mt = Class(IANeighbourDynamicConversationData)
IANeighbourDynamicConversationData.VOICE_CATALOG_XML = "data/IANeighbourDynamicConversationVoices.xml"
--- Dynamic conversation folder: conversations/dynamic/{characterId}/{topic}/
IANeighbourDynamicConversationData.TOPIC_FIELD_MISSION_OFFER = "field_mission_offer"
--- Same offer with only one farmland: no "half work" branches (flat conversation.xml under this topic).
IANeighbourDynamicConversationData.TOPIC_FIELD_MISSION_OFFER_SINGLE = "field_mission_offer_single"
--- Variables that share the single numeric `<FieldNumber>` catalog table (text is raw value,
--- voice suffix is looked up by clamped numeric value).
IANeighbourDynamicConversationData.NUMERIC_VARIABLE_NAMES = {
fieldnumber = true,
field_number = true,
fieldnumbers = true,
field_numbers = true,
totalhectares = true,
total_hectares = true,
}
IANeighbourDynamicConversationData.NUMERIC_CATALOG_KEY = "fieldnumber"
--- Catalog element names parsed from <defaults>/<character> sections. Anything outside this
--- list is ignored. Element name is also the variable name in `voiceCatalog.variables`
--- (lowercased), so adding a new placeholder family is a 3-step change: enum / mission
--- builder / catalog XML.
IANeighbourDynamicConversationData.CATALOG_VARIABLE_ELEMENTS = {
"jobType",
"vehicleNameWithArticle",
"extraRefillNote",
"FieldNumber",
}
--- Parsed defaults + per-character overlays; filled once in loadVoiceCatalogXml.
IANeighbourDynamicConversationData._catalogDefaults = nil
IANeighbourDynamicConversationData._catalogCharacterOverlays = nil
IANeighbourDynamicConversationData._catalogMergedCache = {}
local function trim(s)
if s == nil then
return ""
end
return tostring(s):match("^%s*(.-)%s*$") or ""
end
local function shallowCopyMap(src)
local out = {}
if src ~= nil then
for k, v in pairs(src) do
out[k] = v
end
end
return out
end
--- Parse one <defaults> or <character> section of the voice catalog XML.
--- Catalog element shape (all attributes required for a usable entry):
--- <jobType key="cultivate" suffix="job_cultivate" text="..." textDe="..."/>
--- <vehicleNameWithArticle key="cultivate" suffix="ia_fieldwork_equip_cultivate" text="..." textDe="..."/>
--- <extraRefillNote key="needed" suffix="extra_refill_needed" text="..." textDe="..."/>
--- <FieldNumber key="1" suffix="fld_1" text="1" textDe="1"/>
--- @param xmlFile number GIANTS XML handle
--- @param sectionKey string e.g. "voiceCatalog.defaults" or "voiceCatalog.character(0)"
--- @return table { variables = { [varNameLower] = { [valueKey] = { suffix, text, textDe } } }, numericMin, numericMax }
local function parseVoiceCatalogSection(xmlFile, sectionKey)
local variables = {}
local numericMin, numericMax = nil, nil
for _, elementName in ipairs(IANeighbourDynamicConversationData.CATALOG_VARIABLE_ELEMENTS) do
local varKey = string.lower(elementName)
local isNumeric = (varKey == IANeighbourDynamicConversationData.NUMERIC_CATALOG_KEY)
local map = {}
local idx = 0
while true do
local entryKey = sectionKey .. "." .. elementName .. "(" .. idx .. ")"
local rawKey = getXMLString(xmlFile, entryKey .. "#key", nil)
if rawKey == nil or rawKey == "" then
break
end
local suffix = getXMLString(xmlFile, entryKey .. "#suffix", nil)
local text = getXMLString(xmlFile, entryKey .. "#text", nil)
local textDe = getXMLString(xmlFile, entryKey .. "#textDe", nil)
local storeKey = nil
if isNumeric then
local n = tonumber(trim(rawKey))
if n ~= nil then
storeKey = n
if numericMin == nil or n < numericMin then numericMin = n end
if numericMax == nil or n > numericMax then numericMax = n end
end
else
storeKey = string.lower(trim(rawKey))
end
if storeKey ~= nil then
map[storeKey] = {
suffix = (suffix ~= nil and suffix ~= "") and suffix or nil,
text = (text ~= nil and text ~= "") and text or nil,
textDe = (textDe ~= nil and textDe ~= "") and textDe or nil,
}
end
idx = idx + 1
end
if next(map) ~= nil then
variables[varKey] = map
end
end
return {
named = {},
variables = variables,
numericMin = numericMin,
numericMax = numericMax,
}
end
local function mergeVariableMaps(base, overlay)
if base == nil then return overlay end
if overlay == nil then return base end
local out = {}
for varKey, entries in pairs(base) do
out[varKey] = shallowCopyMap(entries)
end
for varKey, entries in pairs(overlay) do
local merged = out[varKey] or {}
for k, v in pairs(entries) do
merged[k] = v
end
out[varKey] = merged
end
return out
end
local function mergeVoiceCatalogs(base, overlay)
if base == nil then
return overlay
end
if overlay == nil then
return base
end
local namedOut = shallowCopyMap(base.named)
if overlay.named ~= nil then
for k, v in pairs(overlay.named) do
namedOut[k] = v
end
end
local variablesOut = mergeVariableMaps(base.variables, overlay.variables)
-- Re-derive numericMin / numericMax from merged FieldNumber keys so per-character
-- overlays that only add entries above the default range extend it correctly.
local numericMin, numericMax = base.numericMin, base.numericMax
local nums = variablesOut[IANeighbourDynamicConversationData.NUMERIC_CATALOG_KEY]
if nums ~= nil then
numericMin, numericMax = nil, nil
for k, _ in pairs(nums) do
if type(k) == "number" then
if numericMin == nil or k < numericMin then numericMin = k end
if numericMax == nil or k > numericMax then numericMax = k end
end
end
end
return {
named = namedOut,
variables = variablesOut,
numericMin = numericMin,
numericMax = numericMax,
}
end
--- @param IANeighbour neighbour
function IANeighbourDynamicConversationData.new(neighbour)
local self = setmetatable({}, IANeighbourDynamicConversationData._mt)
self.neighbour = neighbour
self.cachedOpenMissions = {}
self.missionCacheAccumMs = 0
self.refreshMissionIntervalMs = 60000
self.openMissionsDirty = true
self.initialized = false
return self
end
function IANeighbourDynamicConversationData.loadVoiceCatalogXml()
if IANeighbourDynamicConversationData._catalogDefaults ~= nil then
return
end
IANeighbourDynamicConversationData._catalogDefaults = {
named = {},
variables = {},
numericMin = nil,
numericMax = nil,
}
IANeighbourDynamicConversationData._catalogCharacterOverlays = {}
if IANeighbours == nil or IANeighbours.dir == nil then
return
end
local path = Utils.getFilename(IANeighbourDynamicConversationData.VOICE_CATALOG_XML, IANeighbours.dir)
if not fileExists(path) then
if IANeighbours.debug then
print("--- IANeighbourDynamicConversationData.loadVoiceCatalogXml() missing " .. tostring(path))
end
return
end
local xmlFile = loadXMLFile("IANeighbourVoiceCatalog", path)
if xmlFile == nil then
return
end
IANeighbourDynamicConversationData._catalogDefaults = parseVoiceCatalogSection(xmlFile, "voiceCatalog.defaults")
local cIdx = 0
while true do
local cKey = "voiceCatalog.character(" .. cIdx .. ")"
local cid = getXMLString(xmlFile, cKey .. "#id", nil)
if cid == nil or cid == "" then
break
end
IANeighbourDynamicConversationData._catalogCharacterOverlays[tostring(cid)] = parseVoiceCatalogSection(xmlFile, cKey)
cIdx = cIdx + 1
end
delete(xmlFile)
if IANeighbours.debug then
print("--- IANeighbourDynamicConversationData.loadVoiceCatalogXml() OK defaults + " .. tostring(cIdx) .. " character overlays")
end
end
--- Merged voice catalog for this neighbour (character id = neighbour.id from scenario).
-- @param IANeighbour neighbour
--- @return table
function IANeighbourDynamicConversationData.getVoiceCatalogForNeighbour(neighbour)
IANeighbourDynamicConversationData.loadVoiceCatalogXml()
local cid = neighbour ~= nil and neighbour.id ~= nil and tostring(neighbour.id) or "0"
if IANeighbourDynamicConversationData._catalogMergedCache[cid] ~= nil then
return IANeighbourDynamicConversationData._catalogMergedCache[cid]
end
local base = IANeighbourDynamicConversationData._catalogDefaults
local over = IANeighbourDynamicConversationData._catalogCharacterOverlays[cid]
local merged = mergeVoiceCatalogs(base, over)
IANeighbourDynamicConversationData._catalogMergedCache[cid] = merged
return merged
end
function IANeighbourDynamicConversationData:initialize()
if self.initialized then
return
end
IANeighbourDynamicConversationData.loadVoiceCatalogXml()
self:refreshOpenMissionsCache()
self.openMissionsDirty = false
self.missionCacheAccumMs = 0
self.initialized = true
end
function IANeighbourDynamicConversationData:update(dt)
if not self.initialized then
return
end
local ms = (dt or 0)
self.missionCacheAccumMs = self.missionCacheAccumMs + ms
if self.missionCacheAccumMs >= self.refreshMissionIntervalMs then
self.missionCacheAccumMs = 0
self.openMissionsDirty = true
end
if self.openMissionsDirty then
self:refreshOpenMissionsCache()
self.openMissionsDirty = false
end
end
function IANeighbourDynamicConversationData:refreshOpenMissionsCache()
local n = self.neighbour
if n == nil or IANeighbours == nil or IANeighbours.gameLoopHelper == nil then
self.cachedOpenMissions = {}
return
end
self.cachedOpenMissions = IANeighbours.gameLoopHelper:getAllOpenFieldwork(n)
end
--- @return table array of { farmlandId, config, nextCropFruitTypeIndex? }
function IANeighbourDynamicConversationData:getOpenFieldMissions()
if not self.initialized then
self:initialize()
end
if self.openMissionsDirty then
self:refreshOpenMissionsCache()
self.openMissionsDirty = false
end
return self.cachedOpenMissions
end
--- Mod-relative base folder for this neighbour and topic (variants are numeric subfolders).
-- @param string topicKey e.g. field_mission_offer
--- @return string e.g. conversations/dynamic/17/field_mission_offer
function IANeighbourDynamicConversationData:getDynamicConversationBaseDir(topicKey)
if self.neighbour == nil or self.neighbour.id == nil or topicKey == nil or topicKey == "" then
return nil
end
return "conversations/dynamic/" .. tostring(self.neighbour.id) .. "/" .. tostring(topicKey)
end
--- Load dynamic conversation.xml for one mission into an IAConversation (variant picked randomly when pickVariant true).
-- The caller is responsible for filling `mission.variableMap` (see file header).
-- @param IAConversation conversation
-- @param string topicKey folder under conversations/dynamic/<neighbourId>/
-- @param table mission (see file header)
-- @param table|nil callbacksByAction e.g. accept, decline, accept_with_equipment, accept_half, accept_half_with_equipment, after_*
-- @param boolean|nil pickVariant default true
-- @param function|nil entryAvailabilityFilter optional function(entry) -> boolean
--- @return boolean
function IANeighbourDynamicConversationData:loadDynamicConversationForMission(conversation, topicKey, mission, callbacksByAction, pickVariant, entryAvailabilityFilter)
if conversation == nil or self.neighbour == nil or mission == nil then
return false
end
if not self.initialized then
self:initialize()
end
local baseDir = self:getDynamicConversationBaseDir(topicKey)
if baseDir == nil then
return false
end
local catalog = IANeighbourDynamicConversationData.getVoiceCatalogForNeighbour(self.neighbour)
if pickVariant == nil then
pickVariant = true
end
conversation.entryAvailabilityFilter = entryAvailabilityFilter
return conversation:loadDynamicConversationFromDirectory(baseDir, pickVariant, self.neighbour, mission.variableMap or {}, catalog, callbacksByAction or {})
end
--- Create a new IAConversation instance and load the dynamic tree for one mission (caller owns lifecycle; call stop() when done).
-- @param string topicKey e.g. IANeighbourDynamicConversationData.TOPIC_FIELD_MISSION_OFFER
-- @param table mission (see file header)
-- @param table|nil callbacksByAction
-- @param function|nil entryAvailabilityFilter optional function(entry) -> boolean
-- @return IAConversation|nil
function IANeighbourDynamicConversationData:createLoadedConversationForMission(topicKey, mission, callbacksByAction, entryAvailabilityFilter)
if IAConversation == nil then
return nil
end
local conv = IAConversation.new()
if not self:loadDynamicConversationForMission(conv, topicKey, mission, callbacksByAction, true, entryAvailabilityFilter) then
return nil
end
return conv
end