2018-12-25 14:02:25 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2018-12-30 18:00:05 +08:00
|
|
|
using Revitalize.Framework.Utilities;
|
|
|
|
using StardewValley;
|
2018-12-25 14:02:25 +08:00
|
|
|
|
|
|
|
namespace Revitalize.Framework.Crafting
|
|
|
|
{
|
2019-08-22 10:06:17 +08:00
|
|
|
/// <summary>
|
|
|
|
/// A crafting recipe.
|
|
|
|
/// </summary>
|
2018-12-25 14:02:25 +08:00
|
|
|
public class Recipe
|
|
|
|
{
|
2019-08-22 10:06:17 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The ingredients necessary to craft this recipe.
|
|
|
|
/// </summary>
|
2019-09-05 05:20:05 +08:00
|
|
|
public List<CraftingRecipeComponent> ingredients;
|
2019-08-22 10:06:17 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The items produced by this recipe.
|
|
|
|
/// </summary>
|
2019-09-05 05:20:05 +08:00
|
|
|
public List<CraftingRecipeComponent> outputs;
|
2018-12-25 14:02:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
public Item DisplayItem
|
|
|
|
{
|
2019-09-12 13:48:38 +08:00
|
|
|
get => this.outputs.ElementAt(0).item;
|
2018-12-25 14:02:25 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 10:06:17 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The description for the crafting recipe.
|
|
|
|
/// </summary>
|
2018-12-25 14:02:25 +08:00
|
|
|
public string outputDescription;
|
2019-08-22 10:06:17 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The name for the crafting recipe.
|
|
|
|
/// </summary>
|
2018-12-25 14:02:25 +08:00
|
|
|
public string outputName;
|
|
|
|
|
2019-08-22 10:06:17 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The stats that this recipe costs. Magic, HP, stamina, gold, etc.
|
|
|
|
/// </summary>
|
2019-01-06 15:25:50 +08:00
|
|
|
public StatCost statCost;
|
2018-12-25 14:02:25 +08:00
|
|
|
|
2019-09-12 09:04:12 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The number of in-game minutes it takes to craft this item.
|
|
|
|
/// </summary>
|
|
|
|
public int timeToCraft;
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
public Recipe() { }
|
2018-12-25 14:02:25 +08:00
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Constructor for single item output.</summary>
|
2018-12-25 14:02:25 +08:00
|
|
|
/// <param name="inputs">All the ingredients required to make the output.</param>
|
|
|
|
/// <param name="output">The item given as output with how many</param>
|
2019-09-12 09:04:12 +08:00
|
|
|
public Recipe(List<CraftingRecipeComponent> inputs, CraftingRecipeComponent output, StatCost StatCost = null,int TimeToCraft=0)
|
2018-12-25 14:02:25 +08:00
|
|
|
{
|
|
|
|
this.ingredients = inputs;
|
2019-09-05 05:20:05 +08:00
|
|
|
this.outputDescription = output.item.getDescription();
|
|
|
|
this.outputName = output.item.DisplayName;
|
|
|
|
this.outputs = new List<CraftingRecipeComponent>()
|
2018-12-30 18:00:05 +08:00
|
|
|
{
|
2019-09-05 05:20:05 +08:00
|
|
|
output
|
2018-12-30 18:00:05 +08:00
|
|
|
};
|
2019-01-06 15:25:50 +08:00
|
|
|
this.statCost = StatCost ?? new StatCost();
|
2019-09-12 09:04:12 +08:00
|
|
|
this.timeToCraft = TimeToCraft;
|
2018-12-25 14:02:25 +08:00
|
|
|
}
|
|
|
|
|
2019-09-12 09:04:12 +08:00
|
|
|
public Recipe(List<CraftingRecipeComponent> inputs, List<CraftingRecipeComponent> outputs, string OutputName, string OutputDescription, Item DisplayItem = null, StatCost StatCost = null,int TimeToCraft=0)
|
2018-12-25 14:02:25 +08:00
|
|
|
{
|
|
|
|
this.ingredients = inputs;
|
|
|
|
this.outputs = outputs;
|
|
|
|
this.outputName = OutputName;
|
|
|
|
this.outputDescription = OutputDescription;
|
2019-01-06 15:25:50 +08:00
|
|
|
this.statCost = StatCost ?? new StatCost();
|
2019-09-12 09:04:12 +08:00
|
|
|
this.timeToCraft = TimeToCraft;
|
2018-12-25 14:02:25 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Checks if a player contains all recipe ingredients.</summary>
|
2018-12-25 14:02:25 +08:00
|
|
|
public bool PlayerContainsAllIngredients()
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
return this.InventoryContainsAllIngredient(Game1.player.Items.ToList());
|
2018-12-25 14:02:25 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Checks if a player contains a recipe ingredient.</summary>
|
2019-09-05 05:20:05 +08:00
|
|
|
public bool PlayerContainsIngredient(CraftingRecipeComponent pair)
|
2018-12-25 14:02:25 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
return this.InventoryContainsIngredient(Game1.player.Items.ToList(), pair);
|
2018-12-25 14:02:25 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Checks if an inventory contains all items.</summary>
|
2019-08-23 04:43:11 +08:00
|
|
|
public bool InventoryContainsAllIngredient(IList<Item> items)
|
2018-12-25 14:02:25 +08:00
|
|
|
{
|
2019-09-05 05:20:05 +08:00
|
|
|
foreach (CraftingRecipeComponent pair in this.ingredients)
|
2018-12-30 18:00:05 +08:00
|
|
|
if (!this.InventoryContainsIngredient(items, pair)) return false;
|
2018-12-25 14:02:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Checks if an inventory contains an ingredient.</summary>
|
2019-09-05 05:20:05 +08:00
|
|
|
public bool InventoryContainsIngredient(IList<Item> items, CraftingRecipeComponent pair)
|
2018-12-25 14:02:25 +08:00
|
|
|
{
|
|
|
|
foreach (Item i in items)
|
|
|
|
{
|
2019-09-05 05:20:05 +08:00
|
|
|
if (i != null && this.ItemEqualsOther(i, pair.item) && pair.requiredAmount <= i.Stack)
|
2018-12-25 14:02:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Checks roughly if two items equal each other.</summary>
|
2018-12-25 14:02:25 +08:00
|
|
|
public bool ItemEqualsOther(Item self, Item other)
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
return
|
|
|
|
self.Name == other.Name
|
|
|
|
&& self.getCategoryName() == other.getCategoryName()
|
|
|
|
&& self.GetType() == other.GetType();
|
2018-12-25 14:02:25 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 10:06:17 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Consumes all of the ingredients for the recipe.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="from"></param>
|
2019-08-23 04:43:11 +08:00
|
|
|
public void consume(ref IList<Item> from)
|
2018-12-25 14:02:25 +08:00
|
|
|
{
|
2019-08-23 04:43:11 +08:00
|
|
|
if (this.InventoryContainsAllIngredient(from) == false)
|
2018-12-30 18:00:05 +08:00
|
|
|
return;
|
2018-12-25 14:02:25 +08:00
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
InventoryManager manager = new InventoryManager(from);
|
2018-12-25 14:02:25 +08:00
|
|
|
List<Item> removalList = new List<Item>();
|
2019-09-05 05:20:05 +08:00
|
|
|
foreach (CraftingRecipeComponent pair in this.ingredients)
|
2018-12-25 14:02:25 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
foreach (Item item in manager.items)
|
2018-12-25 14:02:25 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
if (item == null) continue;
|
2019-09-05 05:20:05 +08:00
|
|
|
if (this.ItemEqualsOther(item, pair.item))
|
2018-12-25 14:02:25 +08:00
|
|
|
{
|
2019-09-05 05:20:05 +08:00
|
|
|
if (item.Stack == pair.requiredAmount)
|
2018-12-30 18:00:05 +08:00
|
|
|
removalList.Add(item); //remove the item
|
2018-12-25 14:02:25 +08:00
|
|
|
else
|
2019-09-05 05:20:05 +08:00
|
|
|
item.Stack -= pair.requiredAmount; //or reduce the stack size.
|
2018-12-25 14:02:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var v in removalList)
|
|
|
|
manager.items.Remove(v);
|
|
|
|
removalList.Clear();
|
|
|
|
from = manager.items;
|
|
|
|
}
|
|
|
|
|
2019-08-22 10:06:17 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Produces outputs for the crafting recipe.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="to"></param>
|
|
|
|
/// <param name="dropToGround"></param>
|
|
|
|
/// <param name="isPlayerInventory"></param>
|
2019-08-23 04:43:11 +08:00
|
|
|
public void produce(ref IList<Item> to, bool dropToGround = false, bool isPlayerInventory = false)
|
2018-12-25 14:02:25 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
var manager = isPlayerInventory
|
|
|
|
? new InventoryManager(new List<Item>())
|
|
|
|
: new InventoryManager(to);
|
2019-09-05 05:20:05 +08:00
|
|
|
foreach (CraftingRecipeComponent pair in this.outputs)
|
2018-12-25 14:23:25 +08:00
|
|
|
{
|
2019-09-05 05:20:05 +08:00
|
|
|
Item item = pair.item.getOne();
|
|
|
|
item.addToStack(pair.requiredAmount - 1);
|
2018-12-30 18:00:05 +08:00
|
|
|
bool added = manager.addItem(item);
|
|
|
|
if (!added && dropToGround)
|
|
|
|
Game1.createItemDebris(item, Game1.player.getStandingPosition(), Game1.player.getDirection());
|
2018-12-25 14:02:25 +08:00
|
|
|
}
|
|
|
|
to = manager.items;
|
|
|
|
}
|
|
|
|
|
2019-08-22 10:06:17 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Consumes all ingredients in given inventory and adds in outputs to the other given inventory.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="from">The inventory to take ingredients from.</param>
|
|
|
|
/// <param name="to">The inventory to put outputs into.</param>
|
|
|
|
/// <param name="dropToGround">Should this item be dropped to the ground when crafted?</param>
|
|
|
|
/// <param name="isPlayerInventory">Checks to see if the invventory is the player's</param>
|
2019-09-03 10:02:58 +08:00
|
|
|
public void craft(ref IList<Item> from, ref IList<Item> to, bool dropToGround = false, bool isPlayerInventory = false)
|
2018-12-25 14:02:25 +08:00
|
|
|
{
|
2018-12-25 14:23:25 +08:00
|
|
|
InventoryManager manager = new InventoryManager(to);
|
|
|
|
if (manager.ItemCount + this.outputs.Count >= manager.capacity)
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
if (isPlayerInventory)
|
|
|
|
Game1.showRedMessage("Inventory Full");
|
2018-12-25 14:23:25 +08:00
|
|
|
else return;
|
|
|
|
}
|
2018-12-30 18:00:05 +08:00
|
|
|
this.consume(ref from);
|
|
|
|
this.produce(ref to, dropToGround, isPlayerInventory);
|
2018-12-25 14:02:25 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 10:06:17 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Actually crafts the recipe.
|
|
|
|
/// </summary>
|
2018-12-25 14:02:25 +08:00
|
|
|
public void craft()
|
|
|
|
{
|
2019-08-23 04:43:11 +08:00
|
|
|
IList<Item> playerItems = Game1.player.Items;
|
|
|
|
IList<Item> outPutItems = new List<Item>();
|
2018-12-30 18:00:05 +08:00
|
|
|
this.craft(ref playerItems, ref outPutItems, true, true);
|
2018-12-25 14:23:25 +08:00
|
|
|
|
|
|
|
Game1.player.Items = playerItems; //Set the items to be post consumption.
|
2018-12-30 18:00:05 +08:00
|
|
|
foreach (Item I in outPutItems)
|
2018-12-25 14:23:25 +08:00
|
|
|
Game1.player.addItemToInventory(I); //Add all items produced.
|
2019-01-06 15:25:50 +08:00
|
|
|
this.statCost.payCost();
|
2018-12-25 14:02:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool PlayerCanCraft()
|
|
|
|
{
|
2019-01-06 15:25:50 +08:00
|
|
|
return this.PlayerContainsAllIngredients() && this.statCost.canSafelyAffordCost();
|
2018-12-25 14:02:25 +08:00
|
|
|
}
|
|
|
|
|
2019-09-03 10:02:58 +08:00
|
|
|
public bool CanCraft(IList<Item> items)
|
2018-12-25 14:02:25 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
return this.InventoryContainsAllIngredient(items);
|
2018-12-25 14:02:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|