Skip to content

Commit a61e1a4

Browse files
authoredJul 8, 2018
Lua templating reading (part 4): s16, v2s16, v2f (#7512)
1 parent 94cd2bf commit a61e1a4

File tree

5 files changed

+61
-24
lines changed

5 files changed

+61
-24
lines changed
 

Diff for: ‎src/script/common/c_converter.cpp

-15
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,6 @@ v2s16 read_v2s16(lua_State *L, int index)
113113
return p;
114114
}
115115

116-
v2s16 check_v2s16(lua_State *L, int index)
117-
{
118-
v2s16 p;
119-
CHECK_POS_TAB(index);
120-
lua_getfield(L, index, "x");
121-
CHECK_POS_COORD("x");
122-
p.X = lua_tonumber(L, -1);
123-
lua_pop(L, 1);
124-
lua_getfield(L, index, "y");
125-
CHECK_POS_COORD("y");
126-
p.Y = lua_tonumber(L, -1);
127-
lua_pop(L, 1);
128-
return p;
129-
}
130-
131116
void push_v2s16(lua_State *L, v2s16 p)
132117
{
133118
lua_newtable(L);

Diff for: ‎src/script/common/c_converter.h

-2
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ void setboolfield(lua_State *L, int table,
100100
const char *fieldname, bool value);
101101

102102
v3f checkFloatPos (lua_State *L, int index);
103-
v2f check_v2f (lua_State *L, int index);
104-
v2s16 check_v2s16 (lua_State *L, int index);
105103
v3f check_v3f (lua_State *L, int index);
106104
v3s16 check_v3s16 (lua_State *L, int index);
107105

Diff for: ‎src/script/common/helper.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,26 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2020
#include "helper.h"
2121
#include <cmath>
2222
#include <sstream>
23+
#include <irr_v2d.h>
2324
#include "c_types.h"
25+
#include "c_internal.h"
26+
27+
// imported from c_converter.cpp with pure C++ style
28+
static inline void check_lua_type(lua_State *L, int index, const char *name, int type)
29+
{
30+
int t = lua_type(L, index);
31+
if (t != type) {
32+
std::string traceback = script_get_backtrace(L);
33+
throw LuaError(std::string("Invalid ") + (name) + " (expected " +
34+
lua_typename(L, (type)) + " got " + lua_typename(L, t) +
35+
").\n" + traceback);
36+
}
37+
}
38+
39+
// imported from c_converter.cpp
40+
#define CHECK_POS_COORD(name) \
41+
check_lua_type(L, -1, "position coordinate '" name "'", LUA_TNUMBER)
42+
#define CHECK_POS_TAB(index) check_lua_type(L, index, "position", LUA_TTABLE)
2443

2544
bool LuaHelper::isNaN(lua_State *L, int idx)
2645
{
@@ -43,6 +62,11 @@ template <> bool LuaHelper::readParam(lua_State *L, int index, const bool &defau
4362
return lua_toboolean(L, index) != 0;
4463
}
4564

65+
template <> s16 LuaHelper::readParam(lua_State *L, int index)
66+
{
67+
return lua_tonumber(L, index);
68+
}
69+
4670
template <> float LuaHelper::readParam(lua_State *L, int index)
4771
{
4872
if (isNaN(L, index))
@@ -51,6 +75,36 @@ template <> float LuaHelper::readParam(lua_State *L, int index)
5175
return (float)luaL_checknumber(L, index);
5276
}
5377

78+
template <> v2s16 LuaHelper::readParam(lua_State *L, int index)
79+
{
80+
v2s16 p;
81+
CHECK_POS_TAB(index);
82+
lua_getfield(L, index, "x");
83+
CHECK_POS_COORD("x");
84+
p.X = readParam<s16>(L, -1);
85+
lua_pop(L, 1);
86+
lua_getfield(L, index, "y");
87+
CHECK_POS_COORD("y");
88+
p.Y = readParam<s16>(L, -1);
89+
lua_pop(L, 1);
90+
return p;
91+
}
92+
93+
template <> v2f LuaHelper::readParam(lua_State *L, int index)
94+
{
95+
v2f p;
96+
CHECK_POS_TAB(index);
97+
lua_getfield(L, index, "x");
98+
CHECK_POS_COORD("x");
99+
p.X = readParam<float>(L, -1);
100+
lua_pop(L, 1);
101+
lua_getfield(L, index, "y");
102+
CHECK_POS_COORD("y");
103+
p.Y = readParam<float>(L, -1);
104+
lua_pop(L, 1);
105+
return p;
106+
}
107+
54108
template <> std::string LuaHelper::readParam(lua_State *L, int index)
55109
{
56110
std::string result;

Diff for: ‎src/script/lua_api/l_noise.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ int LuaPerlinNoise::l_get_2d(lua_State *L)
4040
{
4141
NO_MAP_LOCK_REQUIRED;
4242
LuaPerlinNoise *o = checkobject(L, 1);
43-
v2f p = check_v2f(L, 2);
43+
v2f p = readParam<v2f>(L, 2);
4444
lua_Number val = NoisePerlin2D(&o->np, p.X, p.Y, 0);
4545
lua_pushnumber(L, val);
4646
return 1;
@@ -166,7 +166,7 @@ int LuaPerlinNoiseMap::l_get_2d_map(lua_State *L)
166166
size_t i = 0;
167167

168168
LuaPerlinNoiseMap *o = checkobject(L, 1);
169-
v2f p = check_v2f(L, 2);
169+
v2f p = readParam<v2f>(L, 2);
170170

171171
Noise *n = o->noise;
172172
n->perlinMap2D(p.X, p.Y);
@@ -189,8 +189,8 @@ int LuaPerlinNoiseMap::l_get_2d_map_flat(lua_State *L)
189189
NO_MAP_LOCK_REQUIRED;
190190

191191
LuaPerlinNoiseMap *o = checkobject(L, 1);
192-
v2f p = check_v2f(L, 2);
193-
bool use_buffer = lua_istable(L, 3);
192+
v2f p = readParam<v2f>(L, 2);
193+
bool use_buffer = lua_istable(L, 3);
194194

195195
Noise *n = o->noise;
196196
n->perlinMap2D(p.X, p.Y);
@@ -275,7 +275,7 @@ int LuaPerlinNoiseMap::l_calc_2d_map(lua_State *L)
275275
NO_MAP_LOCK_REQUIRED;
276276

277277
LuaPerlinNoiseMap *o = checkobject(L, 1);
278-
v2f p = check_v2f(L, 2);
278+
v2f p = readParam<v2f>(L, 2);
279279

280280
Noise *n = o->noise;
281281
n->perlinMap2D(p.X, p.Y);

Diff for: ‎src/script/lua_api/l_object.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ int ObjectRef::l_set_animation(lua_State *L)
458458
// Do it
459459
v2f frames = v2f(1, 1);
460460
if (!lua_isnil(L, 2))
461-
frames = read_v2f(L, 2);
461+
frames = readParam<v2f>(L, 2);
462462
float frame_speed = 15;
463463
if (!lua_isnil(L, 3))
464464
frame_speed = lua_tonumber(L, 3);
@@ -955,7 +955,7 @@ int ObjectRef::l_set_sprite(lua_State *L)
955955
// Do it
956956
v2s16 p(0,0);
957957
if (!lua_isnil(L, 2))
958-
p = read_v2s16(L, 2);
958+
p = readParam<v2s16>(L, 2);
959959
int num_frames = 1;
960960
if (!lua_isnil(L, 3))
961961
num_frames = lua_tonumber(L, 3);

0 commit comments

Comments
 (0)
Please sign in to comment.