From 65ea2a564c94cadbcb6e59380f8cc4a8066d8151 Mon Sep 17 00:00:00 2001 From: PatrikLundell Date: Fri, 17 Jan 2025 12:05:35 +0100 Subject: [PATCH] typified sm_pos in vehicle --- src/editmap.cpp | 2 +- src/game.cpp | 2 +- src/map.cpp | 32 ++++++++++++++++---------------- src/mapgen.cpp | 12 ++++++------ src/vehicle.cpp | 26 +++++++++++++------------- src/vehicle.h | 4 ++-- src/vehicle_move.cpp | 6 +++--- src/vehicle_use.cpp | 2 +- tests/item_spawn_test.cpp | 2 +- 9 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/editmap.cpp b/src/editmap.cpp index 290498c65dea3..efe1eae6fcb2e 100644 --- a/src/editmap.cpp +++ b/src/editmap.cpp @@ -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 diff --git a/src/game.cpp b/src/game.cpp index 025e31fefdf83..f178310a8b281 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1223,7 +1223,7 @@ vehicle *game::place_vehicle_nearby( tripoint_bub_sm quotient; point_sm_ms remainder; std::tie( quotient, remainder ) = coords::project_remain( abs_local ); - veh->sm_pos = quotient.raw(); + veh->sm_pos = quotient; veh->pos = remainder; veh->unlock(); // always spawn unlocked diff --git a/src/map.cpp b/src/map.cpp index 337534f274ab3..b255283656f59 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -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 ); } @@ -576,12 +576,12 @@ std::unique_ptr map::detach_vehicle( vehicle *veh ) return std::unique_ptr(); } - 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 @@ -592,10 +592,10 @@ std::unique_ptr 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(); } @@ -621,8 +621,8 @@ std::unique_ptr 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(); } @@ -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 ) { @@ -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( veh.part_count() ) ) { debugmsg( "invalid c.part %d", c.part ); @@ -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(); @@ -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; @@ -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 ); } } diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 6fb87f8c1d1c2..f67794b868932 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -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( 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(); @@ -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 ); @@ -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 ); diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 17c8ffea97b9e..cca1365cc0287 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -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; @@ -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( tripoint_bub_ms( elem, sm_pos.z ) ); + if( creatures.creature_at( {elem, sm_pos.z()} ) ) { + npc *guy = creatures.creature_at( { elem, sm_pos.z() } ); if( guy && !guy->in_vehicle ) { stop = true; break; @@ -1906,7 +1906,7 @@ bool vehicle::merge_rackable_vehicle( vehicle *carry_veh, const std::vector 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(); @@ -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" ); @@ -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; @@ -3623,7 +3623,7 @@ tripoint_abs_omt vehicle::global_omt_location() const tripoint_bub_ms vehicle::pos_bub() const { - return coords::project_to( tripoint_bub_sm( sm_pos ) ) + rebase_rel( pos ); + return coords::project_to( sm_pos ) + rebase_rel( pos ); } tripoint_bub_ms vehicle::bub_part_pos( const int index ) const @@ -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; } @@ -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() ); } @@ -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; } diff --git a/src/vehicle.h b/src/vehicle.h index f73e4540e6f70..1e005e558a163 100644 --- a/src/vehicle.h +++ b/src/vehicle.h @@ -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 ); @@ -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) diff --git a/src/vehicle_move.cpp b/src/vehicle_move.cpp index 9e0c00d5e8370..781481207dbb1 100644 --- a/src/vehicle_move.cpp +++ b/src/vehicle_move.cpp @@ -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( critter ); @@ -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(); @@ -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; diff --git a/src/vehicle_use.cpp b/src/vehicle_use.cpp index 0a67b8f8c7ef4..03d7ad175172e 100644 --- a/src/vehicle_use.cpp +++ b/src/vehicle_use.cpp @@ -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 ); diff --git a/tests/item_spawn_test.cpp b/tests/item_spawn_test.cpp index 371ad1114d888..ce34b3fc5dbf7 100644 --- a/tests/item_spawn_test.cpp +++ b/tests/item_spawn_test.cpp @@ -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 ovp_cargo = here.veh_at( pos ).cargo(); REQUIRE( ovp_cargo ); for( item &it : ovp_cargo->items() ) {