]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_localplayer.cpp
Script API: Check that SAOs are still usable before attempting to use them
[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 #include "client/localplayer.h"
24 #include "hud.h"
25 #include "common/c_content.h"
26
27 LuaLocalPlayer::LuaLocalPlayer(LocalPlayer *m) : m_localplayer(m)
28 {
29 }
30
31 void LuaLocalPlayer::create(lua_State *L, LocalPlayer *m)
32 {
33         lua_getglobal(L, "core");
34         luaL_checktype(L, -1, LUA_TTABLE);
35         int objectstable = lua_gettop(L);
36         lua_getfield(L, -1, "localplayer");
37
38         // Duplication check
39         if (lua_type(L, -1) == LUA_TUSERDATA) {
40                 lua_pop(L, 1);
41                 return;
42         }
43
44         LuaLocalPlayer *o = new LuaLocalPlayer(m);
45         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
46         luaL_getmetatable(L, className);
47         lua_setmetatable(L, -2);
48
49         lua_pushvalue(L, lua_gettop(L));
50         lua_setfield(L, objectstable, "localplayer");
51 }
52
53 int LuaLocalPlayer::l_get_velocity(lua_State *L)
54 {
55         LocalPlayer *player = getobject(L, 1);
56
57         push_v3f(L, player->getSpeed() / BS);
58         return 1;
59 }
60
61 int LuaLocalPlayer::l_get_hp(lua_State *L)
62 {
63         LocalPlayer *player = getobject(L, 1);
64
65         lua_pushinteger(L, player->hp);
66         return 1;
67 }
68
69 int LuaLocalPlayer::l_get_name(lua_State *L)
70 {
71         LocalPlayer *player = getobject(L, 1);
72
73         lua_pushstring(L, player->getName());
74         return 1;
75 }
76
77 int LuaLocalPlayer::l_is_attached(lua_State *L)
78 {
79         LocalPlayer *player = getobject(L, 1);
80
81         lua_pushboolean(L, player->getParent() != nullptr);
82         return 1;
83 }
84
85 int LuaLocalPlayer::l_is_touching_ground(lua_State *L)
86 {
87         LocalPlayer *player = getobject(L, 1);
88
89         lua_pushboolean(L, player->touching_ground);
90         return 1;
91 }
92
93 int LuaLocalPlayer::l_is_in_liquid(lua_State *L)
94 {
95         LocalPlayer *player = getobject(L, 1);
96
97         lua_pushboolean(L, player->in_liquid);
98         return 1;
99 }
100
101 int LuaLocalPlayer::l_is_in_liquid_stable(lua_State *L)
102 {
103         LocalPlayer *player = getobject(L, 1);
104
105         lua_pushboolean(L, player->in_liquid_stable);
106         return 1;
107 }
108
109 int LuaLocalPlayer::l_get_liquid_viscosity(lua_State *L)
110 {
111         LocalPlayer *player = getobject(L, 1);
112
113         lua_pushinteger(L, player->liquid_viscosity);
114         return 1;
115 }
116
117 int LuaLocalPlayer::l_is_climbing(lua_State *L)
118 {
119         LocalPlayer *player = getobject(L, 1);
120
121         lua_pushboolean(L, player->is_climbing);
122         return 1;
123 }
124
125 int LuaLocalPlayer::l_swimming_vertical(lua_State *L)
126 {
127         LocalPlayer *player = getobject(L, 1);
128
129         lua_pushboolean(L, player->swimming_vertical);
130         return 1;
131 }
132
133 int LuaLocalPlayer::l_get_physics_override(lua_State *L)
134 {
135         LocalPlayer *player = getobject(L, 1);
136
137         lua_newtable(L);
138         lua_pushnumber(L, player->physics_override_speed);
139         lua_setfield(L, -2, "speed");
140
141         lua_pushnumber(L, player->physics_override_jump);
142         lua_setfield(L, -2, "jump");
143
144         lua_pushnumber(L, player->physics_override_gravity);
145         lua_setfield(L, -2, "gravity");
146
147         lua_pushboolean(L, player->physics_override_sneak);
148         lua_setfield(L, -2, "sneak");
149
150         lua_pushboolean(L, player->physics_override_sneak_glitch);
151         lua_setfield(L, -2, "sneak_glitch");
152
153         return 1;
154 }
155
156 int LuaLocalPlayer::l_get_override_pos(lua_State *L)
157 {
158         LocalPlayer *player = getobject(L, 1);
159
160         push_v3f(L, player->getPosition());
161         return 1;
162 }
163
164 int LuaLocalPlayer::l_get_last_pos(lua_State *L)
165 {
166         LocalPlayer *player = getobject(L, 1);
167
168         push_v3f(L, player->last_position / BS);
169         return 1;
170 }
171
172 int LuaLocalPlayer::l_get_last_velocity(lua_State *L)
173 {
174         LocalPlayer *player = getobject(L, 1);
175
176         push_v3f(L, player->last_speed);
177         return 1;
178 }
179
180 int LuaLocalPlayer::l_get_last_look_vertical(lua_State *L)
181 {
182         LocalPlayer *player = getobject(L, 1);
183
184         lua_pushnumber(L, -1.0 * player->last_pitch * core::DEGTORAD);
185         return 1;
186 }
187
188 int LuaLocalPlayer::l_get_last_look_horizontal(lua_State *L)
189 {
190         LocalPlayer *player = getobject(L, 1);
191
192         lua_pushnumber(L, (player->last_yaw + 90.) * core::DEGTORAD);
193         return 1;
194 }
195
196 int LuaLocalPlayer::l_get_key_pressed(lua_State *L)
197 {
198         LocalPlayer *player = getobject(L, 1);
199
200         lua_pushinteger(L, player->last_keyPressed);
201         return 1;
202 }
203
204 int LuaLocalPlayer::l_get_breath(lua_State *L)
205 {
206         LocalPlayer *player = getobject(L, 1);
207
208         lua_pushinteger(L, player->getBreath());
209         return 1;
210 }
211
212 int LuaLocalPlayer::l_get_pos(lua_State *L)
213 {
214         LocalPlayer *player = getobject(L, 1);
215
216         push_v3f(L, player->getPosition() / BS);
217         return 1;
218 }
219
220 int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L)
221 {
222         LocalPlayer *player = getobject(L, 1);
223
224         lua_newtable(L);
225         lua_pushnumber(L, player->movement_acceleration_default);
226         lua_setfield(L, -2, "default");
227
228         lua_pushnumber(L, player->movement_acceleration_air);
229         lua_setfield(L, -2, "air");
230
231         lua_pushnumber(L, player->movement_acceleration_fast);
232         lua_setfield(L, -2, "fast");
233
234         return 1;
235 }
236
237 int LuaLocalPlayer::l_get_movement_speed(lua_State *L)
238 {
239         LocalPlayer *player = getobject(L, 1);
240
241         lua_newtable(L);
242         lua_pushnumber(L, player->movement_speed_walk);
243         lua_setfield(L, -2, "walk");
244
245         lua_pushnumber(L, player->movement_speed_crouch);
246         lua_setfield(L, -2, "crouch");
247
248         lua_pushnumber(L, player->movement_speed_fast);
249         lua_setfield(L, -2, "fast");
250
251         lua_pushnumber(L, player->movement_speed_climb);
252         lua_setfield(L, -2, "climb");
253
254         lua_pushnumber(L, player->movement_speed_jump);
255         lua_setfield(L, -2, "jump");
256
257         return 1;
258 }
259
260 int LuaLocalPlayer::l_get_movement(lua_State *L)
261 {
262         LocalPlayer *player = getobject(L, 1);
263
264         lua_newtable(L);
265
266         lua_pushnumber(L, player->movement_liquid_fluidity);
267         lua_setfield(L, -2, "liquid_fluidity");
268
269         lua_pushnumber(L, player->movement_liquid_fluidity_smooth);
270         lua_setfield(L, -2, "liquid_fluidity_smooth");
271
272         lua_pushnumber(L, player->movement_liquid_sink);
273         lua_setfield(L, -2, "liquid_sink");
274
275         lua_pushnumber(L, player->movement_gravity);
276         lua_setfield(L, -2, "gravity");
277
278         return 1;
279 }
280
281
282 // hud_add(self, form)
283 int LuaLocalPlayer::l_hud_add(lua_State *L)
284 {
285         LocalPlayer *player = getobject(L, 1);
286
287         HudElement *elem = new HudElement;
288         read_hud_element(L, elem);
289
290         u32 id = player->addHud(elem);
291         if (id == U32_MAX) {
292                 delete elem;
293                 return 0;
294         }
295         lua_pushnumber(L, id);
296         return 1;
297 }
298
299 // hud_remove(self, id)
300 int LuaLocalPlayer::l_hud_remove(lua_State *L)
301 {
302         LocalPlayer *player = getobject(L, 1);
303         u32 id = luaL_checkinteger(L, 2);
304         HudElement *element = player->removeHud(id);
305         if (!element)
306                 lua_pushboolean(L, false);
307         else
308                 lua_pushboolean(L, true);
309         delete element;
310         return 1;
311 }
312
313 // hud_change(self, id, stat, data)
314 int LuaLocalPlayer::l_hud_change(lua_State *L)
315 {
316         LocalPlayer *player = getobject(L, 1);
317
318         u32 id = luaL_checkinteger(L, 2);
319
320         HudElement *element = player->getHud(id);
321         if (!element)
322                 return 0;
323
324         void *unused;
325         read_hud_change(L, element, &unused);
326
327         lua_pushboolean(L, true);
328         return 1;
329 }
330
331 // hud_get(self, id)
332 int LuaLocalPlayer::l_hud_get(lua_State *L)
333 {
334         LocalPlayer *player = getobject(L, 1);
335
336         u32 id = luaL_checkinteger(L, -1);
337
338         HudElement *e = player->getHud(id);
339         if (!e) {
340                 lua_pushnil(L);
341                 return 1;
342         }
343
344         push_hud_element(L, e);
345         return 1;
346 }
347
348 LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg)
349 {
350         luaL_checktype(L, narg, LUA_TUSERDATA);
351
352         void *ud = luaL_checkudata(L, narg, className);
353         if (!ud)
354                 luaL_typerror(L, narg, className);
355
356         return *(LuaLocalPlayer **)ud;
357 }
358
359 LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref)
360 {
361         return ref->m_localplayer;
362 }
363
364 LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg)
365 {
366         LuaLocalPlayer *ref = checkobject(L, narg);
367         assert(ref);
368         LocalPlayer *player = getobject(ref);
369         assert(player);
370         return player;
371 }
372
373 int LuaLocalPlayer::gc_object(lua_State *L)
374 {
375         LuaLocalPlayer *o = *(LuaLocalPlayer **)(lua_touserdata(L, 1));
376         delete o;
377         return 0;
378 }
379
380 void LuaLocalPlayer::Register(lua_State *L)
381 {
382         lua_newtable(L);
383         int methodtable = lua_gettop(L);
384         luaL_newmetatable(L, className);
385         int metatable = lua_gettop(L);
386
387         lua_pushliteral(L, "__metatable");
388         lua_pushvalue(L, methodtable);
389         lua_settable(L, metatable); // hide metatable from lua getmetatable()
390
391         lua_pushliteral(L, "__index");
392         lua_pushvalue(L, methodtable);
393         lua_settable(L, metatable);
394
395         lua_pushliteral(L, "__gc");
396         lua_pushcfunction(L, gc_object);
397         lua_settable(L, metatable);
398
399         lua_pop(L, 1); // Drop metatable
400
401         luaL_openlib(L, 0, methods, 0); // fill methodtable
402         lua_pop(L, 1);                  // Drop methodtable
403 }
404
405 const char LuaLocalPlayer::className[] = "LocalPlayer";
406 const luaL_Reg LuaLocalPlayer::methods[] = {
407                 luamethod(LuaLocalPlayer, get_velocity),
408                 luamethod(LuaLocalPlayer, get_hp),
409                 luamethod(LuaLocalPlayer, get_name),
410                 luamethod(LuaLocalPlayer, is_attached),
411                 luamethod(LuaLocalPlayer, is_touching_ground),
412                 luamethod(LuaLocalPlayer, is_in_liquid),
413                 luamethod(LuaLocalPlayer, is_in_liquid_stable),
414                 luamethod(LuaLocalPlayer, get_liquid_viscosity),
415                 luamethod(LuaLocalPlayer, is_climbing),
416                 luamethod(LuaLocalPlayer, swimming_vertical),
417                 luamethod(LuaLocalPlayer, get_physics_override),
418                 luamethod(LuaLocalPlayer, get_override_pos),
419                 luamethod(LuaLocalPlayer, get_last_pos),
420                 luamethod(LuaLocalPlayer, get_last_velocity),
421                 luamethod(LuaLocalPlayer, get_last_look_horizontal),
422                 luamethod(LuaLocalPlayer, get_last_look_vertical),
423                 luamethod(LuaLocalPlayer, get_key_pressed),
424                 luamethod(LuaLocalPlayer, get_breath),
425                 luamethod(LuaLocalPlayer, get_pos),
426                 luamethod(LuaLocalPlayer, get_movement_acceleration),
427                 luamethod(LuaLocalPlayer, get_movement_speed),
428                 luamethod(LuaLocalPlayer, get_movement),
429                 luamethod(LuaLocalPlayer, hud_add),
430                 luamethod(LuaLocalPlayer, hud_remove),
431                 luamethod(LuaLocalPlayer, hud_change),
432                 luamethod(LuaLocalPlayer, hud_get),
433
434                 {0, 0}
435 };