Skip to content

Commit

Permalink
Make vehicle::handle_trap use references
Browse files Browse the repository at this point in the history
  • Loading branch information
irwiss committed Jul 11, 2023
1 parent 8777f1c commit a5ad96c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
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
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
2 changes: 1 addition & 1 deletion src/vehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -1721,7 +1721,7 @@ class vehicle
bool just_detect, bool bash_floor );

// Process the trap beneath
void handle_trap( const tripoint &p, int part );
void handle_trap( const tripoint &p, vehicle_part &vp_wheel );
void activate_magical_follow();
void activate_animal_follow();
/**
Expand Down
13 changes: 6 additions & 7 deletions src/vehicle_move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1201,10 +1201,10 @@ veh_collision vehicle::part_collision( int part, const tripoint &p,
return ret;
}

void vehicle::handle_trap( const tripoint &p, int part )
void vehicle::handle_trap( const tripoint &p, vehicle_part &vp_wheel )
{
int pwh = part_with_feature( part, VPFLAG_WHEEL, true );
if( pwh < 0 ) {
if( !vp_wheel.info().has_flag( VPFLAG_WHEEL ) ) {
debugmsg( "vehicle::handle_trap called on non-WHEEL part" );
return;
}
map &here = get_map();
Expand All @@ -1214,7 +1214,7 @@ void vehicle::handle_trap( const tripoint &p, int part )
// If the trap doesn't exist, we can't interact with it, so just return
return;
}
vehicle_handle_trap_data veh_data = tr.vehicle_data;
const vehicle_handle_trap_data &veh_data = tr.vehicle_data;

if( veh_data.is_falling ) {
return;
Expand All @@ -1226,9 +1226,9 @@ void vehicle::handle_trap( const tripoint &p, int part )
if( seen ) {
if( known ) {
//~ %1$s: name of the vehicle; %2$s: name of the related vehicle part; %3$s: trap name
add_msg( m_bad, _( "The %1$s's %2$s runs over %3$s." ), name, parts[ part ].name(), tr.name() );
add_msg( m_bad, _( "The %1$s's %2$s runs over %3$s." ), name, vp_wheel.name(), tr.name() );
} else {
add_msg( m_bad, _( "The %1$s's %2$s runs over something." ), name, parts[ part ].name() );
add_msg( m_bad, _( "The %1$s's %2$s runs over something." ), name, vp_wheel.name() );
}
}

Expand All @@ -1242,7 +1242,6 @@ void vehicle::handle_trap( const tripoint &p, int part )
explosion_handler::explosion( source, p, veh_data.damage, 0.5f, false, veh_data.shrapnel );
} else {
// Hit the wheel directly since it ran right over the trap.
vehicle_part &vp_wheel = parts[part];
damage_direct( here, vp_wheel, veh_data.damage );
}
bool still_has_trap = true;
Expand Down

0 comments on commit a5ad96c

Please sign in to comment.