Skip to content

Commit

Permalink
Merge pull request #77008 from PatrikLundell/vehicle
Browse files Browse the repository at this point in the history
typified a bit of vehicle.h + dependents
  • Loading branch information
Maleclypse authored Nov 2, 2024
2 parents e9980fd + 0b91286 commit d8f797e
Show file tree
Hide file tree
Showing 24 changed files with 647 additions and 296 deletions.
4 changes: 2 additions & 2 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1775,12 +1775,12 @@ void game::validate_linked_vehicles()
{
for( wrapped_vehicle &veh : m.get_vehicles() ) {
vehicle *v = veh.v;
if( v->tow_data.other_towing_point != tripoint_zero ) {
if( v->tow_data.other_towing_point != tripoint_bub_ms_zero ) {
vehicle *other_v = veh_pointer_or_null( m.veh_at( v->tow_data.other_towing_point ) );
if( other_v ) {
// the other vehicle is towing us.
v->tow_data.set_towing( other_v, v );
v->tow_data.other_towing_point = tripoint_zero;
v->tow_data.other_towing_point = tripoint_bub_ms_zero;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/iexamine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,7 @@ elevator_vehicles _get_vehicles_on_elevator( std::vector<tripoint_bub_ms> const
bool inbounds = true;
bool can_block = false;
for( const vpart_reference &vp : v.v->get_all_parts() ) {
tripoint_bub_ms const p = tripoint_bub_ms( v.pos + vp.part().precalc[0] );
tripoint_bub_ms const p = tripoint_bub_ms( v.pos ) + vp.part().precalc[0];
auto const eit = std::find( elevator.cbegin(), elevator.cend(), p );
inbounds &= eit != elevator.cend();
if( !inbounds ) {
Expand Down
19 changes: 12 additions & 7 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13601,6 +13601,11 @@ ret_val<void> item::link_to( const optional_vpart_position &first_linked_vp,
}

ret_val<void> item::link_to( vehicle &veh, const point &mount, link_state link_type )
{
return item::link_to( veh, point_rel_ms( mount ), link_type );
}

ret_val<void> item::link_to( vehicle &veh, const point_rel_ms &mount, link_state link_type )
{
if( !can_link_up() ) {
return ret_val<void>::make_failure( _( "The %s doesn't have a cable!" ), type_name() );
Expand Down Expand Up @@ -13648,7 +13653,7 @@ ret_val<void> item::link_to( vehicle &veh, const point &mount, link_state link_t
link().target = link_type;
link().t_veh = veh.get_safe_reference();
link().t_abs_pos = get_map().getglobal( link().t_veh->pos_bub() );
link().t_mount = mount;
link().t_mount = mount.raw();
link().s_bub_pos = tripoint_min; // Forces the item to check the length during process_link.

update_link_traits();
Expand Down Expand Up @@ -13912,22 +13917,22 @@ bool item::process_link( map &here, Character *carrier, const tripoint &pos )
int link_vp_index = -1;
if( link().target == link_state::vehicle_port ) {
for( int idx : t_veh->cable_ports ) {
if( t_veh->part( idx ).mount == link().t_mount ) {
if( t_veh->part( idx ).mount.raw() == link().t_mount ) {
link_vp_index = idx;
break;
}
}
} else if( link().target == link_state::vehicle_battery ) {
for( int idx : t_veh->batteries ) {
if( t_veh->part( idx ).mount == link().t_mount ) {
if( t_veh->part( idx ).mount.raw() == link().t_mount ) {
link_vp_index = idx;
break;
}
}
if( link_vp_index == -1 ) {
// Check cable_ports, since that includes appliances
for( int idx : t_veh->cable_ports ) {
if( t_veh->part( idx ).mount == link().t_mount ) {
if( t_veh->part( idx ).mount.raw() == link().t_mount ) {
link_vp_index = idx;
break;
}
Expand Down Expand Up @@ -13960,7 +13965,7 @@ bool item::process_link( map &here, Character *carrier, const tripoint &pos )

// If either of the link's connected sides moved, check the cable's length.
if( length_check_needed ) {
link().length = rl_dist( pos, t_veh_bub_pos.raw() + t_veh->part( link_vp_index ).precalc[0] );
link().length = rl_dist( pos, t_veh_bub_pos.raw() + t_veh->part( link_vp_index ).precalc[0].raw() );
if( check_length() ) {
return reset_link( true, carrier, link_vp_index );
}
Expand Down Expand Up @@ -14065,14 +14070,14 @@ bool item::reset_link( bool unspool_if_too_long, Character *p, int vpart_index,
// Find the vp_part index the cable is linked to.
if( link().target == link_state::vehicle_port ) {
for( int idx : t_veh->cable_ports ) {
if( t_veh->part( idx ).mount == link().t_mount ) {
if( t_veh->part( idx ).mount.raw() == link().t_mount ) {
vpart_index = idx;
break;
}
}
} else if( link().target == link_state::vehicle_battery ) {
for( int idx : t_veh->batteries ) {
if( t_veh->part( idx ).mount == link().t_mount ) {
if( t_veh->part( idx ).mount.raw() == link().t_mount ) {
vpart_index = idx;
break;
}
Expand Down
3 changes: 3 additions & 0 deletions src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -1551,8 +1551,11 @@ class item : public visitable
* @param link_type What type of connection to make. If set to link_state::automatic, will automatically determine which type to use. Defaults to link_state::no_link.
* @return true if the item was successfully connected.
*/
// TODO: Get rid of untyped overload.
ret_val<void> link_to( vehicle &veh, const point &mount,
link_state link_type = link_state::no_link );
ret_val<void> link_to( vehicle &veh, const point_rel_ms &mount,
link_state link_type = link_state::no_link );

/**
* @brief Updates all parts of the item's link_data that don't have to do with its connection. Initializes the link if needed.
Expand Down
16 changes: 8 additions & 8 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ vehicle *map::move_vehicle( vehicle &veh, const tripoint_rel_ms &dp, const tiler
}
int part_num = veh.get_non_fake_part( coll.part );

const point &collision_point = veh.part( coll.part ).mount;
const point_rel_ms &collision_point = veh.part( coll.part ).mount;
const int coll_dmg = coll.imp;

// Shock damage, if the target part is a rotor treat as an aimed hit.
Expand Down Expand Up @@ -1059,8 +1059,8 @@ float map::vehicle_vehicle_collision( vehicle &veh, vehicle &veh2,
}

// Used to calculate the epicenter of the collision.
point epicenter1;
point epicenter2;
point_rel_ms epicenter1;
point_rel_ms epicenter2;

float dmg;
// Vertical collisions will be simpler for a while (1D)
Expand Down Expand Up @@ -1194,8 +1194,8 @@ float map::vehicle_vehicle_collision( vehicle &veh, vehicle &veh2,
veh2.damage( *this, target_parm, dmg2_part, damage_bash );
}

epicenter2.x /= coll_parts_cnt;
epicenter2.y /= coll_parts_cnt;
epicenter2.x() /= coll_parts_cnt;
epicenter2.y() /= coll_parts_cnt;

if( dmg2_part > 100 ) {
// Shake vehicle because of collision
Expand Down Expand Up @@ -1573,7 +1573,7 @@ bool map::displace_vehicle( vehicle &veh, const tripoint_rel_ms &dp, const bool

// ramps make everything super tricky
int psg_offset_z = -ramp_offset;
tripoint next_pos; // defaults to 0,0,0
tripoint_rel_ms next_pos; // defaults to 0,0,0
if( parts_to_move.empty() ) {
next_pos = veh_part.precalc[1];
}
Expand Down Expand Up @@ -5993,7 +5993,7 @@ void map::process_items_in_vehicle( vehicle &cur_veh, submap &current_submap )
{
const bool engine_heater_is_on = cur_veh.has_part( "E_HEATER", true ) && cur_veh.engine_on;
for( const vpart_reference &vp : cur_veh.get_any_parts( VPFLAG_FLUIDTANK ) ) {
vp.part().process_contents( *this, vp.pos(), engine_heater_is_on );
vp.part().process_contents( *this, vp.pos_bub(), engine_heater_is_on );
}

auto cargo_parts = cur_veh.get_parts_including_carried( VPFLAG_CARGO );
Expand Down Expand Up @@ -9791,7 +9791,7 @@ void map::build_obstacle_cache(
// Cache all the vehicle stuff in one loop
for( wrapped_vehicle &v : vehs ) {
for( const vpart_reference &vp : v.v->get_all_parts() ) {
tripoint_bub_ms p { v.pos + vp.part().precalc[0]};
tripoint_bub_ms p { v.pos + vp.part().precalc[0].raw()};
if( p.z() != start.z() ) {
break;
}
Expand Down
13 changes: 7 additions & 6 deletions src/map_extras.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,15 @@ static bool mx_helicopter( map &m, const tripoint &abs_sub )
// Get the bounding box, centered on mount(0,0), move the wreckage forward/backward
// half it's length so that it spawns more over the center of the debris area
const bounding_box bbox = veh.get_bounding_box();
const point length( std::abs( bbox.p2.x - bbox.p1.x ), std::abs( bbox.p2.y - bbox.p1.y ) );
const point offset( veh.dir_vec().x * length.x / 2, veh.dir_vec().y * length.y / 2 );
const point min( std::abs( bbox.p1.x ), std::abs( bbox.p1.y ) );
const int x_max = SEEX * 2 - bbox.p2.x - 1;
const int y_max = SEEY * 2 - bbox.p2.y - 1;
const point_rel_ms length( std::abs( bbox.p2.x() - bbox.p1.x() ),
std::abs( bbox.p2.y() - bbox.p1.y() ) );
const point_rel_ms offset( veh.dir_vec().x * length.x() / 2, veh.dir_vec().y * length.y() / 2 );
const point_rel_ms min( std::abs( bbox.p1.x() ), std::abs( bbox.p1.y() ) );
const int x_max = SEEX * 2 - bbox.p2.x() - 1;
const int y_max = SEEY * 2 - bbox.p2.y() - 1;

// Clamp x1 & y1 such that no parts of the vehicle extend over the border of the submap.
wreckage_pos = { clamp( c.x + offset.x, min.x, x_max ), clamp( c.y + offset.y, min.y, y_max ), abs_sub.z };
wreckage_pos = { clamp( c.x + offset.x(), min.x(), x_max ), clamp( c.y + offset.y(), min.y(), y_max ), abs_sub.z};
}

vehicle *wreckage = m.add_vehicle( crashed_hull, wreckage_pos.raw(), dir1, rng( 1, 33 ), 1 );
Expand Down
4 changes: 2 additions & 2 deletions src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7112,8 +7112,8 @@ std::unique_ptr<vehicle> map::add_vehicle_to_map(
map_pos.to_string() );
}
did_merge = true;
const point target_point = first_veh_parts.front()->mount;
const point source_point = parts_to_move.front()->mount;
const point_rel_ms target_point = first_veh_parts.front()->mount;
const point_rel_ms source_point = parts_to_move.front()->mount;
for( const vehicle_part *vp : parts_to_move ) {
const vpart_info &vpi = vp->info();
// TODO: change mount points to be tripoint
Expand Down
28 changes: 14 additions & 14 deletions src/savegame_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3158,8 +3158,8 @@ void vehicle_part::deserialize( const JsonObject &data )
variant = info().variant_default;
}

data.read( "mount_dx", mount.x );
data.read( "mount_dy", mount.y );
data.read( "mount_dx", mount.x() );
data.read( "mount_dy", mount.y() );
data.read( "open", open );
int direction_int;
data.read( "direction", direction_int );
Expand All @@ -3173,8 +3173,8 @@ void vehicle_part::deserialize( const JsonObject &data )
if( std::abs( z_offset ) > 10 ) {
data.throw_error_at( "z_offset", "z_offset out of range" );
}
precalc[0].z = z_offset;
precalc[1].z = z_offset;
precalc[0].z() = z_offset;
precalc[1].z() = z_offset;
}

JsonArray ja_carried = data.get_array( "carried_stack" );
Expand Down Expand Up @@ -3215,8 +3215,8 @@ void vehicle_part::serialize( JsonOut &json ) const
json.member( "variant", variant );
}
json.member( "base", base );
json.member( "mount_dx", mount.x );
json.member( "mount_dy", mount.y );
json.member( "mount_dx", mount.x() );
json.member( "mount_dy", mount.y() );
json.member( "open", open );
json.member( "direction", std::lround( to_degrees( direction ) ) );
json.member( "blood", blood );
Expand All @@ -3234,8 +3234,8 @@ void vehicle_part::serialize( JsonOut &json ) const
}
json.member( "passenger_id", passenger_id );
json.member( "crew_id", crew_id );
if( precalc[0].z ) {
json.member( "z_offset", precalc[0].z );
if( precalc[0].z() ) {
json.member( "z_offset", precalc[0].z() );
}
json.member( "items", items );
json.member( "tools", tools );
Expand All @@ -3261,19 +3261,19 @@ void vehicle_part::carried_part_data::deserialize( const JsonObject &data )
{
data.read( "veh_name", veh_name );
face_dir = units::from_degrees( data.get_int( "face_dir" ) );
data.read( "mount_x", mount.x );
data.read( "mount_y", mount.y );
data.read( "mount_z", mount.z );
data.read( "mount_x", mount.x() );
data.read( "mount_y", mount.y() );
data.read( "mount_z", mount.z() );
}

void vehicle_part::carried_part_data::serialize( JsonOut &json ) const
{
json.start_object();
json.member( "veh_name", veh_name );
json.member( "face_dir", std::lround( to_degrees( face_dir ) ) );
json.member( "mount_x", mount.x );
json.member( "mount_y", mount.y );
json.member( "mount_z", mount.z );
json.member( "mount_x", mount.x() );
json.member( "mount_y", mount.y() );
json.member( "mount_z", mount.z() );
json.end_object();
}

Expand Down
10 changes: 5 additions & 5 deletions src/turret.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ int vehicle::turrets_aim_and_fire( std::vector<vehicle_part *> &turrets )
turret_data turret = turret_query( *t );
npc &cpu = t->get_targeting_npc( *this );
shots += turret.fire( cpu, tripoint_bub_ms( t->target.second ) );
t->reset_target( global_part_pos3( *t ) );
t->reset_target( bub_part_pos( *t ) );
}
}
}
Expand All @@ -408,7 +408,7 @@ bool vehicle::turrets_aim( std::vector<vehicle_part *> &turrets )
debugmsg( "Expected a valid vehicle turret" );
return false;
}
t->reset_target( global_part_pos3( *t ) );
t->reset_target( bub_part_pos( *t ) );
}

avatar &player_character = get_avatar();
Expand Down Expand Up @@ -507,7 +507,7 @@ void vehicle::turrets_set_targeting()
}

// clear the turret's current targets to prevent unwanted auto-firing
tripoint pos = locations[ sel ];
tripoint_bub_ms pos = tripoint_bub_ms( locations[ sel ] );
turrets[ sel ]->reset_target( pos );
}
}
Expand Down Expand Up @@ -622,7 +622,7 @@ int vehicle::automatic_fire_turret( vehicle_part &pt )
// Manual target not set, find one automatically.
// BEWARE: Calling turret_data.fire on tripoint min coordinates starts a crash
// triggered at `trajectory.insert( trajectory.begin(), source )` at ranged.cpp:236
pt.reset_target( pos.raw() );
pt.reset_target( pos );
int boo_hoo;

// TODO: calculate chance to hit and cap range based upon this
Expand Down Expand Up @@ -662,7 +662,7 @@ int vehicle::automatic_fire_turret( vehicle_part &pt )

// Get the turret's target and reset it
tripoint_bub_ms targ( target.second );
pt.reset_target( pos.raw() );
pt.reset_target( pos );

shots = gun.fire( cpu, targ );

Expand Down
6 changes: 3 additions & 3 deletions src/veh_appliance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,13 +401,13 @@ void veh_app_interact::refill()
act = player_activity( ACT_VEHICLE, 1000, static_cast<int>( 'f' ) );
act.targets.push_back( target );
act.str_values.push_back( pt->info().id.str() );
const point q = veh->coord_translate( pt->mount );
const point_rel_ms q = veh->coord_translate( pt->mount );
map &here = get_map();
for( const tripoint &p : veh->get_points( true ) ) {
act.coord_set.insert( here.getglobal( p ).raw() );
}
act.values.push_back( here.getglobal( veh->pos_bub() ).x() + q.x );
act.values.push_back( here.getglobal( veh->pos_bub() ).y() + q.y );
act.values.push_back( here.getglobal( veh->pos_bub() ).x() + q.x() );
act.values.push_back( here.getglobal( veh->pos_bub() ).y() + q.y() );
act.values.push_back( a_point.x );
act.values.push_back( a_point.y );
act.values.push_back( -a_point.x );
Expand Down
16 changes: 8 additions & 8 deletions src/veh_interact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ player_activity veh_interact::serialize_activity()

// if we're working on an existing part, use that part as the reference point
// otherwise (e.g. installing a new frame), just use part 0
const point q = veh->coord_translate( pt ? pt->mount : veh->part( 0 ).mount );
const point_rel_ms q = veh->coord_translate( pt ? pt->mount : veh->part( 0 ).mount );
const vehicle_part *vpt = pt ? pt : &veh->part( 0 );
map &here = get_map();
for( const tripoint &p : veh->get_points( true ) ) {
res.coord_set.insert( here.getglobal( p ).raw() );
}
res.values.push_back( here.getglobal( veh->pos_bub() ).x() + q.x ); // values[0]
res.values.push_back( here.getglobal( veh->pos_bub() ).y() + q.y ); // values[1]
res.values.push_back( here.getglobal( veh->pos_bub() ).x() + q.x() ); // values[0]
res.values.push_back( here.getglobal( veh->pos_bub() ).y() + q.y() ); // values[1]
res.values.push_back( dd.x ); // values[2]
res.values.push_back( dd.y ); // values[3]
res.values.push_back( -dd.x ); // values[4]
Expand Down Expand Up @@ -850,7 +850,7 @@ bool veh_interact::update_part_requirements()
const vehicle_part &vp = veh->part( p );
if( !vp.info().has_flag( "TRACKED" ) ) {
// tracked parts don't contribute to axle complexity
axles.insert( vp.mount.x );
axles.insert( vp.mount.x() );
}
}

Expand Down Expand Up @@ -1123,7 +1123,7 @@ void veh_interact::do_repair()
if( reason == task_reason::INVALID_TARGET ) {
vehicle_part *most_repairable = get_most_repairable_part();
if( most_repairable && most_repairable->is_repairable() ) {
move_cursor( ( most_repairable->mount + dd ).rotate( 3 ) );
move_cursor( ( most_repairable->mount.raw() + dd ).rotate( 3 ) );
return;
}
}
Expand Down Expand Up @@ -1659,7 +1659,7 @@ void veh_interact::overview( const overview_enable_t &enable,
}

if( overview_pos >= 0 && static_cast<size_t>( overview_pos ) < overview_opts.size() ) {
move_cursor( ( overview_opts[overview_pos].part->mount + dd ).rotate( 3 ) );
move_cursor( ( overview_opts[overview_pos].part->mount.raw() + dd ).rotate( 3 ) );
}

if( overview_pos >= 0 && static_cast<size_t>( overview_pos ) < overview_opts.size() &&
Expand Down Expand Up @@ -2363,7 +2363,7 @@ void veh_interact::display_veh()
for( const int structural_part_idx : veh->all_parts_at_location( "structure" ) ) {
const vehicle_part &vp = veh->part( structural_part_idx );
const vpart_display vd = veh->get_display_of_tile( vp.mount, false, false );
const point q = ( vp.mount + dd ).rotate( 3 );
const point q = ( vp.mount.raw() + dd ).rotate( 3 );

if( q != point_zero ) { // cursor is not on this part
mvwputch( w_disp, h_size + q, vd.color, vd.symbol_curses );
Expand Down Expand Up @@ -3355,7 +3355,7 @@ void veh_interact::complete_vehicle( Character &you )
}

// Save these values now so they aren't lost when parts or vehicles are destroyed.
const point part_mount = vp->mount;
const point_rel_ms part_mount = vp->mount;
const tripoint_bub_ms part_pos = veh.bub_part_pos( *vp );

veh.unlink_cables( part_mount, you,
Expand Down
Loading

0 comments on commit d8f797e

Please sign in to comment.