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,71 @@
using System.Collections.Generic;
using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
// Turns a freshly spawned vanilla mob into a trader the moment it wakes up, if its
// prefab is one of the ones listed in FactionMerchantCatalog. Everything needed to
// reuse vanilla Trader/StoreGui (buy list, dialogue, look-at rig) is wired up here;
// the actual "sell your stuff back" second column lives in FactionMerchantSellPanel.
[HarmonyPatch(typeof(Character), "Awake")]
public static class FactionMerchantSetupPatch
{
// 6s to match the "battuta visibile 6 secondi" rule.
private const float DialogHideDelay = 6f;
private static readonly string[] GenericGreets = { "..." };
private static readonly string[] GenericGoodbye = { "..." };
private static readonly string[] GenericStartTrade = { "What do you need?" };
private static readonly string[] GenericBuy = { "Deal." };
private static readonly string[] GenericSell = { "Thanks." };
public static void Postfix(Character __instance)
{
if (__instance == null) return;
GameObject go = ((Component)__instance).gameObject;
if (go.GetComponent<FactionMerchant>() != null) return; // already set up
string prefabName = go.name.Replace("(Clone)", "").Trim();
if (!FactionMerchantCatalog.Shops.TryGetValue(prefabName, out var shop)) return;
var merchant = go.AddComponent<FactionMerchant>();
merchant.Shop = shop;
Trader trader = go.GetComponent<Trader>() ?? go.AddComponent<Trader>();
trader.m_name = shop.DisplayName;
trader.m_hideDialogDelay = DialogHideDelay;
trader.m_items.Clear();
foreach (var entry in shop.Items)
{
GameObject itemPrefab = ObjectDB.instance != null ? ObjectDB.instance.GetItemPrefab(entry.PrefabName) : null;
ItemDrop itemDrop = itemPrefab != null ? itemPrefab.GetComponent<ItemDrop>() : null;
if (itemDrop == null)
{
Debug.LogWarning($"[Factions] {shop.DisplayName}: unrecognized item prefab '{entry.PrefabName}', skipped.");
continue;
}
trader.m_items.Add(new Trader.TradeItem
{
m_prefab = itemDrop,
m_stack = 1,
m_price = entry.Price,
});
}
// Vanilla Trader.RandomTalk() indexes into these lists unconditionally, so they
// can never be left empty even though we do not care about the flavour text here.
trader.m_randomGreets = new List<string>(GenericGreets);
trader.m_randomGoodbye = new List<string>(GenericGoodbye);
trader.m_randomStartTrade = new List<string>(GenericStartTrade);
trader.m_randomBuy = new List<string>(GenericBuy);
trader.m_randomSell = new List<string>(GenericSell);
trader.m_randomTalk = new List<string>(FactionMerchantJokes.Lines.TryGetValue(shop.JokeCategory, out var lines) ? lines : GenericGreets);
// Trader.Update() is skipped entirely for these (FactionMerchantSuppressAmbientPatch),
// so none of its proximity-driven greet/stand/look-at logic ever runs and a LookAt
// component would never be used - not added here on purpose.
}
}
}