301 lines
13 KiB
C#
301 lines
13 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 = "Blood Potion";
|
|
private const string SuperiorPotionDisplayName = "Superior Blood Potion";
|
|
|
|
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", "Blood Potion Mead Base",
|
|
"Blood, berries and crypt light, waiting on a barrel.");
|
|
GameObject potion = CloneItem(PotionName, "MeadHealthMinor", PotionDisplayName,
|
|
"Drink and the hunger stops mattering for a while. Fills every stomach with blood essence, " +
|
|
"full or not, and knits the flesh back together.");
|
|
GameObject superiorBase = CloneItem(SuperiorBaseName, "MeadBaseHealthMedium", "Superior Blood Potion Mead Base",
|
|
"The twice-rendered draught, still raw.");
|
|
GameObject superiorPotion = CloneItem(SuperiorPotionName, "MeadHealthMedium", SuperiorPotionDisplayName,
|
|
"The same mercy, twice as deep.");
|
|
|
|
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 = $"Stomachs filled with blood. Healing {Mathf.RoundToInt(fraction * 100f)}% over {HealDuration:0} seconds.";
|
|
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 = "";
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|