Ejemplo n.º 1
0
    private void Recalculate(bool changeTarget)
    {
        List <GameObject> agents = new List <GameObject>();

        agents.Add(gameObject);


        // Source to Target
        if (changeTarget || Target != null)
        {
            Destroy(Target);
            Target  = SpawnAgentsScript.Spawn(false, agents, color);
            replans = 0; // can't forget this
        }
        var edge = DrawVisibilityGraphScript.DrawVisibilityEdge(
            transform.position, Target.transform.position,
            Color.green, false, true, false, true
            );

        Path = new List <Vector2>();
        if (edge != null)
        {
            Path.Add(edge[1]);
        }

        if (Path.Count > 0)   // if there's a direct path between two points, no need to even compute A*, just walk the path!
        {
            return;
        }

        // Clone a fresh new graph
        Graph          = new VisibilityGraph();
        Graph.Vertices = OriginalGraph.ClonedVertices();
        Graph.AdjList  = OriginalGraph.ClonedAdjList();
        Graph.SetSource(transform.position);
        Graph.SetTarget(Target.transform.position);

        // Debug.DrawLine(transform.position, Target, color, 100);

        // Graph.PrintNumEdges();

        // Edges from source/target to boundaries/obstacles
        DrawVisibilityGraphScript.DrawFromBoundaries(
            transform.position, Target.transform.position, color, Graph
            );

        DrawVisibilityGraphScript.DrawFromObstacles(
            transform.position, Target.transform.position, color, Graph
            );

        // Graph.PrintNumEdges();

        /*
         * if (Graph.AdjList.ContainsKey(transform.position)) {
         *  Debug.Log(Graph.AdjList[transform.position].Count);
         * } else {
         *  Debug.Log("Nada");
         * }
         */

        if (
            Graph.AdjList.ContainsKey(transform.position) &&
            Graph.AdjList[transform.position].Count > 0 &&
            Graph.AdjList.ContainsKey(Target.transform.position) &&
            Graph.AdjList[Target.transform.position].Count > 0
            )
        {
            // Debug.Log("A Star time.");
            // Debug.Log(Time.realtimeSinceStartup);
            float planningTime = Time.realtimeSinceStartup;
            Path         = Graph.AStar();
            planningTime = Time.realtimeSinceStartup - planningTime;
            StatsScript.TotalPlanningTime += planningTime;
            // Debug.Log(Time.realtimeSinceStartup);
        }
    }
Ejemplo n.º 2
0
    public GameObject Spawn( // Purpose is to make sure no overlap
        bool isSource, List <GameObject> agents, Color color
        )
    {
        Vector2[] spots =
        {
            new Vector2(-2.94f, 0.66f),
            new Vector2(0.01f,  0.66f),
            new Vector2(2.22f,  0.66f),
            new Vector2(-1.96f, -1.68f)
        };

        float      randX;
        float      randY;
        Vector2    rand = Vector2.up;
        GameObject agent;

        if (isSource)
        {
            agent = Instantiate(AgentPrefab, AgentContainer.transform);
        }
        else
        {
            agent = Instantiate(TargetPrefab, TargetContainer.transform);
        }
        agent.GetComponent <SpriteRenderer>().color = color;
        bool guarantee = true;

        RaycastHit2D hit = Physics2D.Raycast(
            Vector2.up,
            Vector2.up,
            0
            ); // placeholder, Unity won't let me set it to null

        while (guarantee || (
                   hit.collider != null &&
                   (
                       hit.collider.gameObject.CompareTag("Barbed Wire") ||
                       hit.collider.gameObject.CompareTag("Boundaries")
                   )
                   ) || IsPointInObstacle(rand) ||
               Touching(agent, agents)
               )
        {
            guarantee = false;

            randX = Random.Range(-7.25f, 5.25f);
            randY = Random.Range(-5.61f, 3.69f);
            rand  = new Vector2(randX, randY);
            agent.transform.position = rand;
            // Physics2D.SyncTransforms();

            foreach (Vector2 spot in spots)
            {
                hit = Physics2D.Raycast(
                    rand,
                    spot - rand,
                    Vector2.Distance(spot, rand),
                    1, -0.5f, 0.5f
                    );

                /*
                 * Debug.Log(hit.collider);
                 * Debug.DrawRay(
                 *  rand,
                 *  spot - rand,
                 *  color, 1000, false
                 * );
                 */


                if (!(
                        hit.collider != null &&
                        (
                            hit.collider.gameObject.CompareTag("Barbed Wire") ||
                            hit.collider.gameObject.CompareTag("Boundaries")
                        )
                        ))
                {
                    break;
                }
            }
        }


        /* FAILED ATTEMPT AT RAYCASTHITALL
         * bool fail = true;
         * while (fail || IsPointInObstacle(rand) || Touching(agent, agents)) {
         *  randX = Random.Range(-7.25f, 5.25f);
         *  randY = Random.Range(-5.61f, 3.69f);
         *  rand = new Vector2(randX, randY);
         *  agent.transform.position = rand;
         *
         *  foreach (Vector2 spot in spots) {
         *      RaycastHit2D[] hits = Physics2D.RaycastAll(
         *          rand,
         *          spot - rand,
         *          Vector2.Distance(spot, rand)
         *      );
         *
         *      int falseCalls = 0;
         *      bool exit = false;
         *      foreach (RaycastHit2D hit in hits) {
         *          if (hit.collider == null) {
         *              fail = false;
         *              break;
         *          } else if (!hit.collider.gameObject.CompareTag("Agent")) {
         *              Debug.Log(hit.collider);
         *              if (
         *                  hit.collider.gameObject.CompareTag("Barbed Wire")
         || hit.collider.gameObject.CompareTag("Boundaries")
         ||             ) {
         ||                 exit = true;
         ||             } else {
         ||                 fail = false;
         ||             }
         ||             break;
         ||         } else {
         ||             falseCalls++;
         ||         }
         ||     }
         ||
         ||     if (!exit) {
         ||         if (hits.Length - falseCalls <= 0) {
         ||             fail = false;
         ||         }
         ||     }
         || }
         ||}
         */

        // Passing down graphs, stats, id
        if (isSource)
        {
            VisibilityGraph graph     = GetComponent <DrawVisibilityGraph>().Graph;
            VisibilityGraph graphCopy = new VisibilityGraph();
            graphCopy.Vertices = graph.ClonedVertices();
            graphCopy.AdjList  = graph.ClonedAdjList();
            agent.GetComponent <Pathing>().OriginalGraph = graphCopy;

            agent.GetComponent <Pathing>().SpawnAgentsScript
                = GetComponent <SpawnAgents>();
            agent.GetComponent <Pathing>().DrawVisibilityGraphScript
                = GetComponent <DrawVisibilityGraph>();
            agent.GetComponent <Pathing>().StatsScript = StatsScript;
            agent.GetComponent <Pathing>().Id          = Id;
            Id++;
        }

        return(agent);
    }