Files
Neutron/tests/GamePort.GrapplerLiveTests/Program.cs
T

184 lines
13 KiB
C#

using System.Numerics;
using GamePort.Assets;
using GamePort.Game;
using GamePort.Application;
var tests=new(string,Action)[]{
("actual VR08 fire attaches after body and ordinary apply still runs",Attach),
("retained input attaches after ordinary publication",Retained),
("held reel, jump detach, loop and retained first scale",Reel),
("contact suffix uses strict60 boundary and no bounce impulse",Contact),
("actual VR08 collision delivers the attached contact callback",LiveContact),
("same body context replays stale wall, zero delta skips callback",BodyContext),
("source ground/air locals, gains, clipping and old-eye aim",Camera),
("camera transitions retain reattach look but cleanup restores defaults",CameraStates),
("control loss alone retains gadget, named Jimmy cleanup releases",Ownership),
("actual RETURN cleans selection before transition",Return),
("noclip/reset release and process scalar retention",Lifecycle),
("exact rope/swing pivot, fixed first scale and110 cursor",Visuals),
("actual aim rejection skips late primary actions",AimGate),
};
foreach(var(name,test)in tests){test();Console.WriteLine("PASS "+name);}Console.WriteLine($"{tests.Length} live Grappler groups passed.");
static void Attach()
{
using var f=new Fixture();f.Attach();
Check(f.G.Attached&&f.G.Attachments==1&&f.P.GadgetMode==7,"owned raw7");
Check(f.P.PhysicsSteps==1&&f.P.ControllerSteps==1,"both phases run once");
Check(f.P.Velocity.Y==-150&&f.P.JumpState==0&&!f.G.SwingPose,
"fresh input writes attachment100 before supported ground apply subtracts250; SWING starts next input");
int body=f.P.PhysicsSteps;f.Binding.Step(default);
Check(f.P.PhysicsSteps==body+1&&f.G.SwingPose&&f.P.Pose==PlayerPose.Swing,"ordinary body and swing pose continue");
Check(float.IsFinite(f.P.ActorRoot.Length()),"finite attached root");
}
static void Retained()
{
using var f=new Fixture();f.P.InputAfterPlayerUpdate=true;f.Attach();
Check(f.G.Attached&&f.P.PhysicsSteps==1&&f.P.ControllerSteps==1&&f.P.Velocity.Y==100,"late input keeps100 publication");
}
static void Reel()
{
using var f=new Fixture();int stops=0;var sounds=new List<int>();f.G.Sound+=(id,_)=>sounds.Add(id);f.G.LoopStopped+=()=>stops++;
f.Attach();var first=f.G.Memory.FirstLength;var scale=f.G.Memory.RopeScaleZ;var length=f.G.Memory.Length;
f.Binding.Step(new(false,false,false,false,false,Fire:true));
Check(f.G.Attached&&f.G.Memory.Length<length&&sounds.Contains(31)&&sounds.Contains(34),"attach and reel sounds/length");
f.Binding.Step(new(false,false,false,false,true));
Check(!f.G.Attached&&!f.G.SwingPose&&!f.G.LoopPlaying&&stops>0,"jump withoutfire detach");
Check(f.G.Memory.FirstLength==first&&f.G.Memory.RopeScaleZ==scale,"detach retains geometry scalar");
f.I.Inventory.SelectGadget(-1);f.I.Inventory.SelectGadget(7);
Check(f.G.Memory.FirstLength==first&&f.G.Memory.RopeScaleZ==scale,"reequip retains first scale");
f.G.StepInput(f.P.ActorRoot,new(0,f.AimYaw,0),Vector3.Zero,new(false,false,false,false,false,Fire:true),1f/60,d=>new(d,Vector3.Zero));
Check(f.G.Attached&&f.G.Memory.FirstLength==first,"reattach retains first length");
f.G.StepInput(f.P.ActorRoot,new(0,f.AimYaw,0),Vector3.Zero,new(false,false,false,false,true,Fire:true),1f/60,d=>new(d,Vector3.Zero));
Check(f.G.Attached,"simultaneous fire+jump does not detach");
}
static void Contact()
{
using var f=new Fixture();f.Attach();f.G.Memory.Swing=new(36,48,.7f);f.G.Contact();
Check(f.G.Attached&&f.G.Memory.Swing.X==0&&f.G.Memory.Swing.Z==0,"equal60 clears only");
f.G.Memory.Swing=new(60.001f,0,.7f);f.G.Contact();
Check(!f.G.Attached&&f.P.Velocity==Vector3.Zero&&f.G.Memory.Swing.Elapsed==.7f,"fast contact detaches zero velocity no impulse");
}
static void LiveContact()
{
using var f=new Fixture();f.Attach();
//Explicit root/scalar fixture: cross a large authored vertical wall, using
//the live constraint and47B4B0 path rather than calling Contact directly.
var index=Enumerable.Range(0,f.World.CollisionPolygons.Length).First(i=>
MathF.Abs(f.World.CollisionPolygons[i].Normal.Y)<.01f &&
Vector3.Cross(f.World.Vertices[f.World.Indices[3*i+1]].Position-f.World.Vertices[f.World.Indices[3*i]].Position,
f.World.Vertices[f.World.Indices[3*i+2]].Position-f.World.Vertices[f.World.Indices[3*i]].Position).Length()>200000);
var center=(f.World.Vertices[f.World.Indices[3*index]].Position+f.World.Vertices[f.World.Indices[3*index+1]].Position+
f.World.Vertices[f.World.Indices[3*index+2]].Position)/3;
var normal=f.World.CollisionPolygons[index].Normal;
f.P.ApplyAuthoredTransform(center+normal*80,f.P.Yaw);
f.G.Memory.Anchor=center-normal*80+Vector3.UnitY*116;
f.G.Memory.Length=0;f.G.Memory.Swing=new(100,0,1);f.Binding.Step(default);
Check(f.G.Contacts>0&&!f.G.Attached&&!f.G.LoopPlaying, $"live source wall callback detaches: contacts={f.G.Contacts}, attached={f.G.Attached}");
}
static void BodyContext()
{
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,75.9f,70),new(0,0,-1200),Vector3.Zero,Vector3.Zero,false,0),1f/60);
Check(result.Contact==wall,"body accepted wall");var saved=physics.LastStaticContext!;var nearest=saved.Nearest;
var root=result.Body.Position;var moved=physics.MoveAttached(root,root+Vector3.UnitZ*10,60);
Check(moved.CallbackPolygon==wall&&ReferenceEquals(saved,physics.LastStaticContext),"stale terminal wall retained");
Check(saved.Nearest==nearest,"departure adds no accepted hit");
Check(physics.MoveAttached(moved.Position,moved.Position,60).CallbackPolygon==null,"zero delta skips stale callback");
}
static void Camera()
{
var locals=GrapplerCameraLocals.Default.Equip().Attach();var eye=new Vector3(0,200,-350);
var ground=GrapplerCamera.Follow(locals,Vector3.Zero,Vector3.Zero,0,0,eye,.1f,null);
Near(ground.DesiredEye,new(0,380,-650),"ground adjusted locals");Near(ground.Look,new(0,169,80),"ground look");
Near(ground.Eye,new(0,221.6f,-374),"ground effective gains");
var air=GrapplerCamera.Follow(locals with{Gains=new(99,88,77)},Vector3.Zero,Vector3.Zero,0,1,eye,.1f,null);
Near(air.DesiredEye,new(0,400,-650),"air raw eye");Near(air.Eye,new(0,240,-386),"air fixed gains");
var clipped=GrapplerCamera.Follow(locals,Vector3.Zero,Vector3.Zero,0,0,eye,.1f,(a,b)=>new(a+(b-a)*.5f,0,0,0));
Near(clipped.DesiredEye,ground.Look+(ground.DesiredEye-ground.Look)*.375f,"clip .75 of hit from look");Check(clipped.RayGain==1.5f,"obstruction gain");
var(p,y)=OriginalHeading.Angles(ground.Look-eye);
Near(Vector3.Normalize(ground.Target-ground.Eye),RocketOrientation.Rotate(Vector3.UnitZ,new(p*.02197265625f,y*.02197265625f,0)),"aim from old eye");
var pitched=GrapplerCamera.Follow(locals,Vector3.Zero,new(30,40,15),2,1,eye,.1f,null);
Near(pitched.DesiredEye,RocketOrientation.Rotate(locals.Eye,new(0,RocketOrientation.AddAxis(40,2),15)),"air eye ignores pitch");
}
static void CameraStates()
{
var c=GrapplerCameraLocals.Default with{Gains=new(3,4,5)};Check(c.Equip().Gains==c.Gains,"equip retains gains");
c=c.Equip().Attach().Detach().Attach();Near(c.Look,new(0,130,100),"reattach retains detach look");
using var f=new Fixture();f.Attach();f.G.Release();Check(f.G.Camera==GrapplerCameraLocals.Default,"cleanup normal defaults");
}
static void Ownership()
{
using var f=new Fixture();f.Attach();var physics=f.P.PhysicsSteps;
f.P.Step(default,false,false);
Check(f.G.Attached&&f.G.Equipped&&f.P.PhysicsSteps==physics+1&&f.P.Grounded,
"controlfalse still integrates the supported body without a generic gadget release or invented takeoff");
f.D.ReleasePlayerGadget();Check(!f.G.Attached&&!f.G.Equipped&&f.P.GadgetMode==-1,"namedJimmy selected cleanup");
f.I.Inventory.SelectGadget(7);f.D.Trigger("gr");Check(!f.G.Equipped,"actualVR08 intro MCA/CAM cleanup");
}
static void Return()
{
using var f=new Fixture();f.Attach();f.I.SetReturnContext("level1b.gam","STARTEXP");
var portal=GamLevel.Load("../Original/gam/VR08.gam").Entities.First(e=>e.Kind=="LOAD"&&e.Text("LevelName")=="RETURN");
LevelTransition? transition=null;f.I.Transition+=t=>{Check(!f.G.Equipped&&!f.G.Attached,"cleanup before event");transition=t;};
f.I.Update(portal.Position,60);Check(transition is{Returning:true,LevelFile:"level1b.gam"},"actual RETURN");
}
static void Lifecycle()
{
var memory=new GrapplerMemory();using(var f=new Fixture(memory)){f.Attach();f.P.SetNoclip(true);Check(!f.G.Equipped&&f.I.Inventory.SelectedGadget==-1,"noclip safety release");}
var first=memory.FirstLength;Check(first>0,"process scalar set");
using var next=new Fixture(memory);Check(next.G.Memory.FirstLength==first&&!next.G.Attached,"retained scalar fresh scene helper");
next.P.Reset();Check(memory.FirstLength==0&&next.P.GadgetMode==-1,"debug reset clears memory");
}
static void Visuals()
{
using var f=new Fixture();var visuals=new GrapplerVisuals(f.World,"artifacts/extracted");f.Equip();
f.G.StepInput(f.P.ActorRoot,new(0,f.AimYaw,0),Vector3.Zero,default,1f/60,d=>new(d,Vector3.Zero));
visuals.Update(visuals.Mesh,f.G,f.P.ActorRoot,Vector3.Zero,0,f.P.Appearance,new(0,0,-100),Vector3.Zero);
var cursor=visuals.Mesh.Draws[^1];Check(cursor.Visible&&cursor.Blend==MeshBlend.Alpha,"cursor alpha");
var vertices=visuals.Mesh.Vertices;NearF(Vector3.Distance(vertices[^4].Position,vertices[^3].Position),110,"cursor width");
NearF(Vector3.Distance(vertices[^3].Position,vertices[^2].Position),110,"cursor height");
f.G.StepInput(f.P.ActorRoot,new(0,f.AimYaw,0),Vector3.Zero,new(false,false,false,false,false,Fire:true),1f/60,d=>new(d,Vector3.Zero));
f.G.StepInput(f.P.ActorRoot,new(0,f.AimYaw,0),Vector3.Zero,default,1f/60,d=>new(d,Vector3.Zero));
visuals.Update(visuals.Mesh,f.G,f.P.ActorRoot,Vector3.Zero,.1,f.P.Appearance,new(0,0,-100),Vector3.Zero);
var rope=visuals.Mesh.Draws[^3];var swing=visuals.Mesh.Draws[^2];Check(rope.Visible&&swing.Visible,"rope and sourceSWING");
var index=(int)visuals.Mesh.Indices[(int)rope.FirstIndex];var before=vertices[index].Position;
f.G.Memory.Length*=.5f;visuals.Update(visuals.Mesh,f.G,f.P.ActorRoot,Vector3.Zero,.1,f.P.Appearance,new(0,0,-100),Vector3.Zero);
Near(vertices[index].Position,before,"render uses firstscale not currentlength");
f.G.Release();visuals.Update(visuals.Mesh,f.G,f.P.ActorRoot,Vector3.Zero,.1,f.P.Appearance,new(0,0,-100),Vector3.Zero);
Check(!rope.Visible&&!swing.Visible&&!cursor.Visible,"cleanup hides all helpers");
}
static void AimGate()
{
using var f=new Fixture();f.Equip();int late=0;f.P.InputActions+=_=>late++;
var ray=new GrapplerRay(f.World,"VR08");var hit=ray.Trace(f.P.ActorRoot,new(0,f.AimYaw,0))!.Value;
var root=hit.Hit.Position-Vector3.Normalize(ray.Endpoint(f.P.ActorRoot,new(0,f.AimYaw,0))-f.P.ActorRoot)*200;
f.P.ApplyAuthoredTransform(root,f.AimYaw);f.Binding.Step(new(false,false,false,false,false,Fire:true));
Check(!f.G.Attached&&late==0&&!f.G.CursorVisible,"close surface ends active input");
}
static void Check(bool v,string m){if(!v)throw new Exception(m);}
static void Near(Vector3 a,Vector3 b,string m)=>Check(Vector3.Distance(a,b)<.004f,$"{m}: {a} != {b}");
static void NearF(float a,float b,string m)=>Check(Math.Abs(a-b)<.004f,$"{m}: {a} != {b}");
sealed class Fixture:IDisposable
{
public Mesh World{get;}=Mesh.LoadSceneJson("artifacts/extracted/VR08/scene.json");
public SceneDirector D{get;}public SceneInteractions I{get;}public LabSimulation P{get;}public PlayerSceneBinding Binding{get;}
public GrapplerGadget G=>P.Grappler!;public float AimYaw{get;}
public Fixture(GrapplerMemory? memory=null)
{
var level=GamLevel.Load("../Original/gam/VR08.gam");var save=OriginalSave.Load("../Original/NewGame.tsk");var tasks=new TaskStates(save.Tasks);
D=new(level,tasks,(_,_)=>0);I=new(D,tasks,save);var start=level.FindStart()!;D.Begin(start,activateStartTrigger:false);
var ray=OriginalCameraRay.FromRouteScene(World,"VR08");
P=new(new(World),new MorphAnimation("artifacts/extracted/animations/jimstop/animation.json").Base,start.Position,start.Rotation.Y,
(a,b)=>ray.Trace(a,b),(a,b)=>ray.TraceGeometry(a,b),OriginalStaticCollision.FromRouteScene(World,"VR08")){LevelCode="VR08"};
P.EnableGrappler(World,memory??new());Binding=new(D,P,I.Inventory,level.Player!.Tag!);
var aim=new GrapplerRay(World,"VR08");AimYaw=Enumerable.Range(0,360).First(y=>aim.Aim(P.ActorRoot,new(0,y,0),false,false,0,1f/60).CursorVisible);
P.ApplyAuthoredTransform(P.ActorRoot,AimYaw);
}
public void Equip(){I.Inventory.SetState(0,7,1);I.Inventory.SelectGadget(7);}
public void Attach(){Equip();Binding.Step(new(false,false,false,false,false,Fire:true));}
public void Dispose(){G.Release();Binding.Dispose();}
}