33 #define LOG_LUA LOG_STREAM(info, log_scripting_lua)
34 #define ERR_LUA LOG_STREAM(err, log_scripting_lua)
44 using std::string_view;
51 string_view
id = luaL_checkstring(L, 2);
65 string_view
id = luaL_checkstring(L, 2);
75 const t_translation::starting_positions::left_map& left = m.
special_locations().left;
77 t_translation::starting_positions::left_const_iterator it;
78 if (lua_isnoneornil(L, 2)) {
82 it = left.find(luaL_checkstring(L, 2));
83 if (it == left.end()) {
88 if (it == left.end()) {
91 lua_pushstring(L, it->first.c_str());
190 throw "luaW_type_error didn't throw";
203 if(lua_isnumber(L, 1) && lua_isnumber(L, 2)) {
204 int w = lua_tointeger(L, 1);
205 int h = lua_tointeger(L, 2);
209 string_view data_str = luaL_checkstring(L, 1);
229 m->lua_map_ref::~lua_map_ref();
242 bool replace_if_failed =
false;
245 replace_if_failed =
true;
247 t_str = luaL_checkstring(L, -1);
251 t_str = luaL_checkstring(L, 3);
252 if(t_str.front() ==
'^') {
254 }
else if(t_str.back() ==
'^') {
261 if(
auto gm =
dynamic_cast<gamemap*
>(&map)) {
273 }
else map.
set_terrain(loc, ter, mode, replace_if_failed);
291 char const *m = luaL_checkstring(L, 2);
301 if(strcmp(m,
"special_locations") == 0) {
329 char const *m = luaL_checkstring(L, 2);
330 std::string err_msg =
"unknown modifiable property of map: ";
332 return luaL_argerror(L, 2, err_msg.c_str());
357 template<
bool with_border>
368 if(prev_loc.
wml_x() ==
w) {
369 if(prev_loc.
wml_y() ==
h) {
373 x = with_border ? 0 : 1;
374 y = prev_loc.
wml_y() + 1;
377 x = prev_loc.
wml_x() + 1;
378 y = prev_loc.
wml_y();
384 lua_replace(L, lua_upvalueindex(2));
387 lua_pushinteger(L, x);
388 lua_pushinteger(L, y);
397 bool with_border = lua_isboolean(L, 2) ?
luaW_toboolean(L, 2) :
false;
402 lua_pushcclosure(L, impl_terrainmap_iter<true>, 2);
404 lua_pushcclosure(L, impl_terrainmap_iter<false>, 2);
430 std::vector<gamemap::overlay_rule> rules;
431 for (
int i = 1, i_end = lua_rawlen(L,
index);
i <= i_end; ++
i)
434 if(!lua_istable(L, -1)) {
435 luaL_argerror(L,
index,
"rules must be a table of tables");
438 auto& rule = rules.back();
457 if(!terrain.empty()) {
458 rule.terrain_ = terrain[0];
492 bool ignore_special_locations =
false;
493 std::vector<gamemap::overlay_rule> rules;
495 if(lua_istable(L, 4)) {
497 ignore_special_locations =
luaW_table_get_def(L, 4,
"ignore_special_locations",
false);
500 if(!lua_istable(L, -1)) {
501 return luaL_argerror(L, 4,
"rules must be a table");
508 if(lua_isstring(L, 3)) {
509 const std::string t_str = luaL_checkstring(L, 3);
510 std::unique_ptr<gamemap_base> mask;
511 if(
dynamic_cast<gamemap*
>(&map)) {
512 auto mask_ptr =
new gamemap(
"");
513 mask_ptr->read(t_str,
false);
514 mask.reset(mask_ptr);
518 map.
overlay(*mask, loc, rules,
is_odd, ignore_special_locations);
521 map.
overlay(mask, loc, rules,
is_odd, ignore_special_locations);
525 if(
auto gmap =
dynamic_cast<gamemap*
>(&map)) {
527 t.fix_villages(*gmap);
542 if(!lua_isnoneornil(L, 2)) {
543 string_view mode_str = luaL_checkstring(L, 2);
544 if(mode_str ==
"base") {
546 }
else if(mode_str ==
"overlay") {
548 }
else if(mode_str !=
"both") {
549 return luaL_argerror(L, 2,
"must be one of 'base', 'overlay', or 'both'");
553 lua_newuserdatauv(L, 0, 2);
554 lua_pushinteger(L,
int(mode));
564 static const char* mode_strs[] = {
"base",
"overlay",
"both"};
566 string_view t_str = luaL_checkstring(L, -1);
568 int mode = luaL_checkinteger(L, -1);
569 lua_pushfstring(L,
"replace_if_failed('%s', '%s')", t_str.data(), mode_strs[mode]);
576 std::ostringstream cmd_out;
578 cmd_out <<
"Adding terrain map metatable...\n";
582 lua_setfield(L, -2,
"__gc");
584 lua_setfield(L, -2,
"__index");
586 lua_setfield(L, -2,
"__newindex");
588 lua_setfield(L, -2,
"__metatable");
592 lua_setfield(L, -2,
"__tostring");
594 lua_setfield(L, -2,
"__metatable");
596 cmd_out <<
"Adding special locations metatable...\n";
600 lua_setfield(L, -2,
"__index");
602 lua_setfield(L, -2,
"__newindex");
604 lua_setfield(L, -2,
"__pairs");
606 lua_setfield(L, -2,
"__metatable");
608 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