Fix for engineered modules in SLEF imports from Inara.

This commit is contained in:
Alexey Gorb
2021-08-11 11:55:24 +02:00
parent 45508ba2d4
commit 74829a09c0
2 changed files with 32 additions and 24 deletions

View File

@@ -327,26 +327,43 @@ export function setPercent(ship, m, percent) {
ship.clearModifications(m); ship.clearModifications(m);
// Pick given value as multiplier // Pick given value as multiplier
const mult = percent / 100; const mult = percent / 100;
const features = m.blueprint.grades[m.blueprint.grade].features; setQualityCB(m.blueprint, mult, (featureName, value) => ship.setModification(m, featureName, value));
}
/**
* Sets the blueprint quality and fires a callback for each property affected.
* @param {Object} blueprint The ship for which to perform the modifications
* @param {Number} quality The quality to apply - float number 0 to 1.
* @param {Function} cb The Callback to run for each property. Function (featureName, value)
*/
export function setQualityCB(blueprint, quality, cb) {
// Pick given value as multiplier
const features = blueprint.grades[blueprint.grade].features;
for (const featureName in features) { for (const featureName in features) {
let value; let value;
if (Modifications.modifications[featureName].higherbetter) { if (Modifications.modifications[featureName].higherbetter) {
// Higher is better, but is this making it better or worse? // Higher is better, but is this making it better or worse?
if (features[featureName][0] < 0 || (features[featureName][0] === 0 && features[featureName][1] < 0)) { if (features[featureName][0] < 0 || (features[featureName][0] === 0 && features[featureName][1] < 0)) {
value = features[featureName][1] + ((features[featureName][0] - features[featureName][1]) * mult); value = features[featureName][1] + ((features[featureName][0] - features[featureName][1]) * quality);
} else { } else {
value = features[featureName][0] + ((features[featureName][1] - features[featureName][0]) * mult); value = features[featureName][0] + ((features[featureName][1] - features[featureName][0]) * quality);
} }
} else { } else {
// Higher is worse, but is this making it better or worse? // Higher is worse, but is this making it better or worse?
if (features[featureName][0] < 0 || (features[featureName][0] === 0 && features[featureName][1] < 0)) { if (features[featureName][0] < 0 || (features[featureName][0] === 0 && features[featureName][1] < 0)) {
value = features[featureName][0] + ((features[featureName][1] - features[featureName][0]) * mult); value = features[featureName][0] + ((features[featureName][1] - features[featureName][0]) * quality);
} else { } else {
value = features[featureName][1] + ((features[featureName][0] - features[featureName][1]) * mult); value = features[featureName][1] + ((features[featureName][0] - features[featureName][1]) * quality);
} }
} }
_setValue(ship, m, featureName, value); if (Modifications.modifications[featureName].type == 'percentage') {
value = value * 10000;
} else if (Modifications.modifications[featureName].type == 'numeric') {
value = value * 100;
}
cb(featureName, value);
} }
} }

View File

@@ -4,7 +4,7 @@ import { Ships } from 'coriolis-data/dist';
import Module from '../shipyard/Module'; import Module from '../shipyard/Module';
import { Modules } from 'coriolis-data/dist'; import { Modules } from 'coriolis-data/dist';
import { Modifications } from 'coriolis-data/dist'; import { Modifications } from 'coriolis-data/dist';
import { getBlueprint } from './BlueprintFunctions'; import { getBlueprint, setQualityCB } from './BlueprintFunctions';
/** /**
* Obtain a module given its FD Name * Obtain a module given its FD Name
@@ -168,8 +168,6 @@ export function shipFromLoadoutJSON(json) {
const hardpointSlot = json.Modules.find(elem => elem.Slot.toLowerCase() === hardpointName.toLowerCase()); const hardpointSlot = json.Modules.find(elem => elem.Slot.toLowerCase() === hardpointName.toLowerCase());
if (!hardpointSlot) { if (!hardpointSlot) {
// This can happen with old imports that don't contain new hardpoints // This can happen with old imports that don't contain new hardpoints
} else if (!hardpointSlot) {
// No module
} else { } else {
hardpoint = _moduleFromFdName(hardpointSlot.Item); hardpoint = _moduleFromFdName(hardpointSlot.Item);
ship.use(ship.hardpoints[hardpointArrayNum], hardpoint, true); ship.use(ship.hardpoints[hardpointArrayNum], hardpoint, true);
@@ -303,20 +301,13 @@ function _addModifications(module, modifiers, quality, blueprint, grade, special
} }
} }
} else if (quality) { } else if (quality) {
if (module.blueprint.grades) { setQualityCB(module.blueprint, quality, (featureName, value) => {
const features = module.blueprint.grades[Number(grade)].features; // The resistance values of bulkheads need an extra adjustment.
Object.keys(features).map(featureKey => { if (featureName === 'kinres' || featureName === 'thermres' || featureName === 'causres' || featureName === 'explres') {
/* let baseValue = module.get(featureName, false);
Here we compute the value to use for this feature based on the range and quality. value = (1 - baseValue) * value;
We do this by finding the difference between the low and high ends of the range. }
This gives us the maximum increase at 100% quality. Then we multiply this number by module.setModValue(featureName, value, false);
the quality to determine the actual increase. Lastly we add the actual increase to });
the low end of the range back in to determine the final value.
Value = ((High End of Range - Low End of Range) * Quality) + Low End of Range
*/
let value = (((features[featureKey][1] * 100) - (features[featureKey][0] * 100) * Number(quality)) + (features[featureKey][0] * 100)) * 100;
module.setModValue(featureKey, value, true);
});
}
} }
} }