refactor(streaming): extract the update frame

This commit is contained in:
Erik 2026-07-22 01:01:04 +02:00
parent 4e4aac2c5a
commit 0bc9fda9de
13 changed files with 912 additions and 184 deletions

View file

@ -123,6 +123,7 @@ public sealed class GameWindow : IDisposable
_landblockPresentationPipeline;
private AcDream.App.Rendering.EquippedChildRenderController? _equippedChildRenderer;
private AcDream.App.Streaming.StreamingController? _streamingController;
private AcDream.App.Update.IStreamingFramePhase _streamingFrame = null!;
private AcDream.App.Streaming.WorldRevealCoordinator? _worldReveal;
private int _streamingRadius = 2; // default 5×5 (kept for debug overlay getStreamingRadius callback)
private int _nearRadius = 4; // Phase A.5 T16: two-tier near ring (default 4 → 9×9)
@ -531,7 +532,12 @@ public sealed class GameWindow : IDisposable
}
private AcDream.App.Rendering.ChaseCamera? _chaseCamera;
private AcDream.App.Rendering.RetailChaseCamera? _retailChaseCamera;
private bool _playerMode;
private readonly AcDream.App.Input.LocalPlayerModeState _localPlayerMode = new();
private bool _playerMode
{
get => _localPlayerMode.IsPlayerMode;
set => _localPlayerMode.IsPlayerMode = value;
}
private readonly AcDream.App.Input.LocalPlayerIdentityState _localPlayerIdentity = new();
private uint _playerServerGuid
{
@ -661,18 +667,6 @@ public sealed class GameWindow : IDisposable
private readonly AcDream.App.World.LiveWorldOriginState _liveWorldOrigin = new();
private int _liveCenterX => _liveWorldOrigin.CenterX;
private int _liveCenterY => _liveWorldOrigin.CenterY;
// #192 (2026-07-09): true once the player's first canonical Position has
// been accepted — i.e. _liveCenterX/Y reflects a real, server-confirmed
// position, not the Holtburg startup placeholder. Renderability is not a
// prerequisite: a partial CreateObject may carry Position before Setup.
// Gates
// streaming via StreamingReadinessGate.ShouldStream so no landblock is ever
// built around a still-guessed center in live mode. See
// AcDream.Core.World.StreamingReadinessGate for the full mechanism this
// closes (stale-centered geometry built during the InWorld-but-not-yet-
// located window, applied late once its build finished).
private bool _liveCenterKnown => _liveWorldOrigin.IsKnown;
// K-fix1 (2026-04-26): cached at startup so per-frame branches are
// single-flag reads instead of env-var lookups. True iff
// ACDREAM_LIVE=1 was set when the window came up.
@ -699,7 +693,11 @@ public sealed class GameWindow : IDisposable
// Latches true the first time chase mode becomes active. Used by
// IsLiveModeWaitingForLogin to suppress the pre-login render gate
// for subsequent fly-mode toggles.
private bool _chaseModeEverEntered;
private bool _chaseModeEverEntered
{
get => _localPlayerMode.ChaseModeEverEntered;
set => _localPlayerMode.ChaseModeEverEntered = value;
}
/// <summary>
/// Phase 6.6/6.7: server-guid → local WorldEntity lookup so
@ -2654,6 +2652,22 @@ public sealed class GameWindow : IDisposable
// gated behind ACDREAM_LIVE=1 so the default run path is unchanged.
_liveWorldOrigin.SetPlaceholder(centerX, centerY);
_liveSessionController = new AcDream.App.Net.LiveSessionController();
_streamingFrame = new AcDream.App.Streaming.StreamingFrameController(
_options.LiveMode,
_localPlayerMode,
_playerControllerSlot,
_liveSessionController,
_liveWorldOrigin,
_liveEntityNetworkUpdates!,
new AcDream.App.Streaming.FlyCameraStreamingObserverSource(
_cameraController!),
new AcDream.App.Streaming.PhysicsStreamingDungeonCellSource(
_physicsEngine),
_streamingOriginRecenter!,
_streamingController!,
new AcDream.App.Streaming.LiveProjectionRescueRebucketter(
_worldState,
_liveEntities));
var liveEffectFrame = new AcDream.App.Update.LiveEffectFrameController(
_translucencyFades,
_animationHookFrames!,
@ -2803,7 +2817,7 @@ public sealed class GameWindow : IDisposable
}
finally
{
_playerMode = false;
_localPlayerMode.ResetSession();
_playerController = null;
_playerHost = null;
_chaseCamera = null;
@ -2811,7 +2825,6 @@ public sealed class GameWindow : IDisposable
_playerMouseDeltaX = 0f;
_rmbHeld = false;
_autoRunActive = false;
_chaseModeEverEntered = false;
_lastMovementTruthOutbound = null;
_localPlayerShadow.Clear();
_spawnClaimRangeMemo = null;
@ -3836,169 +3849,9 @@ public sealed class GameWindow : IDisposable
// reuse the same timestamp for animation hooks and the later drain.
_scriptRunner?.PublishTime(frameTiming.ScriptTime);
// Phase A.1: advance the streaming controller FIRST so the initial
// landblocks are loaded into GpuWorldState before live-session
// CreateObject events drain. The earlier order (live tick first,
// streaming tick second) caused the initial CreateObject flood from
// login to land before any landblock was loaded; live projection placement
// is a no-op for unloaded landblocks, so all 40+ NPCs/weenies were
// silently dropped on the first frame and never rendered.
//
// K-fix1 (2026-04-26): skip streaming entirely while live mode is
// configured but the chase camera hasn't engaged yet — otherwise
// the orbit camera at startup centers on the hardcoded
// 0xA9B4 (Holtburg) and Holtburg landblocks render briefly
// until the player spawn arrives + auto-entry switches to chase.
//
// #106 gate-3 (2026-06-09): narrowed to PRE-LOGIN only. The original
// form also blocked the in-world window between EnterWorld and
// auto-entry, which deadlocked the spawn-ground entry hold:
// auto-entry waits for terrain under the spawn, terrain streaming
// waited for chase mode. Once the session is InWorld the player's
// landblock is known (the fly-camera else-if below was written for
// exactly this window) — stream it. Pre-login, streaming stays
// blocked, which is the hardcoded-Holtburg flash K-fix1 targeted.
// The world-geometry RENDER gate (IsLiveModeWaitingForLogin at the
// draw site) is unchanged — the pre-entry screen still shows sky
// only.
// Recenter cancellation/session-reset requests must keep converging
// even when ordinary streaming is temporarily gated off. This call is
// a no-op outside a pending teleport or session-boundary origin
// retirement transaction.
_streamingOriginRecenter?.Advance();
bool liveInWorld = _liveSessionController?.IsInWorld == true;
// #192: liveInWorld alone used to be sufficient here — but InWorld fires
// right after the login handshake, before the player's own spawn
// CreateObject (and hence the real center) has arrived. Streaming that
// ran in that gap baked landblock offsets from the still-placeholder
// _liveCenterX/Y (Holtburg), producing stale-positioned geometry applied
// late once its build finished. Require _liveCenterKnown too — see
// AcDream.Core.World.StreamingReadinessGate for the full mechanism.
if (_streamingController is not null && _cameraController is not null
&& AcDream.Core.World.StreamingReadinessGate.ShouldStream(
LiveModeEnabled, _chaseModeEverEntered, liveInWorld, _liveCenterKnown))
{
int observerCx = _liveCenterX;
int observerCy = _liveCenterY;
if (_playerMode && _playerController is not null
&& _playerController.State == AcDream.App.Input.PlayerState.PortalSpace)
{
// Teleport hold (#135): the local player position is frozen at the
// PRE-teleport spot, expressed in the OLD center frame. The
// origin coordinator commits _liveCenterX/Y to the destination
// only after old-window presentation retirement converges; the
// streaming controller remains gated until that edge. Follow
// the active shared origin directly — the stale position-derived offset
// (_liveCenterX + floor(frozenPos/192)) could land ≥2 landblocks off
// the dungeon and trip ExitDungeonExpand, re-streaming the very
// neighbor window the pre-collapse just suppressed. Correct for an
// outdoor teleport too: pre-load the destination during the hold.
//
// NOTE: these assignments equal the observerCx/Cy defaults initialized
// above — the LOAD-BEARING effect of this branch is INHIBITING the
// position-derived offset in the else-if below while the player position
// is frozen, not the (redundant) assignment. Kept explicit for clarity.
observerCx = _liveCenterX;
observerCy = _liveCenterY;
}
else if (_playerMode && _playerController is not null)
{
// Player mode: follow the physics-resolved player position.
// The player walks via the local physics engine; the server
// doesn't echo back our autonomous moves, so _lastLivePlayer*
// stays at the login position. Compute the landblock from the
// controller's current world-space position instead.
var pp = _playerController.Position;
observerCx = _liveCenterX + (int)System.Math.Floor(pp.X / 192f);
observerCy = _liveCenterY + (int)System.Math.Floor(pp.Y / 192f);
}
else if (liveInWorld)
{
// Live, not yet in player mode: the login auto-entry hold, or a live
// fly-camera spectator. Follow the PLAYER's server-known landblock; if it
// hasn't arrived yet, KEEP the _liveCenterX/_liveCenterY default — which is
// the spawn/teleport recenter (the dungeon landblock at a dungeon login).
//
// #135 regression fix (2026-06-14): this MUST NOT fall through to the
// fly-camera projection below. During a dungeon-login hold the streaming is
// pre-collapsed onto the spawn landblock; a camera-derived observer far from
// it trips ExitDungeonExpand and unloads the dungeon before it can hydrate —
// the player is never placed and login hangs with no dungeon. Previously
// The last-player-landblock hint was once set by ANY entity, so a dungeon-local NPC
// kept this branch on the dungeon; once it was filtered to the player guid
// (line ~4507), a not-yet-arrived player UP dropped to the camera branch.
// The fly camera is the OFFLINE observer only.
if (_liveEntityNetworkUpdates?.LastLivePlayerLandblockId is { } lid)
{
observerCx = (int)((lid >> 24) & 0xFFu);
observerCy = (int)((lid >> 16) & 0xFFu);
}
// else: keep the _liveCenterX/_liveCenterY default (the spawn recenter).
}
else
{
// Offline: project the fly camera's world-space position back into
// landblock coordinates. OrbitCamera doesn't expose Position via
// ICamera, but FlyCamera.Position is always updated (even when the
// orbit camera is Active), so this is safe in both modes.
// Each landblock is 192 world units wide.
var camPos = _cameraController.Fly.Position;
observerCx = _liveCenterX + (int)System.Math.Floor(camPos.X / 192f);
observerCy = _liveCenterY + (int)System.Math.Floor(camPos.Y / 192f);
}
// Dungeon gate (#133 FPS): when the player stands in a SEALED EnvCell
// (indoor cell that doesn't see outside — the same predicate that kills
// the sun/sky, playerInsideCell below), collapse streaming to the single
// dungeon landblock. AC dungeons have no adjacent landblocks; the 25×25
// window otherwise pulls in ~129 unrelated ocean-grid dungeons. Building
// interiors (cottage/inn) have SeenOutside cells, so they are NOT gated
// and keep their surrounding terrain.
// True only for a sealed indoor cell. Read the physics CurrCell's own
// SeenOutside (ObjCell.SeenOutside, set from the EnvCell dat flags) rather
// than the render registry: the registry lookup only succeeds AFTER the
// landblock FINALIZES (~tens of seconds for a 205-cell dungeon), which
// delayed the collapse and let the full 25×25 neighbor window churn in
// first (the "~30s to stabilize" report). CurrCell.SeenOutside is set the
// moment the player is placed, so the collapse now engages at the snap.
// AP-36 / #145: during a teleport HOLD the player is unplaced and CurrCell is
// the frozen SOURCE cell. Suppress the source-cell gate so a teleport OUT of a
// dungeon follows the destination (the PortalSpace observer pin above) instead
// of staying pinned to the source dungeon — see DungeonStreamingGate.
bool isTeleportHold =
_playerController is { State: AcDream.App.Input.PlayerState.PortalSpace };
var currCell = _physicsEngine.DataCache?.CellGraph.CurrCell;
bool currSealedDungeon =
currCell is AcDream.Core.World.Cells.EnvCell pcEnv && !pcEnv.SeenOutside;
var gate = AcDream.App.Streaming.DungeonStreamingGate.Compute(
isTeleportHold, currSealedDungeon, currCell?.Id ?? 0u);
bool insideDungeon = gate.InsideDungeon;
if (gate.ObserverLandblockKey is { } cellLb)
{
// Pin the collapse to the cell's OWN landblock (cell id high 16 bits),
// NOT the position-derived observer landblock. A dungeon's EnvCells sit
// at arbitrary world coords (the "ocean" placement) with negative local
// offsets, so floor(pp.Y/192) lands one landblock off — which collapses
// onto the WRONG landblock and unloads the real dungeon, nulling CurrCell
// and breaking the render (the Bug-A coordinate class). The cell id is the
// authoritative landblock.
observerCx = (int)((cellLb >> 8) & 0xFFu);
observerCy = (int)(cellLb & 0xFFu);
}
_streamingController.Tick(observerCx, observerCy, insideDungeon);
// Re-inject persistent entities rescued from unloaded landblocks
// into the current center landblock (the one the observer is in).
var rescued = _worldState.DrainRescued();
if (rescued.Count > 0)
{
uint centerLb = (uint)((observerCx << 24) | (observerCy << 16) | 0xFFFF);
foreach (var entity in rescued)
_liveEntities?.RebucketLiveEntity(entity.ServerGuid, centerLb);
}
}
// Streaming publishes before inbound CreateObject dispatch so newly
// accepted live projections can enter a resident bucket this frame.
_streamingFrame.Tick();
// Input callbacks feed the current object tick. Packets already read
// by Core.Net remain queued until the retail object/physics phase has