38 #define ERR_NG LOG_STREAM(err, log_mapgen)
39 #define LOG_NG LOG_STREAM(info, log_mapgen)
40 #define DBG_NG LOG_STREAM(debug, log_mapgen)
48 template <member_callback method>
50 return ((lua_kernel_base::get_lua_kernel<mapgen_lua_kernel>(L)).*method)(L);
58 std::mt19937& rng = lua_kernel_base::get_lua_kernel<mapgen_lua_kernel>(L).get_default_rng();
59 if(lua_isnoneornil(L, 1)) {
60 double r = double (rng());
61 double r_max = double (rng.max());
68 if(lua_isnumber(L, 2)) {
69 min = lua_check<int32_t>(L, 1);
70 max = lua_check<int32_t>(L, 2);
74 max = lua_check<int32_t>(L, 1);
77 return luaL_argerror(L, 1,
"min > max");
79 lua_push(L, min +
static_cast<int>(rng() % (max - min + 1)));
89 std::mt19937& rng = lua_kernel_base::get_lua_kernel<mapgen_lua_kernel>(L).get_default_rng();
91 int width = luaL_checkinteger(L, 1);
92 int height = luaL_checkinteger(L, 2);
99 arg.
nplayers = cfg[
"nplayers"].to_int(2);
100 arg.
nvillages = cfg[
"nvillages"].to_int(0);
102 arg.
hill_size = cfg[
"hill_size"].to_int(0);
106 arg.
max_lakes = cfg[
"max_lakes"].to_int(0);
110 uint32_t seed = cfg[
"seed"].to_int(0);
127 std::mt19937& rng = lua_kernel_base::get_lua_kernel<mapgen_lua_kernel>(L).get_default_rng();
129 int width = luaL_checkinteger(L, 1);
130 int height = luaL_checkinteger(L, 2);
138 int iterations = cfg[
"iterations"].to_int(1);
139 int hill_size = cfg[
"hill_size"].to_int(1);
140 int island_size = cfg[
"island_size"].to_int(width/2);
141 int center_x = cfg[
"center_x"].to_int(width/2);
142 int center_y = cfg[
"center_y"].to_int(height/2);
143 bool flip_layout = cfg[
"flip_format"].to_bool();
144 bool as_locset = cfg[
"location_set"].to_bool(
false);
145 uint32_t seed = cfg[
"seed"].to_int(0);
152 lua_createtable (L, width * height, 0);
153 assert(
int(res.size()) == width);
154 assert((width == 0 ||
int(res[0].
size()) == height));
156 for(
int x = 0; x != width; ++x) {
157 for(
int y = 0; y != height; ++y) {
159 lua_pushinteger (L,
h);
162 lua_rawseti(L, -2, loc_hash(loc));
164 int i = flip_layout ? (y + x * height) : (x + y * width);
165 lua_rawseti(L, -2,
i);
187 if(lua_isfunction(L, arg)) {
188 const char *
msg = lua_pushfstring(L,
"%s expected, got %s", lua_typename(L, LUA_TFUNCTION), luaL_typename(L, 3));
189 return luaL_argerror(L, 3,
msg);
191 std::optional<lua_pathfind_cost_calculator> calc;
194 if(lua_istable(L, 3)) {
198 return luaL_argerror(L, 3,
"missing key: calculate");
201 width = luaL_checkinteger(L, -1);
203 return luaL_argerror(L, 3,
"missing key: width");
206 height = luaL_checkinteger(L, -1);
208 return luaL_argerror(L, 3,
"missing key: height");
215 width = luaL_checkinteger(L, 4);
216 height = luaL_checkinteger(L, 5);
217 if(lua_isboolean(L, 6)) {
223 int nb = res.
steps.size();
224 lua_createtable(L, nb, 0);
225 for (
int i = 0;
i < nb; ++
i)
227 lua_createtable(L, 2, 0);
228 lua_pushinteger(L, res.
steps[
i].wml_x());
229 lua_rawseti(L, -2, 1);
230 lua_pushinteger(L, res.
steps[
i].wml_y());
231 lua_rawseti(L, -2, 2);
232 lua_rawseti(L, -2,
i + 1);
250 lua_getglobal(L,
"mathx");
252 lua_setfield(L, -2,
"random");
256 static luaL_Reg
const map_callbacks[] {
269 assert(lua_istable(L,-1));
270 luaL_setfuncs(L, map_callbacks, 0);
272 assert(lua_gettop(L) == 0);
275 cmd_log_ <<
"Adding paths module...\n";
276 static luaL_Reg
const path_callbacks[] {
280 lua_getglobal(L,
"wesnoth");
282 luaL_setfuncs(L, path_callbacks, 0);
283 lua_setfield(L, -2,
"paths");
287 lua_getglobal(L,
"wml");
288 static luaL_Reg
const wml_callbacks[] {
291 {
"get_variable", &dispatch<&mapgen_lua_kernel::intf_get_variable>},
292 {
"get_all_vars", &dispatch<&mapgen_lua_kernel::intf_get_all_vars>},
295 luaL_setfuncs(L, wml_callbacks, 0);
316 char const *m = luaL_checkstring(L, 1);
335 if (!lua_isstring(
mState,-1)) {
336 std::string
msg =
"expected a string, found a ";
342 return lua_tostring(
mState, -1);
351 if (!lua_istable(
mState, -1)) {
352 std::string
msg =
"expected a config (table), found a ";
359 std::string
msg =
"expected a config, but it is malformed ";
A config object defines a single node in a WML file, with access to child nodes.
bool has_attribute(config_key_type key) const
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)
Generate a height-map.
std::string default_generate_map(generator_data data, std::map< map_location, std::string > *labels, const config &cfg)
Generate the map.
std::vector< std::vector< int > > height_map
bool protected_call(int nArgs, int nRets, error_handler)
bool load_string(char const *prog, const std::string &name, error_handler)
virtual uint32_t get_random_seed()
virtual void throw_exception(char const *msg, char const *context="Lua error")
int intf_get_variable(lua_State *L)
virtual uint32_t get_random_seed()
void run_generator(const char *prog, const config &generator)
void user_config(const char *prog, const config &generator)
std::optional< std::mt19937 > default_rng_
mapgen_lua_kernel(const config *vars)
std::mt19937 & get_default_rng()
std::optional< uint32_t > random_seed_
std::string create_map(const char *prog, const config &generator, std::optional< uint32_t > seed)
int intf_get_all_vars(lua_State *L)
config create_scenario(const char *prog, const config &generator, std::optional< uint32_t > seed)
Information on a WML variable.
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
Interfaces for manipulating version numbers of engine, add-ons, etc.
Standard logging facilities (interface).
void luaW_pushconfig(lua_State *L, const config &cfg)
Converts a config object to a Lua table pushed at the top of the stack.
config luaW_checkconfig(lua_State *L, int index)
Converts an optional table or vconfig to a config object.
bool luaW_pushvariable(lua_State *L, variable_access_const &v)
bool luaW_toboolean(lua_State *L, int n)
bool luaW_tableget(lua_State *L, int index, const char *key)
bool luaW_toconfig(lua_State *L, int index, config &cfg)
Converts an optional table or vconfig to a config object.
map_location luaW_checklocation(lua_State *L, int index)
Converts an optional table or pair of integers to a map location object.
bool luaW_getglobal(lua_State *L, const std::vector< std::string > &path)
Pushes the value found by following the variadic names (char *), if the value is not nil.
int intf_mg_get_tiles_radius(lua_State *L)
int intf_terrainfilter_create(lua_State *L)
Create a filter.
int intf_mg_get_locations(lua_State *L)
int intf_terrainmap_create(lua_State *L)
Create a map.
int dispatch(lua_State *L)
static int intf_default_generate_height_map(lua_State *L)
calls the default mapgenerator.
static lg::log_domain log_mapgen("mapgen")
static int intf_find_path(lua_State *L)
Finds a path between two locations.
static int intf_random(lua_State *L)
Returns a random number, same interface as math.random.
int(mapgen_lua_kernel::* member_callback)(lua_State *)
static int intf_default_generate(lua_State *L)
calls the default mapgenerator.
int intf_tovconfig(lua_State *L)
Creates a vconfig containing the WML table.
std::string register_metatables(lua_State *L)
std::string register_metatables(lua_State *L)
plain_route a_star_search(const map_location &src, const map_location &dst, double stop_at, const cost_calculator &calc, const std::size_t width, const std::size_t height, const teleport_map *teleports, bool border)
rng * generator
This generator is automatically synced during synced context.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
void lua_push(lua_State *L, const T &val)
Error used to report an error in a lua script or in the lua interpreter.
Cost function object relying on a Lua function.
Encapsulates the map of the game.
Structure which holds a single route between one location and another.
std::vector< map_location > steps
int move_cost
Movement cost for reaching the end of the route.