예제 #1
0
        public static void RunMarineMicroGame(SynchronousApiClient client)
        {
            if (!client.InitiateSinglePlayerGame(MARINE_MICRO_MAP_PATH, Race.Terran))
            {
                return;
            }

            var gameState = client.GetGameState();

            IBot bot = new SimpleMarineBot();

            while (true)
            {
                if (exit)
                {
                    client.LeaveGame();
                    break;
                }
                else
                {
                    var commands = bot.Act(gameState);
                    client.SendCommands(commands);
                    client.Step();
                    gameState = client.GetGameState();
                }

                // Exit once all of our units are dead - probably not going to be what we do in the long run,
                // but it works for the example map we're currently on.
                if (gameState.RawUnits.All(unit => unit.Alliance == Alliance.Enemy))
                {
                    exit = true;
                }
            }
        }
예제 #2
0
        public static void PlayAgainstStandardAI(IBot bot)
        {
            // TODO: One or more of the following:
            // 1. Get the process that this spawns, so we can terminate it on close
            // 2. Find a better way to spawn the game process directly, so we don't
            //    have to get a second process that this one spawns
            // 3. Check if there's already a waiting client so we can reuse it
            var info = new ProcessStartInfo(GAME_EXECUTABLE_PATH);

            info.WorkingDirectory = BASE_GAME_PATH + "/Support64";
            info.Arguments        = GAME_EXECUTABLE_ARGS_PLAYER1;

            using (Process gameProcess = Process.Start(info))
            {
                using (var client = new SynchronousApiClient("ws://127.0.0.1:5000/sc2api"))
                {
                    if (!client.InitiateGameAgainstComputer(LADDER_ABYSSAL_REEF_MAP_PATH, Race.Terran, Difficulty.MediumHard))
                    {
                        return;
                    }

                    var gameState = client.GetGameState();

                    //SaveMapData(gameState);

                    while (true)
                    {
                        var commands = bot.Act(gameState);
                        client.SendCommands(commands);
                        client.Step();
                        gameState = client.GetGameState();
                    }
                }
            }
        }
예제 #3
0
        public static void StartHarvesting(SynchronousApiClient client, GameState gameState)
        {
            var harvesters = gameState.Units.Where(unit => unit is TerranUnit terranUnit && terranUnit.TerranUnitType == TerranUnitType.SCV).ToList();
            var minerals   = gameState.NeutralUnits.Where(unit => unit.IsMineralDeposit);

            var commands = new List <Command>();

            foreach (var harvester in harvesters)
            {
                commands.Add(harvester.Harvest(harvester.GetClosest(minerals)));
            }

            client.SendCommands(commands);
        }
예제 #4
0
        public static void RunEmptyMapGame(SynchronousApiClient client)
        {
            if (!client.InitiateSinglePlayerGame(EMPTY_MAP_PATH, Race.Terran))
            {
                return;
            }

            var gameState = client.GetGameState();

            var playerId = gameState.GameInfo.PlayerInfo[0].PlayerId;

            var debugRequest = new Request {
                Debug = new RequestDebug()
            };

            debugRequest.Debug.Debug.Add(new DebugCommand {
                GameState = DebugGameState.Minerals
            });
            debugRequest.Debug.Debug.Add(
                new DebugCommand
            {
                CreateUnit = new DebugCreateUnit
                {
                    Owner = (int)playerId,
                    Pos   = new Point2D {
                        X = 15f, Y = 15f
                    },
                    Quantity = 1,
                    UnitType = gameState.UnitTypes.Values.Single(unit => string.Equals(unit.Name, "SCV")).UnitId
                }
            });

            var debugResponse = client.Call(debugRequest);

            for (var i = 0; i < 20; i++)
            {
                client.Step();
            }

            gameState = client.GetGameState();

            // Broke this, probably not much point in fixing it
            //client.SendCommands(new[] { new BuildCommand(gameState.RawUnits[0], TerranBuildingType.SupplyDepot, 15, 15) });

            while (true)
            {
                client.Step();
                gameState = client.GetGameState();
            }
        }
예제 #5
0
        public static ProxyStarcraft.Unit GetBuilder(BuildingOrUnitType buildingOrUnit, GameState gameState, SynchronousApiClient client)
        {
            // TODO: Exclude units/structures already building things
            var buildAction = gameState.Translator.GetBuildAction(buildingOrUnit);

            foreach (var unit in gameState.Units)
            {
                var abilities = client.GetAbilities(unit.Tag);

                if (abilities.Contains(buildAction))
                {
                    return(unit);
                }
            }

            return(null);
        }
예제 #6
0
        public static void PlayOneOnOne(IBot bot1, IBot bot2)
        {
            // TODO: One or more of the following:
            // 1. Get the process that this spawns, so we can terminate it on close
            // 2. Find a better way to spawn the game process directly, so we don't
            //    have to get a second process that this one spawns
            // 3. Check if there's already a waiting client so we can reuse it

            // TODO: Reduce duplication
            var info1 = new ProcessStartInfo(GAME_EXECUTABLE_PATH);

            info1.WorkingDirectory = BASE_GAME_PATH + "/Support64";
            info1.Arguments        = GAME_EXECUTABLE_ARGS_PLAYER1;

            var info2 = new ProcessStartInfo(GAME_EXECUTABLE_PATH);

            info2.WorkingDirectory = BASE_GAME_PATH + "/Support64";
            info2.Arguments        = GAME_EXECUTABLE_ARGS_PLAYER2;

            using (Process gameProcess1 = Process.Start(info1))
            {
                using (Process gameProcess2 = Process.Start(info2))
                {
                    using (var client1 = new SynchronousApiClient("ws://127.0.0.1:5000/sc2api"))
                    {
                        bot1.Register(client1);

                        using (var client2 = new SynchronousApiClient("ws://127.0.0.1:5001/sc2api"))
                        {
                            bot2.Register(client2);

                            var initiateGameSuccess = client1.InitiateGameAgainstBot(LADDER_ABYSSAL_REEF_MAP_PATH, bot1.Race, bot2.Race);

                            if (!client2.JoinGameAgainstBot(bot2.Race))
                            {
                                return;
                            }

                            if (!initiateGameSuccess.Result)
                            {
                                return;
                            }

                            var gameState1 = client1.GetGameState();
                            var gameState2 = client2.GetGameState();

                            //SaveMapData(gameState1);

                            while (true)
                            {
                                client1.SendCommands(bot1.Act(gameState1));
                                client2.SendCommands(bot2.Act(gameState2));

                                client1.Step();
                                client2.Step();

                                gameState1 = client1.GetGameState();
                                gameState2 = client2.GetGameState();
                            }
                        }
                    }
                }
            }
        }