I am making a tower defense like game and have gotten to a point were i know crash when making the tower. Ive been testing the tracker out with the mouse cursor, so the towers had to follow that, (Which worked out greatly, thanks to some good people at psp-programming for their help)
but now ive added enemy AI, and so i need to have the towers follow them, now I've made what i thought were the necessary adjustments, but now when i make a tower, it crashes, I've added some benchmarks in the code, now, if I don't make an enemy appear on the screen, or have some on the screen with some left over it crashes Right After the "Bench 5a", However if i have all of your enemy's depleted (and/or out of range?), it Crashes after, "Bench 6", I think it either has a problem with tempDist/curDist or
Code: Select all
s_gunner[i].range
here is the function/cause of my pain,
Code: Select all
void attackNearestEnemy(int i)
{
int e;
int enemyIndex; // index to the enemy with the smallest distance
float curDist, tempDist; // smallest distance; temporary distance to compare to curDist
if(tower_guntop1->angle > 359) {
tower_guntop1->angle = 0;
}
// loop through all the enemies
for(e=0;e<MAX_ENEMIES-1;e++) { //is only one because im testing it with the cursor, (which there is only one of)
//got rid of sqrtf for speed as it is not needed unless you need to know the exact value
//but since we are just checking if its in range we dont need it
oslDebug("bench 1");
float tempDist = (((s_gunner[i].x+Map.offSet_x) - enemy[e].x) * ((s_gunner[i].x+Map.offSet_x) - enemy[e].x)) + (((s_gunner[i].y+Map.offSet_y) - enemy[e].y) * ((s_gunner[i].y+Map.offSet_y) - enemy[e].y));
//square the check as multiplying is wayy faster than sqrtf
oslDebug("bench 2");
if(tempDist < (s_gunner[i].range*s_gunner[i].range)) {
oslDebug("bench 3");
// if this is the first time through, equalize the distances so that they can compare
oslDebug("enemy 'e' = %d",e);
if(e = 0) {
oslDebug("bench 4");
enemyIndex = 0;
curDist = tempDist;
} else {
oslDebug("bench 5a");
// if the new distance is less than the current smallest distance
if(tempDist < curDist) {
oslDebug("bench 5b");
// the new distance becomes the current smallest distance
curDist = tempDist;
oslDebug("bench 5c");
// record the enemy's index number
enemyIndex = e;
oslDebug("bench 5d");
}
oslDebug("bench 5e");
oslDebug("enemy = %d",enemyIndex);
}
oslDebug("bench 5f");
}
oslDebug("bench 5g");
}//end of for loop
// get the direction the enemy is from the tower (it could be enemy - tower too and you'll get the same result)
oslDebug("bench 6");
if(curDist < (s_gunner[i].range*s_gunner[i].range)) {
oslDebug("bench 7");
s_gunner[i].angle = ((atan2f((s_gunner[i].x+Map.offSet_x) - enemy[enemyIndex].x, -((s_gunner[i].y+Map.offSet_y) - enemy[enemyIndex].y)) + GU_PI) * 180)/GU_PI;
//s_gunner[i].angle/ 57.1139;//converts Radians to degrees
}else{
oslDebug("bench 8");
// your enemy is done for...
//attack(rot); // attack here
s_gunner[i].angle += 1;
}
oslDebug("bench 9");
tower_guntop1->angle = s_gunner[i].angle;
};
Help would be very much appreciated, thank you for your time!