Trying to learn more about pathing - having some issues with attacking while running
I'm trying to figure out a pathing issue I've been having. For some reason when P1 (Attacker) is attacking P2 (Target) and P2 is running P1 just follows and isn't able to get an attack off. I'm not exactly sure why but I believe I've found the start of the problem. I had this issue 6 months ago and had another source and started copy and pasting an entire new pathing system, but that lead to way too many eek and t2 errors. This time I want to figure it out properly and see if I can solve the problem instead of replacing it.
Any guidance would be appreciated, I've tried to be pretty thorough in hopefully providing the correct code to look at.
Thanks,
Crazz
Can Attack method:
Code:
public static boolean canReach(Character attacker, CombatMethod method, Character target) {
if (attacker.isPlayer() && target.isPlayer()) {
System.out.println("Can reach is called" + attacker.getAsPlayer().getUsername() + " target: " + target.getAsPlayer().getUsername());
}
if (!validTarget(attacker, target)) {
return false;
}
// Walk back if npc is too far away from spawn position.
if (attacker.isNpc()) {
NPC npc = attacker.getAsNpc();
if (npc.getDefinition().doesRetreat()) {
if (npc.getMovementCoordinator().getCoordinateState() == CoordinateState.RETREATING) {
npc.getCombat().reset();
return false;
}
if (npc.getPosition().getDistance(npc.getSpawnPosition()) >= npc.getDefinition()
.getCombatFollowDistance()) {
npc.getCombat().reset();
npc.getMovementCoordinator().setCoordinateState(CoordinateState.RETREATING);
return false;
}
}
}
// Check distance
Position attackerPos = attacker.getPosition();
Position targetPos = target.getPosition();
System.out.println("Distance || attacker position : " + attacker + " Attacker speed: " + target.getBaseAttackSpeed() + " target: " + targetPos + " targ speed: " + target.getBaseAttackSpeed());
boolean inDistance = false;
int requiredDistance = method.getAttackDistance(attacker);
System.out.println("Method attacker speed: " + method.getAttackSpeed(attacker) + " Method: Attacker Distance: " + method.getAttackDistance(attacker));
Boundary targetBoundary = target.getBoundary();
System.out.println("Target Boundary B1(" + target.getBoundary().getX() + ", " + target.getBoundary().getY() + ") B2(" + target.getBoundary().getX2() + ", " + target.getBoundary().getY2()+ ")");
// Check if we're inside of the target
if (targetBoundary.inside(attackerPos)) {
System.out.println("Inside Boundary: " + targetBoundary.inside(attackerPos));
return false;
}
// Get target's occupied tiles
List<Position> tilesOccupied = new ArrayList<Position>();
for (int bX = target.getPosition().getX(); bX <= targetBoundary.getX2(); bX++) {
for (int bY = targetBoundary.getY(); bY <= targetBoundary.getY2(); bY++) {
tilesOccupied.add(new Position(bX, bY, targetPos.getZ()));
}
}
// Make sure we're in distance with at least one of
// the occupied tiles
for (Position pos : tilesOccupied) {
if (attackerPos.getDistance(pos) <= requiredDistance) {
inDistance = true;
break;
}
}
if (!inDistance) {
return false;
}
// Don't allow diagonal attacks for smaller entities
if (method.getCombatType() == CombatType.MELEE && attacker.getSize() == 1 && target.getSize() == 1) {
if (RS317PathFinder.isInDiagonalBlock(attackerPos, targetPos)) {
return false;
}
}
// Make sure we the path is clear for projectiles
if (!RegionManager.canProjectileAttack(attacker, target)) {
return false;
}
return true;
}
Results from Print Statements:
Code:
Can reach is called attacker: Crazzmc target: Crazz
Distance || attacker position : Position values: [x, y, z] - [3143, 3652, 0]. Attacker speed: 4 target: Position values: [x, y, z] - [3143, 3653, 0]. targ speed: 4
Method attacker speed: 4 Method: Attacker Distance: 1
Target Boundary B1(3143, 3653) B2(3143, 3653)
Can reach is called attacker: Crazzmc target: Crazz
Distance || attacker position : Position values: [x, y, z] - [3143, 3652, 0]. Attacker speed: 4 target: Position values: [x, y, z] - [3142, 3655, 0]. targ speed: 4
Method attacker speed: 4 Method: Attacker Distance: 1
Target Boundary B1(3142, 3655) B2(3142, 3655)
EDIT: It's a one way street, the initial attacker (P1) will always have this issue, but when the target P2 starts attacking P1, P2 runs adn attacks P1 the entire way. I am not sure why this is the case. I even got rid of all the variables and it still happens. Any ideas what could cause this? I have a feeling like it may be here:
Code:
// Get target's occupied tiles
List<Position> tilesOccupied = new ArrayList<Position>();
for (int bX = target.getPosition().getX(); bX <= targetBoundary.getX2(); bX++) {
for (int bY = targetBoundary.getY(); bY <= targetBoundary.getY2(); bY++) {
tilesOccupied.add(new Position(bX, bY, targetPos.getZ()));
}
}