Allow to set an absolute value for a module

This commit is contained in:
Felix Linker
2018-09-15 00:31:00 +02:00
parent 8c4957c21e
commit c4186faa4a
2 changed files with 169 additions and 96 deletions

View File

@@ -135,7 +135,7 @@ export default class Module {
* @param {Number} modified Whether to return the raw or modified value
* @return {Number} The value queried
*/
_getValue(name, modified) {
get(name, modified = true) {
if (modified) {
return this._getModifiedValue(name);
} else {
@@ -143,6 +143,85 @@ export default class Module {
}
}
/**
* Sets mod values such that the overall result for the given stat equals value
* @param {String} name The name of the modification
* @param {Number} value The value to effectively set
* @param {Boolean} valueIsWithSpecial True when value includes an special
* effects
*/
set(name, value, valueIsWithSpecial) {
const modification = Modifications.modifications[name];
if (!modification || isNaN(value)) {
// TODO: throw?
return;
}
let baseValue = this[name];
let modValue = 0;
if (modification.method === 'overwrite') {
modValue = value;
} else if (modification.method === 'additive') {
// additive modifications can be given without a base value
if (!baseValue) {
baseValue = 0;
}
modValue = value - baseValue;
if (this.grp === 'hr' &&
(name === 'kinres' || name === 'thermres' || name === 'explres')) {
modValue = modValue / (1 - baseValue);
}
} else if (name === 'shieldboost' || name === 'hullboost') {
modValue = (1 + value) / (1 + baseValue) - 1;
} else { // multiplicative
modValue = value / baseValue - 1;
}
if (modification.type === 'percentage') {
modValue = modValue * 10000;
} else if (modification.type === 'numeric' && name !== 'burst' &&
name !== 'burstrof') {
modValue = modValue * 100;
}
this.setModValue(name, modValue, valueIsWithSpecial);
}
/**
* Returns a value for a given modification in pretty format, i.e. percentages
* are returned as 90 not as 0.9.
* @param {String} name Name of the modification to get the value for
* @param {Boolean} [modified = true] If set to false, the raw value of the
* raw value of the stat is returned
* @param {Number} [places = 2] Number of decimal places to round
* @return {Number} Value for given stat
*/
getPretty(name, modified = true, places = 2) {
const formatingOptions = STATS_FORMATING[name];
let val = this.get(name, modified) || 0;
if (formatingOptions && formatingOptions.format.startsWith('pct')) {
return 100 * val;
}
// Round to two decimal places
let precisionMult = 10 ** places;
return Math.round(val * precisionMult) / precisionMult;
}
/**
* Same as {@see Module#set} but values expects value that are percentages to
* come in format 90 as opposed to 0.9.
* @param {String} name The name of the modification
* @param {Number} value The value to effectively set
* @param {Boolean} valueIsWithSpecial True when value includes an special
*/
setPretty(name, value, valueIsWithSpecial) {
const formatingOptions = STATS_FORMATING[name];
if (formatingOptions && formatingOptions.format.startsWith('pct')) {
value = value / 100;
}
this.set(name, value, valueIsWithSpecial);
}
/**
* Helper to obtain a modified value using standard multipliers
* @param {String} name the name of the modifier to obtain
@@ -164,34 +243,34 @@ export default class Module {
modValue = this.getModValue(name);
}
if (modValue) {
if (!result && modification.method === 'additive') {
// If the modification is additive and no value is given by default we
// start at zero
result = 0;
}
if (result !== undefined) {
if (modification.method === 'additive') {
// Resistance modding for hull reinforcement packages has additional
// diminishing returns implemented. The mod value gets lowered by
// the amount of base resistance the hrp has.
if (this.grp === 'hr' &&
(name === 'kinres' || name === 'thermres' || name === 'explres')) {
modValue = modValue * (1 - result);
}
result = result + modValue;
} else if (modification.method === 'overwrite') {
result = modValue;
} else if (name === 'shieldboost' || name === 'hullboost') {
result = (1 + result) * (1 + modValue) - 1;
} else {
result = result * (1 + modValue);
if (!result && modification.method === 'additive') {
// If the modification is additive and no value is given by default we
// start at zero
result = 0;
}
} else if (name === 'burst' || name === 'burstrof') {
// Burst and burst rate of fire are special, as it can not exist but
// have a modification
result = modValue / 100;
}
if (result !== undefined) {
if (modification.method === 'additive') {
// Resistance modding for hull reinforcement packages has additional
// diminishing returns implemented. The mod value gets lowered by
// the amount of base resistance the hrp has.
if (this.grp === 'hr' &&
(name === 'kinres' || name === 'thermres' || name === 'explres')) {
modValue = modValue * (1 - result);
}
result = result + modValue;
} else if (modification.method === 'overwrite') {
result = modValue;
} else if (name === 'shieldboost' || name === 'hullboost') {
result = (1 + result) * (1 + modValue) - 1;
} else {
result = result * (1 + modValue);
}
} else if (name === 'burst' || name === 'burstrof') {
// Burst and burst rate of fire are special, as it can not exist but
// have a modification
result = modValue / 100;
}
}
}
@@ -266,7 +345,7 @@ export default class Module {
* @return {Number} the power generation of this module
*/
getPowerGeneration(modified = true) {
return this._getValue('pgen', modified);
return this.get('pgen', modified);
}
/**
@@ -275,7 +354,7 @@ export default class Module {
* @return {Number} the power usage of this module
*/
getPowerUsage(modified = true) {
return this._getValue('power', modified);
return this.get('power', modified);
}
/**
@@ -284,7 +363,7 @@ export default class Module {
* @return {Number} the integrity of this module
*/
getIntegrity(modified = true) {
return this._getValue('integrity', modified);
return this.get('integrity', modified);
}
/**
@@ -293,7 +372,7 @@ export default class Module {
* @return {Number} the mass of this module
*/
getMass(modified = true) {
return this._getValue('mass', modified);
return this.get('mass', modified);
}
/**
@@ -302,7 +381,7 @@ export default class Module {
* @return {Number} the thermal efficiency of this module
*/
getThermalEfficiency(modified = true) {
return this._getValue('eff', modified);
return this.get('eff', modified);
}
/**
@@ -311,7 +390,7 @@ export default class Module {
* @return {Number} the maximum fuel per jump of this module
*/
getMaxFuelPerJump(modified = true) {
return this._getValue('maxfuel', modified);
return this.get('maxfuel', modified);
}
/**
@@ -320,7 +399,7 @@ export default class Module {
* @return {Number} the systems capacity of this module
*/
getSystemsCapacity(modified = true) {
return this._getValue('syscap', modified);
return this.get('syscap', modified);
}
/**
@@ -329,7 +408,7 @@ export default class Module {
* @return {Number} the engines capacity of this module
*/
getEnginesCapacity(modified = true) {
return this._getValue('engcap', modified);
return this.get('engcap', modified);
}
/**
@@ -338,7 +417,7 @@ export default class Module {
* @return {Number} the weapons capacity of this module
*/
getWeaponsCapacity(modified = true) {
return this._getValue('wepcap', modified);
return this.get('wepcap', modified);
}
/**
@@ -347,7 +426,7 @@ export default class Module {
* @return {Number} the systems recharge rate of this module
*/
getSystemsRechargeRate(modified = true) {
return this._getValue('sysrate', modified);
return this.get('sysrate', modified);
}
/**
@@ -356,7 +435,7 @@ export default class Module {
* @return {Number} the engines recharge rate of this module
*/
getEnginesRechargeRate(modified = true) {
return this._getValue('engrate', modified);
return this.get('engrate', modified);
}
/**
@@ -365,7 +444,7 @@ export default class Module {
* @return {Number} the weapons recharge rate of this module
*/
getWeaponsRechargeRate(modified = true) {
return this._getValue('weprate', modified);
return this.get('weprate', modified);
}
/**
@@ -374,7 +453,7 @@ export default class Module {
* @return {Number} the kinetic resistance of this module
*/
getKineticResistance(modified = true) {
return this._getValue('kinres', modified);
return this.get('kinres', modified);
}
/**
@@ -383,7 +462,7 @@ export default class Module {
* @return {Number} the thermal resistance of this module
*/
getThermalResistance(modified = true) {
return this._getValue('thermres', modified);
return this.get('thermres', modified);
}
/**
@@ -392,7 +471,7 @@ export default class Module {
* @return {Number} the explosive resistance of this module
*/
getExplosiveResistance(modified = true) {
return this._getValue('explres', modified);
return this.get('explres', modified);
}
/**
@@ -401,7 +480,7 @@ export default class Module {
* @return {Number} the caustic resistance of this module
*/
getCausticResistance(modified = true) {
return this._getValue('causres', modified);
return this.get('causres', modified);
}
/**
@@ -410,7 +489,7 @@ export default class Module {
* @return {Number} the regeneration rate of this module
*/
getRegenerationRate(modified = true) {
return this._getValue('regen', modified);
return this.get('regen', modified);
}
/**
@@ -419,7 +498,7 @@ export default class Module {
* @return {Number} the broken regeneration rate of this module
*/
getBrokenRegenerationRate(modified = true) {
return this._getValue('brokenregen', modified);
return this.get('brokenregen', modified);
}
/**
@@ -428,7 +507,7 @@ export default class Module {
* @return {Number} the range rate of this module
*/
getRange(modified = true) {
return this._getValue('range', modified);
return this.get('range', modified);
}
/**
@@ -439,7 +518,7 @@ export default class Module {
getFalloff(modified = true) {
if (!modified) {
const range = this.getRange(false);
const falloff = this._getValue('falloff', false);
const falloff = this.get('falloff', false);
return (falloff > range ? range : falloff);
}
@@ -465,7 +544,7 @@ export default class Module {
* @return {Number} the range of this module
*/
getRangeT(modified = true) {
return this._getValue('ranget', modified);
return this.get('ranget', modified);
}
/**
@@ -474,7 +553,7 @@ export default class Module {
* @return {Number} the scan time of this module
*/
getScanTime(modified = true) {
return this._getValue('scantime', modified);
return this.get('scantime', modified);
}
/**
@@ -483,7 +562,7 @@ export default class Module {
* @return {Number} the capture arc of this module
*/
getCaptureArc(modified = true) {
return this._getValue('arc', modified);
return this.get('arc', modified);
}
/**
@@ -492,7 +571,7 @@ export default class Module {
* @return {Number} the hull reinforcement of this module
*/
getHullReinforcement(modified = true) {
return this._getValue('hullreinforcement', modified);
return this.get('hullreinforcement', modified);
}
/**
@@ -501,7 +580,7 @@ export default class Module {
* @return {Number} the protection of this module
*/
getProtection(modified = true) {
return this._getValue('protection', modified);
return this.get('protection', modified);
}
/**
@@ -510,7 +589,7 @@ export default class Module {
* @return {Number} the delay of this module
*/
getDelay(modified = true) {
return this._getValue('delay', modified);
return this.get('delay', modified);
}
/**
@@ -519,7 +598,7 @@ export default class Module {
* @return {Number} the duration of this module
*/
getDuration(modified = true) {
return this._getValue('duration', modified);
return this.get('duration', modified);
}
/**
@@ -528,7 +607,7 @@ export default class Module {
* @return {Number} the shield boost of this module
*/
getShieldBoost(modified = true) {
return this._getValue('shieldboost', modified);
return this.get('shieldboost', modified);
}
/**
@@ -555,7 +634,7 @@ export default class Module {
* @return {Number} the optimum mass of this module
*/
getOptMass(modified = true) {
return this._getValue('optmass', modified);
return this.get('optmass', modified);
}
/**
@@ -646,7 +725,7 @@ export default class Module {
* @return {Number} the damage of this module
*/
getDamage(modified = true) {
return this._getValue('damage', modified);
return this.get('damage', modified);
}
/**
@@ -655,7 +734,7 @@ export default class Module {
* @return {Number} the distributor draw of this module
*/
getDistDraw(modified = true) {
return this._getValue('distdraw', modified);
return this.get('distdraw', modified);
}
/**
@@ -664,7 +743,7 @@ export default class Module {
* @return {Number} the thermal load of this module
*/
getThermalLoad(modified = true) {
return this._getValue('thermload', modified);
return this.get('thermload', modified);
}
/**
@@ -673,7 +752,7 @@ export default class Module {
* @return {Number} the rounds per shot of this module
*/
getRoundsPerShot(modified = true) {
return this._getValue('roundspershot', modified);
return this.get('roundspershot', modified);
}
/**
@@ -755,7 +834,7 @@ export default class Module {
*/
getClip(modified = true) {
// Clip size is always rounded up
let result = this._getValue('clip', modified);
let result = this.get('clip', modified);
if (result) { result = Math.ceil(result); }
return result;
}
@@ -766,7 +845,7 @@ export default class Module {
* @return {Number} the ammo size of this module
*/
getAmmo(modified = true) {
return this._getValue('ammo', modified);
return this.get('ammo', modified);
}
/**
@@ -775,7 +854,7 @@ export default class Module {
* @return {Number} the reload time of this module
*/
getReload(modified = true) {
return this._getValue('reload', modified);
return this.get('reload', modified);
}
/**
@@ -784,7 +863,7 @@ export default class Module {
* @return {Number} the burst size of this module
*/
getBurst(modified = true) {
return this._getValue('burst', modified);
return this.get('burst', modified);
}
/**
@@ -793,7 +872,7 @@ export default class Module {
* @return {Number} the burst rate of fire of this module
*/
getBurstRoF(modified = true) {
return this._getValue('burstrof', modified);
return this.get('burstrof', modified);
}
/**
@@ -808,7 +887,7 @@ export default class Module {
getRoF(modified = true) {
const burst = this.getBurst(modified) || 1;
const burstRoF = this.getBurstRoF(modified) || 1;
const intRoF = this._getValue('rof', modified);
const intRoF = this.get('rof', modified);
return burst / (((burst - 1) / burstRoF) + 1 / intRoF);
}
@@ -819,7 +898,7 @@ export default class Module {
* @return {Number} the facing limit for this module
*/
getFacingLimit(modified = true) {
return this._getValue('facinglimit', modified);
return this.get('facinglimit', modified);
}
/**
@@ -828,7 +907,7 @@ export default class Module {
* @return {Number} the hull boost for this module
*/
getHullBoost(modified = true) {
return this._getValue('hullboost', modified);
return this.get('hullboost', modified);
}
/**
@@ -837,7 +916,7 @@ export default class Module {
* @return {Number} the shield reinforcement for this module
*/
getShieldReinforcement(modified = true) {
return this._getValue('shieldreinforcement', modified);
return this.get('shieldreinforcement', modified);
}
/**
@@ -846,7 +925,7 @@ export default class Module {
* @return {Number} the shield addition for this module
*/
getShieldAddition(modified = true) {
return this._getValue('shieldaddition', modified);
return this.get('shieldaddition', modified);
}
/**
@@ -855,7 +934,7 @@ export default class Module {
* @return {Number} the jump range boost for this module
*/
getJumpBoost(modified = true) {
return this._getValue('jumpboost', modified);
return this.get('jumpboost', modified);
}
/**
@@ -864,7 +943,7 @@ export default class Module {
* @return {Number} the piercing for this module
*/
getPiercing(modified = true) {
return this._getValue('piercing', modified);
return this.get('piercing', modified);
}
/**
@@ -873,7 +952,7 @@ export default class Module {
* @return {Number} the bays for this module
*/
getBays(modified) {
return this._getValue('bays', modified);
return this.get('bays', modified);
}
/**
@@ -882,7 +961,7 @@ export default class Module {
* @return {Number} the rebuilds per bay for this module
*/
getRebuildsPerBay(modified = true) {
return this._getValue('rebuildsperbay', modified);
return this.get('rebuildsperbay', modified);
}
/**
@@ -891,7 +970,7 @@ export default class Module {
* @return {Number} the jitter for this module
*/
getJitter(modified = true) {
return this._getValue('jitter', modified);
return this.get('jitter', modified);
}
/**
@@ -909,7 +988,7 @@ export default class Module {
* @return {string} the shot speed for this module
*/
getShotSpeed(modified = true) {
return this._getValue('shotspeed', modified);
return this.get('shotspeed', modified);
}
/**
@@ -918,7 +997,7 @@ export default class Module {
* @return {string} the spinup for this module
*/
getSpinup(modified = true) {
return this._getValue('spinup', modified);
return this.get('spinup', modified);
}
/**
@@ -927,7 +1006,7 @@ export default class Module {
* @return {string} the time for this module
*/
getTime(modified = true) {
return this._getValue('time', modified);
return this.get('time', modified);
}
/**
@@ -936,7 +1015,7 @@ export default class Module {
* @return {string} the time for this module
*/
getHackTime(modified = true) {
return this._getValue('hacktime', modified);
return this.get('hacktime', modified);
}
}

View File

@@ -493,68 +493,62 @@ export default class Ship {
* @param {Object} name The name of the modification to change
* @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
* @param {bool} sentfromui True if this update was sent from the UI
* @param {bool} isAbsolute True if value is an absolute value and not a
* modification value
*/
setModification(m, name, value, sentfromui) {
setModification(m, name, value, sentfromui, isAbsolute) {
if (isNaN(value)) {
// Value passed is invalid; reset it to 0
value = 0;
}
if (isAbsolute) {
m.setPretty(name, value, sentfromui);
} else {
m.setModValue(name, value, sentfromui);
}
// Handle special cases
if (name === 'pgen') {
// Power generation
m.setModValue(name, value, sentfromui);
this.updatePowerGenerated();
} else if (name === 'power') {
// Power usage
m.setModValue(name, value, sentfromui);
this.updatePowerUsed();
} else if (name === 'mass') {
// Mass
m.setModValue(name, value, sentfromui);
this.recalculateMass();
this.updateMovement();
this.updateJumpStats();
} else if (name === 'maxfuel') {
m.setModValue(name, value, sentfromui);
this.updateJumpStats();
} else if (name === 'optmass') {
m.setModValue(name, value, sentfromui);
// Could be for any of thrusters, FSD or shield
this.updateMovement();
this.updateJumpStats();
this.recalculateShield();
} else if (name === 'optmul') {
m.setModValue(name, value, sentfromui);
// Could be for any of thrusters, FSD or shield
this.updateMovement();
this.updateJumpStats();
this.recalculateShield();
} else if (name === 'shieldboost') {
m.setModValue(name, value, sentfromui);
this.recalculateShield();
} else if (name === 'hullboost' || name === 'hullreinforcement' || name === 'modulereinforcement') {
m.setModValue(name, value, sentfromui);
this.recalculateArmour();
} else if (name === 'shieldreinforcement') {
m.setModValue(name, value, sentfromui);
this.recalculateShieldCells();
} else if (name === 'burst' || name == 'burstrof' || name === 'clip' || name === 'damage' || name === 'distdraw' || name === 'jitter' || name === 'piercing' || name === 'range' || name === 'reload' || name === 'rof' || name === 'thermload') {
m.setModValue(name, value, sentfromui);
this.recalculateDps();
this.recalculateHps();
this.recalculateEps();
} else if (name === 'explres' || name === 'kinres' || name === 'thermres') {
m.setModValue(name, value, sentfromui);
// Could be for shields or armour
this.recalculateArmour();
this.recalculateShield();
} else if (name === 'engcap') {
m.setModValue(name, value, sentfromui);
// Might have resulted in a change in boostability
this.updateMovement();
} else {
// Generic
m.setModValue(name, value, sentfromui);
}
}