Ejemplo n.º 1
0
        private void ResetPathFinder()
        {
            if (_todoList != null)
            {
                _todoList.ForEach(ResetNode);
            }
            if (_doneList != null)
            {
                _doneList.ForEach(ResetNode);
            }

            _todoList    = new List <HexNode>();
            _doneList    = new List <HexNode>();
            _done        = false;
            _path        = null;
            _currentNode = null;

            //setup for next path
            if (_startNode != null)
            {
                _todoList.Add(_startNode);
                _startNode.CostCurrent  = 0;
                _startNode.CostEstimate = 0;
                _startNode.CostCombined = 0;
            }
        }
Ejemplo n.º 2
0
 public void SetStartNode(HexNode start)
 {
     if (_startNode != null)
     {
         ResetNode(_startNode);
     }
     _startNode = start;
     ResetPathFinder();
 }
Ejemplo n.º 3
0
        public bool Step()
        {
            //are we able to find a path??
            if (_done || _startNode == null || _endNode == null || _todoList.Count == 0)
            {
                _done = true;
                return(true);
            }

            //we are not done, start and end are set and there is at least 1 item on the open list...

            //get a node from the open list
            _currentNode = _todoList[0];
            _todoList.RemoveAt(0);

            //and move that node to the closed list (one way or another, we are done with it...)
            _doneList.Add(_currentNode);

            if (_currentNode == _endNode)
            {
                GeneratePath();
                _done = true;
            }
            else
            {
                //get all children and process them
                foreach (HexNode neighbor in _currentNode.Neighbors)
                {
                    if (neighbor != _endNode && neighbor.HasOccupant)
                    {
                        continue;
                    }

                    //if (_doneList.IndexOf(neighbor) == -1 && _todoList.IndexOf(neighbor) == -1)
                    if (!_doneList.Contains(neighbor) && !_todoList.Contains(neighbor))
                    {
                        if (neighbor.CostCurrent < _currentNode.CostCurrent)
                        {
                            neighbor.Parent = _currentNode;
                        }

                        neighbor.CostCurrent = _currentNode.CostCurrent +
                                               Vector3.Distance(_currentNode.Position, neighbor.Position);

                        neighbor.CostEstimate = Vector3.Distance(neighbor.Position, _endNode.Position);

                        neighbor.CostCombined = neighbor.CostCurrent + neighbor.CostEstimate;

                        _todoList.Add(neighbor);
                    }
                }

                _todoList.Sort();
            }

            return(_done);
        }
Ejemplo n.º 4
0
 public void Search(HexNode start, HexNode end)
 {
     SetStartNode(start);
     SetEndNode(end);
     while (!_done)
     {
         Step();
     }
 }
Ejemplo n.º 5
0
 public void SetEndNode(HexNode end)
 {
     if (_endNode != null)
     {
         ResetNode(_endNode);
     }
     _endNode = end;
     ResetPathFinder();
 }
Ejemplo n.º 6
0
        private void ResetNode(HexNode node)
        {
            node.CostCurrent  = 0;
            node.CostEstimate = 0;
            node.CostCombined = 0;

            if (node.Parent != null)
            {
                node.Parent = null;
            }
        }
Ejemplo n.º 7
0
        private void ConstructPath(HexNode end)
        {
            _path = new List <HexNode>();
            HexNode node = end;

            while (node != null)
            {
                _path.Add(node);
                node = node.Parent;
            }
            _path.Reverse();
        }
Ejemplo n.º 8
0
        private void Find()
        {
            _startNode.CostCurrent  = 0;
            _startNode.CostEstimate = Vector3.Distance(_startNode.Position, _endNode.Position);
            _startNode.CostCombined = _startNode.CostCurrent + _startNode.CostEstimate;

            _openList.Add(_startNode);

            while (_openList.Count > 0)
            {
                _openList.Sort();

                HexNode current = _openList[0];

                if (current == _endNode)
                {
                    ConstructPath(_endNode);
                    _done = true;
                    return;
                }

                _openList.Remove(current);
                _closedList.Add(current);

                foreach (var neighbor in current.Neighbors)
                {
                    neighbor.CostCurrent = current.CostCurrent + Vector3.Distance(current.Position, neighbor.Position);

                    if (!_closedList.Contains(neighbor))
                    {
                        neighbor.CostEstimate = Vector3.Distance(neighbor.Position, _endNode.Position);
                        neighbor.CostCombined = neighbor.CostCurrent + neighbor.CostEstimate;

                        if (!_openList.Contains(neighbor))
                        {
                            _openList.Add(neighbor);
                        }
                        else
                        {
                            HexNode openNeighbor = _openList[_openList.IndexOf(neighbor)];

                            if (neighbor.CostCurrent < openNeighbor.CostCurrent)
                            {
                                openNeighbor.CostCurrent = neighbor.CostCurrent;
                                openNeighbor.Parent      = neighbor.Parent;
                            }
                        }
                    }
                }
            }
            _done = true;
        }
Ejemplo n.º 9
0
        private void GeneratePath()
        {
            List <HexNode> nodeList = new List <HexNode>();
            HexNode        node     = _endNode;

            while (node != null)
            {
                nodeList.Add(node);
                node = node.Parent;
            }
            nodeList.Reverse();

            _path = nodeList;
        }
Ejemplo n.º 10
0
        public void Search(HexNode start, HexNode end)
        {
            if (_startNode != null)
            {
                ResetNode(_startNode);
            }
            if (_endNode != null)
            {
                ResetNode(_endNode);
            }

            _startNode = start;
            _endNode   = end;

            ResetPathfinder();

            Find();
        }
Ejemplo n.º 11
0
        // Update is called once per frame
        void Update()
        {
            startNode = _manager.GetHexNode(StartNodeIndex);
            endNode   = _manager.GetHexNode(EndNodeIndex);
            if (Input.GetKeyDown(KeyCode.I))
            {
                if (startNode != null && endNode != null)
                {
                    Debug.Log("Successfully loaded the nodes into the testagent!");
                }
                else
                {
                    Debug.Log("Could not properly load the nodes!");
                }
            }
            if (startNode != null && endNode != null)
            {
                //Debug.Log(startNode);
                //Debug.Log(endNode);
                Vector3 sP = startNode.Position;
                Debug.DrawLine(sP, new Vector3(sP.x, sP.y + 20, sP.z), Color.blue);
                Vector3 eP = endNode.Position;
                Debug.DrawLine(eP, new Vector3(eP.x, eP.y + 20, eP.z), Color.green);
            }
            if (Input.GetKeyDown(KeyCode.U))
            {
                if (startNode != null && endNode != null)
                {
                    _pathfinder.Search(startNode, endNode);
                    Debug.Log("Starting to search");
                    if (_pathfinder.Done)
                    {
                        _path = _pathfinder.Path;
                        if (_path != null)
                        {
                            Debug.Log("Path found!\n" + "Path length : " + _path.Count);

                            string pathIndexes = "PathIndexes: \n";
                            foreach (var node in _path)
                            {
                                pathIndexes += node.Index + " ";
                            }
                            Debug.Log(pathIndexes);
                        }
                        else
                        {
                            Debug.Log("No solution was found!");
                        }
                    }
                }
                else
                {
                    Debug.Log("the start/end node was not set yet!");
                }
            }

            if (_path != null)
            {
                DebugPath();
            }
        }