Skip to content

Commit

Permalink
Merge pull request #79208 from PatrikLundell/vehicle
Browse files Browse the repository at this point in the history
typified sm_pos in vehicle
  • Loading branch information
Maleclypse authored Jan 18, 2025
2 parents 018223c + 65ea2a5 commit d21d5f2
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/editmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1929,7 +1929,7 @@ void editmap::mapgen_preview( const real_coords &tc, uilist &gmenu )
std::swap( *destsm, *srcsm );

for( auto &veh : destsm->vehicles ) {
veh->sm_pos = dest_pos.raw();
veh->sm_pos = rebase_bub( dest_pos );
}

if( !destsm->spawns.empty() ) { // trigger spawnpoints
Expand Down
2 changes: 1 addition & 1 deletion src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ vehicle *game::place_vehicle_nearby(
tripoint_bub_sm quotient;
point_sm_ms remainder;
std::tie( quotient, remainder ) = coords::project_remain<coords::sm>( abs_local );
veh->sm_pos = quotient.raw();
veh->sm_pos = quotient;
veh->pos = remainder;

veh->unlock(); // always spawn unlocked
Expand Down
32 changes: 16 additions & 16 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ void map::reset_vehicles_sm_pos()
submap *const sm = get_submap_at_grid( grid );
if( sm != nullptr ) {
for( auto const &elem : sm->vehicles ) {
elem->sm_pos = grid.raw();
elem->sm_pos = rebase_bub( grid );
add_vehicle_to_cache( &*elem );
_add_vehicle_to_list( ch, &*elem );
}
Expand Down Expand Up @@ -576,12 +576,12 @@ std::unique_ptr<vehicle> map::detach_vehicle( vehicle *veh )
return std::unique_ptr<vehicle>();
}

int z = veh->sm_pos.z;
int z = veh->sm_pos.z();
if( z < -OVERMAP_DEPTH || z > OVERMAP_HEIGHT ) {
debugmsg( "detach_vehicle got a vehicle outside allowed z-level range! name=%s, submap:%d,%d,%d",
veh->name, veh->sm_pos.x, veh->sm_pos.y, veh->sm_pos.z );
debugmsg( "detach_vehicle got a vehicle outside allowed z-level range! name=%s, submap:%s",
veh->name, veh->sm_pos.to_string() );
// Try to fix by moving the vehicle here
z = veh->sm_pos.z = abs_sub.z();
z = veh->sm_pos.z() = abs_sub.z();
}

// Unboard all passengers before detaching
Expand All @@ -592,10 +592,10 @@ std::unique_ptr<vehicle> map::detach_vehicle( vehicle *veh )
}
}
veh->invalidate_towing( true );
submap *const current_submap = get_submap_at_grid( tripoint_rel_sm( veh->sm_pos ) );
submap *const current_submap = get_submap_at_grid( rebase_rel( veh->sm_pos ) );
if( current_submap == nullptr ) {
debugmsg( "Tried to detach vehicle at (%d,%d,%d) but the submap is not loaded", veh->sm_pos.x,
veh->sm_pos.y, veh->sm_pos.z );
debugmsg( "Tried to detach vehicle at %s but the submap is not loaded",
veh->sm_pos.to_string() );
return std::unique_ptr<vehicle>();
}

Expand All @@ -621,8 +621,8 @@ std::unique_ptr<vehicle> map::detach_vehicle( vehicle *veh )
return result;
}
}
debugmsg( "detach_vehicle can't find it! name=%s, submap:%d,%d,%d", veh->name, veh->sm_pos.x,
veh->sm_pos.y, veh->sm_pos.z );
debugmsg( "detach_vehicle can't find it! name=%s, submap:%s", veh->name,
veh->sm_pos.to_string() );
return std::unique_ptr<vehicle>();
}

Expand Down Expand Up @@ -780,7 +780,7 @@ vehicle *map::move_vehicle( vehicle &veh, const tripoint_rel_ms &dp, const tiler
// Ensured by the splitting above
cata_assert( vertical == ( dp.xy() == point_rel_ms::zero ) );

const int target_z = dp.z() + veh.sm_pos.z;
const int target_z = dp.z() + veh.sm_pos.z();
// limit vehicles to at most OVERMAP_HEIGHT - 1; this mitigates getting to zlevel 10 easily
// and causing `get_cache_ref( zlev + 1 );` call in map::build_sunlight_cache overflowing
if( target_z < -OVERMAP_DEPTH || target_z > OVERMAP_HEIGHT - 1 ) {
Expand Down Expand Up @@ -1007,7 +1007,7 @@ float map::vehicle_vehicle_collision( vehicle &veh, vehicle &veh2,
// parts are damaged/broken on both sides,
// remaining times are normalized
const veh_collision &c = collisions[0];
const bool vertical = veh.sm_pos.z != veh2.sm_pos.z;
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 );
Expand Down Expand Up @@ -1298,7 +1298,7 @@ VehicleList map::get_vehicles( const tripoint_bub_ms &start, const tripoint_bub_
}
for( const auto &elem : current_submap->vehicles ) {
// Ensure the vehicle z-position is correct
elem->sm_pos.z = cz;
elem->sm_pos.z() = cz;
wrapped_vehicle w;
w.v = elem.get();
w.pos = w.v->pos_bub();
Expand Down Expand Up @@ -8158,7 +8158,7 @@ void map::loadn( const point_bub_sm &grid, bool update_vehicles )
vehicle *veh = iter->get();
if( veh->part_count() > 0 ) {
// Always fix submap coordinates for easier Z-level-related operations
veh->sm_pos = { grid.raw(), z};
veh->sm_pos = { grid, z};
iter++;
if( main_inbounds ) {
_main_requires_cleanup = true;
Expand Down Expand Up @@ -8686,9 +8686,9 @@ void map::copy_grid( const tripoint_rel_sm &to, const tripoint_rel_sm &from )
from.z() );
return;
}
setsubmap( get_nonant( tripoint_rel_sm( to ) ), smap );
setsubmap( get_nonant( to ), smap );
for( auto &it : smap->vehicles ) {
it->sm_pos = to.raw();
it->sm_pos = rebase_bub( to );
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6899,7 +6899,7 @@ vehicle *map::add_vehicle( const vproto_id &type, const tripoint_bub_ms &p, cons
tripoint_bub_sm quotient;
point_sm_ms remainder;
std::tie( quotient, remainder ) = coords::project_remain<coords::sm>( p_ms );
veh->sm_pos = quotient.raw();
veh->sm_pos = quotient;
veh->pos = remainder;
veh->init_state( *this, veh_fuel, veh_status, force_status );
veh->place_spawn_items();
Expand All @@ -6914,17 +6914,17 @@ vehicle *map::add_vehicle( const vproto_id &type, const tripoint_bub_ms &p, cons
vehicle *placed_vehicle = placed_vehicle_up.get();

if( placed_vehicle != nullptr ) {
submap *place_on_submap = get_submap_at_grid( tripoint_rel_sm( placed_vehicle->sm_pos ) );
submap *place_on_submap = get_submap_at_grid( rebase_rel( placed_vehicle->sm_pos ) );
if( place_on_submap == nullptr ) {
debugmsg( "Tried to add vehicle at (%d,%d,%d) but the submap is not loaded",
placed_vehicle->sm_pos.x, placed_vehicle->sm_pos.y, placed_vehicle->sm_pos.z );
debugmsg( "Tried to add vehicle at %s but the submap is not loaded",
placed_vehicle->sm_pos.to_string() );
return placed_vehicle;
}
place_on_submap->ensure_nonuniform();
place_on_submap->vehicles.push_back( std::move( placed_vehicle_up ) );
invalidate_max_populated_zlev( p.z() );

level_cache &ch = get_cache( placed_vehicle->sm_pos.z );
level_cache &ch = get_cache( placed_vehicle->sm_pos.z() );
ch.vehicle_list.insert( placed_vehicle );
add_vehicle_to_cache( placed_vehicle );

Expand Down Expand Up @@ -7211,7 +7211,7 @@ void map::rotate( int turns )
sm->rotate( turns );

for( auto &veh : sm->vehicles ) {
veh->sm_pos = tripoint( p.raw(), z_level );
veh->sm_pos = { rebase_bub( p ), z_level };
}

update_vehicle_list( sm, z_level );
Expand Down
26 changes: 13 additions & 13 deletions src/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,8 @@ bool vehicle::precollision_check( units::angle &angle, map &here, bool follow_pr
if( stop ) {
break;
}
const optional_vpart_position ovp = here.veh_at( tripoint_bub_ms( elem.x(), elem.y(), sm_pos.z ) );
if( here.impassable_ter_furn( tripoint_bub_ms( elem.x(), elem.y(), sm_pos.z ) ) || ( ovp &&
const optional_vpart_position ovp = here.veh_at( { elem, sm_pos.z() } );
if( here.impassable_ter_furn( { elem, sm_pos.z() } ) || ( ovp &&
&ovp->vehicle() != this ) ) {
stop = true;
break;
Expand All @@ -840,8 +840,8 @@ bool vehicle::precollision_check( units::angle &angle, map &here, bool follow_pr
}
}
bool its_a_pet = false;
if( creatures.creature_at( tripoint_bub_ms( elem, sm_pos.z ) ) ) {
npc *guy = creatures.creature_at<npc>( tripoint_bub_ms( elem, sm_pos.z ) );
if( creatures.creature_at( {elem, sm_pos.z()} ) ) {
npc *guy = creatures.creature_at<npc>( { elem, sm_pos.z() } );
if( guy && !guy->in_vehicle ) {
stop = true;
break;
Expand Down Expand Up @@ -1906,7 +1906,7 @@ bool vehicle::merge_rackable_vehicle( vehicle *carry_veh, const std::vector<int>
add_msg( _( "You load the %1$s on the rack." ), carry_veh->name );
here.destroy_vehicle( carry_veh );
here.dirty_vehicle_list.insert( this );
here.set_transparency_cache_dirty( sm_pos.z );
here.set_transparency_cache_dirty( sm_pos.z() );
here.set_seen_cache_dirty( tripoint_bub_ms::zero );
here.invalidate_map_cache( here.get_abs_sub().z() );
here.rebuild_vehicle_level_caches();
Expand Down Expand Up @@ -2131,11 +2131,11 @@ bool vehicle::remove_part( vehicle_part &vp, RemovePartHandler &handler )
// if a windshield is removed (usually destroyed) also remove curtains
// attached to it.
if( remove_dependent_part( "WINDOW", "CURTAIN" ) || vpi.has_flag( VPFLAG_OPAQUE ) ) {
handler.set_transparency_cache_dirty( sm_pos.z );
handler.set_transparency_cache_dirty( sm_pos.z() );
}

if( vpi.has_flag( VPFLAG_ROOF ) || vpi.has_flag( VPFLAG_OPAQUE ) ) {
handler.set_floor_cache_dirty( sm_pos.z + 1 );
handler.set_floor_cache_dirty( sm_pos.z() + 1 );
}

remove_dependent_part( "SEAT", "SEATBELT" );
Expand Down Expand Up @@ -2638,7 +2638,7 @@ bool vehicle::split_vehicles( map &here,
new_vehicle->zones_dirty = true;

here.dirty_vehicle_list.insert( new_vehicle );
here.set_transparency_cache_dirty( sm_pos.z );
here.set_transparency_cache_dirty( sm_pos.z() );
here.set_seen_cache_dirty( tripoint_bub_ms::zero );
if( !new_labels.empty() ) {
new_vehicle->labels = new_labels;
Expand Down Expand Up @@ -3623,7 +3623,7 @@ tripoint_abs_omt vehicle::global_omt_location() const

tripoint_bub_ms vehicle::pos_bub() const
{
return coords::project_to<coords::ms>( tripoint_bub_sm( sm_pos ) ) + rebase_rel( pos );
return coords::project_to<coords::ms>( sm_pos ) + rebase_rel( pos );
}

tripoint_bub_ms vehicle::bub_part_pos( const int index ) const
Expand All @@ -3636,10 +3636,10 @@ tripoint_bub_ms vehicle::bub_part_pos( const vehicle_part &pt ) const
return pos_bub() + pt.precalc[ 0 ];
}

void vehicle::set_submap_moved( const tripoint_sm_ms &p )
void vehicle::set_submap_moved( const tripoint_bub_sm &p )
{
const point_abs_ms old_msp = global_square_location().xy();
sm_pos = p.raw();
sm_pos = p;
if( !tracking_on ) {
return;
}
Expand Down Expand Up @@ -5998,7 +5998,7 @@ void vehicle::on_move()
is_passenger( pc ), player_is_driving_this_veh(), remote_controlled( pc ),
is_flying_in_air(), is_watercraft() && can_float(), can_use_rails(),
is_falling, is_in_water( true ) && !can_float(),
skidding, velocity, sm_pos.z
skidding, velocity, sm_pos.z()
);
}

Expand Down Expand Up @@ -8183,7 +8183,7 @@ void vehicle::update_time( const time_point &update_to )

map &here = get_map();

if( sm_pos.z < 0 ) {
if( sm_pos.z() < 0 ) {
last_update = update_to;
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/vehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -2151,7 +2151,7 @@ class vehicle
* This should be called only when the vehicle has actually been moved, not when
* the map is just shifted (in the later case simply set smx/smy directly).
*/
void set_submap_moved( const tripoint_sm_ms &p );
void set_submap_moved( const tripoint_bub_sm &p );
void use_autoclave( int p );
void use_washing_machine( int p );
void use_dishwasher( int p );
Expand Down Expand Up @@ -2337,7 +2337,7 @@ class vehicle
* is loaded into the map the values are directly set. The vehicles position does
* not change therefore no call to set_submap_moved is required.
*/
tripoint sm_pos = tripoint::zero; // NOLINT(cata-serialize)
tripoint_bub_sm sm_pos = tripoint_bub_sm::zero; // NOLINT(cata-serialize)

// alternator load as a percentage of engine power, in units of 0.1% so 1000 is 100.0%
int alternator_load = 0; // NOLINT(cata-serialize)
Expand Down
6 changes: 3 additions & 3 deletions src/vehicle_move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ veh_collision vehicle::part_collision( int part, const tripoint_bub_ms &p,
{
// Vertical collisions need to be handled differently
// All collisions have to be either fully vertical or fully horizontal for now
const bool vert_coll = bash_floor || p.z() != sm_pos.z;
const bool vert_coll = bash_floor || p.z() != sm_pos.z();
Creature *critter = get_creature_tracker().creature_at( p, true );
Character *ph = dynamic_cast<Character *>( critter );

Expand Down Expand Up @@ -1404,7 +1404,7 @@ bool vehicle::check_heli_ascend( Character &p ) const
p.add_msg_if_player( m_bad, _( "It would be unsafe to try and take off while you are moving." ) );
return false;
}
if( sm_pos.z + 1 >= OVERMAP_HEIGHT ) {
if( sm_pos.z() + 1 >= OVERMAP_HEIGHT ) {
return false; // don't allow trying to ascend to max zlevel
}
map &here = get_map();
Expand Down Expand Up @@ -1850,7 +1850,7 @@ vehicle *vehicle::act_on_map()
g->setremoteveh( nullptr );
}

here.on_vehicle_moved( sm_pos.z );
here.on_vehicle_moved( sm_pos.z() );
// Destroy vehicle (sank to nowhere)
here.destroy_vehicle( this );
return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/vehicle_use.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1322,7 +1322,7 @@ void vehicle::open_or_close( const int part_index, const bool opening )
part_open_or_close( part_index, opening );
insides_dirty = true;
map &here = get_map();
here.set_transparency_cache_dirty( sm_pos.z );
here.set_transparency_cache_dirty( sm_pos.z() );
const tripoint_bub_ms part_location = mount_to_tripoint( parts[part_index].mount );
here.set_seen_cache_dirty( tripoint_bub_ms( part_location ) );
const int dist = rl_dist( get_player_character().pos_bub(), part_location );
Expand Down
2 changes: 1 addition & 1 deletion tests/item_spawn_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ TEST_CASE( "correct_amounts_of_an_item_spawn_inside_a_container", "[item_spawn]"
vehicle *veh = here.add_vehicle( cs_data.vehicle, vehpos, 0_degrees, 0, 0 );
REQUIRE( veh );
REQUIRE( here.get_vehicles().size() == 1 );
const tripoint_bub_ms pos( point_bub_ms::zero, veh->sm_pos.z );
const tripoint_bub_ms pos( point_bub_ms::zero, veh->sm_pos.z() );
const std::optional<vpart_reference> ovp_cargo = here.veh_at( pos ).cargo();
REQUIRE( ovp_cargo );
for( item &it : ovp_cargo->items() ) {
Expand Down

0 comments on commit d21d5f2

Please sign in to comment.