Skip to content

fix: NPCs move on with their daily routine - a routine change ends the routine's state (Harad leaves the anvil, sleepers get up) - #575

Open
BoroBongo wants to merge 7 commits into
fix/npcs-start-over-after-sleepfrom
fix/routine-state-ends-on-routine-change
Open

BoroBongo wants to merge 7 commits into
fix/npcs-start-over-after-sleepfrom
fix/routine-state-ends-on-routine-change

Conversation

@BoroBongo

Copy link
Copy Markdown
Contributor

What

When a visible NPC's routine entry changes, the state the routine started now ends the engine way. Its ZS_*_End runs (for example AI_UseMob(-1), standing up from the anvil), and then the next routine state starts. Example: G2 Harad goes forge fire -> anvil -> cooling bucket every 10 minutes, where before he stayed at the first mob. Sleepers get up in the morning while you watch.

Cases:

  • States started by a perception or by AI_StartState from outside a routine state are not ended: talking, fighting, watching the player.
  • A state started from inside a routine state counts as part of the routine. Example: G2 ZS_GotoBed -> ZS_Sleep.
  • Not ended for an NPC in a dialog, a culled NPC, or an NPC that is down.
  • Not ended when the next entry is the same activity at the same spot. Example: G1 Bilgot's campfire 08-23 / 23-08.
  • If the routine changes while a routine state is still queued (an NPC walking to bed), that queued state is dropped once it is due. The NPC doesn't go to sleep under the old entry.
  • AI_UseMob(-1) without a mob in use does nothing, like OpenGothic.
  • After a time jump the start-over from PR A owns the visible NPCs, so the routine change doesn't end their states as well.

Why

In a playtest, Harad stayed at the anvil forever and visible sleepers never got up. A FIXME in NpcRoutineService said this was missing.

How to test manually

  • G2 Khorinis 07:00-08:00: Harad moves between the forge fire, the anvil and the cooling bucket every 10 game minutes. The log shows [Routine] Harad: routine changed - ... ends, next ....
  • G2: Harad sleeps 22:00-07:00. At 07:00, in view, he gets up and goes to the forge.
  • Talk to Harad across 07:10. The dialog isn't cut; afterwards he goes to the new spot.
  • G1 Bilgot doesn't stand up at 23:00.
  • G2: at about 22:00, while a citizen walks to bed, set the time past his next routine change. He doesn't lie down and stay.
  • Provoke a guard just before his routine change. His warning or attack isn't cut.
  • Sleep with NPCs around (together with PR A). No NPC snaps or is stuck halfway through standing up.

Save/load

Nothing new is saved. ZS_Sleep (G2) and the monster ZS_MM_Rtn_* states are now saved as routine states; after a load they restart their routine entry.

Known gaps / follow-ups

  • Earlier work on the same problem: Todi's remote branch fix/routine-change-ends-state (2026-10-29..31, no PR, e.g. 235f284 "NPCs in sight follow their daily routine by the clock"). It was not used here and is left untouched. Worth comparing before merging.
  • NPC code cleanup after the 2026-11-25 fixes: split NpcService, one place per rule #568: three classes write CurrentLoopState from outside AiHandler; that should become one operation.
  • ADR: Docs/adr/2026-11-25-routine-change-ends-state.md

🤖 Generated with Claude Code

BoroBongo and others added 7 commits November 25, 2026 22:50
…e routine's state (Harad leaves the anvil, sleepers get up)

- NpcRoutineService: when a visible NPC's routine entry changes (time or Npc_ExchangeRoutine) and its state is a
  routine state, it ends like the engine: _End once the current actions are done, then the new entry's state;
  perception/AI_StartState states, the dialog partner, dead/knocked out NPCs and same state+waypoint entries are left
  alone; culled NPCs keep the culling behaviour; removes the FIXME and the double NotifyNpcRoutineChanged
- AiHandler/NpcAiService/StartState: AI_StartState from a routine state's start/loop function stays a routine state
  (engine keeps the end time) - G2 TA_Sleep is ZS_GotoBed -> ZS_Sleep
- UseMob: AI_UseMob(x, -1) without a mob in use does nothing (engine) instead of walking to the nearest mob
- NpcMeshCullingService.IsAwake; ADR 2026-11-25-routine-change-ends-state, npc-ai.md

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…be put on their spot - the time-skip reset alone starts them over, the routine change doesn't end their states then

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… down for hours - the sleep queued for the old routine entry gives way to the new one

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… - the AI loop no longer overwrites it with "looping"

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ally frozen NPC counts like everywhere else

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… hero talks to the NPC

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…a reaction - the routine change doesn't cut it off

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@BoroBongo

Copy link
Copy Markdown
Contributor Author

Review (architecture-reviewer + gothic-reviewer)

Gothic: OK to merge. Architecture: fits with concerns. Fixed in this PR:

Follow-up: #568 (one operation for ending a state).

Note: Todi's earlier remote branch fix/routine-change-ends-state covers the same problem. It is left untouched (#571 was opened on it by mistake and is closed).

Editor check (G2): at 07:10 the log shows [Routine] Harad: routine changed - ZS_SMITH_FIRE ends, next ZS_SMITH_ANVIL. He leaves the fire and walks to the anvil. The author confirmed fire -> anvil -> bucket in play.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant