home *** CD-ROM | disk | FTP | other *** search
/ Hacker 9 / HACKER09.ISO / Games / StarSiege.exe / Starsiege / Missions.vol / CTF_Stab_n_Grab.cs next >
Text File  |  1998-07-27  |  14KB  |  480 lines

  1. //
  2. // ctfStdLib.cs 
  3. //
  4. // Simple capture the flag routines
  5. //
  6.  
  7. $maxFlagCount = 5;           // no of flags required by a team to end the game
  8. $flagValue    = 25;          // points your team gets for capturing
  9. $carrierValue =  5;          //  "      "    "    "    " killing carrier
  10. $ctfWon = 0;
  11.  
  12. //--------------------------------------------------------------------------------
  13.  
  14. function setDefaultMissionOptions()
  15. {
  16.    $server::TeamPlay = true;
  17.    $server::AllowDeathmatch = false;
  18.    $server::AllowTeamPlay = true;    
  19. }
  20.  
  21. //--------------------------------------------------------------------------------
  22.  function onMissionStart()
  23. {
  24.     initGlobalVars();
  25. }
  26.  
  27. function player::onAdd(%this)
  28. {
  29.    chat(%this, 0, strcat("Welcome to CTF, ", getName(%this), ". Here are the rules:"));
  30.    chat(%this, 0, "- Your flag must be at home for a capture!");
  31.    chat(%this, 0, strcat("- Captures are worth ", $flagValue, " points"));
  32.    chat(%this, 0, strcat("- Flag carriers are worth ", $carrierValue, " points"));
  33.    chat(%this, 0, strcat("- Generic kills are worth ZERO points"));
  34.    chat(%this, 0, strcat("- First team to get ", $maxFlagCount, " flags wins."));
  35.  
  36.    %color    = teamToColor(getTeam(%this));
  37.    %colorKey = strcat(%color, "FlagCarried");
  38.    %teamPlayerCount = getTeamPlayerCount(getTeam(%this));
  39.  
  40.    // if no one has players flag, and they are the only player of that color
  41.    // setFlag to true   
  42.    if(!dataRetrieve(0,%colorKey) && %teamPlayerCount <= 1)
  43.    {
  44.            setFlag(%color, true );
  45.    }
  46. }
  47.  
  48. //--------------------------------------------------------------------------------
  49. function player::onRemove(%this)
  50. {
  51.     %teamPlayerCount = getTeamPlayerCount(getTeam(%this));
  52.        if(%teamPlayerCount <= 1)
  53.     {
  54.         %color = teamToColor(getTeam(%this));
  55.         setFlag(%color, false);                    
  56.     }
  57. }
  58.  
  59.  
  60. //--------------------------------------------------------------------------------
  61. function vehicle::onAdd(%this)
  62. {
  63.     %color = teamToColor(getTeam(%this));
  64.     %flagKey = strcat(%color, "FlagCount");
  65.  
  66.     // if the flag isn't at the base, but no one has it, correct situation
  67.     if(dataRetrieve(0, %flagKey) && !dataRetrieve(0, strcat(%color, "FlagCarried")))
  68.     {
  69.         setFlag(%color, true);
  70.     }
  71. }
  72.  
  73. //--------------------------------------------------------------------------------
  74. function vehicle::onDestroyed(%destroyed,%destroyer)
  75. {
  76.    // if the destroyed vehicle has a flag, it goes back to it's base
  77.    %color = dataRetrieve(%destroyed, "hasFlag");
  78.    if (%color != "") {
  79.       playerDropsFlag(%destroyed);
  80.       // let everyone know this guy drops the flag
  81.       wallDim(strcat(getName(%destroyed), " surrenders the ", %color, " flag"));
  82.  
  83.       // destroyer's team gets credit for killing the carrier
  84.       %key = strcat(getTeam(%destroyer), "CarriersKilled");
  85.       dataStore(0, %key, 1 + dataRetrieve(0, %key));
  86.    }
  87. }
  88.  
  89. //--------------------------------------------------------------------------------
  90. function setFlag(%color, %bool)
  91. {
  92.     %flagCountKey = strcat(%color, "FlagCount");
  93.     
  94.     // set flag to visible
  95.     if(%bool)
  96.     {
  97.            dataStore(0, %flagCountKey, 0);    
  98.            setShapeVisibility(getObjectId(strcat("MissionGroup\\Scenario\\", %color, "Base\\", %color, "Flag")), true);                
  99.     }
  100.     
  101.     // set flag to non-visible
  102.     else
  103.     {
  104.         dataStore(0, %flagCountKey, 1);
  105.         setShapeVisibility(getObjectId(strcat("MissionGroup\\Scenario\\", %color, "Base\\", %color, "Flag")),false);    
  106.     }
  107. }
  108.  
  109. function playerDropsFlag(%vehicle)
  110. {
  111.    // figure out which color of flag the guy is carrying
  112.    %color = dataRetrieve(%vehicle, "hasFlag");
  113.    // the player is no longer carrying this or any flag
  114.    dataRelease(%vehicle, "hasFlag");
  115.    setVehicleSpecialIdentity(%vehicle, false);
  116.    dataStore(0, strcat(%color, "FlagCarried"), 0);
  117.    
  118.    %teamPlayerCount = getTeamPlayerCount(colorToTeam(%color));
  119.    if(%teamPlayerCount < 1)
  120.    {
  121.            setFlag(%color, false);    
  122.    }    
  123.    else
  124.    {    
  125.            setFlag(%color, true);   
  126.    }
  127. }
  128.  
  129.  
  130. //--------------------------------------------------------------------------------
  131.  
  132. function checkFlagRetrieved(%atColor, %vehicle)
  133. {
  134.    // is the game over already?
  135.    if ($ctfWon != 0) 
  136.       { return; }
  137.  
  138.    // you made it back to your own base, do you have any other teams' flags?
  139.    if (dataRetrieve(%vehicle, "hasFlag") == "") 
  140.       { return; }
  141.  
  142.    // is the player's team flag at his base when he brings back the enemy's flag?
  143.    if (dataRetrieve(0, strcat(%atColor, "FlagCount")) != 0) 
  144.       { return; }
  145.  
  146.    // player is no longer carrying the flag
  147.    %hasColor = dataRetrieve(%vehicle, "hasFlag");
  148.    playerDropsFlag(%vehicle);
  149.  
  150.    // player's team gets credit for stealing the flag
  151.    %collectedKey = strcat(getTeam(%vehicle), "FlagsCollected");
  152.    %collectedCount = dataRetrieve(0, %collectedKey);
  153.    %collectedCount = %collectedCount + 1;
  154.    dataStore(0, %collectedKey, %collectedCount);
  155.  
  156.    // let everyone know what happened
  157.    %needMore = $maxFlagCount - %collectedCount;
  158.    if (%needMore == 0) {
  159.       wallDim(strcat(getName(%vehicle), " captured the ", %hasColor, " flag!"));
  160.       $ctfWon = 1;
  161.       schedule("missionEndConditionMet();", 5.0);
  162.       winEvent(getTeam(%vehicle));
  163.       playerDropsFlag(%vehicle);
  164.    }
  165.    else {
  166.       wallDim(strcat(getName(%vehicle), " captured the ", %hasColor, " flag! ", getTeam(%vehicle), " needs ", %needMore, " more to win"));
  167.    }
  168. }
  169.  
  170.  
  171. //--------------------------------------------------------------------------------
  172. function initGlobalVars()
  173. {
  174.     dataStore(0, "BlueFlagsCollected", 0);
  175.     dataStore(0, "RedFlagsCollected", 0);
  176.     dataStore(0, "YellowFlagsCollected", 0);
  177.     dataStore(0, "PurpleFlagsCollected", 0);
  178.  
  179.     dataStore(0, "blueFlagCount", 1);
  180.     dataStore(0, "redFlagCount", 1);
  181.     dataStore(0, "yellowFlagCount", 1);
  182.     dataStore(0, "purpleFlagCount", 1);
  183.  
  184.     dataStore(0, "yellowFlagCarried", 0);
  185.     dataStore(0, "blueFlagCarried", 0);
  186.     dataStore(0, "redFlagCarried", 0);
  187.     dataStore(0, "purpleFlagCarried", 0);
  188.  
  189.     setShapeVisibility(getObjectId("MissionGroup\\Scenario\\yellowBase\\yellowFlag"), false);
  190.       setShapeVisibility(getObjectId("MissionGroup\\Scenario\\blueBase\\blueFlag"), false);
  191.       setShapeVisibility(getObjectId("MissionGroup\\Scenario\\redBase\\redFlag"), false);
  192.       setShapeVisibility(getObjectId("MissionGroup\\Scenario\\purpleBase\\purpleFlag"), false);
  193. }
  194.  
  195.     
  196.  
  197. function winEvent(%Team)
  198. {
  199.     %r = %g = %b = 0;
  200.  
  201.     if(%Team == Yellow)
  202.     {
  203.         %r = %g = 0.5;
  204.     }
  205.     
  206.     if(%Team == Blue)
  207.     {
  208.         %b = 0.5;
  209.     }
  210.     
  211.     if(%Team == Red)
  212.     {
  213.         %r = 0.5;
  214.     }
  215.     
  216.     if(%Team == Purple)
  217.     {
  218.         %r = 0.345;
  219.         %g = 0.1254;
  220.         %b = 0.254;
  221.     }
  222.  
  223.     %count = playerManager::getPlayerCount();
  224.     for(%i = 0; %i < %count; %i = %i +1)
  225.     {
  226.         %curPlayerNum = playerManager::getPlayerNum(%i);
  227.         messageBox(%curPlayerNum, strcat("GAME OVER! \n", %Team, " won the game!"));
  228.         fadeEvent(%curPlayerNum, out, 6.0, %r, %g, %b);
  229.     }
  230. }
  231.  
  232.  
  233. function checkFlagStolen(%color, %vehicle)
  234. {
  235.    // is the game over already?
  236.    if ($ctfWon != 0)
  237.       { return; }
  238.           
  239.    // you just hit the trigger at an enemy base, is there a flag here?
  240.    %flagCountKey = strcat(%color, "FlagCount");
  241.    %flagCount = dataRetrieve(0, %flagCountKey);
  242.    
  243.    echo(%flagCount);
  244.    if (%flagCount != 0 || dataRetrieve(%vehicle, "hasFlag") != "") 
  245.    { return; }
  246.  
  247.    if (getVehicleName(%vehicle) == "none")
  248.       { return; }
  249.  
  250.    // stop the flag shape from rendering
  251.    setShapeVisibility(getObjectId(strcat("MissionGroup\\Scenario\\", %color, "Base\\", %color, "Flag")), false);
  252.  
  253.    // setup the vehicle to carry the flag
  254.    setVehicleSpecialIdentity(%vehicle, true, %color);
  255.    dataStore(%vehicle, "hasFlag", %color);
  256.  
  257.    // increment flag count so nobody else can pickup this flag
  258.    dataStore(0, %flagCountKey, 1);
  259.  
  260.    // set global bool for flag of that color
  261.    dataStore(0, strcat(%color, "FlagCarried"), 1); 
  262.  
  263.    // let everyone know what happened
  264.    wallDim(strcat(getName(%vehicle), " STOLE the ", %color, " flag"));
  265. }
  266.  
  267. //-------------------------------------------------------------------------------
  268. function colorToTeam(%color)
  269. {
  270.     if(%color == "yellow")
  271.     { return "Yellow";}
  272.     if(%color == "blue")
  273.     {return "Blue";}
  274.     if(%color == "red")
  275.     {return "Red";}
  276.     if(%color == "purple")
  277.     {return "Purple";}
  278.  
  279.     return 0;
  280. }
  281. function teamToColor(%team)
  282. {
  283.     if(%team == "Yellow")
  284.     {return "yellow";}
  285.     if(%team == "Blue")
  286.     {return "blue";}
  287.     if(%team == "Red")
  288.     {return "red";}
  289.     if(%team == "Purple")
  290.     {return "purple";}
  291.  
  292.     return 0;
  293. }   
  294.  
  295. //--------------------------------------------------------------------------------
  296. function getTeamPlayerCount(%team)
  297. {
  298.     %count = playerManager::getPlayerCount();
  299.     %teamPlayerCount = 0;
  300.  
  301.     for(%i = 0; %i < %count; %i = %i + 1 )
  302.     {
  303.         %curPlayerNum = playerManager::getPlayerNum(%i);
  304.         if(getTeam(%curPlayerNum) == %team)
  305.         {
  306.             %teamPlayerCount = %teamPlayerCount + 1;
  307.         }    
  308.     }
  309.     return %teamPlayerCount;        
  310. }
  311.  
  312. //--------------------------------------------------------------------------------
  313. function initFlagTrigger(%name, %color)
  314. {
  315.    %flagCountKey = strcat(%color, "FlagCount");
  316.    dataStore(0, %flagCountKey, 1);
  317.    %flagsCollectedKey = strcat(%name, "FlagsCollected");
  318.    dataStore(0, %flagsCollected, 0);
  319.    %carriersKilledKey = strcat(%name, "CarriersKilled");
  320.    dataStore(0, %carriersKilledKey, 0);
  321. }
  322.  
  323. //--------------------------------------------------------------------------------
  324. function flagTriggerOnEnter(%colorName, %color, %object)
  325. {
  326.    %flagKey = strcat(%color, "FlagCount");
  327.    %teamPlayerCount = getTeamPlayerCount(%colorName);
  328.  
  329.    if(%teamPlayerCount < 1 && dataRetrieve(0, %flagKey) != 1)
  330.    {
  331.            %flagKey = strcat(%color, "FlagCount");
  332.            dataStore(0, %flagKey, 1);
  333.         setShapeVisibility(getObjectId(strcat("MissionGroup\\Scenario\\", %color, "Base\\", %color, "Flag")),false);                    
  334.            wallDim(strcat("The ", %color, " flag vanishes!"));
  335.    }
  336.  
  337.    if (getTeam(%object) != %colorName) {
  338.       checkFlagStolen(%color, %object);
  339.    }
  340.    else {
  341.       checkFlagRetrieved(%color, %object);          
  342.    }
  343. }
  344.  
  345. //--------------------------------------------------------------------------------
  346.  
  347. //--------------------------------------------------------------------------------
  348.  
  349. // Team color specific code
  350.  
  351. // Yellow
  352. function yellowFlagTrigger::trigger::onAdd(%this)
  353. {
  354.    initFlagTrigger("Yellow", "yellow");
  355. }
  356. function yellowFlagTrigger::trigger::onEnter(%this, %object)
  357. {
  358.    flagTriggerOnEnter("Yellow", "yellow", %object);
  359. }
  360.  
  361. // Blue
  362. function blueFlagTrigger::trigger::onAdd(%this)
  363. {
  364.    initFlagTrigger("Blue", "blue");
  365. }
  366. function blueFlagTrigger::trigger::onEnter(%this, %object)
  367. {
  368.    flagTriggerOnEnter("Blue", "blue", %object);
  369. }
  370.  
  371. // Red
  372. function redFlagTrigger::trigger::onAdd(%this)
  373. {
  374.    initFlagTrigger("Red", "red");
  375. }
  376. function redFlagTrigger::trigger::onEnter(%this, %object)
  377. {
  378.    flagTriggerOnEnter("Red", "red", %object);
  379. }
  380.  
  381. // Purple
  382. function purpleFlagTrigger::trigger::onAdd(%this)
  383. {
  384.    initFlagTrigger("Purple", "purple");
  385. }
  386. function purpleFlagTrigger::trigger::onEnter(%this, %object)
  387. {
  388.    flagTriggerOnEnter("Purple", "purple", %object);
  389. }
  390.  
  391. //--------------------------------------------------------------------------------
  392.  
  393. //--------------------------------------------------------------------------------
  394.  
  395. // Scoreboard code
  396.  
  397. function getTeamCarriersKilled(%a)
  398. {
  399.    %key = strcat(getTeamNameFromTeamId(%a), "CarriersKilled");
  400.    return dataRetrieve(0, %key);
  401. }
  402.  
  403. function getPlayerCarriersKilled(%a)
  404. {
  405.    %key = strcat(getTeam(%a), "CarriersKilled");
  406.    return dataRetrieve(0, %key);
  407. }
  408.  
  409. function getTeamFlags(%a)
  410. {
  411.    %collectedKey = strcat(getTeamNameFromTeamId(%a), "FlagsCollected");
  412.    %flags = dataRetrieve(0, %collectedKey);
  413.    if (%flags == "") {
  414.       return 0;
  415.    }
  416.    else {
  417.       return %flags;
  418.    }
  419. }
  420.  
  421. function getPlayerFlags(%a)
  422. {
  423.    %collectedKey = strcat(getTeam(%a), "FlagsCollected");
  424.    %flags = dataRetrieve(0, %collectedKey);
  425.    if (%flags == "") {
  426.       return 0;
  427.    }
  428.    else {
  429.       return %flags;
  430.    }
  431. }   
  432.  
  433. function getPlayerScore(%a)
  434. {
  435.    return(getPlayerFlags(%a) * $flagValue + getPlayerCarriersKilled(%a) * $carrierValue);
  436. }
  437.  
  438. function getTeamScore(%a)
  439. {
  440.    return(getTeamFlags(%a) * $flagValue + getTeamCarriersKilled(%a) * $carrierValue);
  441. }
  442.  
  443. function initScoreBoard()
  444. {
  445.    deleteVariables("$ScoreBoard::PlayerColumn*");
  446.    deleteVariables("$ScoreBoard::TeamColumn*");
  447.  
  448.    // Player ScoreBoard column headings
  449.    $ScoreBoard::PlayerColumnHeader1 = "TEAM";
  450.    $ScoreBoard::PlayerColumnHeader2 = "FLAGS";
  451.    $ScoreBoard::PlayerColumnHeader3 = "SCORE";
  452.    $ScoreBoard::PlayerColumnHeader4 = "CARRIER KILLS";
  453.    $ScoreBoard::PlayerColumnHeader5 = "PING";
  454.  
  455.    // Player ScoreBoard column functions
  456.    $ScoreBoard::PlayerColumnFunction1 = "getTeam";
  457.    $ScoreBoard::PlayerColumnFunction2 = "getPlayerFlags";
  458.    $ScoreBoard::PlayerColumnFunction3 = "getPlayerScore";
  459.    $ScoreBoard::PlayerColumnFunction4 = "getPlayerCarriersKilled";
  460.    $ScoreBoard::PlayerColumnFunction5 = "getPing";
  461.  
  462.    //Team ScoreBoard column headings
  463.    $ScoreBoard::TeamColumnHeader1 = "FLAGS";
  464.    $ScoreBoard::TeamColumnHeader2 = "SCORE";
  465.    $ScoreBoard::TeamColumnHeader3 = "CARRIERS KILLED";
  466.  
  467.   
  468.    // Team ScoreBoard column functions
  469.    $ScoreBoard::TeamColumnFunction1 = "getTeamFlags";
  470.    $ScoreBoard::TeamColumnFunction2 = "getTeamScore";
  471.    $ScoreBoard::TeamColumnFunction3 = "getTeamCarriersKilled";
  472.  
  473.    // tell server to process all the scoreboard definitions defined above
  474.    serverInitScoreBoard();   
  475. }
  476.  
  477. //--------------------------------------------------------------------------------
  478.  
  479.  
  480.