Move more values to functions over direct variable access

This commit is contained in:
Cmdr McDonald
2016-10-29 23:32:52 +01:00
parent 2b464f34e5
commit 0df051e40f
9 changed files with 289 additions and 102 deletions

View File

@@ -42,11 +42,10 @@ export default class ModificationsMenu extends TranslatedComponent {
let { m } = props;
let list = [];
for (let modId of Modifications.validity[m.grp]) {
let modifiers = Modifications.modifiers[modId]
list.push(<div className={'cb'} key={modId}>
<div className={'cb'}>{translate(modifiers.name)}{' (%)'}</div>
<NumberEditor className={'cb'} style={'width: 100%, text-align: center'} step={0.01} stepModifier={1} decimals={2} initialValue={m.getModValue(modId) ? m.getModValue(modId) * 100 : 0} value={m.getModValue(modId) ? m.getModValue(modId) * 100 : 0} onValueChange={this._updateValue.bind(this, modId)} />
for (let modName of Modifications.validity[m.grp]) {
list.push(<div className={'cb'} key={modName}>
<div className={'cb'}>{translate(modName)}{' (%)'}</div>
<NumberEditor className={'cb'} style={{ width: '100%', textAlign: 'center' }} step={0.01} stepModifier={1} decimals={2} initialValue={m.getModValue(modName) ? m.getModValue(modName) * 100 : 0} value={m.getModValue(modName) ? m.getModValue(modName) * 100 : 0} onValueChange={this._updateValue.bind(this, modName)} />
</div>);
}
@@ -64,16 +63,16 @@ export default class ModificationsMenu extends TranslatedComponent {
/**
* Update modification given a value.
* @param {Number} modId The ID of the modification
* @param {Number} name The name of the modification
* @param {Number} value The value to set, in the range [0,1]
*/
_updateValue(modId, value) {
_updateValue(name, value) {
let scaledValue = Math.floor(value * 100) / 10000;
let m = this.props.m;
let ship = this.props.ship;
ship.setModification(m, modId, scaledValue);
ship.setModification(m, name, scaledValue);
this.props.onChange();
}

View File

@@ -69,22 +69,22 @@ export default class StandardSlot extends TranslatedComponent {
<div/>
<div className={'cb'}>
{ m.grp == 'bh' && m.name ? <div className='l'>{translate(m.name)}</div> : null }
{ m.optmass ? <div className='l'>{translate('optimal mass')}: {m.optmass}{units.T}</div> : null }
{ m.maxmass ? <div className='l'>{translate('max mass')}: {m.maxmass}{units.T}</div> : null }
{ m.range ? <div className='l'>{translate('range')}: {m.range}{units.km}</div> : null }
{ m.getOptimalMass() ? <div className='l'>{translate('optimal mass')}: {m.getOptimalMass()}{units.T}</div> : null }
{ m.getMaxMass() ? <div className='l'>{translate('max mass')}: {m.getMaxMass()}{units.T}</div> : null }
{ m.getRange() ? <div className='l'>{translate('range')}: {m.getRange()}{units.km}</div> : null }
{ m.time ? <div className='l'>{translate('time')}: {formats.time(m.time)}</div> : null }
{ m.eff ? <div className='l'>{translate('efficiency')}: {m.eff}</div> : null }
{ m.getThermalEfficiency() ? <div className='l'>{translate('efficiency')}: {m.getThermalEfficiency()}</div> : null }
{ m.getPowerGeneration() > 0 ? <div className='l'>{translate('power')}: {formats.round(m.getPowerGeneration())}{units.MW}</div> : null }
{ m.maxfuel ? <div className='l'>{translate('max')} {translate('fuel')}: {m.maxfuel}{units.T}</div> : null }
{ m.weaponcapacity ? <div className='l'>{translate('WEP')}: {m.weaponcapacity}{units.MJ} / {m.weaponrecharge}{units.MW}</div> : null }
{ m.systemcapacity ? <div className='l'>{translate('SYS')}: {m.systemcapacity}{units.MJ} / {m.systemrecharge}{units.MW}</div> : null }
{ m.enginecapacity ? <div className='l'>{translate('ENG')}: {m.enginecapacity}{units.MJ} / {m.enginerecharge}{units.MW}</div> : null }
{ m.getMaxFuelPerJump() ? <div className='l'>{translate('max')} {translate('fuel')}: {m.getMaxFuelPerJump()}{units.T}</div> : null }
{ m.getWeaponsCapacity() ? <div className='l'>{translate('WEP')}: {m.getWeaponsCapacity()}{units.MJ} / {m.getWeaponsRechargeRate()}{units.MW}</div> : null }
{ m.getSystemsCapacity() ? <div className='l'>{translate('SYS')}: {m.getSystemsCapacity()}{units.MJ} / {m.getSystemsRechargeRate()}{units.MW}</div> : null }
{ m.getEnginesCapacity() ? <div className='l'>{translate('ENG')}: {m.getEnginesCapacity()}{units.MJ} / {m.getEnginesRechargeRate()}{units.MW}</div> : null }
</div>
</div>
</div>
{menu}
</div>
);
//{ validMods.length > 0 ? <div className='r' ><button onClick={this._showModificationsMenu.bind(this, m)} onContextMenu={stopCtxPropagation} onMouseOver={termtip.bind(null, 'modifications')} onMouseOut={tooltip.bind(null, null)}><ListModifications /></button></div> : null }
// { validMods.length > 0 ? <div className='r' ><button onClick={this._showModificationsMenu.bind(this, m)} onContextMenu={stopCtxPropagation} onMouseOver={termtip.bind(null, 'modifications')} onMouseOut={tooltip.bind(null, null)}><ListModifications /></button></div> : null }
}
}

View File

@@ -163,7 +163,7 @@ export default class StandardSlotSection extends SlotSection {
selected={currentMenu == st[4]}
onChange={this.props.onChange}
ship={ship}
warning= {m => m.enginecapacity < ship.boostEnergy}
warning= {m => m.getEnginesCapacity() < ship.boostEnergy}
/>;
slots[6] = <StandardSlot
@@ -175,7 +175,6 @@ export default class StandardSlotSection extends SlotSection {
selected={currentMenu == st[5]}
onChange={this.props.onChange}
ship={ship}
warning= {m => m.enginecapacity < ship.boostEnergy}
/>;
slots[7] = <StandardSlot

View File

@@ -8,7 +8,7 @@
* @return {number} Distance in Light Years
*/
export function jumpRange(mass, fsd, fuel) {
return Math.pow(Math.min(fuel === undefined ? fsd.maxfuel : fuel, fsd.maxfuel) / fsd.fuelmul, 1 / fsd.fuelpower) * fsd.optmass / mass;
return Math.pow(Math.min(fuel === undefined ? fsd.getMaxFuelPerJump() : fuel, fsd.getMaxFuelPerJump()) / fsd.fuelmul, 1 / fsd.fuelpower) * fsd.getOptimalMass() / mass;
}
/**
@@ -20,15 +20,15 @@ export function jumpRange(mass, fsd, fuel) {
* @return {number} Distance in Light Years
*/
export function fastestRange(mass, fsd, fuel) {
let fuelRemaining = fuel % fsd.maxfuel; // Fuel left after making N max jumps
let jumps = Math.floor(fuel / fsd.maxfuel);
let fuelRemaining = fuel % fsd.getMaxFuelPerJump(); // Fuel left after making N max jumps
let jumps = Math.floor(fuel / fsd.getMaxFuelPerJump());
mass += fuelRemaining;
// Going backwards, start with the last jump using the remaining fuel
let fastestRange = fuelRemaining > 0 ? Math.pow(fuelRemaining / fsd.fuelmul, 1 / fsd.fuelpower) * fsd.optmass / mass : 0;
let fastestRange = fuelRemaining > 0 ? Math.pow(fuelRemaining / fsd.fuelmul, 1 / fsd.fuelpower) * fsd.getOptimalMass() / mass : 0;
// For each max fuel jump, calculate the max jump range based on fuel mass left in the tank
for (let j = 0; j < jumps; j++) {
mass += fsd.maxfuel;
fastestRange += Math.pow(fsd.maxfuel / fsd.fuelmul, 1 / fsd.fuelpower) * fsd.optmass / mass;
fastestRange += Math.pow(fsd.getMaxFuelPerJump() / fsd.fuelmul, 1 / fsd.fuelpower) * fsd.getOptimalMass() / mass;
}
return fastestRange;
};

View File

@@ -27,42 +27,50 @@ export default class Module {
}
/**
* Get a value for a given modification ID
* @param {Number} modId The ID of the modification
* @return {Number} The value of the modification, as a decimal value from -1 to 1
* 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 from -1 to 1
*/
getModValue(modId) {
return this.mods ? this.mods[modId] / 10000 : null;
getModValue(name) {
return this.mods ? this.mods[name] / 10000 : null;
}
/**
* Set a value for a given modification ID
* @param {Number} modId The ID of the modification
* @param {Number} value The value of the modification, as a decimal value from -1 to 1
* @param {Number} name The name of the modification
* @param {Number} value The value of the modification, as a decimal value from -1 to 1
*/
setModValue(modId, value) {
setModValue(name, value) {
if (value == null || value == 0) {
delete this.mods[modId];
delete this.mods[name];
} else {
// Store value with 2dp
this.mods[modId] = Math.round(value * 10000);
this.mods[name] = Math.round(value * 10000);
}
}
/**
* Helper to obtain a modified value using standard multipliers
* @param {String} name the name of the modifier to obtain
* @return {Number} the mass of this module
*/
_getModifiedValue(name) {
let result = 0;
if (this[name]) {
result = this[name];
if (result) {
let mult = this.getModValue(name);
if (mult) { result = result * (1 + mult); }
}
}
return result;
}
/**
* Get the power generation of this module, taking in to account modifications
* @return {Number} the power generation of this module
*/
getPowerGeneration() {
let result = 0;
if (this.pGen) {
result = this.pGen;
if (result) {
let mult = this.getModValue(1);
if (mult) { result = result * (1 + mult); }
}
}
return result;
return this._getModifiedValue('pGen');
}
/**
@@ -70,15 +78,7 @@ export default class Module {
* @return {Number} the power usage of this module
*/
getPowerUsage() {
let result = 0;
if (this.power) {
result = this.power;
if (result) {
let mult = this.getModValue(2);
if (mult) { result = result * (1 + mult); }
}
}
return result;
return this._getModifiedValue('power');
}
/**
@@ -86,15 +86,7 @@ export default class Module {
* @return {Number} the integrity of this module
*/
getIntegrity() {
let result = 0;
if (this.health) {
result = this.health;
if (result) {
let mult = this.getModValue(3);
if (mult) { result = result * (1 + mult); }
}
}
return result;
return this._getModifiedValue('integrity');
}
/**
@@ -102,14 +94,206 @@ export default class Module {
* @return {Number} the mass of this module
*/
getMass() {
let result = 0;
if (this.mass) {
result = this.mass;
if (result) {
let mult = this.getModValue(4);
if (mult) { result = result * (1 + mult); }
}
}
return result;
return this._getModifiedValue('mass');
}
/**
* Get the thermal efficiency of this module, taking in to account modifications
* @return {Number} the thermal efficiency of this module
*/
getThermalEfficiency() {
return this._getModifiedValue('eff');
}
/**
* Get the maximum mass of this module, taking in to account modifications
* @return {Number} the maximum mass of this module
*/
getMaxMass() {
return this._getModifiedValue('maxmass');
}
/**
* Get the optimal mass of this module, taking in to account modifications
* @return {Number} the optimal mass of this module
*/
getOptimalMass() {
return this._getModifiedValue('optmass');
}
/**
* Get the optimal multiplier of this module, taking in to account modifications
* @return {Number} the optimal multiplier of this module
*/
getOptimalMultiplier() {
return this._getModifiedValue('optmult');
}
/**
* Get the damage per second for this module, taking in to account modifications
* @return {Number} the damage per second of this module
*/
getDamagePerSecond() {
return this._getModifiedValue('dps');
}
/**
* Get the energy per second for this module, taking in to account modifications
* @return {Number} the energy per second of this module
*/
getEnergyPerSecond() {
return this._getModifiedValue('eps');
}
/**
* Get the heat per second for this module, taking in to account modifications
* @return {Number} the heat per second of this module
*/
getHeatPerSecond() {
return this._getModifiedValue('hps');
}
/**
* Get the maximum fuel per jump for this module, taking in to account modifications
* @return {Number} the maximum fuel per jump of this module
*/
getMaxFuelPerJump() {
return this._getModifiedValue('maxfuel');
}
/**
* Get the systems capacity for this module, taking in to account modifications
* @return {Number} the systems capacity of this module
*/
getSystemsCapacity() {
return this._getModifiedValue('syscap');
}
/**
* Get the engines capacity for this module, taking in to account modifications
* @return {Number} the engines capacity of this module
*/
getEnginesCapacity() {
return this._getModifiedValue('engcap');
}
/**
* Get the weapons capacity for this module, taking in to account modifications
* @return {Number} the weapons capacity of this module
*/
getWeaponsCapacity() {
return this._getModifiedValue('wepcap');
}
/**
* Get the systems recharge rate for this module, taking in to account modifications
* @return {Number} the systems recharge rate of this module
*/
getSystemsRechargeRate() {
return this._getModifiedValue('sysrate');
}
/**
* Get the engines recharge rate for this module, taking in to account modifications
* @return {Number} the engines recharge rate of this module
*/
getEnginesRechargeRate() {
return this._getModifiedValue('engrate');
}
/**
* Get the weapons recharge rate for this module, taking in to account modifications
* @return {Number} the weapons recharge rate of this module
*/
getWeaponsRechargeRate() {
return this._getModifiedValue('weprate');
}
/**
* Get the kinetic resistance for this module, taking in to account modifications
* @return {Number} the kinetic resistance of this module
*/
getKineticResistance() {
return this._getModifiedValue('kinres');
}
/**
* Get the thermal resistance for this module, taking in to account modifications
* @return {Number} the thermal resistance of this module
*/
getThermalResistance() {
return this._getModifiedValue('thermres');
}
/**
* Get the explosive resistance for this module, taking in to account modifications
* @return {Number} the explosive resistance of this module
*/
getExplosiveResistance() {
return this._getModifiedValue('explres');
}
/**
* Get the regeneration rate for this module, taking in to account modifications
* @return {Number} the regeneration rate of this module
*/
getRegenerationRate() {
return this._getModifiedValue('regen');
}
/**
* Get the broken regeneration rate for this module, taking in to account modifications
* @return {Number} the broken regeneration rate of this module
*/
getBrokenRegenerationRate() {
return this._getModifiedValue('brokenregen');
}
/**
* Get the range rate for this module, taking in to account modifications
* @return {Number} the range rate of this module
*/
getRange() {
return this._getModifiedValue('range');
}
/**
* Get the capture arc for this module, taking in to account modifications
* @return {Number} the capture arc of this module
*/
getCaptureArc() {
return this._getModifiedValue('arc');
}
/**
* Get the armour for this module, taking in to account modifications
* @return {Number} the armour of this module
*/
getArmour() {
return this._getModifiedValue('armour');
}
/**
* Get the delay for this module, taking in to account modifications
* @return {Number} the delay of this module
*/
getDelay() {
return this._getModifiedValue('delay');
}
/**
* Get the duration for this module, taking in to account modifications
* @return {Number} the duration of this module
*/
getDuration() {
return this._getModifiedValue('duration');
}
/**
* Get the shield reinforcement for this module, taking in to account modifications
* @return {Number} the shield reinforcement of this module
*/
getShieldReinforcement() {
return this._getModifiedValue('shieldreinforcement');
}
}

View File

@@ -126,7 +126,7 @@ export default class ModuleSet {
let pd = this.standard[4][0];
for (let p of this.standard[4]) {
if (p.mass < pd.mass && p.enginecapacity >= boostEnergy) {
if (p.mass < pd.mass && p.getEnginesCapacity() >= boostEnergy) {
pd = p;
}
}

View File

@@ -76,7 +76,7 @@ export function toDetailedBuild(buildName, ship) {
ship: ship.name,
references: [{
name: 'Coriolis.io',
url: `https://coriolis.io/outfit/${ship.id}/${code}?bn=${encodeURIComponent(buildName)}`,
url: `https://coriolis.edcd.io/outfit/${ship.id}/${code}?bn=${encodeURIComponent(buildName)}`,
code,
shipId: ship.id
}],

View File

@@ -35,7 +35,7 @@ function decodeModsToArray(code, arr) {
let mods = moduleMods[i].split(';');
for (let j = 0; j < mods.length; j++) {
let modElements = mods[j].split(':');
arr[i][Number(modElements[0])] = Number(modElements[1]);
arr[i][modElements[0]] = Number(modElements[1]);
}
}
}
@@ -135,7 +135,7 @@ export default class Ship {
*/
canThrust() {
return this.getSlotStatus(this.standard[1]) == 3 && // Thrusters are powered
this.ladenMass < this.standard[1].m.maxmass; // Max mass not exceeded
this.ladenMass < this.standard[1].m.getMaxMass(); // Max mass not exceeded
}
/**
@@ -143,9 +143,9 @@ export default class Ship {
* @return {[type]} True if boost capable
*/
canBoost() {
return this.canThrust() && // Thrusters operational
this.getSlotStatus(this.standard[4]) == 3 && // Power distributor operational
this.boostEnergy <= this.standard[4].m.enginecapacity; // PD capacitor is sufficient for boost
return this.canThrust() && // Thrusters operational
this.getSlotStatus(this.standard[4]) == 3 && // Power distributor operational
this.boostEnergy <= this.standard[4].m.getEnginesCapacity(); // PD capacitor is sufficient for boost
}
/**
@@ -179,7 +179,7 @@ export default class Ship {
*/
calcUnladenRange(massDelta, fuel, fsd) {
fsd = fsd || this.standard[2].m;
return Calc.jumpRange(this.unladenMass + (massDelta || 0) + Math.min(fsd.maxfuel, fuel || this.fuelCapacity), fsd || this.standard[2].m, fuel);
return Calc.jumpRange(this.unladenMass + (massDelta || 0) + Math.min(fsd.getMaxFuelPerJump(), fuel || this.fuelCapacity), fsd || this.standard[2].m, fuel);
}
/**
@@ -405,31 +405,37 @@ export default class Ship {
/**
* Set a modification value
* @param {Object} m The module to change
* @param {Object} mId The ID of the modification to change
* @param {Object} name The name of the modification to change
* @param {Number} value The new value of the modification
*/
setModification(m, mId, value) {
setModification(m, name, value) {
// Handle special cases
if (mId == 1) {
if (name == 'pGen') {
// Power generation
m.setModValue(mId, value);
m.setModValue(name, value);
this.updatePower();
} else if (mId == 2) {
} else if (name == 'power') {
// Power usage
m.setModValue(mId, value);
m.setModValue(name, value);
this.updatePower();
} else if (mId == 4) {
} else if (name == 'mass') {
// Mass
let oldMass = m.getMass();
m.setModValue(mId, value);
m.setModValue(name, value);
let newMass = m.getMass();
this.unladenMass = this.unladenMass - oldMass + newMass;
this.ladenMass = this.ladenMass - oldMass + newMass;
this.updateTopSpeed();
this.updateJumpStats();
} else if (name == 'maxfuel') {
m.setModValue(name, value);
this.updateJumpStats();
} else if (name == 'optmass') {
m.setModValue(name, value);
this.updateJumpStats();
} else {
// Generic
m.setModValue(mId, value);
m.setModValue(name, value);
}
}
@@ -566,7 +572,7 @@ export default class Ship {
}
if (parts[3]) {
//decodeModsToArray(LZString.decompressFromBase64(parts[3].replace(/-/g, '/')), mods);
// decodeModsToArray(LZString.decompressFromBase64(parts[3].replace(/-/g, '/')), mods);
decodeModsToArray(parts[3], mods);
}
@@ -960,7 +966,7 @@ export default class Ship {
}
allMods.push(slotMods.join(';'));
}
//this.serialized.modifications = LZString.compressToBase64(allMods.join(',').replace(/,+$/, '')).replace(/\//g, '-');
// this.serialized.modifications = LZString.compressToBase64(allMods.join(',').replace(/,+$/, '')).replace(/\//g, '-');
this.serialized.modifications = allMods.join(',').replace(/,+$/, '');
return this;
}
@@ -1132,5 +1138,4 @@ export default class Ship {
}
return this;
}
}

View File

@@ -123,12 +123,12 @@ const PROP_BLACKLIST = {
mass: 1,
cost: 1,
recover: 1,
weaponcapacity: 1,
weaponrecharge: 1,
enginecapacity: 1,
enginerecharge: 1,
systemcapacity: 1,
systemrecharge: 1,
wepcap: 1,
weprate: 1,
engcap: 1,
engrate: 1,
syscap: 1,
sysrate: 1,
breachdps: 1,
breachmin: 1,
breachmax: 1,
@@ -251,21 +251,21 @@ export function diffDetails(language, m, mm) {
if (m.grp == 'pd') {
propDiffs.push(<div key='wep'>
{`${translate('WEP')}: `}
<span className={diffClass(m.weaponcapacity, mm.weaponcapacity)}>{m.weaponcapacity}{units.MJ}</span>
<span className={diffClass(m.getWeaponsCapacity(), mm.getWeaponsCapacity())}>{m.getWeaponsCapacity()}{units.MJ}</span>
{' / '}
<span className={diffClass(m.weaponrecharge, mm.weaponrecharge)}>{m.weaponrecharge}{units.MW}</span>
<span className={diffClass(m.getWeaponsRechargeRate(), mm.getWeaponsRechargeRate())}>{m.getWeaponsRechargeRate()}{units.MW}</span>
</div>);
propDiffs.push(<div key='sys'>
{`${translate('SYS')}: `}
<span className={diffClass(m.systemcapacity, mm.systemcapacity)}>{m.systemcapacity}{units.MJ}</span>
<span className={diffClass(m.getSystemsCapacity(), mm.getSystemsCapacity())}>{m.getSystemsCapacity()}{units.MJ}</span>
{' / '}
<span className={diffClass(m.systemrecharge, mm.systemrecharge)}>{m.systemrecharge}{units.MW}</span>
<span className={diffClass(m.getSystemsRechargeRate(), mm.getSystemsRechargeRate())}>{m.getSystemsRechargeRate()}{units.MW}</span>
</div>);
propDiffs.push(<div key='eng'>
{`${translate('ENG')}: `}
<span className={diffClass(m.enginecapacity, mm.enginecapacity)}>{m.enginecapacity}{units.MJ}</span>
<span className={diffClass(m.getEnginesCapacity(), mm.getEnginesCapacity())}>{m.getEnginesCapacity()}{units.MJ}</span>
{' / '}
<span className={diffClass(m.enginerecharge, mm.enginerecharge)}>{m.enginerecharge}{units.MW}</span>
<span className={diffClass(m.getEnginesRechargeRate(), mm.getEnginesRechargeRate())}>{m.getEnginesRechargeRate()}{units.MW}</span>
</div>);
}