public void ExecuteAI(Game gameState) { if (Ships.Any(f => f.Orders.Count() == 0)) { Ship[] targetList = new Ship[gameState.Players.Where(f => f != this && !f.IsDefeated).Max(p => p.Ships.Where(s => !s.IsDestroyed).Count())]; //build lists of targets+distance (jump closest target) int counter =0; List<Tuple<Ship, double>> AllEnemyShips = new List<Tuple<Ship, double>>(); foreach(Ship ship in this.Ships.Where(f=>!f.IsDestroyed && f.Orders.Count()==0)) { foreach(Player p in gameState.Players.Where(f=>f!=this && !f.IsDefeated)) foreach(Ship s in p.Ships.Where(f=>!f.IsDestroyed)) AllEnemyShips.Add(new Tuple<Ship,double>(s,LocationCollection.GetTacticalDistance(ship.TacticalPosition,s.TacticalPosition))); counter++; } // take highest target from all lists List<TargetTracker> EachEnemyShip = new List<TargetTracker>(); List<Ship> EnemyShips = new List<Ship>(); foreach (Tuple<Ship, double> enemyShip in AllEnemyShips) { if (!EnemyShips.Contains(enemyShip.Item1)) EnemyShips.Add(enemyShip.Item1); } foreach (Ship enemyShip in EnemyShips) { double sumDistance = AllEnemyShips.Where(f=>f.Item1==enemyShip).Sum(f=>f.Item2); EachEnemyShip.Add(new TargetTracker(enemyShip, sumDistance, 0)); } // find closest friendly ship foreach (Ship ship in this.Ships.Where(f => !f.IsDestroyed)) { Ship targetShip = EachEnemyShip.First(f => f.Range == EachEnemyShip.Min(z => z.Range)).Ship; //add move order (Move to target and Minimum Weapon Range) int range = Convert.ToInt32(ship.Parts.Where(f=>!f.IsDestroyed && f is WeaponPart).Min(f=>((WeaponPart)f).Range)); MoveToShipAtRange mtsar = new MoveToShipAtRange(targetShip, range, gameState.CombatLocations); ship.Orders.Add(mtsar); //add attack orders (Attack target with all weapons) foreach (WeaponPart weapon in ship.Parts.Where(f=>!f.IsDestroyed && f is WeaponPart)) { FireWeaponAtTarget fwat = new FireWeaponAtTarget(weapon, targetShip); ship.Orders.Add(fwat); } // calculate if this ship is enough to destroy target within Aggressiveness // this Damage Per Turn double totalDPT = 0; foreach (WeaponPart weapon in ship.Parts.Where(f => !f.IsDestroyed && f is WeaponPart)) if (weapon.ReloadTime > 0) totalDPT += (weapon.WeaponDamage / weapon.ReloadTime); else totalDPT += weapon.WeaponDamage; // miss chance totalDPT = totalDPT * 0.9d; //aggressiveness double totalDPA = totalDPT * _aggressiveness; // target total health double targetHealth = targetShip.HP.Current; foreach (DefensePart defense in targetShip.Parts.Where(f => !f.IsDestroyed && f is DefensePart)) { targetHealth += defense.DR + defense.HP.Current; } TargetTracker target = EachEnemyShip.First(f=>f.Ship==targetShip); if (target.TotalDPA + totalDPA >= targetHealth) EachEnemyShip.Remove(EachEnemyShip.First(f => f.Ship == targetShip)); else target.TotalDPA += totalDPA; } } }
void MenuWeapon_Click(object sender, RoutedEventArgs e) { try { Tuple<Ship, List<WeaponPart>> fireWeapons = (Tuple<Ship, List<WeaponPart>>)((MenuItem)sender).CommandParameter; foreach (WeaponPart weapon in fireWeapons.Item2) { try { FireWeaponAtTarget fwat = new FireWeaponAtTarget(weapon, fireWeapons.Item1); fwat.OnWeaponFired += new OrderDelegates.TacticalWeaponFiredEvent(onWeaponFiredHandler); currentShip.Orders.Add(fwat); } catch (Exception ex) { if (LogEverything) Logger(ex); } } ShowShipStatus(); } catch (Exception ex) { if (LogEverything) Logger(ex); } }