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
|
|
|
|
{
|
|
|
|
public class Recipe
|
|
|
|
{
|
|
|
|
public Dictionary<Item, int> ingredients;
|
|
|
|
public Dictionary<Item, int> outputs;
|
|
|
|
|
|
|
|
private Item displayItem;
|
|
|
|
|
|
|
|
public Item DisplayItem
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
get => this.displayItem ?? this.outputs.ElementAt(0).Key;
|
|
|
|
set => this.displayItem = value;
|
2018-12-25 14:02:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public string outputDescription;
|
|
|
|
public string outputName;
|
|
|
|
|
|
|
|
|
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>
|
2018-12-30 18:00:05 +08:00
|
|
|
public Recipe(Dictionary<Item, int> inputs, KeyValuePair<Item, int> output)
|
2018-12-25 14:02:25 +08:00
|
|
|
{
|
|
|
|
this.ingredients = inputs;
|
|
|
|
this.DisplayItem = output.Key;
|
|
|
|
this.outputDescription = output.Key.getDescription();
|
|
|
|
this.outputName = output.Key.DisplayName;
|
2018-12-30 18:00:05 +08:00
|
|
|
this.outputs = new Dictionary<Item, int>
|
|
|
|
{
|
|
|
|
[output.Key] = output.Value
|
|
|
|
};
|
2018-12-25 14:02:25 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
public Recipe(Dictionary<Item, int> inputs, Dictionary<Item, int> outputs, string OutputName, string OutputDescription, Item DisplayItem = null)
|
2018-12-25 14:02:25 +08:00
|
|
|
{
|
|
|
|
this.ingredients = inputs;
|
|
|
|
this.outputs = outputs;
|
|
|
|
this.outputName = OutputName;
|
|
|
|
this.outputDescription = OutputDescription;
|
|
|
|
this.DisplayItem = DisplayItem;
|
|
|
|
}
|
|
|
|
|
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>
|
|
|
|
public bool PlayerContainsIngredient(KeyValuePair<Item, int> 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>
|
2018-12-25 14:02:25 +08:00
|
|
|
public bool InventoryContainsAllIngredient(List<Item> items)
|
|
|
|
{
|
|
|
|
foreach (KeyValuePair<Item, int> 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>
|
|
|
|
public bool InventoryContainsIngredient(List<Item> items, KeyValuePair<Item, int> pair)
|
2018-12-25 14:02:25 +08:00
|
|
|
{
|
|
|
|
foreach (Item i in items)
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
if (i != null && this.ItemEqualsOther(i, pair.Key) && pair.Value == 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
|
|
|
}
|
|
|
|
|
|
|
|
public void consume(ref List<Item> from)
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
if (!this.InventoryContainsAllIngredient(from))
|
|
|
|
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>();
|
2018-12-30 18:00:05 +08:00
|
|
|
foreach (KeyValuePair<Item, int> 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;
|
|
|
|
if (this.ItemEqualsOther(item, pair.Key))
|
2018-12-25 14:02:25 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
if (item.Stack == pair.Value)
|
|
|
|
removalList.Add(item); //remove the item
|
2018-12-25 14:02:25 +08:00
|
|
|
else
|
2018-12-30 18:00:05 +08:00
|
|
|
item.Stack -= pair.Value; //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;
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
public void produce(ref List<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);
|
|
|
|
foreach (KeyValuePair<Item, int> pair in this.outputs)
|
2018-12-25 14:23:25 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
Item item = pair.Key.getOne();
|
|
|
|
item.addToStack(pair.Value - 1);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
public void craft(ref List<Item> from, ref List<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
|
|
|
}
|
|
|
|
|
|
|
|
public void craft()
|
|
|
|
{
|
|
|
|
List<Item> playerItems = Game1.player.Items.ToList();
|
2018-12-25 14:23:25 +08:00
|
|
|
List<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.
|
2018-12-25 14:02:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool PlayerCanCraft()
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
return this.PlayerContainsAllIngredients();
|
2018-12-25 14:02:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool CanCraft(List<Item> items)
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
return this.InventoryContainsAllIngredient(items);
|
2018-12-25 14:02:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|