3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "lua_api/l_object.h"
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"
30 #include "serverobject.h"
31 #include "content_sao.h"
32 #include "remoteplayer.h"
35 #include "scripting_server.h"
42 ObjectRef* ObjectRef::checkobject(lua_State *L, int narg)
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
50 ServerActiveObject* ObjectRef::getobject(ObjectRef *ref)
52 ServerActiveObject *co = ref->m_object;
56 LuaEntitySAO* ObjectRef::getluaobject(ObjectRef *ref)
58 ServerActiveObject *obj = getobject(ref);
61 if (obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY)
63 return (LuaEntitySAO*)obj;
66 PlayerSAO* ObjectRef::getplayersao(ObjectRef *ref)
68 ServerActiveObject *obj = getobject(ref);
71 if (obj->getType() != ACTIVEOBJECT_TYPE_PLAYER)
73 return (PlayerSAO*)obj;
76 RemotePlayer *ObjectRef::getplayer(ObjectRef *ref)
78 PlayerSAO *playersao = getplayersao(ref);
79 if (playersao == NULL)
81 return playersao->getPlayer();
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;
95 int ObjectRef::l_remove(lua_State *L)
99 ObjectRef *ref = checkobject(L, 1);
100 ServerActiveObject *co = getobject(ref);
103 if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
106 co->clearChildAttachments();
107 co->clearParentAttachment();
109 verbosestream << "ObjectRef::l_remove(): id=" << co->getId() << std::endl;
110 co->m_pending_removal = true;
115 // returns: {x=num, y=num, z=num}
116 int ObjectRef::l_get_pos(lua_State *L)
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;
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");
133 // set_pos(self, pos)
134 int ObjectRef::l_set_pos(lua_State *L)
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;
142 v3f pos = checkFloatPos(L, 2);
148 // move_to(self, pos, continuous=false)
149 int ObjectRef::l_move_to(lua_State *L)
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;
157 v3f pos = checkFloatPos(L, 2);
159 bool continuous = readParam<bool>(L, 3);
161 co->moveTo(pos, continuous);
165 // punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
166 int ObjectRef::l_punch(lua_State *L)
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);
176 if (lua_type(L, 5) != LUA_TTABLE)
177 dir = co->getBasePosition() - puncher->getBasePosition();
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);
186 u16 src_original_hp = co->getHP();
187 u16 dst_origin_hp = puncher->getHP();
190 co->punch(dir, &toolcap, puncher, time_from_last_punch);
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));
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));
208 // right_click(self, clicker); clicker = an another ObjectRef
209 int ObjectRef::l_right_click(lua_State *L)
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;
224 // hp = number of hitpoints (2 * number of hearts)
226 int ObjectRef::l_set_hp(lua_State *L)
228 NO_MAP_LOCK_REQUIRED;
231 ObjectRef *ref = checkobject(L, 1);
232 luaL_checknumber(L, 2);
233 ServerActiveObject *co = getobject(ref);
238 int hp = lua_tonumber(L, 2);
241 PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
242 reason.from_mod = true;
243 if (lua_istable(L, 3)) {
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;
253 reason.lua_reference = luaL_ref(L, LUA_REGISTRYINDEX);
257 co->setHP(hp, reason);
258 if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
259 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, reason);
261 if (reason.hasLuaReference())
262 luaL_unref(L, LUA_REGISTRYINDEX, reason.lua_reference);
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)
273 NO_MAP_LOCK_REQUIRED;
274 ObjectRef *ref = checkobject(L, 1);
275 ServerActiveObject *co = getobject(ref);
278 lua_pushnumber(L, 1);
281 int hp = co->getHP();
282 /*infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
283 <<" hp="<<hp<<std::endl;*/
285 lua_pushnumber(L, hp);
289 // get_inventory(self)
290 int ObjectRef::l_get_inventory(lua_State *L)
292 NO_MAP_LOCK_REQUIRED;
293 ObjectRef *ref = checkobject(L, 1);
294 ServerActiveObject *co = getobject(ref);
295 if (co == NULL) return 0;
297 InventoryLocation loc = co->getInventoryLocation();
298 if (getServer(L)->getInventory(loc) != NULL)
299 InvRef::create(L, loc);
301 lua_pushnil(L); // An object may have no inventory (nil)
305 // get_wield_list(self)
306 int ObjectRef::l_get_wield_list(lua_State *L)
308 NO_MAP_LOCK_REQUIRED;
309 ObjectRef *ref = checkobject(L, 1);
310 ServerActiveObject *co = getobject(ref);
311 if (co == NULL) return 0;
313 lua_pushstring(L, co->getWieldList().c_str());
317 // get_wield_index(self)
318 int ObjectRef::l_get_wield_index(lua_State *L)
320 NO_MAP_LOCK_REQUIRED;
321 ObjectRef *ref = checkobject(L, 1);
322 ServerActiveObject *co = getobject(ref);
323 if (co == NULL) return 0;
325 lua_pushinteger(L, co->getWieldIndex() + 1);
329 // get_wielded_item(self)
330 int ObjectRef::l_get_wielded_item(lua_State *L)
332 NO_MAP_LOCK_REQUIRED;
333 ObjectRef *ref = checkobject(L, 1);
334 ServerActiveObject *co = getobject(ref);
337 LuaItemStack::create(L, ItemStack());
341 LuaItemStack::create(L, co->getWieldedItem());
345 // set_wielded_item(self, itemstack or itemstring or table or nil)
346 int ObjectRef::l_set_wielded_item(lua_State *L)
348 NO_MAP_LOCK_REQUIRED;
349 ObjectRef *ref = checkobject(L, 1);
350 ServerActiveObject *co = getobject(ref);
351 if (co == NULL) return 0;
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));
358 lua_pushboolean(L, success);
362 // set_armor_groups(self, groups)
363 int ObjectRef::l_set_armor_groups(lua_State *L)
365 NO_MAP_LOCK_REQUIRED;
366 ObjectRef *ref = checkobject(L, 1);
367 ServerActiveObject *co = getobject(ref);
368 if (co == NULL) return 0;
370 ItemGroupList groups;
371 read_groups(L, 2, groups);
372 co->setArmorGroups(groups);
376 // get_armor_groups(self)
377 int ObjectRef::l_get_armor_groups(lua_State *L)
379 NO_MAP_LOCK_REQUIRED;
380 ObjectRef *ref = checkobject(L, 1);
381 ServerActiveObject *co = getobject(ref);
385 push_groups(L, co->getArmorGroups());
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)
393 NO_MAP_LOCK_REQUIRED;
394 ObjectRef *ref = checkobject(L, 1);
395 PlayerSAO *co = (PlayerSAO *) getobject(ref);
396 if (co == NULL) return 0;
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;
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;
418 if (!lua_isnil(L, 3)) {
419 co->m_physics_override_jump = lua_tonumber(L, 3);
420 co->m_physics_override_sent = false;
422 if (!lua_isnil(L, 4)) {
423 co->m_physics_override_gravity = lua_tonumber(L, 4);
424 co->m_physics_override_sent = false;
430 // get_physics_override(self)
431 int ObjectRef::l_get_physics_override(lua_State *L)
433 NO_MAP_LOCK_REQUIRED;
434 ObjectRef *ref = checkobject(L, 1);
435 PlayerSAO *co = (PlayerSAO *)getobject(ref);
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");
455 // set_animation(self, frame_range, frame_speed, frame_blend, frame_loop)
456 int ObjectRef::l_set_animation(lua_State *L)
458 NO_MAP_LOCK_REQUIRED;
459 ObjectRef *ref = checkobject(L, 1);
460 ServerActiveObject *co = getobject(ref);
461 if (co == NULL) return 0;
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);
479 // get_animation(self)
480 int ObjectRef::l_get_animation(lua_State *L)
482 NO_MAP_LOCK_REQUIRED;
483 ObjectRef *ref = checkobject(L, 1);
484 ServerActiveObject *co = getobject(ref);
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);
495 lua_pushnumber(L, frame_speed);
496 lua_pushnumber(L, frame_blend);
497 lua_pushboolean(L, frame_loop);
501 // set_local_animation(self, {stand/idle}, {walk}, {dig}, {walk+dig}, frame_speed)
502 int ObjectRef::l_set_local_animation(lua_State *L)
504 NO_MAP_LOCK_REQUIRED;
505 ObjectRef *ref = checkobject(L, 1);
506 RemotePlayer *player = getplayer(ref);
511 for (int i=0;i<4;i++) {
512 if (!lua_isnil(L, 2+1))
513 frames[i] = read_v2s32(L, 2+i);
515 float frame_speed = 30;
516 if (!lua_isnil(L, 6))
517 frame_speed = lua_tonumber(L, 6);
519 getServer(L)->setLocalPlayerAnimations(player, frames, frame_speed);
520 lua_pushboolean(L, true);
524 // get_local_animation(self)
525 int ObjectRef::l_get_local_animation(lua_State *L)
528 ObjectRef *ref = checkobject(L, 1);
529 RemotePlayer *player = getplayer(ref);
535 player->getLocalAnimations(frames, &frame_speed);
537 for (const v2s32 &frame : frames) {
538 push_v2s32(L, frame);
541 lua_pushnumber(L, frame_speed);
545 // set_eye_offset(self, v3f first pv, v3f third pv)
546 int ObjectRef::l_set_eye_offset(lua_State *L)
548 NO_MAP_LOCK_REQUIRED;
549 ObjectRef *ref = checkobject(L, 1);
550 RemotePlayer *player = getplayer(ref);
554 v3f offset_first = v3f(0, 0, 0);
555 v3f offset_third = v3f(0, 0, 0);
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);
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
568 getServer(L)->setPlayerEyeOffset(player, offset_first, offset_third);
569 lua_pushboolean(L, true);
573 // get_eye_offset(self)
574 int ObjectRef::l_get_eye_offset(lua_State *L)
576 NO_MAP_LOCK_REQUIRED;
577 ObjectRef *ref = checkobject(L, 1);
578 RemotePlayer *player = getplayer(ref);
582 push_v3f(L, player->eye_offset_first);
583 push_v3f(L, player->eye_offset_third);
587 // send_mapblock(self, pos)
588 int ObjectRef::l_send_mapblock(lua_State *L)
590 NO_MAP_LOCK_REQUIRED;
591 ObjectRef *ref = checkobject(L, 1);
593 RemotePlayer *player = getplayer(ref);
596 v3s16 p = read_v3s16(L, 2);
598 session_t peer_id = player->getPeerId();
599 bool r = getServer(L)->SendBlock(peer_id, p);
601 lua_pushboolean(L, r);
605 // set_animation_frame_speed(self, frame_speed)
606 int ObjectRef::l_set_animation_frame_speed(lua_State *L)
608 NO_MAP_LOCK_REQUIRED;
609 ObjectRef *ref = checkobject(L, 1);
610 ServerActiveObject *co = getobject(ref);
615 if (!lua_isnil(L, 2)) {
616 float frame_speed = lua_tonumber(L, 2);
617 co->setAnimationSpeed(frame_speed);
618 lua_pushboolean(L, true);
620 lua_pushboolean(L, false);
625 // set_bone_position(self, std::string bone, v3f position, v3f rotation)
626 int ObjectRef::l_set_bone_position(lua_State *L)
628 NO_MAP_LOCK_REQUIRED;
629 ObjectRef *ref = checkobject(L, 1);
630 ServerActiveObject *co = getobject(ref);
631 if (co == NULL) return 0;
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);
646 // get_bone_position(self, bone)
647 int ObjectRef::l_get_bone_position(lua_State *L)
649 NO_MAP_LOCK_REQUIRED;
650 ObjectRef *ref = checkobject(L, 1);
651 ServerActiveObject *co = getobject(ref);
656 if (!lua_isnil(L, 2))
657 bone = readParam<std::string>(L, 2);
659 v3f position = v3f(0, 0, 0);
660 v3f rotation = v3f(0, 0, 0);
661 co->getBonePosition(bone, &position, &rotation);
663 push_v3f(L, position);
664 push_v3f(L, rotation);
668 // set_attach(self, parent, bone, position, rotation)
669 int ObjectRef::l_set_attach(lua_State *L)
673 ObjectRef *ref = checkobject(L, 1);
674 ObjectRef *parent_ref = checkobject(L, 2);
675 ServerActiveObject *co = getobject(ref);
676 ServerActiveObject *parent = getobject(parent_ref);
684 v3f position = v3f(0, 0, 0);
685 v3f rotation = v3f(0, 0, 0);
686 co->getAttachment(&parent_id, &bone, &position, &rotation);
688 ServerActiveObject *old_parent = env->getActiveObject(parent_id);
689 old_parent->removeAttachmentChild(co->getId());
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());
707 int ObjectRef::l_get_attach(lua_State *L)
711 ObjectRef *ref = checkobject(L, 1);
712 ServerActiveObject *co = getobject(ref);
719 v3f position = v3f(0, 0, 0);
720 v3f rotation = v3f(0, 0, 0);
721 co->getAttachment(&parent_id, &bone, &position, &rotation);
724 ServerActiveObject *parent = env->getActiveObject(parent_id);
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);
734 int ObjectRef::l_set_detach(lua_State *L)
738 ObjectRef *ref = checkobject(L, 1);
739 ServerActiveObject *co = getobject(ref);
743 co->clearParentAttachment();
747 // set_properties(self, properties)
748 int ObjectRef::l_set_properties(lua_State *L)
750 NO_MAP_LOCK_REQUIRED;
751 ObjectRef *ref = checkobject(L, 1);
752 ServerActiveObject *co = getobject(ref);
753 if (co == NULL) return 0;
754 ObjectProperties *prop = co->accessObjectProperties();
757 read_object_properties(L, 2, prop, getServer(L)->idef());
758 if (prop->hp_max < co->getHP()) {
759 PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
760 co->setHP(prop->hp_max, reason);
761 if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
762 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, reason);
764 co->notifyObjectPropertiesModified();
768 // get_properties(self)
769 int ObjectRef::l_get_properties(lua_State *L)
771 NO_MAP_LOCK_REQUIRED;
772 ObjectRef *ref = checkobject(L, 1);
773 ServerActiveObject *co = getobject(ref);
776 ObjectProperties *prop = co->accessObjectProperties();
779 push_object_properties(L, prop);
784 int ObjectRef::l_is_player(lua_State *L)
786 NO_MAP_LOCK_REQUIRED;
787 ObjectRef *ref = checkobject(L, 1);
788 RemotePlayer *player = getplayer(ref);
789 lua_pushboolean(L, (player != NULL));
793 // set_nametag_attributes(self, attributes)
794 int ObjectRef::l_set_nametag_attributes(lua_State *L)
796 NO_MAP_LOCK_REQUIRED;
797 ObjectRef *ref = checkobject(L, 1);
798 ServerActiveObject *co = getobject(ref);
802 ObjectProperties *prop = co->accessObjectProperties();
806 lua_getfield(L, 2, "color");
807 if (!lua_isnil(L, -1)) {
808 video::SColor color = prop->nametag_color;
809 read_color(L, -1, &color);
810 prop->nametag_color = color;
814 std::string nametag = getstringfield_default(L, 2, "text", "");
815 prop->nametag = nametag;
817 co->notifyObjectPropertiesModified();
818 lua_pushboolean(L, true);
822 // get_nametag_attributes(self)
823 int ObjectRef::l_get_nametag_attributes(lua_State *L)
825 NO_MAP_LOCK_REQUIRED;
826 ObjectRef *ref = checkobject(L, 1);
827 ServerActiveObject *co = getobject(ref);
831 ObjectProperties *prop = co->accessObjectProperties();
835 video::SColor color = prop->nametag_color;
838 push_ARGB8(L, color);
839 lua_setfield(L, -2, "color");
840 lua_pushstring(L, prop->nametag.c_str());
841 lua_setfield(L, -2, "text");
845 /* LuaEntitySAO-only */
847 // set_velocity(self, {x=num, y=num, z=num})
848 int ObjectRef::l_set_velocity(lua_State *L)
850 NO_MAP_LOCK_REQUIRED;
851 ObjectRef *ref = checkobject(L, 1);
852 LuaEntitySAO *co = getluaobject(ref);
853 if (co == NULL) return 0;
854 v3f pos = checkFloatPos(L, 2);
856 co->setVelocity(pos);
860 // add_velocity(self, {x=num, y=num, z=num})
861 int ObjectRef::l_add_velocity(lua_State *L)
863 NO_MAP_LOCK_REQUIRED;
864 ObjectRef *ref = checkobject(L, 1);
865 LuaEntitySAO *co = getluaobject(ref);
868 v3f pos = checkFloatPos(L, 2);
870 co->addVelocity(pos);
874 // get_velocity(self)
875 int ObjectRef::l_get_velocity(lua_State *L)
877 NO_MAP_LOCK_REQUIRED;
878 ObjectRef *ref = checkobject(L, 1);
879 LuaEntitySAO *co = getluaobject(ref);
880 if (co == NULL) return 0;
882 v3f v = co->getVelocity();
887 // set_acceleration(self, {x=num, y=num, z=num})
888 int ObjectRef::l_set_acceleration(lua_State *L)
890 NO_MAP_LOCK_REQUIRED;
891 ObjectRef *ref = checkobject(L, 1);
892 LuaEntitySAO *co = getluaobject(ref);
893 if (co == NULL) return 0;
895 v3f pos = checkFloatPos(L, 2);
897 co->setAcceleration(pos);
901 // get_acceleration(self)
902 int ObjectRef::l_get_acceleration(lua_State *L)
904 NO_MAP_LOCK_REQUIRED;
905 ObjectRef *ref = checkobject(L, 1);
906 LuaEntitySAO *co = getluaobject(ref);
907 if (co == NULL) return 0;
909 v3f v = co->getAcceleration();
914 // set_rotation(self, {x=num, y=num, z=num})
915 // Each 'num' is in radians
916 int ObjectRef::l_set_rotation(lua_State *L)
918 NO_MAP_LOCK_REQUIRED;
919 ObjectRef *ref = checkobject(L, 1);
920 LuaEntitySAO *co = getluaobject(ref);
924 v3f rotation = check_v3f(L, 2) * core::RADTODEG;
925 co->setRotation(rotation);
929 // get_rotation(self)
930 // returns: {x=num, y=num, z=num}
931 // Each 'num' is in radians
932 int ObjectRef::l_get_rotation(lua_State *L)
934 NO_MAP_LOCK_REQUIRED;
935 ObjectRef *ref = checkobject(L, 1);
936 LuaEntitySAO *co = getluaobject(ref);
941 v3f rotation = co->getRotation() * core::DEGTORAD;
942 push_v3f(L, rotation);
946 // set_yaw(self, radians)
947 int ObjectRef::l_set_yaw(lua_State *L)
949 NO_MAP_LOCK_REQUIRED;
950 ObjectRef *ref = checkobject(L, 1);
951 LuaEntitySAO *co = getluaobject(ref);
953 if (co == NULL) return 0;
955 throw LuaError("ObjectRef::set_yaw: NaN value is not allowed.");
957 float yaw = readParam<float>(L, 2) * core::RADTODEG;
958 co->setRotation(v3f(0, yaw, 0));
963 int ObjectRef::l_get_yaw(lua_State *L)
965 NO_MAP_LOCK_REQUIRED;
966 ObjectRef *ref = checkobject(L, 1);
967 LuaEntitySAO *co = getluaobject(ref);
971 float yaw = co->getRotation().Y * core::DEGTORAD;
972 lua_pushnumber(L, yaw);
976 // set_texture_mod(self, mod)
977 int ObjectRef::l_set_texture_mod(lua_State *L)
979 NO_MAP_LOCK_REQUIRED;
980 ObjectRef *ref = checkobject(L, 1);
981 LuaEntitySAO *co = getluaobject(ref);
982 if (co == NULL) return 0;
984 std::string mod = luaL_checkstring(L, 2);
985 co->setTextureMod(mod);
989 // get_texture_mod(self)
990 int ObjectRef::l_get_texture_mod(lua_State *L)
992 NO_MAP_LOCK_REQUIRED;
993 ObjectRef *ref = checkobject(L, 1);
994 LuaEntitySAO *co = getluaobject(ref);
995 if (co == NULL) return 0;
997 std::string mod = co->getTextureMod();
998 lua_pushstring(L, mod.c_str());
1002 // set_sprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
1003 // select_horiz_by_yawpitch=false)
1004 int ObjectRef::l_set_sprite(lua_State *L)
1006 NO_MAP_LOCK_REQUIRED;
1007 ObjectRef *ref = checkobject(L, 1);
1008 LuaEntitySAO *co = getluaobject(ref);
1009 if (co == NULL) return 0;
1012 if (!lua_isnil(L, 2))
1013 p = readParam<v2s16>(L, 2);
1015 if (!lua_isnil(L, 3))
1016 num_frames = lua_tonumber(L, 3);
1017 float framelength = 0.2;
1018 if (!lua_isnil(L, 4))
1019 framelength = lua_tonumber(L, 4);
1020 bool select_horiz_by_yawpitch = false;
1021 if (!lua_isnil(L, 5))
1022 select_horiz_by_yawpitch = readParam<bool>(L, 5);
1023 co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
1028 // get_entity_name(self)
1029 int ObjectRef::l_get_entity_name(lua_State *L)
1031 NO_MAP_LOCK_REQUIRED;
1032 ObjectRef *ref = checkobject(L, 1);
1033 LuaEntitySAO *co = getluaobject(ref);
1034 log_deprecated(L,"Deprecated call to \"get_entity_name");
1035 if (co == NULL) return 0;
1037 std::string name = co->getName();
1038 lua_pushstring(L, name.c_str());
1042 // get_luaentity(self)
1043 int ObjectRef::l_get_luaentity(lua_State *L)
1045 NO_MAP_LOCK_REQUIRED;
1046 ObjectRef *ref = checkobject(L, 1);
1047 LuaEntitySAO *co = getluaobject(ref);
1048 if (co == NULL) return 0;
1050 luaentity_get(L, co->getId());
1056 // is_player_connected(self)
1057 int ObjectRef::l_is_player_connected(lua_State *L)
1059 NO_MAP_LOCK_REQUIRED;
1060 ObjectRef *ref = checkobject(L, 1);
1061 RemotePlayer *player = getplayer(ref);
1062 lua_pushboolean(L, (player != NULL && player->getPeerId() != PEER_ID_INEXISTENT));
1066 // get_player_name(self)
1067 int ObjectRef::l_get_player_name(lua_State *L)
1069 NO_MAP_LOCK_REQUIRED;
1070 ObjectRef *ref = checkobject(L, 1);
1071 RemotePlayer *player = getplayer(ref);
1072 if (player == NULL) {
1073 lua_pushlstring(L, "", 0);
1077 lua_pushstring(L, player->getName());
1081 // get_player_velocity(self)
1082 int ObjectRef::l_get_player_velocity(lua_State *L)
1084 NO_MAP_LOCK_REQUIRED;
1085 ObjectRef *ref = checkobject(L, 1);
1086 RemotePlayer *player = getplayer(ref);
1087 if (player == NULL) {
1092 push_v3f(L, player->getSpeed() / BS);
1096 // get_look_dir(self)
1097 int ObjectRef::l_get_look_dir(lua_State *L)
1099 NO_MAP_LOCK_REQUIRED;
1100 ObjectRef *ref = checkobject(L, 1);
1101 PlayerSAO* co = getplayersao(ref);
1102 if (co == NULL) return 0;
1104 float pitch = co->getRadLookPitchDep();
1105 float yaw = co->getRadYawDep();
1106 v3f v(std::cos(pitch) * std::cos(yaw), std::sin(pitch), std::cos(pitch) *
1113 // get_look_pitch(self)
1114 int ObjectRef::l_get_look_pitch(lua_State *L)
1116 NO_MAP_LOCK_REQUIRED;
1119 "Deprecated call to get_look_pitch, use get_look_vertical instead");
1121 ObjectRef *ref = checkobject(L, 1);
1122 PlayerSAO* co = getplayersao(ref);
1123 if (co == NULL) return 0;
1125 lua_pushnumber(L, co->getRadLookPitchDep());
1130 // get_look_yaw(self)
1131 int ObjectRef::l_get_look_yaw(lua_State *L)
1133 NO_MAP_LOCK_REQUIRED;
1136 "Deprecated call to get_look_yaw, use get_look_horizontal instead");
1138 ObjectRef *ref = checkobject(L, 1);
1139 PlayerSAO* co = getplayersao(ref);
1140 if (co == NULL) return 0;
1142 lua_pushnumber(L, co->getRadYawDep());
1146 // get_look_pitch2(self)
1147 int ObjectRef::l_get_look_vertical(lua_State *L)
1149 NO_MAP_LOCK_REQUIRED;
1150 ObjectRef *ref = checkobject(L, 1);
1151 PlayerSAO* co = getplayersao(ref);
1152 if (co == NULL) return 0;
1154 lua_pushnumber(L, co->getRadLookPitch());
1158 // get_look_yaw2(self)
1159 int ObjectRef::l_get_look_horizontal(lua_State *L)
1161 NO_MAP_LOCK_REQUIRED;
1162 ObjectRef *ref = checkobject(L, 1);
1163 PlayerSAO* co = getplayersao(ref);
1164 if (co == NULL) return 0;
1166 lua_pushnumber(L, co->getRadRotation().Y);
1170 // set_look_vertical(self, radians)
1171 int ObjectRef::l_set_look_vertical(lua_State *L)
1173 NO_MAP_LOCK_REQUIRED;
1174 ObjectRef *ref = checkobject(L, 1);
1175 PlayerSAO* co = getplayersao(ref);
1176 if (co == NULL) return 0;
1177 float pitch = readParam<float>(L, 2) * core::RADTODEG;
1179 co->setLookPitchAndSend(pitch);
1183 // set_look_horizontal(self, radians)
1184 int ObjectRef::l_set_look_horizontal(lua_State *L)
1186 NO_MAP_LOCK_REQUIRED;
1187 ObjectRef *ref = checkobject(L, 1);
1188 PlayerSAO* co = getplayersao(ref);
1189 if (co == NULL) return 0;
1190 float yaw = readParam<float>(L, 2) * core::RADTODEG;
1192 co->setPlayerYawAndSend(yaw);
1197 // set_look_pitch(self, radians)
1198 int ObjectRef::l_set_look_pitch(lua_State *L)
1200 NO_MAP_LOCK_REQUIRED;
1203 "Deprecated call to set_look_pitch, use set_look_vertical instead.");
1205 ObjectRef *ref = checkobject(L, 1);
1206 PlayerSAO* co = getplayersao(ref);
1207 if (co == NULL) return 0;
1208 float pitch = readParam<float>(L, 2) * core::RADTODEG;
1210 co->setLookPitchAndSend(pitch);
1215 // set_look_yaw(self, radians)
1216 int ObjectRef::l_set_look_yaw(lua_State *L)
1218 NO_MAP_LOCK_REQUIRED;
1221 "Deprecated call to set_look_yaw, use set_look_horizontal instead.");
1223 ObjectRef *ref = checkobject(L, 1);
1224 PlayerSAO* co = getplayersao(ref);
1225 if (co == NULL) return 0;
1226 float yaw = readParam<float>(L, 2) * core::RADTODEG;
1228 co->setPlayerYawAndSend(yaw);
1232 // set_breath(self, breath)
1233 int ObjectRef::l_set_breath(lua_State *L)
1235 NO_MAP_LOCK_REQUIRED;
1236 ObjectRef *ref = checkobject(L, 1);
1237 PlayerSAO* co = getplayersao(ref);
1238 if (co == NULL) return 0;
1239 u16 breath = luaL_checknumber(L, 2);
1240 co->setBreath(breath);
1246 int ObjectRef::l_get_breath(lua_State *L)
1248 NO_MAP_LOCK_REQUIRED;
1249 ObjectRef *ref = checkobject(L, 1);
1250 PlayerSAO* co = getplayersao(ref);
1251 if (co == NULL) return 0;
1253 u16 breath = co->getBreath();
1254 lua_pushinteger (L, breath);
1258 // set_attribute(self, attribute, value)
1259 int ObjectRef::l_set_attribute(lua_State *L)
1262 "Deprecated call to set_attribute, use MetaDataRef methods instead.");
1264 ObjectRef *ref = checkobject(L, 1);
1265 PlayerSAO* co = getplayersao(ref);
1269 std::string attr = luaL_checkstring(L, 2);
1270 if (lua_isnil(L, 3)) {
1271 co->getMeta().removeString(attr);
1273 std::string value = luaL_checkstring(L, 3);
1274 co->getMeta().setString(attr, value);
1279 // get_attribute(self, attribute)
1280 int ObjectRef::l_get_attribute(lua_State *L)
1283 "Deprecated call to get_attribute, use MetaDataRef methods instead.");
1285 ObjectRef *ref = checkobject(L, 1);
1286 PlayerSAO* co = getplayersao(ref);
1290 std::string attr = luaL_checkstring(L, 2);
1293 if (co->getMeta().getStringToRef(attr, value)) {
1294 lua_pushstring(L, value.c_str());
1302 // get_meta(self, attribute)
1303 int ObjectRef::l_get_meta(lua_State *L)
1305 ObjectRef *ref = checkobject(L, 1);
1306 PlayerSAO *co = getplayersao(ref);
1310 PlayerMetaRef::create(L, &co->getMeta());
1315 // set_inventory_formspec(self, formspec)
1316 int ObjectRef::l_set_inventory_formspec(lua_State *L)
1318 NO_MAP_LOCK_REQUIRED;
1319 ObjectRef *ref = checkobject(L, 1);
1320 RemotePlayer *player = getplayer(ref);
1321 if (player == NULL) return 0;
1322 std::string formspec = luaL_checkstring(L, 2);
1324 player->inventory_formspec = formspec;
1325 getServer(L)->reportInventoryFormspecModified(player->getName());
1326 lua_pushboolean(L, true);
1330 // get_inventory_formspec(self) -> formspec
1331 int ObjectRef::l_get_inventory_formspec(lua_State *L)
1333 NO_MAP_LOCK_REQUIRED;
1334 ObjectRef *ref = checkobject(L, 1);
1335 RemotePlayer *player = getplayer(ref);
1336 if (player == NULL) return 0;
1338 std::string formspec = player->inventory_formspec;
1339 lua_pushlstring(L, formspec.c_str(), formspec.size());
1343 // set_formspec_prepend(self, formspec)
1344 int ObjectRef::l_set_formspec_prepend(lua_State *L)
1346 NO_MAP_LOCK_REQUIRED;
1347 ObjectRef *ref = checkobject(L, 1);
1348 RemotePlayer *player = getplayer(ref);
1352 std::string formspec = luaL_checkstring(L, 2);
1354 player->formspec_prepend = formspec;
1355 getServer(L)->reportFormspecPrependModified(player->getName());
1356 lua_pushboolean(L, true);
1360 // get_formspec_prepend(self) -> formspec
1361 int ObjectRef::l_get_formspec_prepend(lua_State *L)
1363 NO_MAP_LOCK_REQUIRED;
1364 ObjectRef *ref = checkobject(L, 1);
1365 RemotePlayer *player = getplayer(ref);
1369 std::string formspec = player->formspec_prepend;
1370 lua_pushlstring(L, formspec.c_str(), formspec.size());
1374 // get_player_control(self)
1375 int ObjectRef::l_get_player_control(lua_State *L)
1377 NO_MAP_LOCK_REQUIRED;
1378 ObjectRef *ref = checkobject(L, 1);
1379 RemotePlayer *player = getplayer(ref);
1380 if (player == NULL) {
1381 lua_pushlstring(L, "", 0);
1385 const PlayerControl &control = player->getPlayerControl();
1387 lua_pushboolean(L, control.up);
1388 lua_setfield(L, -2, "up");
1389 lua_pushboolean(L, control.down);
1390 lua_setfield(L, -2, "down");
1391 lua_pushboolean(L, control.left);
1392 lua_setfield(L, -2, "left");
1393 lua_pushboolean(L, control.right);
1394 lua_setfield(L, -2, "right");
1395 lua_pushboolean(L, control.jump);
1396 lua_setfield(L, -2, "jump");
1397 lua_pushboolean(L, control.aux1);
1398 lua_setfield(L, -2, "aux1");
1399 lua_pushboolean(L, control.sneak);
1400 lua_setfield(L, -2, "sneak");
1401 lua_pushboolean(L, control.LMB);
1402 lua_setfield(L, -2, "LMB");
1403 lua_pushboolean(L, control.RMB);
1404 lua_setfield(L, -2, "RMB");
1408 // get_player_control_bits(self)
1409 int ObjectRef::l_get_player_control_bits(lua_State *L)
1411 NO_MAP_LOCK_REQUIRED;
1412 ObjectRef *ref = checkobject(L, 1);
1413 RemotePlayer *player = getplayer(ref);
1414 if (player == NULL) {
1415 lua_pushlstring(L, "", 0);
1419 lua_pushnumber(L, player->keyPressed);
1423 // hud_add(self, form)
1424 int ObjectRef::l_hud_add(lua_State *L)
1426 NO_MAP_LOCK_REQUIRED;
1427 ObjectRef *ref = checkobject(L, 1);
1428 RemotePlayer *player = getplayer(ref);
1432 HudElement *elem = new HudElement;
1433 read_hud_element(L, elem);
1435 u32 id = getServer(L)->hudAdd(player, elem);
1436 if (id == U32_MAX) {
1441 lua_pushnumber(L, id);
1445 // hud_remove(self, id)
1446 int ObjectRef::l_hud_remove(lua_State *L)
1448 NO_MAP_LOCK_REQUIRED;
1449 ObjectRef *ref = checkobject(L, 1);
1450 RemotePlayer *player = getplayer(ref);
1455 if (!lua_isnil(L, 2))
1456 id = lua_tonumber(L, 2);
1458 if (!getServer(L)->hudRemove(player, id))
1461 lua_pushboolean(L, true);
1465 // hud_change(self, id, stat, data)
1466 int ObjectRef::l_hud_change(lua_State *L)
1468 NO_MAP_LOCK_REQUIRED;
1469 ObjectRef *ref = checkobject(L, 1);
1470 RemotePlayer *player = getplayer(ref);
1474 u32 id = lua_isnumber(L, 2) ? lua_tonumber(L, 2) : -1;
1476 HudElement *e = player->getHud(id);
1481 HudElementStat stat = read_hud_change(L, e, &value);
1483 getServer(L)->hudChange(player, id, stat, value);
1485 lua_pushboolean(L, true);
1489 // hud_get(self, id)
1490 int ObjectRef::l_hud_get(lua_State *L)
1492 NO_MAP_LOCK_REQUIRED;
1493 ObjectRef *ref = checkobject(L, 1);
1494 RemotePlayer *player = getplayer(ref);
1498 u32 id = lua_tonumber(L, -1);
1500 HudElement *e = player->getHud(id);
1503 push_hud_element(L, e);
1507 // hud_set_flags(self, flags)
1508 int ObjectRef::l_hud_set_flags(lua_State *L)
1510 NO_MAP_LOCK_REQUIRED;
1511 ObjectRef *ref = checkobject(L, 1);
1512 RemotePlayer *player = getplayer(ref);
1520 const EnumString *esp = es_HudBuiltinElement;
1521 for (int i = 0; esp[i].str; i++) {
1522 if (getboolfield(L, 2, esp[i].str, flag)) {
1523 flags |= esp[i].num * flag;
1527 if (!getServer(L)->hudSetFlags(player, flags, mask))
1530 lua_pushboolean(L, true);
1534 int ObjectRef::l_hud_get_flags(lua_State *L)
1536 NO_MAP_LOCK_REQUIRED;
1537 ObjectRef *ref = checkobject(L, 1);
1538 RemotePlayer *player = getplayer(ref);
1543 lua_pushboolean(L, player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE);
1544 lua_setfield(L, -2, "hotbar");
1545 lua_pushboolean(L, player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE);
1546 lua_setfield(L, -2, "healthbar");
1547 lua_pushboolean(L, player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE);
1548 lua_setfield(L, -2, "crosshair");
1549 lua_pushboolean(L, player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE);
1550 lua_setfield(L, -2, "wielditem");
1551 lua_pushboolean(L, player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE);
1552 lua_setfield(L, -2, "breathbar");
1553 lua_pushboolean(L, player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE);
1554 lua_setfield(L, -2, "minimap");
1555 lua_pushboolean(L, player->hud_flags & HUD_FLAG_MINIMAP_RADAR_VISIBLE);
1556 lua_setfield(L, -2, "minimap_radar");
1561 // hud_set_hotbar_itemcount(self, hotbar_itemcount)
1562 int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L)
1564 NO_MAP_LOCK_REQUIRED;
1565 ObjectRef *ref = checkobject(L, 1);
1566 RemotePlayer *player = getplayer(ref);
1570 s32 hotbar_itemcount = lua_tonumber(L, 2);
1572 if (!getServer(L)->hudSetHotbarItemcount(player, hotbar_itemcount))
1575 lua_pushboolean(L, true);
1579 // hud_get_hotbar_itemcount(self)
1580 int ObjectRef::l_hud_get_hotbar_itemcount(lua_State *L)
1582 NO_MAP_LOCK_REQUIRED;
1583 ObjectRef *ref = checkobject(L, 1);
1584 RemotePlayer *player = getplayer(ref);
1588 lua_pushnumber(L, player->getHotbarItemcount());
1592 // hud_set_hotbar_image(self, name)
1593 int ObjectRef::l_hud_set_hotbar_image(lua_State *L)
1595 NO_MAP_LOCK_REQUIRED;
1596 ObjectRef *ref = checkobject(L, 1);
1597 RemotePlayer *player = getplayer(ref);
1601 std::string name = readParam<std::string>(L, 2);
1603 getServer(L)->hudSetHotbarImage(player, name);
1607 // hud_get_hotbar_image(self)
1608 int ObjectRef::l_hud_get_hotbar_image(lua_State *L)
1610 NO_MAP_LOCK_REQUIRED;
1611 ObjectRef *ref = checkobject(L, 1);
1612 RemotePlayer *player = getplayer(ref);
1616 const std::string &name = player->getHotbarImage();
1617 lua_pushlstring(L, name.c_str(), name.size());
1621 // hud_set_hotbar_selected_image(self, name)
1622 int ObjectRef::l_hud_set_hotbar_selected_image(lua_State *L)
1624 NO_MAP_LOCK_REQUIRED;
1625 ObjectRef *ref = checkobject(L, 1);
1626 RemotePlayer *player = getplayer(ref);
1630 std::string name = readParam<std::string>(L, 2);
1632 getServer(L)->hudSetHotbarSelectedImage(player, name);
1636 // hud_get_hotbar_selected_image(self)
1637 int ObjectRef::l_hud_get_hotbar_selected_image(lua_State *L)
1639 NO_MAP_LOCK_REQUIRED;
1640 ObjectRef *ref = checkobject(L, 1);
1641 RemotePlayer *player = getplayer(ref);
1645 const std::string &name = player->getHotbarSelectedImage();
1646 lua_pushlstring(L, name.c_str(), name.size());
1650 // set_sky(self, bgcolor, type, list, clouds = true)
1651 int ObjectRef::l_set_sky(lua_State *L)
1653 NO_MAP_LOCK_REQUIRED;
1654 ObjectRef *ref = checkobject(L, 1);
1655 RemotePlayer *player = getplayer(ref);
1659 video::SColor bgcolor(255,255,255,255);
1660 read_color(L, 2, &bgcolor);
1662 std::string type = luaL_checkstring(L, 3);
1664 std::vector<std::string> params;
1665 if (lua_istable(L, 4)) {
1667 while (lua_next(L, 4) != 0) {
1668 // key at index -2 and value at index -1
1669 if (lua_isstring(L, -1))
1670 params.emplace_back(readParam<std::string>(L, -1));
1672 params.emplace_back("");
1673 // removes value, keeps key for next iteration
1678 if (type == "skybox" && params.size() != 6)
1679 throw LuaError("skybox expects 6 textures");
1682 if (lua_isboolean(L, 5))
1683 clouds = readParam<bool>(L, 5);
1685 getServer(L)->setSky(player, bgcolor, type, params, clouds);
1686 lua_pushboolean(L, true);
1691 int ObjectRef::l_get_sky(lua_State *L)
1693 NO_MAP_LOCK_REQUIRED;
1694 ObjectRef *ref = checkobject(L, 1);
1695 RemotePlayer *player = getplayer(ref);
1698 video::SColor bgcolor(255, 255, 255, 255);
1700 std::vector<std::string> params;
1703 player->getSky(&bgcolor, &type, ¶ms, &clouds);
1704 type = type.empty() ? "regular" : type;
1706 push_ARGB8(L, bgcolor);
1707 lua_pushlstring(L, type.c_str(), type.size());
1710 for (const std::string ¶m : params) {
1711 lua_pushlstring(L, param.c_str(), param.size());
1712 lua_rawseti(L, -2, i++);
1714 lua_pushboolean(L, clouds);
1718 // set_clouds(self, {density=, color=, ambient=, height=, thickness=, speed=})
1719 int ObjectRef::l_set_clouds(lua_State *L)
1721 NO_MAP_LOCK_REQUIRED;
1722 ObjectRef *ref = checkobject(L, 1);
1723 RemotePlayer *player = getplayer(ref);
1726 if (!lua_istable(L, 2))
1729 CloudParams cloud_params = player->getCloudParams();
1731 cloud_params.density = getfloatfield_default(L, 2, "density", cloud_params.density);
1733 lua_getfield(L, 2, "color");
1734 if (!lua_isnil(L, -1))
1735 read_color(L, -1, &cloud_params.color_bright);
1737 lua_getfield(L, 2, "ambient");
1738 if (!lua_isnil(L, -1))
1739 read_color(L, -1, &cloud_params.color_ambient);
1742 cloud_params.height = getfloatfield_default(L, 2, "height", cloud_params.height );
1743 cloud_params.thickness = getfloatfield_default(L, 2, "thickness", cloud_params.thickness);
1745 lua_getfield(L, 2, "speed");
1746 if (lua_istable(L, -1)) {
1748 new_speed.X = getfloatfield_default(L, -1, "x", 0);
1749 new_speed.Y = getfloatfield_default(L, -1, "z", 0);
1750 cloud_params.speed = new_speed;
1754 getServer(L)->setClouds(player, cloud_params);
1755 lua_pushboolean(L, true);
1759 int ObjectRef::l_get_clouds(lua_State *L)
1761 NO_MAP_LOCK_REQUIRED;
1762 ObjectRef *ref = checkobject(L, 1);
1763 RemotePlayer *player = getplayer(ref);
1766 const CloudParams &cloud_params = player->getCloudParams();
1769 lua_pushnumber(L, cloud_params.density);
1770 lua_setfield(L, -2, "density");
1771 push_ARGB8(L, cloud_params.color_bright);
1772 lua_setfield(L, -2, "color");
1773 push_ARGB8(L, cloud_params.color_ambient);
1774 lua_setfield(L, -2, "ambient");
1775 lua_pushnumber(L, cloud_params.height);
1776 lua_setfield(L, -2, "height");
1777 lua_pushnumber(L, cloud_params.thickness);
1778 lua_setfield(L, -2, "thickness");
1780 lua_pushnumber(L, cloud_params.speed.X);
1781 lua_setfield(L, -2, "x");
1782 lua_pushnumber(L, cloud_params.speed.Y);
1783 lua_setfield(L, -2, "y");
1784 lua_setfield(L, -2, "speed");
1790 // override_day_night_ratio(self, brightness=0...1)
1791 int ObjectRef::l_override_day_night_ratio(lua_State *L)
1793 NO_MAP_LOCK_REQUIRED;
1794 ObjectRef *ref = checkobject(L, 1);
1795 RemotePlayer *player = getplayer(ref);
1799 bool do_override = false;
1801 if (!lua_isnil(L, 2)) {
1803 ratio = readParam<float>(L, 2);
1806 if (!getServer(L)->overrideDayNightRatio(player, do_override, ratio))
1809 lua_pushboolean(L, true);
1813 // get_day_night_ratio(self)
1814 int ObjectRef::l_get_day_night_ratio(lua_State *L)
1816 NO_MAP_LOCK_REQUIRED;
1817 ObjectRef *ref = checkobject(L, 1);
1818 RemotePlayer *player = getplayer(ref);
1824 player->getDayNightRatio(&do_override, &ratio);
1827 lua_pushnumber(L, ratio);
1834 ObjectRef::ObjectRef(ServerActiveObject *object):
1837 //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
1840 // Creates an ObjectRef and leaves it on top of stack
1841 // Not callable from Lua; all references are created on the C side.
1842 void ObjectRef::create(lua_State *L, ServerActiveObject *object)
1844 ObjectRef *o = new ObjectRef(object);
1845 //infostream<<"ObjectRef::create: o="<<o<<std::endl;
1846 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1847 luaL_getmetatable(L, className);
1848 lua_setmetatable(L, -2);
1851 void ObjectRef::set_null(lua_State *L)
1853 ObjectRef *o = checkobject(L, -1);
1857 void ObjectRef::Register(lua_State *L)
1860 int methodtable = lua_gettop(L);
1861 luaL_newmetatable(L, className);
1862 int metatable = lua_gettop(L);
1864 lua_pushliteral(L, "__metatable");
1865 lua_pushvalue(L, methodtable);
1866 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
1868 lua_pushliteral(L, "__index");
1869 lua_pushvalue(L, methodtable);
1870 lua_settable(L, metatable);
1872 lua_pushliteral(L, "__gc");
1873 lua_pushcfunction(L, gc_object);
1874 lua_settable(L, metatable);
1876 lua_pop(L, 1); // drop metatable
1878 markAliasDeprecated(methods);
1879 luaL_openlib(L, 0, methods, 0); // fill methodtable
1880 lua_pop(L, 1); // drop methodtable
1882 // Cannot be created from Lua
1883 //lua_register(L, className, create_object);
1886 const char ObjectRef::className[] = "ObjectRef";
1887 luaL_Reg ObjectRef::methods[] = {
1888 // ServerActiveObject
1889 luamethod(ObjectRef, remove),
1890 luamethod_aliased(ObjectRef, get_pos, getpos),
1891 luamethod_aliased(ObjectRef, set_pos, setpos),
1892 luamethod_aliased(ObjectRef, move_to, moveto),
1893 luamethod(ObjectRef, punch),
1894 luamethod(ObjectRef, right_click),
1895 luamethod(ObjectRef, set_hp),
1896 luamethod(ObjectRef, get_hp),
1897 luamethod(ObjectRef, get_inventory),
1898 luamethod(ObjectRef, get_wield_list),
1899 luamethod(ObjectRef, get_wield_index),
1900 luamethod(ObjectRef, get_wielded_item),
1901 luamethod(ObjectRef, set_wielded_item),
1902 luamethod(ObjectRef, set_armor_groups),
1903 luamethod(ObjectRef, get_armor_groups),
1904 luamethod(ObjectRef, set_animation),
1905 luamethod(ObjectRef, get_animation),
1906 luamethod(ObjectRef, set_animation_frame_speed),
1907 luamethod(ObjectRef, set_bone_position),
1908 luamethod(ObjectRef, get_bone_position),
1909 luamethod(ObjectRef, set_attach),
1910 luamethod(ObjectRef, get_attach),
1911 luamethod(ObjectRef, set_detach),
1912 luamethod(ObjectRef, set_properties),
1913 luamethod(ObjectRef, get_properties),
1914 luamethod(ObjectRef, set_nametag_attributes),
1915 luamethod(ObjectRef, get_nametag_attributes),
1916 // LuaEntitySAO-only
1917 luamethod_aliased(ObjectRef, set_velocity, setvelocity),
1918 luamethod(ObjectRef, add_velocity),
1919 luamethod_aliased(ObjectRef, get_velocity, getvelocity),
1920 luamethod_aliased(ObjectRef, set_acceleration, setacceleration),
1921 luamethod_aliased(ObjectRef, get_acceleration, getacceleration),
1922 luamethod_aliased(ObjectRef, set_yaw, setyaw),
1923 luamethod_aliased(ObjectRef, get_yaw, getyaw),
1924 luamethod(ObjectRef, set_rotation),
1925 luamethod(ObjectRef, get_rotation),
1926 luamethod_aliased(ObjectRef, set_texture_mod, settexturemod),
1927 luamethod_aliased(ObjectRef, set_sprite, setsprite),
1928 luamethod(ObjectRef, get_entity_name),
1929 luamethod(ObjectRef, get_luaentity),
1931 luamethod(ObjectRef, is_player),
1932 luamethod(ObjectRef, is_player_connected),
1933 luamethod(ObjectRef, get_player_name),
1934 luamethod(ObjectRef, get_player_velocity),
1935 luamethod(ObjectRef, get_look_dir),
1936 luamethod(ObjectRef, get_look_pitch),
1937 luamethod(ObjectRef, get_look_yaw),
1938 luamethod(ObjectRef, get_look_vertical),
1939 luamethod(ObjectRef, get_look_horizontal),
1940 luamethod(ObjectRef, set_look_horizontal),
1941 luamethod(ObjectRef, set_look_vertical),
1942 luamethod(ObjectRef, set_look_yaw),
1943 luamethod(ObjectRef, set_look_pitch),
1944 luamethod(ObjectRef, get_breath),
1945 luamethod(ObjectRef, set_breath),
1946 luamethod(ObjectRef, get_attribute),
1947 luamethod(ObjectRef, set_attribute),
1948 luamethod(ObjectRef, get_meta),
1949 luamethod(ObjectRef, set_inventory_formspec),
1950 luamethod(ObjectRef, get_inventory_formspec),
1951 luamethod(ObjectRef, set_formspec_prepend),
1952 luamethod(ObjectRef, get_formspec_prepend),
1953 luamethod(ObjectRef, get_player_control),
1954 luamethod(ObjectRef, get_player_control_bits),
1955 luamethod(ObjectRef, set_physics_override),
1956 luamethod(ObjectRef, get_physics_override),
1957 luamethod(ObjectRef, hud_add),
1958 luamethod(ObjectRef, hud_remove),
1959 luamethod(ObjectRef, hud_change),
1960 luamethod(ObjectRef, hud_get),
1961 luamethod(ObjectRef, hud_set_flags),
1962 luamethod(ObjectRef, hud_get_flags),
1963 luamethod(ObjectRef, hud_set_hotbar_itemcount),
1964 luamethod(ObjectRef, hud_get_hotbar_itemcount),
1965 luamethod(ObjectRef, hud_set_hotbar_image),
1966 luamethod(ObjectRef, hud_get_hotbar_image),
1967 luamethod(ObjectRef, hud_set_hotbar_selected_image),
1968 luamethod(ObjectRef, hud_get_hotbar_selected_image),
1969 luamethod(ObjectRef, set_sky),
1970 luamethod(ObjectRef, get_sky),
1971 luamethod(ObjectRef, set_clouds),
1972 luamethod(ObjectRef, get_clouds),
1973 luamethod(ObjectRef, override_day_night_ratio),
1974 luamethod(ObjectRef, get_day_night_ratio),
1975 luamethod(ObjectRef, set_local_animation),
1976 luamethod(ObjectRef, get_local_animation),
1977 luamethod(ObjectRef, set_eye_offset),
1978 luamethod(ObjectRef, get_eye_offset),
1979 luamethod(ObjectRef, send_mapblock),