public override void ApplyGuarantee(Scheme scheme, Random rng) { if (MinimumWeaponCount != null) { _minimumWeaponCount = MinimumWeaponCount.GenerateValue(rng); } if (MaximumWeaponCount != null) { _maximumWeaponCount = MaximumWeaponCount.GenerateValue(rng); } if (MinimumSettingValue != null) { _minimumSettingValue = MinimumSettingValue.GenerateValue(rng); } if (MaximumSettingValue != null) { _maximumSettingValue = MaximumSettingValue.GenerateValue(rng); } if ((!_minimumWeaponCount.HasValue && !_maximumWeaponCount.HasValue) || (!_minimumSettingValue.HasValue && !_maximumSettingValue.HasValue)) { return; } List <Weapon> weaponsMatchingGuarantee = new List <Weapon>(); List <Weapon> weaponsNotMatchingGuarantee = new List <Weapon>(); foreach (Weapon weapon in scheme.Weapons) { if (CategoryInclusionFilter.HasValue && !SchemeTypes.HasWeaponCategoryFlags(weapon.WeaponType, CategoryInclusionFilter.Value, InclusionMatchType)) { continue; } if (CategoryExclusionFilter.HasValue && SchemeTypes.HasWeaponCategoryFlags(weapon.WeaponType, CategoryExclusionFilter.Value, ExclusionMatchType)) { continue; } Setting setting = weapon.Access(WeaponSetting); if ((!_minimumSettingValue.HasValue || setting.Value >= _minimumSettingValue.Value) && (!_maximumSettingValue.HasValue || setting.Value <= _maximumSettingValue.Value)) { weaponsMatchingGuarantee.Add(weapon); } else { weaponsNotMatchingGuarantee.Add(weapon); } } //In general, prefer changing weapons whose value generators could have generated the desired value. if (_minimumWeaponCount.HasValue && weaponsMatchingGuarantee.Count < _minimumWeaponCount.Value) { HandleNotEnoughWeaponsMatchingGuarantee(weaponsNotMatchingGuarantee, _minimumWeaponCount.Value - weaponsMatchingGuarantee.Count, rng); } else if (_maximumWeaponCount.HasValue && weaponsMatchingGuarantee.Count > _maximumWeaponCount.Value) { int weaponCountExcess = weaponsMatchingGuarantee.Count - _maximumWeaponCount.Value; if (!MaximumWeaponCountForcedValue.HasValue) { HandleTooManyWeaponsMeetingGuarantee(weaponsMatchingGuarantee, weaponCountExcess, rng); } else { HandleTooManyWeaponsMeetingGuaranteeWithForcedValue(weaponsMatchingGuarantee, weaponCountExcess, rng); } } }