Code:
public void followPlayer(int i, int playerId) {
if (Server.playerHandler.players[playerId] == null) {
return;
}
if (Server.playerHandler.players[playerId].respawnTimer > 0) {
npcs[i].facePlayer(0);
npcs[i].randomWalk = true;
npcs[i].underAttack = false;
return;
}
int playerX = Server.playerHandler.players[playerId].absX;
int playerY = Server.playerHandler.players[playerId].absY;
npcs[i].randomWalk = false;
boolean canwalk = true;
if((npcs[i].spawnedBy > 0) || ((npcs[i].absX < npcs[i].makeX + Config.NPC_FOLLOW_DISTANCE) && (npcs[i].absX > npcs[i].makeX - Config.NPC_FOLLOW_DISTANCE) && (npcs[i].absY < npcs[i].makeY + Config.NPC_FOLLOW_DISTANCE) && (npcs[i].absY > npcs[i].makeY - Config.NPC_FOLLOW_DISTANCE))) {
if(npcs[i].heightLevel == Server.playerHandler.players[playerId].heightLevel) {
if(Server.playerHandler.players[playerId] != null && npcs[i] != null) {
if(playerY < npcs[i].absY) {
npcs[i].moveX = GetMove(npcs[i].absX, playerX);
npcs[i].moveY = GetMove(npcs[i].absY, playerY+1);
if(WorldMap.isWalkAble(npcs[i].heightLevel, npcs[i].absX, npcs[i].absY, npcs[i].absX-npcs[i].moveX, npcs[i].absY) == false)
canwalk = false;
} else if(playerY > npcs[i].absY) {
npcs[i].moveX = GetMove(npcs[i].absX, playerX);
npcs[i].moveY = GetMove(npcs[i].absY, playerY-1);
if(WorldMap.isWalkAble(npcs[i].heightLevel, npcs[i].absX, npcs[i].absY, npcs[i].absX-npcs[i].moveX, npcs[i].absY) == false)
canwalk = false;
} else if(playerX < npcs[i].absX) {
npcs[i].moveX = GetMove(npcs[i].absX, playerX+1);
npcs[i].moveY = GetMove(npcs[i].absY, playerY);
if(WorldMap.isWalkAble(npcs[i].heightLevel, npcs[i].absX, npcs[i].absY, npcs[i].absX-npcs[i].moveX, npcs[i].absY) == false)
canwalk = false;
} else if(playerX > npcs[i].absX) {
npcs[i].moveX = GetMove(npcs[i].absX, playerX-1);
npcs[i].moveY = GetMove(npcs[i].absY, playerY);
if(WorldMap.isWalkAble(npcs[i].heightLevel, npcs[i].absX, npcs[i].absY, npcs[i].absX-npcs[i].moveX, npcs[i].absY) == false)
canwalk = false;
} else if(playerX == npcs[i].absX || playerY == npcs[i].absY) {
int o = Misc.random(3);
switch(o) {
case 0:
npcs[i].moveX = GetMove(npcs[i].absX, playerX);
npcs[i].moveY = GetMove(npcs[i].absY, playerY+1);
if(WorldMap.isWalkAble(npcs[i].heightLevel, npcs[i].absX, npcs[i].absY, npcs[i].absX-npcs[i].moveX, npcs[i].absY) == false)
canwalk = false;
break;
case 1:
npcs[i].moveX = GetMove(npcs[i].absX, playerX);
npcs[i].moveY = GetMove(npcs[i].absY, playerY-1);
if(WorldMap.isWalkAble(npcs[i].heightLevel, npcs[i].absX, npcs[i].absY, npcs[i].absX-npcs[i].moveX, npcs[i].absY) == false)
canwalk = false;
break;
case 2:
npcs[i].moveX = GetMove(npcs[i].absX, playerX+1);
npcs[i].moveY = GetMove(npcs[i].absY, playerY);
if(WorldMap.isWalkAble(npcs[i].heightLevel, npcs[i].absX, npcs[i].absY, npcs[i].absX-npcs[i].moveX, npcs[i].absY) == false)
canwalk = false;
break;
case 3:
npcs[i].moveX = GetMove(npcs[i].absX, playerX-1);
npcs[i].moveY = GetMove(npcs[i].absY, playerY);
if(WorldMap.isWalkAble(npcs[i].heightLevel, npcs[i].absX, npcs[i].absY, npcs[i].absX-npcs[i].moveX, npcs[i].absY) == false)
canwalk = false;
break;
}
}
if(WorldMap.isWalkAble(npcs[i].heightLevel, npcs[i].absX, npcs[i].absY, npcs[i].absX-npcs[i].moveX, npcs[i].absY) == false)
canwalk = false;
if(canwalk == true)
npcs[i].getNextNPCMovement(i);
npcs[i].updateRequired = true;
}
}
if(WorldMap.isWalkAble(npcs[i].heightLevel, npcs[i].absX, npcs[i].absY, npcs[i].absX-npcs[i].moveX, npcs[i].absY) == false)
canwalk = false;
} else {
if(canwalk == true)
npcs[i].facePlayer(0);
npcs[i].randomWalk = true;
npcs[i].underAttack = false;
}
}