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

  1. #include "ie.h"
  2.  
  3. #include "Cat.h"
  4. #include "util.h"
  5.  
  6. #include <math.h>
  7.  
  8. Cat::Cat ( char * name )
  9. {
  10.     setName ( name );
  11.     registerAnims ();
  12.     makeBrain     ();
  13.  
  14.     m_alignment  = A_Cats;
  15.     m_sightRange = 15;
  16.  
  17. }
  18.  
  19. void Cat::registerAnims ()
  20. {
  21.     m_anims[ CA_LieDown ] = new Anim ( 1.0f );
  22.     m_anims[ CA_Yawn    ] = new Anim ( 1.0f );
  23.     m_anims[ CA_Rest    ] = new Anim ( 2.0f, true );
  24.     m_anims[ CA_GetUp   ] = new Anim ( 1.0f );
  25.     m_anims[ CA_Stretch ] = new Anim ( 0.5f );
  26.     m_anims[ CA_Walk    ] = new Anim ( 1.0f, true );
  27.     m_anims[ CA_Run     ] = new Anim ( 1.0f, true );
  28.     m_anims[ CA_Fight   ] = new Anim ( 1.0f, true );
  29.     m_anims[ CA_Die     ] = new Anim ( 1.5f );
  30. };
  31.  
  32. const char * Cat::getAttack ( int & damage, float & prepareTime )
  33. {
  34.     const int numAttacks = 4;
  35.  
  36.     static const char * attacks[numAttacks] = {"bites", "scratchs", "snarls at", "swipes"};
  37.     static int          damagel[numAttacks] = {15, 10, 0, 5};
  38.     static float        ptime  [numAttacks] = {0.9f, 0.6f, 0.1f, 0.3f};
  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 * Cat::getDamageReact ( float & rtime )
  52. {
  53.     const int numReact = 3;
  54.  
  55.     static const char * desc[ numReact ] = {"screeches", "yowls", "jumps"};
  56.     static float        time[ numReact ] = {0.2f, 0.5f, 0.6f};
  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.  
  68. IE_START(Cat)
  69.  
  70.     GOAL (Wander)
  71.  
  72.         IF (Tired)      GOSUB (Nap)
  73.         IF (Chased)     GOTO  (Flee)
  74.  
  75.     GOAL (Nap)
  76.  
  77.         IF (Rested)     RETURN
  78.  
  79.     GOAL (Flee)
  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.