asmrobot
2019-10-14 730fe7ea65bcadbe235e40bb54b2410d14495267
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using RichCreator.Utility;
using RichCreator.Utility.Captures;
using RichCreator.Utility.CV;
using RichCreator.Utility.InputControl;
using RichCreator.Utility.Structs;
using Emgu.CV;
using Emgu.CV.Structure;
using RichCreator.Maps;
using RichCreator.Maps.Lindong;
using RichCreator.StateMachines;
using RichCreator.Utilitys;
using static RichCreator.Utilitys.AttackRectangle;
using Utils= RichCreator.Utilitys.Utils;
using ZTImage.Collections;
 
namespace RichCreator.Jobs.StateMachines
{
    /// <summary>
    /// 状态动作 
    /// </summary>
    /// <param name=""></param>
    /// <param name=""></param>
    /// <param name=""></param>
    /// <returns></returns>
    public delegate KillMonsterStateResult StateAction(Image<Rgb, byte> image, Image<Hsv, byte> hsvImage);
 
    /// <summary>
    /// 杀怪状态机
    /// </summary>
    public class KillMonsterStateMachine
    {
        private const Int32 NoMoveMaxMillSecond = 2000;//最大未移动容忍毫秒数
        private StateAction[] states;//状态动作列表
        private MapHouse house;//当前房间
        private ZTPoint miniMapStart;//小地图区域
        private ZTRectangle gameRect;//游戏区域
        private Int32 preHouseIndex = 0;//上一房间编号
        private MoveState moveState;//移动状态
        private OutOfBounds outOfBounds;//是否在禁区
        private bool exitResult;//退出结果
 
        
        private ZTPoint roleLastPosition=ZTPoint.Empty;//角色最后位置
        private Int32 runningStep = RunningStep.None;//是否恢复的状态
 
 
        #region Find Door Info
        //门坐标
        private ZTPoint stateDoorPosition;
        //角色位置
        private ZTPoint stateRolePosition;
        //离开的门朝向
        private Direction stateDoorLevelDirect = Direction.None;
        //定位点方框
        private MultiList<ZTRectangle, Int32> stateLocationRectangle = new MultiList<ZTRectangle, int>();
        //怪
        private ZTPoint[] stateMonsters;
 
 
        #endregion
 
 
        public KillMonsterStateMachine(MapHouse house, ZTPoint miniMapStart, ZTRectangle gameRect,Int32 preHouseIndex,Int32 runningStep)
        {
            this.runningStep = runningStep;
            this.house = house;
            this.miniMapStart = miniMapStart;
            this.gameRect = gameRect;
            this.preHouseIndex = preHouseIndex;
            moveState = new MoveState(this.gameRect, this.house);
            outOfBounds = new OutOfBounds(this.gameRect,moveState);
 
            InitStates();
        }
        
        /// <summary>
        /// 状态机开始
        /// </summary>
        /// <param name="gameRect"></param>
        /// <param name="cancellationToken"></param>
        /// <param name="timeoutMillSecond"></param>
        /// <returns></returns>
        public Int32 Start(CancellationToken cancellationToken, Int32 timeoutMillSecond)
        {
            DateTime expireTime = DateTime.Now.AddMilliseconds(timeoutMillSecond);
            KillMonsterStateResult result = new KillMonsterStateResult(STATE_Start, false);
            if (this.runningStep > RunningStep.None)
            {
                result = new KillMonsterStateResult(STATE_FindMonster, true);
            }
            System.Drawing.Bitmap bitmap;
            //原图
            Image<Rgb, byte> image = null;
            //色彩hsv
            Image<Hsv, byte> hsvImage = new Image<Hsv, byte>(gameRect.End.X - gameRect.Start.X + 1, gameRect.End.Y - gameRect.Start.Y + 1);
 
            while (true)
            {
                byte currentState = result.NextState;
                if (cancellationToken.IsCancellationRequested)
                {
                    G.Instance.DebugWriter("取消刷图");
                    this.moveState.StopMove();
                    return JobResult.Cancel;
                }
 
                if (DateTime.Now > expireTime)
                {
                    G.Instance.DebugWriter("刷图超时");
                    this.moveState.StopMove();
                    return JobResult.Timeout;
                }
                if (result.NextState == STATE_Exit)
                {
                    G.Instance.DebugWriter("退出状态");
                    this.moveState.StopMove();
                    if (this.exitResult)
                    {
                        return JobResult.Success;
                    }
                    return JobResult.Failed;
                    
                }
 
                bool refreshScreen = result.RefreshScreen;
                if (refreshScreen)
                {
                    using (bitmap = ScreenCapture.Instance.CaptureScreen())
                    {
                        image = new Image<Rgb, byte>(bitmap);
                        image = image.GetSubRect(new System.Drawing.Rectangle(gameRect.Start.X, gameRect.Start.Y, gameRect.End.X - gameRect.Start.X + 1, gameRect.End.Y - gameRect.Start.Y + 1));
                        CvInvoke.CvtColor(image, hsvImage, Emgu.CV.CvEnum.ColorConversion.Rgb2Hsv);
                    }
 
                    if (this.house.Index != 15)
                    {
                        //计算是否进入其它房间
                        Int32 houseIndex;
                        if (!LindongCVHelper.GetCurrentHouseIndex(out houseIndex, image, this.miniMapStart))
                        {
                            //得不到房间
                            if (!FuncUtils.TimeoutCancelableWrap(30 * 1000, cancellationToken, () => {
                                using (bitmap = ScreenCapture.Instance.CaptureScreen())
                                {
                                    image = new Image<Rgb, byte>(bitmap);
                                    image = image.GetSubRect(new System.Drawing.Rectangle(gameRect.Start.X, gameRect.Start.Y, gameRect.End.X - gameRect.Start.X + 1, gameRect.End.Y - gameRect.Start.Y + 1));
                                    return LindongCVHelper.GetCurrentHouseIndex(out houseIndex, image, this.miniMapStart);
                                }
                            }, 50))
                            {
                                return JobResult.Failed;
                            }
                        }
                        if (houseIndex != this.house.Index)
                        {
                            this.moveState.StopMove();
                            return JobResult.Success;
                        }
                    }                    
                }
 
                StateAction action = states[currentState];
                result =action(image, hsvImage);
                if (refreshScreen)
                {
                    image.Dispose();
                }
                G.Instance.DebugWriter("next state:" + result.NextState.ToString());
            }
        }
        
        #region States
 
        private const byte STATE_FindMonster = 0;//找怪
        private const byte STATE_PrepareReleaseSkill = 1;//释放技能预处理,计算位置
        private const byte STATE_ReleaseSkill = 2;//释放技能
        private const byte STATE_DetectDoorIsOpen = 3;//查找进入下一房间的门是否开了
        private const byte STATE_MoveToDoor = 4;//移动进门
 
        private const byte STATE_Start = 8;//开始状态 
        private const byte STATE_Exit = 9;//完成状态
        
 
        /// <summary>
        /// 初始化状态
        /// </summary>
        private void InitStates()
        {
            //状态列表
            states = new StateAction[STATE_Exit+1];
            states[STATE_FindMonster] = FindMonster;
            states[STATE_PrepareReleaseSkill] = PrepareReleaseSkill;
            states[STATE_ReleaseSkill] = ReleaseSkill;
            states[STATE_DetectDoorIsOpen] = DetectDoorIsOpen;
            states[STATE_MoveToDoor] = MoveToDoor;
            states[STATE_Start] = StartState;
        }
 
 
        private KillMonsterStateResult StartState(Image<Rgb, byte> image, Image<Hsv, byte> hsvImage)
        {
            SingleEntryHouseSkill.ReleaseSkill(this.house.Index, this.preHouseIndex,this.moveState);
            return new KillMonsterStateResult(STATE_FindMonster, true);
        }
 
        /// <summary>
        /// 0.找怪
        /// </summary>
        /// <param name="image"></param>
        /// <param name="hsvImage"></param>
        /// <returns></returns>
        private KillMonsterStateResult FindMonster(Image<Rgb, byte> image, Image<Hsv, byte> hsvImage)
        {
            #region coll
            //while (true)
            //{
            //    image = ScreenCapture.Instance.CaptureScreenReturnImage();
            //    image = image.GetSubRect(new System.Drawing.Rectangle(gameRect.Start.X, gameRect.Start.Y, gameRect.End.X - gameRect.Start.X + 1, gameRect.End.Y - gameRect.Start.Y + 1));
            //    CvInvoke.CvtColor(image, hsvImage, Emgu.CV.CvEnum.ColorConversion.Rgb2Hsv);
            //    ZTPoint start=DnfCVHelper.FindRole(hsvImage, this.gameRect);
 
            //    Int32 dur = RandomUtils.KeyPressDuration;
            //    G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false,HIDCode.LeftArrow);
            //    Thread.Sleep(dur);
            //    G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false);
            //    Thread.Sleep(dur);
            //    G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false, HIDCode.LeftArrow);
            //    Thread.Sleep(dur);
            //    G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false);
 
            //    image = ScreenCapture.Instance.CaptureScreenReturnImage();
            //    image = image.GetSubRect(new System.Drawing.Rectangle(gameRect.Start.X, gameRect.Start.Y, gameRect.End.X - gameRect.Start.X + 1, gameRect.End.Y - gameRect.Start.Y + 1));
            //    CvInvoke.CvtColor(image, hsvImage, Emgu.CV.CvEnum.ColorConversion.Rgb2Hsv);
            //    ZTPoint end = DnfCVHelper.FindRole(hsvImage, this.gameRect);
            //    G.Instance.InfoWriter($"dur:{dur},start:{start},end{end},distance:{end.X - start.X}");
            //    Thread.Sleep(2000);
 
            //}
            #endregion
            //怪
            this.stateMonsters = LindongCVHelper.FindMonster(hsvImage,this.gameRect);
            //主角
            this.stateRolePosition = DnfCVHelper.FindRole(hsvImage, this.gameRect);
            //定位点
            DnfCVHelper.GetLocationPoint(out stateLocationRectangle, hsvImage, this.gameRect);
 
            if (this.stateMonsters.Length > 0)
            {
                G.Instance.DebugWriter(string.Format("已找到{0}个怪", this.stateMonsters.Length));
                //未找到主角
                if (stateRolePosition == ZTPoint.Empty)
                {
                    this.moveState.StopMove();
                    G.Instance.DebugWriter("找不到角色,Send X");
                    G.Instance.InputControl.PressKey(1000, HIDCode.X);
                    return new KillMonsterStateResult(STATE_FindMonster, true);
                }
 
                return new KillMonsterStateResult(STATE_PrepareReleaseSkill, false);
            }
            else
            {
                //未查找到主角
                
 
                if (stateRolePosition == ZTPoint.Empty)
                {
                    if (this.moveState.IsMoving && !this.moveState.IsFindRoleMoving)
                    {
                        this.moveState.StopMove();
                    }
 
 
                    //是否刷完
                    if (IsComplete(image))
                    {
                        return new KillMonsterStateResult(STATE_Exit, false);
                    }
 
                    this.moveState.FindRoleMove();
                    return new KillMonsterStateResult(STATE_FindMonster, true);
                }
                return new KillMonsterStateResult(STATE_DetectDoorIsOpen, true);
            }
 
            
        }
 
        private DateTime FindRoleLastNoMoveTime = DateTime.MaxValue;//最后没移动时间
        private bool FindRoleLastNoMoveStart = false;//没移动是否开始计时
        /// <summary>
        /// 1.释放技能预处理,计算位置
        /// </summary>
        /// <param name="image"></param>
        /// <param name="hsvImage"></param>
        /// <returns></returns>
        private KillMonsterStateResult PrepareReleaseSkill(Image<Rgb, byte> image, Image<Hsv, byte> hsvImage)
        {
            if (this.moveState.IsMoving&&!this.moveState.IsAttackMoving)
            {
                this.moveState.StopMove();
            }
            SkillInfo skill = this.house.Skills.SyncPeek();
            
 
            //计算攻击移动距离
            HIDCode dir = HIDCode.RightArrow;
            bool needMove = false;
            ZTSize moveDistance = AttackRectangle.GetMoveDistance(this.gameRect, stateRolePosition, stateMonsters, skill, out dir, out needMove);
            if (!needMove)
            {
                this.roleLastPosition = ZTPoint.Empty;
                //不需要移动
                if (dir != HIDCode.NoEvent)
                {
                    G.Instance.InputControl.PressKey(100, dir);
                }
                this.moveState.StopMove();
                G.Instance.DebugWriter(string.Format("不需移动直接发技能,距离:{0}", moveDistance.ToString()));
                return new KillMonsterStateResult(STATE_ReleaseSkill, false);
            }
            
            //没有移动,可能有障碍物
            if (stateRolePosition == this.roleLastPosition)
            {
                if (FindRoleLastNoMoveStart && (DateTime.Now - FindRoleLastNoMoveTime).TotalMilliseconds > NoMoveMaxMillSecond)
                {
                    //开始计时 并且 超过最大未移动容忍时间
                    G.Instance.DebugWriter("find role no mvoe:" + stateRolePosition.ToString());
                    this.moveState.StopMove();
                    G.Instance.InputControl.PressKey(RandomUtils.KeyPressDuration, HIDCode.X);
                    FindRoleLastNoMoveTime = DateTime.Now;
                }
                else if (!FindRoleLastNoMoveStart)
                {
                    //没开始计时 则 开始计时
                    FindRoleLastNoMoveStart = true;
                    FindRoleLastNoMoveTime = DateTime.Now;
                }
            }
            else
            {
                //移动了 取消计时
                this.roleLastPosition = stateRolePosition;
                FindRoleLastNoMoveStart = false;
            }
            
            G.Instance.DebugWriter("attack move :"+moveDistance.ToString()+",role:"+stateRolePosition.ToString()+",monster1:"+stateMonsters[0].ToString()+",skill:"+skill.Key.ToString());
            this.moveState.AttackMove(moveDistance);
            return new KillMonsterStateResult(STATE_FindMonster, true);
        }
 
        /// <summary>
        /// 2.释放技能
        /// </summary>
        /// <param name="image"></param>
        /// <param name="hsvImage"></param>
        /// <returns></returns>
        private KillMonsterStateResult ReleaseSkill(Image<Rgb, byte> image, Image<Hsv, byte> hsvImage)
        {
            SkillInfo skill = this.house.Skills.SyncDeQueue();
            if (skill.Key == HIDCode.X)
            {
                for (int i = 0; i < 5; i++)
                {
                    G.Instance.InputControl.PressKey(RandomUtils.KeyPressDuration, skill.Key);
                    Thread.Sleep(200);
                }
            }
            else
            {
                G.Instance.InputControl.PressKey(RandomUtils.KeyPressDuration, skill.Key);
                Thread.Sleep((Int32)skill.ReleaseTime);
            }
            
            G.Instance.DebugWriter(string.Format("发完技能,技能按键:{0},技能名称:{1}", skill.Key, skill.SkillName));            
            return new KillMonsterStateResult(STATE_FindMonster, true);
        }
 
        
        /// <summary>
        /// 3.检测进入下一房间的门是否开了
        /// </summary>
        /// <returns></returns>
        private KillMonsterStateResult DetectDoorIsOpen(Image<Rgb, byte> image, Image<Hsv, byte> hsvImage)
        {
            if (this.moveState.IsAttackMoving|| this.moveState.IsFindRoleMoving)
            {
                this.moveState.StopMove();
            }
 
            ////查找当前房间编号,看是否进入其它房间
            //Int32 houseIndex;
            //if (!LindongCVHelper.GetCurrentHouseIndex(out houseIndex, image, this.miniMapStart))
            //{
            if (IsComplete(image))
            {
                return new KillMonsterStateResult(STATE_Exit, false);
            }
 
            //    G.Instance.DebugWriter("检测是否进入下一房间时,未发现小地图中有人物标记,id"+this.house.Index+",isend:"+this.house.IsEnd.ToString());
            //    return new KillMonsterStateResult(STATE_FindMonster, true);
            //}
 
            //if (houseIndex != this.house.Index)
            //{
            //    //已进入其它房间
            //    this.moveState.StopMove();
            //    this.exitResult = true;
            //    return new KillMonsterStateResult(STATE_Exit, false);
            //}
 
            ////拾取物品,获取最近一个物品位置并步行过去
            //ZTPoint thingItemPosition = GetNearlyThingItem(image, rolePosition);
            //if (thingItemPosition != ZTPoint.Empty)
            //{
            //    if (this.moveState.IsMoving && !this.moveState.IsPickupMoving)
            //    {
            //        this.moveState.StopMove();
            //    }
 
            //    this.moveState.PickupMove(rolePosition, thingItemPosition);
            //    return new KillMonsterStateResult(STATE_FindMonster, true);
            //}
 
            //if (this.moveState.IsPickupMoving)
            //{
            //    this.moveState.StopMove();
            //}
 
            //查找真实的门
            ZTPoint doorPosition = LindongCVHelper.FindDoor(out stateDoorLevelDirect,hsvImage, this.house.DoorDirection,this.gameRect);
            if (doorPosition != ZTPoint.Empty)
            {
                G.Instance.InfoWriter("door find:"+doorPosition.ToString());
                this.moveState.StopMove();
                this.stateDoorPosition = doorPosition;
                
                //找到门,向门移动          
                return new KillMonsterStateResult(STATE_MoveToDoor, false);
            }
            else
            {
                //未找到门,循环移动
                Int32 areaID = 0;
                if (this.outOfBounds.InOutOfBound(this.house.Index, stateRolePosition, out areaID))
                {
                    this.outOfBounds.MoveToCommonBound(areaID);
                }
                else
                {
                    this.moveState.FindDoorMove(stateRolePosition);
                }
            }
            
            return new KillMonsterStateResult(STATE_FindMonster, true);
        }
        
        private const Int32 XDoorLevelOffset = 200;
        private const Int32 YDoorLevelOffset = 100;
        /// <summary>
        /// 4.找门
        /// </summary>
        /// <param name="image"></param>
        /// <param name="hsvImage"></param>
        /// <returns></returns>
        private KillMonsterStateResult MoveToDoor(Image<Rgb, byte> image, Image<Hsv, byte> hsvImage)
        {
            Int32 limitLine = 0;
            Int32 diff = 0;
            //传过来人物坐标和门的坐标,根据门的朝向计算人物走向
            switch (stateDoorLevelDirect)
            {
                case Direction.Up:
                    //门在上方
                    limitLine = stateDoorPosition.Y + YDoorLevelOffset;                    
                    if (stateRolePosition.Y < limitLine)
                    {
                        //下移
                        this.moveState.SyncMove(new ZTPoint(0, limitLine - stateRolePosition.Y));
                    }
 
                    //垂直对齐
                    diff = stateDoorPosition.X - stateRolePosition.X;
                    this.moveState.SyncMove(new ZTPoint(diff, 0));
 
                    //移动y,进入
                    this.moveState.SyncMove(new ZTPoint(0, this.gameRect.Start.Y - stateRolePosition.Y));
                    break;
 
                case Direction.Right:
                    //门在右侧
                    limitLine = stateDoorPosition.X - XDoorLevelOffset;
                    if (stateRolePosition.X > limitLine)
                    {
                        //如果角色位于门右侧,先向左移
                        this.moveState.SyncMove(new ZTPoint(limitLine - stateRolePosition.X, 0));
                    }
 
                    //水平对齐
                    diff = stateDoorPosition.Y - stateRolePosition.Y;
                    this.moveState.SyncMove(new ZTPoint(0, diff));
 
                    //移动x,进入
                    this.moveState.SyncMove(new ZTPoint(this.gameRect.End.X - stateRolePosition.X, 0));
                    break;
 
                case Direction.Bottom:
                    //门在下方
                    limitLine = stateDoorPosition.Y - YDoorLevelOffset;
                    if (stateRolePosition.Y > limitLine)
                    {
                        //如果角色在门下方,先向上移
                        this.moveState.SyncMove(new ZTPoint(0,limitLine- stateRolePosition.Y));
                    }
 
                    //垂直对齐
                    diff = stateDoorPosition.X - stateRolePosition.X;
                    this.moveState.SyncMove(new ZTPoint(diff, 0));
 
                    //移动y,进入
                    this.moveState.SyncMove(new ZTPoint(0, this.gameRect.End.Y - stateRolePosition.Y));
                    break;
 
                case Direction.Left:
                    //门在左侧
                    limitLine = stateDoorPosition.X + XDoorLevelOffset;
                    if (stateRolePosition.X < limitLine)
                    {
                        //如果角色位于门左侧,先向右移
                        this.moveState.SyncMove(new ZTPoint(limitLine - stateRolePosition.X, 0));
                    }
 
                    //水平对齐
                    diff = stateDoorPosition.Y - stateRolePosition.Y;
                    this.moveState.SyncMove(new ZTPoint(0, diff));
 
                    //移动x,进入
                    this.moveState.SyncMove(new ZTPoint(this.gameRect.Start.X- stateRolePosition.X, 0));
                    break;
            }
            
            return new KillMonsterStateResult(STATE_DetectDoorIsOpen, true);
        }
        #endregion
 
        /// <summary>
        /// 得到最近的物品
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public ZTPoint GetNearlyThingItem(Image<Rgb, byte> image,ZTPoint rolePosition)
        {
            List<ZTPoint> points=DnfCVHelper.GetThingItemPoints(image, this.gameRect);
            if (points.Count <= 0)
            {
                return ZTPoint.Empty;
            }
 
            if (points.Count == 1)
            {
                return points[0];
            }
            
            double distance = 0;
            ZTPoint result = ZTPoint.Empty;
            for (int i = 0; i < points.Count; i++)
            {
                double temp = ZTImage.Utils.GetDistance(rolePosition.X, rolePosition.Y, points[i].X, points[i].Y);
                if (i == 0)
                {
                    distance = temp;
                    result = points[0];
                    continue;
                }
 
                if (temp < distance)
                {
                    distance = temp;
                    result = points[i];
                }
            }
 
            return result;
        }
 
        /// <summary>
        /// 是否完成
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public bool IsComplete(Image<Rgb,byte> image)
        {
            if (this.house.IsEnd)
            {
 
                if (DnfCVHelper.IsJiangli(image, this.gameRect))
                {
                    //翻牌
                    this.moveState.StopMove();
                    Fanpai();
                    Thread.Sleep(5000);
                    this.exitResult = true;
                    return true;
                }
 
                if (DnfCVHelper.IsCompleteRoom(image, this.gameRect))
                {
                    //已经刷完
                    this.moveState.StopMove();
                    this.exitResult = true;
                    return true;
                }
            }
            return false;
        }
 
        #region 翻牌
        /// <summary>
        /// 牌的位置
        /// </summary>
        private static readonly ZTPoint[] CardList = new ZTPoint[] {
            new ZTPoint(364,231),
            new ZTPoint(561,231),
            new ZTPoint(744,231),
            new ZTPoint(924,231),
 
 
            new ZTPoint(364,570),
            new ZTPoint(561,570),
            new ZTPoint(744,570),
            new ZTPoint(924,570),
 
        };
        /// <summary>
        /// 翻牌
        /// 1-4
        /// 5-8
        /// </summary>
        /// <param name="number"></param>
        private void Fanpai()
        {
            Thread.Sleep(2000);
            Int32 number = RandomUtils.G(1, 4);
            ZTPoint willPosition = this.gameRect.Start.Add(CardList[number - 1]);
            G.Instance.InputControl.MoveToAndClick(RandomUtils.PointRange(willPosition,10));
 
            //判断黄金版是否可以翻
            if (DnfCVHelper.HasMowangqiyueCard(this.gameRect))
            {
                number = RandomUtils.G(5, 8);
                willPosition = this.gameRect.Start.Add(CardList[number - 1]);
                G.Instance.InputControl.MoveToAndClick(RandomUtils.PointRange(willPosition, 10));
            }
 
        }
        #endregion
    }
}