Files @ 95e1ae426eb3
Branch filter:

Location: ATITD-Tools/Desert-Paint-Codex/Models/RecipeSearchNode.cs - annotation

Jason Maltzen
Add an info box when a detected clipped reaction could be extrapolated from a white shift.
  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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
57e33102e42b
57e33102e42b
57e33102e42b
57e33102e42b
57e33102e42b
57e33102e42b
57e33102e42b
57e33102e42b
57e33102e42b
57e33102e42b
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
57e33102e42b
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
d63ade4d8489
d63ade4d8489
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
d63ade4d8489
d63ade4d8489
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
d63ade4d8489
d63ade4d8489
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
d63ade4d8489
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
d63ade4d8489
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
d63ade4d8489
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
d63ade4d8489
40eaee10ae56
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
d63ade4d8489
40eaee10ae56
40eaee10ae56
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace DesertPaintCodex.Models
{
    public class RecipeSearchNode
    {
        private readonly uint[] _reagents;
        private readonly uint _invalidReagent;

        private int _nextReagentPos;
        public int ReagentCount => _nextReagentPos;

        private uint _minConcentration = 10;
        public uint MinConcentration
        {
            get => _minConcentration;
            set
            {
                if (value < 10) value = 10;
                _minConcentration = value;
            }
        }

        private readonly bool[] _reagentInUse;
        private readonly List<Reagent> _costSortedReagents;
        public PaintRecipe? TestRecipe { get; set; }

        public uint CurrentTargetQuantity { get; set; }
        public uint MaxConcentration { 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; }

        public bool ShouldLog { get; private set; }

        public Action<string>? WriteLog = null;

        private uint _maxReagents;
        public uint MaxReagents
        {
            get => _maxReagents;
            set
            {
                if (value < 1) value = 1;
                _maxReagents = value;
                CurrentWeights = new uint[_maxReagents];
            }
        }

        public uint[] CurrentWeights { get; private set; }
        
        public uint LastReagent => _reagents[_nextReagentPos - 1];

        public RecipeSearchNode(RecipeSearchNode other)
        {
            _costSortedReagents = new List<Reagent>(other._costSortedReagents);
            _reagents = new uint[_costSortedReagents.Count];
            _invalidReagent = (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.ReagentCount;

            CurrentTargetQuantity = other.CurrentTargetQuantity;
            MaxConcentration = other.MaxConcentration;
            UsedQuantity = other.UsedQuantity;
            CatalystCount = other.CatalystCount;
            FullQuantityDepth = other.FullQuantityDepth;
            FullQuantity = other.FullQuantity;
            MinReagents = other.MinReagents;
            MaxReagents = other.MaxReagents;
            CurrentWeights = new uint[MaxReagents];
            for (int i = 0; i < MaxReagents; ++i)
            {
                CurrentWeights[i] = other.CurrentWeights[i];
            }

            RefreshShouldLog();
        }

        public RecipeSearchNode(List<Reagent> costSortedReagents, uint[] reagents)
        {
            _costSortedReagents = new List<Reagent>(costSortedReagents);
            _reagents = new uint[costSortedReagents.Count];
            _invalidReagent = (uint)costSortedReagents.Count;
            _nextReagentPos = reagents.Length;
            for (int i = _reagents.Length - 1; i >= _reagents.Length; --i)
            {
                reagents[i] = _invalidReagent;
            }
            for (int i = reagents.Length - 1; i >= 0; --i)
            {
                _reagents[i] = reagents[i];
                if (reagents[i] == _invalidReagent)
                {
                    _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 != _invalidReagent)
                {
                    _reagentInUse[reagentIdx] = true;
                }
            }

            MinReagents = (uint) _nextReagentPos;
            MaxReagents = (uint) _nextReagentPos;
            CurrentWeights = new uint[MaxReagents];
            UsedQuantity = 0;

            RefreshShouldLog();
        }

        // top-level search
        public RecipeSearchNode(List<Reagent> costSortedReagents, uint startReagent)
        {
            _costSortedReagents = new List<Reagent>(costSortedReagents);
            _reagents = new uint[costSortedReagents.Count];
            _invalidReagent = (uint)costSortedReagents.Count;
            _nextReagentPos = 0;
            for (int i = 0; i < _reagents.Length; ++i)
            {
                _reagents[i] = _invalidReagent;
            }
            _reagentInUse = new bool[costSortedReagents.Count];
            for (uint reagentIdx = 0; reagentIdx < costSortedReagents.Count; ++reagentIdx)
            {
                _reagentInUse[reagentIdx] = false;
            }
            _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;
            CurrentWeights = new uint[MaxReagents];
            UsedQuantity = 0;

            RefreshShouldLog();
        }

        public RecipeSearchNode(List<Reagent> costSortedReagents)
        {
            _costSortedReagents = costSortedReagents;
            _reagents = new uint[costSortedReagents.Count];
            _invalidReagent = (uint)costSortedReagents.Count;
            _nextReagentPos = 0;
            for (int i = 0; i < _reagents.Length; ++i)
            {
                _reagents[i] = _invalidReagent;
            }
            _reagentInUse = new bool[costSortedReagents.Count];
            for (uint reagentIdx = 0; reagentIdx < costSortedReagents.Count; ++reagentIdx)
            {
                _reagentInUse[reagentIdx] = false;
            }
            _reagents[_nextReagentPos++] = NextFreeReagent(0);
            MinReagents = 0;
            MaxReagents = 1;
            CurrentWeights = new uint[MaxReagents];
            UsedQuantity = 0;

            RefreshShouldLog();
        }

        private void RefreshShouldLog()
        {
            ShouldLog = false;
            // (ReagentCount == 5) && (GetReagent(0).Name == "Iron") && (GetReagent(1).Name == "Red Sand") && (GetReagent(2).Name == "Sulfur") && (GetReagent(3).Name == "Carrot") && (GetReagent(4).Name == "Copper");
        }

        public Reagent GetReagent(int idx)
        {
            return _costSortedReagents[(int)_reagents[idx]];
        }

        public void RemoveLastReagent()
        {
            uint reagentIdx = _reagents[_nextReagentPos - 1];
            ReleaseReagent(reagentIdx);
            if (_costSortedReagents[(int)reagentIdx].IsCatalyst)
            {
                --CatalystCount;
            }
            _reagents[_nextReagentPos - 1] = _invalidReagent;
            --_nextReagentPos;
            RefreshShouldLog();
        }

        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;
            }
            RefreshShouldLog();
        }

        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()
        {
            if (ReagentCount >= MaxReagents) return false;

            uint nextReagent = NextFreeReagent(0);
            _reagents[_nextReagentPos++] = nextReagent;
            if (_costSortedReagents[(int)nextReagent].IsCatalyst)
            {
                ++CatalystCount;
            }
            InitForQuantity(CurrentTargetQuantity);

            return true;
        }

        public void InitForQuantity(uint quantity)
        {
            //System.Console.WriteLine("Init for quantity: {0}, reagent count: {1} ({2} catalysts)", quantity, ReagentCount, CatalystCount);
            CurrentTargetQuantity = quantity;
            if (CurrentTargetQuantity < (MinConcentration + CatalystCount))
            {
                // invalid quantity
                return;
            }
            UsedQuantity = 0;
            if (ShouldLog)
            {
                WriteLog?.Invoke($" == initializing {this} @ quantity {CurrentTargetQuantity} with {CatalystCount} catalysts ==");
            }
            uint remainingReagents = ((uint)_nextReagentPos); // Remaining reagents, including catalysts
            uint remainingWeight = CurrentTargetQuantity - CatalystCount;
            for (int i = 0; i < _nextReagentPos; ++i)
            {
                Reagent reagent = GetReagent(i);

                if (reagent.IsCatalyst)
                {
                    //Console.WriteLine("Init catalyst {0} weight 1", reagent.Name);
                    CurrentWeights[i] = 1;
                    ++UsedQuantity;
                    // This takes quantity but not weight (concentration)
                    if (ShouldLog)
                    {
                        WriteLog?.Invoke($"    + 1 {reagent.Name} (catalyst)");
                    }
                }
                else
                {
                    uint reagentMaxWeight = reagent.RecipeMax;
                    if (ReagentCount <= FullQuantityDepth)
                    {
                        reagentMaxWeight = Math.Max(FullQuantity, reagentMaxWeight);
                    }
                    uint weight = Math.Min(remainingWeight - (remainingReagents-1), reagentMaxWeight);
                    //Console.WriteLine("Init reagent {0} weight {1}", reagent.Name, weight);
                    if (ShouldLog)
                    {
                        WriteLog?.Invoke($"    + {weight} {reagent.Name} (remain: weight={remainingWeight} reagents={remainingReagents})");
                    }
                    remainingWeight -= weight;
                    CurrentWeights[i] = weight;
                    UsedQuantity += weight;
                }
                --remainingReagents;
            }
            RefreshShouldLog();
        }

        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("MinConcentration: {0}", MinConcentration);
            writer.WriteLine("MaxConcentration: {0}", MaxConcentration);
            writer.WriteLine("UsedQuantity: {0}", UsedQuantity);
            writer.WriteLine("CatalystCount: {0}", CatalystCount);
            writer.WriteLine("FullQuantity: {0}", FullQuantity);
            writer.WriteLine("FullQuantityDepth: {0}", FullQuantityDepth);
            writer.WriteLine("---EndNode---");
            
        }

        static readonly Regex keyValueRegex = new Regex(@"(\w+)\:\s*(.*)\s*$");
        static readonly Regex reagentPartsRegex = new Regex(@"(?<id>\d+),(?<inUse>\d+),(?<weight>\d+)");
        public bool LoadState(StreamReader reader)
        {
            string? line = reader.ReadLine();
            
            if ((line == null) || !line.Equals("---SearchNode---"))
            {
                return false;
            }

            bool success = true;
            while ((line = reader.ReadLine()) != null)
            {
                if (line.Equals("---EndNode---"))
                {
                    break;
                }
                Match 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] = _invalidReagent;
                                    _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 != _invalidReagent)
                                        {
                                            _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);
                                MaxConcentration = value;
                            }
                            break;
                        case "MinConcentration":
                            {
                                uint value = uint.Parse(match.Groups[2].Value);
                                MinConcentration = value;
                            }
                            break;
                        case "MaxConcentration":
                            {
                                uint value = uint.Parse(match.Groups[2].Value);
                                MaxConcentration = 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;
                }
            }
            RefreshShouldLog();
            return success;
        }

        public override string ToString()
        {
            if (_nextReagentPos == 0)
            {
                return "No ingredients";
            }
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(_costSortedReagents[(int)_reagents[0]].Name);
            for (int idx = 1; idx < _nextReagentPos; ++idx)
            {
                Reagent reagent = _costSortedReagents[(int)_reagents[idx]];
                sb.Append($", {reagent.Name}");
            }
            return sb.ToString();
        }
    }
}