From a5ad96c3300657116b0fd7caf622fc6164105ae2 Mon Sep 17 00:00:00 2001 From: irwiss Date: Tue, 11 Jul 2023 14:49:07 +0300 Subject: [PATCH] Make vehicle::handle_trap use references --- src/grab.cpp | 5 +++-- src/map.cpp | 16 +++++++++------- src/vehicle.h | 2 +- src/vehicle_move.cpp | 13 ++++++------- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/grab.cpp b/src/grab.cpp index f8925e24ce2cf..5a18753a42f51 100644 --- a/src/grab.cpp +++ b/src/grab.cpp @@ -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 ); } } diff --git a/src/map.cpp b/src/map.cpp index 692842f61d363..1e69df254714a 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -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( ( ( 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() ) ); diff --git a/src/vehicle.h b/src/vehicle.h index 18732c0c5a93b..2958f8473ea44 100644 --- a/src/vehicle.h +++ b/src/vehicle.h @@ -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(); /** diff --git a/src/vehicle_move.cpp b/src/vehicle_move.cpp index 84a2588fcecfa..4095252a8ab29 100644 --- a/src/vehicle_move.cpp +++ b/src/vehicle_move.cpp @@ -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(); @@ -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; @@ -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() ); } } @@ -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;