28 #define LOG_LUA LOG_STREAM(info, log_scripting_lua)
29 #define ERR_LUA LOG_STREAM(err, log_scripting_lua)
39 using std::string_view;
46 string_view
id = luaL_checkstring(L, 2);
60 string_view
id = luaL_checkstring(L, 2);
70 const t_translation::starting_positions::left_map& left = m.
special_locations().left;
72 t_translation::starting_positions::left_const_iterator it;
73 if (lua_isnoneornil(L, 2)) {
77 it = left.find(luaL_checkstring(L, 2));
78 if (it == left.end()) {
83 if (it == left.end()) {
86 lua_pushstring(L, it->first.c_str());
185 throw "luaW_type_error didn't throw";
198 if(lua_isnumber(L, 1) && lua_isnumber(L, 2)) {
199 int w = lua_tointeger(L, 1);
200 int h = lua_tointeger(L, 2);
204 string_view data_str = luaL_checkstring(L, 1);
224 m->lua_map_ref::~lua_map_ref();
237 bool replace_if_failed =
false;
240 replace_if_failed =
true;
242 t_str = luaL_checkstring(L, -1);
246 t_str = luaL_checkstring(L, 3);
247 if(t_str.front() ==
'^') {
249 }
else if(t_str.back() ==
'^') {
256 if(
auto gm =
dynamic_cast<gamemap*
>(&map)) {
268 }
else map.
set_terrain(loc, ter, mode, replace_if_failed);
286 char const *m = luaL_checkstring(L, 2);
296 if(strcmp(m,
"special_locations") == 0) {
324 char const *m = luaL_checkstring(L, 2);
325 std::string err_msg =
"unknown modifiable property of map: ";
327 return luaL_argerror(L, 2, err_msg.c_str());
352 template<
bool with_border>
363 if(prev_loc.
wml_x() ==
w) {
364 if(prev_loc.
wml_y() ==
h) {
368 x = with_border ? 0 : 1;
369 y = prev_loc.
wml_y() + 1;
372 x = prev_loc.
wml_x() + 1;
373 y = prev_loc.
wml_y();
379 lua_replace(L, lua_upvalueindex(2));
382 lua_pushinteger(L, x);
383 lua_pushinteger(L, y);
392 bool with_border = lua_isboolean(L, 2) ?
luaW_toboolean(L, 2) :
false;
397 lua_pushcclosure(L, impl_terrainmap_iter<true>, 2);
399 lua_pushcclosure(L, impl_terrainmap_iter<false>, 2);
425 std::vector<gamemap::overlay_rule> rules;
426 for (
int i = 1, i_end = lua_rawlen(L,
index);
i <= i_end; ++
i)
429 if(!lua_istable(L, -1)) {
430 luaL_argerror(L,
index,
"rules must be a table of tables");
433 auto& rule = rules.back();
452 if(!terrain.empty()) {
453 rule.terrain_ = terrain[0];
487 bool ignore_special_locations =
false;
488 std::vector<gamemap::overlay_rule> rules;
490 if(lua_istable(L, 4)) {
492 ignore_special_locations =
luaW_table_get_def(L, 4,
"ignore_special_locations",
false);
495 if(!lua_istable(L, -1)) {
496 return luaL_argerror(L, 4,
"rules must be a table");
503 if(lua_isstring(L, 3)) {
504 const std::string t_str = luaL_checkstring(L, 3);
505 std::unique_ptr<gamemap_base> mask;
506 if(
dynamic_cast<gamemap*
>(&map)) {
507 auto mask_ptr =
new gamemap(
"");
508 mask_ptr->read(t_str,
false);
509 mask.reset(mask_ptr);
513 map.
overlay(*mask, loc, rules,
is_odd, ignore_special_locations);
516 map.
overlay(mask, loc, rules,
is_odd, ignore_special_locations);
520 if(
auto gmap =
dynamic_cast<gamemap*
>(&map)) {
522 t.fix_villages(*gmap);
537 if(!lua_isnoneornil(L, 2)) {
538 string_view mode_str = luaL_checkstring(L, 2);
539 if(mode_str ==
"base") {
541 }
else if(mode_str ==
"overlay") {
543 }
else if(mode_str !=
"both") {
544 return luaL_argerror(L, 2,
"must be one of 'base', 'overlay', or 'both'");
548 lua_newuserdatauv(L, 0, 2);
549 lua_pushinteger(L,
int(mode));
559 static const char* mode_strs[] = {
"base",
"overlay",
"both"};
561 string_view t_str = luaL_checkstring(L, -1);
563 int mode = luaL_checkinteger(L, -1);
564 lua_pushfstring(L,
"replace_if_failed('%s', '%s')", t_str.data(), mode_strs[mode]);
571 std::ostringstream cmd_out;
573 cmd_out <<
"Adding terrain map metatable...\n";
577 lua_setfield(L, -2,
"__gc");
579 lua_setfield(L, -2,
"__index");
581 lua_setfield(L, -2,
"__newindex");
583 lua_setfield(L, -2,
"__metatable");
587 lua_setfield(L, -2,
"__tostring");
589 lua_setfield(L, -2,
"__metatable");
591 cmd_out <<
"Adding special locations metatable...\n";
595 lua_setfield(L, -2,
"__index");
597 lua_setfield(L, -2,
"__newindex");
599 lua_setfield(L, -2,
"__pairs");
601 lua_setfield(L, -2,
"__metatable");
603 return cmd_out.str();
bool change_terrain(const map_location &loc, const std::string &t, const std::string &mode, bool replace_if_failed)
void needs_rebuild(bool b)
Sets whether the screen (map visuals) needs to be rebuilt.
terrain_code get_terrain(const map_location &loc) const
Looks up terrain at a particular location.
int w() const
Effective map width.
void set_special_location(const std::string &id, const map_location &loc)
int h() const
Effective map height.
void overlay(const gamemap_base &m, map_location loc, const std::vector< overlay_rule > &rules=std::vector< overlay_rule >(), bool is_odd=false, bool ignore_special_locations=false)
Overlays another map onto this one at the given position.
map_location special_location(const std::string &id) const
virtual void set_terrain(const map_location &loc, const terrain_code &terrain, const terrain_type_data::merge_mode mode=terrain_type_data::BOTH, bool replace_if_failed=false)=0
Clobbers over the terrain at location 'loc', with the given terrain.
int total_width() const
Real width of the map, including borders.
std::string to_string() const
bool on_board_with_border(const map_location &loc) const
int total_height() const
Real height of the map, including borders.
int border_size() const
Size of the map border.
bool on_board(const map_location &loc) const
Tell if a location is on the map.
location_map & special_locations()
Encapsulates the map of the game.
void set_terrain(const map_location &loc, const terrain_code &terrain, const terrain_type_data::merge_mode mode=terrain_type_data::BOTH, bool replace_if_failed=false) override
Clobbers over the terrain at location 'loc', with the given terrain.
mapgen_gamemap(std::string_view data)
game_display & get_display() override
Get a reference to a display member a derived class uses.
This class stores all the data for a single 'side' (in game nomenclature).
Standard logging facilities (interface).
void luaW_pushlocation(lua_State *L, const map_location &ml)
Converts a map location object to a Lua table pushed at the top of the stack.
bool luaW_toboolean(lua_State *L, int n)
int luaW_type_error(lua_State *L, int narg, const char *tname)
std::string_view luaW_tostring(lua_State *L, int index)
bool luaW_tableget(lua_State *L, int index, const char *key)
bool luaW_tolocation(lua_State *L, int index, map_location &loc)
Converts an optional table or pair of integers to a map location 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.
#define return_string_attrib(name, accessor)
#define return_int_attrib(name, accessor)
static int impl_slocs_get(lua_State *L)
static const char terrainmapKey[]
static void luaW_push_terrain(lua_State *L, gamemap_base &map, map_location loc)
static lg::log_domain log_scripting_lua("scripting/lua")
static const char maplocationKey[]
static std::vector< gamemap::overlay_rule > read_rules_vector(lua_State *L, int index)
int intf_terrainmap_create(lua_State *L)
Create a map.
static int impl_terrainmap_get(lua_State *L)
Gets some data on a map (__index metamethod).
int intf_terrain_mask(lua_State *L)
Replaces part of the map.
static int impl_replace_if_failed_tostring(lua_State *L)
static void impl_merge_terrain(lua_State *L, gamemap_base &map, map_location loc)
static int impl_slocs_iter(lua_State *L)
int intf_on_border(lua_State *L)
static int impl_terrainmap_set(lua_State *L)
Sets some data on a map (__newindex metamethod).
int intf_on_board(lua_State *L)
int intf_terrainmap_iter(lua_State *L)
static int impl_slocs_set(lua_State *L)
static int impl_terrainmap_iter(lua_State *L)
int intf_terrainmap_get(lua_State *L)
static int impl_terrainmap_collect(lua_State *L)
Destroys a map object before it is collected (__gc metamethod).
static int impl_slocs_next(lua_State *L)
static void simplemerge(t_translation::terrain_code old_t, t_translation::terrain_code &new_t, const terrain_type_data::merge_mode mode)
gamemap_base & luaW_checkterrainmap(lua_State *L, int index)
static const char mapReplaceIfFailedKey[]
bool luaW_isterrainmap(lua_State *L, int index)
int intf_replace_if_failed(lua_State *L)
gamemap_base * luaW_toterrainmap(lua_State *L, int index)
constexpr bool is_odd(T num)
std::string register_metatables(lua_State *L)
play_controller * controller
terrain_code read_terrain_code(std::string_view str, const ter_layer filler)
Reads a single terrain from a string.
ter_map read_game_map(std::string_view str, starting_positions &starting_positions, coordinate border_offset)
Reads a gamemap string into a 2D vector.
std::vector< terrain_code > ter_list
ter_list read_list(std::string_view str, const ter_layer filler)
Reads a list of terrains from a string, when reading the.
std::string write_terrain_code(const terrain_code &tcode)
Writes a single terrain code to a string.
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
std::decay_t< T > luaW_table_get_def(lua_State *L, int index, std::string_view k, const T &def)
returns t[k] where k is the table at index index and k is k or def if it is not convertible to the co...
std::unique_ptr< gamemap_base > owned_map
lua_map_ref(int w, int h, const t_translation::terrain_code &ter)
lua_map_ref(string_view data)
lua_map_ref(gamemap_base &ref)
Encapsulates the map of the game.
terrain_code & get(int x, int y)
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
static map_location::direction s