Skip to content

Commit

Permalink
Fix tow cables crashing and not keeping connections properly (CleverR…
Browse files Browse the repository at this point in the history
…aven#71482)

* Add check for valid part index

* Keep tow cable linked when disconnecting one end
  • Loading branch information
Kamayana authored Feb 5, 2024
1 parent 67626c7 commit ea08bf0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
7 changes: 5 additions & 2 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13244,8 +13244,11 @@ ret_val<void> item::link_to( vehicle &veh, const point &mount, link_state link_t
return ret_val<void>::make_failure( _( "That vehicle already has a tow-line attached." ) );
} else if( !veh.is_external_part( veh.mount_to_tripoint( mount ) ) ) {
return ret_val<void>::make_failure( _( "You can't attach a tow-line to an internal part." ) );
} else if( !veh.part( veh.part_at( mount ) ).carried_stack.empty() ) {
return ret_val<void>::make_failure( _( "You can't attach a tow-line to a racked part." ) );
} else {
const int part_at = veh.part_at( mount );
if( part_at != -1 && !veh.part( part_at ).carried_stack.empty() ) {
return ret_val<void>::make_failure( _( "You can't attach a tow-line to a racked part." ) );
}
}
} else {
const link_up_actor *it_actor = static_cast<const link_up_actor *>
Expand Down
22 changes: 13 additions & 9 deletions src/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6741,17 +6741,25 @@ void vehicle::invalidate_towing( bool first_vehicle, Character *remover )
}
const int tow_cable_idx = get_tow_part();
if( first_vehicle ) {
vehicle *other_veh = is_towing() ? tow_data.get_towed() :
const bool first_veh_is_towing = is_towing();
vehicle *other_veh = first_veh_is_towing ? tow_data.get_towed() :
is_towed() ? tow_data.get_towed_by() : nullptr;
const int other_tow_cable_idx = other_veh ? other_veh->get_tow_part() : -1;
const point other_tow_cable_mount = other_veh && other_tow_cable_idx > -1 ?
other_veh->part( other_tow_cable_idx ).mount : point();
if( other_veh ) {
other_veh->invalidate_towing();
}
if( tow_cable_idx > -1 ) {
vehicle_part &vp = parts[tow_cable_idx];
item drop = part_to_item( vp );
drop.set_damage( 0 );
const int other_tow_cable_idx = other_veh ? other_veh->get_tow_part() : -1;
tripoint drop_pos = global_part_pos3( vp );
remove_part( vp );
if( other_tow_cable_idx > -1 ) {
drop.reset_link( false );
drop.link_to( *other_veh, other_veh->part( other_tow_cable_idx ).mount );
if( is_towing() ) {
drop.link_to( *other_veh, other_tow_cable_mount, link_state::vehicle_tow );
if( first_veh_is_towing ) {
drop.link().source = link_state::no_link;
drop.link().target = link_state::vehicle_tow;
} else {
Expand All @@ -6770,12 +6778,8 @@ void vehicle::invalidate_towing( bool first_vehicle, Character *remover )
put_into_vehicle_or_drop( *remover, item_drop_reason::deliberate, drops );
}
} else {
get_map().add_item_or_charges( global_part_pos3( vp ), drop );
get_map().add_item_or_charges( drop_pos, drop );
}
remove_part( vp );
}
if( other_veh ) {
other_veh->invalidate_towing();
}
tow_data.clear_towing();
} else {
Expand Down

0 comments on commit ea08bf0

Please sign in to comment.