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"
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"},
75 ObjectRef* ObjectRef::checkobject(lua_State *L, int narg)
77 luaL_checktype(L, narg, LUA_TUSERDATA);
78 void *ud = luaL_checkudata(L, narg, className);
79 if(!ud) luaL_typerror(L, narg, className);
80 return *(ObjectRef**)ud; // unbox pointer
83 ServerActiveObject* ObjectRef::getobject(ObjectRef *ref)
85 ServerActiveObject *co = ref->m_object;
89 LuaEntitySAO* ObjectRef::getluaobject(ObjectRef *ref)
91 ServerActiveObject *obj = getobject(ref);
94 if(obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY)
96 return (LuaEntitySAO*)obj;
99 PlayerSAO* ObjectRef::getplayersao(ObjectRef *ref)
101 ServerActiveObject *obj = getobject(ref);
104 if(obj->getType() != ACTIVEOBJECT_TYPE_PLAYER)
106 return (PlayerSAO*)obj;
109 Player* ObjectRef::getplayer(ObjectRef *ref)
111 PlayerSAO *playersao = getplayersao(ref);
112 if(playersao == NULL)
114 return playersao->getPlayer();
117 // Exported functions
120 int ObjectRef::gc_object(lua_State *L) {
121 ObjectRef *o = *(ObjectRef **)(lua_touserdata(L, 1));
122 //infostream<<"ObjectRef::gc_object: o="<<o<<std::endl;
128 int ObjectRef::l_remove(lua_State *L)
130 NO_MAP_LOCK_REQUIRED;
131 ObjectRef *ref = checkobject(L, 1);
132 ServerActiveObject *co = getobject(ref);
133 if(co == NULL) return 0;
134 verbosestream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
135 co->m_removed = true;
140 // returns: {x=num, y=num, z=num}
141 int ObjectRef::l_getpos(lua_State *L)
143 NO_MAP_LOCK_REQUIRED;
144 ObjectRef *ref = checkobject(L, 1);
145 ServerActiveObject *co = getobject(ref);
146 if(co == NULL) return 0;
147 v3f pos = co->getBasePosition() / BS;
149 lua_pushnumber(L, pos.X);
150 lua_setfield(L, -2, "x");
151 lua_pushnumber(L, pos.Y);
152 lua_setfield(L, -2, "y");
153 lua_pushnumber(L, pos.Z);
154 lua_setfield(L, -2, "z");
159 int ObjectRef::l_setpos(lua_State *L)
161 NO_MAP_LOCK_REQUIRED;
162 ObjectRef *ref = checkobject(L, 1);
163 //LuaEntitySAO *co = getluaobject(ref);
164 ServerActiveObject *co = getobject(ref);
165 if(co == NULL) return 0;
167 v3f pos = checkFloatPos(L, 2);
173 // moveto(self, pos, continuous=false)
174 int ObjectRef::l_moveto(lua_State *L)
176 NO_MAP_LOCK_REQUIRED;
177 ObjectRef *ref = checkobject(L, 1);
178 //LuaEntitySAO *co = getluaobject(ref);
179 ServerActiveObject *co = getobject(ref);
180 if(co == NULL) return 0;
182 v3f pos = checkFloatPos(L, 2);
184 bool continuous = lua_toboolean(L, 3);
186 co->moveTo(pos, continuous);
190 // punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
191 int ObjectRef::l_punch(lua_State *L)
193 NO_MAP_LOCK_REQUIRED;
194 ObjectRef *ref = checkobject(L, 1);
195 ObjectRef *puncher_ref = checkobject(L, 2);
196 ServerActiveObject *co = getobject(ref);
197 ServerActiveObject *puncher = getobject(puncher_ref);
198 if(co == NULL) return 0;
199 if(puncher == NULL) return 0;
201 if(lua_type(L, 5) != LUA_TTABLE)
202 dir = co->getBasePosition() - puncher->getBasePosition();
204 dir = read_v3f(L, 5);
205 float time_from_last_punch = 1000000;
206 if(lua_isnumber(L, 3))
207 time_from_last_punch = lua_tonumber(L, 3);
208 ToolCapabilities toolcap = read_tool_capabilities(L, 4);
211 co->punch(dir, &toolcap, puncher, time_from_last_punch);
215 // right_click(self, clicker); clicker = an another ObjectRef
216 int ObjectRef::l_right_click(lua_State *L)
218 NO_MAP_LOCK_REQUIRED;
219 ObjectRef *ref = checkobject(L, 1);
220 ObjectRef *ref2 = checkobject(L, 2);
221 ServerActiveObject *co = getobject(ref);
222 ServerActiveObject *co2 = getobject(ref2);
223 if(co == NULL) return 0;
224 if(co2 == NULL) return 0;
231 // hp = number of hitpoints (2 * number of hearts)
233 int ObjectRef::l_set_hp(lua_State *L)
235 NO_MAP_LOCK_REQUIRED;
236 ObjectRef *ref = checkobject(L, 1);
237 luaL_checknumber(L, 2);
238 ServerActiveObject *co = getobject(ref);
239 if(co == NULL) return 0;
240 int hp = lua_tonumber(L, 2);
241 /*infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
242 <<" hp="<<hp<<std::endl;*/
250 // returns: number of hitpoints (2 * number of hearts)
251 // 0 if not applicable to this type of object
252 int ObjectRef::l_get_hp(lua_State *L)
254 NO_MAP_LOCK_REQUIRED;
255 ObjectRef *ref = checkobject(L, 1);
256 ServerActiveObject *co = getobject(ref);
259 lua_pushnumber(L, 1);
262 int hp = co->getHP();
263 /*infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
264 <<" hp="<<hp<<std::endl;*/
266 lua_pushnumber(L, hp);
270 // get_inventory(self)
271 int ObjectRef::l_get_inventory(lua_State *L)
273 NO_MAP_LOCK_REQUIRED;
274 ObjectRef *ref = checkobject(L, 1);
275 ServerActiveObject *co = getobject(ref);
276 if(co == NULL) return 0;
278 InventoryLocation loc = co->getInventoryLocation();
279 if(getServer(L)->getInventory(loc) != NULL)
280 InvRef::create(L, loc);
282 lua_pushnil(L); // An object may have no inventory (nil)
286 // get_wield_list(self)
287 int ObjectRef::l_get_wield_list(lua_State *L)
289 NO_MAP_LOCK_REQUIRED;
290 ObjectRef *ref = checkobject(L, 1);
291 ServerActiveObject *co = getobject(ref);
292 if(co == NULL) return 0;
294 lua_pushstring(L, co->getWieldList().c_str());
298 // get_wield_index(self)
299 int ObjectRef::l_get_wield_index(lua_State *L)
301 NO_MAP_LOCK_REQUIRED;
302 ObjectRef *ref = checkobject(L, 1);
303 ServerActiveObject *co = getobject(ref);
304 if(co == NULL) return 0;
306 lua_pushinteger(L, co->getWieldIndex() + 1);
310 // get_wielded_item(self)
311 int ObjectRef::l_get_wielded_item(lua_State *L)
313 NO_MAP_LOCK_REQUIRED;
314 ObjectRef *ref = checkobject(L, 1);
315 ServerActiveObject *co = getobject(ref);
318 LuaItemStack::create(L, ItemStack());
322 LuaItemStack::create(L, co->getWieldedItem());
326 // set_wielded_item(self, itemstack or itemstring or table or nil)
327 int ObjectRef::l_set_wielded_item(lua_State *L)
329 NO_MAP_LOCK_REQUIRED;
330 ObjectRef *ref = checkobject(L, 1);
331 ServerActiveObject *co = getobject(ref);
332 if(co == NULL) return 0;
334 ItemStack item = read_item(L, 2, getServer(L));
335 bool success = co->setWieldedItem(item);
336 lua_pushboolean(L, success);
340 // set_armor_groups(self, groups)
341 int ObjectRef::l_set_armor_groups(lua_State *L)
343 NO_MAP_LOCK_REQUIRED;
344 ObjectRef *ref = checkobject(L, 1);
345 ServerActiveObject *co = getobject(ref);
346 if(co == NULL) return 0;
348 ItemGroupList groups;
349 read_groups(L, 2, groups);
350 co->setArmorGroups(groups);
354 // set_physics_override(self, physics_override_speed, physics_override_jump,
355 // physics_override_gravity, sneak, sneak_glitch)
356 int ObjectRef::l_set_physics_override(lua_State *L)
358 ObjectRef *ref = checkobject(L, 1);
359 PlayerSAO *co = (PlayerSAO *) getobject(ref);
360 if(co == NULL) return 0;
362 if (lua_istable(L, 2)) {
363 co->m_physics_override_speed = getfloatfield_default(L, 2, "speed", co->m_physics_override_speed);
364 co->m_physics_override_jump = getfloatfield_default(L, 2, "jump", co->m_physics_override_jump);
365 co->m_physics_override_gravity = getfloatfield_default(L, 2, "gravity", co->m_physics_override_gravity);
366 co->m_physics_override_sneak = getboolfield_default(L, 2, "sneak", co->m_physics_override_sneak);
367 co->m_physics_override_sneak_glitch = getboolfield_default(L, 2, "sneak_glitch", co->m_physics_override_sneak_glitch);
368 co->m_physics_override_sent = false;
370 // old, non-table format
371 if(!lua_isnil(L, 2)){
372 co->m_physics_override_speed = lua_tonumber(L, 2);
373 co->m_physics_override_sent = false;
375 if(!lua_isnil(L, 3)){
376 co->m_physics_override_jump = lua_tonumber(L, 3);
377 co->m_physics_override_sent = false;
379 if(!lua_isnil(L, 4)){
380 co->m_physics_override_gravity = lua_tonumber(L, 4);
381 co->m_physics_override_sent = false;
387 // set_animation(self, frame_range, frame_speed, frame_blend)
388 int ObjectRef::l_set_animation(lua_State *L)
390 NO_MAP_LOCK_REQUIRED;
391 ObjectRef *ref = checkobject(L, 1);
392 ServerActiveObject *co = getobject(ref);
393 if(co == NULL) return 0;
395 v2f frames = v2f(1, 1);
397 frames = read_v2f(L, 2);
398 float frame_speed = 15;
400 frame_speed = lua_tonumber(L, 3);
401 float frame_blend = 0;
403 frame_blend = lua_tonumber(L, 4);
404 co->setAnimation(frames, frame_speed, frame_blend);
408 // set_local_animation(self, {stand/idle}, {walk}, {dig}, {walk+dig}, frame_speed)
409 int ObjectRef::l_set_local_animation(lua_State *L)
411 //NO_MAP_LOCK_REQUIRED;
412 ObjectRef *ref = checkobject(L, 1);
413 Player *player = getplayer(ref);
418 for (int i=0;i<4;i++) {
419 if(!lua_isnil(L, 2+1))
420 frames[i] = read_v2s32(L, 2+i);
422 float frame_speed = 30;
424 frame_speed = lua_tonumber(L, 6);
426 if (!getServer(L)->setLocalPlayerAnimations(player, frames, frame_speed))
429 lua_pushboolean(L, true);
433 // set_eye_offset(self, v3f first pv, v3f third pv)
434 int ObjectRef::l_set_eye_offset(lua_State *L)
436 //NO_MAP_LOCK_REQUIRED;
437 ObjectRef *ref = checkobject(L, 1);
438 Player *player = getplayer(ref);
442 v3f offset_first = v3f(0, 0, 0);
443 v3f offset_third = v3f(0, 0, 0);
446 offset_first = read_v3f(L, 2);
448 offset_third = read_v3f(L, 3);
450 // Prevent abuse of offset values (keep player always visible)
451 offset_third.X = rangelim(offset_third.X,-10,10);
452 offset_third.Z = rangelim(offset_third.Z,-5,5);
453 /* TODO: if possible: improve the camera colision detetion to allow Y <= -1.5) */
454 offset_third.Y = rangelim(offset_third.Y,-10,15); //1.5*BS
456 if (!getServer(L)->setPlayerEyeOffset(player, offset_first, offset_third))
459 lua_pushboolean(L, true);
463 // set_bone_position(self, std::string bone, v3f position, v3f rotation)
464 int ObjectRef::l_set_bone_position(lua_State *L)
466 NO_MAP_LOCK_REQUIRED;
467 ObjectRef *ref = checkobject(L, 1);
468 ServerActiveObject *co = getobject(ref);
469 if(co == NULL) return 0;
471 std::string bone = "";
473 bone = lua_tostring(L, 2);
474 v3f position = v3f(0, 0, 0);
476 position = read_v3f(L, 3);
477 v3f rotation = v3f(0, 0, 0);
479 rotation = read_v3f(L, 4);
480 co->setBonePosition(bone, position, rotation);
484 // set_attach(self, parent, bone, position, rotation)
485 int ObjectRef::l_set_attach(lua_State *L)
487 NO_MAP_LOCK_REQUIRED;
488 ObjectRef *ref = checkobject(L, 1);
489 ObjectRef *parent_ref = checkobject(L, 2);
490 ServerActiveObject *co = getobject(ref);
491 ServerActiveObject *parent = getobject(parent_ref);
492 if(co == NULL) return 0;
493 if(parent == NULL) return 0;
495 std::string bone = "";
497 bone = lua_tostring(L, 3);
498 v3f position = v3f(0, 0, 0);
500 position = read_v3f(L, 4);
501 v3f rotation = v3f(0, 0, 0);
503 rotation = read_v3f(L, 5);
504 co->setAttachment(parent->getId(), bone, position, rotation);
509 int ObjectRef::l_set_detach(lua_State *L)
511 NO_MAP_LOCK_REQUIRED;
512 ObjectRef *ref = checkobject(L, 1);
513 ServerActiveObject *co = getobject(ref);
514 if(co == NULL) return 0;
516 co->setAttachment(0, "", v3f(0,0,0), v3f(0,0,0));
520 // set_properties(self, properties)
521 int ObjectRef::l_set_properties(lua_State *L)
523 NO_MAP_LOCK_REQUIRED;
524 ObjectRef *ref = checkobject(L, 1);
525 ServerActiveObject *co = getobject(ref);
526 if(co == NULL) return 0;
527 ObjectProperties *prop = co->accessObjectProperties();
530 read_object_properties(L, 2, prop);
531 co->notifyObjectPropertiesModified();
535 /* LuaEntitySAO-only */
537 // setvelocity(self, {x=num, y=num, z=num})
538 int ObjectRef::l_setvelocity(lua_State *L)
540 NO_MAP_LOCK_REQUIRED;
541 ObjectRef *ref = checkobject(L, 1);
542 LuaEntitySAO *co = getluaobject(ref);
543 if(co == NULL) return 0;
544 v3f pos = checkFloatPos(L, 2);
546 co->setVelocity(pos);
551 int ObjectRef::l_getvelocity(lua_State *L)
553 NO_MAP_LOCK_REQUIRED;
554 ObjectRef *ref = checkobject(L, 1);
555 LuaEntitySAO *co = getluaobject(ref);
556 if(co == NULL) return 0;
558 v3f v = co->getVelocity();
563 // setacceleration(self, {x=num, y=num, z=num})
564 int ObjectRef::l_setacceleration(lua_State *L)
566 NO_MAP_LOCK_REQUIRED;
567 ObjectRef *ref = checkobject(L, 1);
568 LuaEntitySAO *co = getluaobject(ref);
569 if(co == NULL) return 0;
571 v3f pos = checkFloatPos(L, 2);
573 co->setAcceleration(pos);
577 // getacceleration(self)
578 int ObjectRef::l_getacceleration(lua_State *L)
580 NO_MAP_LOCK_REQUIRED;
581 ObjectRef *ref = checkobject(L, 1);
582 LuaEntitySAO *co = getluaobject(ref);
583 if(co == NULL) return 0;
585 v3f v = co->getAcceleration();
590 // setyaw(self, radians)
591 int ObjectRef::l_setyaw(lua_State *L)
593 NO_MAP_LOCK_REQUIRED;
594 ObjectRef *ref = checkobject(L, 1);
595 LuaEntitySAO *co = getluaobject(ref);
596 if(co == NULL) return 0;
597 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
604 int ObjectRef::l_getyaw(lua_State *L)
606 NO_MAP_LOCK_REQUIRED;
607 ObjectRef *ref = checkobject(L, 1);
608 LuaEntitySAO *co = getluaobject(ref);
609 if(co == NULL) return 0;
611 float yaw = co->getYaw() * core::DEGTORAD;
612 lua_pushnumber(L, yaw);
616 // settexturemod(self, mod)
617 int ObjectRef::l_settexturemod(lua_State *L)
619 NO_MAP_LOCK_REQUIRED;
620 ObjectRef *ref = checkobject(L, 1);
621 LuaEntitySAO *co = getluaobject(ref);
622 if(co == NULL) return 0;
624 std::string mod = luaL_checkstring(L, 2);
625 co->setTextureMod(mod);
629 // setsprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
630 // select_horiz_by_yawpitch=false)
631 int ObjectRef::l_setsprite(lua_State *L)
633 NO_MAP_LOCK_REQUIRED;
634 ObjectRef *ref = checkobject(L, 1);
635 LuaEntitySAO *co = getluaobject(ref);
636 if(co == NULL) return 0;
640 p = read_v2s16(L, 2);
643 num_frames = lua_tonumber(L, 3);
644 float framelength = 0.2;
646 framelength = lua_tonumber(L, 4);
647 bool select_horiz_by_yawpitch = false;
649 select_horiz_by_yawpitch = lua_toboolean(L, 5);
650 co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
655 // get_entity_name(self)
656 int ObjectRef::l_get_entity_name(lua_State *L)
658 NO_MAP_LOCK_REQUIRED;
659 ObjectRef *ref = checkobject(L, 1);
660 LuaEntitySAO *co = getluaobject(ref);
661 log_deprecated(L,"Deprecated call to \"get_entity_name");
662 if(co == NULL) return 0;
664 std::string name = co->getName();
665 lua_pushstring(L, name.c_str());
669 // get_luaentity(self)
670 int ObjectRef::l_get_luaentity(lua_State *L)
672 NO_MAP_LOCK_REQUIRED;
673 ObjectRef *ref = checkobject(L, 1);
674 LuaEntitySAO *co = getluaobject(ref);
675 if(co == NULL) return 0;
677 luaentity_get(L, co->getId());
684 int ObjectRef::l_is_player(lua_State *L)
686 NO_MAP_LOCK_REQUIRED;
687 ObjectRef *ref = checkobject(L, 1);
688 Player *player = getplayer(ref);
689 lua_pushboolean(L, (player != NULL));
693 // is_player_connected(self)
694 int ObjectRef::l_is_player_connected(lua_State *L)
696 NO_MAP_LOCK_REQUIRED;
697 ObjectRef *ref = checkobject(L, 1);
698 Player *player = getplayer(ref);
699 lua_pushboolean(L, (player != NULL && player->peer_id != 0));
703 // get_player_name(self)
704 int ObjectRef::l_get_player_name(lua_State *L)
706 NO_MAP_LOCK_REQUIRED;
707 ObjectRef *ref = checkobject(L, 1);
708 Player *player = getplayer(ref);
710 lua_pushlstring(L, "", 0);
714 lua_pushstring(L, player->getName());
718 // get_look_dir(self)
719 int ObjectRef::l_get_look_dir(lua_State *L)
721 NO_MAP_LOCK_REQUIRED;
722 ObjectRef *ref = checkobject(L, 1);
723 Player *player = getplayer(ref);
724 if(player == NULL) return 0;
726 float pitch = player->getRadPitch();
727 float yaw = player->getRadYaw();
728 v3f v(cos(pitch)*cos(yaw), sin(pitch), cos(pitch)*sin(yaw));
733 // get_look_pitch(self)
734 int ObjectRef::l_get_look_pitch(lua_State *L)
736 NO_MAP_LOCK_REQUIRED;
737 ObjectRef *ref = checkobject(L, 1);
738 Player *player = getplayer(ref);
739 if(player == NULL) return 0;
741 lua_pushnumber(L, player->getRadPitch());
745 // get_look_yaw(self)
746 int ObjectRef::l_get_look_yaw(lua_State *L)
748 NO_MAP_LOCK_REQUIRED;
749 ObjectRef *ref = checkobject(L, 1);
750 Player *player = getplayer(ref);
751 if(player == NULL) return 0;
753 lua_pushnumber(L, player->getRadYaw());
757 // set_look_pitch(self, radians)
758 int ObjectRef::l_set_look_pitch(lua_State *L)
760 NO_MAP_LOCK_REQUIRED;
761 ObjectRef *ref = checkobject(L, 1);
762 PlayerSAO* co = getplayersao(ref);
763 if(co == NULL) return 0;
764 float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
770 // set_look_yaw(self, radians)
771 int ObjectRef::l_set_look_yaw(lua_State *L)
773 NO_MAP_LOCK_REQUIRED;
774 ObjectRef *ref = checkobject(L, 1);
775 PlayerSAO* co = getplayersao(ref);
776 if(co == NULL) return 0;
777 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
783 // set_breath(self, breath)
784 int ObjectRef::l_set_breath(lua_State *L)
786 NO_MAP_LOCK_REQUIRED;
787 ObjectRef *ref = checkobject(L, 1);
788 PlayerSAO* co = getplayersao(ref);
789 if(co == NULL) return 0;
790 u16 breath = luaL_checknumber(L, 2);
792 co->setBreath(breath);
793 co->m_breath_not_sent = true;
798 int ObjectRef::l_get_breath(lua_State *L)
800 NO_MAP_LOCK_REQUIRED;
801 ObjectRef *ref = checkobject(L, 1);
802 PlayerSAO* co = getplayersao(ref);
803 if(co == NULL) return 0;
805 u16 breath = co->getBreath();
806 lua_pushinteger (L, breath);
810 // set_inventory_formspec(self, formspec)
811 int ObjectRef::l_set_inventory_formspec(lua_State *L)
813 NO_MAP_LOCK_REQUIRED;
814 ObjectRef *ref = checkobject(L, 1);
815 Player *player = getplayer(ref);
816 if(player == NULL) return 0;
817 std::string formspec = luaL_checkstring(L, 2);
819 player->inventory_formspec = formspec;
820 getServer(L)->reportInventoryFormspecModified(player->getName());
821 lua_pushboolean(L, true);
825 // get_inventory_formspec(self) -> formspec
826 int ObjectRef::l_get_inventory_formspec(lua_State *L)
828 NO_MAP_LOCK_REQUIRED;
829 ObjectRef *ref = checkobject(L, 1);
830 Player *player = getplayer(ref);
831 if(player == NULL) return 0;
833 std::string formspec = player->inventory_formspec;
834 lua_pushlstring(L, formspec.c_str(), formspec.size());
838 // get_player_control(self)
839 int ObjectRef::l_get_player_control(lua_State *L)
841 NO_MAP_LOCK_REQUIRED;
842 ObjectRef *ref = checkobject(L, 1);
843 Player *player = getplayer(ref);
845 lua_pushlstring(L, "", 0);
849 PlayerControl control = player->getPlayerControl();
851 lua_pushboolean(L, control.up);
852 lua_setfield(L, -2, "up");
853 lua_pushboolean(L, control.down);
854 lua_setfield(L, -2, "down");
855 lua_pushboolean(L, control.left);
856 lua_setfield(L, -2, "left");
857 lua_pushboolean(L, control.right);
858 lua_setfield(L, -2, "right");
859 lua_pushboolean(L, control.jump);
860 lua_setfield(L, -2, "jump");
861 lua_pushboolean(L, control.aux1);
862 lua_setfield(L, -2, "aux1");
863 lua_pushboolean(L, control.sneak);
864 lua_setfield(L, -2, "sneak");
865 lua_pushboolean(L, control.LMB);
866 lua_setfield(L, -2, "LMB");
867 lua_pushboolean(L, control.RMB);
868 lua_setfield(L, -2, "RMB");
872 // get_player_control_bits(self)
873 int ObjectRef::l_get_player_control_bits(lua_State *L)
875 NO_MAP_LOCK_REQUIRED;
876 ObjectRef *ref = checkobject(L, 1);
877 Player *player = getplayer(ref);
879 lua_pushlstring(L, "", 0);
883 lua_pushnumber(L, player->keyPressed);
887 // hud_add(self, form)
888 int ObjectRef::l_hud_add(lua_State *L)
890 ObjectRef *ref = checkobject(L, 1);
891 Player *player = getplayer(ref);
895 HudElement *elem = new HudElement;
897 elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type",
898 es_HudElementType, HUD_ELEM_TEXT);
900 lua_getfield(L, 2, "position");
901 elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
904 lua_getfield(L, 2, "scale");
905 elem->scale = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
908 lua_getfield(L, 2, "size");
909 elem->size = lua_istable(L, -1) ? read_v2s32(L, -1) : v2s32();
912 elem->name = getstringfield_default(L, 2, "name", "");
913 elem->text = getstringfield_default(L, 2, "text", "");
914 elem->number = getintfield_default(L, 2, "number", 0);
915 elem->item = getintfield_default(L, 2, "item", 0);
916 elem->dir = getintfield_default(L, 2, "direction", 0);
918 // Deprecated, only for compatibility's sake
920 elem->dir = getintfield_default(L, 2, "dir", 0);
922 lua_getfield(L, 2, "alignment");
923 elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
926 lua_getfield(L, 2, "offset");
927 elem->offset = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
930 lua_getfield(L, 2, "world_pos");
931 elem->world_pos = lua_istable(L, -1) ? read_v3f(L, -1) : v3f();
934 /* check for known deprecated element usage */
935 if ((elem->type == HUD_ELEM_STATBAR) && (elem->size == v2s32())) {
936 log_deprecated(L,"Deprecated usage of statbar without size!");
939 u32 id = getServer(L)->hudAdd(player, elem);
945 lua_pushnumber(L, id);
949 // hud_remove(self, id)
950 int ObjectRef::l_hud_remove(lua_State *L)
952 ObjectRef *ref = checkobject(L, 1);
953 Player *player = getplayer(ref);
958 if (!lua_isnil(L, 2))
959 id = lua_tonumber(L, 2);
961 if (!getServer(L)->hudRemove(player, id))
964 lua_pushboolean(L, true);
968 // hud_change(self, id, stat, data)
969 int ObjectRef::l_hud_change(lua_State *L)
971 ObjectRef *ref = checkobject(L, 1);
972 Player *player = getplayer(ref);
976 u32 id = lua_isnumber(L, 2) ? lua_tonumber(L, 2) : -1;
978 HudElement *e = player->getHud(id);
982 HudElementStat stat = HUD_STAT_NUMBER;
983 if (lua_isstring(L, 3)) {
985 std::string statstr = lua_tostring(L, 3);
986 stat = string_to_enum(es_HudElementStat, statint, statstr) ?
987 (HudElementStat)statint : HUD_STAT_NUMBER;
993 e->pos = read_v2f(L, 4);
997 e->name = luaL_checkstring(L, 4);
1000 case HUD_STAT_SCALE:
1001 e->scale = read_v2f(L, 4);
1005 e->text = luaL_checkstring(L, 4);
1008 case HUD_STAT_NUMBER:
1009 e->number = luaL_checknumber(L, 4);
1013 e->item = luaL_checknumber(L, 4);
1017 e->dir = luaL_checknumber(L, 4);
1020 case HUD_STAT_ALIGN:
1021 e->align = read_v2f(L, 4);
1024 case HUD_STAT_OFFSET:
1025 e->offset = read_v2f(L, 4);
1028 case HUD_STAT_WORLD_POS:
1029 e->world_pos = read_v3f(L, 4);
1030 value = &e->world_pos;
1033 e->size = read_v2s32(L, 4);
1038 getServer(L)->hudChange(player, id, stat, value);
1040 lua_pushboolean(L, true);
1044 // hud_get(self, id)
1045 int ObjectRef::l_hud_get(lua_State *L)
1047 ObjectRef *ref = checkobject(L, 1);
1048 Player *player = getplayer(ref);
1052 u32 id = lua_tonumber(L, -1);
1054 HudElement *e = player->getHud(id);
1060 lua_pushstring(L, es_HudElementType[(u8)e->type].str);
1061 lua_setfield(L, -2, "type");
1063 push_v2f(L, e->pos);
1064 lua_setfield(L, -2, "position");
1066 lua_pushstring(L, e->name.c_str());
1067 lua_setfield(L, -2, "name");
1069 push_v2f(L, e->scale);
1070 lua_setfield(L, -2, "scale");
1072 lua_pushstring(L, e->text.c_str());
1073 lua_setfield(L, -2, "text");
1075 lua_pushnumber(L, e->number);
1076 lua_setfield(L, -2, "number");
1078 lua_pushnumber(L, e->item);
1079 lua_setfield(L, -2, "item");
1081 lua_pushnumber(L, e->dir);
1082 lua_setfield(L, -2, "direction");
1084 // Deprecated, only for compatibility's sake
1085 lua_pushnumber(L, e->dir);
1086 lua_setfield(L, -2, "dir");
1088 push_v3f(L, e->world_pos);
1089 lua_setfield(L, -2, "world_pos");
1094 // hud_set_flags(self, flags)
1095 int ObjectRef::l_hud_set_flags(lua_State *L)
1097 ObjectRef *ref = checkobject(L, 1);
1098 Player *player = getplayer(ref);
1106 const EnumString *esp = es_HudBuiltinElement;
1107 for (int i = 0; esp[i].str; i++) {
1108 if (getboolfield(L, 2, esp[i].str, flag)) {
1109 flags |= esp[i].num * flag;
1113 if (!getServer(L)->hudSetFlags(player, flags, mask))
1116 lua_pushboolean(L, true);
1120 int ObjectRef::l_hud_get_flags(lua_State *L)
1122 ObjectRef *ref = checkobject(L, 1);
1123 Player *player = getplayer(ref);
1128 lua_pushboolean(L, player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE);
1129 lua_setfield(L, -2, "hotbar");
1130 lua_pushboolean(L, player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE);
1131 lua_setfield(L, -2, "healthbar");
1132 lua_pushboolean(L, player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE);
1133 lua_setfield(L, -2, "crosshair");
1134 lua_pushboolean(L, player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE);
1135 lua_setfield(L, -2, "wielditem");
1136 lua_pushboolean(L, player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE);
1137 lua_setfield(L, -2, "breathbar");
1142 // hud_set_hotbar_itemcount(self, hotbar_itemcount)
1143 int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L)
1145 ObjectRef *ref = checkobject(L, 1);
1146 Player *player = getplayer(ref);
1150 s32 hotbar_itemcount = lua_tonumber(L, 2);
1152 if (!getServer(L)->hudSetHotbarItemcount(player, hotbar_itemcount))
1155 lua_pushboolean(L, true);
1159 // hud_set_hotbar_image(self, name)
1160 int ObjectRef::l_hud_set_hotbar_image(lua_State *L)
1162 ObjectRef *ref = checkobject(L, 1);
1163 Player *player = getplayer(ref);
1167 std::string name = lua_tostring(L, 2);
1169 getServer(L)->hudSetHotbarImage(player, name);
1173 // hud_set_hotbar_selected_image(self, name)
1174 int ObjectRef::l_hud_set_hotbar_selected_image(lua_State *L)
1176 ObjectRef *ref = checkobject(L, 1);
1177 Player *player = getplayer(ref);
1181 std::string name = lua_tostring(L, 2);
1183 getServer(L)->hudSetHotbarSelectedImage(player, name);
1187 // set_sky(self, bgcolor, type, list)
1188 int ObjectRef::l_set_sky(lua_State *L)
1190 ObjectRef *ref = checkobject(L, 1);
1191 Player *player = getplayer(ref);
1195 video::SColor bgcolor(255,255,255,255);
1196 if (!lua_isnil(L, 2))
1197 bgcolor = readARGB8(L, 2);
1199 std::string type = luaL_checkstring(L, 3);
1201 std::vector<std::string> params;
1202 if (lua_istable(L, 4)) {
1203 int table = lua_gettop(L);
1205 while (lua_next(L, table) != 0) {
1206 // key at index -2 and value at index -1
1207 if (lua_isstring(L, -1))
1208 params.push_back(lua_tostring(L, -1));
1210 params.push_back("");
1211 // removes value, keeps key for next iteration
1216 if (type == "skybox" && params.size() != 6)
1217 throw LuaError("skybox expects 6 textures");
1219 if (!getServer(L)->setSky(player, bgcolor, type, params))
1222 lua_pushboolean(L, true);
1226 // override_day_night_ratio(self, brightness=0...1)
1227 int ObjectRef::l_override_day_night_ratio(lua_State *L)
1229 ObjectRef *ref = checkobject(L, 1);
1230 Player *player = getplayer(ref);
1234 bool do_override = false;
1236 if (!lua_isnil(L, 2)){
1238 ratio = luaL_checknumber(L, 2);
1241 if (!getServer(L)->overrideDayNightRatio(player, do_override, ratio))
1244 lua_pushboolean(L, true);
1248 ObjectRef::ObjectRef(ServerActiveObject *object):
1251 //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
1254 ObjectRef::~ObjectRef()
1257 infostream<<"ObjectRef destructing for id="
1258 <<m_object->getId()<<std::endl;
1260 infostream<<"ObjectRef destructing for id=unknown"<<std::endl;*/
1263 // Creates an ObjectRef and leaves it on top of stack
1264 // Not callable from Lua; all references are created on the C side.
1265 void ObjectRef::create(lua_State *L, ServerActiveObject *object)
1267 ObjectRef *o = new ObjectRef(object);
1268 //infostream<<"ObjectRef::create: o="<<o<<std::endl;
1269 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1270 luaL_getmetatable(L, className);
1271 lua_setmetatable(L, -2);
1274 void ObjectRef::set_null(lua_State *L)
1276 ObjectRef *o = checkobject(L, -1);
1280 void ObjectRef::Register(lua_State *L)
1283 int methodtable = lua_gettop(L);
1284 luaL_newmetatable(L, className);
1285 int metatable = lua_gettop(L);
1287 lua_pushliteral(L, "__metatable");
1288 lua_pushvalue(L, methodtable);
1289 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
1291 lua_pushliteral(L, "__index");
1292 lua_pushvalue(L, methodtable);
1293 lua_settable(L, metatable);
1295 lua_pushliteral(L, "__gc");
1296 lua_pushcfunction(L, gc_object);
1297 lua_settable(L, metatable);
1299 lua_pop(L, 1); // drop metatable
1301 luaL_openlib(L, 0, methods, 0); // fill methodtable
1302 lua_pop(L, 1); // drop methodtable
1304 // Cannot be created from Lua
1305 //lua_register(L, className, create_object);
1308 const char ObjectRef::className[] = "ObjectRef";
1309 const luaL_reg ObjectRef::methods[] = {
1310 // ServerActiveObject
1311 luamethod(ObjectRef, remove),
1312 luamethod(ObjectRef, getpos),
1313 luamethod(ObjectRef, setpos),
1314 luamethod(ObjectRef, moveto),
1315 luamethod(ObjectRef, punch),
1316 luamethod(ObjectRef, right_click),
1317 luamethod(ObjectRef, set_hp),
1318 luamethod(ObjectRef, get_hp),
1319 luamethod(ObjectRef, get_inventory),
1320 luamethod(ObjectRef, get_wield_list),
1321 luamethod(ObjectRef, get_wield_index),
1322 luamethod(ObjectRef, get_wielded_item),
1323 luamethod(ObjectRef, set_wielded_item),
1324 luamethod(ObjectRef, set_armor_groups),
1325 luamethod(ObjectRef, set_physics_override),
1326 luamethod(ObjectRef, set_animation),
1327 luamethod(ObjectRef, set_bone_position),
1328 luamethod(ObjectRef, set_attach),
1329 luamethod(ObjectRef, set_detach),
1330 luamethod(ObjectRef, set_properties),
1331 // LuaEntitySAO-only
1332 luamethod(ObjectRef, setvelocity),
1333 luamethod(ObjectRef, getvelocity),
1334 luamethod(ObjectRef, setacceleration),
1335 luamethod(ObjectRef, getacceleration),
1336 luamethod(ObjectRef, setyaw),
1337 luamethod(ObjectRef, getyaw),
1338 luamethod(ObjectRef, settexturemod),
1339 luamethod(ObjectRef, setsprite),
1340 luamethod(ObjectRef, get_entity_name),
1341 luamethod(ObjectRef, get_luaentity),
1343 luamethod(ObjectRef, is_player),
1344 luamethod(ObjectRef, is_player_connected),
1345 luamethod(ObjectRef, get_player_name),
1346 luamethod(ObjectRef, get_look_dir),
1347 luamethod(ObjectRef, get_look_pitch),
1348 luamethod(ObjectRef, get_look_yaw),
1349 luamethod(ObjectRef, set_look_yaw),
1350 luamethod(ObjectRef, set_look_pitch),
1351 luamethod(ObjectRef, get_breath),
1352 luamethod(ObjectRef, set_breath),
1353 luamethod(ObjectRef, set_inventory_formspec),
1354 luamethod(ObjectRef, get_inventory_formspec),
1355 luamethod(ObjectRef, get_player_control),
1356 luamethod(ObjectRef, get_player_control_bits),
1357 luamethod(ObjectRef, hud_add),
1358 luamethod(ObjectRef, hud_remove),
1359 luamethod(ObjectRef, hud_change),
1360 luamethod(ObjectRef, hud_get),
1361 luamethod(ObjectRef, hud_set_flags),
1362 luamethod(ObjectRef, hud_get_flags),
1363 luamethod(ObjectRef, hud_set_hotbar_itemcount),
1364 luamethod(ObjectRef, hud_set_hotbar_image),
1365 luamethod(ObjectRef, hud_set_hotbar_selected_image),
1366 luamethod(ObjectRef, set_sky),
1367 luamethod(ObjectRef, override_day_night_ratio),
1368 luamethod(ObjectRef, set_local_animation),
1369 luamethod(ObjectRef, set_eye_offset),