asmrobot
2019-11-21 589ed88a5924a7494e21b95b6bbff5e46ff49ddd
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
using Emgu.CV;
using Emgu.CV.Structure;
using RichCreator.Utility.Captures;
using RichCreator.Utility.Structs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace RichCreator.Dnf
{
    /// <summary>
    /// 小地图
    /// </summary>
    public abstract class IMiniMap
    {
        public ZTRectangle GameRect { get; private set; }
 
        public ZTPoint MinimapOffset { get; private set; }
 
        
 
        public IMiniMap(ZTRectangle gameRect,ZTPoint miniMapOffset)
        {
            this.GameRect = gameRect;
            this.MinimapOffset = miniMapOffset.Add(gameRect.Start);
        }
 
        /// <summary>
        /// 获取房间号
        /// </summary>
        /// <param name="houseIndex">房间号,以0开始</param>
        /// <param name="image">图像,可以传空,传空则自获取</param>
        /// <returns>true:获取成功,false:获取失败</returns>
        public bool GetCurrentHouseIndex(out int houseIndex, Image<Rgb, byte> image)
        {
            if (image == null)
            {
                image = ScreenCapture.Instance.CaptureScreenReturnImage();
                image = image.GetSubRect(new System.Drawing.Rectangle(this.GameRect.Start.X, this.GameRect.Start.Y, this.GameRect.End.X - this.GameRect.Start.X + 1, this.GameRect.End.Y - this.GameRect.Start.Y + 1));
            }
            return GetCurrentHouse(out houseIndex, image);
        }
 
        /// <summary>
        /// 在超时之前获取房间号
        /// </summary>
        /// <param name="houseIndex">房间号,以0开始</param>
        /// <param name="image">图像,可以传空,传空则自获取</param>
        /// <param name="timeoutMillSecond">超时毫秒数</param>
        /// <returns>true:获取成功,false:获取失败</returns>
        public bool GetCurrentHouseIndexWaitTimeout(out int houseIndex, Image<Rgb, byte> image, CancellationToken cancelToken, Int32 timeoutMillSecond)
        {
            houseIndex = 0;
            Int32 houseid = -1;
            if (image != null)
            {
                if (GetCurrentHouse(out houseid, image))
                {
                    houseIndex = houseid;
                    return true;
                }
            }
            if (!FuncUtils.TimeoutCancelableWrap(timeoutMillSecond, cancelToken, () =>
            {
                using (System.Drawing.Bitmap bitmap = ScreenCapture.Instance.CaptureScreen())
                {
                    image = new Image<Rgb, byte>(bitmap);
                    image = image.GetSubRect(new System.Drawing.Rectangle(this.GameRect.Start.X, this.GameRect.Start.Y, this.GameRect.End.X - this.GameRect.Start.X + 1, this.GameRect.End.Y - this.GameRect.Start.Y + 1));
                    return GetCurrentHouse(out houseid, image);
                }
            }, 50))
            {
                return false;
            }
            houseIndex = houseid;
            return true;
        }
        
        /// <summary>
        /// 计算当前房间号
        /// </summary>
        /// <param name="houseIndex"></param>
        /// <param name="image"></param>
        /// <returns></returns>
        protected abstract bool GetCurrentHouse(out Int32 houseIndex, Image<Rgb, byte> image);
    }
}