• Advertisement

Archived

This topic is now archived and is closed to further replies.

Integration of AI into objects... Also, will this method work?

This topic is 5714 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all! I'm new to the world of AI and have a question regarding my current game. I'm making a simple Space Invaders clone, and am wondering how AI is implemented. I don't know if you embed the AI directly in the objects member functions, or what! However, if you do, then I've written the following pseudo-code to do the enemy ships firing. Is this a good approach for simple AI, or am I way off base?
      
enum ENEMY_DIRECTION
{
  IMMOBILE=0, TOWARDS=1, STOPPED=2
};

//*********************************************************************
// bool CShip::CheckAttack(const CShip& enemy)                        
// This function determines if a craft should fire at the enemy craft 
// For calculations, it assumes NO acceleration.. All instantaneous   
// craft velocities are == to the max velocity or the ship is at rest 
// RETURN VALUES: true means attack should be performed               
//                false means attack should not be performed          
// NOTE: THIS DOES NOT YET TAKE INTO ACCOUNT THE ACTIVE WEAPON!  IF   
//        THE WEAPON IS THE SPREAD-FIRE CANNON THIS WILL FAIL BADLY    
//*********************************************************************
bool CShip::CheckAttack(const CShip& enemy)
{
/*
  CShip(THIS_SHIP)
   |   | \ 
   |  \ z
   |      |       |-----      x     CShip(enemy)
*/
 float enemyX = enemy.getX();
 float enemyY = enemy.getY();

 float x = enemyX - m_position.x;
 float y = enemyY - m_position.y;

 //float xsquared = x*x;

 //float ysquared = y*y;

 //float zsquared = (xsquared + ysquared);


 ENEMY_DIRECTION enemyDir = AWAY;

 //* Find out how far away the enemy craft is, then determine

 //* if it is coming towards me, away from me, or sitting still

 float enemySpeed = enemy.getSpeed();

 if(enemySpeed != 0 && m_position.x > enemyX)
    if(x > 0)
      enemyDir = TOWARDS;

 if(enemySpeed != 0 && m_position.x < enemyX)
    if(x < 0)
      enemyDir = TOWARDS;

 if(enemySpeed == 0)
    enemyDir = STOPPED;

 if(enemyDir == STOPPED)
 {
    if(enemyX == m_position.x)
        return true;  //* Sitting duck

    else
        return false; //* Lame craft is hiding in corner

 }

 float timeUntilBulletHits = y / getActiveWeaponMuzzleVelocity();
 float timeUntilEnemyBelow = x / enemySpeed;

 // If bullet can hit ground in less time than it would take for the enemy

 // to travel directly below, I can possibly score a hit

 // If bullet time is greater than travel time, the ship could go right past

 // way before my bullet would be able to hit it.

 if(timeUntilBulletHits >= timeUntilEnemyBelow*0.95f &&
    timeUntilBulletHits <= timeUntilEnemyBelow)
 {
   if(TOWARDS_ME)
   { //* present course will bring enemy within fire-range

     return true;
   }
 }

 return false;
}
      
[edited by - camcbri on May 31, 2002 3:53:04 PM] [edited by - camcbri on May 31, 2002 3:56:53 PM]

Share this post


Link to post
Share on other sites
Advertisement
as far as i can tell, the original space invaders just had them fire randomly, and more so at higher levels (with an occasional bomb or homing bomb).
or am i thinking of a different game?

Share this post


Link to post
Share on other sites
well.. i hope to make the enemies a little more accurate than the original if they really did just fire randomly!

basically, what i''m doing is determining if the player is heading toward the enemy, away from, or sitting still.

if he''s sitting still and under the enemy, i return true but if he''s still and not under me, I return false.

then, i figure out how long it would take for the bullet to get to the player if he were moving toward the enemy. If he can beat the bullet by a long ways and pass under me, I don''t want to fire. SO, I return true if the player ship is within 95% and 100% of the distance that he could traverse before getting directly under the current enemy ship position..

it''s all boils down to velocity = distance / time, but I wanted to ask if there are any shortcuts, or if I shouldn''t put this into the CShip class at all, but in some AI library? I remember very little from my physics classes so velocity was the first thing I thought of!

I thought that this would work for a demo mode too... If both the player ship and the enemy ships are derived from CShip, then the computer could play both sides! Now I just have to teach it how to evade...

Share this post


Link to post
Share on other sites

  • Advertisement