/// <summary> /// Advances the game by one turn. /// </summary> public void NextTurn() { foreach (Faction faction in world.GetFactions().Where(f => f.Alive)) { // Economy faction.Wealth += GetFactionIncome(faction.Id); faction.Wealth -= GetFactionOutcome(faction.Id); // Recruit // TODO: Find a way around the hardcoded "militia" unit identifier GetArmy(faction.Id, "militia").Size += GetFactionRecruitment(faction.Id); // A.I. if (faction.Id == PlayerFactionId) { continue; } AiBuild(faction.Id); AiRecruit(faction.Id); string provinceId = attack.ChooseProvinceToAttack(faction.Id); if (GetFactionTroopsAmount(faction.Id) < GetWorld().MinTroopsPerAttack || string.IsNullOrEmpty(provinceId)) { continue; } AttackProvince(faction.Id, provinceId); } Parallel.ForEach(GetProvinces(), province => province.Locked = false); UpdateFactionsAliveStatus(); CheckForWinner(); Turn += 1; }
/// <summary> /// Attacks the province. /// </summary> /// <param name="factionId">Faction identifier.</param> /// <param name="provinceId">Province identifier.</param> public BattleResult AttackProvince(string factionId, string provinceId) { Province targetProvince = world.GetProvinces().FirstOrDefault(r => r.Id == provinceId); if (string.IsNullOrWhiteSpace(provinceId) || targetProvince.Locked || !world.FactionBordersProvince(factionId, provinceId)) { throw new InvalidTargetProvinceException(provinceId); } Faction attackerFaction = world.GetFactions().FirstOrDefault(f => f.Id == factionId); Faction defenderFaction = world.GetFactions().FirstOrDefault(f => f.Id == targetProvince.FactionId); if (defenderFaction.Id == attackerFaction.Id || defenderFaction.Id == GameDefines.GAIA_FACTION) { throw new InvalidTargetProvinceException(provinceId); } while (world.GetFactionTroopsAmount(attackerFaction.Id) > 0 && world.GetFactionTroopsAmount(defenderFaction.Id) > 0) { Army attackerArmy = world.GetFactionArmies(attackerFaction.Id) .Where(a => a.Size > 0) .GetRandomElement(); Army defenderArmy = world.GetFactionArmies(defenderFaction.Id) .Where(a => a.Size > 0) .GetRandomElement(); Unit attackerUnit = world.GetUnits().FirstOrDefault(u => u.Id == attackerArmy.UnitId); Unit defenderUnit = world.GetUnits().FirstOrDefault(u => u.Id == defenderArmy.UnitId); // TODO: Attack and Defence bonuses int attackerTroopsLeft = (attackerUnit.Health * attackerArmy.Size - defenderUnit.Power * defenderArmy.Size) / attackerUnit.Health; int defenderTroopsLeft = (defenderUnit.Health * defenderArmy.Size - attackerUnit.Power * attackerArmy.Size) / defenderUnit.Health; attackerArmy.Size = Math.Max(0, attackerTroopsLeft); defenderArmy.Size = Math.Max(0, defenderTroopsLeft); } // TODO: In the GameDomainService I should change the realations based on wether the // province was sovereign or not if (world.GetFactionTroopsAmount(attackerFaction.Id) > world.GetFactionTroopsAmount(defenderFaction.Id)) { world.TransferProvince(provinceId, factionId); targetProvince.Locked = true; return(BattleResult.Victory); } return(BattleResult.Defeat); }