Files @ 71732f305328
Branch filter:

Location: ATITD-Tools/Desert-Paint-Lab/PaintRecipe.cs

jmaltzen
Pre-allocate a bunch of map entries, given that we know there are a fixed set of colors. Use fixed arrays in places where maps/sets were excessive. Switch away from using Gtk.Application.Invoke, which leaks a bunch of memory.
/*
 * 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;
using System.Collections.Generic;

namespace DesertPaintLab
{
    public class PaintRecipe
    {
        public struct RGB
        {
            public int r;
            public int g;
            public int b;
        };

        public class RecipeIngredient
        {
            public string name;
            public uint quantity;

            public RecipeIngredient(string name, uint quantity)
            {
                this.name = name;
                this.quantity = quantity;
            }

            public RecipeIngredient(RecipeIngredient other)
            {
                this.name = other.name;
                this.quantity = other.quantity;
            }

            public override string ToString()
            {
                return String.Format("{0} {1}", name, quantity);
            }
        };

        private List<RecipeIngredient> recipe = new List<RecipeIngredient>();
        private List<string> reagents = new List<string>();

        private bool dirty = false;
        private PaintColor reactedColor = new PaintColor();
        private PaintColor baseColor = new PaintColor();
        private ReactionSet reactions;

        public PaintRecipe()
        {
        }

        public PaintRecipe(PaintRecipe other)
        {
            this.dirty = true;
            this.reactions = other.reactions;
            foreach (string reagentName in other.reagents)
            {
                this.reagents.Add(reagentName);
            }
            foreach (RecipeIngredient copyIngredient in other.recipe)
            {
                RecipeIngredient ingredient = new RecipeIngredient(copyIngredient);
                this.recipe.Add(ingredient);
            }
        }

        public void CopyFrom(PaintRecipe other)
        {
            this.dirty = true;
            this.reactions = other.reactions;
            this.reagents.Clear();
            foreach (string reagentName in other.reagents)
            {
                this.reagents.Add(reagentName);
            }
            this.recipe.Clear();
            foreach (RecipeIngredient otherIngredient in other.recipe)
            {
                RecipeIngredient ingredient = new RecipeIngredient(otherIngredient);
                this.recipe.Add(ingredient);
            }
        }

        public List<RecipeIngredient> Ingredients
        {
            get {
                return recipe;
            }
        }

        public ReactionSet Reactions
        {
            get
            {
                return reactions;
            }
            set
            {
                dirty = true;
                reactions = value;
            }
        }

        public PaintColor ReactedColor
        {
            get
            {
                if (dirty)
                {
                    ComputeBaseColor();
                    ComputeReactedColor();
                    dirty = false;
                }
                return reactedColor;
            }
        }

        public PaintColor BaseColor
        {
            get
            {
                if (dirty)
                {
                    ComputeBaseColor();
                    ComputeReactedColor();
                    dirty = false;
                }
                return baseColor;
            }
        }

        public void AddReagent(String reagentName)
        {
            AddReagent(reagentName, 1);
        }

        public void AddReagent(String reagentName, uint quantity)
        {
            if (quantity == 0)
            {
                return;
            }
            Reagent reagent = ReagentManager.GetReagent(reagentName);
            if (reagent == null)
            {
                Console.WriteLine("ERROR: invalid reagent {0}", reagentName);
                return;
            }

            RecipeIngredient ingredient = recipe.Find(x => x.name.Equals(reagentName));
            if (ingredient != null)
            {
                if (!reagent.IsCatalyst)
                {
                    ingredient.quantity += quantity;
                    dirty = true;
                }
            }
            else
            {
                RecipeIngredient newIngredient = new RecipeIngredient(reagentName, reagent.IsCatalyst ? 1 : quantity);
                recipe.Add(newIngredient);
                dirty = true;
            }
            reagents.Add(reagentName);
        }

        public void Clear()
        {
            reagents.Clear();
            recipe.Clear();
        }

        byte CalculateColor(int baseSum, uint pigmentCount, int reactSum)
        {
            // Changed to Math.Floor from Math.Round, since Round appears to be incorrect.
            return (byte)Math.Max(Math.Min(Math.Floor((((float)baseSum / (float)pigmentCount) + (float)reactSum)), 255), 0);
        }

        // Compute the color including reactions based on the player's profile
        private void ComputeReactedColor()
        {
            RGB baseRGB;
            baseRGB.r = 0;
            baseRGB.g = 0;
            baseRGB.b = 0;
            RGB reactionColor;
            reactionColor.r = 0;
            reactionColor.g = 0;
            reactionColor.b = 0;

            uint pigmentCount = 0;

            // track visited reagents so the reaction is only applied once
            SortedDictionary<string,bool> reagentSet = new SortedDictionary<string,bool>();
            List<Reagent> prevReagents = new List<Reagent>();

            foreach (RecipeIngredient ingredient in recipe)
            {
                string reagentName = ingredient.name;
                if (reagentName == null)
                {
                    continue;   
                }
                
                Reagent reagent = ReagentManager.GetReagent(reagentName);
                if (!reagent.IsCatalyst)
                {
                    baseRGB.r += (reagent.Color.Red * (int)ingredient.quantity);
                    baseRGB.g += (reagent.Color.Green * (int)ingredient.quantity);
                    baseRGB.b += (reagent.Color.Blue * (int)ingredient.quantity);
                    pigmentCount += ingredient.quantity;
                }

                if (!reagentSet.ContainsKey(reagentName) && reagentSet.Count <= 4)
                {
                    reagentSet[reagentName] = true;
                    // Run reactions.
                    foreach (Reagent otherReagent in prevReagents)
                    {
                        Reaction reaction = reactions.Find(otherReagent, reagent);
                        if (reaction != null)
                        {
                            reactionColor.r += reaction.Red;
                            reactionColor.g += reaction.Green;
                            reactionColor.b += reaction.Blue;
                        }
                    }
                    prevReagents.Add(reagent);
                }
            }
            reactedColor.Red = CalculateColor(baseRGB.r, pigmentCount, reactionColor.r);
            reactedColor.Green = CalculateColor(baseRGB.g, pigmentCount, reactionColor.g);
            reactedColor.Blue = CalculateColor(baseRGB.b, pigmentCount, reactionColor.b);
        }

        // Compute the base color without any reactions
        private void ComputeBaseColor()
        {
            RGB baseRGB;
            baseRGB.r = 0;
            baseRGB.g = 0;
            baseRGB.b = 0;
            uint pigmentCount = 0;
            foreach (RecipeIngredient ingredient in recipe)
            {
                string reagentName = ingredient.name;
                if (reagentName == null)
                {
                    continue;   
                }
                
                Reagent reagent = ReagentManager.GetReagent(reagentName);
                if (!reagent.IsCatalyst)
                {
                    baseRGB.r += (reagent.Color.Red * (int)ingredient.quantity);
                    baseRGB.g += (reagent.Color.Green * (int)ingredient.quantity);
                    baseRGB.b += (reagent.Color.Blue * (int)ingredient.quantity);
                    pigmentCount += ingredient.quantity;
                }
            }
            baseColor.Red = CalculateColor(baseRGB.r, pigmentCount, 0);
            baseColor.Green = CalculateColor(baseRGB.g, pigmentCount, 0);
            baseColor.Blue = CalculateColor(baseRGB.b, pigmentCount, 0);
        }

        // Compute the base color without any reactions
        public uint Cost
        {
            get {
                uint cost = 0;
                foreach (RecipeIngredient ingredient in recipe)
                {
                    string reagentName = ingredient.name;
                    if (reagentName == null)
                    {
                        continue;   
                    }
                    
                    Reagent reagent = ReagentManager.GetReagent(reagentName);
                    cost += (reagent.Cost * ingredient.quantity);
                }
                return cost;
            }
        }

        public uint GetBulkCost(uint quantity)
        {
            uint cost = 0;
            foreach (RecipeIngredient ingredient in recipe)
            {
                string reagentName = ingredient.name;
                if (reagentName == null)
                {
                    continue;   
                }
                
                Reagent reagent = ReagentManager.GetReagent(reagentName);
                cost += (reagent.Cost * ingredient.quantity);
            }
            uint batchCount = (uint)Math.Ceiling((double)quantity / (double)cost);  // number of batches require to make quantity
            return batchCount * cost;
        }

        public bool IsValid
        {
            get
            {
                uint weight = 0;
                foreach (RecipeIngredient ingredient in recipe)
                {
                    string reagentName = ingredient.name;
                    if (reagentName == null)
                    {
                        continue;   
                    }
                    
                    Reagent reagent = ReagentManager.GetReagent(reagentName);
                    if (!reagent.IsCatalyst)
                    {
                        weight += ingredient.quantity;
                    }
                }
                return (weight >= 10);
            }
        }

        public bool CheckMissingReactions(ref List<KeyValuePair<string, string>> missing)
        {
            missing.Clear();

            SortedDictionary<string,bool> reagentSet = new SortedDictionary<string,bool>();
            List<Reagent> prevReagents = new List<Reagent>();

            foreach (RecipeIngredient ingredient in recipe)
            {
                string reagentName = ingredient.name;
                if (reagentName == null)
                {
                    continue;   
                }
                
                Reagent reagent = ReagentManager.GetReagent(reagentName);
                if (!reagentSet.ContainsKey(reagentName) && reagentSet.Count <= 4)
                {
                    reagentSet[reagentName] = true;
                    // Run reactions.
                    foreach (Reagent otherReagent in prevReagents)
                    {
                        Reaction reaction = reactions.Find(otherReagent, reagent);
                        if (reaction == null)
                        {
                            missing.Add(new KeyValuePair<string, string>(otherReagent.Name, reagent.Name));
                        }
                    }
                    prevReagents.Add(reagent);
                }
            }
            return (missing.Count > 0);
        }
    }
}