Skip to content
This repository has been archived by the owner on Apr 7, 2023. It is now read-only.

Commit

Permalink
Bulkhead refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin McLeod committed Mar 10, 2016
1 parent c3fe0a0 commit 1574806
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 88 deletions.
2 changes: 1 addition & 1 deletion src/app/components/CostSection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ export default class CostSection extends TranslatedComponent {
let retrofitCosts = [];
let retrofitTotal = 0, i, l, item;

if (ship.bulkheads.index != retrofitShip.bulkheads.index) {
if (ship.bulkheads.m.index != retrofitShip.bulkheads.m.index) {
item = {
buyClassRating: ship.bulkheads.m.class + ship.bulkheads.m.rating,
buyName: ship.bulkheads.m.name,
Expand Down
3 changes: 2 additions & 1 deletion src/app/components/StandardSlot.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ export default class StandardSlot extends TranslatedComponent {
<div className={'sz'}>{slot.maxClass}</div>
<div>
<div className='l'>{classRating + ' ' + translate(m.grp)}</div>
<div className={'r'}>{m.mass || m.fuel}{units.T}</div>
<div className={'r'}>{m.mass || m.fuel || 0}{units.T}</div>
<div className={'cb'}>
{ 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 }
Expand Down
72 changes: 13 additions & 59 deletions src/app/components/StandardSlotSection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class StandardSlotSection extends SlotSection {
constructor(props, context) {
super(props, context, 'standard', 'standard');
this._optimizeStandard = this._optimizeStandard.bind(this);
this._hideDiff = this._hideDiff.bind(this);
this._selectBulkhead = this._selectBulkhead.bind(this);
}

/**
Expand Down Expand Up @@ -65,10 +65,10 @@ export default class StandardSlotSection extends SlotSection {

/**
* Use the specified bulkhead
* @param {number} bulkheadIndex 0 - 4
* @param {Object} bulkhead Bulkhead module details
*/
_selectBulkhead(bulkheadIndex) {
this.props.ship.useBulkhead(bulkheadIndex);
_selectBulkhead(bulkhead) {
this.props.ship.useBulkhead(bulkhead.index);
this.context.tooltip();
this.props.onChange();
this._close();
Expand All @@ -81,26 +81,6 @@ export default class StandardSlotSection extends SlotSection {
this._optimizeStandard();
}

/**
* Show the bulkhead diff tooltip
* @param {number} bhIndex Potential Bulkhead alternative
* @param {SyntheticEvent} event Event
*/
_bhDiff(bhIndex, event) {
let ship = this.props.ship;
this.context.tooltip(
diffDetails.call(ship, this.context.language, ModuleUtils.bulkheads(ship.id, bhIndex), ship.bulkheads.m),
event.currentTarget.getBoundingClientRect()
);
}

/**
* Hide the diff tooltip
*/
_hideDiff() {
this.context.tooltip();
}

/**
* Generate the slot React Components
* @return {Array} Array of Slots
Expand All @@ -116,41 +96,15 @@ export default class StandardSlotSection extends SlotSection {
let avail = ship.getAvailableModules().standard;
let bh = ship.bulkheads;

slots[0] = (
<div key='bh' className={cn('slot', { selected: currentMenu === bh })} onClick={open.bind(this, bh)}>
<div className={'details-container'}>
<div className={'details'}>
<div className={'sz'}>8</div>
<div>
<div className={'l'}>{translate('bh')}</div>
<div className={'r'}>{bh.m.mass}{units.T}</div>
<div className={'cl l'}>{translate(bh.m.name)}</div>
</div>
</div>
</div>
{currentMenu === bh &&
<div className='select' onClick={ e => e.stopPropagation() }>
<ul>
<li onClick={selBulkhead.bind(this, 0)} onMouseOver={this._bhDiff.bind(this, 0)} onMouseLeave={this._hideDiff} className={cn('lc', { active: bh.index == 0 })}>
{translate('Lightweight Alloy')}
</li>
<li onClick={selBulkhead.bind(this, 1)} onMouseOver={this._bhDiff.bind(this, 1)} onMouseLeave={this._hideDiff} className={cn('lc', { active: bh.index == 1 })}>
{translate('Reinforced Alloy')}
</li>
<li onClick={selBulkhead.bind(this, 2)} onMouseOver={this._bhDiff.bind(this, 2)} onMouseLeave={this._hideDiff} className={cn('lc', { active: bh.index == 2 })}>
{translate('Military Grade Composite')}
</li>
<li onClick={selBulkhead.bind(this, 3)} onMouseOver={this._bhDiff.bind(this, 3)} onMouseLeave={this._hideDiff} className={cn('lc', { active: bh.index == 3 })}>
{translate('Mirrored Surface Composite')}
</li>
<li onClick={selBulkhead.bind(this, 4)} onMouseOver={this._bhDiff.bind(this, 4)} onMouseLeave={this._hideDiff} className={cn('lc', { active: bh.index == 4 })}>
{translate('Reactive Surface Composite')}
</li>
</ul>
</div>
}
</div>
);
slots[0] = <StandardSlot
key='bh'
slot={bh}
modules={ship.getAvailableModules().bulkheads}
onOpen={open.bind(this, bh)}
onSelect={this._selectBulkhead}
selected={currentMenu == bh}
ship={ship}
/>;

slots[1] = <StandardSlot
key='pp'
Expand Down
26 changes: 21 additions & 5 deletions src/app/shipyard/ModuleSet.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

import { BulkheadNames } from './Constants';

/**
* Filter eligble modules based on parameters
* @param {Array} arr Available modules array
Expand All @@ -19,12 +21,13 @@ export default class ModuleSet {
/**
* Instantiate the module set
* @param {Object} modules All Modules
* @param {number} mass Ship mass
* @param {Array} maxStandardArr Array of standard slots classes/sizes
* @param {Array} maxInternal Array of internal slots classes/sizes
* @param {Array} maxHardPoint Array of hardpoint slots classes/sizes
* @param {Object} shipData Ship Specifications Data (see coriolis-data/Ships)
*/
constructor(modules, mass, maxStandardArr, maxInternal, maxHardPoint) {
constructor(modules, shipData) {
let maxInternal = isNaN(shipData.slots.internal[0]) ? shipData.slots.internal[0].class : shipData.slots.internal[0];
let mass = shipData.properties.hullMass + 6.5;
let maxStandardArr = shipData.slots.standard;
let maxHardPoint = shipData.slots.hardpoints[0];
let stnd = modules.standard;
this.mass = mass;
this.standard = {};
Expand All @@ -33,6 +36,10 @@ export default class ModuleSet {
this.hpClass = {};
this.intClass = {};

this.bulkheads = shipData.bulkheads.map((b, i) => {
return Object.assign({ grp: 'bh', name: BulkheadNames[i], index: i, class: '', rating: '' }, b);
});

this.standard[0] = filter(stnd.pp, maxStandardArr[0], 0, mass); // Power Plant
this.standard[2] = filter(stnd.fsd, maxStandardArr[2], 0, mass); // FSD
this.standard[4] = filter(stnd.pd, maxStandardArr[4], 0, mass); // Power Distributor
Expand All @@ -53,6 +60,15 @@ export default class ModuleSet {
}
}

/**
* Get the specified bulkhead
* @param {integer} index Bulkhead index
* @return {Object} Bulkhead module details
*/
getBulkhead(index) {
return this.bulkheads[index] || null;
}

/**
* Determine the modules that areeligible for an internal slot
* @param {integer} c The max class module that can be mounted in the slot
Expand Down
19 changes: 1 addition & 18 deletions src/app/shipyard/ModuleUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,6 @@ export function findHardpointId(groupName, clss, rating, name, mount, missile) {
return h ? h.id : 0;
}

/**
* Looks up the bulkhead module for a specific ship and bulkhead
* @param {String} shipId Unique ship Id/Key
* @param {string|number} index Index for the specified bulkhead
* @return {Object} The bulkhead module object
*/
export function bulkheads(shipId, index) {
let bulkhead = Ships[shipId].bulkheads[index];
bulkhead.class = 1;
bulkhead.rating = 'I';
bulkhead.name = BulkheadNames[index];

return bulkhead;
}

/**
* Get the bulkhead index for the given bulkhead name
* @param {String} bulkheadName Bulkhead name in english
Expand Down Expand Up @@ -215,7 +200,5 @@ export function isShieldGenerator(g) {
* @return {ModuleSet} The set of modules the ship can install
*/
export function forShip(shipId) {
let ship = Ships[shipId];
let maxInternal = isNaN(ship.slots.internal[0]) ? ship.slots.internal[0].class : ship.slots.internal[0];
return new ModuleSet(Modules, ship.properties.hullMass + 6.5, ship.slots.standard, maxInternal, ship.slots.hardpoints[0]);
return new ModuleSet(Modules, Ships[shipId]);
}
2 changes: 1 addition & 1 deletion src/app/shipyard/Serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function toDetailedBuild(buildName, ship) {
}],
components: {
standard: {
bulkheads: BulkheadNames[ship.bulkheads.index],
bulkheads: BulkheadNames[ship.bulkheads.m.index],
cargoHatch: { enabled: Boolean(ship.cargoHatch.enabled), priority: ship.cargoHatch.priority + 1 },
powerPlant: { class: standard[0].m.class, rating: standard[0].m.rating, enabled: Boolean(standard[0].enabled), priority: standard[0].priority + 1 },
thrusters: { class: standard[1].m.class, rating: standard[1].m.rating, enabled: Boolean(standard[1].enabled), priority: standard[1].priority + 1 },
Expand Down
5 changes: 2 additions & 3 deletions src/app/shipyard/Ship.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export default class Ship {
*/
getStandardString() {
if(!this.serialized.standard) {
this.serialized.standard = this.bulkheads.index + this.standard.reduce((arr, slot, i) => {
this.serialized.standard = this.bulkheads.m.index + this.standard.reduce((arr, slot, i) => {
arr[i] = slot.m ? slot.m.class + slot.m.rating : '-';
return arr;
}, new Array(this.standard.length)).join('');
Expand Down Expand Up @@ -855,8 +855,7 @@ export default class Ship {
*/
useBulkhead(index, preventUpdate) {
let oldBulkhead = this.bulkheads.m;
this.bulkheads.index = index;
this.bulkheads.m = ModuleUtils.bulkheads(this.id, index);
this.bulkheads.m = this.availCS.getBulkhead(index);
this.bulkheads.discountedCost = this.bulkheads.m.cost * this.moduleCostMultiplier;
this.armourMultiplier = ArmourMultiplier[index];
this.updateStats(this.bulkheads, this.bulkheads.m, oldBulkhead, preventUpdate);
Expand Down

0 comments on commit 1574806

Please sign in to comment.