Implement original gadget panel and playable shrink ray

This commit is contained in:
UnixNight committed 2026-10-27 12:18:45 -04:00
1 parent 0272f7bef7
commit 7b5230ceb8
57 files changed
+3545 -48

No files matched your search

@@ -0,0 +1,84 @@
using System.Numerics;
using System.Text.Json;
using GamePort.Assets;
using GamePort.Game;
namespace GamePort.Application;
/// <summary>Original screens.omt gadget art in the existing uniformly scaled UI viewport.</summary>
public sealed class GadgetMenuOverlay
{
private sealed record Quad(int Vertex, uint FirstIndex, uint Width, uint Height);
private readonly Dictionary<int, Quad> quads = [];
public Mesh Mesh { get; }
public GadgetMenuOverlay(Mesh combined, string manifest)
{
var full = Path.GetFullPath(manifest);
if (new FileInfo(full).Length > 128 * 1024) throw new InvalidDataException("Gadget manifest exceeds limit");
var directory = Path.GetDirectoryName(full)!;
using var document = JsonDocument.Parse(File.ReadAllBytes(full), new JsonDocumentOptions { MaxDepth = 12 });
var root = document.RootElement;
if (root.GetProperty("schema").GetInt32() != 1 || root.GetProperty("referenceWidth").GetInt32() != 640
|| root.GetProperty("referenceHeight").GetInt32() != 480)
throw new InvalidDataException("Unsupported gadget resource format");
var entries = root.GetProperty("textures");
if (entries.GetArrayLength() != 54) throw new InvalidDataException("Expected54 original gadget canvases");
var vertices = combined.Vertices.ToList();
var indices = combined.Indices.ToList();
var draws = combined.Draws.ToList();
if (!combined.HasExplicitDraws && draws.Count == 0 && indices.Count != 0)
draws.Add(new(0, (uint)indices.Count, -1));
var textures = new Dictionary<int, PngImage>(combined.Textures);
long total = 0;
foreach (var entry in entries.EnumerateArray())
{
var id = entry.GetProperty("id").GetInt32();
var width = entry.GetProperty("width").GetUInt32();
var height = entry.GetProperty("height").GetUInt32();
if (width is 0 or > 640 || height is 0 or > 480) throw new InvalidDataException("Invalid gadget canvas dimensions");
var path = Path.GetFullPath(Path.Combine(directory, entry.GetProperty("path").GetString()!));
if (!path.StartsWith(directory + Path.DirectorySeparatorChar, StringComparison.Ordinal))
throw new InvalidDataException("Gadget canvas escapes prepared directory");
var bytes = checked((long)width * height * 4);
total += bytes;
if (total > 16 * 1024 * 1024 || new FileInfo(path).Length != bytes)
throw new InvalidDataException("Invalid gadget canvas pixel count");
var texture = int.MinValue;
while (textures.ContainsKey(texture)) texture++;
textures.Add(texture, new(width, height, File.ReadAllBytes(path)));
if (!quads.TryAdd(id, new(vertices.Count, (uint)indices.Count, width, height)))
throw new InvalidDataException("Duplicate gadget canvas");
var first = (uint)vertices.Count;
vertices.AddRange([new(Vector3.Zero, new(0,0), Vector3.One), new(Vector3.Zero, new(1,0), Vector3.One),
new(Vector3.Zero, new(1,1), Vector3.One), new(Vector3.Zero, new(0,1), Vector3.One)]);
indices.AddRange([first, first + 1, first + 2, first, first + 2, first + 3]);
draws.Add(new((uint)indices.Count - 6, 6, texture) { ScreenSpace = true, Visible = false });
}
Mesh = new(vertices.ToArray(), indices.ToArray()) { Draws = draws.ToArray(), HasExplicitDraws = true,
Textures = textures, CollisionPolygons = combined.CollisionPolygons,
HasCollisionMaterialIds = combined.HasCollisionMaterialIds };
}
public void Update(Mesh destination, IReadOnlyList<GadgetCanvas> visible, uint width, uint height)
{
if (destination.Vertices.Length < Mesh.Vertices.Length || visible.Count > quads.Count)
throw new ArgumentException("Destination omits gadget overlay ranges");
foreach (var quad in quads.Values) Draw(quad).Visible = false;
var viewport = new UiViewport(width, height);
foreach (var canvas in visible)
{
if (!quads.TryGetValue(canvas.Id, out var quad)) throw new InvalidDataException("Unprepared gadget canvas");
var draw = Draw(quad);
if (draw.Visible) throw new ArgumentException("Gadget canvas occurs more than once");
draw.Visible = true;
var point = canvas.Position + viewport.CenterOffset;
destination.Vertices[quad.Vertex].Position = viewport.ToNdc(point);
destination.Vertices[quad.Vertex + 1].Position = viewport.ToNdc(point + new Vector2(quad.Width, 0));
destination.Vertices[quad.Vertex + 2].Position = viewport.ToNdc(point + new Vector2(quad.Width, quad.Height));
destination.Vertices[quad.Vertex + 3].Position = viewport.ToNdc(point + new Vector2(0, quad.Height));
}
MeshDraw Draw(Quad quad) => destination.Draws.FirstOrDefault(d => d.FirstIndex == quad.FirstIndex
&& d.IndexCount == 6 && d.ScreenSpace) ?? throw new ArgumentException("Destination omits gadget draws");
}
}
@@ -47,14 +47,14 @@ public sealed class PreviewAnimationLibrary
if (ReferenceEquals(clip, Current)) Name = label;
}
foreach (var (pose, folder) in new[] { (PlayerPose.Idle, "jimstop"), (PlayerPose.Run, "jimrun"),
(PlayerPose.Backpedal, "jimbackpedal"), (PlayerPose.Jump, "jimjump"), (PlayerPose.Fall, "jimfall"), (PlayerPose.Fly, "jimfly") })
(PlayerPose.Backpedal, "jimbackpedal"), (PlayerPose.Jump, "jimjump"), (PlayerPose.Fall, "jimfall"), (PlayerPose.Fly, "jimfly"), (PlayerPose.Shoot, "jimshoot") })
{
var path = Path.Combine(parent, folder, "animation.json");
if (!File.Exists(path)) continue;
movement[pose] = Load(path);
}
foreach (var (action, folder) in new[] { ("STOP", "jimstop"), ("TALK", "jimtalk"), ("RUN", "jimrun"),
("JUMP", "jimjump"), ("FALL", "jimfall"), ("SCRATCH", "jimscratch"), ("FLY", "jimfly") })
("JUMP", "jimjump"), ("FALL", "jimfall"), ("SCRATCH", "jimscratch"), ("FLY", "jimfly"), ("SHOOT", "jimshoot") })
{
var path = Path.Combine(parent, folder, "animation.json");
if (File.Exists(path)) actions[action] = Load(path);
@@ -0,0 +1,40 @@
using System.Numerics;
using GamePort.Assets;
using GamePort.Game;
namespace GamePort.Application;
/// <summary>Owned original RAY mesh and three texture phases; material alpha0.5, additive default.</summary>
public sealed class ShrinkRayVisuals
{
private readonly MorphAnimation clip;
private readonly int offset;
private readonly MeshDraw[] phases;
public Mesh Mesh { get; }
public ShrinkRayVisuals(Mesh combined, string preparedRoot, string originalRoot)
{
clip = new(Path.Combine(preparedRoot,"actors/shrink-ray/ray/animation.json"));
offset = combined.Vertices.Length;
var id = int.MinValue + 12000;
while (combined.Textures.ContainsKey(id)) id++;
var mesh = clip.AppendTo(combined,id);
phases = new MeshDraw[3]; phases[0] = mesh.Draws[^1];
for (var phase = 1; phase < phases.Length; phase++)
{
do id++; while (mesh.Textures.ContainsKey(id));
mesh.Textures.Add(id,PngImage.Load(AssetPath.Resolve(originalRoot,$"png/ray000{phase}.png")));
phases[phase] = new(phases[0].FirstIndex,phases[0].IndexCount,id);
}
foreach (var draw in phases) { draw.Visible = false; draw.Opacity = .5f; draw.Blend = MeshBlend.Additive; }
Mesh = mesh with {Draws=[.. mesh.Draws,phases[1],phases[2]]};
}
public void Update(Mesh destination, ShrinkRay ray, double seconds)
{
for (var phase = 0; phase < phases.Length; phase++) phases[phase].Visible = ray.Visible && ray.TexturePhase == phase;
if (!ray.Visible) return;
var model = Matrix4x4.CreateRotationY(-ray.Yaw * MathF.PI / 180) * Matrix4x4.CreateTranslation(ray.Position);
clip.Sample(seconds,destination.Vertices.AsSpan(offset,clip.Base.Vertices.Length),model);
}
}
@@ -0,0 +1,73 @@
using System.Numerics;
using GamePort.Assets;
using GamePort.Game;
namespace GamePort.Application;
/// <summary>Original3SOL WALK/SHRINK morphs. No combat or patrol AI is inferred.</summary>
public sealed class ShrinkSoldierVisuals
{
private sealed class Visual(ShrinkSoldier soldier, int offset, MeshDraw draw)
{
public ShrinkSoldier Soldier = soldier;
public int Offset = offset;
public MeshDraw Draw = draw;
public bool Shrunk;
public double Time;
}
private readonly List<Visual> visuals = [];
private readonly MorphAnimation walk, shrink;
public Mesh Mesh { get; }
public ShrinkSoldierVisuals(Mesh combined, IReadOnlyList<ShrinkSoldier> soldiers, string preparedRoot, string level)
{
var vr = level.Length == 4 && level.StartsWith("VR0",StringComparison.Ordinal) && level[3] is >= '1' and <= '8';
var family = Path.Combine(preparedRoot,"actors",vr ? "soldier-vr" : "soldier");
walk = new(Path.Combine(family,"soldwalk/animation.json"));
shrink = new(Path.Combine(family,"soldshrink/animation.json"));
if (walk.Base.Vertices.Length != shrink.Base.Vertices.Length || !walk.Base.Indices.SequenceEqual(shrink.Base.Indices))
throw new InvalidDataException("Original soldier morph topology differs");
foreach (var soldier in soldiers)
{
var offset = combined.Vertices.Length;
var id = int.MinValue + 13000 + visuals.Count;
while (combined.Textures.ContainsKey(id)) id++;
combined = walk.AppendTo(combined,id);
visuals.Add(new(soldier,offset,combined.Draws[^1]));
}
Mesh = combined; Update(0,Mesh);
}
public void Update(double seconds, Mesh destination)
{
foreach (var visual in visuals)
{
if (visual.Shrunk != visual.Soldier.Shrunk)
{
//False selector preserves frame ordinal; within-frame clock is
//an explicit port approximation. WALK(true) restarts at frame0.
var ordinal = (int)Math.Floor(visual.Time / walk.SampleInterval) % walk.FrameCount;
visual.Time = visual.Soldier.Shrunk ? Math.Min(ordinal,shrink.FrameCount - 1) * shrink.SampleInterval : 0;
visual.Shrunk = visual.Soldier.Shrunk;
}
visual.Time += seconds;
var actor = visual.Soldier.Actor;
var rotation = Matrix4x4.CreateRotationX(actor.Rotation.X * MathF.PI / 180)
* Matrix4x4.CreateRotationY(-actor.Rotation.Y * MathF.PI / 180)
* Matrix4x4.CreateRotationZ(actor.Rotation.Z * MathF.PI / 180);
var clip = visual.Shrunk ? shrink : walk;
clip.Sample(visual.Time,destination.Vertices.AsSpan(visual.Offset,clip.Base.Vertices.Length),
rotation * Matrix4x4.CreateTranslation(actor.Position),!visual.Shrunk);
visual.Draw.Visible = actor.Visible;
}
}
public float RenderedHeight(string tag, Mesh destination)
{
var visual = visuals.Single(v => v.Soldier.Actor.Tag == tag);
var vertices = destination.Vertices.AsSpan(visual.Offset,walk.Base.Vertices.Length);
var low = float.PositiveInfinity; var high = float.NegativeInfinity;
foreach (var vertex in vertices) { low = MathF.Min(low,vertex.Position.Y); high = MathF.Max(high,vertex.Position.Y); }
return high - low;
}
}