worldbox-observer-mod/IdleSpectator/Events/Patches/BuildingEventPatches.cs

135 lines
3.8 KiB
C#

using ai.behaviours;
using HarmonyLib;
using UnityEngine;
namespace IdleSpectator;
/// <summary>Building damage/destroy/eat interest patches from mutation discovery.</summary>
[HarmonyPatch]
public static class BuildingEventPatches
{
[HarmonyPatch(typeof(BehDealDamageToTargetBuilding), nameof(BehDealDamageToTargetBuilding.execute))]
[HarmonyPostfix]
public static void PostfixDamageBuilding(Actor pActor, BehResult __result)
{
// Routine siege chips are Layer B noise. Camera waits for consume/destroy.
_ = pActor;
_ = __result;
}
[HarmonyPatch(typeof(BehConsumeTargetBuilding), nameof(BehConsumeTargetBuilding.execute))]
[HarmonyPostfix]
public static void PostfixConsumeBuilding(Actor pActor, BehResult __result)
{
if (pActor == null || !pActor.isAlive() || __result == BehResult.Stop)
{
return;
}
Building food = null;
try
{
food = pActor.beh_building_target;
}
catch
{
food = null;
}
string label = EventReason.BuildingEat(pActor, food);
Emit("building_consume", 78f, pActor, label);
}
[HarmonyPatch(typeof(Building), "startDestroyBuilding")]
[HarmonyPostfix]
public static void PostfixStartDestroy(Building __instance)
{
if (__instance == null || AgentHarness.Busy)
{
return;
}
try
{
Vector3 pos = Vector3.zero;
object posObj = __instance.GetType().GetField("current_position")?.GetValue(__instance)
?? __instance.GetType().GetProperty("current_position")?.GetValue(__instance, null);
if (posObj is Vector3 v)
{
pos = v;
}
string buildingName = "";
try
{
if (__instance.asset != null)
{
buildingName = __instance.asset.id ?? "";
}
}
catch
{
buildingName = "";
}
Actor near = pos != Vector3.zero
? EventFeedUtil.NearestUnit(pos, 48f)
: null;
string label = EventReason.BuildingFalls(buildingName, near);
if (near == null)
{
if (pos == Vector3.zero)
{
return;
}
string locKey = "building:destroy:" + buildingName + ":loc";
EventFeedUtil.Register(
locKey,
"building",
"Spectacle",
label,
string.IsNullOrEmpty(buildingName) ? "building_destroy" : buildingName,
88f,
pos,
follow: null,
locationOnly: true);
return;
}
string key = "building:destroy:" + buildingName + ":" + EventFeedUtil.SafeId(near);
EventFeedUtil.Register(
key,
"building",
"Spectacle",
label,
string.IsNullOrEmpty(buildingName) ? "building_destroy" : buildingName,
88f,
near.current_position,
near);
}
catch
{
// ignore API mismatches
}
}
private static void Emit(string eventId, float strength, Actor actor, string label)
{
if (AgentHarness.Busy)
{
return;
}
string key = "building:" + eventId + ":" + EventFeedUtil.SafeId(actor);
EventFeedUtil.Register(
key,
"building",
"Spectacle",
label,
eventId,
strength,
actor.current_position,
actor);
}
}