-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
lua_object.hpp
209 lines (164 loc) · 5.23 KB
/
lua_object.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
Copyright (C) 2011 - 2016 by Dmitry Kovalenko <nephro.wes@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Lua object(value) wrapper implementation
*/
#ifndef LUA_OBJECT_HPP_INCLUDED
#define LUA_OBJECT_HPP_INCLUDED
#include "config.hpp"
#include "lua/lua.h"
#include "map/location.hpp"
#include "resources.hpp"
#include "scripting/lua_common.hpp"
#include "terrain/filter.hpp"
#include "variable.hpp"
#include "ai/default/contexts.hpp"
#include "ai/lua/aspect_advancements.hpp"
#include <iterator>
#include <string>
#include <vector>
namespace ai {
class lua_object_base {
public:
lua_object_base();
virtual ~lua_object_base() {}
virtual void store(lua_State* L, int n) = 0;
};
template <typename T>
class lua_object : public lua_object_base
{
public:
lua_object()
: value_()
{
// empty
}
std::shared_ptr<T> get()
{
return value_;
}
void store(lua_State* L, int n)
{
this->value_ = std::shared_ptr<T>(to_type(L, lua_absindex(L, n)));
}
protected:
// A group of functions that deal with the translation of the results to C++
std::shared_ptr<T> to_type(lua_State *, int)
{
return std::shared_ptr<T>();
}
std::shared_ptr<T> value_;
};
template <>
inline std::shared_ptr<double> lua_object<double>::to_type(lua_State *L, int n)
{
return std::shared_ptr<double>(new double(lua_tonumber(L, n)));
}
template <>
inline std::shared_ptr<std::string> lua_object<std::string>::to_type(lua_State *L, int n)
{
return std::shared_ptr<std::string>(new std::string(lua_tostring(L, n)));
}
template <>
inline std::shared_ptr<bool> lua_object<bool>::to_type(lua_State *L, int n)
{
return std::shared_ptr<bool>(new bool(luaW_toboolean(L, n)));
}
template <>
inline std::shared_ptr<int> lua_object<int>::to_type(lua_State *L, int n)
{
return std::shared_ptr<int>(new int(static_cast<int>(lua_tointeger(L, n))));
}
template <>
inline std::shared_ptr< std::vector<std::string> > lua_object< std::vector<std::string> >::to_type(lua_State *L, int n)
{
std::shared_ptr< std::vector<std::string> > v = std::shared_ptr< std::vector<std::string> >(new std::vector<std::string>());
int l = lua_rawlen(L, n);
for (int i = 1; i < l + 1; ++i)
{
lua_pushinteger(L, i);
lua_gettable(L, n);
std::string s = lua_tostring(L, -1);
lua_settop(L, n);
v->push_back(s);
}
return v;
}
template <>
inline std::shared_ptr<config> lua_object<config>::to_type(lua_State *L, int n)
{
std::shared_ptr<config> cfg = std::shared_ptr<config>(new config());
luaW_toconfig(L, n, *cfg);
return cfg;
}
template <>
inline std::shared_ptr<terrain_filter> lua_object<terrain_filter>::to_type(lua_State *L, int n)
{
std::shared_ptr<config> cfg = std::shared_ptr<config>(new config());
std::shared_ptr<vconfig> vcfg = std::shared_ptr<vconfig>(new vconfig(*cfg));
if (!luaW_tovconfig(L, n, *vcfg)) {
cfg->add_child("not");
}
vcfg->make_safe();
std::shared_ptr<terrain_filter> tf(new terrain_filter(*vcfg, resources::filter_con));
return tf;
}
template <>
inline std::shared_ptr<std::vector<target> > lua_object< std::vector<target> >::to_type(lua_State *L, int n)
{
std::shared_ptr<std::vector<target> > targets = std::shared_ptr<std::vector<target> >(new std::vector<target>());
std::back_insert_iterator< std::vector<target> > tg(*targets);
int l = lua_rawlen(L, n);
for (int i = 1; i <= l; ++i)
{
lua_rawgeti(L, n, i); // st n + 1 TABLE @ N table @ n + 1
lua_pushstring(L, "loc"); // st n + 2
lua_rawget(L, -2); // st n + 2
lua_pushstring(L, "x"); // st n + 3
lua_rawget(L, -2); // st n + 3
int x = static_cast<int>(lua_tointeger(L, -1)); // st n + 3
lua_pop(L, 1); // st n + 2
lua_pushstring(L, "y"); // st n + 3
lua_rawget(L, -2); // st n + 3
int y = static_cast<int>(lua_tointeger(L, -1)); // st n + 3
lua_pop(L, 2); // st n + 1
lua_pushstring(L, "type"); // st n + 2
lua_rawget(L, -2); // st n + 2
target::TYPE type = target::TYPE::EXPLICIT;
if(lua_isnumber(L, -1)) {
type = target::TYPE::from_int(static_cast<int>(lua_tointeger(L, -1))); // st n + 2
} else if(lua_isstring(L, -1)) {
type = target::TYPE::string_to_enum(lua_tostring(L, -1)); // st n + 2
}
lua_pop(L, 1); // st n + 1
lua_pushstring(L, "value");
lua_rawget(L, -2);
int value = static_cast<int>(lua_tointeger(L, -1));
map_location ml(x, y, wml_loc());
*tg = target(ml, value, type);
}
lua_settop(L, n);
return targets;
}
template <>
inline std::shared_ptr<unit_advancements_aspect> lua_object<unit_advancements_aspect>::to_type(lua_State *L, int n)
{
std::shared_ptr<unit_advancements_aspect> uaa = std::shared_ptr<unit_advancements_aspect>(new unit_advancements_aspect(L, n));
return uaa;
}
// This one is too complex to define in the header.
struct aspect_attacks_lua_filter;
template <>
std::shared_ptr<aspect_attacks_lua_filter> lua_object<aspect_attacks_lua_filter>::to_type(lua_State *L, int n);
} // end of namespace ai
#endif