Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package twcore.bots.battlebot.PublicOps.Flags;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Random;
- import twcore.bots.battlebot.ChatBuffer;
- import twcore.bots.battlebot.DoorMan;
- import twcore.bots.battlebot.PublicOps.PublicPlayer;
- import twcore.core.BotAction;
- import twcore.core.events.PlayerDeath;
- import twcore.core.events.PlayerPosition;
- import twcore.core.game.Player;
- public class BaseFlagging {
- BotAction b;
- ChatBuffer bzChat;
- DoorMan bob;
- public BaseFlagging(BotAction b, ChatBuffer c, DoorMan bob)
- {
- this.b = b;
- this.bzChat = c;
- this.bob = bob;
- }
- /*-------------------------------------------------------------------------------\
- *
- * NEEDED VARS
- *
- *-------------------------------------------------------------------------------*/
- private boolean m_GameOn = false;
- public boolean GameOn(){ return this.m_GameOn; }
- private long m_GameStartTimeStamp;
- // Flag lvz
- private int m_LvzOn = 1428;
- private int m_LvzOff = 1427;
- // Door to toggle on and off
- private byte m_DoorNumber = 5;
- // Map room coords
- private short[] m_MapRoom1 = new short[]{ 225 * 16, 778 * 16, 499 * 16, 846 * 16};
- private short[] m_MapRoom2 = new short[]{ 499 * 16, 792 * 16, 785 * 16, 846 * 16};
- private short[] m_FlagHotSpot = new short[]{ 12336, 13328, 12496, 13488 };
- // Settings for timer
- private FlagTimer m_StartGameSet;
- // settings for game timer
- private FlagTimer m_GameTimerSet;
- // If base has initially been raided
- private boolean m_BeenRaided = false;
- private String m_WhoRaided = "~nobody~";
- private short m_FreqRaided = 8025;
- private long m_RaidTime = 0;
- // Freq that owns the flag
- private short m_BaseOwner = 8025;
- private long m_BaseTimestamp;
- /*-------------------------------------------------------------------------------\
- *
- * SUBSPACE EVENTS
- *
- *-------------------------------------------------------------------------------*/
- public void PlayerPositionEvent(PlayerPosition p)
- {
- // If there is no game check for stragglers in the area
- if (!m_GameOn)
- {
- // Flag room
- if (InRegion(p,m_MapRoom1)||InRegion(p,m_MapRoom2))
- {
- // Warp them back
- b.sendPrivateMessage(p.getPlayerID(), "There are no matches at this time and you are not authorized to be in this area.");
- b.warpTo(p.getPlayerID(), 512,512);
- }
- // No need to track flag hotspot unless game has started
- return;
- }
- // Flag HOTSPOT - If a new freq has taken possesion
- if (InRegion(p, m_FlagHotSpot) && b.getPlayer(p.getPlayerID()).getFrequency() != m_BaseOwner)
- baseRaided(p);
- }
- public void PlayerDeathEvent(PlayerDeath pd)
- {
- if (!m_GameOn) return;
- short killer_x = b.getPlayer(pd.getKillerID()).getXLocation();
- short killer_y = b.getPlayer(pd.getKillerID()).getYLocation();
- short killed_x = b.getPlayer(pd.getKilleeID()).getXLocation();
- short killed_y = b.getPlayer(pd.getKilleeID()).getYLocation();
- if (InRegion(killer_x,killer_y,m_MapRoom1) ||
- InRegion(killer_x,killer_y,m_MapRoom2) ||
- InRegion(killed_x,killed_y,m_MapRoom1) ||
- InRegion(killed_x,killed_y,m_MapRoom2))
- {
- // Get basing freqs
- BaseFreq killerF = getBaseFreq(b.getPlayer(pd.getKillerID()).getFrequency());
- BaseFreq killedF = getBaseFreq(b.getPlayer(pd.getKilleeID()).getFrequency());
- // Get players
- BasePlayer killer = killerF.getPlayer(b.getPlayerName(pd.getKillerID()));
- BasePlayer killed = killedF.getPlayer(b.getPlayerName(pd.getKilleeID()));
- if (killerF.getFreq() == killedF.getFreq())
- {
- // TK Code
- return;
- }
- else
- {
- killer.KilledPlayer();
- killed.Died();
- if (killer.getMultis() > 1)
- {
- updateMultiKillPlayer(killer);
- }
- }
- b.sendArenaMessage("FlagDeath Event");
- }
- }
- /*-------------------------------------------------------------------------------\
- *
- * MultiKills
- *
- *-------------------------------------------------------------------------------*/
- private List<MultiMessageEvent> m_MultiKillMessages = new ArrayList<MultiMessageEvent>();
- private void updateMultiKillList()
- {
- if (m_MultiKillMessages.isEmpty()) return;
- if (m_MultiKillMessages.get(0).timeToSend())
- {
- b.sendArenaMessage(m_MultiKillMessages.get(0).getMultiMessage(), 14);
- Timers.remove(0);
- }
- }
- private void updateMultiKillPlayer(BasePlayer BPlayer)
- {
- for (MultiMessageEvent m: m_MultiKillMessages)
- {
- if (m.getPlayerName() == BPlayer.getPlayerName())
- {
- m.updateMultiPlayer(BPlayer.getMultis());
- return;
- }
- }
- m_MultiKillMessages.add( new MultiMessageEvent(BPlayer.getPlayerName(),BPlayer.getMultis()));
- bzChat.debugMessage("Adding multi message to queue.");
- }
- private class MultiMessageEvent
- {
- public MultiMessageEvent(String PlayerName, int Kills)
- {
- this.m_PlayerName = PlayerName;
- this.m_Kills = Kills;
- }
- private String m_PlayerName;
- public String getPlayerName(){ return m_PlayerName; }
- private long m_Timestamp = System.currentTimeMillis();
- public void updateMultiPlayer(int Kills)
- {
- this.m_Kills = Kills;
- this.m_Timestamp = System.currentTimeMillis();
- }
- private int m_Kills;
- public boolean timeToSend(){ return (System.currentTimeMillis() - m_Timestamp > 6500) ? true: false; }
- public String getMultiMessage()
- {
- return "Multikill for [ "+m_PlayerName+" ]! Kills[ "+m_Kills+" ] ";
- }
- }
- /*-------------------------------------------------------------------------------\
- *
- * BASE FLAG TIMER
- *
- *-------------------------------------------------------------------------------*/
- // List of timed events
- private List<FlagTimer> Timers = new ArrayList<FlagTimer>();
- // main timer for game
- public void BaseTimer()
- {
- if ( !m_MultiKillMessages.isEmpty() && m_MultiKillMessages.get(0).timeToSend())
- {
- b.sendArenaMessage(m_MultiKillMessages.get(0).getMultiMessage(), 14);
- Timers.remove(0);
- }
- //updateMultiKillList();
- // sort through timers and update
- for (int i=Timers.size()-1; i> -1; i--)
- {
- if (!Timers.get(i).TimerExpired() )
- Timers.get(i).UpdateTimer(b, bzChat);
- else
- {
- doTimerTask(Timers.get(i).getTimerTask());
- Timers.remove(i);
- }
- }
- }
- // Timer tasks
- private void doTimerTask(int Task)
- {
- switch(Task)
- {
- case 1:
- doGameTimer();
- break;
- case 2:
- doGameOver();
- break;
- }
- }
- /*-------------------------------------------------------------------------------\
- *
- * MAIN FUNCTIONS
- *
- *-------------------------------------------------------------------------------*/
- public void m_StartTimer()
- {
- // ## Debug ##
- bzChat.debugMessage(" - [T20 Attack] Starting timer for Game start.");
- // Record the start to timer
- m_StartGameSet.StartTimer();
- // add to our timer list
- Timers.add(m_StartGameSet);
- }
- private void doGameTimer()
- {
- // ## Debug ##
- bzChat.debugMessage(" - [T20 Attack] GameStarted");
- // turn game on
- m_GameOn = true;
- // Open the map room doors
- bob.ChangeDoorStatus(m_DoorNumber, !m_GameOn);
- // Start Game timer
- m_GameTimerSet.StartTimer();
- m_GameStartTimeStamp = System.currentTimeMillis();
- // add to our timer list
- Timers.add(m_GameTimerSet);
- }
- private void doGameOver()
- {
- // ## Debug ##
- bzChat.debugMessage(" - [T20 Attack] GameEnded");
- // turn game off
- m_GameOn = false;
- // Close the map room doors
- bob.ChangeDoorStatus(m_DoorNumber, !m_GameOn);
- // Record the last time held
- BaseFreq raided = getBaseFreq(m_BaseOwner);
- raided.addHoldTime(System.currentTimeMillis() - m_BaseTimestamp);
- for(BaseFreq bf: FreqList)
- b.sendArenaMessage("Freq[ "+bf.getFreq()+" ] Total Time[ "+getTimeMs(bf.getTotalHoldTime())+" ]");
- m_BeenRaided = false;
- m_BaseOwner = 8025;
- }
- // Base has been raided
- private void baseRaided(PlayerPosition p)
- {
- // Grab freq info
- BaseFreq raiders = getBaseFreq(b.getPlayer(p.getPlayerID()).getFrequency());
- // Has base been initialy raided?
- if (!m_BeenRaided)
- {
- // Record initial raiders - getTimeMs
- m_BeenRaided = true;
- m_WhoRaided = b.getPlayerName(p.getPlayerID());
- m_FreqRaided = raiders.getFreq();
- m_RaidTime = System.currentTimeMillis() - m_GameStartTimeStamp;
- // ## DEBUG ##
- bzChat.debugMessage("Initial Base Raid: Player[ "+m_WhoRaided+" ] Freq[ "+m_FreqRaided+" ] Time[ "+getTimeMs(m_RaidTime)+" ]");
- }
- else
- {
- // Grab losers info
- BaseFreq raided = getBaseFreq(m_BaseOwner);
- raided.addHoldTime(System.currentTimeMillis() - m_BaseTimestamp);
- b.sendOpposingTeamMessageByFrequency(m_BaseOwner, "Flag lost. Time held [ "+getTimeMs(System.currentTimeMillis() - m_BaseTimestamp)+" ] TotalHoldTime[ "+getTimeMs(raided.getTotalHoldTime())+" ]");
- }
- // make them owners
- m_BaseOwner = raiders.getFreq();
- m_BaseTimestamp = System.currentTimeMillis();
- // Toggle gfx
- FlagToggle(raiders.getFreq());
- }
- // Toggle flag lvz for raid
- private void FlagToggle(short Freq)
- {
- // Get all Players
- Iterator<Player> i = b.getPlayingPlayerIterator();
- // Toggle lvz off for all players
- b.sendUnfilteredPublicMessage("*objset -" + m_LvzOn + ", +" + m_LvzOff + ",");
- // Get all players on same freq and toggle lvz.
- while( i.hasNext() ){
- Player p = i.next();
- if( p.getFrequency() == Freq){
- //Toggle lvz on for freq
- b.sendUnfilteredPrivateMessage( p.getPlayerID(), "*objset +" + m_LvzOn + ", -" + m_LvzOff + ",");
- }
- }
- }
- /*-------------------------------------------------------------------------------\
- *
- * Player Object Methods
- *
- *-------------------------------------------------------------------------------*/
- private class BasePlayer
- {
- public BasePlayer(String PlayerName)
- {
- this.m_PlayerName = PlayerName;
- }
- private String m_PlayerName;
- public String getPlayerName(){ return m_PlayerName; }
- private int m_CapturedFlags = 0;
- public int getCapturedFlags(){ return this.m_CapturedFlags;}
- private int m_TKs = 0;
- public int getTKs(){ return this.m_TKs;}
- public void killedTeamMate(){ this.m_TKs += 1;}
- private int m_Multi = 0;
- public int getMultis(){ return this.m_Multi;}
- private int m_BestMulti = 1;
- public int getBestMulti(){ return this.m_BestMulti;}
- private long m_KillTimestamp = System.currentTimeMillis();
- private int m_Kills = 0;
- public int getKills(){ return this.m_Kills;}
- public void KilledPlayer()
- {
- // add player kills
- this.m_Kills += 1;
- // check for multikill
- if (System.currentTimeMillis() - m_KillTimestamp < 5000)
- {
- m_Multi+=1;
- if (m_Multi > m_BestMulti) m_BestMulti = m_Multi;
- }
- else m_Multi = 1;
- // update kill timestamp
- m_KillTimestamp = System.currentTimeMillis();
- }
- private int m_Deaths = 0;
- public int getDeaths(){ return this.m_Deaths;}
- public void Died(){ this.m_Deaths += 1;}
- private int m_DamageDealt = 0;
- public int getDamageDealt() { return this.m_DamageDealt; }
- public void dealtDamage(int Damage) { this.m_DamageDealt += Damage;}
- private int m_DamageTaken = 0;
- public int getDamageTaken() { return this.m_DamageTaken; }
- public void tookDamage(int Damage) { this.m_DamageTaken += Damage;}
- }
- /*-------------------------------------------------------------------------------\
- *
- * Freq Object Methods
- *
- *-------------------------------------------------------------------------------*/
- // Master list of active freqs
- private List<BaseFreq> FreqList = new ArrayList<BaseFreq>();
- // Get freq object by freq number
- private BaseFreq getBaseFreq(short freq)
- {
- for(BaseFreq b:FreqList)
- if (b.getFreq() == freq) return b;
- BaseFreq newBF = new BaseFreq(freq);
- FreqList.add(newBF);
- return FreqList.get((FreqList.indexOf(newBF)));
- }
- // Object to store freq info
- private class BaseFreq
- {
- public BaseFreq(short freq)
- {
- this.b_Freq = freq;
- }
- private short b_Freq;
- public short getFreq(){ return this.b_Freq;}
- private List<BasePlayer> b_Players = new ArrayList<BasePlayer>();
- public BasePlayer getPlayer(String PlayerName)
- {
- // Find player on list
- for (BasePlayer b: b_Players)
- if (b.getPlayerName() == PlayerName) return b;
- // Make new player if not found
- BasePlayer b = new BasePlayer(PlayerName);
- b_Players.add(b);
- return b_Players.get(b_Players.indexOf(b));
- }
- private List<Long> b_HoldTimes = new ArrayList<Long>();
- public void addHoldTime(long time)
- {
- b_HoldTimes.add(time);
- };
- public long getTotalHoldTime()
- {
- long T2R = 0;
- for (long time:b_HoldTimes)
- T2R += time;
- return T2R;
- }
- }
- /*-------------------------------------------------------------------------------\
- *
- * MISC FUNCTIONS
- *
- *-------------------------------------------------------------------------------*/
- public void InitializeGame()
- {
- // Hide flag on start of module
- b.sendUnfilteredPublicMessage("*objset -1427,-1428,");
- // Store all settings for Timer to start game
- m_StartGameSet = new FlagTimer(1,35);
- m_StartGameSet.setInitialMessage("T20 BaseAttack starting in @time@ ! Hop in to join the fun.", 4);
- m_StartGameSet.setEndMessage("GO GO GO - Defend T20!", 104);
- m_StartGameSet.setNotification(60, "[@time@] until T20 Base Attack starts! Head over to O17 now!!", 0);
- m_StartGameSet.setNotification(30, "[@time@] until T20 Base Attack starts! Head over to O17 now!!", 0);
- m_StartGameSet.setNotification(5, "- 5 -", 26);
- m_StartGameSet.setNotification(4, "- 4 -", 26);
- m_StartGameSet.setNotification(3, "- 3 -", 26);
- m_StartGameSet.setNotification(2, "- 2 -", 26);
- m_StartGameSet.setNotification(1, "- 1 -", 26);
- // Settings for Game Timer
- m_GameTimerSet = new FlagTimer(2,185);
- m_GameTimerSet.setInitialMessage("T20 BaseAttack - All your flag are belong to us... Defend the O-17 Flag! [Game Time = @time@] ", 0);
- m_GameTimerSet.setEndMessage(" ----- Score Printout here -----.", 104);
- m_GameTimerSet.setNotification(180, "T20 Attack - @time@ remaining!", 0);
- m_GameTimerSet.setNotification(60, "T20 Attack - @time@ remaining!", 0);
- m_GameTimerSet.setNotification(30, "T20 Attack - @time@ remaining!", 0);
- m_GameTimerSet.setNotification(5, "- 5 -", 26);
- m_GameTimerSet.setNotification(4, "- 4 -", 26);
- m_GameTimerSet.setNotification(3, "- 3 -", 26);
- m_GameTimerSet.setNotification(2, "- 2 -", 26);
- m_GameTimerSet.setNotification(1, "- 1 -", 26);
- }
- // Simple collision check - seeing if player is in a given region
- private boolean InRegion( PlayerPosition p, short[] Region)
- { return InRegion(p.getXLocation(),p.getYLocation(),Region); }
- // Simple collision check - seeing if player is in a given region
- private boolean InRegion( short x, short y, short[] Region)
- {
- return (x > Region[0] &&
- x < Region[2] &&
- y > Region[1] &&
- y < Region[3]) ? true:false;
- }
- // Format time to print
- public String getTimeMs(long elapsedTime) {
- String format = String.format("%%0%dd", 2);
- elapsedTime = elapsedTime / 1000;
- String seconds = String.format(format, elapsedTime % 60);
- String minutes = String.format(format, (elapsedTime % 3600) / 60);
- String hours = String.format(format, elapsedTime / 3600);
- long milli = elapsedTime - ((elapsedTime % 60) + ((elapsedTime % 3600) / 60) + (elapsedTime / 3600));
- String time = hours + "h:" + minutes + "m:" + seconds +"s:" + milli + "ms";
- return time;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement