using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; class Inventory { private int Weight = 0; private int maxWeight = 280; private int[] totalHarvested = new int[11]; private int[] totalFertilizerUsed = new int[8]; private Cargo cargo = new Cargo(); private Cargo itemStorage = new Cargo(); private int Refills = 0; // Item list by index: // --------------------------------------- // (0) Fertilizers // 1: 10-26-26 // 2: 14-35-14 // 3: 17-17-17 // 4: 20-20 // 5: 28-28 // 6: DAP // 7: Urea // (1) Crops: // 1: Barley // 2: Cotton // 3: Ground Nuts // 4: Maize // 5: Millets // 6: Oil Seeds // 7: Paddy // 8: Pulses // 9: Sugarcane // 10: Tobacco // 11: Wheat // // Number - 1 to get the index in the array // Adds all the items to the houses inventory for pickup at the houses position public void initInventorySystem() { itemStorage.initStorageItems(); } public int getWeight() { return Weight; } // Adds item to the tractor. // 0 = Fertilizer // 1 = Crops public bool addItem(int newItemIndex, int Type) { Items newItem; if (Type == 1) { totalHarvested[newItemIndex]++; } newItem = itemStorage.getItemByIndex(newItemIndex, Type); if (maxWeight >= Weight + newItem.getWeight()) { cargo.addItem(newItem, Type); Weight = Weight + newItem.getWeight(); return true; } else { return false; } } // Uses an item from the tractor. // 0 = Fertilizer // 1 = Crops public bool useItem(int Index, int Type, bool isClearing) { if (cargo.itemExists(Index, Type)) { if (Type == 0 && !isClearing) { totalFertilizerUsed[Index]++; } removeItem(Index, Type); return true; } return false; } public int getMaxWeight() { return maxWeight; } // Checks whether or not the tractor has the item in its inventory. // 0 = Fertilizer // 1 = Crops public bool itemExists(int Index, int Type) { return cargo.itemExists(Index, Type); } public void removeItem(int Index, int Type) { Weight = Weight - itemStorage.getItemByIndex(Index, Type).getWeight(); cargo.removeItem(Index, Type); } public Cargo getPredefinedItems() { return itemStorage; } public void clearInventory() { for (int i = 0; i <= 6; i++) while (useItem(i, 0, true)) ; for (int i = 0; i <= 10; i++) while (useItem(i, 1, true)) ; } public void fillWithFertilizer() { Refills++; int fert_amount = 7; float weightPerFertilizer = maxWeight / fert_amount; float min = weightPerFertilizer / 10; float max = (2 * weightPerFertilizer) - min; float expectedUse = Refills * weightPerFertilizer; double[] useFactor = new double[fert_amount]; for (int i = 0; i < fert_amount; i++) { if (Refills <= 1) useFactor[i] = weightPerFertilizer; else if (totalFertilizerUsed[i] / expectedUse + 0.85f > 1.0f) useFactor[i] = Math.Round(Math.Pow((totalFertilizerUsed[i] / expectedUse) + 0.50f, 0.6f + Math.Min(Refills / 6.0f, 1.0f)) * weightPerFertilizer); else useFactor[i] = Math.Round(Math.Pow((totalFertilizerUsed[i] / expectedUse) + 0.50f, 1.0f + Math.Min(Refills / 6.0f, 1.8f)) * weightPerFertilizer); if (useFactor[i] < min) useFactor[i] = min; else if (useFactor[i] > max) useFactor[i] = max; } int j = 0; bool change = false; while (getWeight() < getMaxWeight() - fert_amount) { if (useFactor[j] > 0) { addItem(j, 0); useFactor[j]--; change = true; } j++; if (j >= fert_amount) { if (!change) break; else { j = 0; change = false; } } } int m = 0; while (getWeight() < getMaxWeight()- fert_amount) { if (m > 6) m = 0; addItem(m, 0); m++; } } public bool isMissingFertilizer() { for (int i = 0; i <= 6; i++) { if (!itemExists(i, 0)) return true; } return false; } public void printItems(Input input, SpriteBatch spriteBatch, SpriteFont Bold) { spriteBatch.DrawString(Bold, "Crops: ", new Vector2(470, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 2), Color.DarkRed); spriteBatch.DrawString(Bold, "Harvested:", new Vector2(600, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 2), Color.DarkRed); for (int i = 0; i < 11; i++) //Print Crops { spriteBatch.DrawString(Bold, cargo.getCount(i, 1) + " " + itemStorage.getItemByIndex(i, 1).getItemType(), new Vector2(470, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 20 * i + 22), Color.Teal); spriteBatch.DrawString(Bold, totalHarvested[i].ToString(), new Vector2(620, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 20 * i + 22), Color.Teal); } spriteBatch.DrawString(Bold, "Fertilizers: ", new Vector2(700, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 2), Color.DarkRed); spriteBatch.DrawString(Bold, "Used ", new Vector2(830, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 2), Color.DarkRed); for (int i = 0; i < 7; i++) //Print Fertilizers { spriteBatch.DrawString(Bold, cargo.getCount(i, 0) + " " + itemStorage.getItemByIndex(i, 0).getItemType(), new Vector2(700, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 20 * i + 22), Color.Teal); spriteBatch.DrawString(Bold, totalFertilizerUsed[i].ToString(), new Vector2(835, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 20 * i + 22), Color.Teal); } } }