-
Notifications
You must be signed in to change notification settings - Fork 5
/
lua-recastnavigation.cpp
241 lines (204 loc) · 5.54 KB
/
lua-recastnavigation.cpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#define LUA_LIB
#ifdef __cplusplus
extern "C"
{
#endif
#include <lua.h>
#include <lauxlib.h>
LUAMOD_API int luaopen_recastnavigation(lua_State *L);
#ifdef __cplusplus
}
#endif
#include "recastnavigation.h"
static void *
check_userdata(lua_State *L, int idx)
{
void *ret = lua_touserdata(L, idx);
luaL_argcheck(L, ret != NULL, idx, "Userdata should not be NULL");
return ret;
}
static void
create_meta(lua_State *L, luaL_Reg *l, const char *name, lua_CFunction tostring, lua_CFunction gcfunc)
{
int n = 0;
while (l[n].name)
++n;
lua_newtable(L);
lua_createtable(L, 0, n);
int i;
for (i = 0; i < n; i++)
{
lua_pushcfunction(L, l[i].func);
lua_setfield(L, -2, l[i].name);
}
lua_setfield(L, -2, "__index");
lua_pushstring(L, name);
lua_setfield(L, -2, "__metatable");
if (tostring)
{
lua_pushcfunction(L, tostring);
lua_setfield(L, -2, "__tostring");
}
if (gcfunc)
{
lua_pushcfunction(L, gcfunc);
lua_setfield(L, -2, "__gc");
}
}
struct s_navigation
{
int64_t scene;
RecastNavigationHandle *handle;
};
static int
lnew(lua_State *L)
{
int64_t scene = luaL_checknumber(L, 1);
size_t l;
const char *respath = luaL_checklstring(L, 2, &l);
struct s_navigation *nav = (struct s_navigation *)lua_newuserdata(L, sizeof(struct s_navigation));
nav->scene = scene;
nav->handle = NULL;
nav->handle = RecastNavigationHandle::Create(respath);
if (!nav->handle)
{
lua_pushnil(L);
return 1;
}
lua_pushvalue(L, lua_upvalueindex(1));
lua_setmetatable(L, -2);
return 1;
}
static int
lrelease(lua_State *L)
{
struct s_navigation *nav = (struct s_navigation *)check_userdata(L, 1);
printf("recastnavigation release [%lld]\n", nav->scene);
if (nav->handle)
{
delete nav->handle;
nav->handle = NULL;
}
else
{
printf("recastnavigation release handle is null");
}
return 0;
}
static int
lFindStraightPath(lua_State *L)
{
struct s_navigation *nav = (struct s_navigation *)check_userdata(L, 1);
float start_x = luaL_checknumber(L, 2);
float start_y = luaL_checknumber(L, 3);
float start_z = luaL_checknumber(L, 4);
float end_x = luaL_checknumber(L, 5);
float end_y = luaL_checknumber(L, 6);
float end_z = luaL_checknumber(L, 7);
NFVector3 start(start_x, start_y, start_z);
NFVector3 end(end_x, end_y, end_z);
std::vector<NFVector3> paths;
int pos = nav->handle->FindStraightPath(start, end, paths);
if (pos <= 0)
{
lua_pushboolean(L, false);
return 1;
}
lua_pushboolean(L, true);
lua_newtable(L);
for (size_t i = 0; i < paths.size(); i++)
{
lua_createtable(L, 0, 3);
lua_pushinteger(L, paths[i].X());
lua_rawseti(L, -2, 1);
lua_pushinteger(L, paths[i].Y());
lua_rawseti(L, -2, 2);
lua_pushinteger(L, paths[i].Z());
lua_rawseti(L, -2, 3);
lua_rawseti(L, -2, i + 1);
}
return 2;
}
static int
lFindRandomPointAroundCircle(lua_State *L)
{
struct s_navigation *nav = (struct s_navigation *)check_userdata(L, 1);
float center_x = luaL_checknumber(L, 2);
float center_y = luaL_checknumber(L, 3);
float center_z = luaL_checknumber(L, 4);
int max_points = luaL_checknumber(L, 5);
float maxRadius = luaL_checknumber(L, 6);
NFVector3 center(center_x, center_y, center_z);
std::vector<NFVector3> paths;
int size = nav->handle->FindRandomPointAroundCircle(center, paths, max_points, maxRadius);
if (size <= 0)
{
lua_pushboolean(L, false);
return 1;
}
lua_pushboolean(L, true);
lua_newtable(L);
for (size_t i = 0; i < paths.size(); i++)
{
lua_newtable(L);
lua_createtable(L, 0, 3);
lua_pushinteger(L, paths[i].X());
lua_rawseti(L, -2, 1);
lua_pushinteger(L, paths[i].Y());
lua_rawseti(L, -2, 2);
lua_pushinteger(L, paths[i].Z());
lua_rawseti(L, -2, 3);
lua_rawseti(L, -2, i + 1);
}
return 2;
}
static int
lRaycast(lua_State *L)
{
struct s_navigation *nav = (struct s_navigation *)check_userdata(L, 1);
float start_x = luaL_checknumber(L, 2);
float start_y = luaL_checknumber(L, 3);
float start_z = luaL_checknumber(L, 4);
float end_x = luaL_checknumber(L, 5);
float end_y = luaL_checknumber(L, 6);
float end_z = luaL_checknumber(L, 7);
NFVector3 start(start_x, start_y, start_z);
NFVector3 end(end_x, end_y, end_z);
std::vector<NFVector3> hitPointVec;
int res = nav->handle->Raycast(start, end, hitPointVec);
lua_pushinteger(L, res);
lua_newtable(L);
for (size_t i = 0; i < hitPointVec.size(); i++)
{
lua_createtable(L, 0, 3);
lua_pushinteger(L, hitPointVec[i].X());
lua_rawseti(L, -2, 1);
lua_pushinteger(L, hitPointVec[i].Y());
lua_rawseti(L, -2, 2);
lua_pushinteger(L, hitPointVec[i].Z());
lua_rawseti(L, -2, 3);
lua_rawseti(L, -2, i + 1);
}
return 2;
}
static void
lnavmesh(lua_State *L)
{
luaL_Reg l[] = {
{"FindStraightPath", lFindStraightPath},
{"FindRandomPointAroundCircle", lFindRandomPointAroundCircle},
{"Raycast", lRaycast},
{NULL, NULL},
};
create_meta(L, l, "navmesh", NULL, lrelease);
lua_pushcclosure(L, lnew, 1);
}
LUAMOD_API int
luaopen_recastnavigation(lua_State *L)
{
luaL_checkversion(L);
lua_newtable(L);
lnavmesh(L);
lua_setfield(L, -2, "navmesh");
return 1;
}