Files
@ 71732f305328
Branch filter:
Location: ATITD-Tools/Desert-Paint-Lab/PaintRecipe.cs - annotation
71732f305328
12.6 KiB
text/x-csharp
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.
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | 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 f28757bb21cb f28757bb21cb f28757bb21cb f28757bb21cb f28757bb21cb f28757bb21cb f28757bb21cb 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 2040107278aa 2040107278aa 2040107278aa f28757bb21cb f28757bb21cb 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa f28757bb21cb f28757bb21cb f28757bb21cb f28757bb21cb 2481742fac33 2481742fac33 2481742fac33 2481742fac33 2481742fac33 2481742fac33 2481742fac33 2481742fac33 2481742fac33 2481742fac33 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 71732f305328 2481742fac33 2481742fac33 78ecf5feebe4 78ecf5feebe4 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 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 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 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 2040107278aa f28757bb21cb f28757bb21cb 2040107278aa f28757bb21cb 2040107278aa 2040107278aa f28757bb21cb f28757bb21cb f28757bb21cb 2040107278aa f28757bb21cb 2040107278aa 2040107278aa 2040107278aa 2040107278aa f28757bb21cb f28757bb21cb f28757bb21cb f28757bb21cb f28757bb21cb 2040107278aa f28757bb21cb f28757bb21cb f28757bb21cb f28757bb21cb f28757bb21cb 2040107278aa f28757bb21cb 2040107278aa f28757bb21cb f28757bb21cb f28757bb21cb f28757bb21cb f28757bb21cb f28757bb21cb f28757bb21cb f28757bb21cb 2040107278aa 2040107278aa 2040107278aa 2040107278aa f28757bb21cb 2040107278aa 2040107278aa f28757bb21cb 2040107278aa 2040107278aa 2040107278aa f28757bb21cb 2040107278aa 2040107278aa f28757bb21cb 2040107278aa 2040107278aa 2040107278aa f28757bb21cb f28757bb21cb 2040107278aa f28757bb21cb f28757bb21cb 2040107278aa 2040107278aa 2040107278aa f28757bb21cb f28757bb21cb f28757bb21cb 2040107278aa f28757bb21cb 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa f28757bb21cb 2040107278aa f28757bb21cb f28757bb21cb f28757bb21cb f28757bb21cb f28757bb21cb f28757bb21cb f28757bb21cb f28757bb21cb 2040107278aa 2040107278aa 2040107278aa 2040107278aa f28757bb21cb f28757bb21cb 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 2040107278aa 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 | /*
* 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);
}
}
}
|