Exemplo n.º 1
0
        PathfinderAStar FindPathToCurrentJob(bool withNeighbors)
        {
            PathfinderAStar newPathfinder = new PathfinderAStar(world, currentCell, destinationCell);             // May not be used if going to soft object first but need this for verifying that we can reach target area

            if (newPathfinder.Length == 0)
            {
                if (!withNeighbors)
                {
                    return(null);
                }
                foreach (Cell neighbor in destinationCell.GetNeighbors(false))
                {
                    newPathfinder = new PathfinderAStar(world, currentCell, neighbor);
                    if (newPathfinder != null && newPathfinder.Length > 0)
                    {
                        return(newPathfinder);
                    }
                }
                return(null);
            }
            return(newPathfinder);
        }
Exemplo n.º 2
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);
            }
        }