]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_object.cpp
guiConfirmRegistration: Set focus to text field (#8761)
[dragonfireclient.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, co, prop, getServer(L)->idef());
761         co->notifyObjectPropertiesModified();
762         return 0;
763 }
764
765 // get_properties(self)
766 int ObjectRef::l_get_properties(lua_State *L)
767 {
768         NO_MAP_LOCK_REQUIRED;
769         ObjectRef *ref = checkobject(L, 1);
770         ServerActiveObject *co = getobject(ref);
771         if (co == NULL)
772                 return 0;
773         ObjectProperties *prop = co->accessObjectProperties();
774         if (!prop)
775                 return 0;
776         push_object_properties(L, prop);
777         return 1;
778 }
779
780 // is_player(self)
781 int ObjectRef::l_is_player(lua_State *L)
782 {
783         NO_MAP_LOCK_REQUIRED;
784         ObjectRef *ref = checkobject(L, 1);
785         RemotePlayer *player = getplayer(ref);
786         lua_pushboolean(L, (player != NULL));
787         return 1;
788 }
789
790 // set_nametag_attributes(self, attributes)
791 int ObjectRef::l_set_nametag_attributes(lua_State *L)
792 {
793         NO_MAP_LOCK_REQUIRED;
794         ObjectRef *ref = checkobject(L, 1);
795         ServerActiveObject *co = getobject(ref);
796
797         if (co == NULL)
798                 return 0;
799         ObjectProperties *prop = co->accessObjectProperties();
800         if (!prop)
801                 return 0;
802
803         lua_getfield(L, 2, "color");
804         if (!lua_isnil(L, -1)) {
805                 video::SColor color = prop->nametag_color;
806                 read_color(L, -1, &color);
807                 prop->nametag_color = color;
808         }
809         lua_pop(L, 1);
810
811         std::string nametag = getstringfield_default(L, 2, "text", "");
812         prop->nametag = nametag;
813
814         co->notifyObjectPropertiesModified();
815         lua_pushboolean(L, true);
816         return 1;
817 }
818
819 // get_nametag_attributes(self)
820 int ObjectRef::l_get_nametag_attributes(lua_State *L)
821 {
822         NO_MAP_LOCK_REQUIRED;
823         ObjectRef *ref = checkobject(L, 1);
824         ServerActiveObject *co = getobject(ref);
825
826         if (co == NULL)
827                 return 0;
828         ObjectProperties *prop = co->accessObjectProperties();
829         if (!prop)
830                 return 0;
831
832         video::SColor color = prop->nametag_color;
833
834         lua_newtable(L);
835         push_ARGB8(L, color);
836         lua_setfield(L, -2, "color");
837         lua_pushstring(L, prop->nametag.c_str());
838         lua_setfield(L, -2, "text");
839         return 1;
840 }
841
842 /* LuaEntitySAO-only */
843
844 // set_velocity(self, {x=num, y=num, z=num})
845 int ObjectRef::l_set_velocity(lua_State *L)
846 {
847         NO_MAP_LOCK_REQUIRED;
848         ObjectRef *ref = checkobject(L, 1);
849         LuaEntitySAO *co = getluaobject(ref);
850         if (co == NULL) return 0;
851         v3f pos = checkFloatPos(L, 2);
852         // Do it
853         co->setVelocity(pos);
854         return 0;
855 }
856
857 // add_velocity(self, {x=num, y=num, z=num})
858 int ObjectRef::l_add_velocity(lua_State *L)
859 {
860         NO_MAP_LOCK_REQUIRED;
861         ObjectRef *ref = checkobject(L, 1);
862         LuaEntitySAO *co = getluaobject(ref);
863         if (!co)
864                 return 0;
865         v3f pos = checkFloatPos(L, 2);
866         // Do it
867         co->addVelocity(pos);
868         return 0;
869 }
870
871 // get_velocity(self)
872 int ObjectRef::l_get_velocity(lua_State *L)
873 {
874         NO_MAP_LOCK_REQUIRED;
875         ObjectRef *ref = checkobject(L, 1);
876         LuaEntitySAO *co = getluaobject(ref);
877         if (co == NULL) return 0;
878         // Do it
879         v3f v = co->getVelocity();
880         pushFloatPos(L, v);
881         return 1;
882 }
883
884 // set_acceleration(self, {x=num, y=num, z=num})
885 int ObjectRef::l_set_acceleration(lua_State *L)
886 {
887         NO_MAP_LOCK_REQUIRED;
888         ObjectRef *ref = checkobject(L, 1);
889         LuaEntitySAO *co = getluaobject(ref);
890         if (co == NULL) return 0;
891         // pos
892         v3f pos = checkFloatPos(L, 2);
893         // Do it
894         co->setAcceleration(pos);
895         return 0;
896 }
897
898 // get_acceleration(self)
899 int ObjectRef::l_get_acceleration(lua_State *L)
900 {
901         NO_MAP_LOCK_REQUIRED;
902         ObjectRef *ref = checkobject(L, 1);
903         LuaEntitySAO *co = getluaobject(ref);
904         if (co == NULL) return 0;
905         // Do it
906         v3f v = co->getAcceleration();
907         pushFloatPos(L, v);
908         return 1;
909 }
910
911 // set_rotation(self, {x=num, y=num, z=num})
912 // Each 'num' is in radians
913 int ObjectRef::l_set_rotation(lua_State *L)
914 {
915         NO_MAP_LOCK_REQUIRED;
916         ObjectRef *ref = checkobject(L, 1);
917         LuaEntitySAO *co = getluaobject(ref);
918         if (!co)
919                 return 0;
920
921         v3f rotation = check_v3f(L, 2) * core::RADTODEG;
922         co->setRotation(rotation);
923         return 0;
924 }
925
926 // get_rotation(self)
927 // returns: {x=num, y=num, z=num}
928 // Each 'num' is in radians
929 int ObjectRef::l_get_rotation(lua_State *L)
930 {
931         NO_MAP_LOCK_REQUIRED;
932         ObjectRef *ref = checkobject(L, 1);
933         LuaEntitySAO *co = getluaobject(ref);
934         if (!co)
935                 return 0;
936
937         lua_newtable(L);
938         v3f rotation = co->getRotation() * core::DEGTORAD;
939         push_v3f(L, rotation);
940         return 1;
941 }
942
943 // set_yaw(self, radians)
944 int ObjectRef::l_set_yaw(lua_State *L)
945 {
946         NO_MAP_LOCK_REQUIRED;
947         ObjectRef *ref = checkobject(L, 1);
948         LuaEntitySAO *co = getluaobject(ref);
949
950         if (co == NULL) return 0;
951         if (isNaN(L, 2))
952                 throw LuaError("ObjectRef::set_yaw: NaN value is not allowed.");
953
954         float yaw = readParam<float>(L, 2) * core::RADTODEG;
955         co->setRotation(v3f(0, yaw, 0));
956         return 0;
957 }
958
959 // get_yaw(self)
960 int ObjectRef::l_get_yaw(lua_State *L)
961 {
962         NO_MAP_LOCK_REQUIRED;
963         ObjectRef *ref = checkobject(L, 1);
964         LuaEntitySAO *co = getluaobject(ref);
965         if (!co)
966                 return 0;
967
968         float yaw = co->getRotation().Y * core::DEGTORAD;
969         lua_pushnumber(L, yaw);
970         return 1;
971 }
972
973 // set_texture_mod(self, mod)
974 int ObjectRef::l_set_texture_mod(lua_State *L)
975 {
976         NO_MAP_LOCK_REQUIRED;
977         ObjectRef *ref = checkobject(L, 1);
978         LuaEntitySAO *co = getluaobject(ref);
979         if (co == NULL) return 0;
980         // Do it
981         std::string mod = luaL_checkstring(L, 2);
982         co->setTextureMod(mod);
983         return 0;
984 }
985
986 // get_texture_mod(self)
987 int ObjectRef::l_get_texture_mod(lua_State *L)
988 {
989         NO_MAP_LOCK_REQUIRED;
990         ObjectRef *ref = checkobject(L, 1);
991         LuaEntitySAO *co = getluaobject(ref);
992         if (co == NULL) return 0;
993         // Do it
994         std::string mod = co->getTextureMod();
995         lua_pushstring(L, mod.c_str());
996         return 1;
997 }
998
999 // set_sprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
1000 //           select_horiz_by_yawpitch=false)
1001 int ObjectRef::l_set_sprite(lua_State *L)
1002 {
1003         NO_MAP_LOCK_REQUIRED;
1004         ObjectRef *ref = checkobject(L, 1);
1005         LuaEntitySAO *co = getluaobject(ref);
1006         if (co == NULL) return 0;
1007         // Do it
1008         v2s16 p(0,0);
1009         if (!lua_isnil(L, 2))
1010                 p = readParam<v2s16>(L, 2);
1011         int num_frames = 1;
1012         if (!lua_isnil(L, 3))
1013                 num_frames = lua_tonumber(L, 3);
1014         float framelength = 0.2;
1015         if (!lua_isnil(L, 4))
1016                 framelength = lua_tonumber(L, 4);
1017         bool select_horiz_by_yawpitch = false;
1018         if (!lua_isnil(L, 5))
1019                 select_horiz_by_yawpitch = readParam<bool>(L, 5);
1020         co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
1021         return 0;
1022 }
1023
1024 // DEPRECATED
1025 // get_entity_name(self)
1026 int ObjectRef::l_get_entity_name(lua_State *L)
1027 {
1028         NO_MAP_LOCK_REQUIRED;
1029         ObjectRef *ref = checkobject(L, 1);
1030         LuaEntitySAO *co = getluaobject(ref);
1031         log_deprecated(L,"Deprecated call to \"get_entity_name");
1032         if (co == NULL) return 0;
1033         // Do it
1034         std::string name = co->getName();
1035         lua_pushstring(L, name.c_str());
1036         return 1;
1037 }
1038
1039 // get_luaentity(self)
1040 int ObjectRef::l_get_luaentity(lua_State *L)
1041 {
1042         NO_MAP_LOCK_REQUIRED;
1043         ObjectRef *ref = checkobject(L, 1);
1044         LuaEntitySAO *co = getluaobject(ref);
1045         if (co == NULL) return 0;
1046         // Do it
1047         luaentity_get(L, co->getId());
1048         return 1;
1049 }
1050
1051 /* Player-only */
1052
1053 // is_player_connected(self)
1054 int ObjectRef::l_is_player_connected(lua_State *L)
1055 {
1056         NO_MAP_LOCK_REQUIRED;
1057         ObjectRef *ref = checkobject(L, 1);
1058         RemotePlayer *player = getplayer(ref);
1059         lua_pushboolean(L, (player != NULL && player->getPeerId() != PEER_ID_INEXISTENT));
1060         return 1;
1061 }
1062
1063 // get_player_name(self)
1064 int ObjectRef::l_get_player_name(lua_State *L)
1065 {
1066         NO_MAP_LOCK_REQUIRED;
1067         ObjectRef *ref = checkobject(L, 1);
1068         RemotePlayer *player = getplayer(ref);
1069         if (player == NULL) {
1070                 lua_pushlstring(L, "", 0);
1071                 return 1;
1072         }
1073         // Do it
1074         lua_pushstring(L, player->getName());
1075         return 1;
1076 }
1077
1078 // get_player_velocity(self)
1079 int ObjectRef::l_get_player_velocity(lua_State *L)
1080 {
1081         NO_MAP_LOCK_REQUIRED;
1082         ObjectRef *ref = checkobject(L, 1);
1083         RemotePlayer *player = getplayer(ref);
1084         if (player == NULL) {
1085                 lua_pushnil(L);
1086                 return 1;
1087         }
1088         // Do it
1089         push_v3f(L, player->getSpeed() / BS);
1090         return 1;
1091 }
1092
1093 // get_look_dir(self)
1094 int ObjectRef::l_get_look_dir(lua_State *L)
1095 {
1096         NO_MAP_LOCK_REQUIRED;
1097         ObjectRef *ref = checkobject(L, 1);
1098         PlayerSAO* co = getplayersao(ref);
1099         if (co == NULL) return 0;
1100         // Do it
1101         float pitch = co->getRadLookPitchDep();
1102         float yaw = co->getRadYawDep();
1103         v3f v(std::cos(pitch) * std::cos(yaw), std::sin(pitch), std::cos(pitch) *
1104                 std::sin(yaw));
1105         push_v3f(L, v);
1106         return 1;
1107 }
1108
1109 // DEPRECATED
1110 // get_look_pitch(self)
1111 int ObjectRef::l_get_look_pitch(lua_State *L)
1112 {
1113         NO_MAP_LOCK_REQUIRED;
1114
1115         log_deprecated(L,
1116                 "Deprecated call to get_look_pitch, use get_look_vertical instead");
1117
1118         ObjectRef *ref = checkobject(L, 1);
1119         PlayerSAO* co = getplayersao(ref);
1120         if (co == NULL) return 0;
1121         // Do it
1122         lua_pushnumber(L, co->getRadLookPitchDep());
1123         return 1;
1124 }
1125
1126 // DEPRECATED
1127 // get_look_yaw(self)
1128 int ObjectRef::l_get_look_yaw(lua_State *L)
1129 {
1130         NO_MAP_LOCK_REQUIRED;
1131
1132         log_deprecated(L,
1133                 "Deprecated call to get_look_yaw, use get_look_horizontal instead");
1134
1135         ObjectRef *ref = checkobject(L, 1);
1136         PlayerSAO* co = getplayersao(ref);
1137         if (co == NULL) return 0;
1138         // Do it
1139         lua_pushnumber(L, co->getRadYawDep());
1140         return 1;
1141 }
1142
1143 // get_look_pitch2(self)
1144 int ObjectRef::l_get_look_vertical(lua_State *L)
1145 {
1146         NO_MAP_LOCK_REQUIRED;
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->getRadLookPitch());
1152         return 1;
1153 }
1154
1155 // get_look_yaw2(self)
1156 int ObjectRef::l_get_look_horizontal(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->getRadRotation().Y);
1164         return 1;
1165 }
1166
1167 // set_look_vertical(self, radians)
1168 int ObjectRef::l_set_look_vertical(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         float pitch = readParam<float>(L, 2) * core::RADTODEG;
1175         // Do it
1176         co->setLookPitchAndSend(pitch);
1177         return 1;
1178 }
1179
1180 // set_look_horizontal(self, radians)
1181 int ObjectRef::l_set_look_horizontal(lua_State *L)
1182 {
1183         NO_MAP_LOCK_REQUIRED;
1184         ObjectRef *ref = checkobject(L, 1);
1185         PlayerSAO* co = getplayersao(ref);
1186         if (co == NULL) return 0;
1187         float yaw = readParam<float>(L, 2) * core::RADTODEG;
1188         // Do it
1189         co->setPlayerYawAndSend(yaw);
1190         return 1;
1191 }
1192
1193 // DEPRECATED
1194 // set_look_pitch(self, radians)
1195 int ObjectRef::l_set_look_pitch(lua_State *L)
1196 {
1197         NO_MAP_LOCK_REQUIRED;
1198
1199         log_deprecated(L,
1200                 "Deprecated call to set_look_pitch, use set_look_vertical instead.");
1201
1202         ObjectRef *ref = checkobject(L, 1);
1203         PlayerSAO* co = getplayersao(ref);
1204         if (co == NULL) return 0;
1205         float pitch = readParam<float>(L, 2) * core::RADTODEG;
1206         // Do it
1207         co->setLookPitchAndSend(pitch);
1208         return 1;
1209 }
1210
1211 // DEPRECATED
1212 // set_look_yaw(self, radians)
1213 int ObjectRef::l_set_look_yaw(lua_State *L)
1214 {
1215         NO_MAP_LOCK_REQUIRED;
1216
1217         log_deprecated(L,
1218                 "Deprecated call to set_look_yaw, use set_look_horizontal instead.");
1219
1220         ObjectRef *ref = checkobject(L, 1);
1221         PlayerSAO* co = getplayersao(ref);
1222         if (co == NULL) return 0;
1223         float yaw = readParam<float>(L, 2) * core::RADTODEG;
1224         // Do it
1225         co->setPlayerYawAndSend(yaw);
1226         return 1;
1227 }
1228
1229 // set_breath(self, breath)
1230 int ObjectRef::l_set_breath(lua_State *L)
1231 {
1232         NO_MAP_LOCK_REQUIRED;
1233         ObjectRef *ref = checkobject(L, 1);
1234         PlayerSAO* co = getplayersao(ref);
1235         if (co == NULL) return 0;
1236         u16 breath = luaL_checknumber(L, 2);
1237         co->setBreath(breath);
1238
1239         return 0;
1240 }
1241
1242 // get_breath(self)
1243 int ObjectRef::l_get_breath(lua_State *L)
1244 {
1245         NO_MAP_LOCK_REQUIRED;
1246         ObjectRef *ref = checkobject(L, 1);
1247         PlayerSAO* co = getplayersao(ref);
1248         if (co == NULL) return 0;
1249         // Do it
1250         u16 breath = co->getBreath();
1251         lua_pushinteger (L, breath);
1252         return 1;
1253 }
1254
1255 // set_attribute(self, attribute, value)
1256 int ObjectRef::l_set_attribute(lua_State *L)
1257 {
1258         log_deprecated(L,
1259                 "Deprecated call to set_attribute, use MetaDataRef methods instead.");
1260
1261         ObjectRef *ref = checkobject(L, 1);
1262         PlayerSAO* co = getplayersao(ref);
1263         if (co == NULL)
1264                 return 0;
1265
1266         std::string attr = luaL_checkstring(L, 2);
1267         if (lua_isnil(L, 3)) {
1268                 co->getMeta().removeString(attr);
1269         } else {
1270                 std::string value = luaL_checkstring(L, 3);
1271                 co->getMeta().setString(attr, value);
1272         }
1273         return 1;
1274 }
1275
1276 // get_attribute(self, attribute)
1277 int ObjectRef::l_get_attribute(lua_State *L)
1278 {
1279         log_deprecated(L,
1280                 "Deprecated call to get_attribute, use MetaDataRef methods instead.");
1281
1282         ObjectRef *ref = checkobject(L, 1);
1283         PlayerSAO* co = getplayersao(ref);
1284         if (co == NULL)
1285                 return 0;
1286
1287         std::string attr = luaL_checkstring(L, 2);
1288
1289         std::string value;
1290         if (co->getMeta().getStringToRef(attr, value)) {
1291                 lua_pushstring(L, value.c_str());
1292                 return 1;
1293         }
1294
1295         return 0;
1296 }
1297
1298
1299 // get_meta(self, attribute)
1300 int ObjectRef::l_get_meta(lua_State *L)
1301 {
1302         ObjectRef *ref = checkobject(L, 1);
1303         PlayerSAO *co = getplayersao(ref);
1304         if (co == NULL)
1305                 return 0;
1306
1307         PlayerMetaRef::create(L, &co->getMeta());
1308         return 1;
1309 }
1310
1311
1312 // set_inventory_formspec(self, formspec)
1313 int ObjectRef::l_set_inventory_formspec(lua_State *L)
1314 {
1315         NO_MAP_LOCK_REQUIRED;
1316         ObjectRef *ref = checkobject(L, 1);
1317         RemotePlayer *player = getplayer(ref);
1318         if (player == NULL) return 0;
1319         std::string formspec = luaL_checkstring(L, 2);
1320
1321         player->inventory_formspec = formspec;
1322         getServer(L)->reportInventoryFormspecModified(player->getName());
1323         lua_pushboolean(L, true);
1324         return 1;
1325 }
1326
1327 // get_inventory_formspec(self) -> formspec
1328 int ObjectRef::l_get_inventory_formspec(lua_State *L)
1329 {
1330         NO_MAP_LOCK_REQUIRED;
1331         ObjectRef *ref = checkobject(L, 1);
1332         RemotePlayer *player = getplayer(ref);
1333         if (player == NULL) return 0;
1334
1335         std::string formspec = player->inventory_formspec;
1336         lua_pushlstring(L, formspec.c_str(), formspec.size());
1337         return 1;
1338 }
1339
1340 // set_formspec_prepend(self, formspec)
1341 int ObjectRef::l_set_formspec_prepend(lua_State *L)
1342 {
1343         NO_MAP_LOCK_REQUIRED;
1344         ObjectRef *ref = checkobject(L, 1);
1345         RemotePlayer *player = getplayer(ref);
1346         if (player == NULL)
1347                 return 0;
1348
1349         std::string formspec = luaL_checkstring(L, 2);
1350
1351         player->formspec_prepend = formspec;
1352         getServer(L)->reportFormspecPrependModified(player->getName());
1353         lua_pushboolean(L, true);
1354         return 1;
1355 }
1356
1357 // get_formspec_prepend(self) -> formspec
1358 int ObjectRef::l_get_formspec_prepend(lua_State *L)
1359 {
1360         NO_MAP_LOCK_REQUIRED;
1361         ObjectRef *ref = checkobject(L, 1);
1362         RemotePlayer *player = getplayer(ref);
1363         if (player == NULL)
1364                  return 0;
1365
1366         std::string formspec = player->formspec_prepend;
1367         lua_pushlstring(L, formspec.c_str(), formspec.size());
1368         return 1;
1369 }
1370
1371 // get_player_control(self)
1372 int ObjectRef::l_get_player_control(lua_State *L)
1373 {
1374         NO_MAP_LOCK_REQUIRED;
1375         ObjectRef *ref = checkobject(L, 1);
1376         RemotePlayer *player = getplayer(ref);
1377         if (player == NULL) {
1378                 lua_pushlstring(L, "", 0);
1379                 return 1;
1380         }
1381
1382         const PlayerControl &control = player->getPlayerControl();
1383         lua_newtable(L);
1384         lua_pushboolean(L, control.up);
1385         lua_setfield(L, -2, "up");
1386         lua_pushboolean(L, control.down);
1387         lua_setfield(L, -2, "down");
1388         lua_pushboolean(L, control.left);
1389         lua_setfield(L, -2, "left");
1390         lua_pushboolean(L, control.right);
1391         lua_setfield(L, -2, "right");
1392         lua_pushboolean(L, control.jump);
1393         lua_setfield(L, -2, "jump");
1394         lua_pushboolean(L, control.aux1);
1395         lua_setfield(L, -2, "aux1");
1396         lua_pushboolean(L, control.sneak);
1397         lua_setfield(L, -2, "sneak");
1398         lua_pushboolean(L, control.LMB);
1399         lua_setfield(L, -2, "LMB");
1400         lua_pushboolean(L, control.RMB);
1401         lua_setfield(L, -2, "RMB");
1402         return 1;
1403 }
1404
1405 // get_player_control_bits(self)
1406 int ObjectRef::l_get_player_control_bits(lua_State *L)
1407 {
1408         NO_MAP_LOCK_REQUIRED;
1409         ObjectRef *ref = checkobject(L, 1);
1410         RemotePlayer *player = getplayer(ref);
1411         if (player == NULL) {
1412                 lua_pushlstring(L, "", 0);
1413                 return 1;
1414         }
1415         // Do it
1416         lua_pushnumber(L, player->keyPressed);
1417         return 1;
1418 }
1419
1420 // hud_add(self, form)
1421 int ObjectRef::l_hud_add(lua_State *L)
1422 {
1423         NO_MAP_LOCK_REQUIRED;
1424         ObjectRef *ref = checkobject(L, 1);
1425         RemotePlayer *player = getplayer(ref);
1426         if (player == NULL)
1427                 return 0;
1428
1429         HudElement *elem = new HudElement;
1430         read_hud_element(L, elem);
1431
1432         u32 id = getServer(L)->hudAdd(player, elem);
1433         if (id == U32_MAX) {
1434                 delete elem;
1435                 return 0;
1436         }
1437
1438         lua_pushnumber(L, id);
1439         return 1;
1440 }
1441
1442 // hud_remove(self, id)
1443 int ObjectRef::l_hud_remove(lua_State *L)
1444 {
1445         NO_MAP_LOCK_REQUIRED;
1446         ObjectRef *ref = checkobject(L, 1);
1447         RemotePlayer *player = getplayer(ref);
1448         if (player == NULL)
1449                 return 0;
1450
1451         u32 id = -1;
1452         if (!lua_isnil(L, 2))
1453                 id = lua_tonumber(L, 2);
1454
1455         if (!getServer(L)->hudRemove(player, id))
1456                 return 0;
1457
1458         lua_pushboolean(L, true);
1459         return 1;
1460 }
1461
1462 // hud_change(self, id, stat, data)
1463 int ObjectRef::l_hud_change(lua_State *L)
1464 {
1465         NO_MAP_LOCK_REQUIRED;
1466         ObjectRef *ref = checkobject(L, 1);
1467         RemotePlayer *player = getplayer(ref);
1468         if (player == NULL)
1469                 return 0;
1470
1471         u32 id = lua_isnumber(L, 2) ? lua_tonumber(L, 2) : -1;
1472
1473         HudElement *e = player->getHud(id);
1474         if (!e)
1475                 return 0;
1476
1477         void *value = NULL;
1478         HudElementStat stat = read_hud_change(L, e, &value);
1479
1480         getServer(L)->hudChange(player, id, stat, value);
1481
1482         lua_pushboolean(L, true);
1483         return 1;
1484 }
1485
1486 // hud_get(self, id)
1487 int ObjectRef::l_hud_get(lua_State *L)
1488 {
1489         NO_MAP_LOCK_REQUIRED;
1490         ObjectRef *ref = checkobject(L, 1);
1491         RemotePlayer *player = getplayer(ref);
1492         if (player == NULL)
1493                 return 0;
1494
1495         u32 id = lua_tonumber(L, -1);
1496
1497         HudElement *e = player->getHud(id);
1498         if (!e)
1499                 return 0;
1500         push_hud_element(L, e);
1501         return 1;
1502 }
1503
1504 // hud_set_flags(self, flags)
1505 int ObjectRef::l_hud_set_flags(lua_State *L)
1506 {
1507         NO_MAP_LOCK_REQUIRED;
1508         ObjectRef *ref = checkobject(L, 1);
1509         RemotePlayer *player = getplayer(ref);
1510         if (player == NULL)
1511                 return 0;
1512
1513         u32 flags = 0;
1514         u32 mask  = 0;
1515         bool flag;
1516
1517         const EnumString *esp = es_HudBuiltinElement;
1518         for (int i = 0; esp[i].str; i++) {
1519                 if (getboolfield(L, 2, esp[i].str, flag)) {
1520                         flags |= esp[i].num * flag;
1521                         mask  |= esp[i].num;
1522                 }
1523         }
1524         if (!getServer(L)->hudSetFlags(player, flags, mask))
1525                 return 0;
1526
1527         lua_pushboolean(L, true);
1528         return 1;
1529 }
1530
1531 int ObjectRef::l_hud_get_flags(lua_State *L)
1532 {
1533         NO_MAP_LOCK_REQUIRED;
1534         ObjectRef *ref = checkobject(L, 1);
1535         RemotePlayer *player = getplayer(ref);
1536         if (player == NULL)
1537                 return 0;
1538
1539         lua_newtable(L);
1540         lua_pushboolean(L, player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE);
1541         lua_setfield(L, -2, "hotbar");
1542         lua_pushboolean(L, player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE);
1543         lua_setfield(L, -2, "healthbar");
1544         lua_pushboolean(L, player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE);
1545         lua_setfield(L, -2, "crosshair");
1546         lua_pushboolean(L, player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE);
1547         lua_setfield(L, -2, "wielditem");
1548         lua_pushboolean(L, player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE);
1549         lua_setfield(L, -2, "breathbar");
1550         lua_pushboolean(L, player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE);
1551         lua_setfield(L, -2, "minimap");
1552         lua_pushboolean(L, player->hud_flags & HUD_FLAG_MINIMAP_RADAR_VISIBLE);
1553         lua_setfield(L, -2, "minimap_radar");
1554
1555         return 1;
1556 }
1557
1558 // hud_set_hotbar_itemcount(self, hotbar_itemcount)
1559 int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L)
1560 {
1561         NO_MAP_LOCK_REQUIRED;
1562         ObjectRef *ref = checkobject(L, 1);
1563         RemotePlayer *player = getplayer(ref);
1564         if (player == NULL)
1565                 return 0;
1566
1567         s32 hotbar_itemcount = lua_tonumber(L, 2);
1568
1569         if (!getServer(L)->hudSetHotbarItemcount(player, hotbar_itemcount))
1570                 return 0;
1571
1572         lua_pushboolean(L, true);
1573         return 1;
1574 }
1575
1576 // hud_get_hotbar_itemcount(self)
1577 int ObjectRef::l_hud_get_hotbar_itemcount(lua_State *L)
1578 {
1579         NO_MAP_LOCK_REQUIRED;
1580         ObjectRef *ref = checkobject(L, 1);
1581         RemotePlayer *player = getplayer(ref);
1582         if (player == NULL)
1583                 return 0;
1584
1585         lua_pushnumber(L, player->getHotbarItemcount());
1586         return 1;
1587 }
1588
1589 // hud_set_hotbar_image(self, name)
1590 int ObjectRef::l_hud_set_hotbar_image(lua_State *L)
1591 {
1592         NO_MAP_LOCK_REQUIRED;
1593         ObjectRef *ref = checkobject(L, 1);
1594         RemotePlayer *player = getplayer(ref);
1595         if (player == NULL)
1596                 return 0;
1597
1598         std::string name = readParam<std::string>(L, 2);
1599
1600         getServer(L)->hudSetHotbarImage(player, name);
1601         return 1;
1602 }
1603
1604 // hud_get_hotbar_image(self)
1605 int ObjectRef::l_hud_get_hotbar_image(lua_State *L)
1606 {
1607         NO_MAP_LOCK_REQUIRED;
1608         ObjectRef *ref = checkobject(L, 1);
1609         RemotePlayer *player = getplayer(ref);
1610         if (player == NULL)
1611                 return 0;
1612
1613         const std::string &name = player->getHotbarImage();
1614         lua_pushlstring(L, name.c_str(), name.size());
1615         return 1;
1616 }
1617
1618 // hud_set_hotbar_selected_image(self, name)
1619 int ObjectRef::l_hud_set_hotbar_selected_image(lua_State *L)
1620 {
1621         NO_MAP_LOCK_REQUIRED;
1622         ObjectRef *ref = checkobject(L, 1);
1623         RemotePlayer *player = getplayer(ref);
1624         if (player == NULL)
1625                 return 0;
1626
1627         std::string name = readParam<std::string>(L, 2);
1628
1629         getServer(L)->hudSetHotbarSelectedImage(player, name);
1630         return 1;
1631 }
1632
1633 // hud_get_hotbar_selected_image(self)
1634 int ObjectRef::l_hud_get_hotbar_selected_image(lua_State *L)
1635 {
1636         NO_MAP_LOCK_REQUIRED;
1637         ObjectRef *ref = checkobject(L, 1);
1638         RemotePlayer *player = getplayer(ref);
1639         if (player == NULL)
1640                 return 0;
1641
1642         const std::string &name = player->getHotbarSelectedImage();
1643         lua_pushlstring(L, name.c_str(), name.size());
1644         return 1;
1645 }
1646
1647 // set_sky(self, bgcolor, type, list, clouds = true)
1648 int ObjectRef::l_set_sky(lua_State *L)
1649 {
1650         NO_MAP_LOCK_REQUIRED;
1651         ObjectRef *ref = checkobject(L, 1);
1652         RemotePlayer *player = getplayer(ref);
1653         if (player == NULL)
1654                 return 0;
1655
1656         video::SColor bgcolor(255,255,255,255);
1657         read_color(L, 2, &bgcolor);
1658
1659         std::string type = luaL_checkstring(L, 3);
1660
1661         std::vector<std::string> params;
1662         if (lua_istable(L, 4)) {
1663                 lua_pushnil(L);
1664                 while (lua_next(L, 4) != 0) {
1665                         // key at index -2 and value at index -1
1666                         if (lua_isstring(L, -1))
1667                                 params.emplace_back(readParam<std::string>(L, -1));
1668                         else
1669                                 params.emplace_back("");
1670                         // removes value, keeps key for next iteration
1671                         lua_pop(L, 1);
1672                 }
1673         }
1674
1675         if (type == "skybox" && params.size() != 6)
1676                 throw LuaError("skybox expects 6 textures");
1677
1678         bool clouds = true;
1679         if (lua_isboolean(L, 5))
1680                 clouds = readParam<bool>(L, 5);
1681
1682         getServer(L)->setSky(player, bgcolor, type, params, clouds);
1683         lua_pushboolean(L, true);
1684         return 1;
1685 }
1686
1687 // get_sky(self)
1688 int ObjectRef::l_get_sky(lua_State *L)
1689 {
1690         NO_MAP_LOCK_REQUIRED;
1691         ObjectRef *ref = checkobject(L, 1);
1692         RemotePlayer *player = getplayer(ref);
1693         if (player == NULL)
1694                 return 0;
1695         video::SColor bgcolor(255, 255, 255, 255);
1696         std::string type;
1697         std::vector<std::string> params;
1698         bool clouds;
1699
1700         player->getSky(&bgcolor, &type, &params, &clouds);
1701         type = type.empty() ? "regular" : type;
1702
1703         push_ARGB8(L, bgcolor);
1704         lua_pushlstring(L, type.c_str(), type.size());
1705         lua_newtable(L);
1706         s16 i = 1;
1707         for (const std::string &param : params) {
1708                 lua_pushlstring(L, param.c_str(), param.size());
1709                 lua_rawseti(L, -2, i++);
1710         }
1711         lua_pushboolean(L, clouds);
1712         return 4;
1713 }
1714
1715 // set_clouds(self, {density=, color=, ambient=, height=, thickness=, speed=})
1716 int ObjectRef::l_set_clouds(lua_State *L)
1717 {
1718         NO_MAP_LOCK_REQUIRED;
1719         ObjectRef *ref = checkobject(L, 1);
1720         RemotePlayer *player = getplayer(ref);
1721         if (!player)
1722                 return 0;
1723         if (!lua_istable(L, 2))
1724                 return 0;
1725
1726         CloudParams cloud_params = player->getCloudParams();
1727
1728         cloud_params.density = getfloatfield_default(L, 2, "density", cloud_params.density);
1729
1730         lua_getfield(L, 2, "color");
1731         if (!lua_isnil(L, -1))
1732                 read_color(L, -1, &cloud_params.color_bright);
1733         lua_pop(L, 1);
1734         lua_getfield(L, 2, "ambient");
1735         if (!lua_isnil(L, -1))
1736                 read_color(L, -1, &cloud_params.color_ambient);
1737         lua_pop(L, 1);
1738
1739         cloud_params.height    = getfloatfield_default(L, 2, "height",    cloud_params.height   );
1740         cloud_params.thickness = getfloatfield_default(L, 2, "thickness", cloud_params.thickness);
1741
1742         lua_getfield(L, 2, "speed");
1743         if (lua_istable(L, -1)) {
1744                 v2f new_speed;
1745                 new_speed.X = getfloatfield_default(L, -1, "x", 0);
1746                 new_speed.Y = getfloatfield_default(L, -1, "z", 0);
1747                 cloud_params.speed = new_speed;
1748         }
1749         lua_pop(L, 1);
1750
1751         getServer(L)->setClouds(player, cloud_params);
1752         lua_pushboolean(L, true);
1753         return 1;
1754 }
1755
1756 int ObjectRef::l_get_clouds(lua_State *L)
1757 {
1758         NO_MAP_LOCK_REQUIRED;
1759         ObjectRef *ref = checkobject(L, 1);
1760         RemotePlayer *player = getplayer(ref);
1761         if (!player)
1762                 return 0;
1763         const CloudParams &cloud_params = player->getCloudParams();
1764
1765         lua_newtable(L);
1766         lua_pushnumber(L, cloud_params.density);
1767         lua_setfield(L, -2, "density");
1768         push_ARGB8(L, cloud_params.color_bright);
1769         lua_setfield(L, -2, "color");
1770         push_ARGB8(L, cloud_params.color_ambient);
1771         lua_setfield(L, -2, "ambient");
1772         lua_pushnumber(L, cloud_params.height);
1773         lua_setfield(L, -2, "height");
1774         lua_pushnumber(L, cloud_params.thickness);
1775         lua_setfield(L, -2, "thickness");
1776         lua_newtable(L);
1777         lua_pushnumber(L, cloud_params.speed.X);
1778         lua_setfield(L, -2, "x");
1779         lua_pushnumber(L, cloud_params.speed.Y);
1780         lua_setfield(L, -2, "y");
1781         lua_setfield(L, -2, "speed");
1782
1783         return 1;
1784 }
1785
1786
1787 // override_day_night_ratio(self, brightness=0...1)
1788 int ObjectRef::l_override_day_night_ratio(lua_State *L)
1789 {
1790         NO_MAP_LOCK_REQUIRED;
1791         ObjectRef *ref = checkobject(L, 1);
1792         RemotePlayer *player = getplayer(ref);
1793         if (player == NULL)
1794                 return 0;
1795
1796         bool do_override = false;
1797         float ratio = 0.0f;
1798         if (!lua_isnil(L, 2)) {
1799                 do_override = true;
1800                 ratio = readParam<float>(L, 2);
1801         }
1802
1803         if (!getServer(L)->overrideDayNightRatio(player, do_override, ratio))
1804                 return 0;
1805
1806         lua_pushboolean(L, true);
1807         return 1;
1808 }
1809
1810 // get_day_night_ratio(self)
1811 int ObjectRef::l_get_day_night_ratio(lua_State *L)
1812 {
1813         NO_MAP_LOCK_REQUIRED;
1814         ObjectRef *ref = checkobject(L, 1);
1815         RemotePlayer *player = getplayer(ref);
1816         if (player == NULL)
1817                 return 0;
1818
1819         bool do_override;
1820         float ratio;
1821         player->getDayNightRatio(&do_override, &ratio);
1822
1823         if (do_override)
1824                 lua_pushnumber(L, ratio);
1825         else
1826                 lua_pushnil(L);
1827
1828         return 1;
1829 }
1830
1831 ObjectRef::ObjectRef(ServerActiveObject *object):
1832         m_object(object)
1833 {
1834         //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
1835 }
1836
1837 // Creates an ObjectRef and leaves it on top of stack
1838 // Not callable from Lua; all references are created on the C side.
1839 void ObjectRef::create(lua_State *L, ServerActiveObject *object)
1840 {
1841         ObjectRef *o = new ObjectRef(object);
1842         //infostream<<"ObjectRef::create: o="<<o<<std::endl;
1843         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1844         luaL_getmetatable(L, className);
1845         lua_setmetatable(L, -2);
1846 }
1847
1848 void ObjectRef::set_null(lua_State *L)
1849 {
1850         ObjectRef *o = checkobject(L, -1);
1851         o->m_object = NULL;
1852 }
1853
1854 void ObjectRef::Register(lua_State *L)
1855 {
1856         lua_newtable(L);
1857         int methodtable = lua_gettop(L);
1858         luaL_newmetatable(L, className);
1859         int metatable = lua_gettop(L);
1860
1861         lua_pushliteral(L, "__metatable");
1862         lua_pushvalue(L, methodtable);
1863         lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
1864
1865         lua_pushliteral(L, "__index");
1866         lua_pushvalue(L, methodtable);
1867         lua_settable(L, metatable);
1868
1869         lua_pushliteral(L, "__gc");
1870         lua_pushcfunction(L, gc_object);
1871         lua_settable(L, metatable);
1872
1873         lua_pop(L, 1);  // drop metatable
1874
1875         markAliasDeprecated(methods);
1876         luaL_openlib(L, 0, methods, 0);  // fill methodtable
1877         lua_pop(L, 1);  // drop methodtable
1878
1879         // Cannot be created from Lua
1880         //lua_register(L, className, create_object);
1881 }
1882
1883 const char ObjectRef::className[] = "ObjectRef";
1884 luaL_Reg ObjectRef::methods[] = {
1885         // ServerActiveObject
1886         luamethod(ObjectRef, remove),
1887         luamethod_aliased(ObjectRef, get_pos, getpos),
1888         luamethod_aliased(ObjectRef, set_pos, setpos),
1889         luamethod_aliased(ObjectRef, move_to, moveto),
1890         luamethod(ObjectRef, punch),
1891         luamethod(ObjectRef, right_click),
1892         luamethod(ObjectRef, set_hp),
1893         luamethod(ObjectRef, get_hp),
1894         luamethod(ObjectRef, get_inventory),
1895         luamethod(ObjectRef, get_wield_list),
1896         luamethod(ObjectRef, get_wield_index),
1897         luamethod(ObjectRef, get_wielded_item),
1898         luamethod(ObjectRef, set_wielded_item),
1899         luamethod(ObjectRef, set_armor_groups),
1900         luamethod(ObjectRef, get_armor_groups),
1901         luamethod(ObjectRef, set_animation),
1902         luamethod(ObjectRef, get_animation),
1903         luamethod(ObjectRef, set_animation_frame_speed),
1904         luamethod(ObjectRef, set_bone_position),
1905         luamethod(ObjectRef, get_bone_position),
1906         luamethod(ObjectRef, set_attach),
1907         luamethod(ObjectRef, get_attach),
1908         luamethod(ObjectRef, set_detach),
1909         luamethod(ObjectRef, set_properties),
1910         luamethod(ObjectRef, get_properties),
1911         luamethod(ObjectRef, set_nametag_attributes),
1912         luamethod(ObjectRef, get_nametag_attributes),
1913         // LuaEntitySAO-only
1914         luamethod_aliased(ObjectRef, set_velocity, setvelocity),
1915         luamethod(ObjectRef, add_velocity),
1916         luamethod_aliased(ObjectRef, get_velocity, getvelocity),
1917         luamethod_aliased(ObjectRef, set_acceleration, setacceleration),
1918         luamethod_aliased(ObjectRef, get_acceleration, getacceleration),
1919         luamethod_aliased(ObjectRef, set_yaw, setyaw),
1920         luamethod_aliased(ObjectRef, get_yaw, getyaw),
1921         luamethod(ObjectRef, set_rotation),
1922         luamethod(ObjectRef, get_rotation),
1923         luamethod_aliased(ObjectRef, set_texture_mod, settexturemod),
1924         luamethod_aliased(ObjectRef, set_sprite, setsprite),
1925         luamethod(ObjectRef, get_entity_name),
1926         luamethod(ObjectRef, get_luaentity),
1927         // Player-only
1928         luamethod(ObjectRef, is_player),
1929         luamethod(ObjectRef, is_player_connected),
1930         luamethod(ObjectRef, get_player_name),
1931         luamethod(ObjectRef, get_player_velocity),
1932         luamethod(ObjectRef, get_look_dir),
1933         luamethod(ObjectRef, get_look_pitch),
1934         luamethod(ObjectRef, get_look_yaw),
1935         luamethod(ObjectRef, get_look_vertical),
1936         luamethod(ObjectRef, get_look_horizontal),
1937         luamethod(ObjectRef, set_look_horizontal),
1938         luamethod(ObjectRef, set_look_vertical),
1939         luamethod(ObjectRef, set_look_yaw),
1940         luamethod(ObjectRef, set_look_pitch),
1941         luamethod(ObjectRef, get_breath),
1942         luamethod(ObjectRef, set_breath),
1943         luamethod(ObjectRef, get_attribute),
1944         luamethod(ObjectRef, set_attribute),
1945         luamethod(ObjectRef, get_meta),
1946         luamethod(ObjectRef, set_inventory_formspec),
1947         luamethod(ObjectRef, get_inventory_formspec),
1948         luamethod(ObjectRef, set_formspec_prepend),
1949         luamethod(ObjectRef, get_formspec_prepend),
1950         luamethod(ObjectRef, get_player_control),
1951         luamethod(ObjectRef, get_player_control_bits),
1952         luamethod(ObjectRef, set_physics_override),
1953         luamethod(ObjectRef, get_physics_override),
1954         luamethod(ObjectRef, hud_add),
1955         luamethod(ObjectRef, hud_remove),
1956         luamethod(ObjectRef, hud_change),
1957         luamethod(ObjectRef, hud_get),
1958         luamethod(ObjectRef, hud_set_flags),
1959         luamethod(ObjectRef, hud_get_flags),
1960         luamethod(ObjectRef, hud_set_hotbar_itemcount),
1961         luamethod(ObjectRef, hud_get_hotbar_itemcount),
1962         luamethod(ObjectRef, hud_set_hotbar_image),
1963         luamethod(ObjectRef, hud_get_hotbar_image),
1964         luamethod(ObjectRef, hud_set_hotbar_selected_image),
1965         luamethod(ObjectRef, hud_get_hotbar_selected_image),
1966         luamethod(ObjectRef, set_sky),
1967         luamethod(ObjectRef, get_sky),
1968         luamethod(ObjectRef, set_clouds),
1969         luamethod(ObjectRef, get_clouds),
1970         luamethod(ObjectRef, override_day_night_ratio),
1971         luamethod(ObjectRef, get_day_night_ratio),
1972         luamethod(ObjectRef, set_local_animation),
1973         luamethod(ObjectRef, get_local_animation),
1974         luamethod(ObjectRef, set_eye_offset),
1975         luamethod(ObjectRef, get_eye_offset),
1976         luamethod(ObjectRef, send_mapblock),
1977         {0,0}
1978 };