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