-
Notifications
You must be signed in to change notification settings - Fork 1
/
RecursiveSearch.cs
241 lines (208 loc) · 7.98 KB
/
RecursiveSearch.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
namespace RecursiveCraft
{
public class RecursiveSearch
{
public CraftingSource CraftingSource;
public CraftingState CraftingState;
public Dictionary<int, int> Inventory;
public int MaxDepth;
public Dictionary<Recipe, bool> PossibleCraftCache;
public RecursiveSearch(Dictionary<int, int> inventory, CraftingSource craftingSource, int maxDepth)
{
PossibleCraftCache = new Dictionary<Recipe, bool>();
Inventory = inventory;
CraftingSource = craftingSource;
MaxDepth = maxDepth;
}
public RecursiveSearch(Dictionary<int, int> inventory, CraftingSource craftingSource) : this(inventory,
craftingSource, RecursiveCraft.DepthSearch)
{
}
public RecipeInfo FindIngredientsForRecipe(Recipe recipe, int timeCraft = 1)
{
CraftingState = new CraftingState(Inventory);
bool craftable = CraftableAmount(recipe, recipe.createItem.stack * timeCraft,
recipe.createItem.stack * timeCraft, new List<int>()).Item1 > 0;
if (timeCraft == 1)
PossibleCraftCache[recipe] = craftable;
return !craftable ? null : CreateRecipeInfo();
}
public RecipeInfo CreateRecipeInfo()
{
Dictionary<int, int> usedItems = new Dictionary<int, int>();
foreach (KeyValuePair<int, int> keyValuePair in CraftingState.Inventory)
{
if (!Inventory.TryGetValue(keyValuePair.Key, out int amount))
amount = 0;
amount -= keyValuePair.Value;
if (amount != 0)
usedItems.Add(keyValuePair.Key, amount);
}
Dictionary<int, int> trueUsedItems = new Dictionary<int, int>();
foreach (KeyValuePair<int, int> keyValuePair in CraftingState.TrueInventory)
{
if (!Inventory.TryGetValue(keyValuePair.Key, out int amount))
amount = 0;
amount -= keyValuePair.Value;
if (amount != 0)
trueUsedItems.Add(keyValuePair.Key, amount);
}
return new RecipeInfo(usedItems, trueUsedItems, CraftingState.RecipeUsed);
}
public (int, int) CraftableAmount(Recipe recipe, int amount, int trueAmount, List<int> forbiddenItems)
{
if (!IsAvailable(recipe)) return (0, 0);
CraftingState oldState = CraftingState;
CraftingState = new CraftingState(oldState);
List<int> newForbiddenItems = forbiddenItems.ToList();
List<int> recipeAcceptedGroups =
(List<int>) RecursiveCraft.GetAcceptedGroups.Invoke(null, new object[] {recipe});
int timeCraft = (amount + recipe.createItem.stack - 1) / recipe.createItem.stack;
int trueTimeCraft = (trueAmount + recipe.createItem.stack - 1) / recipe.createItem.stack;
foreach (Item ingredient in recipe.requiredItem)
{
if (ingredient.type == ItemID.None) break;
int ingredientsNeeded = timeCraft * ingredient.stack;
int trueIngredientsNeeded =
trueTimeCraft * ingredient.stack - DiscountRecipe(recipe, trueTimeCraft, ingredient);
List<int> ingredientList = ListAllIngredient(recipeAcceptedGroups, ingredient);
ingredientList.RemoveAll(forbiddenItems.Contains);
UseExistingIngredients(ingredientList, ref ingredientsNeeded, ref trueIngredientsNeeded);
if (ingredientsNeeded > 0 && MaxDepth - CraftingState.Depth != 0)
{
if (!newForbiddenItems.Contains(recipe.createItem.type))
newForbiddenItems.Add(recipe.createItem.type);
foreach (int validItem in ingredientList)
{
UseIngredientFromRecipe(newForbiddenItems,
validItem, ref ingredientsNeeded, ref trueIngredientsNeeded);
if (ingredientsNeeded <= 0)
break;
}
}
if (ingredientsNeeded > 0)
{
timeCraft -= (ingredientsNeeded + ingredient.stack - 1) / ingredient.stack;
if (trueTimeCraft != 0)
trueTimeCraft -= (ingredientsNeeded + ingredient.stack - 1) / ingredient.stack;
break;
}
}
if (timeCraft <= 0)
{
CraftingState = oldState;
return (0, 0);
}
else if (amount > timeCraft * recipe.createItem.stack)
{
CraftingState = oldState;
return CraftableAmount(recipe, timeCraft * recipe.createItem.stack,
trueTimeCraft * recipe.createItem.stack, forbiddenItems);
}
else
{
UseItems(recipe, amount, trueAmount, timeCraft, trueTimeCraft);
return (amount, trueAmount);
}
}
public void UseIngredientFromRecipe(List<int> newForbiddenItems,
int validItem, ref int ingredientsNeeded, ref int trueIngredientsNeeded)
{
if (!newForbiddenItems.Contains(validItem) &&
RecursiveCraft.RecipeByResult.TryGetValue(validItem, out List<Recipe> usableRecipes))
foreach (Recipe ingredientRecipe in usableRecipes)
{
(int craftedAmount, int trueCraftedAmount) = CraftableAmount(ingredientRecipe, ingredientsNeeded,
trueIngredientsNeeded, newForbiddenItems);
ingredientsNeeded -= craftedAmount;
trueIngredientsNeeded -= trueCraftedAmount;
if (ingredientsNeeded <= 0) return;
}
}
public int DiscountRecipe(Recipe recipe, int trueTimeCraft, Item ingredient)
{
int discount = 0;
if (recipe.alchemy && CraftingSource.AlchemyTable)
for (int i = 0; i < trueTimeCraft; i++)
if (Main.rand.Next(3) == 0)
discount += ingredient.stack;
return discount;
}
public static List<int> ListAllIngredient(IEnumerable<int> recipeAcceptedGroups, Item ingredient)
{
List<int> ingredientList = new List<int>();
foreach (int validItem in recipeAcceptedGroups
.Select(recipeAcceptedGroup => RecipeGroup.recipeGroups[recipeAcceptedGroup])
.Where(recipeGroup => recipeGroup.ContainsItem(ingredient.netID)).SelectMany(recipeGroup =>
recipeGroup.ValidItems.Where(validItem => !ingredientList.Contains(validItem))))
ingredientList.Add(validItem);
if (ingredientList.Count == 0)
ingredientList.Add(ingredient.type);
return ingredientList;
}
public void UseExistingIngredients(IEnumerable<int> ingredientList, ref int ingredientsNeeded,
ref int trueIngredientsNeeded)
{
foreach (int validItem in ingredientList)
if (CraftingState.Inventory.TryGetValue(validItem, out int availableAmount))
{
int usedAmount = Math.Min(ingredientsNeeded, availableAmount);
CraftingState.Inventory[validItem] -= usedAmount;
ingredientsNeeded -= usedAmount;
usedAmount = Math.Min(trueIngredientsNeeded, availableAmount);
CraftingState.TrueInventory[validItem] -= usedAmount;
trueIngredientsNeeded -= usedAmount;
if (ingredientsNeeded == 0)
break;
}
}
public void UseItems(Recipe recipe, int amount, int trueAmount, int timeCraft, int trueTimeCraft)
{
if (amount < timeCraft * recipe.createItem.stack)
{
if (CraftingState.Inventory.ContainsKey(recipe.createItem.type))
CraftingState.Inventory[recipe.createItem.type] += timeCraft * recipe.createItem.stack - amount;
else
CraftingState.Inventory.Add(recipe.createItem.type, timeCraft * recipe.createItem.stack - amount);
}
if (trueAmount < trueTimeCraft * recipe.createItem.stack)
{
if (CraftingState.TrueInventory.ContainsKey(recipe.createItem.type))
CraftingState.TrueInventory[recipe.createItem.type] +=
trueTimeCraft * recipe.createItem.stack - trueAmount;
else
CraftingState.TrueInventory.Add(recipe.createItem.type,
trueTimeCraft * recipe.createItem.stack - trueAmount);
}
if (CraftingState.RecipeUsed.ContainsKey(recipe))
CraftingState.RecipeUsed[recipe] += trueTimeCraft;
else
CraftingState.RecipeUsed.Add(recipe, trueTimeCraft);
}
public bool IsAvailable(Recipe recipe)
{
if (PossibleCraftCache.TryGetValue(recipe, out bool craftable) && !craftable)
return false;
if (!RecipeHooks.RecipeAvailable(recipe))
return false;
if (recipe.requiredTile.TakeWhile(tile => tile != -1).Any(tile => !CraftingSource.AdjTile[tile]))
return false;
if (recipe.needWater && !CraftingSource.AdjWater &&
!CraftingSource.AdjTile[172])
return false;
if (recipe.needHoney && !CraftingSource.AdjHoney)
return false;
if (recipe.needLava && !CraftingSource.AdjLava)
return false;
if (recipe.needSnowBiome && !CraftingSource.ZoneSnow)
return false;
return true;
}
}
}