public void DetermineConnectionsAndShortestBasePaths(Team team)
{
// Prerequisite: When we call this method, we may already access an empty queue of squares with the name "queue".
foreach (GameSquare baseSquare in team.homebase)
{
square.basePathLength = 0; // As this square belongs to the base, its distance to the base is 0.
square.presentDirection = Adjacencies.None; // We only need the directionVariable for line squares (as you will see below).
queue.Enqueue(square); // Initially, we want the queue to consist of the three base squares only.
}
// Here we go now with our modified breadth-first search that loops until the queue is empty.
while (queue.Count != 0)
{
// Take the cheapest node (i.e., the node at the head of our queue) and travel on from there...
Square cheapestNode = queue.Dequeue();
// If that node is a tower that was not yet visited ...
if (cheapestNode.isTower && cheapestNode.basePathLength == 1000)
{
// Enter the ElecTon flow-direction for this square, if it is no baseSquare
if (!cheapestNode.isBaseSquare)
{
// As we are now coming FROM the base and want to send ElecTons TO the base we have to reverse the direction.
// (As typical for shortest path data strucutres we do not store whole paths, but instead - for each node - the
// direction that leads to the base on a shortest path.)
cheapestNode.basePathSuccessor = CounterAdjacency(cheapestNode.presentDirection);
}
// Determine the neighbour towers and define the corresponding line squares.
DetermineNeighbourPillars(newPillar);
ConnectToNeighbourPillars(newPillar);
foreach (Adjacencies direction in orthogonalNeighbours)
{
// If this direction leads to a neighbour that has not yet been visited.
// (Note that line squares have already been created.)
if (cheapestNode.neighbourPillars[(int)direction] != null &&
cheapestNode.neighbourPillars[(int)direction].basePathLength == 1000)
{
// Check the neighbour to this direction...
GameSquare neighbour = Neighbour(direction, cheapestNode);
// ...store the incremented basePathLength
square.basePathLength = cheapestNode.basePathLength + 1;
// ...store the direction in which we have been going (as we may not change our direction on a line square)...
square.presentDirection = direction;
// ...and add it to the queue.
queue.Enqueue(square);
}
}
}
// If the cheapest node is a line square, we may not change our walking direction (which we stored under presentDirection).
// Therefore, we have to keep going into this direction until we hit a tower.
else if (!cheapestNode.pillar)
{
// Check the neighbour to our present direction
GameSquare neighbour = Neighbour(cheapestNode.presentDirection, cheapestNode);
// The basePathLength corresponds to the incremented basePathLenght of its predecessor.
square.basePathLength = cheapestNode.basePathLength + 1;
// And we still have walk on into our presentDirection when we dequeue this node.
square.presentDirection = cheapestNode.presentDirection;
// Add the square to the queue with its new entries.
queue.Enqueue(square);
}
}
}