Skip to content

Commit

Permalink
Make map::field_at return a const reference.
Browse files Browse the repository at this point in the history
Code outside the map is not supposed to change the fields directly, instead one should use the field related functions of the map class.

Add a non-const get_field function for non-const access the fields.
That function is protected, so it's only usable from inside the class.

Added wrapper for for const access to the field class: iterator begin/end and findField.
  • Loading branch information
BevapDin committed Oct 14, 2014
1 parent bb90b0c commit c6ee5d5
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/cata_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ bool cata_tiles::draw_trap(int x, int y)
bool cata_tiles::draw_field_or_item(int x, int y)
{
// check for field
field &f = g->m.field_at(x, y);
const field &f = g->m.field_at(x, y);
// check for items
const std::vector<item> &items = g->m.i_at(x, y);
field_id f_id = f.fieldSymbol();
Expand Down
14 changes: 7 additions & 7 deletions src/editmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ void editmap_hilight::draw( editmap * hm, bool update ) {
t_sym = furniture_type.sym;
t_col = furniture_type.color;
}
field *t_field = &g->m.field_at(x, y);
const field *t_field = &g->m.field_at(x, y);
if ( t_field->fieldCount() > 0 ) {
field_id t_ftype = t_field->fieldSymbol();
field_entry *t_fld = t_field->findField( t_ftype );
const field_entry *t_fld = t_field->findField( t_ftype );
if ( t_fld != NULL ) {
t_col = fieldlist[t_ftype].color[t_fld->getFieldDensity()-1];
t_sym = fieldlist[t_ftype].sym;
Expand Down Expand Up @@ -427,7 +427,7 @@ void editmap::update_view(bool update_info)
target_frn = g->m.furn(target.x, target.y);
furn_t furniture_type = furnlist[target_frn];

cur_field = &g->m.field_at(target.x, target.y);
cur_field = &g->m.get_field(target.x, target.y);
cur_trap = g->m.tr_at(target.x, target.y);
const Creature *critter = g->critter_at( target.x, target.y );

Expand Down Expand Up @@ -465,10 +465,10 @@ void editmap::update_view(bool update_info)
t_sym = furniture_type.sym;
t_col = furniture_type.color;
}
field *t_field = &g->m.field_at(x, y);
const field *t_field = &g->m.field_at(x, y);
if ( t_field->fieldCount() > 0 ) {
field_id t_ftype = t_field->fieldSymbol();
field_entry *t_fld = t_field->findField( t_ftype );
const field_entry *t_fld = t_field->findField( t_ftype );
if ( t_fld != NULL ) {
t_col = fieldlist[t_ftype].color[t_fld->getFieldDensity()-1];
t_sym = fieldlist[t_ftype].sym;
Expand Down Expand Up @@ -1018,7 +1018,7 @@ int editmap::edit_fld()
if ( fdens != fsel_dens || target_list.size() > 1 ) {
for(std::vector<point>::iterator it = target_list.begin();
it != target_list.end(); ++it) {
field *t_field = &g->m.field_at(it->x, it->y);
field *t_field = &g->m.get_field(it->x, it->y);
field_entry *t_fld = t_field->findField((field_id)idx);
int t_dens = 0;
if ( t_fld != NULL ) {
Expand All @@ -1044,7 +1044,7 @@ int editmap::edit_fld()
} else if ( fmenu.selected == 0 && fmenu.keypress == '\n' ) {
for(std::vector<point>::iterator it = target_list.begin();
it != target_list.end(); ++it) {
field *t_field = &g->m.field_at(it->x, it->y);
field *t_field = &g->m.get_field(it->x, it->y);
if ( t_field->fieldCount() > 0 ) {
for ( auto field_list_it = t_field->begin();
field_list_it != t_field->end(); /* noop */ ) {
Expand Down
45 changes: 30 additions & 15 deletions src/field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ void map::spread_gas( field_entry *cur, int x, int y, field_id curtype,
int current_age = cur->getFieldAge();
if (current_density > 1 && current_age > 0 && !spread.empty()) {
point p = spread[ rng( 0, spread.size() - 1 ) ];
field_entry *candidate_field = field_at(p.x, p.y).findField( curtype );
field_entry *candidate_field = get_field(p.x, p.y).findField( curtype );
int candidate_density = candidate_field ? candidate_field->getFieldDensity() : 0;
// Nearby gas grows thicker, and ages are shared.
int age_fraction = 0.5 + current_age / current_density;
Expand All @@ -381,7 +381,7 @@ void map::spread_gas( field_entry *cur, int x, int y, field_id curtype,
cur->setFieldAge(current_age - age_fraction);
// Or, just create a new field.
} else if ( add_field( p.x, p.y, curtype, 1 ) ) {
field_at(p.x, p.y).findField( curtype )->setFieldAge(age_fraction);
get_field(p.x, p.y).findField( curtype )->setFieldAge(age_fraction);
cur->setFieldDensity( current_density - 1 );
cur->setFieldAge(current_age - age_fraction);
}
Expand Down Expand Up @@ -458,7 +458,7 @@ bool map::process_fields_in_submap( submap *const current_submap,
field &curfield = current_submap->fld[locx][locy];
for( auto it = curfield.begin(); it != curfield.end();) {
//Iterating through all field effects in the submap's field.
field_entry * cur = const_cast<field_entry*>( &it->second );
field_entry * cur = &it->second;

curtype = cur->getFieldType();
// Setting our return value. fd_null really doesn't exist anymore,
Expand Down Expand Up @@ -812,7 +812,7 @@ bool map::process_fields_in_submap( submap *const current_submap,
for (int j = 0; j < 3 && cur->getFieldAge() < 0; j++) {
int fx = x + ((i + starti) % 3) - 1;
int fy = y + ((j + startj) % 3) - 1;
tmpfld = field_at(fx, fy).findField(fd_fire);
tmpfld = get_field(fx, fy).findField(fd_fire);
if (tmpfld && tmpfld != cur && cur->getFieldAge() < 0 &&
tmpfld->getFieldDensity() < 3 &&
(in_pit == (ter(fx, fy) == t_pit))) {
Expand Down Expand Up @@ -879,7 +879,7 @@ bool map::process_fields_in_submap( submap *const current_submap,
for (int j = 0; j < 3; j++) {
int fx = x + ((i + starti) % 3) - 1, fy = y + ((j + startj) % 3) - 1;
if (INBOUNDS(fx, fy)) {
field &nearby_field = field_at(fx, fy);
field &nearby_field = get_field(fx, fy);
field_entry *nearwebfld = nearby_field.findField(fd_web);
int spread_chance = 25 * (cur->getFieldDensity() - 1);
if (nearwebfld) {
Expand Down Expand Up @@ -1048,7 +1048,7 @@ bool map::process_fields_in_submap( submap *const current_submap,
case fd_gas_vent:
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
field &wandering_field = field_at(i, j);
field &wandering_field = get_field(i, j);
tmpfld = wandering_field.findField(fd_toxic_gas);
if (tmpfld && tmpfld->getFieldDensity() < 3) {
tmpfld->setFieldDensity(tmpfld->getFieldDensity() + 1);
Expand Down Expand Up @@ -1111,7 +1111,7 @@ bool map::process_fields_in_submap( submap *const current_submap,
}
if (valid.empty()) { // Spread to adjacent space, then
int px = x + rng(-1, 1), py = y + rng(-1, 1);
field_entry *elec = field_at( px, py ).findField( fd_electricity );
field_entry *elec = get_field( px, py ).findField( fd_electricity );
if (move_cost(px, py) > 0 && elec != nullptr &&
elec->getFieldDensity() < 3) {
elec->setFieldDensity( elec->getFieldDensity() + 1 );
Expand Down Expand Up @@ -1293,7 +1293,7 @@ bool map::process_fields_in_submap( submap *const current_submap,
squares_in_direction( x, y, g->u.xpos(), g->u.ypos() );
for( auto new_position = candidate_positions.begin();
new_position != candidate_positions.end(); ++new_position ) {
field &target_field = field_at( new_position->x,
field &target_field = get_field( new_position->x,
new_position->y );
// Only shift if there are no bees already there.
// TODO: Figure out a way to merge bee fields without allowing
Expand Down Expand Up @@ -1385,7 +1385,7 @@ If you wish for a field effect to do something over time (propagate, interact wi
void map::step_in_field(int x, int y)
{
// A copy of the current field for reference. Do not add fields to it, use map::add_field
field &curfield = field_at(x, y);
field &curfield = get_field(x, y);
int veh_part; // vehicle part existing on this tile.
vehicle *veh = NULL; // Vehicle reference if there is one.
bool inside = false; // Are we inside?
Expand All @@ -1403,7 +1403,7 @@ void map::step_in_field(int x, int y)
// When removing a field, do field_list_it = curfield.removeField(type) and continue
// This ensures proper iteration through the fields.
for( auto field_list_it = curfield.begin(); field_list_it != curfield.end(); ){
field_entry * cur = const_cast<field_entry*>( &field_list_it->second );
field_entry * cur = &field_list_it->second;

//Do things based on what field effect we are currently in.
switch (cur->getFieldType()) {
Expand Down Expand Up @@ -1725,11 +1725,11 @@ void map::mon_in_field(int x, int y, monster *z)
if (z->digging()) {
return; // Digging monsters are immune to fields
}
field &curfield = field_at(x, y);
field &curfield = get_field(x, y);

int dam = 0;
for( auto field_list_it = curfield.begin(); field_list_it != curfield.end(); ) {
field_entry * cur = const_cast<field_entry*>( &field_list_it->second );
field_entry * cur = &field_list_it->second;

switch (cur->getFieldType()) {
case fd_null:
Expand Down Expand Up @@ -2075,7 +2075,7 @@ field_entry *field::findField( const field_id field_to_find )
return nullptr;
}

const field_entry *field::findFieldc( const field_id field_to_find )
const field_entry *field::findFieldc( const field_id field_to_find ) const
{
const auto it = field_list.find( field_to_find );
if( it != field_list.end() ) {
Expand All @@ -2084,6 +2084,11 @@ const field_entry *field::findFieldc( const field_id field_to_find )
return nullptr;
}

const field_entry *field::findField( const field_id field_to_find ) const
{
return findFieldc( field_to_find );
}

/*
Function: addfield
Inserts the given field_id into the field list for a given tile if it does not already exist.
Expand Down Expand Up @@ -2137,12 +2142,22 @@ unsigned int field::fieldCount() const
return field_list.size();
}

std::map<field_id, field_entry>::const_iterator field::begin()
std::map<field_id, field_entry>::iterator field::begin()
{
return field_list.begin();
}

std::map<field_id, field_entry>::const_iterator field::end()
std::map<field_id, field_entry>::const_iterator field::begin() const
{
return field_list.begin();
}

std::map<field_id, field_entry>::iterator field::end()
{
return field_list.end();
}

std::map<field_id, field_entry>::const_iterator field::end() const
{
return field_list.end();
}
Expand Down
9 changes: 6 additions & 3 deletions src/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ class field{
//Returns a field entry corresponding to the field_id parameter passed in.
//If no fields are found then a field_entry with type fd_null is returned.
field_entry* findField(const field_id field_to_find);
const field_entry* findFieldc(const field_id field_to_find); //for when you want a const field_entry.
const field_entry* findFieldc(const field_id field_to_find) const; //for when you want a const field_entry.
const field_entry* findField(const field_id field_to_find) const; //for when you want a const field_entry, but don't know it

//Inserts the given field_id into the field list for a given tile if it does not already exist.
//Returns false if the field_id already exists, true otherwise.
Expand All @@ -205,10 +206,12 @@ class field{
//Note: If you are using "field_at" function, set the return to a temporary field variable! If you somehow
//query an out of bounds field location it returns a different field every inquery. This means that
//the start and end iterators won't match up and will crash the system.
std::map<field_id, field_entry>::const_iterator begin();
std::map<field_id, field_entry>::iterator begin();
std::map<field_id, field_entry>::const_iterator begin() const;

//Returns the vector iterator to end searching through the list.
std::map<field_id, field_entry>::const_iterator end();
std::map<field_id, field_entry>::iterator end();
std::map<field_id, field_entry>::const_iterator end() const;

//Returns the total move cost from all fields
int move_cost() const;
Expand Down
4 changes: 2 additions & 2 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8858,7 +8858,7 @@ void game::print_terrain_info(int lx, int ly, WINDOW *w_look, int column, int &l

void game::print_fields_info(int lx, int ly, WINDOW *w_look, int column, int &line)
{
field &tmpfield = m.field_at(lx, ly);
const field &tmpfield = m.field_at(lx, ly);
for( auto &fld : tmpfield ) {
const field_entry *cur = &fld.second;
mvwprintz(w_look, line++, column, fieldlist[cur->getFieldType()].color[cur->getFieldDensity() - 1],
Expand Down Expand Up @@ -12737,7 +12737,7 @@ bool game::plmove(int dx, int dy)
u.set_underwater(false);

//Ask for EACH bad field, maybe not? Maybe say "theres X bad shit in there don't do it."
field &tmpfld = m.field_at(x, y);
const field &tmpfld = m.field_at(x, y);
for( auto &fld : tmpfld ) {
const field_entry &cur = fld.second;
field_id curType = cur.getFieldType();
Expand Down
2 changes: 1 addition & 1 deletion src/lightmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void map::generate_lightmap()
for(int sy = 0; sy < LIGHTMAP_CACHE_Y; ++sy) {
const ter_id terrain = ter(sx, sy);
const std::vector<item> &items = i_at(sx, sy);
field &current_field = field_at(sx, sy);
const field &current_field = field_at(sx, sy);
// When underground natural_light is 0, if this changes we need to revisit
// Only apply this whole thing if the player is inside,
// buildings will be shadowed when outside looking in.
Expand Down
37 changes: 25 additions & 12 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ bool map::trans(const int x, const int y)
}
if( tertr ) {
// Fields may obscure the view, too
field &curfield = field_at( x,y );
const field &curfield = field_at( x,y );
for( auto &fld : curfield ) {
//If ANY field blocks vision, the tile does.
if(!fieldlist[fld.second.getFieldType()].transparent[fld.second.getFieldDensity() - 1]) {
Expand Down Expand Up @@ -1577,7 +1577,7 @@ bool map::moppable_items_at(const int x, const int y)
return true;
}
}
field &fld = field_at(x, y);
const field &fld = field_at(x, y);
if(fld.findField(fd_blood) != 0 || fld.findField(fd_blood_veggy) != 0 ||
fld.findField(fd_blood_insect) != 0 || fld.findField(fd_blood_invertebrate) != 0
|| fld.findField(fd_bile) != 0 || fld.findField(fd_slime) != 0 ||
Expand Down Expand Up @@ -3631,17 +3631,30 @@ void map::remove_trap(const int x, const int y)
/*
* Get wrapper for all fields at xy
*/
field& map::field_at(const int x, const int y)
const field &map::field_at( const int x, const int y ) const
{
if (!INBOUNDS(x, y)) {
nulfield = field();
return nulfield;
}
if( !inbounds( x, y ) ) {
nulfield = field();
return nulfield;
}

int lx, ly;
submap * const current_submap = get_submap_at(x, y, lx, ly);
int lx, ly;
submap *const current_submap = get_submap_at( x, y, lx, ly );

return current_submap->fld[lx][ly];
}

field &map::get_field( const int x, const int y )
{
if( !inbounds( x, y ) ) {
nulfield = field();
return nulfield;
}

int lx, ly;
submap *const current_submap = get_submap_at( x, y, lx, ly );

return current_submap->fld[lx][ly];
return current_submap->fld[lx][ly];
}

/*
Expand Down Expand Up @@ -3970,7 +3983,7 @@ void map::drawsq(WINDOW* w, player &u, const int x, const int y, const bool inve
const ter_id curr_ter = ter(x,y);
const furn_id curr_furn = furn(x,y);
const trap_id curr_trap = tr_at(x, y);
field &curr_field = field_at(x, y);
const field &curr_field = field_at(x, y);
const std::vector<item> &curr_items = i_at(x, y);
long sym;
bool hi = false;
Expand Down Expand Up @@ -5123,7 +5136,7 @@ void map::build_transparency_cache()
continue;
}

field &curfield = field_at(x,y);
const field &curfield = field_at(x,y);
for( auto &fld : curfield ) {
const field_entry * cur = &fld.second;
if( !fieldlist[cur->getFieldType()].transparent[cur->getFieldDensity() - 1] ) {
Expand Down
5 changes: 3 additions & 2 deletions src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ void add_corpse(int x, int y);
const std::set<point> &trap_locations(trap_id t) const;

// Fields
field& field_at(const int x, const int y);
const field& field_at(const int x, const int y) const;

int get_field_age(const point p, const field_id t);
int get_field_strength(const point p, const field_id t);
Expand Down Expand Up @@ -632,7 +632,7 @@ void add_corpse(int x, int y);

std::vector<item> nulitems; // Returned when &i_at() is asked for an OOB value
ter_id nulter; // Returned when &ter() is asked for an OOB value
field nulfield; // Returned when &field_at() is asked for an OOB value
mutable field nulfield; // Returned when &field_at() is asked for an OOB value
vehicle nulveh; // Returned when &veh_at() is asked for an OOB value
int null_temperature; // Because radiation does it too

Expand All @@ -653,6 +653,7 @@ void add_corpse(int x, int y);
void set_abs_sub(const int x, const int y, const int z);

private:
field& get_field(const int x, const int y);
void spread_gas( field_entry *cur, int x, int y, field_id curtype,
int percent_spread, int outdoor_age_speedup );
void create_hot_air( int x, int y, int density );
Expand Down
2 changes: 1 addition & 1 deletion src/monmove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ bool monster::can_move_to(int x, int y) const
return false;

// don't enter fire or electricity ever
field &local_field = g->m.field_at(x, y);
const field &local_field = g->m.field_at(x, y);
if (local_field.findField(fd_fire) || local_field.findField(fd_electricity))
return false;
}
Expand Down

0 comments on commit c6ee5d5

Please sign in to comment.