Skip to content

Commit

Permalink
Move struct mapgendata into separate header and source file
Browse files Browse the repository at this point in the history
Also convert it into a class for consistency.

And add some preliminary documentation.
  • Loading branch information
BevapDin committed Sep 23, 2019
1 parent cfc7f8a commit 7b96cf5
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 206 deletions.
2 changes: 1 addition & 1 deletion src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class submap;
class item_location;
class map_cursor;
struct maptile;
struct mapgendata;
class mapgendata;
class basecamp;
class computer;
class Character;
Expand Down
137 changes: 0 additions & 137 deletions src/mapgen_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,6 @@ const mtype_id mon_spider_widow_giant( "mon_spider_widow_giant" );
const mtype_id mon_spider_cellar_giant( "mon_spider_cellar_giant" );
const mtype_id mon_zombie_jackson( "mon_zombie_jackson" );

mapgendata::mapgendata( oter_id north, oter_id east, oter_id south, oter_id west,
oter_id northeast, oter_id southeast, oter_id southwest, oter_id northwest,
oter_id up, oter_id down, int z, const regional_settings &rsettings, map &mp )
: t_nesw{ north, east, south, west, northeast, southeast, southwest, northwest }
, t_above( up )
, t_below( down )
, zlevel( z )
, region( rsettings )
, m( mp )
, default_groundcover( region.default_groundcover )
{
}

tripoint rotate_point( const tripoint &p, int rotations )
{
if( p.x < 0 || p.x >= SEEX * 2 ||
Expand Down Expand Up @@ -185,77 +172,6 @@ building_gen_pointer get_mapgen_cfunction( const std::string &ident )
return iter == pointers.end() ? nullptr : iter->second;
}

void mapgendata::set_dir( int dir_in, int val )
{
switch( dir_in ) {
case 0:
n_fac = val;
break;
case 1:
e_fac = val;
break;
case 2:
s_fac = val;
break;
case 3:
w_fac = val;
break;
case 4:
ne_fac = val;
break;
case 5:
se_fac = val;
break;
case 6:
sw_fac = val;
break;
case 7:
nw_fac = val;
break;
default:
debugmsg( "Invalid direction for mapgendata::set_dir. dir_in = %d", dir_in );
break;
}
}

void mapgendata::fill( int val )
{
n_fac = val;
e_fac = val;
s_fac = val;
w_fac = val;
ne_fac = val;
se_fac = val;
sw_fac = val;
nw_fac = val;
}

int &mapgendata::dir( int dir_in )
{
switch( dir_in ) {
case 0:
return n_fac;
case 1:
return e_fac;
case 2:
return s_fac;
case 3:
return w_fac;
case 4:
return ne_fac;
case 5:
return se_fac;
case 6:
return sw_fac;
case 7:
return nw_fac;
default:
debugmsg( "Invalid direction for mapgendata::set_dir. dir_in = %d", dir_in );
//return something just so the compiler doesn't freak out. Not really correct, though.
return n_fac;
}
}

ter_id grass_or_dirt()
{
if( one_in( 4 ) ) {
Expand All @@ -272,59 +188,6 @@ ter_id clay_or_sand()
return t_clay;
}

void mapgendata::square_groundcover( const int x1, const int y1, const int x2, const int y2 )
{
m.draw_square_ter( this->default_groundcover, point( x1, y1 ), point( x2, y2 ) );
}
void mapgendata::fill_groundcover()
{
m.draw_fill_background( this->default_groundcover );
}
bool mapgendata::is_groundcover( const ter_id &iid ) const
{
for( const auto &pr : default_groundcover ) {
if( pr.obj == iid ) {
return true;
}
}

return false;
}

bool mapgendata::has_basement() const
{
const std::vector<std::string> &all_basements = region.city_spec.basements.all;
return std::any_of( all_basements.begin(), all_basements.end(), [this]( const std::string & b ) {
return t_below == oter_id( b );
} );
}

ter_id mapgendata::groundcover()
{
const ter_id *tid = default_groundcover.pick();
return tid != nullptr ? *tid : t_null;
}

const oter_id &mapgendata::neighbor_at( om_direction::type dir ) const
{
// TODO: De-uglify, implement proper conversion somewhere
switch( dir ) {
case om_direction::type::north:
return north();
case om_direction::type::east:
return east();
case om_direction::type::south:
return south();
case om_direction::type::west:
return west();
default:
break;
}

debugmsg( "Tried to get neighbor from invalid direction %d", dir );
return north();
}

void mapgen_rotate( map *m, oter_id terrain_type, bool north_is_down )
{
const auto dir = terrain_type->get_dir();
Expand Down
69 changes: 1 addition & 68 deletions src/mapgen_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,84 +8,17 @@
#include <utility>

#include "type_id.h"
#include "weighted_list.h"
#include "point.h"
#include "mapgendata.h"

class time_point;
struct regional_settings;
class map;

class mission;

using mapgen_update_func = std::function<void( const tripoint &map_pos3, mission *miss )>;
class JsonObject;

namespace om_direction
{
enum class type : int;
} // namespace om_direction

struct mapgendata {
public:
oter_id t_nesw[8];
int n_fac = 0; // dir == 0
int e_fac = 0; // dir == 1
int s_fac = 0; // dir == 2
int w_fac = 0; // dir == 3
int ne_fac = 0; // dir == 4
int se_fac = 0; // dir == 5
int sw_fac = 0; // dir == 6
int nw_fac = 0; // dir == 7
oter_id t_above;
oter_id t_below;
int zlevel;
const regional_settings &region;
map &m;
weighted_int_list<ter_id> default_groundcover;
mapgendata( oter_id t_north, oter_id t_east, oter_id t_south, oter_id t_west,
oter_id northeast, oter_id southeast, oter_id southwest, oter_id northwest,
oter_id up, oter_id down, int z, const regional_settings &rsettings, map &mp );
void set_dir( int dir_in, int val );
void fill( int val );
int &dir( int dir_in );
const oter_id &north() const {
return t_nesw[0];
}
const oter_id &east() const {
return t_nesw[1];
}
const oter_id &south() const {
return t_nesw[2];
}
const oter_id &west() const {
return t_nesw[3];
}
const oter_id &neast() const {
return t_nesw[4];
}
const oter_id &seast() const {
return t_nesw[5];
}
const oter_id &swest() const {
return t_nesw[6];
}
const oter_id &nwest() const {
return t_nesw[7];
}
const oter_id &above() const {
return t_above;
}
const oter_id &below() const {
return t_below;
}
const oter_id &neighbor_at( om_direction::type dir ) const;
void fill_groundcover();
void square_groundcover( int x1, int y1, int x2, int y2 );
ter_id groundcover();
bool is_groundcover( const ter_id &iid ) const;
bool has_basement() const;
};

/**
* Calculates the coordinates of a rotated point.
* Should match the `mapgen_*` rotation.
Expand Down
143 changes: 143 additions & 0 deletions src/mapgendata.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#include "mapgendata.h"

#include "regional_settings.h"
#include "map.h"

mapgendata::mapgendata( oter_id north, oter_id east, oter_id south, oter_id west,
oter_id northeast, oter_id southeast, oter_id southwest, oter_id northwest,
oter_id up, oter_id down, int z, const regional_settings &rsettings, map &mp )
: t_nesw{ north, east, south, west, northeast, southeast, southwest, northwest }
, t_above( up )
, t_below( down )
, zlevel( z )
, region( rsettings )
, m( mp )
, default_groundcover( region.default_groundcover )
{
}

void mapgendata::set_dir( int dir_in, int val )
{
switch( dir_in ) {
case 0:
n_fac = val;
break;
case 1:
e_fac = val;
break;
case 2:
s_fac = val;
break;
case 3:
w_fac = val;
break;
case 4:
ne_fac = val;
break;
case 5:
se_fac = val;
break;
case 6:
sw_fac = val;
break;
case 7:
nw_fac = val;
break;
default:
debugmsg( "Invalid direction for mapgendata::set_dir. dir_in = %d", dir_in );
break;
}
}

void mapgendata::fill( int val )
{
n_fac = val;
e_fac = val;
s_fac = val;
w_fac = val;
ne_fac = val;
se_fac = val;
sw_fac = val;
nw_fac = val;
}

int &mapgendata::dir( int dir_in )
{
switch( dir_in ) {
case 0:
return n_fac;
case 1:
return e_fac;
case 2:
return s_fac;
case 3:
return w_fac;
case 4:
return ne_fac;
case 5:
return se_fac;
case 6:
return sw_fac;
case 7:
return nw_fac;
default:
debugmsg( "Invalid direction for mapgendata::set_dir. dir_in = %d", dir_in );
//return something just so the compiler doesn't freak out. Not really correct, though.
return n_fac;
}
}

void mapgendata::square_groundcover( const int x1, const int y1, const int x2, const int y2 )
{
m.draw_square_ter( default_groundcover, point( x1, y1 ), point( x2, y2 ) );
}

void mapgendata::fill_groundcover()
{
m.draw_fill_background( default_groundcover );
}

bool mapgendata::is_groundcover( const ter_id &iid ) const
{
for( const auto &pr : default_groundcover ) {
if( pr.obj == iid ) {
return true;
}
}

return false;
}

bool mapgendata::has_basement() const
{
const std::vector<std::string> &all_basements = region.city_spec.basements.all;
return std::any_of( all_basements.begin(), all_basements.end(), [this]( const std::string & b ) {
return t_below == oter_id( b );
} );
}

ter_id mapgendata::groundcover()
{
const ter_id *tid = default_groundcover.pick();
return tid != nullptr ? *tid : t_null;
}

const oter_id &mapgendata::neighbor_at( om_direction::type dir ) const
{
// TODO: De-uglify, implement proper conversion somewhere
switch( dir ) {
case om_direction::type::north:
return north();
case om_direction::type::east:
return east();
case om_direction::type::south:
return south();
case om_direction::type::west:
return west();
default:
break;
}

debugmsg( "Tried to get neighbor from invalid direction %d", dir );
return north();
}
Loading

0 comments on commit 7b96cf5

Please sign in to comment.