From 9d34048e2aa601f278b8ab7db0a85281b9d94a1f Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 21:07:35 +0000 Subject: [PATCH 01/10] fix: after the hero slept every NPC starts over at its routine for the new time - sleepers get up, chasers give up (lost in the split of 26c60bf1e) Wld_SetTime (PC_Sleep, Nadja, a world's startup) and Marvin's "set time" only moved the clock: visible NPCs kept their state until it ended by itself, so the sleepers slept on through the morning. GameTimeService.SetTime now raises GameTimeSkipped; NpcService recalculates every routine and resets each living NPC like after culling (AiHandler.ResetAfterTimeSkip - it was still there, nobody called it): visible ones one per frame, culled ones when they show up. Dead and knocked out NPCs stay. Co-Authored-By: Claude Opus 5.5 --- .../Domain/Marvin/MarvinCheatDomain.cs | 2 +- .../Scripts/GlobalEventDispatcher.cs | 5 +++ .../Scripts/Services/Npc/NpcRoutineService.cs | 9 ++++ .../Scripts/Services/Npc/NpcService.cs | 41 ++++++++++++++++++- .../Scripts/Services/World/GameTimeService.cs | 2 + 5 files changed, 57 insertions(+), 2 deletions(-) diff --git a/Assets/Gothic-Core/Scripts/Domain/Marvin/MarvinCheatDomain.cs b/Assets/Gothic-Core/Scripts/Domain/Marvin/MarvinCheatDomain.cs index 13382c6ea..c5f2d634f 100644 --- a/Assets/Gothic-Core/Scripts/Domain/Marvin/MarvinCheatDomain.cs +++ b/Assets/Gothic-Core/Scripts/Domain/Marvin/MarvinCheatDomain.cs @@ -153,7 +153,7 @@ public bool Insert(string instanceName, int amount, bool isItem) } /// - /// NPC routines follow with the next game minute tick (NpcRoutineService.OnGameTimeMinuteChanged). + /// NPCs start over at their routine for the new time (GameTimeSkipped, NpcService.ResetNpcsAfterTimeSkip). /// public bool SetTime(int hour, int minute) { diff --git a/Assets/Gothic-Core/Scripts/GlobalEventDispatcher.cs b/Assets/Gothic-Core/Scripts/GlobalEventDispatcher.cs index e5ef283d2..4a44ffa1f 100644 --- a/Assets/Gothic-Core/Scripts/GlobalEventDispatcher.cs +++ b/Assets/Gothic-Core/Scripts/GlobalEventDispatcher.cs @@ -40,6 +40,11 @@ public static class GlobalEventDispatcher public static readonly UnityEvent GameTimeMinuteChangeCallback = new(); public static readonly UnityEvent GameTimeHourChangeCallback = new(); + /// + /// The game time jumped (Wld_SetTime: the hero slept, a night with Nadja, a world's startup; Marvin's "set time"). + /// + public static readonly UnityEvent GameTimeSkipped = new(); + public static readonly UnityEvent MusicZoneEntered = new(); public static readonly UnityEvent MusicZoneExited = new(); public static readonly UnityEvent LevelChangeTriggered = new(); diff --git a/Assets/Gothic-Core/Scripts/Services/Npc/NpcRoutineService.cs b/Assets/Gothic-Core/Scripts/Services/Npc/NpcRoutineService.cs index f09d66bc3..ebed6fad4 100644 --- a/Assets/Gothic-Core/Scripts/Services/Npc/NpcRoutineService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Npc/NpcRoutineService.cs @@ -36,6 +36,15 @@ public void Init() /// FIXME - Original G1 interrupts the current ZS_* state of visible NPCs when their routine changes. /// private void OnGameTimeMinuteChanged(DateTime now) + { + RecalculateAllNpcRoutines(); + } + + /// + /// Every NPC's routine for the time right now: at each game minute and right after a time jump + /// (NpcService.ResetNpcsAfterTimeSkip). + /// + public void RecalculateAllNpcRoutines() { foreach (var npc in _multiTypeCacheService.NpcCache) { diff --git a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs index 78a44b9ea..5654d218e 100644 --- a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs @@ -53,6 +53,7 @@ public class NpcService : ILazyInitQueue [Inject] private readonly VobService _vobService; [Inject] private readonly NpcInventoryService _npcInventoryService; [Inject] private readonly NpcLookService _npcLookService; + [Inject] private readonly NpcRoutineService _npcRoutineService; // Supporter class where the whole Init() logic is outsourced for better readability. @@ -62,6 +63,8 @@ public class NpcService : ILazyInitQueue private readonly NearestFirstQueue _objectsToInitQueue = new(); private Queue _objectToReEnableQueue = new(); + // NPCs to start over after the hero slept - culled ones when they show up again. + private readonly HashSet _timeSkipResets = new(); private DaedalusVm _vm => _gameStateService.GothicVm; @@ -84,12 +87,14 @@ public void Init() GlobalEventDispatcher.CreateNpc.AddListener(CreateVobNpc); // An armor change of an already built NPC swaps its body mesh (G2 Greg in the farmer clothes). GlobalEventDispatcher.NpcVisualBodyChanged.AddListener(_initializerDomain.RebuildBody); + GlobalEventDispatcher.GameTimeSkipped.AddListener(ResetNpcsAfterTimeSkip); } public void ClearQueues() { _objectsToInitQueue.Clear(); _objectToReEnableQueue.Clear(); + _timeSkipResets.Clear(); } public int CountQueuedWithin(Vector3 center, float radius) @@ -172,7 +177,10 @@ private IEnumerator ReEnableNpcCoroutine() { var npcData = _objectToReEnableQueue.Dequeue(); // If we walked to an NPC in our game, the NPC will be re-enabled and Routines get reset. - npcData.PrefabProps?.AiHandler?.ReEnableNpc(); + if (_timeSkipResets.Remove(npcData)) + npcData.PrefabProps?.AiHandler?.ResetAfterTimeSkip(); + else + npcData.PrefabProps?.AiHandler?.ReEnableNpc(); yield return _frameSkipperService.TrySkipToNextFrameCoroutine(); } } @@ -716,6 +724,37 @@ public bool InitNpc(GameObject go, bool initImmediately = false) return true; } + /// + /// After sleeping only the time jumped: NPCs kept sleeping, chasing the hero or standing where they were. Now + /// every living NPC starts over like after culling: routines are recalculated for the new time, visible NPCs + /// reset one per frame (no spawning into each other), culled ones when they show up again. Dead and knocked + /// out NPCs stay as they are. + /// + private void ResetNpcsAfterTimeSkip() + { + _npcRoutineService.RecalculateAllNpcRoutines(); + + var visibleCount = 0; + foreach (var npc in _multiTypeCacheService.NpcCache) + { + if (npc?.Props == null || npc.Vob == null || npc.Vob.Player) + continue; + if (npc.Props.BodyState is VmGothicEnums.BodyState.BsDead or VmGothicEnums.BodyState.BsUnconscious) + continue; + if (!_timeSkipResets.Add(npc)) + continue; + + if (npc.Go != null && npc.Go.activeInHierarchy) + { + _objectToReEnableQueue.Enqueue(npc); + visibleCount++; + } + } + + Logger.Log($"[TimeSkipReset] Time skipped - resetting {visibleCount} visible NPC(s), " + + $"{_timeSkipResets.Count - visibleCount} culled ones when they show up.", LogCat.Ai); + } + public void ReEnableNpc(NpcContainer npcData) { _objectToReEnableQueue.Enqueue(npcData); diff --git a/Assets/Gothic-Core/Scripts/Services/World/GameTimeService.cs b/Assets/Gothic-Core/Scripts/Services/World/GameTimeService.cs index 22d4d9d4f..7403a95b0 100644 --- a/Assets/Gothic-Core/Scripts/Services/World/GameTimeService.cs +++ b/Assets/Gothic-Core/Scripts/Services/World/GameTimeService.cs @@ -162,6 +162,8 @@ public void SetTime(int hour, int minute) if (daysToAdd > 0) _time = _time.AddDays(daysToAdd); + + GlobalEventDispatcher.GameTimeSkipped.Invoke(); } public float GetSkyTime() From 233b04809c669462028cf28a20537276c08278ae Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 21:46:20 +0000 Subject: [PATCH 02/10] fix: in G2 the NPCs near the hero get up after a sleep again - the time jump of the world's startup no longer leaves stale marks G2's STARTUP_* scripts call Wld_SetTime while the world loads: every NPC got a time-skip mark, an NPC's first build never used it up, and at the first real sleep the marked visible NPCs were skipped - Lobart's farmers slept on. A time jump while the world loads is ignored now (logged once), never built NPCs are not marked (they start fresh), a first build drops the mark, and visible NPCs are always queued (once). The log counts visible and culled NPCs. Co-Authored-By: Claude Opus 5.5 --- .../Scripts/Services/Npc/NpcService.cs | 44 +++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs index 5654d218e..85a647f77 100644 --- a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs @@ -65,6 +65,9 @@ public class NpcService : ILazyInitQueue private Queue _objectToReEnableQueue = new(); // NPCs to start over after the hero slept - culled ones when they show up again. private readonly HashSet _timeSkipResets = new(); + // From the loading scene until the world runs (WorldSceneLoaded): the world's STARTUP_* scripts run meanwhile. + private bool _isWorldLoading; + private bool _isTimeSkipWhileLoadingLogged; private DaedalusVm _vm => _gameStateService.GothicVm; @@ -88,6 +91,12 @@ public void Init() // An armor change of an already built NPC swaps its body mesh (G2 Greg in the farmer clothes). GlobalEventDispatcher.NpcVisualBodyChanged.AddListener(_initializerDomain.RebuildBody); GlobalEventDispatcher.GameTimeSkipped.AddListener(ResetNpcsAfterTimeSkip); + GlobalEventDispatcher.LoadingSceneLoaded.AddListener(() => + { + _isWorldLoading = true; + _isTimeSkipWhileLoadingLogged = false; + }); + GlobalEventDispatcher.WorldSceneLoaded.AddListener(() => _isWorldLoading = false); } public void ClearQueues() @@ -729,30 +738,53 @@ public bool InitNpc(GameObject go, bool initImmediately = false) /// every living NPC starts over like after culling: routines are recalculated for the new time, visible NPCs /// reset one per frame (no spawning into each other), culled ones when they show up again. Dead and knocked /// out NPCs stay as they are. + /// A jump while the world loads (G2's STARTUP_* scripts call Wld_SetTime) is ignored: every NPC starts fresh + /// anyway, and the marks would never be used up. /// private void ResetNpcsAfterTimeSkip() { + if (_isWorldLoading) + { + if (!_isTimeSkipWhileLoadingLogged) + { + _isTimeSkipWhileLoadingLogged = true; + Logger.Log("[TimeSkipReset] Time skipped while the world loads - NPCs start fresh, no reset.", + LogCat.Ai); + } + return; + } + _npcRoutineService.RecalculateAllNpcRoutines(); var visibleCount = 0; + var culledCount = 0; foreach (var npc in _multiTypeCacheService.NpcCache) { if (npc?.Props == null || npc.Vob == null || npc.Vob.Player) continue; if (npc.Props.BodyState is VmGothicEnums.BodyState.BsDead or VmGothicEnums.BodyState.BsUnconscious) continue; - if (!_timeSkipResets.Add(npc)) + // Never built: it starts fresh at its routine when it shows up (InitNpc). + if (npc.Go == null) continue; - if (npc.Go != null && npc.Go.activeInHierarchy) + _timeSkipResets.Add(npc); + + if (npc.Go.activeInHierarchy) { - _objectToReEnableQueue.Enqueue(npc); + // Marked again by a second jump, or coming into view right now: queued once is enough. + if (!_objectToReEnableQueue.Contains(npc)) + _objectToReEnableQueue.Enqueue(npc); visibleCount++; } + else + { + culledCount++; + } } Logger.Log($"[TimeSkipReset] Time skipped - resetting {visibleCount} visible NPC(s), " + - $"{_timeSkipResets.Count - visibleCount} culled ones when they show up.", LogCat.Ai); + $"{culledCount} culled ones when they show up.", LogCat.Ai); } public void ReEnableNpc(NpcContainer npcData) @@ -921,6 +953,10 @@ private void EventNpcMeshCullingChanged(NpcContainer npcContainer, NpcLoader npc { var initializedNow = InitNpc(npcLoader.gameObject); + // Built for the first time: it starts at its routine for the time right now anyway. + if (initializedNow) + _timeSkipResets.Remove(npcContainer); + // If the NPC !wasOutOfDistance (==wasInDistanceAlready), then we spawned our VRPlayer next to the NPC // (e.g. from a save game) and we need to go on with the current routine instead of "resetting" the routine. // (Which would respawn NPC at a waypoint, which is wrong.) From aed8266f2e253964925227c8948d96022069dfe2 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 21:47:03 +0000 Subject: [PATCH 03/10] fix: followers, guides and summons stay with the hero after a sleep - the time-skip reset no longer teleports them to a waypoint The reset after a time jump sends an NPC to its routine waypoint like after culling. NPCs in a guide/follow state (ZS_GuidePC/ZS_FollowPC, ZS_Guide_Player/ZS_Follow_Player), party members (AIV_PARTYMEMBER, e.g. Mordrag leading the hero) and the hero's summons (AIV_MM_PARTYMEMBER) are kept now; the log counts them. Co-Authored-By: Claude Opus 5.5 --- .../Scripts/Const/DaedalusConst.cs | 1 + .../Scripts/Services/Npc/NpcService.cs | 33 ++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Assets/Gothic-Core/Scripts/Const/DaedalusConst.cs b/Assets/Gothic-Core/Scripts/Const/DaedalusConst.cs index 5adfa229a..14992e9c3 100644 --- a/Assets/Gothic-Core/Scripts/Const/DaedalusConst.cs +++ b/Assets/Gothic-Core/Scripts/Const/DaedalusConst.cs @@ -48,6 +48,7 @@ public class DaedalusConst public const string SpellBad = "SPELL_BAD"; public const string AivEnemyOverride = "AIV_ENEMYOVERRIDE"; public const string AivLastHitByRangedWeapon = "AIV_LASTHITBYRANGEDWEAPON"; + public const string AivPartyMember = "AIV_PARTYMEMBER"; public const string AivMmPartyMember = "AIV_MM_PARTYMEMBER"; public const string NpcTalentMage = "NPC_TALENT_MAGE"; public const string NpcAttackFinishDistance = "NPC_ATTACK_FINISH_DISTANCE"; diff --git a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs index 85a647f77..c4cb1e07e 100644 --- a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs @@ -756,8 +756,11 @@ private void ResetNpcsAfterTimeSkip() _npcRoutineService.RecalculateAllNpcRoutines(); + var partyAivar = GetAiVarIndex(DaedalusConst.AivPartyMember); + var summonPartyAivar = GetAiVarIndex(DaedalusConst.AivMmPartyMember); var visibleCount = 0; var culledCount = 0; + var keptCount = 0; foreach (var npc in _multiTypeCacheService.NpcCache) { if (npc?.Props == null || npc.Vob == null || npc.Vob.Player) @@ -767,6 +770,13 @@ private void ResetNpcsAfterTimeSkip() // Never built: it starts fresh at its routine when it shows up (InitNpc). if (npc.Go == null) continue; + // Followers, guides and summons stay with the hero: the reset would teleport them to a waypoint. + if (IsWithHero(npc, partyAivar, summonPartyAivar)) + { + _timeSkipResets.Remove(npc); + keptCount++; + continue; + } _timeSkipResets.Add(npc); @@ -784,7 +794,28 @@ private void ResetNpcsAfterTimeSkip() } Logger.Log($"[TimeSkipReset] Time skipped - resetting {visibleCount} visible NPC(s), " + - $"{culledCount} culled ones when they show up.", LogCat.Ai); + $"{culledCount} culled ones when they show up, keeping {keptCount} with the hero.", LogCat.Ai); + } + + /// + /// Guides and followers (escort states), party members (AIV_PARTYMEMBER: Mordrag leading the hero) and the + /// hero's summons (AIV_MM_PARTYMEMBER). + /// + private static bool IsWithHero(NpcContainer npc, int partyAivar, int summonPartyAivar) + { + if (NpcHelperService.IsInEscortState(npc.Instance)) + return true; + if (partyAivar >= 0 && npc.Instance.GetAiVar(partyAivar) != 0) + return true; + return summonPartyAivar >= 0 && npc.Instance.GetAiVar(summonPartyAivar) != 0; + } + + /// + /// The index of an AIV_* constant, or -1 when the scripts don't have it. + /// + private int GetAiVarIndex(string aivarName) + { + return _vm.GetSymbolByName(aivarName)?.GetInt(0) ?? -1; } public void ReEnableNpc(NpcContainer npcData) From 21d86468c920dbb40db6395d6c5fe543e7c2aa24 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 21:47:24 +0000 Subject: [PATCH 04/10] fix: the NPC the hero talks to keeps the dialog when Marvin sets the time - the time-skip reset leaves him out A time jump during a dialog (Marvin's "set time"; sleeping opens no dialog) reset the dialog partner like after culling: empty AI queue, teleport to his routine waypoint, while the dialog was still open. Co-Authored-By: Claude Opus 5.5 --- .../Gothic-Core/Scripts/Services/Npc/NpcService.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs index c4cb1e07e..1b4a35b24 100644 --- a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs @@ -737,7 +737,7 @@ public bool InitNpc(GameObject go, bool initImmediately = false) /// After sleeping only the time jumped: NPCs kept sleeping, chasing the hero or standing where they were. Now /// every living NPC starts over like after culling: routines are recalculated for the new time, visible NPCs /// reset one per frame (no spawning into each other), culled ones when they show up again. Dead and knocked - /// out NPCs stay as they are. + /// out NPCs, the hero's followers and summons and his dialog partner stay as they are. /// A jump while the world loads (G2's STARTUP_* scripts call Wld_SetTime) is ignored: every NPC starts fresh /// anyway, and the marks would never be used up. /// @@ -770,8 +770,9 @@ private void ResetNpcsAfterTimeSkip() // Never built: it starts fresh at its routine when it shows up (InitNpc). if (npc.Go == null) continue; - // Followers, guides and summons stay with the hero: the reset would teleport them to a waypoint. - if (IsWithHero(npc, partyAivar, summonPartyAivar)) + // Followers, guides and summons stay with the hero: the reset would teleport them to a waypoint. The + // NPC talking to the hero (Marvin's "set time" during a dialog) keeps its dialog. + if (IsWithHero(npc, partyAivar, summonPartyAivar) || IsInDialogWithHero(npc)) { _timeSkipResets.Remove(npc); keptCount++; @@ -810,6 +811,12 @@ private static bool IsWithHero(NpcContainer npc, int partyAivar, int summonParty return summonPartyAivar >= 0 && npc.Instance.GetAiVar(summonPartyAivar) != 0; } + private bool IsInDialogWithHero(NpcContainer npc) + { + var dialogs = _gameStateService.Dialogs; + return dialogs.IsInDialog && dialogs.CurrentDialogNpc == npc; + } + /// /// The index of an AIV_* constant, or -1 when the scripts don't have it. /// From d927fc1ebee80995d56fe8266a49105641d576e3 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 21:47:40 +0000 Subject: [PATCH 05/10] fix: a magically frozen NPC starts over after a sleep too - the time-skip reset uses the shared "is down" rule The reset skipped every NPC with BS_UNCONSCIOUS, which ZS_MagicFreeze also sets. It now uses IsDown(): dead or knocked out, not frozen. Co-Authored-By: Claude Opus 5.5 --- Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs index 1b4a35b24..4ef9314f7 100644 --- a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs @@ -765,7 +765,8 @@ private void ResetNpcsAfterTimeSkip() { if (npc?.Props == null || npc.Vob == null || npc.Vob.Player) continue; - if (npc.Props.BodyState is VmGothicEnums.BodyState.BsDead or VmGothicEnums.BodyState.BsUnconscious) + // Dead or knocked out (a magically frozen NPC isn't down: it starts over too). + if (npc.IsDown()) continue; // Never built: it starts fresh at its routine when it shows up (InitNpc). if (npc.Go == null) From 66c90a7baf7d149b85a53bd7a4befc8b5d821ed3 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 21:48:06 +0000 Subject: [PATCH 06/10] docs: decision record - after a time jump every NPC starts over, except the down, the hero's company and his dialog partner Co-Authored-By: Claude Opus 5.5 --- Docs/adr/2026-11-25-time-skip-npc-reset.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Docs/adr/2026-11-25-time-skip-npc-reset.md diff --git a/Docs/adr/2026-11-25-time-skip-npc-reset.md b/Docs/adr/2026-11-25-time-skip-npc-reset.md new file mode 100644 index 000000000..2feecc22e --- /dev/null +++ b/Docs/adr/2026-11-25-time-skip-npc-reset.md @@ -0,0 +1,22 @@ +# ADR: After a time jump every NPC starts over + +- Date: 2026-11-25 +- Technical details: NpcService.ResetNpcsAfterTimeSkip and AiHandler.ResetAfterTimeSkip (Core) + +## Context + +When the hero sleeps, the scripts move the clock forward (Wld_SetTime); Marvin's "set time" does the same. Only the clock moved: NPCs kept doing what they did before the jump. Sleepers slept on through the morning, guards kept chasing the hero after a night in bed. The original game ends the current activity of an NPC when its daily routine changes, so after the jump everyone is where the new time wants them. This behaviour existed before and was lost when an earlier branch was split into PRs. + +## Decisions + +1. **A time jump makes every living NPC start over at its daily routine for the new time, like an NPC that comes back into view.** NPCs near the hero start over one after another (so they don't appear inside each other), NPCs out of view when they come back. They forget a fight in progress and put their weapon away. Not chosen: only recalculating the routines - an NPC keeps its current activity until it ends by itself, which for a sleeper is the whole morning. +2. **Some NPCs are left out: dead and knocked out ones, the hero's followers, guides and summons, and the NPC the hero is talking to.** Starting over would bring the downed back to their feet, teleport the hero's company to a waypoint far away, and break an open dialog (Marvin can set the time while talking). A magically frozen NPC is not down and starts over. +3. **A time jump while a world is loading is ignored.** Gothic 2's world startup scripts set the time; every NPC starts fresh there anyway. +4. **Not chosen for now: ending the current activity whenever a routine changes, as the original game does.** That is the general rule (also for the hourly routine changes without a jump) and is done in its own PR (fix/routine-change-ends-state). The time jump reset stays as the quick, complete answer for sleeping. + +## Consequences + +- After sleeping, NPCs near the hero visibly jump to their routine spots for the new time, one per frame. +- An NPC that was out of view during the jump starts over when it comes back into view, even much later. It also forgets a fight it had at that moment. +- The pending resets are not saved: saving after a sleep and loading again lets NPCs out of view continue normally instead of starting over. +- Once routine changes end the current activity in general, this record may be replaced. From 25d5c70421b72b3384aae6271a73a83906b143ad Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 22:47:09 +0000 Subject: [PATCH 07/10] refactor: one rule for "the hero talks to this NPC" - DialogModel.IsTalkingTo, used by the time-skip reset Co-Authored-By: Claude Opus 5.5 --- Assets/Gothic-Core/Scripts/Models/Npc/DialogModel.cs | 8 ++++++++ Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs | 8 +------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Assets/Gothic-Core/Scripts/Models/Npc/DialogModel.cs b/Assets/Gothic-Core/Scripts/Models/Npc/DialogModel.cs index 09f46fde6..b503546b8 100644 --- a/Assets/Gothic-Core/Scripts/Models/Npc/DialogModel.cs +++ b/Assets/Gothic-Core/Scripts/Models/Npc/DialogModel.cs @@ -37,6 +37,14 @@ public class DialogModel /// public InfoInstance ExitInfo; + /// + /// The hero is in a dialog with this NPC. + /// + public bool IsTalkingTo(NpcContainer npc) + { + return IsInDialog && npc != null && CurrentDialogNpc == npc; + } + public void Dispose() { IsInDialog = false; diff --git a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs index 4ef9314f7..6a38625e9 100644 --- a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs @@ -773,7 +773,7 @@ private void ResetNpcsAfterTimeSkip() continue; // Followers, guides and summons stay with the hero: the reset would teleport them to a waypoint. The // NPC talking to the hero (Marvin's "set time" during a dialog) keeps its dialog. - if (IsWithHero(npc, partyAivar, summonPartyAivar) || IsInDialogWithHero(npc)) + if (IsWithHero(npc, partyAivar, summonPartyAivar) || _gameStateService.Dialogs.IsTalkingTo(npc)) { _timeSkipResets.Remove(npc); keptCount++; @@ -812,12 +812,6 @@ private static bool IsWithHero(NpcContainer npc, int partyAivar, int summonParty return summonPartyAivar >= 0 && npc.Instance.GetAiVar(summonPartyAivar) != 0; } - private bool IsInDialogWithHero(NpcContainer npc) - { - var dialogs = _gameStateService.Dialogs; - return dialogs.IsInDialog && dialogs.CurrentDialogNpc == npc; - } - /// /// The index of an AIV_* constant, or -1 when the scripts don't have it. /// From e8f40092d705a72fbd53669a6573eb86f3c81923 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 22:48:22 +0000 Subject: [PATCH 08/10] refactor: a time jump while the world loads is no time jump - GameTimeService doesn't raise GameTimeSkipped then, the NPC reset drops its own loading flag Co-Authored-By: Claude Opus 5.5 --- .../Scripts/GlobalEventDispatcher.cs | 3 ++- .../Scripts/Services/Npc/NpcService.cs | 24 ++----------------- .../Scripts/Services/World/GameTimeService.cs | 24 +++++++++++++++++++ 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/Assets/Gothic-Core/Scripts/GlobalEventDispatcher.cs b/Assets/Gothic-Core/Scripts/GlobalEventDispatcher.cs index 4a44ffa1f..de60c95fc 100644 --- a/Assets/Gothic-Core/Scripts/GlobalEventDispatcher.cs +++ b/Assets/Gothic-Core/Scripts/GlobalEventDispatcher.cs @@ -41,7 +41,8 @@ public static class GlobalEventDispatcher public static readonly UnityEvent GameTimeHourChangeCallback = new(); /// - /// The game time jumped (Wld_SetTime: the hero slept, a night with Nadja, a world's startup; Marvin's "set time"). + /// The game time jumped while a world runs (Wld_SetTime: the hero slept, a night with Nadja; Marvin's + /// "set time"). Not raised while a world loads (its STARTUP_* scripts set the time). /// public static readonly UnityEvent GameTimeSkipped = new(); diff --git a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs index 6a38625e9..808d90d7d 100644 --- a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs @@ -65,9 +65,6 @@ public class NpcService : ILazyInitQueue private Queue _objectToReEnableQueue = new(); // NPCs to start over after the hero slept - culled ones when they show up again. private readonly HashSet _timeSkipResets = new(); - // From the loading scene until the world runs (WorldSceneLoaded): the world's STARTUP_* scripts run meanwhile. - private bool _isWorldLoading; - private bool _isTimeSkipWhileLoadingLogged; private DaedalusVm _vm => _gameStateService.GothicVm; @@ -91,12 +88,6 @@ public void Init() // An armor change of an already built NPC swaps its body mesh (G2 Greg in the farmer clothes). GlobalEventDispatcher.NpcVisualBodyChanged.AddListener(_initializerDomain.RebuildBody); GlobalEventDispatcher.GameTimeSkipped.AddListener(ResetNpcsAfterTimeSkip); - GlobalEventDispatcher.LoadingSceneLoaded.AddListener(() => - { - _isWorldLoading = true; - _isTimeSkipWhileLoadingLogged = false; - }); - GlobalEventDispatcher.WorldSceneLoaded.AddListener(() => _isWorldLoading = false); } public void ClearQueues() @@ -738,22 +729,11 @@ public bool InitNpc(GameObject go, bool initImmediately = false) /// every living NPC starts over like after culling: routines are recalculated for the new time, visible NPCs /// reset one per frame (no spawning into each other), culled ones when they show up again. Dead and knocked /// out NPCs, the hero's followers and summons and his dialog partner stay as they are. - /// A jump while the world loads (G2's STARTUP_* scripts call Wld_SetTime) is ignored: every NPC starts fresh - /// anyway, and the marks would never be used up. + /// A jump while the world loads (G2's STARTUP_* scripts call Wld_SetTime) doesn't come here: GameTimeService + /// doesn't raise GameTimeSkipped then (every NPC starts fresh anyway, and the marks would never be used up). /// private void ResetNpcsAfterTimeSkip() { - if (_isWorldLoading) - { - if (!_isTimeSkipWhileLoadingLogged) - { - _isTimeSkipWhileLoadingLogged = true; - Logger.Log("[TimeSkipReset] Time skipped while the world loads - NPCs start fresh, no reset.", - LogCat.Ai); - } - return; - } - _npcRoutineService.RecalculateAllNpcRoutines(); var partyAivar = GetAiVarIndex(DaedalusConst.AivPartyMember); diff --git a/Assets/Gothic-Core/Scripts/Services/World/GameTimeService.cs b/Assets/Gothic-Core/Scripts/Services/World/GameTimeService.cs index 7403a95b0..2660c983b 100644 --- a/Assets/Gothic-Core/Scripts/Services/World/GameTimeService.cs +++ b/Assets/Gothic-Core/Scripts/Services/World/GameTimeService.cs @@ -1,9 +1,11 @@ using System; using System.Collections; +using Gothic.Core.Logging; using Gothic.Core.Manager; using Gothic.Core.Services.Config; using Reflex.Attributes; using UnityEngine; +using Logger = Gothic.Core.Logging.Logger; namespace Gothic.Core.Services.World { @@ -35,6 +37,9 @@ public enum GameTimeInterval private static readonly float _oneIngameSecond = 0.06944f; private DateTime _time = new(1, 1, 1, 15, 0, 0); private Coroutine _timeTickCoroutineHandler; + // From the loading scene until the world runs (WorldSceneLoaded): the world's STARTUP_* scripts run meanwhile. + private bool _isWorldLoading; + private bool _isTimeSkipWhileLoadingLogged; public void Init() @@ -58,6 +63,9 @@ public void ResetToStartTime() private void PreWorldLoaded() { + _isWorldLoading = true; + _isTimeSkipWhileLoadingLogged = false; + // Pause Coroutine until next world is loaded. if (_timeTickCoroutineHandler != null) { @@ -67,6 +75,7 @@ private void PreWorldLoaded() private void PostWorldLoaded() { + _isWorldLoading = false; _timeTickCoroutineHandler = _unityMonoService.StartCoroutine(TimeTick()); } @@ -152,6 +161,10 @@ public void SetDayAndTime(int day, int hour, int minute) _minutesInHour = minute; } + /// + /// Wld_SetTime and Marvin's "set time". Raises GameTimeSkipped only while a world runs: a jump while the world + /// loads (G2's STARTUP_* scripts call Wld_SetTime) isn't one for anybody - every NPC starts fresh anyway. + /// public void SetTime(int hour, int minute) { // From G2: Setzt die Uhrzeit auf hour:min. hour kann größer als 23 sein, um zum nächsten Tag zu springen. @@ -163,6 +176,17 @@ public void SetTime(int hour, int minute) if (daysToAdd > 0) _time = _time.AddDays(daysToAdd); + if (_isWorldLoading) + { + if (!_isTimeSkipWhileLoadingLogged) + { + _isTimeSkipWhileLoadingLogged = true; + Logger.Log($"[GameTime] Time set to {_time:HH:mm} while the world loads - " + + "no GameTimeSkipped (NPCs start fresh anyway).", LogCat.Loading); + } + return; + } + GlobalEventDispatcher.GameTimeSkipped.Invoke(); } From b2495c84e9a67d53b66c06fe1221e6a852b46808 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 22:49:26 +0000 Subject: [PATCH 09/10] refactor: one rule for "the hero's company" - NpcHelperService.IsHeroCompanion (escort, party member, summon), used by the time-skip reset Co-Authored-By: Claude Opus 5.5 --- .../Scripts/Services/Npc/NpcHelperService.cs | 19 ++++++++++++++ .../Scripts/Services/Npc/NpcService.cs | 26 ++----------------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/Assets/Gothic-Core/Scripts/Services/Npc/NpcHelperService.cs b/Assets/Gothic-Core/Scripts/Services/Npc/NpcHelperService.cs index 264cf24ca..65a61c779 100644 --- a/Assets/Gothic-Core/Scripts/Services/Npc/NpcHelperService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Npc/NpcHelperService.cs @@ -491,6 +491,25 @@ public static bool IsInEscortState(NpcInstance npc) return false; } + /// + /// The hero's company: guides and followers (escort states), party members (AIV_PARTYMEMBER: Mordrag leading + /// the hero) and his summons (AIV_MM_PARTYMEMBER). An AIV_* constant the scripts don't have counts as unset. + /// + public bool IsHeroCompanion(NpcInstance npc) + { + if (npc == null) + return false; + if (IsInEscortState(npc)) + return true; + return IsAiVarSet(npc, DaedalusConst.AivPartyMember) || IsAiVarSet(npc, DaedalusConst.AivMmPartyMember); + } + + private bool IsAiVarSet(NpcInstance npc, string aivarName) + { + var symbol = _gameStateService.GothicVm.GetSymbolByName(aivarName); + return symbol != null && npc.GetAiVar(symbol.GetInt(0)) != 0; + } + private bool CanSenseNpcQuietly(NpcInstance self, NpcInstance other, VmGothicEnums.NpcSenses senses, bool freeLOS, float range, float senseRangeMeters) { diff --git a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs index 808d90d7d..b0b74c48a 100644 --- a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs @@ -54,6 +54,7 @@ public class NpcService : ILazyInitQueue [Inject] private readonly NpcInventoryService _npcInventoryService; [Inject] private readonly NpcLookService _npcLookService; [Inject] private readonly NpcRoutineService _npcRoutineService; + [Inject] private readonly NpcHelperService _npcHelperService; // Supporter class where the whole Init() logic is outsourced for better readability. @@ -736,8 +737,6 @@ private void ResetNpcsAfterTimeSkip() { _npcRoutineService.RecalculateAllNpcRoutines(); - var partyAivar = GetAiVarIndex(DaedalusConst.AivPartyMember); - var summonPartyAivar = GetAiVarIndex(DaedalusConst.AivMmPartyMember); var visibleCount = 0; var culledCount = 0; var keptCount = 0; @@ -753,7 +752,7 @@ private void ResetNpcsAfterTimeSkip() continue; // Followers, guides and summons stay with the hero: the reset would teleport them to a waypoint. The // NPC talking to the hero (Marvin's "set time" during a dialog) keeps its dialog. - if (IsWithHero(npc, partyAivar, summonPartyAivar) || _gameStateService.Dialogs.IsTalkingTo(npc)) + if (_npcHelperService.IsHeroCompanion(npc.Instance) || _gameStateService.Dialogs.IsTalkingTo(npc)) { _timeSkipResets.Remove(npc); keptCount++; @@ -779,27 +778,6 @@ private void ResetNpcsAfterTimeSkip() $"{culledCount} culled ones when they show up, keeping {keptCount} with the hero.", LogCat.Ai); } - /// - /// Guides and followers (escort states), party members (AIV_PARTYMEMBER: Mordrag leading the hero) and the - /// hero's summons (AIV_MM_PARTYMEMBER). - /// - private static bool IsWithHero(NpcContainer npc, int partyAivar, int summonPartyAivar) - { - if (NpcHelperService.IsInEscortState(npc.Instance)) - return true; - if (partyAivar >= 0 && npc.Instance.GetAiVar(partyAivar) != 0) - return true; - return summonPartyAivar >= 0 && npc.Instance.GetAiVar(summonPartyAivar) != 0; - } - - /// - /// The index of an AIV_* constant, or -1 when the scripts don't have it. - /// - private int GetAiVarIndex(string aivarName) - { - return _vm.GetSymbolByName(aivarName)?.GetInt(0) ?? -1; - } - public void ReEnableNpc(NpcContainer npcData) { _objectToReEnableQueue.Enqueue(npcData); From 3220abcf33512589cb223c6e6def2c33fd21b3e4 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 22:49:53 +0000 Subject: [PATCH 10/10] docs: decision record - after a time jump the start-over owns the NPCs, the routine change PR stacked on it doesn't end their activities then Co-Authored-By: Claude Opus 5.5 --- Docs/adr/2026-11-25-time-skip-npc-reset.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Docs/adr/2026-11-25-time-skip-npc-reset.md b/Docs/adr/2026-11-25-time-skip-npc-reset.md index 2feecc22e..4ba57478d 100644 --- a/Docs/adr/2026-11-25-time-skip-npc-reset.md +++ b/Docs/adr/2026-11-25-time-skip-npc-reset.md @@ -1,7 +1,7 @@ # ADR: After a time jump every NPC starts over - Date: 2026-11-25 -- Technical details: NpcService.ResetNpcsAfterTimeSkip and AiHandler.ResetAfterTimeSkip (Core) +- Technical details: NpcService.ResetNpcsAfterTimeSkip, AiHandler.ResetAfterTimeSkip and GameTimeService.SetTime (Core) ## Context @@ -11,12 +11,12 @@ When the hero sleeps, the scripts move the clock forward (Wld_SetTime); Marvin's 1. **A time jump makes every living NPC start over at its daily routine for the new time, like an NPC that comes back into view.** NPCs near the hero start over one after another (so they don't appear inside each other), NPCs out of view when they come back. They forget a fight in progress and put their weapon away. Not chosen: only recalculating the routines - an NPC keeps its current activity until it ends by itself, which for a sleeper is the whole morning. 2. **Some NPCs are left out: dead and knocked out ones, the hero's followers, guides and summons, and the NPC the hero is talking to.** Starting over would bring the downed back to their feet, teleport the hero's company to a waypoint far away, and break an open dialog (Marvin can set the time while talking). A magically frozen NPC is not down and starts over. -3. **A time jump while a world is loading is ignored.** Gothic 2's world startup scripts set the time; every NPC starts fresh there anyway. -4. **Not chosen for now: ending the current activity whenever a routine changes, as the original game does.** That is the general rule (also for the hourly routine changes without a jump) and is done in its own PR (fix/routine-change-ends-state). The time jump reset stays as the quick, complete answer for sleeping. +3. **A time jump while a world is loading is no time jump.** Gothic 2's world startup scripts set the time; every NPC starts fresh there anyway. The game clock itself doesn't report such a jump, so nothing that waits for time jumps has to filter it out. +4. **After a time jump the start-over owns the NPCs, not the general routine change.** Ending the current activity whenever a routine changes, as the original game does, is the general rule (also for the hourly routine changes without a jump) and comes in its own PR (fix/routine-change-ends-state), stacked on this one. After a jump that rule stays out of the way: the routines are recalculated for the new time, but it doesn't end the activities, because the NPCs near the hero start over anyway. Not chosen: letting both act - an NPC would begin to get up or walk off, and a few frames later the start-over cuts that off and puts it on its spot. ## Consequences - After sleeping, NPCs near the hero visibly jump to their routine spots for the new time, one per frame. - An NPC that was out of view during the jump starts over when it comes back into view, even much later. It also forgets a fight it had at that moment. - The pending resets are not saved: saving after a sleep and loading again lets NPCs out of view continue normally instead of starting over. -- Once routine changes end the current activity in general, this record may be replaced. +- With the general routine change merged too, an NPC that starts over after a jump doesn't play the end of its old activity (a sleeper doesn't stand up first, it is put on its spot standing).