The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lua_object.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2017 by Dmitry Kovalenko <nephro.wes@gmail.com>
3  Part of the Battle for Wesnoth Project http://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 /**
16  * @file
17  * Lua object(value) wrapper implementation
18  */
19 
20 
21 #ifndef LUA_OBJECT_HPP_INCLUDED
22 #define LUA_OBJECT_HPP_INCLUDED
23 
24 #include "config.hpp"
25 #include "lua/lua.h"
26 #include "map/location.hpp"
27 #include "resources.hpp"
28 #include "scripting/lua_common.hpp"
29 #include "terrain/filter.hpp"
30 #include "variable.hpp"
31 #include "ai/default/contexts.hpp"
33 
34 #include <iterator>
35 #include <string>
36 #include <vector>
37 
38 
39 namespace ai {
40 
41 
43 
44 public:
46  virtual ~lua_object_base() {}
47 
48  virtual void store(lua_State* L, int n) = 0;
49 };
50 
51 template <typename T>
53 {
54 
55 public:
56 
58  : value_()
59  {
60  // empty
61  }
62 
63  std::shared_ptr<T> get()
64  {
65  return value_;
66  }
67 
68  void store(lua_State* L, int n)
69  {
70  this->value_ = to_type(L, lua_absindex(L, n));
71  }
72 
73 protected:
74 
75  // A group of functions that deal with the translation of the results to C++
76  std::shared_ptr<T> to_type(lua_State *, int)
77  {
78  return std::shared_ptr<T>();
79  }
80 
81  std::shared_ptr<T> value_;
82 };
83 
84 template <>
85 inline std::shared_ptr<double> lua_object<double>::to_type(lua_State *L, int n)
86 {
87  return std::make_shared<double>(lua_tonumber(L, n));
88 }
89 
90 template <>
91 inline std::shared_ptr<std::string> lua_object<std::string>::to_type(lua_State *L, int n)
92 {
93  return std::make_shared<std::string>(lua_tostring(L, n));
94 }
95 
96 template <>
97 inline std::shared_ptr<bool> lua_object<bool>::to_type(lua_State *L, int n)
98 {
99  return std::make_shared<bool>(luaW_toboolean(L, n));
100 }
101 
102 template <>
103 inline std::shared_ptr<int> lua_object<int>::to_type(lua_State *L, int n)
104 {
105  return std::make_shared<int>(static_cast<int>(lua_tointeger(L, n)));
106 }
107 
108 template <>
109 inline std::shared_ptr< std::vector<std::string> > lua_object< std::vector<std::string> >::to_type(lua_State *L, int n)
110 {
111  std::shared_ptr<std::vector<std::string>> v(new std::vector<std::string>());
112  int l = lua_rawlen(L, n);
113  for (int i = 1; i < l + 1; ++i)
114  {
115  lua_pushinteger(L, i);
116  lua_gettable(L, n);
117  std::string s = lua_tostring(L, -1);
118  lua_settop(L, n);
119  v->push_back(s);
120  }
121 
122  return v;
123 }
124 
125 template <>
126 inline std::shared_ptr<config> lua_object<config>::to_type(lua_State *L, int n)
127 {
128  std::shared_ptr<config> cfg(new config());
129  luaW_toconfig(L, n, *cfg);
130  return cfg;
131 }
132 
133 template <>
134 inline std::shared_ptr<terrain_filter> lua_object<terrain_filter>::to_type(lua_State *L, int n)
135 {
136  std::shared_ptr<config> cfg(new config());
137  std::shared_ptr<vconfig> vcfg(new vconfig(*cfg));
138  if (!luaW_tovconfig(L, n, *vcfg)) {
139  cfg->add_child("not");
140  }
141  vcfg->make_safe();
142  std::shared_ptr<terrain_filter> tf(new terrain_filter(*vcfg, resources::filter_con));
143  return tf;
144 }
145 
146 template <>
147 inline std::shared_ptr<std::vector<target> > lua_object< std::vector<target> >::to_type(lua_State *L, int n)
148 {
149  std::shared_ptr<std::vector<target>> targets(new std::vector<target>());
150  std::back_insert_iterator< std::vector<target> > tg(*targets);
151  int l = lua_rawlen(L, n);
152 
153  for (int i = 1; i <= l; ++i)
154  {
155  lua_rawgeti(L, n, i); // st n + 1 TABLE @ N table @ n + 1
156 
157  lua_pushstring(L, "loc"); // st n + 2
158  lua_rawget(L, -2); // st n + 2
159 
160  lua_pushstring(L, "x"); // st n + 3
161  lua_rawget(L, -2); // st n + 3
162  int x = static_cast<int>(lua_tointeger(L, -1)); // st n + 3
163  lua_pop(L, 1); // st n + 2
164 
165  lua_pushstring(L, "y"); // st n + 3
166  lua_rawget(L, -2); // st n + 3
167  int y = static_cast<int>(lua_tointeger(L, -1)); // st n + 3
168 
169  lua_pop(L, 2); // st n + 1
170 
171  lua_pushstring(L, "type"); // st n + 2
172  lua_rawget(L, -2); // st n + 2
173  target::TYPE type = target::TYPE::EXPLICIT;
174  if(lua_isnumber(L, -1)) {
175  type = target::TYPE::from_int(static_cast<int>(lua_tointeger(L, -1))); // st n + 2
176  } else if(lua_isstring(L, -1)) {
177  type = target::TYPE::string_to_enum(lua_tostring(L, -1)); // st n + 2
178  }
179  lua_pop(L, 1); // st n + 1
180 
181 
182  lua_pushstring(L, "value");
183  lua_rawget(L, -2);
184  int value = static_cast<int>(lua_tointeger(L, -1));
185 
186  map_location ml(x, y, wml_loc());
187 
188  *tg = target(ml, value, type);
189  }
190 
191  lua_settop(L, n);
192  return targets;
193 }
194 
195 template <>
196 inline std::shared_ptr<unit_advancements_aspect> lua_object<unit_advancements_aspect>::to_type(lua_State *L, int n)
197 {
198  return std::make_shared<unit_advancements_aspect>(L, n);
199 }
200 
201 // This one is too complex to define in the header.
203 template <>
204 std::shared_ptr<aspect_attacks_lua_filter> lua_object<aspect_attacks_lua_filter>::to_type(lua_State *L, int n);
205 } // end of namespace ai
206 
207 
208 #endif
TYPE
UNSCALED : image will be drawn "as is" without changing size, even in case of redraw SCALED_TO_ZOOM :...
Definition: image.hpp:189
std::vector< char_t > string
bool luaW_tovconfig(lua_State *L, int index, vconfig &vcfg)
Gets an optional vconfig from either a table or a userdata.
Definition: lua_common.cpp:793
LUA_API void lua_settop(lua_State *L, int idx)
Definition: lapi.cpp:172
LUA_API int lua_rawgeti(lua_State *L, int idx, lua_Integer n)
Definition: lapi.cpp:657
std::shared_ptr< T > value_
Definition: lua_object.hpp:81
LUA_API int lua_gettable(lua_State *L, int idx)
Definition: lapi.cpp:611
#define lua_tointeger(L, i)
Definition: lua.h:342
LUA_API int lua_rawget(lua_State *L, int idx)
Definition: lapi.cpp:646
#define lua_tonumber(L, i)
Definition: lua.h:341
Definitions for the interface to Wesnoth Markup Language (WML).
LUA_API int lua_absindex(lua_State *L, int idx)
Definition: lapi.cpp:160
virtual ~lua_object_base()
Definition: lua_object.hpp:46
#define lua_pop(L, n)
Definition: lua.h:344
std::shared_ptr< T > to_type(lua_State *, int)
Definition: lua_object.hpp:76
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
virtual void store(lua_State *L, int n)=0
LUA_API int lua_isstring(lua_State *L, int idx)
Definition: lapi.cpp:283
bool luaW_toboolean(lua_State *L, int n)
Definition: lua_common.cpp:847
void store(lua_State *L, int n)
Definition: lua_object.hpp:68
filter_context * filter_con
Definition: resources.cpp:23
Encapsulates the map of the game.
Definition: location.hpp:40
bool luaW_toconfig(lua_State *L, int index, config &cfg)
Converts an optional table or vconfig to a config object.
Definition: lua_common.cpp:702
static map_location::DIRECTION s
Default AI contexts.
#define i
#define lua_tostring(L, i)
Definition: lua.h:366
LUA_API int lua_isnumber(lua_State *L, int idx)
Definition: lapi.cpp:276
LUA_API size_t lua_rawlen(lua_State *L, int idx)
Definition: lapi.cpp:392
A variable-expanding proxy for the config class.
Definition: variable.hpp:42
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
static map_location::DIRECTION n
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
Definition: lapi.cpp:466
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.cpp:491