A log carries only vanilla's HoverText, which is not an Interactable, so the game's Use on a log did nothing. Grab Log was read off the keyboard alone (and the radial menu), and a gamepad had no button for it. A prefix on Player.Interact grabs the log for a True Form Trol at tier 7+, the way the Aswid's Dominate rides the same button. The Grab Log key still works on top of it, with a short debounce so the two readers of an E press grab once. The hover prompt now shows the Use key, which resolves to the pad's own button.
181 lines
8.5 KiB
C#
181 lines
8.5 KiB
C#
using HarmonyLib;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Jotunn.Managers;
|
|
using BepInEx;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
public static class TrolLogWeapon
|
|
{
|
|
// Two readers can ask for the same press: the game's own Use (Trol_LogGrabInteract_Patch)
|
|
// and the Grab Log key, which is E by default like Use. The second one in the same press
|
|
// would only say the troll already holds a log.
|
|
private const float GrabDebounce = 0.5f;
|
|
private static float lastGrabTime = -100f;
|
|
|
|
public static void GrabLog(Humanoid user, TreeLog logTarget)
|
|
{
|
|
if (user == null || logTarget == null) return;
|
|
if (!TrolRace.IsTrol()) return;
|
|
if (BossProgressionManager.GetCurrentProgressionState() < 7) return;
|
|
if (Time.time - lastGrabTime < GrabDebounce) return;
|
|
|
|
var seman = user.GetSEMan();
|
|
if (seman == null) return;
|
|
|
|
if (!seman.HaveStatusEffect("SE_TrolTrueForm".GetStableHashCode()))
|
|
{
|
|
user.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_trol_must_true_form_lift_this"));
|
|
return;
|
|
}
|
|
|
|
string logName = Utils.GetPrefabName(logTarget.gameObject);
|
|
if (!logName.EndsWith("_half"))
|
|
{
|
|
logName += "_half";
|
|
}
|
|
string weaponName = "TrolWeapon_" + logName;
|
|
|
|
GameObject weaponPrefab = PrefabManager.Cache.GetPrefab<GameObject>(weaponName);
|
|
if (weaponPrefab == null)
|
|
{
|
|
weaponName = "TrolWeapon_beech_log_half"; // Fallback
|
|
weaponPrefab = PrefabManager.Cache.GetPrefab<GameObject>(weaponName);
|
|
}
|
|
|
|
if (weaponPrefab == null)
|
|
{
|
|
Jotunn.Logger.LogWarning("TrolWeapon prefab not found: " + weaponName);
|
|
return;
|
|
}
|
|
|
|
// Check if already holding a weapon
|
|
ItemDrop.ItemData rightItem = HarmonyLib.Traverse.Create(user).Field<ItemDrop.ItemData>("m_rightItem").Value;
|
|
if (rightItem != null && rightItem.m_dropPrefab != null && rightItem.m_dropPrefab.name.StartsWith("TrolWeapon_"))
|
|
{
|
|
user.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_trol_are_already_holding_log"));
|
|
return;
|
|
}
|
|
|
|
// Play pickup animation
|
|
var zanim = HarmonyLib.Traverse.Create(user).Field("m_zanim").GetValue<ZSyncAnimation>();
|
|
if (zanim != null)
|
|
{
|
|
zanim.SetTrigger("interact");
|
|
}
|
|
|
|
// Unequip current items for safety
|
|
ItemDrop.ItemData currentRight = HarmonyLib.Traverse.Create(user).Field<ItemDrop.ItemData>("m_rightItem").Value;
|
|
if (currentRight != null) user.UnequipItem(currentRight);
|
|
ItemDrop.ItemData currentLeft = HarmonyLib.Traverse.Create(user).Field<ItemDrop.ItemData>("m_leftItem").Value;
|
|
if (currentLeft != null) user.UnequipItem(currentLeft);
|
|
|
|
// A grid position out of the visible grid's bounds ((99, 99), same idea as a "hidden"
|
|
// slot) used to be how this stayed out of the way - but InventoryGrid.UpdateGui maps
|
|
// every single item in the inventory to a grid cell with y * width + x and no bounds
|
|
// check at all, for every player who ever ends up holding this, equipped or not. A
|
|
// position that far out throws ArgumentOutOfRangeException every frame the inventory
|
|
// screen is open, which both floods the log (visible as a stutter/freeze) and aborts
|
|
// the render loop partway through, corrupting every other slot's icon until the item
|
|
// is gone. FindEmptySlot is what every other item picked up in Valheim actually uses -
|
|
// it shows up in the grid like any other equipped weapon instead, which is correct.
|
|
ItemDrop wepDrop = weaponPrefab.GetComponent<ItemDrop>();
|
|
ItemDrop.ItemData newItem = wepDrop.m_itemData.Clone();
|
|
newItem.m_dropPrefab = weaponPrefab;
|
|
|
|
Vector2i gridPos = HarmonyLib.Traverse.Create(user.GetInventory()).Method("FindEmptySlot", false).GetValue<Vector2i>();
|
|
if (gridPos.x == -1)
|
|
{
|
|
user.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_trol_no_room_lift"));
|
|
return;
|
|
}
|
|
newItem.m_gridPos = gridPos;
|
|
newItem.m_durability = newItem.GetMaxDurability();
|
|
|
|
var m_inventory = HarmonyLib.Traverse.Create(user.GetInventory()).Field<List<ItemDrop.ItemData>>("m_inventory").Value;
|
|
if (m_inventory != null)
|
|
{
|
|
m_inventory.Add(newItem);
|
|
HarmonyLib.Traverse.Create(user.GetInventory()).Method("Changed").GetValue();
|
|
}
|
|
|
|
// Equip it
|
|
user.EquipItem(newItem);
|
|
|
|
// Destroy the log in the world
|
|
ZNetView nview = logTarget.GetComponent<ZNetView>();
|
|
if (nview != null && nview.IsValid())
|
|
{
|
|
nview.ClaimOwnership();
|
|
nview.Destroy();
|
|
}
|
|
else
|
|
{
|
|
UnityEngine.Object.Destroy(logTarget.gameObject);
|
|
}
|
|
|
|
lastGrabTime = Time.time;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// A fallen log already shows a hover prompt in vanilla - its own name, via a HoverText
|
|
/// component vanilla itself attaches (confirmed live: GetComponentInParent<Hoverable>()
|
|
/// on a log resolves to HoverText, not TreeLog itself, which only implements IDestructible).
|
|
/// A first attempt added a second, competing Hoverable component to the same GameObject and
|
|
/// it was never reached - Unity's GetComponentInParent<T>() returns only the first
|
|
/// match, and vanilla's own HoverText was already there first. The fix is to append to
|
|
/// HoverText's own result instead of trying to out-compete it.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(HoverText), "GetHoverText")]
|
|
public static class Trol_LogHoverText_Patch
|
|
{
|
|
public static void Postfix(HoverText __instance, ref string __result)
|
|
{
|
|
if (Player.m_localPlayer == null || !TrolRace.IsTrol()) return;
|
|
if (BossProgressionManager.GetCurrentProgressionState() < 7) return;
|
|
if (!Player.m_localPlayer.GetSEMan().HaveStatusEffect("SE_TrolTrueForm".GetStableHashCode())) return;
|
|
|
|
TreeLog log = ((Component)__instance).GetComponentInParent<TreeLog>();
|
|
if (log == null) return;
|
|
|
|
// Already holding a log - grabbing again is a no-op in TrolLogWeapon.GrabLog, so no
|
|
// prompt either.
|
|
ItemDrop.ItemData rightItem = HarmonyLib.Traverse.Create(Player.m_localPlayer).Field<ItemDrop.ItemData>("m_rightItem").Value;
|
|
if (rightItem != null && rightItem.m_dropPrefab != null && rightItem.m_dropPrefab.name.StartsWith("TrolWeapon_")) return;
|
|
|
|
// The game's Use key, which ModText resolves to the pad's own button on a gamepad. The
|
|
// Grab Log key still works, but only Use reaches every device.
|
|
__result += ModText.Get("rov_hover_trol_grab_log");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Grab Log on the game's own Use, keyboard and gamepad alike, the way the Aswid's Dominate
|
|
/// works. Dominate is an Interactable, so vanilla's Player.Interact finds it; a log carries
|
|
/// only HoverText, which is not one, so Use on a log did nothing and the Grab Log key (read
|
|
/// off the keyboard) was the only way in. A pad had none.
|
|
///
|
|
/// Taken over only for a True Form Trol at tier 7+, who is the one shown the prompt; anyone
|
|
/// else's Use on a log goes on to vanilla, which does nothing with it.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(Player), "Interact")]
|
|
public static class Trol_LogGrabInteract_Patch
|
|
{
|
|
public static bool Prefix(Player __instance, GameObject go, bool hold)
|
|
{
|
|
if (__instance != Player.m_localPlayer || go == null || !TrolRace.IsTrol()) return true;
|
|
if (BossProgressionManager.GetCurrentProgressionState() < 7) return true;
|
|
if (!__instance.GetSEMan().HaveStatusEffect("SE_TrolTrueForm".GetStableHashCode())) return true;
|
|
if (__instance.InAttack() || __instance.InDodge()) return true;
|
|
|
|
TreeLog log = go.GetComponentInParent<TreeLog>();
|
|
if (log == null) return true;
|
|
|
|
// Holding Use repeats the call every frame: one press, one grab.
|
|
if (!hold) TrolLogWeapon.GrabLog(__instance, log);
|
|
return false;
|
|
}
|
|
}
|
|
}
|