PsyOps

shuttlebot.java

May 29th, 2014
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.57 KB | None | 0 0
  1. package twcore.bots.shuttlebot;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Timer;
  6. import java.util.TimerTask;
  7.  
  8. import twcore.core.BotAction;
  9. import twcore.core.BotSettings;
  10. import twcore.core.EventRequester;
  11. import twcore.core.SubspaceBot;
  12. import twcore.core.events.ArenaJoined;
  13. import twcore.core.events.LoggedOn;
  14. import twcore.core.events.Message;
  15. import twcore.core.events.PlayerPosition;
  16.  
  17. public class shuttlebot extends SubspaceBot {
  18.  
  19.     private BotSettings m_botSettings;
  20.  
  21.     public shuttlebot(BotAction botAction) {
  22.         super(botAction);
  23.         requestEvents();
  24.  
  25.         // m_botSettings contains the data specified in file <botname>.cfg
  26.         m_botSettings = m_botAction.getBotSettings();
  27.         b = m_botAction;
  28.     }
  29.  
  30.     public void requestEvents() {
  31.         EventRequester req = m_botAction.getEventRequester();
  32.         req.request(EventRequester.MESSAGE);
  33.         req.request(EventRequester.ARENA_JOINED);
  34.         req.request(EventRequester.PLAYER_POSITION);
  35.         req.request(EventRequester.LOGGED_ON);
  36.     }
  37.     /*-------------------------------------------------------------------------------\
  38.      *
  39.      *                             NEEDED VARS
  40.      *                            
  41.      *-------------------------------------------------------------------------------*/
  42.     // Used for Game timer
  43.     Timer timer = new Timer();
  44.     BotAction b;
  45.  
  46.     // Master list of houses
  47.     List<PlayerHouse> Houses = new ArrayList<PlayerHouse>();
  48.     // Master list of all booths
  49.     List<int[]> m_booths = new ArrayList<int[]>();
  50.     /*-------------------------------------------------------------------------------\
  51.      *
  52.      *                             SUBSPACE EVENTS
  53.      *                            
  54.      *-------------------------------------------------------------------------------*/
  55.     public void handleEvent(Message event) {
  56.         // Retreive name. If the message is remote, then event.getMessager() returns null, and event.getPlayerID returns a value.
  57.         // If the message is from the same arena, event.getMessager() returns a string, and event.getPlayerID will return 0.
  58.         String name = event.getMessager() != null ? event.getMessager() : m_botAction.getPlayerName(event.getPlayerID());
  59.         if (name == null) name = "-anonymous-";
  60.  
  61.         // Default implemented command: !die
  62.         if (event.getMessageType() == Message.PRIVATE_MESSAGE && event.getMessage().equalsIgnoreCase("!die")) {
  63.             //m_botAction.sendPublicMessage(name + " commanded me to die. Disconnecting...");
  64.             try { Thread.sleep(50); } catch (Exception e) {};
  65.             m_botAction.die();
  66.         }
  67.     }
  68.  
  69.     public void handleEvent(LoggedOn event) {
  70.         m_botAction.joinArena(m_botSettings.getString("arena"));
  71.     }
  72.  
  73.     public void handleEvent(ArenaJoined event) {
  74.         m_botAction.setReliableKills(1);
  75.         initializeBot();
  76.     }
  77.    
  78.     public void handleEvent(PlayerPosition event) {
  79.         trackPlayer(event);
  80.     }
  81.     /*-------------------------------------------------------------------------------\
  82.      *
  83.      *                             MAIN ZONE TIMER
  84.      *                            
  85.      *-------------------------------------------------------------------------------*/
  86.    // Reset playerlist just so it doesnt get filled
  87.     class GameTimer extends TimerTask {
  88.         @Override
  89.         public void run()
  90.         {
  91.             PlayerList = new ArrayList<ShuttlePlayer>();
  92.         }
  93.     }
  94.     /*-------------------------------------------------------------------------------\
  95.      *
  96.      *                        PLAYER HOUSE FUNCTIONS
  97.      *                            
  98.      *-------------------------------------------------------------------------------*/
  99.     class PlayerHouse
  100.     {
  101.         public PlayerHouse(String Owner, int xCoord, int yCoord)
  102.         {
  103.             this.m_Owner = Owner;
  104.             this.m_Coords = new int[]{xCoord,yCoord};
  105.         }
  106.        
  107.         private String m_Owner;
  108.         public String getOwner()
  109.         {
  110.             return m_Owner;
  111.         }
  112.        
  113.         private int[] m_Coords = new int[2];
  114.         public int[] getHouseCoords()
  115.         {
  116.             return m_Coords;
  117.         }
  118.     }
  119.     /*-------------------------------------------------------------------------------\
  120.      *
  121.      *                        SHUTTLE PLAYER FUNCTIONS
  122.      *                            
  123.      *-------------------------------------------------------------------------------*/
  124.     List<ShuttlePlayer> PlayerList = new ArrayList<ShuttlePlayer>();
  125.     class ShuttlePlayer
  126.     {
  127.         public ShuttlePlayer(String PlayerName)
  128.         {
  129.             this.m_PlayerName = PlayerName;
  130.         }
  131.         private String m_PlayerName;
  132.         public String getPlayerName()
  133.         {
  134.             return m_PlayerName;
  135.         }
  136.        
  137.         private long m_WarpStamp = System.currentTimeMillis();
  138.         public boolean allowedToWarp()
  139.         {
  140.             if (System.currentTimeMillis() - m_WarpStamp < 1500) return false;
  141.             m_WarpStamp = System.currentTimeMillis();
  142.             return true;
  143.         }
  144.     }
  145.     public ShuttlePlayer getPlayer(String PlayerName)
  146.     {
  147.         for(ShuttlePlayer p:PlayerList)
  148.             if (p.getPlayerName().equalsIgnoreCase(PlayerName))
  149.             {
  150.                 return p;
  151.             }
  152.        
  153.         ShuttlePlayer p = new ShuttlePlayer(PlayerName);
  154.         PlayerList.add(p);
  155.         return PlayerList.get(PlayerList.indexOf(p));
  156.     }
  157.     public void trackPlayer(PlayerPosition p)
  158.     {
  159.         ShuttlePlayer sp = getPlayer(b.getPlayerName(p.getPlayerID()));
  160.        
  161.         if(!sp.allowedToWarp()) return;
  162.        
  163.         for (int[] i:m_booths)
  164.         {
  165.             if (InRegion(p,i))
  166.             {
  167.                 for(PlayerHouse h:Houses)
  168.                 {
  169.                     if(h.getOwner().toLowerCase().equals(sp.getPlayerName().toLowerCase()))
  170.                     {
  171.                         b.warpTo(sp.getPlayerName(), h.getHouseCoords()[0], h.getHouseCoords()[1]);
  172.                     }
  173.                 }
  174.             }
  175.         }
  176.     }
  177.     // Simple collision check - seeing if player is in a given region
  178.     private boolean InRegion( PlayerPosition p, int[] Region)
  179.     {   return InRegion(p.getXLocation(),p.getYLocation(),Region);  }
  180.     // Simple collision check - seeing if player is in a given region
  181.     private boolean InRegion( int x, int y, int[] Region)
  182.     {
  183.         return (x > Region[0] && x < Region[2] &&
  184.                 y > Region[1] && y < Region[3]) ? true:false;
  185.     }
  186.     /*-------------------------------------------------------------------------------\
  187.      *
  188.      *                             INITIALIZE BOT
  189.      *                            
  190.      *-------------------------------------------------------------------------------*/
  191.     public void initializeBot()
  192.     {
  193.         String cfg;
  194.         int num = 0;
  195.  
  196.         while ((cfg = b.getBotSettings().getString("booth" + (num))) != null)
  197.         {
  198.             String[] coord = cfg.trim().split(" ");
  199.             m_booths.add( new int[] {
  200.                     Integer.parseInt(coord[0]) * 16,
  201.                     Integer.parseInt(coord[1]) * 16,
  202.                     Integer.parseInt(coord[2]) * 16,
  203.                     Integer.parseInt(coord[3]) * 16 });
  204.             num++;
  205.         }
  206.        
  207.         num = 0;
  208.        
  209.         while ((cfg = b.getBotSettings().getString("houseowner" + (num))) != null)
  210.         {
  211.             String[] coord = b.getBotSettings().getString("housecoord" + (num)).trim().split(" ");
  212.             Houses.add(new PlayerHouse(cfg,Integer.parseInt(coord[0]),Integer.parseInt(coord[1])));
  213.             num++;
  214.         }
  215.        
  216.         b.sendArenaMessage("Booths loaded:[ "+m_booths.size()+" ]. Houses Loaded:[ "+Houses.size()+" ]");
  217.        
  218.         // Start Main GameTimer
  219.         timer.scheduleAtFixedRate(new GameTimer(),5000 ,300000); //delay in milliseconds
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment