The Battle for Wesnoth  1.13.4+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 - 2016 by Dmitry Kovalenko <[email protected]>
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_api.hpp"
29 #include "scripting/lua_common.hpp"
30 #include "terrain/filter.hpp"
31 #include "variable.hpp"
32 #include "ai/default/contexts.hpp"
34 
35 #include <boost/shared_ptr.hpp>
36 #include <iterator>
37 #include <string>
38 #include <vector>
39 
40 
41 namespace ai {
42 
43 
45 
46 public:
48  virtual ~lua_object_base() {}
49 
50  virtual void store(lua_State* L, int n) = 0;
51 };
52 
53 template <typename T>
55 {
56 
57 public:
58 
60  : value_()
61  {
62  // empty
63  }
64 
66  {
67  return value_;
68  }
69 
70  void store(lua_State* L, int n)
71  {
73  }
74 
75 protected:
76 
77  // A group of functions that deal with the translation of the results to C++
79  {
80  return boost::shared_ptr<T>();
81  }
82 
84 };
85 
86 template <>
88 {
89  return boost::shared_ptr<double>(new double(lua_tonumber(L, n)));
90 }
91 
92 template <>
94 {
96 }
97 
98 template <>
100 {
101  return boost::shared_ptr<bool>(new bool(luaW_toboolean(L, n)));
102 }
103 
104 template <>
106 {
107  return boost::shared_ptr<int>(new int(lua_tointeger(L, n)));
108 }
109 
110 template <>
112 {
114  int l = lua_rawlen(L, n);
115  for (int i = 1; i < l + 1; ++i)
116  {
117  lua_pushinteger(L, i);
118  lua_gettable(L, n);
119  std::string s = lua_tostring(L, -1);
120  lua_settop(L, n);
121  v->push_back(s);
122  }
123 
124  return v;
125 }
126 
127 template <>
129 {
131  luaW_toconfig(L, n, *cfg);
132  return cfg;
133 }
134 
135 template <>
137 {
140  if (!luaW_tovconfig(L, n, *vcfg)) {
141  cfg->add_child("not");
142  }
143  vcfg->make_safe();
145  return tf;
146 }
147 
148 template <>
150 {
151  boost::shared_ptr<std::vector<target> > targets = boost::shared_ptr<std::vector<target> >(new std::vector<target>());
152  std::back_insert_iterator< std::vector<target> > tg(*targets);
153  int l = lua_rawlen(L, n);
154 
155  for (int i = 1; i <= l; ++i)
156  {
157  lua_rawgeti(L, n, i); // st n + 1 TABLE @ N table @ n + 1
158 
159  lua_pushstring(L, "loc"); // st n + 2
160  lua_rawget(L, -2); // st n + 2
161 
162  lua_pushstring(L, "x"); // st n + 3
163  lua_rawget(L, -2); // st n + 3
164  int x = lua_tointeger(L, -1); // st n + 3
165  lua_pop(L, 1); // st n + 2
166 
167  lua_pushstring(L, "y"); // st n + 3
168  lua_rawget(L, -2); // st n + 3
169  int y = lua_tointeger(L, -1); // st n + 3
170 
171  lua_pop(L, 2); // st n + 1
172 
173  lua_pushstring(L, "type"); // st n + 2
174  lua_rawget(L, -2); // st n + 2
175  target::TYPE type = target::TYPE::EXPLICIT;
176  if(lua_isnumber(L, -1)) {
177  type = target::TYPE::from_int(lua_tointeger(L, -1)); // st n + 2
178  } else if(lua_isstring(L, -1)) {
179  type = target::TYPE::string_to_enum(lua_tostring(L, -1)); // st n + 2
180  }
181  lua_pop(L, 1); // st n + 1
182 
183 
184  lua_pushstring(L, "value");
185  lua_rawget(L, -2);
186  int value = lua_tointeger(L, -1);
187 
188  map_location ml(x - 1, y - 1);
189 
190  *tg = target(ml, value, type);
191  }
192 
193  lua_settop(L, n);
194  return targets;
195 }
196 
197 template <>
199 {
201  return uaa;
202 }
203 
204 // This one is too complex to define in the header.
206 template <>
208 } // end of namespace ai
209 
210 
211 #endif
LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
Definition: lapi.cpp:643
TYPE
UNSCALED : image will be drawn "as is" without changing size, even in case of redraw SCALED_TO_ZOOM :...
Definition: image.hpp:208
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:757
LUA_API void lua_settop(lua_State *L, int idx)
Definition: lapi.cpp:159
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
#define lua_tointeger(L, i)
Definition: lua.h:319
boost::shared_ptr< T > value_
Definition: lua_object.hpp:83
#define lua_tonumber(L, i)
Definition: lua.h:318
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
Definitions for the interface to Wesnoth Markup Language (WML).
LUA_API int lua_absindex(lua_State *L, int idx)
Definition: lapi.cpp:147
virtual ~lua_object_base()
Definition: lua_object.hpp:48
#define lua_pop(L, n)
Definition: lua.h:322
GLdouble l
Definition: glew.h:6966
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:57
virtual void store(lua_State *L, int n)=0
LUA_API int lua_isstring(lua_State *L, int idx)
Definition: lapi.cpp:268
bool luaW_toboolean(lua_State *L, int n)
Definition: lua_common.cpp:811
void store(lua_State *L, int n)
Definition: lua_object.hpp:70
filter_context * filter_con
Definition: resources.cpp:23
const GLdouble * v
Definition: glew.h:1359
GLsizei const GLfloat * value
Definition: glew.h:1817
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
Encapsulates the map of the game.
Definition: location.hpp:38
bool luaW_toconfig(lua_State *L, int index, config &cfg)
Converts an optional table or vconfig to a config object.
Definition: lua_common.cpp:675
Default AI contexts.
size_t i
Definition: function.cpp:1057
#define lua_tostring(L, i)
Definition: lua.h:345
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
LUA_API int lua_isnumber(lua_State *L, int idx)
Definition: lapi.cpp:261
LUA_API void lua_gettable(lua_State *L, int idx)
Definition: lapi.cpp:613
LUA_API size_t lua_rawlen(lua_State *L, int idx)
Definition: lapi.cpp:401
GLclampd n
Definition: glew.h:5903
boost::shared_ptr< T > to_type(lua_State *, int)
Definition: lua_object.hpp:78
A variable-expanding proxy for the config class.
Definition: variable.hpp:36
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLdouble s
Definition: glew.h:1358
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
Definition: lapi.cpp:477
LUA_API void lua_rawget(lua_State *L, int idx)
Definition: lapi.cpp:633
GLsizei const GLcharARB ** string
Definition: glew.h:4503
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.cpp:507
GLenum target
Definition: glew.h:5190