Recover dynamic box shared-context ownership and kernel winding

This commit is contained in:
UnixNight committed 2026-11-07 00:20:22 -05:00
1 parent 346d350176
commit 135ff3642a
18 files changed
+640 -1

No files matched your search

@@ -0,0 +1,144 @@
using System.Numerics;
using System.Text.Json;
using GamePort.Game;
//Test composition of recovered47A5E0 ownership over existing source kernels.
//Not wired into gameplay: actors/getters/transform are fixtures; point roots W1,
//empty static grid and non-reentrant normal callbacks bound this comparison.
var fixture=new Fixture(args is ["--construction-winding"]);var rows=new List<Observation>();
void Query(string name)=>rows.Add(fixture.Query(name));
fixture.Reset();Query("face_stale_polygon");
fixture.Reset();fixture.Context.LastPolygon=null;Query("face_no_polygon");
fixture.Reset();fixture.Inhibit=true;Query("inhibited");
fixture.Reset();fixture.Geometry=false;Query("geometry_off");
fixture.Reset();fixture.Root.Z=200;Query("broadphase_reject");
fixture.Reset();fixture.Mode=2;Query("other_box_overlap");
fixture.Reset();fixture.Mode=2;fixture.Root.Z=51;Query("other_box_reject");
fixture.Reset();fixture.Previous.Z=100;fixture.Root.Z=80;Query("no_triangle_hit");
fixture.Reset();fixture.Mutate=true;Query("callback_root_overwritten");
fixture.Reset();fixture.Mutate=fixture.Inhibit=true;Query("inhibited_callback_root_survives");
fixture.Reset();fixture.StaticGeometry=false;Query("static_geometry_off_stale_polygon");
fixture.Reset();fixture.ForceStatic=true;Query("forced_static_mode");
fixture.Reset();fixture.Root.Y=fixture.Previous.Y=45;Query("near_top_edge");
fixture.Reset();fixture.BoxRoot.X=fixture.Root.X=fixture.Previous.X=100;Query("translated_box");
fixture.Reset();fixture.Rotate=true;fixture.Root=new(15,0,0,1);fixture.Previous=new(50,0,0);Query("rotated_box");
fixture.Reset();Query("chain_first");fixture.Previous=new(fixture.Root.X,fixture.Root.Y,fixture.Root.Z);fixture.Root.Z=25;Query("chain_away");
//Fixed finite original observations; no generated trace is needed for normal tests.
string[] references=[
"1|0,0,20,1|40|10|0,0,10,1|1|1|1|1|1|0",
"1|0,0,20,1|40|10|0,0,10,1|0|1|0|1|1|0",
"1|0,0,15,1|40|10|0,0,10,1|1|1|0|0|0|0",
"0|0,0,15,1|77|88|111,222,333,1|1|0|0|0|0|0",
"0|0,0,200,1|77|88|111,222,333,1|1|0|0|0|0|0",
"1|0,0,15,1|77|88|111,222,333,1|1|0|0|0|0|0",
"0|0,0,51,1|77|88|111,222,333,1|1|0|0|0|0|0",
"0|0,0,80,1|-1|10|111,222,333,1|1|0|0|0|0|0",
"1|0,0,20,1|40|10|0,0,10,1|1|1|1|1|1|0",
"1|777,888,999,2|40|10|0,0,10,1|1|1|0|0|0|0",
"1|0,0,20,1|40|10|0,0,10,1|1|1|1|1|1|0",
"1|0,0,20,1|40|10|0,0,10,1|1|1|1|1|1|0",
"1|0,45,20,1|40|10|0,45,10,1|1|1|1|1|1|0",
"1|100,0,20,1|40|10|100,0,10,1|1|1|1|1|1|0",
"1|20,0,0,1|40|10|10,0,0,1|1|1|1|1|1|0",
"1|0,0,20,1|40|10|0,0,10,1|1|1|1|1|1|0",
"0|0,0,25,1|-1|10|0,0,10,1|1|1|1|1|1|0"
];
for(var i=0;i<rows.Count;i++)
if(rows[i].Reference()!=references[i])throw new Exception($"{rows[i].Name}: {rows[i].Reference()} != {references[i]}");
if(args.Length==2&&args[0]=="--native")
{
using var data=JsonDocument.Parse(File.ReadAllText(args[1]));
var cases=data.RootElement.GetProperty("cases").EnumerateArray().ToArray();
if(cases.Length!=rows.Count)throw new Exception("Original case count");
for(var i=0;i<rows.Count;i++)
{
var actual=JsonSerializer.SerializeToElement(rows[i],new JsonSerializerOptions{PropertyNamingPolicy=JsonNamingPolicy.CamelCase});
foreach(var property in cases[i].EnumerateObject())
if(!actual.TryGetProperty(property.Name,out var value)||!JsonElement.DeepEquals(value,property.Value))
throw new Exception($"{rows[i].Name}: original {property.Name} differs");
}
var arguments=JsonSerializer.SerializeToElement(fixture.KernelArguments);
if(!JsonElement.DeepEquals(arguments,data.RootElement.GetProperty("kernel_triangles")))
throw new Exception("Original12 kernel triangles/normals differ");
Console.WriteLine("Matched17 complete original dynamic/context observations and12 kernel triangle/normal arguments.");
}
else if(args.Length!=0)throw new ArgumentException("Use --native <observations.json>");
Console.WriteLine("PASS17 dynamic context/reclip compositions; live collision admission unchanged.");
sealed record Observation(string Name,int Contact,float[] Root,float Face,float Edge,float[] Hit,
int Last,int DynamicCallbacks,int StaticCallbacks,int Setters,int Passes,int Mode)
{
public string Reference()=>string.Join('|',Contact,string.Join(',',Root),Face,Edge,string.Join(',',Hit),
Last,DynamicCallbacks,StaticCallbacks,Setters,Passes,Mode);
}
sealed class Fixture(bool constructionWinding=false)
{
public Vector4 Root;
public Vector3 Previous,BoxRoot;
public bool Inhibit,Geometry,StaticGeometry,ForceStatic,Rotate,Mutate;
public int Mode;
public StaticCollisionContext Context=null!;
private readonly OriginalStaticCollision world=new([]);
private readonly CameraRayPolygon stale=new(new(new(-100,-100,0),new(-100,100,0),new(100,100,0)),Vector3.UnitZ);
private int dynamicCallbacks,staticCallbacks,setters;
public List<float[][]> KernelArguments {get;}=[];
public void Reset()
{
Root=new(0,0,15,1);Previous=new(0,0,50);BoxRoot=default;
Inhibit=ForceStatic=Rotate=Mutate=false;Geometry=StaticGeometry=true;Mode=0;
Context=new(88,77){Nearest=new(77,88,new(111,222,333)),LastPolygon=stale};
dynamicCallbacks=staticCallbacks=setters=0;
}
private void Notify(CameraRayPolygon? polygon)
{
if(polygon is null)dynamicCallbacks++;else staticCallbacks++;
if(Mutate)Root=new(777,888,999,2);
}
public Observation Query(string name)
{
var current=new Vector3(Root.X,Root.Y,Root.Z);
var transform=Rotate?new Matrix4x4(0,0,-1,0,0,1,0,0,1,0,0,0,BoxRoot.X,BoxRoot.Y,BoxRoot.Z,1)
:Matrix4x4.CreateTranslation(BoxRoot);
var box=new DynamicBoxCollider(1,BoxRoot,new(-50,-50,-10),new(50,50,10),transform,40);
var sphere=new DynamicSphereMotion(Previous,current,10,GeometryCollisionEnabled:Geometry,Mode:Mode);
bool contact=false;
if(DynamicSceneCollision.BroadPhase(box,sphere))
{
if(Mode==2)contact=true;
else
{
Context.Nearest=Context.Nearest with{FaceDistance=-1,EdgeDistance=10};
var candidate=current;
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);
//47A5E0's vector argument pushes reverse the constructed
//triangle winding at the kernel boundary; normal is retained.
var kernelTriangle=constructionWinding?triangle:new SupportTriangle(triangle.A,triangle.C,triangle.B);
if(name=="face_stale_polygon")KernelArguments.Add([
[kernelTriangle.A.X,kernelTriangle.A.Y,kernelTriangle.A.Z,1],
[kernelTriangle.B.X,kernelTriangle.B.Y,kernelTriangle.B.Z,1],
[kernelTriangle.C.X,kernelTriangle.C.Y,kernelTriangle.C.Z,1],
[normal.X,normal.Y,normal.Z,0]]);
var hit=OriginalSphereResponse.Resolve(kernelTriangle,normal,Previous,candidate,10,Context.Nearest);
if(hit.Contact==SphereContact.None)continue;
candidate=hit.Position;Context.Nearest=hit.State;Notify(null);
}
contact=Context.Nearest.FaceDistance!=-1||Context.Nearest.EdgeDistance<10;
if(contact&&!Inhibit)
{
var clipped=world.Move(Previous,candidate-Previous,10,Context,edges:false,
lowerEdges:ForceStatic,geometryEnabled:StaticGeometry);
if(clipped.CallbackPolygon is{} polygon)Notify(polygon);
Root=new(clipped.Position,1);setters++;
}
}
}
var p=Context.Nearest.Hit??throw new Exception("Seeded hit lost");
return new(name,contact?1:0,[Root.X,Root.Y,Root.Z,Root.W],Context.Nearest.FaceDistance,
Context.Nearest.EdgeDistance,[p.X,p.Y,p.Z,1],Context.LastPolygon==stale?1:0,
dynamicCallbacks,staticCallbacks,setters,Context.Passes,0);
}
}