acdream/tests/AcDream.Core.Tests/CharGen/ChargenAppearanceFactoryTests.cs
Erik 55bfd9ca82 feat(chargen): Campaign CC slice CC6a — index→ObjDesc factory + preview renderer foundation
Delivers the CC6a foundation half of the chargen 3D preview: the missing
index->ObjDesc appearance factory the campaign plan's acdream-seams
section named, plus a static-pose offscreen renderer following
PrivateEntityViewportRenderer's proven paperdoll/appraisal architecture.
Page mount, spin/color-wheel controls, and rotate/zoom behavior stay out
of scope per the CC4-parallel worktree contract (CC6b, after CC4 merges).

Core (src/AcDream.Core/CharGen/, pure, no Chorizite on public surfaces):
ChargenAppearanceFactory.TryCompose ports gmCG3DView::Update @0x004EE9D0's
ObjDesc rebuild in its exact decompiled order - base body, hair style,
clothing in retail's own Headgear/Trousers/Shirt/Footwear order (not the
UI tab order or the wire's field order, both of which differ), eyes
(bald-aware), nose, mouth, then the unconditional skin subpalette, hair
color, eye color. ChargenPalSetMath ports PalSet::GetPaletteID's
shade-to-index formula, cross-checked three ways (decomp control flow,
ACE's PaletteSet.GetPaletteID "Taken from acclient.c" citation, ACViewer's
identical slider math). ChargenPalSet/ChargenClothingTable are pure
projections behind IChargenPalSetSource/IChargenClothingTableSource so the
factory itself never touches a dat.

Content (src/AcDream.Content/CharGen/): ChargenAppearanceCatalog is the
cached dat-backed implementation of those two source interfaces, mirroring
ChargenTableReader's no-leak discipline.

App (src/AcDream.App/Rendering/): ChargenPreviewRenderer is a third facade
over PrivateEntityViewportRenderer beside PaperdollViewportRenderer and
CreatureAppraisalViewportRenderer - no existing rendering file touched.
ChargenPreviewCamera carries the four retail-verbatim per-heritage eye
profiles from gmCGAppearancePage::Update @0x0047E8F0 (cross-checked
against ZoomIn/ZoomOut's identical literals) plus the recovered rotation
(3.0 s/revolution) and zoom-tween (0.6 s, reconstructed from the
decompiler's garbled float literals - the plan's own "measure if it
matters" note is resolved, not garbled beyond recovery). Rotation applies
to the character model, not the camera, per gmCGAppearancePage::DoRotation.
ChargenPreviewEntityBuilder resolves Setup/GfxObj/Surface/Animation itself
(there is no live entity yet), reusing DatLiveEntityProjectionMaterializer's
surface-override algorithm and RetailPaperdollPoseApplicator's held-pose
technique, generalized to chargen's per-heritage rest-pose DID.

Two register rows filed: TS-83 (the plan-named CC6a static-pose-vs-retail-
idle-loop staging, CC6b to retire) and TS-82 (measured, not assumed - the
un-ported clothing Setup-substitution fallback chain costs nothing for the
9 standard heritages with clothing UI, but Undead's default gear choices
genuinely lack ClothingBaseEffects coverage for Undead's own body Setup).

Tests: ChargenPalSetMathTests, ChargenAppearanceFactoryTests (hand-built
fixtures), ChargenAppearanceCatalogInstalledDatTests (installed-DAT sweep,
all 26 heritage/gender combinations, zero missing PalSet/ClothingTable
ids), ChargenPreviewCameraTests, ChargenPreviewEntityBuilderTests
(installed-DAT-gated, proves a real 34-part Aluvian mesh resolves).
Core.Tests 4767/1 skip, Content.Tests 146/0, App.Tests 5121/6 skips - all
pre-existing skips, zero failures, full solution Release build green.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-15 17:22:06 +02:00

436 lines
20 KiB
C#

using AcDream.Core.CharGen;
namespace AcDream.Core.Tests.CharGen;
/// <summary>
/// Hand-built-fixture tests for <see cref="ChargenAppearanceFactory.TryCompose"/>.
/// Real installed-DAT coverage (every heritage/gender's default selection,
/// verifying no missing PalSet/ClothingTable ids) lives in
/// AcDream.Content.Tests.CharGen.ChargenAppearanceCatalogInstalledDatTests.
/// </summary>
public sealed class ChargenAppearanceFactoryTests
{
private const uint HeritageId = 1u;
private const int GenderKey = 1;
private const uint BodySetupId = 0x0200_0001u;
private const uint AlternateBodySetupId = 0x0200_00FFu;
private const uint BasePaletteId = 0x0400_0001u;
private const uint SkinPalSetId = 0x0F00_0001u;
private const uint HairColorPalSetId = 0x0F00_0002u;
private const uint EyeColorPaletteId = 0x0400_0099u; // direct palette id, no PalSet indirection.
private const uint HeadgearClothingTableId = 0x1900_0001u;
private const uint TrousersClothingTableId = 0x1900_0002u;
private const uint ShirtClothingTableId = 0x1900_0003u;
private const uint FootwearClothingTableId = 0x1900_0004u;
private static ChargenObjDesc MakeObjDesc(uint tag) => new(
0u,
[],
[new ChargenTextureChange((byte)tag, 0x0500_0000u + tag, 0x0500_1000u + tag)],
[new ChargenAnimPartChange((byte)tag, 0x0100_0000u + tag)]);
private static ChargenGenderOptions MakeGender(uint alternateHairSetup = 0u, bool baldHairStyle = false) => new(
GenderKey: GenderKey,
Name: "Male",
Scale: 100u,
SetupId: BodySetupId,
SoundTableId: 0x0900_0001u,
IconId: 0x0600_0001u,
BasePaletteId: BasePaletteId,
SkinPalSetId: SkinPalSetId,
PhysicsTableId: 0x0D00_0001u,
MotionTableId: 0x0900_0002u,
CombatTableId: 0x0000_0001u,
BaseObjDesc: MakeObjDesc(0),
HairColors: [HairColorPalSetId],
HairStyles:
[
new ChargenHairStyle(0x0600_0002u, baldHairStyle, alternateHairSetup, MakeObjDesc(1)),
],
EyeColors: [EyeColorPaletteId],
EyeStrips:
[
new ChargenEyeStrip(0x0600_0003u, 0x0600_0004u, MakeObjDesc(2), MakeObjDesc(20)),
],
NoseStrips: [new ChargenFaceStrip(0x0600_0005u, MakeObjDesc(3))],
MouthStrips: [new ChargenFaceStrip(0x0600_0006u, MakeObjDesc(4))],
Headgears: [new ChargenGearOption("Cap", HeadgearClothingTableId, 0x3000_0001u)],
Shirts: [new ChargenGearOption("Shirt", ShirtClothingTableId, 0x3000_0002u)],
Pants: [new ChargenGearOption("Pants", TrousersClothingTableId, 0x3000_0003u)],
Footwear: [new ChargenGearOption("Boots", FootwearClothingTableId, 0x3000_0004u)],
ClothingColors: [7u]);
private static ChargenOptions MakeOptions(ChargenGenderOptions gender)
{
var heritage = new ChargenHeritageOptions(
HeritageId, "Test", 0x0600_0001u, BodySetupId, BodySetupId,
180u, 100u, [0], [],
new Dictionary<uint, ChargenSkillCost>(), [],
new Dictionary<int, ChargenGenderOptions> { [GenderKey] = gender });
return new ChargenOptions(
[],
new Dictionary<uint, ChargenHeritageOptions> { [HeritageId] = heritage },
new Dictionary<uint, ChargenSkillCost>());
}
/// <summary>One dye choice per clothing table: palette-template id 7,
/// one PalSet, one range (real units 80/16 → packed (10,2)).</summary>
private static ChargenClothingTable MakeClothingTable(uint clothingTableId, uint palSetId, uint bodySetupId)
{
var partChanges = new[] { new ChargenAnimPartChange(5, 0x0100_5000u + clothingTableId) };
var textureChanges = new[] { new ChargenTextureChange(5, 0x0500_5000u, 0x0500_6000u) };
var baseEffects = new Dictionary<uint, ChargenClothingBaseEffect>
{
[bodySetupId] = new ChargenClothingBaseEffect(partChanges, textureChanges),
};
var choice = new ChargenClothingSubPaletteChoice(
palSetId, [new ChargenClothingSubPaletteRange(80u, 16u)]);
var templates = new Dictionary<uint, ChargenClothingPaletteTemplate>
{
[7u] = new ChargenClothingPaletteTemplate([choice]),
};
return new ChargenClothingTable(baseEffects, templates);
}
private sealed class FakePalSetSource : IChargenPalSetSource
{
private readonly Dictionary<uint, ChargenPalSet> _sets = new();
public void Add(uint id, params uint[] paletteIds) => _sets[id] = new ChargenPalSet(paletteIds);
public ChargenPalSet? TryGetPalSet(uint palSetId) => _sets.TryGetValue(palSetId, out var s) ? s : null;
}
private sealed class FakeClothingTableSource : IChargenClothingTableSource
{
private readonly Dictionary<uint, ChargenClothingTable> _tables = new();
public void Add(uint id, ChargenClothingTable table) => _tables[id] = table;
public ChargenClothingTable? TryGetClothingTable(uint clothingTableId) =>
_tables.TryGetValue(clothingTableId, out var t) ? t : null;
}
private static (FakePalSetSource pal, FakeClothingTableSource clothing) MakeSources(uint bodySetupId = BodySetupId)
{
var pal = new FakePalSetSource();
pal.Add(SkinPalSetId, 0x0400_0010u, 0x0400_0011u, 0x0400_0012u);
pal.Add(HairColorPalSetId, 0x0400_0020u, 0x0400_0021u);
var clothingDyePalSetId = 0x0F00_0003u;
pal.Add(clothingDyePalSetId, 0x0400_0030u, 0x0400_0031u);
var clothing = new FakeClothingTableSource();
clothing.Add(HeadgearClothingTableId, MakeClothingTable(HeadgearClothingTableId, clothingDyePalSetId, bodySetupId));
clothing.Add(TrousersClothingTableId, MakeClothingTable(TrousersClothingTableId, clothingDyePalSetId, bodySetupId));
clothing.Add(ShirtClothingTableId, MakeClothingTable(ShirtClothingTableId, clothingDyePalSetId, bodySetupId));
clothing.Add(FootwearClothingTableId, MakeClothingTable(FootwearClothingTableId, clothingDyePalSetId, bodySetupId));
return (pal, clothing);
}
[Fact]
public void TryCompose_ReturnsFalse_WhenHeritageIsUnknown()
{
ChargenOptions options = MakeOptions(MakeGender());
var (pal, clothing) = MakeSources();
bool ok = ChargenAppearanceFactory.TryCompose(
options, heritageId: 999u, GenderKey, ChargenAppearanceSelection.Default,
pal, clothing, out _);
Assert.False(ok);
}
[Fact]
public void TryCompose_ReturnsFalse_WhenGenderIsUnknown()
{
ChargenOptions options = MakeOptions(MakeGender());
var (pal, clothing) = MakeSources();
bool ok = ChargenAppearanceFactory.TryCompose(
options, HeritageId, genderKey: 999, ChargenAppearanceSelection.Default,
pal, clothing, out _);
Assert.False(ok);
}
[Fact]
public void TryCompose_DefaultSelection_ResolvesBodySetupAndUnconditionalSkinSubpalette()
{
ChargenOptions options = MakeOptions(MakeGender());
var (pal, clothing) = MakeSources();
bool ok = ChargenAppearanceFactory.TryCompose(
options, HeritageId, GenderKey, ChargenAppearanceSelection.Default,
pal, clothing, out ChargenAppearanceResult result);
Assert.True(ok);
Assert.Equal(BodySetupId, result.SetupId);
Assert.Equal(BasePaletteId, result.BasePaletteId);
Assert.Empty(result.MissingPalSetIds);
Assert.Empty(result.MissingClothingTableIds);
// UnsetShade (-1.0) is out of [0,1], so GetPaletteIndex returns -1 and
// the skin block is skipped for THIS test's default selection — the
// "unconditional" behavior is that the block always RUNS (always
// attempts the PalSet lookup), not that it always emits an entry.
Assert.DoesNotContain(result.ObjDesc.SubPalettes, sp => sp.Offset == 0);
// Base body's own ObjDesc still lands (tag 0's texture/anim change).
Assert.Contains(result.ObjDesc.AnimPartChanges, c => c.PartIndex == 0);
}
[Fact]
public void TryCompose_SkinShadeSelected_EmitsSkinSubpaletteAtPackedOffsetZeroCountTwentyFour()
{
ChargenOptions options = MakeOptions(MakeGender());
var (pal, clothing) = MakeSources();
var selection = ChargenAppearanceSelection.Default with { SkinShade = 0.5 };
ChargenAppearanceFactory.TryCompose(
options, HeritageId, GenderKey, selection, pal, clothing, out ChargenAppearanceResult result);
ChargenSubPalette skin = Assert.Single(result.ObjDesc.SubPalettes, sp => sp.Offset == 0 && sp.NumColors == 24);
Assert.Equal(0x0400_0011u, skin.SubPaletteId); // index 1 of 3 at shade 0.5.
}
[Fact]
public void TryCompose_HairColorSelected_EmitsHairSubpaletteAtPackedOffsetTwentyFourCountEight()
{
ChargenOptions options = MakeOptions(MakeGender());
var (pal, clothing) = MakeSources();
var selection = ChargenAppearanceSelection.Default with { HairColor = 0u, HairShade = 1.0 };
ChargenAppearanceFactory.TryCompose(
options, HeritageId, GenderKey, selection, pal, clothing, out ChargenAppearanceResult result);
ChargenSubPalette hair = Assert.Single(result.ObjDesc.SubPalettes, sp => sp.Offset == 24 && sp.NumColors == 8);
Assert.Equal(0x0400_0021u, hair.SubPaletteId); // last of the two at shade 1.0.
}
[Fact]
public void TryCompose_EyeColorSelected_UsesRawPaletteIdDirectlyNoShadeIndirection()
{
ChargenOptions options = MakeOptions(MakeGender());
var (pal, clothing) = MakeSources();
var selection = ChargenAppearanceSelection.Default with { EyeColor = 0u };
ChargenAppearanceFactory.TryCompose(
options, HeritageId, GenderKey, selection, pal, clothing, out ChargenAppearanceResult result);
ChargenSubPalette eye = Assert.Single(result.ObjDesc.SubPalettes, sp => sp.Offset == 32 && sp.NumColors == 8);
Assert.Equal(EyeColorPaletteId, eye.SubPaletteId);
}
[Fact]
public void TryCompose_HairStyleSelected_AppendsHairObjDescAfterBase()
{
ChargenOptions options = MakeOptions(MakeGender());
var (pal, clothing) = MakeSources();
var selection = ChargenAppearanceSelection.Default with { HairStyle = 0u };
ChargenAppearanceFactory.TryCompose(
options, HeritageId, GenderKey, selection, pal, clothing, out ChargenAppearanceResult result);
Assert.Equal(0u, (uint)result.ObjDesc.AnimPartChanges[0].PartIndex); // base first.
Assert.Contains(result.ObjDesc.AnimPartChanges, c => c.PartIndex == 1); // hair style second.
}
[Fact]
public void TryCompose_HairStyleWithAlternateSetup_OverridesBodySetupId()
{
ChargenOptions options = MakeOptions(MakeGender(alternateHairSetup: AlternateBodySetupId));
var (pal, clothing) = MakeSources(bodySetupId: AlternateBodySetupId);
var selection = ChargenAppearanceSelection.Default with { HairStyle = 0u };
ChargenAppearanceFactory.TryCompose(
options, HeritageId, GenderKey, selection, pal, clothing, out ChargenAppearanceResult result);
Assert.Equal(AlternateBodySetupId, result.SetupId);
}
[Fact]
public void TryCompose_BothSetupSourcesZero_FallsBackToHumanSetupId()
{
ChargenGenderOptions gender = MakeGender() with { SetupId = 0u };
ChargenOptions options = MakeOptions(gender);
var (pal, clothing) = MakeSources(bodySetupId: 0u);
ChargenAppearanceFactory.TryCompose(
options, HeritageId, GenderKey, ChargenAppearanceSelection.Default,
pal, clothing, out ChargenAppearanceResult result);
Assert.Equal(ChargenAppearanceFactory.HumanSetupId, result.SetupId);
}
[Fact]
public void TryCompose_EyeStripSelected_UsesNonBaldObjDesc_WhenHairStyleIsNotBald()
{
ChargenOptions options = MakeOptions(MakeGender(baldHairStyle: false));
var (pal, clothing) = MakeSources();
var selection = ChargenAppearanceSelection.Default with { HairStyle = 0u, EyesStrip = 0u };
ChargenAppearanceFactory.TryCompose(
options, HeritageId, GenderKey, selection, pal, clothing, out ChargenAppearanceResult result);
// tag 2 = non-bald eye ObjDesc, tag 20 = bald eye ObjDesc.
Assert.Contains(result.ObjDesc.AnimPartChanges, c => c.PartId == 0x0100_0002u);
Assert.DoesNotContain(result.ObjDesc.AnimPartChanges, c => c.PartId == 0x0100_0014u);
}
[Fact]
public void TryCompose_EyeStripSelected_UsesBaldObjDesc_WhenHairStyleIsBald()
{
ChargenOptions options = MakeOptions(MakeGender(baldHairStyle: true));
var (pal, clothing) = MakeSources();
var selection = ChargenAppearanceSelection.Default with { HairStyle = 0u, EyesStrip = 0u };
ChargenAppearanceFactory.TryCompose(
options, HeritageId, GenderKey, selection, pal, clothing, out ChargenAppearanceResult result);
Assert.Contains(result.ObjDesc.AnimPartChanges, c => c.PartId == 0x0100_0014u); // tag 20, bald.
Assert.DoesNotContain(result.ObjDesc.AnimPartChanges, c => c.PartId == 0x0100_0002u); // tag 2, non-bald.
}
[Fact]
public void TryCompose_NoseAndMouthStripsSelected_AppendBothObjDescs()
{
ChargenOptions options = MakeOptions(MakeGender());
var (pal, clothing) = MakeSources();
var selection = ChargenAppearanceSelection.Default with { NoseStrip = 0u, MouthStrip = 0u };
ChargenAppearanceFactory.TryCompose(
options, HeritageId, GenderKey, selection, pal, clothing, out ChargenAppearanceResult result);
Assert.Contains(result.ObjDesc.AnimPartChanges, c => c.PartIndex == 3); // nose tag.
Assert.Contains(result.ObjDesc.AnimPartChanges, c => c.PartIndex == 4); // mouth tag.
}
[Fact]
public void TryCompose_AllFourClothingSlotsSelected_AppearInRetailOrderHeadgearTrousersShirtFootwear()
{
ChargenOptions options = MakeOptions(MakeGender());
var (pal, clothing) = MakeSources();
var selection = ChargenAppearanceSelection.Default with
{
HeadgearStyle = 0u,
TrousersStyle = 0u,
ShirtStyle = 0u,
FootwearStyle = 0u,
};
ChargenAppearanceFactory.TryCompose(
options, HeritageId, GenderKey, selection, pal, clothing, out ChargenAppearanceResult result);
// Only the base body's own tag (PartIndex 0) and the four clothing
// slots' PartIndex-5 overrides are present (no hair style/strips
// selected) — asserting the full ordered sequence pins retail's
// Headgear → Trousers → Shirt → Footwear append order directly.
uint[] expectedPartIds =
[
0x0100_0000u, // base body tag.
0x0100_5000u + HeadgearClothingTableId,
0x0100_5000u + TrousersClothingTableId,
0x0100_5000u + ShirtClothingTableId,
0x0100_5000u + FootwearClothingTableId,
];
Assert.Equal(expectedPartIds, result.ObjDesc.AnimPartChanges.Select(c => c.PartId).ToArray());
}
[Fact]
public void TryCompose_ClothingSlotWithColor_EmitsPartTextureAndDyeSubpalette()
{
ChargenOptions options = MakeOptions(MakeGender());
var (pal, clothing) = MakeSources();
var selection = ChargenAppearanceSelection.Default with
{
HeadgearStyle = 0u,
HeadgearColor = 0u, // gender.ClothingColors[0] = 7u == the fixture's palette-template key.
HeadgearShade = 0.0,
};
ChargenAppearanceFactory.TryCompose(
options, HeritageId, GenderKey, selection, pal, clothing, out ChargenAppearanceResult result);
Assert.Contains(result.ObjDesc.AnimPartChanges, c => c.PartId == 0x0100_5000u + HeadgearClothingTableId);
Assert.Contains(result.ObjDesc.TextureChanges, c => c.PartIndex == 5 && c.NewTextureId == 0x0500_6000u);
// Real range (80, 16) packed by /8 => (10, 2).
Assert.Contains(result.ObjDesc.SubPalettes, sp => sp.Offset == 10 && sp.NumColors == 2);
}
[Fact]
public void TryCompose_ClothingSlotWithoutColor_SkipsDyeSubpaletteButKeepsPartTextureChanges()
{
ChargenOptions options = MakeOptions(MakeGender());
var (pal, clothing) = MakeSources();
var selection = ChargenAppearanceSelection.Default with { HeadgearStyle = 0u };
ChargenAppearanceFactory.TryCompose(
options, HeritageId, GenderKey, selection, pal, clothing, out ChargenAppearanceResult result);
Assert.Contains(result.ObjDesc.AnimPartChanges, c => c.PartId == 0x0100_5000u + HeadgearClothingTableId);
Assert.DoesNotContain(result.ObjDesc.SubPalettes, sp => sp.Offset == 10 && sp.NumColors == 2);
}
[Fact]
public void TryCompose_UnknownClothingTableId_IsRecordedAsMissingAndSkipped()
{
ChargenGenderOptions gender = MakeGender();
gender = gender with
{
Headgears = [new ChargenGearOption("Missing", 0x1900_00FFu, 0x3000_0099u)],
};
ChargenOptions options = MakeOptions(gender);
var (pal, clothing) = MakeSources();
var selection = ChargenAppearanceSelection.Default with { HeadgearStyle = 0u };
bool ok = ChargenAppearanceFactory.TryCompose(
options, HeritageId, GenderKey, selection, pal, clothing, out ChargenAppearanceResult result);
Assert.True(ok);
Assert.Contains(0x1900_00FFu, result.MissingClothingTableIds);
Assert.DoesNotContain(result.ObjDesc.AnimPartChanges, c => c.PartIndex == 5);
}
[Fact]
public void TryCompose_UnknownHairColorPalSetId_IsRecordedAsMissingAndSkipped()
{
ChargenGenderOptions gender = MakeGender() with { HairColors = [0x0F00_00FFu] };
ChargenOptions options = MakeOptions(gender);
var (pal, clothing) = MakeSources();
var selection = ChargenAppearanceSelection.Default with { HairColor = 0u, HairShade = 0.5 };
bool ok = ChargenAppearanceFactory.TryCompose(
options, HeritageId, GenderKey, selection, pal, clothing, out ChargenAppearanceResult result);
Assert.True(ok);
Assert.Contains(0x0F00_00FFu, result.MissingPalSetIds);
Assert.DoesNotContain(result.ObjDesc.SubPalettes, sp => sp.Offset == 24);
}
[Fact]
public void TryCompose_BodySetupAbsentFromClothingBaseEffects_IsRecordedButDoesNotThrow()
{
ChargenOptions options = MakeOptions(MakeGender());
var (pal, clothing) = MakeSources(bodySetupId: 0x0200_DEADu); // different from the resolved body setup.
var selection = ChargenAppearanceSelection.Default with { HeadgearStyle = 0u };
bool ok = ChargenAppearanceFactory.TryCompose(
options, HeritageId, GenderKey, selection, pal, clothing, out ChargenAppearanceResult result);
Assert.True(ok);
Assert.Contains(HeadgearClothingTableId, result.ClothingTablesMissingBaseEffectForSetup);
Assert.DoesNotContain(result.ObjDesc.AnimPartChanges, c => c.PartIndex == 5);
}
[Fact]
public void TryCompose_OutOfRangeStyleIndex_IsTreatedAsUnselected()
{
ChargenOptions options = MakeOptions(MakeGender());
var (pal, clothing) = MakeSources();
var selection = ChargenAppearanceSelection.Default with { HairStyle = 999u, EyesStrip = 999u };
bool ok = ChargenAppearanceFactory.TryCompose(
options, HeritageId, GenderKey, selection, pal, clothing, out ChargenAppearanceResult result);
Assert.True(ok);
Assert.DoesNotContain(result.ObjDesc.AnimPartChanges, c => c.PartIndex == 1);
Assert.DoesNotContain(result.ObjDesc.AnimPartChanges, c => c.PartIndex == 2);
}
}