Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package twcore.bots.shuttlebot;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Timer;
- import java.util.TimerTask;
- 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.LoggedOn;
- import twcore.core.events.Message;
- import twcore.core.events.PlayerPosition;
- public class shuttlebot extends SubspaceBot {
- private BotSettings m_botSettings;
- public shuttlebot(BotAction botAction) {
- super(botAction);
- requestEvents();
- // m_botSettings contains the data specified in file <botname>.cfg
- m_botSettings = m_botAction.getBotSettings();
- b = m_botAction;
- }
- public void requestEvents() {
- EventRequester req = m_botAction.getEventRequester();
- req.request(EventRequester.MESSAGE);
- req.request(EventRequester.ARENA_JOINED);
- req.request(EventRequester.PLAYER_POSITION);
- req.request(EventRequester.LOGGED_ON);
- }
- /*-------------------------------------------------------------------------------\
- *
- * NEEDED VARS
- *
- *-------------------------------------------------------------------------------*/
- // Used for Game timer
- Timer timer = new Timer();
- BotAction b;
- // Master list of houses
- List<PlayerHouse> Houses = new ArrayList<PlayerHouse>();
- // Master list of all booths
- List<int[]> m_booths = new ArrayList<int[]>();
- /*-------------------------------------------------------------------------------\
- *
- * SUBSPACE EVENTS
- *
- *-------------------------------------------------------------------------------*/
- public void handleEvent(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-";
- // 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();
- }
- }
- public void handleEvent(LoggedOn event) {
- m_botAction.joinArena(m_botSettings.getString("arena"));
- }
- public void handleEvent(ArenaJoined event) {
- m_botAction.setReliableKills(1);
- initializeBot();
- }
- public void handleEvent(PlayerPosition event) {
- trackPlayer(event);
- }
- /*-------------------------------------------------------------------------------\
- *
- * MAIN ZONE TIMER
- *
- *-------------------------------------------------------------------------------*/
- // Reset playerlist just so it doesnt get filled
- class GameTimer extends TimerTask {
- @Override
- public void run()
- {
- PlayerList = new ArrayList<ShuttlePlayer>();
- }
- }
- /*-------------------------------------------------------------------------------\
- *
- * PLAYER HOUSE FUNCTIONS
- *
- *-------------------------------------------------------------------------------*/
- class PlayerHouse
- {
- public PlayerHouse(String Owner, int xCoord, int yCoord)
- {
- this.m_Owner = Owner;
- this.m_Coords = new int[]{xCoord,yCoord};
- }
- private String m_Owner;
- public String getOwner()
- {
- return m_Owner;
- }
- private int[] m_Coords = new int[2];
- public int[] getHouseCoords()
- {
- return m_Coords;
- }
- }
- /*-------------------------------------------------------------------------------\
- *
- * SHUTTLE PLAYER FUNCTIONS
- *
- *-------------------------------------------------------------------------------*/
- List<ShuttlePlayer> PlayerList = new ArrayList<ShuttlePlayer>();
- class ShuttlePlayer
- {
- public ShuttlePlayer(String PlayerName)
- {
- this.m_PlayerName = PlayerName;
- }
- private String m_PlayerName;
- public String getPlayerName()
- {
- return m_PlayerName;
- }
- private long m_WarpStamp = System.currentTimeMillis();
- public boolean allowedToWarp()
- {
- if (System.currentTimeMillis() - m_WarpStamp < 1500) return false;
- m_WarpStamp = System.currentTimeMillis();
- return true;
- }
- }
- public ShuttlePlayer getPlayer(String PlayerName)
- {
- for(ShuttlePlayer p:PlayerList)
- if (p.getPlayerName().equalsIgnoreCase(PlayerName))
- {
- return p;
- }
- ShuttlePlayer p = new ShuttlePlayer(PlayerName);
- PlayerList.add(p);
- return PlayerList.get(PlayerList.indexOf(p));
- }
- public void trackPlayer(PlayerPosition p)
- {
- ShuttlePlayer sp = getPlayer(b.getPlayerName(p.getPlayerID()));
- if(!sp.allowedToWarp()) return;
- for (int[] i:m_booths)
- {
- if (InRegion(p,i))
- {
- for(PlayerHouse h:Houses)
- {
- if(h.getOwner().toLowerCase().equals(sp.getPlayerName().toLowerCase()))
- {
- b.warpTo(sp.getPlayerName(), h.getHouseCoords()[0], h.getHouseCoords()[1]);
- }
- }
- }
- }
- }
- // Simple collision check - seeing if player is in a given region
- private boolean InRegion( PlayerPosition p, int[] Region)
- { return InRegion(p.getXLocation(),p.getYLocation(),Region); }
- // Simple collision check - seeing if player is in a given region
- private boolean InRegion( int x, int y, int[] Region)
- {
- return (x > Region[0] && x < Region[2] &&
- y > Region[1] && y < Region[3]) ? true:false;
- }
- /*-------------------------------------------------------------------------------\
- *
- * INITIALIZE BOT
- *
- *-------------------------------------------------------------------------------*/
- public void initializeBot()
- {
- String cfg;
- int num = 0;
- while ((cfg = b.getBotSettings().getString("booth" + (num))) != null)
- {
- String[] coord = cfg.trim().split(" ");
- m_booths.add( new int[] {
- Integer.parseInt(coord[0]) * 16,
- Integer.parseInt(coord[1]) * 16,
- Integer.parseInt(coord[2]) * 16,
- Integer.parseInt(coord[3]) * 16 });
- num++;
- }
- num = 0;
- while ((cfg = b.getBotSettings().getString("houseowner" + (num))) != null)
- {
- String[] coord = b.getBotSettings().getString("housecoord" + (num)).trim().split(" ");
- Houses.add(new PlayerHouse(cfg,Integer.parseInt(coord[0]),Integer.parseInt(coord[1])));
- num++;
- }
- b.sendArenaMessage("Booths loaded:[ "+m_booths.size()+" ]. Houses Loaded:[ "+Houses.size()+" ]");
- // Start Main GameTimer
- timer.scheduleAtFixedRate(new GameTimer(),5000 ,300000); //delay in milliseconds
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment