Ports retail's ParticleEmitterInfo / Particle::Init / Particle::Update
(0x005170d0..0x0051d400) and PhysicsScript runtime to a C# data-layer
plus a Silk.NET billboard renderer. Sky-PES path is debug-only behind
ACDREAM_ENABLE_SKY_PES because named-retail decomp confirms GameSky
copies SkyObject.pes_id but never reads it (CreateDeletePhysicsObjects
0x005073c0, MakeObject 0x00506ee0, UseTime 0x005075b0).
Post-review fixes folded into this commit:
H1: AttachLocal (is_parent_local=1) follows live parent each frame.
ParticleSystem.UpdateEmitterAnchor + ParticleHookSink.UpdateEntityAnchor
let the owning subsystem refresh AnchorPos every tick — matches
ParticleEmitter::UpdateParticles 0x0051d2d4 which re-reads the live
parent frame when is_parent_local != 0. Drops the renderer-side
cameraOffset hack that only worked when the parent was the camera.
H3: Strip the long stale comment in GfxObjMesh.cs that contradicted the
retail-faithful (1 - translucency) opacity formula. The code was
right; the comment was a leftover from an earlier hypothesis and
would have invited a wrong "fix".
M1: SkyRenderer tracks textures whose wrap mode it set to ClampToEdge
and restores them to Repeat at end-of-pass, so non-sky renderers
that share the GL handle can't silently inherit clamped wrap state.
M2: Post-scene Z-offset (-120m) only fires when the SkyObject is
weather-flagged AND bit 0x08 is clear, matching retail
GameSky::UpdatePosition 0x00506dd0. The old code applied it to
every post-scene object — a no-op today (every Dereth post-scene
entry happens to be weather-flagged) but a future post-scene-only
sun rim would have been pushed below the camera.
M4: ParticleSystem.EmitterDied event lets ParticleHookSink prune dead
handles from the per-entity tracking dictionaries, fixing a slow
leak where naturally-expired emitters' handles stayed in the
ConcurrentBag forever during long sessions.
M5: SkyPesEntityId moves the post-scene flag bit to 0x08000000 so it
can't ever overlap the object-index range. Synthetic IDs stay in
the reserved 0xFxxxxxxx space.
New tests (ParticleSystemTests + ParticleHookSinkTests):
- UpdateEmitterAnchor_AttachLocal_ParticlePositionFollowsLiveAnchor
- UpdateEmitterAnchor_AttachLocalCleared_ParticleFrozenAtSpawnOrigin
- EmitterDied_FiresOncePerHandle_AfterAllParticlesExpire
- Birthrate_PerSec_EmitsOnePerTickWhenIntervalElapsed (retail-faithful
single-emit-per-frame behavior)
- UpdateEntityAnchor_WithAttachLocal_MovesParticleToLiveAnchor
- EmitterDied_PrunesPerEntityHandleTracking
dotnet build green, dotnet test green: 695 / 393 / 243 = 1331 passed
(up from 1325).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
234 lines
8.5 KiB
C#
234 lines
8.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Numerics;
|
|
using AcDream.Core.Physics;
|
|
using AcDream.Core.Vfx;
|
|
using DatReaderWriter;
|
|
using DatReaderWriter.Types;
|
|
using Xunit;
|
|
using DatPhysicsScript = DatReaderWriter.DBObjs.PhysicsScript;
|
|
|
|
namespace AcDream.Core.Tests.Vfx;
|
|
|
|
public sealed class PhysicsScriptRunnerTests
|
|
{
|
|
/// <summary>
|
|
/// Recording sink so tests can assert each hook dispatch.
|
|
/// </summary>
|
|
private sealed class RecordingSink : IAnimationHookSink
|
|
{
|
|
public List<(uint EntityId, Vector3 Pos, AnimationHook Hook)> Calls = new();
|
|
public void OnHook(uint entityId, Vector3 worldPos, AnimationHook hook)
|
|
=> Calls.Add((entityId, worldPos, hook));
|
|
}
|
|
|
|
private static DatPhysicsScript BuildScript(params (double time, AnimationHook hook)[] items)
|
|
{
|
|
var script = new DatPhysicsScript();
|
|
foreach (var (t, h) in items)
|
|
script.ScriptData.Add(new PhysicsScriptData { StartTime = t, Hook = h });
|
|
return script;
|
|
}
|
|
|
|
private static CreateParticleHook CreateHook(uint emitterInfoId)
|
|
=> new CreateParticleHook { EmitterInfoId = emitterInfoId };
|
|
|
|
private static PhysicsScriptRunner MakeRunner(RecordingSink sink, params (uint id, DatPhysicsScript script)[] scripts)
|
|
{
|
|
// Build an in-memory resolver from the script table — no DatCollection needed.
|
|
var table = new Dictionary<uint, DatPhysicsScript>();
|
|
foreach (var (id, s) in scripts) table[id] = s;
|
|
return new PhysicsScriptRunner(
|
|
id => table.TryGetValue(id, out var s) ? s : null,
|
|
sink);
|
|
}
|
|
|
|
[Fact]
|
|
public void Play_UnknownScript_ReturnsFalse()
|
|
{
|
|
var sink = new RecordingSink();
|
|
var runner = MakeRunner(sink); // no scripts registered
|
|
Assert.False(runner.Play(0xDEADBEEF, entityId: 1, anchorWorldPos: Vector3.Zero));
|
|
Assert.Empty(sink.Calls);
|
|
}
|
|
|
|
[Fact]
|
|
public void Play_ZeroScriptId_IgnoredSilently()
|
|
{
|
|
var sink = new RecordingSink();
|
|
var runner = MakeRunner(sink);
|
|
Assert.False(runner.Play(0, entityId: 1, anchorWorldPos: Vector3.Zero));
|
|
Assert.Equal(0, runner.ActiveScriptCount);
|
|
}
|
|
|
|
[Fact]
|
|
public void HooksFire_InOrder_AtScheduledTimes()
|
|
{
|
|
var script = BuildScript(
|
|
(0.0, CreateHook(100)),
|
|
(0.5, CreateHook(101)),
|
|
(1.0, CreateHook(102)));
|
|
|
|
var sink = new RecordingSink();
|
|
var runner = MakeRunner(sink, (0xAAu, script));
|
|
runner.Play(scriptId: 0xAA, entityId: 0x7, anchorWorldPos: new Vector3(1, 2, 3));
|
|
|
|
runner.Tick(0.25f);
|
|
Assert.Single(sink.Calls);
|
|
Assert.Equal(100u, ((CreateParticleHook)sink.Calls[0].Hook).EmitterInfoId.DataId);
|
|
|
|
runner.Tick(0.35f); // total 0.6
|
|
Assert.Equal(2, sink.Calls.Count);
|
|
Assert.Equal(101u, ((CreateParticleHook)sink.Calls[1].Hook).EmitterInfoId.DataId);
|
|
|
|
runner.Tick(0.9f); // total 1.5
|
|
Assert.Equal(3, sink.Calls.Count);
|
|
Assert.Equal(102u, ((CreateParticleHook)sink.Calls[2].Hook).EmitterInfoId.DataId);
|
|
Assert.Equal(0, runner.ActiveScriptCount); // fully consumed
|
|
}
|
|
|
|
[Fact]
|
|
public void EntityIdAndAnchor_ArePassedThrough()
|
|
{
|
|
var script = BuildScript((0.0, CreateHook(1)));
|
|
var sink = new RecordingSink();
|
|
var runner = MakeRunner(sink, (0xAAu, script));
|
|
|
|
var anchor = new Vector3(123, 45, 67);
|
|
runner.Play(scriptId: 0xAA, entityId: 0xCAFE, anchorWorldPos: anchor);
|
|
runner.Tick(0.1f);
|
|
|
|
Assert.Single(sink.Calls);
|
|
Assert.Equal(0xCAFEu, sink.Calls[0].EntityId);
|
|
Assert.Equal(anchor, sink.Calls[0].Pos);
|
|
}
|
|
|
|
[Fact]
|
|
public void Replay_SameScriptSameEntity_Replaces_DoesNotStack()
|
|
{
|
|
var script = BuildScript(
|
|
(0.0, CreateHook(1)),
|
|
(1.0, CreateHook(2)));
|
|
|
|
var sink = new RecordingSink();
|
|
var runner = MakeRunner(sink, (0xAAu, script));
|
|
|
|
runner.Play(scriptId: 0xAA, entityId: 1, anchorWorldPos: Vector3.Zero);
|
|
runner.Tick(0.1f);
|
|
Assert.Single(sink.Calls);
|
|
|
|
// Re-play — the old instance should be replaced, not stacked.
|
|
runner.Play(scriptId: 0xAA, entityId: 1, anchorWorldPos: Vector3.Zero);
|
|
Assert.Equal(1, runner.ActiveScriptCount);
|
|
runner.Tick(0.1f);
|
|
Assert.Equal(2, sink.Calls.Count);
|
|
// Hook 0 fires AGAIN (fresh timeline from t=0), not hook 1.
|
|
Assert.Equal(1u, ((CreateParticleHook)sink.Calls[1].Hook).EmitterInfoId.DataId);
|
|
}
|
|
|
|
[Fact]
|
|
public void Replay_DifferentEntities_BothActiveConcurrently()
|
|
{
|
|
var script = BuildScript((0.0, CreateHook(42)));
|
|
var sink = new RecordingSink();
|
|
var runner = MakeRunner(sink, (0xAAu, script));
|
|
|
|
runner.Play(scriptId: 0xAA, entityId: 0x1, anchorWorldPos: new Vector3(1, 0, 0));
|
|
runner.Play(scriptId: 0xAA, entityId: 0x2, anchorWorldPos: new Vector3(2, 0, 0));
|
|
Assert.Equal(2, runner.ActiveScriptCount);
|
|
|
|
runner.Tick(0.1f);
|
|
Assert.Equal(2, sink.Calls.Count);
|
|
Assert.Contains(sink.Calls, c => c.EntityId == 1u);
|
|
Assert.Contains(sink.Calls, c => c.EntityId == 2u);
|
|
}
|
|
|
|
[Fact]
|
|
public void StopAllForEntity_CancelsEntityScripts_LeavesOthers()
|
|
{
|
|
var script = BuildScript(
|
|
(0.0, CreateHook(1)),
|
|
(1.0, CreateHook(2)));
|
|
|
|
var sink = new RecordingSink();
|
|
var runner = MakeRunner(sink, (0xAAu, script));
|
|
|
|
runner.Play(scriptId: 0xAA, entityId: 1, anchorWorldPos: Vector3.Zero);
|
|
runner.Play(scriptId: 0xAA, entityId: 2, anchorWorldPos: Vector3.Zero);
|
|
runner.Tick(0.1f); // both fire hook 0
|
|
Assert.Equal(2, sink.Calls.Count);
|
|
|
|
runner.StopAllForEntity(1);
|
|
Assert.Equal(1, runner.ActiveScriptCount);
|
|
runner.Tick(2.0f); // only entity 2's script should fire hook 1
|
|
Assert.Equal(3, sink.Calls.Count);
|
|
Assert.Equal(2u, sink.Calls[^1].EntityId);
|
|
}
|
|
|
|
[Fact]
|
|
public void CallPES_NestedScript_SpawnsOnSameEntity()
|
|
{
|
|
var outer = BuildScript((0.0, new CallPESHook { PES = 0xBB, Pause = 0f }));
|
|
var inner = BuildScript((0.0, CreateHook(99)));
|
|
|
|
var sink = new RecordingSink();
|
|
var runner = MakeRunner(sink, (0xAAu, outer), (0xBBu, inner));
|
|
runner.Play(scriptId: 0xAA, entityId: 0x7, anchorWorldPos: new Vector3(1, 2, 3));
|
|
|
|
// First tick fires the CallPES hook. Inner script gets queued to
|
|
// _active but does NOT fire this tick (we iterate _active
|
|
// backwards, and the inner is appended AFTER the current index) —
|
|
// matches retail's linked-list insertion semantics. Inner fires
|
|
// on the NEXT tick instead.
|
|
runner.Tick(0.1f);
|
|
Assert.Empty(sink.Calls); // CallPES handled inline, no direct sink hit
|
|
Assert.Equal(1, runner.ActiveScriptCount); // inner is queued, outer done
|
|
|
|
// Second tick — inner's hook at t=0 fires now.
|
|
runner.Tick(0.1f);
|
|
Assert.Single(sink.Calls);
|
|
Assert.Equal(99u, ((CreateParticleHook)sink.Calls[0].Hook).EmitterInfoId.DataId);
|
|
Assert.Equal(0x7u, sink.Calls[0].EntityId);
|
|
}
|
|
|
|
[Fact]
|
|
public void CallPES_WithPause_DelaysSubScript()
|
|
{
|
|
var outer = BuildScript((0.0, new CallPESHook { PES = 0xBB, Pause = 0.5f }));
|
|
var inner = BuildScript((0.0, CreateHook(99)));
|
|
|
|
var sink = new RecordingSink();
|
|
var runner = MakeRunner(sink, (0xAAu, outer), (0xBBu, inner));
|
|
runner.Play(scriptId: 0xAA, entityId: 0x7, anchorWorldPos: Vector3.Zero);
|
|
|
|
// CallPES fires immediately, but inner script's hook is gated by Pause.
|
|
runner.Tick(0.1f);
|
|
Assert.Empty(sink.Calls); // inner hook waiting on Pause=0.5s
|
|
|
|
runner.Tick(0.5f); // total 0.6 > 0.5 pause
|
|
Assert.Single(sink.Calls);
|
|
}
|
|
|
|
[Fact]
|
|
public void CallPES_SelfLoopWithPause_DoesNotReplaceCurrentInstance()
|
|
{
|
|
var script = BuildScript(
|
|
(0.0, new CallPESHook { PES = 0xAA, Pause = 30f }),
|
|
(0.0, CreateHook(123)));
|
|
|
|
var sink = new RecordingSink();
|
|
var runner = MakeRunner(sink, (0xAAu, script));
|
|
runner.Play(scriptId: 0xAA, entityId: 0x7, anchorWorldPos: Vector3.Zero);
|
|
|
|
runner.Tick(0.1f);
|
|
|
|
Assert.Single(sink.Calls);
|
|
Assert.Equal(123u, ((CreateParticleHook)sink.Calls[0].Hook).EmitterInfoId.DataId);
|
|
Assert.Equal(1, runner.ActiveScriptCount);
|
|
|
|
runner.Tick(29.8f);
|
|
Assert.Single(sink.Calls);
|
|
|
|
runner.Tick(0.3f);
|
|
Assert.Equal(2, sink.Calls.Count);
|
|
}
|
|
}
|