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)
65 return (LuaEntitySAO*)obj;
68 PlayerSAO* ObjectRef::getplayersao(ObjectRef *ref)
70 ServerActiveObject *obj = getobject(ref);
73 if (obj->getType() != ACTIVEOBJECT_TYPE_PLAYER)
77 return (PlayerSAO*)obj;
80 RemotePlayer *ObjectRef::getplayer(ObjectRef *ref)
82 PlayerSAO *playersao = getplayersao(ref);
83 if (playersao == NULL)
85 return playersao->getPlayer();
91 int ObjectRef::gc_object(lua_State *L) {
92 ObjectRef *o = *(ObjectRef **)(lua_touserdata(L, 1));
93 //infostream<<"ObjectRef::gc_object: o="<<o<<std::endl;
99 int ObjectRef::l_remove(lua_State *L)
103 ObjectRef *ref = checkobject(L, 1);
104 ServerActiveObject *co = getobject(ref);
107 if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
110 co->clearChildAttachments();
111 co->clearParentAttachment();
113 verbosestream << "ObjectRef::l_remove(): id=" << co->getId() << std::endl;
114 co->m_pending_removal = true;
119 // returns: {x=num, y=num, z=num}
120 int ObjectRef::l_get_pos(lua_State *L)
122 NO_MAP_LOCK_REQUIRED;
123 ObjectRef *ref = checkobject(L, 1);
124 ServerActiveObject *co = getobject(ref);
125 if (co == NULL) return 0;
126 v3f pos = co->getBasePosition() / BS;
128 lua_pushnumber(L, pos.X);
129 lua_setfield(L, -2, "x");
130 lua_pushnumber(L, pos.Y);
131 lua_setfield(L, -2, "y");
132 lua_pushnumber(L, pos.Z);
133 lua_setfield(L, -2, "z");
137 // set_pos(self, pos)
138 int ObjectRef::l_set_pos(lua_State *L)
140 NO_MAP_LOCK_REQUIRED;
141 ObjectRef *ref = checkobject(L, 1);
142 //LuaEntitySAO *co = getluaobject(ref);
143 ServerActiveObject *co = getobject(ref);
144 if (co == NULL) return 0;
146 v3f pos = checkFloatPos(L, 2);
152 // move_to(self, pos, continuous=false)
153 int ObjectRef::l_move_to(lua_State *L)
155 NO_MAP_LOCK_REQUIRED;
156 ObjectRef *ref = checkobject(L, 1);
157 //LuaEntitySAO *co = getluaobject(ref);
158 ServerActiveObject *co = getobject(ref);
159 if (co == NULL) return 0;
161 v3f pos = checkFloatPos(L, 2);
163 bool continuous = readParam<bool>(L, 3);
165 co->moveTo(pos, continuous);
169 // punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
170 int ObjectRef::l_punch(lua_State *L)
172 NO_MAP_LOCK_REQUIRED;
173 ObjectRef *ref = checkobject(L, 1);
174 ObjectRef *puncher_ref = checkobject(L, 2);
175 ServerActiveObject *co = getobject(ref);
176 ServerActiveObject *puncher = getobject(puncher_ref);
180 if (lua_type(L, 5) != LUA_TTABLE)
181 dir = co->getBasePosition() - puncher->getBasePosition();
183 dir = read_v3f(L, 5);
184 float time_from_last_punch = 1000000;
185 if (lua_isnumber(L, 3))
186 time_from_last_punch = lua_tonumber(L, 3);
187 ToolCapabilities toolcap = read_tool_capabilities(L, 4);
190 u16 src_original_hp = co->getHP();
191 u16 dst_origin_hp = puncher->getHP();
194 u16 wear = co->punch(dir, &toolcap, puncher, time_from_last_punch);
195 lua_pushnumber(L, wear);
197 // If the punched is a player, and its HP changed
198 if (src_original_hp != co->getHP() &&
199 co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
200 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co,
201 PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, puncher));
204 // If the puncher is a player, and its HP changed
205 if (dst_origin_hp != puncher->getHP() &&
206 puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
207 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)puncher,
208 PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, co));
213 // right_click(self, clicker); clicker = an another ObjectRef
214 int ObjectRef::l_right_click(lua_State *L)
216 NO_MAP_LOCK_REQUIRED;
217 ObjectRef *ref = checkobject(L, 1);
218 ObjectRef *ref2 = checkobject(L, 2);
219 ServerActiveObject *co = getobject(ref);
220 ServerActiveObject *co2 = getobject(ref2);
221 if (co == NULL) return 0;
222 if (co2 == NULL) return 0;
229 // hp = number of hitpoints (2 * number of hearts)
231 int ObjectRef::l_set_hp(lua_State *L)
233 NO_MAP_LOCK_REQUIRED;
236 ObjectRef *ref = checkobject(L, 1);
237 luaL_checknumber(L, 2);
238 ServerActiveObject *co = getobject(ref);
243 int hp = lua_tonumber(L, 2);
246 PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
247 reason.from_mod = true;
248 if (lua_istable(L, 3)) {
251 lua_getfield(L, -1, "type");
252 if (lua_isstring(L, -1) &&
253 !reason.setTypeFromString(readParam<std::string>(L, -1))) {
254 errorstream << "Bad type given!" << std::endl;
258 reason.lua_reference = luaL_ref(L, LUA_REGISTRYINDEX);
262 co->setHP(hp, reason);
263 if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
264 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, reason);
266 if (reason.hasLuaReference())
267 luaL_unref(L, LUA_REGISTRYINDEX, reason.lua_reference);
274 // returns: number of hitpoints (2 * number of hearts)
275 // 0 if not applicable to this type of object
276 int ObjectRef::l_get_hp(lua_State *L)
278 NO_MAP_LOCK_REQUIRED;
279 ObjectRef *ref = checkobject(L, 1);
280 ServerActiveObject *co = getobject(ref);
283 lua_pushnumber(L, 1);
286 int hp = co->getHP();
287 /*infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
288 <<" hp="<<hp<<std::endl;*/
290 lua_pushnumber(L, hp);
294 // get_inventory(self)
295 int ObjectRef::l_get_inventory(lua_State *L)
297 NO_MAP_LOCK_REQUIRED;
298 ObjectRef *ref = checkobject(L, 1);
299 ServerActiveObject *co = getobject(ref);
300 if (co == NULL) return 0;
302 InventoryLocation loc = co->getInventoryLocation();
303 if (getServer(L)->getInventory(loc) != NULL)
304 InvRef::create(L, loc);
306 lua_pushnil(L); // An object may have no inventory (nil)
310 // get_wield_list(self)
311 int ObjectRef::l_get_wield_list(lua_State *L)
313 NO_MAP_LOCK_REQUIRED;
314 ObjectRef *ref = checkobject(L, 1);
315 ServerActiveObject *co = getobject(ref);
319 lua_pushstring(L, co->getWieldList().c_str());
323 // get_wield_index(self)
324 int ObjectRef::l_get_wield_index(lua_State *L)
326 NO_MAP_LOCK_REQUIRED;
327 ObjectRef *ref = checkobject(L, 1);
328 ServerActiveObject *co = getobject(ref);
332 lua_pushinteger(L, co->getWieldIndex() + 1);
336 // get_wielded_item(self)
337 int ObjectRef::l_get_wielded_item(lua_State *L)
339 NO_MAP_LOCK_REQUIRED;
340 ObjectRef *ref = checkobject(L, 1);
341 ServerActiveObject *co = getobject(ref);
344 LuaItemStack::create(L, ItemStack());
348 ItemStack selected_item;
349 co->getWieldedItem(&selected_item, nullptr);
350 LuaItemStack::create(L, selected_item);
354 // set_wielded_item(self, itemstack or itemstring or table or nil)
355 int ObjectRef::l_set_wielded_item(lua_State *L)
357 NO_MAP_LOCK_REQUIRED;
358 ObjectRef *ref = checkobject(L, 1);
359 ServerActiveObject *co = getobject(ref);
360 if (co == NULL) return 0;
362 ItemStack item = read_item(L, 2, getServer(L)->idef());
363 bool success = co->setWieldedItem(item);
364 if (success && co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
365 getServer(L)->SendInventory((PlayerSAO *)co, true);
367 lua_pushboolean(L, success);
371 // set_armor_groups(self, groups)
372 int ObjectRef::l_set_armor_groups(lua_State *L)
374 NO_MAP_LOCK_REQUIRED;
375 ObjectRef *ref = checkobject(L, 1);
376 ServerActiveObject *co = getobject(ref);
377 if (co == NULL) return 0;
379 ItemGroupList groups;
380 read_groups(L, 2, groups);
381 co->setArmorGroups(groups);
385 // get_armor_groups(self)
386 int ObjectRef::l_get_armor_groups(lua_State *L)
388 NO_MAP_LOCK_REQUIRED;
389 ObjectRef *ref = checkobject(L, 1);
390 ServerActiveObject *co = getobject(ref);
394 push_groups(L, co->getArmorGroups());
398 // set_physics_override(self, physics_override_speed, physics_override_jump,
399 // physics_override_gravity, sneak, sneak_glitch, new_move)
400 int ObjectRef::l_set_physics_override(lua_State *L)
402 NO_MAP_LOCK_REQUIRED;
403 ObjectRef *ref = checkobject(L, 1);
404 PlayerSAO *co = (PlayerSAO *) getobject(ref);
405 if (co == NULL) return 0;
407 if (lua_istable(L, 2)) {
408 co->m_physics_override_speed = getfloatfield_default(
409 L, 2, "speed", co->m_physics_override_speed);
410 co->m_physics_override_jump = getfloatfield_default(
411 L, 2, "jump", co->m_physics_override_jump);
412 co->m_physics_override_gravity = getfloatfield_default(
413 L, 2, "gravity", co->m_physics_override_gravity);
414 co->m_physics_override_sneak = getboolfield_default(
415 L, 2, "sneak", co->m_physics_override_sneak);
416 co->m_physics_override_sneak_glitch = getboolfield_default(
417 L, 2, "sneak_glitch", co->m_physics_override_sneak_glitch);
418 co->m_physics_override_new_move = getboolfield_default(
419 L, 2, "new_move", co->m_physics_override_new_move);
420 co->m_physics_override_sent = false;
422 // old, non-table format
423 if (!lua_isnil(L, 2)) {
424 co->m_physics_override_speed = lua_tonumber(L, 2);
425 co->m_physics_override_sent = false;
427 if (!lua_isnil(L, 3)) {
428 co->m_physics_override_jump = lua_tonumber(L, 3);
429 co->m_physics_override_sent = false;
431 if (!lua_isnil(L, 4)) {
432 co->m_physics_override_gravity = lua_tonumber(L, 4);
433 co->m_physics_override_sent = false;
439 // get_physics_override(self)
440 int ObjectRef::l_get_physics_override(lua_State *L)
442 NO_MAP_LOCK_REQUIRED;
443 ObjectRef *ref = checkobject(L, 1);
444 PlayerSAO *co = (PlayerSAO *)getobject(ref);
449 lua_pushnumber(L, co->m_physics_override_speed);
450 lua_setfield(L, -2, "speed");
451 lua_pushnumber(L, co->m_physics_override_jump);
452 lua_setfield(L, -2, "jump");
453 lua_pushnumber(L, co->m_physics_override_gravity);
454 lua_setfield(L, -2, "gravity");
455 lua_pushboolean(L, co->m_physics_override_sneak);
456 lua_setfield(L, -2, "sneak");
457 lua_pushboolean(L, co->m_physics_override_sneak_glitch);
458 lua_setfield(L, -2, "sneak_glitch");
459 lua_pushboolean(L, co->m_physics_override_new_move);
460 lua_setfield(L, -2, "new_move");
464 // set_animation(self, frame_range, frame_speed, frame_blend, frame_loop)
465 int ObjectRef::l_set_animation(lua_State *L)
467 NO_MAP_LOCK_REQUIRED;
468 ObjectRef *ref = checkobject(L, 1);
469 ServerActiveObject *co = getobject(ref);
470 if (co == NULL) return 0;
472 v2f frames = v2f(1, 1);
473 if (!lua_isnil(L, 2))
474 frames = readParam<v2f>(L, 2);
475 float frame_speed = 15;
476 if (!lua_isnil(L, 3))
477 frame_speed = lua_tonumber(L, 3);
478 float frame_blend = 0;
479 if (!lua_isnil(L, 4))
480 frame_blend = lua_tonumber(L, 4);
481 bool frame_loop = true;
482 if (lua_isboolean(L, 5))
483 frame_loop = readParam<bool>(L, 5);
484 co->setAnimation(frames, frame_speed, frame_blend, frame_loop);
488 // get_animation(self)
489 int ObjectRef::l_get_animation(lua_State *L)
491 NO_MAP_LOCK_REQUIRED;
492 ObjectRef *ref = checkobject(L, 1);
493 ServerActiveObject *co = getobject(ref);
497 v2f frames = v2f(1,1);
498 float frame_speed = 15;
499 float frame_blend = 0;
500 bool frame_loop = true;
501 co->getAnimation(&frames, &frame_speed, &frame_blend, &frame_loop);
504 lua_pushnumber(L, frame_speed);
505 lua_pushnumber(L, frame_blend);
506 lua_pushboolean(L, frame_loop);
510 // set_local_animation(self, {stand/idle}, {walk}, {dig}, {walk+dig}, frame_speed)
511 int ObjectRef::l_set_local_animation(lua_State *L)
513 NO_MAP_LOCK_REQUIRED;
514 ObjectRef *ref = checkobject(L, 1);
515 RemotePlayer *player = getplayer(ref);
520 for (int i=0;i<4;i++) {
521 if (!lua_isnil(L, 2+1))
522 frames[i] = read_v2s32(L, 2+i);
524 float frame_speed = 30;
525 if (!lua_isnil(L, 6))
526 frame_speed = lua_tonumber(L, 6);
528 getServer(L)->setLocalPlayerAnimations(player, frames, frame_speed);
529 lua_pushboolean(L, true);
533 // get_local_animation(self)
534 int ObjectRef::l_get_local_animation(lua_State *L)
537 ObjectRef *ref = checkobject(L, 1);
538 RemotePlayer *player = getplayer(ref);
544 player->getLocalAnimations(frames, &frame_speed);
546 for (const v2s32 &frame : frames) {
547 push_v2s32(L, frame);
550 lua_pushnumber(L, frame_speed);
554 // set_eye_offset(self, v3f first pv, v3f third pv)
555 int ObjectRef::l_set_eye_offset(lua_State *L)
557 NO_MAP_LOCK_REQUIRED;
558 ObjectRef *ref = checkobject(L, 1);
559 RemotePlayer *player = getplayer(ref);
563 v3f offset_first = v3f(0, 0, 0);
564 v3f offset_third = v3f(0, 0, 0);
566 if (!lua_isnil(L, 2))
567 offset_first = read_v3f(L, 2);
568 if (!lua_isnil(L, 3))
569 offset_third = read_v3f(L, 3);
571 // Prevent abuse of offset values (keep player always visible)
572 offset_third.X = rangelim(offset_third.X,-10,10);
573 offset_third.Z = rangelim(offset_third.Z,-5,5);
574 /* TODO: if possible: improve the camera colision detetion to allow Y <= -1.5) */
575 offset_third.Y = rangelim(offset_third.Y,-10,15); //1.5*BS
577 getServer(L)->setPlayerEyeOffset(player, offset_first, offset_third);
578 lua_pushboolean(L, true);
582 // get_eye_offset(self)
583 int ObjectRef::l_get_eye_offset(lua_State *L)
585 NO_MAP_LOCK_REQUIRED;
586 ObjectRef *ref = checkobject(L, 1);
587 RemotePlayer *player = getplayer(ref);
591 push_v3f(L, player->eye_offset_first);
592 push_v3f(L, player->eye_offset_third);
596 // send_mapblock(self, pos)
597 int ObjectRef::l_send_mapblock(lua_State *L)
599 NO_MAP_LOCK_REQUIRED;
600 ObjectRef *ref = checkobject(L, 1);
602 RemotePlayer *player = getplayer(ref);
605 v3s16 p = read_v3s16(L, 2);
607 session_t peer_id = player->getPeerId();
608 bool r = getServer(L)->SendBlock(peer_id, p);
610 lua_pushboolean(L, r);
614 // set_animation_frame_speed(self, frame_speed)
615 int ObjectRef::l_set_animation_frame_speed(lua_State *L)
617 NO_MAP_LOCK_REQUIRED;
618 ObjectRef *ref = checkobject(L, 1);
619 ServerActiveObject *co = getobject(ref);
624 if (!lua_isnil(L, 2)) {
625 float frame_speed = lua_tonumber(L, 2);
626 co->setAnimationSpeed(frame_speed);
627 lua_pushboolean(L, true);
629 lua_pushboolean(L, false);
634 // set_bone_position(self, std::string bone, v3f position, v3f rotation)
635 int ObjectRef::l_set_bone_position(lua_State *L)
637 NO_MAP_LOCK_REQUIRED;
638 ObjectRef *ref = checkobject(L, 1);
639 ServerActiveObject *co = getobject(ref);
640 if (co == NULL) return 0;
643 if (!lua_isnil(L, 2))
644 bone = readParam<std::string>(L, 2);
645 v3f position = v3f(0, 0, 0);
646 if (!lua_isnil(L, 3))
647 position = check_v3f(L, 3);
648 v3f rotation = v3f(0, 0, 0);
649 if (!lua_isnil(L, 4))
650 rotation = check_v3f(L, 4);
651 co->setBonePosition(bone, position, rotation);
655 // get_bone_position(self, bone)
656 int ObjectRef::l_get_bone_position(lua_State *L)
658 NO_MAP_LOCK_REQUIRED;
659 ObjectRef *ref = checkobject(L, 1);
660 ServerActiveObject *co = getobject(ref);
665 if (!lua_isnil(L, 2))
666 bone = readParam<std::string>(L, 2);
668 v3f position = v3f(0, 0, 0);
669 v3f rotation = v3f(0, 0, 0);
670 co->getBonePosition(bone, &position, &rotation);
672 push_v3f(L, position);
673 push_v3f(L, rotation);
677 // set_attach(self, parent, bone, position, rotation)
678 int ObjectRef::l_set_attach(lua_State *L)
682 ObjectRef *ref = checkobject(L, 1);
683 ObjectRef *parent_ref = checkobject(L, 2);
684 ServerActiveObject *co = getobject(ref);
685 ServerActiveObject *parent = getobject(parent_ref);
693 v3f position = v3f(0, 0, 0);
694 v3f rotation = v3f(0, 0, 0);
695 co->getAttachment(&parent_id, &bone, &position, &rotation);
697 ServerActiveObject *old_parent = env->getActiveObject(parent_id);
698 old_parent->removeAttachmentChild(co->getId());
702 if (!lua_isnil(L, 3))
703 bone = readParam<std::string>(L, 3);
704 position = v3f(0, 0, 0);
705 if (!lua_isnil(L, 4))
706 position = read_v3f(L, 4);
707 rotation = v3f(0, 0, 0);
708 if (!lua_isnil(L, 5))
709 rotation = read_v3f(L, 5);
710 co->setAttachment(parent->getId(), bone, position, rotation);
711 parent->addAttachmentChild(co->getId());
716 int ObjectRef::l_get_attach(lua_State *L)
720 ObjectRef *ref = checkobject(L, 1);
721 ServerActiveObject *co = getobject(ref);
728 v3f position = v3f(0, 0, 0);
729 v3f rotation = v3f(0, 0, 0);
730 co->getAttachment(&parent_id, &bone, &position, &rotation);
733 ServerActiveObject *parent = env->getActiveObject(parent_id);
735 getScriptApiBase(L)->objectrefGetOrCreate(L, parent);
736 lua_pushlstring(L, bone.c_str(), bone.size());
737 push_v3f(L, position);
738 push_v3f(L, rotation);
743 int ObjectRef::l_set_detach(lua_State *L)
747 ObjectRef *ref = checkobject(L, 1);
748 ServerActiveObject *co = getobject(ref);
752 co->clearParentAttachment();
756 // set_properties(self, properties)
757 int ObjectRef::l_set_properties(lua_State *L)
759 NO_MAP_LOCK_REQUIRED;
760 ObjectRef *ref = checkobject(L, 1);
761 ServerActiveObject *co = getobject(ref);
765 ObjectProperties *prop = co->accessObjectProperties();
769 read_object_properties(L, 2, co, prop, getServer(L)->idef());
770 co->notifyObjectPropertiesModified();
774 // get_properties(self)
775 int ObjectRef::l_get_properties(lua_State *L)
777 NO_MAP_LOCK_REQUIRED;
778 ObjectRef *ref = checkobject(L, 1);
779 ServerActiveObject *co = getobject(ref);
782 ObjectProperties *prop = co->accessObjectProperties();
785 push_object_properties(L, prop);
790 int ObjectRef::l_is_player(lua_State *L)
792 NO_MAP_LOCK_REQUIRED;
793 ObjectRef *ref = checkobject(L, 1);
794 RemotePlayer *player = getplayer(ref);
795 lua_pushboolean(L, (player != NULL));
799 // set_nametag_attributes(self, attributes)
800 int ObjectRef::l_set_nametag_attributes(lua_State *L)
802 NO_MAP_LOCK_REQUIRED;
803 ObjectRef *ref = checkobject(L, 1);
804 ServerActiveObject *co = getobject(ref);
808 ObjectProperties *prop = co->accessObjectProperties();
812 lua_getfield(L, 2, "color");
813 if (!lua_isnil(L, -1)) {
814 video::SColor color = prop->nametag_color;
815 read_color(L, -1, &color);
816 prop->nametag_color = color;
820 std::string nametag = getstringfield_default(L, 2, "text", "");
821 prop->nametag = nametag;
823 co->notifyObjectPropertiesModified();
824 lua_pushboolean(L, true);
828 // get_nametag_attributes(self)
829 int ObjectRef::l_get_nametag_attributes(lua_State *L)
831 NO_MAP_LOCK_REQUIRED;
832 ObjectRef *ref = checkobject(L, 1);
833 ServerActiveObject *co = getobject(ref);
837 ObjectProperties *prop = co->accessObjectProperties();
841 video::SColor color = prop->nametag_color;
844 push_ARGB8(L, color);
845 lua_setfield(L, -2, "color");
846 lua_pushstring(L, prop->nametag.c_str());
847 lua_setfield(L, -2, "text");
851 /* LuaEntitySAO-only */
853 // set_velocity(self, {x=num, y=num, z=num})
854 int ObjectRef::l_set_velocity(lua_State *L)
856 NO_MAP_LOCK_REQUIRED;
857 ObjectRef *ref = checkobject(L, 1);
858 LuaEntitySAO *co = getluaobject(ref);
859 if (co == NULL) return 0;
860 v3f pos = checkFloatPos(L, 2);
862 co->setVelocity(pos);
866 // add_velocity(self, {x=num, y=num, z=num})
867 int ObjectRef::l_add_velocity(lua_State *L)
869 NO_MAP_LOCK_REQUIRED;
870 ObjectRef *ref = checkobject(L, 1);
871 LuaEntitySAO *co = getluaobject(ref);
874 v3f pos = checkFloatPos(L, 2);
876 co->addVelocity(pos);
880 // get_velocity(self)
881 int ObjectRef::l_get_velocity(lua_State *L)
883 NO_MAP_LOCK_REQUIRED;
884 ObjectRef *ref = checkobject(L, 1);
885 LuaEntitySAO *co = getluaobject(ref);
886 if (co == NULL) return 0;
888 v3f v = co->getVelocity();
893 // set_acceleration(self, {x=num, y=num, z=num})
894 int ObjectRef::l_set_acceleration(lua_State *L)
896 NO_MAP_LOCK_REQUIRED;
897 ObjectRef *ref = checkobject(L, 1);
898 LuaEntitySAO *co = getluaobject(ref);
899 if (co == NULL) return 0;
901 v3f pos = checkFloatPos(L, 2);
903 co->setAcceleration(pos);
907 // get_acceleration(self)
908 int ObjectRef::l_get_acceleration(lua_State *L)
910 NO_MAP_LOCK_REQUIRED;
911 ObjectRef *ref = checkobject(L, 1);
912 LuaEntitySAO *co = getluaobject(ref);
913 if (co == NULL) return 0;
915 v3f v = co->getAcceleration();
920 // set_rotation(self, {x=num, y=num, z=num})
921 // Each 'num' is in radians
922 int ObjectRef::l_set_rotation(lua_State *L)
924 NO_MAP_LOCK_REQUIRED;
925 ObjectRef *ref = checkobject(L, 1);
926 LuaEntitySAO *co = getluaobject(ref);
930 v3f rotation = check_v3f(L, 2) * core::RADTODEG;
931 co->setRotation(rotation);
935 // get_rotation(self)
936 // returns: {x=num, y=num, z=num}
937 // Each 'num' is in radians
938 int ObjectRef::l_get_rotation(lua_State *L)
940 NO_MAP_LOCK_REQUIRED;
941 ObjectRef *ref = checkobject(L, 1);
942 LuaEntitySAO *co = getluaobject(ref);
947 v3f rotation = co->getRotation() * core::DEGTORAD;
948 push_v3f(L, rotation);
952 // set_yaw(self, radians)
953 int ObjectRef::l_set_yaw(lua_State *L)
955 NO_MAP_LOCK_REQUIRED;
956 ObjectRef *ref = checkobject(L, 1);
957 LuaEntitySAO *co = getluaobject(ref);
959 if (co == NULL) return 0;
961 throw LuaError("ObjectRef::set_yaw: NaN value is not allowed.");
963 float yaw = readParam<float>(L, 2) * core::RADTODEG;
964 co->setRotation(v3f(0, yaw, 0));
969 int ObjectRef::l_get_yaw(lua_State *L)
971 NO_MAP_LOCK_REQUIRED;
972 ObjectRef *ref = checkobject(L, 1);
973 LuaEntitySAO *co = getluaobject(ref);
977 float yaw = co->getRotation().Y * core::DEGTORAD;
978 lua_pushnumber(L, yaw);
982 // set_texture_mod(self, mod)
983 int ObjectRef::l_set_texture_mod(lua_State *L)
985 NO_MAP_LOCK_REQUIRED;
986 ObjectRef *ref = checkobject(L, 1);
987 LuaEntitySAO *co = getluaobject(ref);
988 if (co == NULL) return 0;
990 std::string mod = luaL_checkstring(L, 2);
991 co->setTextureMod(mod);
995 // get_texture_mod(self)
996 int ObjectRef::l_get_texture_mod(lua_State *L)
998 NO_MAP_LOCK_REQUIRED;
999 ObjectRef *ref = checkobject(L, 1);
1000 LuaEntitySAO *co = getluaobject(ref);
1001 if (co == NULL) return 0;
1003 std::string mod = co->getTextureMod();
1004 lua_pushstring(L, mod.c_str());
1008 // set_sprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
1009 // select_horiz_by_yawpitch=false)
1010 int ObjectRef::l_set_sprite(lua_State *L)
1012 NO_MAP_LOCK_REQUIRED;
1013 ObjectRef *ref = checkobject(L, 1);
1014 LuaEntitySAO *co = getluaobject(ref);
1015 if (co == NULL) return 0;
1018 if (!lua_isnil(L, 2))
1019 p = readParam<v2s16>(L, 2);
1021 if (!lua_isnil(L, 3))
1022 num_frames = lua_tonumber(L, 3);
1023 float framelength = 0.2;
1024 if (!lua_isnil(L, 4))
1025 framelength = lua_tonumber(L, 4);
1026 bool select_horiz_by_yawpitch = false;
1027 if (!lua_isnil(L, 5))
1028 select_horiz_by_yawpitch = readParam<bool>(L, 5);
1029 co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
1034 // get_entity_name(self)
1035 int ObjectRef::l_get_entity_name(lua_State *L)
1037 NO_MAP_LOCK_REQUIRED;
1038 ObjectRef *ref = checkobject(L, 1);
1039 LuaEntitySAO *co = getluaobject(ref);
1040 log_deprecated(L,"Deprecated call to \"get_entity_name");
1041 if (co == NULL) return 0;
1043 std::string name = co->getName();
1044 lua_pushstring(L, name.c_str());
1048 // get_luaentity(self)
1049 int ObjectRef::l_get_luaentity(lua_State *L)
1051 NO_MAP_LOCK_REQUIRED;
1052 ObjectRef *ref = checkobject(L, 1);
1053 LuaEntitySAO *co = getluaobject(ref);
1054 if (co == NULL) return 0;
1056 luaentity_get(L, co->getId());
1062 // is_player_connected(self)
1063 int ObjectRef::l_is_player_connected(lua_State *L)
1065 NO_MAP_LOCK_REQUIRED;
1066 // This method was once added for a bugfix, but never documented
1067 log_deprecated(L, "is_player_connected is undocumented and "
1068 "will be removed in a future release");
1069 ObjectRef *ref = checkobject(L, 1);
1070 RemotePlayer *player = getplayer(ref);
1071 lua_pushboolean(L, (player != NULL && player->getPeerId() != PEER_ID_INEXISTENT));
1075 // get_player_name(self)
1076 int ObjectRef::l_get_player_name(lua_State *L)
1078 NO_MAP_LOCK_REQUIRED;
1079 ObjectRef *ref = checkobject(L, 1);
1080 RemotePlayer *player = getplayer(ref);
1081 if (player == NULL) {
1082 lua_pushlstring(L, "", 0);
1086 lua_pushstring(L, player->getName());
1090 // get_player_velocity(self)
1091 int ObjectRef::l_get_player_velocity(lua_State *L)
1093 NO_MAP_LOCK_REQUIRED;
1094 ObjectRef *ref = checkobject(L, 1);
1095 RemotePlayer *player = getplayer(ref);
1096 if (player == NULL) {
1101 push_v3f(L, player->getSpeed() / BS);
1105 // add_player_velocity(self, {x=num, y=num, z=num})
1106 int ObjectRef::l_add_player_velocity(lua_State *L)
1108 NO_MAP_LOCK_REQUIRED;
1109 ObjectRef *ref = checkobject(L, 1);
1110 v3f vel = checkFloatPos(L, 2);
1112 RemotePlayer *player = getplayer(ref);
1113 PlayerSAO *co = getplayersao(ref);
1117 session_t peer_id = player->getPeerId();
1118 if (peer_id == PEER_ID_INEXISTENT)
1121 co->setMaxSpeedOverride(vel);
1122 getServer(L)->SendPlayerSpeed(peer_id, vel);
1126 // get_look_dir(self)
1127 int ObjectRef::l_get_look_dir(lua_State *L)
1129 NO_MAP_LOCK_REQUIRED;
1130 ObjectRef *ref = checkobject(L, 1);
1131 PlayerSAO* co = getplayersao(ref);
1132 if (co == NULL) return 0;
1134 float pitch = co->getRadLookPitchDep();
1135 float yaw = co->getRadYawDep();
1136 v3f v(std::cos(pitch) * std::cos(yaw), std::sin(pitch), std::cos(pitch) *
1143 // get_look_pitch(self)
1144 int ObjectRef::l_get_look_pitch(lua_State *L)
1146 NO_MAP_LOCK_REQUIRED;
1149 "Deprecated call to get_look_pitch, use get_look_vertical instead");
1151 ObjectRef *ref = checkobject(L, 1);
1152 PlayerSAO* co = getplayersao(ref);
1153 if (co == NULL) return 0;
1155 lua_pushnumber(L, co->getRadLookPitchDep());
1160 // get_look_yaw(self)
1161 int ObjectRef::l_get_look_yaw(lua_State *L)
1163 NO_MAP_LOCK_REQUIRED;
1166 "Deprecated call to get_look_yaw, use get_look_horizontal instead");
1168 ObjectRef *ref = checkobject(L, 1);
1169 PlayerSAO* co = getplayersao(ref);
1170 if (co == NULL) return 0;
1172 lua_pushnumber(L, co->getRadYawDep());
1176 // get_look_pitch2(self)
1177 int ObjectRef::l_get_look_vertical(lua_State *L)
1179 NO_MAP_LOCK_REQUIRED;
1180 ObjectRef *ref = checkobject(L, 1);
1181 PlayerSAO* co = getplayersao(ref);
1182 if (co == NULL) return 0;
1184 lua_pushnumber(L, co->getRadLookPitch());
1188 // get_look_yaw2(self)
1189 int ObjectRef::l_get_look_horizontal(lua_State *L)
1191 NO_MAP_LOCK_REQUIRED;
1192 ObjectRef *ref = checkobject(L, 1);
1193 PlayerSAO* co = getplayersao(ref);
1194 if (co == NULL) return 0;
1196 lua_pushnumber(L, co->getRadRotation().Y);
1200 // set_look_vertical(self, radians)
1201 int ObjectRef::l_set_look_vertical(lua_State *L)
1203 NO_MAP_LOCK_REQUIRED;
1204 ObjectRef *ref = checkobject(L, 1);
1205 PlayerSAO* co = getplayersao(ref);
1206 if (co == NULL) return 0;
1207 float pitch = readParam<float>(L, 2) * core::RADTODEG;
1209 co->setLookPitchAndSend(pitch);
1213 // set_look_horizontal(self, radians)
1214 int ObjectRef::l_set_look_horizontal(lua_State *L)
1216 NO_MAP_LOCK_REQUIRED;
1217 ObjectRef *ref = checkobject(L, 1);
1218 PlayerSAO* co = getplayersao(ref);
1219 if (co == NULL) return 0;
1220 float yaw = readParam<float>(L, 2) * core::RADTODEG;
1222 co->setPlayerYawAndSend(yaw);
1227 // set_look_pitch(self, radians)
1228 int ObjectRef::l_set_look_pitch(lua_State *L)
1230 NO_MAP_LOCK_REQUIRED;
1233 "Deprecated call to set_look_pitch, use set_look_vertical instead.");
1235 ObjectRef *ref = checkobject(L, 1);
1236 PlayerSAO* co = getplayersao(ref);
1237 if (co == NULL) return 0;
1238 float pitch = readParam<float>(L, 2) * core::RADTODEG;
1240 co->setLookPitchAndSend(pitch);
1245 // set_look_yaw(self, radians)
1246 int ObjectRef::l_set_look_yaw(lua_State *L)
1248 NO_MAP_LOCK_REQUIRED;
1251 "Deprecated call to set_look_yaw, use set_look_horizontal instead.");
1253 ObjectRef *ref = checkobject(L, 1);
1254 PlayerSAO* co = getplayersao(ref);
1255 if (co == NULL) return 0;
1256 float yaw = readParam<float>(L, 2) * core::RADTODEG;
1258 co->setPlayerYawAndSend(yaw);
1262 // set_fov(self, degrees[, is_multiplier])
1263 int ObjectRef::l_set_fov(lua_State *L)
1265 NO_MAP_LOCK_REQUIRED;
1266 ObjectRef *ref = checkobject(L, 1);
1267 RemotePlayer *player = getplayer(ref);
1271 player->setFov({ static_cast<f32>(luaL_checknumber(L, 2)), readParam<bool>(L, 3) });
1272 getServer(L)->SendPlayerFov(player->getPeerId());
1278 int ObjectRef::l_get_fov(lua_State *L)
1280 NO_MAP_LOCK_REQUIRED;
1281 ObjectRef *ref = checkobject(L, 1);
1282 RemotePlayer *player = getplayer(ref);
1286 PlayerFovSpec fov_spec = player->getFov();
1287 lua_pushnumber(L, fov_spec.fov);
1288 lua_pushboolean(L, fov_spec.is_multiplier);
1293 // set_breath(self, breath)
1294 int ObjectRef::l_set_breath(lua_State *L)
1296 NO_MAP_LOCK_REQUIRED;
1297 ObjectRef *ref = checkobject(L, 1);
1298 PlayerSAO* co = getplayersao(ref);
1299 if (co == NULL) return 0;
1300 u16 breath = luaL_checknumber(L, 2);
1301 co->setBreath(breath);
1307 int ObjectRef::l_get_breath(lua_State *L)
1309 NO_MAP_LOCK_REQUIRED;
1310 ObjectRef *ref = checkobject(L, 1);
1311 PlayerSAO* co = getplayersao(ref);
1312 if (co == NULL) return 0;
1314 u16 breath = co->getBreath();
1315 lua_pushinteger (L, breath);
1319 // set_attribute(self, attribute, value)
1320 int ObjectRef::l_set_attribute(lua_State *L)
1323 "Deprecated call to set_attribute, use MetaDataRef methods instead.");
1325 ObjectRef *ref = checkobject(L, 1);
1326 PlayerSAO* co = getplayersao(ref);
1330 std::string attr = luaL_checkstring(L, 2);
1331 if (lua_isnil(L, 3)) {
1332 co->getMeta().removeString(attr);
1334 std::string value = luaL_checkstring(L, 3);
1335 co->getMeta().setString(attr, value);
1340 // get_attribute(self, attribute)
1341 int ObjectRef::l_get_attribute(lua_State *L)
1344 "Deprecated call to get_attribute, use MetaDataRef methods instead.");
1346 ObjectRef *ref = checkobject(L, 1);
1347 PlayerSAO* co = getplayersao(ref);
1351 std::string attr = luaL_checkstring(L, 2);
1354 if (co->getMeta().getStringToRef(attr, value)) {
1355 lua_pushstring(L, value.c_str());
1363 // get_meta(self, attribute)
1364 int ObjectRef::l_get_meta(lua_State *L)
1366 ObjectRef *ref = checkobject(L, 1);
1367 PlayerSAO *co = getplayersao(ref);
1371 PlayerMetaRef::create(L, &co->getMeta());
1376 // set_inventory_formspec(self, formspec)
1377 int ObjectRef::l_set_inventory_formspec(lua_State *L)
1379 NO_MAP_LOCK_REQUIRED;
1380 ObjectRef *ref = checkobject(L, 1);
1381 RemotePlayer *player = getplayer(ref);
1382 if (player == NULL) return 0;
1383 std::string formspec = luaL_checkstring(L, 2);
1385 player->inventory_formspec = formspec;
1386 getServer(L)->reportInventoryFormspecModified(player->getName());
1387 lua_pushboolean(L, true);
1391 // get_inventory_formspec(self) -> formspec
1392 int ObjectRef::l_get_inventory_formspec(lua_State *L)
1394 NO_MAP_LOCK_REQUIRED;
1395 ObjectRef *ref = checkobject(L, 1);
1396 RemotePlayer *player = getplayer(ref);
1397 if (player == NULL) return 0;
1399 std::string formspec = player->inventory_formspec;
1400 lua_pushlstring(L, formspec.c_str(), formspec.size());
1404 // set_formspec_prepend(self, formspec)
1405 int ObjectRef::l_set_formspec_prepend(lua_State *L)
1407 NO_MAP_LOCK_REQUIRED;
1408 ObjectRef *ref = checkobject(L, 1);
1409 RemotePlayer *player = getplayer(ref);
1413 std::string formspec = luaL_checkstring(L, 2);
1415 player->formspec_prepend = formspec;
1416 getServer(L)->reportFormspecPrependModified(player->getName());
1417 lua_pushboolean(L, true);
1421 // get_formspec_prepend(self) -> formspec
1422 int ObjectRef::l_get_formspec_prepend(lua_State *L)
1424 NO_MAP_LOCK_REQUIRED;
1425 ObjectRef *ref = checkobject(L, 1);
1426 RemotePlayer *player = getplayer(ref);
1430 std::string formspec = player->formspec_prepend;
1431 lua_pushlstring(L, formspec.c_str(), formspec.size());
1435 // get_player_control(self)
1436 int ObjectRef::l_get_player_control(lua_State *L)
1438 NO_MAP_LOCK_REQUIRED;
1439 ObjectRef *ref = checkobject(L, 1);
1440 RemotePlayer *player = getplayer(ref);
1441 if (player == NULL) {
1442 lua_pushlstring(L, "", 0);
1446 const PlayerControl &control = player->getPlayerControl();
1448 lua_pushboolean(L, control.up);
1449 lua_setfield(L, -2, "up");
1450 lua_pushboolean(L, control.down);
1451 lua_setfield(L, -2, "down");
1452 lua_pushboolean(L, control.left);
1453 lua_setfield(L, -2, "left");
1454 lua_pushboolean(L, control.right);
1455 lua_setfield(L, -2, "right");
1456 lua_pushboolean(L, control.jump);
1457 lua_setfield(L, -2, "jump");
1458 lua_pushboolean(L, control.aux1);
1459 lua_setfield(L, -2, "aux1");
1460 lua_pushboolean(L, control.sneak);
1461 lua_setfield(L, -2, "sneak");
1462 lua_pushboolean(L, control.LMB);
1463 lua_setfield(L, -2, "LMB");
1464 lua_pushboolean(L, control.RMB);
1465 lua_setfield(L, -2, "RMB");
1469 // get_player_control_bits(self)
1470 int ObjectRef::l_get_player_control_bits(lua_State *L)
1472 NO_MAP_LOCK_REQUIRED;
1473 ObjectRef *ref = checkobject(L, 1);
1474 RemotePlayer *player = getplayer(ref);
1475 if (player == NULL) {
1476 lua_pushlstring(L, "", 0);
1480 lua_pushnumber(L, player->keyPressed);
1484 // hud_add(self, form)
1485 int ObjectRef::l_hud_add(lua_State *L)
1487 NO_MAP_LOCK_REQUIRED;
1488 ObjectRef *ref = checkobject(L, 1);
1489 RemotePlayer *player = getplayer(ref);
1493 HudElement *elem = new HudElement;
1494 read_hud_element(L, elem);
1496 u32 id = getServer(L)->hudAdd(player, elem);
1497 if (id == U32_MAX) {
1502 lua_pushnumber(L, id);
1506 // hud_remove(self, id)
1507 int ObjectRef::l_hud_remove(lua_State *L)
1509 NO_MAP_LOCK_REQUIRED;
1510 ObjectRef *ref = checkobject(L, 1);
1511 RemotePlayer *player = getplayer(ref);
1516 if (!lua_isnil(L, 2))
1517 id = lua_tonumber(L, 2);
1519 if (!getServer(L)->hudRemove(player, id))
1522 lua_pushboolean(L, true);
1526 // hud_change(self, id, stat, data)
1527 int ObjectRef::l_hud_change(lua_State *L)
1529 NO_MAP_LOCK_REQUIRED;
1530 ObjectRef *ref = checkobject(L, 1);
1531 RemotePlayer *player = getplayer(ref);
1535 u32 id = lua_isnumber(L, 2) ? lua_tonumber(L, 2) : -1;
1537 HudElement *e = player->getHud(id);
1542 HudElementStat stat = read_hud_change(L, e, &value);
1544 getServer(L)->hudChange(player, id, stat, value);
1546 lua_pushboolean(L, true);
1550 // hud_get(self, id)
1551 int ObjectRef::l_hud_get(lua_State *L)
1553 NO_MAP_LOCK_REQUIRED;
1554 ObjectRef *ref = checkobject(L, 1);
1555 RemotePlayer *player = getplayer(ref);
1559 u32 id = lua_tonumber(L, -1);
1561 HudElement *e = player->getHud(id);
1564 push_hud_element(L, e);
1568 // hud_set_flags(self, flags)
1569 int ObjectRef::l_hud_set_flags(lua_State *L)
1571 NO_MAP_LOCK_REQUIRED;
1572 ObjectRef *ref = checkobject(L, 1);
1573 RemotePlayer *player = getplayer(ref);
1581 const EnumString *esp = es_HudBuiltinElement;
1582 for (int i = 0; esp[i].str; i++) {
1583 if (getboolfield(L, 2, esp[i].str, flag)) {
1584 flags |= esp[i].num * flag;
1588 if (!getServer(L)->hudSetFlags(player, flags, mask))
1591 lua_pushboolean(L, true);
1595 int ObjectRef::l_hud_get_flags(lua_State *L)
1597 NO_MAP_LOCK_REQUIRED;
1598 ObjectRef *ref = checkobject(L, 1);
1599 RemotePlayer *player = getplayer(ref);
1604 lua_pushboolean(L, player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE);
1605 lua_setfield(L, -2, "hotbar");
1606 lua_pushboolean(L, player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE);
1607 lua_setfield(L, -2, "healthbar");
1608 lua_pushboolean(L, player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE);
1609 lua_setfield(L, -2, "crosshair");
1610 lua_pushboolean(L, player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE);
1611 lua_setfield(L, -2, "wielditem");
1612 lua_pushboolean(L, player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE);
1613 lua_setfield(L, -2, "breathbar");
1614 lua_pushboolean(L, player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE);
1615 lua_setfield(L, -2, "minimap");
1616 lua_pushboolean(L, player->hud_flags & HUD_FLAG_MINIMAP_RADAR_VISIBLE);
1617 lua_setfield(L, -2, "minimap_radar");
1622 // hud_set_hotbar_itemcount(self, hotbar_itemcount)
1623 int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L)
1625 NO_MAP_LOCK_REQUIRED;
1626 ObjectRef *ref = checkobject(L, 1);
1627 RemotePlayer *player = getplayer(ref);
1631 s32 hotbar_itemcount = lua_tonumber(L, 2);
1633 if (!getServer(L)->hudSetHotbarItemcount(player, hotbar_itemcount))
1636 lua_pushboolean(L, true);
1640 // hud_get_hotbar_itemcount(self)
1641 int ObjectRef::l_hud_get_hotbar_itemcount(lua_State *L)
1643 NO_MAP_LOCK_REQUIRED;
1644 ObjectRef *ref = checkobject(L, 1);
1645 RemotePlayer *player = getplayer(ref);
1649 lua_pushnumber(L, player->getHotbarItemcount());
1653 // hud_set_hotbar_image(self, name)
1654 int ObjectRef::l_hud_set_hotbar_image(lua_State *L)
1656 NO_MAP_LOCK_REQUIRED;
1657 ObjectRef *ref = checkobject(L, 1);
1658 RemotePlayer *player = getplayer(ref);
1662 std::string name = readParam<std::string>(L, 2);
1664 getServer(L)->hudSetHotbarImage(player, name);
1668 // hud_get_hotbar_image(self)
1669 int ObjectRef::l_hud_get_hotbar_image(lua_State *L)
1671 NO_MAP_LOCK_REQUIRED;
1672 ObjectRef *ref = checkobject(L, 1);
1673 RemotePlayer *player = getplayer(ref);
1677 const std::string &name = player->getHotbarImage();
1678 lua_pushlstring(L, name.c_str(), name.size());
1682 // hud_set_hotbar_selected_image(self, name)
1683 int ObjectRef::l_hud_set_hotbar_selected_image(lua_State *L)
1685 NO_MAP_LOCK_REQUIRED;
1686 ObjectRef *ref = checkobject(L, 1);
1687 RemotePlayer *player = getplayer(ref);
1691 std::string name = readParam<std::string>(L, 2);
1693 getServer(L)->hudSetHotbarSelectedImage(player, name);
1697 // hud_get_hotbar_selected_image(self)
1698 int ObjectRef::l_hud_get_hotbar_selected_image(lua_State *L)
1700 NO_MAP_LOCK_REQUIRED;
1701 ObjectRef *ref = checkobject(L, 1);
1702 RemotePlayer *player = getplayer(ref);
1706 const std::string &name = player->getHotbarSelectedImage();
1707 lua_pushlstring(L, name.c_str(), name.size());
1711 // set_sky(self, bgcolor, type, list, clouds = true)
1712 int ObjectRef::l_set_sky(lua_State *L)
1714 NO_MAP_LOCK_REQUIRED;
1715 ObjectRef *ref = checkobject(L, 1);
1716 RemotePlayer *player = getplayer(ref);
1720 video::SColor bgcolor(255,255,255,255);
1721 read_color(L, 2, &bgcolor);
1723 std::string type = luaL_checkstring(L, 3);
1725 std::vector<std::string> params;
1726 if (lua_istable(L, 4)) {
1728 while (lua_next(L, 4) != 0) {
1729 // key at index -2 and value at index -1
1730 if (lua_isstring(L, -1))
1731 params.emplace_back(readParam<std::string>(L, -1));
1733 params.emplace_back("");
1734 // removes value, keeps key for next iteration
1739 if (type == "skybox" && params.size() != 6)
1740 throw LuaError("skybox expects 6 textures");
1743 if (lua_isboolean(L, 5))
1744 clouds = readParam<bool>(L, 5);
1746 getServer(L)->setSky(player, bgcolor, type, params, clouds);
1747 lua_pushboolean(L, true);
1752 int ObjectRef::l_get_sky(lua_State *L)
1754 NO_MAP_LOCK_REQUIRED;
1755 ObjectRef *ref = checkobject(L, 1);
1756 RemotePlayer *player = getplayer(ref);
1759 video::SColor bgcolor(255, 255, 255, 255);
1761 std::vector<std::string> params;
1764 player->getSky(&bgcolor, &type, ¶ms, &clouds);
1765 type = type.empty() ? "regular" : type;
1767 push_ARGB8(L, bgcolor);
1768 lua_pushlstring(L, type.c_str(), type.size());
1771 for (const std::string ¶m : params) {
1772 lua_pushlstring(L, param.c_str(), param.size());
1773 lua_rawseti(L, -2, i++);
1775 lua_pushboolean(L, clouds);
1779 // set_clouds(self, {density=, color=, ambient=, height=, thickness=, speed=})
1780 int ObjectRef::l_set_clouds(lua_State *L)
1782 NO_MAP_LOCK_REQUIRED;
1783 ObjectRef *ref = checkobject(L, 1);
1784 RemotePlayer *player = getplayer(ref);
1787 if (!lua_istable(L, 2))
1790 CloudParams cloud_params = player->getCloudParams();
1792 cloud_params.density = getfloatfield_default(L, 2, "density", cloud_params.density);
1794 lua_getfield(L, 2, "color");
1795 if (!lua_isnil(L, -1))
1796 read_color(L, -1, &cloud_params.color_bright);
1798 lua_getfield(L, 2, "ambient");
1799 if (!lua_isnil(L, -1))
1800 read_color(L, -1, &cloud_params.color_ambient);
1803 cloud_params.height = getfloatfield_default(L, 2, "height", cloud_params.height );
1804 cloud_params.thickness = getfloatfield_default(L, 2, "thickness", cloud_params.thickness);
1806 lua_getfield(L, 2, "speed");
1807 if (lua_istable(L, -1)) {
1809 new_speed.X = getfloatfield_default(L, -1, "x", 0);
1810 new_speed.Y = getfloatfield_default(L, -1, "z", 0);
1811 cloud_params.speed = new_speed;
1815 getServer(L)->setClouds(player, cloud_params);
1816 lua_pushboolean(L, true);
1820 int ObjectRef::l_get_clouds(lua_State *L)
1822 NO_MAP_LOCK_REQUIRED;
1823 ObjectRef *ref = checkobject(L, 1);
1824 RemotePlayer *player = getplayer(ref);
1827 const CloudParams &cloud_params = player->getCloudParams();
1830 lua_pushnumber(L, cloud_params.density);
1831 lua_setfield(L, -2, "density");
1832 push_ARGB8(L, cloud_params.color_bright);
1833 lua_setfield(L, -2, "color");
1834 push_ARGB8(L, cloud_params.color_ambient);
1835 lua_setfield(L, -2, "ambient");
1836 lua_pushnumber(L, cloud_params.height);
1837 lua_setfield(L, -2, "height");
1838 lua_pushnumber(L, cloud_params.thickness);
1839 lua_setfield(L, -2, "thickness");
1841 lua_pushnumber(L, cloud_params.speed.X);
1842 lua_setfield(L, -2, "x");
1843 lua_pushnumber(L, cloud_params.speed.Y);
1844 lua_setfield(L, -2, "y");
1845 lua_setfield(L, -2, "speed");
1851 // override_day_night_ratio(self, brightness=0...1)
1852 int ObjectRef::l_override_day_night_ratio(lua_State *L)
1854 NO_MAP_LOCK_REQUIRED;
1855 ObjectRef *ref = checkobject(L, 1);
1856 RemotePlayer *player = getplayer(ref);
1860 bool do_override = false;
1862 if (!lua_isnil(L, 2)) {
1864 ratio = readParam<float>(L, 2);
1867 if (!getServer(L)->overrideDayNightRatio(player, do_override, ratio))
1870 lua_pushboolean(L, true);
1874 // get_day_night_ratio(self)
1875 int ObjectRef::l_get_day_night_ratio(lua_State *L)
1877 NO_MAP_LOCK_REQUIRED;
1878 ObjectRef *ref = checkobject(L, 1);
1879 RemotePlayer *player = getplayer(ref);
1885 player->getDayNightRatio(&do_override, &ratio);
1888 lua_pushnumber(L, ratio);
1895 ObjectRef::ObjectRef(ServerActiveObject *object):
1898 //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
1901 // Creates an ObjectRef and leaves it on top of stack
1902 // Not callable from Lua; all references are created on the C side.
1903 void ObjectRef::create(lua_State *L, ServerActiveObject *object)
1905 ObjectRef *o = new ObjectRef(object);
1906 //infostream<<"ObjectRef::create: o="<<o<<std::endl;
1907 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1908 luaL_getmetatable(L, className);
1909 lua_setmetatable(L, -2);
1912 void ObjectRef::set_null(lua_State *L)
1914 ObjectRef *o = checkobject(L, -1);
1918 void ObjectRef::Register(lua_State *L)
1921 int methodtable = lua_gettop(L);
1922 luaL_newmetatable(L, className);
1923 int metatable = lua_gettop(L);
1925 lua_pushliteral(L, "__metatable");
1926 lua_pushvalue(L, methodtable);
1927 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
1929 lua_pushliteral(L, "__index");
1930 lua_pushvalue(L, methodtable);
1931 lua_settable(L, metatable);
1933 lua_pushliteral(L, "__gc");
1934 lua_pushcfunction(L, gc_object);
1935 lua_settable(L, metatable);
1937 lua_pop(L, 1); // drop metatable
1939 markAliasDeprecated(methods);
1940 luaL_openlib(L, 0, methods, 0); // fill methodtable
1941 lua_pop(L, 1); // drop methodtable
1943 // Cannot be created from Lua
1944 //lua_register(L, className, create_object);
1947 const char ObjectRef::className[] = "ObjectRef";
1948 luaL_Reg ObjectRef::methods[] = {
1949 // ServerActiveObject
1950 luamethod(ObjectRef, remove),
1951 luamethod_aliased(ObjectRef, get_pos, getpos),
1952 luamethod_aliased(ObjectRef, set_pos, setpos),
1953 luamethod_aliased(ObjectRef, move_to, moveto),
1954 luamethod(ObjectRef, punch),
1955 luamethod(ObjectRef, right_click),
1956 luamethod(ObjectRef, set_hp),
1957 luamethod(ObjectRef, get_hp),
1958 luamethod(ObjectRef, get_inventory),
1959 luamethod(ObjectRef, get_wield_list),
1960 luamethod(ObjectRef, get_wield_index),
1961 luamethod(ObjectRef, get_wielded_item),
1962 luamethod(ObjectRef, set_wielded_item),
1963 luamethod(ObjectRef, set_armor_groups),
1964 luamethod(ObjectRef, get_armor_groups),
1965 luamethod(ObjectRef, set_animation),
1966 luamethod(ObjectRef, get_animation),
1967 luamethod(ObjectRef, set_animation_frame_speed),
1968 luamethod(ObjectRef, set_bone_position),
1969 luamethod(ObjectRef, get_bone_position),
1970 luamethod(ObjectRef, set_attach),
1971 luamethod(ObjectRef, get_attach),
1972 luamethod(ObjectRef, set_detach),
1973 luamethod(ObjectRef, set_properties),
1974 luamethod(ObjectRef, get_properties),
1975 luamethod(ObjectRef, set_nametag_attributes),
1976 luamethod(ObjectRef, get_nametag_attributes),
1977 // LuaEntitySAO-only
1978 luamethod_aliased(ObjectRef, set_velocity, setvelocity),
1979 luamethod(ObjectRef, add_velocity),
1980 luamethod_aliased(ObjectRef, get_velocity, getvelocity),
1981 luamethod_aliased(ObjectRef, set_acceleration, setacceleration),
1982 luamethod_aliased(ObjectRef, get_acceleration, getacceleration),
1983 luamethod_aliased(ObjectRef, set_yaw, setyaw),
1984 luamethod_aliased(ObjectRef, get_yaw, getyaw),
1985 luamethod(ObjectRef, set_rotation),
1986 luamethod(ObjectRef, get_rotation),
1987 luamethod_aliased(ObjectRef, set_texture_mod, settexturemod),
1988 luamethod_aliased(ObjectRef, set_sprite, setsprite),
1989 luamethod(ObjectRef, get_entity_name),
1990 luamethod(ObjectRef, get_luaentity),
1992 luamethod(ObjectRef, is_player),
1993 luamethod(ObjectRef, is_player_connected),
1994 luamethod(ObjectRef, get_player_name),
1995 luamethod(ObjectRef, get_player_velocity),
1996 luamethod(ObjectRef, add_player_velocity),
1997 luamethod(ObjectRef, get_look_dir),
1998 luamethod(ObjectRef, get_look_pitch),
1999 luamethod(ObjectRef, get_look_yaw),
2000 luamethod(ObjectRef, get_look_vertical),
2001 luamethod(ObjectRef, get_look_horizontal),
2002 luamethod(ObjectRef, set_look_horizontal),
2003 luamethod(ObjectRef, set_look_vertical),
2004 luamethod(ObjectRef, set_look_yaw),
2005 luamethod(ObjectRef, set_look_pitch),
2006 luamethod(ObjectRef, get_fov),
2007 luamethod(ObjectRef, set_fov),
2008 luamethod(ObjectRef, get_breath),
2009 luamethod(ObjectRef, set_breath),
2010 luamethod(ObjectRef, get_attribute),
2011 luamethod(ObjectRef, set_attribute),
2012 luamethod(ObjectRef, get_meta),
2013 luamethod(ObjectRef, set_inventory_formspec),
2014 luamethod(ObjectRef, get_inventory_formspec),
2015 luamethod(ObjectRef, set_formspec_prepend),
2016 luamethod(ObjectRef, get_formspec_prepend),
2017 luamethod(ObjectRef, get_player_control),
2018 luamethod(ObjectRef, get_player_control_bits),
2019 luamethod(ObjectRef, set_physics_override),
2020 luamethod(ObjectRef, get_physics_override),
2021 luamethod(ObjectRef, hud_add),
2022 luamethod(ObjectRef, hud_remove),
2023 luamethod(ObjectRef, hud_change),
2024 luamethod(ObjectRef, hud_get),
2025 luamethod(ObjectRef, hud_set_flags),
2026 luamethod(ObjectRef, hud_get_flags),
2027 luamethod(ObjectRef, hud_set_hotbar_itemcount),
2028 luamethod(ObjectRef, hud_get_hotbar_itemcount),
2029 luamethod(ObjectRef, hud_set_hotbar_image),
2030 luamethod(ObjectRef, hud_get_hotbar_image),
2031 luamethod(ObjectRef, hud_set_hotbar_selected_image),
2032 luamethod(ObjectRef, hud_get_hotbar_selected_image),
2033 luamethod(ObjectRef, set_sky),
2034 luamethod(ObjectRef, get_sky),
2035 luamethod(ObjectRef, set_clouds),
2036 luamethod(ObjectRef, get_clouds),
2037 luamethod(ObjectRef, override_day_night_ratio),
2038 luamethod(ObjectRef, get_day_night_ratio),
2039 luamethod(ObjectRef, set_local_animation),
2040 luamethod(ObjectRef, get_local_animation),
2041 luamethod(ObjectRef, set_eye_offset),
2042 luamethod(ObjectRef, get_eye_offset),
2043 luamethod(ObjectRef, send_mapblock),