private void UpdateStatsByModifiers(Descriptor descriptor) { if (descriptor != null && descriptor.GetType() == typeof (Race) && Classes.Count > 0) { var modifiers = new Dictionary<StatType, object[]>(); float totalPercents = 0; foreach (Class c in Classes) { foreach (Modifier modifier in c.Modifiers) { if (!modifiers.ContainsKey(modifier.Stat)) modifiers.Add(modifier.Stat, new object[] {new Modifier(), 0}); var m = (Modifier) modifiers[modifier.Stat][0]; var total = (int) modifiers[modifier.Stat][1]; m.PercentValue += modifier.PercentValue; m.Value += modifier.Value; modifiers[modifier.Stat][1] = ++total; } } foreach (var modifier in modifiers) { var m = (Modifier) modifier.Value[0]; var total = (short) modifier.Value[1]; m.PercentValue /= total; m.Value /= total; totalPercents += m.PercentValue; } if (totalPercents < 100) totalPercents = 100; int pointWeight = _random.Next(1, 101); int pointWeightTotal = 0; int numStats = 0; for (int i = 0; i < _numPointWeights.Length; ++i) { pointWeightTotal += _numPointWeights[i]; if (pointWeight <= pointWeightTotal) { if (_numPointWeights[i] - 6 < 0) { int spread = _numPointWeights[i]; _numPointWeights[i] = 0; for (int j = 0; j < spread; ++j) { int index = j%_numPointWeights.Length; if (i != index) ++_numPointWeights[index]; else ++spread; } } else { for (int j = 0; j < _numPointWeights.Length; ++j) { if (i != j) _numPointWeights[j] += 2; else _numPointWeights[j] -= 6; } } numStats = i + 2; break; } } for (int i = 0; i < numStats; ++i) { int weight = _random.Next(1, (int) Math.Round(totalPercents) + 1); float weightTotal = 0; foreach (var modifier in modifiers) { var m = (Modifier) modifier.Value[0]; weightTotal += m.PercentValue; if (weight <= weightTotal) { int modifierValue = m.Value; if (modifierValue == 0) modifierValue = 1; BaseStats[modifier.Key] += modifierValue; EffectiveStats[modifier.Key] += modifierValue; break; } } } } }
private void UpdateStats(Descriptor oldDescriptor, Descriptor newDescriptor) { if ((oldDescriptor != null && oldDescriptor.GetType() == typeof (Race)) || (newDescriptor != null && newDescriptor.GetType() == typeof (Race))) { var oldRace = (Race) oldDescriptor; var newRace = (Race) newDescriptor; if (oldDescriptor != null) { BaseStats -= oldRace.Stats; EffectiveStats -= oldRace.Stats; } else { if (BaseStats != 0 && EffectiveStats != 0) { BaseStats -= newRace.Stats; EffectiveStats -= newRace.Stats; } } if (newDescriptor != null) { BaseStats += newRace.Stats; EffectiveStats += newRace.Stats; } } }