예제 #1
0
        private void HaulingJobForInputs(ProductionChain prodChain)
        {
            int  numHaulingJobs = 0;
            bool isProcessing   = InputProcessed.ToInt() > 0;

            //// for all inputs in production chain
            foreach (Item reqInputItem in prodChain.Input)
            {
                if (isProcessing && !reqInputItem.HasHopper)
                {
                    continue;
                }

                //// if there is no hauling job for input object type, create one
                Job    furnJob;
                string requiredType       = reqInputItem.ObjectType;
                bool   existingHaulingJob = ParentFurniture.Jobs.HasJobWithPredicate(x => x.RequestedItems.ContainsKey(requiredType), out furnJob);

                if (existingHaulingJob)
                {
                    numHaulingJobs++;
                }
                else
                {
                    Tile inTile = World.Current.GetTileAt(
                        ParentFurniture.Tile.X + reqInputItem.SlotPosX,
                        ParentFurniture.Tile.Y + reqInputItem.SlotPosY,
                        ParentFurniture.Tile.Z);

                    // create job for desired input resource
                    string desiredInv    = reqInputItem.ObjectType;
                    int    desiredAmount = reqInputItem.Amount;

                    if (reqInputItem.HasHopper)
                    {
                        desiredAmount = PrototypeManager.Inventory.Get(desiredInv).MaxStackSize;
                    }

                    if (inTile.Inventory != null && inTile.Inventory.Type == reqInputItem.ObjectType &&
                        inTile.Inventory.StackSize <= desiredAmount)
                    {
                        desiredAmount = desiredAmount - inTile.Inventory.StackSize;
                    }

                    if (desiredAmount > 0)
                    {
                        Job job = new Job(
                            inTile,
                            null,           // beware: passed jobObjectType is expected Furniture only !!
                            null,
                            0.4f,
                            new RequestedItem[] { new RequestedItem(desiredInv, desiredAmount, desiredAmount) },
                            Job.JobPriority.High,
                            "hauling",
                            false,
                            false,
                            false);

                        job.Description  = string.Format("Hauling '{0}' to '{1}'", desiredInv, ParentFurniture.GetName());
                        job.OnJobWorked += PlaceInventoryToWorkshopInput;
                        ParentFurniture.Jobs.Add(job);
                        numHaulingJobs++;
                    }
                }
            }

            InputHaulingJobsCount.SetValue(numHaulingJobs);
        }
예제 #2
0
        public override void FixedFrequencyUpdate(float deltaTime)
        {
            // if there is enough input, do the processing and store item to output
            // - remove items from input
            // - add param to reflect factory can provide output (has output inside)
            //   - as output will be produced after time, it is possible that output spot can be ocupied meanwhile
            // - process for specified time
            // - if output slot is free, provide output (if not, keep output 'inside' factory)
            if (ParentFurniture.IsBeingDestroyed)
            {
                return;
            }

            if (RunConditions != null && AreParameterConditionsFulfilled(RunConditions.ParamConditions) == false)
            {
                IsRunning.SetValue(false);
                return;
            }

            float efficiency = 1f;

            if (Efficiency != null)
            {
                efficiency = RetrieveFloatFor(Efficiency, ParentFurniture);
            }

            string curSetupChainName = CurrentProductionChainName.ToString();

            if (!string.IsNullOrEmpty(curSetupChainName))
            {
                ProductionChain prodChain = GetProductionChainByName(curSetupChainName);

                // if there is no processing in progress
                if (InputProcessed.ToInt() == 0)
                {
                    // check input slots for input inventory
                    List <KeyValuePair <Tile, int> > flaggedForTaking = CheckForInventoryAtInput(prodChain);

                    // if all the input requirements are ok, you can start processing:
                    if (flaggedForTaking.Count == prodChain.Input.Count)
                    {
                        // consume input inventory
                        ConsumeInventories(flaggedForTaking);

                        InputProcessed.SetValue(1); // check if it can be bool
                        IsRunning.SetValue(true);

                        // reset processing timer and set max time for processing for this prod. chain
                        CurrentProcessingTime.SetValue(0f);
                        MaxProcessingTime.SetValue(prodChain.ProcessingTime);
                        HasAllNeededInputInventory.SetValue(true);
                    }
                    else
                    {
                        HasAllNeededInputInventory.SetValue(false);
                    }
                }
                else
                {
                    // processing is in progress
                    CurrentProcessingTime.ChangeFloatValue(deltaTime * efficiency);
                    IsRunning.SetValue(true);

                    if (CurrentProcessingTime.ToFloat() >= MaxProcessingTime.ToFloat())
                    {
                        List <TileObjectTypeAmount> outPlacement = CheckForInventoryAtOutput(prodChain);

                        // if output placement was found for all products, place them
                        if (outPlacement.Count == 0 || outPlacement.Count == prodChain.Output.Count)
                        {
                            PlaceInventories(outPlacement);
                            //// processing done, can fetch input for another processing
                            InputProcessed.SetValue(0);
                            IsRunning.SetValue(false);
                            CurrentProcessingTime.SetValue(0f);
                        }
                    }
                }
            }
        }