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"
21 #include "lua_api/l_internal.h"
22 #include "lua_api/l_inventory.h"
23 #include "lua_api/l_item.h"
24 #include "common/c_converter.h"
25 #include "common/c_content.h"
28 #include "serverobject.h"
29 #include "content_sao.h"
32 #include "scripting_game.h"
34 struct EnumString es_HudElementType[] =
36 {HUD_ELEM_IMAGE, "image"},
37 {HUD_ELEM_TEXT, "text"},
38 {HUD_ELEM_STATBAR, "statbar"},
39 {HUD_ELEM_INVENTORY, "inventory"},
40 {HUD_ELEM_WAYPOINT, "waypoint"},
44 struct EnumString es_HudElementStat[] =
46 {HUD_STAT_POS, "position"},
47 {HUD_STAT_POS, "pos"}, /* Deprecated, only for compatibility's sake */
48 {HUD_STAT_NAME, "name"},
49 {HUD_STAT_SCALE, "scale"},
50 {HUD_STAT_TEXT, "text"},
51 {HUD_STAT_NUMBER, "number"},
52 {HUD_STAT_ITEM, "item"},
53 {HUD_STAT_DIR, "direction"},
54 {HUD_STAT_ALIGN, "alignment"},
55 {HUD_STAT_OFFSET, "offset"},
56 {HUD_STAT_WORLD_POS, "world_pos"},
60 struct EnumString es_HudBuiltinElement[] =
62 {HUD_FLAG_HOTBAR_VISIBLE, "hotbar"},
63 {HUD_FLAG_HEALTHBAR_VISIBLE, "healthbar"},
64 {HUD_FLAG_CROSSHAIR_VISIBLE, "crosshair"},
65 {HUD_FLAG_WIELDITEM_VISIBLE, "wielditem"},
66 {HUD_FLAG_BREATHBAR_VISIBLE, "breathbar"},
67 {HUD_FLAG_MINIMAP_VISIBLE, "minimap"},
76 ObjectRef* ObjectRef::checkobject(lua_State *L, int narg)
78 luaL_checktype(L, narg, LUA_TUSERDATA);
79 void *ud = luaL_checkudata(L, narg, className);
80 if (!ud) luaL_typerror(L, narg, className);
81 return *(ObjectRef**)ud; // unbox pointer
84 ServerActiveObject* ObjectRef::getobject(ObjectRef *ref)
86 ServerActiveObject *co = ref->m_object;
90 LuaEntitySAO* ObjectRef::getluaobject(ObjectRef *ref)
92 ServerActiveObject *obj = getobject(ref);
95 if (obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY)
97 return (LuaEntitySAO*)obj;
100 PlayerSAO* ObjectRef::getplayersao(ObjectRef *ref)
102 ServerActiveObject *obj = getobject(ref);
105 if (obj->getType() != ACTIVEOBJECT_TYPE_PLAYER)
107 return (PlayerSAO*)obj;
110 RemotePlayer *ObjectRef::getplayer(ObjectRef *ref)
112 PlayerSAO *playersao = getplayersao(ref);
113 if (playersao == NULL)
115 return playersao->getPlayer();
118 // Exported functions
121 int ObjectRef::gc_object(lua_State *L) {
122 ObjectRef *o = *(ObjectRef **)(lua_touserdata(L, 1));
123 //infostream<<"ObjectRef::gc_object: o="<<o<<std::endl;
129 int ObjectRef::l_remove(lua_State *L)
133 ObjectRef *ref = checkobject(L, 1);
134 ServerActiveObject *co = getobject(ref);
137 if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
140 const UNORDERED_SET<int> &child_ids = co->getAttachmentChildIds();
141 UNORDERED_SET<int>::const_iterator it;
142 for (it = child_ids.begin(); it != child_ids.end(); ++it) {
143 // Child can be NULL if it was deleted earlier
144 if (ServerActiveObject *child = env->getActiveObject(*it))
145 child->setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0));
148 verbosestream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
149 co->m_removed = true;
154 // returns: {x=num, y=num, z=num}
155 int ObjectRef::l_getpos(lua_State *L)
157 NO_MAP_LOCK_REQUIRED;
158 ObjectRef *ref = checkobject(L, 1);
159 ServerActiveObject *co = getobject(ref);
160 if (co == NULL) return 0;
161 v3f pos = co->getBasePosition() / BS;
163 lua_pushnumber(L, pos.X);
164 lua_setfield(L, -2, "x");
165 lua_pushnumber(L, pos.Y);
166 lua_setfield(L, -2, "y");
167 lua_pushnumber(L, pos.Z);
168 lua_setfield(L, -2, "z");
173 int ObjectRef::l_setpos(lua_State *L)
175 NO_MAP_LOCK_REQUIRED;
176 ObjectRef *ref = checkobject(L, 1);
177 //LuaEntitySAO *co = getluaobject(ref);
178 ServerActiveObject *co = getobject(ref);
179 if (co == NULL) return 0;
181 v3f pos = checkFloatPos(L, 2);
187 // moveto(self, pos, continuous=false)
188 int ObjectRef::l_moveto(lua_State *L)
190 NO_MAP_LOCK_REQUIRED;
191 ObjectRef *ref = checkobject(L, 1);
192 //LuaEntitySAO *co = getluaobject(ref);
193 ServerActiveObject *co = getobject(ref);
194 if (co == NULL) return 0;
196 v3f pos = checkFloatPos(L, 2);
198 bool continuous = lua_toboolean(L, 3);
200 co->moveTo(pos, continuous);
204 // punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
205 int ObjectRef::l_punch(lua_State *L)
207 NO_MAP_LOCK_REQUIRED;
208 ObjectRef *ref = checkobject(L, 1);
209 ObjectRef *puncher_ref = checkobject(L, 2);
210 ServerActiveObject *co = getobject(ref);
211 ServerActiveObject *puncher = getobject(puncher_ref);
212 if (co == NULL) return 0;
213 if (puncher == NULL) return 0;
215 if (lua_type(L, 5) != LUA_TTABLE)
216 dir = co->getBasePosition() - puncher->getBasePosition();
218 dir = read_v3f(L, 5);
219 float time_from_last_punch = 1000000;
220 if (lua_isnumber(L, 3))
221 time_from_last_punch = lua_tonumber(L, 3);
222 ToolCapabilities toolcap = read_tool_capabilities(L, 4);
225 s16 src_original_hp = co->getHP();
226 s16 dst_origin_hp = puncher->getHP();
229 co->punch(dir, &toolcap, puncher, time_from_last_punch);
231 // If the punched is a player, and its HP changed
232 if (src_original_hp != co->getHP() &&
233 co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
234 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co);
237 // If the puncher is a player, and its HP changed
238 if (dst_origin_hp != puncher->getHP() &&
239 puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
240 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)puncher);
245 // right_click(self, clicker); clicker = an another ObjectRef
246 int ObjectRef::l_right_click(lua_State *L)
248 NO_MAP_LOCK_REQUIRED;
249 ObjectRef *ref = checkobject(L, 1);
250 ObjectRef *ref2 = checkobject(L, 2);
251 ServerActiveObject *co = getobject(ref);
252 ServerActiveObject *co2 = getobject(ref2);
253 if (co == NULL) return 0;
254 if (co2 == NULL) return 0;
261 // hp = number of hitpoints (2 * number of hearts)
263 int ObjectRef::l_set_hp(lua_State *L)
265 NO_MAP_LOCK_REQUIRED;
266 ObjectRef *ref = checkobject(L, 1);
267 luaL_checknumber(L, 2);
268 ServerActiveObject *co = getobject(ref);
269 if (co == NULL) return 0;
270 int hp = lua_tonumber(L, 2);
271 /*infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
272 <<" hp="<<hp<<std::endl;*/
275 if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
276 getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co);
283 // returns: number of hitpoints (2 * number of hearts)
284 // 0 if not applicable to this type of object
285 int ObjectRef::l_get_hp(lua_State *L)
287 NO_MAP_LOCK_REQUIRED;
288 ObjectRef *ref = checkobject(L, 1);
289 ServerActiveObject *co = getobject(ref);
292 lua_pushnumber(L, 1);
295 int hp = co->getHP();
296 /*infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
297 <<" hp="<<hp<<std::endl;*/
299 lua_pushnumber(L, hp);
303 // get_inventory(self)
304 int ObjectRef::l_get_inventory(lua_State *L)
306 NO_MAP_LOCK_REQUIRED;
307 ObjectRef *ref = checkobject(L, 1);
308 ServerActiveObject *co = getobject(ref);
309 if (co == NULL) return 0;
311 InventoryLocation loc = co->getInventoryLocation();
312 if (getServer(L)->getInventory(loc) != NULL)
313 InvRef::create(L, loc);
315 lua_pushnil(L); // An object may have no inventory (nil)
319 // get_wield_list(self)
320 int ObjectRef::l_get_wield_list(lua_State *L)
322 NO_MAP_LOCK_REQUIRED;
323 ObjectRef *ref = checkobject(L, 1);
324 ServerActiveObject *co = getobject(ref);
325 if (co == NULL) return 0;
327 lua_pushstring(L, co->getWieldList().c_str());
331 // get_wield_index(self)
332 int ObjectRef::l_get_wield_index(lua_State *L)
334 NO_MAP_LOCK_REQUIRED;
335 ObjectRef *ref = checkobject(L, 1);
336 ServerActiveObject *co = getobject(ref);
337 if (co == NULL) return 0;
339 lua_pushinteger(L, co->getWieldIndex() + 1);
343 // get_wielded_item(self)
344 int ObjectRef::l_get_wielded_item(lua_State *L)
346 NO_MAP_LOCK_REQUIRED;
347 ObjectRef *ref = checkobject(L, 1);
348 ServerActiveObject *co = getobject(ref);
351 LuaItemStack::create(L, ItemStack());
355 LuaItemStack::create(L, co->getWieldedItem());
359 // set_wielded_item(self, itemstack or itemstring or table or nil)
360 int ObjectRef::l_set_wielded_item(lua_State *L)
362 NO_MAP_LOCK_REQUIRED;
363 ObjectRef *ref = checkobject(L, 1);
364 ServerActiveObject *co = getobject(ref);
365 if (co == NULL) return 0;
367 ItemStack item = read_item(L, 2, getServer(L));
368 bool success = co->setWieldedItem(item);
369 if (success && co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
370 getServer(L)->SendInventory(((PlayerSAO*)co));
372 lua_pushboolean(L, success);
376 // set_armor_groups(self, groups)
377 int ObjectRef::l_set_armor_groups(lua_State *L)
379 NO_MAP_LOCK_REQUIRED;
380 ObjectRef *ref = checkobject(L, 1);
381 ServerActiveObject *co = getobject(ref);
382 if (co == NULL) return 0;
384 ItemGroupList groups;
385 read_groups(L, 2, groups);
386 co->setArmorGroups(groups);
390 // get_armor_groups(self)
391 int ObjectRef::l_get_armor_groups(lua_State *L)
393 NO_MAP_LOCK_REQUIRED;
394 ObjectRef *ref = checkobject(L, 1);
395 ServerActiveObject *co = getobject(ref);
399 push_groups(L, co->getArmorGroups());
403 // set_physics_override(self, physics_override_speed, physics_override_jump,
404 // physics_override_gravity, sneak, sneak_glitch)
405 int ObjectRef::l_set_physics_override(lua_State *L)
407 NO_MAP_LOCK_REQUIRED;
408 ObjectRef *ref = checkobject(L, 1);
409 PlayerSAO *co = (PlayerSAO *) getobject(ref);
410 if (co == NULL) return 0;
412 if (lua_istable(L, 2)) {
413 co->m_physics_override_speed = getfloatfield_default(L, 2, "speed", co->m_physics_override_speed);
414 co->m_physics_override_jump = getfloatfield_default(L, 2, "jump", co->m_physics_override_jump);
415 co->m_physics_override_gravity = getfloatfield_default(L, 2, "gravity", co->m_physics_override_gravity);
416 co->m_physics_override_sneak = getboolfield_default(L, 2, "sneak", co->m_physics_override_sneak);
417 co->m_physics_override_sneak_glitch = getboolfield_default(L, 2, "sneak_glitch", co->m_physics_override_sneak_glitch);
418 co->m_physics_override_sent = false;
420 // old, non-table format
421 if (!lua_isnil(L, 2)) {
422 co->m_physics_override_speed = lua_tonumber(L, 2);
423 co->m_physics_override_sent = false;
425 if (!lua_isnil(L, 3)) {
426 co->m_physics_override_jump = lua_tonumber(L, 3);
427 co->m_physics_override_sent = false;
429 if (!lua_isnil(L, 4)) {
430 co->m_physics_override_gravity = lua_tonumber(L, 4);
431 co->m_physics_override_sent = false;
437 // get_physics_override(self)
438 int ObjectRef::l_get_physics_override(lua_State *L)
440 NO_MAP_LOCK_REQUIRED;
441 ObjectRef *ref = checkobject(L, 1);
442 PlayerSAO *co = (PlayerSAO *)getobject(ref);
447 lua_pushnumber(L, co->m_physics_override_speed);
448 lua_setfield(L, -2, "speed");
449 lua_pushnumber(L, co->m_physics_override_jump);
450 lua_setfield(L, -2, "jump");
451 lua_pushnumber(L, co->m_physics_override_gravity);
452 lua_setfield(L, -2, "gravity");
453 lua_pushboolean(L, co->m_physics_override_sneak);
454 lua_setfield(L, -2, "sneak");
455 lua_pushboolean(L, co->m_physics_override_sneak_glitch);
456 lua_setfield(L, -2, "sneak_glitch");
460 // set_animation(self, frame_range, frame_speed, frame_blend, frame_loop)
461 int ObjectRef::l_set_animation(lua_State *L)
463 NO_MAP_LOCK_REQUIRED;
464 ObjectRef *ref = checkobject(L, 1);
465 ServerActiveObject *co = getobject(ref);
466 if (co == NULL) return 0;
468 v2f frames = v2f(1, 1);
469 if (!lua_isnil(L, 2))
470 frames = read_v2f(L, 2);
471 float frame_speed = 15;
472 if (!lua_isnil(L, 3))
473 frame_speed = lua_tonumber(L, 3);
474 float frame_blend = 0;
475 if (!lua_isnil(L, 4))
476 frame_blend = lua_tonumber(L, 4);
477 bool frame_loop = true;
478 if (lua_isboolean(L, 5))
479 frame_loop = lua_toboolean(L, 5);
480 co->setAnimation(frames, frame_speed, frame_blend, frame_loop);
484 // get_animation(self)
485 int ObjectRef::l_get_animation(lua_State *L)
487 NO_MAP_LOCK_REQUIRED;
488 ObjectRef *ref = checkobject(L, 1);
489 ServerActiveObject *co = getobject(ref);
493 v2f frames = v2f(1,1);
494 float frame_speed = 15;
495 float frame_blend = 0;
496 bool frame_loop = true;
497 co->getAnimation(&frames, &frame_speed, &frame_blend, &frame_loop);
500 lua_pushnumber(L, frame_speed);
501 lua_pushnumber(L, frame_blend);
502 lua_pushboolean(L, frame_loop);
506 // set_local_animation(self, {stand/idle}, {walk}, {dig}, {walk+dig}, frame_speed)
507 int ObjectRef::l_set_local_animation(lua_State *L)
509 NO_MAP_LOCK_REQUIRED;
510 ObjectRef *ref = checkobject(L, 1);
511 RemotePlayer *player = getplayer(ref);
516 for (int i=0;i<4;i++) {
517 if (!lua_isnil(L, 2+1))
518 frames[i] = read_v2s32(L, 2+i);
520 float frame_speed = 30;
521 if (!lua_isnil(L, 6))
522 frame_speed = lua_tonumber(L, 6);
524 if (!getServer(L)->setLocalPlayerAnimations(player, frames, frame_speed))
527 lua_pushboolean(L, true);
531 // get_local_animation(self)
532 int ObjectRef::l_get_local_animation(lua_State *L)
535 ObjectRef *ref = checkobject(L, 1);
536 RemotePlayer *player = getplayer(ref);
542 player->getLocalAnimations(frames, &frame_speed);
544 for (int i = 0; i < 4; i++) {
545 push_v2s32(L, frames[i]);
548 lua_pushnumber(L, frame_speed);
552 // set_eye_offset(self, v3f first pv, v3f third pv)
553 int ObjectRef::l_set_eye_offset(lua_State *L)
555 NO_MAP_LOCK_REQUIRED;
556 ObjectRef *ref = checkobject(L, 1);
557 RemotePlayer *player = getplayer(ref);
561 v3f offset_first = v3f(0, 0, 0);
562 v3f offset_third = v3f(0, 0, 0);
564 if (!lua_isnil(L, 2))
565 offset_first = read_v3f(L, 2);
566 if (!lua_isnil(L, 3))
567 offset_third = read_v3f(L, 3);
569 // Prevent abuse of offset values (keep player always visible)
570 offset_third.X = rangelim(offset_third.X,-10,10);
571 offset_third.Z = rangelim(offset_third.Z,-5,5);
572 /* TODO: if possible: improve the camera colision detetion to allow Y <= -1.5) */
573 offset_third.Y = rangelim(offset_third.Y,-10,15); //1.5*BS
575 if (!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 // set_bone_position(self, std::string bone, v3f position, v3f rotation)
597 int ObjectRef::l_set_bone_position(lua_State *L)
599 NO_MAP_LOCK_REQUIRED;
600 ObjectRef *ref = checkobject(L, 1);
601 ServerActiveObject *co = getobject(ref);
602 if (co == NULL) return 0;
604 std::string bone = "";
605 if (!lua_isnil(L, 2))
606 bone = lua_tostring(L, 2);
607 v3f position = v3f(0, 0, 0);
608 if (!lua_isnil(L, 3))
609 position = check_v3f(L, 3);
610 v3f rotation = v3f(0, 0, 0);
611 if (!lua_isnil(L, 4))
612 rotation = check_v3f(L, 4);
613 co->setBonePosition(bone, position, rotation);
617 // get_bone_position(self, bone)
618 int ObjectRef::l_get_bone_position(lua_State *L)
620 NO_MAP_LOCK_REQUIRED;
621 ObjectRef *ref = checkobject(L, 1);
622 ServerActiveObject *co = getobject(ref);
626 std::string bone = "";
627 if (!lua_isnil(L, 2))
628 bone = lua_tostring(L, 2);
630 v3f position = v3f(0, 0, 0);
631 v3f rotation = v3f(0, 0, 0);
632 co->getBonePosition(bone, &position, &rotation);
634 push_v3f(L, position);
635 push_v3f(L, rotation);
639 // set_attach(self, parent, bone, position, rotation)
640 int ObjectRef::l_set_attach(lua_State *L)
644 ObjectRef *ref = checkobject(L, 1);
645 ObjectRef *parent_ref = checkobject(L, 2);
646 ServerActiveObject *co = getobject(ref);
647 ServerActiveObject *parent = getobject(parent_ref);
654 std::string bone = "";
655 v3f position = v3f(0, 0, 0);
656 v3f rotation = v3f(0, 0, 0);
657 co->getAttachment(&parent_id, &bone, &position, &rotation);
659 ServerActiveObject *old_parent = env->getActiveObject(parent_id);
660 old_parent->removeAttachmentChild(co->getId());
664 if (!lua_isnil(L, 3))
665 bone = lua_tostring(L, 3);
666 position = v3f(0, 0, 0);
667 if (!lua_isnil(L, 4))
668 position = read_v3f(L, 4);
669 rotation = v3f(0, 0, 0);
670 if (!lua_isnil(L, 5))
671 rotation = read_v3f(L, 5);
672 co->setAttachment(parent->getId(), bone, position, rotation);
673 parent->addAttachmentChild(co->getId());
678 int ObjectRef::l_get_attach(lua_State *L)
682 ObjectRef *ref = checkobject(L, 1);
683 ServerActiveObject *co = getobject(ref);
689 std::string bone = "";
690 v3f position = v3f(0, 0, 0);
691 v3f rotation = v3f(0, 0, 0);
692 co->getAttachment(&parent_id, &bone, &position, &rotation);
695 ServerActiveObject *parent = env->getActiveObject(parent_id);
697 getScriptApiBase(L)->objectrefGetOrCreate(L, parent);
698 lua_pushlstring(L, bone.c_str(), bone.size());
699 push_v3f(L, position);
700 push_v3f(L, rotation);
705 int ObjectRef::l_set_detach(lua_State *L)
709 ObjectRef *ref = checkobject(L, 1);
710 ServerActiveObject *co = getobject(ref);
715 std::string bone = "";
718 co->getAttachment(&parent_id, &bone, &position, &rotation);
719 ServerActiveObject *parent = NULL;
721 parent = env->getActiveObject(parent_id);
724 co->setAttachment(0, "", v3f(0,0,0), v3f(0,0,0));
726 parent->removeAttachmentChild(co->getId());
730 // set_properties(self, properties)
731 int ObjectRef::l_set_properties(lua_State *L)
733 NO_MAP_LOCK_REQUIRED;
734 ObjectRef *ref = checkobject(L, 1);
735 ServerActiveObject *co = getobject(ref);
736 if (co == NULL) return 0;
737 ObjectProperties *prop = co->accessObjectProperties();
740 read_object_properties(L, 2, prop);
741 co->notifyObjectPropertiesModified();
745 // get_properties(self)
746 int ObjectRef::l_get_properties(lua_State *L)
748 NO_MAP_LOCK_REQUIRED;
749 ObjectRef *ref = checkobject(L, 1);
750 ServerActiveObject *co = getobject(ref);
753 ObjectProperties *prop = co->accessObjectProperties();
756 push_object_properties(L, prop);
761 int ObjectRef::l_is_player(lua_State *L)
763 NO_MAP_LOCK_REQUIRED;
764 ObjectRef *ref = checkobject(L, 1);
765 RemotePlayer *player = getplayer(ref);
766 lua_pushboolean(L, (player != NULL));
770 // set_nametag_attributes(self, attributes)
771 int ObjectRef::l_set_nametag_attributes(lua_State *L)
773 NO_MAP_LOCK_REQUIRED;
774 ObjectRef *ref = checkobject(L, 1);
775 ServerActiveObject *co = getobject(ref);
779 ObjectProperties *prop = co->accessObjectProperties();
783 lua_getfield(L, 2, "color");
784 if (!lua_isnil(L, -1)) {
785 video::SColor color = prop->nametag_color;
786 read_color(L, -1, &color);
787 prop->nametag_color = color;
791 std::string nametag = getstringfield_default(L, 2, "text", "");
792 prop->nametag = nametag;
794 co->notifyObjectPropertiesModified();
795 lua_pushboolean(L, true);
799 // get_nametag_attributes(self)
800 int ObjectRef::l_get_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 video::SColor color = prop->nametag_color;
815 push_ARGB8(L, color);
816 lua_setfield(L, -2, "color");
817 lua_pushstring(L, prop->nametag.c_str());
818 lua_setfield(L, -2, "text");
822 /* LuaEntitySAO-only */
824 // setvelocity(self, {x=num, y=num, z=num})
825 int ObjectRef::l_setvelocity(lua_State *L)
827 NO_MAP_LOCK_REQUIRED;
828 ObjectRef *ref = checkobject(L, 1);
829 LuaEntitySAO *co = getluaobject(ref);
830 if (co == NULL) return 0;
831 v3f pos = checkFloatPos(L, 2);
833 co->setVelocity(pos);
838 int ObjectRef::l_getvelocity(lua_State *L)
840 NO_MAP_LOCK_REQUIRED;
841 ObjectRef *ref = checkobject(L, 1);
842 LuaEntitySAO *co = getluaobject(ref);
843 if (co == NULL) return 0;
845 v3f v = co->getVelocity();
850 // setacceleration(self, {x=num, y=num, z=num})
851 int ObjectRef::l_setacceleration(lua_State *L)
853 NO_MAP_LOCK_REQUIRED;
854 ObjectRef *ref = checkobject(L, 1);
855 LuaEntitySAO *co = getluaobject(ref);
856 if (co == NULL) return 0;
858 v3f pos = checkFloatPos(L, 2);
860 co->setAcceleration(pos);
864 // getacceleration(self)
865 int ObjectRef::l_getacceleration(lua_State *L)
867 NO_MAP_LOCK_REQUIRED;
868 ObjectRef *ref = checkobject(L, 1);
869 LuaEntitySAO *co = getluaobject(ref);
870 if (co == NULL) return 0;
872 v3f v = co->getAcceleration();
877 // setyaw(self, radians)
878 int ObjectRef::l_setyaw(lua_State *L)
880 NO_MAP_LOCK_REQUIRED;
881 ObjectRef *ref = checkobject(L, 1);
882 LuaEntitySAO *co = getluaobject(ref);
883 if (co == NULL) return 0;
884 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
891 int ObjectRef::l_getyaw(lua_State *L)
893 NO_MAP_LOCK_REQUIRED;
894 ObjectRef *ref = checkobject(L, 1);
895 LuaEntitySAO *co = getluaobject(ref);
896 if (co == NULL) return 0;
898 float yaw = co->getYaw() * core::DEGTORAD;
899 lua_pushnumber(L, yaw);
903 // settexturemod(self, mod)
904 int ObjectRef::l_settexturemod(lua_State *L)
906 NO_MAP_LOCK_REQUIRED;
907 ObjectRef *ref = checkobject(L, 1);
908 LuaEntitySAO *co = getluaobject(ref);
909 if (co == NULL) return 0;
911 std::string mod = luaL_checkstring(L, 2);
912 co->setTextureMod(mod);
916 // setsprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
917 // select_horiz_by_yawpitch=false)
918 int ObjectRef::l_setsprite(lua_State *L)
920 NO_MAP_LOCK_REQUIRED;
921 ObjectRef *ref = checkobject(L, 1);
922 LuaEntitySAO *co = getluaobject(ref);
923 if (co == NULL) return 0;
926 if (!lua_isnil(L, 2))
927 p = read_v2s16(L, 2);
929 if (!lua_isnil(L, 3))
930 num_frames = lua_tonumber(L, 3);
931 float framelength = 0.2;
932 if (!lua_isnil(L, 4))
933 framelength = lua_tonumber(L, 4);
934 bool select_horiz_by_yawpitch = false;
935 if (!lua_isnil(L, 5))
936 select_horiz_by_yawpitch = lua_toboolean(L, 5);
937 co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
942 // get_entity_name(self)
943 int ObjectRef::l_get_entity_name(lua_State *L)
945 NO_MAP_LOCK_REQUIRED;
946 ObjectRef *ref = checkobject(L, 1);
947 LuaEntitySAO *co = getluaobject(ref);
948 log_deprecated(L,"Deprecated call to \"get_entity_name");
949 if (co == NULL) return 0;
951 std::string name = co->getName();
952 lua_pushstring(L, name.c_str());
956 // get_luaentity(self)
957 int ObjectRef::l_get_luaentity(lua_State *L)
959 NO_MAP_LOCK_REQUIRED;
960 ObjectRef *ref = checkobject(L, 1);
961 LuaEntitySAO *co = getluaobject(ref);
962 if (co == NULL) return 0;
964 luaentity_get(L, co->getId());
970 // is_player_connected(self)
971 int ObjectRef::l_is_player_connected(lua_State *L)
973 NO_MAP_LOCK_REQUIRED;
974 ObjectRef *ref = checkobject(L, 1);
975 RemotePlayer *player = getplayer(ref);
976 lua_pushboolean(L, (player != NULL && player->peer_id != 0));
980 // get_player_name(self)
981 int ObjectRef::l_get_player_name(lua_State *L)
983 NO_MAP_LOCK_REQUIRED;
984 ObjectRef *ref = checkobject(L, 1);
985 RemotePlayer *player = getplayer(ref);
986 if (player == NULL) {
987 lua_pushlstring(L, "", 0);
991 lua_pushstring(L, player->getName());
995 // get_player_velocity(self)
996 int ObjectRef::l_get_player_velocity(lua_State *L)
998 NO_MAP_LOCK_REQUIRED;
999 ObjectRef *ref = checkobject(L, 1);
1000 RemotePlayer *player = getplayer(ref);
1001 if (player == NULL) {
1006 push_v3f(L, player->getSpeed() / BS);
1010 // get_look_dir(self)
1011 int ObjectRef::l_get_look_dir(lua_State *L)
1013 NO_MAP_LOCK_REQUIRED;
1014 ObjectRef *ref = checkobject(L, 1);
1015 PlayerSAO* co = getplayersao(ref);
1016 if (co == NULL) return 0;
1018 float pitch = co->getRadPitchDep();
1019 float yaw = co->getRadYawDep();
1020 v3f v(cos(pitch)*cos(yaw), sin(pitch), cos(pitch)*sin(yaw));
1026 // get_look_pitch(self)
1027 int ObjectRef::l_get_look_pitch(lua_State *L)
1029 NO_MAP_LOCK_REQUIRED;
1032 "Deprecated call to get_look_pitch, use get_look_vertical instead");
1034 ObjectRef *ref = checkobject(L, 1);
1035 PlayerSAO* co = getplayersao(ref);
1036 if (co == NULL) return 0;
1038 lua_pushnumber(L, co->getRadPitchDep());
1043 // get_look_yaw(self)
1044 int ObjectRef::l_get_look_yaw(lua_State *L)
1046 NO_MAP_LOCK_REQUIRED;
1049 "Deprecated call to get_look_yaw, use get_look_horizontal instead");
1051 ObjectRef *ref = checkobject(L, 1);
1052 PlayerSAO* co = getplayersao(ref);
1053 if (co == NULL) return 0;
1055 lua_pushnumber(L, co->getRadYawDep());
1059 // get_look_pitch2(self)
1060 int ObjectRef::l_get_look_vertical(lua_State *L)
1062 NO_MAP_LOCK_REQUIRED;
1063 ObjectRef *ref = checkobject(L, 1);
1064 PlayerSAO* co = getplayersao(ref);
1065 if (co == NULL) return 0;
1067 lua_pushnumber(L, co->getRadPitch());
1071 // get_look_yaw2(self)
1072 int ObjectRef::l_get_look_horizontal(lua_State *L)
1074 NO_MAP_LOCK_REQUIRED;
1075 ObjectRef *ref = checkobject(L, 1);
1076 PlayerSAO* co = getplayersao(ref);
1077 if (co == NULL) return 0;
1079 lua_pushnumber(L, co->getRadYaw());
1083 // set_look_vertical(self, radians)
1084 int ObjectRef::l_set_look_vertical(lua_State *L)
1086 NO_MAP_LOCK_REQUIRED;
1087 ObjectRef *ref = checkobject(L, 1);
1088 PlayerSAO* co = getplayersao(ref);
1089 if (co == NULL) return 0;
1090 float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
1092 co->setPitchAndSend(pitch);
1096 // set_look_horizontal(self, radians)
1097 int ObjectRef::l_set_look_horizontal(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;
1103 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
1105 co->setYawAndSend(yaw);
1110 // set_look_pitch(self, radians)
1111 int ObjectRef::l_set_look_pitch(lua_State *L)
1113 NO_MAP_LOCK_REQUIRED;
1116 "Deprecated call to set_look_pitch, use set_look_vertical instead.");
1118 ObjectRef *ref = checkobject(L, 1);
1119 PlayerSAO* co = getplayersao(ref);
1120 if (co == NULL) return 0;
1121 float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
1123 co->setPitchAndSend(pitch);
1128 // set_look_yaw(self, radians)
1129 int ObjectRef::l_set_look_yaw(lua_State *L)
1131 NO_MAP_LOCK_REQUIRED;
1134 "Deprecated call to set_look_yaw, use set_look_horizontal instead.");
1136 ObjectRef *ref = checkobject(L, 1);
1137 PlayerSAO* co = getplayersao(ref);
1138 if (co == NULL) return 0;
1139 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
1141 co->setYawAndSend(yaw);
1145 // set_breath(self, breath)
1146 int ObjectRef::l_set_breath(lua_State *L)
1148 NO_MAP_LOCK_REQUIRED;
1149 ObjectRef *ref = checkobject(L, 1);
1150 PlayerSAO* co = getplayersao(ref);
1151 if (co == NULL) return 0;
1152 u16 breath = luaL_checknumber(L, 2);
1153 co->setBreath(breath);
1159 int ObjectRef::l_get_breath(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 u16 breath = co->getBreath();
1167 lua_pushinteger (L, breath);
1171 // set_inventory_formspec(self, formspec)
1172 int ObjectRef::l_set_inventory_formspec(lua_State *L)
1174 NO_MAP_LOCK_REQUIRED;
1175 ObjectRef *ref = checkobject(L, 1);
1176 RemotePlayer *player = getplayer(ref);
1177 if (player == NULL) return 0;
1178 std::string formspec = luaL_checkstring(L, 2);
1180 player->inventory_formspec = formspec;
1181 getServer(L)->reportInventoryFormspecModified(player->getName());
1182 lua_pushboolean(L, true);
1186 // get_inventory_formspec(self) -> formspec
1187 int ObjectRef::l_get_inventory_formspec(lua_State *L)
1189 NO_MAP_LOCK_REQUIRED;
1190 ObjectRef *ref = checkobject(L, 1);
1191 RemotePlayer *player = getplayer(ref);
1192 if (player == NULL) return 0;
1194 std::string formspec = player->inventory_formspec;
1195 lua_pushlstring(L, formspec.c_str(), formspec.size());
1199 // get_player_control(self)
1200 int ObjectRef::l_get_player_control(lua_State *L)
1202 NO_MAP_LOCK_REQUIRED;
1203 ObjectRef *ref = checkobject(L, 1);
1204 RemotePlayer *player = getplayer(ref);
1205 if (player == NULL) {
1206 lua_pushlstring(L, "", 0);
1210 const PlayerControl &control = player->getPlayerControl();
1212 lua_pushboolean(L, control.up);
1213 lua_setfield(L, -2, "up");
1214 lua_pushboolean(L, control.down);
1215 lua_setfield(L, -2, "down");
1216 lua_pushboolean(L, control.left);
1217 lua_setfield(L, -2, "left");
1218 lua_pushboolean(L, control.right);
1219 lua_setfield(L, -2, "right");
1220 lua_pushboolean(L, control.jump);
1221 lua_setfield(L, -2, "jump");
1222 lua_pushboolean(L, control.aux1);
1223 lua_setfield(L, -2, "aux1");
1224 lua_pushboolean(L, control.sneak);
1225 lua_setfield(L, -2, "sneak");
1226 lua_pushboolean(L, control.LMB);
1227 lua_setfield(L, -2, "LMB");
1228 lua_pushboolean(L, control.RMB);
1229 lua_setfield(L, -2, "RMB");
1233 // get_player_control_bits(self)
1234 int ObjectRef::l_get_player_control_bits(lua_State *L)
1236 NO_MAP_LOCK_REQUIRED;
1237 ObjectRef *ref = checkobject(L, 1);
1238 RemotePlayer *player = getplayer(ref);
1239 if (player == NULL) {
1240 lua_pushlstring(L, "", 0);
1244 lua_pushnumber(L, player->keyPressed);
1248 // hud_add(self, form)
1249 int ObjectRef::l_hud_add(lua_State *L)
1251 NO_MAP_LOCK_REQUIRED;
1252 ObjectRef *ref = checkobject(L, 1);
1253 RemotePlayer *player = getplayer(ref);
1257 HudElement *elem = new HudElement;
1259 elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type",
1260 es_HudElementType, HUD_ELEM_TEXT);
1262 lua_getfield(L, 2, "position");
1263 elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
1266 lua_getfield(L, 2, "scale");
1267 elem->scale = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
1270 lua_getfield(L, 2, "size");
1271 elem->size = lua_istable(L, -1) ? read_v2s32(L, -1) : v2s32();
1274 elem->name = getstringfield_default(L, 2, "name", "");
1275 elem->text = getstringfield_default(L, 2, "text", "");
1276 elem->number = getintfield_default(L, 2, "number", 0);
1277 elem->item = getintfield_default(L, 2, "item", 0);
1278 elem->dir = getintfield_default(L, 2, "direction", 0);
1280 // Deprecated, only for compatibility's sake
1282 elem->dir = getintfield_default(L, 2, "dir", 0);
1284 lua_getfield(L, 2, "alignment");
1285 elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
1288 lua_getfield(L, 2, "offset");
1289 elem->offset = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
1292 lua_getfield(L, 2, "world_pos");
1293 elem->world_pos = lua_istable(L, -1) ? read_v3f(L, -1) : v3f();
1296 /* check for known deprecated element usage */
1297 if ((elem->type == HUD_ELEM_STATBAR) && (elem->size == v2s32())) {
1298 log_deprecated(L,"Deprecated usage of statbar without size!");
1301 u32 id = getServer(L)->hudAdd(player, elem);
1302 if (id == U32_MAX) {
1307 lua_pushnumber(L, id);
1311 // hud_remove(self, id)
1312 int ObjectRef::l_hud_remove(lua_State *L)
1314 NO_MAP_LOCK_REQUIRED;
1315 ObjectRef *ref = checkobject(L, 1);
1316 RemotePlayer *player = getplayer(ref);
1321 if (!lua_isnil(L, 2))
1322 id = lua_tonumber(L, 2);
1324 if (!getServer(L)->hudRemove(player, id))
1327 lua_pushboolean(L, true);
1331 // hud_change(self, id, stat, data)
1332 int ObjectRef::l_hud_change(lua_State *L)
1334 NO_MAP_LOCK_REQUIRED;
1335 ObjectRef *ref = checkobject(L, 1);
1336 RemotePlayer *player = getplayer(ref);
1340 u32 id = lua_isnumber(L, 2) ? lua_tonumber(L, 2) : -1;
1342 HudElement *e = player->getHud(id);
1346 HudElementStat stat = HUD_STAT_NUMBER;
1347 if (lua_isstring(L, 3)) {
1349 std::string statstr = lua_tostring(L, 3);
1350 stat = string_to_enum(es_HudElementStat, statint, statstr) ?
1351 (HudElementStat)statint : HUD_STAT_NUMBER;
1357 e->pos = read_v2f(L, 4);
1361 e->name = luaL_checkstring(L, 4);
1364 case HUD_STAT_SCALE:
1365 e->scale = read_v2f(L, 4);
1369 e->text = luaL_checkstring(L, 4);
1372 case HUD_STAT_NUMBER:
1373 e->number = luaL_checknumber(L, 4);
1377 e->item = luaL_checknumber(L, 4);
1381 e->dir = luaL_checknumber(L, 4);
1384 case HUD_STAT_ALIGN:
1385 e->align = read_v2f(L, 4);
1388 case HUD_STAT_OFFSET:
1389 e->offset = read_v2f(L, 4);
1392 case HUD_STAT_WORLD_POS:
1393 e->world_pos = read_v3f(L, 4);
1394 value = &e->world_pos;
1397 e->size = read_v2s32(L, 4);
1402 getServer(L)->hudChange(player, id, stat, value);
1404 lua_pushboolean(L, true);
1408 // hud_get(self, id)
1409 int ObjectRef::l_hud_get(lua_State *L)
1411 NO_MAP_LOCK_REQUIRED;
1412 ObjectRef *ref = checkobject(L, 1);
1413 RemotePlayer *player = getplayer(ref);
1417 u32 id = lua_tonumber(L, -1);
1419 HudElement *e = player->getHud(id);
1425 lua_pushstring(L, es_HudElementType[(u8)e->type].str);
1426 lua_setfield(L, -2, "type");
1428 push_v2f(L, e->pos);
1429 lua_setfield(L, -2, "position");
1431 lua_pushstring(L, e->name.c_str());
1432 lua_setfield(L, -2, "name");
1434 push_v2f(L, e->scale);
1435 lua_setfield(L, -2, "scale");
1437 lua_pushstring(L, e->text.c_str());
1438 lua_setfield(L, -2, "text");
1440 lua_pushnumber(L, e->number);
1441 lua_setfield(L, -2, "number");
1443 lua_pushnumber(L, e->item);
1444 lua_setfield(L, -2, "item");
1446 lua_pushnumber(L, e->dir);
1447 lua_setfield(L, -2, "direction");
1449 // Deprecated, only for compatibility's sake
1450 lua_pushnumber(L, e->dir);
1451 lua_setfield(L, -2, "dir");
1453 push_v3f(L, e->world_pos);
1454 lua_setfield(L, -2, "world_pos");
1459 // hud_set_flags(self, flags)
1460 int ObjectRef::l_hud_set_flags(lua_State *L)
1462 NO_MAP_LOCK_REQUIRED;
1463 ObjectRef *ref = checkobject(L, 1);
1464 RemotePlayer *player = getplayer(ref);
1472 const EnumString *esp = es_HudBuiltinElement;
1473 for (int i = 0; esp[i].str; i++) {
1474 if (getboolfield(L, 2, esp[i].str, flag)) {
1475 flags |= esp[i].num * flag;
1479 if (!getServer(L)->hudSetFlags(player, flags, mask))
1482 lua_pushboolean(L, true);
1486 int ObjectRef::l_hud_get_flags(lua_State *L)
1488 NO_MAP_LOCK_REQUIRED;
1489 ObjectRef *ref = checkobject(L, 1);
1490 RemotePlayer *player = getplayer(ref);
1495 lua_pushboolean(L, player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE);
1496 lua_setfield(L, -2, "hotbar");
1497 lua_pushboolean(L, player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE);
1498 lua_setfield(L, -2, "healthbar");
1499 lua_pushboolean(L, player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE);
1500 lua_setfield(L, -2, "crosshair");
1501 lua_pushboolean(L, player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE);
1502 lua_setfield(L, -2, "wielditem");
1503 lua_pushboolean(L, player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE);
1504 lua_setfield(L, -2, "breathbar");
1505 lua_pushboolean(L, player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE);
1506 lua_setfield(L, -2, "minimap");
1511 // hud_set_hotbar_itemcount(self, hotbar_itemcount)
1512 int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L)
1514 NO_MAP_LOCK_REQUIRED;
1515 ObjectRef *ref = checkobject(L, 1);
1516 RemotePlayer *player = getplayer(ref);
1520 s32 hotbar_itemcount = lua_tonumber(L, 2);
1522 if (!getServer(L)->hudSetHotbarItemcount(player, hotbar_itemcount))
1525 lua_pushboolean(L, true);
1529 // hud_get_hotbar_itemcount(self)
1530 int ObjectRef::l_hud_get_hotbar_itemcount(lua_State *L)
1532 NO_MAP_LOCK_REQUIRED;
1533 ObjectRef *ref = checkobject(L, 1);
1534 RemotePlayer *player = getplayer(ref);
1538 s32 hotbar_itemcount = getServer(L)->hudGetHotbarItemcount(player);
1540 lua_pushnumber(L, hotbar_itemcount);
1544 // hud_set_hotbar_image(self, name)
1545 int ObjectRef::l_hud_set_hotbar_image(lua_State *L)
1547 NO_MAP_LOCK_REQUIRED;
1548 ObjectRef *ref = checkobject(L, 1);
1549 RemotePlayer *player = getplayer(ref);
1553 std::string name = lua_tostring(L, 2);
1555 getServer(L)->hudSetHotbarImage(player, name);
1559 // hud_get_hotbar_image(self)
1560 int ObjectRef::l_hud_get_hotbar_image(lua_State *L)
1562 NO_MAP_LOCK_REQUIRED;
1563 ObjectRef *ref = checkobject(L, 1);
1564 RemotePlayer *player = getplayer(ref);
1568 std::string name = getServer(L)->hudGetHotbarImage(player);
1569 lua_pushlstring(L, name.c_str(), name.size());
1573 // hud_set_hotbar_selected_image(self, name)
1574 int ObjectRef::l_hud_set_hotbar_selected_image(lua_State *L)
1576 NO_MAP_LOCK_REQUIRED;
1577 ObjectRef *ref = checkobject(L, 1);
1578 RemotePlayer *player = getplayer(ref);
1582 std::string name = lua_tostring(L, 2);
1584 getServer(L)->hudSetHotbarSelectedImage(player, name);
1588 // hud_get_hotbar_selected_image(self)
1589 int ObjectRef::l_hud_get_hotbar_selected_image(lua_State *L)
1591 NO_MAP_LOCK_REQUIRED;
1592 ObjectRef *ref = checkobject(L, 1);
1593 RemotePlayer *player = getplayer(ref);
1597 const std::string &name = getServer(L)->hudGetHotbarSelectedImage(player);
1598 lua_pushlstring(L, name.c_str(), name.size());
1602 // set_sky(self, bgcolor, type, list)
1603 int ObjectRef::l_set_sky(lua_State *L)
1605 NO_MAP_LOCK_REQUIRED;
1606 ObjectRef *ref = checkobject(L, 1);
1607 RemotePlayer *player = getplayer(ref);
1611 video::SColor bgcolor(255,255,255,255);
1612 read_color(L, 2, &bgcolor);
1614 std::string type = luaL_checkstring(L, 3);
1616 std::vector<std::string> params;
1617 if (lua_istable(L, 4)) {
1618 int table = lua_gettop(L);
1620 while (lua_next(L, table) != 0) {
1621 // key at index -2 and value at index -1
1622 if (lua_isstring(L, -1))
1623 params.push_back(lua_tostring(L, -1));
1625 params.push_back("");
1626 // removes value, keeps key for next iteration
1631 if (type == "skybox" && params.size() != 6)
1632 throw LuaError("skybox expects 6 textures");
1634 if (!getServer(L)->setSky(player, bgcolor, type, params))
1637 lua_pushboolean(L, true);
1642 int ObjectRef::l_get_sky(lua_State *L)
1644 NO_MAP_LOCK_REQUIRED;
1645 ObjectRef *ref = checkobject(L, 1);
1646 RemotePlayer *player = getplayer(ref);
1649 video::SColor bgcolor(255, 255, 255, 255);
1651 std::vector<std::string> params;
1653 player->getSky(&bgcolor, &type, ¶ms);
1654 type = type == "" ? "regular" : type;
1656 push_ARGB8(L, bgcolor);
1657 lua_pushlstring(L, type.c_str(), type.size());
1660 for (std::vector<std::string>::iterator it = params.begin();
1661 it != params.end(); ++it) {
1662 lua_pushlstring(L, it->c_str(), it->size());
1663 lua_rawseti(L, -2, i);
1669 // override_day_night_ratio(self, brightness=0...1)
1670 int ObjectRef::l_override_day_night_ratio(lua_State *L)
1672 NO_MAP_LOCK_REQUIRED;
1673 ObjectRef *ref = checkobject(L, 1);
1674 RemotePlayer *player = getplayer(ref);
1678 bool do_override = false;
1680 if (!lua_isnil(L, 2)) {
1682 ratio = luaL_checknumber(L, 2);
1685 if (!getServer(L)->overrideDayNightRatio(player, do_override, ratio))
1688 lua_pushboolean(L, true);
1692 // get_day_night_ratio(self)
1693 int ObjectRef::l_get_day_night_ratio(lua_State *L)
1695 NO_MAP_LOCK_REQUIRED;
1696 ObjectRef *ref = checkobject(L, 1);
1697 RemotePlayer *player = getplayer(ref);
1703 player->getDayNightRatio(&do_override, &ratio);
1706 lua_pushnumber(L, ratio);
1713 ObjectRef::ObjectRef(ServerActiveObject *object):
1716 //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
1719 ObjectRef::~ObjectRef()
1722 infostream<<"ObjectRef destructing for id="
1723 <<m_object->getId()<<std::endl;
1725 infostream<<"ObjectRef destructing for id=unknown"<<std::endl;*/
1728 // Creates an ObjectRef and leaves it on top of stack
1729 // Not callable from Lua; all references are created on the C side.
1730 void ObjectRef::create(lua_State *L, ServerActiveObject *object)
1732 ObjectRef *o = new ObjectRef(object);
1733 //infostream<<"ObjectRef::create: o="<<o<<std::endl;
1734 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1735 luaL_getmetatable(L, className);
1736 lua_setmetatable(L, -2);
1739 void ObjectRef::set_null(lua_State *L)
1741 ObjectRef *o = checkobject(L, -1);
1745 void ObjectRef::Register(lua_State *L)
1748 int methodtable = lua_gettop(L);
1749 luaL_newmetatable(L, className);
1750 int metatable = lua_gettop(L);
1752 lua_pushliteral(L, "__metatable");
1753 lua_pushvalue(L, methodtable);
1754 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
1756 lua_pushliteral(L, "__index");
1757 lua_pushvalue(L, methodtable);
1758 lua_settable(L, metatable);
1760 lua_pushliteral(L, "__gc");
1761 lua_pushcfunction(L, gc_object);
1762 lua_settable(L, metatable);
1764 lua_pop(L, 1); // drop metatable
1766 luaL_openlib(L, 0, methods, 0); // fill methodtable
1767 lua_pop(L, 1); // drop methodtable
1769 // Cannot be created from Lua
1770 //lua_register(L, className, create_object);
1773 const char ObjectRef::className[] = "ObjectRef";
1774 const luaL_reg ObjectRef::methods[] = {
1775 // ServerActiveObject
1776 luamethod(ObjectRef, remove),
1777 luamethod(ObjectRef, getpos),
1778 luamethod(ObjectRef, setpos),
1779 luamethod(ObjectRef, moveto),
1780 luamethod(ObjectRef, punch),
1781 luamethod(ObjectRef, right_click),
1782 luamethod(ObjectRef, set_hp),
1783 luamethod(ObjectRef, get_hp),
1784 luamethod(ObjectRef, get_inventory),
1785 luamethod(ObjectRef, get_wield_list),
1786 luamethod(ObjectRef, get_wield_index),
1787 luamethod(ObjectRef, get_wielded_item),
1788 luamethod(ObjectRef, set_wielded_item),
1789 luamethod(ObjectRef, set_armor_groups),
1790 luamethod(ObjectRef, get_armor_groups),
1791 luamethod(ObjectRef, set_animation),
1792 luamethod(ObjectRef, get_animation),
1793 luamethod(ObjectRef, set_bone_position),
1794 luamethod(ObjectRef, get_bone_position),
1795 luamethod(ObjectRef, set_attach),
1796 luamethod(ObjectRef, get_attach),
1797 luamethod(ObjectRef, set_detach),
1798 luamethod(ObjectRef, set_properties),
1799 luamethod(ObjectRef, get_properties),
1800 luamethod(ObjectRef, set_nametag_attributes),
1801 luamethod(ObjectRef, get_nametag_attributes),
1802 // LuaEntitySAO-only
1803 luamethod(ObjectRef, setvelocity),
1804 luamethod(ObjectRef, getvelocity),
1805 luamethod(ObjectRef, setacceleration),
1806 luamethod(ObjectRef, getacceleration),
1807 luamethod(ObjectRef, setyaw),
1808 luamethod(ObjectRef, getyaw),
1809 luamethod(ObjectRef, settexturemod),
1810 luamethod(ObjectRef, setsprite),
1811 luamethod(ObjectRef, get_entity_name),
1812 luamethod(ObjectRef, get_luaentity),
1814 luamethod(ObjectRef, is_player),
1815 luamethod(ObjectRef, is_player_connected),
1816 luamethod(ObjectRef, get_player_name),
1817 luamethod(ObjectRef, get_player_velocity),
1818 luamethod(ObjectRef, get_look_dir),
1819 luamethod(ObjectRef, get_look_pitch),
1820 luamethod(ObjectRef, get_look_yaw),
1821 luamethod(ObjectRef, get_look_vertical),
1822 luamethod(ObjectRef, get_look_horizontal),
1823 luamethod(ObjectRef, set_look_horizontal),
1824 luamethod(ObjectRef, set_look_vertical),
1825 luamethod(ObjectRef, set_look_yaw),
1826 luamethod(ObjectRef, set_look_pitch),
1827 luamethod(ObjectRef, get_breath),
1828 luamethod(ObjectRef, set_breath),
1829 luamethod(ObjectRef, set_inventory_formspec),
1830 luamethod(ObjectRef, get_inventory_formspec),
1831 luamethod(ObjectRef, get_player_control),
1832 luamethod(ObjectRef, get_player_control_bits),
1833 luamethod(ObjectRef, set_physics_override),
1834 luamethod(ObjectRef, get_physics_override),
1835 luamethod(ObjectRef, hud_add),
1836 luamethod(ObjectRef, hud_remove),
1837 luamethod(ObjectRef, hud_change),
1838 luamethod(ObjectRef, hud_get),
1839 luamethod(ObjectRef, hud_set_flags),
1840 luamethod(ObjectRef, hud_get_flags),
1841 luamethod(ObjectRef, hud_set_hotbar_itemcount),
1842 luamethod(ObjectRef, hud_get_hotbar_itemcount),
1843 luamethod(ObjectRef, hud_set_hotbar_image),
1844 luamethod(ObjectRef, hud_get_hotbar_image),
1845 luamethod(ObjectRef, hud_set_hotbar_selected_image),
1846 luamethod(ObjectRef, hud_get_hotbar_selected_image),
1847 luamethod(ObjectRef, set_sky),
1848 luamethod(ObjectRef, get_sky),
1849 luamethod(ObjectRef, override_day_night_ratio),
1850 luamethod(ObjectRef, get_day_night_ratio),
1851 luamethod(ObjectRef, set_local_animation),
1852 luamethod(ObjectRef, get_local_animation),
1853 luamethod(ObjectRef, set_eye_offset),
1854 luamethod(ObjectRef, get_eye_offset),