Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.manikfox.robots.sc2;
- import java.awt.event.KeyEvent;
- import java.util.Random;
- import com.manikfox.robots.libs.RobotLib;
- /**
- * Created with IntelliJ IDEA. User: Christopher Date: 20/09/12 Time: 7:42 PM To change this template use File |
- * Settings | File Templates.
- */
- public class CoopWinner
- {
- static String STARCRAFT = "SC2";
- static RobotLib robotLib;
- static int INTERVAL_TIME_SHOOT = 50;
- public static void main(final String[] args) throws Exception
- {
- final long count = 0;
- robotLib = new RobotLib();
- //robotLib.bringToFocus(STARCRAFT);
- final Random generator = new Random();
- //Sleep 10 seconds before starting
- Thread.sleep(10000);
- while (true)
- {
- robotLib.keyPress(KeyEvent.VK_SPACE);
- Thread.sleep(getRandomNumber(generator));
- }
- }
- private static int getRandomNumber(final Random generator)
- {
- final int random = generator.nextInt() % INTERVAL_TIME_SHOOT;
- if (random < 0)
- {
- return random * -1;
- }
- return random;
- }
- }
- package com.manikfox.robots.libs;
- import javax.imageio.ImageIO;
- /**
- * Created by IntelliJ IDEA.
- * User: cfox
- * Date: 9/27/12
- * Time: 8:13 AM
- * To change this template use File | Settings | File Templates.
- */
- public class RobotLib {
- private String PROJECT_LOCATION = getClass().getResource("").getFile() + "../../../../" ;
- final Robot r;
- public RobotLib() throws Exception {
- r = new Robot();
- }
- public void bringToFocus(String process) throws Exception {
- Process child;
- child = Runtime.getRuntime().exec( PROJECT_LOCATION + "bin/cmdow.exe /T");
- BufferedReader br = new BufferedReader(new InputStreamReader(child.getInputStream()));
- String line;
- while ((line = br.readLine()) != null)
- if (line.contains(process)) {
- Runtime.getRuntime().exec(PROJECT_LOCATION + "bin\\cmdow.exe " + line.split(" ")[0] + " /ACT");
- delayMilli(20);
- Runtime.getRuntime().exec(PROJECT_LOCATION + "bin\\cmdow.exe " + line.split(" ")[0] + " /ACT");
- }
- delaySecs(3);
- }
- public void keyPress(int key) {
- r.keyPress(key);
- delayMilli(50);
- r.keyRelease(key);
- delayMilli(50);
- }
- public void mouseRightClick(int x, int y) {
- r.mouseMove(x, y);
- delayMilli(200);
- r.mousePress(InputEvent.BUTTON3_MASK);
- delayMilli(200);
- r.mouseRelease(InputEvent.BUTTON3_MASK);
- }
- public void mouseClick(int x, int y) {
- r.mouseMove(x, y);
- delayMilli(30);
- r.mousePress(InputEvent.BUTTON1_MASK);
- delayMilli(30);
- r.mouseRelease(InputEvent.BUTTON1_MASK);
- }
- public void delayMilli(int milli) {
- try {
- Thread.sleep(milli);
- } catch (InterruptedException e) {
- }
- }
- public void delaySecs(int seconds) {
- delayMilli(seconds * 1000);
- }
- public void log(String logMessage, String logFile) {
- try {
- FileWriter outFile = new FileWriter(PROJECT_LOCATION + "output/" + logFile + ".log", true); //opens file
- outFile.append(new Date() + ": " + logMessage + "\n");
- outFile.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public boolean isCurrentScreen(int x, int y, Color c) {
- return r.getPixelColor(x, y).getRGB() == c.getRGB();
- }
- public void type(String x) {
- int length = x.length();
- for (int i = 0; i < length; i++) {
- char character = x.charAt(i);
- type(character);
- }
- }
- public void takeScreenShot(String file) throws Exception {
- Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
- BufferedImage capture = r.createScreenCapture(screenRect);
- ImageIO.write(capture, "bmp", new File(PROJECT_LOCATION + "output\\" + file + ".bmp"));
- }
- public void type(char character) {
- switch (character) {
- case 'a':
- keyPress(KeyEvent.VK_A);
- break;
- case 'b':
- keyPress(KeyEvent.VK_B);
- break;
- case 'c':
- keyPress(KeyEvent.VK_C);
- break;
- case 'd':
- keyPress(KeyEvent.VK_D);
- break;
- case 'e':
- keyPress(KeyEvent.VK_E);
- break;
- case 'f':
- keyPress(KeyEvent.VK_F);
- break;
- case 'g':
- keyPress(KeyEvent.VK_G);
- break;
- case 'h':
- keyPress(KeyEvent.VK_H);
- break;
- case 'i':
- keyPress(KeyEvent.VK_I);
- break;
- case 'j':
- keyPress(KeyEvent.VK_J);
- break;
- case 'k':
- keyPress(KeyEvent.VK_K);
- break;
- case 'l':
- keyPress(KeyEvent.VK_L);
- break;
- case 'm':
- keyPress(KeyEvent.VK_M);
- break;
- case 'n':
- keyPress(KeyEvent.VK_N);
- break;
- case 'o':
- keyPress(KeyEvent.VK_O);
- break;
- case 'p':
- keyPress(KeyEvent.VK_P);
- break;
- case 'q':
- keyPress(KeyEvent.VK_Q);
- break;
- case 'r':
- keyPress(KeyEvent.VK_R);
- break;
- case 's':
- keyPress(KeyEvent.VK_S);
- break;
- case 't':
- keyPress(KeyEvent.VK_T);
- break;
- case 'u':
- keyPress(KeyEvent.VK_U);
- break;
- case 'v':
- keyPress(KeyEvent.VK_V);
- break;
- case 'w':
- keyPress(KeyEvent.VK_W);
- break;
- case 'x':
- keyPress(KeyEvent.VK_X);
- break;
- case 'y':
- keyPress(KeyEvent.VK_Y);
- break;
- case 'z':
- keyPress(KeyEvent.VK_Z);
- break;
- case '`':
- keyPress(KeyEvent.VK_BACK_QUOTE);
- break;
- case '0':
- keyPress(KeyEvent.VK_0);
- break;
- case '1':
- keyPress(KeyEvent.VK_1);
- break;
- case '2':
- keyPress(KeyEvent.VK_2);
- break;
- case '3':
- keyPress(KeyEvent.VK_3);
- break;
- case '4':
- keyPress(KeyEvent.VK_4);
- break;
- case '5':
- keyPress(KeyEvent.VK_5);
- break;
- case '6':
- keyPress(KeyEvent.VK_6);
- break;
- case '7':
- keyPress(KeyEvent.VK_7);
- break;
- case '8':
- keyPress(KeyEvent.VK_8);
- break;
- case '9':
- keyPress(KeyEvent.VK_9);
- break;
- case '-':
- keyPress(KeyEvent.VK_MINUS);
- break;
- case '=':
- keyPress(KeyEvent.VK_EQUALS);
- break;
- case '!':
- keyPress(KeyEvent.VK_EXCLAMATION_MARK);
- break;
- case '@':
- keyPress(KeyEvent.VK_AT);
- break;
- case '#':
- keyPress(KeyEvent.VK_NUMBER_SIGN);
- break;
- case '$':
- keyPress(KeyEvent.VK_DOLLAR);
- break;
- case '^':
- keyPress(KeyEvent.VK_CIRCUMFLEX);
- break;
- case '&':
- keyPress(KeyEvent.VK_AMPERSAND);
- break;
- case '*':
- keyPress(KeyEvent.VK_ASTERISK);
- break;
- case '(':
- keyPress(KeyEvent.VK_LEFT_PARENTHESIS);
- break;
- case ')':
- keyPress(KeyEvent.VK_RIGHT_PARENTHESIS);
- break;
- case '_':
- keyPress(KeyEvent.VK_UNDERSCORE);
- break;
- case '+':
- keyPress(KeyEvent.VK_PLUS);
- break;
- case '\t':
- keyPress(KeyEvent.VK_TAB);
- break;
- case '\n':
- keyPress(KeyEvent.VK_ENTER);
- break;
- case '[':
- keyPress(KeyEvent.VK_OPEN_BRACKET);
- break;
- case ']':
- keyPress(KeyEvent.VK_CLOSE_BRACKET);
- break;
- case '\\':
- keyPress(KeyEvent.VK_BACK_SLASH);
- break;
- case ';':
- keyPress(KeyEvent.VK_SEMICOLON);
- break;
- case ':':
- keyPress(KeyEvent.VK_COLON);
- break;
- case '\'':
- keyPress(KeyEvent.VK_QUOTE);
- break;
- case '"':
- keyPress(KeyEvent.VK_QUOTEDBL);
- break;
- case ',':
- keyPress(KeyEvent.VK_COMMA);
- break;
- case '<':
- keyPress(KeyEvent.VK_LESS);
- break;
- case '.':
- keyPress(KeyEvent.VK_PERIOD);
- break;
- case '>':
- keyPress(KeyEvent.VK_GREATER);
- break;
- case '/':
- keyPress(KeyEvent.VK_SLASH);
- break;
- case ' ':
- keyPress(KeyEvent.VK_SPACE);
- break;
- default:
- throw new IllegalArgumentException("Cannot type character " + character);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement