From f95f20fa82126b9d25c20c65022c37d8f6d3eadd Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Mon, 20 Oct 2025 13:11:02 -0400 Subject: [PATCH] v3.3.0: moved handing unreachable target in hunting to its own method --- .../actors/mobs/DM200.java | 14 +---- .../actors/mobs/Golem.java | 14 +---- .../actors/mobs/Guard.java | 2 - .../actors/mobs/Mob.java | 52 ++++++++++--------- .../actors/mobs/Tengu.java | 46 ++++++++-------- 5 files changed, 54 insertions(+), 74 deletions(-) diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/DM200.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/DM200.java index e99a80fd5..3f42a217f 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/DM200.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/DM200.java @@ -178,19 +178,7 @@ public class DM200 extends Mob { } else { //attempt to swap targets if the current one can't be reached or vented at - if (!recursing) { - Char oldEnemy = enemy; - enemy = null; - enemy = chooseEnemy(); - if (enemy != null && enemy != oldEnemy) { - recursing = true; - boolean result = act(enemyInFOV, justAlerted); - recursing = false; - return result; - } - } - spend( TICK ); - return true; + return handleUnreachableTarget(enemyInFOV, justAlerted); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Golem.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Golem.java index b6ac78436..dfa5958f7 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Golem.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Golem.java @@ -247,19 +247,7 @@ public class Golem extends Mob { } else { //attempt to swap targets if the current one can't be reached or teleported - if (!recursing) { - Char oldEnemy = enemy; - enemy = null; - enemy = chooseEnemy(); - if (enemy != null && enemy != oldEnemy) { - recursing = true; - boolean result = act(enemyInFOV, justAlerted); - recursing = false; - return result; - } - } - spend( TICK ); - return true; + return handleUnreachableTarget(enemyInFOV, justAlerted); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Guard.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Guard.java index 7588000ad..69703a0ea 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Guard.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Guard.java @@ -181,8 +181,6 @@ public class Guard extends Mob { && !isCharmedBy( enemy ) && !canAttack( enemy ) && Dungeon.level.distance( pos, enemy.pos ) < 5 - - && chain(enemy.pos)){ return !(sprite.visible || enemy.sprite.visible); } else { diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Mob.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Mob.java index 4e71da10d..49c898aee 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Mob.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Mob.java @@ -1214,9 +1214,6 @@ public abstract class Mob extends Char { public static final String TAG = "HUNTING"; - //prevents rare infinite loop cases - protected boolean recursing = false; - @Override public boolean act( boolean enemyInFOV, boolean justAlerted ) { enemySeen = enemyInFOV; @@ -1252,27 +1249,7 @@ public abstract class Mob extends Char { } else { - //if moving towards an enemy isn't possible, try to switch targets to another enemy that is closer - //unless we have already done that and still can't move toward them, then move on. - if (!recursing) { - Char oldEnemy = enemy; - enemy = null; - enemy = chooseEnemy(); - if (enemy != null && enemy != oldEnemy) { - recursing = true; - boolean result = act(enemyInFOV, justAlerted); - recursing = false; - return result; - } - } - - spend( TICK ); - if (!enemyInFOV) { - sprite.showLost(); - state = WANDERING; - target = ((Mob.Wandering)WANDERING).randomDestination(); - } - return true; + return handleUnreachableTarget(enemyInFOV, justAlerted); } } } @@ -1293,6 +1270,33 @@ public abstract class Mob extends Char { } return swapped; } + + //prevents rare infinite loop cases + protected boolean recursing = false; + + //Try to switch targets to another enemy that is closer or reachable + //unless we have already done that and still can't move toward them, then move on. + protected boolean handleUnreachableTarget(boolean enemyInFOV, boolean justAlerted){ + if (!recursing) { + Char oldEnemy = enemy; + enemy = null; + enemy = chooseEnemy(); + if (enemy != null && enemy != oldEnemy) { + recursing = true; + boolean result = act(enemyInFOV, justAlerted); + recursing = false; + return result; + } + } + + spend( TICK ); + if (!enemyInFOV) { + sprite.showLost(); + state = WANDERING; + target = ((Mob.Wandering)WANDERING).randomDestination(); + } + return true; + } } protected class Fleeing implements AiState { diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Tengu.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Tengu.java index e8e74f892..1b4d8bcd1 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Tengu.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Tengu.java @@ -400,30 +400,32 @@ public class Tengu extends Mob { } else { - //Try to switch targets to another enemy that is closer - //unless we have already done that and still can't attack them, then move on. - if (!recursing) { - Char oldEnemy = enemy; - enemy = null; - enemy = chooseEnemy(); - if (enemy != null && enemy != oldEnemy) { - recursing = true; - boolean result = act(enemyInFOV, justAlerted); - recursing = false; - return result; - } - } - - //attempt to use an ability, even if enemy can't be decided - if (canUseAbility()){ - return useAbility(); - } - - spend( TICK ); - return true; - + return handleUnreachableTarget(enemyInFOV, justAlerted); } } + + @Override + protected boolean handleUnreachableTarget(boolean enemyInFOV, boolean justAlerted) { + Char oldEnemy = enemy; + enemy = null; + enemy = chooseEnemy(); + if (enemy != null && enemy != oldEnemy) { + recursing = true; + boolean result = act(enemyInFOV, justAlerted); + recursing = false; + return result; + } + + //attempt to use an ability, even if enemy can't be decided + //Tengu is always hunting, so we don't lose enemy in this case + if (canUseAbility()){ + return useAbility(); + } + + spend( TICK ); + return true; + + } } //*****************************************************************************************