Terraria ModLoader  0.11.7.8
A mod to make and play Terraria mods
RecipeFinder.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
2 using Terraria.ID;
4 
5 namespace Terraria.ModLoader
6 {
10  public class RecipeFinder
11  {
12  private List<Item> items = new List<Item>();
13  private List<int> groups = new List<int>();
14  private Item result = new Item();
15  private List<int> tiles = new List<int>();
19  public bool needWater;
23  public bool needLava;
27  public bool needHoney;
28 
29  public RecipeFinder() {
30  }
31 
37  public void AddIngredient(int itemID, int stack = 1) {
38  if (itemID <= 0 || itemID >= ItemLoader.ItemCount) {
39  throw new RecipeException("No item has ID " + itemID);
40  }
41  Item item = new Item();
42  item.SetDefaults(itemID, false);
43  item.stack = stack;
44  items.Add(item);
45  }
46 
52  public void AddRecipeGroup(string name, int stack = 1) {
53  if (!RecipeGroup.recipeGroupIDs.ContainsKey(name)) {
54  throw new RecipeException("No recipe group is named " + name);
55  }
56  int id = RecipeGroup.recipeGroupIDs[name];
57  RecipeGroup rec = RecipeGroup.recipeGroups[id];
58  AddIngredient(rec.ValidItems[rec.IconicItemIndex], stack);
59  groups.Add(id);
60  }
61 
67  public void AddRecipeGroup(int recipeGroupID, int stack = 1)
68  {
69  if (!RecipeGroup.recipeGroupIDs.ContainsValue(recipeGroupID)) {
70  throw new RecipeException("No recipe group has the RecipeGroupID " + recipeGroupID);
71  }
72  RecipeGroup rec = RecipeGroup.recipeGroups[recipeGroupID];
73  AddIngredient(rec.ValidItems[rec.IconicItemIndex], stack);
74  groups.Add(recipeGroupID);
75  }
76 
82  public void SetResult(int itemID, int stack = 1) {
83  if (itemID <= 0 || itemID >= ItemLoader.ItemCount) {
84  throw new RecipeException("No item has ID " + itemID);
85  }
86  result.SetDefaults(itemID, false);
87  result.stack = stack;
88  }
89 
94  public void AddTile(int tileID) {
95  if (tileID < 0 || tileID >= TileLoader.TileCount) {
96  throw new RecipeException("No tile has ID " + tileID);
97  }
98  tiles.Add(tileID);
99  }
100 
105  public Recipe FindExactRecipe() {
106  for (int k = 0; k < Recipe.numRecipes; k++) {
107  Recipe recipe = Main.recipe[k];
108  bool matches = true;
109  List<Item> checkItems = new List<Item>(items);
110  for (int i = 0; i < Recipe.maxRequirements; i++) {
111  Item item = recipe.requiredItem[i];
112  if (item.type == 0) {
113  break;
114  }
115  bool itemMatched = false;
116  for (int j = 0; j < checkItems.Count; j++) {
117  if (item.type == checkItems[j].type && item.stack == checkItems[j].stack) {
118  itemMatched = true;
119  checkItems.RemoveAt(j);
120  break;
121  }
122  }
123  if (!itemMatched) {
124  matches = false;
125  break;
126  }
127  }
128  if (checkItems.Count > 0) {
129  matches = false;
130  }
131  List<int> checkGroups = new List<int>(groups);
132  List<int> acceptedGroups = GetAcceptedGroups(recipe);
133  for (int i = 0; i < acceptedGroups.Count; i++) {
134  int group = acceptedGroups[i];
135  bool groupMatched = false;
136  for (int j = 0; j < checkGroups.Count; j++) {
137  if (group == checkGroups[j]) {
138  groupMatched = true;
139  checkGroups.RemoveAt(j);
140  break;
141  }
142  }
143  if (!groupMatched) {
144  matches = false;
145  break;
146  }
147  }
148  if (checkGroups.Count > 0) {
149  matches = false;
150  }
151  if (result.type != recipe.createItem.type || result.stack != recipe.createItem.stack) {
152  matches = false;
153  }
154  List<int> checkTiles = new List<int>(tiles);
155  for (int i = 0; i < Recipe.maxRequirements; i++) {
156  int tile = recipe.requiredTile[i];
157  if (tile == -1) {
158  break;
159  }
160  bool tileMatched = false;
161  for (int j = 0; j < checkTiles.Count; j++) {
162  if (tile == checkTiles[j]) {
163  tileMatched = true;
164  checkTiles.RemoveAt(j);
165  break;
166  }
167  }
168  if (!tileMatched) {
169  matches = false;
170  break;
171  }
172  }
173  if (checkTiles.Count > 0) {
174  matches = false;
175  }
176  if (needWater != recipe.needWater) {
177  matches = false;
178  }
179  else if (needLava != recipe.needLava) {
180  matches = false;
181  }
182  else if (needHoney != recipe.needHoney) {
183  matches = false;
184  }
185  if (matches) {
186  return recipe;
187  }
188  }
189  return null;
190  }
191 
196  public List<Recipe> SearchRecipes() {
197  List<Recipe> recipes = new List<Recipe>();
198  for (int k = 0; k < Recipe.numRecipes; k++) {
199  Recipe recipe = Main.recipe[k];
200  bool matches = true;
201  List<Item> checkItems = new List<Item>(items);
202  for (int i = 0; i < Recipe.maxRequirements; i++) {
203  Item item = recipe.requiredItem[i];
204  if (item.type == 0) {
205  break;
206  }
207  for (int j = 0; j < checkItems.Count; j++) {
208  if (item.type == checkItems[j].type && item.stack >= checkItems[j].stack) {
209  checkItems.RemoveAt(j);
210  break;
211  }
212  }
213  }
214  if (checkItems.Count > 0) {
215  matches = false;
216  }
217  List<int> checkGroups = new List<int>(groups);
218  List<int> acceptedGroups = GetAcceptedGroups(recipe);
219  for (int i = 0; i < acceptedGroups.Count; i++) {
220  int group = acceptedGroups[i];
221  for (int j = 0; j < checkGroups.Count; j++) {
222  if (group == checkGroups[j]) {
223  checkGroups.RemoveAt(j);
224  break;
225  }
226  }
227  }
228  if (checkGroups.Count > 0) {
229  matches = false;
230  }
231  if (result.type != 0) {
232  if (result.type != recipe.createItem.type) {
233  matches = false;
234  }
235  else if (result.stack > recipe.createItem.stack) {
236  matches = false;
237  }
238  }
239  List<int> checkTiles = new List<int>(tiles);
240  for (int i = 0; i < Recipe.maxRequirements; i++) {
241  int tile = recipe.requiredTile[i];
242  if (tile == -1) {
243  break;
244  }
245  for (int j = 0; j < checkTiles.Count; j++) {
246  if (tile == checkTiles[j]) {
247  checkTiles.RemoveAt(j);
248  break;
249  }
250  }
251  }
252  if (checkTiles.Count > 0) {
253  matches = false;
254  }
255  if (needWater && !recipe.needWater) {
256  matches = false;
257  }
258  else if (needLava && !recipe.needLava) {
259  matches = false;
260  }
261  else if (needHoney && !recipe.needHoney) {
262  matches = false;
263  }
264  if (matches) {
265  recipes.Add(recipe);
266  }
267  }
268  return recipes;
269  }
270 
271  private static List<int> GetAcceptedGroups(Recipe recipe) {
272  List<int> acceptedGroups = new List<int>(recipe.acceptedGroups);
273  if (recipe.anyWood) {
274  acceptedGroups.Add(RecipeGroupID.Wood);
275  }
276  if (recipe.anyIronBar) {
277  acceptedGroups.Add(RecipeGroupID.IronBar);
278  }
279  if (recipe.anySand) {
280  acceptedGroups.Add(RecipeGroupID.Sand);
281  }
282  if (recipe.anyPressurePlate) {
283  acceptedGroups.Add(RecipeGroupID.PressurePlate);
284  }
285  if (recipe.anyFragment) {
286  acceptedGroups.Add(RecipeGroupID.Fragment);
287  }
288  return acceptedGroups;
289  }
290  }
291 }
bool needHoney
Adds the requirement of being nearby honey to the search criteria. Defaults to false.
Definition: RecipeFinder.cs:27
void AddTile(int tileID)
Adds a required crafting station with the given tile type to the search criteria. ...
Definition: RecipeFinder.cs:94
List< Recipe > SearchRecipes()
Searches for all recipes that include the search criteria, then returns them in a list...
void AddIngredient(int itemID, int stack=1)
Adds an ingredient with the given item type and stack size to the search criteria.
Definition: RecipeFinder.cs:37
This class will search through all existing recipes for you based on criteria that you give it...
Definition: RecipeFinder.cs:10
This serves as the central class from which tile-related functions are supported and carried out...
Definition: TileLoader.cs:14
void AddRecipeGroup(int recipeGroupID, int stack=1)
Adds a recipe group ingredient with the given RecipeGroupID and stack size to the search criteria...
Definition: RecipeFinder.cs:67
void SetResult(int itemID, int stack=1)
Sets the search criteria&#39;s result to the given item type and stack size.
Definition: RecipeFinder.cs:82
bool needLava
Adds the requirement of being nearby lava to the search criteria. Defaults to false.
Definition: RecipeFinder.cs:23
bool needWater
Adds the requirement of being nearby water to the search criteria. Defaults to false.
Definition: RecipeFinder.cs:19
Recipe FindExactRecipe()
Searches for a recipe that matches the search criteria exactly, then returns it. That means the recip...
This serves as the central class from which item-related functions are carried out. It also stores a list of mod items by ID.
Definition: ItemLoader.cs:21
static List< int > GetAcceptedGroups(Recipe recipe)
void AddRecipeGroup(string name, int stack=1)
Adds a recipe group ingredient with the given RecipeGroup name and stack size to the search criteria...
Definition: RecipeFinder.cs:52