factionsmerchants

This commit is contained in:
Ferdinando 2026-08-06 10:13:57 +02:00
commit ce87e987c9
8 changed files with 980 additions and 0 deletions

View file

@ -0,0 +1,193 @@
using System.Collections.Generic;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace RacesOfValheim
{
// Builds the second column the brainstorm asked for: every item in the current
// trader's catalogue that the player is currently carrying, priced the same as the
// buy column (buy price == sell price, per the Factions design rules). Left click
// sells one unit, ctrl+click sells the whole stack.
//
// Vanilla StoreGui has no such list - its own "Sell" button only ever sells a single
// arbitrary valuable item (GetSellableItem(), first m_value > 0 item found) - so this
// is a genuinely new panel, cloned at runtime from the existing buy-list column.
[HarmonyPatch(typeof(StoreGui), "Awake")]
public static class FactionMerchantSellPanelSetupPatch
{
public static void Postfix(StoreGui __instance)
{
FactionMerchantSellPanel.CreateSellColumn(__instance);
}
}
[HarmonyPatch(typeof(StoreGui), "FillList")]
public static class FactionMerchantSellPanelFillPatch
{
public static void Postfix(StoreGui __instance)
{
FactionMerchantSellPanel.RefreshSellColumn(__instance);
}
}
[HarmonyPatch(typeof(StoreGui), "Hide")]
public static class FactionMerchantSellPanelHidePatch
{
public static void Postfix(StoreGui __instance)
{
FactionMerchantSellPanel.ClearSellColumn();
}
}
public static class FactionMerchantSellPanel
{
// Column spacing is a best guess (list width isn't visible from outside the Unity
// editor) - nudge this if the two columns overlap or sit too far apart in-game.
private const float ColumnOffsetX = 260f;
private static RectTransform _sellListRoot;
private static readonly List<GameObject> _sellRows = new List<GameObject>();
public static void CreateSellColumn(StoreGui gui)
{
if (_sellListRoot != null || gui.m_listRoot == null) return;
GameObject clone = Object.Instantiate(gui.m_listRoot.gameObject, gui.m_listRoot.parent);
clone.name = "FactionSellList";
RectTransform rt = clone.GetComponent<RectTransform>();
rt.anchoredPosition = gui.m_listRoot.anchoredPosition + new Vector2(ColumnOffsetX, 0f);
_sellListRoot = rt;
}
public static void ClearSellColumn()
{
foreach (GameObject row in _sellRows)
{
if (row != null) Object.Destroy(row);
}
_sellRows.Clear();
}
public static void RefreshSellColumn(StoreGui gui)
{
ClearSellColumn();
Trader trader = FactionMerchantRuntime.GetActiveTrader(gui);
if (_sellListRoot == null || trader == null) return;
FactionMerchant merchant = ((Component)trader).GetComponent<FactionMerchant>();
if (merchant == null) return;
Player player = Player.m_localPlayer;
if (player == null) return;
List<ItemDrop.ItemData> owned = player.GetInventory().GetAllItems();
int rowIndex = 0;
foreach (FactionMerchantCatalog.CatalogEntry entry in merchant.Shop.Items)
{
List<ItemDrop.ItemData> stacks = owned.FindAll(i => i.m_dropPrefab != null && i.m_dropPrefab.name == entry.PrefabName);
if (stacks.Count == 0) continue;
int totalCount = 0;
foreach (ItemDrop.ItemData stack in stacks) totalCount += stack.m_stack;
BuildRow(gui, entry, stacks[0], totalCount, rowIndex);
rowIndex++;
}
float baseSize = Traverse.Create(gui).Field("m_itemlistBaseSize").GetValue<float>();
float height = Mathf.Max(baseSize, rowIndex * gui.m_itemSpacing);
_sellListRoot.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
}
private static void BuildRow(StoreGui gui, FactionMerchantCatalog.CatalogEntry entry, ItemDrop.ItemData sample, int totalCount, int rowIndex)
{
GameObject element = Object.Instantiate(gui.m_listElement, _sellListRoot);
element.SetActive(true);
RectTransform rt = (RectTransform)element.transform;
float listWidth = _sellListRoot.rect.width;
float elementWidth = rt.rect.width;
float xCenterOffset = (listWidth - elementWidth) / 2f;
rt.anchoredPosition = new Vector2(xCenterOffset, rowIndex * -gui.m_itemSpacing - xCenterOffset);
Image icon = element.transform.Find("icon").GetComponent<Image>();
icon.sprite = sample.m_shared.m_icons[0];
string label = Localization.instance.Localize(sample.m_shared.m_name);
if (totalCount > 1) label = label + " x" + totalCount;
TMP_Text nameLabel = element.transform.Find("name").GetComponent<TMP_Text>();
nameLabel.text = label;
TMP_Text priceLabel = FindDescendant<TMP_Text>(element.transform, "price");
if (priceLabel != null) priceLabel.text = entry.Price.ToString();
UITooltip tooltip = element.GetComponent<UITooltip>();
if (tooltip != null)
{
tooltip.Set(sample.m_shared.m_name, sample.GetTooltip(totalCount), gui.m_tooltipAnchor, default(Vector2));
}
Button button = element.GetComponent<Button>();
button.onClick.AddListener(() =>
{
bool sellStack = Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);
SellCatalogItem(gui, entry, sellStack);
});
_sellRows.Add(element);
}
private static void SellCatalogItem(StoreGui gui, FactionMerchantCatalog.CatalogEntry entry, bool sellStack)
{
Player player = Player.m_localPlayer;
if (player == null) return;
Inventory inv = player.GetInventory();
List<ItemDrop.ItemData> stacks = inv.GetAllItems().FindAll(i => i.m_dropPrefab != null && i.m_dropPrefab.name == entry.PrefabName);
if (stacks.Count == 0) return;
int sold;
if (sellStack)
{
sold = 0;
foreach (ItemDrop.ItemData stack in new List<ItemDrop.ItemData>(stacks))
{
sold += stack.m_stack;
inv.RemoveItem(stack);
}
}
else
{
sold = 1;
inv.RemoveOneItem(stacks[0]);
}
if (sold <= 0) return;
int coinsEarned = entry.Price * sold;
GameObject coinPrefab = ((Component)gui.m_coinPrefab).gameObject;
inv.AddItem(coinPrefab.name, coinsEarned, gui.m_coinPrefab.m_itemData.m_quality, gui.m_coinPrefab.m_itemData.m_variant, 0L, "");
Trader trader = FactionMerchantRuntime.GetActiveTrader(gui);
trader?.OnSold();
// FillList() is private in this build's assembly reference; also re-triggers
// FactionMerchantSellPanelFillPatch's Postfix, which repaints this column.
Traverse.Create(gui).Method("FillList").GetValue();
}
// "price" sits deeper than a direct child in the vanilla list-element prefab
// (vanilla itself reaches it via Utils.FindChild rather than Transform.Find), so a
// small recursive search stands in for that helper here.
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;
}
}
}