Skip to content

Commit

Permalink
Merge pull request #6 from mrbesen/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
mrbesen authored Nov 26, 2017
2 parents 2ba3e83 + 487a263 commit 1b1dff4
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 53 deletions.
102 changes: 57 additions & 45 deletions src/mrbesen/cr/auto/clicker/Clicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,21 @@ public class Clicker implements Runnable{
private final int waittime = 50;//time between mouse teleports and clicks

private int mincolordistance = 35;
private Overlay ov = null;

OSType os;
private int alt_key;

long started = -1;

private void sleep( int ms) {
//update ui
Main.get().ui.printTime((int) ( (System.currentTimeMillis()-started) / 1000 ));

if(skipbattle)
return;
try {
if(ms > 1000) {
Thread.sleep(1000);
Thread.sleep(1000);//split the sleep time.
sleep(ms-1000);
} else
Thread.sleep(ms);
Expand Down Expand Up @@ -93,33 +94,27 @@ public boolean isRunning() {
public void run() {
sleep(1000);//chill ma

//determine os
String oss = System.getProperty("os.name").toLowerCase();
if(oss.contains("nix") | oss.contains("nux") | oss.contains("aix"))
os = OSType.Linux;
else if(oss.contains("win"))
os = OSType.Windows;
else if(oss.contains("mac"))
os = OSType.OSX;
else
Main.get().ui.info("OS not supported for backfocus: " + oss);

alt_key = ( oss.contains("nix") | oss.contains("nux") | oss.contains("aix") | oss.contains("win") ? KeyEvent.VK_ALT : KeyEvent.VK_META);
//windows and linux have another alt key than mac, so lets determine the OS type, to determine the key code.

int card = 0;
try {
Robot rob = new Robot();
while(should_run) {
Main.get().ui.info("Starting Battle.");
sleep(500);
clickL(rob, battle);//smash the start button
sleep(1000);
clickL(rob, battle);//press start again (if there is an alert poping up)
backfocus(rob);
//battle is starting up
sleep(9000);//wait for the battle to start (loading screen)
Main.get().ui.info("Battle started.");
inbattle = true;
float modifier = 1;
long start = System.currentTimeMillis();
long lastwait = start;//actions like moving mouse and do stuff gets messured and subtracted of the wait's
Main.get().ui.info("Battle begins.");
while( ((System.currentTimeMillis() - start) / 6000) < 41 & should_run & !skipbattle) {

//check für ok-button
Expand Down Expand Up @@ -169,13 +164,13 @@ else if(round(start) >= (115 - (truppenwait / 2))) //remove half waittime and do
clickL(rob, end);//ok button
backfocus(rob);
Main.get().ui.info("Battle ended.");
sleep(9000);//9 sec-loading screen
sleep(7000);//7 sec-loading screen
//checken, ob Arena wechsel pop-up
while(checkOK(arena_switch, rob,arena_view) & should_run) {
System.out.println("Arena found, clicking");
Main.get().ui.info("Arena found, clicking");
clickL(rob, arena_switch);
backfocus(rob);
sleep(2000);
sleep(2000);//wait 2 seconds
}
}
} catch (AWTException e) {
Expand Down Expand Up @@ -207,18 +202,14 @@ private void playout(int card, Robot rob) {

private void backfocus(Robot bot) {
if(backfocus) {
if(os == OSType.Windows | os == OSType.Linux)
bot.keyPress(KeyEvent.VK_ALT);
else//osx / unsupported
bot.keyPress(KeyEvent.VK_META);


bot.keyPress(alt_key);

bot.keyPress(KeyEvent.VK_TAB);
sleep(waittime);
bot.keyRelease(KeyEvent.VK_TAB);
if(os == OSType.Windows | os == OSType.Linux)
bot.keyRelease(KeyEvent.VK_ALT);
else//osx / unsupported
bot.keyRelease(KeyEvent.VK_META);

bot.keyRelease(alt_key);
}
}

Expand Down Expand Up @@ -286,18 +277,26 @@ public boolean bothset() {
return (end != null & battle != null);
}

/**
* Perform a Leftclick.
* @param b the Robot to use
* @param a the Position to click
*/
private void clickL(Robot b, Point a) {
if(!should_run)
return;
Point old = getMouse();
b.mouseMove(a.x, a.y);
sleep(50);
sleep(waittime);
clickL(b);
sleep(50);
b.mouseMove(old.x, old.y);
sleep(50);
sleep(waittime);
}

/**
* Perform a Leftclick
* @param b
*/
private void clickL(Robot b) {//40 ms delay
b.mousePress(InputEvent.BUTTON1_MASK);
sleep(waittime);
Expand All @@ -323,22 +322,22 @@ private boolean checkOK(Point p, Robot bot, Color goalcolor) {
return false;
//long start = System.currentTimeMillis();
int count = 0;
BufferedImage img = bot.createScreenCapture(new Rectangle(p.x-10, p.y-10, 20, 20));//smile
for (int x = 0; x < 20; x++) {
for (int y = 0; y < 20; y++) {
BufferedImage img = bot.createScreenCapture(getRect(p.x, p.y));//smile
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
int color = img.getRGB(x, y);
int red = (color & 0x00ff0000) >> 16;
int green = (color & 0x0000ff00) >> 8;
int blue = color & 0x000000ff;
double distance = Math.sqrt(Math.pow((blue - goalcolor.getBlue()), 2)
int green = (color & 0x0000ff00) >> 8;
int blue = color & 0x000000ff;
double distance = Math.sqrt(Math.pow((blue - goalcolor.getBlue()), 2)
+ Math.pow((red - goalcolor.getRed()), 2) + Math.pow((green - goalcolor.getGreen()), 2));//calculate the distance between the goalcolor and the test color
if (distance < mincolordistance)
count++;
if (distance < mincolordistance)
count++;
}
}

// System.out.println("counts: " + count);//some performance checking
return count > 70;
System.out.println("counts: " + count);//some performance checking
return count > 4;//engough pixel have the right color
}

public Point getMouse() {
Expand Down Expand Up @@ -374,7 +373,7 @@ public void setColor(Color c, int colornum, int minimumdistance) {
arena_view = c;
break;
}
if(mincolordistance < minimumdistance)
if(mincolordistance < minimumdistance)//enlarging mindistance
mincolordistance = minimumdistance;
System.out.println(colornum + ": "+c.getRed() + " " + c.getGreen() + " " + c.getBlue());
}
Expand All @@ -388,10 +387,23 @@ public void setPause(boolean b) {
paused = b;
}

private enum OSType {
Linux,
Windows,
OSX,
unsupported
public static Rectangle getRect(int x, int y) {
return new Rectangle(x-2, y-2, 5, 5);
}

public void toggleOverlay() {
if(ov == null) {
try {
ov = new Overlay();
ov.set(playout, cardslots, end, battle, arena_switch);
ov.init();
} catch(Exception e) {
System.out.println("Catched Exception, while inflateing Overlay: ");
e.printStackTrace();
}
} else {
ov.close();
ov = null;
}
}
}
86 changes: 86 additions & 0 deletions src/mrbesen/cr/auto/clicker/Overlay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package mrbesen.cr.auto.clicker;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;

public class Overlay {

JFrame frame;
Point spawn;
Point cards[];
Point ok;
Point battle;
Point arenaview;

public Overlay() {
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
if(frame != null) {
frame.dispose();
}
}
}, "Shutdownhook-Overlaycloser"));
}

void set(Point spawn, Point[] cards, Point ok, Point battle, Point arenaview) {
this.spawn = spawn;
this.cards = cards;
this.ok = ok;
this.battle = battle;
this.arenaview = arenaview;
}

public void init() {
frame = new JFrame("Bot Overlay");
frame.setUndecorated(true);
frame.setOpacity(0.5f);

int width=0, height=0;
int x=spawn.x,y=spawn.y;
for(Point p : getlist()) {
if(p != null) {
if(p.x < x) {
width += (x-p.x);//umsoviel weiter machen, wie nach links verschoben wird
x = p.x;
}
if(x+width < p.x) {
width += x+width-p.x;
}
if(p.y < y) {
height += (y-p.y);
y = p.y;
}
if(y+height < p.y) {
height += y+height-p.y;
}
}
}

frame.setSize(width, height);
frame.setLocation(x, y);

frame.setVisible(true);

Graphics gra = frame.getGraphics();
gra.setColor(new Color(255, 0, 0));//red
for(Point p : cards) {
if(p != null) {
gra.drawRect(p.x-1, p.y-1, 300, 300);
}
}
System.out.println("Overlay is da!");
}

public void close() {
frame.dispose();
}


private Point[] getlist() {
return new Point[] {spawn,ok,battle,arenaview, cards[0],cards[1],cards[2],cards[3]};
}

}
13 changes: 6 additions & 7 deletions src/mrbesen/cr/auto/clicker/PosColSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.util.Comparator;
Expand All @@ -24,14 +23,14 @@ public void run() {
try {
Robot rob = new Robot();
Point p = ui.bot.getMouse();
BufferedImage img = rob.createScreenCapture(new Rectangle(p.x-10, p.y-10, 20, 20));
BufferedImage img = rob.createScreenCapture(Clicker.getRect(p.x, p.y));
//calculate avg color;
int red = 0;
int green = 0;
int blue = 0;
int count = 0;
for (int x = 0; x < 20; x++) {
for (int y = 0; y < 20; y++) {
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
int color = img.getRGB(x, y);
red += (color & 0x00ff0000) >> 16;
green += (color & 0x0000ff00) >> 8;
Expand All @@ -46,8 +45,8 @@ public void run() {

//calculate distances:
List<Integer> dist = new LinkedList<Integer>();
for (int x = 0; x < 20; x++) {
for (int y = 0; y < 20; y++) {
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
int color = img.getRGB(x, y);
int redf = (color & 0x00ff0000) >> 16;
int greenf = (color & 0x0000ff00) >> 8;
Expand All @@ -72,7 +71,7 @@ public int compare(Integer o1, Integer o2) {
}
});

int miniumdistance = dist.get(150);//at least the first 100 tests should fit
int miniumdistance = dist.get((int) (dist.size()*0.3f));//at least the first third tests should fit
// int maximumdistance = dist.get(dist.size()-1);
System.out.println("minimum distance: " + miniumdistance );
ui.bot.setColor(c,colornum, miniumdistance);
Expand Down
1 change: 1 addition & 0 deletions src/mrbesen/cr/auto/clicker/PosSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public PosSelector(UI ui, String text, boolean required, int num) {

@Override
public void run() {
button.setBackground(Color.MAGENTA);
try {
for(int i = 5; i > 0; i--) {//countdown
ui.info(i + "");
Expand Down
Loading

0 comments on commit 1b1dff4

Please sign in to comment.