The Battle for Wesnoth  1.17.17+dev
lua_terrainmap.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2018 - 2023
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include <cstddef>
18 #include <string>
19 #include "scripting/lua_common.hpp"
20 #include "map/location.hpp"
21 #include "map/map.hpp"
22 #include "terrain/translation.hpp"
23 #include "terrain/type_data.hpp"
24 
25 struct lua_State;
26 class lua_unit;
27 struct map_location;
28 
29 // Unlike the original gamemap, this offers 'raw' access to the data.
30 // The original gamemap uses terrain type data.
31 class mapgen_gamemap : public gamemap_base {
32 public:
33  explicit mapgen_gamemap(std::string_view data);
34  mapgen_gamemap(int w, int h, terrain_code);
35  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;
36 
37  template<typename F>
38  void for_each_loc(const F& f) const
39  {
40  for (int x = 0; x < total_width(); ++x) {
41  for (int y = 0; y < total_height(); ++y) {
42  f({ x, y , wml_loc()});
43  }
44  }
45  }
46 };
47 
48 int intf_terrain_mask(lua_State *L);
49 
50 bool luaW_isterrainmap(lua_State* L, int index);
51 
52 gamemap_base* luaW_toterrainmap(lua_State *L, int index);
53 
54 gamemap_base& luaW_checkterrainmap(lua_State *L, int index);
55 
56 void lua_terrainmap_setmetatable(lua_State *L);
57 
58 int intf_terrainmap_create(lua_State *L);
59 int intf_terrainmap_get(lua_State *L);
60 
61 int intf_replace_if_failed(lua_State* L);
62 int intf_terrainmap_iter(lua_State* L);
63 int intf_on_board(lua_State* L);
64 int intf_on_border(lua_State* L);
65 
66 namespace lua_terrainmap {
67  std::string register_metatables(lua_State *L);
68 }
int w() const
Effective map width.
Definition: map.hpp:50
int h() const
Effective map height.
Definition: map.hpp:53
int total_width() const
Real width of the map, including borders.
Definition: map.hpp:59
int total_height() const
Real height of the map, including borders.
Definition: map.hpp:62
Storage for a unit, either owned by the Lua code (ptr != 0), a local variable unit (c_ptr !...
Definition: lua_unit.hpp:81
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.
void for_each_loc(const F &f) const
mapgen_gamemap(std::string_view data)
int intf_terrainmap_create(lua_State *L)
Create a map.
int intf_terrain_mask(lua_State *L)
Replaces part of the map.
int intf_on_border(lua_State *L)
void lua_terrainmap_setmetatable(lua_State *L)
int intf_on_board(lua_State *L)
int intf_terrainmap_iter(lua_State *L)
int intf_terrainmap_get(lua_State *L)
gamemap_base & luaW_checkterrainmap(lua_State *L, int index)
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)
std::string register_metatables(lua_State *L)
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.
Definition: unicode.cpp:72
std::string_view data
Definition: picture.cpp:199
Encapsulates the map of the game.
Definition: location.hpp:38
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:49
#define f