using System.Numerics; using System.Text; using System.Buffers.Binary; using GamePort.Game; using GamePort.Assets; var count=0; void Check(bool value,string message){if(!value)throw new Exception(message);} void Near(float a,float b,string message)=>Check(MathF.Abs(a-b)<.02f,$"{message}: {a} != {b}"); void Test(string name,Action action){action();count++;Console.WriteLine($"PASS {name}");} void Hold(BaseballGadget device,bool down=false,bool up=false,float energy=20,bool supported=true) { var input=new PlayerInput(down,up,false,false,false,Fire:true); device.StepCameraInput(input); device.StepInput(input,Vector3.Zero,Vector3.Zero,supported,energy,false,()=>{},_=>{},_=>{}); } void Release(BaseballGadget device,Action? cost=null)=>device.StepInput(default,Vector3.Zero,Vector3.Zero,false,0,false,()=>{},_=>{},cost??(_=>{})); Mesh Floor()=>new([new(new(-4000,0,-4000),Vector2.Zero,Vector3.One),new(new(4000,0,-4000),Vector2.Zero,Vector3.One), new(new(4000,0,4000),Vector2.Zero,Vector3.One),new(new(-4000,0,4000),Vector2.Zero,Vector3.One)], [0,1,2,0,2,3]) {HasCollisionMaterialIds=true,CollisionPolygons=[new(0,0,Vector3.UnitY,"floor","floor",0),new(0,1,Vector3.UnitY,"floor","floor",0)]}; Test("first hold arms, next captures pitch, label retains prior capture",()=>{ var g=new BaseballGadget();g.Equip();Hold(g,true); Check(g.PendingThrow&&g.AimVisible&&g.SavedPitch==0,"first hold arms only"); Hold(g,true);Near(g.SavedPitch,10+2f/3,"camera pitch");Check(g.DisplayPitch==0,"first numeric label precedes capture"); var old=g.SavedPitch;Hold(g,true);Check(g.DisplayPitch==old&&g.SavedPitch>old,"stored label order"); for(var i=0;i<200;i++)Hold(g,true);Check(g.SavedPitch==45,"upper clamp"); for(var i=0;i<200;i++)Hold(g,up:true);Check(g.SavedPitch==0,"lower clamp"); }); Test("release launches once with sound before fixed energy debit",()=>{ var g=new BaseballGadget();g.Equip();Hold(g);Hold(g);var order=new List();g.Thrown+=()=>{ Check(g.PendingThrow&&!g.Projectiles[0].Visible,"sound callback precedes launch and pending clear");order.Add("sound");}; Release(g,cost=>{Check(cost==-1,"fixed cost");order.Add("cost");});Release(g); Check(order.SequenceEqual(new[]{"sound","cost"})&&g.Throws==1&&!g.PendingThrow,"one release transaction"); Check(g.Projectiles[0].Visible&&g.NextSlot==1,"first owned pool slot"); }); Test("pending throw survives cleanup and can release at zero energy",()=>{ var g=new BaseballGadget();g.Equip();Hold(g);Hold(g,true);var saved=g.SavedPitch;g.Release(); Check(g.PendingThrow&&!g.AimVisible&&g.CameraPitch==0&&g.SavedPitch==saved,"source cleanup scalar ownership"); Release(g);Check(g.Throws==1&&g.Projectiles[0].Visible,"release predicate has no fresh energy or raw-mode gate"); }); Test("twenty reused slots, no invented ammo or lifetime",()=>{ var g=new BaseballGadget();g.Equip(); for(var i=0;i<23;i++){Hold(g);Release(g);} Check(g.Throws==23&&g.NextSlot==3&&g.Projectiles.Count==20&&g.Projectiles.All(b=>b.Visible),"fixed wraparound pool"); g.Release();Check(g.Projectiles.All(b=>b.Visible),"unequip retains helper bodies"); g.Reset();Check(g.Throws==0&&g.Projectiles.All(b=>!b.Visible),"modern new scene clears port-owned pool"); }); Test("upright launch offset, quick-normalized speed and45-degree slope",()=>{ var ball=new BaseballProjectile();ball.Launch(new(100,200,300),Vector3.Zero,45); Check(ball.Position==new Vector3(100,280,350),"local80/50 offset");Near(ball.Velocity.Y/ball.Velocity.Z,1,"slope45/45"); Near(OriginalBodySupport.QuickMagnitude(ball.Velocity),1800,"published speed"); ball.Launch(Vector3.Zero,new(0,90,0),0);Near(ball.Position.X,-50,"rotated offset");Near(ball.Velocity.X,-1800,"yaw90 velocity"); }); Test("landing queues old horizontal velocity and-.65Y before support clearsY",()=>{ var physics=new OriginalBodyPhysics(OriginalStaticCollision.FromRouteScene(Floor(),"VR07")); var result=physics.Step(new(new(0,16,0),new(60,-120,0),Vector3.Zero,Vector3.Zero),1f/60, radius:15,factor:1,friction:.46f,profile:BodyResponseProfile.Baseball); Check(result.Landing!=null&&result.Body.Supported&&result.Body.Velocity.Y==0,"landing and liveY reset"); Near(result.Body.Impulse.X,60,"queued oldX");Near(result.Body.Impulse.Y,(120+800f/60)*.65f,"queued bounce"); Near(result.Body.Velocity.X,59.54f,"source .46 floor friction follows landing callback"); var next=physics.Step(result.Body,1f/60,radius:15,factor:1,friction:.46f,profile:BodyResponseProfile.Baseball); Check(next.Body.Impulse==Vector3.Zero,"one consumption");Check(next.Body.Velocity.X>result.Body.Velocity.X,"horizontal impulse is added, not replaced"); }); Test("ball wall callback reflects and damps horizontal velocity while preserving falling Y",()=>{ var wall=new CameraRayPolygon(new(new(-1000,-1000,0),new(-1000,1000,0),new(1000,1000,0)),Vector3.UnitZ); var physics=new OriginalBodyPhysics(new([wall])); var result=physics.Step(new(new(0,100,30),new(0,-10,-1800),Vector3.Zero,Vector3.Zero),1f/60, radius:15,factor:1,friction:.46f,profile:BodyResponseProfile.Baseball); Check(result.Contact==wall,"source static dispatcher finds the actual wall"); Near(result.Body.Velocity.Z,972,"horizontal reflection and1-.46 damping"); Near(result.Body.Velocity.Y,-10-800f/60,"wall callback keeps liveY"); Check(result.Body.Impulse==Vector3.Zero&&!result.Body.Supported,"wall is not a landing callback"); }); Test("target cooldown runs while hidden and end arrival teleports to Start",()=>{ var actor=new SceneDirector(new("VR07",[Entity("3TAR","target",("StartPosX",-500f),("DestPosX",500f),("RespawnTime",1f))]),new TaskStates([]),(_,_)=>1).Actors[0]; var t=new MovingBaseballTarget(actor);Check(t.Hit()&&!t.Hit()&&!actor.Visible,"one callback per respawn"); t.StepFrame(.5f);Check(!actor.Visible,"still hidden");t.StepFrame(.5f);Check(actor.Visible&&t.Remaining==0,"exact zero expires"); actor.Position=new(450,0,0);t.StepFrame(0);Check(actor.Position==t.Start&&t.Velocity.X>0,"source teleports without swapping endpoints"); }); Test("VR07 keeps six duplicate-tag targets by source instance",()=>{ var level=GamLevel.Load("../Original/gam/VR07.gam");var d=new SceneDirector(level,new TaskStates([]),(_,_)=>1); var mesh=Mesh.LoadSceneJson("artifacts/extracted/VR07/scene.json");var collision=OriginalStaticCollision.FromRouteScene(mesh,"VR07"); Check(!collision.HasSpecialPlayerContacts&&collision.PolygonCount>0,"ordinary source wall profile"); var p=new LabSimulation(new SceneCollision(mesh),AseMesh.Load("../Original/ASE/jimstop.ase"),level.Entities.First(e=>e.Kind=="STRT").Position,0){LevelCode="VR07"}; var scene=new SceneBaseballs(d,p,mesh);Check(scene.Targets.Count==6,"all source records"); Check(scene.Targets.Select(t=>t.Actor.SourceGam!.Offset).Distinct().Count()==6,"instances are never resolved by duplicate tag"); var first=scene.Targets[0];var second=scene.Targets[1];first.Hit();Check(!first.Actor.Visible&&second.Actor.Visible,"per-instance state"); }); Test("named-Jimmy cutscene restores companion while retaining pending throw",()=>{ var mesh=Floor();var p=new LabSimulation(new SceneCollision(mesh),AseMesh.Load("../Original/ASE/jimstop.ase"),new(0,80,0),0){LevelCode="LV1B"}; var d=new SceneDirector(new("LV1B",[Entity("3JIM","JIM1"),Entity("3DOG","C3DGODDARD"),Entity("3MCA","cleanup",("CameraTarget0","none"))]),new TaskStates([]),(_,_)=>1); var bag=new SceneInventory(new int[19]);bag.SetState(0,6,1);using var binding=new PlayerSceneBinding(d,p,bag,"JIM1"); bag.SelectGadget(6);binding.Step(new(false,false,false,false,false,Fire:true)); Check(!d.FindActor("C3DGODDARD")!.Visible&&p.Baseball.PendingThrow,"equip hides companion and arms"); d.Trigger("cleanup");Check(p.GadgetMode==-1&&p.Baseball.PendingThrow&&d.FindActor("C3DGODDARD")!.Visible,"named cleanup preserves source latch"); }); Test("Baseball cleanup restores companion even in Invisible's blocked levels",()=>{ foreach(var code in new[]{"LEV6","LEV7","LV1C","LV1D","LV4A","LV6A"}) { var p=new LabSimulation(new SceneCollision(Floor()),AseMesh.Load("../Original/ASE/jimstop.ase"),new(0,80,0),0){LevelCode=code}; var d=new SceneDirector(new(code,[Entity("3JIM","JIM1"),Entity("3DOG","C3DGODDARD")]),new TaskStates([]),(_,_)=>1); var bag=new SceneInventory(new int[19]);bag.SetState(0,6,1);using var binding=new PlayerSceneBinding(d,p,bag,"JIM1"); bag.SelectGadget(6);bag.SelectGadget(-1);var companion=d.FindActor("C3DGODDARD")!; Check(companion.Visible&&companion.BodyEnabled&&companion.MovementEnabled,"raw6 unconditional restoration "+code); } }); Test("actual contact activates trophy only after strict fourth hit and honors HasCollision",()=>{ foreach(var hasCollision in new[]{0,-1}) { var mesh=Floor();var p=new LabSimulation(new SceneCollision(mesh),AseMesh.Load("../Original/ASE/jimstop.ase"),new(1000,80,0),0){LevelCode="VR07"}; var d=new SceneDirector(new("VR07",[Entity("3TAR","target",("PositionY",100f),("PositionZ",100f), ("StartPosY",100f),("StartPosZ",100f),("DestPosX",1000f),("RespawnTime",.25f),("ActivateObject","trophy"),("HitsRequired",3)), Entity("3TRO","trophy",("InitiallyVisible",0),("HasCollision",hasCollision))]),new TaskStates([]),(_,_)=>1); var scene=new SceneBaseballs(d,p,mesh);var trophy=d.FindActor("trophy")!; Check(!trophy.Visible&&!trophy.BodyEnabled,"initial hidden trophy contact disabled"); var score=0;var sounds=new List();scene.ScoreAwarded+=x=>score+=x;scene.Sound+=sounds.Add; for(var i=0;i<4;i++) { p.Baseball.Projectiles[i].Launch(new(0,20,0),Vector3.Zero,0); scene.StepBodies(1f/60); foreach(var ball in p.Baseball.Projectiles)ball.Hide(); if(i<3)Check(!trophy.Visible,"strict threshold must not activate at "+(i+1)); scene.StepTargets(.5f); } Check(scene.Hits==4&&score==40&&trophy.Visible&&trophy.BodyEnabled==(hasCollision!=0),"source contact and conditional enable"); Check(sounds.SequenceEqual(new[]{199})&&!d.Blocked,"parent428 does not invoke trigger interpreter"); } }); Console.WriteLine($"Baseball:{count} groups passed; pair scheduling/full enemy combat remain separate."); GamEntity Entity(string kind,string tag,params(string Name,object Value)[] values) { var props=new Dictionary(StringComparer.Ordinal); foreach(var(name,value) in new[]{("ObjectTag",(object)tag)}.Concat(values)) { if(value is string text)props[name]=new(name,1,Encoding.Latin1.GetBytes(text)); else{var raw=new byte[4];if(value is float f)BinaryPrimitives.WriteInt32BigEndian(raw,BitConverter.SingleToInt32Bits(f)); else BinaryPrimitives.WriteInt32BigEndian(raw,(int)value);props[name]=new(name,value is float?3u:6u,raw);} } return new(kind,props.Count,props,[]); }