using System.Numerics; using System.Buffers.Binary; using System.Text; using GamePort.Assets; using GamePort.Game; using GamePort.Application; void Check(bool value, string message) { if (!value) throw new Exception(message); } var count = 0; void Test(string name, Action action) { action(); count++; Console.WriteLine($"PASS {name}"); } void Charge(InvisibleGadget gadget, float energy = 20, bool exemption = false) { for (var i = 0; i < 450 && gadget.Recharge != 7; i++) gadget.Step(1f / 60, true, false, false, energy, exemption, _ => throw new Exception("Recharge spends no energy")); Check(gadget.Recharge == 7, "ready clamp must reach exact7"); } InvisibleGadget Ready(float energy = 20, bool exemption = false) { var gadget = new InvisibleGadget(); Check(gadget.Equip(energy, exemption), "equip"); Charge(gadget, energy, exemption); return gadget; } Test("energy low warning uses19/20 hysteresis and preserves callback order", () => { var energy = new PlayerEnergy(); var sounds = new List(); energy.Adjust(-1, "LV1B", sounds.Add); Check(energy.Value == 19 && sounds.Count == 0, "exact19 is not low"); energy.Adjust(-.5f, "LV1B", id => { Check(energy.Value == 18.5f && !energy.LowWarning, "low callback after scalar/before latch"); sounds.Add(id); }); energy.Adjust(-1, "LV1B", sounds.Add); Check(sounds.SequenceEqual(new[] {182}), "warn once per low episode"); energy.Value = 20; energy.Adjust(0, "LV1B"); Check(energy.LowWarning, "exact20 keeps latch"); energy.Value = MathF.BitIncrement(20); energy.Adjust(0, "LV1B"); Check(!energy.LowWarning, "above20 clears"); energy.Adjust(-2, "LV1B", sounds.Add); Check(sounds.SequenceEqual(new[] {182,182}), "renewed episode warns"); }); Test("empty crossing sounds before write, then low warning; continuing empty does not repeat", () => { var energy = new PlayerEnergy { Value = 1 }; var seen = new List(); energy.Adjust(-2, "LEV1", id => { seen.Add(id); Check(id == 80 ? energy.Value == 1 : energy.Value == 0, "source write order"); }); Check(seen.SequenceEqual(new[] {80,182}) && energy.Value == 0, "crossing then low sound"); energy.Adjust(-2, "LEV1", seen.Add); Check(seen.Count == 2, "empty is not another crossing"); energy.Value = 200; energy.Adjust(0, "LEV1"); Check(energy.Value == 100 && !energy.LowWarning, "upper clamp clears latch"); }); Test("tutorial energy helper bypasses scalar, sounds and warning latch", () => { var energy = new PlayerEnergy { Value = 1 }; energy.Adjust(0, "LV1B"); for (var i = 1; i <= 8; i++) { energy.Adjust(-100, $"VR0{i}", _ => throw new Exception("tutorial sound")); Check(energy.Value == 1 && energy.LowWarning, "tutorial must not clear retained low latch"); } Check(!PlayerEnergy.IsTutorial("VR09") && !PlayerEnergy.IsTutorial("vr03"), "exact numeric FourCC range"); }); Test("energy latch is retained separately from original saved scalar", () => { var state = new PlayerEnergy { Value = 1 }; state.Adjust(0, "LV1B"); var world = new Mesh([new(new(-1000,0,-1000),Vector2.Zero,Vector3.One),new(new(1000,0,-1000),Vector2.Zero,Vector3.One), new(new(1000,0,1000),Vector2.Zero,Vector3.One),new(new(-1000,0,1000),Vector2.Zero,Vector3.One)],[0,2,1,0,3,2]); var standing = AseMesh.Load("../Original/ASE/jimstop.ase"); var first = new LabSimulation(new SceneCollision(world),standing,new(0,80,0),0,energy:state); var second = new LabSimulation(new SceneCollision(world),standing,new(0,80,0),0,energy:state) {LevelCode="LEV1"}; var sounds = 0; second.EnergySound += _ => sounds++; second.Energy = 2; second.AdjustEnergy(-.5f); Check(first.Energy == 1.5f && second.LowEnergyWarning && sounds == 0, "ordinary load shares scalar/latch owner"); second.Energy = 100; Check(second.LowEnergyWarning, "raw save/debug scalar assignment is not helper rearm"); }); Test("Invisible equip gates zero energy but tutorial override permits it", () => { var device = new InvisibleGadget(); Check(!device.Equip(0,false) && !device.Equipped, "ordinary empty equip fails"); Check(device.Equip(0,true) && device.Recharge == 0 && device.Remaining == 0 && device.Targetable, "VR override equips with fresh clocks"); }); Test("held fire cannot activate on same invocation that recharge clamps7", () => { var device = new InvisibleGadget(); device.Equip(20,false); var spends=0; while (device.Recharge != 7) device.Step(.5f,true,true,false,20,false,_=>spends++); Check(spends == 0 && device.Targetable && device.MeterReady, "first charge before first activation"); device.Step(.5f,true,true,false,20,false,delta=>{Check(delta==-2,"fixed cost");spends++;}); Check(spends==1 && device.Remaining==6.5f && device.Recharge==0 && !device.Targetable && device.Opacity==.7f, "activation decrements remaining in same invocation, not a new-frame7"); }); Test("activation needs strictly more than2; primary with jump does not activate", () => { var device=Ready(2); var spends=0; device.Step(.1f,true,true,false,2,false,_=>spends++); Check(!device.Active && device.Recharge==7 && spends==0,"exact2 rejected"); device.Step(.1f,true,true,true,3,false,_=>spends++); Check(!device.Active,"jump blocks activation only"); device.Step(.1f,true,true,false,MathF.BitIncrement(2),false,_=>spends++); Check(device.Active && spends==1,"strict threshold"); device.Step(.1f,true,false,true,0,false,_=>spends++); Check(device.Remaining<6.9f,"active continues during jump/empty energy"); }); Test("raw NULL ownership and primary suppressor freeze timers independently", () => { var device=Ready(); device.Step(.1f,true,true,false,20,false,_=>{}); var before=device.Remaining; device.Step(.25f,false,false,false,20,false,_=>{}); Check(device.Remaining==before && !device.MeterVisible && device.Active,"null freezes, no cleanup"); device.Step(.25f,true,true,false,20,false,_=>{},suppressed:true); Check(device.Remaining==before && device.MeterVisible,"suppressed primary returns"); device.Step(.25f,true,true,true,20,false,_=>{},suppressed:true); Check(device.Remaining { var device=Ready(); device.Step(0,true,true,false,20,false,_=>{}); for(var i=0;i<8;i++) device.Step(.5f,true,false,false,18,false,_=>{}); Check(device.Remaining==3 && device.ConventionalBlend,"trunc(3*5) odd conventionalalpha"); device.Step(.25f,true,false,false,18,false,_=>{}); Check(device.Remaining==2.75f && device.ConventionalBlend,"odd13"); device.Step(.25f,true,false,false,18,false,_=>{}); Check(device.Remaining==2.5f && !device.ConventionalBlend,"even12 additive"); for(var i=0;i<5;i++) device.Step(.5f,true,false,false,18,false,_=>{}); Check(!device.Active && device.Targetable && device.Opacity==1 && device.Recharge==0 && !device.MeterReady,"expiry invocation does not recharge"); device.Step(.1f,true,false,false,18,false,_=>{}); Check(device.Recharge==.1f,"next invocation starts recharge"); }); Test("held primary reactivates after recharge without adding edge latch", () => { var device=Ready(); var spends=0; for(var i=0;i<35;i++) device.Step(.5f,true,true,false,20,false,_=>spends++); Check(spends==2 && device.Activations==2,"repeat held activation after active+recharge"); }); Test("release and reset own material/target/meter cleanup", () => { var device=Ready(); device.Step(.1f,true,true,false,20,false,_=>{}); device.Release(); Check(!device.Equipped && !device.Active && device.Remaining==7 && device.Recharge==0 && device.Targetable && !device.MeterVisible && device.Opacity==1, "source release stores7, not active restart"); device.Reset(); Check(device.Remaining==0 && device.Activations==0,"modern reset starts clean"); }); Test("original atlas material changes leave geometry and authored visibility intact", () => { var device=Ready(); var draw=new MeshDraw(0,3,1) {Visible=false}; device.Step(0,true,true,false,20,false,_=>{}); PlayerGadgetMaterial.Apply(draw,device); Check(draw.Opacity==.7f && draw.Blend==MeshBlend.Additive && !draw.Visible,"source alpha.7, additive branch, no show/hide invention"); for(var i=0;i<8;i++)device.Step(.5f,true,false,false,18,false,_=>{}); PlayerGadgetMaterial.Apply(draw,device); Check(draw.Blend==MeshBlend.Alpha,"flash routes indexed material alpha"); device.Release(); PlayerGadgetMaterial.Apply(draw,device); Check(draw.Opacity==1 && draw.Blend==MeshBlend.Opaque,"cleanup restores opaque"); }); Test("binding distinguishes another control owner, AI NULL freeze and named camera cleanup", () => { var jim=Entity("3JIM","JIM1"); var dog=Entity("3DOG","C3DGODDARD",("AIState",1)); var other=Entity("3AIT","other",("PlayerControlled","C3DGODDARD")); var freeze=Entity("3AIT","freeze",("PlayerControlled","NULL")); var keep=Entity("3CAM","keep",("DeactivateInv",0),("CameraTarget","JIM1"),("SoundIndex",1)); var cleanup=Entity("3MCA","cleanup",("CameraTarget0","none")); var director=new SceneDirector(new("LV1B",[jim,dog,other,freeze,keep,cleanup]),new TaskStates([]),(_,_)=>1); var body=Body(); var bag=new SceneInventory(new int[19]); bag.SetState(0,2,1); using var binding=new PlayerSceneBinding(director,body,bag,"JIM1"); bag.SelectGadget(2); var companion=director.FindActor("C3DGODDARD")!; Check(!companion.Visible&&!companion.BodyEnabled&&!companion.MovementEnabled&&companion.Active,"equip hides/disables body, not AI activation"); var integrations=0; var patrol=new ActorPatrol(companion,director.FindActor,(state,_)=>{integrations++;return state with{Position=state.Position+Vector3.UnitX};}); patrol.Step(.1f); Check(integrations==0,"disabled body does not consume provisional integration"); Charge(body.Invisible); director.Trigger("other"); var controllers=body.ControllerSteps; binding.Step(new(false,false,false,false,false,Fire:true)); Check(body.Invisible.Active && body.ControllerSteps==controllers && body.Energy==18,"nonnull other owner permits wrapper activation without Jimmy input"); director.Trigger("freeze"); var remaining=body.Invisible.Remaining; binding.Step(new(false,false,false,false,false)); Check(body.Invisible.Active && body.Invisible.Remaining==remaining && !body.Invisible.MeterVisible,"AI NULL freezes without cleanup"); director.Trigger("keep"); Check(body.Invisible.Active,"explicit DeactivateInv0 and absent raw owner preserve effect"); director.Skip(); director.Trigger("cleanup"); Check(!body.Invisible.Active && body.GadgetMode==-1 && bag.SelectedGadget==-1 && companion.Visible && companion.BodyEnabled && companion.MovementEnabled,"default named-Jimmy cleanup even when raw owner NULL/first MCA target none"); patrol.Step(.1f); Check(integrations==1,"companion body resumes"); }); Test("failed ordinary equip retains display2 with raw mode-1", () => { var director=new SceneDirector(new("LV1B",[Entity("3JIM","JIM1")]),new TaskStates([]),(_,_)=>1); var body=Body(); body.Energy=0; var bag=new SceneInventory(new int[19]); bag.SetState(0,2,1); using var binding=new PlayerSceneBinding(director,body,bag,"JIM1"); bag.SelectGadget(2); Check(bag.SelectedGadget==2 && body.GadgetMode==-1 && !body.Invisible.Equipped,"display callback precedes failed equip"); }); Test("original meter resources stretch and tint with uniform widescreen placement", () => { var world=new Mesh([new(new(1,2,3),Vector2.Zero,Vector3.One)],[0,0,0]){HasCollisionMaterialIds=true}; var overlay=new InvisibleMeterOverlay(world,"artifacts/extracted/invisible-meter/meter.json"); var device=Ready(); foreach(var (width,height) in new[]{(640u,480u),(1920u,1080u),(3440u,1440u)}) { overlay.Update(overlay.Mesh,device,width,height); var a=overlay.Mesh.Vertices[5];var b=overlay.Mesh.Vertices[7]; var view=new UiViewport(width,height); Check(MathF.Abs((b.Position.X-a.Position.X)*width/2/view.Scale-27)<.0002f,"source fill width27"); Check(device.MeterPercent>=100 && device.MeterPercent<101,"source percentage precedes recharge clamp"); Check(Math.Abs((b.Position.Y-a.Position.Y)*height/2/view.Scale-device.MeterPercent*1.04)<.0005,"source fill percent times1.04"); Check(a.Color==Vector3.One && a.Uv==Vector2.Zero && b.Uv==new Vector2(.9999f,.9999f),"white ready tint and full image stretch"); Check(overlay.Mesh.Vertices[0].Position==new Vector3(1,2,3)&&overlay.Mesh.HasCollisionMaterialIds,"preserve world and collision identities"); } device.Release();overlay.Update(overlay.Mesh,device,1920,1080); Check(overlay.Mesh.Draws.Skip(1).All(d=>!d.Visible),"unequip hides both original sprites"); }); Console.WriteLine($"Invisible/energy:{count} groups passed; full enemy AI, original runtime and native Windows comparison remain separate."); LabSimulation Body() { var world=new Mesh([new(new(-1000,0,-1000),Vector2.Zero,Vector3.One),new(new(1000,0,-1000),Vector2.Zero,Vector3.One), new(new(1000,0,1000),Vector2.Zero,Vector3.One),new(new(-1000,0,1000),Vector2.Zero,Vector3.One)],[0,2,1,0,3,2]); return new(new SceneCollision(world),AseMesh.Load("../Original/ASE/jimstop.ase"),new(0,80,0),0); } GamEntity Entity(string kind,string tag,params (string Name,object Value)[] values) { var properties=new Dictionary(StringComparer.Ordinal); foreach(var (name,value) in new[]{("ObjectTag",(object)tag)}.Concat(values)) { if(value is string text)properties[name]=new(name,1,Encoding.Latin1.GetBytes(text)); else {var raw=new byte[4];BinaryPrimitives.WriteInt32BigEndian(raw,(int)value);properties[name]=new(name,6,raw);} } return new(kind,properties.Count,properties,[]); }