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 if(co->getType() == ACTIVEOBJECT_TYPE_PLAYER) return 0;
135 verbosestream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
136 co->m_removed = true;
141 // returns: {x=num, y=num, z=num}
142 int ObjectRef::l_getpos(lua_State *L)
144 NO_MAP_LOCK_REQUIRED;
145 ObjectRef *ref = checkobject(L, 1);
146 ServerActiveObject *co = getobject(ref);
147 if(co == NULL) return 0;
148 v3f pos = co->getBasePosition() / BS;
150 lua_pushnumber(L, pos.X);
151 lua_setfield(L, -2, "x");
152 lua_pushnumber(L, pos.Y);
153 lua_setfield(L, -2, "y");
154 lua_pushnumber(L, pos.Z);
155 lua_setfield(L, -2, "z");
160 int ObjectRef::l_setpos(lua_State *L)
162 NO_MAP_LOCK_REQUIRED;
163 ObjectRef *ref = checkobject(L, 1);
164 //LuaEntitySAO *co = getluaobject(ref);
165 ServerActiveObject *co = getobject(ref);
166 if(co == NULL) return 0;
168 v3f pos = checkFloatPos(L, 2);
174 // moveto(self, pos, continuous=false)
175 int ObjectRef::l_moveto(lua_State *L)
177 NO_MAP_LOCK_REQUIRED;
178 ObjectRef *ref = checkobject(L, 1);
179 //LuaEntitySAO *co = getluaobject(ref);
180 ServerActiveObject *co = getobject(ref);
181 if(co == NULL) return 0;
183 v3f pos = checkFloatPos(L, 2);
185 bool continuous = lua_toboolean(L, 3);
187 co->moveTo(pos, continuous);
191 // punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
192 int ObjectRef::l_punch(lua_State *L)
194 NO_MAP_LOCK_REQUIRED;
195 ObjectRef *ref = checkobject(L, 1);
196 ObjectRef *puncher_ref = checkobject(L, 2);
197 ServerActiveObject *co = getobject(ref);
198 ServerActiveObject *puncher = getobject(puncher_ref);
199 if(co == NULL) return 0;
200 if(puncher == NULL) return 0;
202 if(lua_type(L, 5) != LUA_TTABLE)
203 dir = co->getBasePosition() - puncher->getBasePosition();
205 dir = read_v3f(L, 5);
206 float time_from_last_punch = 1000000;
207 if(lua_isnumber(L, 3))
208 time_from_last_punch = lua_tonumber(L, 3);
209 ToolCapabilities toolcap = read_tool_capabilities(L, 4);
212 s16 src_original_hp = co->getHP();
213 s16 dst_origin_hp = puncher->getHP();
216 co->punch(dir, &toolcap, puncher, time_from_last_punch);
218 // If the punched is a player, and its HP changed
219 if (src_original_hp != co->getHP() &&
220 co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
221 getServer(L)->SendPlayerHPOrDie(((PlayerSAO*)co)->getPeerID(),
225 // If the puncher is a player, and its HP changed
226 if (dst_origin_hp != puncher->getHP() &&
227 puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
228 getServer(L)->SendPlayerHPOrDie(((PlayerSAO*)puncher)->getPeerID(),
229 puncher->getHP() == 0);
234 // right_click(self, clicker); clicker = an another ObjectRef
235 int ObjectRef::l_right_click(lua_State *L)
237 NO_MAP_LOCK_REQUIRED;
238 ObjectRef *ref = checkobject(L, 1);
239 ObjectRef *ref2 = checkobject(L, 2);
240 ServerActiveObject *co = getobject(ref);
241 ServerActiveObject *co2 = getobject(ref2);
242 if(co == NULL) return 0;
243 if(co2 == NULL) return 0;
250 // hp = number of hitpoints (2 * number of hearts)
252 int ObjectRef::l_set_hp(lua_State *L)
254 NO_MAP_LOCK_REQUIRED;
255 ObjectRef *ref = checkobject(L, 1);
256 luaL_checknumber(L, 2);
257 ServerActiveObject *co = getobject(ref);
258 if(co == NULL) return 0;
259 int hp = lua_tonumber(L, 2);
260 /*infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
261 <<" hp="<<hp<<std::endl;*/
264 if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
265 getServer(L)->SendPlayerHPOrDie(((PlayerSAO*)co)->getPeerID(), co->getHP() == 0);
272 // returns: number of hitpoints (2 * number of hearts)
273 // 0 if not applicable to this type of object
274 int ObjectRef::l_get_hp(lua_State *L)
276 NO_MAP_LOCK_REQUIRED;
277 ObjectRef *ref = checkobject(L, 1);
278 ServerActiveObject *co = getobject(ref);
281 lua_pushnumber(L, 1);
284 int hp = co->getHP();
285 /*infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
286 <<" hp="<<hp<<std::endl;*/
288 lua_pushnumber(L, hp);
292 // get_inventory(self)
293 int ObjectRef::l_get_inventory(lua_State *L)
295 NO_MAP_LOCK_REQUIRED;
296 ObjectRef *ref = checkobject(L, 1);
297 ServerActiveObject *co = getobject(ref);
298 if(co == NULL) return 0;
300 InventoryLocation loc = co->getInventoryLocation();
301 if(getServer(L)->getInventory(loc) != NULL)
302 InvRef::create(L, loc);
304 lua_pushnil(L); // An object may have no inventory (nil)
308 // get_wield_list(self)
309 int ObjectRef::l_get_wield_list(lua_State *L)
311 NO_MAP_LOCK_REQUIRED;
312 ObjectRef *ref = checkobject(L, 1);
313 ServerActiveObject *co = getobject(ref);
314 if(co == NULL) return 0;
316 lua_pushstring(L, co->getWieldList().c_str());
320 // get_wield_index(self)
321 int ObjectRef::l_get_wield_index(lua_State *L)
323 NO_MAP_LOCK_REQUIRED;
324 ObjectRef *ref = checkobject(L, 1);
325 ServerActiveObject *co = getobject(ref);
326 if(co == NULL) return 0;
328 lua_pushinteger(L, co->getWieldIndex() + 1);
332 // get_wielded_item(self)
333 int ObjectRef::l_get_wielded_item(lua_State *L)
335 NO_MAP_LOCK_REQUIRED;
336 ObjectRef *ref = checkobject(L, 1);
337 ServerActiveObject *co = getobject(ref);
340 LuaItemStack::create(L, ItemStack());
344 LuaItemStack::create(L, co->getWieldedItem());
348 // set_wielded_item(self, itemstack or itemstring or table or nil)
349 int ObjectRef::l_set_wielded_item(lua_State *L)
351 NO_MAP_LOCK_REQUIRED;
352 ObjectRef *ref = checkobject(L, 1);
353 ServerActiveObject *co = getobject(ref);
354 if(co == NULL) return 0;
356 ItemStack item = read_item(L, 2, getServer(L));
357 bool success = co->setWieldedItem(item);
358 if (success && co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
359 getServer(L)->SendInventory(((PlayerSAO*)co));
361 lua_pushboolean(L, success);
365 // set_armor_groups(self, groups)
366 int ObjectRef::l_set_armor_groups(lua_State *L)
368 NO_MAP_LOCK_REQUIRED;
369 ObjectRef *ref = checkobject(L, 1);
370 ServerActiveObject *co = getobject(ref);
371 if(co == NULL) return 0;
373 ItemGroupList groups;
374 read_groups(L, 2, groups);
375 co->setArmorGroups(groups);
379 // set_physics_override(self, physics_override_speed, physics_override_jump,
380 // physics_override_gravity, sneak, sneak_glitch)
381 int ObjectRef::l_set_physics_override(lua_State *L)
383 ObjectRef *ref = checkobject(L, 1);
384 PlayerSAO *co = (PlayerSAO *) getobject(ref);
385 if(co == NULL) return 0;
387 if (lua_istable(L, 2)) {
388 co->m_physics_override_speed = getfloatfield_default(L, 2, "speed", co->m_physics_override_speed);
389 co->m_physics_override_jump = getfloatfield_default(L, 2, "jump", co->m_physics_override_jump);
390 co->m_physics_override_gravity = getfloatfield_default(L, 2, "gravity", co->m_physics_override_gravity);
391 co->m_physics_override_sneak = getboolfield_default(L, 2, "sneak", co->m_physics_override_sneak);
392 co->m_physics_override_sneak_glitch = getboolfield_default(L, 2, "sneak_glitch", co->m_physics_override_sneak_glitch);
393 co->m_physics_override_sent = false;
395 // old, non-table format
396 if(!lua_isnil(L, 2)){
397 co->m_physics_override_speed = lua_tonumber(L, 2);
398 co->m_physics_override_sent = false;
400 if(!lua_isnil(L, 3)){
401 co->m_physics_override_jump = lua_tonumber(L, 3);
402 co->m_physics_override_sent = false;
404 if(!lua_isnil(L, 4)){
405 co->m_physics_override_gravity = lua_tonumber(L, 4);
406 co->m_physics_override_sent = false;
412 // set_animation(self, frame_range, frame_speed, frame_blend)
413 int ObjectRef::l_set_animation(lua_State *L)
415 NO_MAP_LOCK_REQUIRED;
416 ObjectRef *ref = checkobject(L, 1);
417 ServerActiveObject *co = getobject(ref);
418 if(co == NULL) return 0;
420 v2f frames = v2f(1, 1);
422 frames = read_v2f(L, 2);
423 float frame_speed = 15;
425 frame_speed = lua_tonumber(L, 3);
426 float frame_blend = 0;
428 frame_blend = lua_tonumber(L, 4);
429 co->setAnimation(frames, frame_speed, frame_blend);
433 // set_local_animation(self, {stand/idle}, {walk}, {dig}, {walk+dig}, frame_speed)
434 int ObjectRef::l_set_local_animation(lua_State *L)
436 //NO_MAP_LOCK_REQUIRED;
437 ObjectRef *ref = checkobject(L, 1);
438 Player *player = getplayer(ref);
443 for (int i=0;i<4;i++) {
444 if(!lua_isnil(L, 2+1))
445 frames[i] = read_v2s32(L, 2+i);
447 float frame_speed = 30;
449 frame_speed = lua_tonumber(L, 6);
451 if (!getServer(L)->setLocalPlayerAnimations(player, frames, frame_speed))
454 lua_pushboolean(L, true);
458 // set_eye_offset(self, v3f first pv, v3f third pv)
459 int ObjectRef::l_set_eye_offset(lua_State *L)
461 //NO_MAP_LOCK_REQUIRED;
462 ObjectRef *ref = checkobject(L, 1);
463 Player *player = getplayer(ref);
467 v3f offset_first = v3f(0, 0, 0);
468 v3f offset_third = v3f(0, 0, 0);
471 offset_first = read_v3f(L, 2);
473 offset_third = read_v3f(L, 3);
475 // Prevent abuse of offset values (keep player always visible)
476 offset_third.X = rangelim(offset_third.X,-10,10);
477 offset_third.Z = rangelim(offset_third.Z,-5,5);
478 /* TODO: if possible: improve the camera colision detetion to allow Y <= -1.5) */
479 offset_third.Y = rangelim(offset_third.Y,-10,15); //1.5*BS
481 if (!getServer(L)->setPlayerEyeOffset(player, offset_first, offset_third))
484 lua_pushboolean(L, true);
488 // set_bone_position(self, std::string bone, v3f position, v3f rotation)
489 int ObjectRef::l_set_bone_position(lua_State *L)
491 NO_MAP_LOCK_REQUIRED;
492 ObjectRef *ref = checkobject(L, 1);
493 ServerActiveObject *co = getobject(ref);
494 if(co == NULL) return 0;
496 std::string bone = "";
498 bone = lua_tostring(L, 2);
499 v3f position = v3f(0, 0, 0);
501 position = read_v3f(L, 3);
502 v3f rotation = v3f(0, 0, 0);
504 rotation = read_v3f(L, 4);
505 co->setBonePosition(bone, position, rotation);
509 // set_attach(self, parent, bone, position, rotation)
510 int ObjectRef::l_set_attach(lua_State *L)
512 NO_MAP_LOCK_REQUIRED;
513 ObjectRef *ref = checkobject(L, 1);
514 ObjectRef *parent_ref = checkobject(L, 2);
515 ServerActiveObject *co = getobject(ref);
516 ServerActiveObject *parent = getobject(parent_ref);
517 if(co == NULL) return 0;
518 if(parent == NULL) return 0;
520 std::string bone = "";
522 bone = lua_tostring(L, 3);
523 v3f position = v3f(0, 0, 0);
525 position = read_v3f(L, 4);
526 v3f rotation = v3f(0, 0, 0);
528 rotation = read_v3f(L, 5);
529 co->setAttachment(parent->getId(), bone, position, rotation);
534 int ObjectRef::l_set_detach(lua_State *L)
536 NO_MAP_LOCK_REQUIRED;
537 ObjectRef *ref = checkobject(L, 1);
538 ServerActiveObject *co = getobject(ref);
539 if(co == NULL) return 0;
541 co->setAttachment(0, "", v3f(0,0,0), v3f(0,0,0));
545 // set_properties(self, properties)
546 int ObjectRef::l_set_properties(lua_State *L)
548 NO_MAP_LOCK_REQUIRED;
549 ObjectRef *ref = checkobject(L, 1);
550 ServerActiveObject *co = getobject(ref);
551 if(co == NULL) return 0;
552 ObjectProperties *prop = co->accessObjectProperties();
555 read_object_properties(L, 2, prop);
556 co->notifyObjectPropertiesModified();
561 int ObjectRef::l_is_player(lua_State *L)
563 NO_MAP_LOCK_REQUIRED;
564 ObjectRef *ref = checkobject(L, 1);
565 Player *player = getplayer(ref);
566 lua_pushboolean(L, (player != NULL));
570 /* LuaEntitySAO-only */
572 // setvelocity(self, {x=num, y=num, z=num})
573 int ObjectRef::l_setvelocity(lua_State *L)
575 NO_MAP_LOCK_REQUIRED;
576 ObjectRef *ref = checkobject(L, 1);
577 LuaEntitySAO *co = getluaobject(ref);
578 if(co == NULL) return 0;
579 v3f pos = checkFloatPos(L, 2);
581 co->setVelocity(pos);
586 int ObjectRef::l_getvelocity(lua_State *L)
588 NO_MAP_LOCK_REQUIRED;
589 ObjectRef *ref = checkobject(L, 1);
590 LuaEntitySAO *co = getluaobject(ref);
591 if(co == NULL) return 0;
593 v3f v = co->getVelocity();
598 // setacceleration(self, {x=num, y=num, z=num})
599 int ObjectRef::l_setacceleration(lua_State *L)
601 NO_MAP_LOCK_REQUIRED;
602 ObjectRef *ref = checkobject(L, 1);
603 LuaEntitySAO *co = getluaobject(ref);
604 if(co == NULL) return 0;
606 v3f pos = checkFloatPos(L, 2);
608 co->setAcceleration(pos);
612 // getacceleration(self)
613 int ObjectRef::l_getacceleration(lua_State *L)
615 NO_MAP_LOCK_REQUIRED;
616 ObjectRef *ref = checkobject(L, 1);
617 LuaEntitySAO *co = getluaobject(ref);
618 if(co == NULL) return 0;
620 v3f v = co->getAcceleration();
625 // setyaw(self, radians)
626 int ObjectRef::l_setyaw(lua_State *L)
628 NO_MAP_LOCK_REQUIRED;
629 ObjectRef *ref = checkobject(L, 1);
630 LuaEntitySAO *co = getluaobject(ref);
631 if(co == NULL) return 0;
632 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
639 int ObjectRef::l_getyaw(lua_State *L)
641 NO_MAP_LOCK_REQUIRED;
642 ObjectRef *ref = checkobject(L, 1);
643 LuaEntitySAO *co = getluaobject(ref);
644 if(co == NULL) return 0;
646 float yaw = co->getYaw() * core::DEGTORAD;
647 lua_pushnumber(L, yaw);
651 // settexturemod(self, mod)
652 int ObjectRef::l_settexturemod(lua_State *L)
654 NO_MAP_LOCK_REQUIRED;
655 ObjectRef *ref = checkobject(L, 1);
656 LuaEntitySAO *co = getluaobject(ref);
657 if(co == NULL) return 0;
659 std::string mod = luaL_checkstring(L, 2);
660 co->setTextureMod(mod);
664 // setsprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
665 // select_horiz_by_yawpitch=false)
666 int ObjectRef::l_setsprite(lua_State *L)
668 NO_MAP_LOCK_REQUIRED;
669 ObjectRef *ref = checkobject(L, 1);
670 LuaEntitySAO *co = getluaobject(ref);
671 if(co == NULL) return 0;
675 p = read_v2s16(L, 2);
678 num_frames = lua_tonumber(L, 3);
679 float framelength = 0.2;
681 framelength = lua_tonumber(L, 4);
682 bool select_horiz_by_yawpitch = false;
684 select_horiz_by_yawpitch = lua_toboolean(L, 5);
685 co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
690 // get_entity_name(self)
691 int ObjectRef::l_get_entity_name(lua_State *L)
693 NO_MAP_LOCK_REQUIRED;
694 ObjectRef *ref = checkobject(L, 1);
695 LuaEntitySAO *co = getluaobject(ref);
696 log_deprecated(L,"Deprecated call to \"get_entity_name");
697 if(co == NULL) return 0;
699 std::string name = co->getName();
700 lua_pushstring(L, name.c_str());
704 // get_luaentity(self)
705 int ObjectRef::l_get_luaentity(lua_State *L)
707 NO_MAP_LOCK_REQUIRED;
708 ObjectRef *ref = checkobject(L, 1);
709 LuaEntitySAO *co = getluaobject(ref);
710 if(co == NULL) return 0;
712 luaentity_get(L, co->getId());
718 // is_player_connected(self)
719 int ObjectRef::l_is_player_connected(lua_State *L)
721 NO_MAP_LOCK_REQUIRED;
722 ObjectRef *ref = checkobject(L, 1);
723 Player *player = getplayer(ref);
724 lua_pushboolean(L, (player != NULL && player->peer_id != 0));
728 // get_player_name(self)
729 int ObjectRef::l_get_player_name(lua_State *L)
731 NO_MAP_LOCK_REQUIRED;
732 ObjectRef *ref = checkobject(L, 1);
733 Player *player = getplayer(ref);
735 lua_pushlstring(L, "", 0);
739 lua_pushstring(L, player->getName());
743 // get_look_dir(self)
744 int ObjectRef::l_get_look_dir(lua_State *L)
746 NO_MAP_LOCK_REQUIRED;
747 ObjectRef *ref = checkobject(L, 1);
748 Player *player = getplayer(ref);
749 if(player == NULL) return 0;
751 float pitch = player->getRadPitch();
752 float yaw = player->getRadYaw();
753 v3f v(cos(pitch)*cos(yaw), sin(pitch), cos(pitch)*sin(yaw));
758 // get_look_pitch(self)
759 int ObjectRef::l_get_look_pitch(lua_State *L)
761 NO_MAP_LOCK_REQUIRED;
762 ObjectRef *ref = checkobject(L, 1);
763 Player *player = getplayer(ref);
764 if(player == NULL) return 0;
766 lua_pushnumber(L, player->getRadPitch());
770 // get_look_yaw(self)
771 int ObjectRef::l_get_look_yaw(lua_State *L)
773 NO_MAP_LOCK_REQUIRED;
774 ObjectRef *ref = checkobject(L, 1);
775 Player *player = getplayer(ref);
776 if(player == NULL) return 0;
778 lua_pushnumber(L, player->getRadYaw());
782 // set_look_pitch(self, radians)
783 int ObjectRef::l_set_look_pitch(lua_State *L)
785 NO_MAP_LOCK_REQUIRED;
786 ObjectRef *ref = checkobject(L, 1);
787 PlayerSAO* co = getplayersao(ref);
788 if(co == NULL) return 0;
789 float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
795 // set_look_yaw(self, radians)
796 int ObjectRef::l_set_look_yaw(lua_State *L)
798 NO_MAP_LOCK_REQUIRED;
799 ObjectRef *ref = checkobject(L, 1);
800 PlayerSAO* co = getplayersao(ref);
801 if(co == NULL) return 0;
802 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
808 // set_breath(self, breath)
809 int ObjectRef::l_set_breath(lua_State *L)
811 NO_MAP_LOCK_REQUIRED;
812 ObjectRef *ref = checkobject(L, 1);
813 PlayerSAO* co = getplayersao(ref);
814 if(co == NULL) return 0;
815 u16 breath = luaL_checknumber(L, 2);
817 co->setBreath(breath);
819 // If the object is a player sent the breath to client
820 if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
821 getServer(L)->SendPlayerBreath(((PlayerSAO*)co)->getPeerID());
827 int ObjectRef::l_get_breath(lua_State *L)
829 NO_MAP_LOCK_REQUIRED;
830 ObjectRef *ref = checkobject(L, 1);
831 PlayerSAO* co = getplayersao(ref);
832 if(co == NULL) return 0;
834 u16 breath = co->getBreath();
835 lua_pushinteger (L, breath);
839 // set_inventory_formspec(self, formspec)
840 int ObjectRef::l_set_inventory_formspec(lua_State *L)
842 NO_MAP_LOCK_REQUIRED;
843 ObjectRef *ref = checkobject(L, 1);
844 Player *player = getplayer(ref);
845 if(player == NULL) return 0;
846 std::string formspec = luaL_checkstring(L, 2);
848 player->inventory_formspec = formspec;
849 getServer(L)->reportInventoryFormspecModified(player->getName());
850 lua_pushboolean(L, true);
854 // get_inventory_formspec(self) -> formspec
855 int ObjectRef::l_get_inventory_formspec(lua_State *L)
857 NO_MAP_LOCK_REQUIRED;
858 ObjectRef *ref = checkobject(L, 1);
859 Player *player = getplayer(ref);
860 if(player == NULL) return 0;
862 std::string formspec = player->inventory_formspec;
863 lua_pushlstring(L, formspec.c_str(), formspec.size());
867 // get_player_control(self)
868 int ObjectRef::l_get_player_control(lua_State *L)
870 NO_MAP_LOCK_REQUIRED;
871 ObjectRef *ref = checkobject(L, 1);
872 Player *player = getplayer(ref);
874 lua_pushlstring(L, "", 0);
878 PlayerControl control = player->getPlayerControl();
880 lua_pushboolean(L, control.up);
881 lua_setfield(L, -2, "up");
882 lua_pushboolean(L, control.down);
883 lua_setfield(L, -2, "down");
884 lua_pushboolean(L, control.left);
885 lua_setfield(L, -2, "left");
886 lua_pushboolean(L, control.right);
887 lua_setfield(L, -2, "right");
888 lua_pushboolean(L, control.jump);
889 lua_setfield(L, -2, "jump");
890 lua_pushboolean(L, control.aux1);
891 lua_setfield(L, -2, "aux1");
892 lua_pushboolean(L, control.sneak);
893 lua_setfield(L, -2, "sneak");
894 lua_pushboolean(L, control.LMB);
895 lua_setfield(L, -2, "LMB");
896 lua_pushboolean(L, control.RMB);
897 lua_setfield(L, -2, "RMB");
901 // get_player_control_bits(self)
902 int ObjectRef::l_get_player_control_bits(lua_State *L)
904 NO_MAP_LOCK_REQUIRED;
905 ObjectRef *ref = checkobject(L, 1);
906 Player *player = getplayer(ref);
908 lua_pushlstring(L, "", 0);
912 lua_pushnumber(L, player->keyPressed);
916 // hud_add(self, form)
917 int ObjectRef::l_hud_add(lua_State *L)
919 ObjectRef *ref = checkobject(L, 1);
920 Player *player = getplayer(ref);
924 HudElement *elem = new HudElement;
926 elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type",
927 es_HudElementType, HUD_ELEM_TEXT);
929 lua_getfield(L, 2, "position");
930 elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
933 lua_getfield(L, 2, "scale");
934 elem->scale = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
937 lua_getfield(L, 2, "size");
938 elem->size = lua_istable(L, -1) ? read_v2s32(L, -1) : v2s32();
941 elem->name = getstringfield_default(L, 2, "name", "");
942 elem->text = getstringfield_default(L, 2, "text", "");
943 elem->number = getintfield_default(L, 2, "number", 0);
944 elem->item = getintfield_default(L, 2, "item", 0);
945 elem->dir = getintfield_default(L, 2, "direction", 0);
947 // Deprecated, only for compatibility's sake
949 elem->dir = getintfield_default(L, 2, "dir", 0);
951 lua_getfield(L, 2, "alignment");
952 elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
955 lua_getfield(L, 2, "offset");
956 elem->offset = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
959 lua_getfield(L, 2, "world_pos");
960 elem->world_pos = lua_istable(L, -1) ? read_v3f(L, -1) : v3f();
963 /* check for known deprecated element usage */
964 if ((elem->type == HUD_ELEM_STATBAR) && (elem->size == v2s32())) {
965 log_deprecated(L,"Deprecated usage of statbar without size!");
968 u32 id = getServer(L)->hudAdd(player, elem);
974 lua_pushnumber(L, id);
978 // hud_remove(self, id)
979 int ObjectRef::l_hud_remove(lua_State *L)
981 ObjectRef *ref = checkobject(L, 1);
982 Player *player = getplayer(ref);
987 if (!lua_isnil(L, 2))
988 id = lua_tonumber(L, 2);
990 if (!getServer(L)->hudRemove(player, id))
993 lua_pushboolean(L, true);
997 // hud_change(self, id, stat, data)
998 int ObjectRef::l_hud_change(lua_State *L)
1000 ObjectRef *ref = checkobject(L, 1);
1001 Player *player = getplayer(ref);
1005 u32 id = lua_isnumber(L, 2) ? lua_tonumber(L, 2) : -1;
1007 HudElement *e = player->getHud(id);
1011 HudElementStat stat = HUD_STAT_NUMBER;
1012 if (lua_isstring(L, 3)) {
1014 std::string statstr = lua_tostring(L, 3);
1015 stat = string_to_enum(es_HudElementStat, statint, statstr) ?
1016 (HudElementStat)statint : HUD_STAT_NUMBER;
1022 e->pos = read_v2f(L, 4);
1026 e->name = luaL_checkstring(L, 4);
1029 case HUD_STAT_SCALE:
1030 e->scale = read_v2f(L, 4);
1034 e->text = luaL_checkstring(L, 4);
1037 case HUD_STAT_NUMBER:
1038 e->number = luaL_checknumber(L, 4);
1042 e->item = luaL_checknumber(L, 4);
1046 e->dir = luaL_checknumber(L, 4);
1049 case HUD_STAT_ALIGN:
1050 e->align = read_v2f(L, 4);
1053 case HUD_STAT_OFFSET:
1054 e->offset = read_v2f(L, 4);
1057 case HUD_STAT_WORLD_POS:
1058 e->world_pos = read_v3f(L, 4);
1059 value = &e->world_pos;
1062 e->size = read_v2s32(L, 4);
1067 getServer(L)->hudChange(player, id, stat, value);
1069 lua_pushboolean(L, true);
1073 // hud_get(self, id)
1074 int ObjectRef::l_hud_get(lua_State *L)
1076 ObjectRef *ref = checkobject(L, 1);
1077 Player *player = getplayer(ref);
1081 u32 id = lua_tonumber(L, -1);
1083 HudElement *e = player->getHud(id);
1089 lua_pushstring(L, es_HudElementType[(u8)e->type].str);
1090 lua_setfield(L, -2, "type");
1092 push_v2f(L, e->pos);
1093 lua_setfield(L, -2, "position");
1095 lua_pushstring(L, e->name.c_str());
1096 lua_setfield(L, -2, "name");
1098 push_v2f(L, e->scale);
1099 lua_setfield(L, -2, "scale");
1101 lua_pushstring(L, e->text.c_str());
1102 lua_setfield(L, -2, "text");
1104 lua_pushnumber(L, e->number);
1105 lua_setfield(L, -2, "number");
1107 lua_pushnumber(L, e->item);
1108 lua_setfield(L, -2, "item");
1110 lua_pushnumber(L, e->dir);
1111 lua_setfield(L, -2, "direction");
1113 // Deprecated, only for compatibility's sake
1114 lua_pushnumber(L, e->dir);
1115 lua_setfield(L, -2, "dir");
1117 push_v3f(L, e->world_pos);
1118 lua_setfield(L, -2, "world_pos");
1123 // hud_set_flags(self, flags)
1124 int ObjectRef::l_hud_set_flags(lua_State *L)
1126 ObjectRef *ref = checkobject(L, 1);
1127 Player *player = getplayer(ref);
1135 const EnumString *esp = es_HudBuiltinElement;
1136 for (int i = 0; esp[i].str; i++) {
1137 if (getboolfield(L, 2, esp[i].str, flag)) {
1138 flags |= esp[i].num * flag;
1142 if (!getServer(L)->hudSetFlags(player, flags, mask))
1145 lua_pushboolean(L, true);
1149 int ObjectRef::l_hud_get_flags(lua_State *L)
1151 ObjectRef *ref = checkobject(L, 1);
1152 Player *player = getplayer(ref);
1157 lua_pushboolean(L, player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE);
1158 lua_setfield(L, -2, "hotbar");
1159 lua_pushboolean(L, player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE);
1160 lua_setfield(L, -2, "healthbar");
1161 lua_pushboolean(L, player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE);
1162 lua_setfield(L, -2, "crosshair");
1163 lua_pushboolean(L, player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE);
1164 lua_setfield(L, -2, "wielditem");
1165 lua_pushboolean(L, player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE);
1166 lua_setfield(L, -2, "breathbar");
1171 // hud_set_hotbar_itemcount(self, hotbar_itemcount)
1172 int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L)
1174 ObjectRef *ref = checkobject(L, 1);
1175 Player *player = getplayer(ref);
1179 s32 hotbar_itemcount = lua_tonumber(L, 2);
1181 if (!getServer(L)->hudSetHotbarItemcount(player, hotbar_itemcount))
1184 lua_pushboolean(L, true);
1188 // hud_set_hotbar_image(self, name)
1189 int ObjectRef::l_hud_set_hotbar_image(lua_State *L)
1191 ObjectRef *ref = checkobject(L, 1);
1192 Player *player = getplayer(ref);
1196 std::string name = lua_tostring(L, 2);
1198 getServer(L)->hudSetHotbarImage(player, name);
1202 // hud_set_hotbar_selected_image(self, name)
1203 int ObjectRef::l_hud_set_hotbar_selected_image(lua_State *L)
1205 ObjectRef *ref = checkobject(L, 1);
1206 Player *player = getplayer(ref);
1210 std::string name = lua_tostring(L, 2);
1212 getServer(L)->hudSetHotbarSelectedImage(player, name);
1216 // set_sky(self, bgcolor, type, list)
1217 int ObjectRef::l_set_sky(lua_State *L)
1219 ObjectRef *ref = checkobject(L, 1);
1220 Player *player = getplayer(ref);
1224 video::SColor bgcolor(255,255,255,255);
1225 read_color(L, 2, &bgcolor);
1227 std::string type = luaL_checkstring(L, 3);
1229 std::vector<std::string> params;
1230 if (lua_istable(L, 4)) {
1231 int table = lua_gettop(L);
1233 while (lua_next(L, table) != 0) {
1234 // key at index -2 and value at index -1
1235 if (lua_isstring(L, -1))
1236 params.push_back(lua_tostring(L, -1));
1238 params.push_back("");
1239 // removes value, keeps key for next iteration
1244 if (type == "skybox" && params.size() != 6)
1245 throw LuaError("skybox expects 6 textures");
1247 if (!getServer(L)->setSky(player, bgcolor, type, params))
1250 lua_pushboolean(L, true);
1254 // override_day_night_ratio(self, brightness=0...1)
1255 int ObjectRef::l_override_day_night_ratio(lua_State *L)
1257 ObjectRef *ref = checkobject(L, 1);
1258 Player *player = getplayer(ref);
1262 bool do_override = false;
1264 if (!lua_isnil(L, 2)){
1266 ratio = luaL_checknumber(L, 2);
1269 if (!getServer(L)->overrideDayNightRatio(player, do_override, ratio))
1272 lua_pushboolean(L, true);
1276 // set_nametag_attributes(self, attributes)
1277 int ObjectRef::l_set_nametag_attributes(lua_State *L)
1279 NO_MAP_LOCK_REQUIRED;
1280 ObjectRef *ref = checkobject(L, 1);
1281 PlayerSAO *playersao = getplayersao(ref);
1282 if (playersao == NULL)
1285 lua_getfield(L, 2, "color");
1286 if (!lua_isnil(L, -1)) {
1287 video::SColor color = playersao->getNametagColor();
1288 if (!read_color(L, -1, &color))
1290 playersao->setNametagColor(color);
1293 lua_pushboolean(L, true);
1297 // get_nametag_attributes(self)
1298 int ObjectRef::l_get_nametag_attributes(lua_State *L)
1300 NO_MAP_LOCK_REQUIRED;
1301 ObjectRef *ref = checkobject(L, 1);
1302 PlayerSAO *playersao = getplayersao(ref);
1303 if (playersao == NULL)
1306 video::SColor color = playersao->getNametagColor();
1309 push_ARGB8(L, color);
1310 lua_setfield(L, -2, "color");
1315 ObjectRef::ObjectRef(ServerActiveObject *object):
1318 //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
1321 ObjectRef::~ObjectRef()
1324 infostream<<"ObjectRef destructing for id="
1325 <<m_object->getId()<<std::endl;
1327 infostream<<"ObjectRef destructing for id=unknown"<<std::endl;*/
1330 // Creates an ObjectRef and leaves it on top of stack
1331 // Not callable from Lua; all references are created on the C side.
1332 void ObjectRef::create(lua_State *L, ServerActiveObject *object)
1334 ObjectRef *o = new ObjectRef(object);
1335 //infostream<<"ObjectRef::create: o="<<o<<std::endl;
1336 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1337 luaL_getmetatable(L, className);
1338 lua_setmetatable(L, -2);
1341 void ObjectRef::set_null(lua_State *L)
1343 ObjectRef *o = checkobject(L, -1);
1347 void ObjectRef::Register(lua_State *L)
1350 int methodtable = lua_gettop(L);
1351 luaL_newmetatable(L, className);
1352 int metatable = lua_gettop(L);
1354 lua_pushliteral(L, "__metatable");
1355 lua_pushvalue(L, methodtable);
1356 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
1358 lua_pushliteral(L, "__index");
1359 lua_pushvalue(L, methodtable);
1360 lua_settable(L, metatable);
1362 lua_pushliteral(L, "__gc");
1363 lua_pushcfunction(L, gc_object);
1364 lua_settable(L, metatable);
1366 lua_pop(L, 1); // drop metatable
1368 luaL_openlib(L, 0, methods, 0); // fill methodtable
1369 lua_pop(L, 1); // drop methodtable
1371 // Cannot be created from Lua
1372 //lua_register(L, className, create_object);
1375 const char ObjectRef::className[] = "ObjectRef";
1376 const luaL_reg ObjectRef::methods[] = {
1377 // ServerActiveObject
1378 luamethod(ObjectRef, remove),
1379 luamethod(ObjectRef, getpos),
1380 luamethod(ObjectRef, setpos),
1381 luamethod(ObjectRef, moveto),
1382 luamethod(ObjectRef, punch),
1383 luamethod(ObjectRef, right_click),
1384 luamethod(ObjectRef, set_hp),
1385 luamethod(ObjectRef, get_hp),
1386 luamethod(ObjectRef, get_inventory),
1387 luamethod(ObjectRef, get_wield_list),
1388 luamethod(ObjectRef, get_wield_index),
1389 luamethod(ObjectRef, get_wielded_item),
1390 luamethod(ObjectRef, set_wielded_item),
1391 luamethod(ObjectRef, set_armor_groups),
1392 luamethod(ObjectRef, set_physics_override),
1393 luamethod(ObjectRef, set_animation),
1394 luamethod(ObjectRef, set_bone_position),
1395 luamethod(ObjectRef, set_attach),
1396 luamethod(ObjectRef, set_detach),
1397 luamethod(ObjectRef, set_properties),
1398 // LuaEntitySAO-only
1399 luamethod(ObjectRef, setvelocity),
1400 luamethod(ObjectRef, getvelocity),
1401 luamethod(ObjectRef, setacceleration),
1402 luamethod(ObjectRef, getacceleration),
1403 luamethod(ObjectRef, setyaw),
1404 luamethod(ObjectRef, getyaw),
1405 luamethod(ObjectRef, settexturemod),
1406 luamethod(ObjectRef, setsprite),
1407 luamethod(ObjectRef, get_entity_name),
1408 luamethod(ObjectRef, get_luaentity),
1410 luamethod(ObjectRef, is_player),
1411 luamethod(ObjectRef, is_player_connected),
1412 luamethod(ObjectRef, get_player_name),
1413 luamethod(ObjectRef, get_look_dir),
1414 luamethod(ObjectRef, get_look_pitch),
1415 luamethod(ObjectRef, get_look_yaw),
1416 luamethod(ObjectRef, set_look_yaw),
1417 luamethod(ObjectRef, set_look_pitch),
1418 luamethod(ObjectRef, get_breath),
1419 luamethod(ObjectRef, set_breath),
1420 luamethod(ObjectRef, set_inventory_formspec),
1421 luamethod(ObjectRef, get_inventory_formspec),
1422 luamethod(ObjectRef, get_player_control),
1423 luamethod(ObjectRef, get_player_control_bits),
1424 luamethod(ObjectRef, hud_add),
1425 luamethod(ObjectRef, hud_remove),
1426 luamethod(ObjectRef, hud_change),
1427 luamethod(ObjectRef, hud_get),
1428 luamethod(ObjectRef, hud_set_flags),
1429 luamethod(ObjectRef, hud_get_flags),
1430 luamethod(ObjectRef, hud_set_hotbar_itemcount),
1431 luamethod(ObjectRef, hud_set_hotbar_image),
1432 luamethod(ObjectRef, hud_set_hotbar_selected_image),
1433 luamethod(ObjectRef, set_sky),
1434 luamethod(ObjectRef, override_day_night_ratio),
1435 luamethod(ObjectRef, set_local_animation),
1436 luamethod(ObjectRef, set_eye_offset),
1437 luamethod(ObjectRef, set_nametag_attributes),
1438 luamethod(ObjectRef, get_nametag_attributes),