35 #define ERR_NG LOG_STREAM(err, log_mapgen)
36 #define LOG_NG LOG_STREAM(info, log_mapgen)
37 #define DBG_NG LOG_STREAM(debug, log_mapgen)
44 template <member_callback method>
46 return ((lua_kernel_base::get_lua_kernel<mapgen_lua_kernel>(L)).*method)(L);
54 std::mt19937& rng = lua_kernel_base::get_lua_kernel<mapgen_lua_kernel>(L).get_default_rng();
55 if(lua_isnoneornil(L, 1)) {
56 double r = double (rng());
57 double r_max = double (rng.max());
64 if(lua_isnumber(L, 2)) {
65 min = lua_check<int32_t>(L, 1);
66 max = lua_check<int32_t>(L, 2);
70 max = lua_check<int32_t>(L, 1);
73 return luaL_argerror(L, 1,
"min > max");
75 lua_push(L, min +
static_cast<int>(rng() % (max - min + 1)));
85 std::mt19937& rng = lua_kernel_base::get_lua_kernel<mapgen_lua_kernel>(L).get_default_rng();
87 int width = luaL_checkinteger(L, 1);
88 int height = luaL_checkinteger(L, 2);
95 arg.
nplayers = cfg[
"nplayers"].to_int(2);
96 arg.
nvillages = cfg[
"nvillages"].to_int(0);
98 arg.
hill_size = cfg[
"hill_size"].to_int(0);
102 arg.
max_lakes = cfg[
"max_lakes"].to_int(0);
106 uint32_t seed = cfg[
"seed"].to_int(0);
123 std::mt19937& rng = lua_kernel_base::get_lua_kernel<mapgen_lua_kernel>(L).get_default_rng();
125 int width = luaL_checkinteger(L, 1);
126 int height = luaL_checkinteger(L, 2);
134 int iterations = cfg[
"iterations"].to_int(1);
135 int hill_size = cfg[
"hill_size"].to_int(1);
136 int island_size = cfg[
"island_size"].to_int(width/2);
137 int center_x = cfg[
"center_x"].to_int(width/2);
138 int center_y = cfg[
"center_y"].to_int(height/2);
139 bool flip_layout = cfg[
"flip_format"].to_bool();
140 bool as_locset = cfg[
"location_set"].to_bool(
false);
141 uint32_t seed = cfg[
"seed"].to_int(0);
148 lua_createtable (L, width * height, 0);
149 assert(
int(res.size()) == width);
150 assert((width == 0 ||
int(res[0].
size()) == height));
152 for(
int x = 0; x != width; ++x) {
153 for(
int y = 0; y != height; ++y) {
155 lua_pushinteger (L,
h);
158 lua_rawseti(L, -2, loc_hash(
loc));
160 int i = flip_layout ? (y + x * height) : (x + y * width);
161 lua_rawseti(L, -2,
i);
183 if(lua_isfunction(L, arg)) {
184 const char *
msg = lua_pushfstring(L,
"%s expected, got %s", lua_typename(L, LUA_TFUNCTION), luaL_typename(L, 3));
185 return luaL_argerror(L, 3,
msg);
187 utils::optional<lua_pathfind_cost_calculator> calc;
190 if(lua_istable(L, 3)) {
194 return luaL_argerror(L, 3,
"missing key: calculate");
197 width = luaL_checkinteger(L, -1);
199 return luaL_argerror(L, 3,
"missing key: width");
202 height = luaL_checkinteger(L, -1);
204 return luaL_argerror(L, 3,
"missing key: height");
211 width = luaL_checkinteger(L, 4);
212 height = luaL_checkinteger(L, 5);
213 if(lua_isboolean(L, 6)) {
219 int nb = res.
steps.size();
220 lua_createtable(L, nb, 0);
221 for (
int i = 0;
i < nb; ++
i)
223 lua_createtable(L, 2, 0);
224 lua_pushinteger(L, res.
steps[
i].wml_x());
225 lua_rawseti(L, -2, 1);
226 lua_pushinteger(L, res.
steps[
i].wml_y());
227 lua_rawseti(L, -2, 2);
228 lua_rawseti(L, -2,
i + 1);
246 lua_getglobal(L,
"mathx");
248 lua_setfield(L, -2,
"random");
252 static luaL_Reg
const map_callbacks[] {
265 assert(lua_istable(L,-1));
266 luaL_setfuncs(L, map_callbacks, 0);
268 assert(lua_gettop(L) == 0);
271 cmd_log_ <<
"Adding paths module...\n";
272 static luaL_Reg
const path_callbacks[] {
276 lua_getglobal(L,
"wesnoth");
278 luaL_setfuncs(L, path_callbacks, 0);
279 lua_setfield(L, -2,
"paths");
283 lua_getglobal(L,
"wml");
284 static luaL_Reg
const wml_callbacks[] {
287 {
"get_variable", &dispatch<&mapgen_lua_kernel::intf_get_variable>},
288 {
"get_all_vars", &dispatch<&mapgen_lua_kernel::intf_get_all_vars>},
291 luaL_setfuncs(L, wml_callbacks, 0);
312 char const *m = luaL_checkstring(L, 1);
331 if (!lua_isstring(
mState,-1)) {
332 std::string
msg =
"expected a string, found a ";
338 return lua_tostring(
mState, -1);
347 if (!lua_istable(
mState, -1)) {
348 std::string
msg =
"expected a config (table), found a ";
355 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 load_string(char const *prog, const std::string &name, const error_handler &)
bool protected_call(int nArgs, int nRets, const 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::string create_map(const char *prog, const config &generator, utils::optional< uint32_t > seed)
config create_scenario(const char *prog, const config &generator, utils::optional< uint32_t > seed)
mapgen_lua_kernel(const config *vars)
std::mt19937 & get_default_rng()
utils::optional< uint32_t > random_seed_
int intf_get_all_vars(lua_State *L)
utils::optional< std::mt19937 > default_rng_
Information on a WML variable.
Definitions for the interface to Wesnoth Markup Language (WML).
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
@ border
The border of the map.
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(std::string_view 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)
rect dst
Location on the final composed sheet.
rect src
Non-transparent portion of the surface to compose.
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.