Skip to content

Commit

Permalink
Merge pull request #66816 from irwiss/veh-cleanups-2
Browse files Browse the repository at this point in the history
Migrate some vehicle code from part indexes to part references pt2
  • Loading branch information
dseguin authored Jul 12, 2023
2 parents 0392482 + 9039d5f commit 00ebda7
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 164 deletions.
9 changes: 4 additions & 5 deletions src/activity_item_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1045,15 +1045,14 @@ static activity_reason_info can_do_activity_there( const activity_id &act, Chara
std::vector<vehicle_part *> parts =
veh->get_parts_at( src_loc.raw(), "", part_status_flag::any );
for( vehicle_part *part_elem : parts ) {
const vpart_info &vpinfo = part_elem->info();
int vpindex = veh->index_of_part( part_elem, true );
const int vpindex = veh->index_of_part( part_elem, true );
// if part is not on this vehicle, or if its attached to another part that needs to be removed first.
if( vpindex == -1 || !veh->can_unmount( vpindex ) ) {
if( vpindex < 0 || !veh->can_unmount( *part_elem ).success() ) {
continue;
}
const vpart_info &vpinfo = part_elem->info();
// If removing this part would make the vehicle non-flyable, avoid it
if( veh->would_removal_prevent_flyable( *part_elem,
player_character ) ) {
if( veh->would_removal_prevent_flyable( *part_elem, player_character ) ) {
return activity_reason_info::fail( do_activity_reason::WOULD_PREVENT_VEH_FLYING );
}
// this is the same part that somebody else wants to work on, or already is.
Expand Down
5 changes: 3 additions & 2 deletions src/grab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,9 @@ bool game::grabbed_veh_move( const tripoint &dp )

for( int p : wheel_indices ) {
if( one_in( 2 ) ) {
tripoint wheel_p = grabbed_vehicle->global_part_pos3( grabbed_part );
grabbed_vehicle->handle_trap( wheel_p, p );
vehicle_part &vp_wheel = grabbed_vehicle->part( p );
tripoint wheel_p = grabbed_vehicle->global_part_pos3( vp_wheel );
grabbed_vehicle->handle_trap( wheel_p, vp_wheel );
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/item_location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,10 @@ class item_location::impl::item_on_vehicle : public item_location::impl

void remove_item() override {
on_contents_changed();
item &base = cur.veh.part( cur.part ).base;
vehicle_part &vp = cur.veh.part( cur.part );
item &base = vp.base;
if( &base == target() ) {
cur.veh.remove_part( cur.part ); // vehicle_part::base
cur.veh.remove_part( vp ); // vehicle_part::base
} else {
cur.remove_item( *target() ); // item within CARGO
}
Expand Down
16 changes: 9 additions & 7 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,22 +849,24 @@ vehicle *map::move_vehicle( vehicle &veh, const tripoint &dp, const tileray &fac
const float weight_to_damage_factor = 0.05f; // Nobody likes a magic number.
const float vehicle_mass_kg = to_kilogram( veh.total_mass() );

for( const int &w : wheel_indices ) {
const tripoint wheel_p = veh.global_part_pos3( w );
for( const int vp_wheel_idx : wheel_indices ) {
vehicle_part &vp_wheel = veh.part( vp_wheel_idx );
const vpart_info &vpi_wheel = vp_wheel.info();
const tripoint wheel_p = veh.global_part_pos3( vp_wheel );
if( one_in( 2 ) && displace_water( wheel_p ) ) {
sounds::sound( wheel_p, 4, sounds::sound_t::movement, _( "splash!" ), false,
"environment", "splash" );
}

veh.handle_trap( wheel_p, w );
if( !has_flag( ter_furn_flag::TFLAG_SEALED, wheel_p ) ) {
const float wheel_area = veh.part( w ).wheel_area();
veh.handle_trap( wheel_p, vp_wheel );
// dont use vp_wheel or vp_wheel_idx below this - handle_trap might've removed it from parts

if( !has_flag( ter_furn_flag::TFLAG_SEALED, wheel_p ) ) {
// Damage is calculated based on the weight of the vehicle,
// The area of it's wheels, and the area of the wheel running over the items.
// This number is multiplied by weight_to_damage_factor to get reasonable results, damage-wise.
const int wheel_damage = static_cast<int>( ( ( wheel_area / vehicle_grounded_wheel_area ) *
vehicle_mass_kg ) * weight_to_damage_factor );
const int wheel_damage = vpi_wheel.wheel_area() / vehicle_grounded_wheel_area *
vehicle_mass_kg * weight_to_damage_factor;

//~ %1$s: vehicle name
smash_items( wheel_p, wheel_damage, string_format( _( "weight of %1$s" ), veh.disp_name() ) );
Expand Down
5 changes: 3 additions & 2 deletions src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6735,10 +6735,11 @@ std::unique_ptr<vehicle> map::add_vehicle_to_map(
std::vector<int> parts_in_square = veh_to_add->parts_at_relative( source_point, true );
std::set<int> parts_to_check;
for( int index = parts_in_square.size() - 1; index >= 0; index-- ) {
vehicle_part &vp = veh_to_add->part( parts_in_square[index] );
if( handler_ptr ) {
veh_to_add->remove_part( parts_in_square[index], *handler_ptr );
veh_to_add->remove_part( vp, *handler_ptr );
} else {
veh_to_add->remove_part( parts_in_square[index] );
veh_to_add->remove_part( vp );
}
parts_to_check.insert( parts_in_square[index] );
}
Expand Down
15 changes: 8 additions & 7 deletions src/veh_interact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,8 @@ task_reason veh_interact::cant_do( char mode )
// remove mode
enough_morale = player_character.has_morale_to_craft();
valid_target = cpart >= 0;
part_free = parts_here.size() > 1 || ( cpart >= 0 && veh->can_unmount( cpart ) );
part_free = parts_here.size() > 1 ||
( cpart >= 0 && veh->can_unmount( veh->part( cpart ) ).success() );
//tool and skill checks processed later
has_tools = true;
has_skill = true;
Expand Down Expand Up @@ -1785,10 +1786,10 @@ bool veh_interact::can_remove_part( int idx, const Character &you )
}
nmsg += res.second;

std::string reason;
if( !veh->can_unmount( idx, reason ) ) {
const ret_val<void> unmount = veh->can_unmount( *sel_vehicle_part );
if( !unmount.success() ) {
//~ %1$s represents the internal color name which shouldn't be translated, %2$s is pre-translated reason
nmsg += string_format( _( "> %1$s%2$s</color>" ), status_color( false ), reason ) + "\n";
nmsg += string_format( _( "> %1$s%2$s</color>" ), status_color( false ), unmount.str() ) + "\n";
ok = false;
}
const nc_color desc_color = sel_vehicle_part->is_broken() ? c_dark_gray : c_light_gray;
Expand Down Expand Up @@ -3273,7 +3274,7 @@ void veh_interact::complete_vehicle( Character &you )
return;
}
}
const vehicle_part &vp = veh.part( vp_index );
vehicle_part &vp = veh.part( vp_index );
const vpart_info &vpi = vp.info();
const bool appliance_removal = static_cast<char>( you.activity.index ) == 'O';
const bool wall_wire_removal = appliance_removal && vpi.id == vpart_ap_wall_wiring;
Expand Down Expand Up @@ -3305,7 +3306,7 @@ void veh_interact::complete_vehicle( Character &you )

// Power cables must remove parts from the target vehicle, too.
if( vpi.has_flag( "POWER_TRANSFER" ) ) {
veh.remove_remote_part( vp_index );
veh.remove_remote_part( vp );
}

if( broken ) {
Expand Down Expand Up @@ -3352,7 +3353,7 @@ void veh_interact::complete_vehicle( Character &you )
here.destroy_vehicle( &veh );
} else {
const tripoint part_pos = veh.global_part_pos3( vp );
veh.remove_part( vp_index );
veh.remove_part( vp );
// part_removal_cleanup calls refresh, so parts_at_relative is valid
veh.part_removal_cleanup();
if( veh.parts_at_relative( vp.mount, true ).empty() ) {
Expand Down
3 changes: 1 addition & 2 deletions src/veh_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ vehicle_part *most_repairable_part( vehicle &veh, Character &who )

bool repair_part( vehicle &veh, vehicle_part &pt, Character &who )
{
int part_index = veh.index_of_part( &pt );
const vpart_info &vp = pt.info();

const requirement_data reqs = pt.is_broken()
Expand Down Expand Up @@ -141,7 +140,7 @@ bool repair_part( vehicle &veh, vehicle_part &pt, Character &who )
const units::angle direction = pt.direction;
const std::string variant = pt.variant;
get_map().spawn_items( who.pos(), pt.pieces_for_broken_part() );
veh.remove_part( part_index );
veh.remove_part( pt );
const int partnum = veh.install_part( mount, vpid, std::move( base ) );
if( partnum >= 0 ) {
vehicle_part &vp = veh.part( partnum );
Expand Down
Loading

0 comments on commit 00ebda7

Please sign in to comment.