//Setup the GA. The Ant class is our actual chromosome while the weights in the ants neural network form the genes. public Genome(Ant[] Ants, int ChromoLength, float MutationRate, float CrossOverRate) { mAnts = Ants; mPopSize = mAnts.Length - 1; //FileSystem.Reset(); - jb check later mGeneration = 0; mMutationRate = MutationRate; mCrossOverRate = CrossOverRate; mChromoLength = ChromoLength; }
protected void IssueOrder(IGameState state, Ant ant, Direction direction) { if (direction != Direction.None) { //only walk if there's no other ant standing in that location if (state.GetIsUnoccupied(state.GetDestination(ant, direction))) { ant.WaitTime = 0; //tell the gamestate that the ant will be moved next turn state.MoveAnt(ant, direction); System.Console.Out.WriteLine("o {0} {1} {2}", ant.Row, ant.Col, direction.ToChar()); } } }
public AntBrain(String FilePath, Ant[] Ants, Logic MyLogic) { BC = new BrainChecker(FilePath); BC.runCheck(); commands = BC.getCommands(); ants = Ants; myLogic = MyLogic; for (int i = 0; i < 10000; i++) { if (commands[i] == null) { commands[i] = new AntCommand("flip", "1", "0", "0", null, null); } } }
public void StartBattle(GameState state, Ant AllyAnt, Ant EnemyAnt, int[,] enemyProximity) { this.enemyProximity = enemyProximity; AllyPlatoon.Clear(); EnemyPlatoon.Clear(); BuildPlatoon(state, AllyPlatoon, AllyAnt); BuildPlatoon(state, EnemyPlatoon, EnemyAnt); if (AllyPlatoon.Count > EnemyPlatoon.Count) { //Attack! var AllyDist = BuildFrontline(state, AllyPlatoon, EnemyPlatoon, out int qty, out int distance); var EnemyDist = BuildFrontline(state, EnemyPlatoon, AllyPlatoon, out int Eqty, out int Edistance); if (Eqty >= qty) { //We have more ants, but they are not on the front line! //Regroup! if (AllyDist.Where(x => x == distance + 1).Count() > Eqty) { //At the end of the turn, we will be in position, no need to move back Regroup(state, AllyDist.ToArray(), distance, true); } else { //Maybe we should move back our units... Regroup(state, AllyDist.ToArray(), distance, true); } } else { Attack(state); } } else { Retreat(state); } }
private bool goToFront(GameState state, int[,] visibility, Ant ant) { int value = visibility.At(ant); Direction dir = Direction.North; foreach (Direction direction in Ants.Aim.Keys) { Location newLoc = state.GetDestination(ant, direction); if (state.GetIsPassable(newLoc) && !state.OccupiedNextRound.At(newLoc) && value > visibility.At(newLoc)) { value = visibility.At(newLoc); dir = direction; } } if (value != visibility.At(ant)) { IssueOrder(state, ant, dir, "Go To Front"); return(true); } return(false); }
public Logic() { testint = 0; graphics = new GraphicsDeviceManager(this); this.graphics.PreferredBackBufferWidth = 903; this.graphics.PreferredBackBufferHeight = 900; //at the moment, the screen is 903x900 resolution, this is for a 150x150 grid of 6x6 squares. it would be 900x900, but the lines are staggered so we needed 3 extra wide //when we are done, we can add extra stuff on one side of the screen and make it 1200x900 (4:3) or something sensible texAntsOnHillRed = new Texture2D[6]; texAntsOnHillBlack = new Texture2D[6]; texAntsOnFoodRed = new Texture2D[6]; texAntsOnFoodBlack = new Texture2D[6]; texAntsBlankRed = new Texture2D[6]; texAntsBlankBlack = new Texture2D[6]; oldkbstate = new KeyboardState(); newkbstate = new KeyboardState(); redAnts = new Ant[127]; blackAnts = new Ant[127]; Content.RootDirectory = "Content"; tiles = new Tile[150, 150]; WG = new WorldGeneration(this, this); WW = new WorldWriter(this); //setWorldChecker(@"C:\Users\Max\TestWorld.txt"); //redBrain = new AntBrain(@"C:\Users\Max\sampleant.txt", redAnts, this); //blackBrain = new AntBrain(@"C:\Users\Max\sampleant.txt", blackAnts, this); gameState = "intro"; loadMapSTR = ""; loadBrainRed = ""; loadBrainBlack = ""; saveMapSTR = ""; menuState = 0; redScore = 0; blackScore = 0; kbreader = new KeyboardReader(this); blackLoaded = false; }
public static void BuildPlatoon(IGameState state, List <Ant> platoonToFill, Ant startingAnt) { var antList = (startingAnt.Team == 0 ? state.MyAnts : state.EnemyAnts).Where(x => state.GetDistance(startingAnt, x) < platoonMaxTotalRadius).ToList();; platoonToFill.Add(startingAnt); var newAnts = new List <Ant>(platoonToFill); //Coud be done using only an index for new Ants instead of another list while (newAnts.Count > 0) { antList = antList.Except(newAnts).ToList(); var current = newAnts.Last(); newAnts.RemoveAt(newAnts.Count - 1); var newFound = antList.Where(x => state.GetDistance(current, x) < platoonRadius).ToList(); newAnts.AddRange(newFound); platoonToFill.AddRange(newFound); } }
private bool getFood(GameState state, int[,] FoodProximity, Ant ant) { if (FoodProximity.At(ant) < FoodRadius) { int value = 999; Direction dir = Direction.North; foreach (Direction direction in Ants.Aim.Keys) { Location newLoc = state.GetDestination(ant, direction); if (state.GetIsPassable(newLoc) && !state.OccupiedNextRound.At(newLoc) && value > FoodProximity.At(newLoc)) { value = FoodProximity.At(newLoc); dir = direction; } } IssueOrder(state, ant, dir, "GetFoodOld"); return(true); } return(false); }
private void btn_go_Click(object sender, EventArgs e) { var myBitmap = new Bitmap(pct_Output.Width, pct_Output.Height); int centerX = Convert.ToInt16(pct_Output.Width * 0.5); int centerY = Convert.ToInt16(pct_Output.Height * 0.5); var firstAnt = new Ant(); firstAnt.locX = centerX; firstAnt.locY = centerY; firstAnt.speedY = 0; firstAnt.speedX = 1; pct_Output.Image = myBitmap; myBitmap.SetPixel(firstAnt.locX, firstAnt.locY, Color.Black); for (int n = 0; n < 20000; n++) { Color pixel = myBitmap.GetPixel(firstAnt.locX, firstAnt.locY); if (pixel.R == 0 && pixel.G == 0 && pixel.B == 0) { //set this pixel white and turn left myBitmap.SetPixel(firstAnt.locX, firstAnt.locY, Color.White); TurnLeft(firstAnt); } else { //set this pixel black and turn right myBitmap.SetPixel(firstAnt.locX, firstAnt.locY, Color.Black); TurnRight(firstAnt); } firstAnt.locX += firstAnt.speedX; firstAnt.locY += firstAnt.speedY; } //myBitmap.SetPixel(MousePosition.X, MousePosition.Y, Color.Black); }
private void TurnRight(Ant ant) { if (ant.speedX == 1) { ant.speedX = 0; ant.speedY = 1; } else if (ant.speedX == -1) { ant.speedX = 0; ant.speedY = -1; } else if (ant.speedY == 1) { ant.speedY = 0; ant.speedX = -1; } else if (ant.speedY == -1) { ant.speedY = 0; ant.speedX = 1; } }
public AntFarm(int AntCnt, int FoodCnt, int Width, int Height) { int idx = 0; mNumAnts = AntCnt; mNumFood = FoodCnt; mWidth = Width; mHeight = Height; mAnts = new Ant[AntCnt]; mFood = new PointF[FoodCnt]; //Add ants to the world for (idx = 0; idx <= mAnts.Length - 1; idx++) { mAnts[idx] = new Ant(new NeuralNetwork(4, 2, 1, 4, 4, Neuron.ActivationFunction.AFSigmoid), (float)rand.NextDouble() * Width, (float)rand.NextDouble() * Height); } //Add food to the world for (idx = 0; idx <= mFood.Length - 1; idx++) { mFood[idx] = new PointF((float)rand.NextDouble() * Width, (float)rand.NextDouble() * Height); } mGenome = new Genome(mAnts, mAnts[1].NumWeights, 0.01F, 0.05F); }
private void BuildGraph(AttackGraph graph, List <Ant> ours, List <Ant> theirs, TacticalMap map) { for (int i = 0; i < ours.Count; i++) { Ant myAnt = ours[i]; // // Get all tiles on tactical map that our ant can navigate on // IEnumerable <TacticalMap.Tile> tiles = Vector2i.AllDirections .Select(direction => Vector2i.Wrap(myAnt.position + direction, map.Width, map.Height)) .Select(p => map[p.x, p.y]); // // Identify enemies // IEnumerable <int> enemies = IdentifyEnemies(tiles); foreach (int enemyId in enemies) { graph.AddNodeGroup(i, enemyId, BuildLinksAssociatedToEnemy(enemyId, tiles)); } } }
public void AddAnt(int row, int col, int team) { map[row, col] = Tile.Ant; Ant ant; if (team == 0) { if (MyAntsNextTurn[row, col] != null) { ant = MyAntsNextTurn[row, col]; ant.SetLocation(row, col); } else { ant = new Ant(row, col, team, antNumber); antNumber++; } MyAnts.Add(ant); //calculate visibility for ant for (int r = -1 * ViewRadius; r <= ViewRadius; ++r) { for (int c = -1 * ViewRadius; c <= ViewRadius; ++c) { int square = r * r + c * c; if (square <= ViewRadius2) { Location loc = GetDestination(ant, new Location(r, c)); //add visible locations to visibilitymap VisibilityMap[loc.Row, loc.Col] = true; } } } } else { ant = new Ant(row, col, team, 0); EnemyAnts.Add(ant); //calculate enemy attack map foreach (Direction direction in Enum.GetValues(typeof(Direction))) { if (direction != Direction.None) { Location newLocation = GetDestination(ant, direction); for (int r = -1 * AttackRadius; r <= AttackRadius; ++r) { for (int c = -1 * AttackRadius; c <= AttackRadius; ++c) { int square = r * r + c * c; if (square <= AttackRadius2) { Location loc = GetDestination(newLocation, new Location(r, c)); if (GetIsPassable(loc)) { //add visible locations to enemyattack map EnemyAttackMap[loc.Row, loc.Col] = true; } } } } } } } }
private int[,] calculateAllyProximity(IGameState state, Ant ant) { return(CalculateProximity(state, state.MyAnts.Where(x => x != ant).ToList())); }
public void AddAnt(int row, int col, int team) { map[row, col] = Tile.Ant; Ant ant = new Ant(row, col, team); if (team == 0) { MyAnts.Add(ant); } else { EnemyAnts.Add(ant); } }
/// <summary> /// Adds the ant to the game state. /// </summary> /// <param name="row">The row.</param> /// <param name="col">The col.</param> /// <param name="team">The team.</param> public void AddAnt(int row, int col, int team) { map[row, col] = Tile.Ant; bool mine = team == 0; //my team is always 0 Ant ant = new Ant(row, col, team); if (MyAnts.Count >= 125) { ant.type = Ant.ATTACKER_ANT; } Location loc = new Location(row, col); ants[loc] = ant; if (mine) { MyAnts.Add(ant); //do some sanity checking before moving the ant; check //if the ant is not moving on to our hill and not moving to an expected location //of a friendly ant if (!isMyHill(ant.Row, ant.Col) && expectedLocation.ContainsKey(loc)) { ant.type = expectedLocation[new Location(ant.Row, ant.Col)].type; } } }
/// <summary> /// Returns the position that the Ant <paramref name="ant"/> will be in the next turn. /// </summary> /// <param name="ant">The ant to be checked</param> /// <returns>the position that the Ant <paramref name="ant"/> will be in the next turn if it exists, /// <c>null</c> otherwise.</returns> public Location GetNextTurnLocation(Ant ant) { foreach (Direction direction in Enum.GetValues(typeof(Direction))) { Location loc = GetDestination(ant, direction); if (this.MyAntsNextTurn[loc.Row, loc.Col] != null && ant.Id == this.MyAntsNextTurn[loc.Row, loc.Col].Id) { return loc; } } return null; }
//tells the gamestate that an ant will be moved next turn public void MoveAnt(Ant ant, Direction direction) { Location newLoc = this.GetDestination(ant, direction); MyAntsNextTurn[newLoc.Row, newLoc.Col] = ant; MyAntsNextTurn[ant.Row, ant.Col] = null; }
private void DontMove(Ant ant) { ant.HasOrder = true; antDestinations.Add(ant); }
public void populate() { int redID = 0, blackID = 0; for (int i = 0; i < 150; i++) { for (int j = 0; j < 150; j++) { if (tiles[j, i].getAntHill()) { if (tiles[j, i].getColour().ToLower().Equals("red")) { redAnts[redID] = new Ant(this, this, "red", j, i, 0, redID); redID++; } else if (tiles[j, i].getColour().ToLower().Equals("black")) { blackAnts[blackID] = new Ant(this, this, "black", j, i, 0, blackID); blackID++; } } } } }
public void AddAnt(int col, int row, int team) { if (team == 0) { var ant = new MyAnt { X = col, Y = row }; var index = MyAnts.BinarySearch(ant); if (index < 0) { MyAnts.Insert(~index, ant); NewAnt(ant); ant.Live = true; ant.Home = Hills.Where(x => x.DirectDistanceTo(ant) == Hills.Min(h => h.DirectDistanceTo(ant))).First(); } else { ApplyAntVision(ant); MyAnts[index].Live = true; } } else { var ant = new Ant { X = col, Y = row, Team = team }; EnemyAnts.Add(ant); } Map[col, row] = Tile.Ant; }
public override void Turn() { age++; // check if there's enough food if (food <= DIE_FOOD) Die (); // check if it can reproduce if (age >= REPRODUCTION_AGE && food >= REPRODUCTION_FOOD) { Ant child = new Ant (field, x, y, generation + 1); child.food = this.food / 2; food = food / 2; field.AddFieldObject (child); } // if (food >= REPRODUCE_FOOD) { // // Ant child = new Ant (field, x, y, generation+1); // child.food = food * 2/3; // food = food * 2/3; // field.AddFieldObject (child); // } // foreach (FieldObject fieldObject in Neighbours()) // if (fieldObject is Grass) // Eat ((Grass)fieldObject); if (field.grass [x, y] > 0) { field.grass [x, y]--; food += GRASS_FOOD; } Move (brain.Compute (new AntNavigationContext (this))); food--; }
public static void AddAnt(int row, int col, int team) { GameObject[] objects = map[row, col].GetObjectsOnTile(); for (int i = 0; i < objects.Length; i++) { Ant a = objects[i] as Ant; if (a != null) { a.StaleLength = 0; return; } } Ant newAnt = new Ant(row, col, team); if (team == 0) MyAnts.Add(newAnt); else EnemyAnts.Add(newAnt); }
private bool explore(GameState state, int[,] visibility, Ant ant) { ////find the nearest undiscovered spot and go in that direction // TODO if two direction are in the way of the "unknown", select the one with the most spread with the other ants { int value = visibility.At(ant); if (value < unknownExplorationDistance) { Direction dir = Direction.North; foreach (Direction direction in Ants.Aim.Keys) { Location newLoc = state.GetDestination(ant, direction); if (state.GetIsPassable(newLoc) && !state.OccupiedNextRound.At(newLoc) && value > visibility.At(newLoc)) { value = visibility.At(newLoc); dir = direction; } } if (value != visibility.At(ant)) { IssueOrder(state, ant, dir, "Explore"); return(true); } } } //try distanciate other ants //var AllyProximity = calculateAllyProximity(state, ant); //if (AllyProximity.At(ant) < 20) //{ // int value = AllyProximity.At(ant); // Direction dir = Direction.North; // foreach (Direction direction in Ants.Aim.Keys) // { // Location newLoc = state.GetDestination(ant, direction); // if (state.GetIsPassable(newLoc) && !state.OccupiedNextRound.At(newLoc) && value < AllyProximity.At(newLoc)) // { // value = AllyProximity.At(newLoc); // dir = direction; // } // } // if (value != AllyProximity.At(ant)) // { // IssueOrder(state, ant, dir); // return true; // } //} ////try all the directions //foreach (Direction direction in Ants.Aim.Keys) //{ // // GetDestination will wrap around the map properly // // and give us a new location // Location newLoc = state.GetDestination(ant, direction); // // GetIsPassable returns true if the location is land // if (state.GetIsPassable(newLoc)) // { // IssueOrder(ant, direction); // // stop now, don't give 1 and multiple orders // return true; // } //} return(false); }
// Local planning (for individual ant) public void WalkThatWay(Ant a, IGameState state) { if (heatMap.GetCell(a) > 0.1f) { heatMap.UpdateCell(a, -0.1f); } string key = LocationToKey(a); if (!currentTasks.ContainsKey(key)) { if (state.MyAnts.Count / 8 > martinSheen.Count && timRobbins.Count > 0) { Location martin = timRobbins[timRobbins.Count - 1]; timRobbins.RemoveAt(timRobbins.Count - 1); martinSheen.Add(martin); currentTasks.Add(key, new CurrentTask(Task.Guaaard, martin)); } else if (state.MyAnts.Count >= armySize) { return; } else { LogShit("Shitjeweet.txt", currentStep + " added or something youknowyourself"); currentTasks.Add(key, new CurrentTask(Task.Roaming, a)); } } CurrentTask task = currentTasks[key]; task.from = a; task.resolved = false; if (task.task != Task.Terminating && task.task != Task.Guaaard) { foreach (Location e in theirHills) { if (state.GetDistance(a, e) <= raidRadius) { task.hill = e; task.task = Task.Terminating; break; } } } if (task.task == Task.Terminating) { if (task.hill.Equals(a)) { theirHills.Remove(task.hill); heatMap.ResetCell(task.hill); } if (!theirHills.Contains(task.hill)) { task.task = Task.Roaming; task.roam = a; } } if (task.task == Task.Dinner) { if (task.food.Equals(a) || (state[task.food.Row, task.food.Col] != Tile.Food && state.GetIsVisible(task.food))) { task.task = Task.Roaming; yummies.Remove(task.food); } } if (task.task == Task.Roaming) { Location f = SearchFood(a, state); if (f != null) { task.food = f; task.task = Task.Dinner; yummies.Add(f); } if (task.roam.Equals(a) || !state.GetIsPassable(task.roam) || state.GetDistance(a, task.roam) <= arrivalRadius) { heatMap.UpdateCell(task.roam, -5f); task.roam = GetNewDestination(a, state); if (task.roam == null) { task.to = task.from; return; } } } List <Location> avoid = new List <Location>(); avoid.AddRange(state.MyHills); avoid.AddRange(martinSheen); if (task.task == Task.Guaaard) { avoid.Remove(task.roam); } Location tgt = null; switch (task.task) { case Task.Roaming: tgt = task.roam; break; case Task.Dinner: tgt = task.food; break; case Task.Terminating: tgt = task.hill; break; case Task.Guaaard: tgt = task.roam; break; } bool recalc = false; if (task.route == null) { recalc = true; } else { foreach (Location l in task.route) { if (!state.GetIsPassable(l)) { recalc = true; break; } } } Location next = null; if (recalc || true) { task.route = Pathfinding.FindPath(a, tgt, state, avoid, influenceMaps); if (task.route != null) { next = task.route[0]; } } //Location next = Pathfinding.FindNextLocation(a, tgt, state, avoid); if (next == null) { task.to = task.from; return; } else { task.to = next; } }
private void TurnLeft(Ant ant) { if (ant.speedX == 1) { ant.speedX = 0; ant.speedY = -1; } else if (ant.speedX == -1) { ant.speedX = 0; ant.speedY = 1; } else if (ant.speedY == 1) { ant.speedY = 0; ant.speedX = 1; } else if (ant.speedY == -1) { ant.speedY = 0; ant.speedX = -1; } }
// Local planning (for individual ant) public void WalkThatWay(Ant a, IGameState state) { if (heatMap.GetCell(a) > 0.1f) heatMap.UpdateCell(a, -0.1f); string key = LocationToKey(a); if (!currentTasks.ContainsKey(key)) { if (state.MyAnts.Count / 8 > martinSheen.Count && timRobbins.Count > 0) { Location martin = timRobbins[timRobbins.Count - 1]; timRobbins.RemoveAt(timRobbins.Count - 1); martinSheen.Add(martin); currentTasks.Add(key, new CurrentTask(Task.Guaaard, martin)); } else if (state.MyAnts.Count >= armySize) { return; } else { LogShit("Shitjeweet.txt", currentStep + " added or something youknowyourself"); currentTasks.Add(key, new CurrentTask(Task.Roaming, a)); } } CurrentTask task = currentTasks[key]; task.from = a; task.resolved = false; if (task.task != Task.Terminating && task.task != Task.Guaaard) { foreach (Location e in theirHills) { if (state.GetDistance(a, e) <= raidRadius) { task.hill = e; task.task = Task.Terminating; break; } } } if (task.task == Task.Terminating) { if (task.hill.Equals(a)) { theirHills.Remove(task.hill); heatMap.ResetCell(task.hill); } if (!theirHills.Contains(task.hill)) { task.task = Task.Roaming; task.roam = a; } } if (task.task == Task.Dinner) { if (task.food.Equals(a) || (state[task.food.Row, task.food.Col] != Tile.Food && state.GetIsVisible(task.food))) { task.task = Task.Roaming; yummies.Remove(task.food); } } if (task.task == Task.Roaming) { Location f = SearchFood(a, state); if (f != null) { task.food = f; task.task = Task.Dinner; yummies.Add(f); } if (task.roam.Equals(a) || !state.GetIsPassable(task.roam) || state.GetDistance(a, task.roam) <= arrivalRadius) { heatMap.UpdateCell(task.roam, -5f); task.roam = GetNewDestination(a, state); if (task.roam == null) { task.to = task.from; return; } } } List<Location> avoid = new List<Location>(); avoid.AddRange(state.MyHills); avoid.AddRange(martinSheen); if (task.task == Task.Guaaard) avoid.Remove(task.roam); Location tgt = null; switch (task.task) { case Task.Roaming: tgt = task.roam; break; case Task.Dinner: tgt = task.food; break; case Task.Terminating: tgt = task.hill; break; case Task.Guaaard: tgt = task.roam; break; } bool recalc = false; if (task.route == null) recalc = true; else foreach (Location l in task.route) if (!state.GetIsPassable(l)) { recalc = true; break; } Location next = null; if (recalc || true) { task.route = Pathfinding.FindPath(a, tgt, state, avoid, influenceMaps); if (task.route != null) next = task.route[0]; } //Location next = Pathfinding.FindNextLocation(a, tgt, state, avoid); if (next == null) { task.to = task.from; return; } else { task.to = next; } }