Files @ add7ccdffd3d
Branch filter:

Location: ATITD-Tools/Desert-Paint-Lab/ReactionRecorder.cs - annotation

jmaltzen
Added tag VERSION_T7_RC12 for changeset fc8ed9e3786e
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
5d0a31247a93
f28757bb21cb
2040107278aa
5d0a31247a93
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
5d0a31247a93
2040107278aa
2040107278aa
2040107278aa
2040107278aa
f28757bb21cb
f28757bb21cb
f28757bb21cb
5d0a31247a93
5d0a31247a93
f28757bb21cb
f28757bb21cb
2040107278aa
f28757bb21cb
2040107278aa
5d0a31247a93
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
2040107278aa
2040107278aa
2040107278aa
f28757bb21cb
5d0a31247a93
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
2040107278aa
2040107278aa
2040107278aa
f28757bb21cb
5d0a31247a93
5d0a31247a93
f28757bb21cb
5d0a31247a93
5d0a31247a93
f28757bb21cb
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
f28757bb21cb
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
f28757bb21cb
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
f28757bb21cb
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
/*
 * Copyright (c) 2015, Jason Maltzen

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

using System;

namespace DesertPaintLab
{
    // ReactionRecorder - business logic for recording paint reactions
    public class ReactionRecorder
    {
        const int COLOR_TOLERANCE = 3;

        const int DEFAULT_SWATCH_HEIGHT = 24;
        const int DEFAULT_SWATCH_WIDTH = 260;
        const int DEFAULT_COLOR_BAR_WIDTH = 306;
        const int DEFAULT_RED_BAR_SPACING = 32;
        const int DEFAULT_GREEN_BAR_SPACING = 42;
        const int DEFAULT_BLUE_BAR_SPACING = 52;

        static int swatchHeight = DEFAULT_SWATCH_HEIGHT;
        static int swatchWidth = DEFAULT_SWATCH_WIDTH;
        static int colorBarWidth = DEFAULT_COLOR_BAR_WIDTH;
        static int redBarSpacing = DEFAULT_RED_BAR_SPACING;
        static int greenBarSpacing = DEFAULT_GREEN_BAR_SPACING;
        static int blueBarSpacing = DEFAULT_BLUE_BAR_SPACING;

        private static bool IsPapyTexture(byte r, byte g, byte b)
        {
            return ((r > 0xD0) && (g >= 0xC4) && (b >= 0x91)) &&
                    ((r < 0xF4) && (g <= 0xED) && (b <= 0xCD));
        }

        public static void SetPixelMultiplier(int pixelMultiplier)
        {
            swatchHeight    = DEFAULT_SWATCH_HEIGHT * pixelMultiplier;
            swatchWidth     = DEFAULT_SWATCH_WIDTH * pixelMultiplier;
            colorBarWidth   = DEFAULT_COLOR_BAR_WIDTH * pixelMultiplier;
            redBarSpacing   = DEFAULT_RED_BAR_SPACING * pixelMultiplier;
            greenBarSpacing = DEFAULT_GREEN_BAR_SPACING * pixelMultiplier;
            blueBarSpacing  = DEFAULT_BLUE_BAR_SPACING * pixelMultiplier;
        }

        unsafe private static void ColorAt(byte *pixBytes, int x, int y, int stride, out byte r, out byte g, out byte b)
        {
            int pixelStart = (y * stride) + (x * 3);
                    r = pixBytes[pixelStart];
                    g = pixBytes[pixelStart + 1];
                    b = pixBytes[pixelStart + 2];
        }

        unsafe private static bool IsPossibleBorderPixel(byte* pixBytes, int x, int y, int stride)
        {
            int testPixelStart = (y * stride) + (x * 3);
            byte r = pixBytes[testPixelStart];
            byte g = pixBytes[testPixelStart+1];
            byte b = pixBytes[testPixelStart+2];

            bool isDarkPixel = false;
            bool isPapyAbove = false;
            bool isPapyBelow = false;
            // 1.) Check if this is a dark pixel.
            isDarkPixel = ((r < 0x46) && (g < 0x46) && (b < 0x46));
            
            // 2.) Check the pixel above it,
            // to see if it's from the papy texture.
            int otherPixelStart = testPixelStart - stride;
            isPapyAbove = ((otherPixelStart >= 0) &&
                IsPapyTexture(pixBytes[otherPixelStart++], pixBytes[otherPixelStart++], pixBytes[otherPixelStart]));

            // 3.) Check the pixel below where the swatch should be,
            // to see if it's also from the papy texture.
            otherPixelStart = testPixelStart + (stride * swatchHeight);
            isPapyBelow = (IsPapyTexture(pixBytes[otherPixelStart++], pixBytes[otherPixelStart++], pixBytes[otherPixelStart]));

            bool result = isDarkPixel && isPapyAbove && isPapyBelow;
            return result;
        }

        unsafe public static bool CaptureReaction(byte* pixBytes, int screenshotWidth, int screenshotHeight, int stride, ref PaintColor reactedColor, ref int redPixelStart)
        {
            byte pixel_r, pixel_g, pixel_b;
            int pixelStart, otherPixelStart;
            bool colorMatch = true;
            for (int x = 0; x < screenshotWidth - colorBarWidth; ++x)
            {
                for (int y = 0; y < (screenshotHeight - 53); ++y)
                {
                    // Look for the color swatch.
                    pixelStart = (y * stride) + (x * 3);
                    pixel_r = pixBytes[pixelStart];
                    pixel_g = pixBytes[pixelStart + 1];
                    pixel_b = pixBytes[pixelStart + 2];
                    
                    bool foundSwatch = IsPossibleBorderPixel(pixBytes, x, y, stride); // ((pixel_r < 0x46) && (pixel_g < 0x46) && (pixel_b < 0x46));
                    if (foundSwatch)
                    {
                        int borderXOffset = 0;
                        for (borderXOffset = 0; foundSwatch && (borderXOffset < swatchWidth); ++borderXOffset)
                        {
                            foundSwatch &= IsPossibleBorderPixel(pixBytes, x + borderXOffset, y, stride);
                        }
                    }

                    if (foundSwatch)
                    {
                        // found a swatch with an appropriate dark top border with papyrus texture above it and papyrus a jump below it
                        // 4.) Scan the left border of the potential swatch
                        // location.
                        colorMatch = true;
                        for (int i = 1; i < swatchHeight - 2; ++i)
                        {
                            otherPixelStart = pixelStart + (stride * i);
                            if ((Math.Abs(pixel_r - pixBytes[otherPixelStart++]) > COLOR_TOLERANCE) ||
                                (Math.Abs(pixel_g - pixBytes[otherPixelStart++]) > COLOR_TOLERANCE) ||
                                (Math.Abs(pixel_b - pixBytes[otherPixelStart]) > COLOR_TOLERANCE))
                            {
                                colorMatch = false;
                                break;
                            }
                        }
                        
                        if (colorMatch)
                        {
                            // WE FOUND THE SWATCH!
                            // Now we know where the color bars are.
                            redPixelStart = pixelStart + (redBarSpacing * stride);
                            int redPixelCount = 0;
                            while ((pixBytes[redPixelStart] > 0x9F) &&
                                   (pixBytes[redPixelStart + 1] < 0x62) &&
                                   (pixBytes[redPixelStart + 2]  < 0x62))
                            {
                                redPixelCount++;
                                // pixBytes[redPixelStart] = 0x00;
                                // pixBytes[redPixelStart + 1] = 0xFF;
                                // pixBytes[redPixelStart + 2] = 0xFF;
                                redPixelStart += 3;
                            }
                                
                            reactedColor.Red = (byte)Math.Round((float)redPixelCount * 255f / (float)colorBarWidth);
                            int greenPixelStart = pixelStart + (greenBarSpacing * stride);
                            
                            int greenPixelCount = 0;
                            while ((pixBytes[greenPixelStart] < 0x62) &&
                                   (pixBytes[greenPixelStart + 1] > 0x9F) &&
                                   (pixBytes[greenPixelStart + 2] < 0x62))
                            {
                                greenPixelCount++;
                                // pixBytes[greenPixelStart] = 0x00;
                                // pixBytes[greenPixelStart + 1] = 0xFF;
                                // pixBytes[greenPixelStart + 2] = 0xFF;
                                greenPixelStart += 3;
                            }

                            reactedColor.Green = (byte)Math.Round((float)greenPixelCount * 255f / (float)colorBarWidth);
                            int bluePixelStart = pixelStart + (blueBarSpacing * stride);
                            
                            int bluePixelCount = 0;
                            while ((pixBytes[bluePixelStart] < 0x62) &&
                                (pixBytes[bluePixelStart + 1] < 0x62) &&
                                (pixBytes[bluePixelStart + 2] > 0x9F))
                            {
                                bluePixelCount++;
                                // pixBytes[bluePixelStart] = 0x00;
                                // pixBytes[bluePixelStart + 1] = 0xFF;
                                // pixBytes[bluePixelStart + 2] = 0xFF;
                                bluePixelStart += 3;
                            }

                            reactedColor.Blue = (byte)Math.Round((float)bluePixelCount * 255f / (float)colorBarWidth);
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        public static bool RecordReaction(PlayerProfile profile, PaintColor expectedColor, PaintColor reactedColor, Reagent[] reagents)
        {
            bool saved = false;
            int r, g, b;
            if (reagents[2] != null)
            {
                // A 3-reagent reaction.
                Reaction reaction1 = profile.FindReaction(reagents[0], reagents[1]);
                Reaction reaction2 = profile.FindReaction(reagents[0], reagents[2]);
                Reaction reaction3 = profile.FindReaction(reagents[1], reagents[2]);
                
                r = reactedColor.Red - expectedColor.Red;
                g = reactedColor.Green - expectedColor.Green;
                b = reactedColor.Blue - expectedColor.Blue;
                
                if (reaction2 == null)
                {
                    r = r - reaction1.Red - reaction3.Red;
                    g = g - reaction1.Green - reaction3.Green;
                    b = b - reaction1.Blue - reaction3.Blue;
                    profile.SetReaction(reagents[0], reagents[2], new Reaction(r, g, b));
                    profile.Save();
                    saved = true;
                }
                else if (reaction3 == null)
                {
                    r = r - reaction1.Red - reaction2.Red;
                    g = g - reaction1.Green - reaction2.Green;
                    b = b - reaction1.Blue - reaction2.Blue;
                    profile.SetReaction(reagents[1], reagents[2], new Reaction(r, g, b));
                    profile.Save();
                    saved = true;
                }   
            }
            else if ((reagents[0] != null) && (reagents[1] != null))
            {
                // A 2-reagent reaction.
                r = reactedColor.Red - expectedColor.Red;
                g = reactedColor.Green - expectedColor.Green;
                b = reactedColor.Blue - expectedColor.Blue;
                profile.SetReaction(reagents[0], reagents[1], new Reaction(r, g, b));
                profile.Save();
                saved = true;
            }
            return saved;
        }
    }
}