Exemplo n.º 1
0
        public void SpawnSoftObject(SoftObject softObject, Cell target, int amount)
        {
            if (softObject == null)
            {
                return;
            }

            List <SoftObject> split = softObject.Split();

            for (int i = 0; i < split.Count; i++)
            {
                SoftObject so       = split[i];
                Cell       nextCell = NearestCell(target, ValidSoftObjectPlacementPredicate(so));

                if (nextCell.SoftObject != null)
                {
                    int remainder = nextCell.SoftObject.IncreaseCount(so.Count, true);
                    if (remainder > 0)
                    {
                        split.Add(so.Clone(remainder));
                    }
                }
                else
                {
                    nextCell.SoftObject = so;
                }
            }
        }
Exemplo n.º 2
0
 public SoftObject NextRequirement()
 {
     return(buildRequirements.FirstOrDefault((requirement) => {
         SoftObject match = deposit.Find((SoftObject softObject) => {
             return softObject.Type == requirement.Type;
         });
         if (match == null || match.Count < requirement.Count)
         {
             return true;
         }
         return false;
     }));
 }
Exemplo n.º 3
0
 protected Predicate <Cell> ValidSoftObjectPlacementPredicate(SoftObject target)
 {
     return((cell) => {
         SoftObject placement = cell.SoftObject;
         if (placement == null)
         {
             return true;
         }
         if (placement != null && target != null && target.Type == placement.Type && placement.Count < placement.StackCount)
         {
             return true;
         }
         return false;
     });
 }
Exemplo n.º 4
0
        // Methods
        public void DepositSoftObject(SoftObject softObject)
        {
            SoftObject requirement = buildRequirements.FirstOrDefault((obj) => { return(obj.Type == softObject.Type); });
            SoftObject existing    = deposit.FirstOrDefault((obj) => { return(obj.Type == softObject.Type); });

            if (requirement == null || (existing != null && requirement.Count - existing.Count >= softObject.Count))
            {
                Console.WriteLine("[Inventory Leak] Depositing object when none is needed!");
                return;
            }

            if (existing != null)
            {
                existing.IncreaseCount(softObject.Count);
            }
            else
            {
                deposit.Add(softObject.Clone());
            }
        }
Exemplo n.º 5
0
        void UpdateJob(float deltaTime)
        {
            // Find Job
            if (currentJob == null && IsIdling)
            {
                jobSearchCooldown -= deltaTime;
                if (jobSearchCooldown > 0)
                {
                    return;
                }

                FindJob();

                if (currentJob == null)
                {
                    jobSearchCooldown = 0.5f;
                    //destinationCell = currentCell;
                    return;
                }
            }

            if (!IsIdling && currentJob == null)
            {
                return;
            }

            if (!currentJob.MeetsBuildRequirements())
            {
                SoftObject next = currentJob.NextRequirement();
                if (carrying != null)                                                                                    // We are carrying something
                {
                    if (carrying.Type == next.Type)                                                                      // We are carrying the thing that's needed
                    {
                        if (currentCell == currentJob.cell || currentCell.GetNeighbors(false).Contains(currentJob.cell)) // We are in the job cell, TODO: understand / fix everything in this branch not sure it works with count and stackCount for soft objects
                        {
                            currentJob.DepositSoftObject(carrying);
                            carrying.ReduceCount(next.Count);
                            if (carrying.Count == 0)
                            {
                                carrying = null;
                            }
                            else
                            {
                                world.DropSoftObject(this, currentCell, Carrying.Count);
                            }
                        }
                        else                             // We aren't in the job cell, keep moving towards the destination
                        {
                            destinationCell = currentJob.cell;
                            return;
                        }
                    }
                    else                         // We don't have required inventory, drop current thing
                    {
                        world.DropSoftObject(this, currentCell, Carrying.Count);
                    }
                }
                else                                                                                // We aren't carrying anything
                {
                    if (currentCell.SoftObject != null && currentCell.SoftObject.Type == next.Type) // We're standing on top of a cell with required inventory
                    {
                        world.PickupSoftObject(this, currentCell, currentCell.SoftObject.Count);
                    }
                    else                             // Need to walk to nearest required inventory
                    {
                        if (currentCell != nextCell) // We're moving somewhere rn so keep doing that
                        {
                            return;
                        }

                        Cell lastCell = pathfinder == null ? null : pathfinder.LastCell();
                        if (pathfinder == null || lastCell == null || lastCell.SoftObject == null || lastCell.SoftObject.Type != next.Type)                           // we ain't going somewhere useful so recalculate
                        {
                            PathfinderAStar newPathfinder = world.PathForNearestSoftObject(next.Type, currentCell);
                            if (newPathfinder == null || newPathfinder.Length == 0)
                            {
                                Console.WriteLine("Abandoning because pathfinder unsuccessful for soft object search");
                                AbandonJob();
                                return;
                            }
                            pathfinder      = newPathfinder;
                            destinationCell = pathfinder.LastCell();
                            NextCell        = pathfinder.Dequeue();
                        }
                    }
                }
                return;
            }

            // Work Job
            // destinationCell = currentJob.cell;
            if (currentCell == currentJob.cell || currentCell.GetNeighbors(false).Contains(currentJob.cell))
            {
                currentJob.Progress(deltaTime, this);
            }
        }
Exemplo n.º 6
0
 public SoftObject(SoftObject clone, int amount)
 {
     type  = clone.Type;
     Count = amount;
 }