The mod's own items, build pieces and summoned creatures now carry $rov_item_, $rov_piece_ and $rov_creature_ tokens for their names and descriptions, and items.json holds the texts in English and Italian. Valheim localizes these fields wherever it shows them: tooltips, hover texts, messages, the build menu. Where a name doubles as an identifier, both sides use the same constant, so the comparisons still hold. The Aswid foods keep their prefix as a constant for the refinery's check and now name the creature with its vanilla token, so "Boar" translates too. The Furling crops get one whole description per crop instead of an English sentence with the metal dropped in. The Race Menu Ward's panel title is a mod UI Text, so it reads the translation directly. Saves are unaffected: items and eaten foods are stored by prefab name. The Sverm hive's goods stay for the merchant step. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
313 lines
14 KiB
C#
313 lines
14 KiB
C#
using System.Collections;
|
|
using HarmonyLib;
|
|
using Jotunn.Configs;
|
|
using Jotunn.Entities;
|
|
using Jotunn.Managers;
|
|
using UnityEngine;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
/// <summary>
|
|
/// The second step of the refinement chain, brewed at the Mead Ketill:
|
|
///
|
|
/// 5 Blood Essence -> 1 Superior Blood Essence
|
|
/// 3 Blood Essence + 5 Blueberries
|
|
/// + 5 Yellow Mushrooms -> Blood Potion mead base
|
|
/// 3 Superior Blood Essence + 5 Sap
|
|
/// + 5 Cloudberry -> Superior Blood Potion mead base
|
|
///
|
|
/// Either base then goes into a Fermenter and comes out as five bottles. Drinking one force
|
|
/// feeds every food slot with the matching essence - full stomachs and all - and heals a
|
|
/// percentage of maximum health over eight seconds.
|
|
/// </summary>
|
|
public static class AswidBloodPotions
|
|
{
|
|
public const string BaseName = "MeadBaseBloodPotion";
|
|
public const string PotionName = "MeadBloodPotion";
|
|
public const string SuperiorBaseName = "MeadBaseSuperiorBloodPotion";
|
|
public const string SuperiorPotionName = "MeadSuperiorBloodPotion";
|
|
|
|
private const string PotionDisplayName = "$rov_item_aswid_bloodpotion_name";
|
|
private const string SuperiorPotionDisplayName = "$rov_item_aswid_superiorbloodpotion_name";
|
|
|
|
private const float HealDuration = 8f;
|
|
private const float HealFraction = 0.05f;
|
|
private const float SuperiorHealFraction = 0.10f;
|
|
|
|
// Five bottles per barrel, as designed.
|
|
private const int BottlesPerBrew = 5;
|
|
|
|
/// <summary>
|
|
/// Blood brews are done in a quarter of the normal time. The fermenter keeps a single
|
|
/// duration for every recipe, so instead of shortening it - which would speed up every
|
|
/// vanilla mead in the barrel - the elapsed time is multiplied while our base is inside.
|
|
/// </summary>
|
|
public const double FermentSpeedMultiplier = 4.0;
|
|
|
|
private static readonly System.Reflection.MethodInfo GetContentMethod =
|
|
AccessTools.Method(typeof(Fermenter), "GetContent");
|
|
|
|
public static void Register()
|
|
{
|
|
SE_BloodPotion drink = CreateEffect("SE_BloodPotion", PotionDisplayName, HealFraction,
|
|
AswidBloodRefinery.EssenceName, "GemstoneRed");
|
|
SE_BloodPotion superiorDrink = CreateEffect("SE_SuperiorBloodPotion", SuperiorPotionDisplayName,
|
|
SuperiorHealFraction, AswidBloodRefinery.SuperiorEssenceName, "MoltenCore");
|
|
|
|
GameObject meadBase = CloneItem(BaseName, "MeadBaseHealthMinor", "$rov_item_aswid_bloodpotionbase_name",
|
|
"$rov_item_aswid_bloodpotionbase_desc");
|
|
GameObject potion = CloneItem(PotionName, "MeadHealthMinor", PotionDisplayName,
|
|
"$rov_item_aswid_bloodpotion_desc");
|
|
GameObject superiorBase = CloneItem(SuperiorBaseName, "MeadBaseHealthMedium", "$rov_item_aswid_superiorbloodpotionbase_name",
|
|
"$rov_item_aswid_superiorbloodpotionbase_desc");
|
|
GameObject superiorPotion = CloneItem(SuperiorPotionName, "MeadHealthMedium", SuperiorPotionDisplayName,
|
|
"$rov_item_aswid_superiorbloodpotion_desc");
|
|
|
|
AttachEffect(potion, drink);
|
|
AttachEffect(superiorPotion, superiorDrink);
|
|
|
|
AddRecipe(AswidBloodRefinery.SuperiorEssenceName, new[]
|
|
{
|
|
new RequirementConfig(AswidBloodRefinery.EssenceName, 5),
|
|
});
|
|
|
|
AddRecipe(BaseName, new[]
|
|
{
|
|
new RequirementConfig(AswidBloodRefinery.EssenceName, 3),
|
|
new RequirementConfig("Blueberries", 5),
|
|
// "Funghi luminosi": the Yellow Mushroom, the one that glows in the burial
|
|
// chambers. Same tier as the blueberries, unlike the Mistlands blue one.
|
|
new RequirementConfig("MushroomYellow", 5),
|
|
});
|
|
|
|
AddRecipe(SuperiorBaseName, new[]
|
|
{
|
|
new RequirementConfig(AswidBloodRefinery.SuperiorEssenceName, 3),
|
|
new RequirementConfig("Sap", 5),
|
|
new RequirementConfig("Cloudberry", 5),
|
|
});
|
|
|
|
AddFermenterConversion(meadBase, potion);
|
|
AddFermenterConversion(superiorBase, superiorPotion);
|
|
}
|
|
|
|
// ---------------------------------------------------------------- items
|
|
|
|
private static GameObject CloneItem(string prefabName, string source, string displayName, string description)
|
|
{
|
|
GameObject prefab = PrefabManager.Instance.CreateClonedPrefab(prefabName, source);
|
|
if (prefab == null)
|
|
{
|
|
Jotunn.Logger.LogWarning($"[Aswid] Could not clone '{source}'; {displayName} not registered.");
|
|
return null;
|
|
}
|
|
|
|
ItemDrop drop = prefab.GetComponent<ItemDrop>();
|
|
if (drop == null) return null;
|
|
|
|
ItemDrop.ItemData.SharedData shared = drop.m_itemData.m_shared;
|
|
shared.m_name = displayName;
|
|
shared.m_description = description;
|
|
// The clone arrives carrying the vanilla health mead's own effect; ours is attached
|
|
// afterwards, and the mead bases must carry none at all.
|
|
shared.m_consumeStatusEffect = null;
|
|
|
|
ItemManager.Instance.AddItem(new CustomItem(prefab, fixReference: false));
|
|
return prefab;
|
|
}
|
|
|
|
private static void AttachEffect(GameObject potion, SE_BloodPotion effect)
|
|
{
|
|
ItemDrop drop = potion != null ? potion.GetComponent<ItemDrop>() : null;
|
|
if (drop == null || effect == null) return;
|
|
drop.m_itemData.m_shared.m_consumeStatusEffect = effect;
|
|
}
|
|
|
|
private static void AddRecipe(string item, RequirementConfig[] requirements)
|
|
{
|
|
ItemManager.Instance.AddRecipe(new CustomRecipe(new RecipeConfig
|
|
{
|
|
Item = item,
|
|
Amount = 1,
|
|
CraftingStation = CraftingStations.MeadKetill,
|
|
Requirements = requirements,
|
|
}));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Teaches the vanilla fermenter one more brew. The vanilla prefab's conversion list is
|
|
/// appended to rather than replaced, so every stock mead still works.
|
|
/// </summary>
|
|
private static void AddFermenterConversion(GameObject from, GameObject to)
|
|
{
|
|
if (from == null || to == null) return;
|
|
|
|
GameObject barrel = PrefabManager.Cache.GetPrefab<GameObject>("fermenter");
|
|
Fermenter fermenter = barrel != null ? barrel.GetComponent<Fermenter>() : null;
|
|
if (fermenter == null)
|
|
{
|
|
Jotunn.Logger.LogWarning("[Aswid] No fermenter prefab; blood potions cannot be brewed.");
|
|
return;
|
|
}
|
|
|
|
ItemDrop fromDrop = from.GetComponent<ItemDrop>();
|
|
ItemDrop toDrop = to.GetComponent<ItemDrop>();
|
|
if (fromDrop == null || toDrop == null) return;
|
|
|
|
foreach (Fermenter.ItemConversion existing in fermenter.m_conversion)
|
|
{
|
|
if (existing != null && existing.m_from == fromDrop) return; // already taught
|
|
}
|
|
|
|
fermenter.m_conversion.Add(new Fermenter.ItemConversion
|
|
{
|
|
m_from = fromDrop,
|
|
m_to = toDrop,
|
|
m_producedItems = BottlesPerBrew,
|
|
});
|
|
}
|
|
|
|
// ---------------------------------------------------------------- status effect
|
|
|
|
private static SE_BloodPotion CreateEffect(string name, string displayName, float fraction,
|
|
string essencePrefab, string iconSource)
|
|
{
|
|
SE_BloodPotion effect = ScriptableObject.CreateInstance<SE_BloodPotion>();
|
|
effect.name = name;
|
|
effect.m_name = displayName;
|
|
effect.m_tooltip = "$rov_se_aswid_bloodpotion_tooltip"; // numbers go in at display, see GetTooltipString
|
|
effect.m_ttl = HealDuration;
|
|
effect.m_healFraction = fraction;
|
|
effect.m_essencePrefab = essencePrefab;
|
|
|
|
// A flat m_healthOverTime cannot express "5% of maximum health", so the real number is
|
|
// computed per drinker in SE_BloodPotion.Setup. One tick a second over the eight.
|
|
effect.m_healthOverTimeDuration = HealDuration;
|
|
effect.m_healthOverTimeInterval = 1f;
|
|
|
|
ItemDrop source = PrefabManager.Cache.GetPrefab<ItemDrop>(iconSource);
|
|
Sprite[] icons = source != null && source.m_itemData != null && source.m_itemData.m_shared != null
|
|
? source.m_itemData.m_shared.m_icons
|
|
: null;
|
|
if (icons != null && icons.Length > 0) effect.m_icon = icons[0];
|
|
|
|
ItemManager.Instance.AddStatusEffect(new CustomStatusEffect(effect, fixReference: false));
|
|
return effect;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Force feeds every food slot with the given essence, ignoring whether the stomachs are
|
|
/// full - that refusal is the whole point of drinking the potion. The Aswid's fourth slot
|
|
/// is filled too once the progression has unlocked it.
|
|
/// </summary>
|
|
public static void FillStomachs(Player player, string essencePrefabName)
|
|
{
|
|
if (player == null || ObjectDB.instance == null) return;
|
|
|
|
GameObject prefab = ObjectDB.instance.GetItemPrefab(essencePrefabName);
|
|
ItemDrop drop = prefab != null ? prefab.GetComponent<ItemDrop>() : null;
|
|
if (drop == null) return;
|
|
|
|
Traverse traverse = Traverse.Create(player);
|
|
IList foods = traverse.Field("m_foods").GetValue<IList>();
|
|
if (foods == null) return;
|
|
|
|
foods.Clear();
|
|
for (int slot = 0; slot < 3; slot++)
|
|
{
|
|
foods.Add(MakeFood(drop, prefab, essencePrefabName));
|
|
}
|
|
|
|
if (AswidRace.IsAswid() && BossProgressionManager.GetCurrentProgressionState() >= 8)
|
|
{
|
|
ItemDrop.ItemData item = drop.m_itemData.Clone();
|
|
item.m_dropPrefab = prefab;
|
|
AswidFoodManager.SetExtraFood(player, item);
|
|
}
|
|
|
|
traverse.Method("UpdateFood", 0f, true).GetValue();
|
|
}
|
|
|
|
/// <summary>True while one of our two mead bases is the thing fermenting in that barrel.</summary>
|
|
public static bool IsBloodBrew(Fermenter fermenter)
|
|
{
|
|
if (fermenter == null || GetContentMethod == null) return false;
|
|
|
|
// Valheim 1.0: GetContent returns the stable hash of the prefab name, not the name. The old
|
|
// "as string" read null for every barrel and the speed-up silently stopped.
|
|
int content = System.Convert.ToInt32(GetContentMethod.Invoke(fermenter, null));
|
|
return content != 0
|
|
&& (content == BaseName.GetStableHashCode() || content == SuperiorBaseName.GetStableHashCode());
|
|
}
|
|
|
|
private static Player.Food MakeFood(ItemDrop drop, GameObject prefab, string prefabName)
|
|
{
|
|
ItemDrop.ItemData item = drop.m_itemData.Clone();
|
|
// Vanilla reads m_dropPrefab.name when it rebuilds a food entry, and a cloned
|
|
// ItemData off a raw prefab does not carry it.
|
|
item.m_dropPrefab = prefab;
|
|
|
|
return new Player.Food
|
|
{
|
|
m_name = prefabName,
|
|
m_item = item,
|
|
m_time = item.m_shared.m_foodBurnTime,
|
|
m_health = item.m_shared.m_food,
|
|
m_stamina = item.m_shared.m_foodStamina,
|
|
m_eitr = item.m_shared.m_foodEitr,
|
|
};
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Blood ferments fast. GetFermentationTime is the single place the barrel measures progress
|
|
/// from - GetStatus is its only caller - so scaling the elapsed time here makes the brew ready
|
|
/// at a quarter of the duration and keeps the hover text agreeing with it, without touching
|
|
/// m_fermentationDuration and dragging every vanilla mead along.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(Fermenter), "GetFermentationTime")]
|
|
public static class AswidBloodPotionFermentPatch
|
|
{
|
|
public static void Postfix(Fermenter __instance, ref double __result)
|
|
{
|
|
if (__result <= 0.0) return;
|
|
if (!AswidBloodPotions.IsBloodBrew(__instance)) return;
|
|
|
|
__result *= AswidBloodPotions.FermentSpeedMultiplier;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The effect both potions apply. Heals a fraction of maximum health rather than a flat
|
|
/// amount, which SE_Stats cannot express on its own, and refills the drinker's food slots.
|
|
/// </summary>
|
|
public class SE_BloodPotion : SE_Stats
|
|
{
|
|
public float m_healFraction = 0.05f;
|
|
public string m_essencePrefab = "";
|
|
|
|
/// <summary>
|
|
/// The tooltip's $1 and $2 are the heal share and the duration. Filled in here, at display
|
|
/// time, because a translation can only be looked up once the language is loaded, and the
|
|
/// game localizes this string after us in a single pass that would leave $1 as it is.
|
|
/// </summary>
|
|
public override string GetTooltipString()
|
|
{
|
|
string text = base.GetTooltipString();
|
|
if (Localization.instance == null) return text;
|
|
return Localization.instance.Localize(text,
|
|
Mathf.RoundToInt(m_healFraction * 100f).ToString(), m_ttl.ToString("0"));
|
|
}
|
|
|
|
public override void Setup(Character character)
|
|
{
|
|
// SEMan clones the effect before setting it up, so writing the resolved amount here
|
|
// touches this drinker's copy only.
|
|
if (character != null) m_healthOverTime = character.GetMaxHealth() * m_healFraction;
|
|
|
|
base.Setup(character);
|
|
|
|
AswidBloodPotions.FillStomachs(character as Player, m_essencePrefab);
|
|
}
|
|
}
|
|
}
|