RacesOfValheim/JotunnModStub/Sverm/SvermMedicine.cs
kanym b0d18b0654 feat(text): on-screen messages come from translation files
Every message the mod puts in the middle or corner of the screen now reads
its text through ModText.Get, and messages.json holds 191 texts in English
and Italian. Values computed in code become $1, $2 in the text, so a
translator can put them wherever the sentence needs them. Branching
messages get one key per branch.

A few messages travel through a field before they are shown: thralls'
refusals and the building limits now hold a token or key there. Boss and
form names inside messages use tokens too, the bosses the game's own, so
"The Elder" arrives translated.

ModText.Get now localizes each argument before inserting it. A token
argument left for a later pass ran into the text after it: "$1's power"
became the unknown token "enemy_eikthyr's".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 20:30:03 +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, ModText.Get("rov_msg_sverm_not_hurt", who));
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;
}
}
}