forked from Realistic-Farming/FS25_NPCFavor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
627 lines (544 loc) · 24.1 KB
/
Copy pathmain.lua
File metadata and controls
627 lines (544 loc) · 24.1 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
-- =========================================================
-- TODO / FUTURE VISION
-- =========================================================
-- LIFECYCLE:
-- [x] Mission load/unload hooks (Mission00.load, FSBaseMission.delete)
-- [x] Late-join initialization for mid-session mod loading
-- [x] Mission validity checks with performance caching
-- [x] Global system reference (g_NPCSystem) for cross-module access
-- [ ] Graceful degradation when dependencies fail to load
-- [ ] Hot-reload support for development without restarting mission
--
-- INPUT SYSTEM:
-- [x] E key interaction via RVB pattern (PlayerInputComponent hook)
-- [x] Dynamic prompt text showing nearest NPC name
-- [x] Dialog visibility suppression when another dialog is open
-- [ ] Configurable keybind (allow rebinding from E to another key)
-- [ ] Gamepad/controller support for NPC interaction
-- [ ] Multi-NPC selection wheel when several NPCs are nearby
--
-- SAVE/LOAD:
-- [x] XML persistence via FSCareerMissionInfo.saveToXMLFile hook
-- [x] Load from savegame on mission start
-- [x] Multiple missionInfo discovery fallbacks
-- [ ] Save file versioning and migration for future data format changes
-- [ ] Backup/restore of NPC save data on corruption
--
-- MULTIPLAYER:
-- [x] NPCStateSyncEvent for full state sync to joining players
-- [x] NPCSettingsSyncEvent for settings broadcast on join
-- [ ] Per-player interaction cooldowns to prevent spam
-- [ ] Conflict resolution when two players interact with same NPC
-- =========================================================
-- =========================================================
-- FS25 NPC Favor Mod (version 1.2.2.5)
-- =========================================================
-- Living NPC Neighborhood System
-- =========================================================
-- Author: TisonK & Lion2009
-- =========================================================
-- COPYRIGHT NOTICE:
-- All rights reserved. Unauthorized redistribution, copying,
-- or claiming this code as your own is strictly prohibited.
-- Original idea: Lion2009
-- Implementation: TisonK
-- =========================================================
-- Add version tracking
local MOD_VERSION = "1.2.2.5"
local MOD_NAME = "FS25_NPCFavor"
local modDirectory = g_currentModDirectory
local modName = g_currentModName
print("[NPC Favor] Starting mod initialization...")
-- Define base classes and utilities
if modDirectory then
print("[NPC Favor] Loading utility files...")
source(modDirectory .. "src/utils/VectorHelper.lua")
source(modDirectory .. "src/utils/TimeHelper.lua")
-- Configuration & settings
source(modDirectory .. "src/settings/NPCConfig.lua")
source(modDirectory .. "src/settings/NPCSettings.lua")
source(modDirectory .. "src/settings/NPCSettingsIntegration.lua")
-- Multiplayer events (must load before NPCSystem which references them)
source(modDirectory .. "src/events/NPCStateSyncEvent.lua")
source(modDirectory .. "src/events/NPCInteractionEvent.lua")
source(modDirectory .. "src/events/NPCSettingsSyncEvent.lua")
-- Core systems in dependency order
print("[NPC Favor] Loading core systems...")
source(modDirectory .. "src/scripts/NPCRelationshipManager.lua")
source(modDirectory .. "src/scripts/NPCFavorSystem.lua")
source(modDirectory .. "src/scripts/NPCEntity.lua")
source(modDirectory .. "src/scripts/NPCAI.lua")
source(modDirectory .. "src/scripts/NPCFieldWork.lua")
source(modDirectory .. "src/scripts/NPCScheduler.lua")
source(modDirectory .. "src/scripts/NPCInteractionUI.lua")
source(modDirectory .. "src/scripts/NPCTeleport.lua")
-- GUI
source(modDirectory .. "src/gui/DialogLoader.lua")
source(modDirectory .. "src/gui/NPCDialog.lua")
source(modDirectory .. "src/gui/NPCListDialog.lua")
source(modDirectory .. "src/gui/NPCFavorManagementDialog.lua")
source(modDirectory .. "src/settings/NPCFavorGUI.lua")
-- Main coordinator
source(modDirectory .. "src/NPCSystem.lua")
print("[NPC Favor] All files loaded successfully")
else
print("[NPC Favor] ERROR - Could not find mod directory!")
return
end
local npcSystem = nil
-- Performance optimization: cache common checks
local function isMissionValid(mission)
return mission and not mission.cancelLoading
end
local function isEnabled()
return npcSystem ~= nil and npcSystem.settings and npcSystem.settings.enabled
end
local function loadedMission(mission, node)
print("[NPC Favor] Mission load finished callback")
if not isMissionValid(mission) then
print("[NPC Favor] Mission not valid, skipping initialization")
return
end
if npcSystem then
-- Register all dialogs via DialogLoader
if DialogLoader and g_gui then
DialogLoader.init(modDirectory)
DialogLoader.register("NPCDialog", NPCDialog, "gui/NPCDialog.xml")
DialogLoader.register("NPCListDialog", NPCListDialog, "gui/NPCListDialog.xml")
DialogLoader.register("NPCFavorManagementDialog", NPCFavorManagementDialog, "gui/NPCFavorManagementDialog.xml") -- NEW
-- Eagerly load ALL dialogs while the mod's ZIP filesystem context
-- is active. Lazy loading later fails with "Failed to open xml file"
-- because FS25 can only resolve mod-internal paths during mission load.
DialogLoader.ensureLoaded("NPCDialog")
DialogLoader.ensureLoaded("NPCListDialog")
DialogLoader.ensureLoaded("NPCFavorManagementDialog")
npcSystem.npcDialogInstance = DialogLoader.getDialog("NPCDialog")
end
-- Initialize NPC entity model loading
if npcSystem.entityManager and npcSystem.entityManager.initialize then
npcSystem.entityManager:initialize(modDirectory)
end
print("[NPC Favor] Calling onMissionLoaded...")
npcSystem:onMissionLoaded()
-- Hook IngameMap.drawFields to render NPC name labels on the map
if g_currentMission.hud and g_currentMission.hud.ingameMap then
g_currentMission.hud.ingameMap.drawFields = Utils.appendedFunction(
g_currentMission.hud.ingameMap.drawFields,
function(map)
if npcSystem and npcSystem.entityManager then
npcSystem.entityManager:drawMapLabels(map)
end
end
)
end
else
print("[NPC Favor] ERROR - npcSystem is nil in loadedMission!")
-- Late initialization fallback
print("[NPC Favor] Attempting late initialization...")
npcSystem = NPCSystem.new(mission, modDirectory, modName)
if npcSystem then
getfenv(0)["g_NPCSystem"] = npcSystem
g_NPCFavorMod = {
version = MOD_VERSION,
name = MOD_NAME,
system = npcSystem
}
print("[NPC Favor] Late initialization successful")
npcSystem:onMissionLoaded()
-- Hook IngameMap.drawFields for NPC name labels (late-init path)
if g_currentMission.hud and g_currentMission.hud.ingameMap then
g_currentMission.hud.ingameMap.drawFields = Utils.appendedFunction(
g_currentMission.hud.ingameMap.drawFields,
function(map)
if npcSystem and npcSystem.entityManager then
npcSystem.entityManager:drawMapLabels(map)
end
end
)
end
else
print("[NPC Favor] ERROR - Failed to create NPCSystem")
end
end
end
local function load(mission)
print("[NPC Favor] Load function called")
if not isMissionValid(mission) then
print("[NPC Favor] Mission not valid, skipping load")
return
end
if npcSystem == nil then
print("[NPC Favor] Initializing version " .. MOD_VERSION .. "...")
print("[NPC Favor] Creating NPCSystem instance...")
npcSystem = NPCSystem.new(mission, modDirectory, modName)
if npcSystem then
getfenv(0)["g_NPCSystem"] = npcSystem
g_NPCFavorMod = {
version = MOD_VERSION,
name = MOD_NAME,
system = npcSystem
}
print("[NPC Favor] NPCSystem instance created successfully")
-- Initialize console commands
if npcSystem.gui then
npcSystem.gui:registerConsoleCommands()
end
else
print("[NPC Favor] ERROR - Failed to create NPCSystem instance")
end
else
print("[NPC Favor] Already initialized")
end
end
local function unload()
print("[NPC Favor] Unload function called")
-- Clean up dialogs
if DialogLoader then
DialogLoader.cleanup()
end
if npcSystem ~= nil then
npcSystem:delete()
npcSystem = nil
getfenv(0)["g_NPCSystem"] = nil
g_NPCFavorMod = nil
print("[NPC Favor] Unloaded successfully")
end
end
-- FS25 Game Hooks
print("[NPC Favor] Setting up game hooks...")
if Mission00 and Mission00.load then
print("[NPC Favor] Hooking Mission00.load")
Mission00.load = Utils.prependedFunction(Mission00.load, load)
elseif g_currentMission and g_currentMission.load then
print("[NPC Favor] Hooking g_currentMission.load")
g_currentMission.load = Utils.prependedFunction(g_currentMission.load, load)
else
print("[NPC Favor] WARNING - No load function found to hook!")
end
if Mission00 and Mission00.loadMission00Finished then
print("[NPC Favor] Hooking Mission00.loadMission00Finished")
Mission00.loadMission00Finished = Utils.appendedFunction(Mission00.loadMission00Finished, loadedMission)
else
print("[NPC Favor] WARNING - Mission00.loadMission00Finished not found")
if g_currentMission and g_currentMission.onMissionLoaded then
print("[NPC Favor] Hooking g_currentMission.onMissionLoaded")
g_currentMission.onMissionLoaded = Utils.appendedFunction(g_currentMission.onMissionLoaded, function(mission)
loadedMission(mission, nil)
end)
end
end
if FSBaseMission and FSBaseMission.delete then
print("[NPC Favor] Hooking FSBaseMission.delete")
FSBaseMission.delete = Utils.appendedFunction(FSBaseMission.delete, unload)
end
if FSBaseMission and FSBaseMission.update then
print("[NPC Favor] Hooking FSBaseMission.update")
FSBaseMission.update = Utils.appendedFunction(FSBaseMission.update, function(mission, dt)
if npcSystem then
npcSystem:update(dt)
end
end)
end
-- Hook draw for HUD rendering (renderOverlay/renderText are ONLY allowed in draw callbacks)
if FSBaseMission and FSBaseMission.draw then
print("[NPC Favor] Hooking FSBaseMission.draw")
FSBaseMission.draw = Utils.appendedFunction(FSBaseMission.draw, function(mission)
if npcSystem then
npcSystem:draw()
end
end)
end
-- =========================================================
-- Block player from entering NPC vehicles
-- =========================================================
-- Real vehicle spawning is currently disabled — FS25 provides no reliable
-- API to prevent player entry into spawned vehicles. The lockNPCVehicle()
-- code and hooks are preserved for future use when a solution is found.
-- =========================================================
-- E Key Input Binding (RVB Pattern from UsedPlus)
-- =========================================================
-- Hook PlayerInputComponent.registerActionEvents to add NPC_INTERACT
-- Game renders [E] automatically, we provide dynamic text
local npcInteractActionEventId = nil
local npcInteractOriginalFunc = nil
local favorMenuActionEventId = nil -- NEW for F6
local npcListActionEventId = nil -- NEW for F7
local function npcInteractActionCallback(self, actionName, inputValue, callbackState, isAnalog)
if inputValue <= 0 then
return
end
if not npcSystem then
return
end
-- Don't open while another dialog is showing
if g_gui:getIsDialogVisible() then
return
end
-- Find nearest interactable NPC and open the dialog
if npcSystem.nearbyNPCs then
local nearest = nil
local nearestDist = 999
for _, npc in ipairs(npcSystem.nearbyNPCs) do
if npc.canInteract and npc.interactionDistance < nearestDist then
nearest = npc
nearestDist = npc.interactionDistance
end
end
if nearest then
-- Freeze NPC while player is talking to them
nearest.isTalking = true
-- Show dialog via DialogLoader (handles lazy loading + data setting)
if DialogLoader and DialogLoader.show then
local dialog = DialogLoader.getDialog("NPCDialog")
if dialog then
dialog:setNPCData(nearest, npcSystem)
end
local shown = DialogLoader.show("NPCDialog")
if not shown then
nearest.isTalking = false
print("[NPC Favor] DialogLoader failed to show NPCDialog")
end
else
-- Fallback to direct g_gui
if npcSystem.npcDialogInstance then
npcSystem.npcDialogInstance:setNPCData(nearest, npcSystem)
end
local ok, err = pcall(function()
g_gui:showDialog("NPCDialog")
end)
if not ok then
nearest.isTalking = false
print("[NPC Favor] showDialog FAILED: " .. tostring(err))
end
end
end
end
end
-- F6: Open Favor Management
local function favorMenuActionCallback(actionName, inputValue, callbackState, isAnalog)
if npcSystem and npcSystem.isInitialized then
if DialogLoader and DialogLoader.show then
DialogLoader.show("NPCFavorManagementDialog", "setNPCSystem", npcSystem)
else
print("[NPC Favor] Favor management dialog not available")
end
end
end
-- F7: Open NPC List
local function npcListActionCallback(actionName, inputValue, callbackState, isAnalog)
if npcSystem and npcSystem.isInitialized then
if DialogLoader and DialogLoader.show then
DialogLoader.show("NPCListDialog", "setNPCSystem", npcSystem)
else
print("[NPC Favor] NPC list dialog not available")
end
end
end
local function hookNPCInteractInput()
if npcInteractOriginalFunc ~= nil then
return -- Already hooked
end
if PlayerInputComponent == nil or PlayerInputComponent.registerActionEvents == nil then
print("[NPC Favor] PlayerInputComponent.registerActionEvents not available")
return
end
npcInteractOriginalFunc = PlayerInputComponent.registerActionEvents
PlayerInputComponent.registerActionEvents = function(inputComponent, ...)
npcInteractOriginalFunc(inputComponent, ...)
if inputComponent.player ~= nil and inputComponent.player.isOwner then
local actionId = InputAction.NPC_INTERACT
if actionId == nil then
print("[NPC Favor] InputAction.NPC_INTERACT not found")
return
end
g_inputBinding:beginActionEventsModification(PlayerInputComponent.INPUT_CONTEXT_NAME)
local success, eventId = g_inputBinding:registerActionEvent(
actionId,
NPCSystem, -- Target object (static reference)
npcInteractActionCallback, -- Callback function
false, -- triggerUp
true, -- triggerDown
false, -- triggerAlways
false, -- startActive (MUST be false)
nil, -- callbackState
true -- disableConflictingBindings
)
g_inputBinding:endActionEventsModification()
if success and eventId ~= nil then
npcInteractActionEventId = eventId
end
end
-- Register F6: Favor Menu
local favorMenuActionId = InputAction.FAVOR_MENU
if favorMenuActionId ~= nil then
local success, eventId = g_inputBinding:registerActionEvent(
favorMenuActionId,
NPCSystem,
favorMenuActionCallback,
false, true, false, false, nil, true
)
if success and eventId ~= nil then
favorMenuActionEventId = eventId
g_inputBinding:setActionEventTextPriority(eventId, GS_PRIO_NORMAL)
g_inputBinding:setActionEventText(eventId, g_i18n:getText("input_FAVOR_MENU") or "Favor Menu")
end
end
-- Register F7: NPC List
local npcListActionId = InputAction.NPC_LIST
if npcListActionId ~= nil then
local success, eventId = g_inputBinding:registerActionEvent(
npcListActionId,
NPCSystem,
npcListActionCallback,
false, true, false, false, nil, true
)
if success and eventId ~= nil then
npcListActionEventId = eventId
g_inputBinding:setActionEventTextPriority(eventId, GS_PRIO_NORMAL)
g_inputBinding:setActionEventText(eventId, g_i18n:getText("input_NPC_LIST") or "NPC List")
end
end
end
end
hookNPCInteractInput()
-- Update hook: control E key prompt visibility based on NPC proximity
if FSBaseMission and FSBaseMission.update then
FSBaseMission.update = Utils.appendedFunction(FSBaseMission.update, function(mission, dt)
if g_inputBinding == nil or not npcSystem then
return
end
-- E key: show "Talk to NPC" when near (hide when dialog is open)
if npcInteractActionEventId ~= nil then
local shouldShow = false
local promptText = g_i18n:getText("input_NPC_INTERACT") or "Talk to NPC"
local isDialogOpen = g_gui:getIsDialogVisible()
if not isDialogOpen and npcSystem.nearbyNPCs then
local nearest = nil
local nearestDist = 999
for _, npc in ipairs(npcSystem.nearbyNPCs) do
if npc.canInteract and npc.interactionDistance < nearestDist then
nearest = npc
nearestDist = npc.interactionDistance
end
end
if nearest then
shouldShow = true
promptText = string.format(g_i18n:getText("npc_interact_talk_to") or "Talk to %s", nearest.name or "NPC")
end
end
g_inputBinding:setActionEventTextPriority(npcInteractActionEventId, GS_PRIO_VERY_HIGH)
g_inputBinding:setActionEventTextVisibility(npcInteractActionEventId, shouldShow)
g_inputBinding:setActionEventActive(npcInteractActionEventId, shouldShow)
if shouldShow then
g_inputBinding:setActionEventText(npcInteractActionEventId, promptText)
end
end
end)
end
-- Multiplayer: send full NPC state + settings to newly joining players
if FSBaseMission and FSBaseMission.sendInitialClientState then
FSBaseMission.sendInitialClientState = Utils.appendedFunction(
FSBaseMission.sendInitialClientState,
function(mission, connection, isReconnect)
if npcSystem and npcSystem.isInitialized then
if NPCStateSyncEvent then
NPCStateSyncEvent.sendToConnection(connection)
end
if NPCSettingsSyncEvent then
NPCSettingsSyncEvent.sendAllToConnection(connection)
end
end
end
)
end
-- =========================================================
-- Save/Load Persistence (following UsedPlus pattern)
-- =========================================================
-- Save: hook FSCareerMissionInfo.saveToXMLFile
-- Load: called from NPCSystem:onMissionLoaded() after NPC init
-- Discover missionInfo for savegame directory access
local function discoverMissionInfo()
-- Method 1: g_currentMission.missionInfo
if g_currentMission and g_currentMission.missionInfo then
return g_currentMission.missionInfo
end
-- Method 2: g_careerScreen.currentSavegame
if g_careerScreen and g_careerScreen.currentSavegame then
local savegame = g_careerScreen.currentSavegame
if savegame and savegame.savegameDirectory then
return { savegameDirectory = savegame.savegameDirectory }
end
end
-- Method 3: g_currentMission.savegameDirectory
if g_currentMission and g_currentMission.savegameDirectory then
return { savegameDirectory = g_currentMission.savegameDirectory }
end
return nil
end
-- Hook save — FS25 calls this when the player saves their game
if FSCareerMissionInfo and FSCareerMissionInfo.saveToXMLFile then
FSCareerMissionInfo.saveToXMLFile = Utils.appendedFunction(
FSCareerMissionInfo.saveToXMLFile,
function(missionInfo)
if npcSystem and npcSystem.isInitialized then
npcSystem:saveToXMLFile(missionInfo)
-- Settings persist to missionInfo.savegameDirectory (UsedPlus pattern)
if npcSystem.settings and npcSystem.settings.saveToXMLFile then
pcall(function() npcSystem.settings:saveToXMLFile(missionInfo) end)
end
end
end
)
end
-- Hook mission start — load saved NPC data after initialization
if Mission00 and Mission00.onStartMission then
Mission00.onStartMission = Utils.appendedFunction(
Mission00.onStartMission,
function(mission)
if npcSystem and npcSystem.isInitialized then
local missionInfo = discoverMissionInfo()
if missionInfo then
npcSystem:loadFromXMLFile(missionInfo)
end
end
end
)
end
-- Multiplayer compatibility check
if g_currentMission and g_currentMission.missionInfo then
if g_currentMission.missionInfo.isMultiplayer then
print("[NPC Favor] Multiplayer mode detected")
end
end
print("========================================")
print(" FS25 NPC Favor v" .. MOD_VERSION .. " LOADED ")
print(" Living Neighborhood System ")
print(" Type 'npcHelp' in console ")
print(" for available commands ")
print("========================================")
-- Late-join: initialize if already in a mission
if g_currentMission and not npcSystem then
print("[NPC Favor] Already in mission, attempting immediate initialization...")
load(g_currentMission)
if g_currentMission.placeables and npcSystem then
print("[NPC Favor] Mission already loaded, calling onMissionLoaded...")
npcSystem:onMissionLoaded()
end
end
addModEventListener({
onLoad = function()
print("[NPC Favor] Mod event listener registered")
end,
onUnload = function()
unload()
end,
onSavegameLoaded = function()
print("[NPC Favor] Savegame loaded event received")
if npcSystem then
npcSystem:onMissionLoaded()
else
print("[NPC Favor] npcSystem is nil in onSavegameLoaded")
end
end
})
print("[NPC Favor] Mod initialization complete")