private static Operator GetCommonFactor(List <Operator> sumTerms, out List <int> positions, out bool front) { Dictionary <Operator, FactorInfo> factorDictionaryFront = new Dictionary <Operator, FactorInfo>(); Dictionary <Operator, FactorInfo> factorDictionaryBack = new Dictionary <Operator, FactorInfo>(); for (int i = 0; i < sumTerms.Count; i++) { Operator sumTerm = sumTerms[i]; UpdateFactorDictionaryWithOperator(factorDictionaryFront, sumTerm.ProductTerms[0], i); UpdateFactorDictionaryWithOperator(factorDictionaryBack, sumTerm.ProductTerms[sumTerm.ProductTerms.Count - 1], i); } FactorInfo commonFactorInfoFront = GetFactorInfo(factorDictionaryFront); FactorInfo commonFactorInfoBack = GetFactorInfo(factorDictionaryBack); FactorInfo commonFactorInfo; if (commonFactorInfoFront.Positions.Count >= commonFactorInfoBack.Positions.Count) { commonFactorInfo = commonFactorInfoFront; front = true; } else { commonFactorInfo = commonFactorInfoBack; front = false; } positions = commonFactorInfo.Positions; return(commonFactorInfo.GetCommonFactor()); }
private static void UpdateFactorDictionary(Dictionary <Operator, FactorInfo> dictionary, Operator op, Rational exponent, int position) { FactorInfo factorInfo = null; if (dictionary.TryGetValue(op, out factorInfo)) { factorInfo.Exponent = Rational.Min(factorInfo.Exponent, exponent); } else { factorInfo = new FactorInfo(op, exponent); dictionary[op] = factorInfo; } factorInfo.Positions.Add(position); }
D3D11_BLEND GetFactorBlendDescImpl( FactorInfo info, AttrCase channel) { switch (channel) { case AttrCase.Color: return(info._color); case AttrCase.Alpha: return(info._alpha); default: throw OperationTooComplexError(info.Range); } }
private decimal EvalModificator(object modificator, FactorInfo factorInfo, Dictionary <FactorType, List <FactorInfo> > defenderFactors, Hero hero, Creature creature, TextWriter log, decimal i2, decimal r2 = 1) { // %HeroLevel% // %CreatureLevel% // %Immunity% // %CreatureAttack% // %CreatureDefense% // %I2% // %R2% // PARAMS string value = string.Empty; decimal retValue = 0; RoundType round = RoundType.None; // get value from modificator if (modificator is Modificator) { value = ((Modificator)modificator).Value; round = ((Modificator)modificator).RoundType; } else if (modificator is LevelModificator) { LevelModificator levelModificator = (LevelModificator)modificator; string level = string.Empty; if (factorInfo.ParentRef != null) { level = factorInfo.ParentRef.Level; } if (string.IsNullOrEmpty(level) || level == "0") { value = levelModificator.Level_0.Value; } else if (level == "1") { value = levelModificator.Level_1.Value; } else if (level == "2") { value = levelModificator.Level_2.Value; } else if (level == "3") { value = levelModificator.Level_3.Value; } round = levelModificator.RoundType; } log.WriteLine("Evaluating value '{0}' for {1}", value, creature.Name); // replace standard values value = value.Replace("%HeroLevel%", hero.Level.ToString()); value = value.Replace("%CreatureLevel%", creature.Level.ToString()); value = value.Replace("%CreatureAttack%", creature.Attack.ToString()); value = value.Replace("%CreatureDefense%", creature.Defense.ToString()); System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo(); nfi.NumberDecimalSeparator = "."; value = value.Replace("%I2%", i2.ToString(nfi)); value = value.Replace("%R2%", i2.ToString(nfi)); // check immunity if (factorInfo.Parent is CreatureAbility) { string immunity = "0"; CreatureAbility ability = (CreatureAbility)factorInfo.Parent; if (defenderFactors.ContainsKey(FactorType.ImmuneToCreatureAbility)) { List <FactorInfo> immunityFactors = defenderFactors[FactorType.ImmuneToCreatureAbility]; foreach (FactorInfo immunityFactor in immunityFactors) { if (ability.Name == immunityFactor.Factor.CreatureAbility) { immunity = "1"; } } } value = value.Replace("%Immunity%", immunity); } // params if (factorInfo.Parent is CreatureAbility) { CreatureAbility ability = (CreatureAbility)factorInfo.Parent; if (!string.IsNullOrEmpty(ability.Params)) { string [] abilityParams = ability.Params.Split(' '); foreach (string param in abilityParams) { string val = "0"; if (factorInfo.Params != null && factorInfo.Params.ContainsKey(param)) { Param refParam = factorInfo.Params[param]; val = refParam.Value.ToString(); } value = value.Replace("%" + param + "%", val); } } } // evaluate string log.WriteLine("Evaluating direct value '{0}' for {1}", value, creature.Name); DataTable dt = new DataTable(); retValue = System.Convert.ToDecimal(dt.Compute(value, string.Empty)); if (round == RoundType.Down) { retValue = System.Math.Floor(retValue); } else if (round == RoundType.Up) { retValue = System.Math.Ceiling(retValue); } else if (round == RoundType.Normal) { retValue = System.Math.Round(retValue); } log.WriteLine("Counted value '{0}' for {1}", retValue, creature.Name); return(retValue); }
private static Symbol GetCommonFactor(List <Symbol> sumTerms, out List <int> positions) { Dictionary <Symbol, FactorInfo> factorDictionary = new Dictionary <Symbol, FactorInfo>(); for (int i = 0; i < sumTerms.Count; i++) { Symbol sumTerm = sumTerms[i]; foreach (Symbol productTerm in sumTerm.ProductTerms) { Symbol factor = null; Rational exponent = Rational.One; if (productTerm.Exponent.SumTerms.Count == 0) { factor = productTerm.ExponentBase; exponent = productTerm.Exponent.SumConstantTerm; } else { factor = productTerm; } FactorInfo factorInfo = null; if (factorDictionary.TryGetValue(factor, out factorInfo)) { factorInfo.Exponent = Rational.Min(factorInfo.Exponent, exponent); } else { factorInfo = new FactorInfo(factor, exponent); factorDictionary[factor] = factorInfo; } factorInfo.Positions.Add(i); } } FactorInfo commonFactorInfo = null; foreach (KeyValuePair <Symbol, FactorInfo> pair in factorDictionary) { if (commonFactorInfo == null || pair.Value.Positions.Count > commonFactorInfo.Positions.Count) { commonFactorInfo = pair.Value; } else if (commonFactorInfo.Positions.Count == pair.Value.Positions.Count) { bool equal = true; for (int i = 0; i < commonFactorInfo.Positions.Count; i++) { if (!(commonFactorInfo.Positions[i] == pair.Value.Positions[i])) { equal = false; break; } } if (equal) { commonFactorInfo = new FactorInfo(commonFactorInfo.GetCommonFactor() * pair.Value.GetCommonFactor(), Rational.One); for (int i = 0; i < pair.Value.Positions.Count; i++) { commonFactorInfo.Positions.Add(pair.Value.Positions[i]); } } } } positions = commonFactorInfo.Positions; return(commonFactorInfo.GetCommonFactor()); }
D3D11_BLEND GetFactorBlendDescImpl( FactorInfo info, AttrCase channel) { switch (channel) { case AttrCase.Color: return info._color; case AttrCase.Alpha: return info._alpha; default: throw OperationTooComplexError(info.Range); } }