acdream/src/AcDream.Core/Meshing/CellMesh.cs
Erik a71db90310 feat(net): Phase 6.6 — parse UpdateMotion (0xF74C) into MotionUpdated event
Server sends UpdateMotion whenever an entity's motion state changes:
NPCs starting a walk cycle, creatures switching to a combat stance,
doors opening, a player waving, etc. Phase 6.1-6.4 already handles
rendering different (stance, forward-command) pairs for the INITIAL
CreateObject, but without this message NPCs freeze in whatever pose
they spawned with and never transition to walking/fighting.

Added UpdateMotion.TryParse with the same ServerMotionState the
CreateObject path uses, reached via a slightly different outer
layout (guid + instance seq + header'd MovementData; the MovementData
starts with the 8-byte sequence/autonomous header this time rather
than being preceded by a length field). Only the (stance, forward-
command) pair is extracted — same subset CreateObject grabs.

WorldSession dispatches MotionUpdated(guid, state) when a 0xF74C
body parses successfully. The App-side wiring (guid→entity lookup
and AnimatedEntity cycle swap) is intentionally deferred to a
separate commit because it touches GameWindow which is currently
being edited by the Phase 9.1 translucent-pass work.

89 Core.Net tests (was 83, +6 for UpdateMotion coverage).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-11 20:33:26 +02:00

128 lines
5.2 KiB
C#

using System.Numerics;
using AcDream.Core.Terrain;
using DatReaderWriter;
using DatReaderWriter.DBObjs;
using DatReaderWriter.Types;
namespace AcDream.Core.Meshing;
/// <summary>
/// Builds renderable sub-meshes from an EnvCell's room geometry (walls,
/// floors, ceilings). The geometry lives in the linked Environment dat:
/// EnvCell.EnvironmentId → Environment → Cells[CellStructure] → CellStruct.
/// This mirrors GfxObjMesh.Build but reads surfaces from EnvCell.Surfaces
/// (not from the CellStruct itself) and uses the same fan-triangulation
/// and per-surface deduplication pattern.
/// </summary>
public static class CellMesh
{
/// <summary>
/// Walk a CellStruct's polygons and produce one <see cref="GfxObjSubMesh"/>
/// per referenced Surface. Surfaces are resolved from <paramref name="envCell"/>.Surfaces
/// (OR'd with 0x08000000 to form the full dat id). Polygons are triangulated as fans.
/// </summary>
/// <param name="envCell">The EnvCell that owns the surface list.</param>
/// <param name="cellStruct">The CellStruct containing the polygon + vertex geometry.</param>
/// <param name="dats">
/// Optional dat collection used to read Surface.Type flags and set
/// <see cref="GfxObjSubMesh.Translucency"/>. When null (e.g. offline tests)
/// all sub-meshes default to <see cref="TranslucencyKind.Opaque"/>.
/// </param>
public static IReadOnlyList<GfxObjSubMesh> Build(EnvCell envCell, CellStruct cellStruct, DatCollection? dats = null)
{
// Group output vertices and indices per surface dat id.
var perSurface = new Dictionary<uint, (List<Vertex> Vertices, List<uint> Indices, Dictionary<(int pos, int uv), uint> Dedupe)>();
foreach (var kvp in cellStruct.Polygons)
{
var poly = kvp.Value;
if (poly.VertexIds.Count < 3)
continue; // degenerate polygon
// Skip if NoPos stippling is set (polygon has no positive surface geometry).
if (poly.Stippling.HasFlag(DatReaderWriter.Enums.StipplingType.NoPos))
continue;
int surfaceIdx = poly.PosSurface;
if (surfaceIdx < 0 || surfaceIdx >= envCell.Surfaces.Count)
continue; // out-of-range surface index
// Surfaces on EnvCell are unqualified ids; OR with 0x08000000 for the full dat id.
uint surfaceId = (uint)envCell.Surfaces[surfaceIdx] | 0x08000000u;
if (!perSurface.TryGetValue(surfaceId, out var bucket))
{
bucket = (new List<Vertex>(), new List<uint>(), new Dictionary<(int, int), uint>());
perSurface[surfaceId] = bucket;
}
// Collect output vertex indices for this polygon.
var polyOut = new List<uint>(poly.VertexIds.Count);
bool skipPoly = false;
for (int i = 0; i < poly.VertexIds.Count; i++)
{
int posIdx = poly.VertexIds[i];
int uvIdx = i < poly.PosUVIndices.Count ? poly.PosUVIndices[i] : 0;
if (!cellStruct.VertexArray.Vertices.TryGetValue((ushort)posIdx, out var sw))
{
skipPoly = true;
break;
}
var texcoord = uvIdx >= 0 && uvIdx < sw.UVs.Count
? new Vector2(sw.UVs[uvIdx].U, sw.UVs[uvIdx].V)
: Vector2.Zero;
// Use normal from vertex data; fall back to up-vector if missing.
var normal = sw.Normal != Vector3.Zero ? sw.Normal : Vector3.UnitZ;
var key = (posIdx, uvIdx);
if (!bucket.Dedupe.TryGetValue(key, out var outIdx))
{
outIdx = (uint)bucket.Vertices.Count;
bucket.Vertices.Add(new Vertex(sw.Origin, normal, texcoord, TerrainLayer: 0));
bucket.Dedupe[key] = outIdx;
}
polyOut.Add(outIdx);
}
if (skipPoly || polyOut.Count < 3)
continue;
// Fan triangulation: (v0, v1, v2), (v0, v2, v3), ...
for (int i = 1; i < polyOut.Count - 1; i++)
{
bucket.Indices.Add(polyOut[0]);
bucket.Indices.Add(polyOut[i]);
bucket.Indices.Add(polyOut[i + 1]);
}
}
// Emit one sub-mesh per surface.
var result = new List<GfxObjSubMesh>(perSurface.Count);
foreach (var kvp in perSurface)
{
// Resolve Surface.Type flags when a DatCollection is available so the
// renderer can split the draw into opaque and translucent passes.
var translucency = TranslucencyKind.Opaque;
if (dats is not null)
{
var surface = dats.Get<Surface>(kvp.Key);
if (surface is not null)
translucency = TranslucencyKindExtensions.FromSurfaceType(surface.Type);
}
result.Add(new GfxObjSubMesh(
SurfaceId: kvp.Key,
Vertices: kvp.Value.Vertices.ToArray(),
Indices: kvp.Value.Indices.ToArray())
{
Translucency = translucency,
});
}
return result;
}
}