]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_localplayer.cpp
8aac5355cba089df0e50895044fbcee92a6e3edd
[dragonfireclient.git] / src / script / lua_api / l_localplayer.cpp
1 /*
2 Minetest
3 Copyright (C) 2017 Dumbeldor, Vincent Glize <vincent.glize@live.fr>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "l_localplayer.h"
21 #include "l_internal.h"
22 #include "script/common/c_converter.h"
23
24 LuaLocalPlayer::LuaLocalPlayer(LocalPlayer *m)
25 {
26         m_localplayer = m;
27 }
28
29 void LuaLocalPlayer::create(lua_State *L, LocalPlayer *m)
30 {
31         LuaLocalPlayer *o = new LuaLocalPlayer(m);
32         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
33         luaL_getmetatable(L, className);
34         lua_setmetatable(L, -2);
35
36         // Keep localplayer object stack id
37         int localplayer_object = lua_gettop(L);
38
39         lua_getglobal(L, "core");
40         luaL_checktype(L, -1, LUA_TTABLE);
41         int coretable = lua_gettop(L);
42
43         lua_pushvalue(L, localplayer_object);
44         lua_setfield(L, coretable, "localplayer");
45 }
46
47 int LuaLocalPlayer::l_get_velocity(lua_State *L)
48 {
49         LocalPlayer *player = getobject(L, 1);
50
51         push_v3f(L, player->getSpeed() / BS);
52         return 1;
53 }
54
55 int LuaLocalPlayer::l_get_hp(lua_State *L)
56 {
57         LocalPlayer *player = getobject(L, 1);
58
59         lua_pushinteger(L, player->hp);
60         return 1;
61 }
62
63 int LuaLocalPlayer::l_get_name(lua_State *L)
64 {
65         LocalPlayer *player = getobject(L, 1);
66
67         lua_pushstring(L, player->getName());
68         return 1;
69 }
70
71 int LuaLocalPlayer::l_is_teleported(lua_State *L)
72 {
73         LocalPlayer *player = getobject(L, 1);
74
75         lua_pushboolean(L, player->got_teleported);
76         return 1;
77 }
78
79 int LuaLocalPlayer::l_is_attached(lua_State *L)
80 {
81         LocalPlayer *player = getobject(L, 1);
82
83         lua_pushboolean(L, player->isAttached);
84         return 1;
85 }
86
87 int LuaLocalPlayer::l_is_touching_ground(lua_State *L)
88 {
89         LocalPlayer *player = getobject(L, 1);
90
91         lua_pushboolean(L, player->touching_ground);
92         return 1;
93 }
94
95 int LuaLocalPlayer::l_is_in_liquid(lua_State *L)
96 {
97         LocalPlayer *player = getobject(L, 1);
98
99         lua_pushboolean(L, player->in_liquid);
100         return 1;
101 }
102
103 int LuaLocalPlayer::l_is_in_liquid_stable(lua_State *L)
104 {
105         LocalPlayer *player = getobject(L, 1);
106
107         lua_pushboolean(L, player->in_liquid_stable);
108         return 1;
109 }
110
111 int LuaLocalPlayer::l_get_liquid_viscosity(lua_State *L)
112 {
113         LocalPlayer *player = getobject(L, 1);
114
115         lua_pushinteger(L, player->liquid_viscosity);
116         return 1;
117 }
118
119 int LuaLocalPlayer::l_is_climbing(lua_State *L)
120 {
121         LocalPlayer *player = getobject(L, 1);
122
123         lua_pushboolean(L, player->is_climbing);
124         return 1;
125 }
126
127 int LuaLocalPlayer::l_swimming_vertical(lua_State *L)
128 {
129         LocalPlayer *player = getobject(L, 1);
130
131         lua_pushboolean(L, player->swimming_vertical);
132         return 1;
133 }
134
135 int LuaLocalPlayer::l_get_physics_override(lua_State *L)
136 {
137         LocalPlayer *player = getobject(L, 1);
138
139         lua_newtable(L);
140         lua_pushnumber(L, player->physics_override_speed);
141         lua_setfield(L, -2, "speed");
142
143         lua_pushnumber(L, player->physics_override_jump);
144         lua_setfield(L, -2, "jump");
145
146         lua_pushnumber(L, player->physics_override_gravity);
147         lua_setfield(L, -2, "gravity");
148
149         lua_pushboolean(L, player->physics_override_sneak);
150         lua_setfield(L, -2, "sneak");
151
152         lua_pushboolean(L, player->physics_override_sneak_glitch);
153         lua_setfield(L, -2, "sneak_glitch");
154
155         return 1;
156 }
157
158 int LuaLocalPlayer::l_get_override_pos(lua_State *L)
159 {
160         LocalPlayer *player = getobject(L, 1);
161
162         push_v3f(L, player->overridePosition);
163         return 1;
164 }
165
166 int LuaLocalPlayer::l_get_last_pos(lua_State *L)
167 {
168         LocalPlayer *player = getobject(L, 1);
169
170         push_v3f(L, player->last_position / BS);
171         return 1;
172 }
173
174 int LuaLocalPlayer::l_get_last_velocity(lua_State *L)
175 {
176         LocalPlayer *player = getobject(L, 1);
177
178         push_v3f(L, player->last_speed);
179         return 1;
180 }
181
182 int LuaLocalPlayer::l_get_last_look_vertical(lua_State *L)
183 {
184         LocalPlayer *player = getobject(L, 1);
185
186         lua_pushnumber(L, -1.0 * player->last_pitch * core::DEGTORAD);
187         return 1;
188 }
189
190 int LuaLocalPlayer::l_get_last_look_horizontal(lua_State *L)
191 {
192         LocalPlayer *player = getobject(L, 1);
193
194         lua_pushnumber(L, (player->last_yaw + 90.) * core::DEGTORAD);
195         return 1;
196 }
197
198 int LuaLocalPlayer::l_get_key_pressed(lua_State *L)
199 {
200         LocalPlayer *player = getobject(L, 1);
201
202         lua_pushinteger(L, player->last_keyPressed);
203         return 1;
204 }
205
206 int LuaLocalPlayer::l_get_breath(lua_State *L)
207 {
208         LocalPlayer *player = getobject(L, 1);
209
210         lua_pushinteger(L, player->getBreath());
211         return 1;
212 }
213
214 int LuaLocalPlayer::l_get_look_dir(lua_State *L)
215 {
216         LocalPlayer *player = getobject(L, 1);
217
218         float pitch = -1.0 * player->getPitch() * core::DEGTORAD;
219         float yaw = (player->getYaw() + 90.) * core::DEGTORAD;
220         v3f v(cos(pitch) * cos(yaw), sin(pitch), cos(pitch) * sin(yaw));
221
222         push_v3f(L, v);
223         return 1;
224 }
225
226 int LuaLocalPlayer::l_get_look_horizontal(lua_State *L)
227 {
228         LocalPlayer *player = getobject(L, 1);
229
230         lua_pushnumber(L, (player->getYaw() + 90.) * core::DEGTORAD);
231         return 1;
232 }
233
234 int LuaLocalPlayer::l_get_look_vertical(lua_State *L)
235 {
236         LocalPlayer *player = getobject(L, 1);
237
238         lua_pushnumber(L, -1.0 * player->getPitch() * core::DEGTORAD);
239         return 1;
240 }
241
242 int LuaLocalPlayer::l_get_pos(lua_State *L)
243 {
244         LocalPlayer *player = getobject(L, 1);
245
246         push_v3f(L, player->getPosition() / BS);
247         return 1;
248 }
249
250 int LuaLocalPlayer::l_get_eye_pos(lua_State *L)
251 {
252         LocalPlayer *player = getobject(L, 1);
253
254         push_v3f(L, player->getEyePosition());
255         return 1;
256 }
257
258 int LuaLocalPlayer::l_get_eye_offset(lua_State *L)
259 {
260         LocalPlayer *player = getobject(L, 1);
261
262         push_v3f(L, player->getEyeOffset());
263         return 1;
264 }
265
266 int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L)
267 {
268         LocalPlayer *player = getobject(L, 1);
269
270         lua_newtable(L);
271         lua_pushnumber(L, player->movement_acceleration_default);
272         lua_setfield(L, -2, "default");
273
274         lua_pushnumber(L, player->movement_acceleration_air);
275         lua_setfield(L, -2, "air");
276
277         lua_pushnumber(L, player->movement_acceleration_fast);
278         lua_setfield(L, -2, "fast");
279
280         return 1;
281 }
282
283 int LuaLocalPlayer::l_get_movement_speed(lua_State *L)
284 {
285         LocalPlayer *player = getobject(L, 1);
286
287         lua_newtable(L);
288         lua_pushnumber(L, player->movement_speed_walk);
289         lua_setfield(L, -2, "walk");
290
291         lua_pushnumber(L, player->movement_speed_crouch);
292         lua_setfield(L, -2, "crouch");
293
294         lua_pushnumber(L, player->movement_speed_fast);
295         lua_setfield(L, -2, "fast");
296
297         lua_pushnumber(L, player->movement_speed_climb);
298         lua_setfield(L, -2, "climb");
299
300         lua_pushnumber(L, player->movement_speed_jump);
301         lua_setfield(L, -2, "jump");
302
303         return 1;
304 }
305
306 int LuaLocalPlayer::l_get_movement(lua_State *L)
307 {
308         LocalPlayer *player = getobject(L, 1);
309
310         lua_newtable(L);
311
312         lua_pushnumber(L, player->movement_liquid_fluidity);
313         lua_setfield(L, -2, "liquid_fluidity");
314
315         lua_pushnumber(L, player->movement_liquid_fluidity_smooth);
316         lua_setfield(L, -2, "liquid_fluidity_smooth");
317
318         lua_pushnumber(L, player->movement_liquid_sink);
319         lua_setfield(L, -2, "liquid_sink");
320
321         lua_pushnumber(L, player->movement_gravity);
322         lua_setfield(L, -2, "gravity");
323
324         return 1;
325 }
326
327 LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg)
328 {
329         luaL_checktype(L, narg, LUA_TUSERDATA);
330
331         void *ud = luaL_checkudata(L, narg, className);
332         if (!ud)
333                 luaL_typerror(L, narg, className);
334
335         return *(LuaLocalPlayer **)ud;
336 }
337
338 LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref)
339 {
340         return ref->m_localplayer;
341 }
342
343 LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg)
344 {
345         LuaLocalPlayer *ref = checkobject(L, narg);
346         assert(ref);
347         LocalPlayer *player = getobject(ref);
348         assert(player);
349         return player;
350 }
351
352 int LuaLocalPlayer::gc_object(lua_State *L)
353 {
354         LuaLocalPlayer *o = *(LuaLocalPlayer **)(lua_touserdata(L, 1));
355         delete o;
356         return 0;
357 }
358
359 void LuaLocalPlayer::Register(lua_State *L)
360 {
361         lua_newtable(L);
362         int methodtable = lua_gettop(L);
363         luaL_newmetatable(L, className);
364         int metatable = lua_gettop(L);
365
366         lua_pushliteral(L, "__metatable");
367         lua_pushvalue(L, methodtable);
368         lua_settable(L, metatable); // hide metatable from lua getmetatable()
369
370         lua_pushliteral(L, "__index");
371         lua_pushvalue(L, methodtable);
372         lua_settable(L, metatable);
373
374         lua_pushliteral(L, "__gc");
375         lua_pushcfunction(L, gc_object);
376         lua_settable(L, metatable);
377
378         lua_pop(L, 1); // Drop metatable
379
380         luaL_openlib(L, 0, methods, 0); // fill methodtable
381         lua_pop(L, 1);                  // Drop methodtable
382 }
383
384 const char LuaLocalPlayer::className[] = "LocalPlayer";
385 const luaL_reg LuaLocalPlayer::methods[] = {
386                 luamethod(LuaLocalPlayer, get_velocity),
387                 luamethod(LuaLocalPlayer, get_hp),
388                 luamethod(LuaLocalPlayer, get_name),
389                 luamethod(LuaLocalPlayer, is_teleported),
390                 luamethod(LuaLocalPlayer, is_attached),
391                 luamethod(LuaLocalPlayer, is_touching_ground),
392                 luamethod(LuaLocalPlayer, is_in_liquid),
393                 luamethod(LuaLocalPlayer, is_in_liquid_stable),
394                 luamethod(LuaLocalPlayer, get_liquid_viscosity),
395                 luamethod(LuaLocalPlayer, is_climbing),
396                 luamethod(LuaLocalPlayer, swimming_vertical),
397                 luamethod(LuaLocalPlayer, get_physics_override),
398                 luamethod(LuaLocalPlayer, get_override_pos),
399                 luamethod(LuaLocalPlayer, get_last_pos),
400                 luamethod(LuaLocalPlayer, get_last_velocity),
401                 luamethod(LuaLocalPlayer, get_last_look_horizontal),
402                 luamethod(LuaLocalPlayer, get_last_look_vertical),
403                 luamethod(LuaLocalPlayer, get_key_pressed),
404                 luamethod(LuaLocalPlayer, get_breath),
405                 luamethod(LuaLocalPlayer, get_look_dir),
406                 luamethod(LuaLocalPlayer, get_look_horizontal),
407                 luamethod(LuaLocalPlayer, get_look_vertical),
408                 luamethod(LuaLocalPlayer, get_pos),
409                 luamethod(LuaLocalPlayer, get_eye_pos),
410                 luamethod(LuaLocalPlayer, get_eye_offset),
411                 luamethod(LuaLocalPlayer, get_movement_acceleration),
412                 luamethod(LuaLocalPlayer, get_movement_speed),
413                 luamethod(LuaLocalPlayer, get_movement),
414
415                 {0, 0}
416 };