Skip to content

Commit

Permalink
Fix Shroomal Ogres, various other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Extremelyd1 committed Mar 30, 2024
1 parent 45246fe commit 73f6266
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 17 deletions.
25 changes: 24 additions & 1 deletion HKMP/Fsm/FsmPatcher.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Hkmp.Logging;
using Hkmp.Util;
using Hkmp.Logging;
using HutongGames.PlayMaker.Actions;

namespace Hkmp.Fsm;
Expand Down Expand Up @@ -36,5 +36,28 @@ private void OnFsmEnable(On.PlayMakerFSM.orig_OnEnable orig, PlayMakerFSM self)
triggerAction.collideTag.Value = "Player";
triggerAction.collideTag.UseVariable = false;
}

// Specific patch for the Battle Control FSM in Fungus2_05 where the Shroomal Ogres are with the Charm Notch
if (self.name.Equals("Battle Scene v2") &&
self.Fsm.Name.Equals("Battle Control") &&
self.gameObject.scene.name.Equals("Fungus2_05")) {
var findBrawler1 = self.GetAction<FindGameObject>("Init", 6);
var findBrawler2 = self.GetAction<FindGameObject>("Init", 8);

// With the way the entity system works, the Mushroom Brawlers might not be found with the existing actions
// We complement these actions by checking if the Brawlers were found and if not, find them another way
self.InsertMethod("Init", 7, () => {
if (findBrawler1.store.Value == null) {
var brawler1 = GameObjectUtil.FindInactiveGameObject("Mushroom Brawler 1");
findBrawler1.store.Value = brawler1;
}
});
self.InsertMethod("Init", 10, () => {
if (findBrawler2.store.Value == null) {
var brawler2 = GameObjectUtil.FindInactiveGameObject("Mushroom Brawler 2");
findBrawler2.store.Value = brawler2;
}
});
}
}
}
28 changes: 18 additions & 10 deletions HKMP/Game/Client/Entity/Action/EntityFsmActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1782,11 +1782,15 @@ private static void ApplyNetworkDataFromAction(EntityNetworkData data, SendEvent
if (action.delay.Value < 1.0 / 1000.0) {
action.Fsm.Event(action.eventTarget, action.sendEvent.Value);
} else {
action.Fsm.DelayedEvent(
action.eventTarget,
FsmEvent.GetFsmEvent(action.sendEvent.Value),
action.delay.Value
);
// We need to delay the event sending ourselves, because the FSM that we are executing in is not enabled
// The usual implementation of SendEventByName will thus not work
MonoBehaviourUtil.Instance.StartCoroutine(DelayEvent());

IEnumerator DelayEvent() {
yield return new WaitForSeconds(action.delay.Value);

action.Fsm.Event(action.eventTarget, action.sendEvent.Value);
}
}
}

Expand All @@ -1806,11 +1810,15 @@ private static void ApplyNetworkDataFromAction(EntityNetworkData data, SendEvent
if (action.delay.Value < 1.0 / 1000.0) {
action.Fsm.Event(action.eventTarget, action.sendEvent.Value);
} else {
action.Fsm.DelayedEvent(
action.eventTarget,
FsmEvent.GetFsmEvent(action.sendEvent.Value),
action.delay.Value
);
// We need to delay the event sending ourselves, because the FSM that we are executing in is not enabled
// The usual implementation of SendEventByNameV2 will thus not work
MonoBehaviourUtil.Instance.StartCoroutine(DelayEvent());

IEnumerator DelayEvent() {
yield return new WaitForSeconds(action.delay.Value);

action.Fsm.Event(action.eventTarget, action.sendEvent.Value);
}
}
}

Expand Down
11 changes: 7 additions & 4 deletions HKMP/Game/Client/Entity/EntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,15 @@ private void FindEntitiesInScene(Scene scene, bool lateLoad) {
var objectsToCheck = Object.FindObjectsOfType<EnemyDeathEffects>()
.Where(e => e.gameObject.scene == scene)
.SelectMany(enemyDeathEffects => {
if (enemyDeathEffects == null) {
try {
enemyDeathEffects.PreInstantiate();
} catch (Exception) {
// If we get an exception it might not be possible to pre-instantiate the enemy death effects
// This can happen when the object uses a PersonalObjectPool, which can't be pre-instantiated
// this early, so we return only the original gameobject
return new[] { enemyDeathEffects.gameObject };
}

enemyDeathEffects.PreInstantiate();

var corpse = ReflectionHelper.GetField<EnemyDeathEffects, GameObject>(
enemyDeathEffects,
"corpse"
Expand Down Expand Up @@ -448,4 +451,4 @@ private void FindEntitiesInScene(Scene scene, bool lateLoad) {
}.Process();
}
}
}
}
1 change: 1 addition & 0 deletions HKMP/Game/Client/Entity/EntityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ internal enum EntityType {
FungifiedHusk,
Shrumeling,
ShrumalWarrior,
ShrumalOgre,
MantisYouth,
MantisWarrior,
MantisThrone,
Expand Down
5 changes: 5 additions & 0 deletions HKMP/Resource/entity-registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@
"type": "ShrumalWarrior",
"fsm_name": "Mush Roller"
},
{
"base_object_name": "Mushroom Brawler",
"type": "ShrumalOgre",
"fsm_name": "Shroom Brawler"
},
{
"base_object_name": "Mantis Flyer Child",
"type": "MantisYouth",
Expand Down
27 changes: 25 additions & 2 deletions HKMP/Util/GameObjectExtensions.cs → HKMP/Util/GameObjectUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
namespace Hkmp.Util;

/// <summary>
/// Class for GameObject extensions.
/// Class for GameObject utility methods and extensions.
/// </summary>
internal static class GameObjectExtensions {
internal static class GameObjectUtil {
/// <summary>
/// Find a GameObject with the given name in the children of the given GameObject.
/// </summary>
Expand All @@ -30,6 +30,11 @@ string name
return null;
}

/// <summary>
/// Get a list of the children of the given GameObject.
/// </summary>
/// <param name="gameObject">The GameObject to get the children for.</param>
/// <returns>A list of the children of the GameObject.</returns>
public static List<GameObject> GetChildren(this GameObject gameObject) {
var children = new List<GameObject>();
for (var i = 0; i < gameObject.transform.childCount; i++) {
Expand All @@ -38,4 +43,22 @@ public static List<GameObject> GetChildren(this GameObject gameObject) {

return children;
}

/// <summary>
/// Find an inactive GameObject with the given name.
/// </summary>
/// <param name="name">The name of the GameObject.</param>
/// <returns>The GameObject is it exists, null otherwise.</returns>
public static GameObject FindInactiveGameObject(string name) {
var transforms = Resources.FindObjectsOfTypeAll<Transform>();
foreach (var transform in transforms) {
if (transform.hideFlags == HideFlags.None) {
if (transform.name == name) {
return transform.gameObject;
}
}
}

return null;
}
}

0 comments on commit 73f6266

Please sign in to comment.