Skip to content

Commit

Permalink
Fix vehicles going glitchy when parts are added (#3550)
Browse files Browse the repository at this point in the history
* Fix part list resize bug

* style(autofix.ci): automated formatting

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
joveeater and autofix-ci[bot] authored Nov 4, 2023
1 parent eb8ffb2 commit d82fd66
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ void vehicle::add_missing_frames()
}
if( !found ) {
// Install missing frame
//TODO!: check
parts.emplace_back( frame_id, i.mount, item::spawn( frame_id->item ), this );
refresh_locations_hack();
}
}
}
Expand Down Expand Up @@ -1625,6 +1625,7 @@ int vehicle::install_part( point dp, vehicle_part &&new_part )
}

parts.push_back( std::move( new_part ) );
refresh_locations_hack();
auto &pt = parts.back();
pt.set_vehicle_hack( this );

Expand Down Expand Up @@ -1782,6 +1783,7 @@ bool vehicle::merge_rackable_vehicle( vehicle *carry_veh, const std::vector<int>
for( int carry_part : carry_map.carry_parts_here ) {
//TODO!: check that the carry veh is really destroyed after this
parts.push_back( std::move( carry_veh->parts[ carry_part ] ) );
refresh_locations_hack();
vehicle_part &carried_part = parts.back();

carried_part.mount = carry_map.carry_mount;
Expand Down Expand Up @@ -2326,6 +2328,7 @@ bool vehicle::split_vehicles( const std::vector<std::vector <int>> &new_vehs,
}
// transfer the vehicle_part to the new vehicle
new_vehicle->parts.emplace_back( std::move( parts[ mov_part ] ) );
new_vehicle->refresh_locations_hack();
vehicle_part &np = new_vehicle->parts.back();
np.mount = new_mount;
np.set_vehicle_hack( new_vehicle );
Expand Down Expand Up @@ -7215,6 +7218,13 @@ bool vehicle::is_loaded() const
return attached && get_map().inbounds( global_pos3() );
}

void vehicle::refresh_locations_hack()
{
for( vehicle_part &part : parts ) {
part.refresh_locations_hack( this );
}
}

vehicle_part &vehicle::get_part_hack( int id )
{
for( vehicle_part &part : parts ) {
Expand Down
1 change: 1 addition & 0 deletions src/vehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ class vehicle

vehicle_part &get_part_hack( int );
int get_part_id_hack( int );
void refresh_locations_hack();

int get_next_hack_id() {
return next_hack_id++;
Expand Down
6 changes: 6 additions & 0 deletions src/vehicle_part.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ vehicle_part::vehicle_part( const vpart_id &vp, point dp, detached_ptr<item> &&o
void vehicle_part::set_vehicle_hack( vehicle *veh )
{
hack_id = veh->get_next_hack_id();
refresh_locations_hack( veh );
}

void vehicle_part::refresh_locations_hack( vehicle *veh )
{
base.set_loc_hack( new vehicle_base_item_location( veh, hack_id ) );
items.set_loc_hack( new vehicle_item_location( veh, hack_id ) );
}
Expand All @@ -88,6 +93,7 @@ void vehicle_part::copy_static_from( const vehicle_part &source )
info_cache = source.info_cache;
ammo_pref = source.ammo_pref;
crew_id = source.crew_id;
hack_id = source.hack_id;
}

//TODO!: This is a bit scuffed and will be until vehicles are game objects.
Expand Down
1 change: 1 addition & 0 deletions src/vehicle_part.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ struct vehicle_part {

/** this can be removed when vehicles are made into GOs */
void set_vehicle_hack( vehicle * );
void refresh_locations_hack( vehicle * );

/**
* Translated name of a part inclusive of any current status effects
Expand Down

0 comments on commit d82fd66

Please sign in to comment.