Skip to content
Permalink
Browse files

Initial implementation of reactors

  • Loading branch information...
mugling committed Nov 27, 2016
1 parent 9b68301 commit 6d550e2ab8704220f56e5182db2b7fda2284a17a
Showing with 34 additions and 7 deletions.
  1. +33 −7 src/vehicle.cpp
  2. +1 −0 src/vehicle.h
@@ -3391,6 +3391,38 @@ int vehicle::discharge_battery (int amount, bool recurse)
amount = traverse_vehicle_graph(this, amount, discharge_visitor);
}

// if insufficient local or network electrical power try to engage any reactors
if( amount > 0 ) {
for( auto &pt : parts ) {

// consider only enabled reactors that have remaining fuel
if( pt.is_reactor() && pt.enabled && pt.ammo_remaining() ) {

const itype *fuel = item::find_type( pt.ammo_current() );
if( fuel->ammo ) {
assert( fuel->ammo->energy > 0 ); // enforced in item_factory.cpp
int density = fuel->ammo->energy;

// calculate electrical power (kJ) reactor will provide
int qty = std::min( amount, int( pt.ammo_remaining() * density ) );

// calculate reactor charges that will be consumed...
double burn = double( qty ) / density;

// partial charges have a proportional chance of being consumed each turn
burn += x_in_y( fmod( burn, 1.0 ) * 1000, 1000 ) ? 1 : 0;

pt.ammo_consume( burn, global_part_pos3( pt ) );
amount -= qty;

if( amount == 0 ) {
break;
}
}
}
}
}

return amount; // non-zero if we weren't able to fulfill demand.
}

@@ -3416,10 +3448,7 @@ void vehicle::idle(bool on_map) {
}
}

int nuclear = 0;
// @todo calculate reactor capacity

if( deficit > generate + nuclear ) {
if( deficit > generate ) {
// insufficient power so disable electrical parts
for( auto &e : parts ) {
if( e.enabled && e.info().epower < 0 ) {
@@ -3442,9 +3471,6 @@ void vehicle::idle(bool on_map) {
}
}

} else if( deficit > generate ) {
// @todo engage reactor

} else {
// as above power generation is in watts but battery storage (kJ) is less granular...
int recharge = generate - deficit;
@@ -797,6 +797,7 @@ class vehicle : public JsonSerializer, public JsonDeserializer

/**
* Try to discharge our (and, optionally, connected vehicles') batteries by the given amount.
* @note if insufficient power is available any active reactors will be engaged
* @return amount of request unfulfilled (0 if totally successful).
*/
int discharge_battery (int amount, bool recurse = true);

0 comments on commit 6d550e2

Please sign in to comment.
You can’t perform that action at this time.