Index: C:/Documents and Settings/Eric/workspace/L2_GameServer/java/net/sf/l2j/gameserver/model/L2Character.java =================================================================== --- C:/Documents and Settings/Eric/workspace/L2_GameServer/java/net/sf/l2j/gameserver/model/L2Character.java (revision 95) +++ C:/Documents and Settings/Eric/workspace/L2_GameServer/java/net/sf/l2j/gameserver/model/L2Character.java (working copy) @@ -2528,32 +2528,55 @@ L2Character target = (L2Character) getTarget(); int tarheading = target.getHeading(); int heading = getHeading(); - int count = tarheading; - for(int i = 0;i < 3500;i++) + + /* test for being behind the target by determining a heading from the target to attacker, + * then checking that it differs from the targets heading by more than 90 degrees. + * + * test for facing the target by determining a heading from attacker to target then + * checking that it is within 45 degrees of attackers heading + */ + + //calculate a heading from the attacker to the target + int dx = (super.getX() - target.getX()); + int dy = (super.getY() - target.getY()); + double distance = Math.sqrt(dx*dx + dy*dy); + double cos; + double sin; + + sin = dy/distance; + cos = dx/distance; + + int testHeading = (int) (Math.atan2(-sin, -cos) * 10430.378350470452724949566316381); + testHeading += 32768; + + int dH = testHeading - tarheading; + + //test that the heading to attacker is more than 90 degrees from targets heading + if ((dH >= -16384 && dH <= 16384) || dH >= 49152 || dH <= -49152) { - if(count > 65535) - { - count = 1; - } - if(heading == count) - { - return true; - } - count++; + //_log.warning("isBehindTarget's L2 Character is not behind the target."); + return false; } - count = tarheading; - for(int i = 0;i < 3500;i++) + + //calculate a heading from the attacker to the target + dx = target.getX() - super.getX(); + dy = target.getY() - super.getY(); + distance = Math.sqrt(dx*dx + dy*dy); + sin = dy/distance; + cos = dx/distance; + + testHeading = (int) (Math.atan2(-sin, -cos) * 10430.378350470452724949566316381); + testHeading += 32768; + + dH = testHeading - heading; + + // test that attacker is facing the target (within 45 degrees) + if ((dH >= -8192 && dH <= 8192) || dH >= 57344 || dH <= -57344) { - if(count < 1) - { - count = 65535; - } - if(heading == count) - { - return true; - } - count--; + return true; } + //_log.warning("isBehindTarget's L2 Character is not facing the target."); + return false; } else {