示例#1
0
        public bool ManualShot(Point target, IBattlefield battlefield)
        {
            if (!battlefield.IsPointInField(target))
            {
                throw new IndexOutOfRangeException($"Point {target.X}, {target.Y} is not in field");
            }

            var cell = battlefield[target];

            if (cell.State.HasFlag(CellState.WasFired))
            {
                throw new ArgumentException($"Can't shoot fired cell: {target.X}, {target.Y}");
            }
            cell.State = battlefield[target].State | CellState.WasFired;

            if (!cell.State.HasFlag(CellState.Ship))
            {
                return(false);
            }
            var ship = battlefield.Ships.Find(s => s.Cells.Contains(cell));

            ship.State = ship.Cells.All(c =>
                                        c.State.HasFlag(CellState.WasFired))
                ? ShipState.Destroyed
                : ShipState.Damaged;
            if (ship.State == ShipState.Damaged)
            {
                AutoShotMethod = new RandomNearShot(this);
            }
            return(true);
        }
示例#2
0
        public override bool Shoot(IBattlefield battlefield)
        {
            var targetShip = battlefield.Ships.FirstOrDefault(s =>
                                                              s.Cells.Count(c => c.State.HasFlag(CellState.WasFired)) >= 2 && s.State == ShipState.Damaged);
            var target = GetRandomCell(targetShip, battlefield);

            target.State = target.State | CellState.WasFired;
            if (!target.State.HasFlag(CellState.Ship))
            {
                return(false);
            }

            if (!targetShip.Cells.All(c => c.State.HasFlag(CellState.WasFired)))
            {
                return(true);
            }

            targetShip.State = ShipState.Destroyed;
            if (battlefield.Ships.Exists(s => s.State == ShipState.Damaged))
            {
                ChangeShotMethod(new RandomNearShot(Player));
            }

            ChangeShotMethod(new RandomShot(Player));
            return(true);
        }
示例#3
0
 public override void SetUp()
 {
     base.SetUp();
     Battlefield = Get <IBattlefieldFactory>().Create();
     Bot1        = Get <IBotWorkshop>().Create(TestBotAI.Idle);
     Bot2        = Get <IBotWorkshop>().Create(TestBotAI.Idle);
 }
 public void SetFighterPositions(Dictionary <Guid, IFighterStats> fighters, IBattlefield battlefield)
 {
     foreach (var fighter in fighters.Values)
     {
         SetFighterPosition(fighter, battlefield);
     }
 }
示例#5
0
 public ShipSpawner(IBattlefield battlefield, Random random)
 {
     this.battlefield             = battlefield;
     this.random                  = random;
     fieldsOccupiedBySpawnedShips = new List <int[]>();
     startingPosition             = new int[2];
 }
示例#6
0
        public override bool Shoot(IBattlefield battlefield)
        {
            var target = battlefield.GetRandomCell();

            while (!IsCellSuitable(target))
            {
                target = battlefield.GetRandomCell();
            }

            target.State = target.State | CellState.WasFired; //adds WasFired flag to cell state

            if (!target.State.HasFlag(CellState.Ship))
            {
                return(false);
            }

            var ship = battlefield.Ships.Find(s => s.Cells.Contains(target));

            // ReSharper disable once PossibleNullReferenceException
            if (ship.Cells.Count == 1)
            {
                ship.State = ShipState.Destroyed;
                return(true);
            }

            ship.State = ShipState.Damaged;
            ChangeShotMethod(new RandomNearShot(Player));
            return(true);
        }
示例#7
0
        public IReadOnlyCollection <IBattleBot> Create(IBattlefield battlefield)
        {
            try
            {
                var result = new Collection <IBattleBot>();
                var files  = AssemblyFiles();
                foreach (var file in files)
                {
                    var assembly  = Assembly.Load(File.ReadAllBytes(file));
                    var botAIType = FindBotAIType(assembly);

                    if (botAIType != null)
                    {
                        var botAI = Activator.CreateInstance(botAIType) as IBotAI;
                        var bot   = BotWorkshop.Create(botAI);
                        result.Add(bot);
                    }
                }

                return(result);
            }
            catch (Exception e)
            {
                Output.Error(e.Message);
                return(new List <IBattleBot>());
            }
        }
示例#8
0
        private void Initialize(IBattlefield battlefield)
        {
            var fields = new Field[BattlefieldSize, BattlefieldSize];

            InitializeFields(fields);
            battlefield.AcquireFields(fields);
        }
        public static Engine GetEngine(
            IEnumerable <IFighterStats> fighters,
            IBattlefield battlefield,
            IBounds bounds,
            ICollection <IEngineFeature> features,
            IMoveOrder moveOrder,
            IFighterPositionGenerator positionGenerator,
            IWinCondition winCondition,
            IStaleCondition staleCondition,
            int actionsPerRound)
        {
            return(new Engine(
                       cfg =>
            {
                cfg.ActionsPerRound = actionsPerRound;
                cfg.Battlefield = battlefield;
                cfg.Bounds = bounds;
                cfg.Features = features;
                cfg.MoveOrder = moveOrder;
                cfg.PositionGenerator = positionGenerator;
                cfg.WinCondition = winCondition;
                cfg.StaleCondition = staleCondition;

                return cfg;
            }, fighters));
        }
示例#10
0
        public override bool Shoot(IBattlefield battlefield)
        {
            var targetShip = battlefield.Ships.FirstOrDefault(s => s.State is ShipState.Damaged);
            //ReSharper disable once PossibleNullReferenceException
            var damagedCell = targetShip.Cells.FirstOrDefault(c => c.State.HasFlag(CellState.WasFired));

            var target = GetRandomCell(damagedCell, battlefield);

            target.State = target.State | CellState.WasFired;

            if (!target.State.HasFlag(CellState.Ship))
            {
                return(false);
            }

            if (targetShip.Cells.All(c => c.State.HasFlag(CellState.WasFired)))
            {
                targetShip.State = ShipState.Destroyed;
                if (battlefield.Ships.Exists(s => s.State == ShipState.Damaged))
                {
                    return(true);
                }
                ChangeShotMethod(new RandomShot(Player));
                return(true);
            }

            ChangeShotMethod(new SmartShot(Player));
            return(true);
        }
示例#11
0
        public override ITurnAction Update(IBot ownBot, IBattlefield battlefield)
        {
            var enemies = battlefield.Bots.Except(new[] { ownBot });

            if (ownBot.AvailableWeapons.Count == 1)
            {
                var closestWeapon = battlefield.Weapons.OrderBy(ownBot.DistanceTo).First();
                return(ownBot.DistanceTo(closestWeapon) < ownBot.Radius
                    ? TurnAction.PickUpWeapon()
                    : TurnAction.MoveTowards(closestWeapon));
            }

            if (enemies.Any())
            {
                var closestEnemy = enemies.OrderBy(ownBot.DistanceTo).First();
                if (ownBot.EquippedWeapon.Ammunition.Remaining > 0)
                {
                    return(ownBot.DistanceTo(closestEnemy) < ownBot.EquippedWeapon.MaxRange / 2
                        ? TurnAction.ShootAt(closestEnemy)
                        : TurnAction.MoveTowards(closestEnemy));
                }

                var closestWeapon = battlefield.Weapons.OrderBy(ownBot.DistanceTo).First();
                return(ownBot.DistanceTo(closestWeapon) < ownBot.Radius
                    ? TurnAction.PickUpWeapon()
                    : TurnAction.MoveTowards(closestWeapon));
            }
            return(TurnAction.Idle);
        }
示例#12
0
        private Cell GetRandomCell(Cell damaged, IBattlefield battlefield)
        {
            var cells = battlefield.GetNeighbours(damaged).Where(c => !c.State.HasFlag(CellState.WasFired) &&
                                                                 (c.Coordinates.X.Equals(damaged.Coordinates.X) ||
                                                                  c.Coordinates.Y.Equals(damaged.Coordinates.Y))).ToList();

            return(RandomHelper.GetHelper().GetRandomCell(cells));
        }
示例#13
0
        private static ITurnAction GetResource(IBot ownBot, IBattlefield battlefield)
        {
            var closestResource = battlefield.Resources.OrderBy(ownBot.DistanceTo).First();

            return(ownBot.DistanceTo(closestResource) < ownBot.Radius
                ? TurnAction.PickUpResource()
                : TurnAction.MoveTowards(closestResource));
        }
示例#14
0
        public void Place(IBattlefield battlefield, ShipPlacement placement)
        {
            var shipPositions = _battleshipPlacement.GetShipPositions(placement);

            foreach (var shipPosition in shipPositions)
            {
                battlefield.MarkCell(shipPosition);
            }
        }
示例#15
0
 public BattlefieldBotViewModel(IBattleBot battleBot, int battlefieldWidth, int battlefieldHeight, IBattlefield battlefield)
 {
     myBattlefieldWidth  = battlefieldWidth;
     myBattlefieldHeight = battlefieldHeight;
     myBattlefield       = battlefield;
     Width  = 40;
     Height = 50;
     UpdateFrom(battleBot);
 }
 public void SetFighterPosition(IFighterStats fighter, IBattlefield battlefield)
 {
     fighter.Set(new Vector3()
     {
         X = random.Next((int)battlefield.CurrentBounds.Low.X, (int)battlefield.CurrentBounds.High.X),
         Y = random.Next((int)battlefield.CurrentBounds.Low.Y, (int)battlefield.CurrentBounds.High.Y),
         Z = random.Next((int)battlefield.CurrentBounds.Low.Z, (int)battlefield.CurrentBounds.High.Z),
     });
 }
示例#17
0
        public void DrawField(IBattlefield battlefield)
        {
            var size = battlefield.Size;

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    var cell = battlefield[new(j, i)];
        public static IPosition GetCenter(this IBattlefield battlefield)
        {
            var centerVector = battlefield.CurrentBounds.High / battlefield.CurrentBounds.Low;
            var center       = default(Position);

            center.Set(centerVector);

            return(center);
        }
示例#19
0
        private ShipPlacement NextShipPlacement(IBattlefield battlefield)
        {
            var direction = _randomDirection.Next();
            var position  = _randomPosition.Next(battlefield.Cells.Keys.ToList());

            var placement = new ShipPlacement(position.x, position.y, direction);

            return(placement);
        }
示例#20
0
        public bool AddShip(IBattlefield battlefield, IShip ship, ShipPlacement placement)
        {
            if (!ship.CanPlace(battlefield, placement))
            {
                return(false);
            }

            ship.Place(battlefield, placement);
            return(true);
        }
示例#21
0
        private void SpawnShips(IBattlefield battlefield, IShip[] ships)
        {
            var shipSpawner = new ShipSpawner(battlefield, random);

            foreach (IShip ship in ships)
            {
                bool result = shipSpawner.SpawnShip(ship);
                Debug.Assert(result, "Spawning Ships failed");
            }
        }
示例#22
0
 public override ITurnAction Update(IBot ownBot, IBattlefield battlefield)
 {
     if (!battlefield.Resources.Any())
     {
         return(TurnAction.Idle);
     }
     return(ownBot.HasResource
         ? BaseResource(ownBot)
         : GetResource(ownBot, battlefield));
 }
示例#23
0
        protected void CalculateBattlefieldStartingPosition(IBattlefield battlefield)
        {
            var mediumRow      = this.StartingRow + this.Height / 2;
            var battlefieldRow = mediumRow - battlefield.RowsCount;

            var mediumCol      = this.StartingCol + this.Width / 2;
            var battlefieldCol = mediumCol - battlefield.ColsCount;

            this.BattlefieldStartingPosition = new Position(battlefieldRow, battlefieldCol);
        }
示例#24
0
        public Map(IMapCreationData creationData,
                   IBattlefieldBuilder battlefieldBuilder,
                   ISpawnService spawnService)
        {
            this.battlefield = battlefieldBuilder.Build(creationData.MapInfo);
            this.Width       = battlefield.Width;
            this.Height      = (byte)(battlefield.Cells.Count / this.Width);

            spawnService.Spawn(this.battlefield.Cells, creationData.UserContents);
        }
        public IPosition GetRoamingPath(IPosition current, IBattlefield battlefield)
        {
            var roam = new Position()
            {
                X = random.Next((int)battlefield.CurrentBounds.Low.X, (int)battlefield.CurrentBounds.High.X),
                Y = random.Next((int)battlefield.CurrentBounds.Low.Y, (int)battlefield.CurrentBounds.High.Y),
                Z = random.Next((int)battlefield.CurrentBounds.Low.Z, (int)battlefield.CurrentBounds.High.Z),
            };

            return(GetPath(current, roam, battlefield));
        }
示例#26
0
        public override ITurnAction Update(IBot ownBot, IBattlefield battlefield)
        {
            if (ownBot.EquippedWeapon.Ammunition.Remaining == 0)
            {
                var weapon = battlefield.Weapons.OrderBy(a => a.DistanceTo(ownBot)).FirstOrDefault();
                if (weapon != null)
                {
                    return(ownBot.DistanceTo(weapon) > ownBot.Radius
                        ? TurnAction.MoveTowards(weapon)
                        : TurnAction.PickUpWeapon());
                }
            }

            if (ownBot.HitPoints.Percent < 30)
            {
                if (ownBot.HasResource)
                {
                    return(TurnAction.DropDownResource());
                }
                return(TurnAction.MoveTowards(battlefield.Hospitals.First()));
            }

            if (ownBot.HasResource)
            {
                return(ownBot.DistanceTo(ownBot.Home) > ownBot.Radius
                    ? TurnAction.MoveTowards(ownBot.Home)
                    : TurnAction.DropDownResource());
            }

            var target = battlefield.Bots.Except(new[] { ownBot })
                         .Where(b => b.HasResource)
                         .OrderBy(b => b.DistanceTo(ownBot))
                         .FirstOrDefault();

            if (target != null)
            {
                return(ownBot.DistanceTo(target) < ownBot.EquippedWeapon.MaxRange
                    ? TurnAction.ShootAt(target)
                    : TurnAction.MoveTowards(target));
            }

            if (battlefield.Resources.Any())
            {
                var resource = battlefield.Resources.OrderBy(r => r.DistanceTo(ownBot)).First();
                if (ownBot.DistanceTo(resource) < ownBot.Radius)
                {
                    return(TurnAction.PickUpResource());
                }
                return(TurnAction.MoveTowards(resource.Position));
            }

            return(TurnAction.Idle);
        }
示例#27
0
        public Player(string name, IWater water, IBattlefield battlefield)
        {
            this.Name  = name;
            this.ships = new List <IShip>();
            SetHealth();
            this.IsAlive = true;

            Guard.WhenArgument(water, "Water").IsNull().Throw();
            this.water = water;

            Guard.WhenArgument(battlefield, "Battlefield").IsNull().Throw();
            this.battlefield = battlefield;
        }
        protected IFighterAction GetSkillMove(IBattlefield battlefield, IFighterStats target, IEnumerable <EngineRoundTick> roundTicks, EngineCalculationValues calculationValues)
        {
            var desiredSkill = 50F.Chance() ?
                               SkillFinder.GetMaxDamageSkill(this, SkillFinder.ExcludeSkillsOnCooldown(this, Skills, roundTicks), calculationValues) :
                               SkillFinder.GetMaxRangeSkill(this, SkillFinder.ExcludeSkillsOnCooldown(this, Skills, roundTicks), calculationValues);

            // reduce skill distance by 0.15F to compensate rounding differences
            var distance = (desiredSkill?.Range ?? 1.5F) - 0.15F;

            return(new Move()
            {
                Actor = this,
                NextPosition = PathFinder.GetPathToEnemy(this, target, distance, battlefield),
            });
        }
示例#29
0
        public bool AddShip(IBattlefield battlefield, IShip ship)
        {
            for (var i = 0; i < _maxAttempts; i++)
            {
                var placement = NextShipPlacement(battlefield);

                if (ship.CanPlace(battlefield, placement))
                {
                    ship.Place(battlefield, placement);

                    return(true);
                }
            }

            return(false);
        }
示例#30
0
        public override ITurnAction Update(IBot ownBot, IBattlefield battlefield)
        {
            if (ownBot.HasResource)
            {
                return(ownBot.DistanceTo(ownBot.Home) > ownBot.Radius
                    ? TurnAction.MoveTowards(ownBot.Home)
                    : TurnAction.DropDownResource());
            }

            if (battlefield.Resources.Any())
            {
                var resource = battlefield.Resources.OrderBy(r => r.DistanceTo(ownBot)).First();
                return(ownBot.DistanceTo(resource) < ownBot.Radius
                    ? TurnAction.PickUpResource()
                    : TurnAction.MoveTowards(resource.Position));
            }

            return(TurnAction.Idle);
        }