Changeset - e940c165aae5
[Not reviewed]
default
0 1 0
Jason Maltzen - 6 years ago 2018-05-03 04:11:51
jason@hiddenachievement.com
Fix bar widths on reaction recorder
1 file changed with 135 insertions and 132 deletions:
0 comments (0 inline, 0 general)
ReactionRecorder.cs
Show inline comments
...
 
@@ -10,50 +10,50 @@
 

	
 
 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;
 
using System.IO;
 

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

	
 
        const int DEFAULT_SWATCH_HEIGHT = 24; // including top and bottom borders
 
        const int DEFAULT_SWATCH_WIDTH = 260;
 
        const int DEFAULT_COLOR_BAR_WIDTH = 306;
 
        const int DEFAULT_SWATCH_WIDTH = 274;
 
        const int DEFAULT_COLOR_BAR_WIDTH = 320;
 
        const int DEFAULT_RED_BAR_SPACING = 32;
 
        const int DEFAULT_GREEN_BAR_SPACING = 42;
 
        const int DEFAULT_BLUE_BAR_SPACING = 52;
 

	
 
        const int DEFAULT_SWATCH_TEST_WIDTH = 26; // width to test on ends of swatch (10% on either end)
 

	
 
        int swatchHeight = DEFAULT_SWATCH_HEIGHT;
 
        int swatchWidth = DEFAULT_SWATCH_WIDTH;
 
        int swatchTestWidth = DEFAULT_SWATCH_TEST_WIDTH;
 
        int colorBarWidth = DEFAULT_COLOR_BAR_WIDTH;
 
        int redBarSpacing = DEFAULT_RED_BAR_SPACING;
 
        int greenBarSpacing = DEFAULT_GREEN_BAR_SPACING;
 
        int blueBarSpacing = DEFAULT_BLUE_BAR_SPACING;
 
        int pixelMultiplier = 1;
 

	
 
        bool firstRun = true;
 
        int lastSwatchX = -1;
 
        int lastSwatchY = -1;
 

	
 
        private static ReactionRecorder _instance;
 
        public static ReactionRecorder Instance
 
        {
 
            get {
 
                if (_instance == null)
...
 
@@ -173,228 +173,231 @@ namespace DesertPaintLab
 

	
 
            // grab the swatch color
 
            int swatchColorStart = testPixelStart + (2 * pixelMultiplier * stride); // 2 rows below the test pixel, skipping the faded color
 
            r = pixBytes[swatchColorStart];
 
            g = pixBytes[swatchColorStart+1];
 
            b = pixBytes[swatchColorStart+2];
 

	
 
            // scan the column from 2 below the top to 2 above the bottom to ensure the color matches
 
            for (int i = (2*pixelMultiplier); result && (i < swatchHeight-(2*pixelMultiplier)); ++i)
 
            {
 
                int otherPixelStart = testPixelStart + (stride * i);
 
                result &= IsColorMatch(pixBytes[otherPixelStart], pixBytes[otherPixelStart+1], pixBytes[otherPixelStart+2], r, g, b);
 
            }
 

	
 
            if (!result)
 
            {
 
                WriteLog("Swatch slice at {0}, {1} failed to match", x, y);
 
            }
 

	
 
            return result;
 
        }
 

	
 
        unsafe private bool IsPossibleSwatchUpperLeft(byte* pixBytes, int x, int y, int stride)
 
        {
 
            int testPixelStart = (y * stride) + (x * 3);
 

 
            if (testPixelStart < stride)
 
            {
 
                return false;
 
            }
 

 
            int testPixelStart = (y * stride) + (x * 3);
 

	
 
            if (testPixelStart < stride)
 
            {
 
                return false;
 
            }
 

	
 
            bool result = true;
 
            // test the left edge for dark pixels
 
            int i = 0;
 
            for (i = 0; result && (i < swatchHeight-pixelMultiplier); ++i)
 
            {
 
                int otherPixelStart = testPixelStart + (stride * i);
 
                result &= IsDarkPixel(pixBytes[otherPixelStart], pixBytes[otherPixelStart+1], pixBytes[otherPixelStart+2]);
 
            }
 
            if (!result)
 
            {
 
                // No dark border on the left side
 
            if (!result)
 
            {
 
                // No dark border on the left side
 
                // WriteLog("Failed to find left border for swatch at {0}, {1}", x, y);
 
                return false;
 
                return false;
 
            }
 

	
 
            // test the dark top border and for papyrus above and below the swatch
 
            bool borderError = false;
 
            int papyErrorCount = 0;
 
            for (i = 0; result && (i < swatchWidth); ++i)
 
            {
 
                int otherPixelStart = testPixelStart + (3 * i);
 
                bool isBorder = IsDarkPixel(pixBytes[otherPixelStart], pixBytes[otherPixelStart+1], pixBytes[otherPixelStart+2]);
 
                result &= isBorder;
 
                if (!isBorder)
 
                {
 
                    borderError = true;
 
                if (!isBorder)
 
                {
 
                    borderError = true;
 
                }
 
                if (otherPixelStart >= stride)
 
                if (otherPixelStart >= stride)
 
                {
 
                    otherPixelStart = otherPixelStart - stride;
 
                    papyErrorCount += (IsPapyTexture(pixBytes[otherPixelStart], pixBytes[otherPixelStart + 1], pixBytes[otherPixelStart + 2]) ? 0 : 1);
 
                    papyErrorCount += (IsPapyTexture(pixBytes[otherPixelStart], pixBytes[otherPixelStart + 1], pixBytes[otherPixelStart + 2]) ? 0 : 1);
 
                }
 
                else
 
                {
 
                    papyErrorCount++;
 
                else
 
                {
 
                    papyErrorCount++;
 
                }
 

	
 
                // Checking along the bottom of the swatch - 
 
                otherPixelStart = testPixelStart + (stride * swatchHeight) + (3 * i);
 
                papyErrorCount += (IsPapyTexture(pixBytes[otherPixelStart], pixBytes[otherPixelStart+1], pixBytes[otherPixelStart+2]) ? 0 : 1);
 
            }
 

	
 
            result &= (papyErrorCount < (swatchWidth / 20)); // allow up to 5% error rate checking for papy texture, because this seems to be inconsistent
 
            if (!result && ((i > (swatchWidth*0.8)) || (papyErrorCount >= (swatchWidth/20))))
 
            {
 
                if (!borderError && (papyErrorCount < swatchWidth))
 
                {
 
                    WriteLog("Found a potential swatch candidate of width {0} at {1},{2} that had {3} failures matching papyrus texture", i, x, y, papyErrorCount);
 
                }
 
                if (!borderError && (papyErrorCount < swatchWidth))
 
                {
 
                    WriteLog("Found a potential swatch candidate of width {0} at {1},{2} that had {3} failures matching papyrus texture", i, x, y, papyErrorCount);
 
                }
 
            }
 

	
 
            return result;
 
        }
 

	
 
        unsafe private bool TestPosition(int x, int y, byte* pixBytes, int screenshotWidth, int screenshotHeight, int stride, ref PaintColor reactedColor, ref int redPixelStart)
 
        {
 
        unsafe private bool TestPosition(int x, int y, 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;
 

 
            // 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 = IsPossibleSwatchUpperLeft(pixBytes, x, y, stride); // ((pixel_r < 0x46) && (pixel_g < 0x46) && (pixel_b < 0x46));
 
            if (foundSwatch)
 
            {
 
                int borderXOffset = 0;
 
                for (borderXOffset = (2 * pixelMultiplier); foundSwatch && (borderXOffset < swatchTestWidth); ++borderXOffset)
 
                {
 
                    foundSwatch &= IsPossibleSwatchSlice(pixBytes, x + borderXOffset, y, stride);
 
                    foundSwatch &= IsPossibleSwatchSlice(pixBytes, x + swatchWidth - 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 = 2; i < swatchHeight - (2 * pixelMultiplier); ++i)
 
                {
 
                    otherPixelStart = pixelStart + (stride * i);
 
                    if (!IsColorMatch(pixel_r, pixel_g, pixel_b, pixBytes[otherPixelStart], pixBytes[otherPixelStart + 1], pixBytes[otherPixelStart + 2]))
 
                    {
 
                        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);
 
                    WriteLog("Found the color swatch at {0}, {1}. Color={2}", x, y, reactedColor);
 
                    return true;
 
                }
 
            }
 
            return false;
 
            bool colorMatch = true;
 

	
 
            // 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 = IsPossibleSwatchUpperLeft(pixBytes, x, y, stride); // ((pixel_r < 0x46) && (pixel_g < 0x46) && (pixel_b < 0x46));
 
            if (foundSwatch)
 
            {
 
                int borderXOffset = 0;
 
                for (borderXOffset = (2 * pixelMultiplier); foundSwatch && (borderXOffset < swatchTestWidth); ++borderXOffset)
 
                {
 
                    foundSwatch &= IsPossibleSwatchSlice(pixBytes, x + borderXOffset, y, stride);
 
                    foundSwatch &= IsPossibleSwatchSlice(pixBytes, x + swatchWidth - 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 = 2; i < swatchHeight - (2 * pixelMultiplier); ++i)
 
                {
 
                    otherPixelStart = pixelStart + (stride * i);
 
                    if (!IsColorMatch(pixel_r, pixel_g, pixel_b, pixBytes[otherPixelStart], pixBytes[otherPixelStart + 1], pixBytes[otherPixelStart + 2]))
 
                    {
 
                        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;
 
                    }
 
                    WriteLog("Color {0}, {1}, {2} is no longer red at {3},{4}", pixBytes[redPixelStart], pixBytes[redPixelStart + 1], pixBytes[redPixelStart + 2], (redPixelStart % stride)/3, redPixelStart / stride);
 

	
 
                    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;
 
                    }
 
                    WriteLog("Color {0}, {1}, {2} is no longer green at pixel offset {3},{4}", pixBytes[greenPixelStart], pixBytes[greenPixelStart + 1], pixBytes[greenPixelStart + 2], (greenPixelStart % stride)/3, redPixelStart / stride);
 

	
 
                    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;
 
                    }
 
                    WriteLog("Color {0}, {1}, {2} is no longer blue at pixel offset {3},{4}", pixBytes[bluePixelStart], pixBytes[bluePixelStart + 1], pixBytes[bluePixelStart + 2], (bluePixelStart % stride)/3, bluePixelStart / stride);
 

	
 
                    reactedColor.Blue = (byte)Math.Round((float)bluePixelCount * 255f / (float)colorBarWidth);
 
                    WriteLog("Found the color swatch at {0}, {1}. Color={2} Red={3}px Green={4}px Blue={5}px", x, y, reactedColor, redPixelCount, greenPixelCount, bluePixelCount);
 
                    return true;
 
                }
 
            }
 
            return false;
 
        }
 

	
 
        unsafe public bool CaptureReaction(byte* pixBytes, int screenshotWidth, int screenshotHeight, int stride, ref PaintColor reactedColor, ref int redPixelStart)
 
        {
 
            if (!firstRun)
 
            {
 
                // If this is not the first run, let's check the last location, to see if the UI is still there.
 
                if (TestPosition(lastSwatchX, lastSwatchY, pixBytes, screenshotWidth, screenshotHeight, stride, ref reactedColor, ref redPixelStart))
 
                {
 
                    return true;
 
                }
 
                else
 
                {
 
                    firstRun = true;
 
                }
 
            if (!firstRun)
 
            {
 
                // If this is not the first run, let's check the last location, to see if the UI is still there.
 
                if (TestPosition(lastSwatchX, lastSwatchY, pixBytes, screenshotWidth, screenshotHeight, stride, ref reactedColor, ref redPixelStart))
 
                {
 
                    return true;
 
                }
 
                else
 
                {
 
                    firstRun = true;
 
                }
 
            }
 

	
 
            for (int x = 0; x < screenshotWidth - colorBarWidth; ++x)
 
            {
 
                for (int y = 0; y < (screenshotHeight - (blueBarSpacing + 1)  /*53*/); ++y)
 
                {
 
                    if (TestPosition(x, y, pixBytes, screenshotWidth, screenshotHeight, stride, ref reactedColor, ref redPixelStart))
 
                    {
 
                        lastSwatchX = x;
 
                        lastSwatchY = y;
 
                        firstRun = false;
 
                        return true;
 
                    if (TestPosition(x, y, pixBytes, screenshotWidth, screenshotHeight, stride, ref reactedColor, ref redPixelStart))
 
                    {
 
                        lastSwatchX = x;
 
                        lastSwatchY = y;
 
                        firstRun = false;
 
                        return true;
 
                    }
 
                }
 
            }
 
            return false;
 
        }
 

	
 
        public 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;
0 comments (0 inline, 0 general)