]> git.lizzy.rs Git - minetest.git/blob - src/script/lua_api/l_localplayer.cpp
Sneak: Improve and fix various things
[minetest.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_attached(lua_State *L)
72 {
73         LocalPlayer *player = getobject(L, 1);
74
75         lua_pushboolean(L, player->isAttached);
76         return 1;
77 }
78
79 int LuaLocalPlayer::l_is_touching_ground(lua_State *L)
80 {
81         LocalPlayer *player = getobject(L, 1);
82
83         lua_pushboolean(L, player->touching_ground);
84         return 1;
85 }
86
87 int LuaLocalPlayer::l_is_in_liquid(lua_State *L)
88 {
89         LocalPlayer *player = getobject(L, 1);
90
91         lua_pushboolean(L, player->in_liquid);
92         return 1;
93 }
94
95 int LuaLocalPlayer::l_is_in_liquid_stable(lua_State *L)
96 {
97         LocalPlayer *player = getobject(L, 1);
98
99         lua_pushboolean(L, player->in_liquid_stable);
100         return 1;
101 }
102
103 int LuaLocalPlayer::l_get_liquid_viscosity(lua_State *L)
104 {
105         LocalPlayer *player = getobject(L, 1);
106
107         lua_pushinteger(L, player->liquid_viscosity);
108         return 1;
109 }
110
111 int LuaLocalPlayer::l_is_climbing(lua_State *L)
112 {
113         LocalPlayer *player = getobject(L, 1);
114
115         lua_pushboolean(L, player->is_climbing);
116         return 1;
117 }
118
119 int LuaLocalPlayer::l_swimming_vertical(lua_State *L)
120 {
121         LocalPlayer *player = getobject(L, 1);
122
123         lua_pushboolean(L, player->swimming_vertical);
124         return 1;
125 }
126
127 int LuaLocalPlayer::l_get_physics_override(lua_State *L)
128 {
129         LocalPlayer *player = getobject(L, 1);
130
131         lua_newtable(L);
132         lua_pushnumber(L, player->physics_override_speed);
133         lua_setfield(L, -2, "speed");
134
135         lua_pushnumber(L, player->physics_override_jump);
136         lua_setfield(L, -2, "jump");
137
138         lua_pushnumber(L, player->physics_override_gravity);
139         lua_setfield(L, -2, "gravity");
140
141         lua_pushboolean(L, player->physics_override_sneak);
142         lua_setfield(L, -2, "sneak");
143
144         lua_pushboolean(L, player->physics_override_sneak_glitch);
145         lua_setfield(L, -2, "sneak_glitch");
146
147         return 1;
148 }
149
150 int LuaLocalPlayer::l_get_override_pos(lua_State *L)
151 {
152         LocalPlayer *player = getobject(L, 1);
153
154         push_v3f(L, player->overridePosition);
155         return 1;
156 }
157
158 int LuaLocalPlayer::l_get_last_pos(lua_State *L)
159 {
160         LocalPlayer *player = getobject(L, 1);
161
162         push_v3f(L, player->last_position / BS);
163         return 1;
164 }
165
166 int LuaLocalPlayer::l_get_last_velocity(lua_State *L)
167 {
168         LocalPlayer *player = getobject(L, 1);
169
170         push_v3f(L, player->last_speed);
171         return 1;
172 }
173
174 int LuaLocalPlayer::l_get_last_look_vertical(lua_State *L)
175 {
176         LocalPlayer *player = getobject(L, 1);
177
178         lua_pushnumber(L, -1.0 * player->last_pitch * core::DEGTORAD);
179         return 1;
180 }
181
182 int LuaLocalPlayer::l_get_last_look_horizontal(lua_State *L)
183 {
184         LocalPlayer *player = getobject(L, 1);
185
186         lua_pushnumber(L, (player->last_yaw + 90.) * core::DEGTORAD);
187         return 1;
188 }
189
190 int LuaLocalPlayer::l_get_key_pressed(lua_State *L)
191 {
192         LocalPlayer *player = getobject(L, 1);
193
194         lua_pushinteger(L, player->last_keyPressed);
195         return 1;
196 }
197
198 int LuaLocalPlayer::l_get_breath(lua_State *L)
199 {
200         LocalPlayer *player = getobject(L, 1);
201
202         lua_pushinteger(L, player->getBreath());
203         return 1;
204 }
205
206 int LuaLocalPlayer::l_get_look_dir(lua_State *L)
207 {
208         LocalPlayer *player = getobject(L, 1);
209
210         float pitch = -1.0 * player->getPitch() * core::DEGTORAD;
211         float yaw = (player->getYaw() + 90.) * core::DEGTORAD;
212         v3f v(cos(pitch) * cos(yaw), sin(pitch), cos(pitch) * sin(yaw));
213
214         push_v3f(L, v);
215         return 1;
216 }
217
218 int LuaLocalPlayer::l_get_look_horizontal(lua_State *L)
219 {
220         LocalPlayer *player = getobject(L, 1);
221
222         lua_pushnumber(L, (player->getYaw() + 90.) * core::DEGTORAD);
223         return 1;
224 }
225
226 int LuaLocalPlayer::l_get_look_vertical(lua_State *L)
227 {
228         LocalPlayer *player = getobject(L, 1);
229
230         lua_pushnumber(L, -1.0 * player->getPitch() * core::DEGTORAD);
231         return 1;
232 }
233
234 int LuaLocalPlayer::l_get_pos(lua_State *L)
235 {
236         LocalPlayer *player = getobject(L, 1);
237
238         push_v3f(L, player->getPosition() / BS);
239         return 1;
240 }
241
242 int LuaLocalPlayer::l_get_eye_pos(lua_State *L)
243 {
244         LocalPlayer *player = getobject(L, 1);
245
246         push_v3f(L, player->getEyePosition());
247         return 1;
248 }
249
250 int LuaLocalPlayer::l_get_eye_offset(lua_State *L)
251 {
252         LocalPlayer *player = getobject(L, 1);
253
254         push_v3f(L, player->getEyeOffset());
255         return 1;
256 }
257
258 int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L)
259 {
260         LocalPlayer *player = getobject(L, 1);
261
262         lua_newtable(L);
263         lua_pushnumber(L, player->movement_acceleration_default);
264         lua_setfield(L, -2, "default");
265
266         lua_pushnumber(L, player->movement_acceleration_air);
267         lua_setfield(L, -2, "air");
268
269         lua_pushnumber(L, player->movement_acceleration_fast);
270         lua_setfield(L, -2, "fast");
271
272         return 1;
273 }
274
275 int LuaLocalPlayer::l_get_movement_speed(lua_State *L)
276 {
277         LocalPlayer *player = getobject(L, 1);
278
279         lua_newtable(L);
280         lua_pushnumber(L, player->movement_speed_walk);
281         lua_setfield(L, -2, "walk");
282
283         lua_pushnumber(L, player->movement_speed_crouch);
284         lua_setfield(L, -2, "crouch");
285
286         lua_pushnumber(L, player->movement_speed_fast);
287         lua_setfield(L, -2, "fast");
288
289         lua_pushnumber(L, player->movement_speed_climb);
290         lua_setfield(L, -2, "climb");
291
292         lua_pushnumber(L, player->movement_speed_jump);
293         lua_setfield(L, -2, "jump");
294
295         return 1;
296 }
297
298 int LuaLocalPlayer::l_get_movement(lua_State *L)
299 {
300         LocalPlayer *player = getobject(L, 1);
301
302         lua_newtable(L);
303
304         lua_pushnumber(L, player->movement_liquid_fluidity);
305         lua_setfield(L, -2, "liquid_fluidity");
306
307         lua_pushnumber(L, player->movement_liquid_fluidity_smooth);
308         lua_setfield(L, -2, "liquid_fluidity_smooth");
309
310         lua_pushnumber(L, player->movement_liquid_sink);
311         lua_setfield(L, -2, "liquid_sink");
312
313         lua_pushnumber(L, player->movement_gravity);
314         lua_setfield(L, -2, "gravity");
315
316         return 1;
317 }
318
319 LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg)
320 {
321         luaL_checktype(L, narg, LUA_TUSERDATA);
322
323         void *ud = luaL_checkudata(L, narg, className);
324         if (!ud)
325                 luaL_typerror(L, narg, className);
326
327         return *(LuaLocalPlayer **)ud;
328 }
329
330 LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref)
331 {
332         return ref->m_localplayer;
333 }
334
335 LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg)
336 {
337         LuaLocalPlayer *ref = checkobject(L, narg);
338         assert(ref);
339         LocalPlayer *player = getobject(ref);
340         assert(player);
341         return player;
342 }
343
344 int LuaLocalPlayer::gc_object(lua_State *L)
345 {
346         LuaLocalPlayer *o = *(LuaLocalPlayer **)(lua_touserdata(L, 1));
347         delete o;
348         return 0;
349 }
350
351 void LuaLocalPlayer::Register(lua_State *L)
352 {
353         lua_newtable(L);
354         int methodtable = lua_gettop(L);
355         luaL_newmetatable(L, className);
356         int metatable = lua_gettop(L);
357
358         lua_pushliteral(L, "__metatable");
359         lua_pushvalue(L, methodtable);
360         lua_settable(L, metatable); // hide metatable from lua getmetatable()
361
362         lua_pushliteral(L, "__index");
363         lua_pushvalue(L, methodtable);
364         lua_settable(L, metatable);
365
366         lua_pushliteral(L, "__gc");
367         lua_pushcfunction(L, gc_object);
368         lua_settable(L, metatable);
369
370         lua_pop(L, 1); // Drop metatable
371
372         luaL_openlib(L, 0, methods, 0); // fill methodtable
373         lua_pop(L, 1);                  // Drop methodtable
374 }
375
376 const char LuaLocalPlayer::className[] = "LocalPlayer";
377 const luaL_Reg LuaLocalPlayer::methods[] = {
378                 luamethod(LuaLocalPlayer, get_velocity),
379                 luamethod(LuaLocalPlayer, get_hp),
380                 luamethod(LuaLocalPlayer, get_name),
381                 luamethod(LuaLocalPlayer, is_attached),
382                 luamethod(LuaLocalPlayer, is_touching_ground),
383                 luamethod(LuaLocalPlayer, is_in_liquid),
384                 luamethod(LuaLocalPlayer, is_in_liquid_stable),
385                 luamethod(LuaLocalPlayer, get_liquid_viscosity),
386                 luamethod(LuaLocalPlayer, is_climbing),
387                 luamethod(LuaLocalPlayer, swimming_vertical),
388                 luamethod(LuaLocalPlayer, get_physics_override),
389                 luamethod(LuaLocalPlayer, get_override_pos),
390                 luamethod(LuaLocalPlayer, get_last_pos),
391                 luamethod(LuaLocalPlayer, get_last_velocity),
392                 luamethod(LuaLocalPlayer, get_last_look_horizontal),
393                 luamethod(LuaLocalPlayer, get_last_look_vertical),
394                 luamethod(LuaLocalPlayer, get_key_pressed),
395                 luamethod(LuaLocalPlayer, get_breath),
396                 luamethod(LuaLocalPlayer, get_look_dir),
397                 luamethod(LuaLocalPlayer, get_look_horizontal),
398                 luamethod(LuaLocalPlayer, get_look_vertical),
399                 luamethod(LuaLocalPlayer, get_pos),
400                 luamethod(LuaLocalPlayer, get_eye_pos),
401                 luamethod(LuaLocalPlayer, get_eye_offset),
402                 luamethod(LuaLocalPlayer, get_movement_acceleration),
403                 luamethod(LuaLocalPlayer, get_movement_speed),
404                 luamethod(LuaLocalPlayer, get_movement),
405
406                 {0, 0}
407 };