Freeciv21
Develop your civilization from humble roots to a global empire
mapgen.cpp File Reference
#include <fc_config.h>
#include <QBitArray>
#include <cstdlib>
#include <cstring>
#include "bitvector.h"
#include "fcintl.h"
#include "log.h"
#include "rand.h"
#include "shared.h"
#include "game.h"
#include "map.h"
#include "road.h"
#include "fair_islands.h"
#include "fracture_map.h"
#include "height_map.h"
#include "islands.h"
#include "mapgen_topology.h"
#include "mapgen_utils.h"
#include "startpos.h"
#include "temperature_map.h"
#include "mapgen.h"
+ Include dependency graph for mapgen.cpp:

Go to the source code of this file.

Classes

struct  river_map
 
struct  DataFilter
 Passed as data to rand_map_pos_filtered() by rand_map_pos_characteristic() More...
 
struct  test_func
 

Macros

#define RIVERS_MAXTRIES   32767
 
#define ini_hmap_low_level()
 
#define map_pos_is_low(ptile)   ((hmap((ptile)) < hmap_low_level))
 
#define PLACE_ONE_TYPE(count, alternate, ter, wc, tc, mc, weight)
 This place randomly a cluster of terrains with some characteristics. More...
 
#define NUM_TEST_FUNCTIONS   9
 

Enumerations

enum  miscellaneous_c { MC_NONE , MC_LOW , MC_NLOW }
 

Functions

static void make_huts (int number)
 This function spreads out huts on the map, a position can be used for a hut if there isn't another hut close and if it's not on the ocean. More...
 
static void add_resources (int prob)
 Add specials to the map with given probability (out of 1000). More...
 
static void adjust_terrain_param ()
 Convert parameters from the server into terrains percents parameters for the generators. More...
 
static int river_test_blocked (struct river_map *privermap, struct tile *ptile, struct extra_type *priver)
 Help function used in make_river(). More...
 
static int river_test_rivergrid (struct river_map *privermap, struct tile *ptile, struct extra_type *priver)
 Help function used in make_river(). More...
 
static int river_test_highlands (struct river_map *privermap, struct tile *ptile, struct extra_type *priver)
 Help function used in make_river(). More...
 
static int river_test_adjacent_ocean (struct river_map *privermap, struct tile *ptile, struct extra_type *priver)
 Help function used in make_river(). More...
 
static int river_test_adjacent_river (struct river_map *privermap, struct tile *ptile, struct extra_type *priver)
 Help function used in make_river(). More...
 
static int river_test_adjacent_highlands (struct river_map *privermap, struct tile *ptile, struct extra_type *priver)
 Help function used in make_river(). More...
 
static int river_test_swamp (struct river_map *privermap, struct tile *ptile, struct extra_type *priver)
 Help function used in make_river(). More...
 
static int river_test_adjacent_swamp (struct river_map *privermap, struct tile *ptile, struct extra_type *priver)
 Help function used in make_river(). More...
 
static int river_test_height_map (struct river_map *privermap, struct tile *ptile, struct extra_type *priver)
 Help function used in make_river(). More...
 
static void river_blockmark (struct river_map *privermap, struct tile *ptile)
 Called from make_river. More...
 
static bool make_river (struct river_map *privermap, struct tile *ptile, struct extra_type *priver)
 Makes a river starting at (x, y). More...
 
static void make_rivers ()
 Calls make_river until there are enough river tiles on the map. More...
 
static void river_types_init ()
 Initialize river types array. More...
 
bool test_wetness (const struct tile *ptile, wetness_c c)
 These functions test for conditions used in rand_map_pos_characteristic. More...
 
static bool test_miscellaneous (const struct tile *ptile, miscellaneous_c c)
 Checks if the given location satisfy some miscellaneous condition. More...
 
static bool condition_filter (const struct tile *ptile, const void *data)
 A filter function to be passed to rand_map_pos_filtered(). More...
 
static struct tilerand_map_pos_characteristic (wetness_c wc, temperature_type tc, miscellaneous_c mc)
 Return random map coordinates which have some conditions and which are not yet placed on pmap. More...
 
static bool terrain_is_too_high (struct tile *ptile, int thill, int my_height)
 We don't want huge areas of hill/mountains, so we put in a plains here and there, where it gets too 'heigh'. More...
 
static void make_relief ()
 make_relief() will convert all squares that are higher than thill to mountains and hills. More...
 
void make_polar ()
 Add frozen tiles in the arctic zone. More...
 
static bool ok_for_separate_poles (struct tile *ptile)
 If separatepoles is set, return false if this tile has to keep ocean. More...
 
static void make_polar_land ()
 Place untextured land at the poles on any tile that is not already covered with TER_FROZEN terrain. More...
 
static void place_terrain (struct tile *ptile, int diff, struct terrain *pterrain, int *to_be_placed, wetness_c wc, temperature_type tc, miscellaneous_c mc)
 Recursively generate terrains. More...
 
static void make_plain (struct tile *ptile, int *to_be_placed)
 A simple function that adds plains grassland or tundra to the current location. More...
 
void make_plains ()
 Make_plains converts all not yet placed terrains to plains (tundra, grass) used by generators 2-4. More...
 
static void make_terrains ()
 Make_terrains calls make_forest, make_dessert,etc with random free locations until there has been made enough. More...
 
static void make_land ()
 make land simply does it all based on a generated heightmap 1) with map.server.landpercent it generates a ocean/unknown map 2) it then calls the above functions to generate the different terrains More...
 
static bool is_tiny_island (struct tile *ptile)
 Returns if this is a 1x1 island. More...
 
static void remove_tiny_islands ()
 Removes all 1x1 islands (sets them to ocean). More...
 
static void print_mapgen_map ()
 Debugging function to print information about the map that's been generated. More...
 
bool map_generate (bool autosize, struct unit_type *initial_unit)
 See stdinhand.c for information on map generation methods. More...
 
static bool near_safe_tiles (struct tile *ptile)
 Return TRUE if a safe tile is in a radius of 1. More...
 
static bool is_resource_close (const struct tile *ptile)
 Return TRUE iff there's a resource within one tile of the given map position. More...
 

Variables

struct extra_typeriver_types [MAX_ROAD_TYPES]
 
int river_type_count = 0
 
int forest_pct = 0
 
int desert_pct = 0
 
int swamp_pct = 0
 
int mountain_pct = 0
 
int jungle_pct = 0
 
int river_pct = 0
 
static int hmap_low_level = 0
 
static struct test_func test_funcs [NUM_TEST_FUNCTIONS]
 

Macro Definition Documentation

◆ ini_hmap_low_level

#define ini_hmap_low_level ( )
Value:
{ \
hmap_low_level = \
}
int hmap_shore_level
Definition: height_map.cpp:24
#define hmap_max_level
Definition: height_map.h:30
int swamp_pct
Definition: mapgen.cpp:95

Definition at line 104 of file mapgen.cpp.

◆ map_pos_is_low

#define map_pos_is_low (   ptile)    ((hmap((ptile)) < hmap_low_level))

Definition at line 111 of file mapgen.cpp.

◆ NUM_TEST_FUNCTIONS

#define NUM_TEST_FUNCTIONS   9

Definition at line 617 of file mapgen.cpp.

◆ PLACE_ONE_TYPE

#define PLACE_ONE_TYPE (   count,
  alternate,
  ter,
  wc,
  tc,
  mc,
  weight 
)
Value:
if ((count) > 0) { \
struct tile *ptile; \
/* Place some terrains */ \
if ((ptile = rand_map_pos_characteristic((wc), (tc), (mc)))) { \
place_terrain(ptile, (weight), (ter), &(count), (wc), (tc), (mc)); \
} else { \
/* If rand_map_pos_temperature returns FALSE we may as well stop */ \
/* looking for this time and go to alternate type. */ \
(alternate) += (count); \
(count) = 0; \
} \
}
static struct tile * rand_map_pos_characteristic(wetness_c wc, temperature_type tc, miscellaneous_c mc)
Return random map coordinates which have some conditions and which are not yet placed on pmap.
Definition: mapgen.cpp:180
Definition: tile.h:42

This place randomly a cluster of terrains with some characteristics.

Definition at line 394 of file mapgen.cpp.

◆ RIVERS_MAXTRIES

#define RIVERS_MAXTRIES   32767

Definition at line 50 of file mapgen.cpp.

Enumeration Type Documentation

◆ miscellaneous_c

Enumerator
MC_NONE 
MC_LOW 
MC_NLOW 

Definition at line 113 of file mapgen.cpp.

Function Documentation

◆ add_resources()

static void add_resources ( int  prob)
static

Add specials to the map with given probability (out of 1000).

Definition at line 1500 of file mapgen.cpp.

Referenced by map_generate().

◆ adjust_terrain_param()

static void adjust_terrain_param ( )
static

Convert parameters from the server into terrains percents parameters for the generators.

Definition at line 1410 of file mapgen.cpp.

Referenced by map_generate().

◆ condition_filter()

static bool condition_filter ( const struct tile ptile,
const void *  data 
)
static

A filter function to be passed to rand_map_pos_filtered().

See rand_map_pos_characteristic for more explanation.

Definition at line 166 of file mapgen.cpp.

Referenced by rand_map_pos_characteristic().

◆ is_resource_close()

static bool is_resource_close ( const struct tile ptile)
static

Return TRUE iff there's a resource within one tile of the given map position.

Definition at line 1484 of file mapgen.cpp.

Referenced by add_resources().

◆ is_tiny_island()

static bool is_tiny_island ( struct tile ptile)
static

Returns if this is a 1x1 island.

Definition at line 1104 of file mapgen.cpp.

Referenced by remove_tiny_islands().

◆ make_huts()

static void make_huts ( int  number)
static

This function spreads out huts on the map, a position can be used for a hut if there isn't another hut close and if it's not on the ocean.

Definition at line 1458 of file mapgen.cpp.

Referenced by map_generate().

◆ make_land()

static void make_land ( )
static

make land simply does it all based on a generated heightmap 1) with map.server.landpercent it generates a ocean/unknown map 2) it then calls the above functions to generate the different terrains

Definition at line 991 of file mapgen.cpp.

Referenced by map_generate().

◆ make_plain()

static void make_plain ( struct tile ptile,
int *  to_be_placed 
)
static

A simple function that adds plains grassland or tundra to the current location.

Definition at line 356 of file mapgen.cpp.

Referenced by make_plains(), and make_terrains().

◆ make_plains()

void make_plains ( )

Make_plains converts all not yet placed terrains to plains (tundra, grass) used by generators 2-4.

Definition at line 377 of file mapgen.cpp.

Referenced by map_generate_island_2or3(), map_generate_island_single(), and map_generate_island_variable().

◆ make_polar()

void make_polar ( )

Add frozen tiles in the arctic zone.

If ruleset has frozen ocean, use that, else use frozen land terrains with appropriate texturing. This is used in generators 2-4.

Definition at line 258 of file mapgen.cpp.

Referenced by initworld().

◆ make_polar_land()

static void make_polar_land ( )
static

Place untextured land at the poles on any tile that is not already covered with TER_FROZEN terrain.

This is used by generators 1 and 5.

Definition at line 301 of file mapgen.cpp.

Referenced by make_land().

◆ make_relief()

static void make_relief ( )
static

make_relief() will convert all squares that are higher than thill to mountains and hills.

Note that thill will be adjusted according to the map.server.steepness value, so increasing map.mountains will result in more hills and mountains.

Definition at line 217 of file mapgen.cpp.

Referenced by make_land().

◆ make_river()

static bool make_river ( struct river_map privermap,
struct tile ptile,
struct extra_type priver 
)
static

Makes a river starting at (x, y).

Returns 1 if it succeeds. Return 0 if it fails. The river is stored in river_map.

How to make a river path look natural

Rivers always flow down. Thus rivers are best implemented on maps where every tile has an explicit height value. However, Freeciv21 has a flat map. But there are certain things that help the user imagine differences in height between tiles. The selection of direction for rivers should confirm and even amplify the user's image of the map's topology.

To decide which direction the river takes, the possible directions are tested in a series of test until there is only 1 direction left. Some tests are fatal. This means that they can sort away all remaining directions. If they do so, the river is aborted. Here follows a description of the test series.

Falling into itself: fatal (river_test_blocked) This is tested by looking up in the river_map array if a tile or every tile surrounding the tile is marked as blocked. A tile is marked as blocked if it belongs to the current river or has been evaluated in a previous iteration in the creation of the current river.

Possible values: 0: Is not falling into itself. 1: Is falling into itself.

Forming a 4-river-grid: optionally fatal (river_test_rivergrid) A minimal 4-river-grid is formed when an intersection in the map grid is surrounded by 4 river tiles. There can be larger river grids consisting of several overlapping minimal 4-river-grids.

Possible values: 0: Is not forming a 4-river-grid. 1: Is forming a 4-river-grid.

Highlands: (river_test_highlands) Rivers must not flow up in mountains or hills if there are alternatives.

Possible values: 0: Is not hills and not mountains. 1: Is hills. 2: Is mountains.

Adjacent ocean: (river_test_adjacent_ocean) Rivers must flow down to coastal areas when possible:

Possible values: 0-100

Adjacent river: (river_test_adjacent_river) Rivers must flow down to areas near other rivers when possible:

Possible values: 0-100

Adjacent highlands: (river_test_adjacent_highlands) Rivers must not flow towards highlands if there are alternatives.

Swamps: (river_test_swamp) Rivers must flow down in swamps when possible.

Possible values: 0: Is swamps. 1: Is not swamps.

Adjacent swamps: (river_test_adjacent_swamp) Rivers must flow towards swamps when possible.

height_map: (river_test_height_map) Rivers must flow in the direction which takes it to the tile with the lowest value on the height_map.

Possible values: n: height_map[...]

If these rules haven't decided the direction, the random number generator gets the desicion. -Erik Sigra

Definition at line 719 of file mapgen.cpp.

Referenced by make_rivers().

◆ make_rivers()

static void make_rivers ( )
static

Calls make_river until there are enough river tiles on the map.

It stops when it has tried to create RIVERS_MAXTRIES rivers. -Erik Sigra

Definition at line 840 of file mapgen.cpp.

Referenced by make_land().

◆ make_terrains()

static void make_terrains ( )
static

Make_terrains calls make_forest, make_dessert,etc with random free locations until there has been made enough.

Comment: funtions as make_swamp, etc. has to have a non 0 probability to place one terrains in called position. Else make_terrains will get in a infinite loop!

Definition at line 415 of file mapgen.cpp.

Referenced by make_land().

◆ map_generate()

bool map_generate ( bool  autosize,
struct unit_type initial_unit 
)

See stdinhand.c for information on map generation methods.

FIXME: Some continent numbers are unused at the end of this function, fx removed completely by remove_tiny_islands. When this function is finished various data is written to "islands", indexed by continent numbers, so a simple renumbering would not work...

If "autosize" is specified then mapgen will automatically size the map based on the map.server.size server parameter and the specified topology. If not map.xsize and map.ysize will be used.

Definition at line 1232 of file mapgen.cpp.

Referenced by srv_ready().

◆ near_safe_tiles()

static bool near_safe_tiles ( struct tile ptile)
static

Return TRUE if a safe tile is in a radius of 1.

This function is used to test where to place specials on the sea.

Definition at line 1441 of file mapgen.cpp.

Referenced by add_resources().

◆ ok_for_separate_poles()

static bool ok_for_separate_poles ( struct tile ptile)
static

If separatepoles is set, return false if this tile has to keep ocean.

Definition at line 281 of file mapgen.cpp.

Referenced by make_polar_land().

◆ place_terrain()

static void place_terrain ( struct tile ptile,
int  diff,
struct terrain pterrain,
int *  to_be_placed,
wetness_c  wc,
temperature_type  tc,
miscellaneous_c  mc 
)
static

Recursively generate terrains.

Definition at line 323 of file mapgen.cpp.

◆ print_mapgen_map()

static void print_mapgen_map ( )
static

Debugging function to print information about the map that's been generated.

Definition at line 1160 of file mapgen.cpp.

Referenced by map_generate().

◆ rand_map_pos_characteristic()

static struct tile* rand_map_pos_characteristic ( wetness_c  wc,
temperature_type  tc,
miscellaneous_c  mc 
)
static

Return random map coordinates which have some conditions and which are not yet placed on pmap.

Returns FALSE if there is no such position.

Definition at line 180 of file mapgen.cpp.

Referenced by make_huts(), make_rivers(), and make_terrains().

◆ remove_tiny_islands()

static void remove_tiny_islands ( )
static

Removes all 1x1 islands (sets them to ocean).

This happens before regenerate_lakes(), so don't need to worry about TER_FRESHWATER here.

Definition at line 1132 of file mapgen.cpp.

Referenced by map_generate().

◆ river_blockmark()

static void river_blockmark ( struct river_map privermap,
struct tile ptile 
)
static

Called from make_river.

Marks all directions as blocked. -Erik Sigra

Definition at line 598 of file mapgen.cpp.

Referenced by make_river().

◆ river_test_adjacent_highlands()

static int river_test_adjacent_highlands ( struct river_map privermap,
struct tile ptile,
struct extra_type priver 
)
static

Help function used in make_river().

See the help there.

Definition at line 543 of file mapgen.cpp.

◆ river_test_adjacent_ocean()

static int river_test_adjacent_ocean ( struct river_map privermap,
struct tile ptile,
struct extra_type priver 
)
static

Help function used in make_river().

See the help there.

Definition at line 523 of file mapgen.cpp.

◆ river_test_adjacent_river()

static int river_test_adjacent_river ( struct river_map privermap,
struct tile ptile,
struct extra_type priver 
)
static

Help function used in make_river().

See the help there.

Definition at line 533 of file mapgen.cpp.

◆ river_test_adjacent_swamp()

static int river_test_adjacent_swamp ( struct river_map privermap,
struct tile ptile,
struct extra_type priver 
)
static

Help function used in make_river().

See the help there.

Definition at line 570 of file mapgen.cpp.

◆ river_test_blocked()

static int river_test_blocked ( struct river_map privermap,
struct tile ptile,
struct extra_type priver 
)
static

Help function used in make_river().

See the help there.

Definition at line 481 of file mapgen.cpp.

◆ river_test_height_map()

static int river_test_height_map ( struct river_map privermap,
struct tile ptile,
struct extra_type priver 
)
static

Help function used in make_river().

See the help there.

Definition at line 588 of file mapgen.cpp.

◆ river_test_highlands()

static int river_test_highlands ( struct river_map privermap,
struct tile ptile,
struct extra_type priver 
)
static

Help function used in make_river().

See the help there.

Definition at line 513 of file mapgen.cpp.

◆ river_test_rivergrid()

static int river_test_rivergrid ( struct river_map privermap,
struct tile ptile,
struct extra_type priver 
)
static

Help function used in make_river().

See the help there.

Definition at line 503 of file mapgen.cpp.

◆ river_test_swamp()

static int river_test_swamp ( struct river_map privermap,
struct tile ptile,
struct extra_type priver 
)
static

Help function used in make_river().

See the help there.

Definition at line 561 of file mapgen.cpp.

◆ river_types_init()

static void river_types_init ( )
static

Initialize river types array.

Definition at line 1537 of file mapgen.cpp.

Referenced by map_generate().

◆ terrain_is_too_high()

static bool terrain_is_too_high ( struct tile ptile,
int  thill,
int  my_height 
)
static

We don't want huge areas of hill/mountains, so we put in a plains here and there, where it gets too 'heigh'.

Return TRUE if the terrain at the given map position is too heigh.

Definition at line 199 of file mapgen.cpp.

Referenced by make_relief().

◆ test_miscellaneous()

static bool test_miscellaneous ( const struct tile ptile,
miscellaneous_c  c 
)
static

Checks if the given location satisfy some miscellaneous condition.

Definition at line 139 of file mapgen.cpp.

Referenced by condition_filter(), and place_terrain().

◆ test_wetness()

bool test_wetness ( const struct tile ptile,
wetness_c  c 
)

These functions test for conditions used in rand_map_pos_characteristic.

Checks if the given location satisfy some wetness condition

Definition at line 122 of file mapgen.cpp.

Referenced by condition_filter(), fill_island(), fill_island_rivers(), and place_terrain().

Variable Documentation

◆ desert_pct

int desert_pct = 0

◆ forest_pct

int forest_pct = 0

◆ hmap_low_level

int hmap_low_level = 0
static

Definition at line 103 of file mapgen.cpp.

◆ jungle_pct

int jungle_pct = 0

◆ mountain_pct

int mountain_pct = 0

◆ river_pct

int river_pct = 0

Definition at line 98 of file mapgen.cpp.

Referenced by adjust_terrain_param(), fair_map_island_new(), make_island(), and make_rivers().

◆ river_type_count

int river_type_count = 0

Definition at line 44 of file mapgen.cpp.

Referenced by fair_map_island_new(), fill_island_rivers(), make_rivers(), and river_types_init().

◆ river_types

struct extra_type* river_types[MAX_ROAD_TYPES]

Definition at line 43 of file mapgen.cpp.

Referenced by fair_map_island_new(), fill_island_rivers(), make_rivers(), and river_types_init().

◆ swamp_pct

int swamp_pct = 0

◆ test_funcs

struct test_func test_funcs[NUM_TEST_FUNCTIONS]
static
Initial value:
= {
{river_test_swamp, false},
static int river_test_adjacent_swamp(struct river_map *privermap, struct tile *ptile, struct extra_type *priver)
Help function used in make_river().
Definition: mapgen.cpp:570
static int river_test_height_map(struct river_map *privermap, struct tile *ptile, struct extra_type *priver)
Help function used in make_river().
Definition: mapgen.cpp:588
static int river_test_adjacent_ocean(struct river_map *privermap, struct tile *ptile, struct extra_type *priver)
Help function used in make_river().
Definition: mapgen.cpp:523
static int river_test_adjacent_highlands(struct river_map *privermap, struct tile *ptile, struct extra_type *priver)
Help function used in make_river().
Definition: mapgen.cpp:543
static int river_test_highlands(struct river_map *privermap, struct tile *ptile, struct extra_type *priver)
Help function used in make_river().
Definition: mapgen.cpp:513
static int river_test_rivergrid(struct river_map *privermap, struct tile *ptile, struct extra_type *priver)
Help function used in make_river().
Definition: mapgen.cpp:503
static int river_test_adjacent_river(struct river_map *privermap, struct tile *ptile, struct extra_type *priver)
Help function used in make_river().
Definition: mapgen.cpp:533
static int river_test_swamp(struct river_map *privermap, struct tile *ptile, struct extra_type *priver)
Help function used in make_river().
Definition: mapgen.cpp:561
static int river_test_blocked(struct river_map *privermap, struct tile *ptile, struct extra_type *priver)
Help function used in make_river().
Definition: mapgen.cpp:481

Definition at line 598 of file mapgen.cpp.

Referenced by make_river().