Base to flag

Version:

1.0

Added on:

14 Mar 2013 16:58

Tags:

Description:
This is a code that works only for CTF or death CTF. When a player carries the red/blue flag and dies, it will move the red/blue base to the player instead of the flag back to the red/blue base. Because the server has to have a different way of detecting when a player loses the flag, there can be an error in the position of the bases. This is why this script doesn't work well for open levels like JE. Still, it can be fun to change the overplayed gamemode CTF a bit :)
  1. /***Constants***/
  2. enum BASE {
  3.   BLUE = 0,
  4.   RED = 1
  5. }
  6.  
  7. const int PLAYSLOT = 32768;
  8.  
  9. /***Variables***/
  10. int BlueBase = -1;
  11. int RedBase = -1;
  12. bool RedKill = false;
  13. bool BlueKill = false;
  14. int RedFlagHolder = -1;
  15. int BlueFlagHolder = -1;
  16.  
  17.  
  18. /***Other functions***/
  19. void ChangeBase(int playerID) {
  20.   //Create a new base
  21.   int NewBase = jjAddObject(OBJECT::CTFBASE, jjPlayers[playerID].xPos, jjPlayers[playerID].yPos);
  22.  
  23.   //Change the color to red if the player lost the red flag
  24.   if (!jjPlayers[playerID].teamRed)
  25.     jjObjects[NewBase].var[1] = BASE::RED;
  26.  
  27.   //Change the global base variable to the new one
  28.   if (jjObjects[NewBase].var[1] == BASE::BLUE)
  29.     BlueBase = NewBase;
  30.   else
  31.     RedBase = NewBase;
  32.  
  33.   //Remove the old base, also if there are any bugged extra bases it will also remove them
  34.   for (int i = 0; i < jjObjectCount; i++)
  35.     if (jjObjects[i].isActive && jjObjects[i].eventID == 244)
  36.       if (jjObjects[i].var[1] == BASE::BLUE && i != BlueBase)
  37.         jjDeleteObject(i);
  38.       else if (jjObjects[i].var[1] == BASE::RED && i != RedBase)
  39.         jjDeleteObject(i);
  40. }
  41.  
  42. void InitBaseID() {
  43.   for (int i = 0; i < jjObjectCount; i++)
  44.     if (jjObjects[i].isActive && jjObjects[i].eventID == 244)
  45.       if (jjObjects[i].var[1] == 0)
  46.         BlueBase = i;
  47.       else
  48.         RedBase = i;
  49. }
  50.  
  51.  
  52. /***Gameplay functions***/
  53. void onMain() {
  54.   if (jjGameTicks == 1)
  55.     InitBaseID();
  56.  
  57.   if (jjGameState == GAME::STARTED) {
  58.  
  59.   //Detect when a flag is captured and change flagvar to 2 for the player that captured the flag
  60.   if (jjObjects[BlueBase].var[0] > 0 && BlueFlagHolder == -1) {
  61.     BlueFlagHolder = jjObjects[BlueBase].var[0] - PLAYSLOT;
  62.     BlueKill = false;
  63.   }
  64.   if (jjObjects[RedBase].var[0] > 0 && RedFlagHolder == -1) {
  65.     RedFlagHolder = jjObjects[RedBase].var[0] - PLAYSLOT;
  66.     RedKill = false;
  67.   }
  68.  
  69.   //Check if the flag holder is killed (CLIENT ONLY)
  70.   if (jjObjects[BlueBase].var[0] > 0) {
  71.     if (jjPlayers[BlueFlagHolder].health == 0 && jjPlayers[BlueFlagHolder].isActive)
  72.       BlueKill = true;
  73.   }
  74.   if (jjObjects[RedBase].var[0] > 0) {
  75.     if (jjPlayers[RedFlagHolder].health == 0 && jjPlayers[RedFlagHolder].isActive)
  76.       RedKill = true;
  77.   }
  78.  
  79.   //Check if the flag is back to base, if so: is it because of a score or player kill?
  80.   if (jjObjects[BlueBase].var[0] == 0 && BlueFlagHolder > -1) {
  81.     if ((!jjIsServer && BlueKill) || (jjIsServer && jjPlayers[BlueFlagHolder].flag == 0)) { //The server uses the .flag variable of a player to see if the player died with the flag, the client uses .health instead
  82.       if (jjPlayers[BlueFlagHolder].isActive) //We dont want to lose the base when someone is ctoĆ­ng :D
  83.         ChangeBase(BlueFlagHolder);
  84.       BlueKill = false;
  85.     } else {
  86.       jjObjects[RedBase].counter = 0;
  87.       jjObjects[RedBase].var[4] = 1;
  88.     }
  89.     BlueFlagHolder = -1;
  90.   }
  91.   if (jjObjects[RedBase].var[0] == 0 && RedFlagHolder > -1) {
  92.     if ((!jjIsServer && RedKill) || (jjIsServer && jjPlayers[RedFlagHolder].flag == 0)) {
  93.       if (jjPlayers[RedFlagHolder].isActive)
  94.         ChangeBase(RedFlagHolder);
  95.       RedKill = false;
  96.     } else{
  97.       jjObjects[BlueBase].counter = 0;
  98.       jjObjects[BlueBase].var[4] = 1;
  99.     }
  100.     RedFlagHolder = -1;
  101.   }
  102.  
  103.   //Also, swapping while holding the flag is not a bug, just a good troll!  (A.k.a. im too lazy to fix that)
  104.   }
  105. }