asmrobot
2019-11-13 576b92fd82f568572bc4beb125fa0ba0191a602f
src/RichCreator.Utility/CV/DnfCVHelper.cs
@@ -68,7 +68,7 @@
        public static bool IsInSaiLiYaHouse(ZTRectangle gameRect)
        {
            ZTRectangle mailText = ZTRectangle.Empty;
            ZTPoint limitPoint = gameRect.GetRatioPoint(0.667f);
            Structs.ZTPoint limitPoint = gameRect.GetRatioPoint(0.667f);
            ZTRectangle limit = new ZTRectangle(limitPoint.X, gameRect.Start.Y, gameRect.End.X, gameRect.End.Y);
            //截图
            System.Drawing.Bitmap bitmap = ScreenCapture.Instance.CaptureScreen();
@@ -374,28 +374,28 @@
        //min:(0,0.,0.0352941192686558),max:(0.,0.,0.0980392172932625)
        private static ZTHsvFloatColor equipmentColorMin = new ZTHsvFloatColor(0,0, 0);
        private static ZTHsvFloatColor equipmentColorMax = new ZTHsvFloatColor(1, 0.7827f, 0.1099f);
        private static ZTPoint[] equipmentComparePositionss;
        private static Structs.ZTPoint[] equipmentComparePositionss;
        /// <summary>
        /// 获取有装备的所有点,只便利前两行
        /// </summary>
        /// <param name="startPoint"></param>
        /// <returns></returns>
        public static List<Int32> GetEquipmentIndexs(ZTPoint startPoint)
        public static List<Int32> GetEquipmentIndexs(Structs.ZTPoint startPoint)
        {
            Image<Rgb, byte> image = ScreenCapture.Instance.CaptureScreenReturnImage();
            return GetEquipmentIndexs(image, startPoint);
        }
        public static List<Int32> GetEquipmentIndexs(Image<Rgb, byte> image,ZTPoint startPoint)
        public static List<Int32> GetEquipmentIndexs(Image<Rgb, byte> image, Structs.ZTPoint startPoint)
        {
            if (equipmentComparePositionss == null)
            {
                equipmentComparePositionss = new ZTPoint[15 * 15];
                equipmentComparePositionss = new Structs.ZTPoint[15 * 15];
                for (int y = 0; y < 15; y++)
                {
                    for (int x = 0; x < 15; x++)
                    {
                        equipmentComparePositionss[y * 15 + x] = new ZTPoint(x, y);
                        equipmentComparePositionss[y * 15 + x] = new Structs.ZTPoint(x, y);
                    }
                }
            }
@@ -445,7 +445,7 @@
            }
            //主角位置
            ZTPoint rolePosition = gameRect.Start + new ZTPoint(rects[0].Start.X + rolePositionOffset.Width, rects[0].Start.Y + rolePositionOffset.Height);
            ZTPoint rolePosition = gameRect.Start.Add(rects[0].Start.X + rolePositionOffset.Width, rects[0].Start.Y + rolePositionOffset.Height);
            return rolePosition;
        }
@@ -474,11 +474,11 @@
        /// <param name="image"></param>
        /// <param name="gameRect"></param>
        /// <returns></returns>
        public static List<ZTPoint> GetThingItemPoints(Image<Rgb, byte> image, ZTRectangle gameRect)
        public static List<Structs.ZTPoint> GetThingItemPoints(Image<Rgb, byte> image, ZTRectangle gameRect)
        {
            List<ZTLine> lines = CVHelper.FindLines(image, xLineMin,gameRect, Orientation.Horizontal, min, max);
            List<ZTLine> filterLines = new List<ZTLine>();
            List<ZTPoint> points = new List<ZTPoint>();
            List<Structs.ZTPoint> points = new List<Structs.ZTPoint>();
            Func<ZTLine, bool> existsLines = (line) =>
            {
                for (int i = 0; i < filterLines.Count; i++)
@@ -530,13 +530,13 @@
                }
                
                filterLines.Add(line);
                points.Add(new ZTPoint(line.X + line.Length / 2, line.Y + thingItemYOffset));
                points.Add(new Structs.ZTPoint(line.X + line.Length / 2, line.Y + thingItemYOffset));
            }
            return points;
        }
        #region Location Point 1
        private static readonly Hsv LocationPointMinHsv = new Hsv(0, 0, 0);
        private static readonly Hsv LocationPointMaxHsv = new Hsv(0, 0, 5);
        private static readonly ZTSize LocationPointMinLimitSize = new ZTSize(33, 33);
@@ -595,10 +595,10 @@
            number = 0;
            byte[,,] datas = rangeImage.Data;
            bool one = false, two = false, four = false, eight = false;
            ZTPoint center = locationPointRect.GetCenterPoint();
            Structs.ZTPoint center = locationPointRect.GetCenterPoint();
            //格1
            ZTPoint lefttop = center.Add(-11);
            Structs.ZTPoint lefttop = center.Add(-11);
            if (!BlockOf6x6IsColor(datas, lefttop, black))
            {
                if (!BlockOf6x6IsColor(datas, lefttop, white))
@@ -615,7 +615,7 @@
                one = true;
            }
            //格2
            ZTPoint righttop = center.Add(new ZTPoint(5,-11));
            Structs.ZTPoint righttop = center.Add(new Structs.ZTPoint(5,-11));
            if (!BlockOf6x6IsColor(datas, righttop, black))
            {
                if (!BlockOf6x6IsColor(datas, righttop, white))
@@ -633,7 +633,7 @@
            }
            //格4
            ZTPoint leftbottom = center.Add(new ZTPoint(-11, 5));
            Structs.ZTPoint leftbottom = center.Add(new Structs.ZTPoint(-11, 5));
            if (!BlockOf6x6IsColor(datas, leftbottom, black))
            {
                if (!BlockOf6x6IsColor(datas, leftbottom, white))
@@ -647,7 +647,7 @@
            }
            //格8
            ZTPoint rightbottom = center.Add(5);
            Structs.ZTPoint rightbottom = center.Add(5);
            if (!BlockOf6x6IsColor(datas, rightbottom, black))
            {
                if (!BlockOf6x6IsColor(datas, rightbottom, white))
@@ -675,7 +675,7 @@
        /// <param name="starty"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        private static bool BlockOf6x6IsColor(byte[,,] datas,ZTPoint start, byte color)
        private static bool BlockOf6x6IsColor(byte[,,] datas, Structs.ZTPoint start, byte color)
        {
            for (int y = start.Y; y < start.Y + 6; y++)
            {
@@ -690,5 +690,348 @@
            return true;
        }
        #endregion
        #region Location Point 2
        private static ColorArray LocationPoint = ColorArray.FromColorString(0, 0, "800,352,0,0,0$791,343,255,255,255$793,343,0,0,0$796,343,255,255,255$798,343,0,0,0$801,343,255,255,255$802,345,0,0,0$802,347,255,255,255$802,349,0,0,0$802,352,255,255,255$797,352,255,255,255$795,352,0,0,0$791,352,255,255,255$791,350,0,0,0$791,348,255,255,255$791,346,0,0,0$796,347,255,255,255$797,348,255,255,255");
        /// <summary>
        /// 获取定位点
        /// </summary>
        /// <param name="image"></param>
        /// <param name="gameRect"></param>
        /// <returns></returns>
        public static ParametersPoint GetLocationPoint(Image<Rgb,byte> image,ZTRectangle gameRect)
        {
            ZTRectangle rect = ZTRectangle.Empty;
            if (CVHelper.FindColorArray(out rect, image, LocationPoint, gameRect))
            {
                //获取定位标记,解析参数
                byte number = 0;
                bool bitValue = true;
                Int32 x = 0, y = 0;
                for (int bitIndex = 0; bitIndex <= 7; bitIndex++)
                {
                    //获取图像中存值的坐标
                    GetBitCoordinate(out x, out y, bitIndex);
                    //解析并设置值
                    if (!ParseBit(out bitValue, image, rect.Start.Add(x, y)))
                    {
                        return ParametersPoint.Empty;
                    }
                    else
                    {
                        number=SetBit(number, bitIndex, bitValue);
                    }
                }
                return new ParametersPoint(rect.GetCenterPoint(), number);
            }
            return ParametersPoint.Empty;
        }
        /// <summary>
        /// 设置指定位
        /// </summary>
        /// <param name="number"></param>
        /// <param name="bitIndex"></param>
        /// <param name="bitValue"></param>
        /// <returns></returns>
        private static byte SetBit(byte number, Int32 bitIndex,bool bitValue)
        {
            byte twonumber = 0;
            switch (bitIndex)
            {
                case 0:
                    if (bitValue)
                    {
                        twonumber = 0b00000001;
                    }
                    else
                    {
                        twonumber = 0b11111110;
                    }
                    break;
                case 1:
                    if (bitValue)
                    {
                        twonumber = 0b00000010;
                    }
                    else
                    {
                        twonumber = 0b11111101;
                    }
                    break;
                case 2:
                    if (bitValue)
                    {
                        twonumber = 0b00000100;
                    }
                    else
                    {
                        twonumber = 0b11111011;
                    }
                    break;
                case 3:
                    if (bitValue)
                    {
                        twonumber = 0b00001000;
                    }
                    else
                    {
                        twonumber = 0b11110111;
                    }
                    break;
                case 4:
                    if (bitValue)
                    {
                        twonumber = 0b00010000;
                    }
                    else
                    {
                        twonumber = 0b11101111;
                    }
                    break;
                case 5:
                    if (bitValue)
                    {
                        twonumber = 0b00100000;
                    }
                    else
                    {
                        twonumber = 0b11011111;
                    }
                    break;
                case 6:
                    if (bitValue)
                    {
                        twonumber = 0b01000000;
                    }
                    else
                    {
                        twonumber = 0b10111111;
                    }
                    break;
                case 7:
                    if (bitValue)
                    {
                        twonumber = 0b10000000;
                    }
                    else
                    {
                        twonumber = 0b01111111;
                    }
                    break;
            }
            if (bitValue)
            {
                return (byte)(number | twonumber);
            }
            else
            {
                return (byte)(number & twonumber);
            }
        }
        /// <summary>
        /// 得到指定位坐标
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="bitIndex"></param>
        private static void GetBitCoordinate(out Int32 x, out Int32 y, Int32 bitIndex)
        {
            x = 0;
            y = 0;
            switch (bitIndex)
            {
                case 0:
                    x = 8;
                    y = 6;
                    break;
                case 1:
                    x = 6;
                    y = 6;
                    break;
                case 2:
                    x = 4;
                    y = 6;
                    break;
                case 3:
                    x = 2;
                    y = 6;
                    break;
                case 4:
                    x = 8;
                    y = 2;
                    break;
                case 5:
                    x = 6;
                    y = 2;
                    break;
                case 6:
                    x = 4;
                    y = 2;
                    break;
                case 7:
                    x = 2;
                    y = 2;
                    break;
            }
        }
        /// <summary>
        /// 解析位状态
        /// true:1
        /// false:0
        /// </summary>
        /// <param name="bit"></param>
        /// <param name="image"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns>true:解析成功,false:解析失败</returns>
        private static bool ParseBit(out bool bit,Image<Rgb, byte> image, ZTPoint point)
        {
            bit = true;
            if (image.Data[point.Y, point.X, 0] == 0xff && image.Data[point.Y, point.X, 1] == 0xff && image.Data[point.Y, point.X, 2] == 0xff&&
                image.Data[point.Y, point.X+1, 0] == 0xff && image.Data[point.Y, point.X+1, 1] == 0xff && image.Data[point.Y, point.X+1, 2] == 0xff&&
                image.Data[point.Y+1, point.X, 0] == 0xff && image.Data[point.Y+1, point.X, 1] == 0xff && image.Data[point.Y+1, point.X, 2] == 0xff&&
                image.Data[point.Y+1, point.X+1, 0] == 0xff && image.Data[point.Y+1, point.X+1, 1] == 0xff && image.Data[point.Y+1, point.X+1, 2] == 0xff)
            {
                //白
                bit = true;
                return true;
            }
            if (image.Data[point.Y, point.X, 0] == 0x00 && image.Data[point.Y, point.X, 1] == 0x00 && image.Data[point.Y, point.X, 2] == 0x00 &&
                image.Data[point.Y, point.X + 1, 0] == 0x00 && image.Data[point.Y, point.X + 1, 1] == 0x00 && image.Data[point.Y, point.X + 1, 2] == 0x00 &&
                image.Data[point.Y + 1, point.X, 0] == 0x00 && image.Data[point.Y + 1, point.X, 1] == 0x00 && image.Data[point.Y + 1, point.X, 2] == 0x00 &&
                image.Data[point.Y + 1, point.X + 1, 0] == 0x00 && image.Data[point.Y + 1, point.X + 1, 1] == 0x00 && image.Data[point.Y + 1, point.X + 1, 2] == 0x00)
            {
                //黑
                bit = false;
                return true;
            }
            return false;
        }
        #endregion
        #region minimap
        /// <summary>
        /// 小方格色块偏移
        /// </summary>
        private static ZTPoint[] colorOffset = new ZTPoint[] {
            new ZTPoint(2,14),
            new ZTPoint(2,15),
            new ZTPoint(15,14),
            new ZTPoint(15,15),
        };
        /// <summary>
        /// 已经走过的房间颜色(青色)
        /// </summary>
        private static ZTHsvFloatColor crossedColorMin = new ZTHsvFloatColor(0.498, 0.998, 0.998);
        private static ZTHsvFloatColor crossedColorMax = new ZTHsvFloatColor(0.502, 1, 1);
        /// <summary>
        /// 是否穿越过的房间(青色)
        /// </summary>
        /// <param name="image"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public static bool IsCrossedHouseColor(Image<Rgb, byte> image, Int32 x, Int32 y)
        {
            if (CVHelper.InRange(image.Data, x, y, crossedColorMin, crossedColorMax, colorOffset))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 当前房间颜色(蓝色)
        /// </summary>
        private static ZTHsvFloatColor currentHouseColorMin = new ZTHsvFloatColor(0.665f, 0.998, 0.998);
        private static ZTHsvFloatColor currentHouseColorMax = new ZTHsvFloatColor(0.669f, 1, 1);
        /// <summary>
        /// 是否当前房间(蓝色)
        /// </summary>
        /// <param name="image"></param>
        /// <param name="minMapStart"></param>
        /// <param name="houseIndex"></param>
        /// <returns></returns>
        public static bool IsCurrentHouseColor(Image<Rgb, byte> image, Int32 x, Int32 y)
        {
            if (CVHelper.InRange(image.Data, x, y, currentHouseColorMin, currentHouseColorMax, colorOffset))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 下一房间颜色(绿色)
        /// </summary>
        private static ZTHsvFloatColor nextHouseColorMin = new ZTHsvFloatColor(0.298f, 0.998, 0.998);
        private static ZTHsvFloatColor nextHouseColorMax = new ZTHsvFloatColor(0.302, 1, 1);
        /// <summary>
        /// 是否下一房间,绿色
        /// </summary>
        /// <param name="image"></param>
        /// <param name="minMapStart"></param>
        /// <param name="houseIndex"></param>
        /// <returns></returns>
        public static bool IsNextHouseColor(Image<Rgb, byte> image, Int32 x, Int32 y)
        {
            if (CVHelper.InRange(image.Data, x, y, nextHouseColorMin, nextHouseColorMax, colorOffset))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 最终房间颜色(红色1)
        /// </summary>
        private static ZTHsvFloatColor overHouse1ColorMin = new ZTHsvFloatColor(0, 0.998, 0.998);
        private static ZTHsvFloatColor overHouse1ColorMax = new ZTHsvFloatColor(0.002, 1, 1);
        /// <summary>
        /// 最终房间颜色(红色2)
        /// </summary>
        private static ZTHsvFloatColor overHouse2ColorMin = new ZTHsvFloatColor(0.998, 0.998, 0.998);
        private static ZTHsvFloatColor overHouse2ColorMax = new ZTHsvFloatColor(1.0f, 1, 1);
        /// <summary>
        /// 是否最终房间
        /// </summary>
        /// <param name="image"></param>
        /// <param name="minMapStart"></param>
        /// <param name="houseIndex"></param>
        /// <returns></returns>
        public static bool IsOverHouseColor(Image<Rgb, byte> image, Int32 x, Int32 y)
        {
            if (CVHelper.InRange(image.Data, x, y, overHouse1ColorMin, overHouse1ColorMax, colorOffset)
                || CVHelper.InRange(image.Data, x, y, overHouse2ColorMin, overHouse2ColorMax, colorOffset))
            {
                return true;
            }
            return false;
        }
        #endregion
    }
}