Пример #1
0
        public Battle(List <Property> properties, World world)
            : base(properties, world)
        {
            Initialize();

            var attackerSquadRaces            = new List <CreatureInfo>();
            var attackerSquadEntityPopulation = new List <int>();
            var attackerSquadNumbers          = new List <int>();
            var attackerSquadDeaths           = new List <int>();
            var attackerSquadSite             = new List <int>();
            var defenderSquadRaces            = new List <CreatureInfo>();
            var defenderSquadEntityPopulation = new List <int>();
            var defenderSquadNumbers          = new List <int>();
            var defenderSquadDeaths           = new List <int>();
            var defenderSquadSite             = new List <int>();

            foreach (Property property in properties)
            {
                switch (property.Name)
                {
                case "outcome":
                    switch (property.Value)
                    {
                    case "attacker won": Outcome = BattleOutcome.AttackerWon; break;

                    case "defender won": Outcome = BattleOutcome.DefenderWon; break;

                    default: Outcome = BattleOutcome.Unknown; world.ParsingErrors.Report("Unknown Battle Outcome: " + property.Value); break;
                    }
                    break;

                case "name": Name = Formatting.InitCaps(property.Value); break;

                case "coords": Coordinates = Formatting.ConvertToLocation(property.Value); break;

                case "war_eventcol": ParentCollection = world.GetEventCollection(Convert.ToInt32(property.Value)); break;

                case "subregion_id": Region = world.GetRegion(Convert.ToInt32(property.Value)); break;

                case "feature_layer_id": UndergroundRegion = world.GetUndergroundRegion(Convert.ToInt32(property.Value)); break;

                case "site_id": Site = world.GetSite(Convert.ToInt32(property.Value)); break;

                case "attacking_hfid": NotableAttackers.Add(world.GetHistoricalFigure(Convert.ToInt32(property.Value))); break;

                case "defending_hfid": NotableDefenders.Add(world.GetHistoricalFigure(Convert.ToInt32(property.Value))); break;

                case "attacking_squad_race": attackerSquadRaces.Add(world.GetCreatureInfo(property.Value)); break;

                case "attacking_squad_entity_pop": attackerSquadEntityPopulation.Add(Convert.ToInt32(property.Value)); break;

                case "attacking_squad_number":
                    int attackerSquadNumber = Convert.ToInt32(property.Value);
                    attackerSquadNumbers.Add(attackerSquadNumber <0 || attackerSquadNumber> Squad.MAX_SIZE ? Squad.MAX_SIZE : attackerSquadNumber);
                    break;

                case "attacking_squad_deaths":
                    int attackerSquadDeath = Convert.ToInt32(property.Value);
                    attackerSquadDeaths.Add(attackerSquadDeath <0 || attackerSquadDeath> Squad.MAX_SIZE ? Squad.MAX_SIZE : attackerSquadDeath);
                    break;

                case "attacking_squad_site": attackerSquadSite.Add(Convert.ToInt32(property.Value)); break;

                case "defending_squad_race": defenderSquadRaces.Add(world.GetCreatureInfo(property.Value)); break;

                case "defending_squad_entity_pop": defenderSquadEntityPopulation.Add(Convert.ToInt32(property.Value)); break;

                case "defending_squad_number":
                    int defenderSquadNumber = Convert.ToInt32(property.Value);
                    defenderSquadNumbers.Add(defenderSquadNumber <0 || defenderSquadNumber> Squad.MAX_SIZE ? Squad.MAX_SIZE : defenderSquadNumber);
                    break;

                case "defending_squad_deaths":
                    int defenderSquadDeath = Convert.ToInt32(property.Value);
                    defenderSquadDeaths.Add(defenderSquadDeath <0 || defenderSquadDeath> Squad.MAX_SIZE ? Squad.MAX_SIZE : defenderSquadDeath);
                    break;

                case "defending_squad_site": defenderSquadSite.Add(Convert.ToInt32(property.Value)); break;

                case "noncom_hfid": NonCombatants.Add(world.GetHistoricalFigure(Convert.ToInt32(property.Value))); break;

                case "individual_merc": property.Known = true; IndividualMercenaries = true; break;

                case "company_merc": property.Known = true; CompanyMercenaries = true; break;

                case "attacking_merc_enid":
                    AttackingMercenaryEntity = world.GetEntity(Convert.ToInt32(property.Value));
                    if (AttackingMercenaryEntity != null)
                    {
                        AttackingMercenaryEntity.Type = EntityType.MercenaryCompany;
                    }
                    break;

                case "defending_merc_enid":
                    DefendingMercenaryEntity = world.GetEntity(Convert.ToInt32(property.Value));
                    if (DefendingMercenaryEntity != null)
                    {
                        DefendingMercenaryEntity.Type = EntityType.MercenaryCompany;
                    }
                    break;

                case "attacking_squad_animated": property.Known = true; AttackingSquadAnimated = true; break;

                case "defending_squad_animated": property.Known = true; DefendingSquadAnimated = true; break;

                case "a_support_merc_enid":
                    var attackerSupportMercenaryEntity = world.GetEntity(Convert.ToInt32(property.Value));
                    if (attackerSupportMercenaryEntity != null)
                    {
                        AttackerSupportMercenaryEntities.Add(attackerSupportMercenaryEntity);
                        attackerSupportMercenaryEntity.Type = EntityType.MercenaryCompany;
                    }
                    break;

                case "d_support_merc_enid":
                    var defenderSupportMercenaryEntity = world.GetEntity(Convert.ToInt32(property.Value));
                    if (defenderSupportMercenaryEntity != null)
                    {
                        DefenderSupportMercenaryEntities.Add(defenderSupportMercenaryEntity);
                        defenderSupportMercenaryEntity.Type = EntityType.MercenaryCompany;
                    }
                    break;

                case "a_support_merc_hfid": AttackerSupportMercenaryHfs.Add(world.GetHistoricalFigure(Convert.ToInt32(property.Value))); break;

                case "d_support_merc_hfid": DefenderSupportMercenaryHfs.Add(world.GetHistoricalFigure(Convert.ToInt32(property.Value))); break;
                }
            }

            if (Collection.OfType <AttackedSite>().Any())
            {
                Attacker = Collection.OfType <AttackedSite>().First().Attacker;
                Defender = Collection.OfType <AttackedSite>().First().Defender;
            }
            else if (Collection.OfType <FieldBattle>().Any())
            {
                Attacker = Collection.OfType <FieldBattle>().First().Attacker;
                Defender = Collection.OfType <FieldBattle>().First().Defender;
            }

            foreach (HistoricalFigure involvedHf in NotableAttackers.Union(NotableDefenders).Where(hf => hf != HistoricalFigure.Unknown))
            {
                involvedHf.Battles.Add(this);
                involvedHf.AddEventCollection(this);
                involvedHf.AddEvent(new BattleFought(involvedHf, this, World));
            }

            foreach (HistoricalFigure involvedSupportMercenaries in AttackerSupportMercenaryHfs.Union(DefenderSupportMercenaryHfs).Where(hf => hf != HistoricalFigure.Unknown))
            {
                involvedSupportMercenaries.Battles.Add(this);
                involvedSupportMercenaries.AddEventCollection(this);
                involvedSupportMercenaries.AddEvent(new BattleFought(involvedSupportMercenaries, this, World, true, true));
            }

            for (int i = 0; i < attackerSquadRaces.Count; i++)
            {
                AttackerSquads.Add(new Squad(attackerSquadRaces[i], attackerSquadNumbers[i], attackerSquadDeaths[i], attackerSquadSite[i], attackerSquadEntityPopulation[i]));
            }

            for (int i = 0; i < defenderSquadRaces.Count; i++)
            {
                DefenderSquads.Add(new Squad(defenderSquadRaces[i], defenderSquadNumbers[i], defenderSquadDeaths[i], defenderSquadSite[i], defenderSquadEntityPopulation[i]));
            }

            var groupedAttackerSquads = from squad in AttackerSquads
                                        group squad by squad.Race into squadRace
                                        select new { Race = squadRace.Key, Count = squadRace.Sum(squad => squad.Numbers), Deaths = squadRace.Sum(squad => squad.Deaths) };

            foreach (var squad in groupedAttackerSquads)
            {
                int   attackerSquadNumber = squad.Count + NotableAttackers.Count(attacker => attacker?.Race?.Id == squad.Race.Id);
                int   attackerSquadDeath  = squad.Deaths + Collection.OfType <HfDied>().Count(death => death.HistoricalFigure?.Race == squad.Race && NotableAttackers.Contains(death.HistoricalFigure));
                Squad attackerSquad       = new Squad(squad.Race, attackerSquadNumber, attackerSquadDeath, -1, -1);
                Attackers.Add(attackerSquad);
            }

            foreach (var attacker in NotableAttackers.Where(hf => Attackers.Count(squad => squad.Race == hf.Race) == 0).GroupBy(hf => hf.Race).Select(race => new { Race = race.Key, Count = race.Count() }))
            {
                var attackerDeath = Collection.OfType <HfDied>().Count(death => NotableAttackers.Contains(death.HistoricalFigure) && death.HistoricalFigure?.Race == attacker.Race);
                Attackers.Add(new Squad(attacker.Race, attacker.Count, attackerDeath, -1, -1));
            }

            var groupedDefenderSquads = from squad in DefenderSquads
                                        group squad by squad.Race into squadRace
                                        select new { Race = squadRace.Key, Count = squadRace.Sum(squad => squad.Numbers), Deaths = squadRace.Sum(squad => squad.Deaths) };

            foreach (var squad in groupedDefenderSquads)
            {
                int defenderSquadNumber = squad.Count + NotableDefenders.Count(defender => defender?.Race?.Id == squad.Race.Id);
                int defenderSquadDeath  = squad.Deaths + Collection.OfType <HfDied>().Count(death => death.HistoricalFigure?.Race == squad.Race && NotableDefenders.Contains(death.HistoricalFigure));
                Defenders.Add(new Squad(squad.Race, defenderSquadNumber, defenderSquadDeath, -1, -1));
            }

            foreach (var defender in NotableDefenders.Where(hf => Defenders.Count(squad => squad.Race == hf.Race) == 0).GroupBy(hf => hf.Race).Select(race => new { Race = race.Key, Count = race.Count() }))
            {
                int defenderDeath = Collection.OfType <HfDied>().Count(death => NotableDefenders.Contains(death.HistoricalFigure) && death.HistoricalFigure.Race == defender.Race);
                Defenders.Add(new Squad(defender.Race, defender.Count, defenderDeath, -1, -1));
            }

            Deaths = new Dictionary <CreatureInfo, int>();
            foreach (Squad squad in Attackers.Concat(Defenders).Where(a => a.Race != null && a.Race != CreatureInfo.Unknown))
            {
                if (Deaths.ContainsKey(squad.Race))
                {
                    Deaths[squad.Race] += squad.Deaths;
                }
                else
                {
                    Deaths[squad.Race] = squad.Deaths;
                }
            }

            AttackerDeathCount = Attackers.Sum(attacker => attacker.Deaths);
            DefenderDeathCount = Defenders.Sum(defender => defender.Deaths);

            if (Outcome == BattleOutcome.AttackerWon)
            {
                Victor = Attacker;
            }
            else if (Outcome == BattleOutcome.DefenderWon)
            {
                Victor = Defender;
            }

            if (ParentCollection is War parentWar)
            {
                if (parentWar.Attacker == Attacker)
                {
                    parentWar.AttackerDeathCount += AttackerDeathCount;
                    parentWar.DefenderDeathCount += DefenderDeathCount;
                }
                else
                {
                    parentWar.AttackerDeathCount += DefenderDeathCount;
                    parentWar.DefenderDeathCount += AttackerDeathCount;
                }
                parentWar.DeathCount += attackerSquadDeaths.Sum() + defenderSquadDeaths.Sum() + Collection.OfType <HfDied>().Count();

                if (Attacker == parentWar.Attacker && Victor == Attacker)
                {
                    parentWar.AttackerVictories.Add(this);
                }
                else
                {
                    parentWar.DefenderVictories.Add(this);
                }
            }

            Site?.Warfare.Add(this);
            Region?.Battles.Add(this);
            UndergroundRegion?.Battles.Add(this);

            if (attackerSquadDeaths.Sum() + defenderSquadDeaths.Sum() + Collection.OfType <HfDied>().Count() == 0)
            {
                Notable = false;
            }

            if (attackerSquadNumbers.Sum() + NotableAttackers.Count > (defenderSquadNumbers.Sum() + NotableDefenders.Count) * 10 && //NotableDefenders outnumbered 10 to 1
                Victor == Attacker &&
                AttackerDeathCount < (NotableAttackers.Count + attackerSquadNumbers.Sum()) * 0.1)    //NotableAttackers losses < 10%
            {
                Notable = false;
            }
            Attacker.AddEventCollection(this);
            if (Defender != Attacker)
            {
                Defender.AddEventCollection(this);
            }
            Region.AddEventCollection(this);
            UndergroundRegion.AddEventCollection(this);
            Site.AddEventCollection(this);
        }
Пример #2
0
        public Battle(List <Property> properties, World world)
            : base(properties, world)
        {
            Initialize();

            var attackerSquadRace             = new List <string>();
            var attackerSquadEntityPopulation = new List <int>();
            var attackerSquadNumbers          = new List <int>();
            var attackerSquadDeaths           = new List <int>();
            var attackerSquadSite             = new List <int>();
            var defenderSquadRace             = new List <string>();
            var defenderSquadEntityPopulation = new List <int>();
            var defenderSquadNumbers          = new List <int>();
            var defenderSquadDeaths           = new List <int>();
            var defenderSquadSite             = new List <int>();

            foreach (Property property in properties)
            {
                switch (property.Name)
                {
                case "outcome":
                    switch (property.Value)
                    {
                    case "attacker won": Outcome = BattleOutcome.AttackerWon; break;

                    case "defender won": Outcome = BattleOutcome.DefenderWon; break;

                    default: Outcome = BattleOutcome.Unknown; world.ParsingErrors.Report("Unknown Battle Outcome: " + property.Value); break;
                    }
                    break;

                case "name": Name = Formatting.InitCaps(property.Value); break;

                case "coords": Coordinates = Formatting.ConvertToLocation(property.Value); break;

                case "war_eventcol": ParentCollection = world.GetEventCollection(Convert.ToInt32(property.Value)); break;

                case "subregion_id": Region = world.GetRegion(Convert.ToInt32(property.Value)); break;

                case "feature_layer_id": UndergroundRegion = world.GetUndergroundRegion(Convert.ToInt32(property.Value)); break;

                case "site_id": Site = world.GetSite(Convert.ToInt32(property.Value)); break;

                case "attacking_hfid": NotableAttackers.Add(world.GetHistoricalFigure(Convert.ToInt32(property.Value))); break;

                case "defending_hfid": NotableDefenders.Add(world.GetHistoricalFigure(Convert.ToInt32(property.Value))); break;

                case "attacking_squad_race": attackerSquadRace.Add(Formatting.FormatRace(property.Value)); break;

                case "attacking_squad_entity_pop": attackerSquadEntityPopulation.Add(Convert.ToInt32(property.Value)); break;

                case "attacking_squad_number": attackerSquadNumbers.Add(Convert.ToInt32(property.Value)); break;

                case "attacking_squad_deaths": attackerSquadDeaths.Add(Convert.ToInt32(property.Value)); break;

                case "attacking_squad_site": attackerSquadSite.Add(Convert.ToInt32(property.Value)); break;

                case "defending_squad_race": defenderSquadRace.Add(Formatting.FormatRace(property.Value)); break;

                case "defending_squad_entity_pop": defenderSquadEntityPopulation.Add(Convert.ToInt32(property.Value)); break;

                case "defending_squad_number": defenderSquadNumbers.Add(Convert.ToInt32(property.Value)); break;

                case "defending_squad_deaths": defenderSquadDeaths.Add(Convert.ToInt32(property.Value)); break;

                case "defending_squad_site": defenderSquadSite.Add(Convert.ToInt32(property.Value)); break;

                case "noncom_hfid": NonCombatants.Add(world.GetHistoricalFigure(Convert.ToInt32(property.Value))); break;
                }
            }

            if (Collection.OfType <AttackedSite>().Any())
            {
                Attacker = Collection.OfType <AttackedSite>().First().Attacker;
                Defender = Collection.OfType <AttackedSite>().First().Defender;
            }
            else if (Collection.OfType <FieldBattle>().Any())
            {
                Attacker = Collection.OfType <FieldBattle>().First().Attacker;
                Defender = Collection.OfType <FieldBattle>().First().Defender;
            }

            foreach (HistoricalFigure attacker in NotableAttackers)
            {
                attacker.Battles.Add(this);
            }

            foreach (HistoricalFigure defender in NotableDefenders)
            {
                defender.Battles.Add(this);
            }

            foreach (HistoricalFigure nonCombatant in NonCombatants)
            {
                nonCombatant.Battles.Add(this);
            }

            for (int i = 0; i < attackerSquadRace.Count; i++)
            {
                AttackerSquads.Add(new Squad(attackerSquadRace[i], attackerSquadNumbers[i], attackerSquadDeaths[i], attackerSquadSite[i], attackerSquadEntityPopulation[i]));
            }

            for (int i = 0; i < defenderSquadRace.Count; i++)
            {
                DefenderSquads.Add(new Squad(defenderSquadRace[i], defenderSquadNumbers[i], defenderSquadDeaths[i], defenderSquadSite[i], defenderSquadEntityPopulation[i]));
            }

            var groupedAttackerSquads = from squad in AttackerSquads
                                        group squad by squad.Race into squadRace
                                        select new { Race = squadRace.Key, Count = squadRace.Sum(squad => squad.Numbers), Deaths = squadRace.Sum(squad => squad.Deaths) };

            foreach (var squad in groupedAttackerSquads)
            {
                Attackers.Add(new Squad(squad.Race, squad.Count + NotableAttackers.Count(attacker => attacker.Race == squad.Race), squad.Deaths + Collection.OfType <HfDied>().Count(death => death.HistoricalFigure.Race == squad.Race && NotableAttackers.Contains(death.HistoricalFigure)), -1, -1));
            }

            foreach (var attacker in NotableAttackers.Where(hf => Attackers.Count(squad => squad.Race == hf.Race) == 0).GroupBy(hf => hf.Race).Select(race => new { Race = race.Key, Count = race.Count() }))
            {
                Attackers.Add(new Squad(attacker.Race, attacker.Count, Collection.OfType <HfDied>().Count(death => NotableAttackers.Contains(death.HistoricalFigure) && death.HistoricalFigure.Race == attacker.Race), -1, -1));
            }
            AttackersAsList = new List <string>();
            foreach (Squad squad in Attackers)
            {
                for (int i = 0; i < squad.Numbers; i++)
                {
                    AttackersAsList.Add(squad.Race);
                }
            }

            var groupedDefenderSquads = from squad in DefenderSquads
                                        group squad by squad.Race into squadRace
                                        select new { Race = squadRace.Key, Count = squadRace.Sum(squad => squad.Numbers), Deaths = squadRace.Sum(squad => squad.Deaths) };

            foreach (var squad in groupedDefenderSquads)
            {
                Defenders.Add(new Squad(squad.Race, squad.Count + NotableDefenders.Count(defender => defender.Race == squad.Race), squad.Deaths + Collection.OfType <HfDied>().Count(death => death.HistoricalFigure.Race == squad.Race && NotableDefenders.Contains(death.HistoricalFigure)), -1, -1));
            }

            foreach (var defender in NotableDefenders.Where(hf => Defenders.Count(squad => squad.Race == hf.Race) == 0).GroupBy(hf => hf.Race).Select(race => new { Race = race.Key, Count = race.Count() }))
            {
                Defenders.Add(new Squad(defender.Race, defender.Count, Collection.OfType <HfDied>().Count(death => NotableDefenders.Contains(death.HistoricalFigure) && death.HistoricalFigure.Race == defender.Race), -1, -1));
            }
            DefendersAsList = new List <string>();
            foreach (Squad squad in Defenders)
            {
                for (int i = 0; i < squad.Numbers; i++)
                {
                    DefendersAsList.Add(squad.Race);
                }
            }

            Deaths = new List <string>();
            foreach (Squad squad in Attackers.Concat(Defenders))
            {
                for (int i = 0; i < squad.Deaths; i++)
                {
                    Deaths.Add(squad.Race);
                }
            }

            AttackerDeathCount = Attackers.Sum(attacker => attacker.Deaths);
            DefenderDeathCount = Defenders.Sum(defender => defender.Deaths);

            if (Outcome == BattleOutcome.AttackerWon)
            {
                Victor = Attacker;
            }
            else if (Outcome == BattleOutcome.DefenderWon)
            {
                Victor = Defender;
            }

            if (ParentCollection is War parentWar)
            {
                if (parentWar.Attacker == Attacker)
                {
                    parentWar.AttackerDeathCount += AttackerDeathCount;
                    parentWar.DefenderDeathCount += DefenderDeathCount;
                }
                else
                {
                    parentWar.AttackerDeathCount += DefenderDeathCount;
                    parentWar.DefenderDeathCount += AttackerDeathCount;
                }
                parentWar.DeathCount += attackerSquadDeaths.Sum() + defenderSquadDeaths.Sum() + Collection.OfType <HfDied>().Count();

                if (Attacker == parentWar.Attacker && Victor == Attacker)
                {
                    parentWar.AttackerVictories.Add(this);
                }
                else
                {
                    parentWar.DefenderVictories.Add(this);
                }
            }

            if (Site != null)
            {
                Site.Warfare.Add(this);
            }

            if (Region != null)
            {
                Region.Battles.Add(this);
            }

            if (UndergroundRegion != null)
            {
                UndergroundRegion.Battles.Add(this);
            }

            if (attackerSquadDeaths.Sum() + defenderSquadDeaths.Sum() + Collection.OfType <HfDied>().Count() == 0)
            {
                Notable = false;
            }

            if (attackerSquadNumbers.Sum() + NotableAttackers.Count > (defenderSquadNumbers.Sum() + NotableDefenders.Count) * 10 && //NotableDefenders outnumbered 10 to 1
                Victor == Attacker &&
                AttackerDeathCount < (NotableAttackers.Count + attackerSquadNumbers.Sum()) * 0.1)    //NotableAttackers lossses < 10%
            {
                Notable = false;
            }
        }