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_server.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 std::unordered_set<int> &child_ids = co->getAttachmentChildIds();
141 std::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_get_pos(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");
172 // set_pos(self, pos)
173 int ObjectRef::l_set_pos(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 // move_to(self, pos, continuous=false)
188 int ObjectRef::l_move_to(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)->idef());
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, new_move)
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(
414 L, 2, "speed", co->m_physics_override_speed);
415 co->m_physics_override_jump = getfloatfield_default(
416 L, 2, "jump", co->m_physics_override_jump);
417 co->m_physics_override_gravity = getfloatfield_default(
418 L, 2, "gravity", co->m_physics_override_gravity);
419 co->m_physics_override_sneak = getboolfield_default(
420 L, 2, "sneak", co->m_physics_override_sneak);
421 co->m_physics_override_sneak_glitch = getboolfield_default(
422 L, 2, "sneak_glitch", co->m_physics_override_sneak_glitch);
423 co->m_physics_override_new_move = getboolfield_default(
424 L, 2, "new_move", co->m_physics_override_new_move);
425 co->m_physics_override_sent = false;
427 // old, non-table format
428 if (!lua_isnil(L, 2)) {
429 co->m_physics_override_speed = lua_tonumber(L, 2);
430 co->m_physics_override_sent = false;
432 if (!lua_isnil(L, 3)) {
433 co->m_physics_override_jump = lua_tonumber(L, 3);
434 co->m_physics_override_sent = false;
436 if (!lua_isnil(L, 4)) {
437 co->m_physics_override_gravity = lua_tonumber(L, 4);
438 co->m_physics_override_sent = false;
444 // get_physics_override(self)
445 int ObjectRef::l_get_physics_override(lua_State *L)
447 NO_MAP_LOCK_REQUIRED;
448 ObjectRef *ref = checkobject(L, 1);
449 PlayerSAO *co = (PlayerSAO *)getobject(ref);
454 lua_pushnumber(L, co->m_physics_override_speed);
455 lua_setfield(L, -2, "speed");
456 lua_pushnumber(L, co->m_physics_override_jump);
457 lua_setfield(L, -2, "jump");
458 lua_pushnumber(L, co->m_physics_override_gravity);
459 lua_setfield(L, -2, "gravity");
460 lua_pushboolean(L, co->m_physics_override_sneak);
461 lua_setfield(L, -2, "sneak");
462 lua_pushboolean(L, co->m_physics_override_sneak_glitch);
463 lua_setfield(L, -2, "sneak_glitch");
464 lua_pushboolean(L, co->m_physics_override_new_move);
465 lua_setfield(L, -2, "new_move");
469 // set_animation(self, frame_range, frame_speed, frame_blend, frame_loop)
470 int ObjectRef::l_set_animation(lua_State *L)
472 NO_MAP_LOCK_REQUIRED;
473 ObjectRef *ref = checkobject(L, 1);
474 ServerActiveObject *co = getobject(ref);
475 if (co == NULL) return 0;
477 v2f frames = v2f(1, 1);
478 if (!lua_isnil(L, 2))
479 frames = read_v2f(L, 2);
480 float frame_speed = 15;
481 if (!lua_isnil(L, 3))
482 frame_speed = lua_tonumber(L, 3);
483 float frame_blend = 0;
484 if (!lua_isnil(L, 4))
485 frame_blend = lua_tonumber(L, 4);
486 bool frame_loop = true;
487 if (lua_isboolean(L, 5))
488 frame_loop = lua_toboolean(L, 5);
489 co->setAnimation(frames, frame_speed, frame_blend, frame_loop);
493 // get_animation(self)
494 int ObjectRef::l_get_animation(lua_State *L)
496 NO_MAP_LOCK_REQUIRED;
497 ObjectRef *ref = checkobject(L, 1);
498 ServerActiveObject *co = getobject(ref);
502 v2f frames = v2f(1,1);
503 float frame_speed = 15;
504 float frame_blend = 0;
505 bool frame_loop = true;
506 co->getAnimation(&frames, &frame_speed, &frame_blend, &frame_loop);
509 lua_pushnumber(L, frame_speed);
510 lua_pushnumber(L, frame_blend);
511 lua_pushboolean(L, frame_loop);
515 // set_local_animation(self, {stand/idle}, {walk}, {dig}, {walk+dig}, frame_speed)
516 int ObjectRef::l_set_local_animation(lua_State *L)
518 NO_MAP_LOCK_REQUIRED;
519 ObjectRef *ref = checkobject(L, 1);
520 RemotePlayer *player = getplayer(ref);
525 for (int i=0;i<4;i++) {
526 if (!lua_isnil(L, 2+1))
527 frames[i] = read_v2s32(L, 2+i);
529 float frame_speed = 30;
530 if (!lua_isnil(L, 6))
531 frame_speed = lua_tonumber(L, 6);
533 if (!getServer(L)->setLocalPlayerAnimations(player, frames, frame_speed))
536 lua_pushboolean(L, true);
540 // get_local_animation(self)
541 int ObjectRef::l_get_local_animation(lua_State *L)
544 ObjectRef *ref = checkobject(L, 1);
545 RemotePlayer *player = getplayer(ref);
551 player->getLocalAnimations(frames, &frame_speed);
553 for (int i = 0; i < 4; i++) {
554 push_v2s32(L, frames[i]);
557 lua_pushnumber(L, frame_speed);
561 // set_eye_offset(self, v3f first pv, v3f third pv)
562 int ObjectRef::l_set_eye_offset(lua_State *L)
564 NO_MAP_LOCK_REQUIRED;
565 ObjectRef *ref = checkobject(L, 1);
566 RemotePlayer *player = getplayer(ref);
570 v3f offset_first = v3f(0, 0, 0);
571 v3f offset_third = v3f(0, 0, 0);
573 if (!lua_isnil(L, 2))
574 offset_first = read_v3f(L, 2);
575 if (!lua_isnil(L, 3))
576 offset_third = read_v3f(L, 3);
578 // Prevent abuse of offset values (keep player always visible)
579 offset_third.X = rangelim(offset_third.X,-10,10);
580 offset_third.Z = rangelim(offset_third.Z,-5,5);
581 /* TODO: if possible: improve the camera colision detetion to allow Y <= -1.5) */
582 offset_third.Y = rangelim(offset_third.Y,-10,15); //1.5*BS
584 if (!getServer(L)->setPlayerEyeOffset(player, offset_first, offset_third))
587 lua_pushboolean(L, true);
591 // get_eye_offset(self)
592 int ObjectRef::l_get_eye_offset(lua_State *L)
594 NO_MAP_LOCK_REQUIRED;
595 ObjectRef *ref = checkobject(L, 1);
596 RemotePlayer *player = getplayer(ref);
600 push_v3f(L, player->eye_offset_first);
601 push_v3f(L, player->eye_offset_third);
605 // set_bone_position(self, std::string bone, v3f position, v3f rotation)
606 int ObjectRef::l_set_bone_position(lua_State *L)
608 NO_MAP_LOCK_REQUIRED;
609 ObjectRef *ref = checkobject(L, 1);
610 ServerActiveObject *co = getobject(ref);
611 if (co == NULL) return 0;
613 std::string bone = "";
614 if (!lua_isnil(L, 2))
615 bone = lua_tostring(L, 2);
616 v3f position = v3f(0, 0, 0);
617 if (!lua_isnil(L, 3))
618 position = check_v3f(L, 3);
619 v3f rotation = v3f(0, 0, 0);
620 if (!lua_isnil(L, 4))
621 rotation = check_v3f(L, 4);
622 co->setBonePosition(bone, position, rotation);
626 // get_bone_position(self, bone)
627 int ObjectRef::l_get_bone_position(lua_State *L)
629 NO_MAP_LOCK_REQUIRED;
630 ObjectRef *ref = checkobject(L, 1);
631 ServerActiveObject *co = getobject(ref);
635 std::string bone = "";
636 if (!lua_isnil(L, 2))
637 bone = lua_tostring(L, 2);
639 v3f position = v3f(0, 0, 0);
640 v3f rotation = v3f(0, 0, 0);
641 co->getBonePosition(bone, &position, &rotation);
643 push_v3f(L, position);
644 push_v3f(L, rotation);
648 // set_attach(self, parent, bone, position, rotation)
649 int ObjectRef::l_set_attach(lua_State *L)
653 ObjectRef *ref = checkobject(L, 1);
654 ObjectRef *parent_ref = checkobject(L, 2);
655 ServerActiveObject *co = getobject(ref);
656 ServerActiveObject *parent = getobject(parent_ref);
663 std::string bone = "";
664 v3f position = v3f(0, 0, 0);
665 v3f rotation = v3f(0, 0, 0);
666 co->getAttachment(&parent_id, &bone, &position, &rotation);
668 ServerActiveObject *old_parent = env->getActiveObject(parent_id);
669 old_parent->removeAttachmentChild(co->getId());
673 if (!lua_isnil(L, 3))
674 bone = lua_tostring(L, 3);
675 position = v3f(0, 0, 0);
676 if (!lua_isnil(L, 4))
677 position = read_v3f(L, 4);
678 rotation = v3f(0, 0, 0);
679 if (!lua_isnil(L, 5))
680 rotation = read_v3f(L, 5);
681 co->setAttachment(parent->getId(), bone, position, rotation);
682 parent->addAttachmentChild(co->getId());
687 int ObjectRef::l_get_attach(lua_State *L)
691 ObjectRef *ref = checkobject(L, 1);
692 ServerActiveObject *co = getobject(ref);
698 std::string bone = "";
699 v3f position = v3f(0, 0, 0);
700 v3f rotation = v3f(0, 0, 0);
701 co->getAttachment(&parent_id, &bone, &position, &rotation);
704 ServerActiveObject *parent = env->getActiveObject(parent_id);
706 getScriptApiBase(L)->objectrefGetOrCreate(L, parent);
707 lua_pushlstring(L, bone.c_str(), bone.size());
708 push_v3f(L, position);
709 push_v3f(L, rotation);
714 int ObjectRef::l_set_detach(lua_State *L)
718 ObjectRef *ref = checkobject(L, 1);
719 ServerActiveObject *co = getobject(ref);
724 std::string bone = "";
727 co->getAttachment(&parent_id, &bone, &position, &rotation);
728 ServerActiveObject *parent = NULL;
730 parent = env->getActiveObject(parent_id);
731 co->setAttachment(0, "", position, rotation);
733 co->setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0));
737 parent->removeAttachmentChild(co->getId());
741 // set_properties(self, properties)
742 int ObjectRef::l_set_properties(lua_State *L)
744 NO_MAP_LOCK_REQUIRED;
745 ObjectRef *ref = checkobject(L, 1);
746 ServerActiveObject *co = getobject(ref);
747 if (co == NULL) return 0;
748 ObjectProperties *prop = co->accessObjectProperties();
751 read_object_properties(L, 2, prop, getServer(L)->idef());
752 co->notifyObjectPropertiesModified();
756 // get_properties(self)
757 int ObjectRef::l_get_properties(lua_State *L)
759 NO_MAP_LOCK_REQUIRED;
760 ObjectRef *ref = checkobject(L, 1);
761 ServerActiveObject *co = getobject(ref);
764 ObjectProperties *prop = co->accessObjectProperties();
767 push_object_properties(L, prop);
772 int ObjectRef::l_is_player(lua_State *L)
774 NO_MAP_LOCK_REQUIRED;
775 ObjectRef *ref = checkobject(L, 1);
776 RemotePlayer *player = getplayer(ref);
777 lua_pushboolean(L, (player != NULL));
781 // set_nametag_attributes(self, attributes)
782 int ObjectRef::l_set_nametag_attributes(lua_State *L)
784 NO_MAP_LOCK_REQUIRED;
785 ObjectRef *ref = checkobject(L, 1);
786 ServerActiveObject *co = getobject(ref);
790 ObjectProperties *prop = co->accessObjectProperties();
794 lua_getfield(L, 2, "color");
795 if (!lua_isnil(L, -1)) {
796 video::SColor color = prop->nametag_color;
797 read_color(L, -1, &color);
798 prop->nametag_color = color;
802 std::string nametag = getstringfield_default(L, 2, "text", "");
803 prop->nametag = nametag;
805 co->notifyObjectPropertiesModified();
806 lua_pushboolean(L, true);
810 // get_nametag_attributes(self)
811 int ObjectRef::l_get_nametag_attributes(lua_State *L)
813 NO_MAP_LOCK_REQUIRED;
814 ObjectRef *ref = checkobject(L, 1);
815 ServerActiveObject *co = getobject(ref);
819 ObjectProperties *prop = co->accessObjectProperties();
823 video::SColor color = prop->nametag_color;
826 push_ARGB8(L, color);
827 lua_setfield(L, -2, "color");
828 lua_pushstring(L, prop->nametag.c_str());
829 lua_setfield(L, -2, "text");
833 /* LuaEntitySAO-only */
835 // set_velocity(self, {x=num, y=num, z=num})
836 int ObjectRef::l_set_velocity(lua_State *L)
838 NO_MAP_LOCK_REQUIRED;
839 ObjectRef *ref = checkobject(L, 1);
840 LuaEntitySAO *co = getluaobject(ref);
841 if (co == NULL) return 0;
842 v3f pos = checkFloatPos(L, 2);
844 co->setVelocity(pos);
848 // get_velocity(self)
849 int ObjectRef::l_get_velocity(lua_State *L)
851 NO_MAP_LOCK_REQUIRED;
852 ObjectRef *ref = checkobject(L, 1);
853 LuaEntitySAO *co = getluaobject(ref);
854 if (co == NULL) return 0;
856 v3f v = co->getVelocity();
861 // set_acceleration(self, {x=num, y=num, z=num})
862 int ObjectRef::l_set_acceleration(lua_State *L)
864 NO_MAP_LOCK_REQUIRED;
865 ObjectRef *ref = checkobject(L, 1);
866 LuaEntitySAO *co = getluaobject(ref);
867 if (co == NULL) return 0;
869 v3f pos = checkFloatPos(L, 2);
871 co->setAcceleration(pos);
875 // get_acceleration(self)
876 int ObjectRef::l_get_acceleration(lua_State *L)
878 NO_MAP_LOCK_REQUIRED;
879 ObjectRef *ref = checkobject(L, 1);
880 LuaEntitySAO *co = getluaobject(ref);
881 if (co == NULL) return 0;
883 v3f v = co->getAcceleration();
888 // set_yaw(self, radians)
889 int ObjectRef::l_set_yaw(lua_State *L)
891 NO_MAP_LOCK_REQUIRED;
892 ObjectRef *ref = checkobject(L, 1);
893 LuaEntitySAO *co = getluaobject(ref);
894 if (co == NULL) return 0;
895 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
902 int ObjectRef::l_get_yaw(lua_State *L)
904 NO_MAP_LOCK_REQUIRED;
905 ObjectRef *ref = checkobject(L, 1);
906 LuaEntitySAO *co = getluaobject(ref);
907 if (co == NULL) return 0;
909 float yaw = co->getYaw() * core::DEGTORAD;
910 lua_pushnumber(L, yaw);
914 // set_texture_mod(self, mod)
915 int ObjectRef::l_set_texture_mod(lua_State *L)
917 NO_MAP_LOCK_REQUIRED;
918 ObjectRef *ref = checkobject(L, 1);
919 LuaEntitySAO *co = getluaobject(ref);
920 if (co == NULL) return 0;
922 std::string mod = luaL_checkstring(L, 2);
923 co->setTextureMod(mod);
927 // get_texture_mod(self)
928 int ObjectRef::l_get_texture_mod(lua_State *L)
930 NO_MAP_LOCK_REQUIRED;
931 ObjectRef *ref = checkobject(L, 1);
932 LuaEntitySAO *co = getluaobject(ref);
933 if (co == NULL) return 0;
935 std::string mod = co->getTextureMod();
936 lua_pushstring(L, mod.c_str());
940 // set_sprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
941 // select_horiz_by_yawpitch=false)
942 int ObjectRef::l_set_sprite(lua_State *L)
944 NO_MAP_LOCK_REQUIRED;
945 ObjectRef *ref = checkobject(L, 1);
946 LuaEntitySAO *co = getluaobject(ref);
947 if (co == NULL) return 0;
950 if (!lua_isnil(L, 2))
951 p = read_v2s16(L, 2);
953 if (!lua_isnil(L, 3))
954 num_frames = lua_tonumber(L, 3);
955 float framelength = 0.2;
956 if (!lua_isnil(L, 4))
957 framelength = lua_tonumber(L, 4);
958 bool select_horiz_by_yawpitch = false;
959 if (!lua_isnil(L, 5))
960 select_horiz_by_yawpitch = lua_toboolean(L, 5);
961 co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
966 // get_entity_name(self)
967 int ObjectRef::l_get_entity_name(lua_State *L)
969 NO_MAP_LOCK_REQUIRED;
970 ObjectRef *ref = checkobject(L, 1);
971 LuaEntitySAO *co = getluaobject(ref);
972 log_deprecated(L,"Deprecated call to \"get_entity_name");
973 if (co == NULL) return 0;
975 std::string name = co->getName();
976 lua_pushstring(L, name.c_str());
980 // get_luaentity(self)
981 int ObjectRef::l_get_luaentity(lua_State *L)
983 NO_MAP_LOCK_REQUIRED;
984 ObjectRef *ref = checkobject(L, 1);
985 LuaEntitySAO *co = getluaobject(ref);
986 if (co == NULL) return 0;
988 luaentity_get(L, co->getId());
994 // is_player_connected(self)
995 int ObjectRef::l_is_player_connected(lua_State *L)
997 NO_MAP_LOCK_REQUIRED;
998 ObjectRef *ref = checkobject(L, 1);
999 RemotePlayer *player = getplayer(ref);
1000 lua_pushboolean(L, (player != NULL && player->peer_id != 0));
1004 // get_player_name(self)
1005 int ObjectRef::l_get_player_name(lua_State *L)
1007 NO_MAP_LOCK_REQUIRED;
1008 ObjectRef *ref = checkobject(L, 1);
1009 RemotePlayer *player = getplayer(ref);
1010 if (player == NULL) {
1011 lua_pushlstring(L, "", 0);
1015 lua_pushstring(L, player->getName());
1019 // get_player_velocity(self)
1020 int ObjectRef::l_get_player_velocity(lua_State *L)
1022 NO_MAP_LOCK_REQUIRED;
1023 ObjectRef *ref = checkobject(L, 1);
1024 RemotePlayer *player = getplayer(ref);
1025 if (player == NULL) {
1030 push_v3f(L, player->getSpeed() / BS);
1034 // get_look_dir(self)
1035 int ObjectRef::l_get_look_dir(lua_State *L)
1037 NO_MAP_LOCK_REQUIRED;
1038 ObjectRef *ref = checkobject(L, 1);
1039 PlayerSAO* co = getplayersao(ref);
1040 if (co == NULL) return 0;
1042 float pitch = co->getRadPitchDep();
1043 float yaw = co->getRadYawDep();
1044 v3f v(cos(pitch)*cos(yaw), sin(pitch), cos(pitch)*sin(yaw));
1050 // get_look_pitch(self)
1051 int ObjectRef::l_get_look_pitch(lua_State *L)
1053 NO_MAP_LOCK_REQUIRED;
1056 "Deprecated call to get_look_pitch, use get_look_vertical instead");
1058 ObjectRef *ref = checkobject(L, 1);
1059 PlayerSAO* co = getplayersao(ref);
1060 if (co == NULL) return 0;
1062 lua_pushnumber(L, co->getRadPitchDep());
1067 // get_look_yaw(self)
1068 int ObjectRef::l_get_look_yaw(lua_State *L)
1070 NO_MAP_LOCK_REQUIRED;
1073 "Deprecated call to get_look_yaw, use get_look_horizontal instead");
1075 ObjectRef *ref = checkobject(L, 1);
1076 PlayerSAO* co = getplayersao(ref);
1077 if (co == NULL) return 0;
1079 lua_pushnumber(L, co->getRadYawDep());
1083 // get_look_pitch2(self)
1084 int ObjectRef::l_get_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;
1091 lua_pushnumber(L, co->getRadPitch());
1095 // get_look_yaw2(self)
1096 int ObjectRef::l_get_look_horizontal(lua_State *L)
1098 NO_MAP_LOCK_REQUIRED;
1099 ObjectRef *ref = checkobject(L, 1);
1100 PlayerSAO* co = getplayersao(ref);
1101 if (co == NULL) return 0;
1103 lua_pushnumber(L, co->getRadYaw());
1107 // set_look_vertical(self, radians)
1108 int ObjectRef::l_set_look_vertical(lua_State *L)
1110 NO_MAP_LOCK_REQUIRED;
1111 ObjectRef *ref = checkobject(L, 1);
1112 PlayerSAO* co = getplayersao(ref);
1113 if (co == NULL) return 0;
1114 float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
1116 co->setPitchAndSend(pitch);
1120 // set_look_horizontal(self, radians)
1121 int ObjectRef::l_set_look_horizontal(lua_State *L)
1123 NO_MAP_LOCK_REQUIRED;
1124 ObjectRef *ref = checkobject(L, 1);
1125 PlayerSAO* co = getplayersao(ref);
1126 if (co == NULL) return 0;
1127 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
1129 co->setYawAndSend(yaw);
1134 // set_look_pitch(self, radians)
1135 int ObjectRef::l_set_look_pitch(lua_State *L)
1137 NO_MAP_LOCK_REQUIRED;
1140 "Deprecated call to set_look_pitch, use set_look_vertical instead.");
1142 ObjectRef *ref = checkobject(L, 1);
1143 PlayerSAO* co = getplayersao(ref);
1144 if (co == NULL) return 0;
1145 float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
1147 co->setPitchAndSend(pitch);
1152 // set_look_yaw(self, radians)
1153 int ObjectRef::l_set_look_yaw(lua_State *L)
1155 NO_MAP_LOCK_REQUIRED;
1158 "Deprecated call to set_look_yaw, use set_look_horizontal instead.");
1160 ObjectRef *ref = checkobject(L, 1);
1161 PlayerSAO* co = getplayersao(ref);
1162 if (co == NULL) return 0;
1163 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
1165 co->setYawAndSend(yaw);
1169 // set_breath(self, breath)
1170 int ObjectRef::l_set_breath(lua_State *L)
1172 NO_MAP_LOCK_REQUIRED;
1173 ObjectRef *ref = checkobject(L, 1);
1174 PlayerSAO* co = getplayersao(ref);
1175 if (co == NULL) return 0;
1176 u16 breath = luaL_checknumber(L, 2);
1177 co->setBreath(breath);
1183 int ObjectRef::l_get_breath(lua_State *L)
1185 NO_MAP_LOCK_REQUIRED;
1186 ObjectRef *ref = checkobject(L, 1);
1187 PlayerSAO* co = getplayersao(ref);
1188 if (co == NULL) return 0;
1190 u16 breath = co->getBreath();
1191 lua_pushinteger (L, breath);
1195 // set_attribute(self, attribute, value)
1196 int ObjectRef::l_set_attribute(lua_State *L)
1198 ObjectRef *ref = checkobject(L, 1);
1199 PlayerSAO* co = getplayersao(ref);
1204 std::string attr = luaL_checkstring(L, 2);
1205 if (lua_isnil(L, 3)) {
1206 co->removeExtendedAttribute(attr);
1208 std::string value = luaL_checkstring(L, 3);
1209 co->setExtendedAttribute(attr, value);
1214 // get_attribute(self, attribute)
1215 int ObjectRef::l_get_attribute(lua_State *L)
1217 ObjectRef *ref = checkobject(L, 1);
1218 PlayerSAO* co = getplayersao(ref);
1223 std::string attr = luaL_checkstring(L, 2);
1225 std::string value = "";
1226 if (co->getExtendedAttribute(attr, &value)) {
1227 lua_pushstring(L, value.c_str());
1235 // set_inventory_formspec(self, formspec)
1236 int ObjectRef::l_set_inventory_formspec(lua_State *L)
1238 NO_MAP_LOCK_REQUIRED;
1239 ObjectRef *ref = checkobject(L, 1);
1240 RemotePlayer *player = getplayer(ref);
1241 if (player == NULL) return 0;
1242 std::string formspec = luaL_checkstring(L, 2);
1244 player->inventory_formspec = formspec;
1245 getServer(L)->reportInventoryFormspecModified(player->getName());
1246 lua_pushboolean(L, true);
1250 // get_inventory_formspec(self) -> formspec
1251 int ObjectRef::l_get_inventory_formspec(lua_State *L)
1253 NO_MAP_LOCK_REQUIRED;
1254 ObjectRef *ref = checkobject(L, 1);
1255 RemotePlayer *player = getplayer(ref);
1256 if (player == NULL) return 0;
1258 std::string formspec = player->inventory_formspec;
1259 lua_pushlstring(L, formspec.c_str(), formspec.size());
1263 // get_player_control(self)
1264 int ObjectRef::l_get_player_control(lua_State *L)
1266 NO_MAP_LOCK_REQUIRED;
1267 ObjectRef *ref = checkobject(L, 1);
1268 RemotePlayer *player = getplayer(ref);
1269 if (player == NULL) {
1270 lua_pushlstring(L, "", 0);
1274 const PlayerControl &control = player->getPlayerControl();
1276 lua_pushboolean(L, control.up);
1277 lua_setfield(L, -2, "up");
1278 lua_pushboolean(L, control.down);
1279 lua_setfield(L, -2, "down");
1280 lua_pushboolean(L, control.left);
1281 lua_setfield(L, -2, "left");
1282 lua_pushboolean(L, control.right);
1283 lua_setfield(L, -2, "right");
1284 lua_pushboolean(L, control.jump);
1285 lua_setfield(L, -2, "jump");
1286 lua_pushboolean(L, control.aux1);
1287 lua_setfield(L, -2, "aux1");
1288 lua_pushboolean(L, control.sneak);
1289 lua_setfield(L, -2, "sneak");
1290 lua_pushboolean(L, control.LMB);
1291 lua_setfield(L, -2, "LMB");
1292 lua_pushboolean(L, control.RMB);
1293 lua_setfield(L, -2, "RMB");
1297 // get_player_control_bits(self)
1298 int ObjectRef::l_get_player_control_bits(lua_State *L)
1300 NO_MAP_LOCK_REQUIRED;
1301 ObjectRef *ref = checkobject(L, 1);
1302 RemotePlayer *player = getplayer(ref);
1303 if (player == NULL) {
1304 lua_pushlstring(L, "", 0);
1308 lua_pushnumber(L, player->keyPressed);
1312 // hud_add(self, form)
1313 int ObjectRef::l_hud_add(lua_State *L)
1315 NO_MAP_LOCK_REQUIRED;
1316 ObjectRef *ref = checkobject(L, 1);
1317 RemotePlayer *player = getplayer(ref);
1321 HudElement *elem = new HudElement;
1323 elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type",
1324 es_HudElementType, HUD_ELEM_TEXT);
1326 lua_getfield(L, 2, "position");
1327 elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
1330 lua_getfield(L, 2, "scale");
1331 elem->scale = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
1334 lua_getfield(L, 2, "size");
1335 elem->size = lua_istable(L, -1) ? read_v2s32(L, -1) : v2s32();
1338 elem->name = getstringfield_default(L, 2, "name", "");
1339 elem->text = getstringfield_default(L, 2, "text", "");
1340 elem->number = getintfield_default(L, 2, "number", 0);
1341 elem->item = getintfield_default(L, 2, "item", 0);
1342 elem->dir = getintfield_default(L, 2, "direction", 0);
1344 // Deprecated, only for compatibility's sake
1346 elem->dir = getintfield_default(L, 2, "dir", 0);
1348 lua_getfield(L, 2, "alignment");
1349 elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
1352 lua_getfield(L, 2, "offset");
1353 elem->offset = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
1356 lua_getfield(L, 2, "world_pos");
1357 elem->world_pos = lua_istable(L, -1) ? read_v3f(L, -1) : v3f();
1360 /* check for known deprecated element usage */
1361 if ((elem->type == HUD_ELEM_STATBAR) && (elem->size == v2s32())) {
1362 log_deprecated(L,"Deprecated usage of statbar without size!");
1365 u32 id = getServer(L)->hudAdd(player, elem);
1366 if (id == U32_MAX) {
1371 lua_pushnumber(L, id);
1375 // hud_remove(self, id)
1376 int ObjectRef::l_hud_remove(lua_State *L)
1378 NO_MAP_LOCK_REQUIRED;
1379 ObjectRef *ref = checkobject(L, 1);
1380 RemotePlayer *player = getplayer(ref);
1385 if (!lua_isnil(L, 2))
1386 id = lua_tonumber(L, 2);
1388 if (!getServer(L)->hudRemove(player, id))
1391 lua_pushboolean(L, true);
1395 // hud_change(self, id, stat, data)
1396 int ObjectRef::l_hud_change(lua_State *L)
1398 NO_MAP_LOCK_REQUIRED;
1399 ObjectRef *ref = checkobject(L, 1);
1400 RemotePlayer *player = getplayer(ref);
1404 u32 id = lua_isnumber(L, 2) ? lua_tonumber(L, 2) : -1;
1406 HudElement *e = player->getHud(id);
1410 HudElementStat stat = HUD_STAT_NUMBER;
1411 if (lua_isstring(L, 3)) {
1413 std::string statstr = lua_tostring(L, 3);
1414 stat = string_to_enum(es_HudElementStat, statint, statstr) ?
1415 (HudElementStat)statint : HUD_STAT_NUMBER;
1421 e->pos = read_v2f(L, 4);
1425 e->name = luaL_checkstring(L, 4);
1428 case HUD_STAT_SCALE:
1429 e->scale = read_v2f(L, 4);
1433 e->text = luaL_checkstring(L, 4);
1436 case HUD_STAT_NUMBER:
1437 e->number = luaL_checknumber(L, 4);
1441 e->item = luaL_checknumber(L, 4);
1445 e->dir = luaL_checknumber(L, 4);
1448 case HUD_STAT_ALIGN:
1449 e->align = read_v2f(L, 4);
1452 case HUD_STAT_OFFSET:
1453 e->offset = read_v2f(L, 4);
1456 case HUD_STAT_WORLD_POS:
1457 e->world_pos = read_v3f(L, 4);
1458 value = &e->world_pos;
1461 e->size = read_v2s32(L, 4);
1466 getServer(L)->hudChange(player, id, stat, value);
1468 lua_pushboolean(L, true);
1472 // hud_get(self, id)
1473 int ObjectRef::l_hud_get(lua_State *L)
1475 NO_MAP_LOCK_REQUIRED;
1476 ObjectRef *ref = checkobject(L, 1);
1477 RemotePlayer *player = getplayer(ref);
1481 u32 id = lua_tonumber(L, -1);
1483 HudElement *e = player->getHud(id);
1489 lua_pushstring(L, es_HudElementType[(u8)e->type].str);
1490 lua_setfield(L, -2, "type");
1492 push_v2f(L, e->pos);
1493 lua_setfield(L, -2, "position");
1495 lua_pushstring(L, e->name.c_str());
1496 lua_setfield(L, -2, "name");
1498 push_v2f(L, e->scale);
1499 lua_setfield(L, -2, "scale");
1501 lua_pushstring(L, e->text.c_str());
1502 lua_setfield(L, -2, "text");
1504 lua_pushnumber(L, e->number);
1505 lua_setfield(L, -2, "number");
1507 lua_pushnumber(L, e->item);
1508 lua_setfield(L, -2, "item");
1510 lua_pushnumber(L, e->dir);
1511 lua_setfield(L, -2, "direction");
1513 // Deprecated, only for compatibility's sake
1514 lua_pushnumber(L, e->dir);
1515 lua_setfield(L, -2, "dir");
1517 push_v3f(L, e->world_pos);
1518 lua_setfield(L, -2, "world_pos");
1523 // hud_set_flags(self, flags)
1524 int ObjectRef::l_hud_set_flags(lua_State *L)
1526 NO_MAP_LOCK_REQUIRED;
1527 ObjectRef *ref = checkobject(L, 1);
1528 RemotePlayer *player = getplayer(ref);
1536 const EnumString *esp = es_HudBuiltinElement;
1537 for (int i = 0; esp[i].str; i++) {
1538 if (getboolfield(L, 2, esp[i].str, flag)) {
1539 flags |= esp[i].num * flag;
1543 if (!getServer(L)->hudSetFlags(player, flags, mask))
1546 lua_pushboolean(L, true);
1550 int ObjectRef::l_hud_get_flags(lua_State *L)
1552 NO_MAP_LOCK_REQUIRED;
1553 ObjectRef *ref = checkobject(L, 1);
1554 RemotePlayer *player = getplayer(ref);
1559 lua_pushboolean(L, player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE);
1560 lua_setfield(L, -2, "hotbar");
1561 lua_pushboolean(L, player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE);
1562 lua_setfield(L, -2, "healthbar");
1563 lua_pushboolean(L, player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE);
1564 lua_setfield(L, -2, "crosshair");
1565 lua_pushboolean(L, player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE);
1566 lua_setfield(L, -2, "wielditem");
1567 lua_pushboolean(L, player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE);
1568 lua_setfield(L, -2, "breathbar");
1569 lua_pushboolean(L, player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE);
1570 lua_setfield(L, -2, "minimap");
1575 // hud_set_hotbar_itemcount(self, hotbar_itemcount)
1576 int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L)
1578 NO_MAP_LOCK_REQUIRED;
1579 ObjectRef *ref = checkobject(L, 1);
1580 RemotePlayer *player = getplayer(ref);
1584 s32 hotbar_itemcount = lua_tonumber(L, 2);
1586 if (!getServer(L)->hudSetHotbarItemcount(player, hotbar_itemcount))
1589 lua_pushboolean(L, true);
1593 // hud_get_hotbar_itemcount(self)
1594 int ObjectRef::l_hud_get_hotbar_itemcount(lua_State *L)
1596 NO_MAP_LOCK_REQUIRED;
1597 ObjectRef *ref = checkobject(L, 1);
1598 RemotePlayer *player = getplayer(ref);
1602 s32 hotbar_itemcount = getServer(L)->hudGetHotbarItemcount(player);
1604 lua_pushnumber(L, hotbar_itemcount);
1608 // hud_set_hotbar_image(self, name)
1609 int ObjectRef::l_hud_set_hotbar_image(lua_State *L)
1611 NO_MAP_LOCK_REQUIRED;
1612 ObjectRef *ref = checkobject(L, 1);
1613 RemotePlayer *player = getplayer(ref);
1617 std::string name = lua_tostring(L, 2);
1619 getServer(L)->hudSetHotbarImage(player, name);
1623 // hud_get_hotbar_image(self)
1624 int ObjectRef::l_hud_get_hotbar_image(lua_State *L)
1626 NO_MAP_LOCK_REQUIRED;
1627 ObjectRef *ref = checkobject(L, 1);
1628 RemotePlayer *player = getplayer(ref);
1632 std::string name = getServer(L)->hudGetHotbarImage(player);
1633 lua_pushlstring(L, name.c_str(), name.size());
1637 // hud_set_hotbar_selected_image(self, name)
1638 int ObjectRef::l_hud_set_hotbar_selected_image(lua_State *L)
1640 NO_MAP_LOCK_REQUIRED;
1641 ObjectRef *ref = checkobject(L, 1);
1642 RemotePlayer *player = getplayer(ref);
1646 std::string name = lua_tostring(L, 2);
1648 getServer(L)->hudSetHotbarSelectedImage(player, name);
1652 // hud_get_hotbar_selected_image(self)
1653 int ObjectRef::l_hud_get_hotbar_selected_image(lua_State *L)
1655 NO_MAP_LOCK_REQUIRED;
1656 ObjectRef *ref = checkobject(L, 1);
1657 RemotePlayer *player = getplayer(ref);
1661 const std::string &name = getServer(L)->hudGetHotbarSelectedImage(player);
1662 lua_pushlstring(L, name.c_str(), name.size());
1666 // set_sky(self, bgcolor, type, list, clouds = true)
1667 int ObjectRef::l_set_sky(lua_State *L)
1669 NO_MAP_LOCK_REQUIRED;
1670 ObjectRef *ref = checkobject(L, 1);
1671 RemotePlayer *player = getplayer(ref);
1675 video::SColor bgcolor(255,255,255,255);
1676 read_color(L, 2, &bgcolor);
1678 std::string type = luaL_checkstring(L, 3);
1680 std::vector<std::string> params;
1681 if (lua_istable(L, 4)) {
1683 while (lua_next(L, 4) != 0) {
1684 // key at index -2 and value at index -1
1685 if (lua_isstring(L, -1))
1686 params.push_back(lua_tostring(L, -1));
1688 params.push_back("");
1689 // removes value, keeps key for next iteration
1694 if (type == "skybox" && params.size() != 6)
1695 throw LuaError("skybox expects 6 textures");
1698 if (lua_isboolean(L, 5))
1699 clouds = lua_toboolean(L, 5);
1701 if (!getServer(L)->setSky(player, bgcolor, type, params, clouds))
1704 lua_pushboolean(L, true);
1709 int ObjectRef::l_get_sky(lua_State *L)
1711 NO_MAP_LOCK_REQUIRED;
1712 ObjectRef *ref = checkobject(L, 1);
1713 RemotePlayer *player = getplayer(ref);
1716 video::SColor bgcolor(255, 255, 255, 255);
1718 std::vector<std::string> params;
1721 player->getSky(&bgcolor, &type, ¶ms, &clouds);
1722 type = type == "" ? "regular" : type;
1724 push_ARGB8(L, bgcolor);
1725 lua_pushlstring(L, type.c_str(), type.size());
1728 for (std::vector<std::string>::iterator it = params.begin();
1729 it != params.end(); ++it) {
1730 lua_pushlstring(L, it->c_str(), it->size());
1731 lua_rawseti(L, -2, i);
1734 lua_pushboolean(L, clouds);
1738 // set_clouds(self, {density=, color=, ambient=, height=, thickness=, speed=})
1739 int ObjectRef::l_set_clouds(lua_State *L)
1741 NO_MAP_LOCK_REQUIRED;
1742 ObjectRef *ref = checkobject(L, 1);
1743 RemotePlayer *player = getplayer(ref);
1746 if (!lua_istable(L, 2))
1749 CloudParams cloud_params = player->getCloudParams();
1751 cloud_params.density = getfloatfield_default(L, 2, "density", cloud_params.density);
1753 lua_getfield(L, 2, "color");
1754 if (!lua_isnil(L, -1))
1755 read_color(L, -1, &cloud_params.color_bright);
1757 lua_getfield(L, 2, "ambient");
1758 if (!lua_isnil(L, -1))
1759 read_color(L, -1, &cloud_params.color_ambient);
1762 cloud_params.height = getfloatfield_default(L, 2, "height", cloud_params.height );
1763 cloud_params.thickness = getfloatfield_default(L, 2, "thickness", cloud_params.thickness);
1765 lua_getfield(L, 2, "speed");
1766 if (lua_istable(L, -1)) {
1768 new_speed.X = getfloatfield_default(L, -1, "x", 0);
1769 new_speed.Y = getfloatfield_default(L, -1, "z", 0);
1770 cloud_params.speed = new_speed;
1774 if (!getServer(L)->setClouds(player, cloud_params.density,
1775 cloud_params.color_bright, cloud_params.color_ambient,
1776 cloud_params.height, cloud_params.thickness,
1777 cloud_params.speed))
1780 player->setCloudParams(cloud_params);
1782 lua_pushboolean(L, true);
1786 int ObjectRef::l_get_clouds(lua_State *L)
1788 NO_MAP_LOCK_REQUIRED;
1789 ObjectRef *ref = checkobject(L, 1);
1790 RemotePlayer *player = getplayer(ref);
1793 const CloudParams &cloud_params = player->getCloudParams();
1796 lua_pushnumber(L, cloud_params.density);
1797 lua_setfield(L, -2, "density");
1798 push_ARGB8(L, cloud_params.color_bright);
1799 lua_setfield(L, -2, "color");
1800 push_ARGB8(L, cloud_params.color_ambient);
1801 lua_setfield(L, -2, "ambient");
1802 lua_pushnumber(L, cloud_params.height);
1803 lua_setfield(L, -2, "height");
1804 lua_pushnumber(L, cloud_params.thickness);
1805 lua_setfield(L, -2, "thickness");
1807 lua_pushnumber(L, cloud_params.speed.X);
1808 lua_setfield(L, -2, "x");
1809 lua_pushnumber(L, cloud_params.speed.Y);
1810 lua_setfield(L, -2, "y");
1811 lua_setfield(L, -2, "speed");
1817 // override_day_night_ratio(self, brightness=0...1)
1818 int ObjectRef::l_override_day_night_ratio(lua_State *L)
1820 NO_MAP_LOCK_REQUIRED;
1821 ObjectRef *ref = checkobject(L, 1);
1822 RemotePlayer *player = getplayer(ref);
1826 bool do_override = false;
1828 if (!lua_isnil(L, 2)) {
1830 ratio = luaL_checknumber(L, 2);
1833 if (!getServer(L)->overrideDayNightRatio(player, do_override, ratio))
1836 lua_pushboolean(L, true);
1840 // get_day_night_ratio(self)
1841 int ObjectRef::l_get_day_night_ratio(lua_State *L)
1843 NO_MAP_LOCK_REQUIRED;
1844 ObjectRef *ref = checkobject(L, 1);
1845 RemotePlayer *player = getplayer(ref);
1851 player->getDayNightRatio(&do_override, &ratio);
1854 lua_pushnumber(L, ratio);
1861 ObjectRef::ObjectRef(ServerActiveObject *object):
1864 //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
1867 ObjectRef::~ObjectRef()
1870 infostream<<"ObjectRef destructing for id="
1871 <<m_object->getId()<<std::endl;
1873 infostream<<"ObjectRef destructing for id=unknown"<<std::endl;*/
1876 // Creates an ObjectRef and leaves it on top of stack
1877 // Not callable from Lua; all references are created on the C side.
1878 void ObjectRef::create(lua_State *L, ServerActiveObject *object)
1880 ObjectRef *o = new ObjectRef(object);
1881 //infostream<<"ObjectRef::create: o="<<o<<std::endl;
1882 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1883 luaL_getmetatable(L, className);
1884 lua_setmetatable(L, -2);
1887 void ObjectRef::set_null(lua_State *L)
1889 ObjectRef *o = checkobject(L, -1);
1893 void ObjectRef::Register(lua_State *L)
1896 int methodtable = lua_gettop(L);
1897 luaL_newmetatable(L, className);
1898 int metatable = lua_gettop(L);
1900 lua_pushliteral(L, "__metatable");
1901 lua_pushvalue(L, methodtable);
1902 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
1904 lua_pushliteral(L, "__index");
1905 lua_pushvalue(L, methodtable);
1906 lua_settable(L, metatable);
1908 lua_pushliteral(L, "__gc");
1909 lua_pushcfunction(L, gc_object);
1910 lua_settable(L, metatable);
1912 lua_pop(L, 1); // drop metatable
1914 luaL_openlib(L, 0, methods, 0); // fill methodtable
1915 lua_pop(L, 1); // drop methodtable
1917 // Cannot be created from Lua
1918 //lua_register(L, className, create_object);
1921 const char ObjectRef::className[] = "ObjectRef";
1922 const luaL_Reg ObjectRef::methods[] = {
1923 // ServerActiveObject
1924 luamethod(ObjectRef, remove),
1925 luamethod_aliased(ObjectRef, get_pos, getpos),
1926 luamethod_aliased(ObjectRef, set_pos, setpos),
1927 luamethod_aliased(ObjectRef, move_to, moveto),
1928 luamethod(ObjectRef, punch),
1929 luamethod(ObjectRef, right_click),
1930 luamethod(ObjectRef, set_hp),
1931 luamethod(ObjectRef, get_hp),
1932 luamethod(ObjectRef, get_inventory),
1933 luamethod(ObjectRef, get_wield_list),
1934 luamethod(ObjectRef, get_wield_index),
1935 luamethod(ObjectRef, get_wielded_item),
1936 luamethod(ObjectRef, set_wielded_item),
1937 luamethod(ObjectRef, set_armor_groups),
1938 luamethod(ObjectRef, get_armor_groups),
1939 luamethod(ObjectRef, set_animation),
1940 luamethod(ObjectRef, get_animation),
1941 luamethod(ObjectRef, set_bone_position),
1942 luamethod(ObjectRef, get_bone_position),
1943 luamethod(ObjectRef, set_attach),
1944 luamethod(ObjectRef, get_attach),
1945 luamethod(ObjectRef, set_detach),
1946 luamethod(ObjectRef, set_properties),
1947 luamethod(ObjectRef, get_properties),
1948 luamethod(ObjectRef, set_nametag_attributes),
1949 luamethod(ObjectRef, get_nametag_attributes),
1950 // LuaEntitySAO-only
1951 luamethod_aliased(ObjectRef, set_velocity, setvelocity),
1952 luamethod_aliased(ObjectRef, get_velocity, getvelocity),
1953 luamethod_aliased(ObjectRef, set_acceleration, setacceleration),
1954 luamethod_aliased(ObjectRef, get_acceleration, getacceleration),
1955 luamethod_aliased(ObjectRef, set_yaw, setyaw),
1956 luamethod_aliased(ObjectRef, get_yaw, getyaw),
1957 luamethod_aliased(ObjectRef, set_texture_mod, settexturemod),
1958 luamethod_aliased(ObjectRef, set_sprite, setsprite),
1959 luamethod(ObjectRef, get_entity_name),
1960 luamethod(ObjectRef, get_luaentity),
1962 luamethod(ObjectRef, is_player),
1963 luamethod(ObjectRef, is_player_connected),
1964 luamethod(ObjectRef, get_player_name),
1965 luamethod(ObjectRef, get_player_velocity),
1966 luamethod(ObjectRef, get_look_dir),
1967 luamethod(ObjectRef, get_look_pitch),
1968 luamethod(ObjectRef, get_look_yaw),
1969 luamethod(ObjectRef, get_look_vertical),
1970 luamethod(ObjectRef, get_look_horizontal),
1971 luamethod(ObjectRef, set_look_horizontal),
1972 luamethod(ObjectRef, set_look_vertical),
1973 luamethod(ObjectRef, set_look_yaw),
1974 luamethod(ObjectRef, set_look_pitch),
1975 luamethod(ObjectRef, get_breath),
1976 luamethod(ObjectRef, set_breath),
1977 luamethod(ObjectRef, get_attribute),
1978 luamethod(ObjectRef, set_attribute),
1979 luamethod(ObjectRef, set_inventory_formspec),
1980 luamethod(ObjectRef, get_inventory_formspec),
1981 luamethod(ObjectRef, get_player_control),
1982 luamethod(ObjectRef, get_player_control_bits),
1983 luamethod(ObjectRef, set_physics_override),
1984 luamethod(ObjectRef, get_physics_override),
1985 luamethod(ObjectRef, hud_add),
1986 luamethod(ObjectRef, hud_remove),
1987 luamethod(ObjectRef, hud_change),
1988 luamethod(ObjectRef, hud_get),
1989 luamethod(ObjectRef, hud_set_flags),
1990 luamethod(ObjectRef, hud_get_flags),
1991 luamethod(ObjectRef, hud_set_hotbar_itemcount),
1992 luamethod(ObjectRef, hud_get_hotbar_itemcount),
1993 luamethod(ObjectRef, hud_set_hotbar_image),
1994 luamethod(ObjectRef, hud_get_hotbar_image),
1995 luamethod(ObjectRef, hud_set_hotbar_selected_image),
1996 luamethod(ObjectRef, hud_get_hotbar_selected_image),
1997 luamethod(ObjectRef, set_sky),
1998 luamethod(ObjectRef, get_sky),
1999 luamethod(ObjectRef, set_clouds),
2000 luamethod(ObjectRef, get_clouds),
2001 luamethod(ObjectRef, override_day_night_ratio),
2002 luamethod(ObjectRef, get_day_night_ratio),
2003 luamethod(ObjectRef, set_local_animation),
2004 luamethod(ObjectRef, get_local_animation),
2005 luamethod(ObjectRef, set_eye_offset),
2006 luamethod(ObjectRef, get_eye_offset),