home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 06 General Architectures / 04 Christian / dog.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-10  |  2.0 KB  |  95 lines

  1. #include "ie.h"
  2.  
  3. #include "dog.h"
  4. #include "util.h"
  5.  
  6. #include <math.h>
  7.  
  8. Dog::Dog ( char * name )
  9. {
  10.     setName ( name );
  11.     registerAnims ();
  12.     makeBrain     ();
  13.  
  14.     m_alignment   = A_Dogs;
  15.     m_sightRange  = 30;
  16. }
  17.  
  18. void Dog::registerAnims ()
  19. {
  20.     m_anims[ CA_LieDown ] = new Anim ( 1.0f );
  21.     m_anims[ CA_Yawn    ] = new Anim ( 1.0f );
  22.     m_anims[ CA_Rest    ] = new Anim ( 2.0f, true );
  23.     m_anims[ CA_GetUp   ] = new Anim ( 1.0f );
  24.     m_anims[ CA_Stretch ] = new Anim ( 0.5f );
  25.     m_anims[ CA_Walk    ] = new Anim ( 1.0f, true );
  26.     m_anims[ CA_Run     ] = new Anim ( 1.0f, true );
  27.     m_anims[ CA_Fight   ] = new Anim ( 1.0f, true );
  28.     m_anims[ CA_Die     ] = new Anim ( 2.0f );
  29. };
  30.  
  31.  
  32. const char * Dog::getAttack ( int & damage, float & prepareTime )
  33. {
  34.     const int numAttacks = 4;
  35.  
  36.     static const char * attacks[numAttacks] = {"bites", "snaps at", "barks at", "paws"};
  37.     static int          damagel[numAttacks] = {20, 8, 0, 4};
  38.     static float        ptime  [numAttacks] = {1.0f, 0.5f, 0.2f, 0.4f};
  39.  
  40.     float r = frand();
  41.  
  42.     int idx = (int)floor(r * (float)numAttacks);
  43.  
  44.     damage      = damagel[ idx ];
  45.     prepareTime = ptime[ idx ];
  46.  
  47.     return attacks[ idx ];
  48.  
  49. }
  50.  
  51. const char * Dog::getDamageReact( float & rtime )
  52. {
  53.     const int numReact = 3;
  54.  
  55.     static const char * desc[ numReact ] =  {"howls", "yelps", "whimpers"};
  56.     static float        time[ numReact ] =  {0.5f, 0.3f, 0.2f};
  57.  
  58.     float r = frand();
  59.  
  60.     int idx = (int)floor(r * (float)numReact);
  61.  
  62.     rtime = time[ idx ];
  63.  
  64.     return desc[idx];
  65. }
  66.  
  67. IE_START(Dog)
  68.  
  69.     GOAL (Wander)
  70.  
  71.         IF (Tired)      GOSUB (Nap)
  72.         IF (SeesEnemy)  GOTO  (Chase)
  73.  
  74.     GOAL (Nap)
  75.  
  76.         IF (Rested)     RETURN
  77.  
  78.     GOAL (Chase)
  79.         
  80.         IF (Caught)     GOTO  (Fight)
  81.  
  82.     GOAL (Fight)
  83.  
  84.         IF (Dying)      GOTO  (Die)
  85.         IF (Damaged)    GOSUB (DamageReact)
  86.         IF (WonFight)   GOTO  (Wander)
  87.  
  88.     GOAL (DamageReact)
  89.  
  90.         IF (Done)        RETURN
  91.  
  92.     GOAL (Die)
  93.  
  94. IE_END
  95.