 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
BlackShark
Joined: 02 Mar 2007 Posts: 11
|
Posted: Wed Apr 09, 2008 4:12 pm Post subject: Tower Defence Target Tracker |
|
|
Hello,
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 variables, but i can't seem to spot the error.
here is the function/cause of my pain,
| Code: | 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;
};
|
it is there along with the "oslDebug("bench #");" so you know what/where im talking about.
Help would be very much appreciated, thank you for your time! _________________ Programmer van der C |
|
| Back to top |
|
 |
jimparis
Joined: 10 Jun 2005 Posts: 1179 Location: Boston
|
Posted: Wed Apr 09, 2008 6:01 pm Post subject: |
|
|
Hi,
Just run the code in psplink, find out why it's crashing (get the exact address and reason), then use a debugger or psp-addr2line to find out the cause. |
|
| Back to top |
|
 |
memon
Joined: 03 Oct 2005 Posts: 63
|
Posted: Thu Apr 10, 2008 6:50 am Post subject: |
|
|
I'm pretty sure the variable 'i' is out of range. You write to the gunner array the first time just after the 'bench 6'. Often reading out of bounds will just read off garbage, but writing there is nastier.
Random nit-pickings ;)
- It is a good habit to initialize your variables
- You can remove a couple of extra comparisons and special cases if you initialize the curDist to gunner range*range
- tempDist defined twice
- your code is likely to be more readable, if you would a pointer to the gunner instead of that array indexing all the time.
- the arguments for atan2 are in order y and x
- I dont quite understand the MAX_ENEMIES-1 on that enemy loop |
|
| Back to top |
|
 |
BlackShark
Joined: 02 Mar 2007 Posts: 11
|
Posted: Thu Apr 10, 2008 8:33 am Post subject: |
|
|
| memon wrote: | I'm pretty sure the variable 'i' is out of range. You write to the gunner array the first time just after the 'bench 6'. Often reading out of bounds will just read off garbage, but writing there is nastier.
Random nit-pickings ;)
- It is a good habit to initialize your variables
- You can remove a couple of extra comparisons and special cases if you initialize the curDist to gunner range*range
- tempDist defined twice
- your code is likely to be more readable, if you would a pointer to the gunner instead of that array indexing all the time.
- the arguments for atan2 are in order y and x
- I dont quite understand the MAX_ENEMIES-1 on that enemy loop |
thank you very much for your response, (and nit picking), what do you mean i is out of range? _________________ Programmer van der C |
|
| Back to top |
|
 |
memon
Joined: 03 Oct 2005 Posts: 63
|
Posted: Thu Apr 10, 2008 8:11 pm Post subject: |
|
|
| Bu out of range I mean that the you are indexing outside the gunner array. A common way to do that is off by one, say, you have 3 gunners and pass i=3 to the function. Or calling function with uninitialized variable, etc. Make sure i >= 0 && i < maxgunners. |
|
| Back to top |
|
 |
BlackShark
Joined: 02 Mar 2007 Posts: 11
|
Posted: Thu Apr 10, 2008 11:42 pm Post subject: |
|
|
O, ok _________________ Programmer van der C |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|