Advertisement
Guest User

SC2 robot

a guest
Mar 11th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.06 KB | None | 0 0
  1. package com.manikfox.robots.sc2;
  2.  
  3. import java.awt.event.KeyEvent;
  4. import java.util.Random;
  5.  
  6. import com.manikfox.robots.libs.RobotLib;
  7.  
  8.  
  9. /**
  10.  * Created with IntelliJ IDEA. User: Christopher Date: 20/09/12 Time: 7:42 PM To change this template use File |
  11.  * Settings | File Templates.
  12.  */
  13. public class CoopWinner
  14. {
  15.  
  16.     static String STARCRAFT = "SC2";
  17.  
  18.     static RobotLib robotLib;
  19.  
  20.     static int INTERVAL_TIME_SHOOT = 50;
  21.  
  22.     public static void main(final String[] args) throws Exception
  23.     {
  24.         final long count = 0;
  25.  
  26.         robotLib = new RobotLib();
  27.  
  28.         //robotLib.bringToFocus(STARCRAFT);
  29.  
  30.         final Random generator = new Random();
  31.  
  32.         //Sleep 10 seconds before starting
  33.         Thread.sleep(10000);
  34.  
  35.         while (true)
  36.         {
  37.  
  38.             robotLib.keyPress(KeyEvent.VK_SPACE);
  39.             Thread.sleep(getRandomNumber(generator));
  40.  
  41.  
  42.         }
  43.     }
  44.  
  45.     private static int getRandomNumber(final Random generator)
  46.     {
  47.         final int random = generator.nextInt() % INTERVAL_TIME_SHOOT;
  48.  
  49.         if (random < 0)
  50.         {
  51.             return random * -1;
  52.         }
  53.  
  54.         return random;
  55.     }
  56.  
  57. }
  58.  
  59.  
  60. package com.manikfox.robots.libs;
  61.  
  62. import javax.imageio.ImageIO;
  63.  
  64. /**
  65.  * Created by IntelliJ IDEA.
  66.  * User: cfox
  67.  * Date: 9/27/12
  68.  * Time: 8:13 AM
  69.  * To change this template use File | Settings | File Templates.
  70.  */
  71. public class RobotLib {
  72.  
  73.     private String PROJECT_LOCATION = getClass().getResource("").getFile() + "../../../../"  ;
  74.  
  75.     final Robot r;
  76.  
  77.     public RobotLib() throws Exception {
  78.         r = new Robot();
  79.     }
  80.  
  81.     public void bringToFocus(String process) throws Exception {
  82.         Process child;
  83.  
  84.         child = Runtime.getRuntime().exec( PROJECT_LOCATION + "bin/cmdow.exe /T");
  85.  
  86.         BufferedReader br = new BufferedReader(new InputStreamReader(child.getInputStream()));
  87.         String line;
  88.  
  89.         while ((line = br.readLine()) != null)
  90.             if (line.contains(process)) {
  91.                 Runtime.getRuntime().exec(PROJECT_LOCATION + "bin\\cmdow.exe " + line.split(" ")[0] + " /ACT");
  92.                 delayMilli(20);
  93.                 Runtime.getRuntime().exec(PROJECT_LOCATION + "bin\\cmdow.exe " + line.split(" ")[0] + " /ACT");
  94.             }
  95.         delaySecs(3);
  96.  
  97.     }
  98.  
  99.     public void keyPress(int key) {
  100.         r.keyPress(key);
  101.         delayMilli(50);
  102.         r.keyRelease(key);
  103.         delayMilli(50);
  104.     }
  105.  
  106.  
  107.     public void mouseRightClick(int x, int y) {
  108.         r.mouseMove(x, y);
  109.         delayMilli(200);
  110.         r.mousePress(InputEvent.BUTTON3_MASK);
  111.         delayMilli(200);
  112.         r.mouseRelease(InputEvent.BUTTON3_MASK);
  113.     }
  114.  
  115.     public void mouseClick(int x, int y) {
  116.         r.mouseMove(x, y);
  117.         delayMilli(30);
  118.         r.mousePress(InputEvent.BUTTON1_MASK);
  119.         delayMilli(30);
  120.         r.mouseRelease(InputEvent.BUTTON1_MASK);
  121.     }
  122.  
  123.     public void delayMilli(int milli) {
  124.         try {
  125.             Thread.sleep(milli);
  126.         } catch (InterruptedException e) {
  127.         }
  128.     }
  129.  
  130.     public void delaySecs(int seconds) {
  131.         delayMilli(seconds * 1000);
  132.     }
  133.  
  134.     public void log(String logMessage, String logFile) {
  135.         try {
  136.             FileWriter outFile = new FileWriter(PROJECT_LOCATION + "output/" + logFile + ".log", true); //opens file
  137.             outFile.append(new Date() + ":  " + logMessage + "\n");
  138.             outFile.close();
  139.         } catch (Exception e) {
  140.             e.printStackTrace();
  141.         }
  142.     }
  143.  
  144.     public boolean isCurrentScreen(int x, int y, Color c) {
  145.         return r.getPixelColor(x, y).getRGB() == c.getRGB();
  146.     }
  147.  
  148.     public void type(String x) {
  149.         int length = x.length();
  150.         for (int i = 0; i < length; i++) {
  151.             char character = x.charAt(i);
  152.             type(character);
  153.         }
  154.     }
  155.  
  156.     public void takeScreenShot(String file) throws Exception {
  157.         Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
  158.         BufferedImage capture = r.createScreenCapture(screenRect);
  159.         ImageIO.write(capture, "bmp", new File(PROJECT_LOCATION + "output\\" + file + ".bmp"));
  160.     }
  161.  
  162.     public void type(char character) {
  163.         switch (character) {
  164.             case 'a':
  165.                 keyPress(KeyEvent.VK_A);
  166.                 break;
  167.             case 'b':
  168.                 keyPress(KeyEvent.VK_B);
  169.                 break;
  170.             case 'c':
  171.                 keyPress(KeyEvent.VK_C);
  172.                 break;
  173.             case 'd':
  174.                 keyPress(KeyEvent.VK_D);
  175.                 break;
  176.             case 'e':
  177.                 keyPress(KeyEvent.VK_E);
  178.                 break;
  179.             case 'f':
  180.                 keyPress(KeyEvent.VK_F);
  181.                 break;
  182.             case 'g':
  183.                 keyPress(KeyEvent.VK_G);
  184.                 break;
  185.             case 'h':
  186.                 keyPress(KeyEvent.VK_H);
  187.                 break;
  188.             case 'i':
  189.                 keyPress(KeyEvent.VK_I);
  190.                 break;
  191.             case 'j':
  192.                 keyPress(KeyEvent.VK_J);
  193.                 break;
  194.             case 'k':
  195.                 keyPress(KeyEvent.VK_K);
  196.                 break;
  197.             case 'l':
  198.                 keyPress(KeyEvent.VK_L);
  199.                 break;
  200.             case 'm':
  201.                 keyPress(KeyEvent.VK_M);
  202.                 break;
  203.             case 'n':
  204.                 keyPress(KeyEvent.VK_N);
  205.                 break;
  206.             case 'o':
  207.                 keyPress(KeyEvent.VK_O);
  208.                 break;
  209.             case 'p':
  210.                 keyPress(KeyEvent.VK_P);
  211.                 break;
  212.             case 'q':
  213.                 keyPress(KeyEvent.VK_Q);
  214.                 break;
  215.             case 'r':
  216.                 keyPress(KeyEvent.VK_R);
  217.                 break;
  218.             case 's':
  219.                 keyPress(KeyEvent.VK_S);
  220.                 break;
  221.             case 't':
  222.                 keyPress(KeyEvent.VK_T);
  223.                 break;
  224.             case 'u':
  225.                 keyPress(KeyEvent.VK_U);
  226.                 break;
  227.             case 'v':
  228.                 keyPress(KeyEvent.VK_V);
  229.                 break;
  230.             case 'w':
  231.                 keyPress(KeyEvent.VK_W);
  232.                 break;
  233.             case 'x':
  234.                 keyPress(KeyEvent.VK_X);
  235.                 break;
  236.             case 'y':
  237.                 keyPress(KeyEvent.VK_Y);
  238.                 break;
  239.             case 'z':
  240.                 keyPress(KeyEvent.VK_Z);
  241.                 break;
  242.             case '`':
  243.                 keyPress(KeyEvent.VK_BACK_QUOTE);
  244.                 break;
  245.             case '0':
  246.                 keyPress(KeyEvent.VK_0);
  247.                 break;
  248.             case '1':
  249.                 keyPress(KeyEvent.VK_1);
  250.                 break;
  251.             case '2':
  252.                 keyPress(KeyEvent.VK_2);
  253.                 break;
  254.             case '3':
  255.                 keyPress(KeyEvent.VK_3);
  256.                 break;
  257.             case '4':
  258.                 keyPress(KeyEvent.VK_4);
  259.                 break;
  260.             case '5':
  261.                 keyPress(KeyEvent.VK_5);
  262.                 break;
  263.             case '6':
  264.                 keyPress(KeyEvent.VK_6);
  265.                 break;
  266.             case '7':
  267.                 keyPress(KeyEvent.VK_7);
  268.                 break;
  269.             case '8':
  270.                 keyPress(KeyEvent.VK_8);
  271.                 break;
  272.             case '9':
  273.                 keyPress(KeyEvent.VK_9);
  274.                 break;
  275.             case '-':
  276.                 keyPress(KeyEvent.VK_MINUS);
  277.                 break;
  278.             case '=':
  279.                 keyPress(KeyEvent.VK_EQUALS);
  280.                 break;
  281.             case '!':
  282.                 keyPress(KeyEvent.VK_EXCLAMATION_MARK);
  283.                 break;
  284.             case '@':
  285.                 keyPress(KeyEvent.VK_AT);
  286.                 break;
  287.             case '#':
  288.                 keyPress(KeyEvent.VK_NUMBER_SIGN);
  289.                 break;
  290.             case '$':
  291.                 keyPress(KeyEvent.VK_DOLLAR);
  292.                 break;
  293.             case '^':
  294.                 keyPress(KeyEvent.VK_CIRCUMFLEX);
  295.                 break;
  296.             case '&':
  297.                 keyPress(KeyEvent.VK_AMPERSAND);
  298.                 break;
  299.             case '*':
  300.                 keyPress(KeyEvent.VK_ASTERISK);
  301.                 break;
  302.             case '(':
  303.                 keyPress(KeyEvent.VK_LEFT_PARENTHESIS);
  304.                 break;
  305.             case ')':
  306.                 keyPress(KeyEvent.VK_RIGHT_PARENTHESIS);
  307.                 break;
  308.             case '_':
  309.                 keyPress(KeyEvent.VK_UNDERSCORE);
  310.                 break;
  311.             case '+':
  312.                 keyPress(KeyEvent.VK_PLUS);
  313.                 break;
  314.             case '\t':
  315.                 keyPress(KeyEvent.VK_TAB);
  316.                 break;
  317.             case '\n':
  318.                 keyPress(KeyEvent.VK_ENTER);
  319.                 break;
  320.             case '[':
  321.                 keyPress(KeyEvent.VK_OPEN_BRACKET);
  322.                 break;
  323.             case ']':
  324.                 keyPress(KeyEvent.VK_CLOSE_BRACKET);
  325.                 break;
  326.             case '\\':
  327.                 keyPress(KeyEvent.VK_BACK_SLASH);
  328.                 break;
  329.             case ';':
  330.                 keyPress(KeyEvent.VK_SEMICOLON);
  331.                 break;
  332.             case ':':
  333.                 keyPress(KeyEvent.VK_COLON);
  334.                 break;
  335.             case '\'':
  336.                 keyPress(KeyEvent.VK_QUOTE);
  337.                 break;
  338.             case '"':
  339.                 keyPress(KeyEvent.VK_QUOTEDBL);
  340.                 break;
  341.             case ',':
  342.                 keyPress(KeyEvent.VK_COMMA);
  343.                 break;
  344.             case '<':
  345.                 keyPress(KeyEvent.VK_LESS);
  346.                 break;
  347.             case '.':
  348.                 keyPress(KeyEvent.VK_PERIOD);
  349.                 break;
  350.             case '>':
  351.                 keyPress(KeyEvent.VK_GREATER);
  352.                 break;
  353.             case '/':
  354.                 keyPress(KeyEvent.VK_SLASH);
  355.                 break;
  356.             case ' ':
  357.                 keyPress(KeyEvent.VK_SPACE);
  358.                 break;
  359.             default:
  360.                 throw new IllegalArgumentException("Cannot type character " + character);
  361.         }
  362.     }
  363.  
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement