public override void DoTurn(GameState state) { try { Log.Debug("Starting Turn " + turn++);// state.TimeRemaining //FoodRadius = (int)Math.Sqrt(state.ViewRadius2); //Calculate proximity //var FoodProximity = calculateFoodProximity(state); var visibility = calculateVisibilityProximity(state); var enemyProximity = CalculateProximity(state, state.EnemyHills.Union <TeamLocation>(state.EnemyAnts).ToList(), 200); var antMoved = new List <Ant>(); var ennemyConsidered = new List <Ant>(); //Attack/defend foreach (Ant enemy in state.EnemyAnts) { if (ennemyConsidered.Contains(enemy)) { continue; } if (Math.Sqrt(state.ViewRadius2) - visibility.At(enemy) < BattleRadius) { var battle = new Battle(); battle.StartBattle(state, state.MyAnts.Except(antMoved).OrderBy(a => state.GetDistance(enemy, a)).First(), enemy, enemyProximity); ennemyConsidered.AddRange(battle.EnemyPlatoon); antMoved.AddRange(battle.AllyPlatoon); } } { //Food.getAnt var antLookingForFood = state.MyAnts.Except(antMoved).ToList(); var antGatheringFood = new Dictionary <Ant, TargetWithDir <Ant> >(); var foodqueue = new Queue <Location>(state.FoodTiles.Union(state.EnemyHills)); while (foodqueue.Count > 0) { var food = foodqueue.Dequeue(); var result = FindClosest(state, food, x => antLookingForFood.Select(ant => (x.Row == ant.Row && x.Col == ant.Col) ? ant : null).Where(y => y != null).FirstOrDefault()); Log.Trace("Food.GetAnt : Food{0} => Ant {1}", food, result?.Target); if (result != null) { if (antGatheringFood.ContainsKey(result.Target)) { Log.Trace("Food.GetAnt : ANT already selected at Food{0} => Ant {1}", antGatheringFood[result.Target].Source, result.Target); if (antGatheringFood[result.Target].Dist > result.Dist) { foodqueue.Enqueue(antGatheringFood[result.Target].Source); } else { continue; } } var dir = result.Dir.Opposite(); Location newLoc = state.GetDestination(result.Target, dir); antGatheringFood.Add(result.Target, result); antLookingForFood.Remove(result.Target); } } foreach (var item in antGatheringFood) { var result = item.Value; var dir = result.Dir.Opposite(); Location newLoc = state.GetDestination(result.Target, dir); if (!state.OccupiedNextRound.At(newLoc)) { IssueOrder(state, result.Target, dir, "GetFood"); } } antMoved.AddRange(antGatheringFood.Keys); } var antToMove = state.MyAnts.Except(antMoved).ToList(); foreach (Ant ant in antToMove) { // check if we have time left to calculate more orders if (state.TimeRemaining < 10) { break; } // General game signals // Defend hill -> converge + sacrifice self //continue if move done //if (getFood(state, FoodProximity, ant)) //Replace par food.getAnt() above // continue; if (explore(state, visibility, ant)) { continue; } goToFront(state, enemyProximity, ant); } } catch (Exception e) { Log.Debug(e.ToString()); } //For debugging //while (state.TimeRemaining > 10) continue; }
/// <summary> /// Calculate the current diffusion scores for layer 1. /// </summary> /// <param name="state">The state.</param> public void diffuseOne(GameState state) { for (int row = 0; row < state.Height; row++) { for (int col = 0; col < state.Width; col++) { if (state.map[row, col] == Tile.Water) { state.diffusionOne[row, col] = WATER_SCORE; } else if (state.map[row, col] == Tile.Ant) { state.diffusionOne[row, col] = MY_ANT_SCORE_D1; Location location = new Location(row, col); if (state.ants.ContainsKey(location)) { Ant ant = state.ants[location]; if (ant.Team != 0) { List<AntHill> myHills = state.MyHills; foreach (AntHill myHill in myHills) { if (state.GetDistance(myHill, ant) <= 12) { state.diffusionOne[row, col] = HILL_IN_DANGER; break; } } } } } else if (state.map[row, col] == Tile.Food) { state.diffusionOne[row, col] = FOOD_SCORE_D1; } else if (state.map[row, col] == Tile.Unseen) { state.diffusionOne[row, col] = UNSEEN_SCORE_D1; } else if (state.isEnemyHill(row, col)) { state.diffusionOne[row, col] = ENEMYHILL_SCORE_D1; } else if (state.isMyHill(row, col)) { state.diffusionOne[row, col] = MYHILL_SCORE; } else { double u = state.diffusionOne[row, col]; Location L = new Location(row, col); Location up = state.GetDestination(L, Direction.North); Location right = state.GetDestination(L, Direction.East); Location left = state.GetDestination(L, Direction.West); Location down = state.GetDestination(L, Direction.South); state.diffusionOne[row, col] = u + D1_COEFFICIENT * (1 + state.diffusionOne[up.Row, up.Col] + state.diffusionOne[down.Row, down.Col] + state.diffusionOne[left.Row, left.Col] + state.diffusionOne[right.Row, right.Col] - u * 4); } } } }
private Goal GatherFood(AntLoc ant, GameState state) { if (!state.FoodTiles.Any()) return Scout(ant, state); Log("foodtiles: " + state.FoodTiles.Count); // look for some food var searchingspots = state.FoodTiles.Where(f => Goals.Count(g => g.EndPoint.Equals(f))==0); Log("searchingspots: " + searchingspots.Count()); //searchingspots = searchingspots.Where(f => f.GetDistance(ant) < GetSearchRadius(state)).OrderBy(f => f.GetDistance(ant)).Take(2); var foodpoints = searchingspots.Select(f => new { Tile = f, Point = f.ToPoint(), Distance = state.GetDistance(ant,f)//astar.Search(ant.ToPoint(), f.ToPoint(), new Object()) ?? Enumerable.Empty<Tile>() }); var closest = foodpoints.OrderBy(f => f.Distance).FirstOrDefault(); if (closest != null) { Log("closest food:" + closest.Tile); return new Goal(closest.Tile, GetPath(ant,closest.Tile,state), (g => !g.HasFood(closest.Tile)), Strategy.GatherFood); } return SpreadOut(ant, state); }
private void SetTurnStrategy(GameState state) { foreach (var a in state.MyAnts) { Log("ant loc: " + a); } Location mycenter = new Location(state,(int)Math.Round(state.MyAnts.Average(a => a.Row)), (int)Math.Round(state.MyAnts.Average(a => a.Col))); Log("ants center:" + mycenter); int totalfriends = state.MyAnts.Count; int closefriends = state.MyAnts.Count(a => state.GetDistance(a, mycenter) <= state.AttackRadius2 * CLOSE_FACTOR); Log("friends:" + totalfriends + "," + closefriends); int totalenemies = state.EnemyAnts.Count; int closeenemies = state.EnemyAnts.Count(a => state.GetDistance(a, mycenter) <= state.AttackRadius2 * CLOSE_FACTOR); Log("enemies:" + totalenemies + "," + closeenemies); int totalfood = state.FoodTiles.Count; int closefood = state.FoodTiles.Count(f => state.GetDistance(f, mycenter) <= state.AttackRadius2 * CLOSE_FACTOR); Log("food:" + totalfood + "," + closefood); if (closefood > 0 || totalfood > 0) AlterStrategy(Strategy.GatherFood, Preference.MildlyEncourage); else { AlterStrategy(Strategy.Scout, Preference.StronglyEncourage); AlterStrategy(Strategy.Scout, Preference.StronglyEncourage); AlterStrategy(Strategy.Scatter, Preference.StronglyEncourage); AlterStrategy(Strategy.SpreadOut, Preference.StronglyEncourage); AlterStrategy(Strategy.SpreadOut, Preference.StronglyEncourage); } if (closeenemies > closefriends) { AlterStrategy(Strategy.Fight, Preference.StronglyDiscourage); AlterStrategy(Strategy.Condense, Preference.MildlyEncourage); AlterStrategy(Strategy.Scatter, Preference.MildlyDiscourage); AlterStrategy(Strategy.Retreat, Preference.StronglyEncourage); } else if (closeenemies == closefriends && closeenemies > 0) { AlterStrategy(Strategy.Condense, Preference.StronglyEncourage); AlterStrategy(Strategy.Scatter, Preference.StronglyDiscourage); AlterStrategy(Strategy.Fight, Preference.MildlyEncourage); } else //if (closeenemies < closefriends) { if (closeenemies > 0) { AlterStrategy(Strategy.Fight, Preference.StronglyEncourage); AlterStrategy(Strategy.Retreat, Preference.StronglyDiscourage); AlterStrategy(Strategy.Condense, Preference.MildlyEncourage); AlterStrategy(Strategy.Scatter, Preference.StronglyDiscourage); } else { AlterStrategy(Strategy.Scout, Preference.MildlyEncourage); AlterStrategy(Strategy.Condense, Preference.MildlyDiscourage); AlterStrategy(Strategy.Scatter, Preference.MildlyEncourage); } } if (totalfriends < totalenemies && totalenemies > 0) { if (closefood > closeenemies) { AlterStrategy(Strategy.GatherFood, Preference.StronglyEncourage); AlterStrategy(Strategy.Fight, Preference.MildlyDiscourage); AlterStrategy(Strategy.Retreat, Preference.MildlyDiscourage); AlterStrategy(Strategy.Scatter, Preference.MildlyEncourage); } else { AlterStrategy(Strategy.Scout, Preference.StronglyEncourage); AlterStrategy(Strategy.Fight, Preference.MildlyDiscourage); AlterStrategy(Strategy.Scatter, Preference.MildlyEncourage); } } else //if (totalfriends > totalenemies) { if (totalenemies > 0) { AlterStrategy(Strategy.Fight, Preference.StronglyEncourage); } AlterStrategy(Strategy.Scout, Preference.MildlyEncourage); AlterStrategy(Strategy.Scatter, Preference.MildlyEncourage); } }
private Goal Fight(AntLoc ant, GameState state) { if (!state.EnemyAnts.Any()) return Scout(ant, state); // look for some food var searchingspots = state.EnemyAnts.Where(f => Goals.Count(g => g.EndPoint.Equals(f)) <= 1); //searchingspots = searchingspots.Where(f => f.GetDistance(ant) < GetSearchRadius(state)).OrderBy(f => f.GetDistance(ant)).Take(2); var antpoint = ant.ToPoint(); var enemypoints = searchingspots.Select(f => new { Tile = f, Point = f.ToPoint(), Distance = state.GetDistance(ant,f)//astar.Search(ant.ToPoint(), f.ToPoint(), new Object()) ?? Enumerable.Empty<Tile>() }); var closest = enemypoints.OrderBy(f => f.Distance).FirstOrDefault(); if (closest != null) { var goal = new Goal(closest.Tile, GetPath(ant, closest.Tile, state), (g => g.IsUnoccupied(closest.Tile)), Strategy.Fight); return goal; } return SpreadOut(ant, state); }