Exemple #1
0
    private void AITowerBeforeMove()
    {
        var potentialTargets = MyPositions.Where(p => IsMyBorder(p) && !IsDefended(p)).ToList();

        var potentialTargetsExtended = new List <Position>();

        foreach (var target in potentialTargets)
        {
            potentialTargetsExtended.AddRange(target.Arounds(true).Where(p => !IsDefended(p) && Map[p.X, p.Y].IsOwned).ToList());
        }
        // Make them distinct
        potentialTargets = potentialTargetsExtended.GroupBy(i => (i.X, i.Y)).Select(x => x.First()).ToList();
        potentialTargets.RemoveAll(p => p.Arounds(true).Where(pA => IsMyBorder(pA) && Map[pA.X, pA.Y].IsOccupied && !IsDefended(pA)).ToList().Count < 2);
        potentialTargets.RemoveAll(p => Map[p.X, p.Y].HasMineSpot);

        while (MyGold > BUILD_COST_TOWER && potentialTargets.Count > 0)
        {
            potentialTargets.Sort(delegate(Position p1, Position p2)
            {
                int count1 = p1.Arounds(true).Where(p => IsMyBorder(p) && Map[p.X, p.Y].IsOccupied && !IsDefended(p)).ToList().Count;
                int count2 = p2.Arounds(true).Where(p => IsMyBorder(p) && Map[p.X, p.Y].IsOccupied && !IsDefended(p)).ToList().Count;
                if (count1 != count2)
                {
                    return(count2 - count1);
                }
                else
                {
                    return((int)(Math.Abs(11 - (p1.X + p1.Y)) - Math.Abs(11 - (p2.X + p2.Y))));
                }
                // Or closer to our Hq ??
            });

            var target       = potentialTargets[0];
            int soldierCount = target.Arounds(true).Where(p => IsMyBorder(p) && Map[p.X, p.Y].IsOccupied && !IsDefended(p)).ToList().Count;
            if (soldierCount < 2)
            {
                break;
            }
            else
            {
                List <Unit> onTheWayUnits = MyUnits.FindAll(u => u.Position.Arounds(true).Exists(p => p == target));
                FreePlace(target);
                Build("TOWER", target);
                potentialTargets.RemoveAt(0);
            }

            //potentialTargets.RemoveAll(p => IsDefended(p));
            potentialTargets.RemoveAll(p => p.Arounds(true).Where(pA => IsMyBorder(pA) && Map[pA.X, pA.Y].IsOccupied && !IsDefended(pA)).ToList().Count < 2);
        }
    }
Exemple #2
0
    // Start new AI =======================================================================

    private bool TryToWin()
    {
        var myBorders = MyPositions.Where(p => IsMyBorder(p)).ToList();

        foreach (var border in myBorders)
        {
            var steps = PathFinding(border, OpponentHq);
            steps.RemoveAt(0);

            if (steps.Exists(p => Map[p.X, p.Y].IsOwned && Map[p.X, p.Y].Active))
            {
                continue;
            }

            if (steps.Count * TRAIN_COST_LEVEL_1 > MyGold)
            {
                continue;
            }

            int cost = 0;
            steps.ForEach(delegate(Position p)
            {
                if (Map[p.X, p.Y].IsOwned)
                {
                    cost += 0;
                }
                else if (IsDefended(p))
                {
                    int index = steps.IndexOf(p);
                    if (index == 0)
                    {
                        cost += TRAIN_COST_LEVEL_3;
                    }
                    else
                    {
                        var arounds = p.Arounds(true);
                        arounds.Remove(steps[index - 1]);
                        arounds.RemoveAll(pA => !(Map[pA.X, pA.Y].OccupiedBy is Building));
                        arounds.RemoveAll(pA => !(Map[pA.X, pA.Y].OccupiedBy as Building).IsTower);
                        if (arounds.Count > 0)
                        {
                            cost += TRAIN_COST_LEVEL_3;
                        }
                        else
                        {
                            cost += TRAIN_COST_LEVEL_1;
                        }
                    }
                }
                else if (!Map[p.X, p.Y].IsOccupied)
                {
                    cost += TRAIN_COST_LEVEL_1;
                }
                else if (Map[p.X, p.Y].OccupiedBy is Unit)
                {
                    switch ((Map[p.X, p.Y].OccupiedBy as Unit).Level)
                    {
                    case 1:
                        cost += TRAIN_COST_LEVEL_2;
                        break;

                    case 2:
                        cost += TRAIN_COST_LEVEL_3;
                        break;

                    case 3:
                        cost += TRAIN_COST_LEVEL_3;
                        break;
                    }
                }
                else
                {
                    if ((Map[p.X, p.Y].OccupiedBy as Building).IsTower)
                    {
                        cost += TRAIN_COST_LEVEL_3;
                    }
                    else
                    {
                        cost += TRAIN_COST_LEVEL_1;
                    }
                }
            });

            if (cost <= MyGold)
            {
                Output.Clear();
                Wait();
                foreach (var p in steps)
                {
                    Console.Error.Write(p + " - ");
                }
                ApplyCut(in steps);
                Console.Error.WriteLine(Output);
                return(true);
            }
        }

        return(false);
    }