예제 #1
0
    public static IEnumerable <Point> FindPointsInCirclesNearestTargets(int pointCount, IEnumerable <Circle> starts, IEnumerable <Point> targets, IEnumerable <Circle> avoids)
    {
        if (pointCount <= 0)
        {
            return(new Point[] { });
        }

        var avoidEdgedStarts = avoids.SelectMany(c => Trig.CalcOuterEdgeOfCircle(c))
                               .Where(p => starts.Any(c => c.IsInRange(p)));

        var potentialStarts = starts.SelectMany(c => Trig.CalcInnerEdgeOfCircle(c))
                              .Concat(avoidEdgedStarts)
                              .Where(p => p.IsOnBoard())
                              .Where(p => !avoids.Any(c => c.IsInRange(p)))
                              .Distinct();

        return(potentialStarts.MinByValue(pointCount, p => targets.Min(t => Trig.Distance(p, t))));
    }
예제 #2
0
    public static Pather.AStar Search(IEnumerable <Point> starts, int stepSize, Point goal, int goalRange, bool avoidPools = true)
    {
        var goalEdge             = Trig.CalcInnerEdgeOfCircle(new Circle(goal, goalRange));
        var additionalNeighboors = goalEdge.Where(n => IsPassable(n)).ToHashSet();

        Func <Point, IEnumerable <Point> > getNeighboors = p =>
        {
            return(Trig.CalcInnerEdgeOfCircle(new Circle(p, stepSize))
                   .Concat(additionalNeighboors)
                   .Where(n => IsPassable(n, avoidPools) && Trig.IsInRange(p, n, stepSize)));
        };

        var astar = new Pather.AStar(
            starts,
            p => Trig.IsInRange(p, goal, goalRange),
            (a, b) => 1,
            p => (int)Math.Ceiling(Trig.Distance(p, goal) / (double)stepSize),
            getNeighboors);

        return(astar);
    }
예제 #3
0
    public static Point Spawn(int plantType, Point goal, int goalRange, bool avoidPools = true)
    {
        if (AI.me.Spores < AI.sporeCosts[plantType])
        {
            return(new Point(-1, -1));
        }

        var uprootRange          = Bb.GetUprootRange(plantType);
        var starts               = Bb.ourMother.Concat(Bb.ourSpawners);
        var goalEdge             = Trig.CalcInnerEdgeOfCircle(new Circle(goal, goalRange));
        var additionalNeighboors = goalEdge.Where(n => IsPassable(n)).ToHashSet();

        Func <Point, IEnumerable <Point> > getNeighboors = p =>
        {
            var stepSize = Bb.plantLookup.ContainsKey(p) ? p.GetPlant().Range : uprootRange;
            return(Trig.CalcInnerEdgeOfCircle(new Circle(p, stepSize))
                   .Concat(additionalNeighboors)
                   .Where(n => IsPassable(n, avoidPools) && Trig.IsInRange(p, n, stepSize)));
        };

        var astar = new Pather.AStar(
            starts,
            p => Trig.IsInRange(p, goal, goalRange) && IsPassable(p, avoidPools),
            (a, b) => 1,
            p => (int)Math.Ceiling(Trig.Distance(p, goal) / (double)uprootRange),
            getNeighboors);

        if (astar.Path.Count() > 1)
        {
            var p = astar.Path.ElementAt(1);
            AI.me.germinate(p.x, p.y, plantType);
            Bb.spawning.Add(p);
            return(p);
        }
        return(new Point(-1, -1));
    }
예제 #4
0
    public static void Main(string[] args)
    {
        if (args.Length < 1)
        {
            var c = new Circle(1, 1, 1);
            Console.WriteLine("IN");
            var ps = Trig.CalcPointsInCircle(c);
            ps.ForEach(p => Console.WriteLine(p));
            Console.WriteLine("OUTER EDGE");
            ps = Trig.CalcOuterEdgeOfCircle(c);
            ps.ForEach(p => Console.WriteLine(p));
            Console.WriteLine("INNER EDGE");
            ps = Trig.CalcInnerEdgeOfCircle(c);
            ps.ForEach(p => Console.WriteLine(p));

            /*
             * var ps = Solver.FindPointsInCirclesNearestTargets(
             *  2,
             *  new Circle(1, 1, 5).Single(),
             *  new Point(20, 1).Single(),
             *  new Circle(5, 1, 5).Single());
             * ps.ForEach(p => Console.WriteLine(p));
             */

            System.Console.WriteLine("Please enter a hostname");
            return;
        }

        IntPtr connection = Client.createConnection();

        AI ai = new AI(connection);

        if (Client.serverConnect(connection, args[0], "19000") == 0)
        {
            System.Console.WriteLine("Unable to connect to server");
            return;
        }

        if (Client.serverLogin(connection, ai.username(), ai.password()) == 0)
        {
            return;
        }

        if (args.Length < 2)
        {
            Client.createGame(connection);
        }
        else
        {
            Client.joinGame(connection, Int32.Parse(args[1]), "player");
        }

        while (Client.networkLoop(connection) != 0)
        {
            if (ai.startTurn())
            {
                Client.endTurn(connection);
            }
            else
            {
                Client.getStatus(connection);
            }
        }

        Client.networkLoop(connection); //Grab end game state
        Client.networkLoop(connection); //Grab log
        ai.end();
    }