forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Tower Defence Target Tracker

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
BlackShark



Joined: 02 Mar 2007
Posts: 11

PostPosted: Wed Apr 09, 2008 4:12 pm    Post subject: Tower Defence Target Tracker Reply with quote

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
Code:
 s_gunner[i].range
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
View user's profile Send private message Visit poster's website
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Wed Apr 09, 2008 6:01 pm    Post subject: Reply with quote

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
View user's profile Send private message
memon



Joined: 03 Oct 2005
Posts: 63

PostPosted: Thu Apr 10, 2008 6:50 am    Post subject: Reply with quote

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
View user's profile Send private message
BlackShark



Joined: 02 Mar 2007
Posts: 11

PostPosted: Thu Apr 10, 2008 8:33 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
memon



Joined: 03 Oct 2005
Posts: 63

PostPosted: Thu Apr 10, 2008 8:11 pm    Post subject: Reply with quote

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
View user's profile Send private message
BlackShark



Joined: 02 Mar 2007
Posts: 11

PostPosted: Thu Apr 10, 2008 11:42 pm    Post subject: Reply with quote

O, ok
_________________
Programmer van der C
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
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