/********* ** Private methods *********/ /// <summary>Try to add an item to the input queue, and adjust its stack size accordingly.</summary> /// <param name="item">The item stack to add.</param> /// <returns>Returns whether any items were taken from the stack.</returns> private bool TryAddInput(ITrackedStack item) { // nothing to add if (item.Count <= 0) { return(false); } // clean up input bin this.Input.clearNulls(); // try adding to input int originalSize = item.Count; IList <Item> slots = this.Input.items; int maxStackSize = this.GetMaxInputStackSize(item.Sample); for (int i = 0; i < Chest.capacity; i++) { // done if (item.Count <= 0) { break; } // add to existing slot if (slots.Count > i) { Item slot = slots[i]; if (item.Sample.canStackWith(slot) && slot.Stack < maxStackSize) { var sample = item.Sample.getOne(); sample.Stack = Math.Min(item.Count, maxStackSize - slot.Stack); // the most items we can add to the stack (in theory) int actualAdded = sample.Stack - slot.addToStack(sample); // how many items were actually added to the stack item.Reduce(actualAdded); } continue; } // add to new slot slots.Add(item.Take(Math.Min(item.Count, maxStackSize))); } return(item.Count < originalSize); }
/// <summary>Store an item stack.</summary> /// <param name="stack">The item stack to store.</param> /// <remarks>If the storage can't hold the entire stack, it should reduce the tracked stack accordingly.</remarks> public void Store(ITrackedStack stack) { if (stack.Count <= 0 || this.Chest.SpecialChestType == Chest.SpecialChestTypes.AutoLoader) { return; } IList <Item> inventory = this.GetInventory(); // try stack into existing slot foreach (Item slot in inventory) { if (slot != null && stack.Sample.canStackWith(slot)) { Item sample = stack.Sample.getOne(); sample.Stack = stack.Count; int added = stack.Count - slot.addToStack(sample); stack.Reduce(added); if (stack.Count <= 0) { return; } } } // try add to empty slot int capacity = this.Chest.GetActualCapacity(); for (int i = 0; i < capacity && i < inventory.Count; i++) { if (inventory[i] == null) { inventory[i] = stack.Take(stack.Count); return; } } // try add new slot if (inventory.Count < capacity) { inventory.Add(stack.Take(stack.Count)); } }
/// <summary>Store an item stack.</summary> /// <param name="stack">The item stack to store.</param> /// <remarks>If the storage can't hold the entire stack, it should reduce the tracked stack accordingly.</remarks> public void Store(ITrackedStack stack) { if (stack.Count <= 0) { return; } IList <Item> inventory = this.Chest.items; // try stack into existing slot foreach (Item slot in inventory) { if (slot != null && stack.Sample.canStackWith(slot)) { int added = stack.Count - slot.addToStack(stack.Count); stack.Reduce(added); if (stack.Count <= 0) { return; } } } // try add to empty slot for (int i = 0; i < Chest.capacity && i < inventory.Count; i++) { if (inventory[i] == null) { inventory[i] = stack.Take(stack.Count); return; } } // try add new slot if (inventory.Count < Chest.capacity) { inventory.Add(stack.Take(stack.Count)); } }
/// <inheritdoc/> public bool Automate_SetInput(IStorage input, MassProductionMachineDefinition mpm, IMachine originalMachine, SObject originalMachineObject) { if (mpm != null) { try { ITrackedStack crop = null; InputInfo cropInfo = null; int inputQuantity = 0; foreach (ITrackedStack item in input.GetItems()) { if (IsValidCrop(item)) { InputInfo inputInfo = new InputInfo() { ID = item.Sample.ParentSheetIndex, Name = item.Sample.Name, Quality = 0, IsFuel = false, BaseQuantity = 1 }; if (item.Sample is SObject obj) { inputInfo.Quality = obj.Quality; } inputQuantity = mpm.Settings.CalculateInputRequired(inputInfo); if (item.Count >= inputQuantity) { crop = item; cropInfo = inputInfo; break; } } } if (crop != null) { crop.Reduce(inputQuantity); int seedID = SEED_LOOKUP[crop.Sample.ParentSheetIndex]; Random random = new Random((int)Game1.stats.DaysPlayed + (int)Game1.uniqueIDForThisGame / 2 + (int)originalMachineObject.TileLocation.X + (int)originalMachineObject.TileLocation.Y * 77 + Game1.timeOfDay); int outputID = seedID; int outputQuantity = mpm.Settings.CalculateOutputProduced(random.Next(1, 4), cropInfo); if (random.NextDouble() < 0.005) { outputID = 499; outputQuantity = mpm.Settings.CalculateOutputProduced(1, cropInfo); } else if (random.NextDouble() < 0.02) { outputID = 770; outputQuantity = mpm.Settings.CalculateOutputProduced(random.Next(1, 5), cropInfo); } originalMachineObject.heldObject.Value = new SObject(outputID, outputQuantity); originalMachineObject.MinutesUntilReady = mpm.Settings.CalculateTimeRequired(20); return(true); } } catch (Exception e) { ModEntry.Instance.Monitor.Log($"{e}", StardewModdingAPI.LogLevel.Error); } } else { return(originalMachine.SetInput(input)); } return(false); }