Skip to content

Commit

Permalink
Merge pull request CleverRaven#66796 from irwiss/veh-cleanups-1
Browse files Browse the repository at this point in the history
Migrate some vehicle code from part indexes to part references
  • Loading branch information
dseguin authored Jul 11, 2023
2 parents 7f44c58 + 99d63fe commit 4ceb3e2
Show file tree
Hide file tree
Showing 12 changed files with 379 additions and 393 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
54 changes: 33 additions & 21 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,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 @@ -905,18 +905,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 Expand Up @@ -5224,40 +5236,40 @@ static bool process_map_items( map &here, item_stack &items, safe_reference<item

static void process_vehicle_items( vehicle &cur_veh, int part )
{
bool washing_machine_finished = false;
vehicle_part &vp = cur_veh.part( part );
const vpart_info &vpi = vp.info();

const bool washer_here = cur_veh.part( part ).enabled &&
( cur_veh.part_flag( part, VPFLAG_WASHING_MACHINE ) ||
cur_veh.part_flag( part, VPFLAG_DISHWASHER ) );
const bool washer_here = vp.enabled &&
( vpi.has_flag( VPFLAG_WASHING_MACHINE ) ||
vpi.has_flag( VPFLAG_DISHWASHER ) );

if( washer_here ) {
bool washing_machine_finished = false;
for( item &n : cur_veh.get_items( part ) ) {
const time_duration washing_time = 90_minutes;
const time_duration time_left = washing_time - n.age();
if( time_left <= 0_turns ) {
n.unset_flag( flag_FILTHY );
washing_machine_finished = true;
cur_veh.part( part ).enabled = false;
vp.enabled = false;
} else if( calendar::once_every( 15_minutes ) ) {
//~ %1$d: Number of minutes remaining, %2$s: Name of the vehicle
add_msg( _( "It should take %1$d minutes to finish washing items in the %2$s." ),
to_minutes<int>( time_left ) + 1, cur_veh.name );
break;
}
}
if( washing_machine_finished && !cur_veh.part_flag( part, VPFLAG_APPLIANCE ) ) {
if( washing_machine_finished && !vpi.has_flag( VPFLAG_APPLIANCE ) ) {
//~ %1$s: Cleaner, %2$s: Name of the vehicle
add_msg( _( "The %1$s in the %2$s has finished washing." ), cur_veh.part( part ).name( false ),
cur_veh.name );
add_msg( _( "The %1$s in the %2$s has finished washing." ), vp.name( false ), cur_veh.name );
} else if( washing_machine_finished ) {
add_msg( _( "The %1$s has finished washing." ), cur_veh.part( part ).name( false ) );
add_msg( _( "The %1$s has finished washing." ), vp.name( false ) );
}
}

const bool autoclave_here = cur_veh.part_flag( part, VPFLAG_AUTOCLAVE ) &&
cur_veh.part( part ).enabled;
bool autoclave_finished = false;
const bool autoclave_here = vpi.has_flag( VPFLAG_AUTOCLAVE ) && vp.enabled;
if( autoclave_here ) {
bool autoclave_finished = false;
for( item &n : cur_veh.get_items( part ) ) {
const time_duration cycle_time = 90_minutes;
const time_duration time_left = cycle_time - n.age();
Expand All @@ -5266,7 +5278,7 @@ static void process_vehicle_items( vehicle &cur_veh, int part )
n.unset_flag( flag_NO_STERILE );
}
autoclave_finished = true;
cur_veh.part( part ).enabled = false;
vp.enabled = false;
} else if( calendar::once_every( 15_minutes ) ) {
const int minutes = to_minutes<int>( time_left ) + 1;
//~ %1$d: Number of minutes remaining, %2$s: Name of the vehicle
Expand All @@ -5276,7 +5288,7 @@ static void process_vehicle_items( vehicle &cur_veh, int part )
break;
}
}
if( autoclave_finished && !cur_veh.part_flag( part, VPFLAG_APPLIANCE ) ) {
if( autoclave_finished && !vpi.has_flag( VPFLAG_APPLIANCE ) ) {
add_msg( _( "The autoclave in the %s has finished its cycle." ), cur_veh.name );
} else if( autoclave_finished ) {
add_msg( _( "The autoclave has finished its cycle." ) );
Expand All @@ -5285,8 +5297,8 @@ static void process_vehicle_items( vehicle &cur_veh, int part )

const int recharge_part_idx = cur_veh.part_with_feature( part, VPFLAG_RECHARGE, true );
if( recharge_part_idx >= 0 ) {
vehicle_part recharge_part = cur_veh.part( recharge_part_idx );
if( !recharge_part.removed && !recharge_part.is_broken() && recharge_part.enabled ) {
const vehicle_part &recharge_part = cur_veh.part( recharge_part_idx );
if( !recharge_part.removed && recharge_part.enabled ) {
for( item &n : cur_veh.get_items( part ) ) {
if( !n.has_flag( flag_RECHARGE ) && !n.has_flag( flag_USE_UPS ) ) {
continue;
Expand Down
3 changes: 2 additions & 1 deletion src/pathfinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ std::vector<tripoint> map::route( const tripoint &f, const tripoint &t,

newg += 2 * hp / bash + 8 + 4;
} else if( part >= 0 ) {
if( !doors || !veh->part_flag( part, VPFLAG_OPENABLE ) ) {
const vehicle_part &vp = veh->part( part );
if( !doors || !vp.info().has_flag( VPFLAG_OPENABLE ) ) {
// Won't be openable, don't try from other sides
layer.state[index] = ASL_CLOSED;
}
Expand Down
2 changes: 1 addition & 1 deletion src/veh_appliance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ bool veh_app_interact::can_unplug()
{
vehicle_part_range vpr = veh->get_all_parts();
return std::any_of( vpr.begin(), vpr.end(), []( const vpart_reference & ref ) {
return ref.vehicle().part_flag( static_cast<int>( ref.part_index() ), "POWER_TRANSFER" );
return ref.info().has_flag( "POWER_TRANSFER" );
} );
}

Expand Down
Loading

0 comments on commit 4ceb3e2

Please sign in to comment.