private static void ProcessPheremoneDecay() { for (int x = 0; x < _grid.GetLength(0); x++) { for (int y = 0; y < _grid.GetLength(1); y++) { Pheromone p = _grid[x, y].Pheromone; p.Decay(); } } }
private void LeavePheremone() { Pheromone p = World.Instance.PheremoneAt(X, Y); if (p.Size < _targetFood.Size * Pheromone.SizeFactor) { p.Size = _targetFood.Size * Pheromone.SizeFactor; } p.Parent = World.Instance.Grid[_previousNode.X, _previousNode.Y].Pheromone; }
private Pheromone NearestPheremone() { for (int x = Location.X - _searchRadius; x < Location.X + _searchRadius; x++) { for (int y = Location.Y - _searchRadius; y < Location.X + _searchRadius; y++) { Pheromone p = World.Instance.PheremoneAt(x, y); if (p.Size >= _pheremoneThreshold) { return(p); } } } return(null); }
// Manually constructs a path by backtracking over pheremones. public static Path ConstructPathFromTrail(Pheromone p) { Pheromone pToCheck = p; LinkedList <Node> waypoints = new LinkedList <Node>(); waypoints.AddFirst(World.Instance.Grid[p.X, p.Y]); LinkedListNode <Node> parentListNode = waypoints.First; Pheromone parent = World.Instance.PheremoneAt(waypoints.First.Value).Parent; do { Node newNode = World.Instance.Grid[pToCheck.X, pToCheck.Y]; waypoints.AddAfter(parentListNode, newNode); parentListNode = waypoints.Find(World.Instance.Grid[pToCheck.X, pToCheck.Y]); pToCheck = parent; parent = pToCheck.Parent; // Sometimes pheremone trails will point in loops that don't lead to food. // If this happens, we simply return a path to a random food object. if (waypoints.Count > 5000) { return(new Path(p.Location, World.Instance.Foods[GameLogic.Random.Next(World.Instance.Foods.Count - 1)].Location)); } } while (parent != null && !parent.Location.IsAt(pToCheck.Location)); // Once the trail reaches the edge of the food object, finish the path by pathing to the centre of the nearest food. // This avoids hangs where ants can't quite reach the food and pathfind forever. Food destination = World.Instance.NearestFood(pToCheck.Location); Node finalNode = World.Instance.Grid[destination.X, destination.Y]; waypoints.AddAfter(parentListNode, finalNode); return(new Path(waypoints)); }