public void TM()
 {
     var config = new TestConfig();
     Commands commands = new Commands(config);
     config.ReadConfig(new string[] {
         "x-offset=10",
         "y-offset=50",
     });
     Script.InitScript(commands);
     var img = new GImage(@"data\leofield.jpg");
     Script s = new Script("test");
     // Грузим исходную картинку, на которой осуществляется поиск
     s.Parse("detectfield");
     s.Parse("fielddetected");
     s.Parse("fieldcells");
     {
         ScriptState st = new ScriptState(s, img);
         Run(s, st);
         StringBuilder sb = new StringBuilder(1024);
         for (var y = 0; y < ScriptState.FieldSize.Height; ++y)
         {
             for (var x = 0; x < ScriptState.FieldSize.Width; ++x)
             {
                 var c = st[x, y];
                 var t = c.NotOpened ? '_' : c.Flag ? 'P' : c.Empty ? ' ' : (char)((byte)'0' + c.Number);
                 sb.Append(t);
                 sb.Append(' ');
             }
             sb.Append("\n");
         }
         File.WriteAllText("out.txt", sb.ToString());
     }
 }
Exemple #2
0
 public void ScriptTestMethod()
 {
     var config = new TestConfig();
     Commands commands = new Commands(config);
     config.ReadConfig(new string[] {
         "x-offset=10",
         "y-offset=50",
         "image=firstpic:data\\hint.png"
     });
     Script.InitScript(commands);
     GImage img = new GImage("data\\testsource.png");
     Script s = new Script("test");
     // Грузим исходную картинку, на которой осуществляется поиск
     s.Parse("match firstpic");
     {
         ScriptState st = new ScriptState(s, img);
         Run(s, st);
         Assert.IsTrue(st.Rectangle.Equals(new Rectangle(1238, 853, 196, 37)), "Не найден прямоугольник");
     }
     s = new Script("test");
     s.Parse("setmode first");
     s.Parse("mode first");
     {
         ScriptState st = new ScriptState(s, img);
         Run(s, st);
         Assert.IsTrue(st.Running, "Тест не продолжился");
     }
     s.Parse("mode nfirst");
     {
         ScriptState st = new ScriptState(s, img);
         Run(s, st);
         Assert.IsFalse(st.Running, "Тест продолжился ошибочно");
     }
 }
 public void DetectField()
 {
     var config = new TestConfig();
     Commands commands = new Commands(config);
     config.ReadConfig(new string[] {
         "x-offset=10",
         "y-offset=50",
     });
     Script.InitScript(commands);
     GImage img = new GImage("data\\fullfield.png");
     Script s = new Script("test");
     // Грузим исходную картинку, на которой осуществляется поиск
     s.Parse("detectfield");
     s.Parse("fielddetected");
     var nimg = new GImage("data\\nofield.png");
     {
         ScriptState st = new ScriptState(s, nimg);
         Run(s, st);
         Assert.IsFalse(st.Running, "Лишнее поле нашлось");
     }
     {
         ScriptState st = new ScriptState(s, img);
         Run(s, st);
         Assert.IsTrue(st.Running, "Поле не нашлось");
     }
 }
Exemple #4
0
 private static void Run(Script s, ScriptState st)
 {
     foreach (var c in s.commands)
     {
         //Program.Info(" {0}", ConsoleColor.DarkGray, c.Name);
         c.Action(st);
         if (!st.Running)
             break;
     }
 }
        public void Action(ScriptState state)
        {
            Shaft[] shafts = new[] {
                                 new Shaft("Леонсио", "leonsio", 13, new Point(-96,169), new Size(450,350)),
                                 new Shaft("Ухта", "uhta", 17, new Point(-88,48), new Size(450,350))
                             };
            foreach (var s in shafts)
            {
                var b = global::kmine.Properties.Resources.ResourceManager.GetObject(s.image) as Bitmap;
                if (b != null)
                {
                    var x = new GImage(b);
                    var u = state.Image.Find(x, new Point());
                    if (!u.IsEmpty)
                    {
                        ScriptState.TotalMines = s.mines;
                        ScriptState.Shaft = s.name;
                        ScriptState.FieldSize = new Size(s.size.Width / 50, s.size.Height / 50);
                        state.ActionDone = true;
                        ///!!! Debug
                        //                        Gray c = new Gray(255);
                        //                        state.Image.Draw(u, c, 1);
                        u = new Rectangle(new Point(u.Left + s.delta.X, u.Top + s.delta.Y), s.size);
                        ScriptState.FieldRectangle = u;
                        //                        state.Image.Draw(u, c, 1);
                        //                        state.Image.Save("2.bmp");
                        break;
                    }
                }
                else Program.log.Error("EdgeDetect: No resource found {0}", s.image);
            }

            // !!! Задать ссылку на угол
            /*var img = new kmine.GImage(global::kmine.Properties.Resources.leonsio);
            if (Check(state, img)) { }
            var tl = state.Image.Find(Top, new Point());
            if (!tl.IsEmpty)
            {

                var TopLeft = new kmine.GImage(global::kmine.Properties.Resources.leonsio);
                tl = state.Image.Find(TopLeft, new Point());
                if (!tl.IsEmpty)
                {
                    var BottomRight = new GImage(global::kmine.Properties.Resources.greenlight);
                    var br = state.Image.Find(BottomRight, new Point());
                    if (!br.IsEmpty)
                    {

                    }
                }
            }*/
        }
 public void Action(ScriptState state)
 {
     if (Img != null)
     {
         // Ищем совпадения
         var p = state.Image.Find(Img, new Point());
         if (p.IsEmpty)
             state.Running = false;
         else
             state.Rectangle = new Rectangle(p.X, p.Y, Img.Width, Img.Height);
     }
     else state.Running = false;
 }
Exemple #7
0
 /// <summary>
 /// Запуск скрипта
 /// </summary>
 /// <param name="img">снятое изображение экрана</param>
 /// <returns>Скрипт выполнил задачу (false - отработал вхолостую)</returns>
 internal bool Run(GImage img)
 {
     Program.log.Info(Name);
     var state = new ScriptState(this, img);
     foreach (var c in commands)
     {
         Program.log.Info(" {0}", c.Name);
         c.Action(state);
         if (!state.Running)
             break;
     }
     return state.ActionDone;
 }
 public void Action(ScriptState state)
 {
     ScriptState.Mode = Mode;
     Program.log.Trace("Set mode {0}",Mode);
 }
 private static void Click(ScriptState state)
 {
     mouse_event(MOUSEEVENTF_LEFTDOWN, state.Mouse.X, state.Mouse.Y, 0, 0);
     mouse_event(MOUSEEVENTF_LEFTUP, state.Mouse.X, state.Mouse.Y, 0, 0);
 }
 public void Action(ScriptState state)
 {
     Click(state);
     if (doubleClick)
         Click(state);
     state.ActionDone = true;
 }
 public void Action(ScriptState state)
 {
     if (state.Rectangle.IsEmpty) return;
     int X = state.Rectangle.X + Cfg.xOffset, Y = state.Rectangle.Y + Cfg.yOffset;
     int W = state.Rectangle.Width / 2;
     int H = state.Rectangle.Height / 2;
     switch (Pos)
     {
         case Position.Center:
             X += W;
             Y += H;
             break;
         case Position.Left:
             Y += H;
             break;
         case Position.Right:
             Y += H;
             X += state.Rectangle.Width;
             break;
         case Position.Top:
             X += W;
             break;
         case Position.Bottom:
             X += W;
             Y += state.Rectangle.Height;
             break;
     }
     state.Mouse = new Point(X, Y);
     SetCursorPos(X, Y);
 }
 public void Action(ScriptState state)
 {
     state.Running = ScriptState.Mode.Equals(Mode);
 }
 public void Action(ScriptState state)
 {
     state.Running = !ScriptState.FieldRectangle.IsEmpty;
 }
        public void Action(ScriptState state)
        {
            state.Image.ROI = ScriptState.FieldRectangle;
            var i = ToGray(state.Image);
            var ok = false;

            for (int y = 0; y < ScriptState.FieldSize.Height; ++y)
                for (int x = 0; x < ScriptState.FieldSize.Width; ++x)
                {
                    i.ROI = new Rectangle(new Point(x * 50, y * 50), new Size(50, 50));
                    var h = new Emgu.CV.DenseHistogram(256, new RangeF(0f, 255f));
                    h.Calculate(new[] { i }, false, null);
                    var valHist = new float[256];
                    h.MatND.ManagedArray.CopyTo(valHist, 0);
                    if (valHist[0] < 1000)
                    {
                        for (int n = 0; n < nums.Length; ++n)
                        {
                            if (nums[n] != null)
                            {
                                var r = i.Find(nums[n], Point.Empty);
                                if (!r.IsEmpty)
                                {
                                    state[x, y].Number = n >= 0 && n <= 9 ? n : n; // !!!! случай флага и незанятой клетки
                                    ok = true;
                                }
                            }
                        }
                    }
                    else state[x, y].Number = -1;
                }
            if (ok) state.ActionDone = true;
        }
 /// <summary>
 /// Проверить шахту на картинке
 /// </summary>
 /// <param name="img"></param>
 /// <returns></returns>
 private bool Check(ScriptState state, GImage img)
 {
     var tl = state.Image.Find(img, new Point());
     return false;
 }