RacesOfValheim/JotunnModStub/Factions/FactionMerchantSetup.cs
Ferdinando c7cbd6bece chore(release): v1.4.18 - Valheim hotfix flight fix, 1.0 shop fixes, Fenrir speed
Drake flight resolves ZInput.GetJoyLeftStickY at runtime, since the
hotfix dropped its bool parameter. Faction merchant TradeItems now set
the 1.0 fields StoreGui dereferences (m_tooltip, m_buyPlayerEffects),
and the Dvergar trader discount clones the whole TradeItem instead of
rebuilding four fields. Trol totem refusals return true so vanilla's
$msg_cantuseon no longer covers them. The Fenrir curse speed modifier
is the bonus alone (SE_Stats adds it), not 1 + bonus. New Discord link.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-18 17:10:24 +02:00

80 lines
4 KiB
C#

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 (!FactionsConfig.Merchants) return;
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,
// Valheim 1.0 fields that StoreGui dereferences without a null check:
// FillList reads m_tooltip.Length, BuySelectedItem calls
// m_buyPlayerEffects.Create. Empty means "use the item's own".
m_name = string.Empty,
m_tooltip = string.Empty,
m_buyKey = string.Empty,
m_incrementKey = string.Empty,
m_buyPlayerEffects = new EffectList(),
});
}
// 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.
}
}
}