using System.Numerics; using System.Text.Json; using GamePort.Game; // Synthetic actor composition, not live scene admission. All roots are points; // callbacks mutate roots only, primitive flags remain constant during each query. var fixture = new Fixture(); var actual = new List(); void Query(string name) => actual.Add(fixture.Query(name)); fixture.Reset(); fixture.Wall(0,35); Query("nearer_wall"); fixture.Reset(); fixture.Wall(0,10); Query("equal_dynamic_face"); fixture.Reset(); fixture.Wall(0,0); Query("far_wall"); fixture.Reset(); fixture.Wall(0,35); fixture.Wall(1,30); Query("two_near_first"); fixture.Reset(); fixture.Wall(0,30); fixture.Wall(1,35); Query("two_far_first"); fixture.Reset(); fixture.Wall(0,35); fixture.Mutate = true; Query("nearer_callback_root"); fixture.Reset(); fixture.Wall(0,35); fixture.Inhibit = true; Query("inhibit_static_visit"); fixture.Reset(); fixture.Wall(0,35); fixture.Geometry = false; Query("geometry_off_visits"); fixture.Reset(); fixture.Wall(0,35); fixture.Force = true; Query("forced_primitive_mode"); fixture.Reset(); fixture.Wall(0,0); fixture.CeilingSeed = true; Query("stale_ceiling_callback"); fixture.Reset(); fixture.Wall(0,0); fixture.Root.Y = 30; fixture.Ceiling(35,true); Query("ceiling_response"); fixture.Reset(); fixture.Wall(0,35); fixture.Previous.Y = -30; fixture.Ceiling(); Query("wall_and_ceiling"); fixture.Reset(); fixture.Wall(0,35); Query("retained_first"); fixture.Previous = new(0,0,50); fixture.Root = new(0,0,15,1); Query("retained_second"); if(args is ["--dump"]) { Console.WriteLine(JsonSerializer.Serialize(new {cases = actual,diagnostic_steps = fixture.DiagnosticSteps})); return; } var native = args is ["--native", _]; if(args.Length != 0 && !native) throw new ArgumentException("Use --native "); using var expected = JsonDocument.Parse(File.ReadAllText(native ? args[1] : Path.Combine(AppContext.BaseDirectory,"expected.json"))); var cases = expected.RootElement.GetProperty("cases").EnumerateArray().ToArray(); if(cases.Length != actual.Count) throw new Exception("Case count differs"); for(var i = 0; i < actual.Count; i++) Compare(cases[i],actual[i],cases[i].GetProperty("name").GetString()!); Compare(expected.RootElement.GetProperty("diagnostic_steps"), JsonSerializer.SerializeToElement(fixture.DiagnosticSteps),"diagnostic_steps"); Console.WriteLine($"PASS {actual.Count} populated-grid dynamic/static comparisons and 12 kernel steps; exact float32, callback order and primitive mode."); static void Compare(JsonElement expected,JsonElement actual,string path) { if(expected.ValueKind != actual.ValueKind) throw new Exception($"{path}: value kind differs"); if(expected.ValueKind == JsonValueKind.Object) { if(expected.EnumerateObject().Count() != actual.EnumerateObject().Count()) throw new Exception($"{path}: fields differ"); foreach(var p in expected.EnumerateObject()) Compare(p.Value,actual.GetProperty(p.Name),path+"."+p.Name); } else if(expected.ValueKind == JsonValueKind.Array) { if(expected.GetArrayLength() != actual.GetArrayLength()) throw new Exception($"{path}: array length differs"); for(var i = 0; i < expected.GetArrayLength(); i++) Compare(expected[i],actual[i],$"{path}[{i}]"); } // Native %.9g and managed shortest-roundtrip text can spell the same float // differently. Compare exact parsed Single values, without an epsilon. else if(expected.ValueKind == JsonValueKind.Number) { if(expected.GetSingle() != actual.GetSingle()) throw new Exception($"{path}: {actual} != original {expected}"); } else if(!JsonElement.DeepEquals(expected,actual)) throw new Exception($"{path}: {actual} != original {expected}"); } sealed class Fixture { public Vector3 Previous; public Vector4 Root; public bool Mutate,Inhibit,Geometry,Force,CeilingSeed; private StaticCollisionContext context = null!; private readonly List polygons = []; private readonly List events = []; private int dynamicCallbacks,staticCallbacks,setters,kernelCalls; private CameraRayPolygon seed; public List DiagnosticSteps { get; } = []; public void Reset() { Previous = new(0,0,50); Root = new(0,0,15,1); Mutate = Inhibit = Force = CeilingSeed = false; Geometry = true; context = new(88,77){Nearest = new(77,88,new(111,222,333))}; seed = new(new(new(-100,-100,0),new(-100,100,0),new(100,100,0),99,0),Vector3.UnitZ); context.LastPolygon = seed; polygons.Clear(); events.Clear(); dynamicCallbacks = staticCallbacks = setters = kernelCalls = 0; } public void Wall(int id,float z) => polygons.Add(new(new(new(-100,-100,z),new(-100,100,z), new(100,100,z),0,id),Vector3.UnitZ)); public void Ceiling(float y = 5,bool positiveZ = false) => polygons.Add(new(new(new(-100,y,-100), positiveZ ? new(-100,y,100) : new(100,y,100), positiveZ ? new(100,y,100) : new(100,y,-100),0,1),-Vector3.UnitY)); private int PolygonId(CameraRayPolygon? p) => p is null ? -1 : p == seed ? 99 : p.Value.Triangle.PolygonId; private void Notify(Vector3 normal,CameraRayPolygon? polygon,int slot) { events.Add($"{slot:x}:{PolygonId(polygon)}:0"); if(polygon is null) dynamicCallbacks++; else staticCallbacks++; if(Mutate) Root = new(777,888,999,2); } public JsonElement Query(string name) { if(CeilingSeed) { seed = seed with { StoredNormal = -Vector3.UnitY }; context.LastPolygon = seed; } var current = new Vector3(Root.X,Root.Y,Root.Z); var candidate = current; context.Nearest = context.Nearest with {FaceDistance = -1,EdgeDistance = 10}; var box = new DynamicBoxCollider(1,Vector3.Zero,new(-50,-50,-10),new(50,50,10),Matrix4x4.Identity,40); foreach(var triangle in DynamicSceneCollision.Triangles(box)) { var cross = Vector3.Cross(triangle.B-triangle.A,triangle.C-triangle.A); var normal = cross/OriginalBodySupport.QuickMagnitude(cross); kernelCalls++; var input = candidate; var hit = OriginalSphereResponse.Resolve(new(triangle.A,triangle.C,triangle.B),normal, Previous,candidate,10,context.Nearest); if(name == "wall_and_ceiling") { var h = hit.State.Hit!.Value; DiagnosticSteps.Add(JsonSerializer.SerializeToElement(new { index = DiagnosticSteps.Count,input = new[]{input.X,input.Y,input.Z,1}, output = new[]{hit.Position.X,hit.Position.Y,hit.Position.Z,1}, contact = hit.Contact == SphereContact.None ? 0 : 1,face = hit.State.FaceDistance, edge = hit.State.EdgeDistance,hit = new[]{h.X,h.Y,h.Z,1} })); } if(hit.Contact == SphereContact.None) continue; candidate = hit.Position; context.Nearest = hit.State; Notify(normal,null,0x374); } var contact = context.Nearest.FaceDistance != -1 || context.Nearest.EdgeDistance < 10; if(contact && !Inhibit) { var world = new OriginalStaticCollision(polygons); var clipped = world.Move(Previous,candidate-Previous,10,context,edges:false, lowerEdges:Force,geometryEnabled:Geometry); if(clipped.CallbackPolygon is {} polygon) Notify(polygon.StoredNormal,polygon,polygon.StoredNormal.Y > -.7f ? 0x374 : 0x370); events.Add("314:-1:0"); Root = new(clipped.Position,1); setters++; } var p = context.Nearest.Hit!.Value; return JsonSerializer.SerializeToElement(new { name,contact = contact ? 1 : 0,root = new[]{Root.X,Root.Y,Root.Z,Root.W}, face = context.Nearest.FaceDistance,edge = context.Nearest.EdgeDistance, hit = new[]{p.X,p.Y,p.Z,1},last = PolygonId(context.LastPolygon), dynamicCallbacks,staticCallbacks,setters,passes = context.Passes, visits = context.NodeVisits,mode = 0, kernelZero = kernelCalls+(Force ? 0 : context.TriangleTests), kernelLower = Force ? context.TriangleTests : 0,events = string.Join(',',events) }); } }