List <MoveData> MoveFromHill(MyAnt ant, GameState state) { var result = new List <MoveData>(); if (ant.Home == null) { return(result); } if (ant.PointFromHill != null) { return(result); } var fromHillDirections = new int[4]; var stochastics = new int[4]; var locs = new Location[4]; var count = 0; for (int i = 0; i < 4; i++) { if (ant.RightDirections[i]) { var newLoc = ant + PathFinding.Directions[i]; if (state.NextTurnPreview[newLoc.X, newLoc.Y]) { continue; } if (ant.Home.DistanceMap[ant.X, ant.Y] < ant.Home.DistanceMap[newLoc.X, newLoc.Y] && ant.Home.DistanceMap[ant.X, ant.Y] != -1) { if (ant.Home.StochasticMap[newLoc.X, newLoc.Y] == 0) { ant.Home.StochasticMap[newLoc.X, newLoc.Y] = PathFinding.GetTotalLeafCount(newLoc, ant.Home); } fromHillDirections[count] = i; stochastics[count] = ant.Home.StochasticMap[newLoc.X, newLoc.Y]; locs[count] = newLoc; count++; } } } if (count == 0 || stochastics.Sum() < 20) { return(result); } else { int r = rnd.Next(stochastics.Sum()); int sum = 0; for (int i = 0; i < count; i++) { if (r >= sum && r < sum + stochastics[i]) { return new List <MoveData> { new MoveData(locs[i], (Direction)fromHillDirections[i]) } } ; sum += stochastics[i]; } return(result); } } List <MoveData> MoveFromHillFast(MyAnt ant, GameState state) { var result = new List <MoveData>(); if (ant.PointFromHill != null) { return(result); } var fromHillDirections = new bool[4]; for (int i = 0; i < 4; i++) { if (ant.RightDirections[i]) { var newLoc = ant + PathFinding.Directions[i]; if (state.NextTurnPreview[newLoc.X, newLoc.Y]) { continue; } if (ant.Home.DistanceMap[ant.X, ant.Y] <= ant.Home.DistanceMap[newLoc.X, newLoc.Y] && ant.Home.DistanceMap[ant.X, ant.Y] != -1) { result.Add(new MoveData(newLoc, (Direction)i)); } } } return(result); } List <MoveData> SpecificMoveFromHill(MyAnt ant, GameState state) { if (ant.Home == null) { return(new List <MoveData>()); } if (ant.PointFromHill != null) { var result = new List <MoveData>(); for (int i = 0; i < 4; i++) { if (ant.RightDirections[i]) { var newLoc = ant + PathFinding.Directions[i]; if (!state.NextTurnPreview[newLoc.X, newLoc.Y]) { if (ant.SpecificPath[newLoc.X, newLoc.Y] != -1 && ant.SpecificPath[newLoc.X, newLoc.Y] < ant.SpecificPath[ant.X, ant.Y]) { result.Add(new MoveData(newLoc, (Direction)i)); } } } } return(result); } int radius = Math.Min(30, Math.Min(state.Width - 1, state.Height - 1)); do { var d = new Location(); for (int dCoo = 0; dCoo < radius; dCoo++) { for (int k = 0; k < 4; k++) { d.X = dCoo * (k / 2 == 0 ? -1 : 1); d.Y = (radius - dCoo) * (k % 2 == 0 ? -1 : 1); var loc = ant + d; if (!state.NextTurnPreview[loc.X, loc.Y]) { if (ant.Home.DistanceMap[loc.X, loc.Y] > ant.Home.DistanceMap[ant.X, ant.Y]) { if (ant.Home.StochasticMap[loc.X, loc.Y] == 0) { ant.Home.StochasticMap[loc.X, loc.Y] = PathFinding.GetTotalLeafCount(loc, ant.Home); } if (ant.Home.StochasticMap[loc.X, loc.Y] < 20) { continue; } ant.PointFromHill = loc; if (ant.SpecificPath == null) { ant.SpecificPath = new int[state.Width, state.Height]; } PathFinding.FillArray(loc, ant.SpecificPath, ant); var result = new List <MoveData>(); for (int i = 0; i < 4; i++) { if (ant.RightDirections[i]) { var newLoc = ant + PathFinding.Directions[i]; if (!state.NextTurnPreview[newLoc.X, newLoc.Y]) { if (ant.SpecificPath[newLoc.X, newLoc.Y] != -1 && ant.SpecificPath[newLoc.X, newLoc.Y] < ant.SpecificPath[ant.X, ant.Y]) { result.Add(new MoveData(newLoc, (Direction)i)); } } } } return(result); } } } } radius++; }while (radius < state.Width && radius < state.Height); return(new List <MoveData>()); } List <MoveData> MoveRandom(MyAnt ant, GameState state) { var result = new List <MoveData>(); for (int i = 0; i < 4; i++) { if (ant.RightDirections[i]) { var newLoc = ant + PathFinding.Directions[i]; if (!state.NextTurnPreview[newLoc.X, newLoc.Y]) { result.Add(new MoveData(newLoc, (Direction)i)); } } } return(result); }
public void EndTurnInput() { // Water CheckSymetryMap(); // Foods for (int i = 0; i < Foods.Count; i++) { if (!Foods[i].IsVisible && VisibleMap[Foods[i].X, Foods[i].Y]) { Map[Foods[i].X, Foods[i].Y] = Tile.Land; Foods.RemoveAt(i); i--; } } foreach (var food in Foods) { foreach (var water in NewWater) { if (food.DistanceMap[water.X, water.Y] > 0) { food.NeedRecalcDistanceMap = true; break; } } } foreach (var food in Foods) { if (food.NeedRecalcDistanceMap) { PathFinding.FillArray(food, food.DistanceMap); } } // Enemy hills for (int i = 0; i < EnemyHills.Count; i++) { if (!EnemyHills[i].IsVisible && VisibleMap[EnemyHills[i].X, EnemyHills[i].Y]) { Map[EnemyHills[i].X, EnemyHills[i].Y] = Tile.Land; EnemyHills.RemoveAt(i); i--; } } foreach (var hill in EnemyHills) { foreach (var water in NewWater) { if (hill.DistanceMap[water.X, water.Y] > 0) { hill.NeedRecalcDistanceMap = true; break; } } } foreach (var hill in EnemyHills) { if (hill.NeedRecalcDistanceMap) { PathFinding.FillArray(hill, hill.DistanceMap); } } // My hills for (int i = Hills.Count - 1; i >= 0; i--) { if (!Hills[i].Live && VisibleMap[Hills[i].X, Hills[i].Y]) { var oldHill = Hills[i]; Hills.RemoveAt(i); foreach (var ant in MyAnts) { if (object.ReferenceEquals(ant.Home, oldHill)) { if (Hills.Count > 0) { ant.Home = Hills.Where(x => x.DirectDistanceTo(ant) == Hills.Min(h => h.DirectDistanceTo(ant))).First(); } else { ant.Home = null; } } } } } foreach (var hill in Hills) { foreach (var water in NewWater) { if (hill.DistanceMap[water.X, water.Y] > 0) { hill.NeedRecalcDistanceMap = true; break; } } } foreach (var hill in Hills) { if (hill.NeedRecalcDistanceMap) { PathFinding.FillArray(hill, hill.DistanceMap); for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { hill.StochasticMap[x, y] = 0; } } } } // My ants for (int i = MyAnts.Count - 1; i >= 0; i--) { if (!MyAnts[i].Live) { MyAnts.RemoveAt(i); } } for (int i = 0; i < MyAnts.Count; i++) { if (MyAnts[i].PointFromHill != null) { for (int j = 0; j < NewWater.Count; j++) { if (MyAnts[i].SpecificPath[NewWater[j].X, NewWater[j].Y] != -1) { PathFinding.FillArray(MyAnts[i].PointFromHill, MyAnts[i].SpecificPath, MyAnts[i]); break; } } } } foreach (var ant in MyAnts) { ant.ClearDirections(); } RefreshDirections(); EnemyAnts.Sort(); }