Advertisement
Guest User

Untitled

a guest
Aug 21st, 2014
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.28 KB | None | 0 0
  1. #include "rulesCore.as";
  2. #include "CTF_Structs";
  3.  
  4.  
  5. shared int decrementTickets(CRules@ this, int team){            //returns 1 if no tickets left, 0 otherwise
  6.     print("decrement tickets for team: "+team);
  7.     s16 numTickets;
  8.  
  9.     if(team==0){
  10.         numTickets=this.get_s16("blueTickets");
  11.         if(numTickets<=0)return 1;
  12.         print("number tickets: "+numTickets);
  13.         numTickets--;
  14.         this.set_s16("blueTickets", numTickets);
  15.         this.Sync("blueTickets", true);
  16. print("blue tickets: "+numTickets);
  17.         return 0;
  18.     }else if(team==1){
  19.         numTickets=this.get_s16("redTickets");
  20.         if(numTickets<=0)return 1;
  21.         print("number tickets: "+numTickets);
  22.         numTickets--;
  23.         this.set_s16("redTickets", numTickets);
  24.         this.Sync("redTickets", true);
  25. print("red tickets: "+numTickets);
  26.         return 0;
  27.     }
  28.     return 1;
  29. }
  30.  
  31.  
  32. shared bool isPlayersLeft(CRules@ this, int team){          //checks if spawning players or alive players
  33.  
  34.     /*RulesCore@ core;
  35.     this.get("core", @core);
  36.  
  37.     CTFTeamInfo@ teamInfo = cast<CTFTeamInfo@>(core.teams[team]);
  38.  
  39. print("alive players left: "+teamInfo.alive_count);
  40. print("spawning player left: "+teamInfo.spawns.length());
  41.  
  42.     if(teamInfo.spawns.length()>0) return true;
  43.     if(teamInfo.alive_count>0) return true;
  44.  
  45.     return false;*/
  46.  
  47.     if(team==0) return this.get_s8("aliveOrSpawningBlue")>0;
  48.     if(team==1) return this.get_s8("aliveOrSpawningRed")>0;
  49.    
  50.     return false;               //just incase
  51.  
  52. }
  53.  
  54. shared bool isSpawning( CRules@ this, CPlayer@ player ){
  55.     RulesCore@ core;
  56.     this.get("core", @core);
  57.     CTFPlayerInfo@ info = cast<CTFPlayerInfo@>(core.getInfoFromPlayer(player));
  58.  
  59.     for (uint i = 0; i < core.teams.length; i++){
  60.         CTFTeamInfo@ team = cast<CTFTeamInfo@>(core.teams[i]);
  61.         int pos = team.spawns.find(info);
  62.  
  63.         if (pos != -1) {
  64.             return true;
  65.         }
  66.     }
  67.     return false;
  68. }
  69.  
  70. shared s8 updateAliveOrSpawning(CRules@ this, int teamNum, s8 incrementBy){         //changes number of alive players on a team by incrementBy (can be negative)
  71.     s8 aliveOrSpawning=0;
  72.     if(teamNum==0){
  73.         aliveOrSpawning=this.get_s8("aliveOrSpawningBlue");
  74.         aliveOrSpawning=aliveOrSpawning+incrementBy;
  75.         //if(aliveOrSpawning<0)aliveOrSpawning=0;
  76.         this.set_s8("aliveOrSpawningBlue", aliveOrSpawning);
  77.         this.Sync("aliveOrSpawningBlue", true);
  78.     }else if(teamNum==1){
  79.         aliveOrSpawning=this.get_s8("aliveOrSpawningRed");
  80.         aliveOrSpawning=aliveOrSpawning+incrementBy;
  81.         //if(aliveOrSpawning<0)aliveOrSpawning=0;
  82.         this.set_s8("aliveOrSpawningRed", aliveOrSpawning);
  83.         this.Sync("aliveOrSpawningRed", true);
  84.     }
  85.     print("updated alive or spawning, team: "+teamNum+" players alive: "+aliveOrSpawning);
  86.     return aliveOrSpawning;
  87. }
  88.  
  89. shared bool teamPlayersAlive(CRules@ this, int teamNum)
  90. {
  91.     CBlob@[] team_blobs;
  92.  
  93.     CBlob@[] player_blobs;
  94.     getBlobsByTag( "player", @player_blobs );
  95.  
  96.     for ( uint i = 0; i < player_blobs.length; i++ )
  97.     {
  98.         CBlob@ blob = player_blobs[i];
  99.         if (blob !is null)
  100.         {
  101.             int team_num = blob.getTeamNum();
  102.             if (team_num == teamNum)
  103.             {
  104.                 team_blobs.push_back(blob);
  105.             }
  106.             else
  107.             {
  108.                 // do nothing
  109.             }
  110.         }
  111.     }
  112.     return team_blobs.length > 0? true : false;
  113. }
  114.  
  115. shared bool checkGameOver(CRules@ this, int teamNum)
  116. {
  117.     bool blue_alive = teamPlayersAlive(this, 0);
  118.     bool red_alive = teamPlayersAlive(this, 1);
  119.  
  120.     /*if (!isPlayersLeft(this, teamNum)) //check if any players left alive or are spawning
  121.     {*/
  122.         if (teamNum == 1 && !red_alive) //teams hardcoded -> if one is dead, other wins (no consideration for more teams)
  123.         {
  124.             print("checking red tickets left: " + this.get_s16("redTickets"));
  125.             if (this.get_s16("redTickets") > 0)
  126.             {
  127.                 return false;
  128.             }
  129.             else
  130.             {
  131.                 this.SetTeamWon( 0 ); //game over!
  132.                 this.SetCurrentState(GAME_OVER);
  133.                 this.SetGlobalMessage( this.getTeam(0).getName() + " wins the game!" );
  134.                 return true;
  135.             }
  136.         }
  137.         else if (teamNum == 0 && !blue_alive) //blue
  138.         {
  139.             print("checking blue tickets left: " + this.get_s16("blueTickets"));
  140.             if (this.get_s16("blueTickets")>0)
  141.             {
  142.                 return false;
  143.             }
  144.             else
  145.             {
  146.                 this.SetTeamWon( 1 ); //game over!
  147.                 this.SetCurrentState(GAME_OVER);
  148.                 this.SetGlobalMessage( this.getTeam(1).getName() + " wins the game!" );
  149.                 return true;
  150.             }
  151.         }
  152.     //}
  153.     return false;
  154. }
  155.  
  156. shared void addTicketSpawn(CRules@ this, CPlayer@ player){
  157. print("player requesting spawn: "+player.getUsername());
  158.  
  159.     int teamNum=player.getTeamNum();
  160.  
  161.     if(!isSpawning(this, player)){              //if they are already spawning, dont remove a ticket, but let them readd to the spawn list
  162. print("player requesting spawn accepted: "+player.getUsername());
  163.  
  164.         if(this.isMatchRunning()){                      //check if build time
  165.             if(decrementTickets(this, teamNum)==1){             //decrementTickets() returns 1 if team out of units
  166. print("players left: "+isPlayersLeft(this, teamNum));
  167.                 checkGameOver(this, teamNum);
  168.                 return;
  169.             }
  170.         }
  171.     }
  172.  
  173.     //only reaches here if player should spawn - code taken from CoreHooks.as and commented out in the original position
  174.     //this code taken from CoreHooks.as, and commented out in its original position
  175. print("spawning the player");
  176.     if(!getNet().isServer())
  177.         return;
  178.  
  179.     RulesCore@ core;
  180.     this.get("core", @core);
  181.  
  182.     if (core !is null) {
  183.         if(!isSpawning(this, player)) updateAliveOrSpawning(this, teamNum, 1);
  184.         core.AddPlayerSpawn(player);
  185.     }
  186.  
  187.     //print("numberPlayers: "+aliveOrSpawningRed+" "+aliveOrSpawningBlue);
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement