311 lines
14 KiB
C#
311 lines
14 KiB
C#
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using HarmonyLib;
|
|
using Jotunn.Configs;
|
|
using Jotunn.Entities;
|
|
using Jotunn.Managers;
|
|
using UnityEngine;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
/// <summary>
|
|
/// The rune barrel: drink for a hired crew.
|
|
///
|
|
/// Second half of the Dvergar chain and the answer to "can they be given potions". They can, and it
|
|
/// costs the mercenaries nothing of their own - a creature carries an SEMan exactly like a player
|
|
/// does, so the barrel pours a vanilla consumable's own status effect straight into whoever is
|
|
/// standing around it. Nothing new had to be added to DvergarBanco.cs for this to work, which was
|
|
/// the whole point of choosing a barrel over a hand-held flask: the buff is something you PREPARE
|
|
/// before walking out, not a button you press mid-fight.
|
|
///
|
|
/// WHAT ACTUALLY WORKS IS NOT A DESIGN DECISION, IT IS A FACT ABOUT EACH POTION. A mead that heals
|
|
/// over time heals a dvergr; a mead that regenerates stamina does nothing at all, because a creature
|
|
/// has no stamina bar to fill. The barrel therefore refuses nothing and promises nothing - it pours
|
|
/// what you put in it. `dumppotions` at the bottom of this file is how the two lists were told
|
|
/// apart: it reads every consumable in ObjectDB and reports, by reflection, what its effect actually
|
|
/// carries, so no guess about a field name is involved.
|
|
///
|
|
/// The serving code is race-agnostic on purpose - it drinks to any RaisedThrall in range, whoever
|
|
/// raised it - but the piece itself is locked to the Dvergar like the bank. If another chain ever
|
|
/// wants a barrel of its own, the lock is the only line that has to change.
|
|
/// </summary>
|
|
public static class DvergarBotte
|
|
{
|
|
public const string PieceName = "DvergarRuneBarrel";
|
|
public const string PieceDisplayName = "Rune Barrel";
|
|
public const string RaceId = DvergarConfig.RaceId;
|
|
|
|
/// <summary>How many drinks it holds. One dose is one mercenary served, so twenty is four
|
|
/// rounds for a full five-crew bank and a reason to keep brewing.</summary>
|
|
public const int MaxDoses = 20;
|
|
|
|
/// <summary>
|
|
/// Who is close enough to be handed a cup. Deliberately a range around the BARREL rather than
|
|
/// "everyone bound to the bank": a crew that is out across the map following its master is not
|
|
/// at the table, and buffing it from three biomes away would make the barrel a remote control.
|
|
/// </summary>
|
|
public const float ServeRange = 30f;
|
|
|
|
private const float BarrelScale = 0.7f;
|
|
|
|
/// <summary>Dvergr brass and verdigris, so it does not read as the Fenrir's trough - which is
|
|
/// the same barrel, tinted brown and rather bigger.</summary>
|
|
private static readonly Color BarrelTint = new Color(0.30f, 0.42f, 0.40f, 1f);
|
|
|
|
public static void Register()
|
|
{
|
|
GameObject prefab = PrefabManager.Instance.CreateClonedPrefab(PieceName, "piece_chest_barrel");
|
|
if (prefab == null)
|
|
{
|
|
Jotunn.Logger.LogWarning("[Dvergar] Could not clone piece_chest_barrel; the rune barrel is " +
|
|
"not registered.");
|
|
return;
|
|
}
|
|
|
|
// The barrel's Container is both the Hoverable and the Interactable, so a chest would
|
|
// swallow the click that fills it and the hover text that reports it. Same strip as the
|
|
// Fenrir trough, and the same reason.
|
|
foreach (Container container in prefab.GetComponentsInChildren<Container>(true))
|
|
{
|
|
if (container != null) Object.DestroyImmediate(container);
|
|
}
|
|
|
|
AswidBloodRefinery.StripOffensive(prefab, PieceName);
|
|
|
|
DvergarBotteBehaviour barrel = prefab.AddComponent<DvergarBotteBehaviour>();
|
|
|
|
foreach (Switch existing in prefab.GetComponentsInChildren<Switch>(true))
|
|
{
|
|
if (existing != null) Object.DestroyImmediate(existing);
|
|
}
|
|
barrel.m_switch = prefab.AddComponent<Switch>();
|
|
barrel.m_switch.m_name = PieceDisplayName;
|
|
|
|
prefab.transform.localScale = Vector3.one * BarrelScale;
|
|
Recolour(prefab, BarrelTint);
|
|
|
|
Piece piece = prefab.GetComponent<Piece>() ?? prefab.AddComponent<Piece>();
|
|
piece.m_craftingStation = null;
|
|
piece.m_resources = new Piece.Requirement[0];
|
|
piece.m_name = PieceDisplayName;
|
|
piece.m_canBeRemoved = true;
|
|
piece.m_groundPiece = true;
|
|
piece.m_groundOnly = true;
|
|
piece.m_icon = BorrowIcon("MeadHealthMinor") ?? BorrowIcon("TrophyDvergr");
|
|
|
|
Piece template = PrefabManager.Cache.GetPrefab<Piece>("piece_workbench");
|
|
if (template != null && template.m_placeEffect != null) piece.m_placeEffect = template.m_placeEffect;
|
|
|
|
// Cheap on purpose. The barrel is not the cost - the potions poured into it are, forever.
|
|
PieceManager.Instance.AddPiece(new CustomPiece(prefab, fixReference: false, new PieceConfig
|
|
{
|
|
Name = PieceDisplayName,
|
|
Description = "Pour a brew in and the crew drinks. What it does to them is whatever it " +
|
|
"would have done to you - if they have the parts for it.",
|
|
PieceTable = PieceTables.Hammer,
|
|
Category = PieceCategories.Crafting,
|
|
Icon = BorrowIcon("MeadHealthMinor") ?? BorrowIcon("TrophyDvergr"),
|
|
Requirements = new[]
|
|
{
|
|
new RequirementConfig("FineWood", 10, 0, true),
|
|
new RequirementConfig("BlackMarble", 5, 0, true),
|
|
new RequirementConfig("Sap", 1, 0, true),
|
|
}
|
|
}));
|
|
}
|
|
|
|
// ---------------------------------------------------------------- what a brew is
|
|
|
|
/// <summary>
|
|
/// The status effect a consumable would hand to whoever drank it, or null if the item is not a
|
|
/// drink at all. This is the ONLY test the barrel makes: anything with an effect on it can go
|
|
/// in, and whether that effect means anything to a dvergr is the potion's business, not ours.
|
|
/// </summary>
|
|
public static StatusEffect BrewEffect(string prefabName)
|
|
{
|
|
if (string.IsNullOrEmpty(prefabName)) return null;
|
|
|
|
ItemDrop drop = PrefabManager.Cache.GetPrefab<ItemDrop>(prefabName);
|
|
if (drop == null || drop.m_itemData == null || drop.m_itemData.m_shared == null) return null;
|
|
|
|
return drop.m_itemData.m_shared.m_consumeStatusEffect;
|
|
}
|
|
|
|
public static bool IsBrew(string prefabName) => BrewEffect(prefabName) != null;
|
|
|
|
public static string BrewDisplayName(string prefabName)
|
|
{
|
|
ItemDrop drop = PrefabManager.Cache.GetPrefab<ItemDrop>(prefabName);
|
|
if (drop == null || drop.m_itemData == null || drop.m_itemData.m_shared == null) return prefabName;
|
|
|
|
return Localization.instance.Localize(drop.m_itemData.m_shared.m_name);
|
|
}
|
|
|
|
public static void EnsureKnown(Player player)
|
|
{
|
|
if (player == null) return;
|
|
|
|
GameObject prefab = PrefabManager.Instance.GetPrefab(PieceName);
|
|
Piece piece = prefab != null ? prefab.GetComponent<Piece>() : null;
|
|
if (piece == null) return;
|
|
|
|
Traverse traverse = Traverse.Create(player);
|
|
traverse.Method("AddKnownPiece", piece).GetValue();
|
|
traverse.Method("UpdateAvailablePiecesList").GetValue();
|
|
}
|
|
|
|
// ---------------------------------------------------------------- helpers
|
|
|
|
private static Sprite BorrowIcon(string prefabName)
|
|
{
|
|
ItemDrop drop = 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 void Recolour(GameObject root, Color tint)
|
|
{
|
|
foreach (Renderer renderer in root.GetComponentsInChildren<Renderer>(true))
|
|
{
|
|
if (renderer == null || renderer is ParticleSystemRenderer) continue;
|
|
|
|
foreach (Material material in renderer.materials)
|
|
{
|
|
if (material == null) continue;
|
|
material.color = tint;
|
|
if (material.HasProperty("_Color")) material.SetColor("_Color", tint);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[HarmonyPatch(typeof(Player), "OnSpawned")]
|
|
public static class DvergarBotteKnownPatch
|
|
{
|
|
public static void Postfix(Player __instance)
|
|
{
|
|
if (__instance != Player.m_localPlayer) return;
|
|
DvergarBotte.EnsureKnown(__instance);
|
|
}
|
|
}
|
|
|
|
// Valheim 1.0 appended a 'bool cheated' parameter to this overload. The type array has to
|
|
// match it exactly or the patch silently fails to bind and PatchAll throws.
|
|
[HarmonyPatch(typeof(Player), "PlacePiece",
|
|
new[] { typeof(Piece), typeof(Vector3), typeof(Quaternion), typeof(bool), typeof(bool) })]
|
|
public static class DvergarBotteRaceLockPatch
|
|
{
|
|
public static bool Prefix(Player __instance, Piece __0)
|
|
{
|
|
if (__instance != Player.m_localPlayer || __0 == null) return true;
|
|
if (Utils.GetPrefabName(__0.gameObject) != DvergarBotte.PieceName) return true;
|
|
if (DvergarRace.IsDvergar()) return true;
|
|
|
|
__instance.Message(MessageHud.MessageType.Center, "You do not know what goes in it.");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// [DEBUG] Every drinkable in the game, and what its effect ACTUALLY carries.
|
|
///
|
|
/// Written because "which potions work on a mercenary" cannot be answered from the names: two meads
|
|
/// with almost the same label can be a heal-over-time and a stamina regen, and only one of those
|
|
/// means anything to a creature. Reading the effect is the answer, and reading it BY REFLECTION is
|
|
/// what makes this command safe to write without knowing a single field name of this build's
|
|
/// StatusEffect classes - a renamed or missing field is one line that does not print, not a
|
|
/// compile error and not a wrong answer.
|
|
///
|
|
/// dumppotions every consumable with an effect
|
|
/// dumppotions mead only the ones whose prefab name contains "mead"
|
|
/// </summary>
|
|
public class DumpPotionsCommand : ConsoleCommand
|
|
{
|
|
public override string Name => "dumppotions";
|
|
|
|
public override string Help =>
|
|
"[DEBUG] Lists every consumable with a status effect and what that effect carries, so it " +
|
|
"can be told whether a mercenary would feel it. Optional filter: dumppotions <substring>.";
|
|
|
|
public override void Run(string[] args)
|
|
{
|
|
if (ObjectDB.instance == null)
|
|
{
|
|
Console.instance.Print("ObjectDB is not up yet - be in a world.");
|
|
return;
|
|
}
|
|
|
|
string filter = args.Length > 0 ? args[0].ToLowerInvariant() : null;
|
|
int found = 0;
|
|
|
|
foreach (GameObject item in ObjectDB.instance.m_items)
|
|
{
|
|
if (item == null) continue;
|
|
if (filter != null && !item.name.ToLowerInvariant().Contains(filter)) continue;
|
|
|
|
ItemDrop drop = item.GetComponent<ItemDrop>();
|
|
if (drop == null || drop.m_itemData == null || drop.m_itemData.m_shared == null) continue;
|
|
|
|
StatusEffect effect = drop.m_itemData.m_shared.m_consumeStatusEffect;
|
|
if (effect == null) continue;
|
|
|
|
found++;
|
|
Jotunn.Logger.LogInfo($"[Potion] {item.name} ({effect.GetType().Name} '{effect.name}')" +
|
|
$"\n {Describe(effect)}");
|
|
}
|
|
|
|
string message = found == 0
|
|
? "No consumable carried a status effect - check the filter."
|
|
: $"{found} drinkable(s) written to the log with everything their effect carries.";
|
|
|
|
Console.instance.Print(message);
|
|
Jotunn.Logger.LogInfo($"[Potion] {message}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Every public field of the effect that is not at its default, as "name=value". Reflection
|
|
/// rather than a hand-written list of fields: this build's StatusEffect subclasses are whatever
|
|
/// they are, and the whole point of the command is to find that out instead of assuming it.
|
|
/// </summary>
|
|
private static string Describe(StatusEffect effect)
|
|
{
|
|
StringBuilder text = new StringBuilder();
|
|
|
|
foreach (FieldInfo field in effect.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance))
|
|
{
|
|
object value = field.GetValue(effect);
|
|
if (IsDefault(value)) continue;
|
|
|
|
// Collections and objects are reported as "present" rather than dumped - what matters
|
|
// is whether the effect has any, not what is in them.
|
|
string printed;
|
|
if (value is float f) printed = f.ToString("0.###");
|
|
else if (value is int || value is bool || value is string) printed = value.ToString();
|
|
else if (value is System.Collections.ICollection collection) printed = $"[{collection.Count}]";
|
|
else printed = value.GetType().Name;
|
|
|
|
if (text.Length > 0) text.Append(", ");
|
|
text.Append(field.Name).Append('=').Append(printed);
|
|
}
|
|
|
|
return text.Length > 0 ? text.ToString() : "(carries nothing - a flavour effect only)";
|
|
}
|
|
|
|
private static bool IsDefault(object value)
|
|
{
|
|
if (value == null) return true;
|
|
if (value is float f) return Mathf.Approximately(f, 0f);
|
|
if (value is int i) return i == 0;
|
|
if (value is bool b) return !b;
|
|
if (value is string s) return string.IsNullOrEmpty(s);
|
|
if (value is System.Collections.ICollection collection) return collection.Count == 0;
|
|
|
|
// A Unity object reference that has been destroyed compares equal to null.
|
|
if (value is Object unityObject) return unityObject == null;
|
|
return false;
|
|
}
|
|
}
|
|
}
|