예제 #1
0
        protected override bool TryExecuteTurn(Randomizer randomizer, IGameLayer gameLayer, GameState state)
        {
            // Only build when has nothing else to do
            var action = randomizer.GetRandomAction(x => x != GameActions.StartBuild);

            if (action == GameActions.Wait)
            {
                action = randomizer.GetRandomAction();
                if (action == GameActions.StartBuild)
                {
                    var building = state.AvailableResidenceBuildings.Find(x => x.BuildingName == "Cabin");
                    if (building.Cost > state.Funds)
                    {
                        Logger.LogWarning("Wanted to build building, but cannot afford it");
                        return(false);
                    }

                    // Prioritize building Cabins
                    var position = randomizer.GetRandomBuildablePosition();
                    if (position == null)
                    {
                        Logger.LogWarning("No valid positions to build building");
                        return(false);
                    }

                    gameLayer.StartBuild(position, building.BuildingName, state.GameId);
                    return(true);
                }
            }
            return(false);
        }
예제 #2
0
        protected override bool TryExecuteTurn(Randomizer randomizer, IGameLayer gameLayer, GameState state)
        {
            BlueprintUtilityBuilding building;
            var buildingName = BuildingName;

            if (!string.IsNullOrWhiteSpace(buildingName))
            {
                building = state.AvailableUtilityBuildings.Find(x => x.BuildingName == buildingName);
            }
            else
            {
                var affordableBuildings = state.AvailableUtilityBuildings
                                          .Where(x => x.Cost <= state.Funds)
                                          .ToArray();
                building = affordableBuildings.ElementAtOrDefault(randomizer.Random.Next(0, affordableBuildings.Length));
            }

            if (building == null)
            {
                // No valid building
                return(false);
            }

            var buildings = state.GetBuiltBuildings().Where(x => x.BuildingName == building.BuildingName).ToArray();

            if (buildings.Length >= MaxNumberOfBuildings)
            {
                // Don't build any more buildings
                return(false);
            }
            if (buildings.Any(x => x.BuildProgress < 100))
            {
                // Already one in progress
                return(false);
            }

            if (building.Cost > state.Funds)
            {
                // Cannot afford it
                Logger.LogWarning("Wanted to build building, but cannot afford it");
                return(false);
            }


            var position = randomizer.GetRandomBuildablePositionNearResidence();

            if (position == null || !state.IsBuildablePosition(position))
            {
                Logger.LogWarning("No valid positions to build utility building");
                return(false);
            }

            gameLayer.StartBuild(position, building.BuildingName, state.GameId);
            return(true);
        }
예제 #3
0
        protected override bool TryExecuteTurn(Randomizer randomizer, IGameLayer gameLayer, GameState state)
        {
            var currentBuildingCount = state.GetBuiltBuildings().Count();

            if (currentBuildingCount > 0)
            {
                return(false);
            }

            var position = randomizer.GetRandomBuildablePosition();

            if (position == null)
            {
                Logger.LogWarning("No valid positions to build building");
                return(false);
            }


            BlueprintResidenceBuilding building;
            var buildingName = BuildingName;

            if (!string.IsNullOrWhiteSpace(buildingName))
            {
                building = state.AvailableResidenceBuildings.Find(x => x.BuildingName == buildingName);
            }
            else
            {
                var affordableBuildings = state.AvailableResidenceBuildings
                                          .Where(x => x.Cost <= state.Funds)
                                          .ToArray();
                building = affordableBuildings.ElementAtOrDefault(randomizer.Random.Next(0, affordableBuildings.Length));
            }

            if (building == null)
            {
                // No valid building
                return(false);
            }

            if (building.Cost > state.Funds)
            {
                // Cannot afford it
                return(false);
            }

            gameLayer.StartBuild(position, building.BuildingName, state.GameId);
            return(true);
        }
예제 #4
0
        public static void ExecuteAction(this IGameLayer gameLayer, GameActions action, Position position, object argument = null)
        {
            var state = gameLayer.GetState();

            switch (action)
            {
            case GameActions.StartBuild:
                var buildingName = (string)argument ?? throw new ArgumentNullException(nameof(argument));
                gameLayer.StartBuild(position, buildingName, state.GameId);
                break;

            case GameActions.Build:
                gameLayer.Build(position, state.GameId);
                break;

            case GameActions.Maintenance:
                gameLayer.Maintenance(position, state.GameId);
                break;

            case GameActions.BuyUpgrade:
                var upgradeName = (string)argument ?? throw new ArgumentNullException(nameof(argument));
                gameLayer.BuyUpgrade(position, upgradeName, state.GameId);
                break;



            case GameActions.Wait:
                gameLayer.Wait(state.GameId);
                break;

            case GameActions.None:
                throw new NotSupportedException();

            default:
                throw new NotImplementedException();
            }
        }
        protected override bool TryExecuteTurn(Randomizer randomizer, IGameLayer gameLayer, GameState state)
        {
            var buildings = state.GetBuiltBuildings().OfType <BuiltResidenceBuilding>().ToArray();

            if (buildings.Length >= MaxNumberOfResidences)
            {
                // Don't build any more buildings
                return(false);
            }

            var completedResidences = state.GetCompletedBuildings().OfType <BuiltResidenceBuilding>().ToArray();

            // Only build when has nothing else to do
            var currentPop = completedResidences.Sum(x => x.CurrentPop);

            var currentPopMax = completedResidences
                                .Join(state.AvailableResidenceBuildings, ok => ok.BuildingName, ik => ik.BuildingName,
                                      (rb, bp) => new { bp, rb })
                                .Sum(x => x.bp.MaxPop);

            var pendingPopMaxIncrease = state.GetBuildingsUnderConstruction().OfType <BuiltResidenceBuilding>()
                                        .Join(state.AvailableResidenceBuildings, ok => ok.BuildingName, ik => ik.BuildingName,
                                              (rb, bp) => new { bp, rb })
                                        .Sum(x => x.bp.MaxPop);

            var currentPopPercentage = currentPopMax > 0 ? currentPop / (double)currentPopMax : 0;


            Logger.LogDebug($"Pop {currentPop}/{currentPopMax} = {currentPopPercentage:P2}		(+ {pendingPopMaxIncrease})");

            if (currentPopPercentage > PopulationPercentageThreshold ||
                buildings.Length < 1)
            {
                var position = randomizer.GetRandomBuildablePosition();
                if (position == null)
                {
                    Logger.LogWarning("No valid positions to build building");
                    return(false);
                }

                BlueprintResidenceBuilding building;
                var buildingName = BuildingName;
                if (!string.IsNullOrWhiteSpace(buildingName))
                {
                    building = state.AvailableResidenceBuildings.Find(x => x.BuildingName == buildingName);
                }
                else
                {
                    var affordableBuildings = state.AvailableResidenceBuildings
                                              .Where(x => x.Cost <= state.Funds)
                                              .ToArray();
                    building = affordableBuildings.ElementAtOrDefault(randomizer.Random.Next(0, affordableBuildings.Length));
                }

                if (building == null)
                {
                    // No valid building
                    return(false);
                }

                if (building.Cost > state.Funds)
                {
                    // Cannot afford it
                    Logger.LogWarning("Wanted to build building, but cannot afford it");
                    return(false);
                }

                gameLayer.StartBuild(position, building.BuildingName, state.GameId);
                return(true);
            }
            return(false);
        }