Skip to content

Commit

Permalink
Merge pull request #37893 from ifreund/submap-encapsulation
Browse files Browse the repository at this point in the history
Improve submap encapsulation
  • Loading branch information
Rivet-the-Zombie authored Feb 13, 2020
2 parents 4eca2cd + bb27d5a commit 3a8c5fb
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 47 deletions.
14 changes: 7 additions & 7 deletions src/lightmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ bool map::build_transparency_cache( const int zlev )
continue;
}

if( !( cur_submap->ter[sx][sy].obj().transparent &&
cur_submap->frn[sx][sy].obj().transparent ) ) {
if( !( cur_submap->get_ter( { sx, sy } ).obj().transparent &&
cur_submap->get_furn( {sx, sy } ).obj().transparent ) ) {
value = LIGHT_TRANSPARENCY_SOLID;
zero_value = LIGHT_TRANSPARENCY_SOLID;
continue;
Expand All @@ -126,7 +126,7 @@ bool map::build_transparency_cache( const int zlev )
zero_value = value;
continue;
}
for( const auto &fld : cur_submap->fld[sx][sy] ) {
for( const auto &fld : cur_submap->get_field( { sx, sy } ) ) {
const field_entry &cur = fld.second;
if( cur.is_transparent() ) {
continue;
Expand Down Expand Up @@ -317,21 +317,21 @@ void map::generate_lightmap( const int zlev )
}
}

if( cur_submap->lum[sx][sy] && has_items( p ) ) {
if( cur_submap->get_lum( { sx, sy } ) && has_items( p ) ) {
auto items = i_at( p );
add_light_from_items( p, items.begin(), items.end() );
}

const ter_id terrain = cur_submap->ter[sx][sy];
const ter_id terrain = cur_submap->get_ter( { sx, sy } );
if( terrain->light_emitted > 0 ) {
add_light_source( p, terrain->light_emitted );
}
const furn_id furniture = cur_submap->frn[sx][sy];
const furn_id furniture = cur_submap->get_furn( {sx, sy } );
if( furniture->light_emitted > 0 ) {
add_light_source( p, furniture->light_emitted );
}

for( auto &fld : cur_submap->fld[sx][sy] ) {
for( auto &fld : cur_submap->get_field( { sx, sy } ) ) {
const field_entry *cur = &fld.second;
const int light_emitted = cur->light_emitted();
if( light_emitted > 0 ) {
Expand Down
50 changes: 24 additions & 26 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2521,7 +2521,7 @@ void map::decay_fields_and_scent( const time_duration &amount )
const int x = sx + smx * SEEX;
const int y = sy + smy * SEEY;

field &fields = cur_submap->fld[sx][sy];
field &fields = cur_submap->get_field( { sx, sy} );
if( !outside_cache[x][y] ) {
to_proc -= fields.field_count();
continue;
Expand Down Expand Up @@ -3901,7 +3901,7 @@ map_stack map::i_at( const tripoint &p )
point l;
submap *const current_submap = get_submap_at( p, l );

return map_stack{ &current_submap->itm[l.x][l.y], p, this };
return map_stack{ &current_submap->get_items( l ), p, this };
}

map_stack::iterator map::i_rem( const tripoint &p, map_stack::const_iterator it )
Expand All @@ -3917,7 +3917,7 @@ map_stack::iterator map::i_rem( const tripoint &p, map_stack::const_iterator it

current_submap->update_lum_rem( l, *it );

return current_submap->itm[l.x][l.y].erase( it );
return current_submap->get_items( l ).erase( it );
}

void map::i_rem( const tripoint &p, item *it )
Expand All @@ -3934,16 +3934,16 @@ void map::i_clear( const tripoint &p )
point l;
submap *const current_submap = get_submap_at( p, l );

for( item &it : current_submap->itm[l.x][l.y] ) {
for( item &it : current_submap->get_items( l ) ) {
// remove from the active items cache (if it isn't there does nothing)
current_submap->active_items.remove( &it );
}
if( current_submap->active_items.empty() ) {
submaps_with_active_items.erase( tripoint( abs_sub.x + p.x / SEEX, abs_sub.y + p.y / SEEY, p.z ) );
}

current_submap->lum[l.x][l.y] = 0;
current_submap->itm[l.x][l.y].clear();
current_submap->set_lum( l, 0 );
current_submap->get_items( l ).clear();
}

item &map::spawn_an_item( const tripoint &p, item new_item,
Expand Down Expand Up @@ -4171,7 +4171,7 @@ item &map::add_item( const tripoint &p, item new_item )
current_submap->is_uniform = false;
current_submap->update_lum_add( l, new_item );

const map_stack::iterator new_pos = current_submap->itm[l.x][l.y].insert( new_item );
const map_stack::iterator new_pos = current_submap->get_items( l ).insert( new_item );
if( new_item.needs_processing() ) {
if( current_submap->active_items.empty() ) {
submaps_with_active_items.insert( tripoint( abs_sub.x + p.x / SEEX, abs_sub.y + p.y / SEEY, p.z ) );
Expand Down Expand Up @@ -4233,7 +4233,7 @@ void map::make_active( item_location &loc )
}
point l;
submap *const current_submap = get_submap_at( loc.position(), l );
cata::colony<item> &item_stack = current_submap->itm[l.x][l.y];
cata::colony<item> &item_stack = current_submap->get_items( l );
cata::colony<item>::iterator iter = item_stack.get_iterator_from_pointer( target );

if( current_submap->active_items.empty() ) {
Expand Down Expand Up @@ -4600,7 +4600,7 @@ bool map::has_items( const tripoint &p ) const
point l;
submap *const current_submap = get_submap_at( p, l );

return !current_submap->itm[l.x][l.y].empty();
return !current_submap->get_items( l ).empty();
}

template <typename Stack>
Expand Down Expand Up @@ -5118,7 +5118,7 @@ const field &map::field_at( const tripoint &p ) const
point l;
submap *const current_submap = get_submap_at( p, l );

return current_submap->fld[l.x][l.y];
return current_submap->get_field( l );
}

/*
Expand All @@ -5134,7 +5134,7 @@ field &map::field_at( const tripoint &p )
point l;
submap *const current_submap = get_submap_at( p, l );

return current_submap->fld[l.x][l.y];
return current_submap->get_field( l );
}

time_duration map::mod_field_age( const tripoint &p, const field_type_id &type,
Expand Down Expand Up @@ -5202,7 +5202,7 @@ field_entry *map::get_field( const tripoint &p, const field_type_id &type )
point l;
submap *const current_submap = get_submap_at( p, l );

return current_submap->fld[l.x][l.y].find_field( type );
return current_submap->get_field( l ).find_field( type );
}

bool map::dangerous_field_at( const tripoint &p )
Expand Down Expand Up @@ -5239,7 +5239,7 @@ bool map::add_field( const tripoint &p, const field_type_id &type, int intensity
submap *const current_submap = get_submap_at( p, l );
current_submap->is_uniform = false;

if( current_submap->fld[l.x][l.y].add_field( type, intensity, age ) ) {
if( current_submap->get_field( l ).add_field( type, intensity, age ) ) {
//Only adding it to the count if it doesn't exist.
if( !current_submap->field_count++ ) {
get_cache( p.z ).field_cache.set( static_cast<size_t>( p.x / SEEX + ( (
Expand Down Expand Up @@ -5276,7 +5276,7 @@ void map::remove_field( const tripoint &p, const field_type_id &field_to_remove
point l;
submap *const current_submap = get_submap_at( p, l );

if( current_submap->fld[l.x][l.y].remove_field( field_to_remove ) ) {
if( current_submap->get_field( l ).remove_field( field_to_remove ) ) {
// Only adjust the count if the field actually existed.
if( !--current_submap->field_count ) {
get_cache( p.z ).field_cache.set( static_cast<size_t>( p.x / SEEX + ( (
Expand Down Expand Up @@ -6554,12 +6554,11 @@ static void generate_uniform( const tripoint &p, const ter_id &terrain_type )
dbg( D_INFO ) << "generate_uniform p: " << p
<< " terrain_type: " << terrain_type.id().str();

constexpr size_t block_size = SEEX * SEEY;
for( int xd = 0; xd <= 1; xd++ ) {
for( int yd = 0; yd <= 1; yd++ ) {
submap *sm = new submap();
sm->is_uniform = true;
std::uninitialized_fill_n( &sm->ter[0][0], block_size, terrain_type );
sm->set_all_ter( terrain_type );
sm->last_touched = calendar::turn;
MAPBUFFER.add_submap( p + point( xd, yd ), sm );
}
Expand Down Expand Up @@ -7036,7 +7035,7 @@ void map::actualize( const tripoint &grid )
}
// plants contain a seed item which must not be removed under any circumstances
if( !furn.has_flag( "DONT_REMOVE_ROTTEN" ) ) {
remove_rotten_items( tmpsub->itm[x][y], pnt );
remove_rotten_items( tmpsub->get_items( { x, y } ), pnt );
}

const auto trap_here = tmpsub->get_trap( p );
Expand Down Expand Up @@ -7095,21 +7094,21 @@ void map::add_roofs( const tripoint &grid )

for( int x = 0; x < SEEX; x++ ) {
for( int y = 0; y < SEEY; y++ ) {
const ter_id ter_here = sub_here->ter[x][y];
const ter_id ter_here = sub_here->get_ter( { x, y } );
if( ter_here != t_open_air ) {
continue;
}

if( !check_roof ) {
// Make sure we don't have open air at lowest z-level
sub_here->ter[x][y] = t_rock_floor;
sub_here->set_ter( { x, y }, t_rock_floor );
continue;
}

const ter_t &ter_below = sub_below->ter[x][y].obj();
const ter_t &ter_below = sub_below->get_ter( { x, y } ).obj();
if( ter_below.roof ) {
// TODO: Make roof variable a ter_id to speed this up
sub_here->ter[x][y] = ter_below.roof.id();
sub_here->set_ter( { x, y }, ter_below.roof.id() );
}
}
}
Expand Down Expand Up @@ -7395,9 +7394,9 @@ fake_map::fake_map( const furn_id &fur_type, const ter_id &ter_type, const trap_
for( int gridy = 0; gridy < my_MAPSIZE; gridy++ ) {
std::unique_ptr<submap> sm = std::make_unique<submap>();

std::uninitialized_fill_n( &sm->ter[0][0], SEEX * SEEY, ter_type );
std::uninitialized_fill_n( &sm->frn[0][0], SEEX * SEEY, fur_type );
std::uninitialized_fill_n( &sm->trp[0][0], SEEX * SEEY, trap_type );
sm->set_all_ter( ter_type );
sm->set_all_furn( fur_type );
sm->set_all_traps( trap_type );

setsubmap( get_nonant( { gridx, gridy, fake_map_z } ), sm.get() );

Expand Down Expand Up @@ -7857,12 +7856,11 @@ void map::draw_fill_background( const ter_id &type )
set_pathfinding_cache_dirty( abs_sub.z );

// Fill each submap rather than each tile
constexpr size_t block_size = SEEX * SEEY;
for( int gridx = 0; gridx < my_MAPSIZE; gridx++ ) {
for( int gridy = 0; gridy < my_MAPSIZE; gridy++ ) {
auto sm = get_submap_at_grid( {gridx, gridy} );
sm->is_uniform = true;
std::uninitialized_fill_n( &sm->ter[0][0], block_size, type );
sm->set_all_ter( type );
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/map_field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ bool map::process_fields_in_submap( submap *const current_submap,
const tripoint &p = thep;
// Get a reference to the field variable from the submap;
// contains all the pointers to the real field effects.
field &curfield = current_submap->fld[locx][locy];
field &curfield = current_submap->get_field( { static_cast<int>( locx ), static_cast<int>( locy ) } );
for( auto it = curfield.begin(); it != curfield.end(); ) {
// Iterating through all field effects in the submap's field.
field_entry &cur = it->second;
Expand Down
2 changes: 0 additions & 2 deletions src/submap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ void maptile_soa<sx, sy>::swap_soa_tile( const point &p, maptile_soa<1, 1> &othe

submap::submap()
{
constexpr size_t elements = SEEX * SEEY;

std::uninitialized_fill_n( &ter[0][0], elements, t_null );
std::uninitialized_fill_n( &frn[0][0], elements, f_null );
std::uninitialized_fill_n( &lum[0][0], elements, 0 );
Expand Down
53 changes: 47 additions & 6 deletions src/submap.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct maptile_soa {
void swap_soa_tile( const point &p, maptile_soa<1, 1> &other );
};

class submap : public maptile_soa<SEEX, SEEY> // TODO: Use private inheritance.
class submap : maptile_soa<SEEX, SEEY>
{
public:
submap();
Expand All @@ -73,6 +73,10 @@ class submap : public maptile_soa<SEEX, SEEY> // TODO: Use private inheritanc
trp[p.x][p.y] = trap;
}

void set_all_traps( const trap_id &trap ) {
std::uninitialized_fill_n( &trp[0][0], elements, trap );
}

furn_id get_furn( const point &p ) const {
return frn[p.x][p.y];
}
Expand All @@ -82,6 +86,10 @@ class submap : public maptile_soa<SEEX, SEEY> // TODO: Use private inheritanc
frn[p.x][p.y] = furn;
}

void set_all_furn( const furn_id &furn ) {
std::uninitialized_fill_n( &frn[0][0], elements, furn );
}

ter_id get_ter( const point &p ) const {
return ter[p.x][p.y];
}
Expand All @@ -91,6 +99,10 @@ class submap : public maptile_soa<SEEX, SEEY> // TODO: Use private inheritanc
ter[p.x][p.y] = terr;
}

void set_all_ter( const ter_id &terr ) {
std::uninitialized_fill_n( &ter[0][0], elements, terr );
}

int get_radiation( const point &p ) const {
return rad[p.x][p.y];
}
Expand All @@ -100,6 +112,15 @@ class submap : public maptile_soa<SEEX, SEEY> // TODO: Use private inheritanc
rad[p.x][p.y] = radiation;
}

uint8_t get_lum( const point &p ) const {
return lum[p.x][p.y];
}

void set_lum( const point &p, uint8_t luminance ) {
is_uniform = false;
lum[p.x][p.y] = luminance;
}

void update_lum_add( const point &p, const item &i ) {
is_uniform = false;
if( i.is_emissive() && lum[p.x][p.y] < 255 ) {
Expand Down Expand Up @@ -130,6 +151,24 @@ class submap : public maptile_soa<SEEX, SEEY> // TODO: Use private inheritanc
}
}

// TODO: Replace this as it essentially makes itm public
cata::colony<item> &get_items( const point &p ) {
return itm[p.x][p.y];
}

const cata::colony<item> &get_items( const point &p ) const {
return itm[p.x][p.y];
}

// TODO: Replace this as it essentially makes fld public
field &get_field( const point &p ) {
return fld[p.x][p.y];
}

const field &get_field( const point &p ) const {
return fld[p.x][p.y];
}

struct cosmetic_t {
point pos;
std::string type;
Expand Down Expand Up @@ -209,6 +248,8 @@ class submap : public maptile_soa<SEEX, SEEY> // TODO: Use private inheritanc
int temperature = 0;

void update_legacy_computer();

static constexpr size_t elements = SEEX * SEEY;
};

/**
Expand Down Expand Up @@ -256,16 +297,16 @@ struct maptile {
}

const field &get_field() const {
return sm->fld[x][y];
return sm->get_field( pos() );
}

field_entry *find_field( const field_type_id &field_to_find ) {
return sm->fld[x][y].find_field( field_to_find );
return sm->get_field( pos() ).find_field( field_to_find );
}

bool add_field( const field_type_id &field_to_add, const int new_intensity,
const time_duration &new_age ) {
const bool ret = sm->fld[x][y].add_field( field_to_add, new_intensity, new_age );
const bool ret = sm->get_field( pos() ).add_field( field_to_add, new_intensity, new_age );
if( ret ) {
sm->field_count++;
}
Expand Down Expand Up @@ -295,12 +336,12 @@ struct maptile {

// For map::draw_maptile
size_t get_item_count() const {
return sm->itm[x][y].size();
return sm->get_items( pos() ).size();
}

// Assumes there is at least one item
const item &get_uppermost_item() const {
return *std::prev( sm->itm[x][y].cend() );
return *std::prev( sm->get_items( pos() ).cend() );
}
};

Expand Down
Loading

0 comments on commit 3a8c5fb

Please sign in to comment.