public void GetCommandsForUnit_AndSomeValidCellExists_PillageCommandGivenCorrectPillager()
        {
            var unitLocation = BuildCell();

            var unit = BuildUnit(unitLocation, 3f);

            var maps = new InfluenceMaps();

            var nearbyCells = new List <IHexCell>()
            {
                BuildCell(), BuildCell(), BuildCell()
            };

            MockGrid.Setup(grid => grid.GetCellsInRadius(unitLocation, 3)).Returns(nearbyCells);

            MockWeightLogic.Setup(tools => tools.GetPillageWeightFunction(unit, maps)).Returns(cell => cell.Index);

            var pillageBrain = Container.Resolve <BarbarianPillageBrain>();

            var commands = pillageBrain.GetCommandsForUnit(unit, maps);

            var pillageCommand = commands[1] as PillageUnitCommand;

            Assert.AreEqual(unit, pillageCommand.Pillager, "pillageCommand has an unexpected Pillager");
        }
        public void GetWanderCommandsForUnit_AndNoValidCandidateExists_ReturnsEmptySet()
        {
            var unitLocation = BuildCell();

            var nearbyCells = new List <IHexCell>();

            MockGrid.Setup(grid => grid.GetCellsInRadius(unitLocation, 3)).Returns(nearbyCells);

            var unit = BuildUnit(unitLocation, 3);

            var maps = new InfluenceMaps();

            Func <IHexCell, int> wanderWeightFunction = cell => 1;

            MockBrainTools.Setup(tools => tools.GetWanderWeightFunction(unit, maps)).Returns(wanderWeightFunction);

            MockCellRandomSampler.Setup(
                sampler => sampler.SampleElementsFromSet(nearbyCells, 1, wanderWeightFunction)
                ).Returns(new List <IHexCell>());

            var wanderBrain = Container.Resolve <BarbarianWanderBrain>();

            var commandList = wanderBrain.GetCommandsForUnit(unit, maps);

            Assert.AreEqual(0, commandList.Count);
        }
        public void GetWanderCommandsForUnit_AndSomeValidCandidateExists_ReturnsListWithOneMoveUnitCommand()
        {
            var unitLocation  = BuildCell();
            var bestCandidate = BuildCell();

            var nearbyCells = new List <IHexCell>();

            MockGrid.Setup(grid => grid.GetCellsInRadius(unitLocation, 3)).Returns(nearbyCells);

            var unit = BuildUnit(unitLocation, 3);

            var maps = new InfluenceMaps();

            Func <IHexCell, int> wanderWeightFunction = cell => 1;

            MockBrainTools.Setup(tools => tools.GetWanderWeightFunction(unit, maps)).Returns(wanderWeightFunction);

            MockCellRandomSampler.Setup(
                sampler => sampler.SampleElementsFromSet(nearbyCells, 1, wanderWeightFunction)
                ).Returns(new List <IHexCell>()
            {
                bestCandidate
            });

            var wanderBrain = Container.Resolve <BarbarianWanderBrain>();

            var commandList = wanderBrain.GetCommandsForUnit(unit, maps);

            Assert.AreEqual(1, commandList.Count, "Unexpected number of commands returned");
            Assert.IsTrue(commandList[0] is MoveUnitCommand, "Command not of type MoveUnitCommand");
        }
        public List <IUnitCommand> GetCommandsForUnit(IUnit unit, InfluenceMaps maps)
        {
            var retval = new List <IUnitCommand>();

            var unitPosition = UnitPositionCanon.GetOwnerOfPossession(unit);

            if (EncampmentLocationCanon.GetPossessionsOfOwner(unitPosition).Any())
            {
                return(retval);
            }

            IHexCell bestCandidate = Grid.GetCellsInRadius(unitPosition, BarbarianConfig.DefendEncampmentRadius)
                                     .FirstOrDefault(HasUndefendedEncampment);

            if (bestCandidate != null)
            {
                var moveCommand = Container.Instantiate <MoveUnitCommand>();

                moveCommand.DesiredLocation = bestCandidate;
                moveCommand.UnitToMove      = unit;

                retval.Add(moveCommand);
            }

            return(retval);
        }
        public void GetWanderCommandsForUnit_AndSomeValidCandidateExists_ReturnedCommandHasCorrectDesiredLocation()
        {
            var unitLocation  = BuildCell();
            var bestCandidate = BuildCell();

            var nearbyCells = new List <IHexCell>();

            MockGrid.Setup(grid => grid.GetCellsInRadius(unitLocation, 3)).Returns(nearbyCells);

            var unit = BuildUnit(unitLocation, 3);

            var maps = new InfluenceMaps();

            Func <IHexCell, int> wanderWeightFunction = cell => 1;

            MockBrainTools.Setup(tools => tools.GetWanderWeightFunction(unit, maps)).Returns(wanderWeightFunction);

            MockCellRandomSampler.Setup(
                sampler => sampler.SampleElementsFromSet(nearbyCells, 1, wanderWeightFunction)
                ).Returns(new List <IHexCell>()
            {
                bestCandidate
            });

            var wanderBrain = Container.Resolve <BarbarianWanderBrain>();

            var commandList = wanderBrain.GetCommandsForUnit(unit, maps);

            var moveCommand = commandList[0] as MoveUnitCommand;

            Assert.AreEqual(bestCandidate, moveCommand.DesiredLocation);
        }
        public void GenerateMaps_AllNullMapsConstructedProperly()
        {
            var maps = new InfluenceMaps()
            {
                AllyPresence   = null,
                EnemyPresence  = new float[3],
                PillagingValue = null
            };

            BuildCell();
            BuildCell();

            var mapGenerator = Container.Resolve <InfluenceMapLogic>();

            mapGenerator.AssignMaps(maps, BuildCiv());

            CollectionAssert.AreEquivalent(
                new float[] { 0f, 0f }, maps.AllyPresence, "AllyPresence not constructed as expected"
                );

            CollectionAssert.AreEquivalent(
                new float[] { 0f, 0f, 0f }, maps.EnemyPresence, "EnemyPresence unexpectedly modified"
                );

            CollectionAssert.AreEquivalent(
                new float[] { 0f, 0f }, maps.PillagingValue, "ImprovementValue not constructed as expected"
                );
        }
        public void GenerateMaps_AllMapsInitializedToZero()
        {
            var maps = new InfluenceMaps()
            {
                AllyPresence   = new float[] { 1, 1 },
                EnemyPresence  = new float[] { 2, 2, 2 },
                PillagingValue = new float[] { 3, 3, 3, 3 },
            };

            BuildCell();
            BuildCell();

            var mapGenerator = Container.Resolve <InfluenceMapLogic>();

            mapGenerator.AssignMaps(maps, BuildCiv());

            CollectionAssert.AreEquivalent(
                new float[] { 0f, 0f }, maps.AllyPresence, "AllyPresence not initialized as expected"
                );

            CollectionAssert.AreEquivalent(
                new float[] { 0f, 0f, 2f }, maps.EnemyPresence, "EnemyPresence not initialized as expected"
                );

            CollectionAssert.AreEquivalent(
                new float[] { 0f, 0f, 3f, 3f }, maps.PillagingValue, "ImprovementValue not initialized as expected"
                );
        }
        public void TrySpawnEncampment_TriesToSpawnAUnitOnNewlyBuiltEncampments()
        {
            var cells = new List <IHexCell>()
            {
                BuildCell(), BuildCell(), BuildCell()
            };

            var maps = new InfluenceMaps();

            Func <IHexCell, int> weightFunction = cell => 1;

            MockSpawningTools.Setup(tools => tools.EncampmentValidityFilter).Returns(cell => true);
            MockSpawningTools.Setup(tools => tools.BuildEncampmentWeightFunction(maps)).Returns(weightFunction);

            MockCellSampler.Setup(
                sampler => sampler.SampleElementsFromSet(It.IsAny <IEnumerable <IHexCell> >(), 1, weightFunction)
                ).Returns(new List <IHexCell>()
            {
                cells[0]
            });

            var newEncampment = BuildEncampment();

            MockEncampmentFactory.Setup(factory => factory.CreateEncampment(cells[0])).Returns(newEncampment);

            var encampmentSpawner = Container.Resolve <BarbarianEncampmentSpawner>();

            encampmentSpawner.TrySpawnEncampment(maps);

            MockUnitSpawner.Verify(spawner => spawner.TrySpawnUnit(newEncampment));
        }
        public void GetCommandsForUnit_AndSomeValidCellExists_ReturnsAMoveAndAPillageCommand()
        {
            var unitLocation = BuildCell();

            var unit = BuildUnit(unitLocation, 3f);

            var maps = new InfluenceMaps();

            var nearbyCells = new List <IHexCell>()
            {
                BuildCell(), BuildCell(), BuildCell()
            };

            MockGrid.Setup(grid => grid.GetCellsInRadius(unitLocation, 3)).Returns(nearbyCells);

            MockWeightLogic.Setup(tools => tools.GetPillageWeightFunction(unit, maps)).Returns(cell => cell.Index);

            var pillageBrain = Container.Resolve <BarbarianPillageBrain>();

            var commands = pillageBrain.GetCommandsForUnit(unit, maps);

            Assert.AreEqual(2, commands.Count, "Unexpected number of commands");

            Assert.IsTrue(commands[0] is MoveUnitCommand, "commands[0] is not of type MoveUnitCommand");
            Assert.IsTrue(commands[1] is PillageUnitCommand, "commands[1] is not of type PillageUnitCommand");
        }
        public void GetCommandsForUnit_ExcludesLocationsWhereUnitCannotMove()
        {
            var unitLocation = BuildCell();

            var unit = BuildUnit(20.2f, 3.5f, unitLocation);

            var maps = new InfluenceMaps();

            IHexCell cellOne = BuildCell(), cellTwo = BuildCell(), cellThree = BuildCell();

            var reachableCellsDict = new Dictionary <IHexCell, float>()
            {
                { cellOne, 1f }, { cellTwo, 4f }, { cellThree, 3f },
            };

            Func <IHexCell, IHexCell, float> pathfindingCostFunction = (current, next) => 0;

            MockUnitPositionCanon.Setup(canon => canon.GetPathfindingCostFunction(unit, false)).Returns(pathfindingCostFunction);

            MockHexPathfinder.Setup(
                pathfinder => pathfinder.GetAllCellsReachableIn(unitLocation, 3.5f, pathfindingCostFunction, AllCells)
                ).Returns(reachableCellsDict);

            MockUnitPositionCanon.Setup(canon => canon.CanPlaceUnitAtLocation(unit, cellOne, false)).Returns(true);
            MockUnitPositionCanon.Setup(canon => canon.CanPlaceUnitAtLocation(unit, cellTwo, false)).Returns(false);
            MockUnitPositionCanon.Setup(canon => canon.CanPlaceUnitAtLocation(unit, cellThree, false)).Returns(true);

            MockBrainTools.Setup(tools => tools.GetFleeWeightFunction(unit, maps)).Returns(cell => reachableCellsDict[cell]);

            var fleeBrain = Container.Resolve <BarbarianFleeBrain>();

            var moveCommand = fleeBrain.GetCommandsForUnit(unit, maps)[0] as MoveUnitCommand;

            Assert.AreEqual(cellThree, moveCommand.DesiredLocation);
        }
        public void TrySpawnEncampment_FiltersCellsThroughCellValidityFilterOfSpawningTools()
        {
            var cells = new List <IHexCell>()
            {
                BuildCell(), BuildCell(), BuildCell()
            };

            var maps = new InfluenceMaps();

            Func <IHexCell, bool> validityFilter = cell => cell == cells[2];
            Func <IHexCell, int>  weightFunction = cell => 1;

            MockSpawningTools.Setup(tools => tools.EncampmentValidityFilter).Returns(validityFilter);
            MockSpawningTools.Setup(tools => tools.BuildEncampmentWeightFunction(maps)).Returns(weightFunction);

            MockCellSampler.Setup(
                sampler => sampler.SampleElementsFromSet(It.IsAny <IEnumerable <IHexCell> >(), 1, weightFunction)
                ).Returns(new List <IHexCell>());

            var encampmentSpawner = Container.Resolve <BarbarianEncampmentSpawner>();

            encampmentSpawner.TrySpawnEncampment(maps);

            MockCellSampler.Verify(
                sampler => sampler.SampleElementsFromSet(
                    It.Is <IEnumerable <IHexCell> >(set => new HashSet <IHexCell>(set).SetEquals(new List <IHexCell>()
            {
                cells[2]
            })),
                    1, weightFunction
                    ), Times.Once
                );
        }
        public void GetPillageUtilityFunction_ReturnedDelegate_UtilityDividedByDistancePlusOne()
        {
            var unitLocation = BuildCell(-1);
            var unit         = BuildUnit(unitLocation);

            var cellOne   = BuildCell(0);
            var cellTwo   = BuildCell(1);
            var cellThree = BuildCell(2);

            MockGrid.Setup(grid => grid.GetDistance(unitLocation, cellOne)).Returns(2);
            MockGrid.Setup(grid => grid.GetDistance(unitLocation, cellTwo)).Returns(3);
            MockGrid.Setup(grid => grid.GetDistance(unitLocation, cellThree)).Returns(4);

            MockBarbarianConfig.Setup(config => config.PillageUtilityCoefficient).Returns(0.01f);

            var maps = new InfluenceMaps()
            {
                PillagingValue = new float[] { 5f, 7.2f, 3.4f }
            };

            var utilityLogic = Container.Resolve <BarbarianUtilityLogic>();

            var returnedDelegate = utilityLogic.GetPillageUtilityFunction(unit, maps);

            Assert.That(Mathf.Approximately(5f * 0.01f / 3, returnedDelegate(cellOne)), "Delegate returned an unexpected value for CellOne");
            Assert.That(Mathf.Approximately(7.2f * 0.01f / 4, returnedDelegate(cellTwo)), "Delegate returned an unexpected value for CellTwo");
            Assert.That(Mathf.Approximately(3.4f * 0.01f / 5, returnedDelegate(cellThree)), "Delegate returned an unexpected value for CellThree");
        }
        public void GetCommandsForUnit_AndUnitHasNoRangedAttackStrength_ReturnsEmptyListIfNoCandidatePassesFilter()
        {
            var unitLocation = BuildCell();

            var unit = BuildUnit(location: unitLocation, canAttack: true);

            var maps = new InfluenceMaps();

            IHexCell cellTwo = BuildCell();

            var cellsByUtility = new Dictionary <IHexCell, float>()
            {
                { BuildCell(), 1f }, { cellTwo, 3f }, { BuildCell(), 2f },
            };

            MockUnitVisibilityLogic.Setup(logic => logic.GetCellsVisibleToUnit(unit)).Returns(cellsByUtility.Keys);

            MockFilterLogic.Setup(logic => logic.GetMeleeAttackFilter(unit)).Returns(cell => false);
            MockUtilityLogic.Setup(logic => logic.GetAttackUtilityFunction(unit, maps)).Returns(cell => cellsByUtility[cell]);

            var attackBrain = Container.Resolve <BarbarianAttackBrain>();

            var commands = attackBrain.GetCommandsForUnit(unit, maps);

            Assert.AreEqual(0, commands.Count);
        }
        public float GetUtilityForUnit(IUnit unit, InfluenceMaps maps)
        {
            if (!unit.CanAttack)
            {
                return(0f);
            }

            IEnumerable <IHexCell> attackCandidates = null;

            if (unit.RangedAttackStrength > 0)
            {
                var unitLocation = UnitPositionCanon.GetOwnerOfPossession(unit);

                attackCandidates = Grid.GetCellsInRadius(unitLocation, unit.AttackRange)
                                   .Where(FilterLogic.GetRangedAttackFilter(unit));
            }
            else
            {
                attackCandidates = UnitVisibilityLogic.GetCellsVisibleToUnit(unit)
                                   .Where(FilterLogic.GetMeleeAttackFilter(unit));
            }

            if (attackCandidates.Any())
            {
                return(attackCandidates.Max(UtilityLogic.GetAttackUtilityFunction(unit, maps)));
            }
            else
            {
                return(0f);
            }
        }
        public void GetCommandsForUnit_AndNoValidLocations_ReturnsEmptyList()
        {
            var unitLocation = BuildCell();

            var unit = BuildUnit(20.2f, 3.5f, unitLocation);

            var maps = new InfluenceMaps();

            var reachableCellsDict = new Dictionary <IHexCell, float>();

            Func <IHexCell, IHexCell, float> pathfindingCostFunction = (current, next) => 0;

            MockUnitPositionCanon.Setup(canon => canon.GetPathfindingCostFunction(unit, false)).Returns(pathfindingCostFunction);

            MockHexPathfinder.Setup(
                pathfinder => pathfinder.GetAllCellsReachableIn(unitLocation, 3.5f, pathfindingCostFunction, AllCells)
                ).Returns(reachableCellsDict);

            MockUnitPositionCanon.Setup(canon => canon.CanPlaceUnitAtLocation(unit, It.IsAny <IHexCell>(), false)).Returns(true);

            MockBrainTools.Setup(tools => tools.GetFleeWeightFunction(unit, maps)).Returns(cell => reachableCellsDict[cell]);

            var fleeBrain = Container.Resolve <BarbarianFleeBrain>();

            CollectionAssert.IsEmpty(fleeBrain.GetCommandsForUnit(unit, maps));
        }
        public void GetCommandsForUnit_AndUnitHasNoRangedAttackStrength_ReturnsASingleAttackCommand_ConfiguredCorrectly()
        {
            var unitLocation = BuildCell();

            var unit = BuildUnit(location: unitLocation, canAttack: true);

            var maps = new InfluenceMaps();

            IHexCell cellTwo = BuildCell();

            var cellsByUtility = new Dictionary <IHexCell, float>()
            {
                { BuildCell(), 1f }, { cellTwo, 3f }, { BuildCell(), 2f },
            };

            MockUnitVisibilityLogic.Setup(logic => logic.GetCellsVisibleToUnit(unit)).Returns(cellsByUtility.Keys);

            MockFilterLogic.Setup(logic => logic.GetMeleeAttackFilter(unit)).Returns(cell => true);
            MockUtilityLogic.Setup(logic => logic.GetAttackUtilityFunction(unit, maps)).Returns(cell => cellsByUtility[cell]);

            var attackBrain = Container.Resolve <BarbarianAttackBrain>();

            var commands = attackBrain.GetCommandsForUnit(unit, maps);

            Assert.AreEqual(1, commands.Count, "Commands has an unexpected number of elements");

            var attackCommand = commands[0] as AttackUnitCommand;

            Assert.IsNotNull(attackCommand, "Command is not of type AttackUnitCommand");

            Assert.AreEqual(unit, attackCommand.Attacker, "Command has an unexpected Attacker value");
            Assert.AreEqual(cellTwo, attackCommand.LocationToAttack, "Command has an unexpected LocationToAttack value");
            Assert.AreEqual(CombatType.Melee, attackCommand.CombatType, "Command has an unexpected CombatType value");
        }
        public void TrySpawnEncampment_BuildsEncampmentFromRandomlySampledCandidate()
        {
            var cells = new List <IHexCell>()
            {
                BuildCell(), BuildCell(), BuildCell()
            };

            var maps = new InfluenceMaps();

            Func <IHexCell, int> weightFunction = cell => 1;

            MockSpawningTools.Setup(tools => tools.EncampmentValidityFilter).Returns(cell => true);
            MockSpawningTools.Setup(tools => tools.BuildEncampmentWeightFunction(maps)).Returns(weightFunction);

            MockCellSampler.Setup(
                sampler => sampler.SampleElementsFromSet(It.IsAny <IEnumerable <IHexCell> >(), 1, weightFunction)
                ).Returns(new List <IHexCell>()
            {
                cells[0]
            });

            var encampmentSpawner = Container.Resolve <BarbarianEncampmentSpawner>();

            encampmentSpawner.TrySpawnEncampment(maps);

            MockEncampmentFactory.Verify(factory => factory.CreateEncampment(cells[0]), Times.Once);
        }
        public void GetCommandsForUnit_AndValidLocationsExist_MoveCommandGivenCorrectUnitToMove()
        {
            var unitLocation = BuildCell();

            var unit = BuildUnit(20.2f, 3.5f, unitLocation);

            var maps = new InfluenceMaps();

            var reachableCellsDict = new Dictionary <IHexCell, float>()
            {
                { BuildCell(), 1f }, { BuildCell(), 2f }, { BuildCell(), 3f },
            };

            Func <IHexCell, IHexCell, float> pathfindingCostFunction = (current, next) => 0;

            MockUnitPositionCanon.Setup(canon => canon.GetPathfindingCostFunction(unit, false)).Returns(pathfindingCostFunction);

            MockHexPathfinder.Setup(
                pathfinder => pathfinder.GetAllCellsReachableIn(unitLocation, 3.5f, pathfindingCostFunction, AllCells)
                ).Returns(reachableCellsDict);

            MockUnitPositionCanon.Setup(canon => canon.CanPlaceUnitAtLocation(unit, It.IsAny <IHexCell>(), false)).Returns(true);

            MockBrainTools.Setup(tools => tools.GetFleeWeightFunction(unit, maps)).Returns(cell => reachableCellsDict[cell]);

            var fleeBrain = Container.Resolve <BarbarianFleeBrain>();

            var moveCommand = fleeBrain.GetCommandsForUnit(unit, maps)[0] as MoveUnitCommand;

            Assert.AreEqual(unit, moveCommand.UnitToMove);
        }
        public void GetCommandsForUnit_AndValidLocationsExist_ReturnsASingleMoveUnitCommand()
        {
            var unitLocation = BuildCell();

            var unit = BuildUnit(20.2f, 3.5f, unitLocation);

            var maps = new InfluenceMaps();

            var reachableCellsDict = new Dictionary <IHexCell, float>()
            {
                { BuildCell(), 1f }, { BuildCell(), 2f }, { BuildCell(), 3f },
            };

            Func <IHexCell, IHexCell, float> pathfindingCostFunction = (current, next) => 0;

            MockUnitPositionCanon.Setup(canon => canon.GetPathfindingCostFunction(unit, false)).Returns(pathfindingCostFunction);

            MockHexPathfinder.Setup(
                pathfinder => pathfinder.GetAllCellsReachableIn(unitLocation, 3.5f, pathfindingCostFunction, AllCells)
                ).Returns(reachableCellsDict);

            MockUnitPositionCanon.Setup(canon => canon.CanPlaceUnitAtLocation(unit, It.IsAny <IHexCell>(), false)).Returns(true);

            MockBrainTools.Setup(tools => tools.GetFleeWeightFunction(unit, maps)).Returns(cell => reachableCellsDict[cell]);

            var fleeBrain = Container.Resolve <BarbarianFleeBrain>();

            var commands = fleeBrain.GetCommandsForUnit(unit, maps);

            Assert.AreEqual(1, commands.Count, "Unexpected number of commands");

            Assert.IsTrue(commands[0] is MoveUnitCommand, "Command is not a MoveUnitCommand as expected");
        }
Example #20
0
        public void ApplyToMaps_IgnoresPillagedImprovements()
        {
            var unpillagedImprovement = BuildImprovement(BuildImprovementTemplate(), false);
            var pillagedImprovement   = BuildImprovement(BuildImprovementTemplate(), true);

            BuildCell(false, unpillagedImprovement, null);
            BuildCell(false, pillagedImprovement, null);
            BuildCell(true, pillagedImprovement, null);

            var targetCiv = BuildCiv(new List <IHexCell>());

            MockAIConfig.Setup(config => config.RoadPillageValue).Returns(10f);
            MockAIConfig.Setup(config => config.NormalImprovementPillageValue).Returns(20f);

            var maps = new InfluenceMaps()
            {
                PillagingValue = new float[3]
            };

            var influenceSource = Container.Resolve <ImprovementInfluenceSource>();

            influenceSource.ApplyToMaps(maps, targetCiv);

            CollectionAssert.AreEqual(
                new float[] { 20f, 0f, 10f }, maps.PillagingValue
                );
        }
        public List <IUnitCommand> GetCommandsForUnit(IUnit unit, InfluenceMaps maps)
        {
            var unitLocation = UnitPositionCanon.GetOwnerOfPossession(unit);

            var reachableCellsByDistance = HexPathfinder.GetAllCellsReachableIn(
                unitLocation, unit.CurrentMovement, UnitPositionCanon.GetPathfindingCostFunction(unit, false),
                Grid.Cells
                );

            var validCandidates = reachableCellsByDistance.Keys.Where(cell => UnitPositionCanon.CanPlaceUnitAtLocation(unit, cell, false));

            IHexCell bestCandidate = validCandidates.MaxElement(BrainTools.GetFleeWeightFunction(unit, maps));

            var retval = new List <IUnitCommand>();

            if (bestCandidate != null)
            {
                var moveCommand = Container.Instantiate <MoveUnitCommand>();

                moveCommand.UnitToMove      = unit;
                moveCommand.DesiredLocation = bestCandidate;

                retval.Add(moveCommand);
            }

            return(retval);
        }
        public List <IUnitCommand> GetCommandsForUnit(IUnit unit, InfluenceMaps maps)
        {
            var unitPosition = UnitPositionCanon.GetOwnerOfPossession(unit);

            var reachableCells = HexPathfinder.GetAllCellsReachableIn(
                unitPosition, unit.CurrentMovement,
                (current, next) => UnitPositionCanon.GetTraversalCostForUnit(unit, current, next, true),
                Grid.Cells
                );

            var cellToAttack = reachableCells.Keys.FirstOrDefault(FilterLogic.GetCaptureCivilianFilter(unit));

            var retval = new List <IUnitCommand>();

            if (cellToAttack != null)
            {
                var attackCommand = Container.Instantiate <AttackUnitCommand>();

                attackCommand.Attacker         = unit;
                attackCommand.LocationToAttack = cellToAttack;
                attackCommand.CombatType       = CombatType.Melee;

                retval.Add(attackCommand);
            }

            return(retval);
        }
Example #23
0
        public void ApplyToMaps_IgnoresCellsClaimedByTargetCiv()
        {
            var cellsClaimedByTargetCiv = new List <IHexCell>()
            {
                BuildCell(true, null, null),
                BuildCell(true, null, null)
            };

            var cellsClaimedByForeignCiv = new List <IHexCell>()
            {
                BuildCell(true, null, null)
            };

            BuildCell(true, null, null);

            var targetCiv = BuildCiv(cellsClaimedByTargetCiv);

            BuildCiv(cellsClaimedByForeignCiv);

            MockAIConfig.Setup(config => config.RoadPillageValue).Returns(10f);

            var maps = new InfluenceMaps()
            {
                PillagingValue = new float[4]
            };

            var influenceSource = Container.Resolve <ImprovementInfluenceSource>();

            influenceSource.ApplyToMaps(maps, targetCiv);

            CollectionAssert.AreEqual(
                new float[] { 0f, 0f, 10f, 10f }, maps.PillagingValue
                );
        }
Example #24
0
        public void GetFleeWeightFunction_ReturnedDelegate_IncreasedByDistanceFromUnitLocationToCell()
        {
            var unitLocation = BuildCell(-1);

            var unit = BuildUnit(unitLocation);

            var cell = BuildCell(1);

            MockGrid.Setup(grid => grid.GetDistance(unitLocation, cell)).Returns(5);

            MockUnitStrengthEstimator.Setup(estimator => estimator.EstimateUnitDefensiveStrength(unit, cell))
            .Returns(11f);

            var maps = new InfluenceMaps()
            {
                AllyPresence  = new float[] { 0f, 0f, 0f },
                EnemyPresence = new float[] { 0f, 0f, 0f }
            };

            var brainTools = Container.Resolve <BarbarianBrainWeightLogic>();

            var returnedDelegate = brainTools.GetFleeWeightFunction(unit, maps);

            Assert.AreEqual(16f, returnedDelegate(cell));
        }
Example #25
0
        public void GetWanderWeightFunction_ReturnedDelegate_DoesNotGoBelowZero()
        {
            var cellToTest   = BuildCell(0);
            var unitLocation = BuildCell(1);

            var unit = BuildUnit(unitLocation);

            var maps = new InfluenceMaps()
            {
                AllyPresence = new float[1] {
                    4.5f
                },
                EnemyPresence = new float[1]
            };

            MockUnitPositionCanon.Setup(canon => canon.CanPlaceUnitAtLocation(unit, cellToTest, false)).Returns(true);

            MockGrid.Setup(grid => grid.GetDistance(cellToTest, unitLocation)).Returns(0);

            MockBarbarianConfig.Setup(config => config.WanderSelectionWeight_Distance).Returns(0f);
            MockBarbarianConfig.Setup(config => config.WanderSelectionWeight_Allies).Returns(5f);

            var brainTools = Container.Resolve <BarbarianBrainWeightLogic>();

            var returnedDelegate = brainTools.GetWanderWeightFunction(unit, maps);

            Assert.AreEqual(0f, returnedDelegate(cellToTest));
        }
Example #26
0
        public void GetWanderWeightFunction_ReturnedDelegate_ValueDecreasedFromEnemyPresenceOfCell()
        {
            var cellToTest   = BuildCell(0);
            var unitLocation = BuildCell(1);

            var unit = BuildUnit(unitLocation);

            var maps = new InfluenceMaps()
            {
                AllyPresence = new float[1] {
                    4.5f
                },
                EnemyPresence = new float[1] {
                    1.3f
                }
            };

            MockUnitPositionCanon.Setup(canon => canon.CanPlaceUnitAtLocation(unit, cellToTest, false)).Returns(true);

            MockGrid.Setup(grid => grid.GetDistance(cellToTest, unitLocation)).Returns(5);

            MockBarbarianConfig.Setup(config => config.WanderSelectionWeight_Distance).Returns(55f);
            MockBarbarianConfig.Setup(config => config.WanderSelectionWeight_Allies).Returns(5f);
            MockBarbarianConfig.Setup(config => config.WanderSelectionWeight_Enemies).Returns(4f);

            var brainTools = Container.Resolve <BarbarianBrainWeightLogic>();

            var returnedDelegate = brainTools.GetWanderWeightFunction(unit, maps);

            Assert.AreEqual(Mathf.RoundToInt(5 * 55f - 4.5f * 5f - 1.3f * 4f), returnedDelegate(cellToTest));
        }
Example #27
0
        public void ApplyToMaps_AppliesNormalImprovementPillageValue_ToCellsWithNonExtractingImprovements()
        {
            var extractingTemplate    = BuildImprovementTemplate();
            var nonExtractingTemplate = BuildImprovementTemplate();

            var resourceOne = BuildResource(extractingTemplate);
            var resourceTwo = BuildResource(null);

            BuildCell(false, BuildImprovement(nonExtractingTemplate), null);
            BuildCell(false, BuildImprovement(nonExtractingTemplate), BuildNode(resourceOne));
            BuildCell(false, BuildImprovement(extractingTemplate), null);
            BuildCell(false, BuildImprovement(extractingTemplate), BuildNode(resourceOne));
            BuildCell(false, BuildImprovement(extractingTemplate), BuildNode(resourceTwo));

            MockAIConfig.Setup(config => config.NormalImprovementPillageValue).Returns(10f);

            var maps = new InfluenceMaps()
            {
                PillagingValue = new float[5]
            };

            var targetCiv = BuildCiv(new List <IHexCell>());

            var influenceSource = Container.Resolve <ImprovementInfluenceSource>();

            influenceSource.ApplyToMaps(maps, targetCiv);

            CollectionAssert.AreEqual(
                new float[] { 10f, 10f, 10f, 0f, 10f }, maps.PillagingValue
                );
        }
Example #28
0
        public void ApplyToMaps_AndTargetCivNull_ThrowsArgumentNullException()
        {
            var maps = new InfluenceMaps();

            var influenceSource = Container.Resolve <ImprovementInfluenceSource>();

            Assert.Throws <ArgumentNullException>(() => influenceSource.ApplyToMaps(maps, null));
        }
Example #29
0
        public float GetUtilityForUnit(IUnit unit, InfluenceMaps maps)
        {
            var unitLocation = UnitPositionCanon.GetOwnerOfPossession(unit);

            return(Grid.GetCellsInRadius(unitLocation, Mathf.RoundToInt(unit.MaxMovement)).Max(
                       UtilityLogic.GetPillageUtilityFunction(unit, maps)
                       ));
        }
        public Func <IHexCell, int> GetPillageWeightFunction(IUnit unit, InfluenceMaps maps)
        {
            var unitLocation = UnitPositionCanon.GetOwnerOfPossession(unit);

            return(delegate(IHexCell cell) {
                return Mathf.RoundToInt(maps.PillagingValue[cell.Index] / (1 + Grid.GetDistance(unitLocation, cell)));
            });
        }