Exemplo n.º 1
0
 private void Update(DijkstraInfo n1, DijkstraInfo n2)
 {
     if (n1.tDist < n2.tDist && this.board.IsFreeTile(new Vector2Int(n2.x, n2.y)))
     {
         n2.tDist = n1.tDist + 1;
     }
 }
Exemplo n.º 2
0
        public void testDijkstra()
        {
            SharedGraph  g = g1();
            DijkstraInfo a = Dijkstra.FindShortestPath(g, 5, 4);

            Assert.AreEqual(13, a.cost);
            List <int> sa = new List <int>()
            {
                5, 1, 8, 3, 7, 4
            };

            Assert.AreEqual(sa, a.shortestPath);

            DijkstraInfo b = Dijkstra.FindShortestPath(g, 4, 8);

            Assert.AreEqual(6, b.cost);
            List <int> sb = new List <int>()
            {
                4, 7, 3, 8
            };

            Assert.AreEqual(sb, b.shortestPath);

            DijkstraInfo c = Dijkstra.FindShortestPath(g, 5, 8);

            Assert.AreEqual(7, c.cost);
            List <int> sc = new List <int>()
            {
                5, 1, 8
            };

            Assert.AreEqual(sc, c.shortestPath);
        }
Exemplo n.º 3
0
 void AddConnectionsToOpen(DijkstraInfo hex)
 {
     if (hex != null)
     {
         foreach (DijkstraInfo c in hex.Connections)
         {
             //If the tested tile has connections it is not impassable
             if (c.Connections.Count > 0)
             {
                 if (!open.Contains(c) && !closed.Contains(c))
                 {
                     open.Add(c);
                     c.costSoFar = hex.costSoFar + (c.cost / 2f) + (hex.cost / 2f);
                     c.root      = hex;
                 }
                 else if (c.costSoFar > hex.costSoFar + (c.cost / 2f) + (hex.cost / 2f))
                 {
                     c.costSoFar = hex.costSoFar + (c.cost / 2f) + (hex.cost / 2f);
                     c.root      = hex;
                     if (closed.Contains(c))
                     {
                         closed.Remove(c);
                         open.Add(c);
                     }
                 }
             }
         }
         open.Remove(hex);
         closed.Add(hex);
     }
 }
Exemplo n.º 4
0
    /// <summary>
    ///
    /// </summary>
    /// <param name="start"></param>
    /// <param name="destination"></param>
    /// <param name="teamId"></param>
    /// <returns></returns>
    public List <DijkstraInfo> DijkstraToBuilding(DijkstraInfo start, List <Buildings> destination, int teamId)
    {
        List <DijkstraInfo> temp = new List <DijkstraInfo>();
        bool destinationFound    = false;
        bool noValidPath         = false;

        open.Add(start);
        DijkstraInfo destinationTile = new DijkstraInfo();

        AddConnectionsToOpen(start);

        while (!destinationFound && !noValidPath)
        {
            DijkstraInfo tile = GetLowestCost();
            for (int i = 0; i < GlobalAttributes.Global.Buildings.Count; ++i)
            {
                if (GlobalAttributes.Global.Buildings[i].hexTransform.Position == tile.GetComponent <HexTile>().hexTransform.Position)
                {
                    for (int j = 0; j < destination.Count; ++j)
                    {
                        if (GlobalAttributes.Global.Buildings[i].BuildingType == destination[j])
                        {
                            destinationFound = true;
                            destinationTile  = tile;
                        }
                        if (tile != null && GlobalAttributes.Global.Buildings[i].BuildingType != destination[j])
                        {
                            AddConnectionsToOpen(tile);
                        }
                        else
                        {
                            noValidPath = true;
                        }
                    }
                }
            }
        }

        if (destinationFound)
        {
            DijkstraInfo tempTile = destinationTile;
            // THIS NEEDS TO LOOK FOR A TILE ON THE building's exclusion zone
            //tempTile.hexTransform.position = destination.hexTransform.position;
            while (tempTile != start && tempTile != null)
            {
                temp.Add(tempTile);
                //tempTile.SetColour(Color.magenta);
                tempTile = (DijkstraInfo)tempTile.root;
            }

            temp.Reverse();
        }
        //state = DJStates.waitingForClear;

        return(temp);
    }
Exemplo n.º 5
0
    private DijkstraInfo FindNext()
    {
        DijkstraInfo bestInfo = null;

        foreach (DijkstraInfo info in this.floodBoard)
        {
            int bestDist = bestInfo != null ? bestInfo.tDist : int.MaxValue;
            if (info.tDist < bestDist && !info.visited)
            {
                bestInfo = info;
            }
        }
        return(bestInfo);
    }
Exemplo n.º 6
0
        static IEnumerable <int> Dijkstra(int[][] g, int start)
        {
            // Arrange
            var tmp = new DijkstraInfo[g.Length];

            for (var i = 0; i < g.Length; i++)
            {
                tmp[i]       = new DijkstraInfo();
                tmp[i].Index = i;
            }

            // Init
            var n   = g.Length;
            var pos = start;

            for (var i = 0; i < g.Length; i++)
            {
                if (g[pos][i] >= 0 && tmp[i].Distance > g[pos][i])
                {
                    tmp[i].Distance = g[pos][i];
                    tmp[i].Path     = pos;
                }
            }
            tmp[pos].IsVisited = true;

            // Loop
            do
            {
                var di = tmp.Where(x => !x.IsVisited && x.Distance < Max).OrderBy(x => x.Distance).FirstOrDefault();
                if (di == null)
                {
                    break;
                }
                pos = di.Index;
                for (var i = 0; i < g.Length; i++)
                {
                    if (g[pos][i] >= 0 && tmp[i].Distance > g[pos][i] + tmp[pos].Distance)
                    {
                        tmp[i].Distance = g[pos][i] + tmp[pos].Distance;
                        tmp[i].Path     = pos;
                    }
                }
                tmp[pos].IsVisited = true;
            }while (--n != 0);

            // Output
            return(tmp.Select(x => x.Distance));
        }
Exemplo n.º 7
0
    /// <summary>
    /// Dijkstra method to find a tile that has a script attached of type T
    /// </summary>
    /// <typeparam name="T">Type to find on destination tile</typeparam>
    /// <param name="start">Tile to start from</param>
    /// <returns>List of tiles between the start and found tile</returns>
    public List <DijkstraInfo> Dijkstra <T>(DijkstraInfo start)
    {
        List <DijkstraInfo> temp = new List <DijkstraInfo>();
        bool destinationFound    = false;
        bool noValidPath         = false;

        open.Add(start);
        DijkstraInfo destination = new DijkstraInfo();

        AddConnectionsToOpen(start);

        while (!destinationFound && !noValidPath)
        {
            DijkstraInfo tile = GetLowestCost();
            if (tile != null && tile != destination)
            {
                AddConnectionsToOpen(tile);
            }
            else if (tile.GetComponent <T>() != null)
            {
                destination      = tile;
                destinationFound = true;
            }
            else
            {
                noValidPath = true;
            }
        }

        if (destinationFound)
        {
            DijkstraInfo tempTile = destination;
            while (tempTile != start && tempTile != null)
            {
                temp.Add(tempTile);
                //tempTile.SetColour(Color.magenta);
                tempTile = (DijkstraInfo)tempTile.root;
            }

            temp.Reverse();
        }
        //state = DJStates.waitingForClear;

        return(temp);
    }
Exemplo n.º 8
0
    private bool Propagate()
    {
        DijkstraInfo node = this.FindNext();

        if (node == null)
        {
            return(false);
        }
        else
        {
            node.visited = true;
            foreach (Vector2Int offset in FloodFillBoard.neighbours)
            {
                if (this.board.InBounds(new Vector2Int(node.x, node.y) + offset))
                {
                    this.Update(node, this.floodBoard[node.x + offset.x, node.y + offset.y]);
                }
            }
            return(true);
        }
    }
Exemplo n.º 9
0
    DijkstraInfo GetLowestCost()
    {
        if (open.Count > 0)
        {
            DijkstraInfo tempTile   = open[0];
            float        lowestCost = tempTile.costSoFar;
            if (open.Count > 1)
            {
                for (int i = 1; i < open.Count; ++i)
                {
                    if (open[i].costSoFar < lowestCost)
                    {
                        lowestCost = open[i].costSoFar;
                        tempTile   = open[i];
                    }
                }
            }
            return(tempTile);
        }

        return(null);
    }