RacesOfValheim/JotunnModStub/Sverm/SvermMedicine.cs
2026-08-11 16:30:20 +02:00

114 lines
5.3 KiB
C#

using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Honey and royal jelly, pointed at one of the colony's own insects instead of eaten.
///
/// SECTION 19.4, and it closes a hole this branch opened itself. The two of them were taken OUT
/// of the biomass on purpose - feeding the hive its own medicine would be the worst kind of bug,
/// quiet and expensive - and until now that left them doing nothing for the colony at all.
///
/// HOW IT IS GIVEN: hold the jar in the hotbar, look at the insect, press the slot. Not a new
/// key, not a new UI, and no aiming mode - pressing a hotbar slot already runs Humanoid.UseItem,
/// and the game already knows which creature the player is looking at.
///
/// NOT A SECOND PREFIX. SvermFood already owns a prefix on Humanoid.UseItem (it swaps a real
/// item for the hidden clone that carries the Sverm's food numbers), and two prefixes on one
/// method that both want to be first is a coin toss - the same class of trap as the two
/// Interactables on the nest. This is called FROM that one, at the top, so the order is written
/// down rather than hoped for.
///
/// Aiming at nothing, or at anything that is not ours, falls through and the honey is eaten as
/// usual: honey is still food, and at double value, which is the whole of section 4.
/// </summary>
public static class SvermMedicine
{
/// <summary>
/// How far the medicine reaches. Player.m_hoveringCreature has NO range cap of its own - it
/// is set from a fifty metre raycast - so without this a jar of honey would heal a seeker
/// most of a field away.
/// </summary>
private const float Reach = 5f;
/// <summary>
/// Tries to use this item as medicine. True means it was handled and nothing else should
/// happen to it.
/// </summary>
public static bool TryAdminister(Player player, ItemDrop.ItemData item)
{
if (player == null) return false;
float healing = HealingOf(item);
if (healing <= 0f) return false;
Character patient = HoveredInsect(player);
if (patient == null) return false; // not pointed at one of ours: it is food again
if (patient.GetHealth() >= patient.GetMaxHealth())
{
// Refused rather than eaten. Somebody looking straight at their own seeker while
// pressing the honey meant to heal it, and swallowing it instead is the wrong
// surprise - especially at ninety points a jar of royal jelly.
// Localized, because a creature's m_name is a token: printing it raw would put
// "$enemy_seeker is not hurt" on the screen.
string who = Localization.instance.Localize(patient.m_name);
player.Message(MessageHud.MessageType.Center, $"{who} is not hurt.");
return true;
}
patient.Heal(healing, true);
Inventory inventory = player.GetInventory();
if (inventory != null) inventory.RemoveItem(item, 1);
// The same feedback eating gives, for the same reason: something has to say it worked.
EffectList consume = Traverse.Create((Humanoid)player).Field<EffectList>("m_consumeItemEffects").Value;
if (consume != null)
{
consume.Create(player.transform.position, Quaternion.identity, player.transform, 1f, -1);
}
return true;
}
/// <summary>What one of this heals, or zero if it is not medicine.</summary>
private static float HealingOf(ItemDrop.ItemData item)
{
if (item == null || item.m_shared == null) return 0f;
string prefabName = item.m_dropPrefab != null ? item.m_dropPrefab.name : "";
if (prefabName == "Honey") return SvermConfig.HoneyHealing;
if (prefabName == SvermConfig.RoyalJellyPrefab) return SvermConfig.RoyalJellyHealing;
return 0f;
}
/// <summary>
/// The colony insect the player is looking at, or null.
///
/// OURS AND NOT EVERY INSECT: a worker, or a troop hatched at a hive. A tamed seeker
/// somebody else raised is not the colony's business, and neither is a wild one.
/// </summary>
private static Character HoveredInsect(Player player)
{
// m_hoveringCreature is private in the assembly this project compiles against - the same
// field, read the same way, as FactionMerchantHoverPatch.
Character hovering = Traverse.Create(player).Field("m_hoveringCreature").GetValue<Character>();
if (hovering == null || hovering.IsDead()) return null;
if (Vector3.Distance(player.transform.position, hovering.transform.position) > Reach) return null;
if (hovering.GetComponent<SvermWorkerBehaviour>() != null) return hovering;
RaisedThrall thrall = hovering.GetComponent<RaisedThrall>();
if (thrall != null && !string.IsNullOrEmpty(thrall.Kind) && thrall.Kind.StartsWith(SvermBrood.PrefabPrefix))
{
return hovering;
}
return null;
}
}
}