diff --git a/JotunnModStub/RaceStationLock.cs b/JotunnModStub/RaceStationLock.cs new file mode 100644 index 0000000..ba84346 --- /dev/null +++ b/JotunnModStub/RaceStationLock.cs @@ -0,0 +1,131 @@ +using System.Collections.Generic; +using System.Linq; +using HarmonyLib; +using TMPro; +using UnityEngine; +using UnityEngine.UI; + +namespace RacesOfValheim +{ + /// + /// 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. + /// + public static class RaceStationLock + { + private static readonly Dictionary Races = new Dictionary + { + { 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" } }, + }; + + /// The races allowed to build this piece, or null for a piece anyone builds. + 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"); + + /// CanBuild only: see the class doc for why IsKnown and CanAlmostBuild stay vanilla. + [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; + } + } + + /// + /// The race in the build panel's requirement row, in the first slot after the materials and + /// the crafting station - the slots Hud.SetupPieceInfo has just filled or switched off. + /// + [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() : 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; + + int slot = piece.m_resources.Length + (piece.m_craftingStation != null ? 1 : 0); + if (slot >= __instance.m_requirementItems.Length) return; + + GameObject row = __instance.m_requirementItems[slot]; + Image icon = row.transform.Find("res_icon")?.GetComponent(); + TMP_Text name = row.transform.Find("res_name")?.GetComponent(); + TMP_Text amount = row.transform.Find("res_amount")?.GetComponent(); + UITooltip tooltip = row.GetComponent(); + 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; + } + } + } +} diff --git a/JotunnModStub/RacesOfValheim.cs b/JotunnModStub/RacesOfValheim.cs index e6f8289..0702646 100644 --- a/JotunnModStub/RacesOfValheim.cs +++ b/JotunnModStub/RacesOfValheim.cs @@ -451,6 +451,28 @@ namespace RacesOfValheim GUIManager.BlockInput(true); } + /// + /// The icon the race menu shows for a race, for anything else that names a race - the race + /// requirement in the build panel (RaceStationLock). One source, so the two never disagree. + /// + public static Sprite RaceIcon(string race) + { + if (Instance == null || string.IsNullOrEmpty(race)) return null; + switch (race) + { + case "Fenrir": return Instance.GetFenrirIcon(); + case "Skadi": return Instance.GetSkadiIcon(); + case SkeletonVisuals.RaceId: return Instance.GetSkeletonIcon(); + case "Aswid": return Instance.GetAswidIcon(); + case "Furling": return Instance.GetFurlingIcon(); + case "Trol": return Instance.GetTrolIcon(); + case DvergarConfig.RaceId: return Instance.GetDvergarIcon(); + case TrentaConfig.RaceId: return Instance.GetTrentaIcon(); + case SvermConfig.RaceId: return Instance.GetSvermIcon(); + } + return race == DraughVisuals.RaceId ? Instance.GetDraughIcon() : Instance.GetHumanIcon(); + } + private Sprite GetHumanIcon() { GameObject clubPrefab = PrefabManager.Cache.GetPrefab("Club")?.gameObject;