View Single Post
  #2  
Old 03-04-2007, 04:45 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Gambet View Post
PHP Code:
//GBaddy
//Version 1.0

//Features:
// Constantly moving about, not idle like most
// boring baddies you see on Graal. 
// When you step into the baddies vision radius,
// he will begin to chase after you, in attack mode.
// While in attack mode, the baddy has a total of
// 3 possible things that he can do, all of which
// are chosen randomly.
// The baddy can choose to swing his sword at
// you, heal himself, and/or cause an explosion
// under him.
// NOTE: I didn't add any damage checks besides
//       Was.Hit, so if Was.Hit only checks for
//       whether or not the baddy was hit by
//       a sword, then that's all that will
//       hurt it for now.
// NOTE #2: I know that it's NOT PERFECT,
//          but it's the first time I've actually,
//          worked with Artificial Intelligence,
//          excluding the current bot that I'm
//          making on Graal X, which is much
//          different. I figured it was a
//          nice learning experience, and I
//          figured some people could find it
//          useful since it's MUCH MORE challenging
//          than the default baddy NPCs.
// NOTE #3: I used some of the mathematical
//          calculations previously created by
//          Stefan in his baddy NPC (the default 
//          Graal baddy NPC).

//By: Gambet

enum 
{
 
WALK,
 
ATTACK,
 
DEAD
}

function 
onCreated()
{
  
showcharacter();
  
this.bodyimg "body2.png";
  
this.shieldimg "no-shield.png";
  
this.nick "GBaddy";
  
this.ap 0;
  for (
a=0a<5a++)
   {
    
this.colors[a] = "black";
   }
  if (
this.ani != "idle")
   {
     
setcharani("idle","");
   }
  
this.health = {10,10};
  
this.mov_speed 0.5;
  
this.count int(random(10,40));
  
this.mode WALK;
  
setTimer(0.1);
}

function 
onPlayerEnters()
{
  
onTimeOut(); //To Wake Up The NPC
  
this.headimg "head" int(random(0,1000)) @ ".png"//To Have A Different Head Each Time You Enter The Level
}

function 
onWas.Hit() //REMOVE THE . - GRAAL FORUMS FILTER IS ANNOYING >O
{
 if (
this.health[0]-(player.swordpower/2) > 0)
  {
    if (
this.ani != "hurt")
     {
       
setcharani("hurt","");
       
scheduleevent(1,"Counter","");
     }
    switch(
this.plyr.dir//Knockback
     
{
      case 
"2":
       if (!
onwall(this.x,this.y+3))
        {
         
this.y+=3;
        }
       break;
      case 
"0":
       if (!
onwall(this.x,this.y-3))
        {
         
this.y-=3;
        }
       break;
      case 
"1":
       if (!
onwall(this.x-3,this.y))
        {
         
this.x-=3;
        }
       break;
      case 
"3":
       if (!
onwall(this.x+3,this.y))
        {
         
this.x+=3;
        }
       break;
     }
     
this.health[0]-=player.swordpower/2;
     
chat this.health[0] @ "/" this.health[1];
     
scheduleevent(2,"HideChat","");
  } else
   {
    if (
this.mode != DEAD)
     {
       
this.mode DEAD;
       
this.health[0] = 0;
       
putexplosion2(3,3,this.x,this.y);
       
putexplosion2(3,3,this.x,this.y+0.5);
       
setcharani("dead","");
       
chat this.health[0] @ "/" this.health[1];
       
scheduleevent(8,"Respawn","");
     }
   }
}

function 
onHideChat()
{
 if (
params[0] == "idle")
  {
    
chat "";
    
setcharani("idle","");
  } else
   {
     
chat "";
   }
}

function 
onCounter()
{
 if (
this.health[0] > 0)
  {
    
setcharani("sword","");
  }
}

function 
onRespawn()
{
 if (
this.mode == DEAD)
  {
    
setcharani("idle","");
    
chat "";
    
this.health[0] = this.health[1];
    
this.mode WALK;
  }
}

function 
onTimeOut()
{
 if (
players.size() > 0
  {
   
setTimer(0.1);
  } else
   {
    
setTimer(0); //To Sleep In Case No Players Are In The Room - Saves CPU Time
   
}
 if (
this.mode != DEAD)
  {
   if (
this.to_check.x in |this.x-10,this.x+10| && this.to_check.y in |this.y-10,this.y+10|)
    {
     if (
this.mode != ATTACK)
      {
        
this.mode ATTACK;
      }
    } else
     {
       
this.mode WALK;
     }
  }
 if (
this.mode == WALK)
  {
    
this.to_check findnearestplayer(this.x,this.y);
    
this.new_x this.vecx(this.dir) * this.mov_speed;
    
this.new_y this.vecy(this.dir) * this.mov_speed;
    
this.count--;
   if (
this.count 0)
    {
      
this.to_add_x this.new_x 1.5 vecx(this.dir);
      
this.to_add_y this.new_y vecy(this.dir);
     if (
onwall(this.to_add_x,this.to_add_y)) 
      {
        
this.dir = (this.dir+2)%4;
      } else 
       {
         
setcharani("walk","");
         
this.new_x;
         
this.new_y;
       }
    } else
     {
       
this.count int(random(10,40));
       
this.dir = (this.dir+1+int(random(0,2))*2)%4;
     }
  }
  if (
this.mode == ATTACK)
   {
     
this.plyr this.to_check;
     
this.dist_x this.plyr.this.x;
     
this.dist_y this.plyr.this.y;
     
this.len = (this.dist_x*this.dist_x+this.dist_y*this.dist_y)^0.5;
     
this.add_x = (this.dist_x this.len);
     
this.add_y = (this.dist_y this.len);
     
this.check_x this.1.5;
     
this.check_y this.2;
     if (!
onwall(this.check_x this.add_x,this.check_y this.add_y))
      {
        
DamagePlayer();
        
Move();
      } else if (!
onwall(this.check_x this.add_x,this.check_y)) 
       {
         
this.+= this.add_x;
         
DamagePlayer();
       } else if (!
onwall(this.check_x,this.check_y this.add_y))
        {
          
this.+= this.add_y;
          
DamagePlayer();
        }
   }
 
setTimer(0.1); 
}

function 
DamagePlayer()
{
 
this.randomize int(random(0,101));
  if (
this.randomize in |0,82|) //Attack With Sword
   
{
     
setcharani("sword","");
   } else if (
this.randomize in |83,95|) //Cause Explosion
    
{
      
putexplosion2(3,3,this.x,this.y);
    } else if (
this.randomize in |95,101|) //Heal
     
{
      if (
this.health[0] < this.health[1])
       {
        if (
this.health[0]+this.health[1])
         {
           
this.temp_add int(random(0,6));
           
this.health[0]+=this.temp_add;
           
chat "*Healed for " this.temp_add "*";
         } else if (
this.health[0]+>= this.health[1])
          {
            
this.health[0] = this.health[1];
            
chat "*Fully Healed*";
          }
           
setcharani("lift","");
           
this.health[0] = this.health[1];
           
scheduleevent(1,"HideChat","idle");
       }
     }
}

function 
Move()
{
  
setcharani("walk","");
  
this.x+=this.add_x * (this.mov_speed 0.6);
  
this.y+=this.add_y * (this.mov_speed 0.6);
  if (
abs(this.dist_x) > abs(this.dist_y)) 
   {
    if (
this.dist_x 0
     {
       
this.dir 3
     } else 
      {
        
this.dir 1;
      }
   } else 
    {
     if (
this.dist_y 0
      {
        
this.dir 2
      } else 
       {
         
this.dir 0;
       }
    }




Very nice, thanks Gambet! A lot of people have been waiting for someone to release baddies.
__________________
Reply With Quote