diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Combo.java b/src/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Combo.java index 6fe9638a0..420807386 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Combo.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Combo.java @@ -171,10 +171,7 @@ public class Combo extends Buff implements ActionIndicator.Action { public void onSelect(Integer cell) { if (cell == null) return; final Char enemy = Actor.findChar( cell ); - int userPos = target.pos; - if (enemy == null || userPos == cell || (Dungeon.hero.belongings.weapon != null ? - Level.distance( userPos, enemy.pos ) > Dungeon.hero.belongings.weapon.reachFactor(Dungeon.hero) - : !Level.adjacent( userPos, enemy.pos ))){ + if (enemy == null || !((Hero)target).canAttack(enemy) || target.isCharmedBy( enemy )){ GLog.w( Messages.get(Combo.class, "bad_target") ); } else { target.sprite.attack(cell, new Callback() { diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/actors/hero/Hero.java b/src/com/shatteredpixel/shatteredpixeldungeon/actors/hero/Hero.java index a947aeb4d..dd1d4cd29 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/actors/hero/Hero.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/actors/hero/Hero.java @@ -95,6 +95,7 @@ import com.shatteredpixel.shatteredpixeldungeon.sprites.HeroSprite; import com.shatteredpixel.shatteredpixeldungeon.ui.AttackIndicator; import com.shatteredpixel.shatteredpixeldungeon.ui.BuffIndicator; import com.shatteredpixel.shatteredpixeldungeon.ui.QuickSlotButton; +import com.shatteredpixel.shatteredpixeldungeon.utils.BArray; import com.shatteredpixel.shatteredpixeldungeon.utils.GLog; import com.shatteredpixel.shatteredpixeldungeon.windows.WndMessage; import com.shatteredpixel.shatteredpixeldungeon.windows.WndResurrect; @@ -103,6 +104,7 @@ import com.watabou.noosa.Camera; import com.watabou.noosa.Game; import com.watabou.noosa.audio.Sample; import com.watabou.utils.Bundle; +import com.watabou.utils.PathFinder; import com.watabou.utils.Random; import java.util.ArrayList; @@ -355,6 +357,27 @@ public class Hero extends Char { } } + + public boolean canAttack(Char enemy){ + if (enemy == null || pos == enemy.pos) + return false; + + //can always attack adjacent enemies + if (Level.adjacent(pos, enemy.pos)) + return true; + + KindOfWeapon wep = Dungeon.hero.belongings.weapon; + + if (wep != null && Level.distance( pos, enemy.pos ) <= wep.reachFactor(this)){ + + PathFinder.buildDistanceMap(enemy.pos, BArray.not(Level.solid, null), wep.reachFactor(this)); + + return PathFinder.distance[pos] <= wep.reachFactor(this); + + } else { + return false; + } + } public float attackDelay() { KindOfWeapon wep = rangedWeapon != null ? rangedWeapon : belongings.weapon; @@ -804,11 +827,7 @@ public class Hero extends Char { enemy = action.target; - boolean inRange = belongings.weapon != null ? - Level.distance( pos, enemy.pos ) <= belongings.weapon.reachFactor(this) - : Level.adjacent( pos, enemy.pos ); - - if (inRange && enemy.isAlive() && !isCharmedBy( enemy )) { + if (enemy.isAlive() && canAttack( enemy ) && !isCharmedBy( enemy )) { spend( attackDelay() ); sprite.attack( enemy.pos ); diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/ui/AttackIndicator.java b/src/com/shatteredpixel/shatteredpixeldungeon/ui/AttackIndicator.java index a748c7217..f089b3b9c 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/ui/AttackIndicator.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/ui/AttackIndicator.java @@ -23,7 +23,6 @@ package com.shatteredpixel.shatteredpixeldungeon.ui; import com.shatteredpixel.shatteredpixeldungeon.Dungeon; import com.shatteredpixel.shatteredpixeldungeon.actors.Char; import com.shatteredpixel.shatteredpixeldungeon.actors.mobs.Mob; -import com.shatteredpixel.shatteredpixeldungeon.levels.Level; import com.shatteredpixel.shatteredpixeldungeon.scenes.PixelScene; import com.shatteredpixel.shatteredpixeldungeon.sprites.CharSprite; import com.watabou.noosa.Game; @@ -97,14 +96,11 @@ public class AttackIndicator extends Tag { private void checkEnemies() { - int heroPos = Dungeon.hero.pos; candidates.clear(); int v = Dungeon.hero.visibleEnemies(); for (int i=0; i < v; i++) { Mob mob = Dungeon.hero.visibleEnemy( i ); - if (Dungeon.hero.belongings.weapon != null ? - Level.distance( heroPos, mob.pos ) <= Dungeon.hero.belongings.weapon.reachFactor(Dungeon.hero) - : Level.adjacent( heroPos, mob.pos )) { + if ( Dungeon.hero.canAttack( mob) ) { candidates.add( mob ); } }