Esempio n. 1
0
    /// <summary>
    /// Called when the end target of out path has changed.
    /// We iterate through the points in our path, and check the distance to
    /// the new target. If the distance is lower, we re-calculate the path
    /// </summary>
    /// <param name="target"></param>
    /// <returns></returns>
    public bool UpdateTarget(Vec2i target, EntityPathFinder epf)
    {
        //If we are currently calculating a path
        if (epf.IsRunning)
        {
            if (epf.Target == target)
            {
                //Target is still valid, so we return true
                return(true);
            }
            else
            {
                //Debug.LogError("Im not sure what to do here?");
                epf.ForceStop();
                return(false);
            }
        }
        if (epf.FoundPath)
        {
        }

        //Check the finder has the same target.
        if (epf.Target == target)
        {
            return(true);
        }

        Vec2i closestPoint      = Path[index];
        int   closestDist       = Vec2i.QuickDistance(target, closestPoint);
        int   closestPointIndex = index;

        for (int i = index; i < Path.Count; i += 2)
        {
            int thisDist = Vec2i.QuickDistance(target, Path[i]) + (i - index);
            if (thisDist < closestDist)
            {
                closestDist       = thisDist;
                closestPoint      = Path[i];
                closestPointIndex = i;
            }
        }
        if (closestPointIndex + 1 < Path.Count)
        {
            int trimAmount = Path.Count - 1 - (closestPointIndex + 1);
            if (trimAmount > 0)
            {
                //Remove end of path
                Path.RemoveRange(closestPointIndex + 1, Path.Count - 1 - (closestPointIndex + 1));
            }
        }
        if (index > 1)
        {
            Path.RemoveRange(0, index - 1);
            index = 0;
        }



        /*
         * List<Vec2i> extension = GameManager.PathFinder.GeneratePath(closestPoint, target);
         * if (extension.Count == 0)
         *  return false;
         *
         *
         * Path.AddRange(extension);
         *
         * Target = target;
         */
        return(true);
    }
Esempio n. 2
0
    private void OnDrawGizmos()
    {
        if (doPath)
        {
            if (epf == null)
            {
                epf = new EntityPathFinder(GameManager.PathFinder);
            }
            Path_ = null;
            if (epf.IsRunning)
            {
                epf.ForceStop();
            }
            else
            {
                epf.FindPath(Player.TilePos, Player.TilePos + GameManager.RNG.RandomVec2i(-100, 100));
            }

            /*Debug.Log("calculating path");
             * GenerationRandom genRan = new GenerationRandom(Time.frameCount);
             * Path_ = GameManager.PathFinder.GeneratePath(Vec2i.FromVector3(Player.Position), Vec2i.FromVector3(Player.Position) + genRan.RandomVec2i(-100, 100));
             */
            return;

            Settlement t = GameManager.TestSettle;
            if (t == null)
            {
                return;
            }
            int   curDist = -1;
            Vec2i pPos    = Vec2i.FromVector2(Player.Position2);
            foreach (SettlementPathNode pn in t.tNodes)
            {
                if (pn == null)
                {
                    continue;
                }

                if (NearestNode == null)
                {
                    NearestNode = pn;
                    curDist     = Vec2i.QuickDistance(pn.Position + t.BaseCoord, pPos);
                }
                else
                {
                    if (Vec2i.QuickDistance(pn.Position + t.BaseCoord, pPos) < curDist)
                    {
                        curDist     = Vec2i.QuickDistance(pn.Position + t.BaseCoord, pPos);
                        NearestNode = pn;
                    }
                }
            }

            path = new List <SettlementPathNode>();
            if (SettlementPathFinder.SettlementPath(NearestNode, t.IMPORTANT, out path, debug: true))
            {
                Debug.Log("Path found!");
            }

            Debug.Log("Path len: " + path.Count);
        }
        if (Path_ == null && epf != null)
        {
            if (epf.IsComplete())
            {
                Path_ = epf.GetPath();
            }
        }


        if (Path_ != null && Path_.Count > 1)
        {
            Color old = Gizmos.color;
            Gizmos.color = Color.yellow;
            //Debug.Log(Vec2i.ToVector3(path[0].Position + t.BaseCoord));
            //Gizmos.DrawCube(Vec2i.ToVector3(path[0].Position + GameManager.TestSettle.BaseCoord), Vector3.one * 2);

            Gizmos.DrawCube(Vec2i.ToVector3(Path_[0]), Vector3.one * 5);
            Gizmos.DrawCube(Vec2i.ToVector3(Path_[Path_.Count - 1]), Vector3.one * 5);

            for (int i = 0; i < Path_.Count - 1; i++)
            {
                Gizmos.DrawLine(Vec2i.ToVector3(Path_[i]), Vec2i.ToVector3(Path_[i + 1]));
                Gizmos.DrawCube(Vec2i.ToVector3(Path_[i]), Vector3.one * 0.5f);
            }
            Gizmos.color = old;
        }
        return;

        if (NearestNode == null)
        {
            Settlement t = GameManager.TestSettle;
            if (t == null)
            {
                return;
            }
            int   curDist = -1;
            Vec2i pPos    = Vec2i.FromVector2(Player.Position2);
            foreach (SettlementPathNode pn in t.tNodes)
            {
                if (pn == null)
                {
                    continue;
                }

                if (NearestNode == null)
                {
                    NearestNode = pn;
                    curDist     = Vec2i.QuickDistance(pn.Position + t.BaseCoord, pPos);
                }
                else
                {
                    if (Vec2i.QuickDistance(pn.Position + t.BaseCoord, pPos) < curDist)
                    {
                        curDist     = Vec2i.QuickDistance(pn.Position + t.BaseCoord, pPos);
                        NearestNode = pn;
                    }
                }
            }

            path = new List <SettlementPathNode>();
            if (SettlementPathFinder.SettlementPath(NearestNode, t.IMPORTANT, out path, debug: true))
            {
                Debug.Log("Path found!");
            }

            Debug.Log("Path len: " + path.Count);
        }
    }
Esempio n. 3
0
    public bool GeneratePath(Vec2i target)
    {
        //if no target, null previous paths and return.
        if (target == null)
        {
            Debug.Log("Target is null - no path");
            EntityPathTarget = null;
            EntityPath       = null;
            return(false);
        }

        if (EPF == null)
        {
            //TODO - get free path finder from parent
            EPF = new EntityPathFinder(GameManager.PathFinder);
        }

        Vec2i tilePos = Vec2i.FromVector3(Entity.Position);

        Debug.Log("Attempting path from " + tilePos + " to " + target);

        /*if(GameManager.PathFinder.NodeValue(tilePos) > 100)
         * {
         *  tilePos = Vec2i.FromVector3(Entity.Position + new Vector3(0.5f, 0, 0.5f));
         *  if(GameManager.PathFinder.NodeValue(tilePos) > 100)
         *  {
         *      Debug.Log("hm");
         *  }
         * }*/
        //if we have no path
        if (EntityPath == null)
        {
            //We check if one is being generated
            if (!EPF.IsRunning)
            {
                //If not, we start generating it
                EPF.FindPath(tilePos, target);
                return(false);
            }

            //Check if the path finder has completed its work
            if (EPF.IsComplete())
            {
                //If the path is found, we set it and return true
                if (EPF.FoundPath)
                {
                    EntityPathTarget = target;
                    EntityPath       = new EntityPath(EPF.GetPath());
                    return(true);
                }
                else
                {
                    Debug.Log("Path from " + tilePos + " to " + target + " not found");

                    return(false);
                }
            }
            return(false);

            /*
             * //If there is no path, Attempt to generate it
             * List<Vec2i> path = GameManager.PathFinder.GeneratePath(tilePos, target);
             * if (path==null || path.Count == 0)
             * {
             *  Debug.Log("Path from " + tilePos + " to " + target + " not found");
             *  return false;
             * }
             * EntityPathTarget = target;
             * EntityPath = new EntityPath(path);
             * return true;*/
        }
        else if (EntityPath.Target == target)
        {//If the current path has the same target, return true
            return(true);
        }
        else
        {
            if (Vec2i.QuickDistance(EntityPath.Target, target) < 5)
            {
                //If the path exists but doesn't have the same target, but targets are close, we attempt to re-calculate the path
                return(EntityPath.UpdateTarget(target, EPF));
            }
            else
            {
                EntityPath = null;
                //We check if one is being generated
                if (!EPF.IsRunning)
                {
                    //If not, we start generating it
                    EPF.FindPath(tilePos, target);
                    return(false);
                }/*
                  * List<Vec2i> newPath = GameManager.PathFinder.GeneratePath(tilePos, target);
                  * if(newPath==null || newPath.Count == 0)
                  * {
                  * Debug.Log("No path found from " + tilePos + " to " + target + " for entity " + Entity);
                  * return false;
                  * }
                  * EntityPath = new EntityPath(newPath);
                  * return true;*/
            }
        }
        return(false);
    }