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