Advertisement
PsyOps

BaseFlagging

May 22nd, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.16 KB | None | 0 0
  1. package twcore.bots.battlebot.PublicOps.Flags;
  2.  
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import java.util.Random;
  9.  
  10. import twcore.bots.battlebot.ChatBuffer;
  11. import twcore.bots.battlebot.DoorMan;
  12. import twcore.bots.battlebot.PublicOps.PublicPlayer;
  13. import twcore.core.BotAction;
  14. import twcore.core.events.PlayerDeath;
  15. import twcore.core.events.PlayerPosition;
  16. import twcore.core.game.Player;
  17.  
  18. public class BaseFlagging {
  19.     BotAction b;
  20.     ChatBuffer bzChat;
  21.     DoorMan bob;
  22.    
  23.     public BaseFlagging(BotAction b, ChatBuffer c, DoorMan bob)
  24.     {
  25.         this.b = b;
  26.         this.bzChat = c;
  27.         this.bob = bob;
  28.     }
  29.    
  30.     /*-------------------------------------------------------------------------------\
  31.      *
  32.      *                             NEEDED VARS
  33.      *                            
  34.      *-------------------------------------------------------------------------------*/
  35.     private boolean m_GameOn = false;
  36.     public boolean GameOn(){ return this.m_GameOn; }
  37.    
  38.     private long m_GameStartTimeStamp;
  39.    
  40.     // Flag lvz
  41.     private int m_LvzOn = 1428;
  42.     private int m_LvzOff = 1427;
  43.    
  44.     // Door to toggle on and off
  45.     private byte m_DoorNumber = 5;
  46.    
  47.     // Map room coords
  48.     private short[] m_MapRoom1 = new short[]{ 225 * 16, 778 * 16, 499 * 16, 846 * 16};
  49.     private short[] m_MapRoom2 = new short[]{ 499 * 16, 792 * 16, 785 * 16, 846 * 16};
  50.     private short[] m_FlagHotSpot = new short[]{ 12336, 13328, 12496, 13488 };
  51.    
  52.     // Settings for timer
  53.     private FlagTimer m_StartGameSet;
  54.     // settings for game timer
  55.     private FlagTimer m_GameTimerSet;
  56.    
  57.     // If base has initially been raided
  58.     private boolean m_BeenRaided = false;
  59.     private String m_WhoRaided = "~nobody~";
  60.     private short m_FreqRaided = 8025;
  61.     private long m_RaidTime = 0;
  62.    
  63.     // Freq that owns the flag
  64.     private short m_BaseOwner = 8025;
  65.     private long m_BaseTimestamp;
  66.    
  67.     /*-------------------------------------------------------------------------------\
  68.      *
  69.      *                             SUBSPACE EVENTS
  70.      *                            
  71.      *-------------------------------------------------------------------------------*/
  72.     public void PlayerPositionEvent(PlayerPosition p)
  73.     {
  74.         // If there is no game check for stragglers in the area
  75.          if (!m_GameOn)
  76.          {
  77.              // Flag room
  78.             if (InRegion(p,m_MapRoom1)||InRegion(p,m_MapRoom2))
  79.             {
  80.                 // Warp them back
  81.                 b.sendPrivateMessage(p.getPlayerID(), "There are no matches at this time and you are not authorized to be in this area.");
  82.                 b.warpTo(p.getPlayerID(), 512,512);
  83.             }
  84.             // No need to track flag hotspot unless game has started
  85.             return;
  86.          }
  87.          
  88.         // Flag HOTSPOT - If a new freq has taken possesion
  89.         if (InRegion(p, m_FlagHotSpot) && b.getPlayer(p.getPlayerID()).getFrequency() != m_BaseOwner)
  90.             baseRaided(p);
  91.     }
  92.     public void PlayerDeathEvent(PlayerDeath pd)
  93.     {
  94.         if (!m_GameOn) return;
  95.         short killer_x = b.getPlayer(pd.getKillerID()).getXLocation();
  96.         short killer_y = b.getPlayer(pd.getKillerID()).getYLocation();
  97.         short killed_x = b.getPlayer(pd.getKilleeID()).getXLocation();
  98.         short killed_y = b.getPlayer(pd.getKilleeID()).getYLocation();
  99.        
  100.         if (InRegion(killer_x,killer_y,m_MapRoom1) ||
  101.                 InRegion(killer_x,killer_y,m_MapRoom2) ||
  102.                 InRegion(killed_x,killed_y,m_MapRoom1) ||
  103.                 InRegion(killed_x,killed_y,m_MapRoom2))
  104.         {
  105.             // Get basing freqs
  106.             BaseFreq killerF = getBaseFreq(b.getPlayer(pd.getKillerID()).getFrequency());
  107.             BaseFreq killedF = getBaseFreq(b.getPlayer(pd.getKilleeID()).getFrequency());
  108.             // Get players
  109.             BasePlayer killer = killerF.getPlayer(b.getPlayerName(pd.getKillerID()));
  110.             BasePlayer killed = killedF.getPlayer(b.getPlayerName(pd.getKilleeID()));
  111.            
  112.             if (killerF.getFreq() == killedF.getFreq())
  113.             {
  114.                 // TK Code
  115.                 return;
  116.             }
  117.             else
  118.             {
  119.                 killer.KilledPlayer();
  120.                 killed.Died();
  121.                
  122.                 if (killer.getMultis() > 1)
  123.                 {
  124.                     updateMultiKillPlayer(killer);
  125.                 }
  126.             }
  127.             b.sendArenaMessage("FlagDeath Event");
  128.         }
  129.     }
  130.     /*-------------------------------------------------------------------------------\
  131.      *
  132.      *                             MultiKills
  133.      *                            
  134.      *-------------------------------------------------------------------------------*/
  135.     private List<MultiMessageEvent> m_MultiKillMessages = new ArrayList<MultiMessageEvent>();
  136.    
  137.     private void updateMultiKillList()
  138.     {
  139.         if (m_MultiKillMessages.isEmpty()) return;
  140.        
  141.         if (m_MultiKillMessages.get(0).timeToSend())
  142.         {
  143.             b.sendArenaMessage(m_MultiKillMessages.get(0).getMultiMessage(), 14);
  144.             Timers.remove(0);
  145.         }
  146.     }
  147.    
  148.     private void updateMultiKillPlayer(BasePlayer BPlayer)
  149.     {
  150.         for (MultiMessageEvent m: m_MultiKillMessages)
  151.         {
  152.             if (m.getPlayerName() == BPlayer.getPlayerName())
  153.             {
  154.                 m.updateMultiPlayer(BPlayer.getMultis());
  155.                 return;
  156.             }
  157.         }
  158.        
  159.         m_MultiKillMessages.add( new MultiMessageEvent(BPlayer.getPlayerName(),BPlayer.getMultis()));
  160.         bzChat.debugMessage("Adding multi message to queue.");
  161.     }
  162.    
  163.     private class MultiMessageEvent
  164.     {
  165.         public MultiMessageEvent(String PlayerName, int Kills)
  166.         {
  167.             this.m_PlayerName = PlayerName;
  168.             this.m_Kills = Kills;
  169.         }
  170.        
  171.         private String m_PlayerName;
  172.         public String getPlayerName(){ return m_PlayerName; }
  173.        
  174.         private long m_Timestamp = System.currentTimeMillis();
  175.         public void updateMultiPlayer(int Kills)
  176.         {
  177.             this.m_Kills = Kills;
  178.             this.m_Timestamp = System.currentTimeMillis();
  179.         }
  180.        
  181.         private int m_Kills;
  182.        
  183.         public boolean timeToSend(){    return (System.currentTimeMillis() - m_Timestamp > 6500) ? true: false; }
  184.        
  185.         public String getMultiMessage()
  186.         {
  187.             return "Multikill for [ "+m_PlayerName+" ]! Kills[ "+m_Kills+" ] ";
  188.         }
  189.     }
  190.    
  191.     /*-------------------------------------------------------------------------------\
  192.      *
  193.      *                             BASE FLAG TIMER
  194.      *                            
  195.      *-------------------------------------------------------------------------------*/
  196.     // List of timed events
  197.     private List<FlagTimer> Timers = new ArrayList<FlagTimer>();
  198.    
  199.     // main timer for game
  200.     public void BaseTimer()
  201.     {
  202.         if ( !m_MultiKillMessages.isEmpty() && m_MultiKillMessages.get(0).timeToSend())
  203.         {
  204.             b.sendArenaMessage(m_MultiKillMessages.get(0).getMultiMessage(), 14);
  205.             Timers.remove(0);
  206.         }
  207.         //updateMultiKillList();
  208.         // sort through timers and update
  209.         for (int i=Timers.size()-1; i> -1; i--)
  210.         {
  211.             if (!Timers.get(i).TimerExpired() )
  212.                 Timers.get(i).UpdateTimer(b, bzChat);
  213.             else
  214.             {
  215.                 doTimerTask(Timers.get(i).getTimerTask());
  216.                 Timers.remove(i);
  217.             }
  218.         }
  219.     }
  220.     // Timer tasks
  221.     private void doTimerTask(int Task)
  222.     {
  223.         switch(Task)
  224.         {
  225.             case 1:
  226.                 doGameTimer();
  227.                 break;
  228.             case 2:
  229.                 doGameOver();
  230.                 break;
  231.         }
  232.     }
  233.     /*-------------------------------------------------------------------------------\
  234.      *
  235.      *                             MAIN FUNCTIONS
  236.      *                            
  237.      *-------------------------------------------------------------------------------*/
  238.     public void m_StartTimer()
  239.     {
  240.         // ## Debug ##
  241.         bzChat.debugMessage("  -  [T20 Attack] Starting timer for Game start.");
  242.        
  243.         // Record the start to timer
  244.         m_StartGameSet.StartTimer();
  245.         // add to our timer list
  246.         Timers.add(m_StartGameSet);
  247.     }
  248.     private void doGameTimer()
  249.     {
  250.         // ## Debug ##
  251.         bzChat.debugMessage("  -  [T20 Attack] GameStarted");
  252.        
  253.         // turn game on
  254.         m_GameOn = true;
  255.         // Open the map room doors
  256.         bob.ChangeDoorStatus(m_DoorNumber, !m_GameOn);
  257.         // Start Game timer
  258.         m_GameTimerSet.StartTimer();
  259.         m_GameStartTimeStamp = System.currentTimeMillis();
  260.         // add to our timer list
  261.         Timers.add(m_GameTimerSet);
  262.     }
  263.     private void doGameOver()
  264.     {
  265.         // ## Debug ##
  266.         bzChat.debugMessage("  -  [T20 Attack] GameEnded");
  267.        
  268.         // turn game off
  269.         m_GameOn = false;
  270.         // Close the map room doors
  271.         bob.ChangeDoorStatus(m_DoorNumber, !m_GameOn);
  272.        
  273.         // Record the last time held
  274.         BaseFreq raided = getBaseFreq(m_BaseOwner);
  275.         raided.addHoldTime(System.currentTimeMillis() - m_BaseTimestamp);
  276.        
  277.         for(BaseFreq bf: FreqList)
  278.             b.sendArenaMessage("Freq[ "+bf.getFreq()+" ] Total Time[ "+getTimeMs(bf.getTotalHoldTime())+" ]");
  279.        
  280.         m_BeenRaided = false;
  281.         m_BaseOwner = 8025;
  282.     }
  283.     // Base has been raided
  284.     private void baseRaided(PlayerPosition p)
  285.     {
  286.         // Grab freq info
  287.         BaseFreq raiders = getBaseFreq(b.getPlayer(p.getPlayerID()).getFrequency());
  288.        
  289.         // Has base been initialy raided?
  290.         if (!m_BeenRaided)
  291.         {
  292.             // Record initial raiders - getTimeMs
  293.             m_BeenRaided = true;
  294.             m_WhoRaided = b.getPlayerName(p.getPlayerID());
  295.             m_FreqRaided = raiders.getFreq();
  296.             m_RaidTime = System.currentTimeMillis() - m_GameStartTimeStamp;
  297.            
  298.             // ## DEBUG ##
  299.             bzChat.debugMessage("Initial Base Raid: Player[ "+m_WhoRaided+" ] Freq[ "+m_FreqRaided+" ] Time[ "+getTimeMs(m_RaidTime)+" ]");
  300.         }
  301.         else
  302.         {
  303.             // Grab losers info
  304.             BaseFreq raided = getBaseFreq(m_BaseOwner);
  305.             raided.addHoldTime(System.currentTimeMillis() - m_BaseTimestamp);
  306.            
  307.             b.sendOpposingTeamMessageByFrequency(m_BaseOwner, "Flag lost. Time held [ "+getTimeMs(System.currentTimeMillis() - m_BaseTimestamp)+" ] TotalHoldTime[ "+getTimeMs(raided.getTotalHoldTime())+" ]");
  308.         }
  309.        
  310.         // make them owners
  311.         m_BaseOwner = raiders.getFreq();
  312.         m_BaseTimestamp = System.currentTimeMillis();
  313.         // Toggle gfx
  314.         FlagToggle(raiders.getFreq());
  315.     }
  316.     // Toggle flag lvz for raid
  317.     private void FlagToggle(short Freq)
  318.     {
  319.         // Get all Players
  320.         Iterator<Player> i = b.getPlayingPlayerIterator();
  321.        
  322.         // Toggle lvz off for all players
  323.         b.sendUnfilteredPublicMessage("*objset -" + m_LvzOn + ", +" + m_LvzOff + ",");
  324.        
  325.          // Get all players on same freq and toggle lvz.
  326.          while( i.hasNext() ){
  327.             Player p = i.next();
  328.             if( p.getFrequency() == Freq){
  329.                  //Toggle lvz on for freq
  330.                 b.sendUnfilteredPrivateMessage( p.getPlayerID(), "*objset +" + m_LvzOn + ", -" + m_LvzOff + ",");
  331.             }
  332.          }
  333.     }
  334.     /*-------------------------------------------------------------------------------\
  335.      *
  336.      *                             Player Object Methods
  337.      *                            
  338.      *-------------------------------------------------------------------------------*/
  339.     private class BasePlayer
  340.     {
  341.         public BasePlayer(String PlayerName)
  342.         {
  343.             this.m_PlayerName = PlayerName;
  344.         }
  345.        
  346.         private String m_PlayerName;
  347.         public  String getPlayerName(){ return m_PlayerName; }
  348.        
  349.         private int m_CapturedFlags = 0;
  350.         public int getCapturedFlags(){  return this.m_CapturedFlags;}
  351.        
  352.         private int m_TKs = 0;
  353.         public int getTKs(){ return this.m_TKs;}
  354.         public void killedTeamMate(){ this.m_TKs += 1;}
  355.        
  356.         private int m_Multi = 0;
  357.         public int getMultis(){ return this.m_Multi;}
  358.         private int m_BestMulti = 1;
  359.         public int getBestMulti(){ return this.m_BestMulti;}
  360.         private long m_KillTimestamp = System.currentTimeMillis();
  361.        
  362.         private int m_Kills = 0;
  363.         public int getKills(){ return this.m_Kills;}
  364.         public void KilledPlayer()
  365.         {
  366.             // add player kills
  367.             this.m_Kills += 1;
  368.             // check for multikill
  369.             if (System.currentTimeMillis() - m_KillTimestamp < 5000)
  370.             {
  371.                 m_Multi+=1;
  372.                 if (m_Multi > m_BestMulti) m_BestMulti = m_Multi;
  373.             }
  374.             else    m_Multi = 1;
  375.            
  376.            
  377.             // update kill timestamp
  378.             m_KillTimestamp = System.currentTimeMillis();
  379.         }
  380.        
  381.         private int m_Deaths = 0;
  382.         public int getDeaths(){ return this.m_Deaths;}
  383.         public void Died(){ this.m_Deaths += 1;}
  384.        
  385.         private int m_DamageDealt = 0;
  386.         public int getDamageDealt() {   return this.m_DamageDealt;  }
  387.         public void dealtDamage(int Damage) { this.m_DamageDealt += Damage;}
  388.        
  389.         private int m_DamageTaken = 0;
  390.         public int getDamageTaken() {   return this.m_DamageTaken;  }
  391.         public void tookDamage(int Damage) { this.m_DamageTaken += Damage;}
  392.     }
  393.     /*-------------------------------------------------------------------------------\
  394.      *
  395.      *                             Freq Object Methods
  396.      *                            
  397.      *-------------------------------------------------------------------------------*/
  398.     // Master list of active freqs
  399.     private List<BaseFreq> FreqList = new ArrayList<BaseFreq>();
  400.     // Get freq object by freq number
  401.     private BaseFreq  getBaseFreq(short freq)
  402.     {
  403.         for(BaseFreq b:FreqList)
  404.             if (b.getFreq() == freq) return b;
  405.        
  406.         BaseFreq newBF = new BaseFreq(freq);
  407.         FreqList.add(newBF);
  408.         return FreqList.get((FreqList.indexOf(newBF)));
  409.     }
  410.     // Object to store freq info
  411.     private class BaseFreq
  412.     {
  413.         public BaseFreq(short freq)
  414.         {
  415.             this.b_Freq = freq;
  416.         }
  417.        
  418.         private short b_Freq;
  419.         public short getFreq(){ return this.b_Freq;}
  420.        
  421.         private List<BasePlayer> b_Players = new ArrayList<BasePlayer>();
  422.         public BasePlayer getPlayer(String PlayerName)
  423.         {
  424.             // Find player on list
  425.             for (BasePlayer b: b_Players)
  426.                 if (b.getPlayerName() == PlayerName) return b;
  427.            
  428.             // Make new player if not found
  429.             BasePlayer b = new BasePlayer(PlayerName);
  430.             b_Players.add(b);
  431.             return b_Players.get(b_Players.indexOf(b));
  432.         }
  433.        
  434.         private List<Long> b_HoldTimes = new ArrayList<Long>();
  435.         public void addHoldTime(long time)
  436.         {
  437.             b_HoldTimes.add(time);
  438.         };
  439.         public long getTotalHoldTime()
  440.         {
  441.             long T2R = 0;
  442.             for (long time:b_HoldTimes)
  443.                 T2R += time;
  444.             return T2R;
  445.         }
  446.     }
  447.     /*-------------------------------------------------------------------------------\
  448.      *
  449.      *                             MISC FUNCTIONS
  450.      *                            
  451.      *-------------------------------------------------------------------------------*/
  452.     public void InitializeGame()
  453.     {
  454.         // Hide flag on start of module
  455.         b.sendUnfilteredPublicMessage("*objset -1427,-1428,");
  456.        
  457.         // Store all settings for Timer to start game
  458.         m_StartGameSet = new FlagTimer(1,35);
  459.         m_StartGameSet.setInitialMessage("T20 BaseAttack starting in @time@ ! Hop in to join the fun.", 4);
  460.         m_StartGameSet.setEndMessage("GO GO GO - Defend T20!", 104);
  461.         m_StartGameSet.setNotification(60, "[@time@] until T20 Base Attack starts! Head over to O17 now!!", 0);
  462.         m_StartGameSet.setNotification(30, "[@time@] until T20 Base Attack starts! Head over to O17 now!!", 0);
  463.         m_StartGameSet.setNotification(5, "- 5 -", 26);
  464.         m_StartGameSet.setNotification(4, "- 4 -", 26);
  465.         m_StartGameSet.setNotification(3, "- 3 -", 26);
  466.         m_StartGameSet.setNotification(2, "- 2 -", 26);
  467.         m_StartGameSet.setNotification(1, "- 1 -", 26);
  468.         // Settings for Game Timer
  469.         m_GameTimerSet = new FlagTimer(2,185);
  470.         m_GameTimerSet.setInitialMessage("T20 BaseAttack - All your flag are belong to us... Defend the O-17 Flag!  [Game Time = @time@] ", 0);
  471.         m_GameTimerSet.setEndMessage(" ----- Score Printout here -----.", 104);
  472.         m_GameTimerSet.setNotification(180, "T20 Attack - @time@ remaining!", 0);
  473.         m_GameTimerSet.setNotification(60, "T20 Attack - @time@ remaining!", 0);
  474.         m_GameTimerSet.setNotification(30, "T20 Attack - @time@ remaining!", 0);
  475.         m_GameTimerSet.setNotification(5, "- 5 -", 26);
  476.         m_GameTimerSet.setNotification(4, "- 4 -", 26);
  477.         m_GameTimerSet.setNotification(3, "- 3 -", 26);
  478.         m_GameTimerSet.setNotification(2, "- 2 -", 26);
  479.         m_GameTimerSet.setNotification(1, "- 1 -", 26);
  480.     }
  481.     // Simple collision check - seeing if player is in a given region
  482.     private boolean InRegion( PlayerPosition p, short[] Region)
  483.     {   return InRegion(p.getXLocation(),p.getYLocation(),Region);  }
  484.     // Simple collision check - seeing if player is in a given region
  485.     private boolean InRegion( short x, short y, short[] Region)
  486.     {
  487.         return (x > Region[0] &&
  488.                 x < Region[2] &&
  489.                 y > Region[1] &&
  490.                 y < Region[3]) ? true:false;
  491.     }
  492.     // Format time to print
  493.     public String getTimeMs(long elapsedTime) {      
  494.         String format = String.format("%%0%dd", 2);  
  495.         elapsedTime = elapsedTime / 1000;  
  496.         String seconds = String.format(format, elapsedTime % 60);  
  497.         String minutes = String.format(format, (elapsedTime % 3600) / 60);  
  498.         String hours = String.format(format, elapsedTime / 3600);  
  499.         long milli = elapsedTime - ((elapsedTime % 60) + ((elapsedTime % 3600) / 60) + (elapsedTime / 3600));
  500.         String time =  hours + "h:" + minutes + "m:" + seconds +"s:" + milli + "ms";  
  501.         return time;  
  502.     }
  503. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement