public static List <KeyColor> getKeyColor(Utils.MyRawBitmapData bitmapData, int num) { List <KeyColor> ret = new List <KeyColor>(); Dictionary <Utils.MyColor, PostValue> htColor = new Dictionary <Utils.MyColor, PostValue>(new Utils.MyColor.EqualityComparer()); for (int y = 0; y < bitmapData.height; y++) { for (int x = 0; x < bitmapData.width; x++) { Utils.MyColor m = bitmapData.GetColor(x, y); if (htColor.ContainsKey(m)) { htColor[m].inc(); } else { htColor[m] = new PostValue(1, new Point(x, y)); } } } foreach (Utils.MyColor m in htColor.Keys) { if (htColor[m].value == 1) { ret.Add(new KeyColor(m, htColor[m].pos)); //Console.WriteLine("getKeyColor " + m + " " + htColor[m].pos); if (ret.Count >= num) { break; } } } return(ret); }
public CellType getCellTYpe(Utils.MyRawBitmapData src, Utils.RECT rect) { BitmapSearch.BitmapSearchResult ret = cellTypeSearch.Search(src, rect, 2, 100); if (ret != null) { return(htJelly[ret.bitmap]); } return(CellType.Normal); }
public BitmapSearchResult Search(Utils.MyRawBitmapData data, Utils.RECT rect, int step = 1, double colorError = 1, bool flgSingle = true) { foreach (KeyColorMyRawBitmapData b in lstBitmap.Keys) { List <Point> listP = GetSubPositions(data, b, rect, step, colorError, flgSingle); if (listP.Count > 0) { return(new BitmapSearchResult(lstBitmap[b], listP[0])); } } return(null); }
private void button4_Click(object sender, EventArgs e) { screen = new Bitmap("candycrush.png"); Bitmap pngImage = new Bitmap("candycrush/ref.png"); Utils.RECT rect = Utils.RECT.fromInt(0, 0, screen.Width, screen.Height); long start = System.Environment.TickCount; Utils.RECT scanRect = new Utils.RECT(); scanRect.left = rect.left; scanRect.top = rect.top; scanRect.right = (rect.left + rect.right) / 4; scanRect.bottom = (rect.top + rect.bottom) / 3; refPoint = Utils.SmartFindBitmap(screen, scanRect, pngImage); if (refPoint.X == 0) { txtDebug.AppendText("Not found"); return; } refPoint.X += 106; refPoint.Y += 69; Bitmap crop = Utils.cropImage(screen, new Rectangle(refPoint.X, refPoint.Y, cellW * NUMCELL, cellH * NUMCELL)); Utils.RECT CELLRECT = Utils.RECT.fromInt(0, 0, cellW, cellH); for (int i = 0; i < NUMCELL; i++) { for (int j = 0; j < NUMCELL; j++) { board.table[i, j] = null; Rectangle cellRect = new Rectangle((i * cellW), (j * cellH), cellW, cellH); Bitmap cropCell = Utils.cropImage(crop, cellRect); Utils.MyRawBitmapData cropCellBitmapData = new Utils.MyRawBitmapData(cropCell); BitmapSearch.BitmapSearchResult searchRet = candySearch.Search(cropCellBitmapData, CELLRECT, 2, 1000); if (searchRet != null) { Point pRet = searchRet.point; board.table[i, j] = htBmp[searchRet.bitmap].clone(); CellType ct = getCellTYpe(cropCellBitmapData, CELLRECT); board.cell[i, j] = ct; } } } long end = System.Environment.TickCount; dbgLine(refPoint + " " + (end - start)); mainRect = new Rectangle(refPoint.X, refPoint.Y, NUMCELL * cellW, NUMCELL * cellH); currentBestMove = board.getBestMove(); lstResult = board.getPossibleMove(); imgMain.Refresh(); }
public static List <Point> GetSubPositions(Utils.MyRawBitmapData main, KeyColorMyRawBitmapData sub, Utils.RECT rect, int step = 1, double colorError = 1, bool flgSingle = true) { List <Point> possiblepos = new List <Point>(); int subwidth = sub.width; int subheight = sub.height; int movewidth = rect.right - subwidth; int moveheight = rect.bottom - subheight; int maxX = rect.left + movewidth - subwidth; int maxY = rect.top + moveheight - subheight; int cX = subwidth / 2; int cY = subheight / 2; if (sub.keyColor != null) { cX = sub.keyColor.pos.X; cY = sub.keyColor.pos.Y; } Utils.MyColor firstColor = sub.GetColor(0, 0); Utils.MyColor CenterColor = sub.GetColor(cX, cY); for (int y = rect.top; y < moveheight; ++y) { for (int x = rect.left; x < movewidth; ++x) { Utils.MyColor curcolor = main.GetColor(x, y); if (possiblepos.Count > 0) { foreach (var item in possiblepos.ToArray()) { int xsub = x - item.X; int ysub = y - item.Y; if (xsub >= subwidth || ysub >= subheight || xsub < 0) { continue; } Utils.MyColor subcolor = sub.GetColor(xsub, ysub); if (!curcolor.nearColor(subcolor, colorError)) { possiblepos.Remove(item); } } } // add part // we should not add pixel that will out of bound if (x > maxX) { continue; } if (y > maxY) { continue; } if (curcolor.nearColor(firstColor, colorError)) { if (CenterColor.nearColor(main.GetColor(x + cX, y + cY), colorError)) { possiblepos.Add(new Point(x, y)); } } } if (flgSingle && (possiblepos.Count > 0)) { if (y - subheight > possiblepos[0].Y) { //Console.WriteLine("found break"); break; } } //Console.WriteLine("Y=" + y + " Count=" + possiblepos.Count); } return(possiblepos); }
private void button1_Click(object sender, EventArgs e) { String title = "Candy Crush"; Candy[,] table = board.table; Color[] refColors = new Color[] { Color.FromArgb(0xEBDEE4), // Color.FromArgb(235,223,228), }; if (Utils.activateWindow(title)) { screen = Utils.GetScreenShot(); Bitmap pngImage = new Bitmap("candycrush/ref.png"); Utils.RECT rect = new Utils.RECT(); IntPtr hwnd = Utils.GetForegroundWindow(); Utils.GetWindowRect(hwnd, out rect); long start = System.Environment.TickCount; txtDebug.AppendText("Active " + rect + " " + hwnd + "\n"); Utils.RECT scanRect = new Utils.RECT(); scanRect.left = rect.left; scanRect.top = rect.top; scanRect.right = (rect.left + rect.right) / 4; scanRect.bottom = (rect.top + rect.bottom) / 3; refPoint = Utils.SmartFindBitmap(screen, scanRect, pngImage); if (refPoint.X <= 0) { txtDebug.AppendText("Not found"); return; } refPoint.X += 106; refPoint.Y += 69; mainRect = new Rectangle(refPoint.X, refPoint.Y, NUMCELL * cellW, NUMCELL * cellH); screen = saveGetScreenShot(mainRect); screen.Save("candycrush.png"); //Utils.MouseMove(refPoint.X, refPoint.Y); Bitmap crop = Utils.cropImage(screen, new Rectangle(refPoint.X, refPoint.Y, cellW * NUMCELL, cellH * NUMCELL)); Utils.RECT CELLRECT = Utils.RECT.fromInt(0, 0, cellW, cellH); for (int i = 0; i < NUMCELL; i++) { for (int j = 0; j < NUMCELL; j++) { board.table[i, j] = null; Rectangle cellRect = new Rectangle((i * cellW), (j * cellH), cellW, cellH); Bitmap cropCell = Utils.cropImage(crop, cellRect); Utils.MyRawBitmapData cropCellBitmapData = new Utils.MyRawBitmapData(cropCell); BitmapSearch.BitmapSearchResult searchRet = candySearch.Search(cropCellBitmapData, CELLRECT, 2, 1000); if (searchRet != null) { Point pRet = searchRet.point; board.table[i, j] = htBmp[searchRet.bitmap].clone(); CellType ct = getCellTYpe(cropCellBitmapData, CELLRECT); board.cell[i, j] = ct; } } } } board.save("table.txt"); board.load("table.txt"); currentBestMove = board.getBestMove(); lstResult = board.getPossibleMove(); imgMain.Refresh(); //txtDebug.AppendText(table.ToString()); }