RacesOfValheim/JotunnModStub/RaceStationLock.cs
kanym 7647b66aa0 fix(stations): the race requirement comes first in the build panel
The race took the first free row after the materials and the crafting
station. Each of those rows now moves one to the right and the race
takes row 0: it decides whether the rest matters at all.
2026-10-17 16:39:56 +02:00

163 lines
8.5 KiB
C#

using System.Collections.Generic;
using System.Linq;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace RacesOfValheim
{
/// <summary>
/// A race station asks for its race the way a piece asks for wood: as a requirement.
///
/// Each station used to have its own lock, or none. Nine refused at placement with a message of
/// their own (PieceDensityLimit gates, the Trol totem, the Dvergar bank and barrel, the Trenta
/// root); seven - the Cursed Body Pile, the Pack Trough, the Bone Pile and Sentry, the Blood
/// Refinery, the Composter and the Heartwood Grove - anybody could build. And none of them said so
/// before the click: the build panel listed the wood and the trophy and nothing else.
///
/// Now every one of them is listed here with its race, and two things follow. The build panel
/// shows the race in the requirement row, with the race's icon and name, blinking red when the
/// player is not of it - the same row and the same blink vanilla uses for a missing crafting
/// station. And Player.HaveRequirements answers false for CanBuild, so the piece is greyed in the
/// menu and a click gets vanilla's "Missing requirements", exactly as for a missing item. IsKnown
/// and CanAlmostBuild are left alone, so the station stays in the menu for everyone to read.
///
/// The stations' own gates stay: they also check boss tiers, and they still catch a build in
/// no-cost debug mode, which skips HaveRequirements altogether.
/// </summary>
public static class RaceStationLock
{
private static readonly Dictionary<string, string[]> Races = new Dictionary<string, string[]>
{
{ TrolTotem.PieceName, new[] { TrolTotem.RaceId } },
{ DraughSpawner.PieceName, new[] { DraughVisuals.RaceId } },
{ DvergarBanco.PieceName, new[] { DvergarConfig.RaceId } },
{ DvergarBotte.PieceName, new[] { DvergarConfig.RaceId } },
{ FenrirMangiatoia.PieceName, new[] { FenrirMangiatoia.RaceId } },
{ SkadiGolem.SeedPieceName, new[] { "Skadi" } },
{ BoneServant.SmithTableName, new[] { SkeletonVisuals.RaceId, "Skadi" } },
{ BoneServant.CookTableName, new[] { "Skadi" } },
{ SkeletonBonePile.PieceName, new[] { SkeletonVisuals.RaceId } },
{ SkeletonSentry.SeedPieceName, new[] { SkeletonVisuals.RaceId } },
{ SvermHive.PieceName, new[] { SvermConfig.RaceId } },
{ SvermHiveMarketPiece.PieceName, new[] { SvermConfig.RaceId } },
{ TrentaConfig.ElderRootPieceName, new[] { TrentaConfig.RaceId } },
{ TrentaConfig.ComposterPieceName, new[] { TrentaConfig.RaceId } },
{ TrentaConfig.GrovePieceName, new[] { TrentaConfig.RaceId } },
{ AswidBloodRefinery.PieceName, new[] { "Aswid" } },
};
/// <summary>The races allowed to build this piece, or null for a piece anyone builds.</summary>
public static string[] For(Piece piece)
{
if (piece == null) return null;
return Races.TryGetValue(Utils.GetPrefabName(piece.gameObject), out string[] races) ? races : null;
}
public static bool Allows(Player player, string[] races)
{
if (races == null) return true;
if (player == null) return false;
string race = FactionHostilityPatch.GetPlayerRace(player);
return race != null && races.Contains(race);
}
public static string NameOf(string race) => ModText.Get("rov_race_" + race.ToLowerInvariant() + "_name");
/// <summary>CanBuild only: see the class doc for why IsKnown and CanAlmostBuild stay vanilla.</summary>
[HarmonyPatch(typeof(Player), nameof(Player.HaveRequirements), new[] { typeof(Piece), typeof(Player.RequirementMode) })]
public static class RaceStation_HaveRequirements_Patch
{
public static void Postfix(Player __instance, Piece piece, Player.RequirementMode mode, ref bool __result)
{
if (!__result || mode != Player.RequirementMode.CanBuild) return;
if (!Allows(__instance, For(piece))) __result = false;
}
}
/// <summary>
/// The race in the build panel's requirement row, FIRST: the race decides whether the rest
/// matters at all. Hud.SetupPieceInfo has just filled one row per material and one for the
/// crafting station; each of those is moved one row to the right, and the race takes row 0.
/// </summary>
[HarmonyPatch(typeof(Hud), "SetupPieceInfo")]
public static class RaceStation_PieceInfo_Patch
{
// Vanilla never sets the name's colour, so a row this patch left red would stay red for
// whatever item the next piece puts in it.
public static void Prefix(Hud __instance)
{
if (__instance.m_requirementItems == null) return;
foreach (GameObject row in __instance.m_requirementItems)
{
TMP_Text name = row != null ? row.transform.Find("res_name")?.GetComponent<TMP_Text>() : null;
if (name != null) name.color = Color.white;
}
}
public static void Postfix(Hud __instance, Piece piece)
{
string[] races = For(piece);
if (races == null || races.Length == 0 || __instance.m_requirementItems == null) return;
GameObject[] rows = __instance.m_requirementItems;
int used = piece.m_resources.Length + (piece.m_craftingStation != null ? 1 : 0);
if (used >= rows.Length) return;
for (int i = used; i > 0; i--) CopyRow(rows[i - 1], rows[i]);
GameObject row = rows[0];
Image icon = row.transform.Find("res_icon")?.GetComponent<Image>();
TMP_Text name = row.transform.Find("res_name")?.GetComponent<TMP_Text>();
TMP_Text amount = row.transform.Find("res_amount")?.GetComponent<TMP_Text>();
UITooltip tooltip = row.GetComponent<UITooltip>();
if (icon == null || name == null || amount == null) return;
row.SetActive(true);
icon.gameObject.SetActive(true);
name.gameObject.SetActive(true);
amount.gameObject.SetActive(true);
string label = string.Join(" / ", races.Select(NameOf));
icon.sprite = RacesOfValheim.RaceIcon(races[0]);
name.text = label;
if (tooltip != null) tooltip.m_text = label;
amount.text = "";
bool ok = Allows(Player.m_localPlayer, races);
icon.color = ok ? Color.white : Color.gray;
// Vanilla's blink for a missing crafting station, on the name since there is no number.
name.color = ok || Mathf.Sin(Time.time * 10f) <= 0f ? Color.white : Color.red;
}
/// <summary>Everything SetupRequirement and the station row write, from one row to the next.</summary>
private static void CopyRow(GameObject from, GameObject to)
{
to.SetActive(from.activeSelf);
foreach (string child in new[] { "res_icon", "res_name", "res_amount" })
{
Transform a = from.transform.Find(child);
Transform b = to.transform.Find(child);
if (a == null || b == null) continue;
b.gameObject.SetActive(a.gameObject.activeSelf);
}
Image fromIcon = from.transform.Find("res_icon")?.GetComponent<Image>();
Image toIcon = to.transform.Find("res_icon")?.GetComponent<Image>();
if (fromIcon != null && toIcon != null) { toIcon.sprite = fromIcon.sprite; toIcon.color = fromIcon.color; }
foreach (string child in new[] { "res_name", "res_amount" })
{
TMP_Text a = from.transform.Find(child)?.GetComponent<TMP_Text>();
TMP_Text b = to.transform.Find(child)?.GetComponent<TMP_Text>();
if (a != null && b != null) { b.text = a.text; b.color = a.color; }
}
UITooltip fromTip = from.GetComponent<UITooltip>();
UITooltip toTip = to.GetComponent<UITooltip>();
if (fromTip != null && toTip != null) toTip.m_text = fromTip.m_text;
}
}
}
}