]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_localplayer.cpp
Automatic item and node colorization (#5640)
[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) : m_localplayer(m)
25 {
26 }
27
28 void LuaLocalPlayer::create(lua_State *L, LocalPlayer *m)
29 {
30         LuaLocalPlayer *o = new LuaLocalPlayer(m);
31         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
32         luaL_getmetatable(L, className);
33         lua_setmetatable(L, -2);
34
35         // Keep localplayer object stack id
36         int localplayer_object = lua_gettop(L);
37
38         lua_getglobal(L, "core");
39         luaL_checktype(L, -1, LUA_TTABLE);
40         int coretable = lua_gettop(L);
41
42         lua_pushvalue(L, localplayer_object);
43         lua_setfield(L, coretable, "localplayer");
44 }
45
46 int LuaLocalPlayer::l_get_velocity(lua_State *L)
47 {
48         LocalPlayer *player = getobject(L, 1);
49
50         push_v3f(L, player->getSpeed() / BS);
51         return 1;
52 }
53
54 int LuaLocalPlayer::l_get_hp(lua_State *L)
55 {
56         LocalPlayer *player = getobject(L, 1);
57
58         lua_pushinteger(L, player->hp);
59         return 1;
60 }
61
62 int LuaLocalPlayer::l_get_name(lua_State *L)
63 {
64         LocalPlayer *player = getobject(L, 1);
65
66         lua_pushstring(L, player->getName());
67         return 1;
68 }
69
70 int LuaLocalPlayer::l_is_attached(lua_State *L)
71 {
72         LocalPlayer *player = getobject(L, 1);
73
74         lua_pushboolean(L, player->isAttached);
75         return 1;
76 }
77
78 int LuaLocalPlayer::l_is_touching_ground(lua_State *L)
79 {
80         LocalPlayer *player = getobject(L, 1);
81
82         lua_pushboolean(L, player->touching_ground);
83         return 1;
84 }
85
86 int LuaLocalPlayer::l_is_in_liquid(lua_State *L)
87 {
88         LocalPlayer *player = getobject(L, 1);
89
90         lua_pushboolean(L, player->in_liquid);
91         return 1;
92 }
93
94 int LuaLocalPlayer::l_is_in_liquid_stable(lua_State *L)
95 {
96         LocalPlayer *player = getobject(L, 1);
97
98         lua_pushboolean(L, player->in_liquid_stable);
99         return 1;
100 }
101
102 int LuaLocalPlayer::l_get_liquid_viscosity(lua_State *L)
103 {
104         LocalPlayer *player = getobject(L, 1);
105
106         lua_pushinteger(L, player->liquid_viscosity);
107         return 1;
108 }
109
110 int LuaLocalPlayer::l_is_climbing(lua_State *L)
111 {
112         LocalPlayer *player = getobject(L, 1);
113
114         lua_pushboolean(L, player->is_climbing);
115         return 1;
116 }
117
118 int LuaLocalPlayer::l_swimming_vertical(lua_State *L)
119 {
120         LocalPlayer *player = getobject(L, 1);
121
122         lua_pushboolean(L, player->swimming_vertical);
123         return 1;
124 }
125
126 int LuaLocalPlayer::l_get_physics_override(lua_State *L)
127 {
128         LocalPlayer *player = getobject(L, 1);
129
130         lua_newtable(L);
131         lua_pushnumber(L, player->physics_override_speed);
132         lua_setfield(L, -2, "speed");
133
134         lua_pushnumber(L, player->physics_override_jump);
135         lua_setfield(L, -2, "jump");
136
137         lua_pushnumber(L, player->physics_override_gravity);
138         lua_setfield(L, -2, "gravity");
139
140         lua_pushboolean(L, player->physics_override_sneak);
141         lua_setfield(L, -2, "sneak");
142
143         lua_pushboolean(L, player->physics_override_sneak_glitch);
144         lua_setfield(L, -2, "sneak_glitch");
145
146         return 1;
147 }
148
149 int LuaLocalPlayer::l_get_override_pos(lua_State *L)
150 {
151         LocalPlayer *player = getobject(L, 1);
152
153         push_v3f(L, player->overridePosition);
154         return 1;
155 }
156
157 int LuaLocalPlayer::l_get_last_pos(lua_State *L)
158 {
159         LocalPlayer *player = getobject(L, 1);
160
161         push_v3f(L, player->last_position / BS);
162         return 1;
163 }
164
165 int LuaLocalPlayer::l_get_last_velocity(lua_State *L)
166 {
167         LocalPlayer *player = getobject(L, 1);
168
169         push_v3f(L, player->last_speed);
170         return 1;
171 }
172
173 int LuaLocalPlayer::l_get_last_look_vertical(lua_State *L)
174 {
175         LocalPlayer *player = getobject(L, 1);
176
177         lua_pushnumber(L, -1.0 * player->last_pitch * core::DEGTORAD);
178         return 1;
179 }
180
181 int LuaLocalPlayer::l_get_last_look_horizontal(lua_State *L)
182 {
183         LocalPlayer *player = getobject(L, 1);
184
185         lua_pushnumber(L, (player->last_yaw + 90.) * core::DEGTORAD);
186         return 1;
187 }
188
189 int LuaLocalPlayer::l_get_key_pressed(lua_State *L)
190 {
191         LocalPlayer *player = getobject(L, 1);
192
193         lua_pushinteger(L, player->last_keyPressed);
194         return 1;
195 }
196
197 int LuaLocalPlayer::l_get_breath(lua_State *L)
198 {
199         LocalPlayer *player = getobject(L, 1);
200
201         lua_pushinteger(L, player->getBreath());
202         return 1;
203 }
204
205 int LuaLocalPlayer::l_get_pos(lua_State *L)
206 {
207         LocalPlayer *player = getobject(L, 1);
208
209         push_v3f(L, player->getPosition() / BS);
210         return 1;
211 }
212
213 int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L)
214 {
215         LocalPlayer *player = getobject(L, 1);
216
217         lua_newtable(L);
218         lua_pushnumber(L, player->movement_acceleration_default);
219         lua_setfield(L, -2, "default");
220
221         lua_pushnumber(L, player->movement_acceleration_air);
222         lua_setfield(L, -2, "air");
223
224         lua_pushnumber(L, player->movement_acceleration_fast);
225         lua_setfield(L, -2, "fast");
226
227         return 1;
228 }
229
230 int LuaLocalPlayer::l_get_movement_speed(lua_State *L)
231 {
232         LocalPlayer *player = getobject(L, 1);
233
234         lua_newtable(L);
235         lua_pushnumber(L, player->movement_speed_walk);
236         lua_setfield(L, -2, "walk");
237
238         lua_pushnumber(L, player->movement_speed_crouch);
239         lua_setfield(L, -2, "crouch");
240
241         lua_pushnumber(L, player->movement_speed_fast);
242         lua_setfield(L, -2, "fast");
243
244         lua_pushnumber(L, player->movement_speed_climb);
245         lua_setfield(L, -2, "climb");
246
247         lua_pushnumber(L, player->movement_speed_jump);
248         lua_setfield(L, -2, "jump");
249
250         return 1;
251 }
252
253 int LuaLocalPlayer::l_get_movement(lua_State *L)
254 {
255         LocalPlayer *player = getobject(L, 1);
256
257         lua_newtable(L);
258
259         lua_pushnumber(L, player->movement_liquid_fluidity);
260         lua_setfield(L, -2, "liquid_fluidity");
261
262         lua_pushnumber(L, player->movement_liquid_fluidity_smooth);
263         lua_setfield(L, -2, "liquid_fluidity_smooth");
264
265         lua_pushnumber(L, player->movement_liquid_sink);
266         lua_setfield(L, -2, "liquid_sink");
267
268         lua_pushnumber(L, player->movement_gravity);
269         lua_setfield(L, -2, "gravity");
270
271         return 1;
272 }
273
274 LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg)
275 {
276         luaL_checktype(L, narg, LUA_TUSERDATA);
277
278         void *ud = luaL_checkudata(L, narg, className);
279         if (!ud)
280                 luaL_typerror(L, narg, className);
281
282         return *(LuaLocalPlayer **)ud;
283 }
284
285 LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref)
286 {
287         return ref->m_localplayer;
288 }
289
290 LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg)
291 {
292         LuaLocalPlayer *ref = checkobject(L, narg);
293         assert(ref);
294         LocalPlayer *player = getobject(ref);
295         assert(player);
296         return player;
297 }
298
299 int LuaLocalPlayer::gc_object(lua_State *L)
300 {
301         LuaLocalPlayer *o = *(LuaLocalPlayer **)(lua_touserdata(L, 1));
302         delete o;
303         return 0;
304 }
305
306 void LuaLocalPlayer::Register(lua_State *L)
307 {
308         lua_newtable(L);
309         int methodtable = lua_gettop(L);
310         luaL_newmetatable(L, className);
311         int metatable = lua_gettop(L);
312
313         lua_pushliteral(L, "__metatable");
314         lua_pushvalue(L, methodtable);
315         lua_settable(L, metatable); // hide metatable from lua getmetatable()
316
317         lua_pushliteral(L, "__index");
318         lua_pushvalue(L, methodtable);
319         lua_settable(L, metatable);
320
321         lua_pushliteral(L, "__gc");
322         lua_pushcfunction(L, gc_object);
323         lua_settable(L, metatable);
324
325         lua_pop(L, 1); // Drop metatable
326
327         luaL_openlib(L, 0, methods, 0); // fill methodtable
328         lua_pop(L, 1);                  // Drop methodtable
329 }
330
331 const char LuaLocalPlayer::className[] = "LocalPlayer";
332 const luaL_Reg LuaLocalPlayer::methods[] = {
333                 luamethod(LuaLocalPlayer, get_velocity),
334                 luamethod(LuaLocalPlayer, get_hp),
335                 luamethod(LuaLocalPlayer, get_name),
336                 luamethod(LuaLocalPlayer, is_attached),
337                 luamethod(LuaLocalPlayer, is_touching_ground),
338                 luamethod(LuaLocalPlayer, is_in_liquid),
339                 luamethod(LuaLocalPlayer, is_in_liquid_stable),
340                 luamethod(LuaLocalPlayer, get_liquid_viscosity),
341                 luamethod(LuaLocalPlayer, is_climbing),
342                 luamethod(LuaLocalPlayer, swimming_vertical),
343                 luamethod(LuaLocalPlayer, get_physics_override),
344                 luamethod(LuaLocalPlayer, get_override_pos),
345                 luamethod(LuaLocalPlayer, get_last_pos),
346                 luamethod(LuaLocalPlayer, get_last_velocity),
347                 luamethod(LuaLocalPlayer, get_last_look_horizontal),
348                 luamethod(LuaLocalPlayer, get_last_look_vertical),
349                 luamethod(LuaLocalPlayer, get_key_pressed),
350                 luamethod(LuaLocalPlayer, get_breath),
351                 luamethod(LuaLocalPlayer, get_pos),
352                 luamethod(LuaLocalPlayer, get_movement_acceleration),
353                 luamethod(LuaLocalPlayer, get_movement_speed),
354                 luamethod(LuaLocalPlayer, get_movement),
355
356                 {0, 0}
357 };