RacesOfValheim/JotunnModStub/Alfar/AlfarNexusStore.cs
Ferdinando 3f8cf1ee27 feat(alfar): v1.6.0 - the Alfar, an eleventh race
Grey, tall and thin, with black eyes. Eitr is armour and fuel (shield,
diet, soft fall, levitation), Heimkall sends a lethal blow home at one
health every 20 minutes. Caster powers from Skrid to the archon
Sameining, with on-screen cooldowns. Automated base around the
Ljoskjarne: drones, three constructs, crystals, Ljosgard, Prismavard.
Compendium page in EN/IT/RU.

README: New in block, race section, progression, station line, changelog.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 17:38:28 +01:00

171 lines
7.5 KiB
C#

using System.Collections.Generic;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace RacesOfValheim
{
/// <summary>
/// The Ljoskjarne's window: the merchant's own store with a column of products in it - the drone,
/// the three constructs, the crystal upgrade, and the crystals to collect.
///
/// It is the Trol war totem's window (TrolTotemStore) cloned for another trader, and for the same
/// reasons: same three patch points (Awake to build, FillList to repaint, Hide to clear), same trick
/// of instantiating StoreGui.m_listElement for the rows. The empty-catalogue guard that keeps vanilla
/// FillList from throwing is already on every trader with no wares (StoreGuiEmptyCatalogueGuardPatch),
/// so this one needs none of its own.
///
/// What is different from the totem: a row's price is a number out of the pack and not coins, so the
/// short figure on the row is the metal or the crystals, and the tooltip carries the whole price.
/// </summary>
public static class AlfarNexusStore
{
private static RectTransform _list;
private static readonly List<GameObject> _rows = new List<GameObject>();
public static AlfarNexusBehaviour ActiveNexus(StoreGui gui)
{
Trader trader = FactionMerchantRuntime.GetActiveTrader(gui);
return trader != null ? trader.GetComponent<AlfarNexusBehaviour>() : null;
}
// ─── build ───────────────────────────────────────────────────────────
public static void Create(StoreGui gui)
{
if (gui == null || gui.m_listRoot == null || _list != null) return;
GameObject clone = Object.Instantiate(gui.m_listRoot.gameObject, gui.m_listRoot.parent);
clone.name = "AlfarProductList";
_list = clone.GetComponent<RectTransform>();
_list.anchoredPosition = gui.m_listRoot.anchoredPosition;
// The clone carries whatever rows the vanilla list held at Awake: it starts empty.
foreach (Transform child in _list) Object.Destroy(child.gameObject);
_list.gameObject.SetActive(false);
}
public static void Clear()
{
foreach (GameObject row in _rows) if (row != null) Object.Destroy(row);
_rows.Clear();
if (_list != null) _list.gameObject.SetActive(false);
}
// ─── fill ────────────────────────────────────────────────────────────
public static void Refresh(StoreGui gui)
{
Clear();
AlfarNexusBehaviour nexus = ActiveNexus(gui);
if (nexus == null) return; // an ordinary merchant: not ours
if (_list == null)
{
Jotunn.Logger.LogWarning("[Alfar] The Ljoskjarne's column was never built (did the patch on StoreGui.Awake fail?).");
return;
}
_list.gameObject.SetActive(true);
int row = 0;
foreach (AlfarNexusBehaviour.Product product in nexus.OnOffer())
{
BuildRow(gui, nexus, product, row++);
}
float baseSize = Traverse.Create(gui).Field("m_itemlistBaseSize").GetValue<float>();
_list.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, Mathf.Max(baseSize, row * gui.m_itemSpacing));
}
private static string IconOf(AlfarNexusBehaviour.Product product)
{
switch (product)
{
case AlfarNexusBehaviour.Product.Drone: return "TrophyDeathsquito";
case AlfarNexusBehaviour.Product.Neistvordr: return "TrophySurtling";
case AlfarNexusBehaviour.Product.Eldingvordr: return "TrophyAbomination";
case AlfarNexusBehaviour.Product.Steinvordr: return "TrophySGolem";
default: return "Crystal";
}
}
private static void BuildRow(StoreGui gui, AlfarNexusBehaviour nexus, AlfarNexusBehaviour.Product product, int rowIndex)
{
GameObject element = Object.Instantiate(gui.m_listElement, _list);
element.SetActive(true);
RectTransform rt = (RectTransform)element.transform;
float offset = (_list.rect.width - rt.rect.width) / 2f;
rt.anchoredPosition = new Vector2(offset, rowIndex * -gui.m_itemSpacing - offset);
Sprite icon = Icon(IconOf(product));
Image iconImage = element.transform.Find("icon").GetComponent<Image>();
if (icon != null) iconImage.sprite = icon;
string name = AlfarNexusBehaviour.ProductName(product);
element.transform.Find("name").GetComponent<TMP_Text>().text = Localization.instance.Localize(name);
TMP_Text price = FindDescendant<TMP_Text>(element.transform, "price");
if (price != null) price.text = AlfarNexusBehaviour.PriceShort(product);
// The coin that sits beside the number on a vanilla row is the wrong picture here.
foreach (Image image in element.GetComponentsInChildren<Image>(true))
{
if (image != null && image.gameObject.name.ToLowerInvariant().Contains("coin")) image.gameObject.SetActive(false);
}
UITooltip tooltip = element.GetComponent<UITooltip>();
if (tooltip != null) tooltip.Set(Localization.instance.Localize(name), nexus.Terms(product), gui.m_tooltipAnchor, default(Vector2));
AlfarNexusBehaviour.Product chosen = product;
element.GetComponent<Button>().onClick.AddListener(() =>
{
nexus.BuildFromStore(chosen);
Traverse.Create(gui).Method("FillList").GetValue(); // repaints the column
});
_rows.Add(element);
}
// ─── helpers ─────────────────────────────────────────────────────────
private static Sprite Icon(string prefabName)
{
ItemDrop drop = Jotunn.Managers.PrefabManager.Cache.GetPrefab<ItemDrop>(prefabName);
Sprite[] icons = drop != null && drop.m_itemData != null && drop.m_itemData.m_shared != null
? drop.m_itemData.m_shared.m_icons
: null;
return icons != null && icons.Length > 0 ? icons[0] : null;
}
private static T FindDescendant<T>(Transform root, string childName) where T : Component
{
foreach (T candidate in root.GetComponentsInChildren<T>(true))
{
if (candidate.gameObject.name == childName) return candidate;
}
return null;
}
}
[HarmonyPatch(typeof(StoreGui), "Awake")]
public static class AlfarNexusStoreSetupPatch
{
public static void Postfix(StoreGui __instance) => AlfarNexusStore.Create(__instance);
}
[HarmonyPatch(typeof(StoreGui), "FillList")]
public static class AlfarNexusStoreFillPatch
{
public static void Postfix(StoreGui __instance) => AlfarNexusStore.Refresh(__instance);
}
[HarmonyPatch(typeof(StoreGui), "Hide")]
public static class AlfarNexusStoreHidePatch
{
public static void Postfix() => AlfarNexusStore.Clear();
}
}