home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 February / Gamestar_81_2006-02_dvd.iso / Red Shark / Common / BaseAITasks.script < prev    next >
Encoding:
Text File  |  2001-09-24  |  5.1 KB  |  243 lines

  1. //-------------------------------------------------------------------
  2. //
  3. //  This code is copyright 2001 by G5 Software.
  4. //  Any unauthorized usage, either in part or in whole of this code
  5. //  is strictly prohibited. Violators WILL be prosecuted to the
  6. //  maximum extent allowed by law.
  7. //
  8. //-------------------------------------------------------------------
  9.  
  10. // *** Bases for AI specific behavior ***
  11. //
  12. // use SetBehaviorTask function to specify mission task for behavior
  13. //
  14.  
  15. class CBaseAITask_BaseTask
  16. {
  17.   //
  18.   // *** 'virtual' functions ***
  19.   //
  20.   // All functions do nothing by default. Overwrite if needed
  21.   //
  22.  
  23.  
  24.   //
  25.   // start up initialization
  26.  
  27.   void Init() {}            // called by CVehicleBehavior after assign
  28.  
  29.   //
  30.   // events
  31.  
  32.   void OnEnemyTargeted() {} // called when enemy targeted by a gun
  33.  
  34.   void OnNoEnemy() {}       // called when all enemy disapeared from radar
  35.  
  36.   void OnUpdate() {}        // called every game takt to synchronize
  37.  
  38.   void OnStopped() {}       // called when unit stop
  39.  
  40.   string OnGetDebugInfo()   // called when behavior renders debug information
  41.   {                         // returned string will be appended to output text
  42.     return "";
  43.   }
  44.  
  45.   void OnOrderedEnemyKilled() // called when the enemy (specified by SetFireStyle_Enemy) was killed
  46.   {
  47.     SetFireStyle_Nearest();
  48.   }
  49.  
  50.   void OnLeaderLost( string _LeaderID) {} // called when leader (specified by SetOrder_Follow or SetOrder_Formation) is lost
  51.   
  52.   void OnEnemyLost( string _EnemyID) {}   // called when enemy (specified by SetOrder_Attack) is lost
  53. }
  54.  
  55. class CBaseAITask_Patrol extends CBaseAITask_BaseTask
  56. {
  57.   //
  58.   //  ***  'pure virtual' functions
  59.   //
  60.   // the functions should be overwritten
  61.   //
  62.  
  63.   // Overwrite this function to specify patrol trip
  64.   // the function shoud return array of vectors
  65.  
  66.   array GetPatrolTrip()
  67.   {
  68.     return array(); // the default implementation returns empty array
  69.   }
  70.  
  71.  
  72.   //
  73.   //  ***  'virtual' functions
  74.   //
  75.   // the functions can be overwritten to implement specific patrol logic
  76.   //
  77.  
  78.   void ResetTripPointIndex()
  79.   {
  80.     m_TripPointIndex = 0;
  81.   }
  82.  
  83.   boolean HasMoreTripPoints()
  84.   {
  85.     array Trip = GetPatrolTrip();
  86.  
  87.     return ( Trip.size() > 0);
  88.   }
  89.  
  90.   void IncrementTripPointIndex()
  91.   {
  92.     // default implementation cycles trip points
  93.  
  94.     array Trip = GetPatrolTrip();
  95.  
  96.     m_TripPointIndex = m_TripPointIndex + 1;
  97.  
  98.     if ( m_TripPointIndex >= Trip.size())
  99.       m_TripPointIndex = 0;
  100.   }
  101.  
  102.   vector GetNowTripPoint()
  103.   {
  104.     array Trip = GetPatrolTrip();
  105.     return ( Trip[m_TripPointIndex]);
  106.   }
  107.  
  108.   float GetTripSpeed()
  109.   {
  110.     // default implementation returns vehicle maximum speed
  111.     return MaxSpeed; // this property returns by script user
  112.   }
  113.  
  114.  
  115.   //
  116.   //  ***  notification handlers
  117.   //
  118.  
  119.   // called by Behavior on initialization
  120.  
  121.   void Init()
  122.   {
  123.     Notify_Init();
  124.  
  125.     StartPatrol();
  126.   }
  127.  
  128.   void StartPatrol()
  129.   {
  130.     m_Active = true;
  131.  
  132.     // set start trip point
  133.     ResetTripPointIndex();
  134.  
  135.     if ( HasMoreTripPoints())
  136.     {
  137.       // order to move to current trip point
  138.       SetOrder_MoveTo(
  139.         GetNowTripPoint(),
  140.         GetTripSpeed());
  141.     }
  142.   }
  143.  
  144.   void ActivatePatrol()
  145.   {
  146.     if ( !m_Active && HasMoreTripPoints())
  147.     {
  148.       // order to move to current trip point
  149.       SetOrder_MoveTo(
  150.         GetNowTripPoint(),
  151.         GetTripSpeed());
  152.     }
  153.  
  154.     m_Active      = true;
  155.     m_AttackState = false;
  156.   }
  157.  
  158.   void DeactivatePatrol()
  159.   {
  160.     m_Active      = false;
  161.     m_AttackState = false;
  162.  
  163.     SetOrder_StopNow();
  164.   }
  165.  
  166.   // called by Behavior when movement stopped
  167.   void OnStopped()
  168.   {
  169.     if ( m_Active && !m_AttackState)
  170.     {
  171.       // trip point reached. move to next one
  172.       IncrementTripPointIndex();
  173.  
  174.       if ( HasMoreTripPoints())
  175.       {
  176.         // order to move to current trip point
  177.         SetOrder_MoveTo(
  178.           GetNowTripPoint(),
  179.           GetTripSpeed());
  180.       };
  181.     }
  182.   }
  183.  
  184.  
  185.   void OnEnemyTargeted()
  186.   {
  187.     Notify_OnEnemyTargeted();
  188.  
  189.     if ( m_Active && !m_AttackState)
  190.     {
  191.       // enemy on radar. attack it
  192.       m_AttackState = true;
  193.  
  194.       SetFireStyle_Enemy( GetTargetedEnemy());
  195.       SetOrder_Attack(
  196.         GetTargetedEnemy(),
  197.         200.0,            // #TMP:#TODO
  198.         300.0,            // #TMP:#TODO
  199.         GetTripSpeed());  // #TMP:#TODO
  200.  
  201.       Notify_BeginAttack();
  202.     };
  203.   }
  204.  
  205.   void OnNoEnemy()
  206.   {
  207.     if ( m_Active && m_AttackState)
  208.     {
  209.       // no more enemy on radar. continue patrolling
  210.       m_AttackState = false;
  211.  
  212.       if ( HasMoreTripPoints())
  213.       {
  214.         // order to move to current trip point
  215.         SetFireStyle_Nearest();
  216.         SetOrder_MoveTo(
  217.           GetNowTripPoint(),
  218.           GetTripSpeed());
  219.       };
  220.  
  221.       Nofify_EndAttack();
  222.     };
  223.   };
  224.  
  225.   // #TMP: special functions
  226.  
  227.   void Notify_Init() {};
  228.   void Notify_BeginAttack() {};
  229.   void Notify_OnEnemyTargeted() {};
  230.   void Nofify_EndAttack() {};
  231.  
  232.  
  233.   //
  234.   // *** 'private' data and operations
  235.   //
  236.  
  237.  
  238.   int      m_TripPointIndex = 0;
  239.   boolean  m_AttackState    = false;
  240.   boolean  m_Active         = true;
  241. }
  242.  
  243.