Example #1
0
 public PathRequest(Vector3 _start, Vector3 _end,
                    Action <Vector3[], bool> _callback, UnitSimple _agent)
 {
     pathStart = _start;
     pathEnd   = _end;
     callback  = _callback;
     agent     = _agent;
 }
Example #2
0
 /*
  * Takes a UnitSimple object and uses its data to fill the agentAffinities string
  * with information on that UnitSimple object's affinities as strings
  */
 public void SetAgentAffinitiesString(UnitSimple agent)
 {
     foreach (KeyValuePair <string, Affinity> aff in agent.affinityTypes)
     {
         agentAffinities += $"{aff.Key} Affinity: ," +
                            $"{aff.Value.AffinityValue}\n";
     }
 }
Example #3
0
    public static void RequestPath(Vector3 pathStart, Vector3 pathEnd,
                                   Action <Vector3[], bool> callback, UnitSimple agent)
    {
        //Debug.Log("Step 1: RequestPath called.");
        PathRequest newRequest = new PathRequest(pathStart, pathEnd, callback, agent);

        instance.pathRequestQueue.Enqueue(newRequest);
        instance.TryProcessNext();
    }
Example #4
0
    private int CalculateTotalPathArchCost(Node startNode, Node endNode, UnitSimple agent)
    {
        int  cost        = 0;
        Node currentNode = endNode;

        cost = CalculateArchitecturalCost(agent, currentNode);

        while (currentNode != startNode)
        {
            currentNode = currentNode.parent;
            cost       += CalculateArchitecturalCost(agent, currentNode);
        }

        return(cost);
    }
Example #5
0
    private static int CalculateArchitecturalCost(UnitSimple agent, Node currentNode)
    {
        int architecturalCost = 0;

        foreach (KeyValuePair <string, ArchitecturalElementContainer> container in GlobalModelData.architecturalElementContainers)
        {
            architecturalCost += MathArchCost.Instance.CalculateCostPerArchFromAffinity(agent.affinityTypes[container.Key].AffinityValue) *
                                 MathArchCost.Instance.NormailzeArchitecturalValue(currentNode.architecturalElementTypes[container.Key]) +
                                 MathArchCost.Instance.ARCHCOST_DEFAULT;

            //UnityEngine.Debug.Log($"Calculated Arch Cost from:\n" +
            //    $"Affinity: {agent.affinityTypes[container.Key].AffinityValue}" +
            //    $"Rate: {MathArchCost.Instance.CalculateCostPerArchFromAffinity(agent.affinityTypes[container.Key].AffinityValue)}" +
            //    $"Architectural Value: {currentNode.architecturalElementTypes[container.Key].ArchitecturalValue}" +
            //    $"Normalized Arch Value: {MathArchCost.Instance.NormailzeArchitecturalValue(currentNode.architecturalElementTypes[container.Key])}" +
            //    $"Partial Arch Cost: {architecturalCost}");
        }

        return(architecturalCost);
    }
Example #6
0
    private string pathString = "Node Path: "; // Initial segment of the string that lists all the nodes within a path

    // Methods for Agent Data
    public void SetAgentData(UnitSimple agent)
    {
        agentData = agent;
    }
Example #7
0
    IEnumerator FindPath(Vector3 startPosition, Vector3 targetPosition, UnitSimple agent)
    {
        // Stopwatch just used to time the processing
        Stopwatch sw = new Stopwatch();

        sw.Start();

        // Start of true pathfinding
        Vector3[] waypoints   = new Vector3[0];
        bool      pathSuccess = false;

        Node startNode  = FindAppropriateNodeFromWorldPoint(startPosition, isFindingNearestWalkableNode);
        Node targetNode = FindAppropriateNodeFromWorldPoint(targetPosition, isFindingNearestWalkableNode);

        UnityEngine.Debug.Log($"PathfindingHeap for {agent.name}: \n" +
                              $" Start walkable: {startNode.walkable}\n" +
                              $" Target Walkability: {targetNode.walkable}");


        if (startNode.walkable && targetNode.walkable)
        {
            Heap <Node>    openSet   = new Heap <Node>(AGridRuntime.Instance.MaxSize);
            HashSet <Node> closedSet = new HashSet <Node>();
            openSet.Add(startNode);

            while (openSet.Count > 0)
            {
                Node currentNode = openSet.RemoveFirst();
                closedSet.Add(currentNode);

                // The target has been found, so exit out of the loop
                if (currentNode == targetNode)
                {
                    sw.Stop();
                    print("Path found: " + sw.ElapsedMilliseconds + " ms");
                    pathSuccess = true;
                    //RetracePath(startNode, targetNode);
                    break;
                }

                foreach (Node neighbor in AGridRuntime.Instance.GetNeighbors(currentNode))
                {
                    if (!neighbor.walkable || closedSet.Contains(neighbor))
                    {
                        continue;
                    }

                    // Factor extra costs into the gCost
                    // Architectural cost of all elements
                    int architecturalCost = CalculateArchitecturalCost(agent, neighbor);

                    // Total cost
                    int newMovementCostToNeighbor = currentNode.gCost +
                                                    GetDistance(currentNode, neighbor) +
                                                    neighbor.movementPenalty +
                                                    architecturalCost;

                    //UnityEngine.Debug.Log($"Costs of Node {currentNode.NodeCoordinates}:\n" +
                    //    $"G Cost: {currentNode.gCost}" +
                    //    $"Arch Cost: {architecturalCost}");

                    if (newMovementCostToNeighbor < neighbor.gCost || !openSet.Contains(neighbor))
                    {
                        neighbor.gCost  = newMovementCostToNeighbor;
                        neighbor.hCost  = GetDistance(neighbor, targetNode);
                        neighbor.parent = currentNode;

                        if (!openSet.Contains(neighbor))
                        {
                            openSet.Add(neighbor);
                        }
                        else
                        {
                            openSet.UpdateItem(neighbor);
                        }
                    }
                }
            }
        }

        yield return(null);

        if (pathSuccess)
        {
            DataRecorder.Instance.SetCurrentPathAgent(agent);
            DataRecorder.Instance.SetCurrentPathDistance(CalculateTotalPathDistance(startNode, targetNode));
            DataRecorder.Instance.SetCurrentPathTotalArchCost(CalculateTotalPathArchCost(startNode, targetNode, agent));
            DataRecorder.Instance.SetCurrentPathTotalCost(CalculateTotalPathCost(startNode, targetNode));
            waypoints = generateNodePath.RetracePath(startNode, targetNode);


            UnityEngine.Debug.Log($"Pathing Total Costs: \n " +
                                  $"Total Path Architectural Cost was: {CalculateTotalPathArchCost(startNode, targetNode, agent)}\n" +
                                  $"Total Path Cost was: {CalculateTotalPathCost(startNode, targetNode)}");
        }


        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
Example #8
0
 public void StartFindPath(Vector3 startPosition, Vector3 targetPosition, UnitSimple agent)
 {
     StartCoroutine(FindPath(startPosition, targetPosition, agent));
 }
 public void SetCurrentPathAgent(UnitSimple agent)
 {
     currentPath.SetAgentData(agent);
     currentPath.SetAgentAffinitiesString(agent);
 }