Skip to content

Commit

Permalink
Merge pull request #34740 from mlangsdorf/multiracks
Browse files Browse the repository at this point in the history
vehicles: allow multiple vehicles on a bike rack
  • Loading branch information
ZhilkinSerg authored Oct 18, 2019
2 parents e31b762 + ee3e2af commit 1471f08
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/vehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ struct vehicle_part {
/** Can a player or NPC use this part as a seat? */
bool is_seat() const;

/* if this is a carried part, what is the name of the carried vehicle */
std::string carried_name() const;
/*@}*/

public:
Expand Down
3 changes: 1 addition & 2 deletions src/vehicle_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,8 @@ void vehicle::print_vparts_descs( const catacurses::window &win, int max_y, int
new_lines += 1;
}
if( vp.has_flag( vehicle_part::carried_flag ) ) {
std::string carried_name = vp.carry_names.top();
possible_msg << string_format( " Part of a %s carried on a rack.\n",
carried_name.substr( vehicle_part::name_offset ) );
vp.carried_name() );
new_lines += 1;
}

Expand Down
8 changes: 8 additions & 0 deletions src/vehicle_part.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,3 +571,11 @@ bool vehicle::assign_seat( vehicle_part &pt, const npc &who )

return true;
}

std::string vehicle_part::carried_name() const
{
if( carry_names.empty() ) {
return std::string();
}
return carry_names.top().substr( name_offset );
}
63 changes: 50 additions & 13 deletions src/vehicle_use.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1718,25 +1718,50 @@ void vehicle::use_bike_rack( int part )
}

// check if we're storing a vehicle on this rack
std::vector<int> carried_parts;
std::vector<int> carry_rack;
std::vector<std::vector<int>> carried_vehicles;
std::vector<std::vector<int>> carrying_racks;
bool found_vehicle = false;
for( const auto &rack_parts : racks_parts ) {
for( auto rack_part : rack_parts ) {
bool full_rack = true;
for( const std::vector<int> &rack_parts : racks_parts ) {
std::vector<int> carried_parts;
std::vector<int> carry_rack;
size_t carry_size = 0;
std::string cur_vehicle;

const auto add_vehicle = []( std::vector<int> &carried_parts,
std::vector<std::vector<int>> &carried_vehicles,
std::vector<int> &carry_rack,
std::vector<std::vector<int>> &carrying_racks ) {
if( !carry_rack.empty() ) {
carrying_racks.emplace_back( carry_rack );
carried_vehicles.emplace_back( carried_parts );
carry_rack.clear();
carried_parts.clear();
}
};

for( const int &rack_part : rack_parts ) {
// skip parts that aren't carrying anything
if( !parts[ rack_part ].has_flag( vehicle_part::carrying_flag ) ) {
add_vehicle( carried_parts, carried_vehicles, carry_rack, carrying_racks );
cur_vehicle.clear();
continue;
}
for( int i = 0; i < 4; i++ ) {
point near_loc = parts[ rack_part ].mount + vehicles::cardinal_d[ i ];
for( const point &mount_dir : vehicles::cardinal_d ) {
point near_loc = parts[ rack_part ].mount + mount_dir;
std::vector<int> near_parts = parts_at_relative( near_loc, true );
if( near_parts.empty() ) {
continue;
}
if( parts[ near_parts[ 0 ] ].has_flag( vehicle_part::carried_flag ) ) {
carry_size += 1;
found_vehicle = true;
// found a carried vehicle part
for( auto carried_part : near_parts ) {
if( parts[ near_parts[ 0 ] ].carried_name() != cur_vehicle ) {
add_vehicle( carried_parts, carried_vehicles, carry_rack, carrying_racks );
cur_vehicle = parts[ near_parts[ 0 ] ].carried_name();
}
for( const int &carried_part : near_parts ) {
carried_parts.push_back( carried_part );
}
carry_rack.push_back( rack_part );
Expand All @@ -1745,15 +1770,27 @@ void vehicle::use_bike_rack( int part )
}
}
}
if( found_vehicle ) {
break;
}

add_vehicle( carried_parts, carried_vehicles, carry_rack, carrying_racks );
full_rack &= carry_size == rack_parts.size();
}
int unload_carried = full_rack ? 0 : -1;
bool success = false;
if( found_vehicle ) {
success = remove_carried_vehicle( carried_parts );
if( found_vehicle && !full_rack ) {
uilist rack_menu;
rack_menu.addentry( 0, true, '0', _( "Load a vehicle on the rack" ) );
for( size_t i = 0; i < carried_vehicles.size(); i++ ) {
rack_menu.addentry( i + 1, true, '1' + i,
string_format( _( "Remove the %s from the rack" ),
parts[ carried_vehicles[i].front() ].carried_name() ) );
}
rack_menu.query();
unload_carried = rack_menu.ret - 1;
}
if( unload_carried > -1 ) {
success = remove_carried_vehicle( carried_vehicles[unload_carried] );
if( success ) {
for( auto rack_part : carry_rack ) {
for( const int &rack_part : carrying_racks[unload_carried] ) {
parts[ rack_part ].remove_flag( vehicle_part::carrying_flag );
}
}
Expand Down

0 comments on commit 1471f08

Please sign in to comment.