home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 58 / pcpp58b_TRIBES.iso / Tribes / base / scripts.vol / ai.cs < prev    next >
Encoding:
Text File  |  1998-12-16  |  6.6 KB  |  216 lines

  1. //
  2. // AI support functions.
  3. // 
  4.  
  5.  
  6. //
  7. // This function creates an AI player using the supplied group of markers 
  8. //    for locations.  The first marker in the group gives the starting location 
  9. //    of the the AI, and the remaining markers specify the path to follow.  
  10. //
  11. // Example call:  
  12. // 
  13. //    createAI( guardNumberOne, "MissionGroup\\Teams\\team0\\guardPath", larmor );
  14. //
  15. function createAI( %aiName, %markerGroup, %armorType, %name )
  16. {
  17.    %group = nameToID( %markerGroup );
  18.    
  19.    if( %group == -1 || Group::objectCount(%group) == 0 )
  20.    {
  21.       echo( %aiName @ "Couldn't create AI: " @ %markerGroup @ " empty or not found." );
  22.       return -1;
  23.    }
  24.    else
  25.    {
  26.       %spawnMarker = Group::getObject(%group, 0);
  27.       %spawnPos = GameBase::getPosition(%spawnMarker);
  28.       %spawnRot = GameBase::getRotation(%spawnMarker);
  29.  
  30.       if( AI::spawn( %aiName, %armorType, %spawnPos, %spawnRot, %name, "male2" ) != "false" )
  31.       {
  32.          // The order number is used for sorting waypoints, and other directives.  
  33.          %orderNumber = 100;
  34.          
  35.          for(%i = 1; %i < Group::objectCount(%group); %i = %i + 1)
  36.          {
  37.              
  38.             %spawnMarker = Group::getObject(%group, %i);
  39.             %spawnPos = GameBase::getPosition(%spawnMarker);
  40.             
  41.             AI::DirectiveWaypoint( %aiName, %spawnPos, %orderNumber );
  42.             
  43.             %orderNumber += 100;
  44.          }
  45.       }
  46.       else{
  47.          echo( "Failure spawning: " @ %aiName );
  48.       }
  49.    }
  50. }
  51.  
  52. //------------------------------------------------------------------
  53. //functions to test and move AI players.
  54. //
  55. //------------------------------------------------------------------
  56.  
  57. //
  58. //This function will spawn an AI player about 5 units away from the 
  59. //player that is passed to the function(%commandIssuer).
  60. //
  61. //
  62. $numAI = 0;
  63. function AI::helper(%aiName, %armorType, %commandIssuer)
  64. {
  65.   
  66.    %spawnMarker = GameBase::getPosition(%commandIssuer);
  67.    %xPos = getWord(%spawnMarker, 0) + floor(getRandom() * 15);
  68.    %yPos = getword(%spawnMarker, 1) + floor(getRandom() * 10);
  69.    %zPos = getWord(%spawnMarker, 2) + 2;
  70.    %rPos = GameBase::getRotation(%commandIssuer);
  71.    
  72.    
  73.    echo("Spawning AI helper at position " @ %xPos @ " " @ %yPos @ " " @ %zPos);
  74.    echo("Current Issuer rotation: " @ %rPos);
  75.       
  76.    %aiSpawnPos = %xPos @ "  " @ %yPos @ "  " @ %zPos;
  77.    %newName = %aiName @ $numAI;
  78.    $numAI++;
  79.    Ai::spawn(%newName, %armorType, %aiSpawnPos, %rPos);
  80.    return ( %newName );
  81. }
  82.  
  83. //
  84. //This function will move an AI player to the position of an object
  85. //that the players LOS is hitting(terrain included). Must be `    within 50 units.
  86. //
  87. //
  88.  
  89. function AI::moveToLOS(%aiName, %commandIssuer) 
  90. {
  91.    %issuerRot = GameBase::getRotation(%commandIssuer);
  92.    %playerObj = Client::getOwnedObject(%commandIssuer);
  93.    %playerPos = GameBase::getPosition(%commandIssuer);
  94.       
  95.    //check within max dist
  96.    if(GameBase::getLOSInfo(%playerObj, 100, %issuerRot))
  97.    { 
  98.       %newIssuedVec = $LOS::position;
  99.       %distance = Vector::getDistance(%playerPos, %newIssuedVec);
  100.       echo("Command accepted, AI player(s) moving....");
  101.       echo("distance to LOS: " @ %distance);
  102.       AI::DirectiveWaypoint( %aiName, %newIssuedVec, 1 );
  103.    }
  104.    else
  105.       echo("Distance to far.");
  106.       echo("LOS point: " @ $LOS::position);
  107. }
  108.  
  109. //This function will move an AI player to a position directly in front of
  110. //the player passed, at a distance that is specified.
  111. function  AI::moveAhead(%aiName, %commandIssuer, %distance) 
  112. {
  113.    
  114.    %issuerRot = GameBase::getRotation(%commandIssuer);
  115.    %commPos  = GameBase::getPosition(%commandIssuer);
  116.    echo("Commanders Position: " @ %commPos);
  117.    
  118.    //get commanders x and y positions
  119.    %comm_x = getWord(%commPos, 0);
  120.    %comm_y = getWord(%commPos, 1);
  121.    
  122.    //get offset x and y positions
  123.    %offSetPos = Vector::getFromRot(%issuerRot, %distance);
  124.    %off_x = getWord(%offSetPos, 0);
  125.    %off_y = getWord(%offSetPos, 1);
  126.    
  127.    //calc new position
  128.    %new_x = %comm_x + %off_x;
  129.    %new_y = %comm_y + %off_y;
  130.    %newPos = %new_x  @ " " @ %new_y @ " 0";
  131.   
  132.    //move AI player
  133.    echo("AI moving to " @ %newPos);
  134.    AI::DirectiveWaypoint(%aiName, %newPos, 1);
  135. }  
  136.  
  137. //
  138. // OK, this is the complete command callback - issued for any command sent
  139. //    to an AI. 
  140. //
  141. function AI::onCommand ( %name, %commander, %command, %waypoint, %targetId, %cmdText, 
  142.          %cmdStatus, %cmdSequence )
  143. {
  144.    if( %command == 2 || %command == 1 )
  145.    {
  146.       // must convert waypoint location into world location.  waypoint location
  147.       //    is given in range [0-1023, 0-1023].  
  148.       %worldLoc = WaypointToWorld ( %waypoint );
  149.       AI::DirectiveWaypoint( %name, %worldLoc, 125 );
  150.       dbecho ( 2, %name @ " IS PROCEEDING TO LOCATION " @ %worldLoc );
  151.    }
  152.    dbecho ( 2, " AI::OnCommand() issued to  " @ %name @ "  with parameters: " );
  153.    dbecho ( 3, "    Cmdr:        " @ %commander );
  154.    dbecho ( 3, "    Command:     " @ %command );
  155.    dbecho ( 3, "    Waypoint:    " @ %waypoint );
  156.    dbecho ( 3, "    TargetId:    " @ %targetId );
  157.    dbecho ( 3, "    cmdText:     " @ %cmdText );
  158.    dbecho ( 3, "    cmdStatus:   " @ %cmdStatus );
  159.    dbecho ( 3, "    cmdSequence: " @ %cmdSequence );
  160. }
  161.  
  162.  
  163. // Play the given wave file FROM %source to %DEST.  The wave name is JUST the basic wave
  164. //    name without voice base info (which it will grab for you from the source client Id).  
  165. //
  166. // Example:
  167. //    Ai::soundHelp( 2051, 2049, cheer3 );
  168. //
  169. function Ai::soundHelper( %sourceId, %destId, %waveFileName )
  170. {
  171.    %wName = strcat( "~w", Client::getVoiceBase( %sourceId ) );
  172.    %wName = strcat( %wName, ".w" );
  173.    %wName = strcat( %wName, %waveFileName );
  174.    %wName = strcat( %wName, ".wav" );
  175.    
  176.    dbecho( 1, "Trying to play " @ %wName );
  177.    
  178.    Client::sendMessage( %destId, 0, %wName );
  179. }
  180.  
  181.  
  182.  
  183.  
  184. //
  185. //incomplete function that is only going to 
  186. //be used for testing.
  187. //
  188. function AI::hunt(%commander) 
  189. {
  190.   %k = 0;
  191.   
  192.   
  193.   %markerPos = GameBase::getPosition(%commander);
  194.   
  195.   %set = newObject("set",SimSet);
  196.   %mask = $SimPlayerObjectType;
  197.   %targets = containerBoxFillSet(%set,%mask,%markerPos,100,100,20,1);
  198.   echo("Number of targets:" @ %targets);
  199.   
  200.   for(%i = 0; %i < %targets; %i++)
  201.   {
  202.     %lockedObj = Group::getObject(%set, %i);
  203.     echo("Object locked:" @ %lockedObj);
  204.     echo("checking for continuity...");
  205.     
  206.     if(Player::getClient(%lockedObj) == -1)
  207.       %lockedObj = Group::getObject(%set, %i++);
  208.   }
  209.   echo("finished with set so deleting...");
  210.   deleteObject(%set);
  211.  
  212.   echo("Target = " @ %lockedObj);
  213.   %target = Player::getClient(%lockedObj);
  214.   AI::attack(%commander, %target);
  215.  
  216. }