public World(int initWidth, int initHeight, int numAnts, int numFoodPiles, int foodPerPile) { t = new Thread(TimeStep); height = initHeight; width = initWidth; field = new int[width + 50, height + 50]; pheromone = new float[width + 50, height + 50]; redAnts = new List<Ant>(); blueAnts = new List<Ant>(); foods = new List<Food>(); anthills = new Anthill[2]; // create anthill Location anthill0 = new Location(width / 4, height / 2); Location anthill1 = new Location(3 * width / 4, height / 2); anthills[0] = new Anthill(new Rectangle(anthill0.x - 7, anthill0.y - 7, 14, 14)); anthills[1] = new Anthill(new Rectangle(anthill1.x - 7, anthill1.y - 7, 14, 14)); // create ants for (int i = 0; i < numAnts; i++ ) { redAnts.Add(new Ant(Ant.Affiliance.Red, this, new Rectangle(anthill0.x - 2, anthill0.y - 2, 4, 4))); blueAnts.Add(new Ant(Ant.Affiliance.Blue, this, new Rectangle(anthill1.x - 2, anthill1.y - 2, 4, 4))); } // create food for (int i = 0; i < numFoodPiles; i++) foods.Add(new Food(new Rectangle((int)rand.Next(25, width - 25), (int)rand.Next(25, height - 25), 10, 10), foodPerPile)); }
private void ReleasePheromoneOnFood(Location foodLocation) { world.pheromone[foodLocation.x, foodLocation.y] += pheromoneStore * .9F; }
/// <summary> /// Finds the closest direction from one point to another in an 8 neighbor system. /// </summary> /// <param name="start">Starting position</param> /// <param name="end">End position</param> /// <returns>Float direction 0-7 for use in an 8 neighbor system.</returns> private float GetSecondaryDirection(Location start, Location end) { // Find direction in radians from start to end points double direction = GetDirectionInRadians(start, end); // Convert from radians to secondary direction denoted by 0-7. // Get direction + pi/8 then divide by pi / 4 direction += Math.PI * .8; direction = (direction * 4) / Math.PI; return (float)direction; }
private void GetFood(Food food) { hasFood = true; Location foodLocation = new Location(food.location.Left, food.location.Top); ReleasePheromoneOnFood(foodLocation); food.GetFood(); }
/// <summary> /// Finds direction in radians from a start to an end point /// </summary> /// <param name="start">Starting position</param> /// <param name="end">End position</param> /// <returns>Direction in radians from start to end as double.</returns> private double GetDirectionInRadians(Location start, Location end) { if ((start.x - end.x) != 0) return Math.Atan((start.y - end.y) / (start.x - end.x)); else if (start.y < end.y) return 3 * Math.PI; else return Math.PI / 2; }