Skip to content

Commit

Permalink
Remove vehicle::part_info(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
irwiss committed Jul 10, 2023
1 parent dc6881b commit 99d63fe
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 93 deletions.
21 changes: 12 additions & 9 deletions src/activity_item_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ static void put_into_vehicle( Character &c, item_drop_reason reason, const std::
if( items.empty() ) {
return;
}

const tripoint where = veh.global_part_pos3( part );
vehicle_part &vp = veh.part( part );
const tripoint where = veh.global_part_pos3( vp );
map &here = get_map();
const std::string ter_name = here.name( where );
int fallen_count = 0;
Expand All @@ -224,7 +224,7 @@ static void put_into_vehicle( Character &c, item_drop_reason reason, const std::
it.charges = 0;
}

if( veh.add_item( part, it ) ) {
if( veh.add_item( vp, it ) ) {
into_vehicle_count += it.count();
} else {
if( it.count_by_charges() ) {
Expand All @@ -239,7 +239,7 @@ static void put_into_vehicle( Character &c, item_drop_reason reason, const std::
it.handle_pickup_ownership( c );
}

const std::string part_name = veh.part_info( part ).name();
const std::string part_name = vp.info().name();

if( same_type( items ) ) {
const item &it = items.front();
Expand Down Expand Up @@ -2736,12 +2736,15 @@ static requirement_check_result generic_multi_activity_check_requirement(
you.activity_vehicle_part_index = 1;
return requirement_check_result::SKIP_LOCATION;
}
const vpart_info &vpinfo = veh->part_info( you.activity_vehicle_part_index );
requirement_data reqs;
if( reason == do_activity_reason::NEEDS_VEH_DECONST ) {
reqs = vpinfo.removal_requirements();
} else if( reason == do_activity_reason::NEEDS_VEH_REPAIR ) {
reqs = vpinfo.repair_requirements();
if( you.activity_vehicle_part_index >= 0 &&
you.activity_vehicle_part_index < static_cast<int>( veh->part_count() ) ) {
const vpart_info &vpi = veh->part( you.activity_vehicle_part_index ).info();
if( reason == do_activity_reason::NEEDS_VEH_DECONST ) {
reqs = vpi.removal_requirements();
} else if( reason == do_activity_reason::NEEDS_VEH_REPAIR ) {
reqs = vpi.repair_requirements();
}
}
const std::string ran_str = random_string( 10 );
const requirement_id req_id( ran_str );
Expand Down
11 changes: 6 additions & 5 deletions src/lightmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1094,13 +1094,15 @@ void map::build_seen_cache( const tripoint &origin, const int target_z, int exte
}
}

for( int mirror : mirrors ) {
bool is_camera = veh->part_info( mirror ).has_flag( "CAMERA" );
for( const int mirror : mirrors ) {
const vehicle_part &vp_mirror = veh->part( mirror );
const vpart_info &vpi_mirror = vp_mirror.info();
const bool is_camera = vpi_mirror.has_flag( "CAMERA" );
if( is_camera && cam_control < 0 ) {
continue; // Player not at camera control, so cameras don't work
}

const tripoint mirror_pos = veh->global_part_pos3( mirror );
const tripoint mirror_pos = veh->global_part_pos3( vp_mirror );

// Determine how far the light has already traveled so mirrors
// don't cheat the light distance falloff.
Expand All @@ -1109,8 +1111,7 @@ void map::build_seen_cache( const tripoint &origin, const int target_z, int exte
if( !is_camera ) {
offsetDistance = penalty + rl_dist( origin, mirror_pos );
} else {
offsetDistance = 60 - veh->part_info( mirror ).bonus *
veh->part( mirror ).hp() / veh->part_info( mirror ).durability;
offsetDistance = 60 - vpi_mirror.bonus * vp_mirror.hp() / vpi_mirror.durability;
mocache = &camera_cache;
( *mocache )[mirror_pos.x][mirror_pos.y] = LIGHT_TRANSPARENCY_OPEN_AIR;
}
Expand Down
22 changes: 17 additions & 5 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ vehicle *map::move_vehicle( vehicle &veh, const tripoint &dp, const tileray &fac
// Shock damage, if the target part is a rotor treat as an aimed hit.
// don't try to deal damage to invalid part (probably removed or destroyed)
if( part_num != -1 ) {
if( veh.part_info( part_num ).rotor_diameter() > 0 ) {
if( veh.part( part_num ).info().rotor_diameter() > 0 ) {
veh.damage( *this, part_num, coll_dmg, damage_bash, true );
} else {
impulse += coll_dmg;
Expand Down Expand Up @@ -903,18 +903,30 @@ float map::vehicle_vehicle_collision( vehicle &veh, vehicle &veh2,
const veh_collision &c = collisions[0];
const bool vertical = veh.sm_pos.z != veh2.sm_pos.z;

if( c.part < 0 || c.part >= static_cast<int>( veh.part_count() ) ) {
debugmsg( "invalid c.part %d", c.part );
return 0.0f;
}

if( c.target_part < 0 || c.target_part >= static_cast<int>( veh2.part_count() ) ) {
debugmsg( "invalid c.target_part %d", c.target_part );
return 0.0f;
}
vehicle_part &vp1 = veh.part( c.part );
vehicle_part &vp2 = veh2.part( c.target_part );

// Check whether avatar sees the collision, and log a message if so
const avatar &you = get_avatar();
const tripoint part1_pos = veh.global_part_pos3( c.part );
const tripoint part2_pos = veh2.global_part_pos3( c.target_part );
const tripoint part1_pos = veh.global_part_pos3( vp1 );
const tripoint part2_pos = veh2.global_part_pos3( vp2 );
if( you.sees( part1_pos ) || you.sees( part2_pos ) ) {
//~ %1$s: first vehicle name (without "the")
//~ %2$s: first part name
//~ %3$s: second vehicle display name (with "the")
//~ %4$s: second part name
add_msg( m_bad, _( "The %1$s's %2$s collides with %3$s's %4$s." ),
veh.name, veh.part_info( c.part ).name(),
veh2.disp_name(), veh2.part_info( c.target_part ).name() );
veh.name, vp1.info().name(),
veh2.disp_name(), vp2.info().name() );
}

// Used to calculate the epicenter of the collision.
Expand Down
87 changes: 41 additions & 46 deletions src/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -831,8 +831,9 @@ void vehicle::smash( map &m, float hp_percent_loss_min, float hp_percent_loss_ma

std::vector<int> parts_in_square = parts_at_relative( part.mount, true );
int structures_found = 0;
for( int &square_part_index : parts_in_square ) {
if( part_info( square_part_index ).location == part_location_structure ) {
for( const int square_part_index : parts_in_square ) {
const vpart_info &vpi = parts[square_part_index].info();
if( vpi.location == part_location_structure ) {
structures_found++;
}
}
Expand Down Expand Up @@ -874,8 +875,8 @@ void vehicle::smash( map &m, float hp_percent_loss_min, float hp_percent_loss_ma
if( p == other_p ) {
continue;
}
const vpart_info &p_info = part_info( p );
const vpart_info &other_p_info = part_info( other_p );
const vpart_info &p_info = parts[p].info();
const vpart_info &other_p_info = parts[other_p].info();

if( p_info.id == other_p_info.id ||
( !p_info.location.empty() && p_info.location == other_p_info.location ) ) {
Expand Down Expand Up @@ -1033,16 +1034,6 @@ void vehicle::backfire( const vehicle_part &vp ) const
string_format( text, vp.name() ), true, "vehicle", "engine_backfire" );
}

const vpart_info &vehicle::part_info( int index, bool include_removed ) const
{
if( index < static_cast<int>( parts.size() ) ) {
if( !parts[index].removed || include_removed ) {
return parts[index].info();
}
}
return vpart_id::NULL_ID().obj();
}

// engines & alternators all have power.
// engines provide, whilst alternators consume.
units::power vehicle::part_vpower_w( const vehicle_part &vp, const bool at_full_hp ) const
Expand Down Expand Up @@ -1271,10 +1262,11 @@ bool vehicle::can_unmount( const int p, std::string &reason ) const
const std::vector<int> parts_here = parts_at_relative( vp_to_remove.mount, false );

// make sure there are no parts which require flags from this part
for( const int &elem : parts_here ) {
for( const std::string &flag : part_info( elem ).get_flags() ) {
for( const int elem : parts_here ) {
const vehicle_part &vp_here = parts[elem];
for( const std::string &flag : vp_here.info().get_flags() ) {
if( vp_to_remove.info().has_flag( json_flag::get( flag ).requires_flag() ) ) {
reason = string_format( _( "Remove the attached %s first." ), part_info( elem ).name() );
reason = string_format( _( "Remove the attached %s first." ), vp_here.name() );
return false;
}
}
Expand All @@ -1301,8 +1293,9 @@ bool vehicle::can_unmount( const int p, std::string &reason ) const
}

// structure parts can only be removed when no non-structure parts are on tile
for( const int &elem : parts_here ) {
if( part_info( elem ).location != part_location_structure ) {
for( const int elem : parts_here ) {
const vehicle_part &vp_here = parts[elem];
if( vp_here.info().location != part_location_structure ) {
reason = _( "Remove all other attached parts first." );
return false;
}
Expand Down Expand Up @@ -2168,11 +2161,12 @@ bool vehicle::find_and_split_vehicles( map &here, std::set<int> exclude )
const point dp = parts[test_part].mount + offset;
std::vector<int> all_neighbor_parts = parts_at_relative( dp, true );
int neighbor_struct_part = -1;
for( int p : all_neighbor_parts ) {
if( parts[p].removed ) {
for( const int p : all_neighbor_parts ) {
const vehicle_part &vp_neighbor = parts[p];
if( vp_neighbor.removed ) {
continue;
}
if( part_info( p ).location == part_location_structure ) {
if( vp_neighbor.info().location == part_location_structure ) {
neighbor_struct_part = p;
break;
}
Expand Down Expand Up @@ -2245,9 +2239,8 @@ bool vehicle::split_vehicles( map &here,
// make sure the split_part0 is a legal 0,0 part
if( split_parts.size() > 1 ) {
for( size_t sp = 0; sp < split_parts.size(); sp++ ) {
int p = split_parts[ sp ];
if( part_info( p ).location == part_location_structure &&
!part_info( p ).has_flag( "PROTRUSION" ) ) {
const vpart_info &vpi_split = parts[split_parts[sp]].info();
if( vpi_split.location == part_location_structure && !vpi_split.has_flag( "PROTRUSION" ) ) {
split_part0 = sp;
break;
}
Expand Down Expand Up @@ -2931,20 +2924,23 @@ std::vector<std::vector<int>> vehicle::find_lines_of_parts(
// start from the real part, otherwise it fails in certain orientations
part = get_non_fake_part( part );

vpart_id part_id = part_info( part ).id;
const vehicle_part &vp = parts[part];
const vpart_id &part_id = vp.info().id;
const point target = vp.mount;
// create vectors of parts on the same X or Y axis
point target = parts[ part ].mount;
for( const vpart_reference &vp : possible_parts ) {
if( vp.part().is_unavailable() ||
!vp.has_feature( "MULTISQUARE" ) ||
vp.info().id != part_id ) {
for( const vpart_reference &vpr : possible_parts ) {
const vehicle_part &vp_other = vpr.part();
const vpart_info &vpi_other = vp_other.info();
if( vp_other.is_unavailable() ||
!vpi_other.has_flag( "MULTISQUARE" ) ||
vpi_other.id != part_id ) {
continue;
}
if( vp.mount().x == target.x ) {
x_parts.push_back( vp.part_index() );
if( vp_other.mount.x == target.x ) {
x_parts.push_back( vpr.part_index() );
}
if( vp.mount().y == target.y ) {
y_parts.push_back( vp.part_index() );
if( vp_other.mount.y == target.y ) {
y_parts.push_back( vpr.part_index() );
}
}

Expand Down Expand Up @@ -4381,7 +4377,7 @@ bool vehicle::sufficient_wheel_config() const
return false;
} else if( wheelcache.size() == 1 ) {
//Has to be a stable wheel, and one wheel can only support a 1-3 tile vehicle
if( !part_info( wheelcache.front() ).has_flag( "STABLE" ) ||
if( !part( wheelcache[0] ).info().has_flag( "STABLE" ) ||
all_parts_at_location( part_location_structure ).size() > 3 ) {
return false;
}
Expand Down Expand Up @@ -5924,7 +5920,7 @@ void vehicle::refresh( const bool remove_fakes )
struct sort_veh_part_vector {
vehicle *veh;
inline bool operator()( const int p1, const int p2 ) const {
return veh->part_info( p1 ).list_order < veh->part_info( p2 ).list_order;
return veh->part( p1 ).info().list_order < veh->part( p2 ).info().list_order;
}
} svpv = { this };

Expand Down Expand Up @@ -7074,28 +7070,27 @@ int vehicle::break_off( map &here, int p, int dmg )

bool vehicle::explode_fuel( int p, const damage_type_id &type )
{
const itype_id &ft = part_info( p ).fuel_type;
vehicle_part &vp = part( p );
const itype_id &ft = vp.info().fuel_type;
item fuel = item( ft );
if( !fuel.has_explosion_data() ) {
return false;
}
const fuel_explosion_data &data = fuel.get_explosion_data();

if( parts[ p ].is_broken() ) {
leak_fuel( parts[ p ] );
if( vp.is_broken() ) {
leak_fuel( vp );
}

int explosion_chance = type == damage_heat ? data.explosion_chance_hot :
data.explosion_chance_cold;
if( one_in( explosion_chance ) ) {
get_event_bus().send<event_type::fuel_tank_explodes>( name );
const int pow = 120 * ( 1 - std::exp( data.explosion_factor / -5000 *
( parts[p].ammo_remaining() * data.fuel_size_factor ) ) );
//debugmsg( "damage check dmg=%d pow=%d amount=%d", dmg, pow, parts[p].amount );

explosion_handler::explosion( nullptr, global_part_pos3( p ), pow, 0.7, data.fiery_explosion );
mod_hp( parts[p], -parts[ p ].hp() );
parts[p].ammo_unset();
( vp.ammo_remaining() * data.fuel_size_factor ) ) );
explosion_handler::explosion( nullptr, global_part_pos3( vp ), pow, 0.7, data.fiery_explosion );
mod_hp( vp, -vp.hp() );
vp.ammo_unset();
}

return true;
Expand Down
7 changes: 2 additions & 5 deletions src/vehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -737,8 +737,8 @@ class vpart_display
* call `map::veh_at()`, and check vehicle type (`veh_null`
* means there's no vehicle there).
* - Vehicle consists of parts (represented by vector). Parts have some
* constant info: see veh_type.h, `vpart_info` structure and
* vpart_list array -- that is accessible through `part_info()` method.
* constant info: see veh_type.h, `vpart_info` structure that is accessible
* through `vehicle_part::info()` method.
* The second part is variable info, see `vehicle_part` structure.
* - Parts are mounted at some point relative to vehicle position (or starting part)
* (`0, 0` in mount coordinates). There can be more than one part at
Expand Down Expand Up @@ -1009,9 +1009,6 @@ class vehicle
// Engine backfire, making a loud noise
void backfire( const vehicle_part &vp ) const;

// get vpart type info for part number (part at given vector index)
const vpart_info &part_info( int index, bool include_removed = false ) const;

/**
* @param dp The coordinate to mount at (in vehicle mount point coords)
* @param vpi The part type to check
Expand Down
5 changes: 3 additions & 2 deletions src/vehicle_move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2162,12 +2162,13 @@ units::angle map::shake_vehicle( vehicle &veh, const int velocity_before,
}
move_resist = std::max( 100, pet_resist );
}
if( veh.part_with_feature( ps, VPFLAG_SEATBELT, true ) == -1 ) {
const int belt_idx = veh.part_with_feature( ps, VPFLAG_SEATBELT, true );
if( belt_idx == -1 ) {
///\EFFECT_STR reduces chance of being thrown from your seat when not wearing a seatbelt
throw_from_seat = d_vel * rng( 80, 120 ) > move_resist;
} else {
// Reduce potential damage based on quality of seatbelt
dmg -= veh.part_info( veh.part_with_feature( ps, VPFLAG_SEATBELT, true ) ).bonus;
dmg -= veh.part( belt_idx ).info().bonus;
}

// Damage passengers if d_vel is too high
Expand Down
Loading

0 comments on commit 99d63fe

Please sign in to comment.