/
RecursiveCraft.cs
258 lines (218 loc) · 7.46 KB
/
RecursiveCraft.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Xna.Framework;
using Mono.Cecil.Cil;
using MonoMod.Cil;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using ILRecipe = IL.Terraria.Recipe;
using OnMain = On.Terraria.Main;
namespace RecursiveCraft
{
public class RecursiveCraft : Mod
{
public static Dictionary<int, List<Recipe>> RecipeByResult;
public static Dictionary<Recipe, RecipeInfo> RecipeInfoCache;
public static List<int> SortedRecipeList;
public static CompoundRecipe CompoundRecipe;
public static MethodInfo GetAcceptedGroups;
public static int DepthSearch;
public static bool InventoryIsOpen;
public static ModHotKey[] Hotkeys;
public static List<Func<bool>> InventoryChecks;
public override void Load()
{
ILRecipe.FindRecipes += ApplyRecursiveSearch;
OnMain.DrawInventory += EditFocusRecipe;
OnMain.Update += ApplyKey;
RecipeInfoCache = new Dictionary<Recipe, RecipeInfo>();
Hotkeys = new[]
{
RegisterHotKey("Infinite crafting depth", "Home"),
RegisterHotKey("+1 crafting depth", "PageUp"),
RegisterHotKey("-1 crafting depth", "PageDown"),
RegisterHotKey("No crafting depth", "End")
};
InventoryChecks = new List<Func<bool>>
{
() => Main.playerInventory
};
GetAcceptedGroups =
typeof(RecipeFinder).GetMethod("GetAcceptedGroups", BindingFlags.NonPublic | BindingFlags.Static);
}
public override void Unload()
{
ILRecipe.FindRecipes -= ApplyRecursiveSearch;
OnMain.DrawInventory -= EditFocusRecipe;
OnMain.Update -= ApplyKey;
RecipeInfoCache = null;
RecipeByResult = null;
SortedRecipeList = null;
Hotkeys = null;
if (CompoundRecipe?.OverridenRecipe != null)
Main.recipe[CompoundRecipe.RecipeId] = CompoundRecipe.OverridenRecipe;
CompoundRecipe = null;
InventoryChecks = null;
GetAcceptedGroups = null;
}
public override void PostAddRecipes()
{
CompoundRecipe = new CompoundRecipe(this);
RecipeByResult = new Dictionary<int, List<Recipe>>();
Dictionary<int, int> ingredientsNeeded = new Dictionary<int, int>();
Dictionary<Recipe, int> correspondingId = new Dictionary<Recipe, int>();
float[] costs = new float[Recipe.maxRecipes];
foreach (Recipe recipe in Main.recipe)
{
int type = recipe.createItem.type;
if (type == ItemID.None) break;
if (!RecipeByResult.TryGetValue(type, out List<Recipe> list))
{
list = new List<Recipe>();
RecipeByResult.Add(type, list);
if (!ingredientsNeeded.ContainsKey(type))
ingredientsNeeded[type] = 0;
}
list.Add(recipe);
}
for (int index = 0; index < Recipe.maxRecipes; index++)
{
Recipe recipe = Main.recipe[index];
correspondingId[recipe] = index;
float cost = 1;
foreach (Item ingredient in recipe.requiredItem)
{
if (ingredient.type == ItemID.None) break;
List<int> recipeAcceptedGroups = (List<int>) GetAcceptedGroups.Invoke(null, new object[] {recipe});
foreach (int possibleIngredient in RecursiveSearch.ListAllIngredient(recipeAcceptedGroups,
ingredient))
{
if (RecipeByResult.TryGetValue(possibleIngredient, out List<Recipe> list))
cost += list.Count * ingredient.stack;
if (ingredientsNeeded.TryGetValue(possibleIngredient, out int timesUsed))
ingredientsNeeded[possibleIngredient] = timesUsed + 1;
else
ingredientsNeeded[possibleIngredient] = 1;
}
}
costs[index] = cost / recipe.createItem.stack;
}
foreach (List<Recipe> recipes in RecipeByResult.Select(keyValuePair => keyValuePair.Value))
recipes.Sort((recipe1, recipe2) =>
(int) (costs[correspondingId[recipe2]] - costs[correspondingId[recipe1]]));
List<KeyValuePair<int, int>> sortedIngredientList = ingredientsNeeded.ToList();
sortedIngredientList.Sort((pair, valuePair) => valuePair.Value - pair.Value);
SortedRecipeList = new List<int>();
foreach (KeyValuePair<int, int> keyValuePair in sortedIngredientList)
if (RecipeByResult.TryGetValue(keyValuePair.Key, out List<Recipe> recipes))
foreach (Recipe recipe in recipes)
SortedRecipeList.Add(correspondingId[recipe]);
}
public static bool UpdateInventoryState()
{
bool wasOpen = InventoryIsOpen;
InventoryIsOpen = false;
foreach (Func<bool> inventoryCheck in InventoryChecks) InventoryIsOpen |= inventoryCheck.Invoke();
return InventoryIsOpen == wasOpen;
}
public void ApplyKey(OnMain.orig_Update orig, Main self, GameTime gameTime)
{
if (UpdateInventoryState())
DepthSearch = ((RecursiveSettings) GetConfig("RecursiveSettings")).DefaultDepth;
if (InventoryIsOpen)
{
int oldDepth = DepthSearch;
if (Hotkeys[0].JustPressed)
{
DepthSearch = -1;
}
else if (Hotkeys[1].JustPressed)
{
if (DepthSearch == -1)
DepthSearch = 5;
else
DepthSearch++;
}
else if (Hotkeys[2].JustPressed)
{
if (DepthSearch == 0)
DepthSearch = 0;
else if (DepthSearch == 5)
DepthSearch = -1;
else
DepthSearch++;
}
else if (Hotkeys[3].JustPressed)
{
DepthSearch = 0;
}
if (oldDepth != DepthSearch)
Recipe.FindRecipes();
}
orig(self, gameTime);
}
public static void EditFocusRecipe(OnMain.orig_DrawInventory orig, Main self)
{
if (CompoundRecipe.OverridenRecipe != null)
Main.recipe[CompoundRecipe.RecipeId] = CompoundRecipe.OverridenRecipe;
int i = Main.availableRecipe[Main.focusRecipe];
Recipe recipe = Main.recipe[i];
if (RecipeInfoCache.TryGetValue(recipe, out RecipeInfo recipeInfo))
{
CompoundRecipe.Apply(i, recipeInfo);
Main.recipe[i] = CompoundRecipe;
}
else
{
CompoundRecipe.OverridenRecipe = null;
}
orig(self);
}
public static void ApplyRecursiveSearch(ILContext il)
{
ILCursor cursor = new ILCursor(il);
if (!cursor.TryGotoNext(MoveType.After,
instruction => instruction.OpCode == OpCodes.Blt_S && instruction.Previous.MatchLdcI4(40)))
return;
ILLabel label = cursor.DefineLabel();
IEnumerable<ILLabel> incomingLabels = cursor.IncomingLabels.ToList();
foreach (ILLabel cursorIncomingLabel in incomingLabels) cursor.MarkLabel(cursorIncomingLabel);
cursor.Emit(OpCodes.Ldloc, 6);
cursor.Emit(OpCodes.Call, typeof(RecursiveCraft).GetMethod("FindRecipes"));
cursor.Emit(OpCodes.Br_S, label);
if (!cursor.TryGotoNext(MoveType.Before,
instruction => instruction.OpCode == OpCodes.Ldc_I4_0 && instruction.Previous.OpCode == OpCodes.Brtrue))
{
cursor.Index -= 3;
cursor.RemoveRange(3);
foreach (ILLabel cursorIncomingLabel in incomingLabels) cursor.MarkLabel(cursorIncomingLabel);
return;
}
cursor.MarkLabel(label);
}
public static void FindRecipes(Dictionary<int, int> inventory)
{
RecipeInfoCache.Clear();
RecursiveSearch recursiveSearch = new RecursiveSearch(inventory, CraftingSource.PlayerAsCraftingSource());
SortedSet<int> sortedAvailableRecipes = new SortedSet<int>();
foreach (int n in SortedRecipeList)
{
Recipe recipe = Main.recipe[n];
if (recipe is CompoundRecipe compoundRecipe)
recipe = compoundRecipe.OverridenRecipe;
RecipeInfo recipeInfo = recursiveSearch.FindIngredientsForRecipe(recipe);
if (recipeInfo != null)
{
if (recipeInfo.RecipeUsed.Count > 1)
RecipeInfoCache.Add(recipe, recipeInfo);
sortedAvailableRecipes.Add(n);
}
}
foreach (int availableRecipe in sortedAvailableRecipes)
Main.availableRecipe[Main.numAvailableRecipes++] = availableRecipe;
}
}
}