/// <summary> /// Parses all effects of a country into a <see cref="CountryModifierBuilder"/>. /// </summary> /// <param name="country">The country to parse effects for. Its buildings, researches, events and their effects must be included.</param> /// <param name="context">The database to use.</param> /// <param name="globals">The <see cref="GlobalValue"/> to use.</param> /// <param name="Parsers">The collection of parsers to use.</param> /// <param name="doApplyEvent">If random events should be applied to the modifier.</param> /// <param name="doApplyPermanent">If effects that have permanenet effects should be applied.</param> /// <returns>The builder containing the modifiers for the country</returns> public static CountryModifierBuilder ParseAllEffectForCountry(this Country country, UnderSeaDatabaseContext context, GlobalValue globals, ModifierParserContainer Parsers, bool doApplyEvent, bool doApplyPermanent) { if (country == null) { throw new ArgumentNullException(nameof(country)); } // Set up builder var builder = new CountryModifierBuilder { BarrackSpace = globals.StartingBarrackSpace, Population = globals.StartingPopulation }; // First handle events if (doApplyEvent && country.CurrentEvent != null) { foreach (var e in country.CurrentEvent.Effects) { if (!Parsers.TryParse(e.Effect, country, context, builder, doApplyPermanent)) { Debug.WriteLine("Event effect with name {0} could not be handled by the provided parsers.", e.Effect.Name); } } } // Then regular effects var effectparents = country.Buildings.Select(b => new { count = b.Count, effects = b.Building.Effects.Select(e => e.Effect) }).Concat(country.Researches.Select(r => new { count = r.Count, effects = r.Research.Effects.Select(e => e.Effect) })).ToList(); foreach (var effectParent in effectparents) { for (int iii = 0; iii < effectParent.count; iii++) { foreach (var e in effectParent.effects) { if (!Parsers.TryParse(e, country, context, builder, doApplyPermanent)) { Debug.WriteLine("Effect with name {0} could not be handled by the provided parsers.", e.Name); } } } } return(builder); }
/// <summary> /// Calculate attack or defense power, based on the units in a command and their modifiers. /// Attack power is modified randomly by 5%. /// </summary> /// <param name="command">The command to get the combat power of.</param> /// <param name="globals">The global values.</param> /// <param name="doGetAttack">If attack or defense power should be calculated.</param> /// <param name="builder">The builder that contains the stat modifiers.</param> /// <returns>The combat power of the command.</returns> protected (double TotalPower, double Modifiers, double BasePower) GetCurrentUnitPower(Command command, GlobalValue globals, bool doGetAttack, CountryModifierBuilder builder) { if (doGetAttack) { var totalModifier = builder.AttackModifier * ((rng.NextDouble() * globals.RandomAttackModifier) + 1 - (globals.RandomAttackModifier / 2)); var basePower = command.Divisions.Sum(d => d.Count * (d.Unit.AttackPower + builder.AttackIncrease)); return(basePower * totalModifier, totalModifier, basePower); } else { var basePower = command.Divisions.Sum(d => d.Count * d.Unit.DefensePower); return(basePower * builder.DefenseModifier, builder.DefenseModifier, basePower); } }