The startup error came from WarVultureVisual.BuildTemplate, not from the Ravager costume: its passes tried Humanoid before CharacterDrop. Components another one still requires now wait for a later pass.
360 lines
17 KiB
C#
360 lines
17 KiB
C#
using System.Collections.Generic;
|
|
using HarmonyLib;
|
|
using Jotunn.Configs;
|
|
using Jotunn.Entities;
|
|
using Jotunn.Managers;
|
|
using UnityEngine;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
/// <summary>
|
|
/// The War Vulture item: a Dead Raiser with the skeleton taken out of it and a vulture trophy
|
|
/// hung in its place. Clicked, it puts the player on a flying bird; clicked again, it brings
|
|
/// them down and takes the bird away.
|
|
///
|
|
/// Why a staff and not a trinket: the game lets a WEAPON be used without any trickery. Cloning
|
|
/// StaffSkeleton buys the click, the cast gesture and the effects for free - which is exactly
|
|
/// the reasoning that produced Skadis Nokkel, whose file this one was written as a twin of. The
|
|
/// Nokkel has since been removed - it opened a portal pair, which broke in multiplayer - so this
|
|
/// file is now the only copy of that pattern and there is no sibling to keep it in step with.
|
|
///
|
|
/// Three things are removed from the vanilla staff, and each is removed TWICE on purpose:
|
|
/// - the eitr, health and stamina costs are zeroed on the clone AND forced to zero in the
|
|
/// getters every cost check flows through (WarVultureFreeCastPatch). The eitr cost is not
|
|
/// cosmetic: it is checked BEFORE the attack starts, so a player without eitr would never
|
|
/// reach the code that summons;
|
|
/// - the summon is nulled on the clone AND the attack trigger itself is taken over, so
|
|
/// vanilla's spawn code is not merely defused but unreachable (WarVultureCastPatch);
|
|
/// - the staff's model is replaced by the trophy under the item's "attach" child, which is
|
|
/// the ONLY child VisEquipment draws - a model hung anywhere else is present, correct and
|
|
/// invisible (design doc 15.11, half an hour paid for once already).
|
|
/// </summary>
|
|
public static class WarVultureItem
|
|
{
|
|
public const string PrefabName = "WarVulture";
|
|
public const string DisplayName = "$rov_item_warvulture_name";
|
|
|
|
private const string SourcePrefab = "StaffSkeleton";
|
|
private const string TrophyPrefab = "TrophyVolture";
|
|
private const string Label = "WarVulture";
|
|
|
|
/// <summary>The three materials. Ids, not display names: the ingot is BlackMetal (the SCRAP
|
|
/// is the ore), and the raw lox meat is LoxMeat, with CookedLoxMeat being the other one.</summary>
|
|
private const string GreydwarfEye = "GreydwarfEye";
|
|
private const string BlackMetalIngot = "BlackMetal";
|
|
private const string RawLoxMeat = "LoxMeat";
|
|
|
|
public static void Register()
|
|
{
|
|
GameObject prefab = PrefabManager.Instance.CreateClonedPrefab(PrefabName, SourcePrefab);
|
|
if (prefab == null)
|
|
{
|
|
Jotunn.Logger.LogWarning($"[{Label}] Could not clone {SourcePrefab}; {DisplayName} not registered.");
|
|
return;
|
|
}
|
|
|
|
ItemDrop drop = prefab.GetComponent<ItemDrop>();
|
|
if (drop == null || drop.m_itemData == null || drop.m_itemData.m_shared == null)
|
|
{
|
|
Jotunn.Logger.LogWarning($"[{Label}] {SourcePrefab} clone has no usable ItemDrop; {DisplayName} not registered.");
|
|
return;
|
|
}
|
|
|
|
ItemDrop.ItemData.SharedData shared = drop.m_itemData.m_shared;
|
|
shared.m_name = DisplayName;
|
|
shared.m_description = "$rov_item_warvulture_desc";
|
|
shared.m_useDurability = false;
|
|
|
|
// ItemType is left exactly as the vanilla staff has it: it decides the equip slot and
|
|
// which animation set the arms use, and the cast gesture is the whole reason this is a
|
|
// staff rather than a trinket.
|
|
|
|
Strip(shared);
|
|
SwapHandModel(prefab);
|
|
|
|
ItemManager.Instance.AddItem(new CustomItem(prefab, fixReference: false));
|
|
|
|
// Built HERE and not at the summon, and the comment on WarVultureVisual explains why in
|
|
// full: this is a context where components may be destroyed immediately, and the summon
|
|
// is not. Getting that wrong once put a live Ashlands predator in the player's world.
|
|
WarVultureVisual.BuildTemplate();
|
|
|
|
// The forge, and not the workbench, for the same reason Trol's Hammer sits on a
|
|
// workbench: the station is picked by the material that is hardest to have. Eyes and
|
|
// meat are picked up off the ground; a black metal ingot means a blast furnace already
|
|
// exists, and the forge is where that metal is worked.
|
|
ItemManager.Instance.AddRecipe(new CustomRecipe(new RecipeConfig
|
|
{
|
|
Item = PrefabName,
|
|
Amount = 1,
|
|
CraftingStation = CraftingStations.Forge,
|
|
MinStationLevel = 1,
|
|
Requirements = new[]
|
|
{
|
|
new RequirementConfig(GreydwarfEye, 10, 0, true),
|
|
new RequirementConfig(BlackMetalIngot, 2, 0, true),
|
|
new RequirementConfig(RawLoxMeat, 2, 0, true),
|
|
},
|
|
}));
|
|
|
|
Jotunn.Logger.LogInfo($"[{Label}] {DisplayName} registered: no eitr, no health, no stamina, no skeleton.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Takes the skeleton staff apart. Every field is zeroed on BOTH attacks, because the
|
|
/// secondary is a separate Attack carrying its own copy of every cost.
|
|
///
|
|
/// The summon hangs off a SpawnAbility on a prefab referenced by the attack, and WHICH
|
|
/// field carries it is not knowable from the game files - so all three candidates go.
|
|
/// Nulling three fields that might each be the culprit costs nothing; guessing wrong costs
|
|
/// a skeleton on every click.
|
|
///
|
|
/// The animation is the one thing deliberately left alone. It is the whole point: the
|
|
/// player raises the trophy, and the bird answers.
|
|
/// </summary>
|
|
private static void Strip(ItemDrop.ItemData.SharedData shared)
|
|
{
|
|
StripAttack(shared.m_attack);
|
|
StripAttack(shared.m_secondaryAttack);
|
|
}
|
|
|
|
private static void StripAttack(Attack attack)
|
|
{
|
|
if (attack == null) return;
|
|
|
|
attack.m_attackEitr = 0f;
|
|
attack.m_drawEitrDrain = 0f;
|
|
attack.m_reloadEitrDrain = 0f;
|
|
|
|
attack.m_attackHealth = 0f;
|
|
attack.m_attackHealthPercentage = 0f;
|
|
attack.m_attackHealthLowBlockUse = false;
|
|
attack.m_attackHealthReturnHit = 0f;
|
|
|
|
attack.m_attackStamina = 0f;
|
|
attack.m_drawStaminaDrain = 0f;
|
|
attack.m_reloadStaminaDrain = 0f;
|
|
|
|
attack.m_attackProjectile = null;
|
|
attack.m_spawnOnTrigger = null;
|
|
attack.m_spawnOnHit = null;
|
|
attack.m_spawnOnHitChance = 0f;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Puts the vulture trophy in the hand where the staff's model was.
|
|
///
|
|
/// "attach" is not a name picked for tidiness: VisEquipment instantiates that child and
|
|
/// nothing else, so this is the only place a held model can live.
|
|
///
|
|
/// The size is a MULTIPLIER rather than a measurement in metres, which is the opposite of
|
|
/// what ModelBorrow does and is right here for one reason: the brief was "the trophy, 40%
|
|
/// smaller". The trophy's own size is the measurement; 0.6 is the relation applied to it.
|
|
/// </summary>
|
|
private static void SwapHandModel(GameObject prefab)
|
|
{
|
|
Transform attach = FindChild(prefab.transform, "attach");
|
|
if (attach == null)
|
|
{
|
|
Jotunn.Logger.LogWarning($"[{Label}] The {SourcePrefab} clone has no 'attach' child, so the " +
|
|
"held model was left as the vanilla staff. VisEquipment draws " +
|
|
"nothing else, so there was nowhere else to put it.");
|
|
return;
|
|
}
|
|
|
|
GameObject trophy = PrefabManager.Cache.GetPrefab<GameObject>(TrophyPrefab);
|
|
if (trophy == null)
|
|
{
|
|
Jotunn.Logger.LogWarning($"[{Label}] No '{TrophyPrefab}' to borrow; the item keeps the " +
|
|
"skeleton staff's look.");
|
|
return;
|
|
}
|
|
|
|
// The old staff mesh goes first, or the hand holds both.
|
|
for (int i = attach.childCount - 1; i >= 0; i--)
|
|
{
|
|
Object.DestroyImmediate(attach.GetChild(i).gameObject);
|
|
}
|
|
foreach (Renderer renderer in attach.GetComponents<Renderer>())
|
|
{
|
|
if (renderer != null) Object.DestroyImmediate(renderer);
|
|
}
|
|
|
|
// Instantiate into a holder that is ALREADY INACTIVE - the one line ModelBorrow exists
|
|
// for. A trophy is an ItemDrop, and Instantiate runs Awake first: waking one for a
|
|
// single frame drops a real trophy on the floor.
|
|
GameObject holder = new GameObject("WarVultureTrophy");
|
|
holder.transform.SetParent(attach, false);
|
|
holder.SetActive(false);
|
|
|
|
GameObject copy = Object.Instantiate(trophy, holder.transform);
|
|
copy.name = "model";
|
|
copy.transform.localPosition = Vector3.zero;
|
|
copy.transform.localRotation = Quaternion.identity;
|
|
copy.transform.localScale = Vector3.one * WarVultureConfig.HandModelScale.Value;
|
|
|
|
StripToMeshesOnly(copy);
|
|
|
|
holder.SetActive(true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The trophy reduced to nothing but its shape.
|
|
///
|
|
/// This used to be a BLACKLIST - scripts, colliders, rigidbodies, ZNetView - and it let
|
|
/// through exactly what a blacklist always lets through: the things nobody thought to name.
|
|
/// A vanilla item prefab carries the sparkle that makes dropped loot findable in long grass,
|
|
/// and that lives in ParticleSystems, ParticleSystemRenderers and Lights. None of those is a
|
|
/// MonoBehaviour, so all of them survived, and the item glittered in the player's hand like
|
|
/// something they had just thrown away.
|
|
///
|
|
/// So it is a whitelist now, the same shape as WarVultureVisual's: MeshFilter,
|
|
/// MeshRenderer, SkinnedMeshRenderer, Transform. Note it is deliberately NARROWER than the
|
|
/// mount's whitelist - "Renderer" would be wrong here, because ParticleSystemRenderer IS a
|
|
/// Renderer and it is half of what we are trying to be rid of.
|
|
///
|
|
/// Passes, because Unity refuses to destroy a component another one still requires, and the
|
|
/// refusal only unblocks once the dependent is gone.
|
|
/// </summary>
|
|
private static void StripToMeshesOnly(GameObject copy)
|
|
{
|
|
for (int pass = 0; pass < 6; pass++)
|
|
{
|
|
bool destroyedSomething = false;
|
|
|
|
foreach (Component component in copy.GetComponentsInChildren<Component>(true))
|
|
{
|
|
if (component == null) continue;
|
|
if (component is Transform) continue;
|
|
if (component is MeshFilter) continue;
|
|
if (component is MeshRenderer) continue;
|
|
if (component is SkinnedMeshRenderer) continue;
|
|
if (ModelBorrow.IsRequired(component)) continue; // a later pass, once its dependent is gone
|
|
|
|
Object.DestroyImmediate(component);
|
|
|
|
if (component == null) destroyedSomething = true;
|
|
}
|
|
|
|
if (!destroyedSomething) break;
|
|
}
|
|
}
|
|
|
|
private static Transform FindChild(Transform root, string name)
|
|
{
|
|
if (root.name == name) return root;
|
|
for (int i = 0; i < root.childCount; i++)
|
|
{
|
|
Transform found = FindChild(root.GetChild(i), name);
|
|
if (found != null) return found;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static bool IsWarVulture(ItemDrop.ItemData item)
|
|
{
|
|
return item != null && item.m_dropPrefab != null && item.m_dropPrefab.name == PrefabName;
|
|
}
|
|
|
|
/// <summary>
|
|
/// True when a cost being asked about belongs to this item.
|
|
///
|
|
/// The fallback on the equipped weapon is necessary rather than belt-and-braces: the cost
|
|
/// getters are called from Attack.Start and m_weapon is not guaranteed to be assigned yet.
|
|
/// It is only consulted when m_weapon is null, so a creature's own attack cannot be zeroed
|
|
/// just because the player happens to be holding the trophy at the time.
|
|
/// </summary>
|
|
public static bool IsWarVultureContext(ItemDrop.ItemData weapon)
|
|
{
|
|
if (weapon != null) return IsWarVulture(weapon);
|
|
|
|
Player local = Player.m_localPlayer;
|
|
return local != null && IsWarVulture(local.GetCurrentWeapon());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Who gets to see the recipe: the same list the bird itself answers to
|
|
/// (WarVultureConfig.AllowedRaces, "Furling,Dvergar" out of the box), read through the same
|
|
/// helper, so the crafting menu and the summon can never disagree. A race that could craft the
|
|
/// item but not call the bird would just be selling someone ten greydwarf eyes' worth of stick.
|
|
///
|
|
/// Player.GetAvailableRecipes is the single funnel the crafting list is built from, and it is
|
|
/// public and takes the list by ref - so a Postfix that removes one entry is the whole gate.
|
|
/// The recipe is still REGISTERED for everybody, which is deliberate: it stays in ObjectDB, so
|
|
/// a character who changes race sees it appear or vanish with no world edit and no re-register.
|
|
///
|
|
/// Filtering here rather than at the craft click also means the refusal never has to be
|
|
/// explained - an item that was never in the list cannot be clicked.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(Player), nameof(Player.GetAvailableRecipes))]
|
|
public static class WarVultureRecipeRacePatch
|
|
{
|
|
private static void Postfix(Player __instance, ref List<Recipe> available)
|
|
{
|
|
if (__instance == null || available == null) return;
|
|
if (WarVultureConfig.RaceAllowed(WarVultureConfig.RaceOf(__instance))) return;
|
|
|
|
available.RemoveAll(recipe =>
|
|
recipe != null &&
|
|
recipe.m_item != null &&
|
|
Utils.GetPrefabName(recipe.m_item.gameObject) == WarVultureItem.PrefabName);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The click. Attack.OnAttackTrigger is used rather than Attack.Start because it takes NO
|
|
/// parameters - its signature cannot drift - and it fires when the animation reaches its
|
|
/// release point, which is when a summon should land.
|
|
///
|
|
/// A Prefix returning FALSE, so for this item vanilla's attack-trigger code never runs at all.
|
|
/// m_weapon and m_character are private on Attack and arrive through Harmony's triple
|
|
/// underscore injection, which is checked at patch time: a rename becomes a startup error
|
|
/// instead of a silent null on every click.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(Attack), "OnAttackTrigger")]
|
|
public static class WarVultureCastPatch
|
|
{
|
|
public static bool Prefix(ItemDrop.ItemData ___m_weapon, Humanoid ___m_character)
|
|
{
|
|
if (!WarVultureItem.IsWarVulture(___m_weapon)) return true;
|
|
|
|
// Still returns false while riding, so vanilla's trigger never runs - but the dismount
|
|
// is NOT taken from here. In the saddle this event is unreliable: the cast clip loses
|
|
// to the sitting pose before its event fires. WarVultureMount reads the button itself.
|
|
WarVultureMount.Toggle(___m_character as Player);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The item costs nothing, guaranteed.
|
|
///
|
|
/// Zeroing the fields on the clone ought to be enough by every reading of the game's own code.
|
|
/// On the Skadi staff the cost came back in game anyway, which means the write to a clone's
|
|
/// SharedData does not survive something - so relying on it alone is relying on a mechanism
|
|
/// already observed to fail. These three getters are the single funnel every cost check flows
|
|
/// through: Attack.Start asks them before it will start, and the tooltip asks them for what to
|
|
/// print. All three are private and take no parameters, so they are addressed by name.
|
|
/// </summary>
|
|
[HarmonyPatch]
|
|
public static class WarVultureFreeCastPatch
|
|
{
|
|
[HarmonyTargetMethods]
|
|
private static System.Collections.Generic.IEnumerable<System.Reflection.MethodBase> TargetMethods()
|
|
{
|
|
yield return AccessTools.Method(typeof(Attack), "GetAttackEitr");
|
|
yield return AccessTools.Method(typeof(Attack), "GetAttackHealth");
|
|
yield return AccessTools.Method(typeof(Attack), "GetAttackStamina");
|
|
}
|
|
|
|
[HarmonyPostfix]
|
|
private static void Postfix(ItemDrop.ItemData ___m_weapon, ref float __result)
|
|
{
|
|
if (__result == 0f) return;
|
|
if (!WarVultureItem.IsWarVultureContext(___m_weapon)) return;
|
|
|
|
__result = 0f;
|
|
}
|
|
}
|
|
}
|