コード例 #1
0
        /// <summary>Intended to be invoked whenever the player inserts materials into a machine that requires inputs, such as when placing copper ore into a furnace.</summary>
        private static void OnInputsInserted(PerformObjectDropInData PODIData)
        {
            if (PODIData == null || PODIData.CurrentHeldObject == null || PODIData.Input == null || !Context.IsMainPlayer)
            {
                return;
            }

            SObject Machine = PODIData.Machine;

            if (Machine == null || !Machine.TryGetCombinedQuantity(out int CombinedQuantity))
            {
                return;
            }

            //  Compute the maximum multiplier we can apply to the input and output based on how many more of the inputs the player has
            int    PreviousInputQuantityUsed = PODIData.PreviousInputQuantity - PODIData.CurrentInputQuantity;
            double MaxMultiplier             = PreviousInputQuantityUsed == 0 ? PODIData.CurrentInputQuantity : Math.Abs(PODIData.PreviousInputQuantity * 1.0 / PreviousInputQuantityUsed);

            //  Modify the output
            int PreviousOutputStack = PODIData.CurrentHeldObjectQuantity;
            int NewOutputStack      = ComputeModifiedStack(CombinedQuantity, MaxMultiplier, PreviousOutputStack, out double OutputEffect, out double DesiredNewOutputValue);

            PODIData.CurrentHeldObject.Stack = NewOutputStack;
            Machine.SetHasModifiedOutput(true);
            ModEntry.LogTrace(CombinedQuantity, PODIData.Machine, PODIData.Machine.TileLocation, "HeldObject.Stack", PreviousOutputStack, DesiredNewOutputValue, NewOutputStack, OutputEffect);

            //  Modify the input
            int    CurrentInputQuantityUsed;
            double InputEffect;
            double DesiredNewInputValue;

            if (PreviousInputQuantityUsed <= 0)
            {
                //  No clue why, but for some machines the game hasn't actually taken the input yet by the time Object.performObjectDropIn finishes.
                //  so assume the input amount was = to 1.
                CurrentInputQuantityUsed = ComputeModifiedStack(CombinedQuantity, MaxMultiplier, 1, out InputEffect, out DesiredNewInputValue) - 1 - Math.Abs(PreviousInputQuantityUsed);
            }
            else
            {
                CurrentInputQuantityUsed = ComputeModifiedStack(CombinedQuantity, MaxMultiplier, PreviousInputQuantityUsed, out InputEffect, out DesiredNewInputValue);
            }
            int NewInputStack = PODIData.PreviousInputQuantity - CurrentInputQuantityUsed;

            PODIData.Input.Stack = NewInputStack;
            if (NewInputStack <= 0)
            {
                if (PODIData.WasInputInInventory)
                {
                    PODIData.Farmer.removeItemFromInventory(PODIData.Input);
                }
                else
                {
                    PODIData.Input.Stack = 1; // Just a failsafe to avoid glitched out Items with zero quantity, such as if the input came from a chest due to the Automate mod
                }
            }
        }
コード例 #2
0
        private static void OnReadyForHarvest(SObject Machine)
        {
            if (Context.IsMainPlayer)
            {
                try
                {
                    if (Machine.heldObject.Value != null && Machine.TryGetCombinedQuantity(out int CombinedQuantity) && !Machine.HasModifiedOutput())
                    {
                        int PreviousOutputStack = Machine.heldObject.Value.Stack;

                        double OutputEffect    = ModEntry.UserConfig.ComputeProcessingPower(CombinedQuantity);
                        double DesiredNewValue = PreviousOutputStack * OutputEffect;
                        int    NewOutputStack  = RNGHelpers.WeightedRound(DesiredNewValue);

                        Machine.heldObject.Value.Stack = NewOutputStack;
                        ModEntry.LogTrace(CombinedQuantity, Machine, Machine.TileLocation, "HeldObject.Stack", PreviousOutputStack, DesiredNewValue, NewOutputStack, OutputEffect);
                    }
                }
                finally { Machine.SetHasModifiedOutput(false); }
            }
        }
コード例 #3
0
        /// <summary>Intended to be invoked whenever the player inserts materials into a machine that requires inputs, such as when placing copper ore into a furnace.</summary>
        private static void OnInputsInserted(PerformObjectDropInData PODIData)
        {
            if (PODIData == null || PODIData.CurrentHeldObject == null || PODIData.Input == null)
            {
                return;
            }

            bool IsCurrentPlayer = (!Context.IsMultiplayer && !Context.IsSplitScreen) || PODIData.Farmer.UniqueMultiplayerID == Game1.player.UniqueMultiplayerID;

            if (!IsCurrentPlayer)
            {
                return;
            }

            SObject Machine = PODIData.Machine;

            if (!ModEntry.UserConfig.ShouldModifyInputsAndOutputs(Machine) || !Machine.TryGetCombinedQuantity(out int CombinedQuantity))
            {
                return;
            }

            int SecondaryInputQuantityAvailable = int.MaxValue;

            if (PODIData.Input.IsOre() && PODIData.Farmer != null && ModEntry.UserConfig.FurnaceMultiplyCoalInputs)
            {
                SecondaryInputQuantityAvailable = PODIData.Farmer.Items.Where(x => x != null && x.IsCoal()).Sum(x => x.Stack);
            }

            //  Compute the maximum multiplier we can apply to the input and output based on how many more of the inputs the player has
            int    PreviousInputQuantityUsed = PODIData.PreviousInputQuantity - PODIData.CurrentInputQuantity;
            double MaxMultiplier             = Math.Min(SecondaryInputQuantityAvailable, PreviousInputQuantityUsed == 0 ?
                                                        PODIData.CurrentInputQuantity :
                                                        Math.Abs(PODIData.PreviousInputQuantity * 1.0 / PreviousInputQuantityUsed));

            //  Modify the output
            int PreviousOutputStack = PODIData.CurrentHeldObjectQuantity;
            int NewOutputStack      = ComputeModifiedStack(CombinedQuantity, MaxMultiplier, PreviousOutputStack, out double OutputEffect, out double DesiredNewOutputValue);

            PODIData.CurrentHeldObject.Stack = NewOutputStack;
            Machine.SetHasModifiedOutput(true);
            ModEntry.LogTrace(CombinedQuantity, PODIData.Machine, PODIData.Machine.TileLocation, "HeldObject.Stack", PreviousOutputStack, DesiredNewOutputValue, NewOutputStack, OutputEffect);

            //  Modify the input
            int    CurrentInputQuantityUsed;
            double InputEffect;
            double DesiredNewInputValue;

            if (PreviousInputQuantityUsed <= 0)
            {
                //  No clue why, but for some machines the game hasn't actually taken the input yet by the time Object.performObjectDropIn finishes.
                //  so assume the input amount was = to 1.
                CurrentInputQuantityUsed = ComputeModifiedStack(CombinedQuantity, MaxMultiplier, 1, out InputEffect, out DesiredNewInputValue) - 1 - Math.Abs(PreviousInputQuantityUsed);
            }
            else
            {
                CurrentInputQuantityUsed = ComputeModifiedStack(CombinedQuantity, MaxMultiplier, PreviousInputQuantityUsed, out InputEffect, out DesiredNewInputValue);
            }
            int NewInputStack = PODIData.PreviousInputQuantity - CurrentInputQuantityUsed;

            PODIData.Input.Stack = NewInputStack;
            if (NewInputStack <= 0)
            {
                if (PODIData.WasInputInInventory)
                {
                    PODIData.Farmer.removeItemFromInventory(PODIData.Input);
                }
                else
                {
                    PODIData.Input.Stack = 1; // Just a failsafe to avoid glitched out Items with zero quantity, such as if the input came from a chest due to the Automate mod
                }
            }

            if (PODIData.Input.IsOre() && PODIData.Farmer != null && ModEntry.UserConfig.FurnaceMultiplyCoalInputs)
            {
                int RemainingCoalToConsume = RNGHelpers.WeightedRound(OutputEffect) - 1; // 1 coal was already automatically consumed by the vanilla function
                for (int i = 0; i < PODIData.Farmer.Items.Count; i++)
                {
                    Item CurrentItem = PODIData.Farmer.Items[i];
                    if (CurrentItem != null && CurrentItem.IsCoal())
                    {
                        int AmountToConsume = Math.Min(CurrentItem.Stack, RemainingCoalToConsume);
                        CurrentItem.Stack      -= AmountToConsume;
                        RemainingCoalToConsume -= AmountToConsume;

                        if (CurrentItem.Stack <= 0)
                        {
                            PODIData.Farmer.removeItemFromInventory(i);
                        }

                        if (RemainingCoalToConsume <= 0)
                        {
                            break;
                        }
                    }
                }
            }
        }