private static bool ProcessMechArmorStructureRatioForLocation( MechDef mechDef, ChassisLocations location, Dictionary <MechValidationType, List <Text> > errorMessages = null, bool applyChanges = false) { var mechLocationDef = mechDef.GetLocationLoadoutDef(location); var chassisLocationDef = mechDef.Chassis.GetLocationDef(location); var armor = Mathf.Max(mechLocationDef.AssignedArmor, 0); var armorRear = Mathf.Max(mechLocationDef.AssignedRearArmor, 0); var structure = chassisLocationDef.InternalStructure; var ratio = location == ChassisLocations.Head ? 3 : 2; var total = armor + armorRear; var totalMax = ratio * structure; if (total <= totalMax) { return(true); } if (applyChanges) { // Control.mod.Logger.LogDebug($"structure={structure} location={location} totalMax={totalMax}"); // Control.mod.Logger.LogDebug($"before AssignedArmor={mechLocationDef.AssignedArmor} AssignedRearArmor={mechLocationDef.AssignedRearArmor}"); if ((location & ChassisLocations.Torso) != 0) { mechLocationDef.AssignedArmor = PrecisionUtils.RoundUp((totalMax * 2 / 3), 5); mechLocationDef.CurrentArmor = mechLocationDef.AssignedArmor; mechLocationDef.AssignedRearArmor = PrecisionUtils.RoundDown((totalMax * 1 / 3), 5); mechLocationDef.CurrentRearArmor = mechLocationDef.AssignedRearArmor; } else { mechLocationDef.AssignedArmor = totalMax; mechLocationDef.CurrentArmor = mechLocationDef.AssignedArmor; } Control.mod.Logger.LogDebug($"set AssignedArmor={mechLocationDef.AssignedArmor} AssignedRearArmor={mechLocationDef.AssignedRearArmor} on location={location}"); } //Control.mod.Logger.LogDebug($"{Mech.GetAbbreviatedChassisLocation(location)} armor={armor} armorRear={armorRear} structure={structure}"); if (errorMessages != null) { var locationName = Mech.GetLongChassisLocation(location); errorMessages[MechValidationType.InvalidHardpoints].Add(new Text($"ARMOR {locationName}: Armor can only be {ratio} times more than structure.")); } return(false); }
internal static void OnArmorAddOrSubtract(MechLabLocationWidget widget, bool isRearArmor, float direction) { var stepPrecision = ArmorMaximizerFeature.Shared.Settings.StepPrecision.Get() ?? ArmorStructureRatioFeature.ArmorPerStep; var stepSize = ArmorMaximizerFeature.Shared.Settings.StepSize.Get() ?? ArmorStructureRatioFeature.ArmorPerStep; var stepDirection = direction < 0 ? -1 : 1; var current = isRearArmor ? widget.currentRearArmor : widget.currentArmor; var updated = stepDirection > 0 ? PrecisionUtils.RoundUp(current + stepSize, stepPrecision) : PrecisionUtils.RoundDown(current - stepSize, stepPrecision); Control.Logger.Trace?.Log($"HandleArmorUpdate stepDirection={stepDirection} current={current} precision={stepPrecision} isRearArmor={isRearArmor}"); if (stepDirection > 0) { var max = isRearArmor ? widget.maxRearArmor : widget.maxArmor; updated = Mathf.Min(updated, max); var maxTotal = ArmorStructureRatioFeature.GetMaximumArmorPoints(widget.chassisLocationDef); var maxOther = maxTotal - updated; var currentOther = isRearArmor ? widget.currentArmor : widget.currentRearArmor; var updatedOther = Mathf.Min(currentOther, maxOther); var otherNotChanged = PrecisionUtils.Equals(currentOther, updatedOther); if (otherNotChanged || !ArmorLocationLocker.IsLocked(widget.loadout.Location, !isRearArmor)) { widget.SetArmor(isRearArmor, updated); widget.SetArmor(!isRearArmor, updatedOther); } Control.Logger.Trace?.Log($"HandleArmorUpdate updated={updated} maxTotal={maxTotal} maxOther={maxOther} currentOther={currentOther} updatedOther={updatedOther} isRearArmor={updatedOther}"); } else { updated = Mathf.Max(updated, 0); widget.SetArmor(isRearArmor, updated); Control.Logger.Trace?.Log($"HandleArmorUpdate updated={updated}"); } }
private static float RoundBy1(float value) { return(PrecisionUtils.RoundDown(value, 1)); }