Files @ 57e33102e42b
Branch filter:

Location: ATITD-Tools/Desert-Paint-Codex/Services/RecipeGenerator.cs

Jason Maltzen
During recipe generation, don't allow min concentration to go be set below 10 or max reagents below 1
  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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using DesertPaintCodex.Models;

namespace DesertPaintCodex.Services
{
    public class NewRecipeEventArgs : EventArgs
    {
        public NewRecipeEventArgs(string color, PaintRecipe recipe)
        {
            Color  = color;
            Recipe = recipe;
        }

        public string Color { get; }

        public PaintRecipe Recipe { get; }
    }
    
    public class RecipeGenerator
    {
        public enum SearchType {
            DepthFirst,
            BreadthFirst
        };

        public SearchType Mode { get; set; }

        public uint MinConcentration { get; private set; } // minimum paint concentration
        public uint MaxConcentration { get; private set; } // maximum paint concentration
        public uint MaxReagents { get; private set; } // maximum number of reagents to use in the recipe
        public uint MinReagents { get; private set; } // minimum number of reagents to use in the recipe
        public uint FullQuantityDepth { get; private set; } // at or equal this number of reagents, ignore ingredient settings for max quantity
        public uint FullQuantity { get; private set; }  // The max number of a reagent to use at full quantity

        public uint MaxThreads { get; set; } = 15;
        
        private bool _running = false;

        private int _runningThreads = 0;
        
        private readonly Dictionary<string, uint> _recipeCosts = new ();
        private readonly Dictionary<string, PaintRecipe> _recipes = new();

        private uint _totalReagents;

        private readonly List<Reagent> _costSortedReagents = new();

        private readonly ConcurrentQueue<RecipeSearchNode> _searchQueue = new();

        private ulong _recipeCount = 0;

        private readonly List<Thread> _generatorThreads = new();
        private readonly object _workerLock = new();

        private bool _requestCancel = false;

        private StreamWriter? _log;

        // events
        public event EventHandler? Finished;
        public event EventHandler? Progress;
        public event EventHandler<NewRecipeEventArgs>? NewRecipe;

        public RecipeGenerator()
        {
            Mode = SearchType.BreadthFirst;
            foreach (PaintColor color in PaletteService.Colors)
            {
                _recipes.Add(color.Name, new PaintRecipe());
                _recipeCosts.Add(color.Name, uint.MaxValue);
            }
            MinReagents = 1;
            MaxReagents = 5;
            MinConcentration = 10;
            MaxConcentration = 20;
        }

        public Dictionary<string, PaintRecipe> Recipes => _recipes;

        public ulong RecipeCount => _recipeCount;

        public bool CanResume => (!_running && (_searchQueue.Count > 0));

        public string? Log
        {
            set => _log = value != null ? new StreamWriter(value) : null;
        }

        public void FlushLog()
        {
            _log?.Flush();
        }

        public void CloseLog()
        {
            _log?.Close();
            _log = null;
        }

        private void WriteLog(string msg)
        {
            if (_log == null) return;
            lock (_workerLock)
            {
                _log.WriteLine(msg);
            }
        }

        private void WriteLog(string msg, params object[] args)
        {
            if (_log == null) return;
            lock(_workerLock)
            {
                _log.WriteLine(msg, args);
            }
        }

        private class ReagentCostSort : IComparer<Reagent>
        {
            public int Compare(Reagent? reagent1, Reagent? reagent2)
            {
                if (reagent1 == null)
                {
                    if (reagent2 == null) return 0;
                    return -1;
                }
                if (reagent2 == null) return 1;
                
                return (int)reagent1.Cost - (int)reagent2.Cost;
            }
        }

        public void InitRecipes(Dictionary<string, PaintRecipe> initialRecipes)
        {
            if (_running)
            {
                return;
            }
            _recipeCosts.Clear();
            _recipes.Clear();
            foreach (PaintRecipe recipe in initialRecipes.Values)
            {
                //PaintRecipe recipeCopy = new PaintRecipe(recipe);
                AddCheapestRecipe(recipe);
            }
        }

        private void InitSortedReagents()
        {
            _costSortedReagents.Clear();
            foreach (string name in ReagentService.Names)
            {
                Reagent reagent = ReagentService.GetReagent(name);
                _costSortedReagents.Add(reagent);
            }
            _costSortedReagents.Sort(new ReagentCostSort());
        }

        // Generate paint recipes.
        // minConcentration - the minimum permitted concentration in a recipe (10 for paint, 50 for ribbons)
        // maxConcentration - the maximum concentration for a recipe
        // minReagents - the minimum number of ingredients in a recipe
        // maxReagents - the maximum number of ingredients allowed in a recipe
        // fullQuantityDepth - at this depth of ingredient or below, allow up to fullQuantity of any reagent
        // fullQuantity - the maximum amount of any reagent to permit up to the full quantity depth. After that, reagents are limited by the
        //                per-reagent value
        public void BeginRecipeGeneration(uint minConcentration, uint maxConcentration, uint minReagents, uint maxReagents, uint fullQuantityDepth, uint fullQuantity)
        {
            if (_running)
            {
                // Already running - don't start again
                return;
            }

            MinConcentration = minConcentration;
            MaxConcentration = maxConcentration;
            MinReagents = minReagents;
            MaxReagents = maxReagents;
            FullQuantity = fullQuantity;
            FullQuantityDepth = fullQuantityDepth;

            // first, sort reagents by cost.
            InitSortedReagents();

            _totalReagents = (uint)_costSortedReagents.Count;

            // Pre-populate recipes list with:
            // 1) 1-ingredient recipes @ min concentration for all enabled ingredients with a count >= min concentration
            // 2) any previously-generated recipes
            WriteLog($"===================== BEGIN RECIPE GENERATION ========================== {DateTime.Now}");
            WriteLog("Pre-populating basic recipes.");
            int enabledReagentCount = 0;
            PaintRecipe recipe = new();
            foreach (var reagent in _costSortedReagents.Where(reagent => reagent.Enabled))
            {
                if (!reagent.IsCatalyst && ((reagent.RecipeMax >= minConcentration) || ((FullQuantityDepth > 0) && (FullQuantity >= minConcentration))))
                {
                    recipe.Clear();
                    recipe.AddReagent(reagent.Name, minConcentration);
                    AddCheapestRecipe(recipe);
                }
                ++enabledReagentCount;
            }
            MaxReagents = (uint)Math.Min(enabledReagentCount, MaxReagents);
            MinReagents = Math.Min(MinReagents, MaxReagents);
            
            _searchQueue.Clear();

            for (uint reagentIdx = 0; reagentIdx < _costSortedReagents.Count; ++reagentIdx)
            {
                if (!_costSortedReagents[(int) reagentIdx].Enabled) continue;
                
                // queue up all combinations of MinReagents
                RecipeSearchNode initialNode = new(_costSortedReagents, reagentIdx)
                {
                    FullQuantity      = FullQuantity,
                    FullQuantityDepth = FullQuantityDepth,
                    MinConcentration  = minConcentration,
                    MaxConcentration  = maxConcentration,
                    MinReagents       = minReagents,
                    MaxReagents       = maxReagents
                };
                initialNode.WriteLog = WriteLog;

                if (MinReagents > 1)
                {
                    while (NextReagentSetBreadthFirst(initialNode, 1, minReagents))
                    {
                        if (initialNode.ReagentCount != minReagents) continue;
                        
                        //Console.WriteLine("Initial node at size {0}/{1} with recipe: {2}", initialNode.ReagentCount, minReagents, initialNode.TestRecipe.ToString());
                        RecipeSearchNode searchNode = new RecipeSearchNode(initialNode);
                        searchNode.WriteLog = WriteLog;
                        _searchQueue.Enqueue(searchNode);
                    }
                }
                else
                {
                    _searchQueue.Enqueue(initialNode);
                }
            }

            _recipeCount = 0;

            WriteLog("Begin recipe generation: MaxConcentration={0} MinReagents={1} MaxReagents={2} FullQuantity={3} FullQuantityDepth={4}", MaxConcentration, MinReagents, MaxReagents, FullQuantity, FullQuantityDepth);

            // start worker threads to do the actual work
            ResumeRecipeGeneration();
        }

        public void ResumeRecipeGeneration()
        {
            if (_running)
            {
                // Already running - don't start again
                return;
            }
            _running = true;
            _requestCancel = false;

            WriteLog("Resuming recipe generation: pre-threads={0} reagent count={1} search queue={2}", _runningThreads, _costSortedReagents.Count, _searchQueue.Count);
            _runningThreads = 0; // presumably!

            int threadCount = Math.Min(Math.Min(_costSortedReagents.Count, _searchQueue.Count), (int)MaxThreads);
            if (threadCount == 0)
            {
                Finished?.Invoke(this, EventArgs.Empty);
            }
            _generatorThreads.Clear();
            Console.WriteLine("Starting {0} generator threads.", threadCount);
            WriteLog($"===== Starting {threadCount} threads.");
            for (int i = 0; i < threadCount; ++i)
            {
                Thread thr = new(Generate) {Priority = ThreadPriority.BelowNormal};
                _generatorThreads.Add(thr);
            }
            foreach (Thread thr in _generatorThreads)
            {
                thr.Start();
            }
        }

        public bool SaveState(string file)
        {
            if (_running)
            {
                // can't save state while running
                return false;
            }

            lock(_workerLock)
            {
                using StreamWriter writer = new(file, false);
                writer.WriteLine("MinReagents: {0}", MinReagents);
                writer.WriteLine("MaxReagents: {0}", MaxReagents);
                writer.WriteLine("FullQuantityDepth: {0}", FullQuantityDepth);
                writer.WriteLine("FullQuantity: {0}", FullQuantity);
                writer.WriteLine("TotalReagents: {0}", _totalReagents);
                writer.WriteLine("RecipeCount: {0}", _recipeCount);
                writer.WriteLine("SearchType: {0}", Mode.ToString());
                foreach (KeyValuePair<string, PaintRecipe> pair in _recipes)
                {
                    PaintRecipe recipe    = pair.Value;
                    string      colorName = PaletteService.FindNearest(recipe.ReactedColor);
                    writer.WriteLine("BeginRecipe: {0}", colorName);
                    foreach (PaintRecipe.ReagentQuantity reagent in recipe.Reagents)
                    {
                        writer.WriteLine("Ingredient: {0}={1}", reagent.Name, reagent.Quantity);
                    }
                    writer.WriteLine("EndRecipe: {0}", colorName);
                }
                writer.WriteLine("SearchNodes: {0}", _searchQueue.Count);
                foreach (RecipeSearchNode node in _searchQueue)
                {
                    node.SaveState(writer);
                }
            }
            return true;
        }

        private static readonly Regex _keyValueRegex = new Regex(@"(?<key>\w+)\:\s*(?<value>.+)\s*");
        private static readonly Regex _reagentRegex = new Regex(@"(?<ingredient>(\w+\s)*\w+)\s*=\s*(?<quantity>\d)\s*");
        
        public bool LoadState(string file)
        {
            // cannot be running, and reactions must be set
            if (_running)
            {
                return false;
            }

            InitSortedReagents();
            bool success = true;

            PaintRecipe? currentRecipe = null;
            using StreamReader reader = new(file, false);
            string? line;
            while (success && ((line = reader.ReadLine()) != null))
            {
                Match match = _keyValueRegex.Match(line);
                if (match.Success)
                {
                    string value = match.Groups["value"].Value;
                    switch(match.Groups["key"].Value)
                    {
                        case "MinReagents":
                            MinReagents = uint.Parse(value);
                            MaxReagents = Math.Max(MinReagents, MaxReagents);
                            break;
                        case "MaxReagents":
                            MaxReagents = uint.Parse(value);
                            MinReagents = Math.Min(MinReagents, MaxReagents);
                            break;
                        case "FullQuantityDepth":
                            FullQuantityDepth = uint.Parse(value);
                            break;
                        case "FullQuantity":
                            FullQuantity = uint.Parse(value);
                            break;
                        case "TotalReagents":
                            _totalReagents = uint.Parse(value);
                            break;
                        case "RecipeCount":
                            if (!ulong.TryParse(value, out _recipeCount))
                            {
                                // must have rolled to negative - try as an int and convert
                                long recipeCountInt = int.Parse(value);
                                _recipeCount = (ulong)(recipeCountInt & 0x00000000ffffffffL);
                            }
                            break;
                        case "BeginRecipe":
                            currentRecipe = new PaintRecipe();
                            break;
                        case "EndRecipe":
                            if (currentRecipe != null)
                            {
                                PaintColor color = currentRecipe.ReactedColor;
                                uint       cost  = currentRecipe.Cost;
                                _recipes[color.Name]     = currentRecipe; // replace
                                _recipeCosts[color.Name] = cost;
                                currentRecipe            = null;
                            }
                            break;
                        case "Ingredient":
                            if (currentRecipe != null)
                            {
                                Match ingredientMatch = _reagentRegex.Match(match.Groups["value"].Value);
                                if (ingredientMatch.Success)
                                {
                                    uint quantity = uint.Parse(ingredientMatch.Groups["quantity"].Value);
                                    currentRecipe.AddReagent(ingredientMatch.Groups["ingredient"].Value, quantity);
                                }
                                else
                                {
                                    success = false;
                                }
                            }
                            break;
                        case "SearchNodes":
                            int nodeCount = int.Parse(match.Groups["value"].Value);
                            for (int i = 0; i < nodeCount; ++i)
                            {
                                RecipeSearchNode node = new(_costSortedReagents)
                                {
                                    FullQuantity      = FullQuantity,
                                    FullQuantityDepth = FullQuantityDepth,
                                    MinReagents       = MinReagents,
                                    MaxReagents       = MaxReagents,
                                    MaxConcentration  = MaxConcentration
                                };
                                node.WriteLog = WriteLog;
                                success                = success && node.LoadState(reader);
                                if (success)
                                {
                                    _searchQueue.Enqueue(node);
                                }
                            }
                            break;
                        case "SearchType":
                            Mode = (SearchType)Enum.Parse(typeof(SearchType), match.Groups["value"].Value);
                            break;
                        default:
                            success = false;
                            break;
                    }
                }
                else
                {
                    success = false;
                    break;
                }
            }
            return success;
        }

        private void Generate()
        {
            lock(_workerLock)
            {
                ++_runningThreads;
            }

            bool ok;
            do
            {
                RecipeSearchNode? node;
                lock (_workerLock)
                {
                    ok = _searchQueue.TryDequeue(out node);
                }

                if (!ok) break;
                
                Debug.Assert(node != null);

                node.WriteLog = WriteLog;
                
                if (Mode == SearchType.DepthFirst)
                {
                    uint targetQuantity = (node.ReagentCount <= FullQuantityDepth)
                        ? ((uint)node.ReagentCount * FullQuantity) 
                        : node.MaxConcentration + 1;
                    do {
                        --targetQuantity;
                        if (node.ShouldLog)
                        {
                            WriteLog($" == Initializing {node} for quantity {targetQuantity}");
                        }
                        node.InitForQuantity(targetQuantity);
                    } while (targetQuantity > MinConcentration && (node.CurrentTargetQuantity != node.UsedQuantity));
    
                    while ((ok = IterateDepthFirst(node)) && !_requestCancel)
                    {
                        Progress?.Invoke(this, EventArgs.Empty);
                    }
                }
                else
                {
                    // breadth-first search
                    uint targetQuantity = MinConcentration - 1;
                    uint quantityLimit = (node.ReagentCount <= FullQuantityDepth) 
                        ? (FullQuantity * (uint)node.ReagentCount) 
                        : node.MaxConcentration;
                    do {
                        ++targetQuantity;
                        if (node.ShouldLog)
                        {
                            WriteLog($" == Initializing {node} for quantity {targetQuantity}");
                        }
                        node.InitForQuantity(targetQuantity);
                    } while ((targetQuantity < quantityLimit) && (node.CurrentTargetQuantity != node.UsedQuantity));
    
                    while ((ok = IterateBreadthFirst(node)) && !_requestCancel)
                    {
                        Progress?.Invoke(this, EventArgs.Empty);
                    }
                }
                if (ok)
                {
                    // stopped because cancel was requested - requeue the node in its current state for resume
                    _searchQueue.Enqueue(node);
                }
            } while (!_requestCancel);

            bool done;
            lock(_workerLock)
            {
                --_runningThreads;
                //generatorThreads.Remove(Thread.CurrentThread);

                done = (_runningThreads == 0);
            }

            if (!done) return;
            
            _running       = false;
            _requestCancel = false;
            Finished?.Invoke(this, EventArgs.Empty);
        }

        // Add the cheapest recipe to the recipe list
        // returns the discarded recipe from the pair (or null if no original recipe to replace)
        private void AddCheapestRecipe(PaintRecipe recipe)
        {
            if (!recipe.IsValidForConcentration(MinConcentration)) return;
            
            string colorName = PaletteService.FindNearest(recipe.ReactedColor);
            lock (_workerLock)
            {
                uint newCost = recipe.Cost;
                if (_recipeCosts.TryGetValue(colorName, out var cost))
                {
                    if (cost < newCost)
                    {
                        // WriteLog($"Skipping recipe (cost {newCost} > {cost}): {recipe}");
                        return;
                    }
                    PaintRecipe origRecipe = _recipes[colorName];
                    if (cost == newCost && recipe.Concentration >= origRecipe.Concentration)
                    {
                        // Same cost, greater concentration - use the lower concentration recipe
                        WriteLog($"Skipping recipe (cost {newCost}, {recipe.Concentration} >= {origRecipe.Concentration}): {recipe}");
                        return;
                    }
                        
                    _recipes[colorName].CopyFrom(recipe); // Copy recipe because the one passed in should be const and will get overwritten
                    _recipeCosts[colorName] = newCost;
                    WriteLog($"Replacing recipe (cost {newCost} < {cost}): {recipe}");
                        
                    if (NewRecipe == null) return;
                        
                    NewRecipeEventArgs args = new(colorName, _recipes[colorName]);
                    NewRecipe(this, args);
                }
                else
                {
                    // This would be an error!
                    _recipeCosts.Add(colorName, newCost);
                    PaintRecipe newRecipe = new PaintRecipe(recipe); // Copy recipe because the one passed in should be const and will get overwritten
                    _recipes.Add(colorName, newRecipe);

                    WriteLog($"New recipe (cost {newCost}): {recipe}");

                    if (NewRecipe == null) return;
                        
                    NewRecipeEventArgs args = new(colorName, newRecipe);
                    NewRecipe(this, args);
                }
            }
        }

        private bool IterateDepthFirst(RecipeSearchNode node)
        {
            TestCurrentRecipe(node);

            // pick recipe quantities at current recipe ingredients/size
            if (NextRecipe(node))
            {
                lock(_workerLock)
                {
                    ++_recipeCount;
                }
                //System.Console.WriteLine("Found next recipe at size {0} qty {1}", node.Reagents.Count, node.CurrentTargetQuantity);
                return true;
            }

            if (NextRecipeSize(node))
            {
                //System.Console.WriteLine("Found next recipe size {0}", node.CurrentTargetQuantity);
                return true;
            }

            // Search for next ingredient combo - all quantity combos for previous were searched
            //System.Console.WriteLine("Finding next ingredient combo");
            do
            {
                if (node.AddNextReagent()) continue;
                
                while ((node.ReagentCount > node.MinReagents) && (node.LastReagent == (_totalReagents-1)))
                {
                    node.RemoveLastReagent();
                }
                if (node.ReagentCount == node.MinReagents)
                {
                    // done
                    return false;
                }
                uint nextReagent = node.NextFreeReagent(node.LastReagent);
                while ((node.ReagentCount > node.MinReagents) && (nextReagent >= _totalReagents))
                {
                    // No more reagents to try at this level
                    node.RemoveLastReagent();
                    if (node.ReagentCount > node.MinReagents)
                    {
                        nextReagent = node.NextFreeReagent(node.LastReagent);
                    }
                }
                if (node.ReagentCount == node.MinReagents)
                {
                    // done
                    return false;
                }
                node.ReplaceLastReagent(nextReagent);
            } while (node.MaxConcentration < (node.MinConcentration + node.CatalystCount));
            node.InitForQuantity(node.MaxConcentration);

            //string outStr = "{0} : {1} : ";
            //for (int i = 0; i < currentReagents.Count; ++i)
            //{
            //    Reagent reagent = costSortedReagents[(int)currentReagents[i]];
            //    if (i > 0)
            //    {
            //        outStr += ", ";
            //    }
            //    outStr += reagent.Name + " (" + reagent.Cost + ")";
            //}
            //Console.WriteLine(outStr, currentReagents.Count, recipeCount);
            return true;
        }

        private bool IterateBreadthFirst(RecipeSearchNode node)
        {
            // pick recipe quantities at current recipe ingredients/size
            TestCurrentRecipe(node);
            lock(_workerLock)
            {
                ++_recipeCount;
            }

            // search all quantities of current recipe
            if (NextRecipe(node))
            {
                //System.Console.WriteLine("Found next recipe at size {0} qty {1}", node.ReagentCount, node.CurrentTargetQuantity);
                return true;
            }

            string origNodeVal = node.ToString();

            // Try next quantity
            uint newQuantity;
            uint quantityLimit = ((uint)node.ReagentCount <= FullQuantityDepth) ? ((uint)node.ReagentCount * FullQuantity) : node.MaxConcentration;
            do {
                newQuantity = node.CurrentTargetQuantity + 1;
                //Console.WriteLine("Try quantity {0}", newQuantity);
                if (newQuantity > quantityLimit) continue;

                if (node.ShouldLog)
                {
                    WriteLog($" == Initializing {node} for quantity {newQuantity}");
                }
                node.InitForQuantity(newQuantity);
                if (node.CurrentTargetQuantity <= node.UsedQuantity)
                {
                    //if (log != null) { lock(log) { log.WriteLine("Update quantity to {0}", node.CurrentTargetQuantity); } }
                    if (node.ShouldLog)
                    {
                        WriteLog($"  == {node} quantity {newQuantity}");
                    }
                    return true;
                }
            } while (newQuantity < quantityLimit);

            bool ok = NextReagentSetBreadthFirst(node, node.MinReagents, node.MaxReagents);
            if (node.ShouldLog)
            {
                if (ok)
                {
                    WriteLog($"==== {origNodeVal} next reagent set: {node} @ {node.CurrentTargetQuantity}");
                }
                else
                {
                    WriteLog($"==== done with reagent set: {origNodeVal}");
                }
            }
            return ok;
        }

        private bool NextReagentSetBreadthFirst(RecipeSearchNode node, uint minReagents, uint maxReagents)
        {
            // search all variants at this depth of recipe
            // increase recipe depth

            // next reagent in last position
            // if at end, pop reagent
            //Console.WriteLine("Finding new recipe after quantity {0}/{1} used {2}", newQuantity, node.MaxConcentration, node.UsedQuantity);
            if (node.ShouldLog)
            {
                WriteLog($" == Initializing {node} for quantity {node.MinConcentration + node.CatalystCount}");
            }
            node.InitForQuantity(node.MinConcentration + node.CatalystCount); // reset quantity
            int currentDepth = node.ReagentCount;
            bool recipeFound;
            do {
                //Console.WriteLine("Current depth: {0}/{1}", currentDepth, node.MaxReagents);
                do {
                    recipeFound = false;
                    
                    // back out until we find a node that can be incremented
                    if (currentDepth <= minReagents) continue;
                    
                    while (node.ReagentCount > minReagents)
                    {
                        if (node.LastReagent < (_totalReagents - 1))
                        {
                            var nextReagent = node.NextFreeReagent(node.LastReagent);
                            if (nextReagent < _totalReagents)
                            {
                                //Console.WriteLine("Replace last reagent with {0}", nextReagent);
                                node.ReplaceLastReagent(nextReagent);
                                break;
                            }
                            else
                            {
                                // shouldn't happen
                                //Console.WriteLine("No available reagents at depth {0}!", node.ReagentCount);
                                node.RemoveLastReagent();
                                if (node.ReagentCount == minReagents)
                                {
                                    // just popped the last reagent at the top level
                                    ++currentDepth;
                                    //if (log != null) { lock(log) { log.WriteLine("Increased depth to {0}/{1}", currentDepth, node.MaxReagents); } }
                                }
                            }
                        }
                        else
                        {
                            //Console.WriteLine("Pop last reagent");
                            node.RemoveLastReagent();
                            if (node.ReagentCount == minReagents)
                            {
                                // just popped the last reagent at the top level
                                ++currentDepth;
                                //if (log != null) { lock(log) { log.WriteLine("Increased depth to {0}/{1} [pop last reagent at top level]", currentDepth, node.MaxReagents); } }
                            }
                        }
                    }
                    // fill in the nodes up to the current depth
                    if (node.ReagentCount >= minReagents && currentDepth <= maxReagents)
                    {
                        recipeFound = true;
                        while (node.ReagentCount < currentDepth)
                        {
                            if (! node.AddNextReagent())
                            {
                                //if (log != null) { lock(log) { log.WriteLine("Failed to add reagent {0}/{1}", node.ReagentCount+1, currentDepth); } }
                                recipeFound = false;
                            }
                        }
                    }
                    //Console.WriteLine("Catalysts: {0} Reagents: {1} Min: {2}", node.CatalystCount, node.ReagentCount, node.MinReagents);
                } while ((node.CatalystCount >= node.ReagentCount) && (node.ReagentCount >= minReagents)); // make sure to skip all-catalyst combinations

                if (recipeFound)
                {
                    break;
                }
                else
                {
                    ++currentDepth;
                    //if (log != null) { lock(log) { log.WriteLine("Increased depth to {0}/{1} [no recipe]", currentDepth, node.MaxReagents); } }
                }
            } while (currentDepth <= maxReagents);

            if (!recipeFound)
            {
                if (node.ShouldLog)
                {
                    WriteLog($" == no more recipes for {node}");
                }
                return false;
            }
            
            node.InitForQuantity(node.MinConcentration+node.CatalystCount); // minimum quantity for this recipe
            
            node.TestRecipe ??= new PaintRecipe();
            node.TestRecipe.Clear();
            
            for (int i = 0; i < node.ReagentCount; ++i)
            {
                node.TestRecipe.AddReagent(node.GetReagent(i).Name, node.CurrentWeights[i]);
            }

            return recipeFound;
        }

        private void TestCurrentRecipe(RecipeSearchNode node)
        {
            node.TestRecipe ??= new PaintRecipe();
            node.TestRecipe.Clear();
            for (int i = 0; i < node.ReagentCount; ++i)
            {
                node.TestRecipe.AddReagent(node.GetReagent(i).Name, node.CurrentWeights[i]);
            }
            if (node.ShouldLog)
            {
                WriteLog($"   -> {node.TestRecipe}");
            }
            AddCheapestRecipe(node.TestRecipe);
            //if (log != null) { lock(log) { log.WriteLine("Tested recipe: {0}", node.TestRecipe); } }
        }

        private bool NextRecipe(RecipeSearchNode node)
        {
            // check for the next recipe
            uint remainingWeight = node.CurrentTargetQuantity - node.CatalystCount;
            if (remainingWeight < MinConcentration)
            {
                // not possible to make a valid recipe
                //Console.WriteLine("Insufficient remaining weight");
                if (node.ShouldLog)
                {
                    WriteLog($" ***** Not enough remaining weight in recipe {node} at target quantity {node.CurrentTargetQuantity} catalysts {node.CatalystCount} min concentration {MinConcentration}");
                }
                return false;
            }
            //uint remainingReagents = (uint)node.Reagents.Count - node.CatalystCount;

            uint depth = (uint)node.ReagentCount;
            uint weightToConsume = 0;
            uint spaceBelow = 0;
            int reagentsBelow = 0;
            // Start from the end of the current reagent list and work to the front
            for (int i = (int)depth-1 ; i >= 0; --i)
            {
                uint currentWeight = node.CurrentWeights[i];

                if ((spaceBelow >= (weightToConsume+1)) && (currentWeight > 1))
                {
                    // reduce this node by 1, allocate remaining weight to reagents below it
                    node.SetWeight(i, currentWeight-1);
                    weightToConsume += 1;
                    for (int j = i+1; j < depth; ++j)
                    {
                        --reagentsBelow;
                        Reagent reagent = node.GetReagent(j);
                        uint allocated = (uint)Math.Min(reagent.IsCatalyst ? 1 : (depth <= FullQuantityDepth ? FullQuantity : reagent.RecipeMax), weightToConsume - reagentsBelow);
                        if (allocated > 100)
                        {
                            Console.WriteLine("ACK: allocated = {0}", allocated);
                        }
                        node.SetWeight(j, allocated);
                        weightToConsume -= allocated;
                    }
                    break;
                }
                else
                {
                    Reagent reagent = node.GetReagent(i);
                    spaceBelow += (reagent.IsCatalyst ? 1 : (depth <= FullQuantityDepth ? FullQuantity : reagent.RecipeMax));
                    weightToConsume += currentWeight;
                    ++reagentsBelow;
                }
            }

            //int recipeWeight = 0;
            //foreach (int weight in node.CurrentWeights)
            //{
            //    recipeWeight += weight;
            //}
            //if ((weightToConsume != 0) || (recipeWeight != node.CurrentTargetQuantity))
            //{
            //    Console.WriteLine("Failed recipe with leftover weight {0} ({1}/{2}):", weightToConsume, recipeWeight, node.CurrentTargetQuantity);
            //    for (int i = 0; i < node.Reagents.Count; ++i)
            //    {
            //        Console.WriteLine("   > {0} {1}", node.Reagent(i).Name, node.CurrentWeights[i]);
            //    }
            //}
            
            return (weightToConsume == 0);
        }

        private static bool NextRecipeSize(RecipeSearchNode node)
        {
            uint newQuantity = node.CurrentTargetQuantity - 1;
            if (newQuantity < (node.MinConcentration + node.CatalystCount))
            {
                return false;
            }

            node.InitForQuantity(newQuantity);
            return node.CurrentTargetQuantity <= node.UsedQuantity;
        }

        public void Wait()
        {
            if (_generatorThreads.Count <= 0) return;
            
            foreach (Thread thr in _generatorThreads)
            {
                thr.Join();
            }
            _generatorThreads.Clear();
        }

        public void Stop()
        {
            _requestCancel = true;
        }

        public void ResetQueue()
        {
            lock (_workerLock)
            {
                // Don't reset the queue ever while running
                if (_running) return;
                _searchQueue.Clear();
            }
        }

        public void Reset()
        {
            foreach (PaintRecipe recipe in _recipes.Values)
            {
                recipe.Clear();
            }
            foreach (string key in _recipeCosts.Keys)
            {
                _recipeCosts[key] = uint.MaxValue;
            }
        }        
    }
}