public bool TryTakeIngredient(Recipe recipe, Item ingredient) { int need = ingredient.stack; int type = ingredient.type; foreach (var item in catalogs) { if (recipe.useWood(item.Key, type) || recipe.useSand(item.Key, type) || recipe.useIronBar(item.Key, type) || recipe.useFragment(item.Key, type) || recipe.AcceptedByItemGroups(item.Key, type) || recipe.usePressurePlate(item.Key, type) || item.Key == type) { int total = item.Value.Total(); if (total >= need) { item.Value.Take(need); return(true); } else { item.Value.Take(total); need -= total; } } } return(false); }
public static void InvLogic(this Recipe recipe, Item[] array, Item requiredItem, int requiredAmount) { for (int k = 0; k < array.Length; k++) { Item item2 = array[k]; if (requiredAmount <= 0) { break; } if (!item2.IsTheSameAs(requiredItem) && !recipe.useWood(item2.type, requiredItem.type) && !recipe.useSand(item2.type, requiredItem.type) && !recipe.useFragment(item2.type, requiredItem.type) && !recipe.useIronBar(item2.type, requiredItem.type) && !recipe.usePressurePlate(item2.type, requiredItem.type) && !recipe.AcceptedByItemGroups(item2.type, requiredItem.type)) { continue; } if (item2.stack > requiredAmount) { item2.stack -= requiredAmount; requiredAmount = 0; } else { requiredAmount -= item2.stack; array[k] = new Item(); } } }
private bool IsItemPoolItemRuined(Recipe recipe, Item reqItem, Item[] pool, int maxPool) { for (int j = 0; j < maxPool; j++) { Item ingItem = pool[j]; bool isIngMatch = ingItem.IsTheSameAs(reqItem) || recipe.useWood(ingItem.type, reqItem.type) || recipe.useSand(ingItem.type, reqItem.type) || recipe.useFragment(ingItem.type, reqItem.type) || recipe.useIronBar(ingItem.type, reqItem.type) || recipe.usePressurePlate(ingItem.type, reqItem.type) || recipe.AcceptedByItemGroups(ingItem.type, reqItem.type); if (isIngMatch && ingItem.prefix == ModContent.PrefixType <RuinedPrefix>()) { return(true); } } return(false); }
/// <summary> /// Reports all the reasons a given recipe for a givne player will fail with a given set of ingredients (defaults to /// the player's inventory). /// </summary> /// <param name="player"></param> /// <param name="recipe"></param> /// <param name="missingTile">Returns the tile IDs (crafting stations) needed for the recipe.</param> /// <param name="missingItemTypesStacks">Returns the missing item ids and amounts for the recipe.</param> /// <param name="availableIngredients"></param> /// <returns></returns> public static RecipeCraftFailReason GetRecipeFailReasons(Player player, Recipe recipe, out int[] missingTile, out IDictionary <int, int> missingItemTypesStacks, IEnumerable <Item> availableIngredients = null ) { RecipeCraftFailReason reason = 0; var missingTileList = new List <int>(); missingItemTypesStacks = new Dictionary <int, int>(); // Get available item ingredients if (availableIngredients == null) { availableIngredients = player.inventory .Take(58) .Where(item => !item.IsAir); bool? _; Item[] chest = PlayerItemHelpers.GetCurrentlyOpenChest(player, out _); if (chest != null) { availableIngredients = availableIngredients.Concat(chest); } } // Process ingredients list into id + stack map IDictionary <int, int> availIngredientInfo = new Dictionary <int, int>(availableIngredients.Count()); foreach (Item item in availableIngredients) { if (availIngredientInfo.ContainsKey(item.netID)) { availIngredientInfo[item.netID] += item.stack; } else { availIngredientInfo[item.netID] = item.stack; } } // Tiles for (int i = 0; i < recipe.requiredTile.Length; i++) { int reqTileType = recipe.requiredTile[i]; if (reqTileType == -1) { break; } if (!player.adjTile[reqTileType]) { missingTileList.Add(reqTileType); reason |= RecipeCraftFailReason.MissingTile; } } // Items for (int i = 0; i < recipe.requiredItem.Length; i++) { Item reqItem = recipe.requiredItem[i]; if (reqItem == null || reqItem.type == 0) { break; } int reqStack = reqItem.stack; bool hasCheckedGroups = false; foreach (var kv in availIngredientInfo) { int itemType = kv.Key; int itemStack = kv.Value; if (recipe.useWood(itemType, reqItem.type) || recipe.useSand(itemType, reqItem.type) || recipe.useIronBar(itemType, reqItem.type) || recipe.useFragment(itemType, reqItem.type) || recipe.usePressurePlate(itemType, reqItem.type) || recipe.AcceptedByItemGroups(itemType, reqItem.type)) { reqStack -= itemStack; hasCheckedGroups = true; } } if (!hasCheckedGroups && availIngredientInfo.ContainsKey(reqItem.netID)) { reqStack -= availIngredientInfo[reqItem.netID]; } // Account for missing ingredients: if (reqStack > 0) { missingItemTypesStacks[reqItem.netID] = reqStack; reason |= RecipeCraftFailReason.MissingItem; } } if (recipe.needWater && !player.adjWater && !player.adjTile[172]) { reason |= RecipeCraftFailReason.NeedsNearbyWater; } if (recipe.needHoney && !player.adjHoney) { reason |= RecipeCraftFailReason.NeedsNearbyHoney; } if (recipe.needLava && !player.adjLava) { reason |= RecipeCraftFailReason.NeedsNearbyLava; } if (recipe.needSnowBiome && !player.ZoneSnow) { reason |= RecipeCraftFailReason.NeedsNearbySnowBiome; } missingTile = missingTileList.ToArray(); return(reason); }
private void validateAndResearchRecipe(Recipe r) { if (IsResearched(r.createItem.type)) { return; } if (r.requiredTile == null || r.requiredTile.Length == 0) { goto label_checkIngredients; } int[] tiles = new int[r.requiredTile.Length]; for (int t = 0; t < tiles.Length; t++) { tiles[t] = r.requiredTile[t]; //Main.NewText("Req tile = " + tiles[t]); } if (tiles.Length > 0) { for (int i = 0; i < tiles.Length; i++) { if (tiles[i] >= 0) { if (tiles[i] >= researchedTileAdj.Length) { return; } if (!researchedTileAdj[tiles[i]]) { return; } } } } label_checkIngredients: if (r.requiredItem == null || r.requiredItem.Length == 0) { goto label_direct_add; } Item[] ing = r.requiredItem; for (int i = 0; i < ing.Length; i++) { if (ing[i] == null || ing[i].IsAir) { // Main.NewText("Was air"); continue; } if (IsResearched(ing[i].type)) { // Main.NewText("Found item= " + ing[i].Name); continue; } for (int g = 0; g < researchedCache.Count; g++) { if (r.AcceptedByItemGroups(researchedCache[g], ing[i].type)) { goto label_next_recipe_item; } if (r.anyIronBar && r.useIronBar(researchedCache[g], ing[i].type)) { goto label_next_recipe_item; } if (r.anyWood && r.useWood(researchedCache[g], ing[i].type)) { goto label_next_recipe_item; } if (r.anySand && r.useSand(researchedCache[g], ing[i].type)) { goto label_next_recipe_item; } if (r.anyPressurePlate && r.usePressurePlate(researchedCache[g], ing[i].type)) { goto label_next_recipe_item; } if (r.anyFragment && r.useFragment(researchedCache[g], ing[i].type)) { goto label_next_recipe_item; } } return; label_next_recipe_item: //Main.NewText("Found item replacement= " + ing[i].Name); continue; } label_direct_add: AddResearchedAmount(r.createItem.type, Int32.MaxValue - 1000); researchedCache.Add(r.createItem.type); CheckRecipesForItem(r.createItem.type); }
private bool RecipeGroupMatch(Recipe recipe, int type1, int type2) { return(recipe.useWood(type1, type2) || recipe.useSand(type1, type2) || recipe.useIronBar(type1, type2) || recipe.useFragment(type1, type2) || recipe.AcceptedByItemGroups(type1, type2) || recipe.usePressurePlate(type1, type2)); }
public static void DoWithdrawItemForCraft(Recipe self, ref Item item, ref int required) { if (StoragePlayer.Get.StorageAccess.X < 0) { return; } var instance = MagicStoragePlus.Instance.StorageUI; bool changed = false; foreach (Item i in instance.items) { if (required <= 0) { if (changed) { instance.RefreshItems(); } return; } if (item.IsTheSameAs(i) || self.useWood(item.type, i.type) || self.useSand(item.type, i.type) || self.useIronBar(item.type, i.type) || self.usePressurePlate(item.type, i.type) || self.useFragment(item.type, i.type) || self.AcceptedByItemGroups(item.type, i.type)) { int count = Math.Min(required, item.stack); required -= count; var request = i.Clone(); request.stack = count; TEStorageHeart heart = StoragePlayer.GetStorageHeart(); if (Main.netMode == 0) { request = heart.TryWithdraw(request); } else { NetHelper.SendWithdraw(heart.ID, request, NetHelper.StorageOp.WithdrawJustRemove); request = new Item(); } if (!request.IsAir) { request.TurnToAir(); changed = true; } } } if (changed) { instance.RefreshItems(); } }
// Like Player.CountItem, except caps at stopCountingAt exactly and counts items that satisfy recipe as well. public int CountItemGroups(Player player, Recipe recipe, int type, int stopCountingAt = 1) { int count = 0; if (type == 0) { return(0); } for (int i = 0; i != 58; i++) { if (!player.inventory[i].IsAir) { int current = player.inventory[i].type; if (recipe.useWood(current, item.type) || recipe.useSand(current, item.type) || recipe.useIronBar(current, item.type) || recipe.useFragment(current, item.type) || recipe.AcceptedByItemGroups(current, item.type) || recipe.usePressurePlate(current, item.type)) { count += player.inventory[i].stack; } else if (current == type) { count += player.inventory[i].stack; } //if (count >= stopCountingAt) //{ // return stopCountingAt; //} } } return(count >= stopCountingAt ? stopCountingAt : count); }