]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_localplayer.cpp
[CSM] Add camera API (#5609)
[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_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_pos(lua_State *L)
207 {
208         LocalPlayer *player = getobject(L, 1);
209
210         push_v3f(L, player->getPosition() / BS);
211         return 1;
212 }
213
214 int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L)
215 {
216         LocalPlayer *player = getobject(L, 1);
217
218         lua_newtable(L);
219         lua_pushnumber(L, player->movement_acceleration_default);
220         lua_setfield(L, -2, "default");
221
222         lua_pushnumber(L, player->movement_acceleration_air);
223         lua_setfield(L, -2, "air");
224
225         lua_pushnumber(L, player->movement_acceleration_fast);
226         lua_setfield(L, -2, "fast");
227
228         return 1;
229 }
230
231 int LuaLocalPlayer::l_get_movement_speed(lua_State *L)
232 {
233         LocalPlayer *player = getobject(L, 1);
234
235         lua_newtable(L);
236         lua_pushnumber(L, player->movement_speed_walk);
237         lua_setfield(L, -2, "walk");
238
239         lua_pushnumber(L, player->movement_speed_crouch);
240         lua_setfield(L, -2, "crouch");
241
242         lua_pushnumber(L, player->movement_speed_fast);
243         lua_setfield(L, -2, "fast");
244
245         lua_pushnumber(L, player->movement_speed_climb);
246         lua_setfield(L, -2, "climb");
247
248         lua_pushnumber(L, player->movement_speed_jump);
249         lua_setfield(L, -2, "jump");
250
251         return 1;
252 }
253
254 int LuaLocalPlayer::l_get_movement(lua_State *L)
255 {
256         LocalPlayer *player = getobject(L, 1);
257
258         lua_newtable(L);
259
260         lua_pushnumber(L, player->movement_liquid_fluidity);
261         lua_setfield(L, -2, "liquid_fluidity");
262
263         lua_pushnumber(L, player->movement_liquid_fluidity_smooth);
264         lua_setfield(L, -2, "liquid_fluidity_smooth");
265
266         lua_pushnumber(L, player->movement_liquid_sink);
267         lua_setfield(L, -2, "liquid_sink");
268
269         lua_pushnumber(L, player->movement_gravity);
270         lua_setfield(L, -2, "gravity");
271
272         return 1;
273 }
274
275 LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg)
276 {
277         luaL_checktype(L, narg, LUA_TUSERDATA);
278
279         void *ud = luaL_checkudata(L, narg, className);
280         if (!ud)
281                 luaL_typerror(L, narg, className);
282
283         return *(LuaLocalPlayer **)ud;
284 }
285
286 LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref)
287 {
288         return ref->m_localplayer;
289 }
290
291 LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg)
292 {
293         LuaLocalPlayer *ref = checkobject(L, narg);
294         assert(ref);
295         LocalPlayer *player = getobject(ref);
296         assert(player);
297         return player;
298 }
299
300 int LuaLocalPlayer::gc_object(lua_State *L)
301 {
302         LuaLocalPlayer *o = *(LuaLocalPlayer **)(lua_touserdata(L, 1));
303         delete o;
304         return 0;
305 }
306
307 void LuaLocalPlayer::Register(lua_State *L)
308 {
309         lua_newtable(L);
310         int methodtable = lua_gettop(L);
311         luaL_newmetatable(L, className);
312         int metatable = lua_gettop(L);
313
314         lua_pushliteral(L, "__metatable");
315         lua_pushvalue(L, methodtable);
316         lua_settable(L, metatable); // hide metatable from lua getmetatable()
317
318         lua_pushliteral(L, "__index");
319         lua_pushvalue(L, methodtable);
320         lua_settable(L, metatable);
321
322         lua_pushliteral(L, "__gc");
323         lua_pushcfunction(L, gc_object);
324         lua_settable(L, metatable);
325
326         lua_pop(L, 1); // Drop metatable
327
328         luaL_openlib(L, 0, methods, 0); // fill methodtable
329         lua_pop(L, 1);                  // Drop methodtable
330 }
331
332 const char LuaLocalPlayer::className[] = "LocalPlayer";
333 const luaL_Reg LuaLocalPlayer::methods[] = {
334                 luamethod(LuaLocalPlayer, get_velocity),
335                 luamethod(LuaLocalPlayer, get_hp),
336                 luamethod(LuaLocalPlayer, get_name),
337                 luamethod(LuaLocalPlayer, is_attached),
338                 luamethod(LuaLocalPlayer, is_touching_ground),
339                 luamethod(LuaLocalPlayer, is_in_liquid),
340                 luamethod(LuaLocalPlayer, is_in_liquid_stable),
341                 luamethod(LuaLocalPlayer, get_liquid_viscosity),
342                 luamethod(LuaLocalPlayer, is_climbing),
343                 luamethod(LuaLocalPlayer, swimming_vertical),
344                 luamethod(LuaLocalPlayer, get_physics_override),
345                 luamethod(LuaLocalPlayer, get_override_pos),
346                 luamethod(LuaLocalPlayer, get_last_pos),
347                 luamethod(LuaLocalPlayer, get_last_velocity),
348                 luamethod(LuaLocalPlayer, get_last_look_horizontal),
349                 luamethod(LuaLocalPlayer, get_last_look_vertical),
350                 luamethod(LuaLocalPlayer, get_key_pressed),
351                 luamethod(LuaLocalPlayer, get_breath),
352                 luamethod(LuaLocalPlayer, get_pos),
353                 luamethod(LuaLocalPlayer, get_movement_acceleration),
354                 luamethod(LuaLocalPlayer, get_movement_speed),
355                 luamethod(LuaLocalPlayer, get_movement),
356
357                 {0, 0}
358 };