allow lua mapgen to use the default mapgen.

This commit is contained in:
gfgtdf 2018-09-24 23:51:12 +02:00
parent 3d1e3b36df
commit 2d5382f54c
4 changed files with 99 additions and 9 deletions

View file

@ -19,6 +19,7 @@
struct generator_data {
generator_data(const config& cfg);
generator_data() = default;
int width;
int height;

View file

@ -254,10 +254,8 @@ default_map_generator_job::default_map_generator_job(uint32_t seed)
*/
height_map default_map_generator_job::generate_height_map(std::size_t width, std::size_t height, std::size_t iterations, std::size_t hill_size, std::size_t island_size, std::size_t island_off_center)
{
height_map res(width, std::vector<int>(height,0));
std::size_t center_x = width/2;
std::size_t center_y = height/2;
size_t center_x = width/2;
size_t center_y = height/2;
LOG_NG << "off-centering...\n";
@ -285,6 +283,12 @@ height_map default_map_generator_job::generate_height_map(std::size_t width, std
break;
}
}
return generate_height_map(width, height, iterations, hill_size, island_size, center_x, center_y);
}
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 center_x, size_t center_y)
{
height_map res(width, std::vector<int>(height,0));
DBG_NG << iterations << " iterations\n";
for(std::size_t i = 0; i != iterations; ++i) {

View file

@ -38,20 +38,26 @@ public:
/** Generate the map. */
std::string default_generate_map(generator_data data, std::map<map_location,std::string>* labels, const config& cfg);
private:
typedef std::vector<std::vector<int>> height_map;
typedef t_translation::ter_map terrain_map;
height_map 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 generate_height_map(size_t width, size_t height,
size_t iterations, size_t hill_size,
size_t island_size, size_t center_x, size_t center_y);
private:
bool generate_river_internal(const height_map& heights,
terrain_map& terrain, int x, int y, std::vector<map_location>& river,
std::set<map_location>& seen_locations, int river_uphill);
std::vector<map_location> generate_river(const height_map& heights, terrain_map& terrain, int x, int y, int river_uphill);
height_map generate_height_map(std::size_t width, std::size_t height,
std::size_t iterations, std::size_t hill_size,
std::size_t island_size, std::size_t island_off_center);
bool generate_lake(t_translation::ter_map& terrain, int x, int y, int lake_fall_off, std::set<map_location>& locs_touched);
map_location random_point_at_side(std::size_t width, std::size_t height);

View file

@ -30,6 +30,7 @@
#include "lua/lauxlib.h"
#include "lua/lua.h"
#include "scripting/push_check.hpp"
#include "generators/default_map_generator_job.hpp"
static lg::log_domain log_mapgen("mapgen");
#define ERR_NG LOG_STREAM(err, log_mapgen)
@ -71,6 +72,82 @@ static int intf_random(lua_State *L)
}
}
/**
* calls the default mapgenerator.
*/
static int intf_default_generate(lua_State *L)
{
std::mt19937& rng = lua_kernel_base::get_lua_kernel<mapgen_lua_kernel>(L).get_default_rng();
int width = luaL_checkinteger(L, 1);
int height = luaL_checkinteger(L, 2);
config cfg = luaW_checkconfig(L, 3);
generator_data arg;
arg.width = width;
arg.height = height;
arg.nplayers = cfg["nplayers"].to_int(2);
arg.nvillages = cfg["nvillages"].to_int(0);
arg.iterations = cfg["iterations"].to_int(0);
arg.hill_size = cfg["hill_size"].to_int(0);
arg.castle_size = cfg["castle_size"].to_int(0);
arg.island_size = cfg["island_size"].to_int(0);
arg.island_off_center = cfg["island_off_center"].to_int(0);
arg.max_lakes = cfg["max_lakes"].to_int(0);
arg.link_castles = cfg["link_castles"].to_bool();
arg.show_labels = cfg["show_labels"].to_bool(0);
uint32_t seed = cfg["seed"].to_int(0);
if(!cfg.has_attribute("seed")) {
seed = rng();
}
default_map_generator_job job(seed);
std::string res = job.default_generate_map(arg, nullptr, cfg);
lua_push(L, res);
return 1;
}
/**
* calls the default mapgenerator.
*/
static int intf_default_generate_height_map(lua_State *L)
{
std::mt19937& rng = lua_kernel_base::get_lua_kernel<mapgen_lua_kernel>(L).get_default_rng();
int width = luaL_checkinteger(L, 1);
int height = luaL_checkinteger(L, 2);
config cfg = luaW_checkconfig(L, 3);
int iterations = cfg["iterations"].to_int(1);
int hill_size = cfg["hill_size"].to_int(1);
int island_size = cfg["island_size"].to_int(width/2);
int center_x = cfg["center_x"].to_int(width/2);
int center_y = cfg["center_y"].to_int(height/2);
bool flip_layout = cfg["flip_format"].to_bool();
uint32_t seed = cfg["seed"].to_int(0);
if(!cfg.has_attribute("seed")) {
seed = rng();
}
default_map_generator_job job(seed);
default_map_generator_job::height_map res = job.generate_height_map(width, height, iterations, hill_size, island_size, center_x, center_y);
lua_createtable (L, width * height, 0);
assert(int(res.size()) == width);
assert((width == 0 || int(res[0].size()) == height));
for(int x = 0; x != width; ++x) {
for(int y = 0; y != height; ++y) {
int h = res[x][y];
int i = flip_layout ? (y + x * height) : (x + y * width);
lua_pushinteger (L, h);
lua_rawseti(L, -2, i);
}
}
return 1;
}
/**
* Finds a path between two locations.
* - Args 1,2: source location.
@ -139,6 +216,8 @@ mapgen_lua_kernel::mapgen_lua_kernel()
{ "random", &intf_random },
{ "create_filter", &intf_terainfilter_create },
{ "create_map", &intf_terainmap_create },
{ "default_generate_height_map", &intf_default_generate_height_map },
{ "generate_default_map", &intf_default_generate },
{ nullptr, nullptr }
};