Check for out-of-bounds breath when setting breath_max (#8493)
[oweals/minetest.git] / src / script / lua_api / l_object.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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 "lua_api/l_object.h"
21 #include <cmath>
22 #include "lua_api/l_internal.h"
23 #include "lua_api/l_inventory.h"
24 #include "lua_api/l_item.h"
25 #include "lua_api/l_playermeta.h"
26 #include "common/c_converter.h"
27 #include "common/c_content.h"
28 #include "log.h"
29 #include "tool.h"
30 #include "serverobject.h"
31 #include "content_sao.h"
32 #include "remoteplayer.h"
33 #include "server.h"
34 #include "hud.h"
35 #include "scripting_server.h"
36
37 /*
38         ObjectRef
39 */
40
41
42 ObjectRef* ObjectRef::checkobject(lua_State *L, int narg)
43 {
44         luaL_checktype(L, narg, LUA_TUSERDATA);
45         void *ud = luaL_checkudata(L, narg, className);
46         if (!ud) luaL_typerror(L, narg, className);
47         return *(ObjectRef**)ud;  // unbox pointer
48 }
49
50 ServerActiveObject* ObjectRef::getobject(ObjectRef *ref)
51 {
52         ServerActiveObject *co = ref->m_object;
53         return co;
54 }
55
56 LuaEntitySAO* ObjectRef::getluaobject(ObjectRef *ref)
57 {
58         ServerActiveObject *obj = getobject(ref);
59         if (obj == NULL)
60                 return NULL;
61         if (obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY)
62                 return NULL;
63         return (LuaEntitySAO*)obj;
64 }
65
66 PlayerSAO* ObjectRef::getplayersao(ObjectRef *ref)
67 {
68         ServerActiveObject *obj = getobject(ref);
69         if (obj == NULL)
70                 return NULL;
71         if (obj->getType() != ACTIVEOBJECT_TYPE_PLAYER)
72                 return NULL;
73         return (PlayerSAO*)obj;
74 }
75
76 RemotePlayer *ObjectRef::getplayer(ObjectRef *ref)
77 {
78         PlayerSAO *playersao = getplayersao(ref);
79         if (playersao == NULL)
80                 return NULL;
81         return playersao->getPlayer();
82 }
83
84 // Exported functions
85
86 // garbage collector
87 int ObjectRef::gc_object(lua_State *L) {
88         ObjectRef *o = *(ObjectRef **)(lua_touserdata(L, 1));
89         //infostream<<"ObjectRef::gc_object: o="<<o<<std::endl;
90         delete o;
91         return 0;
92 }
93
94 // remove(self)
95 int ObjectRef::l_remove(lua_State *L)
96 {
97         GET_ENV_PTR;
98
99         ObjectRef *ref = checkobject(L, 1);
100         ServerActiveObject *co = getobject(ref);
101         if (co == NULL)
102                 return 0;
103         if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
104                 return 0;
105
106         co->clearChildAttachments();
107         co->clearParentAttachment();
108
109         verbosestream << "ObjectRef::l_remove(): id=" << co->getId() << std::endl;
110         co->m_pending_removal = true;
111         return 0;
112 }
113
114 // get_pos(self)
115 // returns: {x=num, y=num, z=num}
116 int ObjectRef::l_get_pos(lua_State *L)
117 {
118         NO_MAP_LOCK_REQUIRED;
119         ObjectRef *ref = checkobject(L, 1);
120         ServerActiveObject *co = getobject(ref);
121         if (co == NULL) return 0;
122         v3f pos = co->getBasePosition() / BS;
123         lua_newtable(L);
124         lua_pushnumber(L, pos.X);
125         lua_setfield(L, -2, "x");
126         lua_pushnumber(L, pos.Y);
127         lua_setfield(L, -2, "y");
128         lua_pushnumber(L, pos.Z);
129         lua_setfield(L, -2, "z");
130         return 1;
131 }
132
133 // set_pos(self, pos)
134 int ObjectRef::l_set_pos(lua_State *L)
135 {
136         NO_MAP_LOCK_REQUIRED;
137         ObjectRef *ref = checkobject(L, 1);
138         //LuaEntitySAO *co = getluaobject(ref);
139         ServerActiveObject *co = getobject(ref);
140         if (co == NULL) return 0;
141         // pos
142         v3f pos = checkFloatPos(L, 2);
143         // Do it
144         co->setPos(pos);
145         return 0;
146 }
147
148 // move_to(self, pos, continuous=false)
149 int ObjectRef::l_move_to(lua_State *L)
150 {
151         NO_MAP_LOCK_REQUIRED;
152         ObjectRef *ref = checkobject(L, 1);
153         //LuaEntitySAO *co = getluaobject(ref);
154         ServerActiveObject *co = getobject(ref);
155         if (co == NULL) return 0;
156         // pos
157         v3f pos = checkFloatPos(L, 2);
158         // continuous
159         bool continuous = readParam<bool>(L, 3);
160         // Do it
161         co->moveTo(pos, continuous);
162         return 0;
163 }
164
165 // punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
166 int ObjectRef::l_punch(lua_State *L)
167 {
168         NO_MAP_LOCK_REQUIRED;
169         ObjectRef *ref = checkobject(L, 1);
170         ObjectRef *puncher_ref = checkobject(L, 2);
171         ServerActiveObject *co = getobject(ref);
172         ServerActiveObject *puncher = getobject(puncher_ref);
173         if (!co || !puncher)
174                 return 0;
175         v3f dir;
176         if (lua_type(L, 5) != LUA_TTABLE)
177                 dir = co->getBasePosition() - puncher->getBasePosition();
178         else
179                 dir = read_v3f(L, 5);
180         float time_from_last_punch = 1000000;
181         if (lua_isnumber(L, 3))
182                 time_from_last_punch = lua_tonumber(L, 3);
183         ToolCapabilities toolcap = read_tool_capabilities(L, 4);
184         dir.normalize();
185
186         u16 src_original_hp = co->getHP();
187         u16 dst_origin_hp = puncher->getHP();
188
189         // Do it
190         co->punch(dir, &toolcap, puncher, time_from_last_punch);
191
192         // If the punched is a player, and its HP changed
193         if (src_original_hp != co->getHP() &&
194                         co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
195                 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co,
196                                 PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, puncher));
197         }
198
199         // If the puncher is a player, and its HP changed
200         if (dst_origin_hp != puncher->getHP() &&
201                         puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
202                 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)puncher,
203                                 PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, co));
204         }
205         return 0;
206 }
207
208 // right_click(self, clicker); clicker = an another ObjectRef
209 int ObjectRef::l_right_click(lua_State *L)
210 {
211         NO_MAP_LOCK_REQUIRED;
212         ObjectRef *ref = checkobject(L, 1);
213         ObjectRef *ref2 = checkobject(L, 2);
214         ServerActiveObject *co = getobject(ref);
215         ServerActiveObject *co2 = getobject(ref2);
216         if (co == NULL) return 0;
217         if (co2 == NULL) return 0;
218         // Do it
219         co->rightClick(co2);
220         return 0;
221 }
222
223 // set_hp(self, hp)
224 // hp = number of hitpoints (2 * number of hearts)
225 // returns: nil
226 int ObjectRef::l_set_hp(lua_State *L)
227 {
228         NO_MAP_LOCK_REQUIRED;
229
230         // Get Object
231         ObjectRef *ref = checkobject(L, 1);
232         luaL_checknumber(L, 2);
233         ServerActiveObject *co = getobject(ref);
234         if (co == NULL)
235                 return 0;
236
237         // Get HP
238         int hp = lua_tonumber(L, 2);
239
240         // Get Reason
241         PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
242         reason.from_mod = true;
243         if (lua_istable(L, 3)) {
244                 lua_pushvalue(L, 3);
245
246                 lua_getfield(L, -1, "type");
247                 if (lua_isstring(L, -1) &&
248                                 !reason.setTypeFromString(readParam<std::string>(L, -1))) {
249                         errorstream << "Bad type given!" << std::endl;
250                 }
251                 lua_pop(L, 1);
252
253                 reason.lua_reference = luaL_ref(L, LUA_REGISTRYINDEX);
254         }
255
256         // Do it
257         co->setHP(hp, reason);
258         if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
259                 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, reason);
260
261         if (reason.hasLuaReference())
262                 luaL_unref(L, LUA_REGISTRYINDEX, reason.lua_reference);
263
264         // Return
265         return 0;
266 }
267
268 // get_hp(self)
269 // returns: number of hitpoints (2 * number of hearts)
270 // 0 if not applicable to this type of object
271 int ObjectRef::l_get_hp(lua_State *L)
272 {
273         NO_MAP_LOCK_REQUIRED;
274         ObjectRef *ref = checkobject(L, 1);
275         ServerActiveObject *co = getobject(ref);
276         if (co == NULL) {
277                 // Default hp is 1
278                 lua_pushnumber(L, 1);
279                 return 1;
280         }
281         int hp = co->getHP();
282         /*infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
283                         <<" hp="<<hp<<std::endl;*/
284         // Return
285         lua_pushnumber(L, hp);
286         return 1;
287 }
288
289 // get_inventory(self)
290 int ObjectRef::l_get_inventory(lua_State *L)
291 {
292         NO_MAP_LOCK_REQUIRED;
293         ObjectRef *ref = checkobject(L, 1);
294         ServerActiveObject *co = getobject(ref);
295         if (co == NULL) return 0;
296         // Do it
297         InventoryLocation loc = co->getInventoryLocation();
298         if (getServer(L)->getInventory(loc) != NULL)
299                 InvRef::create(L, loc);
300         else
301                 lua_pushnil(L); // An object may have no inventory (nil)
302         return 1;
303 }
304
305 // get_wield_list(self)
306 int ObjectRef::l_get_wield_list(lua_State *L)
307 {
308         NO_MAP_LOCK_REQUIRED;
309         ObjectRef *ref = checkobject(L, 1);
310         ServerActiveObject *co = getobject(ref);
311         if (co == NULL) return 0;
312         // Do it
313         lua_pushstring(L, co->getWieldList().c_str());
314         return 1;
315 }
316
317 // get_wield_index(self)
318 int ObjectRef::l_get_wield_index(lua_State *L)
319 {
320         NO_MAP_LOCK_REQUIRED;
321         ObjectRef *ref = checkobject(L, 1);
322         ServerActiveObject *co = getobject(ref);
323         if (co == NULL) return 0;
324         // Do it
325         lua_pushinteger(L, co->getWieldIndex() + 1);
326         return 1;
327 }
328
329 // get_wielded_item(self)
330 int ObjectRef::l_get_wielded_item(lua_State *L)
331 {
332         NO_MAP_LOCK_REQUIRED;
333         ObjectRef *ref = checkobject(L, 1);
334         ServerActiveObject *co = getobject(ref);
335         if (co == NULL) {
336                 // Empty ItemStack
337                 LuaItemStack::create(L, ItemStack());
338                 return 1;
339         }
340         // Do it
341         LuaItemStack::create(L, co->getWieldedItem());
342         return 1;
343 }
344
345 // set_wielded_item(self, itemstack or itemstring or table or nil)
346 int ObjectRef::l_set_wielded_item(lua_State *L)
347 {
348         NO_MAP_LOCK_REQUIRED;
349         ObjectRef *ref = checkobject(L, 1);
350         ServerActiveObject *co = getobject(ref);
351         if (co == NULL) return 0;
352         // Do it
353         ItemStack item = read_item(L, 2, getServer(L)->idef());
354         bool success = co->setWieldedItem(item);
355         if (success && co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
356                 getServer(L)->SendInventory(((PlayerSAO*)co));
357         }
358         lua_pushboolean(L, success);
359         return 1;
360 }
361
362 // set_armor_groups(self, groups)
363 int ObjectRef::l_set_armor_groups(lua_State *L)
364 {
365         NO_MAP_LOCK_REQUIRED;
366         ObjectRef *ref = checkobject(L, 1);
367         ServerActiveObject *co = getobject(ref);
368         if (co == NULL) return 0;
369         // Do it
370         ItemGroupList groups;
371         read_groups(L, 2, groups);
372         co->setArmorGroups(groups);
373         return 0;
374 }
375
376 // get_armor_groups(self)
377 int ObjectRef::l_get_armor_groups(lua_State *L)
378 {
379         NO_MAP_LOCK_REQUIRED;
380         ObjectRef *ref = checkobject(L, 1);
381         ServerActiveObject *co = getobject(ref);
382         if (co == NULL)
383                 return 0;
384         // Do it
385         push_groups(L, co->getArmorGroups());
386         return 1;
387 }
388
389 // set_physics_override(self, physics_override_speed, physics_override_jump,
390 //                      physics_override_gravity, sneak, sneak_glitch, new_move)
391 int ObjectRef::l_set_physics_override(lua_State *L)
392 {
393         NO_MAP_LOCK_REQUIRED;
394         ObjectRef *ref = checkobject(L, 1);
395         PlayerSAO *co = (PlayerSAO *) getobject(ref);
396         if (co == NULL) return 0;
397         // Do it
398         if (lua_istable(L, 2)) {
399                 co->m_physics_override_speed = getfloatfield_default(
400                                 L, 2, "speed", co->m_physics_override_speed);
401                 co->m_physics_override_jump = getfloatfield_default(
402                                 L, 2, "jump", co->m_physics_override_jump);
403                 co->m_physics_override_gravity = getfloatfield_default(
404                                 L, 2, "gravity", co->m_physics_override_gravity);
405                 co->m_physics_override_sneak = getboolfield_default(
406                                 L, 2, "sneak", co->m_physics_override_sneak);
407                 co->m_physics_override_sneak_glitch = getboolfield_default(
408                                 L, 2, "sneak_glitch", co->m_physics_override_sneak_glitch);
409                 co->m_physics_override_new_move = getboolfield_default(
410                                 L, 2, "new_move", co->m_physics_override_new_move);
411                 co->m_physics_override_sent = false;
412         } else {
413                 // old, non-table format
414                 if (!lua_isnil(L, 2)) {
415                         co->m_physics_override_speed = lua_tonumber(L, 2);
416                         co->m_physics_override_sent = false;
417                 }
418                 if (!lua_isnil(L, 3)) {
419                         co->m_physics_override_jump = lua_tonumber(L, 3);
420                         co->m_physics_override_sent = false;
421                 }
422                 if (!lua_isnil(L, 4)) {
423                         co->m_physics_override_gravity = lua_tonumber(L, 4);
424                         co->m_physics_override_sent = false;
425                 }
426         }
427         return 0;
428 }
429
430 // get_physics_override(self)
431 int ObjectRef::l_get_physics_override(lua_State *L)
432 {
433         NO_MAP_LOCK_REQUIRED;
434         ObjectRef *ref = checkobject(L, 1);
435         PlayerSAO *co = (PlayerSAO *)getobject(ref);
436         if (co == NULL)
437                 return 0;
438         // Do it
439         lua_newtable(L);
440         lua_pushnumber(L, co->m_physics_override_speed);
441         lua_setfield(L, -2, "speed");
442         lua_pushnumber(L, co->m_physics_override_jump);
443         lua_setfield(L, -2, "jump");
444         lua_pushnumber(L, co->m_physics_override_gravity);
445         lua_setfield(L, -2, "gravity");
446         lua_pushboolean(L, co->m_physics_override_sneak);
447         lua_setfield(L, -2, "sneak");
448         lua_pushboolean(L, co->m_physics_override_sneak_glitch);
449         lua_setfield(L, -2, "sneak_glitch");
450         lua_pushboolean(L, co->m_physics_override_new_move);
451         lua_setfield(L, -2, "new_move");
452         return 1;
453 }
454
455 // set_animation(self, frame_range, frame_speed, frame_blend, frame_loop)
456 int ObjectRef::l_set_animation(lua_State *L)
457 {
458         NO_MAP_LOCK_REQUIRED;
459         ObjectRef *ref = checkobject(L, 1);
460         ServerActiveObject *co = getobject(ref);
461         if (co == NULL) return 0;
462         // Do it
463         v2f frames = v2f(1, 1);
464         if (!lua_isnil(L, 2))
465                 frames = readParam<v2f>(L, 2);
466         float frame_speed = 15;
467         if (!lua_isnil(L, 3))
468                 frame_speed = lua_tonumber(L, 3);
469         float frame_blend = 0;
470         if (!lua_isnil(L, 4))
471                 frame_blend = lua_tonumber(L, 4);
472         bool frame_loop = true;
473         if (lua_isboolean(L, 5))
474                 frame_loop = readParam<bool>(L, 5);
475         co->setAnimation(frames, frame_speed, frame_blend, frame_loop);
476         return 0;
477 }
478
479 // get_animation(self)
480 int ObjectRef::l_get_animation(lua_State *L)
481 {
482         NO_MAP_LOCK_REQUIRED;
483         ObjectRef *ref = checkobject(L, 1);
484         ServerActiveObject *co = getobject(ref);
485         if (co == NULL)
486                 return 0;
487         // Do it
488         v2f frames = v2f(1,1);
489         float frame_speed = 15;
490         float frame_blend = 0;
491         bool frame_loop = true;
492         co->getAnimation(&frames, &frame_speed, &frame_blend, &frame_loop);
493
494         push_v2f(L, frames);
495         lua_pushnumber(L, frame_speed);
496         lua_pushnumber(L, frame_blend);
497         lua_pushboolean(L, frame_loop);
498         return 4;
499 }
500
501 // set_local_animation(self, {stand/idle}, {walk}, {dig}, {walk+dig}, frame_speed)
502 int ObjectRef::l_set_local_animation(lua_State *L)
503 {
504         NO_MAP_LOCK_REQUIRED;
505         ObjectRef *ref = checkobject(L, 1);
506         RemotePlayer *player = getplayer(ref);
507         if (player == NULL)
508                 return 0;
509         // Do it
510         v2s32 frames[4];
511         for (int i=0;i<4;i++) {
512                 if (!lua_isnil(L, 2+1))
513                         frames[i] = read_v2s32(L, 2+i);
514         }
515         float frame_speed = 30;
516         if (!lua_isnil(L, 6))
517                 frame_speed = lua_tonumber(L, 6);
518
519         getServer(L)->setLocalPlayerAnimations(player, frames, frame_speed);
520         lua_pushboolean(L, true);
521         return 1;
522 }
523
524 // get_local_animation(self)
525 int ObjectRef::l_get_local_animation(lua_State *L)
526 {
527         NO_MAP_LOCK_REQUIRED
528         ObjectRef *ref = checkobject(L, 1);
529         RemotePlayer *player = getplayer(ref);
530         if (player == NULL)
531                 return 0;
532
533         v2s32 frames[4];
534         float frame_speed;
535         player->getLocalAnimations(frames, &frame_speed);
536
537         for (const v2s32 &frame : frames) {
538                 push_v2s32(L, frame);
539         }
540
541         lua_pushnumber(L, frame_speed);
542         return 5;
543 }
544
545 // set_eye_offset(self, v3f first pv, v3f third pv)
546 int ObjectRef::l_set_eye_offset(lua_State *L)
547 {
548         NO_MAP_LOCK_REQUIRED;
549         ObjectRef *ref = checkobject(L, 1);
550         RemotePlayer *player = getplayer(ref);
551         if (player == NULL)
552                 return 0;
553         // Do it
554         v3f offset_first = v3f(0, 0, 0);
555         v3f offset_third = v3f(0, 0, 0);
556
557         if (!lua_isnil(L, 2))
558                 offset_first = read_v3f(L, 2);
559         if (!lua_isnil(L, 3))
560                 offset_third = read_v3f(L, 3);
561
562         // Prevent abuse of offset values (keep player always visible)
563         offset_third.X = rangelim(offset_third.X,-10,10);
564         offset_third.Z = rangelim(offset_third.Z,-5,5);
565         /* TODO: if possible: improve the camera colision detetion to allow Y <= -1.5) */
566         offset_third.Y = rangelim(offset_third.Y,-10,15); //1.5*BS
567
568         getServer(L)->setPlayerEyeOffset(player, offset_first, offset_third);
569         lua_pushboolean(L, true);
570         return 1;
571 }
572
573 // get_eye_offset(self)
574 int ObjectRef::l_get_eye_offset(lua_State *L)
575 {
576         NO_MAP_LOCK_REQUIRED;
577         ObjectRef *ref = checkobject(L, 1);
578         RemotePlayer *player = getplayer(ref);
579         if (player == NULL)
580                 return 0;
581         // Do it
582         push_v3f(L, player->eye_offset_first);
583         push_v3f(L, player->eye_offset_third);
584         return 2;
585 }
586
587 // send_mapblock(self, pos)
588 int ObjectRef::l_send_mapblock(lua_State *L)
589 {
590         NO_MAP_LOCK_REQUIRED;
591         ObjectRef *ref = checkobject(L, 1);
592
593         RemotePlayer *player = getplayer(ref);
594         if (!player)
595                 return 0;
596         v3s16 p = read_v3s16(L, 2);
597
598         session_t peer_id = player->getPeerId();
599         bool r = getServer(L)->SendBlock(peer_id, p);
600
601         lua_pushboolean(L, r);
602         return 1;
603 }
604
605 // set_animation_frame_speed(self, frame_speed)
606 int ObjectRef::l_set_animation_frame_speed(lua_State *L)
607 {
608         NO_MAP_LOCK_REQUIRED;
609         ObjectRef *ref = checkobject(L, 1);
610         ServerActiveObject *co = getobject(ref);
611         if (co == NULL)
612                 return 0;
613
614         // Do it
615         if (!lua_isnil(L, 2)) {
616                 float frame_speed = lua_tonumber(L, 2);
617                 co->setAnimationSpeed(frame_speed);
618                 lua_pushboolean(L, true);
619         } else {
620                 lua_pushboolean(L, false);
621         }
622         return 1;
623 }
624
625 // set_bone_position(self, std::string bone, v3f position, v3f rotation)
626 int ObjectRef::l_set_bone_position(lua_State *L)
627 {
628         NO_MAP_LOCK_REQUIRED;
629         ObjectRef *ref = checkobject(L, 1);
630         ServerActiveObject *co = getobject(ref);
631         if (co == NULL) return 0;
632         // Do it
633         std::string bone;
634         if (!lua_isnil(L, 2))
635                 bone = readParam<std::string>(L, 2);
636         v3f position = v3f(0, 0, 0);
637         if (!lua_isnil(L, 3))
638                 position = check_v3f(L, 3);
639         v3f rotation = v3f(0, 0, 0);
640         if (!lua_isnil(L, 4))
641                 rotation = check_v3f(L, 4);
642         co->setBonePosition(bone, position, rotation);
643         return 0;
644 }
645
646 // get_bone_position(self, bone)
647 int ObjectRef::l_get_bone_position(lua_State *L)
648 {
649         NO_MAP_LOCK_REQUIRED;
650         ObjectRef *ref = checkobject(L, 1);
651         ServerActiveObject *co = getobject(ref);
652         if (co == NULL)
653                 return 0;
654         // Do it
655         std::string bone;
656         if (!lua_isnil(L, 2))
657                 bone = readParam<std::string>(L, 2);
658
659         v3f position = v3f(0, 0, 0);
660         v3f rotation = v3f(0, 0, 0);
661         co->getBonePosition(bone, &position, &rotation);
662
663         push_v3f(L, position);
664         push_v3f(L, rotation);
665         return 2;
666 }
667
668 // set_attach(self, parent, bone, position, rotation)
669 int ObjectRef::l_set_attach(lua_State *L)
670 {
671         GET_ENV_PTR;
672
673         ObjectRef *ref = checkobject(L, 1);
674         ObjectRef *parent_ref = checkobject(L, 2);
675         ServerActiveObject *co = getobject(ref);
676         ServerActiveObject *parent = getobject(parent_ref);
677         if (co == NULL)
678                 return 0;
679         if (parent == NULL)
680                 return 0;
681         // Do it
682         int parent_id = 0;
683         std::string bone;
684         v3f position = v3f(0, 0, 0);
685         v3f rotation = v3f(0, 0, 0);
686         co->getAttachment(&parent_id, &bone, &position, &rotation);
687         if (parent_id) {
688                 ServerActiveObject *old_parent = env->getActiveObject(parent_id);
689                 old_parent->removeAttachmentChild(co->getId());
690         }
691
692         bone = "";
693         if (!lua_isnil(L, 3))
694                 bone = readParam<std::string>(L, 3);
695         position = v3f(0, 0, 0);
696         if (!lua_isnil(L, 4))
697                 position = read_v3f(L, 4);
698         rotation = v3f(0, 0, 0);
699         if (!lua_isnil(L, 5))
700                 rotation = read_v3f(L, 5);
701         co->setAttachment(parent->getId(), bone, position, rotation);
702         parent->addAttachmentChild(co->getId());
703         return 0;
704 }
705
706 // get_attach(self)
707 int ObjectRef::l_get_attach(lua_State *L)
708 {
709         GET_ENV_PTR;
710
711         ObjectRef *ref = checkobject(L, 1);
712         ServerActiveObject *co = getobject(ref);
713         if (co == NULL)
714                 return 0;
715
716         // Do it
717         int parent_id = 0;
718         std::string bone;
719         v3f position = v3f(0, 0, 0);
720         v3f rotation = v3f(0, 0, 0);
721         co->getAttachment(&parent_id, &bone, &position, &rotation);
722         if (!parent_id)
723                 return 0;
724         ServerActiveObject *parent = env->getActiveObject(parent_id);
725
726         getScriptApiBase(L)->objectrefGetOrCreate(L, parent);
727         lua_pushlstring(L, bone.c_str(), bone.size());
728         push_v3f(L, position);
729         push_v3f(L, rotation);
730         return 4;
731 }
732
733 // set_detach(self)
734 int ObjectRef::l_set_detach(lua_State *L)
735 {
736         GET_ENV_PTR;
737
738         ObjectRef *ref = checkobject(L, 1);
739         ServerActiveObject *co = getobject(ref);
740         if (co == NULL)
741                 return 0;
742
743         co->clearParentAttachment();
744         return 0;
745 }
746
747 // set_properties(self, properties)
748 int ObjectRef::l_set_properties(lua_State *L)
749 {
750         NO_MAP_LOCK_REQUIRED;
751         ObjectRef *ref = checkobject(L, 1);
752         ServerActiveObject *co = getobject(ref);
753         if (!co)
754                 return 0;
755
756         ObjectProperties *prop = co->accessObjectProperties();
757         if (!prop)
758                 return 0;
759
760         read_object_properties(L, 2, prop, getServer(L)->idef());
761
762         if (prop->hp_max < co->getHP()) {
763                 PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
764                 co->setHP(prop->hp_max, reason);
765                 if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
766                         getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, reason);
767         }
768
769         if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER &&
770                         prop->breath_max < co->getBreath())
771                 co->setBreath(prop->breath_max);
772
773         co->notifyObjectPropertiesModified();
774         return 0;
775 }
776
777 // get_properties(self)
778 int ObjectRef::l_get_properties(lua_State *L)
779 {
780         NO_MAP_LOCK_REQUIRED;
781         ObjectRef *ref = checkobject(L, 1);
782         ServerActiveObject *co = getobject(ref);
783         if (co == NULL)
784                 return 0;
785         ObjectProperties *prop = co->accessObjectProperties();
786         if (!prop)
787                 return 0;
788         push_object_properties(L, prop);
789         return 1;
790 }
791
792 // is_player(self)
793 int ObjectRef::l_is_player(lua_State *L)
794 {
795         NO_MAP_LOCK_REQUIRED;
796         ObjectRef *ref = checkobject(L, 1);
797         RemotePlayer *player = getplayer(ref);
798         lua_pushboolean(L, (player != NULL));
799         return 1;
800 }
801
802 // set_nametag_attributes(self, attributes)
803 int ObjectRef::l_set_nametag_attributes(lua_State *L)
804 {
805         NO_MAP_LOCK_REQUIRED;
806         ObjectRef *ref = checkobject(L, 1);
807         ServerActiveObject *co = getobject(ref);
808
809         if (co == NULL)
810                 return 0;
811         ObjectProperties *prop = co->accessObjectProperties();
812         if (!prop)
813                 return 0;
814
815         lua_getfield(L, 2, "color");
816         if (!lua_isnil(L, -1)) {
817                 video::SColor color = prop->nametag_color;
818                 read_color(L, -1, &color);
819                 prop->nametag_color = color;
820         }
821         lua_pop(L, 1);
822
823         std::string nametag = getstringfield_default(L, 2, "text", "");
824         prop->nametag = nametag;
825
826         co->notifyObjectPropertiesModified();
827         lua_pushboolean(L, true);
828         return 1;
829 }
830
831 // get_nametag_attributes(self)
832 int ObjectRef::l_get_nametag_attributes(lua_State *L)
833 {
834         NO_MAP_LOCK_REQUIRED;
835         ObjectRef *ref = checkobject(L, 1);
836         ServerActiveObject *co = getobject(ref);
837
838         if (co == NULL)
839                 return 0;
840         ObjectProperties *prop = co->accessObjectProperties();
841         if (!prop)
842                 return 0;
843
844         video::SColor color = prop->nametag_color;
845
846         lua_newtable(L);
847         push_ARGB8(L, color);
848         lua_setfield(L, -2, "color");
849         lua_pushstring(L, prop->nametag.c_str());
850         lua_setfield(L, -2, "text");
851         return 1;
852 }
853
854 /* LuaEntitySAO-only */
855
856 // set_velocity(self, {x=num, y=num, z=num})
857 int ObjectRef::l_set_velocity(lua_State *L)
858 {
859         NO_MAP_LOCK_REQUIRED;
860         ObjectRef *ref = checkobject(L, 1);
861         LuaEntitySAO *co = getluaobject(ref);
862         if (co == NULL) return 0;
863         v3f pos = checkFloatPos(L, 2);
864         // Do it
865         co->setVelocity(pos);
866         return 0;
867 }
868
869 // add_velocity(self, {x=num, y=num, z=num})
870 int ObjectRef::l_add_velocity(lua_State *L)
871 {
872         NO_MAP_LOCK_REQUIRED;
873         ObjectRef *ref = checkobject(L, 1);
874         LuaEntitySAO *co = getluaobject(ref);
875         if (!co)
876                 return 0;
877         v3f pos = checkFloatPos(L, 2);
878         // Do it
879         co->addVelocity(pos);
880         return 0;
881 }
882
883 // get_velocity(self)
884 int ObjectRef::l_get_velocity(lua_State *L)
885 {
886         NO_MAP_LOCK_REQUIRED;
887         ObjectRef *ref = checkobject(L, 1);
888         LuaEntitySAO *co = getluaobject(ref);
889         if (co == NULL) return 0;
890         // Do it
891         v3f v = co->getVelocity();
892         pushFloatPos(L, v);
893         return 1;
894 }
895
896 // set_acceleration(self, {x=num, y=num, z=num})
897 int ObjectRef::l_set_acceleration(lua_State *L)
898 {
899         NO_MAP_LOCK_REQUIRED;
900         ObjectRef *ref = checkobject(L, 1);
901         LuaEntitySAO *co = getluaobject(ref);
902         if (co == NULL) return 0;
903         // pos
904         v3f pos = checkFloatPos(L, 2);
905         // Do it
906         co->setAcceleration(pos);
907         return 0;
908 }
909
910 // get_acceleration(self)
911 int ObjectRef::l_get_acceleration(lua_State *L)
912 {
913         NO_MAP_LOCK_REQUIRED;
914         ObjectRef *ref = checkobject(L, 1);
915         LuaEntitySAO *co = getluaobject(ref);
916         if (co == NULL) return 0;
917         // Do it
918         v3f v = co->getAcceleration();
919         pushFloatPos(L, v);
920         return 1;
921 }
922
923 // set_rotation(self, {x=num, y=num, z=num})
924 // Each 'num' is in radians
925 int ObjectRef::l_set_rotation(lua_State *L)
926 {
927         NO_MAP_LOCK_REQUIRED;
928         ObjectRef *ref = checkobject(L, 1);
929         LuaEntitySAO *co = getluaobject(ref);
930         if (!co)
931                 return 0;
932
933         v3f rotation = check_v3f(L, 2) * core::RADTODEG;
934         co->setRotation(rotation);
935         return 0;
936 }
937
938 // get_rotation(self)
939 // returns: {x=num, y=num, z=num}
940 // Each 'num' is in radians
941 int ObjectRef::l_get_rotation(lua_State *L)
942 {
943         NO_MAP_LOCK_REQUIRED;
944         ObjectRef *ref = checkobject(L, 1);
945         LuaEntitySAO *co = getluaobject(ref);
946         if (!co)
947                 return 0;
948
949         lua_newtable(L);
950         v3f rotation = co->getRotation() * core::DEGTORAD;
951         push_v3f(L, rotation);
952         return 1;
953 }
954
955 // set_yaw(self, radians)
956 int ObjectRef::l_set_yaw(lua_State *L)
957 {
958         NO_MAP_LOCK_REQUIRED;
959         ObjectRef *ref = checkobject(L, 1);
960         LuaEntitySAO *co = getluaobject(ref);
961
962         if (co == NULL) return 0;
963         if (isNaN(L, 2))
964                 throw LuaError("ObjectRef::set_yaw: NaN value is not allowed.");
965
966         float yaw = readParam<float>(L, 2) * core::RADTODEG;
967         co->setRotation(v3f(0, yaw, 0));
968         return 0;
969 }
970
971 // get_yaw(self)
972 int ObjectRef::l_get_yaw(lua_State *L)
973 {
974         NO_MAP_LOCK_REQUIRED;
975         ObjectRef *ref = checkobject(L, 1);
976         LuaEntitySAO *co = getluaobject(ref);
977         if (!co)
978                 return 0;
979
980         float yaw = co->getRotation().Y * core::DEGTORAD;
981         lua_pushnumber(L, yaw);
982         return 1;
983 }
984
985 // set_texture_mod(self, mod)
986 int ObjectRef::l_set_texture_mod(lua_State *L)
987 {
988         NO_MAP_LOCK_REQUIRED;
989         ObjectRef *ref = checkobject(L, 1);
990         LuaEntitySAO *co = getluaobject(ref);
991         if (co == NULL) return 0;
992         // Do it
993         std::string mod = luaL_checkstring(L, 2);
994         co->setTextureMod(mod);
995         return 0;
996 }
997
998 // get_texture_mod(self)
999 int ObjectRef::l_get_texture_mod(lua_State *L)
1000 {
1001         NO_MAP_LOCK_REQUIRED;
1002         ObjectRef *ref = checkobject(L, 1);
1003         LuaEntitySAO *co = getluaobject(ref);
1004         if (co == NULL) return 0;
1005         // Do it
1006         std::string mod = co->getTextureMod();
1007         lua_pushstring(L, mod.c_str());
1008         return 1;
1009 }
1010
1011 // set_sprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
1012 //           select_horiz_by_yawpitch=false)
1013 int ObjectRef::l_set_sprite(lua_State *L)
1014 {
1015         NO_MAP_LOCK_REQUIRED;
1016         ObjectRef *ref = checkobject(L, 1);
1017         LuaEntitySAO *co = getluaobject(ref);
1018         if (co == NULL) return 0;
1019         // Do it
1020         v2s16 p(0,0);
1021         if (!lua_isnil(L, 2))
1022                 p = readParam<v2s16>(L, 2);
1023         int num_frames = 1;
1024         if (!lua_isnil(L, 3))
1025                 num_frames = lua_tonumber(L, 3);
1026         float framelength = 0.2;
1027         if (!lua_isnil(L, 4))
1028                 framelength = lua_tonumber(L, 4);
1029         bool select_horiz_by_yawpitch = false;
1030         if (!lua_isnil(L, 5))
1031                 select_horiz_by_yawpitch = readParam<bool>(L, 5);
1032         co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
1033         return 0;
1034 }
1035
1036 // DEPRECATED
1037 // get_entity_name(self)
1038 int ObjectRef::l_get_entity_name(lua_State *L)
1039 {
1040         NO_MAP_LOCK_REQUIRED;
1041         ObjectRef *ref = checkobject(L, 1);
1042         LuaEntitySAO *co = getluaobject(ref);
1043         log_deprecated(L,"Deprecated call to \"get_entity_name");
1044         if (co == NULL) return 0;
1045         // Do it
1046         std::string name = co->getName();
1047         lua_pushstring(L, name.c_str());
1048         return 1;
1049 }
1050
1051 // get_luaentity(self)
1052 int ObjectRef::l_get_luaentity(lua_State *L)
1053 {
1054         NO_MAP_LOCK_REQUIRED;
1055         ObjectRef *ref = checkobject(L, 1);
1056         LuaEntitySAO *co = getluaobject(ref);
1057         if (co == NULL) return 0;
1058         // Do it
1059         luaentity_get(L, co->getId());
1060         return 1;
1061 }
1062
1063 /* Player-only */
1064
1065 // is_player_connected(self)
1066 int ObjectRef::l_is_player_connected(lua_State *L)
1067 {
1068         NO_MAP_LOCK_REQUIRED;
1069         ObjectRef *ref = checkobject(L, 1);
1070         RemotePlayer *player = getplayer(ref);
1071         lua_pushboolean(L, (player != NULL && player->getPeerId() != PEER_ID_INEXISTENT));
1072         return 1;
1073 }
1074
1075 // get_player_name(self)
1076 int ObjectRef::l_get_player_name(lua_State *L)
1077 {
1078         NO_MAP_LOCK_REQUIRED;
1079         ObjectRef *ref = checkobject(L, 1);
1080         RemotePlayer *player = getplayer(ref);
1081         if (player == NULL) {
1082                 lua_pushlstring(L, "", 0);
1083                 return 1;
1084         }
1085         // Do it
1086         lua_pushstring(L, player->getName());
1087         return 1;
1088 }
1089
1090 // get_player_velocity(self)
1091 int ObjectRef::l_get_player_velocity(lua_State *L)
1092 {
1093         NO_MAP_LOCK_REQUIRED;
1094         ObjectRef *ref = checkobject(L, 1);
1095         RemotePlayer *player = getplayer(ref);
1096         if (player == NULL) {
1097                 lua_pushnil(L);
1098                 return 1;
1099         }
1100         // Do it
1101         push_v3f(L, player->getSpeed() / BS);
1102         return 1;
1103 }
1104
1105 // get_look_dir(self)
1106 int ObjectRef::l_get_look_dir(lua_State *L)
1107 {
1108         NO_MAP_LOCK_REQUIRED;
1109         ObjectRef *ref = checkobject(L, 1);
1110         PlayerSAO* co = getplayersao(ref);
1111         if (co == NULL) return 0;
1112         // Do it
1113         float pitch = co->getRadLookPitchDep();
1114         float yaw = co->getRadYawDep();
1115         v3f v(std::cos(pitch) * std::cos(yaw), std::sin(pitch), std::cos(pitch) *
1116                 std::sin(yaw));
1117         push_v3f(L, v);
1118         return 1;
1119 }
1120
1121 // DEPRECATED
1122 // get_look_pitch(self)
1123 int ObjectRef::l_get_look_pitch(lua_State *L)
1124 {
1125         NO_MAP_LOCK_REQUIRED;
1126
1127         log_deprecated(L,
1128                 "Deprecated call to get_look_pitch, use get_look_vertical instead");
1129
1130         ObjectRef *ref = checkobject(L, 1);
1131         PlayerSAO* co = getplayersao(ref);
1132         if (co == NULL) return 0;
1133         // Do it
1134         lua_pushnumber(L, co->getRadLookPitchDep());
1135         return 1;
1136 }
1137
1138 // DEPRECATED
1139 // get_look_yaw(self)
1140 int ObjectRef::l_get_look_yaw(lua_State *L)
1141 {
1142         NO_MAP_LOCK_REQUIRED;
1143
1144         log_deprecated(L,
1145                 "Deprecated call to get_look_yaw, use get_look_horizontal instead");
1146
1147         ObjectRef *ref = checkobject(L, 1);
1148         PlayerSAO* co = getplayersao(ref);
1149         if (co == NULL) return 0;
1150         // Do it
1151         lua_pushnumber(L, co->getRadYawDep());
1152         return 1;
1153 }
1154
1155 // get_look_pitch2(self)
1156 int ObjectRef::l_get_look_vertical(lua_State *L)
1157 {
1158         NO_MAP_LOCK_REQUIRED;
1159         ObjectRef *ref = checkobject(L, 1);
1160         PlayerSAO* co = getplayersao(ref);
1161         if (co == NULL) return 0;
1162         // Do it
1163         lua_pushnumber(L, co->getRadLookPitch());
1164         return 1;
1165 }
1166
1167 // get_look_yaw2(self)
1168 int ObjectRef::l_get_look_horizontal(lua_State *L)
1169 {
1170         NO_MAP_LOCK_REQUIRED;
1171         ObjectRef *ref = checkobject(L, 1);
1172         PlayerSAO* co = getplayersao(ref);
1173         if (co == NULL) return 0;
1174         // Do it
1175         lua_pushnumber(L, co->getRadRotation().Y);
1176         return 1;
1177 }
1178
1179 // set_look_vertical(self, radians)
1180 int ObjectRef::l_set_look_vertical(lua_State *L)
1181 {
1182         NO_MAP_LOCK_REQUIRED;
1183         ObjectRef *ref = checkobject(L, 1);
1184         PlayerSAO* co = getplayersao(ref);
1185         if (co == NULL) return 0;
1186         float pitch = readParam<float>(L, 2) * core::RADTODEG;
1187         // Do it
1188         co->setLookPitchAndSend(pitch);
1189         return 1;
1190 }
1191
1192 // set_look_horizontal(self, radians)
1193 int ObjectRef::l_set_look_horizontal(lua_State *L)
1194 {
1195         NO_MAP_LOCK_REQUIRED;
1196         ObjectRef *ref = checkobject(L, 1);
1197         PlayerSAO* co = getplayersao(ref);
1198         if (co == NULL) return 0;
1199         float yaw = readParam<float>(L, 2) * core::RADTODEG;
1200         // Do it
1201         co->setPlayerYawAndSend(yaw);
1202         return 1;
1203 }
1204
1205 // DEPRECATED
1206 // set_look_pitch(self, radians)
1207 int ObjectRef::l_set_look_pitch(lua_State *L)
1208 {
1209         NO_MAP_LOCK_REQUIRED;
1210
1211         log_deprecated(L,
1212                 "Deprecated call to set_look_pitch, use set_look_vertical instead.");
1213
1214         ObjectRef *ref = checkobject(L, 1);
1215         PlayerSAO* co = getplayersao(ref);
1216         if (co == NULL) return 0;
1217         float pitch = readParam<float>(L, 2) * core::RADTODEG;
1218         // Do it
1219         co->setLookPitchAndSend(pitch);
1220         return 1;
1221 }
1222
1223 // DEPRECATED
1224 // set_look_yaw(self, radians)
1225 int ObjectRef::l_set_look_yaw(lua_State *L)
1226 {
1227         NO_MAP_LOCK_REQUIRED;
1228
1229         log_deprecated(L,
1230                 "Deprecated call to set_look_yaw, use set_look_horizontal instead.");
1231
1232         ObjectRef *ref = checkobject(L, 1);
1233         PlayerSAO* co = getplayersao(ref);
1234         if (co == NULL) return 0;
1235         float yaw = readParam<float>(L, 2) * core::RADTODEG;
1236         // Do it
1237         co->setPlayerYawAndSend(yaw);
1238         return 1;
1239 }
1240
1241 // set_breath(self, breath)
1242 int ObjectRef::l_set_breath(lua_State *L)
1243 {
1244         NO_MAP_LOCK_REQUIRED;
1245         ObjectRef *ref = checkobject(L, 1);
1246         PlayerSAO* co = getplayersao(ref);
1247         if (co == NULL) return 0;
1248         u16 breath = luaL_checknumber(L, 2);
1249         co->setBreath(breath);
1250
1251         return 0;
1252 }
1253
1254 // get_breath(self)
1255 int ObjectRef::l_get_breath(lua_State *L)
1256 {
1257         NO_MAP_LOCK_REQUIRED;
1258         ObjectRef *ref = checkobject(L, 1);
1259         PlayerSAO* co = getplayersao(ref);
1260         if (co == NULL) return 0;
1261         // Do it
1262         u16 breath = co->getBreath();
1263         lua_pushinteger (L, breath);
1264         return 1;
1265 }
1266
1267 // set_attribute(self, attribute, value)
1268 int ObjectRef::l_set_attribute(lua_State *L)
1269 {
1270         log_deprecated(L,
1271                 "Deprecated call to set_attribute, use MetaDataRef methods instead.");
1272
1273         ObjectRef *ref = checkobject(L, 1);
1274         PlayerSAO* co = getplayersao(ref);
1275         if (co == NULL)
1276                 return 0;
1277
1278         std::string attr = luaL_checkstring(L, 2);
1279         if (lua_isnil(L, 3)) {
1280                 co->getMeta().removeString(attr);
1281         } else {
1282                 std::string value = luaL_checkstring(L, 3);
1283                 co->getMeta().setString(attr, value);
1284         }
1285         return 1;
1286 }
1287
1288 // get_attribute(self, attribute)
1289 int ObjectRef::l_get_attribute(lua_State *L)
1290 {
1291         log_deprecated(L,
1292                 "Deprecated call to get_attribute, use MetaDataRef methods instead.");
1293
1294         ObjectRef *ref = checkobject(L, 1);
1295         PlayerSAO* co = getplayersao(ref);
1296         if (co == NULL)
1297                 return 0;
1298
1299         std::string attr = luaL_checkstring(L, 2);
1300
1301         std::string value;
1302         if (co->getMeta().getStringToRef(attr, value)) {
1303                 lua_pushstring(L, value.c_str());
1304                 return 1;
1305         }
1306
1307         return 0;
1308 }
1309
1310
1311 // get_meta(self, attribute)
1312 int ObjectRef::l_get_meta(lua_State *L)
1313 {
1314         ObjectRef *ref = checkobject(L, 1);
1315         PlayerSAO *co = getplayersao(ref);
1316         if (co == NULL)
1317                 return 0;
1318
1319         PlayerMetaRef::create(L, &co->getMeta());
1320         return 1;
1321 }
1322
1323
1324 // set_inventory_formspec(self, formspec)
1325 int ObjectRef::l_set_inventory_formspec(lua_State *L)
1326 {
1327         NO_MAP_LOCK_REQUIRED;
1328         ObjectRef *ref = checkobject(L, 1);
1329         RemotePlayer *player = getplayer(ref);
1330         if (player == NULL) return 0;
1331         std::string formspec = luaL_checkstring(L, 2);
1332
1333         player->inventory_formspec = formspec;
1334         getServer(L)->reportInventoryFormspecModified(player->getName());
1335         lua_pushboolean(L, true);
1336         return 1;
1337 }
1338
1339 // get_inventory_formspec(self) -> formspec
1340 int ObjectRef::l_get_inventory_formspec(lua_State *L)
1341 {
1342         NO_MAP_LOCK_REQUIRED;
1343         ObjectRef *ref = checkobject(L, 1);
1344         RemotePlayer *player = getplayer(ref);
1345         if (player == NULL) return 0;
1346
1347         std::string formspec = player->inventory_formspec;
1348         lua_pushlstring(L, formspec.c_str(), formspec.size());
1349         return 1;
1350 }
1351
1352 // set_formspec_prepend(self, formspec)
1353 int ObjectRef::l_set_formspec_prepend(lua_State *L)
1354 {
1355         NO_MAP_LOCK_REQUIRED;
1356         ObjectRef *ref = checkobject(L, 1);
1357         RemotePlayer *player = getplayer(ref);
1358         if (player == NULL)
1359                 return 0;
1360
1361         std::string formspec = luaL_checkstring(L, 2);
1362
1363         player->formspec_prepend = formspec;
1364         getServer(L)->reportFormspecPrependModified(player->getName());
1365         lua_pushboolean(L, true);
1366         return 1;
1367 }
1368
1369 // get_formspec_prepend(self) -> formspec
1370 int ObjectRef::l_get_formspec_prepend(lua_State *L)
1371 {
1372         NO_MAP_LOCK_REQUIRED;
1373         ObjectRef *ref = checkobject(L, 1);
1374         RemotePlayer *player = getplayer(ref);
1375         if (player == NULL)
1376                  return 0;
1377
1378         std::string formspec = player->formspec_prepend;
1379         lua_pushlstring(L, formspec.c_str(), formspec.size());
1380         return 1;
1381 }
1382
1383 // get_player_control(self)
1384 int ObjectRef::l_get_player_control(lua_State *L)
1385 {
1386         NO_MAP_LOCK_REQUIRED;
1387         ObjectRef *ref = checkobject(L, 1);
1388         RemotePlayer *player = getplayer(ref);
1389         if (player == NULL) {
1390                 lua_pushlstring(L, "", 0);
1391                 return 1;
1392         }
1393
1394         const PlayerControl &control = player->getPlayerControl();
1395         lua_newtable(L);
1396         lua_pushboolean(L, control.up);
1397         lua_setfield(L, -2, "up");
1398         lua_pushboolean(L, control.down);
1399         lua_setfield(L, -2, "down");
1400         lua_pushboolean(L, control.left);
1401         lua_setfield(L, -2, "left");
1402         lua_pushboolean(L, control.right);
1403         lua_setfield(L, -2, "right");
1404         lua_pushboolean(L, control.jump);
1405         lua_setfield(L, -2, "jump");
1406         lua_pushboolean(L, control.aux1);
1407         lua_setfield(L, -2, "aux1");
1408         lua_pushboolean(L, control.sneak);
1409         lua_setfield(L, -2, "sneak");
1410         lua_pushboolean(L, control.LMB);
1411         lua_setfield(L, -2, "LMB");
1412         lua_pushboolean(L, control.RMB);
1413         lua_setfield(L, -2, "RMB");
1414         return 1;
1415 }
1416
1417 // get_player_control_bits(self)
1418 int ObjectRef::l_get_player_control_bits(lua_State *L)
1419 {
1420         NO_MAP_LOCK_REQUIRED;
1421         ObjectRef *ref = checkobject(L, 1);
1422         RemotePlayer *player = getplayer(ref);
1423         if (player == NULL) {
1424                 lua_pushlstring(L, "", 0);
1425                 return 1;
1426         }
1427         // Do it
1428         lua_pushnumber(L, player->keyPressed);
1429         return 1;
1430 }
1431
1432 // hud_add(self, form)
1433 int ObjectRef::l_hud_add(lua_State *L)
1434 {
1435         NO_MAP_LOCK_REQUIRED;
1436         ObjectRef *ref = checkobject(L, 1);
1437         RemotePlayer *player = getplayer(ref);
1438         if (player == NULL)
1439                 return 0;
1440
1441         HudElement *elem = new HudElement;
1442         read_hud_element(L, elem);
1443
1444         u32 id = getServer(L)->hudAdd(player, elem);
1445         if (id == U32_MAX) {
1446                 delete elem;
1447                 return 0;
1448         }
1449
1450         lua_pushnumber(L, id);
1451         return 1;
1452 }
1453
1454 // hud_remove(self, id)
1455 int ObjectRef::l_hud_remove(lua_State *L)
1456 {
1457         NO_MAP_LOCK_REQUIRED;
1458         ObjectRef *ref = checkobject(L, 1);
1459         RemotePlayer *player = getplayer(ref);
1460         if (player == NULL)
1461                 return 0;
1462
1463         u32 id = -1;
1464         if (!lua_isnil(L, 2))
1465                 id = lua_tonumber(L, 2);
1466
1467         if (!getServer(L)->hudRemove(player, id))
1468                 return 0;
1469
1470         lua_pushboolean(L, true);
1471         return 1;
1472 }
1473
1474 // hud_change(self, id, stat, data)
1475 int ObjectRef::l_hud_change(lua_State *L)
1476 {
1477         NO_MAP_LOCK_REQUIRED;
1478         ObjectRef *ref = checkobject(L, 1);
1479         RemotePlayer *player = getplayer(ref);
1480         if (player == NULL)
1481                 return 0;
1482
1483         u32 id = lua_isnumber(L, 2) ? lua_tonumber(L, 2) : -1;
1484
1485         HudElement *e = player->getHud(id);
1486         if (!e)
1487                 return 0;
1488
1489         void *value = NULL;
1490         HudElementStat stat = read_hud_change(L, e, &value);
1491
1492         getServer(L)->hudChange(player, id, stat, value);
1493
1494         lua_pushboolean(L, true);
1495         return 1;
1496 }
1497
1498 // hud_get(self, id)
1499 int ObjectRef::l_hud_get(lua_State *L)
1500 {
1501         NO_MAP_LOCK_REQUIRED;
1502         ObjectRef *ref = checkobject(L, 1);
1503         RemotePlayer *player = getplayer(ref);
1504         if (player == NULL)
1505                 return 0;
1506
1507         u32 id = lua_tonumber(L, -1);
1508
1509         HudElement *e = player->getHud(id);
1510         if (!e)
1511                 return 0;
1512         push_hud_element(L, e);
1513         return 1;
1514 }
1515
1516 // hud_set_flags(self, flags)
1517 int ObjectRef::l_hud_set_flags(lua_State *L)
1518 {
1519         NO_MAP_LOCK_REQUIRED;
1520         ObjectRef *ref = checkobject(L, 1);
1521         RemotePlayer *player = getplayer(ref);
1522         if (player == NULL)
1523                 return 0;
1524
1525         u32 flags = 0;
1526         u32 mask  = 0;
1527         bool flag;
1528
1529         const EnumString *esp = es_HudBuiltinElement;
1530         for (int i = 0; esp[i].str; i++) {
1531                 if (getboolfield(L, 2, esp[i].str, flag)) {
1532                         flags |= esp[i].num * flag;
1533                         mask  |= esp[i].num;
1534                 }
1535         }
1536         if (!getServer(L)->hudSetFlags(player, flags, mask))
1537                 return 0;
1538
1539         lua_pushboolean(L, true);
1540         return 1;
1541 }
1542
1543 int ObjectRef::l_hud_get_flags(lua_State *L)
1544 {
1545         NO_MAP_LOCK_REQUIRED;
1546         ObjectRef *ref = checkobject(L, 1);
1547         RemotePlayer *player = getplayer(ref);
1548         if (player == NULL)
1549                 return 0;
1550
1551         lua_newtable(L);
1552         lua_pushboolean(L, player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE);
1553         lua_setfield(L, -2, "hotbar");
1554         lua_pushboolean(L, player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE);
1555         lua_setfield(L, -2, "healthbar");
1556         lua_pushboolean(L, player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE);
1557         lua_setfield(L, -2, "crosshair");
1558         lua_pushboolean(L, player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE);
1559         lua_setfield(L, -2, "wielditem");
1560         lua_pushboolean(L, player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE);
1561         lua_setfield(L, -2, "breathbar");
1562         lua_pushboolean(L, player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE);
1563         lua_setfield(L, -2, "minimap");
1564         lua_pushboolean(L, player->hud_flags & HUD_FLAG_MINIMAP_RADAR_VISIBLE);
1565         lua_setfield(L, -2, "minimap_radar");
1566
1567         return 1;
1568 }
1569
1570 // hud_set_hotbar_itemcount(self, hotbar_itemcount)
1571 int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L)
1572 {
1573         NO_MAP_LOCK_REQUIRED;
1574         ObjectRef *ref = checkobject(L, 1);
1575         RemotePlayer *player = getplayer(ref);
1576         if (player == NULL)
1577                 return 0;
1578
1579         s32 hotbar_itemcount = lua_tonumber(L, 2);
1580
1581         if (!getServer(L)->hudSetHotbarItemcount(player, hotbar_itemcount))
1582                 return 0;
1583
1584         lua_pushboolean(L, true);
1585         return 1;
1586 }
1587
1588 // hud_get_hotbar_itemcount(self)
1589 int ObjectRef::l_hud_get_hotbar_itemcount(lua_State *L)
1590 {
1591         NO_MAP_LOCK_REQUIRED;
1592         ObjectRef *ref = checkobject(L, 1);
1593         RemotePlayer *player = getplayer(ref);
1594         if (player == NULL)
1595                 return 0;
1596
1597         lua_pushnumber(L, player->getHotbarItemcount());
1598         return 1;
1599 }
1600
1601 // hud_set_hotbar_image(self, name)
1602 int ObjectRef::l_hud_set_hotbar_image(lua_State *L)
1603 {
1604         NO_MAP_LOCK_REQUIRED;
1605         ObjectRef *ref = checkobject(L, 1);
1606         RemotePlayer *player = getplayer(ref);
1607         if (player == NULL)
1608                 return 0;
1609
1610         std::string name = readParam<std::string>(L, 2);
1611
1612         getServer(L)->hudSetHotbarImage(player, name);
1613         return 1;
1614 }
1615
1616 // hud_get_hotbar_image(self)
1617 int ObjectRef::l_hud_get_hotbar_image(lua_State *L)
1618 {
1619         NO_MAP_LOCK_REQUIRED;
1620         ObjectRef *ref = checkobject(L, 1);
1621         RemotePlayer *player = getplayer(ref);
1622         if (player == NULL)
1623                 return 0;
1624
1625         const std::string &name = player->getHotbarImage();
1626         lua_pushlstring(L, name.c_str(), name.size());
1627         return 1;
1628 }
1629
1630 // hud_set_hotbar_selected_image(self, name)
1631 int ObjectRef::l_hud_set_hotbar_selected_image(lua_State *L)
1632 {
1633         NO_MAP_LOCK_REQUIRED;
1634         ObjectRef *ref = checkobject(L, 1);
1635         RemotePlayer *player = getplayer(ref);
1636         if (player == NULL)
1637                 return 0;
1638
1639         std::string name = readParam<std::string>(L, 2);
1640
1641         getServer(L)->hudSetHotbarSelectedImage(player, name);
1642         return 1;
1643 }
1644
1645 // hud_get_hotbar_selected_image(self)
1646 int ObjectRef::l_hud_get_hotbar_selected_image(lua_State *L)
1647 {
1648         NO_MAP_LOCK_REQUIRED;
1649         ObjectRef *ref = checkobject(L, 1);
1650         RemotePlayer *player = getplayer(ref);
1651         if (player == NULL)
1652                 return 0;
1653
1654         const std::string &name = player->getHotbarSelectedImage();
1655         lua_pushlstring(L, name.c_str(), name.size());
1656         return 1;
1657 }
1658
1659 // set_sky(self, bgcolor, type, list, clouds = true)
1660 int ObjectRef::l_set_sky(lua_State *L)
1661 {
1662         NO_MAP_LOCK_REQUIRED;
1663         ObjectRef *ref = checkobject(L, 1);
1664         RemotePlayer *player = getplayer(ref);
1665         if (player == NULL)
1666                 return 0;
1667
1668         video::SColor bgcolor(255,255,255,255);
1669         read_color(L, 2, &bgcolor);
1670
1671         std::string type = luaL_checkstring(L, 3);
1672
1673         std::vector<std::string> params;
1674         if (lua_istable(L, 4)) {
1675                 lua_pushnil(L);
1676                 while (lua_next(L, 4) != 0) {
1677                         // key at index -2 and value at index -1
1678                         if (lua_isstring(L, -1))
1679                                 params.emplace_back(readParam<std::string>(L, -1));
1680                         else
1681                                 params.emplace_back("");
1682                         // removes value, keeps key for next iteration
1683                         lua_pop(L, 1);
1684                 }
1685         }
1686
1687         if (type == "skybox" && params.size() != 6)
1688                 throw LuaError("skybox expects 6 textures");
1689
1690         bool clouds = true;
1691         if (lua_isboolean(L, 5))
1692                 clouds = readParam<bool>(L, 5);
1693
1694         getServer(L)->setSky(player, bgcolor, type, params, clouds);
1695         lua_pushboolean(L, true);
1696         return 1;
1697 }
1698
1699 // get_sky(self)
1700 int ObjectRef::l_get_sky(lua_State *L)
1701 {
1702         NO_MAP_LOCK_REQUIRED;
1703         ObjectRef *ref = checkobject(L, 1);
1704         RemotePlayer *player = getplayer(ref);
1705         if (player == NULL)
1706                 return 0;
1707         video::SColor bgcolor(255, 255, 255, 255);
1708         std::string type;
1709         std::vector<std::string> params;
1710         bool clouds;
1711
1712         player->getSky(&bgcolor, &type, &params, &clouds);
1713         type = type.empty() ? "regular" : type;
1714
1715         push_ARGB8(L, bgcolor);
1716         lua_pushlstring(L, type.c_str(), type.size());
1717         lua_newtable(L);
1718         s16 i = 1;
1719         for (const std::string &param : params) {
1720                 lua_pushlstring(L, param.c_str(), param.size());
1721                 lua_rawseti(L, -2, i++);
1722         }
1723         lua_pushboolean(L, clouds);
1724         return 4;
1725 }
1726
1727 // set_clouds(self, {density=, color=, ambient=, height=, thickness=, speed=})
1728 int ObjectRef::l_set_clouds(lua_State *L)
1729 {
1730         NO_MAP_LOCK_REQUIRED;
1731         ObjectRef *ref = checkobject(L, 1);
1732         RemotePlayer *player = getplayer(ref);
1733         if (!player)
1734                 return 0;
1735         if (!lua_istable(L, 2))
1736                 return 0;
1737
1738         CloudParams cloud_params = player->getCloudParams();
1739
1740         cloud_params.density = getfloatfield_default(L, 2, "density", cloud_params.density);
1741
1742         lua_getfield(L, 2, "color");
1743         if (!lua_isnil(L, -1))
1744                 read_color(L, -1, &cloud_params.color_bright);
1745         lua_pop(L, 1);
1746         lua_getfield(L, 2, "ambient");
1747         if (!lua_isnil(L, -1))
1748                 read_color(L, -1, &cloud_params.color_ambient);
1749         lua_pop(L, 1);
1750
1751         cloud_params.height    = getfloatfield_default(L, 2, "height",    cloud_params.height   );
1752         cloud_params.thickness = getfloatfield_default(L, 2, "thickness", cloud_params.thickness);
1753
1754         lua_getfield(L, 2, "speed");
1755         if (lua_istable(L, -1)) {
1756                 v2f new_speed;
1757                 new_speed.X = getfloatfield_default(L, -1, "x", 0);
1758                 new_speed.Y = getfloatfield_default(L, -1, "z", 0);
1759                 cloud_params.speed = new_speed;
1760         }
1761         lua_pop(L, 1);
1762
1763         getServer(L)->setClouds(player, cloud_params);
1764         lua_pushboolean(L, true);
1765         return 1;
1766 }
1767
1768 int ObjectRef::l_get_clouds(lua_State *L)
1769 {
1770         NO_MAP_LOCK_REQUIRED;
1771         ObjectRef *ref = checkobject(L, 1);
1772         RemotePlayer *player = getplayer(ref);
1773         if (!player)
1774                 return 0;
1775         const CloudParams &cloud_params = player->getCloudParams();
1776
1777         lua_newtable(L);
1778         lua_pushnumber(L, cloud_params.density);
1779         lua_setfield(L, -2, "density");
1780         push_ARGB8(L, cloud_params.color_bright);
1781         lua_setfield(L, -2, "color");
1782         push_ARGB8(L, cloud_params.color_ambient);
1783         lua_setfield(L, -2, "ambient");
1784         lua_pushnumber(L, cloud_params.height);
1785         lua_setfield(L, -2, "height");
1786         lua_pushnumber(L, cloud_params.thickness);
1787         lua_setfield(L, -2, "thickness");
1788         lua_newtable(L);
1789         lua_pushnumber(L, cloud_params.speed.X);
1790         lua_setfield(L, -2, "x");
1791         lua_pushnumber(L, cloud_params.speed.Y);
1792         lua_setfield(L, -2, "y");
1793         lua_setfield(L, -2, "speed");
1794
1795         return 1;
1796 }
1797
1798
1799 // override_day_night_ratio(self, brightness=0...1)
1800 int ObjectRef::l_override_day_night_ratio(lua_State *L)
1801 {
1802         NO_MAP_LOCK_REQUIRED;
1803         ObjectRef *ref = checkobject(L, 1);
1804         RemotePlayer *player = getplayer(ref);
1805         if (player == NULL)
1806                 return 0;
1807
1808         bool do_override = false;
1809         float ratio = 0.0f;
1810         if (!lua_isnil(L, 2)) {
1811                 do_override = true;
1812                 ratio = readParam<float>(L, 2);
1813         }
1814
1815         if (!getServer(L)->overrideDayNightRatio(player, do_override, ratio))
1816                 return 0;
1817
1818         lua_pushboolean(L, true);
1819         return 1;
1820 }
1821
1822 // get_day_night_ratio(self)
1823 int ObjectRef::l_get_day_night_ratio(lua_State *L)
1824 {
1825         NO_MAP_LOCK_REQUIRED;
1826         ObjectRef *ref = checkobject(L, 1);
1827         RemotePlayer *player = getplayer(ref);
1828         if (player == NULL)
1829                 return 0;
1830
1831         bool do_override;
1832         float ratio;
1833         player->getDayNightRatio(&do_override, &ratio);
1834
1835         if (do_override)
1836                 lua_pushnumber(L, ratio);
1837         else
1838                 lua_pushnil(L);
1839
1840         return 1;
1841 }
1842
1843 ObjectRef::ObjectRef(ServerActiveObject *object):
1844         m_object(object)
1845 {
1846         //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
1847 }
1848
1849 // Creates an ObjectRef and leaves it on top of stack
1850 // Not callable from Lua; all references are created on the C side.
1851 void ObjectRef::create(lua_State *L, ServerActiveObject *object)
1852 {
1853         ObjectRef *o = new ObjectRef(object);
1854         //infostream<<"ObjectRef::create: o="<<o<<std::endl;
1855         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1856         luaL_getmetatable(L, className);
1857         lua_setmetatable(L, -2);
1858 }
1859
1860 void ObjectRef::set_null(lua_State *L)
1861 {
1862         ObjectRef *o = checkobject(L, -1);
1863         o->m_object = NULL;
1864 }
1865
1866 void ObjectRef::Register(lua_State *L)
1867 {
1868         lua_newtable(L);
1869         int methodtable = lua_gettop(L);
1870         luaL_newmetatable(L, className);
1871         int metatable = lua_gettop(L);
1872
1873         lua_pushliteral(L, "__metatable");
1874         lua_pushvalue(L, methodtable);
1875         lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
1876
1877         lua_pushliteral(L, "__index");
1878         lua_pushvalue(L, methodtable);
1879         lua_settable(L, metatable);
1880
1881         lua_pushliteral(L, "__gc");
1882         lua_pushcfunction(L, gc_object);
1883         lua_settable(L, metatable);
1884
1885         lua_pop(L, 1);  // drop metatable
1886
1887         markAliasDeprecated(methods);
1888         luaL_openlib(L, 0, methods, 0);  // fill methodtable
1889         lua_pop(L, 1);  // drop methodtable
1890
1891         // Cannot be created from Lua
1892         //lua_register(L, className, create_object);
1893 }
1894
1895 const char ObjectRef::className[] = "ObjectRef";
1896 luaL_Reg ObjectRef::methods[] = {
1897         // ServerActiveObject
1898         luamethod(ObjectRef, remove),
1899         luamethod_aliased(ObjectRef, get_pos, getpos),
1900         luamethod_aliased(ObjectRef, set_pos, setpos),
1901         luamethod_aliased(ObjectRef, move_to, moveto),
1902         luamethod(ObjectRef, punch),
1903         luamethod(ObjectRef, right_click),
1904         luamethod(ObjectRef, set_hp),
1905         luamethod(ObjectRef, get_hp),
1906         luamethod(ObjectRef, get_inventory),
1907         luamethod(ObjectRef, get_wield_list),
1908         luamethod(ObjectRef, get_wield_index),
1909         luamethod(ObjectRef, get_wielded_item),
1910         luamethod(ObjectRef, set_wielded_item),
1911         luamethod(ObjectRef, set_armor_groups),
1912         luamethod(ObjectRef, get_armor_groups),
1913         luamethod(ObjectRef, set_animation),
1914         luamethod(ObjectRef, get_animation),
1915         luamethod(ObjectRef, set_animation_frame_speed),
1916         luamethod(ObjectRef, set_bone_position),
1917         luamethod(ObjectRef, get_bone_position),
1918         luamethod(ObjectRef, set_attach),
1919         luamethod(ObjectRef, get_attach),
1920         luamethod(ObjectRef, set_detach),
1921         luamethod(ObjectRef, set_properties),
1922         luamethod(ObjectRef, get_properties),
1923         luamethod(ObjectRef, set_nametag_attributes),
1924         luamethod(ObjectRef, get_nametag_attributes),
1925         // LuaEntitySAO-only
1926         luamethod_aliased(ObjectRef, set_velocity, setvelocity),
1927         luamethod(ObjectRef, add_velocity),
1928         luamethod_aliased(ObjectRef, get_velocity, getvelocity),
1929         luamethod_aliased(ObjectRef, set_acceleration, setacceleration),
1930         luamethod_aliased(ObjectRef, get_acceleration, getacceleration),
1931         luamethod_aliased(ObjectRef, set_yaw, setyaw),
1932         luamethod_aliased(ObjectRef, get_yaw, getyaw),
1933         luamethod(ObjectRef, set_rotation),
1934         luamethod(ObjectRef, get_rotation),
1935         luamethod_aliased(ObjectRef, set_texture_mod, settexturemod),
1936         luamethod_aliased(ObjectRef, set_sprite, setsprite),
1937         luamethod(ObjectRef, get_entity_name),
1938         luamethod(ObjectRef, get_luaentity),
1939         // Player-only
1940         luamethod(ObjectRef, is_player),
1941         luamethod(ObjectRef, is_player_connected),
1942         luamethod(ObjectRef, get_player_name),
1943         luamethod(ObjectRef, get_player_velocity),
1944         luamethod(ObjectRef, get_look_dir),
1945         luamethod(ObjectRef, get_look_pitch),
1946         luamethod(ObjectRef, get_look_yaw),
1947         luamethod(ObjectRef, get_look_vertical),
1948         luamethod(ObjectRef, get_look_horizontal),
1949         luamethod(ObjectRef, set_look_horizontal),
1950         luamethod(ObjectRef, set_look_vertical),
1951         luamethod(ObjectRef, set_look_yaw),
1952         luamethod(ObjectRef, set_look_pitch),
1953         luamethod(ObjectRef, get_breath),
1954         luamethod(ObjectRef, set_breath),
1955         luamethod(ObjectRef, get_attribute),
1956         luamethod(ObjectRef, set_attribute),
1957         luamethod(ObjectRef, get_meta),
1958         luamethod(ObjectRef, set_inventory_formspec),
1959         luamethod(ObjectRef, get_inventory_formspec),
1960         luamethod(ObjectRef, set_formspec_prepend),
1961         luamethod(ObjectRef, get_formspec_prepend),
1962         luamethod(ObjectRef, get_player_control),
1963         luamethod(ObjectRef, get_player_control_bits),
1964         luamethod(ObjectRef, set_physics_override),
1965         luamethod(ObjectRef, get_physics_override),
1966         luamethod(ObjectRef, hud_add),
1967         luamethod(ObjectRef, hud_remove),
1968         luamethod(ObjectRef, hud_change),
1969         luamethod(ObjectRef, hud_get),
1970         luamethod(ObjectRef, hud_set_flags),
1971         luamethod(ObjectRef, hud_get_flags),
1972         luamethod(ObjectRef, hud_set_hotbar_itemcount),
1973         luamethod(ObjectRef, hud_get_hotbar_itemcount),
1974         luamethod(ObjectRef, hud_set_hotbar_image),
1975         luamethod(ObjectRef, hud_get_hotbar_image),
1976         luamethod(ObjectRef, hud_set_hotbar_selected_image),
1977         luamethod(ObjectRef, hud_get_hotbar_selected_image),
1978         luamethod(ObjectRef, set_sky),
1979         luamethod(ObjectRef, get_sky),
1980         luamethod(ObjectRef, set_clouds),
1981         luamethod(ObjectRef, get_clouds),
1982         luamethod(ObjectRef, override_day_night_ratio),
1983         luamethod(ObjectRef, get_day_night_ratio),
1984         luamethod(ObjectRef, set_local_animation),
1985         luamethod(ObjectRef, get_local_animation),
1986         luamethod(ObjectRef, set_eye_offset),
1987         luamethod(ObjectRef, get_eye_offset),
1988         luamethod(ObjectRef, send_mapblock),
1989         {0,0}
1990 };