The Battle for Wesnoth  1.15.2+dev
lua_terrainmap.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2018 the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #pragma once
15 
16 #include <cstddef>
17 #include <string>
18 #include "scripting/lua_common.hpp"
19 #include "map/location.hpp"
20 #include "terrain/translation.hpp"
21 #include "terrain/type_data.hpp"
22 
23 struct lua_State;
24 class lua_unit;
25 struct map_location;
26 
27 // this clas is similar to the orginal gamemap class but they have no 'is' rlation:
28 // mapgen_gamemap, unlike gamemap offers 'raw' access to the data
29 // gamemap, unlike mapgen_gamemap uses terain type data.
31 {
32 public:
36  explicit mapgen_gamemap(utils::string_view data);
37  mapgen_gamemap(int w, int h, terrain_code);
38 
39  std::string to_string() const;
40 
41  /** Effective map width. */
42  int w() const { return total_width() - 2; }
43 
44  /** Effective map height. */
45  int h() const { return total_height() - 2; }
46 
47  /** Real width of the map, including borders. */
48  int total_width() const { return tiles_.w; }
49 
50  /** Real height of the map, including borders */
51  int total_height() const { return tiles_.h; }
52 
53  bool on_map(const map_location& loc) const
54  {
55  return loc.wml_x() >= 0 && loc.wml_x() < total_width() && loc.wml_y() >= 0 && loc.wml_y() < total_height();
56  }
57 
58  bool on_map_noborder(const map_location& loc) const
59  {
60  return loc.wml_x() > 0 && loc.wml_x() < total_width() - 1 && loc.wml_y() > 0 && loc.wml_y() < total_height() - 1;
61  }
62 
64  {
65  return tiles_.get(loc.wml_x(), loc.wml_y());
66  }
67 
68  const terrain_code& operator[](const map_location& loc) const
69  {
70  return tiles_.get(loc.wml_x(), loc.wml_y());
71  }
72 
73  void set_terrain(const map_location& loc, const terrain_code & terrain, const terrain_type_data::merge_mode mode);
75 
78 
79  void set_special_location(const std::string& id, const map_location& loc);
80  map_location special_location(const std::string& id) const;
81 
82  template<typename F>
83  void for_each_loc(const F& f) const
84  {
85  for (int x = 0; x < total_width(); ++x) {
86  for (int y = 0; y < total_height(); ++y) {
87  f({ x, y , wml_loc()});
88  }
89  }
90  }
91  static int intf_mg_terrain_mask(lua_State *L);
92 private:
95 };
96 
97 bool luaW_isslocs(lua_State* L, int index);
98 
100 
102 
104 
105 void luaW_pushslocs(lua_State *L, int index);
106 
107 int impl_slocs_get(lua_State* L);
108 
109 int impl_slocs_set(lua_State* L);
110 
111 bool luaW_isterrainmap(lua_State* L, int index);
112 
114 
116 
118 
120 
122 
123 namespace lua_terrainmap {
124  std::string register_metatables(lua_State *L);
125 }
static int intf_mg_terrain_mask(lua_State *L)
Reaplces part of the map.
void lua_terrainmap_setmetatable(lua_State *L)
int impl_slocs_set(lua_State *L)
boost::bimaps::bimap< boost::bimaps::set_of< std::string >, boost::bimaps::multiset_of< coordinate > > starting_positions
t_translation::starting_positions starting_positions
starting_positions & special_locations()
mapgen_gamemap * luaW_toterrainmap(lua_State *L, int index)
std::string to_string() const
static void simplemerge(terrain_code old, terrain_code &t, const terrain_type_data::merge_mode mode)
bool luaW_isslocs(lua_State *L, int index)
int wml_x() const
Definition: location.hpp:157
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:50
t_translation::ter_map tiles_
int h() const
Effective map height.
mapgen_gamemap & luaW_check_slocs(lua_State *L, int index)
void set_terrain(const map_location &loc, const terrain_code &terrain, const terrain_type_data::merge_mode mode)
void set_special_location(const std::string &id, const map_location &loc)
terrain_code & operator[](const map_location &loc)
mapgen_gamemap * luaW_pushmap(lua_State *L, mapgen_gamemap &&u)
bool luaW_isterrainmap(lua_State *L, int index)
mapgen_gamemap(utils::string_view data)
bool on_map(const map_location &loc) const
map_location special_location(const std::string &id) const
int wml_y() const
Definition: location.hpp:158
starting_positions starting_positions_
void for_each_loc(const F &f) const
const starting_positions & special_locations() const
static const ::config * terrain
The terrain used to create the cache.
Definition: minimap.cpp:130
mapgen_gamemap & luaW_checkterrainmap(lua_State *L, int index)
Encapsulates the map of the game.
Definition: location.hpp:42
Storage for a unit, either owned by the Lua code (ptr != 0), a local variable unit (c_ptr != 0)...
Definition: lua_unit.hpp:79
const terrain_code & operator[](const map_location &loc) const
std::string register_metatables(lua_State *)
void luaW_pushslocs(lua_State *L, int index)
index the index of the map object.
mapgen_gamemap * luaW_toslocs(lua_State *L, int index)
int intf_terainmap_create(lua_State *L)
Create a map.
int w() const
Effective map width.
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:71
terrain_code & get(int x, int y)
Definition: translation.hpp:90
int total_height() const
Real height of the map, including borders.
void lua_slocs_setmetatable(lua_State *L)
#define f
double t
Definition: astarsearch.cpp:64
bool on_map_noborder(const map_location &loc) const
int impl_slocs_get(lua_State *L)
int total_width() const
Real width of the map, including borders.