Fixes a few style issues introduced by #661 and also some that were already there
This commit is contained in:
Celtic Minstrel 2016-07-18 00:08:05 -04:00
parent 649b50d93b
commit 275adafa86
3 changed files with 85 additions and 92 deletions

View file

@ -40,17 +40,15 @@ static lg::log_domain log_mapgen("mapgen");
#define LOG_NG LOG_STREAM(info, log_mapgen)
default_map_generator_job::default_map_generator_job()
: rng_(seed_rng::next_seed()),
game_config_(game_config_manager::get()->game_config())
: rng_(seed_rng::next_seed())
, game_config_(game_config_manager::get()->game_config())
{
}
default_map_generator_job::default_map_generator_job(uint32_t seed)
: rng_(seed),
game_config_(game_config_manager::get()->game_config())
: rng_(seed)
, game_config_(game_config_manager::get()->game_config())
{
}
@ -71,9 +69,7 @@ typedef map_location location;
* the center of the map will be inverted (i.e. be valleys). 'island_size' as
* 0 indicates no island.
*/
height_map default_map_generator_job::generate_height_map(size_t width, size_t height,
size_t iterations, size_t hill_size,
size_t island_size, size_t island_off_center)
height_map default_map_generator_job::generate_height_map(size_t width, size_t height, size_t iterations, size_t hill_size, size_t island_size, size_t island_off_center)
{
height_map res(width, std::vector<int>(height,0));
@ -91,16 +87,18 @@ height_map default_map_generator_job::generate_height_map(size_t width, size_t h
center_y += island_off_center;
break;
case 2:
if(center_x < island_off_center)
if(center_x < island_off_center) {
center_x = 0;
else
} else {
center_x -= island_off_center;
}
break;
case 3:
if(center_y < island_off_center)
if(center_y < island_off_center) {
center_y = 0;
else
} else {
center_y -= island_off_center;
}
break;
}
}
@ -120,10 +118,8 @@ height_map default_map_generator_job::generate_height_map(size_t width, size_t h
// Is this a negative hill? (i.e. a valley)
bool is_valley = false;
int x1 = island_size > 0 ? center_x - island_size + (rng_()%(island_size*2)) :
int(rng_()%width);
int y1 = island_size > 0 ? center_y - island_size + (rng_()%(island_size*2)) :
int(rng_()%height);
int x1 = island_size > 0 ? center_x - island_size + (rng_()%(island_size*2)) : int(rng_()%width);
int y1 = island_size > 0 ? center_y - island_size + (rng_()%(island_size*2)) : int(rng_()%height);
// We have to check whether this is actually a valley
if(island_size != 0) {
@ -166,11 +162,13 @@ height_map default_map_generator_job::generate_height_map(size_t width, size_t h
int heighest = 0, lowest = 100000, x;
for(x = 0; size_t(x) != res.size(); ++x) {
for(int y = 0; size_t(y) != res[x].size(); ++y) {
if(res[x][y] > heighest)
if(res[x][y] > heighest) {
heighest = res[x][y];
}
if(res[x][y] < lowest)
if(res[x][y] < lowest) {
lowest = res[x][y];
}
}
}
@ -180,8 +178,9 @@ height_map default_map_generator_job::generate_height_map(size_t width, size_t h
for(int y = 0; size_t(y) != res[x].size(); ++y) {
res[x][y] -= lowest;
res[x][y] *= 1000;
if(heighest != 0)
if(heighest != 0) {
res[x][y] /= heighest;
}
}
}
@ -409,7 +408,7 @@ double road_path_calculator::cost(const location& loc,
const double /*so_far*/) const
{
++calls;
if (loc.x < 0 || loc.y < 0 || loc.x >= static_cast<long>(map_.size()) ||
if(loc.x < 0 || loc.y < 0 || loc.x >= static_cast<long>(map_.size()) ||
loc.y >= static_cast<long>(map_.front().size())) {
return (pathfind::cost_calculator::getNoPathValue());
@ -424,7 +423,7 @@ double road_path_calculator::cost(const location& loc,
double windiness = 1.0;
if (windiness_ > 1) {
if(windiness_ > 1) {
// modified pseudo_random taken from builder.cpp
unsigned int a = (loc.x + 92872973) ^ 918273;
unsigned int b = (loc.y + 1672517) ^ 128123;
@ -446,7 +445,7 @@ double road_path_calculator::cost(const location& loc,
static std::string terrain;
terrain = t_translation::write_terrain_code(c);
double res = getNoPathValue();
if (const config &child = cfg_.find_child("road_cost", "terrain", terrain)) {
if(const config &child = cfg_.find_child("road_cost", "terrain", terrain)) {
res = child["cost"].to_double();
}
@ -482,8 +481,7 @@ bool is_valid_terrain::operator()(int x, int y) const
}
static int rank_castle_location(int x, int y, const is_valid_terrain& valid_terrain, int min_x, int max_x, int min_y, int max_y,
size_t min_distance, const std::vector<map_location>& other_castles, int highest_ranking)
static int rank_castle_location(int x, int y, const is_valid_terrain& valid_terrain, int min_x, int max_x, int min_y, int max_y, size_t min_distance, const std::vector<map_location>& other_castles, int highest_ranking)
{
const map_location loc(x,y);
@ -522,8 +520,7 @@ static int rank_castle_location(int x, int y, const is_valid_terrain& valid_terr
const int x_from_border = std::min<int>(x - min_x,max_x - x);
const int y_from_border = std::min<int>(y - min_y,max_y - y);
const int border_ranking = min_distance - std::min<int>(x_from_border,y_from_border) +
min_distance - x_from_border - y_from_border;
const int border_ranking = min_distance - std::min<int>(x_from_border,y_from_border) + min_distance - x_from_border - y_from_border;
int current_ranking = border_ranking*2 + avg_distance*10 + lowest_distance*10;
static const int num_nearby_locations = 11*11;
@ -569,10 +566,10 @@ static map_location place_village(const t_translation::t_map& map,
const t_translation::t_terrain t = map[i->x][i->y];
const std::string str = t_translation::write_terrain_code(t);
if (const config &child = cfg.find_child("village", "terrain", str)) {
if(const config &child = cfg.find_child("village", "terrain", str)) {
tcode_list_cache::iterator l = adj_liked_cache.find(t);
t_translation::t_list *adjacent_liked;
if (l != adj_liked_cache.end()) {
if(l != adj_liked_cache.end()) {
adjacent_liked = &(l->second);
} else {
adj_liked_cache[t] = t_translation::read_list(child["adjacent_liked"]);
@ -613,10 +610,10 @@ static void flood_name(const map_location& start, const std::string& name, std::
get_adjacent_tiles(start,adj);
size_t n;
//if adjacent tiles are tiles and unnamed, name them
for (n = 0; n < 6; n++) {
for(n = 0; n < 6; n++) {
//we do not care for tiles outside the middle part
//cast to unsigned to skip x < 0 || y < 0 as well.
if (unsigned(adj[n].x) >= width / 3 || unsigned(adj[n].y) >= height / 3) {
if(unsigned(adj[n].x) >= width / 3 || unsigned(adj[n].y) >= height / 3) {
continue;
}
@ -625,7 +622,7 @@ static void flood_name(const map_location& start, const std::string& name, std::
if((t_translation::terrain_matches(terr, tile_types)) && (tile_names.find(loc) == tile_names.end())) {
tile_names.insert(std::pair<location, std::string>(loc, name));
//labeling decision: this is result of trial and error on what looks best in game
if (label_count % 6 == 0) { //ensure that labels do not occur more often than every 6 recursions
if(label_count % 6 == 0) { //ensure that labels do not occur more often than every 6 recursions
labels->insert(std::pair<map_location, std::string>(loc, full_name));
label_count++; //ensure that no adjacent tiles get labeled
}
@ -638,8 +635,8 @@ namespace {
// the configuration file should contain a number of [height] tags:
// [height]
// height=n
// terrain=x
// height=n
// terrain=x
// [/height]
// These should be in descending order of n.
// They are checked sequentially, and if height is greater than n for that tile,
@ -708,8 +705,7 @@ terrain_converter::terrain_converter(const config& cfg)
bool terrain_converter::convert_terrain(const t_translation::t_terrain & terrain,
const int height, const int temperature) const
{
return std::find(from.begin(),from.end(),terrain) != from.end() && height >= min_height && height <= max_height &&
temperature >= min_temp && temperature <= max_temp && to != t_translation::NONE_TERRAIN;
return std::find(from.begin(),from.end(),terrain) != from.end() && height >= min_height && height <= max_height && temperature >= min_temp && temperature <= max_temp && to != t_translation::NONE_TERRAIN;
}
t_translation::t_terrain terrain_converter::convert_to() const
@ -719,9 +715,7 @@ t_translation::t_terrain terrain_converter::convert_to() const
} // end anon namespace
std::string default_map_generator_job::default_generate_map(size_t width, size_t height, size_t island_size, size_t island_off_center,
size_t iterations, size_t hill_size,
size_t max_lakes, size_t nvillages, size_t castle_size, size_t nplayers, bool roads_between_castles,
std::string default_map_generator_job::default_generate_map(size_t width, size_t height, size_t island_size, size_t island_off_center, size_t iterations, size_t hill_size, size_t max_lakes, size_t nvillages, size_t castle_size, size_t nplayers, bool roads_between_castles,
std::map<map_location,std::string>* labels, const config& cfg)
{
log_scope("map generation");
@ -731,7 +725,7 @@ std::string default_map_generator_job::default_generate_map(size_t width, size_t
/** Try to find configuration for castles. */
const config &castle_config = cfg.child("castle");
if (!castle_config) {
if(!castle_config) {
LOG_NG << "Could not find castle configuration\n";
return std::string();
}
@ -763,8 +757,9 @@ std::string default_map_generator_job::default_generate_map(size_t width, size_t
config naming;
naming.append_children(game_config_,"naming");
if(cfg.has_child("naming"))
naming.append_children(cfg,"naming");
if(cfg.has_child("naming")) {
naming.append_children(cfg,"naming");
}
naming.merge_children("naming");
naming = naming.child_or_empty("naming");
@ -789,13 +784,12 @@ std::string default_map_generator_job::default_generate_map(size_t width, size_t
if(naming.has_attribute("base_names")) {
base_name_generator = base_generator_factory.get_name_generator( (naming.has_attribute("base_names") || naming.has_attribute("base_name_generator")) ? "base" : "male" );
}
else {
} else {
base_name_generator = base_generator_factory.get_name_generator("male");
}
river_name_generator = base_generator_factory.get_name_generator("river");
lake_name_generator = base_generator_factory.get_name_generator("lake");
road_name_generator = base_generator_factory.get_name_generator("road");
lake_name_generator = base_generator_factory.get_name_generator("lake");
road_name_generator = base_generator_factory.get_name_generator("road");
bridge_name_generator = base_generator_factory.get_name_generator("bridge");
mountain_name_generator = base_generator_factory.get_name_generator("mountain");
forest_name_generator = base_generator_factory.get_name_generator("forest");
@ -812,8 +806,7 @@ std::string default_map_generator_job::default_generate_map(size_t width, size_t
size_t x, y;
for(x = 0; x != heights.size(); ++x) {
for(y = 0; y != heights[x].size(); ++y) {
for(std::vector<terrain_height_mapper>::const_iterator i = height_conversion.begin();
i != height_conversion.end(); ++i) {
for(std::vector<terrain_height_mapper>::const_iterator i = height_conversion.begin(); i != height_conversion.end(); ++i) {
if(i->convert_terrain(heights[x][y])) {
terrain[x][y] = i->convert_to();
break;
@ -849,7 +842,7 @@ std::string default_map_generator_job::default_generate_map(size_t width, size_t
for(int tries = 0; tries != 100; ++tries) {
const int x = rng_()%width;
const int y = rng_()%height;
if (heights[x][y] > cfg["min_lake_height"].to_int()) {
if(heights[x][y] > cfg["min_lake_height"].to_int()) {
std::vector<location> river = generate_river(heights,
terrain, x, y, cfg["river_frequency"]);
@ -1047,7 +1040,7 @@ std::string default_map_generator_job::default_generate_map(size_t width, size_t
std::set<location> bridges;
road_path_calculator calc(terrain, cfg, rng_());
for (int road = 0; road != nroads; ++road) {
for(int road = 0; road != nroads; ++road) {
log_scope("creating road");
/*
@ -1063,7 +1056,7 @@ std::string default_map_generator_job::default_generate_map(size_t width, size_t
dst.x += width/3 - 1;
dst.y += height/3 - 1;
if (roads_between_castles && road < int(castles.size() * castles.size())) {
if(roads_between_castles && road < int(castles.size() * castles.size())) {
const size_t src_castle = road/castles.size();
const size_t dst_castle = road%castles.size();
if(src_castle >= dst_castle) {
@ -1079,7 +1072,7 @@ std::string default_map_generator_job::default_generate_map(size_t width, size_t
continue;
}
if (calc.cost(src, 0.0) >= 1000.0 || calc.cost(dst, 0.0) >= 1000.0) {
if(calc.cost(src, 0.0) >= 1000.0 || calc.cost(dst, 0.0) >= 1000.0) {
continue;
}
@ -1109,8 +1102,7 @@ std::string default_map_generator_job::default_generate_map(size_t width, size_t
// Find the configuration which tells us
// what to convert this tile to, to make it into a road.
if (const config &child = cfg.find_child("road_cost", "terrain",
t_translation::write_terrain_code(terrain[x][y])))
if(const config &child = cfg.find_child("road_cost", "terrain", t_translation::write_terrain_code(terrain[x][y])))
{
// Convert to bridge means that we want to convert
// depending upon the direction the road is going.
@ -1206,22 +1198,20 @@ std::string default_map_generator_job::default_generate_map(size_t width, size_t
terrain[x][y] = t_translation::HUMAN_KEEP;
const int castles[13][2] = {
{-1, 0}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {0, 1}, {-1, 1},
{-2, 1}, {-2, 0}, {-2, -1}, {-1, -2}, {0, -2}, {1, -2}
{-1, 0}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {0, 1}, {-1, 1},
{-2, 1}, {-2, 0}, {-2, -1}, {-1, -2}, {0, -2}, {1, -2}
};
for (size_t i = 0; i < castle_size - 1; i++) {
terrain[x+castles[i][0]][y+castles[i][1]] = t_translation::HUMAN_CASTLE;
for(size_t i = 0; i < castle_size - 1; i++) {
terrain[x+castles[i][0]][y+castles[i][1]] = t_translation::HUMAN_CASTLE;
}
// Remove all labels under the castle tiles
if(labels != nullptr) {
labels->erase(location(x-width/3,y-height/3));
for (size_t i = 0; i < castle_size - 1; i++) {
labels->erase(location(x+castles[i][0]-width/3,
y+castles[i][1]-height/3));
}
labels->erase(location(x-width/3,y-height/3));
for(size_t i = 0; i < castle_size - 1; i++) {
labels->erase(location(x+castles[i][0]-width/3, y+castles[i][1]-height/3));
}
}
}
@ -1233,17 +1223,17 @@ std::string default_map_generator_job::default_generate_map(size_t width, size_t
*we name these now that everything else is placed (as e.g., placing
* roads could split a forest)
*/
if ( misc_labels != nullptr ) {
for (x = width / 3; x < (width / 3)*2; x++) {
for (y = height / 3; y < (height / 3) * 2;y++) {
if(misc_labels != nullptr) {
for(x = width / 3; x < (width / 3)*2; x++) {
for(y = height / 3; y < (height / 3) * 2;y++) {
//check the terrain of the tile
const location loc(x - width / 3, y - height / 3);
const t_translation::t_terrain terr = terrain[x][y];
std::string name, base_name;
std::set<std::string> used_names;
if (t_translation::terrain_matches(terr, t_translation::ALL_MOUNTAINS)) {
if(t_translation::terrain_matches(terr, t_translation::ALL_MOUNTAINS)) {
//name every 15th mountain
if ((rng_()%15) == 0) {
if((rng_() % 15) == 0) {
for(size_t ntry = 0; ntry != 30 && (ntry == 0 || used_names.count(name) > 0); ++ntry) {
base_name = base_name_generator->generate();
name = mountain_name_generator->generate({{"base", base_name}});
@ -1252,7 +1242,7 @@ std::string default_map_generator_job::default_generate_map(size_t width, size_t
mountain_names.insert(std::pair<location, std::string>(loc, base_name));
}
}
else if (t_translation::terrain_matches(terr, t_translation::ALL_FORESTS)) {
else if(t_translation::terrain_matches(terr, t_translation::ALL_FORESTS)) {
//if the forest tile is not named yet, name it
const std::map<location, std::string>::const_iterator forest_name = forest_names.find(loc);
if(forest_name == forest_names.end()) {
@ -1265,7 +1255,7 @@ std::string default_map_generator_job::default_generate_map(size_t width, size_t
flood_name(loc, base_name, forest_names, t_translation::ALL_FORESTS, terrain, width, height, 0, misc_labels, name);
}
}
else if (t_translation::terrain_matches(terr, t_translation::ALL_SWAMPS)) {
else if(t_translation::terrain_matches(terr, t_translation::ALL_SWAMPS)) {
//if the swamp tile is not named yet, name it
const std::map<location, std::string>::const_iterator swamp_name = swamp_names.find(loc);
if(swamp_name == swamp_names.end()) {
@ -1278,11 +1268,11 @@ std::string default_map_generator_job::default_generate_map(size_t width, size_t
flood_name(loc, base_name, swamp_names, t_translation::ALL_SWAMPS, terrain, width, height, 0, misc_labels, name);
}
}
}//for (y)
}//for (x)
}//if (misc_labels)
}//for(y)
}//for(x)
}//if(misc_labels)
if (nvillages > 0)
if(nvillages > 0)
{
// First we work out the size of the x and y distance between villages
const size_t tiles_per_village = ((width*height)/9)/nvillages;
@ -1304,8 +1294,9 @@ std::string default_map_generator_job::default_generate_map(size_t width, size_t
config village_naming;
village_naming.append_children(game_config_,"village_naming");
if(cfg.has_child("village_naming"))
if(cfg.has_child("village_naming")) {
village_naming.append_children(cfg,"village_naming");
}
village_naming.merge_children("village_naming");
village_naming = village_naming.child_or_empty("village_naming");
@ -1330,7 +1321,7 @@ std::string default_map_generator_job::default_generate_map(size_t width, size_t
const std::string str =
t_translation::write_terrain_code(terrain[res.x][res.y]);
if (const config &child = cfg.find_child("village", "terrain", str))
if(const config &child = cfg.find_child("village", "terrain", str))
{
const std::string &convert_to = child["convert_to"];
if(convert_to != "") {
@ -1339,7 +1330,7 @@ std::string default_map_generator_job::default_generate_map(size_t width, size_t
villages.insert(res);
if ( village_labels != nullptr ) {
if(village_labels != nullptr) {
name_generator_factory village_name_generator_factory{ village_naming, {"base", "male", "village", "lake", "river", "bridge", "grassland", "forest", "hill", "mountain", "mountain_anon", "road", "swamp"} };
@ -1380,9 +1371,9 @@ std::string default_map_generator_job::default_generate_map(size_t width, size_t
const std::map<location,std::string>::const_iterator bridge_name = bridge_names.find(adj[n]);
if(bridge_name != bridge_names.end()) {
//we should always end up here, since if there is an adjacent bridge, there has to be an adjacent river too
symbols["bridge"] = bridge_name->second;
name_type = "river_bridge";
//we should always end up here, since if there is an adjacent bridge, there has to be an adjacent river too
symbols["bridge"] = bridge_name->second;
name_type = "river_bridge";
}
break;

View file

@ -23,28 +23,28 @@
typedef std::map< std::string, t_string > string_map;
class name_generator_invalid_exception : public std::exception {
public:
name_generator_invalid_exception(const char* errMessage):errMessage_(errMessage){}
const char* what() const throw() { return errMessage_; }
private:
const char* errMessage_;
public:
name_generator_invalid_exception(const char* errMessage):errMessage_(errMessage) {}
const char* what() const throw() { return errMessage_; }
private:
const char* errMessage_;
};
class name_generator {
public:
std::string generate(const std::map<std::string,std::string>& variables) const { return utils::interpolate_variables_into_string(generate(), &variables); };
virtual std::string generate() const { return ""; };
name_generator() {};
virtual ~name_generator() {};
virtual std::string generate() const { return ""; }
name_generator() {}
virtual ~name_generator() {}
};
class proxy_name_generator : public name_generator {
const name_generator& base;
public:
proxy_name_generator(const name_generator& b) : base(b) {}
std::string generate() const override { return base.generate(); };
std::string generate() const override { return base.generate(); }
};
#endif

View file

@ -36,7 +36,9 @@ void name_generator_factory::add_name_generator_from_config(const config& config
try {
name_generators_[id] = std::shared_ptr<name_generator>(new context_free_grammar_generator(config[cfg_name]));
}
catch (const name_generator_invalid_exception& ex) { lg::wml_error() << ex.what() << '\n'; }
catch (const name_generator_invalid_exception& ex) {
lg::wml_error() << ex.what() << '\n';
}
}
if(config.has_attribute(markov_name)) {