Implement DEBUGMAP combat frontier

This commit is contained in:
gamer147
2026-07-21 19:30:56 -04:00
parent cc6db721db
commit 6e147014ec
15 changed files with 1318 additions and 207 deletions

View File

@@ -0,0 +1,127 @@
using System;
using System.Collections.Generic;
using Age.Engine.Hosting;
using Age.Engine.Model;
using Age.Engine.Sys4;
using Age.Engine.Vm;
using Xunit;
public class BattleFrontierOpsTests
{
private const int Immediate = 0, InlineString = 2, GlobalInt = 3, GlobalString = 5,
LocalString = 11;
private static OpcodeTable Table() => OpcodeTableJson.Load(Paths.OpcodesJson);
private static Operand I(long value) => new(Immediate, value);
private static Operand G(int address) => new(GlobalInt, address);
[Fact]
public void SurfaceRectCopy_ForwardsTheCompleteBlitRequest()
{
var table = Table();
var script = ScriptAssembler.Assemble(table, "SURFACE_COPY", new List<(int, Operand[])>
{
(0x207, new[] { I(72), I(67), I(12), I(15), I(3), I(4), I(30), I(33) }),
(0x2, Array.Empty<Operand>()),
}, Array.Empty<string>());
var host = new RecordingHost();
new VirtualMachine(script, table, host).Run();
Assert.Equal(new SurfaceRectCopy(72, 67, 12, 15, 3, 4, 30, 33),
Assert.Single(host.SurfaceCopies));
}
[Fact]
public void ScalarBattleHelpers_PreserveNativeSignedBehaviorAndStringAliasing()
{
var table = Table();
var script = ScriptAssembler.Assemble(table, "BATTLE_SCALARS", new List<(int, Operand[])>
{
(0x191, new[] { G(0x100), I(int.MinValue) }),
(0x193, new[] { new Operand(GlobalString, 0x500), new Operand(GlobalString, 0x500),
new Operand(InlineString, 0) }),
(0x1c8, new[] { new Operand(LocalString, 0), I(-42) }),
(0x55, new[] { new Operand(GlobalString, 0x501), new Operand(LocalString, 0) }),
(0x2, Array.Empty<Operand>()),
}, new[] { "/99" });
var vm = new VirtualMachine(script, table, new RecordingHost());
vm.GlobalStrings[0x500] = "17";
vm.Run();
Assert.Equal(int.MinValue, vm.Globals[0x100]);
Assert.Equal("17/99", vm.GlobalStrings[0x500]);
Assert.Equal("-42", vm.GlobalStrings[0x501]);
}
[Fact]
public void MonotonicAndFrameTimeOps_StoreNativeLowDwordSamples()
{
var table = Table();
var host = new SequencedClockHost(0x1_0000_0005, 100, 116);
var script = ScriptAssembler.Assemble(table, "BATTLE_CLOCK", new List<(int, Operand[])>
{
(0xd0, new[] { G(0x100) }),
(0x23c, Array.Empty<Operand>()),
(0x23c, Array.Empty<Operand>()),
(0x2, Array.Empty<Operand>()),
}, Array.Empty<string>());
var vm = new VirtualMachine(script, table, host);
vm.Run();
Assert.Equal(5, vm.Globals[0x100]);
Assert.Equal(100u, vm.Gfx.PreviousFrameTimeMilliseconds);
Assert.Equal(116u, vm.Gfx.CurrentFrameTimeMilliseconds);
}
[Fact]
public void MovieActivityAndAnimationServiceFlags_ControlBattlePollingAndReset()
{
var table = Table();
var host = new RecordingHost();
host.ActiveMovieSurfaces.Add(7);
var script = ScriptAssembler.Assemble(table, "BATTLE_CONTROL", new List<(int, Operand[])>
{
(0x23a, new[] { G(0x100), I(7) }),
(0x23a, new[] { G(0x101), I(8) }),
(0x238, new[] { I(400) }),
(0x24e, new[] { I(2) }),
(0x243, Array.Empty<Operand>()),
(0x2, Array.Empty<Operand>()),
}, Array.Empty<string>());
var vm = new VirtualMachine(script, table, host);
vm.Run();
Assert.Equal(1, vm.Globals[0x100]);
Assert.Equal(0, vm.Globals[0x101]);
Assert.Equal(400, vm.Gfx.AnimClockDurationTicks);
Assert.Equal(1, vm.Gfx.AnimClockGeneration);
Assert.Equal(2, vm.Gfx.AnimationServiceFlags);
}
[Fact]
public void DelayedCombatVoice_ForwardsAllSchedulingOperands()
{
var table = Table();
var host = new RecordingHost();
var script = ScriptAssembler.Assemble(table, "BATTLE_VOICE", new List<(int, Operand[])>
{
(0x2c0, new[] { I(123), I(0), I(275) }),
(0x2, Array.Empty<Operand>()),
}, Array.Empty<string>());
new VirtualMachine(script, table, host).Run();
Assert.Equal((123L, 0, 275L), Assert.Single(host.ScheduledVoiceRequests));
}
private sealed class SequencedClockHost : RecordingHost
{
private readonly Queue<long> _samples;
public SequencedClockHost(params long[] samples) => _samples = new Queue<long>(samples);
public override long InputClockMilliseconds => _samples.Dequeue();
}
}

View File

@@ -0,0 +1,188 @@
using System.Collections.Generic;
using Age.Engine.Model;
using Age.Engine.Sys4;
using Age.Engine.Vm;
using Xunit;
public class IntegerQueueOpsTests
{
private const int Immediate = 0, GlobalInt = 3, LocalInt = 9;
private static Operand I(long value) => new(Immediate, value);
private static Operand G(int address) => new(GlobalInt, address);
private static Operand L(int address) => new(LocalInt, address);
[Fact]
public void IntegerQueues_AreIndependentFifosAndResetDiscardsPendingValues()
{
var table = OpcodeTableJson.Load(Paths.OpcodesJson);
var script = ScriptAssembler.Assemble(table, "INT_QUEUE", new List<(int, Operand[])>
{
(0x132, new[] { I(0) }),
(0x132, new[] { I(1) }),
(0x133, new[] { I(0), I(11) }),
(0x133, new[] { I(0), I(-22) }),
(0x133, new[] { I(1), I(33) }),
(0x134, new[] { I(0), L(0), L(1) }),
(0x55, new[] { G(0x100), L(0) }),
(0x55, new[] { G(0x101), L(1) }),
(0x134, new[] { I(0), L(0), L(1) }),
(0x55, new[] { G(0x102), L(0) }),
(0x55, new[] { G(0x103), L(1) }),
(0x134, new[] { I(1), L(0), L(1) }),
(0x55, new[] { G(0x104), L(0) }),
(0x55, new[] { G(0x105), L(1) }),
(0x55, new[] { L(1), I(777) }),
(0x134, new[] { I(0), L(0), L(1) }),
(0x55, new[] { G(0x106), L(0) }),
(0x55, new[] { G(0x107), L(1) }),
(0x133, new[] { I(1), I(44) }),
(0x132, new[] { I(1) }),
(0x134, new[] { I(1), L(0), L(1) }),
(0x55, new[] { G(0x108), L(0) }),
(0x2, System.Array.Empty<Operand>()),
}, System.Array.Empty<string>());
var vm = new VirtualMachine(script, table, new RecordingHost());
vm.Run();
Assert.Equal(1, vm.Globals[0x100]);
Assert.Equal(11, vm.Globals[0x101]);
Assert.Equal(1, vm.Globals[0x102]);
Assert.Equal(-22, vm.Globals[0x103]);
Assert.Equal(1, vm.Globals[0x104]);
Assert.Equal(33, vm.Globals[0x105]);
Assert.Equal(0, vm.Globals[0x106]);
Assert.Equal(777, vm.Globals[0x107]);
Assert.Equal(0, vm.Globals[0x108]);
}
[Fact]
public void PreloadedScriptSlot_RestartsCodeAndRetainsItsLocalBankAcrossCalls()
{
var table = OpcodeTableJson.Load(Paths.OpcodesJson);
var root = ScriptAssembler.Assemble(table, "PRELOADED_ROOT", new List<(int, Operand[])>
{
(0x6, new[] { I(0x500), I(0x1f) }),
(0x8, new[] { I(0x1f) }),
(0x8, new[] { I(0x1f) }),
(0x2, System.Array.Empty<Operand>()),
}, System.Array.Empty<string>());
var worker = ScriptAssembler.Assemble(table, "PRELOADED_WORKER", new List<(int, Operand[])>
{
(0x50, new[] { L(0), L(0), I(1) }),
(0x55, new[] { G(0x120), L(0) }),
(0x2, System.Array.Empty<Operand>()),
}, System.Array.Empty<string>());
var vm = new VirtualMachine(root, table, new RecordingHost(),
provider: new MapProvider(new Dictionary<long, Script> { [0x500] = worker }));
vm.Run();
Assert.Equal("exit", vm.HaltReason);
Assert.Equal(2, vm.Globals[0x120]);
Assert.Equal(2, vm.CallScriptDispatches);
}
[Fact]
public void RegisteredRealMvseek_ExpandsMovementCostsThroughTheSystem4ServiceAbi()
{
var table = OpcodeTableJson.Load(Paths.OpcodesJson);
var mvseek = Sys4Loader.Load(Paths.Scripts()["MVSEEK.BIN"], table);
var root = ScriptAssembler.Assemble(table, "SYSTEM4_SERVICE_BRIDGE", new List<(int, Operand[])>
{
(0x6, new[] { I(0x3381), I(0x1f) }),
(0x8, new[] { I(0x1f) }),
(0x2, System.Array.Empty<Operand>()),
}, System.Array.Empty<string>());
var vm = new VirtualMachine(root, table, new RecordingHost(),
provider: new MapProvider(new Dictionary<long, Script> { [0x3381] = mvseek }));
SeedSearchGrid(vm, movementPoints: 3);
vm.Run();
Assert.Equal("exit", vm.HaltReason);
Assert.Equal(3, vm.Globals[MovementCell(6, 5)]);
Assert.Equal(3, vm.Globals[MovementCell(4, 5)]);
Assert.Equal(3, vm.Globals[MovementCell(5, 6)]);
Assert.Equal(3, vm.Globals[MovementCell(5, 4)]);
}
[Fact]
public void RealMvseek_ExpandsMovementCostsBeyondTheOrigin()
{
var vm = RealSearchVm("MVSEEK.BIN");
SeedSearchGrid(vm, movementPoints: 3);
vm.Run();
Assert.Equal("exit", vm.HaltReason);
Assert.Equal(3, vm.Globals[MovementCell(6, 5)]);
Assert.Equal(3, vm.Globals[MovementCell(4, 5)]);
Assert.Equal(3, vm.Globals[MovementCell(5, 6)]);
Assert.Equal(3, vm.Globals[MovementCell(5, 4)]);
}
[Fact]
public void RealAtseek_ExpandsAttackDistancesBeyondTheOrigin()
{
var vm = RealSearchVm("ATSEEK.BIN");
SeedSearchGrid(vm, movementPoints: 0);
vm.Globals[0xcc9f3] = 2;
vm.Run();
Assert.Equal("exit", vm.HaltReason);
Assert.Equal(1, vm.Globals[AttackCell(6, 5)]);
Assert.Equal(1, vm.Globals[AttackCell(4, 5)]);
Assert.Equal(1, vm.Globals[AttackCell(5, 6)]);
Assert.Equal(1, vm.Globals[AttackCell(5, 4)]);
}
private static VirtualMachine RealSearchVm(string name)
{
var table = OpcodeTableJson.Load(Paths.OpcodesJson);
var script = Sys4Loader.Load(Paths.Scripts()[name], table);
return new VirtualMachine(script, table, new RecordingHost());
}
private static void SeedSearchGrid(VirtualMachine vm, int movementPoints)
{
const int entity = 0;
const int x = 5, y = 5;
vm.Globals[0x66713] = entity;
vm.Globals[0x5231f + entity] = x;
vm.Globals[0x52351 + entity] = y;
vm.Globals[0x4e11b + entity * 14 + 10] = movementPoints;
vm.Globals[0xccbcf + 1] = 1;
vm.Globals[0xccbcf + 2] = -1;
vm.Globals[0xccbcf + 3] = 0;
vm.Globals[0xccbcf + 4] = 0;
vm.Globals[0xccbd4 + 1] = 0;
vm.Globals[0xccbd4 + 2] = 0;
vm.Globals[0xccbd4 + 3] = 1;
vm.Globals[0xccbd4 + 4] = -1;
// Terrain id 1 is passable. MVSEEK checks destination centers; ATSEEK checks the
// fine-grid edge between the origin and each logical neighbor.
vm.Globals[0xe6ae0 + 1] = 1;
SetTerrain(vm, x * 2, y * 2);
SetTerrain(vm, (x + 1) * 2, y * 2);
SetTerrain(vm, (x - 1) * 2, y * 2);
SetTerrain(vm, x * 2, (y + 1) * 2);
SetTerrain(vm, x * 2, (y - 1) * 2);
SetTerrain(vm, x * 2 + 1, y * 2);
SetTerrain(vm, x * 2 - 1, y * 2);
SetTerrain(vm, x * 2, y * 2 + 1);
SetTerrain(vm, x * 2, y * 2 - 1);
}
private static void SetTerrain(VirtualMachine vm, int x, int y)
=> vm.Globals[0x341ab + y * 53 + x] = 1;
private static int MovementCell(int x, int y) => 0xaba96 + y * 27 + x;
private static int AttackCell(int x, int y) => 0xb8d86 + y * 27 + x;
}

View File

@@ -93,6 +93,25 @@ public class RenderObjectBlendTests
Assert.False(rendered.MultiplyTint);
}
[Fact]
public void CreatedSurfaceMode0_UsesPackedAlphaAsOpacityAndRgbAsModulation()
{
var g = new GfxState();
g.CreateSurface(3);
g.BindDraw(0x100, 3, 0, 0, 10, 10, 0, 0);
g.SetStaticObjectColorResolved(0x100, 0, 0x40, 0x102030);
var rendered = g.SnapshotVisibleObjects().Single();
Assert.Equal(0, rendered.SurfaceResId);
Assert.Equal(-1, rendered.ColorKey);
Assert.Equal(0x40, rendered.Alpha);
Assert.Equal(0, rendered.TintStrength);
Assert.Equal(0x102030, rendered.Tint);
Assert.True(rendered.MultiplyTint);
Assert.Equal(BlendKind.Alpha, rendered.Blend);
}
[Fact]
public void Mode1_UsesAdditiveBlendWithArgbSourceScaleAndRgbModulation()
{

View File

@@ -0,0 +1,80 @@
using Age.Engine.Sys4;
using Age.Engine.Model;
using Xunit;
public class RgbaSurfaceOpsTests
{
[Fact]
public void FillRect_ReplacesClippedPixelsIncludingAlpha()
{
var surface = Row(1, 2, 3, 4);
Assert.True(RgbaSurfaceOps.FillRect(surface, -1, 0, 3, 1, 0xd0, 0x112233));
Assert.Equal(new byte[] { 0x11, 0x22, 0x33, 0xd0 }, surface.Pixels[..4]);
Assert.Equal(new byte[] { 0x11, 0x22, 0x33, 0xd0 }, surface.Pixels[4..8]);
Assert.Equal(3, Values(surface)[2]);
Assert.Equal(4, Values(surface)[3]);
}
[Fact]
public void GeneratedSurfaceBinding_DefaultsToNoColorKey()
{
var gfx = new GfxState();
gfx.CreateSurface(67);
gfx.BindDraw(1, 67, 0, 0, 4, 1, 0, 0);
Assert.Equal(-1, Assert.Single(gfx.SnapshotVisibleObjects()).ColorKey);
}
[Fact]
public void CopyRect_ClipsSourceAndDestinationAsOnePairedRectangle()
{
var source = Row(10, 20, 30, 40);
var destination = Row(1, 2, 3, 4);
Assert.True(RgbaSurfaceOps.CopyRect(source, destination, -1, 0, 4, 1, 1, 0));
Assert.Equal(new byte[] { 1, 2, 10, 20 }, Values(destination));
}
[Fact]
public void CopyRect_UsesStableSourcePixelsForOverlappingSelfCopy()
{
var surface = Row(10, 20, 30, 40);
Assert.True(RgbaSurfaceOps.CopyRect(surface, surface, 0, 0, 3, 1, 1, 0));
Assert.Equal(new byte[] { 10, 10, 20, 30 }, Values(surface));
}
[Fact]
public void WithColorKey_MakesMatchingSourcePixelsTransparentBeforeComposition()
{
var source = new RgbaImage(2, 1, new byte[] { 1, 2, 3, 255, 4, 5, 6, 255 });
var keyed = RgbaSurfaceOps.WithColorKey(source, 0x010203);
Assert.Equal(0, keyed.Pixels[3]);
Assert.Equal(255, keyed.Pixels[7]);
Assert.Equal(255, source.Pixels[3]);
}
private static RgbaImage Row(params byte[] values)
{
var pixels = new byte[values.Length * 4];
for (int index = 0; index < values.Length; index++)
{
pixels[index * 4] = values[index];
pixels[index * 4 + 3] = 255;
}
return new RgbaImage(values.Length, 1, pixels);
}
private static byte[] Values(RgbaImage image)
{
var values = new byte[image.Width];
for (int index = 0; index < values.Length; index++) values[index] = image.Pixels[index * 4];
return values;
}
}

View File

@@ -24,6 +24,7 @@ internal class RecordingHost : IHost
public int HistoryPresentationEnds;
public readonly List<int> ClearedTextLayouts = new();
public readonly List<SurfaceRectFill> SurfaceFills = new();
public readonly List<SurfaceRectCopy> SurfaceCopies = new();
public readonly List<(long First, long Count)> PresentedRanges = new();
public readonly List<AdvWaitIndicatorConfig> WaitIndicators = new();
public readonly List<bool> WaitIndicatorEnabledChanges = new();
@@ -32,6 +33,7 @@ internal class RecordingHost : IHost
public readonly List<(long Resource, int Channel)> SfxLoads = new();
public readonly List<long> Voices = new();
public readonly List<(long Id, int PlaybackVariant)> VoiceRequests = new();
public readonly List<(long Id, int PlaybackVariant, long DelayMs)> ScheduledVoiceRequests = new();
public readonly List<long> VoiceBgmDuckControls = new();
public readonly List<int> SfxStarts = new();
public readonly List<(int Channel, int StartMode, long DelayMs)> ScheduledSfxStarts = new();
@@ -40,6 +42,7 @@ internal class RecordingHost : IHost
public readonly List<(long Resource, int Surface, long Flags, long SyncMask)> Movies = new();
public System.Action? OnPlayMovie;
public long? MovieStopTimeMs;
public readonly HashSet<int> ActiveMovieSurfaces = new();
public readonly List<(long Resource, int Surface, long Flags)> ModalMovies = new();
public readonly List<int> ClearedRenderTargets = new();
public readonly List<(int First, int Count)> ReleasedSurfaceRanges = new();
@@ -78,6 +81,7 @@ internal class RecordingHost : IHost
}
public int MessageWindowAlphaSetting { get; set; }
public void FillSurfaceRect(SurfaceRectFill fill) => SurfaceFills.Add(fill);
public void CopySurfaceRect(SurfaceRectCopy copy) => SurfaceCopies.Add(copy);
public void PresentObjectRange(GfxState gfx, long firstHandle, long count)
=> PresentedRanges.Add((firstHandle, count));
public void ConfigureAdvWaitIndicator(AdvWaitIndicatorConfig config) => WaitIndicators.Add(config);
@@ -143,6 +147,8 @@ internal class RecordingHost : IHost
VoiceRequests.Add((id, playbackVariant));
}
public void SetVoiceBgmDuckControl(long flags) => VoiceBgmDuckControls.Add(flags);
public void ScheduleVoicePlayback(long id, int playbackVariant, long delayMs)
=> ScheduledVoiceRequests.Add((id, playbackVariant, delayMs));
public void LoadSoundEffect(long resourceId, int channel) => SfxLoads.Add((resourceId, channel));
public void StartSoundEffect(int channel) => SfxStarts.Add(channel);
public void ScheduleSoundEffectStart(int channel, int startMode, long delayMs)
@@ -155,6 +161,7 @@ internal class RecordingHost : IHost
OnPlayMovie?.Invoke();
return MovieStopTimeMs;
}
public bool IsMovieSurfaceActive(int surfaceSlot) => ActiveMovieSurfaces.Contains(surfaceSlot);
public void PlayModalMovieToSurface(long rawResourceId, int surfaceSlot, long movieFlags)
=> ModalMovies.Add((rawResourceId, surfaceSlot, movieFlags));
}