Reconstruct lab story, interactions, patrol and jet pack practice flow
This commit is contained in:
1 parent
58424120bb
commit
c929f942e1
84 files changed
+7957
-305
No files matched your search
@@ -0,0 +1,167 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace GamePort.Game;
|
||||
|
||||
public readonly record struct SupportTriangle(Vector3 A, Vector3 B, Vector3 C, int ShapeId = 0, int PolygonId = 0);
|
||||
public readonly record struct SupportHit(Vector3 Position, int ShapeId, int PolygonId, float Distance);
|
||||
public delegate SupportHit? SupportSegmentQuery(Vector3 start, Vector3 end, bool twoSided);
|
||||
|
||||
public readonly record struct SupportBody(
|
||||
Vector3 Position, Vector3 PreviousPosition, Vector3 Velocity, Vector3 Acceleration,
|
||||
float Radius, float Factor, bool PreventLowering = true);
|
||||
|
||||
public readonly record struct SupportResult(
|
||||
SupportBody Body, bool Supported, bool Landed, SupportHit? Hit, Vector3 SupportAnchor);
|
||||
|
||||
/// <summary>
|
||||
/// Bounded translation of EXE47B890 and its source triangle query. This handles
|
||||
/// root support, not the wall/ceiling sphere solver or entity collisions. Lookup
|
||||
/// tables use the recovered initializer constants with modern trig; exact x87
|
||||
/// table values and full original frame ordering still require comparison.
|
||||
/// </summary>
|
||||
public static class OriginalBodySupport
|
||||
{
|
||||
public const float JimmyRadius = 60;
|
||||
public const float JimmyFactor = 1.265f;
|
||||
public const float GoddardRadius = 130;
|
||||
public const float GoddardFactor = 0.3f;
|
||||
private const float TriangleEpsilon = 1.1920928955078125e-7f;
|
||||
private static readonly float[] sine = BuildSine();
|
||||
private static readonly ushort[] halfAcos = BuildHalfAcos();
|
||||
|
||||
/// <summary>OMT1001FF70: largest component divided by a quantized sine lookup.</summary>
|
||||
public static float QuickMagnitude(Vector3 vector)
|
||||
{
|
||||
RequireFinite(vector);
|
||||
var x = (double)MathF.Abs(vector.X);
|
||||
var y = (double)MathF.Abs(vector.Y);
|
||||
var z = (double)MathF.Abs(vector.Z);
|
||||
if (x > y) { if (x > z) (x, z) = (z, x); }
|
||||
else if (y > z) (y, z) = (z, y);
|
||||
// The original passes 0/0 through _ftol. The port handles zero directly
|
||||
// instead of depending on an invalid x87 conversion/control-word mode.
|
||||
if (z == 0) return 0;
|
||||
var otherSquared = x * x + y * y;
|
||||
var index = (int)(otherSquared * 16384 / (otherSquared + z * z)) & 0x3fff;
|
||||
return (float)(z / sine[halfAcos[index] & 0x3fff]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Apply support after motion/wall resolution. The caller captures previous
|
||||
/// grounded state and clears its current grounded flag before this phase.
|
||||
/// A successful support preserves horizontal velocity/acceleration, sets their
|
||||
/// Y components to zero and emits a landing only on an ungrounded-to-grounded step.
|
||||
/// </summary>
|
||||
public static SupportResult Resolve(SupportBody body, bool wasGrounded,
|
||||
SupportSegmentQuery query, bool geometryEnabled = true)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(query);
|
||||
RequireFinite(body.Position); RequireFinite(body.PreviousPosition);
|
||||
RequireFinite(body.Velocity); RequireFinite(body.Acceleration);
|
||||
if (!float.IsFinite(body.Radius) || body.Radius <= 0 || !float.IsFinite(body.Factor))
|
||||
throw new ArgumentOutOfRangeException(nameof(body), "Finite support factor and positive finite radius required");
|
||||
var unchanged = new SupportResult(body, false, false, null, Vector3.Zero);
|
||||
if (!geometryEnabled) return unchanged;
|
||||
|
||||
var product = body.Radius * body.Factor;
|
||||
var top = MathF.Max(body.Position.Y, body.PreviousPosition.Y);
|
||||
var motionHit = query(body.PreviousPosition, body.Position, false);
|
||||
if (motionHit is { } crossed)
|
||||
top += QuickMagnitude(body.Position - body.PreviousPosition)
|
||||
- QuickMagnitude(crossed.Position - body.PreviousPosition);
|
||||
// EXE48D920 is .1f, not .5f. Final root offset always uses r*factor.
|
||||
var clearance = body.Factor == 1 || body.Factor < 0.1f ? body.Radius : product;
|
||||
var start = new Vector3(body.Position.X, top + clearance, body.Position.Z);
|
||||
var end = new Vector3(body.Position.X, body.Position.Y - 2 * body.Radius, body.Position.Z);
|
||||
var hit = query(start, end, false);
|
||||
if (hit is not { } floor) return unchanged;
|
||||
var supportedY = floor.Position.Y + product;
|
||||
// EXE47BAF3 queries iface208. It rejects a downward root correction,
|
||||
// independently of vertical velocity, when this flag is enabled.
|
||||
if (body.PreventLowering && body.Position.Y > supportedY) return unchanged;
|
||||
var grounded = body with {
|
||||
Position = new(body.Position.X, supportedY, body.Position.Z),
|
||||
Velocity = new(body.Velocity.X, 0, body.Velocity.Z),
|
||||
Acceleration = new(body.Acceleration.X, 0, body.Acceleration.Z)
|
||||
};
|
||||
return new SupportResult(grounded, true, !wasGrounded, floor, new Vector3(0, supportedY, 0));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EXE469B70/47C6E0 source-winding segment query. Null-material polygons must
|
||||
/// be included. The caller supplies the statically registered polygons;
|
||||
/// checkpoint/material and game-specific registration filters are separate.
|
||||
/// A linear scan replaces the original 260x260 broad phase. Equal-distance
|
||||
/// selection follows input order, which can differ from original grid order.
|
||||
/// </summary>
|
||||
public static SupportHit? Trace(IEnumerable<SupportTriangle> triangles,
|
||||
Vector3 start, Vector3 end, bool twoSided = false)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(triangles);
|
||||
RequireFinite(start); RequireFinite(end);
|
||||
var direction = end - start;
|
||||
var length = QuickMagnitude(direction);
|
||||
if (length == 0) return null;
|
||||
direction /= length;
|
||||
SupportHit? nearest = null;
|
||||
var nearestDistance = length;
|
||||
foreach (var triangle in triangles)
|
||||
{
|
||||
RequireFinite(triangle.A); RequireFinite(triangle.B); RequireFinite(triangle.C);
|
||||
var edge1 = triangle.B - triangle.A;
|
||||
var edge2 = triangle.C - triangle.A;
|
||||
var p = Cross(direction, edge2);
|
||||
var determinant = Dot(edge1, p);
|
||||
// Source indices use the opposite sign from a conventional right-
|
||||
// handed visible-front-face test. Preserve the actual serialized order.
|
||||
if (twoSided ? MathF.Abs(determinant) < TriangleEpsilon : determinant > -TriangleEpsilon)
|
||||
continue;
|
||||
var inverse = 1 / determinant;
|
||||
var relative = start - triangle.A;
|
||||
var u = Dot(relative, p) * inverse;
|
||||
if (u < 0 || u > 1) continue;
|
||||
var q = Cross(relative, edge1);
|
||||
var v = Dot(q, direction) * inverse;
|
||||
if (v < 0 || u + v > 1) continue;
|
||||
var distance = Dot(q, edge2) * inverse;
|
||||
// Source accepts the beginning, but rejects its endpoint and equal
|
||||
// distance candidates. No invented extra ray distance or skin is used.
|
||||
if (distance < 0 || distance >= length) continue;
|
||||
var position = start + direction * distance;
|
||||
var fromStart = QuickMagnitude(position - start);
|
||||
if (fromStart >= nearestDistance) continue;
|
||||
nearestDistance = fromStart;
|
||||
nearest = new SupportHit(position, triangle.ShapeId, triangle.PolygonId, fromStart);
|
||||
}
|
||||
return nearest;
|
||||
}
|
||||
|
||||
private static float Dot(Vector3 a, Vector3 b) =>
|
||||
(float)((double)a.X * b.X + (double)a.Y * b.Y + (double)a.Z * b.Z);
|
||||
private static Vector3 Cross(Vector3 a, Vector3 b) => new(
|
||||
(float)((double)a.Y * b.Z - (double)a.Z * b.Y),
|
||||
(float)((double)a.Z * b.X - (double)a.X * b.Z),
|
||||
(float)((double)a.X * b.Y - (double)a.Y * b.X));
|
||||
|
||||
private static float[] BuildSine()
|
||||
{
|
||||
var table = new float[16384];
|
||||
// DLL100EDC21 uses this stored double, rather than Math.Tau/16384.
|
||||
for (var i = 0; i < table.Length; i++) table[i] = (float)Math.Sin(i * 0.0003834951969604492);
|
||||
table[0] = table[8192] = 0; table[4096] = 1; table[12288] = -1;
|
||||
return table;
|
||||
}
|
||||
private static ushort[] BuildHalfAcos()
|
||||
{
|
||||
var table = new ushort[16384];
|
||||
// DLL100EDC8C..100EDCB4 truncates half of the acos angular-unit result.
|
||||
for (var i = 0; i < table.Length; i++)
|
||||
table[i] = (ushort)(Math.Acos(i * 0.0001220703125 - 1) * 2607.5945876921437 * 0.5);
|
||||
return table;
|
||||
}
|
||||
private static void RequireFinite(Vector3 vector)
|
||||
{
|
||||
if (!float.IsFinite(vector.X) || !float.IsFinite(vector.Y) || !float.IsFinite(vector.Z))
|
||||
throw new ArgumentOutOfRangeException(nameof(vector), "Finite source coordinates required");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user