Files

104 lines
7.2 KiB
C#

using System.Numerics;
using GamePort.Game;
using GamePort.Assets;
using System.Text.Json;
void Check(bool value, string message) { if (!value) throw new Exception(message); }
void Near(Vector3 actual, Vector3 expected, string message)
{ Check(Vector3.Distance(actual, expected) < .002f, $"{message}: {actual} != {expected}"); }
var wall = new CameraRayPolygon(new(new(-100,-100,0),new(-100,100,0),new(100,100,0),0,0),Vector3.UnitZ);
var world = new OriginalStaticCollision([wall]);
(StaticCollisionMove Move, StaticCollisionContext Context) Run(OriginalStaticCollision scene, Vector3 before, Vector3 delta,
bool edges=true, bool lower=false)
{
var context = new StaticCollisionContext(10);
return (scene.Move(before, delta, 10, context, edges, lower), context);
}
// References are copied-original dispatcher47B4B0 results, not just the kernel.
var face=Run(world,new(0,20,20),new(0,0,-15));
Near(face.Move.Position,new(0,20,10),"source face retry");
Check(face.Context.Passes==2 && face.Context.TriangleTests==4 && face.Move.CallbackPolygon==wall,
"source retry count, duplicate visits and final callback");
Console.WriteLine("PASS original face retry/publication and per-cell duplicate visits");
var edge=Run(world,new(105,100,20),new(0,0,-15));
Near(edge.Move.Position,new(107.071068f,100,7.07106781f),"source edge retry");
Check(edge.Context.Passes==2 && edge.Context.TriangleTests==2,"original edge retry statistics");
var disabled=Run(world,new(105,100,20),new(0,0,-15),edges:false);
Near(disabled.Move.Position,new(105,100,5),"source disabled edge");
Check(disabled.Context.Passes==1 && disabled.Context.TriangleTests==1 && disabled.Move.CallbackPolygon==null,
"disabled edges still scan and publish");
Console.WriteLine("PASS original edge and edge-disabled dispatcher references");
var zero=Run(world,new(0,20,20),Vector3.Zero);
Check(zero.Context.Passes==0 && !zero.Move.Published,"zero delta early exit");
var forced=Run(world,new(0,20,20),Vector3.Zero,lower:true);
Check(forced.Context.Passes==1 && forced.Context.TriangleTests==2 && forced.Move.Published,"source member42C forced zero");
Console.WriteLine("PASS zero-motion and forced-processing source boundaries");
var side=new CameraRayPolygon(new(new(0,-100,-100),new(0,-100,100),new(0,100,100),1,0),Vector3.UnitX);
var corner=Run(new([wall,side]),new(20,20,20),new(-30,0,-30));
Near(corner.Move.Position,new(5,20,10.000001f),"source corner/edge-below rejection");
Check(corner.Context.Passes==2 && corner.Context.TriangleTests==8 && corner.Move.CallbackPolygon==wall,
"terminal normal belongs to last accepted polygon, not last tested polygon");
Console.WriteLine("PASS original corner order and retained last polygon");
var ceiling=new CameraRayPolygon(new(new(-100,100,-100),new(100,100,100),new(100,100,-100)), -Vector3.UnitY);
var under=Run(new([ceiling]),new(0,80,0),new(0,15,0));
Near(under.Move.Position,new(0,90,0),"source ceiling retry");
Check(under.Context.Passes==2 && under.Context.TriangleTests==8 && under.Move.CallbackPolygon==ceiling,
"source ceiling terminal callback");
Console.WriteLine("PASS original ceiling dispatch reference");
Check(OriginalStaticCollision.Bounds(Vector3.Zero,new(1000,0,1000),1010)==new CollisionCells(127,134,127,132),
"source asymmetric Z upper bound");
var masked=new OriginalStaticCollision([wall with { ShapeName="ncwall" },side with { MaterialName="cstart" }]);
Check(masked.PolygonCount==0 && masked.TraceSupport(new(0,20,20),new(0,20,-20))==null,"source registration masks");
Console.WriteLine("PASS asymmetric source bounds and registration exclusions");
var floor=new CameraRayPolygon(new(new(-100,0,-100),new(100,0,-100),new(0,0,100),3,0),Vector3.UnitY);
var supported=new OriginalStaticCollision([floor]);
Check(supported.WallPolygonCount==0 && supported.TraceSupport(new(0,100,0),new(0,-100,0))?.ShapeId==3,
"general support grid remains separate from wall registration");
Console.WriteLine("PASS separate general-grid nearest support query");
var retained = new StaticCollisionContext(10);
world.Move(new(0,20,20),new(0,0,-15),10,retained);
var retainedNearest = retained.Nearest;
var withoutGeometry = world.Move(new(0,20,10),new(0,0,-15),10,retained,geometryEnabled:false);
Near(withoutGeometry.Position,new(0,20,-5),"disabled geometry publishes unclipped candidate");
Check(withoutGeometry.CallbackPolygon==wall && retained.TriangleTests==4 && retained.Nearest==retainedNearest,
"disabled geometry preserves retained last-polygon callback without testing triangles");
Console.WriteLine("PASS disabled geometry and retained callback state");
Check(new OriginalStaticCollision([wall with {MaterialId=61}]).HasSpecialPlayerContacts,
"fence material archive ID must require special callback handling");
Check(!new OriginalStaticCollision([wall with {MaterialId=17}]).HasSpecialPlayerContacts
&& !new OriginalStaticCollision([ceiling with {MaterialId=61}]).HasSpecialPlayerContacts
&& !new OriginalStaticCollision([wall with {MaterialId=61,ShapeName="ncwall"}]).HasSpecialPlayerContacts,
"ordinary material, ceiling370 and excluded shapes cannot trigger player374 special handling");
var fixture = Path.Combine(Path.GetTempPath(), $"neutron-material-{Guid.NewGuid():N}.json");
try
{
var source = new { vertices=new[] {new[]{0,0,0},new[]{1,0,0},new[]{0,1,0}},indices=new[]{0,1,2},
collisionPolygons=new[]{new{shapeId=0,polygonIndex=0,normal=new[]{0,0,1},materialName="fence",materialId=61}} };
File.WriteAllText(fixture,JsonSerializer.Serialize(source));
var loaded=Mesh.LoadSceneJson(fixture);
Check(loaded.HasCollisionMaterialIds && OriginalStaticCollision.FromRouteScene(loaded,"LV1B").HasSpecialPlayerContacts,
"prepared material identity must survive loader and source grid construction");
File.WriteAllText(fixture,File.ReadAllText(fixture).Replace(",\"materialId\":61", ""));
loaded=Mesh.LoadSceneJson(fixture);
Check(!loaded.HasCollisionMaterialIds,"legacy metadata must remain distinguishable from unresolved material");
try { OriginalStaticCollision.FromRouteScene(loaded,"LV1B"); throw new Exception("legacy metadata silently accepted"); }
catch(InvalidDataException) { }
}
finally { File.Delete(fixture); }
Console.WriteLine("PASS material policy IDs and legacy prepared-data rejection");
foreach(var (level,directory,special) in new[]{("LV1B","level1b",true),("VR04","VR04",false),("LEV1","level1",true)})
{
var scene=Mesh.LoadSceneJson(Path.Combine("artifacts/extracted",directory,"scene.json"));
Check(OriginalStaticCollision.FromRouteScene(scene,level).HasSpecialPlayerContacts==special,
$"actual {level} special-contact coverage differs from original archive inventory");
}
Console.WriteLine("PASS prepared lab/tutorial/outside contact coverage");
var tiedFloor = floor with {Triangle=floor.Triangle with {ShapeId=4}};
var supportTies = new OriginalStaticCollision([tiedFloor,floor]);
Check(supportTies.TraceSupport(new(0,100,0),new(0,-100,0))?.ShapeId==3,
"equal-distance support retains original shape/cell/FIFO order");
Check(supportTies.TraceSupport(Vector3.Zero,Vector3.Zero)==null,
"zero-length support has no strictly nearer hit");
Console.WriteLine("PASS support nearest ties and zero-length limit");
Console.WriteLine("Static dispatcher:11 groups passed; source whole-body/scene integration remains separate.");