Hi there JGO, it’s me again.
I am working on a small AI script from the villager that has the woodcutter job. But sometimes it is attacking air/ghosts and I guess also targetting them. I don’t know how this is happening. Most of the times it works, some times he is stuck because my A* pathfinding isn’t that good, nor positioning of the Villager.
Screeny: (The grey plate with the sword on it is that he is attacking, because I couldn’t show you the animation in a pic).
http://img23.imageshack.us/img23/8254/qtge.png
(this one’s positioning is really terrible. I guess it is a small variable that is wrong(My cutprogram likes to add a white border :P).
http://img545.imageshack.us/img545/2335/33ch.png
Codes
Fighting:
public void fighting() {
if(x < fightTargetCon.x){
movingDir = 3;
} else {
movingDir = 2;
}
if(fightTargetCon.isDestroyed()){
wonFight = true;
isFighting = false;
} else if(attackSpeedTimer >= attackSpeed){
attackSpeedTimer = 0;
Random hitChance = new Random();
if(hitChance.nextInt(100) <= accuracy-1){
yourHitHit = true;
hitMissed = false;
yourHitIconShowTimer = 0;
Random randomCriticalChance = new Random();
if(randomCriticalChance.nextInt(100) <= criticalChance -1){
attackDamageModifier = 2;
criticalHit = true;
} else {
attackDamageModifier = 1;
criticalHit = false;
}
Random damageChance = new Random();
fightTargetCon.healthChange((minAttackDamage + damageChance.nextInt(maxAttackDamage-minAttackDamage)) * attackDamageModifier, false);
} else {
hitMissed = true;
yourHitHit = false;
yourHitIconShowTimer = 0;
}
} else {
attackSpeedTimer++;
}
}
The AI (searching for the tree, walking to it and then targetting + attacking):
public void working() {
if(job == "VILLAGER"){
} else if(job == "WOODCUTTER"){
if(!wasLooking && !isMovingOnPath && !wasDoingjob){ //searching for trees.
int eX = 0; int eY = 0; int lowestXY = -1;
boolean negativeX = false; boolean negativeY = false;
for (Entity e : level.getEntities()){
if (e instanceof ConstructionTree){
if(x <= e.x && e.x-x <= jobRadius){
eX = e.x-x;
negativeX = true;
} else if(x > e.x && x-e.x <= jobRadius){
negativeX = false;
eX = x-e.x;
}
if(y <= e.y && e.y-y <= jobRadius){
negativeY = false;
eY = e.y-y;
} else if(y > e.y && y-e.y <= jobRadius){
negativeY = true;
eY = y-e.y;
}
if(lowestXY != -1 && lowestXY > eX + eY || lowestXY == -1 && eX + eY != 0){
targetCon = (Construction)e;
lowestXY = eX + eY;
if(!negativeX){
jobX = x-eX + 8;
} else {
jobX = x+eX - 8;
}
if(!negativeY){
jobY = y+eY - 8;
} else {
jobY = y-eY - 8;
}
}
}
}
wasLooking = true;
if(lowestXY != -1){
findPath(new Point(this.x >> 3, this.y >> 3), new Point((jobX >> 3), (jobY) >> 3));
}
} else if(!wasDoingjob && !isMovingOnPath && wasLooking){ //Targetting trees
fightTargetCon = targetCon;
isFighting = true;
if(wonFight == true){
wonFight = false;
isFighting = false;
wasDoingjob = true;
findPath(new Point(this.x >> 3, this.y >> 3), house);
}
} else if(wasDoingjob && wasLooking && !isMovingOnPath){ //Waiting in his home
home.occupied = true;
isInsideBuilding = true;
if(jobTimer > 400){
jobTimer = 0;
home.occupied = false;
isInsideBuilding = false;
wasLooking = false;
wasDoingjob = false;
} else {
jobTimer++;
}
}
}
}
NOTE:
-There is probably nothing wrong with the attackingscript.
-I think it is a small bug/wrongplaced variable as many times it is working.
-Better performance things are always welcome!
-Ask for thing u want to know.
Already thanks.
-RoseSlayer