Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package twcore.bots.battlebot;
- import twcore.bots.battlebot.PlayerOps.BZPlayer;
- import twcore.bots.battlebot.PlayerOps.PlayerOps;
- import twcore.bots.battlebot.PlayerOps.Status;
- import twcore.bots.battlebot.PublicOps.PublicOps;
- import twcore.core.BotAction;
- import twcore.core.BotSettings;
- import twcore.core.EventRequester;
- import twcore.core.SubspaceBot;
- import twcore.core.events.ArenaJoined;
- import twcore.core.events.FlagClaimed;
- import twcore.core.events.FlagDropped;
- import twcore.core.events.FrequencyChange;
- import twcore.core.events.FrequencyShipChange;
- import twcore.core.events.LoggedOn;
- import twcore.core.events.Message;
- import twcore.core.events.PlayerDeath;
- import twcore.core.events.PlayerEntered;
- import twcore.core.events.PlayerLeft;
- import twcore.core.events.PlayerPosition;
- import twcore.core.events.Prize;
- import twcore.core.events.WatchDamage;
- import twcore.core.game.Player;
- import java.util.*;
- /*----------------------------------------------------------------------------------\
- * Battle Zone Bot:
- *
- * BattleBot will be in control of our public arena for BattleZone
- * The three main statuses for the public arena are:
- * Spectating - In 8025 and speccing
- * Public - Inside Main area and playing normal pub type play/gaming
- * Battle - In one of the outside boxes setup for VS battle or Event games (usually set in subarenas)
- * My main objective for this bot is to organize public play and subarena play all in one area
- * Too much population ends up getting spread out over too many subarenas
- *----------------------------------------------------------------------------------*/
- public class battlebot extends SubspaceBot {
- private BotSettings m_botSettings; // Stores settings for your bot as found in the .cfg file.
- // In this case, it would be settings from battlebot.cfg
- // sending over bot functions to public module
- PublicOps pub;
- // Chat class to limit biller msgs/commands
- ChatBuffer bzChat = new ChatBuffer();
- // bob the door man - plz tip
- DoorMan bob;
- // Shortcut to ba
- BotAction b;
- // Help command handler
- HelpBrowser bzHelp;
- /**
- * Creates a new instance of your bot.
- */
- public battlebot(BotAction botAction) {
- super(botAction);
- requestEvents();
- // m_botSettings contains the data specified in file <botname>.cfg
- m_botSettings = m_botAction.getBotSettings();
- // my shortcut to ba
- b = this.ba;
- // sending needed classes to our own
- bob = new DoorMan(b,bzChat);
- pub = new PublicOps(this,bzChat,bob);
- bzHelp = new HelpBrowser(this);
- }
- /**
- * This method requests event information from any events your bot wishes
- * to "know" about; if left commented your bot will simply ignore them.
- */
- /*-------------------------------------------------------------------------------\
- *
- * EVENT REGISTRY
- *
- *-------------------------------------------------------------------------------*/
- public void requestEvents() {
- EventRequester req = m_botAction.getEventRequester();
- req.request(EventRequester.MESSAGE);
- req.request(EventRequester.ARENA_JOINED);
- req.request(EventRequester.PLAYER_ENTERED);
- req.request(EventRequester.PLAYER_POSITION);
- req.request(EventRequester.FREQUENCY_SHIP_CHANGE);
- req.request(EventRequester.LOGGED_ON);
- req.request(EventRequester.PLAYER_LEFT);
- req.request(EventRequester.PRIZE);
- req.request(EventRequester.FREQUENCY_CHANGE);
- req.request(EventRequester.PLAYER_DEATH);
- req.request(EventRequester.FLAG_CLAIMED);
- req.request(EventRequester.FLAG_DROPPED);
- // req.request(EventRequester.WEAPON_FIRED);
- // req.request(EventRequester.ARENA_LIST);
- req.request(EventRequester.WATCH_DAMAGE);
- // req.request(EventRequester.FLAG_POSITION);
- }
- /*-------------------------------------------------------------------------------\
- *
- * NEEDED VARS
- *
- *-------------------------------------------------------------------------------*/
- // Random numbers duh
- Random ran = new Random();
- // Used for Game timer
- Timer timer = new Timer();
- // Bot Game status
- boolean GameOn = false;
- // Controls my custom Player List
- PlayerOps Players = new PlayerOps();
- // Command to join needed chats
- String ChatCommand = "?chat=battledev,battle";
- // Regions
- // - Start Box
- private short[] m_StartBox = new short[]{ 520 * 16, 510 * 16, 524 * 16, 514 * 16 };
- private short[] m_PublicArea = new short[]{ 0, 328 * 16, 1023 * 16, 768 * 16 };
- 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};
- // Timestamps for updating bot position
- long TS_BotPosUpdate = System.currentTimeMillis() ;
- long Delay_BotPosUpdate = 75;
- boolean BotPosToggle = false;
- String NextWDToggle = "~none~";
- long NextWDTimestamp = System.currentTimeMillis();
- /*-------------------------------------------------------------------------------\
- *
- * SUBSPACE EVENTS
- *
- *-------------------------------------------------------------------------------*/
- // - MESSAGE EVENT
- public void handleEvent(Message event)
- {
- bzCommandHandler(event);
- }
- // - DAMAGE EVENT
- public void handleEvent(WatchDamage w)
- {
- if(!GameOn) return;
- if (pub.damageEventHandled(w)) return;
- }
- // - LOGGEN ON
- public void handleEvent(LoggedOn event)
- {
- m_botAction.joinArena(m_botSettings.getString("arena"));
- }
- // - PLAYER POSITION
- public void handleEvent(PlayerPosition p)
- {
- if(!GameOn) return;
- RegionControl(p);
- }
- // - PLAYER ENTERED
- public void handleEvent(PlayerEntered pe)
- {
- if(!GameOn) return;
- Players.PlayerEntered(m_botAction.getPlayerName(pe.getPlayerID()));
- watchDamageOn(m_botAction.getPlayerName(pe.getPlayerID()));
- }
- // - ARENA JOINED
- public void handleEvent(ArenaJoined event)
- {
- InitializeBot();
- }
- // - FREQUENCY CHANGE
- public void handleEvent(FrequencyChange sc)
- {
- if(!GameOn) return;
- }
- // - GET GREEN EVENT
- public void handleEvent(Prize pr)
- {
- if(!GameOn) return;
- pub.prizeEvent(pr);
- }
- // - FLAG CLAIMED
- public void handleEvent(FlagClaimed fc)
- {
- if(!GameOn) return;
- pub.flagClaimed(fc);
- }
- // - FLAG DROPPED
- public void handleEvent(FlagDropped fd)
- {
- if(!GameOn) return;
- pub.flagDropped(fd);
- }
- // - PLAYER DEATH
- public void handleEvent(PlayerDeath pd)
- {
- if(!GameOn) return;
- if (pub.playerDeath(pd)) return;
- }
- // - PLAYER LEFT
- public void handleEvent(PlayerLeft pl)
- {
- if(!GameOn) return;
- String name = b.getPlayerName(pl.getPlayerID());
- if(name == null) b.sendChatMessage("Player ID Returned null - bug found yay.");
- pub.playerLeftEvent(pl);
- Players.removePlayer(name);
- }
- // - SHIP CHANGE
- public void handleEvent(FrequencyShipChange sc)
- {
- if(!GameOn) return;
- // Grab player info from our custom Player Class
- BZPlayer bp = Players.getPlayer(m_botAction.getPlayerName( sc.getPlayerID()));
- // we shouldnt be getting null here
- if (bp == null) return;
- // Update Main Player status
- if (sc.getShipType() == 0 && bp.PlayerStatus() != Status.Spectating)
- { bp.setStatus(Status.Spectating); }
- if (pub.shipChangeEvent(sc)) return;
- }
- /*-------------------------------------------------------------------------------\
- *
- * MAIN ZONE TIMER
- *
- *-------------------------------------------------------------------------------*/
- class GameTimer extends TimerTask {
- @Override
- public void run() {
- if(!playerListUpdated) updatePlayerList();
- // check to see if its time to toggle bot position
- BotPosUpdate();
- // Update chat buffer module
- bzChat.updateChatBuffer(b);
- // Checks the status of doors and updates if needed
- bob.UpdateDoors();
- // Run pub timer
- pub.pubGameTimer();
- // Check for watchdamage commands
- if (GameOn)updateWatchDamageList();
- }
- }
- /*-------------------------------------------------------------------------------\
- *
- * CHAT COMMANDS
- *
- *-------------------------------------------------------------------------------*/
- // -- HELP
- // Checks our home directory for help files and sends back to player
- public void doHelp(String name, String args) {
- String[] helpMessage;
- if (args == null || args.trim().length() == 0) helpMessage = bzHelp.getHelp("overview");
- else if (args.charAt(0) == '!') helpMessage = bzHelp.getHelp(args.substring(1));
- else helpMessage = bzHelp.getHelp(args);
- for (int i = 0; i < helpMessage.length; i++)
- m_botAction.sendUnfilteredPrivateMessage(name, helpMessage[i]);
- }
- // -- ALL OTHER COMMANDS
- public void bzCommandHandler(Message event) {
- // Retreive name. If the message is remote, then event.getMessager() returns null, and event.getPlayerID returns a value.
- // If the message is from the same arena, event.getMessager() returns a string, and event.getPlayerID will return 0.
- String name = event.getMessager() != null ? event.getMessager() : m_botAction.getPlayerName(event.getPlayerID());
- if (name == null) name = "-anonymous-";
- // Help commands
- if ((event.getMessageType() == Message.PRIVATE_MESSAGE || event.getMessageType() == Message.PUBLIC_MESSAGE)
- && event.getMessage().startsWith("!help"))
- {
- doHelp(b.getPlayerName(event.getPlayerID()),
- event.getMessage().trim() == "!help" ? "":event.getMessage().substring(5, event.getMessage().length()).trim());
- return;
- }
- // Toggle debug mode
- if (event.getMessage().trim().contains("Damage logging ON") || event.getMessage().trim().contains("Damage logging OFF"))
- {
- String response = event.getMessage().trim().split(" ",-1)[2].toLowerCase();
- if (b.getPlayer(watchDamageList.get(0)) == null)
- {
- bzChat.debugMessage("Damage logging: Player [ "+watchDamageList.get(0)+" ] left before command could be sent.");
- watchDamageList.remove(0);
- return;
- }
- if (response.equals("on"))
- {
- bzChat.debugMessage("Damage logging toggled on for: [ "+watchDamageList.get(0)+" ]");
- watchDamageList.remove(0);
- }
- else
- b.sendUnfilteredPrivateMessage(watchDamageList.get(0), "*watchdamage");
- }
- // Toggle debug mode
- if (event.getMessage().trim().equalsIgnoreCase("!test"))
- {
- b.sendArenaMessage("Flag Coords [ "+b.getFlag(0).getXLocation()+" | "+b.getFlag(0).getYLocation()+" ] ");
- short x = b.getPlayer((b.getFlag(0).getPlayerID())).getXLocation();
- short y = b.getPlayer((b.getFlag(0).getPlayerID())).getYLocation();
- b.sendArenaMessage("Player Flag Coords [ "+x+" | "+y+" ] ");
- }
- // Toggle debug mode
- if (event.getMessage().trim().equalsIgnoreCase("!debug"))
- {
- bzChat.setDebug(!bzChat.debugMode);
- return;
- }
- // Checking if public command
- if (pub.isPublicCommand(event)) return;
- // Default implemented command: !die
- if (event.getMessageType() == Message.PRIVATE_MESSAGE && event.getMessage().equalsIgnoreCase("!die")) {
- //m_botAction.sendPublicMessage(name + " commanded me to die. Disconnecting...");
- try { Thread.sleep(50); } catch (Exception e) {};
- m_botAction.die();
- }
- // Returns Status List
- else if (event.getMessage().trim().equalsIgnoreCase("!plist"))
- { for (String s: Players.getList()) m_botAction.sendPrivateMessage(event.getPlayerID(), s); }
- }
- /*-------------------------------------------------------------------------------\
- *
- * ZONE INITIALIZATION TASKS
- *
- *-------------------------------------------------------------------------------*/
- // Initialization tasks to get bot started
- public void InitializeBot()
- {
- // Have bot get reliable kills
- m_botAction.setReliableKills(1);
- // Setting debug messaging to true
- bzChat.setDebug(true);
- // Have bot join chats
- bzChat.sendChatMsg(0, ChatCommand);
- // Start Main GameTimer
- timer.scheduleAtFixedRate(new GameTimer(),5000 ,10); //delay in milliseconds
- // Initialize pub class
- pub.initializePub();
- }
- private boolean playerListUpdated = false;
- private long playerListUpdateTS = System.currentTimeMillis();
- public void updatePlayerList()
- {
- if(System.currentTimeMillis() - playerListUpdateTS < 10000) return;
- playerListUpdated = true;
- // Get all Players
- Iterator<Player> i = b.getPlayerIterator();
- // Get all players on same freq and toggle lvz.
- while( i.hasNext() )
- {
- Player p = i.next();
- Players.PlayerEntered(m_botAction.getPlayerName(p.getPlayerID()));
- watchDamageOn(p.getPlayerName());
- if (p.isPlaying())
- {
- if(InRegion(p.getXLocation(),p.getYLocation(),m_PublicArea))
- {
- pub.playerJoinedPublic(p.getPlayerName());
- }
- else if (InRegion(p.getXLocation(),p.getYLocation(),m_MapRoom1)
- || InRegion(p.getXLocation(),p.getYLocation(),m_MapRoom2))
- {
- b.sendArenaMessage("Warped player out of map room on BotInitialization. [ "+p.getPlayerName()+" ]");
- b.warpTo(p.getPlayerID(), 512, 512);
- pub.playerJoinedPublic(p.getPlayerName());
- }
- else
- {
- b.sendArenaMessage("Warped player out of Battle Box area on BotInitialization. [ "+p.getPlayerName()+" ]");
- b.warpTo(p.getPlayerID(), 512, 512);
- pub.playerJoinedPublic(p.getPlayerName());
- }
- }
- }
- // Start Bot/Game
- GameOn = true;
- watchDamageTS = System.currentTimeMillis();
- // Send initialization message
- bzChat.sendChatMsg(1, m_botAction.getBotName() + " is now initialized.");
- }
- /*-------------------------------------------------------------------------------\
- *
- * MISC FUNCTIONS
- *
- *-------------------------------------------------------------------------------*/
- List<String> watchDamageList = new ArrayList<String>();
- long watchDamageTS = System.currentTimeMillis();
- public void updateWatchDamageList()
- {
- if (!watchDamageList.isEmpty() && System.currentTimeMillis() - watchDamageTS > 1000)
- {
- watchDamageTS = System.currentTimeMillis();
- b.sendUnfilteredPrivateMessage(watchDamageList.get(0), "*watchdamage");
- }
- }
- public void watchDamageOn(String PlayerName)
- {
- watchDamageList.add(PlayerName);
- }
- /*----------------------------------------------\
- * BOT POSITION TOGGLE - helps us get as much player position info as possible
- *---------------------------------------------*/
- // Toggles between 2 diff spots so we can get as many playerposition packets as possible
- public void BotPosUpdate()
- {
- if (System.currentTimeMillis() - TS_BotPosUpdate > Delay_BotPosUpdate)
- {
- // Update timestamp
- TS_BotPosUpdate = System.currentTimeMillis();
- // Toggle position
- BotPosToggle = !BotPosToggle;
- // Stop speccing a player
- b.stopSpectatingPlayer();
- // Send bot to new position
- b.getShip().move((BotPosToggle ? 256:768) * 16, 587 * 16);
- }
- }
- /*----------------------------------------------\
- * MANAGE OUR REGIONS AND HOTSPOTS
- *---------------------------------------------*/
- // Check to see if a player is over a region or hotspot
- public void RegionControl(PlayerPosition p)
- {
- // Grab player info from our cutom Player Class
- BZPlayer bp = Players.getPlayer(m_botAction.getPlayerName( p.getPlayerID()));
- // we shouldnt be getting null here
- if (bp == null) return;
- // Checking if position is for pub - if so ignore the rest
- if(pub.playerPositionHandled(p)) return;
- // ------------ START BOX
- if (InRegion(p,m_StartBox) && bp.PlayerStatus() == Status.Spectating)
- {
- bp.setStatus(Status.Public);
- pub.playerJoinedPublic(bp.PlayerName());
- }
- }
- // 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment