Example #1
0
        public bool Damage(Character attacker, uint amount)
        {
            lock (this)
            {
                amount = Math.Min(amount, Health);

                //does the victim knows its attacker?
                if (Attackers.ContainsKey(attacker))
                {
                    //if so then add to his established dmg bill
                    Attackers[attacker] += amount;
                }
                else
                {
                    //if not so then add him as new attacker
                    Attackers.Add(attacker, amount);
                }

                Health -= amount;   //decrease health by amount dealt
                ShowHpTo(attacker); //show monster's remaining hp bar to attacker

                if (Health <= 0)    //???
                {
                    return(true);
                }

                return(false);
            }
        }
Example #2
0
    private void SetAttackAndDefense()
    {
        Attackers.Clear();
        Defenders.Clear();

        List <Actor> units_within_range = FindObjectsOfType <Actor>().
                                          Where(actor => Vector3.Distance(actor.transform.position, transform.position) < influence_zone_radius).
                                          ToList();

        if (NodeFaction != null)
        {
            // attack and defense is determined by who currently claims the node

            for (int i = 0; i < units_within_range.Count; i++)
            {
                Actor actor = units_within_range[i];

                if (actor != null)
                {
                    if (actor.CurrentFaction.IsHostileTo(NodeFaction))
                    {
                        if (!Attackers.Contains(actor) && actor != null)
                        {
                            Attackers.Add(actor);
                        }
                    }
                    else
                    {
                        if (!Defenders.Contains(actor) && actor != null)
                        {
                            Defenders.Add(actor);
                        }
                    }
                }
            }
        }
        else
        {
            // the attackers are the faction with the most units; everybody else can try to kill them
            // NOTE: the faction that has the most units for the last boost gains the node (of course, then they have to keep it)

            var faction_units = units_within_range
                                .GroupBy(unit => unit.CurrentFaction,
                                         (faction, factions) => new {
                Key   = faction,
                Count = factions.Count()
            })
                                .OrderByDescending(faction => faction.Count);

            Attackers.AddRange(units_within_range.Where(unit => unit.CurrentFaction == faction_units.First().Key));
        }
    }
Example #3
0
 private void AddFighter(Fighter f)
 {
     if (AddToAttackers)
     {
         Attackers.Add(f);
         AttackersList.Items.Add(f.ToString());
     }
     else
     {
         Defenders.Add(f);
         DefendersList.Items.Add(f.ToString());
     }
 }
Example #4
0
        public void DbLoaderAddToCombatList(ICombatGroup group, BattleSide side)
        {
            if (side == BattleSide.Defense)
            {
                Defenders.Add(group, false);
            }
            else
            {
                Attackers.Add(group, false);
            }

            groupIdGen.Set(group.Id);
        }
Example #5
0
        public bool Damage(GameCharacter attacker, uint amount)
        {
            lock (this)
            {
                var originalAmount = amount;

                amount = Math.Min(amount, Health);

                if (Attackers.ContainsKey(attacker))
                {
                    Attackers[attacker] += amount;
                }
                else
                {
                    Attackers.Add(attacker, amount);
                }

                Health -= amount;

                return(Health <= 0);
            }
        }
Example #6
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);
        }
Example #7
0
        public virtual void Damage(double dmg, Character attacker = null, int sound = 0, DamageType damageType = DamageType.RawDamage, DamageFlags flags = DamageFlags.None)
        {
            if (LifeStatus != LifeStatus.Alive)
            {
                return;
            }

            RemoveStatus("Morph");

            var realDamage = dmg;

            if ((flags & DamageFlags.CanBeAbsorbed) == DamageFlags.CanBeAbsorbed)
            {
                if (AbsorbingAbsoluteDamage)
                {
                    CurrentAbsoluteDamageAbsorbed += dmg;
                    if (MaximumAbsoluteDamageAbsorbed <= CurrentAbsoluteDamageAbsorbed)
                    {
                        RemoveStatus("AbsoluteAbsorb");
                    }
                    realDamage = 0;
                    SpellAnimation(AbsoluteAbsorbAnimation, 100);
                }
                else if (AbsorbingPhysicalDamage && damageType == DamageType.Physical)
                {
                    CurrentPhysicalDamageAbsorbed += dmg;
                    if (MaximumPhysicalDamageAbsorbed <= CurrentPhysicalDamageAbsorbed)
                    {
                        RemoveStatus("PhysicalAbsorb");
                    }
                    realDamage = 0;
                    SpellAnimation(PhysicalAbsorbAnimation, 100);
                }
                else if (AbsorbingMagicalDamage && damageType == DamageType.Magical)
                {
                    CurrentMagicalDamageAbsorbed += dmg;
                    if (MaximumMagicalDamageAbsorbed <= CurrentMagicalDamageAbsorbed)
                    {
                        RemoveStatus("MagicalAbsorb");
                    }
                    realDamage = 0;
                    SpellAnimation(MagicalAbsorbAnimation, 100);
                }
            }

            if (damageType == DamageType.Physical)
            {
                realDamage *= ArmorProtection * (1d - PhysicalProtection);
            }
            else if (damageType == DamageType.Magical)
            {
                realDamage -= (realDamage * (MagicResistance / 100d));
                realDamage *= (1d - MagicalProtection);
            }

            if (Map.Flags.HasFlag(MapFlags.PlayerKill))
            {
                dmg *= 0.75;
            }

            if (attacker != null)
            {
                if (!Enemies.Contains(attacker))
                {
                    Enemies.Add(attacker);
                }
                if (!attacker.Enemies.Contains(this))
                {
                    attacker.Enemies.Add(this);
                }
                LastAttacker = attacker;
            }

            if ((flags & DamageFlags.CanBeRedirected) == DamageFlags.CanBeRedirected)
            {
                if (RedirectingPhysicalDamage && damageType == DamageType.Physical)
                {
                    if (PhysicalRedirectTarget != null && PhysicalRedirectTarget != this && WithinRange(PhysicalRedirectTarget, 12))
                    {
                        var yourDamage = realDamage * PhysicalRedirectPercent;
                        realDamage -= yourDamage;
                        PhysicalRedirectTarget.Damage(yourDamage, null, 0, DamageType.RawDamage, DamageFlags.None);
                    }
                    if (--PhysicalRedirectCount == 0)
                    {
                        RemoveStatus("PhysicalRedirect");
                    }
                }

                if (RedirectingMagicalDamage && damageType == DamageType.Magical)
                {
                    if (MagicalRedirectTarget != null && MagicalRedirectTarget != this && WithinRange(MagicalRedirectTarget, 12))
                    {
                        var yourDamage = realDamage * MagicalRedirectPercent;
                        realDamage -= yourDamage;
                        MagicalRedirectTarget.Damage(yourDamage, null, 0, DamageType.RawDamage, DamageFlags.None);
                    }
                    if (--MagicalRedirectCount == 0)
                    {
                        RemoveStatus("MagicalRedirect");
                    }
                }
            }

            if ((flags & DamageFlags.CanBeConvertedToManaDamage) == DamageFlags.CanBeConvertedToManaDamage)
            {
                if (ConvertingPhysicalDamageToManaDamage && damageType == DamageType.Physical)
                {
                    CurrentMP -= (long)realDamage;
                    if (CurrentMP < 0)
                    {
                        CurrentMP = 0;
                    }
                    CurrentPhysicalDamageConvertedToManaDamage += dmg;
                    if (MaximumPhysicalDamageConvertedToManaDamage <= CurrentPhysicalDamageConvertedToManaDamage)
                    {
                        RemoveStatus("PhysicalConvertToMana");
                    }
                    realDamage = 0;
                    SpellAnimation(PhysicalConvertToManaAnimation, 100);
                }
                if (ConvertingMagicalDamageToManaDamage && damageType == DamageType.Magical)
                {
                    CurrentMP -= (long)realDamage;
                    if (CurrentMP < 0)
                    {
                        CurrentMP = 0;
                    }
                    CurrentMagicalDamageConvertedToManaDamage += dmg;
                    if (MaximumMagicalDamageConvertedToManaDamage <= CurrentPhysicalDamageConvertedToManaDamage)
                    {
                        RemoveStatus("MagicalConvertToMana");
                    }
                    realDamage = 0;
                    SpellAnimation(MagicalConvertToManaAnimation, 100);
                }
            }

            CurrentHP -= (long)realDamage;

            if (CurrentHP < 0)
            {
                CurrentHP = 0;
            }

            if (attacker != null)
            {
                if (Attackers.ContainsKey(attacker))
                {
                    Attackers[attacker] += realDamage;
                }
                else
                {
                    Attackers.Add(attacker, realDamage);
                }
            }

            double percent = Math.Floor((double)CurrentHP / (double)MaximumHP * 100.0);

            if (percent < 0)
            {
                percent = 0;
            }

            if (percent > 100)
            {
                percent = 100;
            }

            var dot = (flags & DamageFlags.DamageOverTime) == DamageFlags.DamageOverTime;
            var id  = (attacker != null) ? attacker.ID : ID;

            foreach (Character c in Map.Objects)
            {
                if (WithinRange(c, 12) && (c is Player))
                {
                    var packet = new ServerPacket(0x13);
                    packet.WriteUInt32(id);
                    packet.WriteUInt32(ID);
                    packet.WriteByte(dot);
                    packet.WriteByte((byte)percent);
                    packet.WriteUInt32((uint)realDamage);
                    packet.WriteByte((byte)sound);
                    (c as Player).Client.Enqueue(packet);
                }
            }
        }
Example #8
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;
            }
        }
Example #9
0
 public void AddAttacker(BatchRequestAttacker attacker)
 {
     Attackers.Add(attacker);
 }