Files
@ 41dee13aa976
Branch filter:
Location: ATITD-Tools/Desert-Paint-Lab/RecipeSearchNode.cs
41dee13aa976
17.8 KiB
text/x-csharp
Added tag T7_RC11 for changeset ca4042a2cbb4
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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 | /*
* 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;
using System.IO;
using System.Text.RegularExpressions;
namespace DesertPaintLab
{
public class RecipeSearchNode
{
//int initialReagentCount;
uint[] reagents;
public uint[] Reagents
{
get
{
return reagents;
}
}
uint INVALID_REAGENT;
int nextReagentPos;
public int ReagentCount
{
get
{
return nextReagentPos;
}
}
bool[] reagentInUse;
List<Reagent> costSortedReagents;
PaintRecipe testRecipe = null;
public PaintRecipe TestRecipe
{
get
{
return testRecipe;
}
set
{
testRecipe = value;
}
}
public uint CurrentTargetQuantity { get; set; }
public uint MaxQuantity { get; set; }
public uint UsedQuantity { get; private set; }
public uint CatalystCount { get; set; }
public uint FullQuantityDepth { get; set; }
public uint FullQuantity { get; set; }
public uint MinReagents { get; set; }
uint maxReagents;
public uint MaxReagents
{
get
{
return maxReagents;
}
set
{
maxReagents = value;
currentWeights = new uint[maxReagents];
}
}
uint[] currentWeights;
public uint[] CurrentWeights
{
get
{
return currentWeights;
}
}
public RecipeSearchNode(RecipeSearchNode other)
{
this.costSortedReagents = new List<Reagent>(other.costSortedReagents);
this.reagents = new uint[costSortedReagents.Count];
INVALID_REAGENT = (uint)costSortedReagents.Count;
for (int i = 0; i < costSortedReagents.Count; ++i)
{
this.reagents[i] = other.reagents[i];
}
reagentInUse = new bool[costSortedReagents.Count];
for (uint i = 0; i < costSortedReagents.Count; ++i)
{
reagentInUse[i] = other.reagentInUse[i];
}
nextReagentPos = other.nextReagentPos;
CurrentTargetQuantity = other.CurrentTargetQuantity;
MaxQuantity = other.MaxQuantity;
UsedQuantity = other.UsedQuantity;
CatalystCount = other.CatalystCount;
FullQuantityDepth = other.FullQuantityDepth;
FullQuantity = other.FullQuantity;
MinReagents = other.MinReagents;
MaxReagents = other.MaxReagents;
for (int i = 0; i < MaxReagents; ++i)
{
currentWeights[i] = other.currentWeights[i];
}
}
public RecipeSearchNode(List<Reagent> costSortedReagents, uint[] reagents)
{
this.costSortedReagents = new List<Reagent>(costSortedReagents);
this.reagents = new uint[costSortedReagents.Count];
INVALID_REAGENT = (uint)costSortedReagents.Count;
nextReagentPos = reagents.Length;
for (int i = this.reagents.Length-1; i >= this.reagents.Length; --i)
{
reagents[i] = INVALID_REAGENT;
}
for (int i = reagents.Length-1; i >= 0; --i)
{
this.reagents[i] = reagents[i];
if (reagents[i] == INVALID_REAGENT)
{
nextReagentPos = i;
}
}
reagentInUse = new bool[costSortedReagents.Count];
for (uint reagentIdx = 0; reagentIdx < costSortedReagents.Count; ++reagentIdx)
{
reagentInUse[reagentIdx] = false;
}
foreach (uint reagentIdx in this.reagents)
{
if (reagentIdx != INVALID_REAGENT)
{
reagentInUse[reagentIdx] = true;
}
}
MinReagents = (uint)nextReagentPos;
MaxReagents = (uint)nextReagentPos; // better set this later!
UsedQuantity = 0;
}
// top-level search
public RecipeSearchNode(List<Reagent> costSortedReagents, uint startReagent)
{
this.costSortedReagents = new List<Reagent>(costSortedReagents);
this.reagents = new uint[costSortedReagents.Count];
INVALID_REAGENT = (uint)costSortedReagents.Count;
nextReagentPos = 0;
for (int i = 0; i < reagents.Length; ++i)
{
this.reagents[i] = INVALID_REAGENT;
}
reagentInUse = new bool[costSortedReagents.Count];
for (uint reagentIdx = 0; reagentIdx < costSortedReagents.Count; ++reagentIdx)
{
reagentInUse[reagentIdx] = false;
}
this.reagents[nextReagentPos++] = NextFreeReagent(startReagent);
//Console.WriteLine("Added reagent {0} at pos {1}", this.reagents[nextReagentPos-1], nextReagentPos-1);
MinReagents = 1; // don't iterate up beyond the start reagent
MaxReagents = 1;
UsedQuantity = 0;
}
public RecipeSearchNode(List<Reagent> costSortedReagents)
{
this.costSortedReagents = costSortedReagents;
this.reagents = new uint[costSortedReagents.Count];
INVALID_REAGENT = (uint)costSortedReagents.Count;
nextReagentPos = 0;
for (int i = 0; i < reagents.Length; ++i)
{
this.reagents[i] = INVALID_REAGENT;
}
reagentInUse = new bool[costSortedReagents.Count];
for (uint reagentIdx = 0; reagentIdx < costSortedReagents.Count; ++reagentIdx)
{
reagentInUse[reagentIdx] = false;
}
this.reagents[nextReagentPos++] = NextFreeReagent(0);
MinReagents = 0;
MaxReagents = 1;
UsedQuantity = 0;
}
public Reagent Reagent(int idx)
{
return costSortedReagents[(int)reagents[idx]];
}
public uint LastReagent
{
get
{
return reagents[nextReagentPos - 1];
}
}
public void RemoveLastReagent()
{
uint reagentIdx = reagents[nextReagentPos-1];
ReleaseReagent(reagentIdx);
if (costSortedReagents[(int)reagentIdx].IsCatalyst)
{
--CatalystCount;
}
reagents[nextReagentPos-1] = INVALID_REAGENT;
--nextReagentPos;
}
public void ReplaceLastReagent(uint reagentIdx)
{
uint oldReagentIdx = reagents[nextReagentPos-1];
ReleaseReagent(oldReagentIdx);
reagents[nextReagentPos-1] = reagentIdx;
if (costSortedReagents[(int)oldReagentIdx].IsCatalyst)
{
--CatalystCount;
}
if (costSortedReagents[(int)reagentIdx].IsCatalyst)
{
++CatalystCount;
}
}
public uint NextFreeReagent(uint startIdx)
{
uint idx = startIdx;
for (; idx < costSortedReagents.Count; ++idx)
{
bool inUse = reagentInUse[idx];
if ((inUse == false) && (costSortedReagents[(int)idx].Enabled))
{
//Console.WriteLine("Found free reagent idx {0}", idx);
reagentInUse[idx] = true;
return idx;
}
}
//Console.WriteLine("Failed to find free reagent.");
return (uint)costSortedReagents.Count;
}
private void ReleaseReagent(uint reagentIdx)
{
reagentInUse[reagentIdx] = false;
}
public bool AddNextReagent()
{
bool ok = (nextReagentPos < MaxReagents);
if (ok)
{
uint nextReagent = NextFreeReagent(0);
reagents[nextReagentPos++] = nextReagent;
if (costSortedReagents[(int)nextReagent].IsCatalyst)
{
++CatalystCount;
}
InitForQuantity(CurrentTargetQuantity);
}
return ok;
}
public void InitForQuantity(uint quantity)
{
//System.Console.WriteLine("Init for quantity: {0}, reagent count: {1} ({2} catalysts)", quantity, ReagentCount, CatalystCount);
CurrentTargetQuantity = quantity;
if (CurrentTargetQuantity < (10 + CatalystCount))
{
// invalid quantity
return;
}
UsedQuantity = 0;
uint remainingReagents = ((uint)nextReagentPos - CatalystCount);
uint remainingWeight = CurrentTargetQuantity - CatalystCount;
for (int i = 0; i < nextReagentPos; ++i)
{
Reagent reagent = Reagent(i);
if (reagent.IsCatalyst)
{
//Console.WriteLine("Init catalyst {0} weight 1", reagent.Name);
currentWeights[i] = 1;
++UsedQuantity;
}
else
{
uint reagentMaxWeight = reagent.RecipeMax;
if (ReagentCount <= FullQuantityDepth)
{
reagentMaxWeight = Math.Max(FullQuantity, reagentMaxWeight);
}
uint weight = (uint)Math.Min(remainingWeight - (remainingReagents-1), reagentMaxWeight);
//Console.WriteLine("Init reagent {0} weight {1}", reagent.Name, weight);
remainingWeight -= weight;
currentWeights[i] = weight;
UsedQuantity += weight;
}
--remainingReagents;
}
}
public void SetWeight(int idx, uint quantity)
{
UsedQuantity -= currentWeights[idx];
currentWeights[idx] = quantity;
UsedQuantity += quantity;
}
public void SaveState(StreamWriter writer)
{
writer.WriteLine("---SearchNode---");
writer.WriteLine("MinReagents: {0}", MinReagents);
writer.WriteLine("MaxReagents: {0}", MaxReagents);
writer.WriteLine("Reagents: {0}", nextReagentPos);
for (int i = 0; i < nextReagentPos; ++i)
{
uint idx = reagents[i];
uint weight = currentWeights[i];
writer.WriteLine("Reagent: {0},{1},{2}", idx, reagentInUse[idx] ? 1 : 0, weight);
}
// pulled from parent: List<Reagent> costSortedReagents;
// new on construct: PaintRecipe testRecipe = null;
writer.WriteLine("CurrentTargetQuantity: {0}", CurrentTargetQuantity);
writer.WriteLine("MaxQuantity: {0}", MaxQuantity);
writer.WriteLine("UsedQuantity: {0}", UsedQuantity);
writer.WriteLine("CatalystCount: {0}", CatalystCount);
writer.WriteLine("FullQuantity: {0}", FullQuantity);
writer.WriteLine("FullQuantityDepth: {0}", FullQuantityDepth);
writer.WriteLine("---EndNode---");
}
static Regex keyValueRegex = new Regex(@"(\w+)\:\s*(.*)\s*$");
static Regex reagentPartsRegex = new Regex(@"(?<id>\d+),(?<inUse>\d+),(?<weight>\d+)");
public bool LoadState(StreamReader reader)
{
string line = reader.ReadLine();
if (!line.Equals("---SearchNode---"))
{
return false;
}
bool success = true;
Match match;
while ((line = reader.ReadLine()) != null)
{
if (line.Equals("---EndNode---"))
{
break;
}
match = keyValueRegex.Match(line);
if (match.Success)
{
switch (match.Groups[1].Value)
{
case "Reagents":
{
//int reagentCount = int.Parse(match.Groups[2].Value);
for (int i = 0; i < reagents.Length; ++i)
{
reagents[i] = INVALID_REAGENT;
reagentInUse[i] = false;
}
nextReagentPos = 0;
}
break;
case "Reagent":
{
Match reagentInfo = reagentPartsRegex.Match(match.Groups[2].Value);
if (reagentInfo.Success)
{
uint reagentId = uint.Parse(reagentInfo.Groups["id"].Value);
int isInUse = int.Parse(reagentInfo.Groups["inUse"].Value);
uint weight = uint.Parse(reagentInfo.Groups["weight"].Value);
reagents[nextReagentPos] = reagentId;
currentWeights[nextReagentPos] = weight;
if (isInUse != 0)
{
if (reagentId != INVALID_REAGENT)
{
reagentInUse[reagentId] = true;
}
}
++nextReagentPos;
}
else
{
success = false;
}
}
break;
case "CurrentTargetQuantity":
{
uint value = uint.Parse(match.Groups[2].Value);
CurrentTargetQuantity = value;
}
break;
case "MaxQuantity":
{
uint value = uint.Parse(match.Groups[2].Value);
MaxQuantity = value;
}
break;
case "MinReagents":
{
uint value = uint.Parse(match.Groups[2].Value);
MinReagents = value;
}
break;
case "MaxReagents":
{
uint value = uint.Parse(match.Groups[2].Value);
MaxReagents = value;
}
break;
case "UsedQuantity":
{
uint value = uint.Parse(match.Groups[2].Value);
UsedQuantity = value;
}
break;
case "CatalystCount":
{
uint value = uint.Parse(match.Groups[2].Value);
CatalystCount = value;
}
break;
case "InitialCount":
{
uint value = uint.Parse(match.Groups[2].Value);
MinReagents = value;
}
break;
case "FullQuantity":
{
uint value = uint.Parse(match.Groups[2].Value);
FullQuantity = value;
}
break;
case "FullQuantityDepth":
{
uint value = uint.Parse(match.Groups[2].Value);
FullQuantityDepth = value;
}
break;
default:
success = false;
break;
}
}
else
{
success = false;
break;
}
}
return success;
}
}
}
|