Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor wire loss and vehicle battery charging #64097

Merged
merged 12 commits into from
Mar 20, 2023
Merged
8 changes: 4 additions & 4 deletions data/json/vehicleparts/vehicle_parts.json
Original file line number Diff line number Diff line change
Expand Up @@ -2455,7 +2455,7 @@
"broken_symbol": "*",
"broken_color": "dark_gray",
"damage_modifier": 10,
"epower": "1 W",
"epower": "1 kW",
"//": "Epower for POWER_TRANSFER stuff is how much percentage-wise loss there is in transmission",
"durability": 120,
"description": "Thick copper cable with leads on either end. Attach one end to one vehicle and the other to another, and you can transfer electrical power between the two.",
Expand All @@ -2474,7 +2474,7 @@
"broken_symbol": "*",
"broken_color": "dark_gray",
"damage_modifier": 10,
"epower": "1 W",
"epower": "1 kW",
"//": "Epower for POWER_TRANSFER stuff is how much percentage-wise loss there is in transmission",
"durability": 120,
"description": "A long orange extension cord for connecting appliances. Currently plugged in.",
Expand All @@ -2493,7 +2493,7 @@
"broken_symbol": "*",
"broken_color": "dark_gray",
"damage_modifier": 10,
"epower": "1 W",
"epower": "1 kW",
"//": "Epower for POWER_TRANSFER stuff is how much percentage-wise loss there is in transmission",
"durability": 120,
"description": "An extra long 30m orange extension cord for connecting outdoor appliances. Currently plugged in.",
Expand All @@ -2514,7 +2514,7 @@
"damage_modifier": 10,
"durability": 120,
"description": "Very thick copper cable with leads on either end. Attach one end to one vehicle and the other to another, and you can transfer electrical power between the two.",
"epower": "5 W",
"epower": "5 kW",
"//": "Epower for POWER_TRANSFER stuff is how much percentage-wise loss there is in transmission",
"item": "jumper_cable_heavy",
"requirements": { "removal": { "time": "5 s" } },
Expand Down
41 changes: 40 additions & 1 deletion data/mods/TEST_DATA/appliance.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,52 @@
"broken_symbol": "*",
"broken_color": "dark_gray",
"damage_modifier": 10,
"epower": "1 W",
"epower": "1 kW",
"//": "Epower for POWER_TRANSFER stuff is how much percentage-wise loss there is in transmission",
"durability": 120,
"description": "A long orange extension cord for connecting appliances. Currently plugged in.",
"item": "test_extension_cable",
"requirements": { "removal": { "time": "5 s" } },
"flags": [ "NOINSTALL", "UNMOUNT_ON_DAMAGE", "UNMOUNT_ON_MOVE", "POWER_TRANSFER" ],
"breaks_into": [ { "item": "cable", "charges": [ 1, 10 ] }, { "item": "plastic_chunk", "count": [ 1, 2 ] } ]
},
{
"type": "TOOL",
"id": "test_power_cord_25_loss",
"name": { "str": "test_power_cord_25_loss item" },
"description": "test 25% loss extension cord.",
"to_hit": 1,
"color": "dark_gray",
"symbol": "&",
"material": [ "steel", "plastic" ],
"volume": "500 ml",
"weight": "75 g",
"bashing": 2,
"category": "tools",
"price": 1,
"price_postapoc": 100,
"max_charges": 3,
"initial_charges": 3,
"use_action": [ "CABLE_ATTACH" ],
"flags": [ "CABLE_SPOOL", "POWER_CORD", "SINGLE_USE" ]
},
{
"type": "vehicle_part",
"id": "test_power_cord_25_loss",
"name": { "str": "test 25% loss extension cord part" },
"symbol": "{",
"categories": [ "other" ],
"color": "yellow",
"broken_symbol": "*",
"broken_color": "dark_gray",
"damage_modifier": 10,
"epower": "25 kW",
"//": "Epower for POWER_TRANSFER stuff is how much percentage-wise loss there is in transmission",
"durability": 120,
"description": "A long orange extension cord for connecting appliances. Currently plugged in.",
"item": "test_power_cord_25_loss",
"requirements": { "removal": { "time": "5 s" } },
"flags": [ "NOINSTALL", "UNMOUNT_ON_DAMAGE", "UNMOUNT_ON_MOVE", "POWER_TRANSFER" ],
"breaks_into": [ { "item": "cable", "charges": [ 1, 10 ] }, { "item": "plastic_chunk", "count": [ 1, 2 ] } ]
}
]
4 changes: 2 additions & 2 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1339,11 +1339,11 @@ void activity_handlers::fill_liquid_do_turn( player_activity *act, Character *yo
veh = &vp->vehicle();
part = act_ref.values[4];
if( source_veh &&
source_veh->fuel_left( liquid.typeId(), false, ( veh ? std::function<bool( const vehicle_part & )> { [&]( const vehicle_part & pa )
source_veh->fuel_left( liquid.typeId(), ( veh ? std::function<bool( const vehicle_part & )> { [&]( const vehicle_part & pa )
{
return &veh->part( part ) != &pa;
}
} : return_true<const vehicle_part &> ) ) <= 0 ) {
} : return_true<const vehicle_part &> ) ) <= 0 ) {
act_ref.set_to_null();
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/activity_item_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ static bool are_requirements_nearby(
const cata::optional<vpart_reference> &vp = here.veh_at( elem ).part_with_tool( itype_welder );

if( vp ) {
const int veh_battery = vp->vehicle().fuel_left( itype_battery, true );
const int veh_battery = vp->vehicle().fuel_left( itype_battery );

item welder( itype_welder, calendar::turn_zero );
welder.charges = veh_battery;
Expand Down
2 changes: 1 addition & 1 deletion src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2482,7 +2482,7 @@ vehicle *game::remoteveh()
tripoint vp;
remote_veh_string >> vp.x >> vp.y >> vp.z;
vehicle *veh = veh_pointer_or_null( m.veh_at( vp ) );
if( veh && veh->fuel_left( itype_battery, true ) > 0 ) {
if( veh && veh->fuel_left( itype_battery ) > 0 ) {
remoteveh_cache = veh;
} else {
remoteveh_cache = nullptr;
Expand Down
6 changes: 3 additions & 3 deletions src/iuse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7911,7 +7911,7 @@ static vehicle *pickveh( const tripoint &center, bool advanced )
for( wrapped_vehicle &veh : get_map().get_vehicles() ) {
vehicle *&v = veh.v;
if( rl_dist( center, v->global_pos3() ) < 40 &&
v->fuel_left( itype_battery, true ) > 0 &&
v->fuel_left( itype_battery ) > 0 &&
( !empty( v->get_avail_parts( advctrl ) ) ||
( !advanced && !empty( v->get_avail_parts( ctrl ) ) ) ) ) {
vehs.push_back( v );
Expand Down Expand Up @@ -7952,7 +7952,7 @@ cata::optional<int> iuse::remoteveh( Character *p, item *it, bool t, const tripo
} else if( remote == nullptr ) {
p->add_msg_if_player( _( "Lost contact with the vehicle." ) );
stop = true;
} else if( remote->fuel_left( itype_battery, true ) == 0 ) {
} else if( remote->fuel_left( itype_battery ) == 0 ) {
p->add_msg_if_player( m_bad, _( "The vehicle's battery died." ) );
stop = true;
}
Expand Down Expand Up @@ -10023,7 +10023,7 @@ cata::optional<int> iuse::voltmeter( Character *p, item *, bool, const tripoint
p->add_msg_if_player( _( "There's nothing to measure there." ) );
return cata::nullopt;
}
if( vp->vehicle().fuel_left( itype_battery, true ) ) {
if( vp->vehicle().fuel_left( itype_battery ) ) {
p->add_msg_if_player( _( "The %1$s has voltage." ), vp->vehicle().name );
} else {
p->add_msg_if_player( _( "The %1$s has no voltage." ), vp->vehicle().name );
Expand Down
18 changes: 11 additions & 7 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,17 +585,21 @@ void map::vehmove()
}
}
dirty_vehicle_list.clear();
std::set<vehicle *> origins;
std::map<vehicle *, bool> vehs; // value true means in on map
for( int zlev = minz; zlev <= maxz; ++zlev ) {
const level_cache *cache = get_cache_lazy( zlev );
if( cache ) {
for( vehicle *veh : cache->vehicle_list ) {
origins.emplace( veh );
if( !cache ) {
continue;
}
for( vehicle *veh : cache->vehicle_list ) {
vehs[veh] = true; // force on map vehicles to true
for( const std::pair<vehicle *const, float> &pair : veh->search_connected_vehicles() ) {
vehs.emplace( pair.first, false ); // add with 'false' if does not exist (off map)
}
}
}
for( const std::pair<vehicle *const, bool> &veh_pair : vehicle::enumerate_vehicles( origins ) ) {
veh_pair.first->idle( veh_pair.second );
for( const std::pair<vehicle *const, bool> &veh_pair : vehs ) {
veh_pair.first->idle( /* on_map = */ veh_pair.second );
}

// refresh vehicle zones for moved vehicles
Expand Down Expand Up @@ -5212,7 +5216,7 @@ static void process_vehicle_items( vehicle &cur_veh, int part )
( n.type->battery && n.type->battery->max_capacity > n.energy_remaining() ) ) {
int power = recharge_part.info().bonus;
while( power >= 1000 || x_in_y( power, 1000 ) ) {
const int missing = cur_veh.discharge_battery( 1, true );
const int missing = cur_veh.discharge_battery( 1 );
// Around 85% efficient; a few of the discharges don't actually recharge
if( missing == 0 && !one_in( 7 ) ) {
if( n.is_vehicle_battery() ) {
Expand Down
13 changes: 6 additions & 7 deletions src/veh_appliance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,7 @@ veh_app_interact::veh_app_interact( vehicle &veh, const point &p )
// @returns true if a battery part exists on any vehicle connected to veh
static bool has_battery_in_grid( vehicle *veh )
{
const std::map<vehicle *, bool> veh_map = vehicle::enumerate_vehicles( { veh } );
return std::any_of( veh_map.begin(), veh_map.end(),
[]( const std::pair<vehicle *, bool> &p ) {
return !p.first->batteries.empty();
} );
return !veh->search_connected_batteries().empty();
}

void veh_app_interact::init_ui_windows()
Expand Down Expand Up @@ -225,8 +221,11 @@ void veh_app_interact::draw_info()
}

// Battery power output
units::power charge_rate = veh->net_battery_charge_rate( true, true );
print_charge( _( "Grid battery power flow: " ), charge_rate, row );
units::power grid_flow = 0_W;
for( const std::pair<vehicle *const, float> &pair : veh->search_connected_vehicles() ) {
grid_flow += pair.first->net_battery_charge_rate( /* include_reactors = */ true );
}
print_charge( _( "Grid battery power flow: " ), grid_flow, row );
row++;

// Reactor power output
Expand Down
2 changes: 1 addition & 1 deletion src/veh_interact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,7 @@ void veh_interact::calc_overview()
overview_opts.clear();
overview_headers.clear();

units::power epower = veh->net_battery_charge_rate();
units::power epower = veh->net_battery_charge_rate( /* include_reactors = */ true );
overview_headers["1_ENGINE"] = [this]( const catacurses::window & w, int y ) {
trim_and_print( w, point( 1, y ), getmaxx( w ) - 2, c_light_gray,
string_format( _( "Engines: %sSafe %4d kW</color> %sMax %4d kW</color>" ),
Expand Down
Loading