Fix issues with losing precision due to using decimal modification values. Validate modification information

This commit is contained in:
Cmdr McDonald
2016-11-11 11:15:56 +00:00
parent 782603727a
commit 606eabfec7
4 changed files with 43 additions and 29 deletions

View File

@@ -49,7 +49,7 @@ export default class InternalSlot extends Slot {
{ m.rangeLS ? <div className={'l'}>{translate('range')}: {m.rangeLS}{u.Ls}</div> : null }
{ m.rangeLS === null ? <div className={'l'}>{u.Ls}</div> : null }
{ m.rangeRating ? <div className={'l'}>{translate('range')}: {m.rangeRating}</div> : null }
{ m.getHullReinforcement() ? <div className={'l'}>+{formats.int(m.getHullReinforcement() + ship.baseArmour * m.getModValue('hullboost'))} <u className='cap'>{translate('armour')}</u></div> : null }
{ m.getHullReinforcement() ? <div className={'l'}>+{formats.int(m.getHullReinforcement() + ship.baseArmour * m.getModValue('hullboost') / 10000)} <u className='cap'>{translate('armour')}</u></div> : null }
{ m.passengers ? <div className={'l'}>{translate('passengers')}: {m.passengers}</div> : null }
{ m && validMods.length > 0 ? <div className='r' ><button onClick={this._toggleModifications.bind(this)} onContextMenu={stopCtxPropagation} onMouseOver={termtip.bind(null, 'modifications')} onMouseOut={tooltip.bind(null, null)}><ListModifications /></button></div> : null }

View File

@@ -24,7 +24,7 @@ export default class ModificationsMenu extends TranslatedComponent {
constructor(props, context) {
super(props);
this.state = {};
this.state.value = this.props.m.getModValue(this.props.name) * 100 || 0;
this.state.value = this.props.m.getModValue(this.props.name) / 100 || 0;
}
/**
@@ -32,14 +32,14 @@ export default class ModificationsMenu extends TranslatedComponent {
* @param {Number} value The value to set
*/
_updateValue(value) {
let scaledValue = Math.floor(Number(value) * 100) / 10000;
let scaledValue = Math.floor(Number(value) * 100);
// Limit to +1000% / -100%
if (scaledValue > 10) {
scaledValue = 10;
if (scaledValue > 10000) {
scaledValue = 10000;
value = 1000;
}
if (scaledValue < -1) {
scaledValue = -1;
if (scaledValue < -1000) {
scaledValue = -1000;
value = -100;
}
let m = this.props.m;

View File

@@ -28,16 +28,16 @@ export default class Module {
/**
* Get a value for a given modification
* @param {Number} name The name of the modification
* @return {Number} The value of the modification, as a decimal value where 1 is 100%
* @return {Number} The value of the modification, as an integer value scaled so that 1.23% == 123
*/
getModValue(name) {
return this.mods && this.mods[name] ? this.mods[name] / 10000 : null;
return this.mods && this.mods[name] ? this.mods[name] : null;
}
/**
* Set a value for a given modification ID
* @param {Number} name The name of the modification
* @param {Number} value The value of the modification, as a decimal value where 1 is 100%
* @param {Number} value The value of the modification, as an integer scaled so that -2.34% == -234
*/
setModValue(name, value) {
if (!this.mods) {
@@ -47,8 +47,8 @@ export default class Module {
if (value == null || value == 0) {
delete this.mods[name];
} else {
// Store value with 2dp
this.mods[name] = Math.round(value * 10000);
// Round just to be sure
this.mods[name] = Math.round(value);
}
}
@@ -61,7 +61,7 @@ export default class Module {
_getModifiedValue(name, additive) {
let result = this[name] || (additive ? 0 : null); // Additive NULL === 0
if (result != null) {
const modValue = this.getModValue(name);
const modValue = this.getModValue(name) / 10000;
if (modValue) {
if (additive) {
result = result + modValue;
@@ -299,7 +299,7 @@ export default class Module {
if (this['minmass']) {
result = this['minmass'];
if (result) {
let mult = this.getModValue('optmass');
let mult = this.getModValue('optmass') / 10000;
if (mult) { result = result * (1 + mult); }
}
}
@@ -324,7 +324,7 @@ export default class Module {
if (this['maxmass']) {
result = this['maxmass'];
if (result) {
let mult = this.getModValue('optmass');
let mult = this.getModValue('optmass') / 10000;
if (mult) { result = result * (1 + mult); }
}
}
@@ -341,7 +341,7 @@ export default class Module {
if (this['minmul']) {
result = this['minmul'];
if (result) {
let mult = this.getModValue('optmul');
let mult = this.getModValue('optmul') / 10000;
if (mult) { result = result * (1 + mult); }
}
}
@@ -366,7 +366,7 @@ export default class Module {
if (this['maxmul']) {
result = this['maxmul'];
if (result) {
let mult = this.getModValue('optmul');
let mult = this.getModValue('optmul') / 10000;
if (mult) { result = result * (1 + mult); }
}
}

View File

@@ -195,7 +195,7 @@ export default class Ship {
if (this.shield > 0) {
const sgSlot = this.findInternalByGroup('sg');
if (sgSlot != null) {
let brokenRegenRate = 1 + sgSlot.m.getModValue('brokenregen');
let brokenRegenRate = 1 + sgSlot.m.getModValue('brokenregen') / 10000;
// 50% of shield strength / recovery recharge rate + 15 second delay before recharge starts
return ((this.shield / 2) / (sgSlot.m.recover * brokenRegenRate)) + 15;
}
@@ -213,7 +213,7 @@ export default class Ship {
if (this.shield > 0) {
const sgSlot = this.findInternalByGroup('sg');
if (sgSlot != null) {
let regenRate = 1 + sgSlot.m.getModValue('regen');
let regenRate = 1 + sgSlot.m.getModValue('regen') / 10000;
// 50% -> 100% recharge time, Bi-Weave shields charge at 1.8 MJ/s
return (this.shield / 2) / ((sgSlot.m.grp == 'bsg' ? 1.8 : 1) * regenRate);
}
@@ -399,7 +399,7 @@ export default class Ship {
* Set a modification value
* @param {Object} m The module to change
* @param {Object} name The name of the modification to change
* @param {Number} value The new value of the modification
* @param {Number} value The new value of the modification. The value of the modification is scaled to provide two decimal places of precision in an integer. For example 1.23% is stored as 123
*/
setModification(m, name, value) {
// Handle special cases
@@ -1136,7 +1136,7 @@ export default class Ship {
if (slot.m && slot.m.grp == 'hr') {
armour += slot.m.getHullReinforcement();
// Hull boost for HRPs is applied against the ship's base armour
armour += this.baseArmour * slot.m.getModValue('hullboost');
armour += this.baseArmour * slot.m.getModValue('hullboost') / 10000;
hullExplRes *= (1 - slot.m.getExplosiveResistance());
hullKinRes *= (1 - slot.m.getKineticResistance());
@@ -1220,7 +1220,7 @@ export default class Ship {
let bulkheadMods = new Array();
if (this.bulkheads.m && this.bulkheads.m.mods) {
for (let modKey in this.bulkheads.m.mods) {
bulkheadMods.push(Modifications.modifiers.indexOf(modKey) + ':' + Math.round(this.bulkheads.m.getModValue(modKey) * 10000));
bulkheadMods.push(Modifications.modifiers.indexOf(modKey) + ':' + this.bulkheads.m.getModValue(modKey));
}
}
allMods.push(bulkheadMods.join(';'));
@@ -1229,7 +1229,7 @@ export default class Ship {
let slotMods = new Array();
if (slot.m && slot.m.mods) {
for (let modKey in slot.m.mods) {
slotMods.push(Modifications.modifiers.indexOf(modKey) + ':' + Math.round(slot.m.getModValue(modKey) * 10000));
slotMods.push(Modifications.modifiers.indexOf(modKey) + ':' + slot.m.getModValue(modKey));
}
}
allMods.push(slotMods.join(';'));
@@ -1238,7 +1238,7 @@ export default class Ship {
let slotMods = new Array();
if (slot.m && slot.m.mods) {
for (let modKey in slot.m.mods) {
slotMods.push(Modifications.modifiers.indexOf(modKey) + ':' + Math.round(slot.m.getModValue(modKey) * 10000));
slotMods.push(Modifications.modifiers.indexOf(modKey) + ':' + slot.m.getModValue(modKey));
}
}
allMods.push(slotMods.join(';'));
@@ -1247,7 +1247,7 @@ export default class Ship {
let slotMods = new Array();
if (slot.m && slot.m.mods) {
for (let modKey in slot.m.mods) {
slotMods.push(Modifications.modifiers.indexOf(modKey) + ':' + Math.round(slot.m.getModValue(modKey) * 10000));
slotMods.push(Modifications.modifiers.indexOf(modKey) + ':' + slot.m.getModValue(modKey));
}
}
allMods.push(slotMods.join(';'));
@@ -1295,7 +1295,10 @@ export default class Ship {
let bulkheadMods = new Array();
if (this.bulkheads.m && this.bulkheads.m.mods) {
for (let modKey in this.bulkheads.m.mods) {
bulkheadMods.push({ id: Modifications.modifiers.indexOf(modKey), value: Math.round(this.bulkheads.m.getModValue(modKey) * 10000) });
// Filter out invalid modifications
if (Modifications.validity['bh'] && Modifications.validity['bh'].indexOf(modKey) != -1) {
bulkheadMods.push({ id: Modifications.modifiers.indexOf(modKey), value: this.bulkheads.m.getModValue(modKey) });
}
}
}
slots.push(bulkheadMods);
@@ -1304,7 +1307,10 @@ export default class Ship {
let slotMods = new Array();
if (slot.m && slot.m.mods) {
for (let modKey in slot.m.mods) {
slotMods.push({ id: Modifications.modifiers.indexOf(modKey), value: Math.round(slot.m.getModValue(modKey) * 10000) });
// Filter out invalid modifications
if (Modifications.validity[slot.m.grp] && Modifications.validity[slot.m.grp].indexOf(modKey) != -1) {
slotMods.push({ id: Modifications.modifiers.indexOf(modKey), value: slot.m.getModValue(modKey) });
}
}
}
slots.push(slotMods);
@@ -1314,7 +1320,10 @@ export default class Ship {
let slotMods = new Array();
if (slot.m && slot.m.mods) {
for (let modKey in slot.m.mods) {
slotMods.push({ id: Modifications.modifiers.indexOf(modKey), value: Math.round(slot.m.getModValue(modKey) * 10000) });
// Filter out invalid modifications
if (Modifications.validity[slot.m.grp] && Modifications.validity[slot.m.grp].indexOf(modKey) != -1) {
slotMods.push({ id: Modifications.modifiers.indexOf(modKey), value: slot.m.getModValue(modKey) });
}
}
}
slots.push(slotMods);
@@ -1324,7 +1333,10 @@ export default class Ship {
let slotMods = new Array();
if (slot.m && slot.m.mods) {
for (let modKey in slot.m.mods) {
slotMods.push({ id: Modifications.modifiers.indexOf(modKey), value: Math.round(slot.m.getModValue(modKey) * 10000) });
// Filter out invalid modifications
if (Modifications.validity[slot.m.grp] && Modifications.validity[slot.m.grp].indexOf(modKey) != -1) {
slotMods.push({ id: Modifications.modifiers.indexOf(modKey), value: slot.m.getModValue(modKey) });
}
}
}
slots.push(slotMods);
@@ -1350,6 +1362,7 @@ export default class Ship {
for (let slotMod of slot) {
buffer.writeInt8(slotMod.id, curpos++);
buffer.writeInt32LE(slotMod.value, curpos);
// console.log('ENCODE Slot ' + i + ': ' + Modifications.modifiers[slotMod.id] + ' = ' + slotMod.value);
curpos += 4;
}
buffer.writeInt8(-1, curpos++);
@@ -1382,6 +1395,7 @@ export default class Ship {
while (modificationId != -1) {
let modificationValue = buffer.readInt32LE(curpos);
curpos += 4;
// console.log('DECODE Slot ' + slot + ': ' + Modifications.modifiers[modificationId] + ' = ' + modificationValue);
modifications[Modifications.modifiers[modificationId]] = modificationValue;
modificationId = buffer.readInt8(curpos++);
}