home *** CD-ROM | disk | FTP | other *** search
/ Classic Fond 54 / ClassicFond54.iso / games / stars.rar / multiplayer / multiplayerStdLib.cs < prev    next >
Text File  |  1999-02-19  |  29KB  |  914 lines

  1. //------------------------------------------------------------------------------
  2. //
  3. // Multiplayer standard library
  4. //
  5.  
  6. //-----------------------------------------------
  7. // Utility functions
  8.  
  9.  
  10. // If this variable is set to true, when a teammate hits another teammate repeatedly, 
  11. // he will be penalized. The offender's Herc will be shut down, his reactor and shields
  12. // drained, and all of his ammo (energy too) drained. Once a player has offended, he
  13. // will be on probation temporarily. The probation time depends on how bad (or many times)
  14. // he has attacked his teammates. 
  15. //
  16. // When enabled, the offending player is notified with a text message that he is being
  17. // penalized for team attacking.
  18. $teamHittingPenalty = False;
  19.  
  20.  
  21. function timeDifference(%t1, %t2)
  22. {
  23.    // calculates the time difference t1-t2 and returns it as a string
  24.    // such as "1 minute, 32 seconds"
  25.    
  26.    %diff = %t1 - %t2;
  27.    
  28.    %minutes = 0;
  29.    
  30.    // can't do rounding in console so hack it
  31.    %temp = %diff;
  32.    %temp = %temp - 60;
  33.    
  34.    while(%temp >= 0)
  35.    {
  36.       %minutes = %minutes + 1;
  37.       %temp = %temp - 60;
  38.    }
  39.       
  40.    %seconds = %temp + 60;
  41.    
  42.    
  43.    // prepare output
  44.    %output = "";
  45.    
  46.    if(%minutes > 0)
  47.    {
  48.       %output = %output @ %minutes @ " ";
  49.       if(%minutes == 1)
  50.       {
  51.          %output = %output @ *IDMULT_MINUTE;      
  52.       }
  53.       else
  54.       {
  55.          %output = %output @ *IDMULT_MINUTES;
  56.       }
  57.       
  58.       if(%seconds > 0)
  59.       {
  60.          // make space for seconds output
  61.          %output = %output @ ", ";
  62.       }
  63.    }
  64.    
  65.    if(%seconds > 0)
  66.    {
  67.       %output = %output @ %seconds @ " ";
  68.       if(%seconds == 1)
  69.       {
  70.          %output = %output @ *IDMULT_SECOND;      
  71.       }
  72.       else
  73.       {
  74.          %output = %output @ *IDMULT_SECONDS;
  75.       }
  76.    }
  77.    
  78.    return %output;
  79. }
  80.  
  81.  
  82.  
  83. function withinDistance( %obj1, %obj2, %distance)
  84. {
  85.    %obj1 = getObjectId( %obj1 );
  86.    %obj2 = getObjectId( %obj2 );
  87.  
  88.    %var = getDistance( %obj1, %obj2 );
  89.    
  90.    if( %var > %distance )
  91.    {
  92.       return false;
  93.    }
  94.    else
  95.    {
  96.       return true;
  97.    }
  98. }
  99.  
  100.  
  101. function warp( %obj1, %obj2 )
  102. {
  103.    // set the position of obj1 to the position of obj2
  104.    %obj2X = getPosition( %obj2, "x" );
  105.    %obj2Y = getPosition( %obj2, "y" );
  106.    %obj2Z = getPosition( %obj2, "z" );
  107.    
  108.    setPosition( %obj1, %obj2X, %obj2Y, %obj2Z );
  109. }
  110.  
  111.  
  112. function randomTransport( %object, %x1, %y1, %x2, %y2)
  113. {
  114.    // set the position of %object to a randow spot within the space
  115.    // bounded by (x1, y1) and (x2, y2).  The Z coordinate is going to
  116.    // be set to 40 meters above the terrain at the new point.
  117.    
  118.    %x = randomInt(%x1, %x2);
  119.    %y = randomInt(%y1, %y2);
  120.    %z = getTerrainHeight(%x, %y) + 60;
  121.  
  122.     %playerNum = playerManager::vehicleIdToPlayerNum(%object);
  123.  
  124.    // play a sound at both locations
  125.    playSound(0, IDSFX_TELEPORTER, IDPRF_NEAR, %object);
  126.    playSound(0, IDSFX_TELEPORTER, IDPRF_NEAR, %x, %y, %z);
  127.    
  128.    if(%playerNum != 0)
  129.    {
  130.        fadeEvent(%playerNum, out, 1.0, 1.0, 1.0, 1.0);
  131.        schedule(strcat("fadeEvent(", %playerNum, ",in, 1.0, 1.0, 1.0, 1.0);"), 1.2);
  132.    }
  133.    schedule(strcat("setPosition(", %object, ",", %x,",", %y,",", %z,");"), 1.0);   
  134. }
  135.  
  136. function displayPercentage(%this, %percentage)
  137. {
  138.     %player = playerManager::vehicleIdToPlayerNum( %this );
  139.    if(%player != 0)
  140.    {
  141.        say(%player, 0, *IDMULT_CHAT_SHOW_PERCENTAGE_1 @ %percentage @ *IDMULT_CHAT_SHOW_PERCENTAGE_2);
  142.    }
  143. }
  144.  
  145.  
  146. function getFullPath(%object)
  147. {
  148.    // recursive function to get the path of this thing
  149.    if(%object == 0)
  150.    {
  151.       return "";
  152.    }
  153.    
  154.    %name = getObjectName(%object);
  155.    
  156.    if(%name == "MissionGroup")
  157.    {
  158.       return %name;
  159.    }
  160.    else
  161.    {
  162.       return (getFullPath(getGroup(%object)) @ "\\" @ %name);
  163.    }
  164. }
  165.  
  166. //-----------------------------------------------
  167. // Rules enforcement
  168.  
  169.  
  170. function vehicle::onDestroyed(%destroyed, %destroyer)
  171. {
  172.    // a generic enforcement of rules
  173.  
  174.    if($server::TeamPlay == true)
  175.    {
  176.       if(
  177.          (getTeam(%destroyed) == getTeam(%destroyer)) &&
  178.          (%destroyed != %destroyer)
  179.       )
  180.       {
  181.          antiTeamKill(%destroyer);
  182.       }
  183.    }
  184. }
  185.  
  186. function antiTeamKill(%offender)
  187. {
  188.    // offender is a vehicle
  189.    %player = playerManager::vehicleIdToPlayerNum(%offender);   
  190.    
  191.    if(%player == 0)
  192.       return;  // it's the server
  193.    
  194.    say(%player, %player, "", "rules_violated.wav");
  195.    
  196.    // since whatever we do will kill the offender, avoid recursion   
  197.    resetLastHitBy(%offender);
  198.    
  199.    if(
  200.       (%player.teamKillWarn == true) &&   // already been warned
  201.       (%player != getConnectedClient())   // check to see if it is the hosting player
  202.    )
  203.    {  
  204.       // kick 'em
  205.       
  206.       // reset the warn flag first
  207.       %player.teamKillWarn = false;
  208.       
  209.       kick(%player, *IDMULT_CHAT_TEAM_KILL_KICK);
  210.    }
  211.    else
  212.    {
  213.       // set the warn flag first
  214.       %player.teamKillWarn = true;
  215.       
  216.       // kill the offender
  217.       damageObject(%offender, 1000000);
  218.       
  219.       // tell the player
  220.       messageBox(%player, *IDMULT_CHAT_TEAM_KILL_WARN);
  221.    }
  222. }
  223.  
  224.  
  225. function getNumberOfPlayersOnTeam(%bits)
  226. {
  227.    %name = getTeamNameFromTeamId(%bits);
  228.    
  229.    %count = 0;
  230.  
  231.    %playerCount = playerManager::getPlayerCount();
  232.     for (%p = 0; %p < %playerCount; %p++)
  233.     {
  234.         %player = playerManager::getPlayerNum(%p);
  235.         %team = getTeam(%player);
  236.       
  237.       if(%team == %name)
  238.       {
  239.          %count++;
  240.       }      
  241.    }
  242.    
  243.    return %count;
  244. }
  245.  
  246. //-----------------------------------------------
  247. // Zen Stuff
  248. // these are triggers that heal, reload, or both
  249. // only one player can use it at a time, and each time it is used, it
  250. // will "re-charge" for a certain number of seconds (optional).  The player
  251. // who landed on it is "special" and can stay as long as he/she wants.
  252. // As soon as this person leaves it, he/she is no longer "special".
  253.  
  254.  
  255. // player::onRemove must be run for triggers to handle "special" correctly
  256. // any script that overrides this function should specifically include this code
  257. function player::onRemove(%this)
  258. {
  259.    %vehicle = playerManager::playerNumToVehicleId(%this);
  260.    if(%vehicle != 0)
  261.    {
  262.       if(%vehicle.pad != "")
  263.       {
  264.          // reset the special for the pad this guy was on
  265.          %vehicle.pad.special = "";
  266.       }
  267.    }
  268. }
  269.  
  270. function Zen::onEnter(%trigger, %object, %message, %isReady, %countdown)
  271. {
  272.    // %trigger has been entered
  273.    // %object is the one who hit it
  274.    // %message is a string to display (could be empty).
  275.    // %isReady is a boolean -- if we report, do we tell if trigger is ready?
  276.    // %countdown is a boolean -- if we report, and we tell player if trigger
  277.    //   is ready, and trigger is not ready, do we tell player when trigger
  278.    //   will be ready?
  279.    
  280.    // in all cases, this function will only send one message to the player,
  281.    // since all of the above information will be concatenated.
  282.    
  283.    if(%message != "")
  284.    {
  285.       // the basic message
  286.       %output = %message;
  287.       
  288.       // check for which soundfile we should play
  289.       %soundFile = "";
  290.       if(%message == *IDMULT_CHAT_HEALPAD)
  291.       {
  292.          %soundFile = "repair_ent.wav";
  293.       }
  294.       else if(%message == *IDMULT_CHAT_AMMOPAD)
  295.       {
  296.          %soundFile = "reload_ent.wav";
  297.       }
  298.       
  299.       if(%isReady == true)
  300.       {
  301.          // make space for the next part
  302.          %output = %output @ " ";
  303.       
  304.          // see whether or not it is ready
  305.          if(
  306.             %trigger.wait == false ||
  307.             %trigger.wait == "" ||
  308.             (%trigger.reset <= getCurrentTime())    
  309.          )
  310.          {
  311.             // it's ready
  312.             %output = %output @ *IDMULT_READY; 
  313.          }
  314.          else
  315.          {
  316.             %output = %output @ *IDMULT_NOT_READY;
  317.             
  318.             // trigger isn't ready, but do we display the countdown
  319.             if(
  320.                %countdown == true &&   // display countdown?
  321.                %trigger.reset != ""