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); }
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); }
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); } }
public ShipSpawner(IBattlefield battlefield, Random random) { this.battlefield = battlefield; this.random = random; fieldsOccupiedBySpawnedShips = new List <int[]>(); startingPosition = new int[2]; }
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); }
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>()); } }
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)); }
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); }
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); }
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)); }
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)); }
public void Place(IBattlefield battlefield, ShipPlacement placement) { var shipPositions = _battleshipPlacement.GetShipPositions(placement); foreach (var shipPosition in shipPositions) { battlefield.MarkCell(shipPosition); } }
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), }); }
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); }
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); }
public bool AddShip(IBattlefield battlefield, IShip ship, ShipPlacement placement) { if (!ship.CanPlace(battlefield, placement)) { return(false); } ship.Place(battlefield, placement); return(true); }
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"); } }
public override ITurnAction Update(IBot ownBot, IBattlefield battlefield) { if (!battlefield.Resources.Any()) { return(TurnAction.Idle); } return(ownBot.HasResource ? BaseResource(ownBot) : GetResource(ownBot, battlefield)); }
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); }
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)); }
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); }
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), }); }
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); }
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); }