242 lines
14 KiB
C#
242 lines
14 KiB
C#
using System.Buffers.Binary;
|
|
using System.Numerics;
|
|
using System.Text;
|
|
using GamePort.Assets;
|
|
using GamePort.Game;
|
|
|
|
var root = Path.GetFullPath(args.Length == 1 ? args[0] : "../Original");
|
|
var original = OriginalSave.Load(AssetPath.Resolve(root, "NewGame.tsk"));
|
|
var lab = GamLevel.Load(AssetPath.Resolve(root, "gam/level1b.gam"));
|
|
var counter = 1;
|
|
var tasks = new TaskStates(original.Tasks);
|
|
var director = new SceneDirector(lab, tasks, (_, _) => 5);
|
|
var interactions = new SceneInteractions(director, tasks, original);
|
|
director.Begin(lab.FindStart()!);
|
|
interactions.Reset();
|
|
director.Skip();
|
|
Check(tasks.Get("SCENE") == 30 && director.Controlled, "Intro cannot invent lab completion");
|
|
var explanation = lab.Entities.Single(e => e.Tag == "teleportexplanation");
|
|
director.Touch("JIM1", explanation.Position);
|
|
Check(tasks.Get("SCENE") == 35 && interactions.CurrentObjective == 1, "Original explanation enables objective1/task35");
|
|
director.Skip();
|
|
var shrinkray = director.FindActor("shrinkray")!;
|
|
var checkpoint = Vector3.Zero;
|
|
var help = -1;
|
|
var selected = -1;
|
|
var events = new List<string>();
|
|
interactions.CheckpointCaptured += p => { checkpoint = p; events.Add("checkpoint"); };
|
|
interactions.ObjectiveChanged += objective =>
|
|
{
|
|
if (objective != 2) return;
|
|
Check(tasks.Get("SCENE") == 35, "Objective callback observes old task records before assignment");
|
|
events.Add("objective2");
|
|
};
|
|
tasks.Assigned += (name, state) =>
|
|
{
|
|
if (name != "SCENE" || state != 50) return;
|
|
Check(interactions.CurrentObjective == 2 && tasks.Get(name) == 50,
|
|
"Entity assignment observers see the selected objective and new task records");
|
|
events.Add("task50");
|
|
};
|
|
interactions.PracticeRequested += page => { help = page; events.Add("help"); };
|
|
interactions.Inventory.GadgetSelected += gadget => { selected = gadget; events.Add("gadget"); };
|
|
interactions.Collected += (_, points) => events.Add("score" + points);
|
|
interactions.Update(shrinkray.Position);
|
|
Check(tasks.Get("SCENE") == 50 && interactions.Score == 50, "Shrinkray grants50 and original task35->50");
|
|
Check(interactions.Inventory.State(0, 3) == 1 && selected == 3 && help == 0, "Shrinkray inventory/help/gadget side effects");
|
|
Check(checkpoint == shrinkray.Position && !shrinkray.Visible && !shrinkray.Active, "Capture current root before hiding acquired shrinkray");
|
|
Check(events.SequenceEqual(new[] { "checkpoint", "objective2", "task50", "help", "gadget", "score50" }), "Recovered special hook ordering");
|
|
Check(interactions.Snapshot().ItemStates[301] == 1 && interactions.CurrentObjective == 2, "Acquired state and objective2 persist");
|
|
interactions.Update(shrinkray.Position);
|
|
Check(interactions.Score == 50 && events.Count == 6, "Repeated overlap cannot recollect");
|
|
Console.WriteLine("PASS source intro/explanation/shrinkray state, inventory and event ordering");
|
|
|
|
var carl = director.FindActor("C3DCARL")!;
|
|
Check(carl.Position == director.FindActor("CARLESC3")!.Position && carl.AIstate == 6, "Carl task callback positions state6 at authored marker");
|
|
var talkStarted = 0;
|
|
var taskAtSpeech = -1;
|
|
var scoreAtSpeech = -1;
|
|
director.Speech += (_, _) => { taskAtSpeech = tasks.Get("SCENE"); scoreAtSpeech = interactions.Score; };
|
|
interactions.ConversationStarted += (_, tag) => { talkStarted++; Check(tag == "neutron1a", "Authored TalkTrigger0"); };
|
|
director.SyncActor("JIM1", carl.Position, Vector3.Zero);
|
|
interactions.Update(carl.Position);
|
|
Check(taskAtSpeech == 60 && scoreAtSpeech == 150, "Carl callback runs before dialogue starts");
|
|
Check(tasks.Get("SCENE") == 60 && carl.AIstate == 9 && talkStarted == 1, "Carl ends dispatcher in state9 with scene60");
|
|
for (var i = 0; i < 10; i++) interactions.Update(carl.Position);
|
|
Check(interactions.Score == 150 && talkStarted == 1, "Talk does not repeat on contact stay");
|
|
Check(interactions.CurrentObjective == 3, "Carl transition selects objective3");
|
|
director.Skip();
|
|
LevelTransition? transition = null;
|
|
interactions.Transition += request => transition = request;
|
|
var exit = lab.Entities.Single(e => e.Tag == "LABTONEIGH");
|
|
interactions.Update(exit.Position);
|
|
Check(transition?.LevelFile == "level1.gam" && transition.StartPoint == "level1",
|
|
$"Original lab exit task60 gate opens: request={transition}; controlled={director.Controlled}; blocked={director.Blocked}; tag={director.CurrentTag}");
|
|
Console.WriteLine("PASS source Carl contact/callback/dialogue/task60 and original lab exit gate");
|
|
|
|
var snapshot = interactions.Snapshot();
|
|
var decoded = OriginalSave.Parse(snapshot.Encode());
|
|
Check(decoded.InventoryStates[3] == 1 && decoded.ItemStates[301] == 1 && decoded.Score == 150 && decoded.OtherCounter == 3,
|
|
"Original TSK serializes acquired gadget, score and objective selector");
|
|
Check(decoded.StoredVector == original.StoredVector, "Pickup42AF00 does not overwrite the separate saved transition root");
|
|
var restoredTasks = new TaskStates(decoded.Tasks);
|
|
var restoredDirector = new SceneDirector(lab, restoredTasks, (_, _) => 5);
|
|
var restored = new SceneInteractions(restoredDirector, restoredTasks, decoded);
|
|
restoredDirector.Begin(lab.FindStart()!); restored.Reset(); restoredDirector.Skip();
|
|
Check(!restoredDirector.FindActor("shrinkray")!.Active && restored.Inventory.State(0, 3) == 1, "Resume retains collected shrinkray/inventory");
|
|
restored.Update(restoredDirector.FindActor("shrinkray")!.Position);
|
|
Check(restored.Score == 150, "Resume cannot award shrinkray twice");
|
|
Console.WriteLine("PASS original-format save/resume retains gadget and objective state");
|
|
|
|
// The source permits collection at other task values, but its special shrinkray
|
|
// hook only grants the gadget when SCENE==35. Preserve that awkward boundary.
|
|
foreach (var state in new[] { 30, 36, 50 })
|
|
{
|
|
var boundaryTasks = new TaskStates(original.Tasks.Select(t => t.Name == "SCENE" ? t with { State = state } : t));
|
|
var boundaryDirector = new SceneDirector(lab, boundaryTasks, (_, _) => 1);
|
|
var boundary = new SceneInteractions(boundaryDirector, boundaryTasks, original);
|
|
boundary.Update(boundaryDirector.FindActor("shrinkray")!.Position);
|
|
Check(boundaryTasks.Get("SCENE") == state && boundary.Inventory.State(0, 3) == 0 && boundary.Score == 50,
|
|
"Shrinkray special effect exact task35 gate, ordinary score remains");
|
|
}
|
|
Console.WriteLine("PASS exact shrinkray task gate without invented progression");
|
|
|
|
var body = Entity("3JIM", "jim");
|
|
var camera0 = Entity("3CAM", "first", ("CameraTarget", "jim"), ("SoundDatabase", "voice"), ("SoundIndex", 1));
|
|
var camera1 = Entity("3CAM", "fallback", ("CameraTarget", "jim"), ("SoundDatabase", "voice"), ("SoundIndex", 2));
|
|
foreach (var talkState in new[] { -1, -2, 50, 49 })
|
|
{
|
|
var friend = Entity("3CAR", "friend", ("AIState", 6), ("TargetName", "jim"), ("TaskName", "SCENE"),
|
|
("TalkState0", talkState), ("TalkTrigger0", "missing"),
|
|
("TalkState1", talkState), ("TalkTrigger1", "first"),
|
|
("TalkState2", -2), ("TalkTrigger2", "fallback"));
|
|
var testTasks = new TaskStates([new("SCENE", 50)]);
|
|
var testDirector = new SceneDirector(new("TEST", [body, friend, camera0, camera1]), testTasks, (_, _) => 5);
|
|
var test = new SceneInteractions(testDirector, testTasks, original);
|
|
test.Update(Vector3.Zero);
|
|
Check(testDirector.CurrentTag == (talkState is -2 or 50 ? "first" : "fallback"), "First existing match/wildcard; -1 disabled, missing target skipped");
|
|
Check(testTasks.Get("SCENE") == 60 && test.Score == 100, "Selected trigger alone invokes Carl callback");
|
|
}
|
|
var noneFriend = Entity("3CAR", "friend", ("AIState", 6), ("TargetName", "jim"), ("TaskName", "SCENE"),
|
|
("TalkState0", 49), ("TalkTrigger0", "first"));
|
|
var noTasks = new TaskStates([new("SCENE", 50)]);
|
|
var noDirector = new SceneDirector(new("TEST", [body, noneFriend, camera0]), noTasks, (_, _) => 5);
|
|
var noTalk = new SceneInteractions(noDirector, noTasks, original);
|
|
noTalk.Update(Vector3.Zero);
|
|
Check(noTasks.Get("SCENE") == 50 && noTalk.Score == 0 && noDirector.FindActor("friend")!.AIstate == 0,
|
|
"No matching talk target leaves state0 without award/task mutation");
|
|
Console.WriteLine("PASS talk lookup order, missing/disabled/wildcard states and no-match behavior");
|
|
|
|
foreach (var distance in new[] { 160f, 159.5f })
|
|
{
|
|
var friend = Entity("3CAR", "friend", ("AIState", 6), ("TargetName", "jim"), ("TaskName", "SCENE"),
|
|
("TalkState0", 50), ("TalkTrigger0", "first"));
|
|
var edgeTasks = new TaskStates([new("SCENE", 50)]);
|
|
var edgeDirector = new SceneDirector(new("TEST", [body, friend, camera0]), edgeTasks, (_, _) => 5);
|
|
var edge = new SceneInteractions(edgeDirector, edgeTasks, original);
|
|
edge.Update(new(distance, 0, 0));
|
|
Check(edge.Score == (distance < 160 ? 100 : 0), "Strict original sum-radius Carl contact boundary");
|
|
}
|
|
var waiting = new SceneDirector(lab, new TaskStates(original.Tasks), (_, _) => 5);
|
|
waiting.Begin(lab.FindStart()!);
|
|
var locked = new SceneInteractions(waiting, new TaskStates(original.Tasks), original);
|
|
locked.Update(waiting.FindActor("shrinkray")!.Position);
|
|
Check(locked.Score == 0 && locked.Snapshot().ItemStates[301] == 0, "Noncontrolled intro cannot collect pickups");
|
|
Console.WriteLine("PASS strict recovered Carl radius boundary and active-player control gate");
|
|
|
|
var inventoryRaw = Enumerable.Range(0, 19).Select(i => i == 18 ? int.MinValue : i * 10).ToArray();
|
|
var inventory = new SceneInventory(inventoryRaw);
|
|
inventoryRaw[3] = 999;
|
|
Check(inventory.State(0, 3) == 30 && inventory.State(1, 9) == int.MinValue, "Owned inventory copy preserves unknown signed wire values");
|
|
var inventoryCopy = inventory.Snapshot(); inventoryCopy[3] = 888;
|
|
Check(inventory.State(0, 3) == 30, "Snapshot owns its storage");
|
|
Throws(() => inventory.State(2, 0)); Throws(() => inventory.State(0, 9));
|
|
inventory.SetState(0, 3, 0); Throws(() => inventory.SelectGadget(3));
|
|
inventory.SetState(0, 3, 1); inventory.SelectGadget(3); Check(inventory.SelectedGadget == 3, "Owned gadget selection");
|
|
Console.WriteLine("PASS inventory bounds, ownership, unknown-state preservation and gadget availability");
|
|
|
|
foreach (var state in new[] { int.MinValue, -1, 0, 1, 2, 3, 4, 5, int.MaxValue })
|
|
{
|
|
foreach (var tutorial in Enumerable.Range(1, 8).Select(i => "VR0" + i))
|
|
{
|
|
var availability = new SceneInventory(Enumerable.Repeat(state, 19));
|
|
availability.ApplyLevelAvailability(tutorial);
|
|
var disabled = state is 1 or 4 ? 4 : state;
|
|
Check(Enumerable.Range(0, 8).All(i => availability.State(0, i) == disabled),
|
|
"Tutorial PostLoad only disables owned states1/4");
|
|
Check(availability.State(0, 8) == state && Enumerable.Range(0, 10).All(i => availability.State(1, i) == state),
|
|
"PostLoad availability preserves gadget8 and the independent inventory group");
|
|
var carried = OriginalSave.Parse((original with { InventoryStates = availability.Snapshot() }).Encode());
|
|
var returned = new SceneInventory(carried.InventoryStates);
|
|
returned.ApplyLevelAvailability("LV1B");
|
|
Check(Enumerable.Range(0, 8).All(i => returned.State(0, i) == (state is 1 or 4 ? 1 : state)),
|
|
"Serialized tutorial state returns to enabled owned gadgets in lab");
|
|
}
|
|
}
|
|
foreach (var normal in new[] { "LV1B", "LEV1", "VR00", "VR09", "vr04" })
|
|
{
|
|
var availability = new SceneInventory(Enumerable.Repeat(4, 19));
|
|
availability.ApplyLevelAvailability(normal);
|
|
Check(availability.State(0, 3) == 1 && availability.State(0, 8) == 4,
|
|
"Normal/fourcc-boundary PostLoad enables selected owned gadgets only");
|
|
}
|
|
Check(JetpackBurst.CanStart("VR04", -1, 4, 20), "Tutorial jetpack bypass still works with disabled inventory state4");
|
|
Console.WriteLine("PASS source PostLoad gadget availability, tutorial boundaries and lab RETURN round-trip");
|
|
|
|
// Independently recover the original SCENE setter's two dispatch tables rather
|
|
// than testing the port's switch against another manually copied switch.
|
|
var exe = File.ReadAllBytes(AssetPath.Resolve(root, "Neutron.exe"));
|
|
foreach (var (address, expected) in new (int, string)[] {
|
|
(0x435fcb, "e800060000"), //Contact invokes special hook before score.
|
|
(0x4365fa, "83f823"), //Exact task35.
|
|
(0x43663e, "6a016a036a00"), //Inventory group0,item3,state1.
|
|
(0x436649, "6a00e84009fdff"), //Help page0.
|
|
(0x43667c, "6a03ff9274010000"), //Jimmy gadget3.
|
|
(0x413e2f, "680000c842"), //Carl radius100.
|
|
(0x40a676, "83be0806000006"), //Carl inherited contact requires state6.
|
|
(0x41b7cd, "ff9280010000"), //Derived award before selected trigger.
|
|
(0x41b813, "6a64e8a6f50000"), //Base friend awards100.
|
|
(0x41b7f0, "c786c806000009000000"), //Talk dispatcher ends in state9.
|
|
(0x458880, "8b442404898194040000c20400"), //LOAD task hook only stores value.
|
|
})
|
|
Check(exe.AsSpan(address - 0x400000, expected.Length / 2).SequenceEqual(Convert.FromHexString(expected)),
|
|
"Original interaction instruction evidence at" + address.ToString("X"));
|
|
int U32(int address) => BinaryPrimitives.ReadInt32LittleEndian(exe.AsSpan(address - 0x400000, 4));
|
|
int ExpectedObjective(int state)
|
|
{
|
|
int target;
|
|
if (state is >= 35 and <= 280) target = U32(0x45fbf0 + 4 * exe[0x45fc48 + state - 35 - 0x400000]);
|
|
else if (state == 290) target = 0x45fab6;
|
|
else if (state is >= 300 and <= 550) target = U32(0x45fd40 + 4 * exe[0x45fd98 + state - 300 - 0x400000]);
|
|
else return 0;
|
|
var offset = target - 0x400000;
|
|
return exe[offset] == 0x6a && exe[offset + 1] == 1 && exe[offset + 2] == 0x6a ? exe[offset + 3] : 0;
|
|
}
|
|
for (var state = -1; state <= 600; state++)
|
|
Check(SceneInteractions.ObjectiveForScene(state) == ExpectedObjective(state), "Objective map agrees with original jump tables at state" + state);
|
|
Console.WriteLine("PASS all SCENE objective milestones against original executable dispatch tables");
|
|
|
|
void Check(bool condition, string message) { if (!condition) throw new Exception(message); }
|
|
void Throws(Action action)
|
|
{
|
|
try { action(); } catch (ArgumentOutOfRangeException) { return; }
|
|
throw new Exception("Expected bounded inventory rejection");
|
|
}
|
|
GamEntity Entity(string kind, string tag, params (string Name, object Value)[] properties)
|
|
{
|
|
var values = new Dictionary<string, GamProperty> { ["ObjectTag"] = new("ObjectTag", 1, Encoding.Latin1.GetBytes(tag)) };
|
|
foreach (var (name, value) in properties)
|
|
{
|
|
if (value is string text) values[name] = new(name, 1, Encoding.Latin1.GetBytes(text));
|
|
else
|
|
{
|
|
var bytes = new byte[4];
|
|
BinaryPrimitives.WriteInt32BigEndian(bytes, value is float number ? BitConverter.SingleToInt32Bits(number) : (int)value);
|
|
values[name] = new(name, value is float ? 3u : 6u, bytes);
|
|
}
|
|
}
|
|
// Distinct source offsets retain duplicate-tag contacts independently.
|
|
return new(kind, counter++ * 100, values, []);
|
|
}
|
|
|