Files
MxValheim/MxValheim/EventSystem/Patch.cs
2026-02-15 03:06:48 -05:00

162 lines
6.1 KiB
C#

using HarmonyLib;
using Splatform;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using UnityEngine;
using UnityEngine.UI;
using static MxValheimMod;
using Canvas = UnityEngine.Canvas;
using Image = UnityEngine.UI.Image;
namespace MxValheim.EventSystem
{
internal class EventSystem_Patch
{
// --- 3. HARMONY PATCHES ---
[HarmonyPatch(typeof(Player), nameof(Player.Awake))]
public static class PlayerAwakePatch
{
static void Postfix(Player __instance)
{
// Only trigger for the actual human player, not NPCs/Dummies
if (__instance == Player.m_localPlayer)
{
string pid = GetUserPlayerID(Player.m_localPlayer).ToString();
ZLog.Log($"[ProfileSystem] Spawning finished. Requesting profile for {pid}");
ZRoutedRpc.instance.InvokeRoutedRPC(0, "RPC_RequestProfile", pid);
}
}
}
[HarmonyPatch(typeof(RandEventSystem), nameof(RandEventSystem.FixedUpdate))]
public static class DebugRaidFrequency
{
static void Prefix(RandEventSystem __instance)
{
// Use a small timer check so we don't spam the logic every single frame
// though for debugging, forcing these values is fine.
__instance.m_eventIntervalMin = 10f;
__instance.m_eventChance = 100f;
}
}
[HarmonyPatch(typeof(RandEventSystem), nameof(RandEventSystem.FixedUpdate))]
public static class TimerFreezePatch
{
static void Prefix(RandEventSystem __instance)
{
}
}
[HarmonyPatch(typeof(Hud), nameof(Hud.Update))]
public static class Hud_MonitorPatch
{
private static RandomEvent _lastKnownEvent = null;
static void Postfix()
{
// 1. Safety check: Ensure the system exists
if (RandEventSystem.instance == null) return;
RandomEvent activeEvent = RandEventSystem.instance.GetActiveEvent();
// 2. Logic: Detection
if (activeEvent != null && _lastKnownEvent == null)
{
Debug.Log($"MxEvent: HUD Heartbeat detected Raid Start: {activeEvent.m_name}");
_lastKnownEvent = activeEvent;
// Force HUD Creation and Display
//UpdateWave(true);
}
else if (activeEvent == null && _lastKnownEvent != null)
{
Debug.Log("MxEvent: HUD Heartbeat detected Raid End.");
string pid = GetUserPlayerID(Player.m_localPlayer).ToString();
Heightmap.Biome currentBiome = EnvMan.instance.GetCurrentBiome();
Reward.GiveItemStatic("Coins",Reward.CalculateRewardCoin(Profiles.playerLevel, currentBiome));
System.Random rand = new System.Random();
Reward.GiveItemStatic("Feathers", rand.Next(1,10));
if (rand.Next(100) < 50)
{
Reward.GiveItemRandom(Reward.GetRewardPrefab(Profiles.playerLevel, "food"), 1);
}
if (rand.Next(100) < 25)
{
Reward.GiveItemRandom(Reward.GetRewardPrefab(Profiles.playerLevel, "heal"), 1);
}
if (rand.Next(100) < 10)
{
Reward.GiveItemRandom(Reward.GetRewardPrefab(Profiles.playerLevel, "metal"), 1);
}
_lastKnownEvent = null;
}
}
}
[HarmonyPatch(typeof(Character), nameof(Character.ApplyDamage))]
public static class DeathNotifier
{
static void Postfix(Character __instance)
{
// Check if the character is dead or just died
if (__instance.GetHealth() <= 0f)
{
ZNetView nview = __instance.GetComponent<ZNetView>();
// This check should now pass because ApplyDamage happens before the object is invalidated
if (nview == null || !nview.IsValid() || !nview.IsOwner()) return;
// 2. Check if a raid is active
RandomEvent activeEvent = RandEventSystem.instance.GetActiveEvent();
if (activeEvent == null) return;
Debug.Log("MxEvent: Confirmed Active Event!");
Vector3 playerPos = Player.m_localPlayer.transform.position;
string pid = GetUserPlayerID(Player.m_localPlayer).ToString();
Debug.Log($"EventSystem:ApplyDamage User {pid}");
float distance = Vector3.Distance(playerPos, activeEvent.m_pos);
Debug.Log($"MxEvent: Distance: {distance}");
if (distance <= 96f)
{
int level = __instance.GetLevel();
int exp =
(level == 1) ? 2:
(level == 2) ? 3:
(level == 3) ? 4:
5;
Experience.AddExperience(pid, exp);
/* Send the update
ZRoutedRpc.instance.InvokeRoutedRPC(
ZRoutedRpc.Everybody,
"RPC_UpdateRaidHUD",
$"PHASE: {CurrentPhase}",
KillsInCurrentPhase,
GetTargetForPhase(CurrentPhase)
);*/
}
}
}
}
// Hide HUD when raid ends
[HarmonyPatch(typeof(RandEventSystem), nameof(RandEventSystem.ResetRandomEvent))]
[HarmonyPostfix]
static void OnRaidEnd()
{
}
}
}