Exemple #1
0
 public SolarSystemNode(int systemId, Vector2 position, double cost, double pathCost, SolarSystemConnectionData[] connectedTo, SolarSystemNode parent)
 {
     SystemId  = systemId;
     Position  = position;
     Cost      = cost;
     PathCost  = pathCost;
     ConnectTo = connectedTo;
     _parent   = parent;
 }
        private PathInfo FindPathReversed(int start, int end)
        {
            _startNode = new SolarSystemNode(start, new Vector2((float)_solarSystems[start].X, (float)_solarSystems[start].Y), 0, 0, _solarSystems[start].ConnectedTo, null);

            HeapPriorityQueue <SolarSystemNode> openList = new HeapPriorityQueue <SolarSystemNode>(Size);

            openList.Enqueue(_startNode, 0);

            bool[] brWorld = new bool[Size];
            brWorld[start] = true;

            //Vector2 endPosition = new Vector2((float)_solarSystems[end].X, (float)_solarSystems[end].Y);

            while (openList.Count != 0)
            {
                SolarSystemNode current = openList.Dequeue();

                if (current.SystemId == end)
                {
                    return(GetPathInfo(current));
                }
                //return new SolarSystemNode(end, endPosition, current.PathCost + 1, current.Cost + 1, _solarSystems[end].ConnectedTo, current);

                SolarSystemConnectionData[] surrounding = _solarSystems[current.SystemId].ConnectedTo;


                if (surrounding != null)
                {
                    foreach (var surroundingSystem in surrounding)
                    {
                        var tempId     = surroundingSystem.ToSystemId;
                        var tmp        = new Vector2((float)_solarSystems[tempId].X, (float)_solarSystems[tempId].Y);
                        var brWorldIdx = tempId;

                        if (!PositionIsFree(brWorldIdx) || brWorld[brWorldIdx])
                        {
                            continue;
                        }
                        brWorld[brWorldIdx] = true;

                        var pathCost = current.PathCost;
                        var cost     = pathCost + 1;
                        var node     = new SolarSystemNode(tempId, tmp, cost, pathCost, _solarSystems[tempId].ConnectedTo, current);
                        openList.Enqueue(node, cost);
                    }
                }
                else if (openList.Count == 0)
                {
                    return(GetEmptyPath(_startNode.SystemId));
                }
            }
            return(GetEmptyPath(_startNode.SystemId)); //no path found
        }
        private PathInfo GetPathInfo(SolarSystemNode endNode)
        {
            var tempInfo = new PathInfo();

            var tempNode = endNode;

            var pathSystems = new List <int>();

            var i = 0;

            while (tempNode.HasParent)
            {
                pathSystems.Add(tempNode.SystemId);
                tempNode = tempNode.Parent;
                i++;
            }
            i++;

            var jumpCount = i;

            var pathIDs = new int[jumpCount + 1];

            i = 0;
            foreach (var tempId in pathSystems)
            {
                pathIDs[i] = tempId;
                i++;
            }
            i++;
            pathIDs[i] = _startNode.SystemId;

            tempInfo.PathSystems = pathIDs;
            tempInfo.TotalJumps  = jumpCount;

            return(tempInfo);
        }