feat: red and white tablecloth on the Cook's Table

The cook goes back to the cartography table with its map painted over
offline (scripts/paint_cook_cloth.py from textures pulled by
scripts/extract_carto.py). The three PNGs are embedded in the DLL and
swapped onto copied materials by PieceLook.Retexture. The smith stays on
the black marble table.
This commit is contained in:
kanym 2026-10-28 13:38:28 +01:00
commit 069e098cf0
8 changed files with 238 additions and 22 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 100 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

View file

@ -62,6 +62,8 @@ namespace RacesOfValheim
public const float TableSpacing = 40f;
private const string TableSource = "piece_blackmarble_table";
private const string ClothTableSource = "piece_cartographytable";
private const float ClothTableScale = 0.8f;
/// <summary>
/// One skull, one wishbone, eight stone - the same price for both tables.
@ -197,20 +199,22 @@ namespace RacesOfValheim
private static void RegisterTable(string pieceName, string displayName, string servantPrefab,
ButlerFamily family, string description, string iconSource)
{
// The black marble table: "a slab of bone and slate". It was the cartography table once,
// whose map is baked into its own texture and could not be taken off.
GameObject prefab = PrefabManager.Instance.CreateClonedPrefab(pieceName, TableSource);
// The smith works at the black marble table, "a slab of bone and slate". The cook keeps the
// cartography table, with a tablecloth painted over its map (Dress).
bool kitchen = family == ButlerFamily.Kitchen;
string source = kitchen ? ClothTableSource : TableSource;
GameObject prefab = PrefabManager.Instance.CreateClonedPrefab(pieceName, source);
if (prefab == null)
{
Jotunn.Logger.LogWarning($"[Butler] Could not clone {TableSource}; " +
Jotunn.Logger.LogWarning($"[Butler] Could not clone {source}; " +
$"the {displayName} is not registered.");
return;
}
// A blanket sweep by capability rather than a named DestroyImmediate. The cartography table
// taught it: its MapTable is Interactable AND Hoverable, so it took the E key before our own
// Switch ever saw it. The marble table carries nothing of the kind today, and the sweep
// keeps a future patch from adding one behind our back.
// The MapTable trap, and it is the reason this is a blanket sweep by capability rather than
// a named DestroyImmediate: MapTable is Interactable AND Hoverable, so it takes the E key
// before our own Switch ever sees it and opens the shared map instead. The marble table
// carries nothing of the kind today, and the sweep keeps a future patch from adding one.
foreach (MonoBehaviour behaviour in prefab.GetComponentsInChildren<MonoBehaviour>(true))
{
if (behaviour == null) continue;
@ -218,6 +222,7 @@ namespace RacesOfValheim
Object.DestroyImmediate(behaviour);
}
if (kitchen) prefab.transform.localScale = Vector3.one * ClothTableScale;
Dress(prefab, family);
ServantTableBehaviour table = prefab.AddComponent<ServantTableBehaviour>();
@ -256,8 +261,8 @@ namespace RacesOfValheim
}));
}
// The look. The marble table is 2.6 m by 1.5 m; both props sit either side of its centre, the
// trade's tool the bigger of the two. X and Z only: the height is the table's, measured.
// The smith's look. The marble table is 2.6 m by 1.5 m; both props sit either side of its
// centre, the anvil the bigger of the two. X and Z only: the height is the table's, measured.
private const float MarbleTableTop = 0.82f;
private static readonly Vector3 TradeSpot = new Vector3(-0.35f, 0f, 0f);
private static readonly Vector3 SkullSpot = new Vector3(0.6f, 0f, 0.1f);
@ -265,12 +270,32 @@ namespace RacesOfValheim
private const float MealSize = 0.6f;
private const float SkullSize = 0.45f;
// The cook's cartography table, in its own space BEFORE ClothTableScale (so sizes are divided by
// it to come out the same as the smith's). The cloth covers what was the map: x -2.06 to 0.62,
// read off the mesh. Its top is the Dvergar bank's PileHeight, on the same vanilla mesh; the
// mesh's own height is no use here, since the writing desk stands taller than the cloth.
private const float ClothTop = 1.05f;
private static readonly Vector3 MealOnCloth = new Vector3(-0.75f, ClothTop, 0.2f);
private static readonly Vector3 SkullOnCloth = new Vector3(-1.55f, ClothTop, 0.35f);
/// <summary>
/// "A slab of bone and slate": the black marble table, with the trade's own work in the middle -
/// an anvil for the smith, a meal for the cook - and a skeleton's skull beside it.
/// The trade's own work in the middle - an anvil for the smith, a meal for the cook - and a
/// skeleton's skull beside it. The smith's on the black marble, the cook's on a red and white
/// cloth painted over the cartography table's map.
/// </summary>
private static void Dress(GameObject prefab, ButlerFamily family)
{
if (family == ButlerFamily.Kitchen)
{
LayCloth(prefab);
float size = 1f / ClothTableScale;
ModelBorrow.Attach(prefab.transform, "TableMeal", "MeatPlatter",
MealOnCloth, Quaternion.identity, MealSize * size, "Butler", activeOnly: true);
ModelBorrow.Attach(prefab.transform, "TableSkull", "TrophySkeleton",
SkullOnCloth, Quaternion.Euler(0f, -150f, 0f), SkullSize * size, "Butler", activeOnly: true);
return;
}
// The table's own height off its own mesh, with vanilla's 0.82 m as the fallback. The mesh
// stands on the piece's origin, so its height IS the height of the top.
Transform model = prefab.transform.Find("new");
@ -278,19 +303,32 @@ namespace RacesOfValheim
if (top <= 0f) top = MarbleTableTop;
Vector3 up = Vector3.up * top;
ModelBorrow.Attach(prefab.transform, "TableAnvil", "forge_ext2",
TradeSpot + up, Quaternion.identity, AnvilSize, "Butler", activeOnly: true);
ModelBorrow.Attach(prefab.transform, "TableSkull", "TrophySkeleton",
SkullSpot + up, Quaternion.Euler(0f, -150f, 0f), SkullSize, "Butler", activeOnly: true);
}
if (family == ButlerFamily.Forge)
{
ModelBorrow.Attach(prefab.transform, "TableAnvil", "forge_ext2",
TradeSpot + up, Quaternion.identity, AnvilSize, "Butler", activeOnly: true);
}
else
{
ModelBorrow.Attach(prefab.transform, "TableMeal", "MeatPlatter",
TradeSpot + up, Quaternion.identity, MealSize, "Butler", activeOnly: true);
}
/// <summary>
/// The tablecloth: the cartography table's own textures with the map painted over in red and
/// white squares, by scripts/paint_cook_cloth.py. The worn texture (a damaged table) has a
/// faded, stained cloth of its own, and the normal map is smoothed there so the coastline's
/// relief does not show through.
/// </summary>
private static void LayCloth(GameObject prefab)
{
var cloth = new System.Collections.Generic.Dictionary<string, Texture>();
AddCloth(cloth, "Cartographer_d", "CookCloth_d.png", false);
AddCloth(cloth, "Cartographer_worn_d", "CookCloth_worn_d.png", false);
AddCloth(cloth, "Cartographer_n", "CookCloth_n.png", true);
PieceLook.Retexture(prefab.transform, cloth);
}
private static void AddCloth(System.Collections.Generic.Dictionary<string, Texture> cloth,
string vanilla, string resource, bool linear)
{
Texture2D texture = PieceLook.LoadTexture(resource, linear);
if (texture != null) cloth[vanilla] = texture;
}
public static void EnsureKnown(Player player)

View file

@ -94,6 +94,84 @@ namespace RacesOfValheim
}
}
/// <summary>
/// Swaps textures by the name of the texture they replace: every material under
/// <paramref name="root"/> that uses "Cartographer_d" gets <paramref name="replacements"/>'s
/// "Cartographer_d" instead, whichever slot it sits in. Materials are copied as in Tint.
/// </summary>
public static void Retexture(Transform root, IDictionary<string, Texture> replacements)
{
if (root == null) return;
var copies = new Dictionary<Material, Material>();
foreach (Renderer renderer in root.GetComponentsInChildren<Renderer>(true))
{
if (renderer == null || renderer is ParticleSystemRenderer) continue;
Material[] materials = renderer.sharedMaterials;
bool changed = false;
for (int i = 0; i < materials.Length; i++)
{
Material source = materials[i];
if (source == null) continue;
foreach (string property in source.GetTexturePropertyNames())
{
Texture current = source.GetTexture(property);
if (current == null || !replacements.TryGetValue(current.name, out Texture replacement)) continue;
Material copy = CopyOf(source, copies);
copy.SetTexture(property, replacement);
materials[i] = copy;
changed = true;
}
}
if (changed) renderer.sharedMaterials = materials;
}
}
/// <summary>
/// ImageConversion.LoadImage, found at run time. Its module is built against netstandard 2.1 and
/// this project targets net48, so referencing it at compile time fails the build (CS1705); the
/// game has the module loaded either way.
/// </summary>
private static readonly System.Reflection.MethodInfo LoadImage =
System.Type.GetType("UnityEngine.ImageConversion, UnityEngine.ImageConversionModule")
?.GetMethod("LoadImage", new[] { typeof(Texture2D), typeof(byte[]), typeof(bool) });
/// <summary>
/// A PNG embedded in the DLL (see the csproj's EmbeddedResource), or null with a warning.
/// <paramref name="linear"/> for anything that is data rather than colour - a normal map read
/// as sRGB comes out bent.
/// </summary>
public static Texture2D LoadTexture(string resource, bool linear = false)
{
using (System.IO.Stream stream = typeof(PieceLook).Assembly.GetManifestResourceStream(resource))
{
if (stream == null)
{
Jotunn.Logger.LogWarning($"No embedded texture '{resource}'.");
return null;
}
byte[] bytes = new byte[stream.Length];
int read = 0;
while (read < bytes.Length) read += stream.Read(bytes, read, bytes.Length - read);
Texture2D texture = new Texture2D(2, 2, TextureFormat.RGBA32, true, linear)
{
name = System.IO.Path.GetFileNameWithoutExtension(resource),
anisoLevel = 4,
};
if (LoadImage == null || !(bool)LoadImage.Invoke(null, new object[] { texture, bytes, true }))
{
Jotunn.Logger.LogWarning($"Embedded texture '{resource}' would not decode.");
return null;
}
return texture;
}
}
private static void Recolour(Material material, string property, Color glow)
{
if (!material.HasProperty(property)) return;

View file

@ -53,6 +53,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<!-- Textures painted offline (scripts/paint_cook_cloth.py), carried inside the DLL so a mod
manager has no extra folder to flatten. Loaded by name: PieceLook.LoadTexture("CookCloth_d.png"). -->
<ItemGroup>
<EmbeddedResource Include="Assets\**\*.png" LogicalName="%(Filename)%(Extension)" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />