diff --git a/src/editmap.cpp b/src/editmap.cpp index 8d513f176a1f8..a913d3497949f 100644 --- a/src/editmap.cpp +++ b/src/editmap.cpp @@ -668,7 +668,8 @@ void editmap::draw_main_ui_overlay() std::map> spawns; for( int x = 0; x < 2; x++ ) { for( int y = 0; y < 2; y++ ) { - submap *sm = tmpmap.get_submap_at_grid( { x, y, target.z()} ); + const tripoint_bub_sm_ib submap_pos{ x, y, target.z() }; + submap *sm = tmpmap.get_submap_at_grid( submap_pos ); if( sm ) { const tripoint_bub_ms sm_origin = origin_p + tripoint( x * SEEX, y * SEEY, target.z() ); for( const spawn_point &sp : sm->spawns ) { @@ -1879,7 +1880,7 @@ void editmap::mapgen_preview( const real_coords &tc, uilist &gmenu ) } else if( gpmenu.ret == 1 ) { tmpmap.rotate( 1 ); } else if( gpmenu.ret == 2 ) { - const point_rel_sm target_sub( target.x() / SEEX, target.y() / SEEY ); + const point_bub_sm target_sub( target.x() / SEEX, target.y() / SEEY ); here.set_transparency_cache_dirty( target.z() ); here.set_outside_cache_dirty( target.z() ); @@ -1894,8 +1895,8 @@ void editmap::mapgen_preview( const real_coords &tc, uilist &gmenu ) for( int z = -OVERMAP_DEPTH; z <= OVERMAP_HEIGHT; z++ ) { // Apply previewed mapgen to map. Since this is a function for testing, we try avoid triggering // functions that would alter the results - const tripoint_rel_sm dest_pos = target_sub + tripoint( x, y, z ); - const tripoint_rel_sm src_pos = tripoint_rel_sm{ x, y, z }; + const tripoint_bub_sm dest_pos = target_sub + tripoint( x, y, z ); + const tripoint_bub_sm_ib src_pos = tripoint_bub_sm_ib{ x, y, z }; submap *destsm = here.get_submap_at_grid( dest_pos ); submap *srcsm = tmpmap.get_submap_at_grid( src_pos ); @@ -1921,7 +1922,7 @@ void editmap::mapgen_preview( const real_coords &tc, uilist &gmenu ) // Since we cleared the vehicle cache of the whole z-level (not just the generate map), we add it back here for( int x = 0; x < here.getmapsize(); x++ ) { for( int y = 0; y < here.getmapsize(); y++ ) { - const tripoint_rel_sm dest_pos = tripoint_rel_sm( x, y, target.z() ); + const tripoint_bub_sm_ib dest_pos = tripoint_bub_sm_ib( x, y, target.z() ); const submap *destsm = here.get_submap_at_grid( dest_pos ); if( destsm == nullptr ) { debugmsg( "Tried to update vehicle cache at (%d,%d,%d) but the submap is not loaded", dest_pos.x(), @@ -1968,7 +1969,8 @@ vehicle *editmap::mapgen_veh_query( const tripoint_abs_omt &omt_tgt ) std::vector possible_vehicles; for( int x = 0; x < 2; x++ ) { for( int y = 0; y < 2; y++ ) { - submap *destsm = target_bay.get_submap_at_grid( { x, y, target.z()} ); + const tripoint_bub_sm_ib submap_pos{ x, y, target.z() }; + submap *destsm = target_bay.get_submap_at_grid( submap_pos ); if( destsm == nullptr ) { debugmsg( "Tried to get vehicles at (%d,%d,%d) but the submap is not loaded", x, y, target.z() ); continue; @@ -2005,7 +2007,8 @@ bool editmap::mapgen_veh_destroy( const tripoint_abs_omt &omt_tgt, vehicle *car_ target_bay.load( omt_tgt, false ); for( int x = 0; x < 2; x++ ) { for( int y = 0; y < 2; y++ ) { - submap *destsm = target_bay.get_submap_at_grid( { x, y, target.z()} ); + const tripoint_bub_sm_ib submap_pos{ x, y, target.z() }; + submap *destsm = target_bay.get_submap_at_grid( submap_pos ); if( destsm == nullptr ) { debugmsg( "Tried to destroy vehicle at (%d,%d,%d) but the submap is not loaded", x, y, target.z() ); continue; diff --git a/src/lightmap.cpp b/src/lightmap.cpp index b1181d08d22e4..fc789409548a2 100644 --- a/src/lightmap.cpp +++ b/src/lightmap.cpp @@ -114,7 +114,8 @@ bool map::build_transparency_cache( const int zlev ) // Traverse the submaps in order for( int smx = 0; smx < my_MAPSIZE; ++smx ) { for( int smy = 0; smy < my_MAPSIZE; ++smy ) { - const submap *cur_submap = get_submap_at_grid( {smx, smy, zlev} ); + const tripoint_bub_sm_ib submap_pos{ smx, smy, zlev }; + const submap *cur_submap = get_submap_at_grid( submap_pos ); if( cur_submap == nullptr ) { debugmsg( "Tried to build transparency cache at (%d,%d,%d) but the submap is not loaded", smx, smy, zlev ); @@ -435,7 +436,8 @@ void map::generate_lightmap( const int zlev ) // Traverse the submaps in order for( int smx = 0; smx < my_MAPSIZE; ++smx ) { for( int smy = 0; smy < my_MAPSIZE; ++smy ) { - const submap *cur_submap = get_submap_at_grid( { smx, smy, zlev } ); + const tripoint_bub_sm_ib submap_pos{ smx, smy, zlev }; + const submap *cur_submap = get_submap_at_grid( submap_pos ); if( cur_submap == nullptr ) { debugmsg( "Tried to generate lightmap at (%d,%d,%d) but the submap is not loaded", smx, smy, zlev ); continue; diff --git a/src/map.cpp b/src/map.cpp index 566af69b0287f..12b44a3d31e6d 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -574,7 +574,7 @@ void map::reset_vehicles_sm_pos() level_cache &ch = get_cache( z ); for( int x = 0; x < getmapsize(); x++ ) { for( int y = 0; y < getmapsize(); y++ ) { - tripoint_rel_sm const grid( x, y, z ); + tripoint_bub_sm_ib const grid( x, y, z ); submap *const sm = get_submap_at_grid( grid ); if( sm != nullptr ) { for( auto const &elem : sm->vehicles ) { @@ -629,7 +629,7 @@ 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( tripoint_bub_sm( 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 ); @@ -1338,7 +1338,8 @@ VehicleList map::get_vehicles( const tripoint_bub_ms &start, const tripoint_bub_ for( int cx = chunk_sx; cx <= chunk_ex; ++cx ) { for( int cy = chunk_sy; cy <= chunk_ey; ++cy ) { for( int cz = chunk_sz; cz <= chunk_ez; ++cz ) { - submap *current_submap = get_submap_at_grid( { cx, cy, cz } ); + const tripoint_bub_sm_ib submap_pos{ cx, cy, cz }; + submap *current_submap = get_submap_at_grid( submap_pos ); if( current_submap == nullptr ) { debugmsg( "Tried to process vehicle at (%d,%d,%d) but the submap is not loaded", cx, cy, cz ); continue; @@ -3770,7 +3771,8 @@ void map::decay_fields_and_scent( const time_duration &amount ) const auto &outside_cache = get_cache_ref( smz ).outside_cache; for( int smx = 0; smx < my_MAPSIZE; ++smx ) { for( int smy = 0; smy < my_MAPSIZE; ++smy ) { - submap *cur_submap = get_submap_at_grid( { smx, smy, smz } ); + const tripoint_bub_sm_ib submap_pos{ smx, smy, smz }; + submap *cur_submap = get_submap_at_grid( submap_pos ); if( cur_submap == nullptr ) { debugmsg( "Tried to process field at (%d,%d,%d) but the submap is not loaded", smx, smy, smz ); continue; @@ -5986,7 +5988,7 @@ void map::process_items() submaps_with_vehicles.emplace( pos.x / SEEX, pos.y / SEEY, pos.z ); } for( const tripoint &pos : submaps_with_vehicles ) { - submap *const current_submap = get_submap_at_grid( tripoint_rel_sm( pos ) ); + submap *const current_submap = get_submap_at_grid( tripoint_bub_sm( pos ) ); if( current_submap == nullptr ) { debugmsg( "Tried to process items at (%d,%d,%d) but the submap is not loaded", pos.x, pos.y, pos.z ); @@ -6003,14 +6005,13 @@ void map::process_items() iter = submaps_with_active_items.erase( iter ); continue; } - const tripoint_rel_sm local_pos = abs_pos - abs_sub.xy(); + const tripoint_bub_sm local_pos { ( abs_pos - abs_sub.xy() ).raw() }; submap *const current_submap = get_submap_at_grid( local_pos ); if( current_submap == nullptr ) { debugmsg( "Tried to process items at %s but the submap is not loaded", local_pos.to_string() ); continue; } - // TODO: fix point types process_items_in_submap( *current_submap, local_pos ); if( current_submap->active_items.empty() ) { iter = submaps_with_active_items.erase( iter ); @@ -6020,7 +6021,7 @@ void map::process_items() } } -void map::process_items_in_submap( submap ¤t_submap, const tripoint_rel_sm &gridp ) +void map::process_items_in_submap( submap ¤t_submap, const tripoint_bub_sm &gridp ) { // Get a COPY of the active item list for this submap. // If more are added as a side effect of processing, they are ignored this turn. @@ -8338,10 +8339,10 @@ void map::save() for( int gridy = 0; gridy < my_MAPSIZE; gridy++ ) { if( zlevels ) { for( int gridz = -OVERMAP_DEPTH; gridz <= OVERMAP_HEIGHT; gridz++ ) { - saven( tripoint( gridx, gridy, gridz ) ); + saven( tripoint_bub_sm_ib( gridx, gridy, gridz ) ); } } else { - saven( tripoint( gridx, gridy, abs_sub.z() ) ); + saven( tripoint_bub_sm_ib( gridx, gridy, abs_sub.z() ) ); } } } @@ -8369,7 +8370,7 @@ void map::load( const tripoint_abs_sm &w, const bool update_vehicle, clear_vehicle_level_caches(); for( int gridx = 0; gridx < my_MAPSIZE; gridx++ ) { for( int gridy = 0; gridy < my_MAPSIZE; gridy++ ) { - loadn( point( gridx, gridy ), update_vehicle ); + loadn( point_bub_sm_ib( gridx, gridy ), update_vehicle ); if( pump_events ) { inp_mngr.pump_events(); } @@ -8481,7 +8482,7 @@ void map::shift( const point_rel_sm &sp ) } const tripoint_abs_sm abs = get_abs_sub(); - std::vector loaded_grids; + std::vector loaded_grids; // TODO: fix point types (sp should be relative?) set_abs_sub( abs + sp ); @@ -8532,11 +8533,13 @@ void map::shift( const point_rel_sm &sp ) for( int gridx = x_start; gridx != x_stop; gridx += x_step ) { for( int gridy = y_start; gridy != y_stop; gridy += y_step ) { + const point_bub_sm_ib p{ gridx, gridy }; if( gridx + sp.x() != x_stop && gridy + sp.y() != y_stop ) { for( int gridz = zmin; gridz <= zmax; gridz++ ) { - const tripoint_rel_sm grid( gridx, gridy, gridz ); + const tripoint_bub_sm_ib grid{ p, gridz }; + const tripoint_bub_sm_ib from{ p + sp, gridz }; - copy_grid( grid, grid + sp ); + copy_grid( grid, from ); submap *const cur_submap = get_submap_at_grid( grid ); if( cur_submap == nullptr ) { debugmsg( "Tried to update vehicle list at %s but the submap is not loaded", grid.to_string() ); @@ -8545,7 +8548,7 @@ void map::shift( const point_rel_sm &sp ) update_vehicle_list( cur_submap, gridz ); } } else { - loadn( point( gridx, gridy ), true ); + loadn( p, true ); for( int gridz = zmin; gridz <= zmax; gridz++ ) { loaded_grids.emplace_back( gridx, gridy, gridz ); @@ -8567,7 +8570,7 @@ void map::shift( const point_rel_sm &sp ) } // actualize after loading all submaps to prevent errors // with entities at the edges - for( tripoint_rel_sm loaded_grid : loaded_grids ) { + for( tripoint_bub_sm_ib loaded_grid : loaded_grids ) { actualize( loaded_grid ); } } @@ -8599,23 +8602,23 @@ void map::vertical_shift( const int newz ) // 0,2 1,2 2,2 // (worldx,worldy,worldz) denotes the absolute coordinate of the submap // in grid[0]. -void map::saven( const tripoint &grid ) +void map::saven( const tripoint_bub_sm_ib &grid ) { dbg( D_INFO ) << "map::saven(worldx[" << abs_sub.x() << "], worldy[" << abs_sub.y() << "], worldz[" << abs_sub.z() - << "], gridx[" << grid.x << "], gridy[" << grid.y << "], gridz[" << grid.z << "])"; - const int gridn = get_nonant( tripoint_rel_sm( grid ) ); + << "], grid[" << grid << "])"; + const size_t gridn = get_nonant( grid ); submap *submap_to_save = getsubmap( gridn ); if( submap_to_save == nullptr ) { debugmsg( "Tried to save submap node (%d) but it's not loaded", gridn ); return; } - const tripoint_abs_sm abs = abs_sub.xy() + grid; + const tripoint_abs_sm abs = abs_sub.xy() + grid.raw(); - if( !zlevels && grid.z != abs_sub.z() ) { + if( !zlevels && grid.z() != abs_sub.z() ) { debugmsg( "Tried to save submap (%d,%d,%d) as (%d,%d,%d), which isn't supported in non-z-level builds", - abs.x(), abs.y(), abs_sub.z(), abs.x(), abs.y(), grid.z ); + abs.x(), abs.y(), abs_sub.z(), abs.x(), abs.y(), grid.z() ); } dbg( D_INFO ) << "map::saven abs: " << abs @@ -8671,12 +8674,12 @@ bool generate_uniform_omt( const tripoint_abs_sm &p, const oter_id &terrain_type return ret; } -void map::loadn( const point &grid, bool update_vehicles ) +void map::loadn( const point_bub_sm_ib &grid, bool update_vehicles ) { dbg( D_INFO ) << "map::loadn(game[" << g.get() << "], worldx[" << abs_sub.x() << "], worldy[" << abs_sub.y() << "], grid " << grid << ")"; - const tripoint_abs_sm grid_abs_sub = abs_sub + grid; + const tripoint_abs_sm grid_abs_sub = abs_sub + grid.raw(); const tripoint_abs_omt grid_abs_omt = project_to( grid_abs_sub ); // Get the base submap "grid" is an offset from. const tripoint_abs_sm grid_sm_base = project_to( grid_abs_omt ); @@ -8723,6 +8726,7 @@ void map::loadn( const point &grid, bool update_vehicles ) for( int z = start_z; z <= stop_z; z++ ) { const tripoint_abs_sm pos = { grid_abs_sub.xy(), z }; + const tripoint_bub_sm_ib submap_pos{ grid, z }; // New submap changes the content of the map and all caches must be recalculated set_transparency_cache_dirty( z ); set_seen_cache_dirty( z ); @@ -8730,12 +8734,12 @@ void map::loadn( const point &grid, bool update_vehicles ) set_floor_cache_dirty( z ); set_pathfinding_cache_dirty( z ); tmpsub = MAPBUFFER.lookup_submap( pos ); - setsubmap( get_nonant( tripoint_rel_sm{ grid.x, grid.y, z } ), tmpsub ); + setsubmap( get_nonant( submap_pos ), tmpsub ); if( !tmpsub->active_items.empty() ) { submaps_with_active_items_dirty.emplace( pos ); } if( tmpsub->field_count > 0 ) { - get_cache( z ).field_cache.set( grid.x + grid.y * MAPSIZE ); + get_cache( z ).field_cache.set( grid.x() + grid.y() * MAPSIZE ); } // Destroy bugged no-part vehicles @@ -8744,7 +8748,7 @@ void map::loadn( const point &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, z }; + veh->sm_pos = submap_pos.raw(); iter++; if( main_inbounds ) { _main_requires_cleanup = true; @@ -8773,7 +8777,7 @@ void map::loadn( const point &grid, bool update_vehicles ) } if( zlevels ) { - add_tree_tops( tripoint_rel_sm( grid.x, grid.y, z ) ); + add_tree_tops( submap_pos ); } } } @@ -9144,7 +9148,7 @@ void map::decay_cosmetic_fields( const tripoint &p, } } -void map::actualize( const tripoint_rel_sm &grid ) +void map::actualize( const tripoint_bub_sm_ib &grid ) { submap *const tmpsub = get_submap_at_grid( grid ); if( tmpsub == nullptr ) { @@ -9210,7 +9214,7 @@ void map::actualize( const tripoint_rel_sm &grid ) tmpsub->last_touched = calendar::turn; } -void map::add_tree_tops( const tripoint_rel_sm &grid ) +void map::add_tree_tops( const tripoint_bub_sm_ib &grid ) { if( !zlevels ) { // Can't add things on the level above when the map doesn't contain that level. @@ -9256,7 +9260,7 @@ void map::add_tree_tops( const tripoint_rel_sm &grid ) } } -void map::copy_grid( const tripoint_rel_sm &to, const tripoint_rel_sm &from ) +void map::copy_grid( const tripoint_bub_sm_ib &to, const tripoint_bub_sm_ib &from ) { submap *smap = get_submap_at_grid( from ); if( smap == nullptr ) { @@ -9264,13 +9268,13 @@ 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(); } } -void map::spawn_monsters_submap_group( const tripoint_rel_sm &gp, mongroup &group, +void map::spawn_monsters_submap_group( const tripoint_bub_sm_ib &gp, mongroup &group, bool ignore_sight ) { Character &player_character = get_player_character(); @@ -9414,9 +9418,10 @@ void map::spawn_monsters_submap_group( const tripoint_rel_sm &gp, mongroup &grou group.clear(); } -void map::spawn_monsters_submap( const tripoint_rel_sm &gp, bool ignore_sight, bool spawn_nonlocal ) +void map::spawn_monsters_submap( const tripoint_bub_sm_ib &gp, bool ignore_sight, + bool spawn_nonlocal ) { - const tripoint_abs_sm submap_pos( gp + abs_sub.xy() ); + const tripoint_abs_sm submap_pos( gp.raw() + abs_sub.xy() ); // Load unloaded monsters overmap_buffer.spawn_monster( submap_pos, spawn_nonlocal ); // Only spawn new monsters after existing monsters are loaded. @@ -9514,7 +9519,7 @@ void map::spawn_monsters( bool ignore_sight, bool spawn_nonlocal ) { const int zmin = zlevels ? -OVERMAP_DEPTH : abs_sub.z(); const int zmax = zlevels ? OVERMAP_HEIGHT : abs_sub.z(); - tripoint_rel_sm gp; + tripoint_bub_sm_ib gp; int &gx = gp.x(); int &gy = gp.y(); int &gz = gp.z(); @@ -9598,6 +9603,13 @@ bool map::inbounds( const tripoint_abs_omt &p ) const p.y() <= map_origin.y() + my_HALF_MAPSIZE; } +bool map::inbounds( const tripoint_bub_sm &p ) const +{ + return p.x() >= 0 && p.x() < my_MAPSIZE && + p.y() >= 0 && p.y() < my_MAPSIZE && + p.z() >= -OVERMAP_DEPTH && p.z() <= OVERMAP_HEIGHT; +} + tripoint_range tinymap::points_on_zlevel() const { return map::points_on_zlevel(); @@ -9660,13 +9672,14 @@ fake_map::fake_map( const ter_id &ter_type ) set_abs_sub( tripoint_below_zero ); for( int gridx = 0; gridx < get_my_MAPSIZE(); gridx++ ) { for( int gridy = 0; gridy < get_my_MAPSIZE(); gridy++ ) { + const tripoint_bub_sm_ib p{ gridx, gridy, fake_map_z }; std::unique_ptr sm = std::make_unique(); sm->set_all_ter( ter_type ); sm->set_all_furn( furn_str_id::NULL_ID() ); sm->set_all_traps( tr_null ); - setsubmap( get_nonant( { gridx, gridy, fake_map_z } ), sm.get() ); + setsubmap( get_nonant( p ), sm.get() ); temp_submaps_.emplace_back( std::move( sm ) ); } @@ -9682,13 +9695,14 @@ small_fake_map::small_fake_map( const ter_id &ter_type ) for( int gridx = 0; gridx < get_my_MAPSIZE(); gridx++ ) { for( int gridy = 0; gridy < get_my_MAPSIZE(); gridy++ ) { for( int gridz = -OVERMAP_DEPTH; gridz <= OVERMAP_HEIGHT; gridz++ ) { + const tripoint_bub_sm_ib p{ gridx, gridy, gridz }; std::unique_ptr sm = std::make_unique(); sm->set_all_ter( ter_type ); sm->set_all_furn( furn_str_id::NULL_ID() ); sm->set_all_traps( tr_null ); - setsubmap( get_nonant( { gridx, gridy, gridz } ), sm.get() ); + setsubmap( get_nonant( p ), sm.get() ); temp_submaps_.emplace_back( std::move( sm ) ); } @@ -9842,7 +9856,8 @@ void map::build_outside_cache( const int zlev ) for( int smx = 0; smx < my_MAPSIZE; ++smx ) { for( int smy = 0; smy < my_MAPSIZE; ++smy ) { - const submap *cur_submap = get_submap_at_grid( tripoint_rel_sm{ smx, smy, zlev } ); + const tripoint_bub_sm_ib submap_pos{ smx, smy, zlev }; + const submap *cur_submap = get_submap_at_grid( submap_pos ); if( cur_submap == nullptr ) { debugmsg( "Tried to build outside cache at (%d,%d,%d) but the submap is not loaded", smx, smy, zlev ); @@ -9889,7 +9904,7 @@ void map::build_obstacle_cache( // TODO: Support z-levels. for( int smx = min_submap.x; smx <= max_submap.x; ++smx ) { for( int smy = min_submap.y; smy <= max_submap.y; ++smy ) { - const submap *cur_submap = get_submap_at_grid( tripoint_rel_sm{ smx, smy, start.z } ); + const submap *cur_submap = get_submap_at_grid( tripoint_bub_sm{ smx, smy, start.z } ); if( cur_submap == nullptr ) { debugmsg( "Tried to build obstacle cache at (%d,%d,%d) but the submap is not loaded", smx, smy, start.z ); @@ -9992,8 +10007,9 @@ bool map::build_floor_cache( const int zlev ) for( int smx = 0; smx < my_MAPSIZE; ++smx ) { for( int smy = 0; smy < my_MAPSIZE; ++smy ) { - const submap *cur_submap = get_submap_at_grid( tripoint_rel_sm{ smx, smy, zlev } ); - const submap *below_submap = !lowest_z_lev ? get_submap_at_grid( tripoint_rel_sm{ smx, smy, zlev - 1 } ) : + const tripoint_bub_sm_ib submap_pos{ smx, smy, zlev }; + const submap *cur_submap = get_submap_at_grid( submap_pos ); + const submap *below_submap = !lowest_z_lev ? get_submap_at_grid( tripoint_bub_sm_ib{ smx, smy, zlev - 1 } ) : nullptr; if( cur_submap == nullptr ) { @@ -10273,26 +10289,36 @@ const submap *map::get_submap_at( const tripoint_bub_ms &p ) const return unsafe_get_submap_at( p ); } -submap *map::get_submap_at_grid( const tripoint_rel_sm &gridp ) +submap *map::get_submap_at_grid( const tripoint_bub_sm &gridp ) { - return getsubmap( get_nonant( gridp ) ); + if( !inbounds( gridp ) ) { + debugmsg( "Tried to access invalid submap position at grid=%s", gridp.to_string() ); + return nullptr; + } + return get_submap_at_grid( tripoint_bub_sm_ib( gridp.raw() ) ); } -const submap *map::get_submap_at_grid( const tripoint_rel_sm &gridp ) const +const submap *map::get_submap_at_grid( const tripoint_bub_sm &gridp ) const +{ + if( !inbounds( gridp ) ) { + debugmsg( "Tried to access invalid submap position at grid=%s", gridp.to_string() ); + return nullptr; + } + return get_submap_at_grid( tripoint_bub_sm_ib( gridp.raw() ) ); +} + +submap *map::get_submap_at_grid( const tripoint_bub_sm_ib &gridp ) { return getsubmap( get_nonant( gridp ) ); } -size_t map::get_nonant( const tripoint_rel_sm &gridp ) const +const submap *map::get_submap_at_grid( const tripoint_bub_sm_ib &gridp ) const { - if( gridp.x() < 0 || gridp.x() >= my_MAPSIZE || - gridp.y() < 0 || gridp.y() >= my_MAPSIZE || - gridp.z() < -OVERMAP_DEPTH || gridp.z() > OVERMAP_HEIGHT ) { - debugmsg( "Tried to access invalid map position at grid (%d,%d,%d)", gridp.x(), gridp.y(), - gridp.z() ); - return 0; - } + return getsubmap( get_nonant( gridp ) ); +} +size_t map::get_nonant( const tripoint_bub_sm_ib &gridp ) const +{ if( zlevels ) { const int indexz = gridp.z() + OVERMAP_DEPTH; // Can't be lower than 0 return indexz + ( gridp.x() + gridp.y() * my_MAPSIZE ) * OVERMAP_LAYERS; @@ -10330,7 +10356,8 @@ void map::draw_fill_background( const ter_id &type ) // Fill each submap rather than each tile for( int gridx = 0; gridx < my_MAPSIZE; gridx++ ) { for( int gridy = 0; gridy < my_MAPSIZE; gridy++ ) { - submap *sm = get_submap_at_grid( point{gridx, gridy} ); + const tripoint_bub_sm_ib submap_pos{ gridx, gridy, abs_sub.z() }; + submap *sm = get_submap_at_grid( submap_pos ); if( sm == nullptr ) { debugmsg( "Tried to fill background at (%d,%d) but the submap is not loaded", gridx, gridy ); continue; @@ -10583,7 +10610,8 @@ void map::function_over( const tripoint &start, const tripoint &end, Functor fun for( z = min.z; z <= max.z; z++ ) { for( smx = min_sm.x; smx <= max_sm.x; smx++ ) { for( smy = min_sm.y; smy <= max_sm.y; smy++ ) { - submap const *cur_submap = get_submap_at_grid( { smx, smy, z } ); + const tripoint_bub_sm submap_pos{ smx, smy, z }; + submap const *cur_submap = get_submap_at_grid( submap_pos ); if( cur_submap == nullptr ) { debugmsg( "Tried to function over (%d,%d,%d) but the submap is not loaded", smx, smy, z ); continue; @@ -10760,7 +10788,7 @@ std::list map::get_active_items_in_radius( const tripoint ¢er std::min( maxp.y / SEEY, my_MAPSIZE - 1 ) ); for( const tripoint_abs_sm &abs_submap_loc : submaps_with_active_items ) { - const tripoint_rel_sm submap_loc = ( abs_submap_loc - abs_sub.xy() ); + const tripoint_bub_sm submap_loc { ( abs_submap_loc - abs_sub.xy() ).raw() }; if( submap_loc.x() < ming.x || submap_loc.y() < ming.y || submap_loc.x() > maxg.x || submap_loc.y() > maxg.y ) { continue; @@ -11076,7 +11104,8 @@ int map::calc_max_populated_zlev() bool level_done = false; for( int sx = 0; sx < my_MAPSIZE; sx++ ) { for( int sy = 0; sy < my_MAPSIZE; sy++ ) { - const submap *sm = get_submap_at_grid( { sx, sy, sz } ); + const tripoint_bub_sm_ib submap_pos{ sx, sy, sz }; + const submap *sm = get_submap_at_grid( submap_pos ); if( sm == nullptr ) { debugmsg( "Tried to calc max populated zlev at (%d,%d,%d) but the submap is not loaded", sx, sy, sz ); diff --git a/src/map.h b/src/map.h index d804021569ba5..0ee1c01c73164 100644 --- a/src/map.h +++ b/src/map.h @@ -2175,6 +2175,7 @@ class map bool inbounds( const point &p ) const { return inbounds( tripoint_bub_ms( p.x, p.y, 0 ) ); } + bool inbounds( const tripoint_bub_sm &p ) const; bool inbounds_z( const int z ) const { return z >= -OVERMAP_DEPTH && z <= OVERMAP_HEIGHT; @@ -2237,26 +2238,26 @@ class map void rotten_item_spawn( const item &item, const tripoint &p ); private: // Helper #1 - spawns monsters on one submap - void spawn_monsters_submap( const tripoint_rel_sm &gp, bool ignore_sight, + void spawn_monsters_submap( const tripoint_bub_sm_ib &gp, bool ignore_sight, bool spawn_nonlocal = false ); // Helper #2 - spawns monsters on one submap and from one group on this submap - void spawn_monsters_submap_group( const tripoint_rel_sm &gp, mongroup &group, + void spawn_monsters_submap_group( const tripoint_bub_sm_ib &gp, mongroup &group, bool ignore_sight ); protected: - void saven( const tripoint &grid ); - void loadn( const point &grid, bool update_vehicles ); + void saven( const tripoint_bub_sm_ib &grid ); + void loadn( const point_bub_sm_ib &grid, bool update_vehicles ); /** * Fast forward a submap that has just been loading into this map. * This is used to rot and remove rotten items, grow plants, fill funnels etc. */ - void actualize( const tripoint_rel_sm &grid ); + void actualize( const tripoint_bub_sm_ib &grid ); /** * Hacks in missing tree tops. It CAN be done in other ways, but it would be a lot of work * to get regional translation code to deal with chunks instead of terrain, and then use these * chunks everywhere instead of tree terrain tokens. Maybe some day... */ - void add_tree_tops( const tripoint_rel_sm &grid ); + void add_tree_tops( const tripoint_bub_sm_ib &grid ); /** * Try to fill funnel based items here. Simulates rain from @p since till now. * @param p The location in this map where to fill funnels. @@ -2301,7 +2302,7 @@ class map */ void shift_traps( const tripoint &shift ); - void copy_grid( const tripoint_rel_sm &to, const tripoint_rel_sm &from ); + void copy_grid( const tripoint_bub_sm_ib &to, const tripoint_bub_sm_ib &from ); void draw_map( mapgendata &dat ); void draw_lab( mapgendata &dat ); @@ -2368,7 +2369,7 @@ class map // TODO: fix point types (remove the first overload) inline submap *unsafe_get_submap_at( const tripoint &p ) { cata_assert( inbounds( p ) ); - return get_submap_at_grid( tripoint_rel_sm{ p.x / SEEX, p.y / SEEY, p.z } ); + return get_submap_at_grid( tripoint_bub_sm_ib{ p.x / SEEX, p.y / SEEY, p.z } ); } inline submap *unsafe_get_submap_at( const tripoint_bub_ms &p ) { return unsafe_get_submap_at( p.raw() ); @@ -2376,7 +2377,7 @@ class map // TODO: fix point types (remove the first overload) inline const submap *unsafe_get_submap_at( const tripoint &p ) const { cata_assert( inbounds( p ) ); - return get_submap_at_grid( { p.x / SEEX, p.y / SEEY, p.z } ); + return get_submap_at_grid( tripoint_bub_sm_ib{ p.x / SEEX, p.y / SEEY, p.z } ); } inline const submap *unsafe_get_submap_at( const tripoint_bub_ms &p ) const { return unsafe_get_submap_at( p.raw() ); @@ -2393,23 +2394,11 @@ class map * The same as other get_submap_at, (p) must be valid (@ref inbounds). * Also writes the position within the submap to offset_p */ - // TODO: fix point types (remove the first overload) - submap *unsafe_get_submap_at( const tripoint &p, point &offset_p ) { - offset_p.x = p.x % SEEX; - offset_p.y = p.y % SEEY; - return unsafe_get_submap_at( p ); - } submap *unsafe_get_submap_at( const tripoint_bub_ms p, point_sm_ms &offset_p ) { tripoint_bub_sm sm; std::tie( sm, offset_p ) = project_remain( p ); return unsafe_get_submap_at( p ); } - // TODO: fix point types (remove the first overload) - const submap *unsafe_get_submap_at( const tripoint &p, point &offset_p ) const { - offset_p.x = p.x % SEEX; - offset_p.y = p.y % SEEY; - return unsafe_get_submap_at( p ); - } const submap *unsafe_get_submap_at( const tripoint_bub_ms p, point_sm_ms &offset_p ) const { tripoint_bub_sm sm; @@ -2440,24 +2429,17 @@ class map * be valid: 0 <= x < my_MAPSIZE, same for y. * z must be between -OVERMAP_DEPTH and OVERMAP_HEIGHT */ - submap *get_submap_at_grid( const point &gridp ) { - return getsubmap( get_nonant( gridp ) ); - } - const submap *get_submap_at_grid( const point &gridp ) const { - return getsubmap( get_nonant( gridp ) ); - } - submap *get_submap_at_grid( const tripoint_rel_sm &gridp ); - const submap *get_submap_at_grid( const tripoint_rel_sm &gridp ) const; + submap *get_submap_at_grid( const tripoint_bub_sm &gridp ); + const submap *get_submap_at_grid( const tripoint_bub_sm &gridp ) const; + submap *get_submap_at_grid( const tripoint_bub_sm_ib &gridp ); + const submap *get_submap_at_grid( const tripoint_bub_sm_ib &gridp ) const; protected: /** * Get the index of a submap pointer in the grid given by grid coordinates. The grid * coordinates must be valid: 0 <= x < my_MAPSIZE, same for y. * Version with z-levels checks for z between -OVERMAP_DEPTH and OVERMAP_HEIGHT */ - size_t get_nonant( const tripoint_rel_sm &gridp ) const; - size_t get_nonant( const point &gridp ) const { - return get_nonant( tripoint_rel_sm{ gridp.x, gridp.y, abs_sub.z() } ); - } + size_t get_nonant( const tripoint_bub_sm_ib &gridp ) const; /** * Set the submap pointer in @ref grid at the give index. This is the inverse of * @ref getsubmap, any existing pointer is overwritten. The index must be valid. @@ -2540,7 +2522,7 @@ class map void process_items(); private: // Iterates over every item on the map, passing each item to the provided function. - void process_items_in_submap( submap ¤t_submap, const tripoint_rel_sm &gridp ); + void process_items_in_submap( submap ¤t_submap, const tripoint_bub_sm &gridp ); void process_items_in_vehicles( submap ¤t_submap ); void process_items_in_vehicle( vehicle &cur_veh, submap ¤t_submap ); diff --git a/src/map_field.cpp b/src/map_field.cpp index ce4dfcde57821..776a6ebd777a5 100644 --- a/src/map_field.cpp +++ b/src/map_field.cpp @@ -169,7 +169,8 @@ void map::process_fields() for( int x = 0; x < my_MAPSIZE; x++ ) { for( int y = 0; y < my_MAPSIZE; y++ ) { if( field_cache[ x + y * MAPSIZE ] ) { - submap *const current_submap = get_submap_at_grid( { x, y, z } ); + const tripoint_bub_sm_ib submap_pos{ x, y, z }; + submap *const current_submap = get_submap_at_grid( submap_pos ); if( current_submap == nullptr ) { debugmsg( "Tried to process field at (%d,%d,%d) but the submap is not loaded", x, y, z ); continue; diff --git a/src/mapgen.cpp b/src/mapgen.cpp index b8fa823af8981..33a9db145c4b5 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -214,6 +214,11 @@ static const trap_str_id tr_telepad( "tr_telepad" ); static const vproto_id vehicle_prototype_shopping_cart( "shopping_cart" ); +static const point_bub_sm_ib p_sm_zero{ point_zero }; +static const point_bub_sm_ib p_sm_east{ point_east }; +static const point_bub_sm_ib p_sm_south_east{ point_south_east }; +static const point_bub_sm_ib p_sm_south{ point_south }; + #define dbg(x) DebugLog((x),D_MAP_GEN) << __FILE__ << ":" << __LINE__ << ": " static constexpr int MON_RADIUS = 3; @@ -236,10 +241,11 @@ void map::generate( const tripoint_abs_omt &p, const time_point &when, bool save for( int gridx = 0; gridx < my_MAPSIZE; gridx++ ) { for( int gridy = 0; gridy < my_MAPSIZE; gridy++ ) { for( int gridz = -OVERMAP_DEPTH; gridz <= OVERMAP_HEIGHT; gridz++ ) { - const tripoint_rel_sm pos( gridx, gridy, gridz ); + const tripoint_bub_sm_ib pos( gridx, gridy, gridz ); + const tripoint_abs_sm pos_abs = p_sm_base.xy() + pos.raw(); const size_t grid_pos = get_nonant( pos ); // For some reason 'emplace' doesn't work. emplacing data later overwrote data... - generated[grid_pos] = MAPBUFFER.submap_exists( p_sm_base.xy() + pos ); + generated[grid_pos] = MAPBUFFER.submap_exists( pos_abs ); if( !generated.at( grid_pos ) || !save_results ) { setsubmap( grid_pos, new submap() ); @@ -252,7 +258,7 @@ void map::generate( const tripoint_abs_omt &p, const time_point &when, bool save getsubmap( grid_pos )->last_touched = calendar::turn; } } else { - setsubmap( grid_pos, MAPBUFFER.lookup_submap( p_sm_base.xy() + pos ) ); + setsubmap( grid_pos, MAPBUFFER.lookup_submap( pos_abs ) ); } } } @@ -277,7 +283,7 @@ void map::generate( const tripoint_abs_omt &p, const time_point &when, bool save for( int gridx = 0; gridx <= 1; gridx++ ) { for( int gridy = 0; gridy <= 1; gridy++ ) { - const tripoint_rel_sm pos( gridx, gridy, gridz ); + const tripoint_bub_sm_ib pos( gridx, gridy, gridz ); const size_t grid_pos = get_nonant( pos ); if( ( !generated.at( grid_pos ) || !save_results ) && @@ -302,10 +308,14 @@ void map::generate( const tripoint_abs_omt &p, const time_point &when, bool save density = density / 100; // Not sure if we actually have to check all submaps. - const bool any_missing = !generated.at( get_nonant( { point_rel_sm_zero, p_sm.z() } ) ) || - !generated.at( get_nonant( { point_rel_sm_east, p_sm.z() } ) ) || - !generated.at( get_nonant( { point_rel_sm_south_east, p_sm.z() } ) ) || - !generated.at( get_nonant( { point_rel_sm_south, p_sm.z() } ) ); + const tripoint_bub_sm_ib p_zero { p_sm_zero, gridz }; + const tripoint_bub_sm_ib p_east { p_sm_east, gridz }; + const tripoint_bub_sm_ib p_south_east { p_sm_south_east, gridz }; + const tripoint_bub_sm_ib p_south { p_sm_south, gridz }; + const bool any_missing = !generated.at( get_nonant( p_zero ) ) || + !generated.at( get_nonant( p_east ) ) || + !generated.at( get_nonant( p_south_east ) ) || + !generated.at( get_nonant( p_south ) ); mapgendata dat( { p.xy(), gridz}, *this, density, when, nullptr ); if( ( any_missing || !save_results ) && @@ -316,7 +326,7 @@ void map::generate( const tripoint_abs_omt &p, const time_point &when, bool save // Merge the overlays generated earlier into the current Z level now we have the base map on it. for( int gridx = 0; gridx <= 1; gridx++ ) { for( int gridy = 0; gridy <= 1; gridy++ ) { - const tripoint_rel_sm pos( gridx, gridy, gridz ); + const tripoint_bub_sm_ib pos( gridx, gridy, gridz ); const size_t index = gridx + gridy * 2; if( saved_overlay.at( index ) != nullptr ) { const size_t grid_pos = get_nonant( pos ); @@ -392,11 +402,11 @@ void map::generate( const tripoint_abs_omt &p, const time_point &when, bool save for( int gridx = 0; gridx < my_MAPSIZE; gridx++ ) { for( int gridy = 0; gridy < my_MAPSIZE; gridy++ ) { for( int gridz = -OVERMAP_DEPTH; gridz <= OVERMAP_HEIGHT; gridz++ ) { - const tripoint_rel_sm pos( gridx, gridy, gridz ); + const tripoint_bub_sm_ib pos( gridx, gridy, gridz ); const size_t grid_pos = get_nonant( pos ); if( !generated.at( grid_pos ) ) { if( gridx <= 1 && gridy <= 1 ) { - saven( { gridx, gridy, gridz } ); + saven( pos ); } else { delete getsubmap( grid_pos ); } @@ -6905,7 +6915,7 @@ vehicle *map::add_vehicle( const vproto_id &type, const tripoint &p, const units 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( tripoint_bub_sm( 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 ); @@ -7186,10 +7196,10 @@ void map::rotate( int turns, const bool setpos_safe ) for( int z_level = bottom_level; z_level <= top_level; z_level++ ) { clear_vehicle_list( z_level ); - submap *pz = get_submap_at_grid( { point_rel_sm_zero, z_level } ); - submap *pse = get_submap_at_grid( { point_rel_sm_south_east, z_level } ); - submap *pe = get_submap_at_grid( { point_rel_sm_east, z_level } ); - submap *ps = get_submap_at_grid( { point_rel_sm_south, z_level } ); + submap *pz = get_submap_at_grid( tripoint_bub_sm_ib{ p_sm_zero, z_level } ); + submap *pse = get_submap_at_grid( tripoint_bub_sm_ib{ p_sm_south_east, z_level } ); + submap *pe = get_submap_at_grid( tripoint_bub_sm_ib{ p_sm_east, z_level } ); + submap *ps = get_submap_at_grid( tripoint_bub_sm_ib{ p_sm_south, z_level } ); if( pz == nullptr || pse == nullptr || pe == nullptr || ps == nullptr ) { debugmsg( "Tried to rotate map at (%d,%d) but the submap is not loaded", point_zero.x, point_zero.y ); @@ -7209,7 +7219,7 @@ void map::rotate( int turns, const bool setpos_safe ) for( int k = 0; k < 4; ++k ) { p = p.rotate( turns, { 2, 2 } ); point tmpp = point_south_east - p; - submap *psep = get_submap_at_grid( tripoint_rel_sm( tmpp.x, tmpp.y, z_level ) ); + submap *psep = get_submap_at_grid( tripoint_bub_sm( tmpp.x, tmpp.y, z_level ) ); if( psep == nullptr ) { debugmsg( "Tried to rotate map at (%d,%d) but the submap is not loaded", tmpp.x, tmpp.y ); continue; @@ -7222,7 +7232,7 @@ void map::rotate( int turns, const bool setpos_safe ) for( int j = 0; j < 2; ++j ) { for( int i = 0; i < 2; ++i ) { point p( i, j ); - submap *sm = get_submap_at_grid( tripoint_rel_sm( p.x, p.y, z_level ) ); + submap *sm = get_submap_at_grid( tripoint_bub_sm( p.x, p.y, z_level ) ); if( sm == nullptr ) { debugmsg( "Tried to rotate map at (%d,%d) but the submap is not loaded", p.x, p.y ); continue; @@ -7280,11 +7290,11 @@ void map::mirror( bool mirror_horizontal, bool mirror_vertical ) for( int z_level = zlevels ? -OVERMAP_DEPTH : abs_sub.z(); z_level <= ( zlevels ? OVERMAP_HEIGHT : abs_sub.z() ); z_level++ ) { - submap *pz = get_submap_at_grid( { point_rel_sm_zero, z_level } ); - submap *pse = get_submap_at_grid( { point_rel_sm_south_east, z_level } ); - submap *pe = get_submap_at_grid( {point_rel_sm_east, z_level - } ); - submap *ps = get_submap_at_grid( { point_rel_sm_south, z_level } ); + + submap *pz = get_submap_at_grid( tripoint_bub_sm_ib{ p_sm_zero, z_level } ); + submap *pse = get_submap_at_grid( tripoint_bub_sm_ib{ p_sm_south_east, z_level } ); + submap *pe = get_submap_at_grid( tripoint_bub_sm_ib{ p_sm_east, z_level } ); + submap *ps = get_submap_at_grid( tripoint_bub_sm_ib{ p_sm_south, z_level } ); if( pz == nullptr || pse == nullptr || pe == nullptr || ps == nullptr ) { debugmsg( "Tried to mirror map at (%d, %d, %d) but the submap is not loaded", point_zero.x, point_zero.y, z_level ); @@ -7304,8 +7314,8 @@ void map::mirror( bool mirror_horizontal, bool mirror_vertical ) // Then mirror them. for( int j = 0; j < 2; ++j ) { for( int i = 0; i < 2; ++i ) { - point_rel_sm p( i, j ); - submap *sm = get_submap_at_grid( { p, z_level } ); + const tripoint_bub_sm_ib p( i, j, z_level ); + submap *sm = get_submap_at_grid( p ); if( sm == nullptr ) { debugmsg( "Tried to mirror map at (%d, %d, %d) but the submap is not loaded", p.x(), p.y(), z_level ); diff --git a/tests/map_test.cpp b/tests/map_test.cpp index e9b9f86447364..9ea9720bce1d2 100644 --- a/tests/map_test.cpp +++ b/tests/map_test.cpp @@ -150,12 +150,13 @@ void map::check_submap_active_item_consistency() for( int z = -OVERMAP_DEPTH; z < OVERMAP_HEIGHT; ++z ) { for( int x = 0; x < MAPSIZE; ++x ) { for( int y = 0; y < MAPSIZE; ++y ) { - tripoint_rel_sm p( x, y, z ); + const tripoint_bub_sm p( x, y, z ); + const tripoint_abs_sm p_abs = p.raw() + abs_sub.xy(); submap *s = get_submap_at_grid( p ); REQUIRE( s != nullptr ); bool submap_has_active_items = !s->active_items.empty(); - bool cache_has_active_items = submaps_with_active_items.count( p + abs_sub.xy() ) != 0; - CAPTURE( abs_sub.xy(), p, p + abs_sub.xy() ); + bool cache_has_active_items = submaps_with_active_items.count( p_abs ) != 0; + CAPTURE( abs_sub.xy(), p, p_abs ); CHECK( submap_has_active_items == cache_has_active_items ); } }