diff --git a/DivaBot/.gitignore b/DivaBot/.gitignore index 6413e54..e8276ca 100644 --- a/DivaBot/.gitignore +++ b/DivaBot/.gitignore @@ -11,3 +11,5 @@ /39/ /1925/ /39みゅーじっく!/ +*.png +!/*.png \ No newline at end of file diff --git a/DivaBot/rectangle0.png b/DivaBot/rectangle0.png new file mode 100644 index 0000000..41ff5ef Binary files /dev/null and b/DivaBot/rectangle0.png differ diff --git a/DivaBot/rectangle1.png b/DivaBot/rectangle1.png new file mode 100644 index 0000000..4e6a7f8 Binary files /dev/null and b/DivaBot/rectangle1.png differ diff --git a/DivaBot/rectangle2.png b/DivaBot/rectangle2.png new file mode 100644 index 0000000..4b214e1 Binary files /dev/null and b/DivaBot/rectangle2.png differ diff --git a/DivaBot/rectangle3.png b/DivaBot/rectangle3.png new file mode 100644 index 0000000..3e88fe4 Binary files /dev/null and b/DivaBot/rectangle3.png differ diff --git a/DivaBot/rectangle4.png b/DivaBot/rectangle4.png new file mode 100644 index 0000000..020f9d1 Binary files /dev/null and b/DivaBot/rectangle4.png differ diff --git a/DivaBot/src/sig/CustomRobot.java b/DivaBot/src/sig/CustomRobot.java new file mode 100644 index 0000000..265be64 --- /dev/null +++ b/DivaBot/src/sig/CustomRobot.java @@ -0,0 +1,43 @@ +package sig; + +import java.awt.AWTException; +import java.awt.GraphicsDevice; +import java.awt.Rectangle; +import java.awt.Robot; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; + +import sig.utils.ImageUtils; + +public class CustomRobot extends Robot{ + + BufferedImage currentScreen; + BufferedImage scoreCurrentScreen; + + public CustomRobot() throws AWTException { + super(); + } + + public CustomRobot(GraphicsDevice screen) throws AWTException { + super(screen); + } + + public void refreshScreen() { + currentScreen = super.createScreenCapture(new Rectangle(418+18,204+83,912-18,586-83)); + } + public void refreshScoreScreen() { + scoreCurrentScreen = super.createScreenCapture(new Rectangle(418+23,204+85,912-18,586-83)); + } + + public synchronized BufferedImage createScreenCapture(Rectangle r) { + BufferedImage img2 = ImageUtils.toBufferedImage(currentScreen.getScaledInstance(1227, 690, BufferedImage.SCALE_AREA_AVERAGING)); + return img2.getSubimage(r.x-418, r.y-204, r.width, r.height); + } + public synchronized BufferedImage createNormalScreenCapture(Rectangle r) { + return currentScreen.getSubimage((int)((r.x-418)*(894d/1227)), (int)((r.y-204)*(503d/690)), (int)Math.ceil(r.width*(894d/1227)), (int)Math.ceil(r.height*(503d/690))); + //return super.createScreenCapture(new Rectangle(r.x-418,r.y-204,912-18,586-83)); + } +} diff --git a/DivaBot/src/sig/DrawCanvas.java b/DivaBot/src/sig/DrawCanvas.java index 25db34a..9b14741 100644 --- a/DivaBot/src/sig/DrawCanvas.java +++ b/DivaBot/src/sig/DrawCanvas.java @@ -57,7 +57,9 @@ public class DrawCanvas extends JPanel implements KeyListener{ BufferedImage overallbar; BufferedImage panel,paneloverlay,songpanel,songpaneloverlay; long ratingTime = System.currentTimeMillis()-10000; + long bestPlayTime = System.currentTimeMillis()-10000; int lastRating = -1; + double lastScore = 0d; Thread t = null; boolean scrolling = false; int scrollX = 0; @@ -111,7 +113,7 @@ public class DrawCanvas extends JPanel implements KeyListener{ passes=0; fcCount=0; artist=""; - this.repaint(0,0,1400,100); + this.repaint(0,0,1400,1000); if (t!=null && t.isAlive()) { t.stop(); } @@ -132,6 +134,7 @@ public class DrawCanvas extends JPanel implements KeyListener{ JSONObject obj = FileUtils.readJsonFromUrl("http://45.33.13.215:4501/bestplay/sigonasr2/"+URLEncoder.encode(MyRobot.p.songname, StandardCharsets.UTF_8.toString()).replaceAll("\\+", "%20")+"/"+difficulty); if (obj.has("cool")) { bestPlay = new Result(MyRobot.p.songname,difficulty,obj.getInt("cool"),obj.getInt("fine"),obj.getInt("safe"),obj.getInt("sad"),obj.getInt("worst"),(float)obj.getDouble("percent")); + lastScore = obj.getDouble("score"); } else { bestPlay = null; } @@ -182,11 +185,21 @@ public class DrawCanvas extends JPanel implements KeyListener{ g2.drawImage(panel, 484,935,null); g2.drawImage(panel, 968,935,null); - DrawUtils.drawOutlineText(g2, programFont, 8, 42, 1, Color.WHITE, new Color(0,0,0,64), ((romanizedname.length()>0)?romanizedname:englishname)); - - DrawUtils.drawOutlineText(g2, programFontSmall, 8, 935+42, 1, Color.WHITE, new Color(0,0,0,64),((bestPlay!=null)?bestPlay.display():"")); + String songDisplay = ((romanizedname.length()>0)?romanizedname:englishname) + " - " + artist; + Rectangle2D bounds = TextUtils.calculateStringBoundsFont(songDisplay, programFont); + if (bounds.getWidth()>675) { + DrawUtils.drawOutlineText(g2, programFontSmall, 8, 42, 1, Color.WHITE, new Color(0,0,0,64), songDisplay); + } else { + DrawUtils.drawOutlineText(g2, programFont, 8, 42, 1, Color.WHITE, new Color(0,0,0,64), songDisplay); + } + + if ((bestPlayTime>System.currentTimeMillis()-10000)) { + DrawUtils.drawOutlineText(g2, programFont, 8, 935+42, 1, new Color(220,220,255,(int)Math.min(((System.currentTimeMillis()-bestPlayTime))/5,255)), new Color(0,0,0,64),"New Record!"); + } else { + DrawUtils.drawOutlineText(g2, programFontSmall, 8, 935+42, 1, Color.WHITE, new Color(0,0,0,64),((bestPlay!=null)?bestPlay.display():"")); + } if ((ratingTime>System.currentTimeMillis()-10000)) { - DrawUtils.drawOutlineText(g2, programFont, 484+8, 935+42, 1, new Color(220,220,255,(int)Math.min(((System.currentTimeMillis()-ratingTime))/5,255)), new Color(0,0,0,64),"Rating up! "+lastRating+" -> "+overallrating); + DrawUtils.drawOutlineText(g2, programFontSmall, 484+8, 935+42, 1, new Color(220,220,255,(int)Math.min(((System.currentTimeMillis()-ratingTime))/5,255)), new Color(0,0,0,64),"Rating up! "+lastRating+" -> "+overallrating); } else { DrawUtils.drawOutlineText(g2, programFont, 484+8, 935+42, 1, Color.WHITE, new Color(0,0,0,64),Integer.toString(overallrating)); } diff --git a/DivaBot/src/sig/Filter.java b/DivaBot/src/sig/Filter.java new file mode 100644 index 0000000..29ffcf1 --- /dev/null +++ b/DivaBot/src/sig/Filter.java @@ -0,0 +1,22 @@ +package sig; + +import java.awt.Color; +import java.awt.Point; + +public class Filter { + Point red_threshold; + Point green_threshold; + Point blue_threshold; + + public Filter(Point red_threshold, Point green_threshold, Point blue_threshold) { + this.red_threshold = red_threshold; + this.green_threshold = green_threshold; + this.blue_threshold = blue_threshold; + } + + public boolean isWithinThreshold(Color c) { + return c.getRed()>=red_threshold.x && c.getRed()<=red_threshold.y && + c.getGreen()>=green_threshold.x && c.getGreen()<=green_threshold.y && + c.getBlue()>=blue_threshold.x && c.getBlue()<=blue_threshold.y; + } +} diff --git a/DivaBot/src/sig/MyRobot.java b/DivaBot/src/sig/MyRobot.java index 974ac63..f4c1341 100644 --- a/DivaBot/src/sig/MyRobot.java +++ b/DivaBot/src/sig/MyRobot.java @@ -30,6 +30,8 @@ import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.text.AttributedString; import java.util.ArrayList; import java.util.Arrays; @@ -115,7 +117,7 @@ public class MyRobot{ boolean lastfail; long lastSongSelectTime = System.currentTimeMillis(); - static TypeFace typeface1,typeface2; + static TypeFace2 typeface1,typeface2; static Thread t = null; String prevSongTitle = ""; @@ -208,7 +210,7 @@ public class MyRobot{ //602,260 16,222,202 //901,460 220-255,220-255,160-220 - if (OnResultsScreen() && !recordedResults && !recordingResults && results.size()==0) { + if (OnResultsScreen() && !recordedResults && !recordingResults && results.size()==0 && false) { lastSongSelectTime=System.currentTimeMillis(); //gotoxy(800,64); //click(); @@ -226,25 +228,27 @@ public class MyRobot{ //System.out.println(typeface1.extractNumbersFromImage(MYROBOT.createScreenCapture(new Rectangle(1235,553,115,26)))); //System.out.println(typeface1.extractNumbersFromImage(MYROBOT.createScreenCapture(new Rectangle(1235,583,115,26)))); //System.out.println(typeface2.extractNumbersFromImage(MYROBOT.createScreenCapture(new Rectangle(1428,361,128,30)))); + ImageIO.write(MYROBOT.createNormalScreenCapture(new Rectangle(418,204,1227,690)),"png",new File("scoreimage.png")); File tmp = new File("tmp"); if (tmp.exists()) { FileUtils.deleteFile(tmp); } else { tmp.mkdir(); } - int cool = typeface1.extractNumbersFromImage(MYROBOT.createScreenCapture(new Rectangle(1235,451,115,26)),new File(tmp,"cool")); - int fine = typeface1.extractNumbersFromImage(MYROBOT.createScreenCapture(new Rectangle(1235,484,115,26)),new File(tmp,"fine")); - int safe = typeface1.extractNumbersFromImage(MYROBOT.createScreenCapture(new Rectangle(1235,518,115,26)),new File(tmp,"safe")); - int sad = typeface1.extractNumbersFromImage(MYROBOT.createScreenCapture(new Rectangle(1235,553,115,26)),new File(tmp,"sad")); - int worst = typeface1.extractNumbersFromImage(MYROBOT.createScreenCapture(new Rectangle(1235,583,115,26)),new File(tmp,"worst")); + int cool = typeface1.extractNumbersFromImage(MYROBOT.createNormalScreenCapture(new Rectangle(1235,451,115,26)),new File(tmp,"cool")); + int fine = typeface1.extractNumbersFromImage(MYROBOT.createNormalScreenCapture(new Rectangle(1235,484,115,26)),new File(tmp,"fine")); + int safe = typeface1.extractNumbersFromImage(MYROBOT.createNormalScreenCapture(new Rectangle(1235,518,115,26)),new File(tmp,"safe")); + int sad = typeface1.extractNumbersFromImage(MYROBOT.createNormalScreenCapture(new Rectangle(1235,553,115,26)),new File(tmp,"sad")); + int worst = typeface1.extractNumbersFromImage(MYROBOT.createNormalScreenCapture(new Rectangle(1235,583,115,26)),new File(tmp,"worst")); - boolean fail = textFailPixel(MYROBOT.createScreenCapture(new Rectangle(952,385,1,1))); + boolean fail = textFailPixel(MYROBOT.createNormalScreenCapture(new Rectangle(952,385,1,1))); /*try { ImageIO.write(MYROBOT.createScreenCapture(new Rectangle(1235,583,115,26)),"png",new File("worst.png")); } catch (IOException e) { e.printStackTrace(); }*/ - float percent = (float)typeface2.extractNumbersFromImage(MYROBOT.createScreenCapture(new Rectangle(1428,361,128,30)),new File(tmp,"percent"))/100f; + float percent = (float)typeface2.extractNumbersFromImage(MYROBOT.createNormalScreenCapture(new Rectangle(1428,361,128,30)),new File(tmp,"percent"))/100f; + ImageIO.write(MYROBOT.createNormalScreenCapture(new Rectangle(418,204,1227,690)),"png",new File("test.png")); if (cool==-1 || fine==-1 || safe==-1 || sad==-1 || worst==-1 || percent==-0.01f) { System.out.println("Waiting for results to populate..."); } else @@ -260,18 +264,6 @@ public class MyRobot{ int playId = songFolderFiles.length; File playFolder = new File(selectedSong.title+"/"+difficulty+"/"+playId); playFolder.mkdir(); - try { - /*FileUtils.copyFileDir(new File(tmp,"cool"), new File(playFolder,"cool")); - FileUtils.copyFileDir(new File(tmp,"fine"), new File(playFolder,"fine")); - FileUtils.copyFileDir(new File(tmp,"safe"), new File(playFolder,"safe")); - FileUtils.copyFileDir(new File(tmp,"sad"), new File(playFolder,"sad")); - FileUtils.copyFileDir(new File(tmp,"worst"), new File(playFolder,"worst")); - FileUtils.copyFileDir(new File(tmp,"percent"), new File(playFolder,"percent"));*/ - //FileUtils.deleteFile(tmp); - ImageIO.write(MYROBOT.createScreenCapture(new Rectangle(418,204,1227,690)),"png",new File(playFolder,selectedSong.title+"_"+difficulty+"play_"+cool+"_"+fine+"_"+safe+"_"+sad+"_"+worst+"_"+percent+".png")); - } catch (IOException e) { - e.printStackTrace(); - } recordedResults=true; lastcool=cool; lastfine=fine; @@ -280,7 +272,7 @@ public class MyRobot{ lastworst=worst; lastpercent=percent; lastfail=fail; - + new File("scoreimage.png").renameTo(new File(playFolder,selectedSong.title+"_"+difficulty+"play_"+cool+"_"+fine+"_"+safe+"_"+sad+"_"+worst+"_"+percent+".png")); results.add(new Result(selectedSong.title,difficulty,cool,fine,safe,sad,worst,percent,fail)); SoundUtils.playSound("collect_item.wav"); //gotoxy(800,64); @@ -345,7 +337,15 @@ public class MyRobot{ try { JSONObject obj = FileUtils.readJsonFromUrl("http://45.33.13.215:4501/rating/sigonasr2"); + JSONObject obj2 = FileUtils.readJsonFromUrl("http://45.33.13.215:4501/bestplay/sigonasr2/"+URLEncoder.encode(MyRobot.p.songname, StandardCharsets.UTF_8.toString()).replaceAll("\\+", "%20")+"/"+difficulty); p.lastRating = p.overallrating; + if (obj2.has("score")) { + double newScore = obj2.getDouble("score"); + if (newScore>p.lastScore) { + p.bestPlayTime=System.currentTimeMillis(); + } + p.lastScore = newScore; + } p.overallrating = (int)obj.getDouble("rating"); if (p.lastRating=0 && currentCol.getRed()<=70 + && currentCol.getGreen()>=0 && currentCol.getGreen()<=100 + && currentCol.getBlue()>=0 && currentCol.getBlue()<=120)) { + img.setRGB(x, y, new Color(8,114,140).getRGB()); + } else { + img.setRGB(x, y, Color.WHITE.getRGB()); + } + } else + if ((currentCol.getRed()>=0 && currentCol.getRed()<=105 + && currentCol.getGreen()>=0 && currentCol.getGreen()<=150 + && currentCol.getBlue()>=0 && currentCol.getBlue()<=150) || + (this.equals(DemoApplication.typeface2) && + currentCol.getRed()>=0 && currentCol.getRed()<=120 + && currentCol.getGreen()>=100 && currentCol.getGreen()<=210 + && currentCol.getBlue()>=120 && currentCol.getBlue()<=230 + && currentCol.getGreen()+5200 && p.getGreen()>200 && p.getRed()>200) { state=2; if (DEBUG_IMG) { @@ -107,6 +147,7 @@ public class TypeFace { e.printStackTrace(); } } + //System.out.println("Found light pixel"); } }break; case 2:{ @@ -118,8 +159,17 @@ public class TypeFace { X=img.getWidth(); break; } else { - X+=i.maxX-1; numberImg = i.createImage(); + if (numberImg.getWidth()>=32) { + state=0; + break; + } + X+=i.maxX; + if (numberImg.getHeight()<10 || numberImg.getWidth()<4) { + //A number should be at least 10 pixels high...This is not satisfactory. + state=4; + break; + } //X+=numberImg.getWidth(); state=3; if (DEBUG_IMG) { @@ -131,6 +181,7 @@ public class TypeFace { e.printStackTrace(); } } + //System.out.println("Moving to next step: "); } }break; case 3:{ @@ -140,6 +191,8 @@ public class TypeFace { int[] hits = new int[NUMBER_COUNT]; double highestRatio = 0; int highest = 0; + boolean goNext=false; + boolean foundOne=false; for (int k=0;khighestRatio) { highestRatio= (double)(hits[k])/(WIDTH*HEIGHT); @@ -164,6 +235,7 @@ public class TypeFace { } } } + if (goNext) {break;} } FileUtils.logToFile(((k+1)%NUMBER_COUNT)+":"+((double)(hits[k])/(WIDTH*HEIGHT)), new File(saveLoc,(iterations)+".txt").getPath()); try { @@ -171,6 +243,12 @@ public class TypeFace { } catch (IOException e) { e.printStackTrace(); } + goNext=false; + } + if (!foundOne) { + state=0; + iterations++; + break; } //System.out.println("Matches closest to "+((highest+1)%NUMBER_COUNT)+" with "+highestRatio); iterations++; @@ -178,11 +256,9 @@ public class TypeFace { state=4; }break; case 4:{ - //Find dark pixels again. - if (p.getBlue()>blue_minthreshold && p.getBlue()green_minthreshold && p.getGreen()red_minthreshold && p.getRed()200 && p.getGreen()>200 && p.getRed()>200) { + X-=2; state=0; if (DEBUG_IMG) { try { @@ -193,6 +269,7 @@ public class TypeFace { e.printStackTrace(); } } + //System.out.println("Found next dark pixel"); } }break; } @@ -206,8 +283,11 @@ public class TypeFace { } } - public boolean fillDark(BufferedImage img,int startX,int startY,int x,int y) { + public boolean fillDark(BufferedImage img,int startX,int startY,int x,int y, int iterations) { //rect.AddPixel(new Point(x,y), Color.BLACK); + if (iterations>Math.max(img.getWidth(),img.getHeight())) { + return false; + } img.setRGB(startX+x, startY+y, Color.BLACK.getRGB()); Color p = null; if (startX+x+1blue_fillminthreshold && p.getBlue()green_fillminthreshold && p.getGreen()red_fillminthreshold && p.getRed()blue_fillminthreshold && p.getBlue()green_fillminthreshold && p.getGreen()red_fillminthreshold && p.getRed()blue_fillminthreshold && p.getBlue()green_fillminthreshold && p.getGreen()red_fillminthreshold && p.getRed()blue_fillminthreshold && p.getBlue()green_fillminthreshold && p.getGreen()red_fillminthreshold && p.getRed()22) { + int distance = 0; + int foundIndex = -1; + //Compare the 22x21 range. + for (int i=0;i<10;i++) { + if (debug) { + test = new BufferedImage(22,20,BufferedImage.TYPE_INT_ARGB); + } + boolean ruleBreak=false; + + colorloop: + for (int x=0;x<22;x++) { + for (int y=0;y<20;y++) { + Color fontCol = new Color(font.getRGB(x+i*22,y)); + Color pixelCol = new Color(img.getRGB(xpointer-22+x+1, y)); + if (fontCol.equals(Color.RED) && pixelCol.getRed()<150 + && pixelCol.getGreen()<150 && pixelCol.getBlue()<150) { + //Breaks a rule. + ruleBreak=true; + if (!debug) { + break colorloop; + } else { + test.setRGB(x, y, Color.RED.getRGB()); + } + } else + if (fontCol.equals(Color.GREEN) && (pixelCol.getRed()>150 + || pixelCol.getGreen()>150 || pixelCol.getBlue()>150)) { + //Breaks a rule. + ruleBreak=true; + if (!debug) { + break colorloop; + } else { + test.setRGB(x, y, Color.GREEN.getRGB()); + } + } else + if (debug) { + test.setRGB(x, y, pixelCol.getRGB()); + } + } + } + if (!ruleBreak) { + foundIndex=i; + if (debug) { + System.out.println("Passes as "+((foundIndex+1)%10)); + } + } else + if (debug) { + ImageIO.write(test,"png",new File(System.nanoTime()+"_"+((i+1)%10)+".png")); + } + } + if (foundIndex!=-1) { + //System.out.println(" Closest Match: Index "+((shortestIndex+1)%10)+" ("+shortestDistance+")"); + if (total.equals("")) { + total = Integer.toString((foundIndex+1)%10); + } else { + total = Integer.toString((foundIndex+1)%10)+total; + } + if (debug) { + System.out.println("Input as "+((foundIndex+1)%10)); + System.out.println("-------------"); + } + xpointer-=22; + } else { + //Try shifting the xpointer slowly to the right and try again. + xpointer--; + } + } + + if (total.equals("")) { + return -1; + } else { + return Integer.parseInt(total); + } + } +} diff --git a/DivaBot/test.png b/DivaBot/test.png new file mode 100644 index 0000000..385dda7 Binary files /dev/null and b/DivaBot/test.png differ diff --git a/DivaBot/testimage.png b/DivaBot/testimage.png new file mode 100644 index 0000000..0df17fb Binary files /dev/null and b/DivaBot/testimage.png differ diff --git a/DivaBot/testimage2.png b/DivaBot/testimage2.png new file mode 100644 index 0000000..c1e3c10 Binary files /dev/null and b/DivaBot/testimage2.png differ diff --git a/DivaBot/testimage3.png b/DivaBot/testimage3.png new file mode 100644 index 0000000..da5cefa Binary files /dev/null and b/DivaBot/testimage3.png differ diff --git a/DivaBot/testimage4.png b/DivaBot/testimage4.png new file mode 100644 index 0000000..b05c7cc Binary files /dev/null and b/DivaBot/testimage4.png differ diff --git a/DivaBot/testing.png b/DivaBot/testing.png new file mode 100644 index 0000000..c1a85c0 Binary files /dev/null and b/DivaBot/testing.png differ diff --git a/DivaBot/testscreen.png b/DivaBot/testscreen.png new file mode 100644 index 0000000..ea6906e Binary files /dev/null and b/DivaBot/testscreen.png differ diff --git a/DivaBot/typeface.png b/DivaBot/typeface.png new file mode 100644 index 0000000..eaee81d Binary files /dev/null and b/DivaBot/typeface.png differ