RacesOfValheim/JotunnModStub/Factions/FactionMerchantSetup.cs
Ferdinando 34bdf35325 Merge branch 'war-vulture' into main
Sverm Ravager Strain (replacing the Stinger Morph), Trenta Elder Root, the
Trol food and Black Forest truce work, and the faction FindEnemy patch.

Conflicts resolved:

- FactionHostilityPatch.cs: both sides added a class at the same spot and
  they cover different holes, so both are kept - FactionStaticTargetPatch
  (main, stops same-faction mobs smashing structures) and
  FactionHuntPlayerPatch (war-vulture, closes the HuntPlayer fall-through).

- RaceCompendium.cs: both branches independently fixed the same off-by-one
  in the Trol's progression gates. Took main's wording, which matches the
  code - Axes are in the melee bonus (TrolDamagePatches sends them to
  m_slash), the 4th food slot has no gate at all, and Yagluth is +5 not +10
  (TrolConfig.YagluthMeleeBonus). Kept war-vulture's two new lines, the
  Kindling fire weakness and the Black Forest truce.

- Package/manifest.json: 1.4.8, which is what the root manifest and
  PluginVersion already said.

RacesOfValheim.cs referenced SvermConfig.StingerSE/StingerCdSE, which
war-vulture renamed to Ravager. Git merged those lines without a conflict
and the result did not compile; renamed.

Also in this commit:

- Factions on/off switches (FactionsConfig): "Faction truce" and "Faction
  merchants", both default on, both read live so they take effect without a
  restart. The truce gates its three postfixes; the merchants gate the
  Character.Awake setup plus every behavioural patch, so turning them off
  drops the prompt and the trade window on mobs already awake.

- SerpentMeat sells for 200 instead of 1 at the Fuling bench, moved to keep
  the list's ascending price order (the sell panel renders it as declared).

- DraughFoodPatches mapped "SerpentMeatRaw", which is not a prefab; the
  internal id is SerpentMeat, so serpent meat never cooked for a Draugh.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-25 18:52:10 +02:00

72 lines
3.5 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,
});
}
// 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.
}
}
}