forked from JavidPack/RecipeBrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecipePath.cs
More file actions
1156 lines (1006 loc) · 35.8 KB
/
RecipePath.cs
File metadata and controls
1156 lines (1006 loc) · 35.8 KB
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
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using RecipeBrowser.TagHandlers;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using Terraria;
using Terraria.GameContent.UI.Chat;
using Terraria.ID;
using Terraria.ModLoader;
namespace RecipeBrowser
{
internal class RecipePathTester
{
//internal static IdDictionary Search;
internal static bool print = false;
internal static bool printResults = false;
//internal static bool printResultCounts = true;
//internal static bool calculateAll = false;
//internal static int calculateAllRepeat = 1;
//internal static int inventoryChoice = 9; // doubles as test case.
//internal static int TestRecipeCraftItem = -1;
internal static bool thousandExtra = false;
}
internal static class RecipePath
{
internal static bool sourceInventory = true;
internal static bool sourceBanks = false;
internal static bool sourceChests = false;
internal static bool sourceMagicStorage = false;
internal static bool extendedCraft = false;
internal static bool allowLoots = false;
internal static Dictionary<int, List<int>> loots; // seenBefore, killedBefore --> Damaged before? Keep track separately for non-banner npc?
internal static bool allowMissingStations = false;
internal static bool allowPurchasable = false;
internal static Dictionary<int, List<int>> purchasable;
// internal static HashSet<int> harvestable;
// internal static bool enforceRecipeAvailable = false;
//internal static bool allowMissingItems = false; // Allow missing but Seen items....items of mysterious origin? Harvested and Fished for now.
//internal static int numberAllowedMissingItems = 1; // By count? no, ID? probably.
//internal static bool limitMissingItemsToSeenItemChecklist = false;
// internal static bool isCraftableOptimization = false;
//internal static bool enforceTiles = true; // Main.LocalPlayer.adjTile[tileID] and lava/honey/water?
//internal static bool shortCircitIngredients = true; // 40 vs 49. Is it correct though? --> 1000 extra: 281 vs 333
//internal static int loopLimit = 17; // 12 has no one reaching limit..
internal static void Refresh(bool complete = false) // refresh in-game when needed. Null out during unload.
{
purchasable = null;
if (complete)
{
loots = null; // encountered handled separately, no need to refresh in-game
recipeDictionary = null;
allowLoots = false;
allowMissingStations = false;
allowPurchasable = false;
}
}
//internal static bool useRecipeDictionary = true; // Much faster.
internal static Dictionary<int, List<Recipe>> recipeDictionary;
internal static void InitializeRecipeDictionary()
{
recipeDictionary = new Dictionary<int, List<Recipe>>();
for (int i = 0; i < Recipe.numRecipes; i++)
{
Recipe recipe = Main.recipe[i];
List<Recipe> recipeList;
if (!recipeDictionary.TryGetValue(recipe.createItem.type, out recipeList))
recipeDictionary.Add(recipe.createItem.type, recipeList = new List<Recipe>());
recipeList.Add(recipe);
}
// This removes all troublesome ingredients.
// Ingredients from alchemy mods that just convert to ingredients used in a ton of recipes.
var toRemove = new List<int>();
foreach (var recipeKVP in recipeDictionary) {
if (recipeKVP.Value.Count > 15)
toRemove.Add(recipeKVP.Key);
}
toRemove.ForEach((key) => recipeDictionary.Remove(key));
}
internal static void Adjust(this Dictionary<int, int> d, int key, int adjustment)
{
int currentCount;
d.TryGetValue(key, out currentCount);
d[key] = currentCount + adjustment;
if (d[key] <= 0)
d.Remove(key);
}
internal static void PrepareGetCraftPaths()
{
//RecipePathTester.Search = ItemID.Search;
if (recipeDictionary == null)
InitializeRecipeDictionary();
// Rather than simulating mining tile and tracking items, calculate placetile and styles of items and see if they exist in world?...nah.
//HashSet<int> a = new HashSet<int>();
//for (int i = 0; i < Main.maxTilesX; i++)
//{
// for (int j = 0; j < Main.maxTilesY; j++)
// {
// if(Main.tile[i, j] != null)
// {
// a.Add(Main.tile[i, j].type);
// }
// }
//}
// TODO: Travel Shop NPCLoader.SetupTravelShop()
// sometimes purchasable
if (purchasable == null && allowPurchasable)
{
// only current town NPC.
purchasable = new Dictionary<int, List<int>>();
FieldInfo shopToNPCField = typeof(NPCLoader).GetField("shopToNPC", BindingFlags.Static | BindingFlags.NonPublic);
int[] shopToNPC = (int[])shopToNPCField.GetValue(null);
for (int k = 0; k < 200; k++)
{
// Use the non-smartselect CanChat vanillaCanChat value
if (Main.npc[k].active && NPCLoader.CanChat(Main.npc[k], Main.npc[k].townNPC || Main.npc[k].type == 105 || Main.npc[k].type == 106 || Main.npc[k].type == 123 || Main.npc[k].type == 354 || Main.npc[k].type == 376 || Main.npc[k].type == 579 || Main.npc[k].type == 453))
{
int type = Main.npc[k].type;
int shop = Array.IndexOf(shopToNPC, type);
if (shop == -1)
shop = Main.MaxShopIDs - 1;
Main.instance.shop[shop].SetupShop(shop == Main.MaxShopIDs - 1 ? type : shop);
foreach (var item in Main.instance.shop[shop].item)
{
if (!item.IsAir)
{
List<int> shopList;
if (!purchasable.TryGetValue(item.type, out shopList))
purchasable.Add(item.type, shopList = new List<int>());
shopList.Add(type);
}
}
}
}
// TODO: price/special currency.
}
if (loots == null && allowLoots)
{
loots = new Dictionary<int, List<int>>();
foreach (var lootInfo in LootCache.instance.lootInfos)
{
int itemid = lootInfo.Key.GetID();
if (itemid > 0)
{
List<int> npcs = lootInfo.Value.Select(x => x.GetID()).Where(x => x > 0).ToList();
if (npcs.Count > 0)
loots[itemid] = npcs;
}
}
}
//loots.Add(ItemID.Gel);
//loots.Add(ItemID.CopperBar);
}
// TODO: GetCraftPaths but without a Recipe? Just an item? Buy/Loot
// Bestiary Option? All New Items you can craft if you farm this npc?
internal static List<CraftPath> GetCraftPaths(Recipe recipe, CancellationToken token, bool single)
{
//Main.NewText("GetCraftPaths");
//if(harvestable == null)
//{
// // prevent network spam?
// harvestable = new HashSet<int>();
// for (int i = 0; i < Main.maxTilesX; i++)
// {
// for (int j = 0; j < Main.maxTilesY; j++)
// {
// }
// }
//}
//if (RecipePath.isCraftableOptimization)
//{
// //if (ItemCatalogueUI.instance.craftResults == null)
// throw new Exception();
//}
// TODO: Track money? or just use inventory items. Bank money easily query-able.
// Special Currencies?
var haveItems = CalculateHaveItems();
List<CraftPath> paths = new List<CraftPath>();
CraftPath craftPath = new CraftPath(recipe, haveItems); // Push. Can't pop. // not calling ConsumeResources(recipeNode);
if (RecipePathTester.print)
craftPath.Print();
FindCraftPaths(paths, craftPath, token, single);
//watch.Stop();
//var elapsedMs = watch.ElapsedMilliseconds;
//if (elapsedMs > 1000)
//{
// StringBuilder sb = new StringBuilder();
// foreach (var item in recipe.requiredItem)
// {
// if (!item.IsAir)
// sb.Append(ItemTagHandler.GenerateTag(item));
// }
// sb.Append("-->");
// sb.Append(ItemTagHandler.GenerateTag(recipe.createItem));
// Main.NewText(elapsedMs + ": " + sb.ToString());
//}
if (!allowMissingStations)
{
for (int i = paths.Count - 1; i >= 0; i--)
{
// imkSushisMod caused thousands of paths to be culled here. Cull earlier or limit paths count. Investigate more.
if (paths[i].root.GetAllChildrenPreOrder().OfType<CraftPath.RecipeNode>().Any(x => x.recipe.requiredTile.Any(tile => tile > -1 && !RecipeBrowserPlayer.seenTiles[tile])))
paths.RemoveAt(i);
}
}
return paths;
}
private static Dictionary<int, int> CalculateHaveItems()
{
// TODO: cache, return clone instead.
Dictionary<int, int> haveItems = new Dictionary<int, int>(); // Have items can't have already used items in final recipe.
if (sourceInventory)
{
for (int i = 0; i < 59; i++)
{
Item item = Main.LocalPlayer.inventory[i];
if (i == 58)
item = Main.mouseItem;
//}
//foreach (var item in Main.LocalPlayer.inventory)
//{
if (!item.IsAir)
{
int currentCount;
haveItems.TryGetValue(item.type, out currentCount);
haveItems[item.type] = currentCount + item.stack;
}
}
}
if (RecipePathTester.thousandExtra)
for (int i = 1500; i < 2500; i++)
{
int currentCount;
haveItems.TryGetValue(i, out currentCount);
haveItems[i] = currentCount + 10;
}
return haveItems;
}
// Multithreading to prevent lag?
private static void FindCraftPaths(List<CraftPath> paths, CraftPath inProgress, CancellationToken token, bool single)
{
if (single && paths.Count > 0)
return;
//if(token.CanBeCanceled && watch.ElapsedMilliseconds > 1000)
//{
// Main.NewText("timed out" + watch.ElapsedMilliseconds);
// return;
//}
if (token.IsCancellationRequested) {
inProgress.Print();
return;
}
// Some notion of limiting depth of tree might help.
// TODO
// Limit by Total steps (TODO: steps only, not HAves)
int count = inProgress.root.GetAllChildrenPreOrder().Count(); //.OfType<CraftPath.RecipeNode>()
if (count > 20) {
return;
}
// Current will always be an unfulfilled
CraftPath.UnfulfilledNode current = inProgress.GetCurrent();
if (current == null)
{
paths.Add(inProgress.Clone()); // Clone probably.
return;
}
// Assume current is UnfulfulledItem class...could change later. Kill boss, other conditions maybe? think more about this.
var ViableIngredients = current.item;
var neededStack = current.stack;
// reduce VialbleIngredients to only items not seen above.
current.CheckParentsForRecipeLoopViaIngredients(ViableIngredients);
if (ViableIngredients.Count == 0)
{
//Console.WriteLine();
return;
}
// if(VialbleIngredients.length == 1) optimization?
//List<Recipe> recipes;
//if (!recipeDictionary.TryGetValue(needItem.Key, out recipes))
// recipes = new List<Recipe>();
// Find all Recipes that can fulfill current, which might be a RecipeGroup
var recipeOptions = recipeDictionary.Where(x => ViableIngredients.Contains(x.Key)).SelectMany(x => x.Value).ToList(); // inefficient maybe?
foreach (var recipe in recipeOptions)
{
// Better to do this before.
//if (current.CheckParentsForRecipeLoop(recipe))
// continue;
//if(inProgress.root.recipe.)
if (inProgress.root is CraftPath.RecipeNode)
{
var rootRecipe = (inProgress.root as CraftPath.RecipeNode).recipe;
if (recipe.requiredItem.Any(x => x.type == rootRecipe.createItem.type && x.stack >= rootRecipe.createItem.stack))
{
continue; // Prevents Wood->Platform->Wood loops. Other code checks similar loops but didn't catch these. TODO: Think about RecipeGroups.
}
}
// RecipeAvailable here.
// Allow missing tiles?
// If createItem/ingredients exists in Tree?
int craftMultiple = (neededStack - 1) / recipe.createItem.stack + 1;
// Push recipe consumes Items while populating Children with Unfulfilled or Fulfilled.
var recipeNode = inProgress.Push(current, recipe, craftMultiple);
if (RecipePathTester.print)
inProgress.Print();
int pathsBefore = paths.Count;
FindCraftPaths(paths, inProgress, token, single); // Handles everything from here recursively.
int pathsAfter = paths.Count;
// Pop restores Unfulfilled and restores consumed Items.
inProgress.Pop(current, recipeNode);
//if (pathsAfter > 5) {
// return;
//}
// 3 Iron Ore 6 Lead Ore problem...if paths same size, try with craftMultiple of 1 maybe?
if (pathsBefore == pathsAfter) // and craftMultiple < some number for performance.
{
// And 1 is possible?
}
}
if (allowPurchasable)
{
// TODO recipe groups.
var buyAble = ViableIngredients.Intersect(purchasable.Keys);
//if (ViableIngredients.Any(x => purchasable.Contains(x)))
if (buyAble.Count() > 0)
{
// TODO: If Can afford.
// TODO: Take into account incomplete already owned items?
// UnfulfilledNode should probably be nested under HaveItemNode, or merged together.
CraftPath.BuyItemNode buyItemNode = new CraftPath.BuyItemNode(buyAble.First(), neededStack, current.ChildNumber, current.parent, current.craftPath);
inProgress.Push(current, buyItemNode);
if (RecipePathTester.print)
inProgress.Print();
FindCraftPaths(paths, inProgress, token, single);
inProgress.Pop(current, buyItemNode);
}
}
if (allowLoots && loots != null) // multithread issue if allowLoots checked after.
{
//if (VialbleIngredients.Intersect(loots).Any())
var lootable = ViableIngredients.Intersect(loots.Keys); // TODO recipe groups. --> For loop??
if (lootable.Count() > 0)
{
bool encountered = false;
// Only checks 1 item in Group. Fix later.
var npcs = loots[lootable.First()];
foreach (var npc in npcs) {
int bannerID = Item.NPCtoBanner(npc);
if (bannerID > 0) {
if (NPC.killCount[bannerID] > 0) {
encountered = true;
break;
}
}
}
if (encountered)
{
CraftPath.LootItemNode lootItemNode = new CraftPath.LootItemNode(lootable.First(), neededStack, current.ChildNumber, current.parent, current.craftPath);
inProgress.Push(current, lootItemNode);
if (RecipePathTester.print)
inProgress.Print();
FindCraftPaths(paths, inProgress, token, single);
inProgress.Pop(current, lootItemNode);
}
}
}
// Other sources?
// Fishing?
// Mining/Harvesting?
// Extractinator
// Chests
// Statue
// Traveling Shop
// ItemChecklist? If not any of the others, I must have had it once before.
// returning will result in Popping.
}
// TODO: Crafting Station requirements, IsAvailable
//private static void FindCraftPaths(List<CraftPath> paths, CraftPath inProgress, Recipe current, int recipeMultiple, Dictionary<int, int> haveItems, Dictionary<int, int> needItems)
//{
// //Push main recipe
// // push R 1
// // ??
// // pop R1
// //pop main recipe
public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences)
{
IEnumerable<IEnumerable<T>> result = new[] { Enumerable.Empty<T>() };
foreach (var sequence in sequences)
{
var localSequence = sequence;
result = result.SelectMany(
_ => localSequence,
(seq, item) => seq.Concat(new[] { item })
);
}
return result;
}
internal static List<int> GetAcceptedGroups(Recipe recipe)
{
List<int> acceptedGroups = new List<int>(recipe.acceptedGroups);
if (recipe.anyWood)
{
acceptedGroups.Add(RecipeGroupID.Wood);
}
if (recipe.anyIronBar)
{
acceptedGroups.Add(RecipeGroupID.IronBar);
}
if (recipe.anySand)
{
acceptedGroups.Add(RecipeGroupID.Sand);
}
if (recipe.anyPressurePlate)
{
acceptedGroups.Add(RecipeGroupID.PressurePlate);
}
if (recipe.anyFragment)
{
acceptedGroups.Add(RecipeGroupID.Fragment);
}
return acceptedGroups;
}
}
//internal class CraftPathOld
//{
// internal List<Recipe> recipes = new List<Recipe>();
// internal List<int> craftMultiple = new List<int>();
// internal CraftPath GetClone()
// {
// var clone = new CraftPath();
// clone.recipes = new List<Recipe>(recipes);
// clone.craftMultiple = new List<int>(craftMultiple);
// return clone;
// }
// //internal bool HasLoop()
// //{
// //}
//}
// Allow missing
// -- Lootable Items from seen NPC
// -- Buyable Items from seen town NPC
// -- Crafting stations?
// -- Recipe Available blocked.
internal class CraftPath
{
internal class CraftPathNode
{
internal CraftPathNode parent;
internal int ChildNumber = -1;
internal CraftPathNode[] children;
internal CraftPath craftPath;
public CraftPathNode(int childNumber, CraftPathNode parent, CraftPath craftPath)
{
ChildNumber = childNumber;
this.parent = parent;
this.craftPath = craftPath;
if (parent != null)
parent.children[ChildNumber] = this;
}
bool Fulfilled => false;
internal UnfulfilledNode FindUnfulfilled()
{
if (this is UnfulfilledNode)
return (UnfulfilledNode)this;
if (children != null) // TODO: Initilize to 0? Leave as null?
foreach (var item in children)
{
UnfulfilledNode child = item.FindUnfulfilled();
if (child != null)
return child;
}
return null;
}
internal void Print(int indent)
{
if (!RecipePathTester.print && !RecipePathTester.printResults)
return;
RecipeBrowser.instance.Logger.Info(new String(' ', indent) + ToString());
if (children != null) // TODO: Initilize to 0? Leave as null?
foreach (var item in children)
{
if (item == null)
RecipeBrowser.instance.Logger.Info(new String(' ', indent + 4) + "null");
else
item.Print(indent + 4);
}
}
public virtual string ToUITextString()
{
return ToString();
}
public override string ToString()
{
return base.ToString();
}
internal virtual CraftPathNode Clone()
{
CraftPathNode clone = (CraftPathNode)this.MemberwiseClone();
clone.parent = null;
clone.craftPath = null;
clone.children = children?.Select(x => x?.Clone()).ToArray();
return clone;
}
internal virtual void ConsumeResources(CraftPath path)
{
// Do Consumption here
if (children != null)
foreach (var item in children)
{
item.ConsumeResources(path);
}
}
internal virtual void UnConsumeResources(CraftPath path)
{
// Do Consumption here
if (children != null)
foreach (var item in children)
{
item.UnConsumeResources(path);
}
}
public IEnumerable<CraftPathNode> GetAllChildrenPreOrder()
{
yield return this;
if (children != null)
foreach (var child in children)
{
foreach (var subchild in child.GetAllChildrenPreOrder())
{
yield return subchild;
}
}
}
//GetAllLeafPreOrder
// push and pop to consume haveItem efficiently?
}
// Terminal node
internal class HaveItemNode : CraftPathNode
{
internal int itemid;
internal int stack;
public HaveItemNode(int itemid, int stack, int ChildNumber, CraftPathNode parent, CraftPath craftPath) : base(ChildNumber, parent, craftPath)
{
this.itemid = itemid;
this.stack = stack;
}
internal override void ConsumeResources(CraftPath path)
{
ConsumeItems(path);
base.ConsumeResources(path);
}
internal override void UnConsumeResources(CraftPath path)
{
UnConsumeItems(path);
base.UnConsumeResources(path);
}
internal void ConsumeItems(CraftPath path)
{
path.haveItems.Adjust(itemid, -stack);
}
internal void UnConsumeItems(CraftPath path)
{
path.haveItems.Adjust(itemid, stack);
}
public override string ToString()
{
return $"Have: {Lang.GetItemNameValue(itemid)} ({stack})";
}
public override string ToUITextString()
{
return $"Have: {ItemHoverFixTagHandler.GenerateTag(itemid, stack, null, true)}";
}
}
internal class HaveItemsNode : CraftPathNode
{
internal RecipeGroup recipeGroup;
internal List<Tuple<int, int>> listOfItems;
public HaveItemsNode(RecipeGroup recipeGroup, List<Tuple<int, int>> listOfItems, int ChildNumber, CraftPathNode parent, CraftPath craftPath) : base(ChildNumber, parent, craftPath)
{
this.recipeGroup = recipeGroup;
this.listOfItems = listOfItems;
}
internal override void ConsumeResources(CraftPath path)
{
ConsumeItems(path);
base.ConsumeResources(path);
}
internal override void UnConsumeResources(CraftPath path)
{
UnConsumeItems(path);
base.UnConsumeResources(path);
}
internal void ConsumeItems(CraftPath path)
{
foreach (var item in listOfItems)
path.haveItems.Adjust(item.Item1, -item.Item2);
}
internal void UnConsumeItems(CraftPath path)
{
foreach (var item in listOfItems)
path.haveItems.Adjust(item.Item1, item.Item2);
}
public override string ToString()
{
int count = listOfItems.Sum(x => x.Item2);
return $"{ItemHoverFixTagHandler.GenerateTag(recipeGroup.ValidItems[recipeGroup.IconicItemIndex], count, recipeGroup.GetText(), false)} ({string.Concat(listOfItems.Select(x => ItemHoverFixTagHandler.GenerateTag(x.Item1, x.Item2, null, true)))})";
// return $"Haves: {string.Join(", ", listOfItems.Select(x => $"{Lang.GetItemNameValue(x.Item1)} ({x.Item2})"))}";
}
}
internal class BuyItemNode : CraftPathNode
{
// TODO: storeID
int itemid;
int stack;
public BuyItemNode(int itemid, int stack, int ChildNumber, CraftPathNode parent, CraftPath craftPath) : base(ChildNumber, parent, craftPath)
{
this.itemid = itemid;
this.stack = stack;
}
internal override void ConsumeResources(CraftPath path)
{
ConsumeMoney(path);
base.ConsumeResources(path);
}
internal override void UnConsumeResources(CraftPath path)
{
UnConsumeMoney(path);
base.UnConsumeResources(path);
}
internal void ConsumeMoney(CraftPath path)
{
// TODO. For now assume infinite money.
//path.haveItems.Adjust(itemid, -stack);
}
internal void UnConsumeMoney(CraftPath path)
{
//path.haveItems.Adjust(itemid, stack);
}
public override string ToString()
{
return $"Buy: {Lang.GetItemNameValue(itemid)} ({stack}) from ??";
}
public override string ToUITextString()
{
return $"[image:RecipeBrowser/Images/sortValue]: {ItemHoverFixTagHandler.GenerateTag(itemid, stack)} from {string.Concat(RecipePath.purchasable[itemid].Select(x => $"[npc/head:{x}]"))} for {ItemHoverFixTagHandler.GenerateTag(ItemID.SilverCoin, 3)}"; // TODO: TownNPC Head instead of
}
}
internal class LootItemNode : CraftPathNode
{
// TODO: storeID
int itemid;
int stack;
public LootItemNode(int itemid, int stack, int ChildNumber, CraftPathNode parent, CraftPath craftPath) : base(ChildNumber, parent, craftPath)
{
this.itemid = itemid;
this.stack = stack;
}
public override string ToString()
{
return $"Farm: {Lang.GetItemNameValue(itemid)} ({stack}) from {string.Join(", ", RecipePath.loots[itemid].Select(x => Lang.GetNPCNameValue(x)))}";
}
public override string ToUITextString()
{
var npcs = RecipePath.loots[itemid]; // Only checks 1 item in Group. Fix later.
List<int> encountered = new List<int>();
foreach (var npc in npcs)
{
int bannerID = Item.NPCtoBanner(npc);
if (bannerID > 0)
{
if (NPC.killCount[bannerID] > 0)
encountered.Add(npc);
}
}
return $"[image/s0.8,v2,tFarm:RecipeBrowser/Images/sortDamage] {ItemHoverFixTagHandler.GenerateTag(itemid, stack)} from {string.Concat(encountered.Select(x => $"[npc:{x}]"))}";
//[image/tMissing Tiles[i;{ItemID.MythrilAnvil}]:
//return $"[image/tFarm:RecipeBrowser/Images/sortDamage] {ItemHoverFixTagHandler.GenerateTag(itemid, stack)} from {string.Concat(RecipePath.loots[itemid].Select(x => $"[npc:{x}]"))}";
//return $"Farm: {ItemHoverFixTagHandler.GenerateTag(itemid, stack)} from {string.Concat(RecipePath.loots[itemid].Select(x => $"[npc:{x}]"))}";
}
}
internal class UnfulfilledNode : CraftPathNode
{
internal RecipeGroup recipeGroup;
internal List<int> item;
internal int stack;
public UnfulfilledNode(int item, int stack, int ChildNumber, CraftPathNode parent, CraftPath craftPath) : base(ChildNumber, parent, craftPath)
{
this.stack = stack;
this.item = new List<int>() { item };
}
public UnfulfilledNode(RecipeGroup recipeGroup, int stack, int ChildNumber, CraftPathNode parent, CraftPath craftPath) : base(ChildNumber, parent, craftPath)
{
this.recipeGroup = recipeGroup;
this.item = recipeGroup.ValidItems;
this.stack = stack;
// recipeGroup.ContainsItem probably faster than iterating over item?
}
public override string ToString()
{
return $"Need: { string.Join(", ", item.Select(x => $"{Lang.GetItemNameValue(x)} ({stack})"))}";
}
internal void CheckParentsForRecipeLoopViaIngredients(List<int> vialbleIngredients)
{
var p = parent;
while (p != null)
{
RecipeNode r = p as RecipeNode; // Technically this shouldn't ever fail...
if (r != null)
{
// TODO: 100Wood <- Bundle, Bundle <- Wood+Duplicator problem. Stack size matters? issue.
if (vialbleIngredients.Contains(r.recipe.createItem.type))
{
//Console.WriteLine();
}
if (r.recipe.createItem.type == 9 && (r.craftPath.root as RecipeNode).recipe.createItem.type == 9)
{
//Console.WriteLine(); // 2629 living wood plat
}
vialbleIngredients.Remove(r.recipe.createItem.type);
p = p.parent;
}
else if (p is HaveItemNode) //
{
//Console.WriteLine();
p = p.parent;
}
else if (p is HaveItemsNode)
{
//Console.WriteLine();
p = p.parent;
}
else
{
throw new Exception("How is a parent not a recipe?");
}
}
}
internal bool CheckParentsForRecipeLoop(Recipe recipe)
{
// Yeah, wait until later is a better idea.
var p = parent;
while (p != null)
{
RecipeNode r = p as RecipeNode; // Technically this shouldn't ever fail...
if (r != null)
{
if (r.recipe.createItem.type == recipe.createItem.type && r.recipe.createItem.stack != recipe.createItem.stack)
throw new Exception("Found a stack size problem craft path!");
// If any create item matches an above create item
if (r.recipe.createItem.type == recipe.createItem.type) // is there a Recipe Group problem here? below?
return true;
p = p.parent;
}
else if (p is HaveItemNode || p is HaveItemsNode) // is this OK?
{
p = p.parent;
}
else
{
throw new Exception("How is a parent not a recipe?");
}
}
return false;
// TODO: Ignore recipe group items
// I removed RecipeGroup items. Remember to check this later when actualizing it.
// I could just check Result->Result and bypass any recipe group problem
// 100Wood <- Bundle, Bundle <- Wood+Duplicator problem. Stack size matters?
//HashSet<int> items = new HashSet<int>(recipe.requiredItem.Where(x => !x.IsAir).Select(x => x.type));
//List<int> groups = RecipePath.GetAcceptedGroups(recipe);
//foreach (var groupid in groups)
//{
// items.Remove(RecipeGroup.recipeGroups[groupid].ValidItems[RecipeGroup.recipeGroups[groupid].IconicItemIndex]);
//}
//var p = parent;
//while (p != null)
//{
// RecipeNode r = p as RecipeNode;
// if (r != null)
// {
// // If any create item matches an above create item
// if (p.recipe.createItem.type == recipe.createItem.type) // is there a Recipe Group problem here? below?
// return true;
// // Vs
// if (RecipePath.shortCircitIngredients && recipe.requiredItem.Any(x => x.type == p.recipe.createItem.type)) // TODO: Don't short circuit if parent is creating wood but recipe requires any wood...?
// return true;
// // If any ingredient matches a parent create item
// // OLD
// //if (p.recipe == recipe)
// // return true;
// }
// p = p.parent;
//}
//return false;
}
}
// Change to CraftNode? KillNode -> Farm these NPC for this item
internal class RecipeNode : CraftPathNode
{
internal Recipe recipe;
internal int multiplier;
public RecipeNode(Recipe recipe, int multiplier, int ChildNumber, CraftPathNode parent, CraftPath craftPath) : base(ChildNumber, parent, craftPath)
{
this.recipe = recipe;
this.multiplier = multiplier;
children = new CraftPathNode[recipe.requiredItem.Count(x => !x.IsAir)];
List<int> groups = RecipePath.GetAcceptedGroups(recipe);
for (int i = 0; i < children.Length; i++) // For Each Ingredient.
{
bool itemIsRecipeGroupItem = false;
foreach (var groupid in groups)
{
// 6 wood, 4 shadewood works for 10 any wood.
// multiplier assumes all same Item in ItemGroup used for all Recipes.
if (recipe.requiredItem[i].type == RecipeGroup.recipeGroups[groupid].ValidItems[RecipeGroup.recipeGroups[groupid].IconicItemIndex])
{
bool foundValidItem = false;
bool foundPartialItem = false;
foreach (var validItemID in RecipeGroup.recipeGroups[groupid].ValidItems)
{
if (craftPath.haveItems.ContainsKey(validItemID) && craftPath.haveItems[validItemID] >= recipe.requiredItem[i].stack * multiplier)
{
// Any Wood on left, Wood on Right problem. Wood could be consumed before Wood node, when ShadeWood would be better option.
children[i] = new HaveItemNode(validItemID, recipe.requiredItem[i].stack * multiplier, i, this, craftPath);
foundValidItem = true;
break;
}
else if (craftPath.haveItems.ContainsKey(validItemID))
{
foundPartialItem = true;
}
}
if (!foundValidItem && foundPartialItem)
{
List<Tuple<int, int>> listOfItems = new List<Tuple<int, int>>();
int remaining = recipe.requiredItem[i].stack * multiplier;
foreach (var validItemID in RecipeGroup.recipeGroups[groupid].ValidItems)
{
if (remaining > 0 && craftPath.haveItems.ContainsKey(validItemID))
{
int taken = Math.Min(remaining, craftPath.haveItems[validItemID]);
listOfItems.Add(new Tuple<int, int>(validItemID, taken));
remaining -= taken;
}
}
children[i] = new HaveItemsNode(RecipeGroup.recipeGroups[groupid], listOfItems, i, this, craftPath);
if (remaining > 0)
{
children[i].children = new CraftPathNode[1];
children[i].children[0] = new UnfulfilledNode(RecipeGroup.recipeGroups[groupid], remaining, 0, children[i], craftPath);
}
}
else if (!foundValidItem)
children[i] = new UnfulfilledNode(RecipeGroup.recipeGroups[groupid], recipe.requiredItem[i].stack * multiplier, i, this, craftPath);
itemIsRecipeGroupItem = true;
break;
}
}
// Does it make more sense to nest these, or add more children slots? Hm, Children match up to recipe ingredient index.... Make a BranchNode?
if (!itemIsRecipeGroupItem)
{
// Recipe Groups can have stacks-size different inputs if needed. Ignore for now and handle: 10 wood needed, 9 wood held and 2 platforms held.
if (craftPath.haveItems.ContainsKey(recipe.requiredItem[i].type) && craftPath.haveItems[recipe.requiredItem[i].type] >= recipe.requiredItem[i].stack * multiplier)
{
// Potential problem: Recipe with multiple of same item. Or Item and ItemGroup that share.
// Could implement consumed flag and attempt to consume immediately.
children[i] = new HaveItemNode(recipe.requiredItem[i].type, recipe.requiredItem[i].stack * multiplier, i, this, craftPath);
}
else