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_object.h"
30 #include "content_sao.h"
35 struct EnumString es_HudElementType[] =
37 {HUD_ELEM_IMAGE, "image"},
38 {HUD_ELEM_TEXT, "text"},
39 {HUD_ELEM_STATBAR, "statbar"},
40 {HUD_ELEM_INVENTORY, "inventory"},
41 {HUD_ELEM_WAYPOINT, "waypoint"},
45 struct EnumString es_HudElementStat[] =
47 {HUD_STAT_POS, "position"},
48 {HUD_STAT_POS, "pos"}, /* Deprecated, only for compatibility's sake */
49 {HUD_STAT_NAME, "name"},
50 {HUD_STAT_SCALE, "scale"},
51 {HUD_STAT_TEXT, "text"},
52 {HUD_STAT_NUMBER, "number"},
53 {HUD_STAT_ITEM, "item"},
54 {HUD_STAT_DIR, "direction"},
55 {HUD_STAT_ALIGN, "alignment"},
56 {HUD_STAT_OFFSET, "offset"},
57 {HUD_STAT_WORLD_POS, "world_pos"},
61 struct EnumString es_HudBuiltinElement[] =
63 {HUD_FLAG_HOTBAR_VISIBLE, "hotbar"},
64 {HUD_FLAG_HEALTHBAR_VISIBLE, "healthbar"},
65 {HUD_FLAG_CROSSHAIR_VISIBLE, "crosshair"},
66 {HUD_FLAG_WIELDITEM_VISIBLE, "wielditem"},
67 {HUD_FLAG_BREATHBAR_VISIBLE, "breathbar"},
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 Player* 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)
131 NO_MAP_LOCK_REQUIRED;
132 ObjectRef *ref = checkobject(L, 1);
133 ServerActiveObject *co = getobject(ref);
134 if(co == NULL) 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 co->punch(dir, &toolcap, puncher, time_from_last_punch);
216 // right_click(self, clicker); clicker = an another ObjectRef
217 int ObjectRef::l_right_click(lua_State *L)
219 NO_MAP_LOCK_REQUIRED;
220 ObjectRef *ref = checkobject(L, 1);
221 ObjectRef *ref2 = checkobject(L, 2);
222 ServerActiveObject *co = getobject(ref);
223 ServerActiveObject *co2 = getobject(ref2);
224 if(co == NULL) return 0;
225 if(co2 == NULL) return 0;
232 // hp = number of hitpoints (2 * number of hearts)
234 int ObjectRef::l_set_hp(lua_State *L)
236 NO_MAP_LOCK_REQUIRED;
237 ObjectRef *ref = checkobject(L, 1);
238 luaL_checknumber(L, 2);
239 ServerActiveObject *co = getobject(ref);
240 if(co == NULL) return 0;
241 int hp = lua_tonumber(L, 2);
242 /*infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
243 <<" hp="<<hp<<std::endl;*/
251 // returns: number of hitpoints (2 * number of hearts)
252 // 0 if not applicable to this type of object
253 int ObjectRef::l_get_hp(lua_State *L)
255 NO_MAP_LOCK_REQUIRED;
256 ObjectRef *ref = checkobject(L, 1);
257 ServerActiveObject *co = getobject(ref);
260 lua_pushnumber(L, 1);
263 int hp = co->getHP();
264 /*infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
265 <<" hp="<<hp<<std::endl;*/
267 lua_pushnumber(L, hp);
271 // get_inventory(self)
272 int ObjectRef::l_get_inventory(lua_State *L)
274 NO_MAP_LOCK_REQUIRED;
275 ObjectRef *ref = checkobject(L, 1);
276 ServerActiveObject *co = getobject(ref);
277 if(co == NULL) return 0;
279 InventoryLocation loc = co->getInventoryLocation();
280 if(getServer(L)->getInventory(loc) != NULL)
281 InvRef::create(L, loc);
283 lua_pushnil(L); // An object may have no inventory (nil)
287 // get_wield_list(self)
288 int ObjectRef::l_get_wield_list(lua_State *L)
290 NO_MAP_LOCK_REQUIRED;
291 ObjectRef *ref = checkobject(L, 1);
292 ServerActiveObject *co = getobject(ref);
293 if(co == NULL) return 0;
295 lua_pushstring(L, co->getWieldList().c_str());
299 // get_wield_index(self)
300 int ObjectRef::l_get_wield_index(lua_State *L)
302 NO_MAP_LOCK_REQUIRED;
303 ObjectRef *ref = checkobject(L, 1);
304 ServerActiveObject *co = getobject(ref);
305 if(co == NULL) return 0;
307 lua_pushinteger(L, co->getWieldIndex() + 1);
311 // get_wielded_item(self)
312 int ObjectRef::l_get_wielded_item(lua_State *L)
314 NO_MAP_LOCK_REQUIRED;
315 ObjectRef *ref = checkobject(L, 1);
316 ServerActiveObject *co = getobject(ref);
319 LuaItemStack::create(L, ItemStack());
323 LuaItemStack::create(L, co->getWieldedItem());
327 // set_wielded_item(self, itemstack or itemstring or table or nil)
328 int ObjectRef::l_set_wielded_item(lua_State *L)
330 NO_MAP_LOCK_REQUIRED;
331 ObjectRef *ref = checkobject(L, 1);
332 ServerActiveObject *co = getobject(ref);
333 if(co == NULL) return 0;
335 ItemStack item = read_item(L, 2, getServer(L));
336 bool success = co->setWieldedItem(item);
337 lua_pushboolean(L, success);
341 // set_armor_groups(self, groups)
342 int ObjectRef::l_set_armor_groups(lua_State *L)
344 NO_MAP_LOCK_REQUIRED;
345 ObjectRef *ref = checkobject(L, 1);
346 ServerActiveObject *co = getobject(ref);
347 if(co == NULL) return 0;
349 ItemGroupList groups;
350 read_groups(L, 2, groups);
351 co->setArmorGroups(groups);
355 // set_physics_override(self, physics_override_speed, physics_override_jump,
356 // physics_override_gravity, sneak, sneak_glitch)
357 int ObjectRef::l_set_physics_override(lua_State *L)
359 ObjectRef *ref = checkobject(L, 1);
360 PlayerSAO *co = (PlayerSAO *) getobject(ref);
361 if(co == NULL) return 0;
363 if (lua_istable(L, 2)) {
364 co->m_physics_override_speed = getfloatfield_default(L, 2, "speed", co->m_physics_override_speed);
365 co->m_physics_override_jump = getfloatfield_default(L, 2, "jump", co->m_physics_override_jump);
366 co->m_physics_override_gravity = getfloatfield_default(L, 2, "gravity", co->m_physics_override_gravity);
367 co->m_physics_override_sneak = getboolfield_default(L, 2, "sneak", co->m_physics_override_sneak);
368 co->m_physics_override_sneak_glitch = getboolfield_default(L, 2, "sneak_glitch", co->m_physics_override_sneak_glitch);
369 co->m_physics_override_sent = false;
371 // old, non-table format
372 if(!lua_isnil(L, 2)){
373 co->m_physics_override_speed = lua_tonumber(L, 2);
374 co->m_physics_override_sent = false;
376 if(!lua_isnil(L, 3)){
377 co->m_physics_override_jump = lua_tonumber(L, 3);
378 co->m_physics_override_sent = false;
380 if(!lua_isnil(L, 4)){
381 co->m_physics_override_gravity = lua_tonumber(L, 4);
382 co->m_physics_override_sent = false;
388 // set_animation(self, frame_range, frame_speed, frame_blend)
389 int ObjectRef::l_set_animation(lua_State *L)
391 NO_MAP_LOCK_REQUIRED;
392 ObjectRef *ref = checkobject(L, 1);
393 ServerActiveObject *co = getobject(ref);
394 if(co == NULL) return 0;
396 v2f frames = v2f(1, 1);
398 frames = read_v2f(L, 2);
399 float frame_speed = 15;
401 frame_speed = lua_tonumber(L, 3);
402 float frame_blend = 0;
404 frame_blend = lua_tonumber(L, 4);
405 co->setAnimation(frames, frame_speed, frame_blend);
409 // set_local_animation(self, {stand/idle}, {walk}, {dig}, {walk+dig}, frame_speed)
410 int ObjectRef::l_set_local_animation(lua_State *L)
412 //NO_MAP_LOCK_REQUIRED;
413 ObjectRef *ref = checkobject(L, 1);
414 Player *player = getplayer(ref);
419 for (int i=0;i<4;i++) {
420 if(!lua_isnil(L, 2+1))
421 frames[i] = read_v2s32(L, 2+i);
423 float frame_speed = 30;
425 frame_speed = lua_tonumber(L, 6);
427 if (!getServer(L)->setLocalPlayerAnimations(player, frames, frame_speed))
430 lua_pushboolean(L, true);
434 // set_eye_offset(self, v3f first pv, v3f third pv)
435 int ObjectRef::l_set_eye_offset(lua_State *L)
437 //NO_MAP_LOCK_REQUIRED;
438 ObjectRef *ref = checkobject(L, 1);
439 Player *player = getplayer(ref);
443 v3f offset_first = v3f(0, 0, 0);
444 v3f offset_third = v3f(0, 0, 0);
447 offset_first = read_v3f(L, 2);
449 offset_third = read_v3f(L, 3);
451 // Prevent abuse of offset values (keep player always visible)
452 offset_third.X = rangelim(offset_third.X,-10,10);
453 offset_third.Z = rangelim(offset_third.Z,-5,5);
454 /* TODO: if possible: improve the camera colision detetion to allow Y <= -1.5) */
455 offset_third.Y = rangelim(offset_third.Y,-10,15); //1.5*BS
457 if (!getServer(L)->setPlayerEyeOffset(player, offset_first, offset_third))
460 lua_pushboolean(L, true);
464 // set_bone_position(self, std::string bone, v3f position, v3f rotation)
465 int ObjectRef::l_set_bone_position(lua_State *L)
467 NO_MAP_LOCK_REQUIRED;
468 ObjectRef *ref = checkobject(L, 1);
469 ServerActiveObject *co = getobject(ref);
470 if(co == NULL) return 0;
472 std::string bone = "";
474 bone = lua_tostring(L, 2);
475 v3f position = v3f(0, 0, 0);
477 position = read_v3f(L, 3);
478 v3f rotation = v3f(0, 0, 0);
480 rotation = read_v3f(L, 4);
481 co->setBonePosition(bone, position, rotation);
485 // set_attach(self, parent, bone, position, rotation)
486 int ObjectRef::l_set_attach(lua_State *L)
488 NO_MAP_LOCK_REQUIRED;
489 ObjectRef *ref = checkobject(L, 1);
490 ObjectRef *parent_ref = checkobject(L, 2);
491 ServerActiveObject *co = getobject(ref);
492 ServerActiveObject *parent = getobject(parent_ref);
493 if(co == NULL) return 0;
494 if(parent == NULL) return 0;
496 std::string bone = "";
498 bone = lua_tostring(L, 3);
499 v3f position = v3f(0, 0, 0);
501 position = read_v3f(L, 4);
502 v3f rotation = v3f(0, 0, 0);
504 rotation = read_v3f(L, 5);
505 co->setAttachment(parent->getId(), bone, position, rotation);
510 int ObjectRef::l_set_detach(lua_State *L)
512 NO_MAP_LOCK_REQUIRED;
513 ObjectRef *ref = checkobject(L, 1);
514 ServerActiveObject *co = getobject(ref);
515 if(co == NULL) return 0;
517 co->setAttachment(0, "", v3f(0,0,0), v3f(0,0,0));
521 // set_properties(self, properties)
522 int ObjectRef::l_set_properties(lua_State *L)
524 NO_MAP_LOCK_REQUIRED;
525 ObjectRef *ref = checkobject(L, 1);
526 ServerActiveObject *co = getobject(ref);
527 if(co == NULL) return 0;
528 ObjectProperties *prop = co->accessObjectProperties();
531 read_object_properties(L, 2, prop);
532 co->notifyObjectPropertiesModified();
536 /* LuaEntitySAO-only */
538 // setvelocity(self, {x=num, y=num, z=num})
539 int ObjectRef::l_setvelocity(lua_State *L)
541 NO_MAP_LOCK_REQUIRED;
542 ObjectRef *ref = checkobject(L, 1);
543 LuaEntitySAO *co = getluaobject(ref);
544 if(co == NULL) return 0;
545 v3f pos = checkFloatPos(L, 2);
547 co->setVelocity(pos);
552 int ObjectRef::l_getvelocity(lua_State *L)
554 NO_MAP_LOCK_REQUIRED;
555 ObjectRef *ref = checkobject(L, 1);
556 LuaEntitySAO *co = getluaobject(ref);
557 if(co == NULL) return 0;
559 v3f v = co->getVelocity();
564 // setacceleration(self, {x=num, y=num, z=num})
565 int ObjectRef::l_setacceleration(lua_State *L)
567 NO_MAP_LOCK_REQUIRED;
568 ObjectRef *ref = checkobject(L, 1);
569 LuaEntitySAO *co = getluaobject(ref);
570 if(co == NULL) return 0;
572 v3f pos = checkFloatPos(L, 2);
574 co->setAcceleration(pos);
578 // getacceleration(self)
579 int ObjectRef::l_getacceleration(lua_State *L)
581 NO_MAP_LOCK_REQUIRED;
582 ObjectRef *ref = checkobject(L, 1);
583 LuaEntitySAO *co = getluaobject(ref);
584 if(co == NULL) return 0;
586 v3f v = co->getAcceleration();
591 // setyaw(self, radians)
592 int ObjectRef::l_setyaw(lua_State *L)
594 NO_MAP_LOCK_REQUIRED;
595 ObjectRef *ref = checkobject(L, 1);
596 LuaEntitySAO *co = getluaobject(ref);
597 if(co == NULL) return 0;
598 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
605 int ObjectRef::l_getyaw(lua_State *L)
607 NO_MAP_LOCK_REQUIRED;
608 ObjectRef *ref = checkobject(L, 1);
609 LuaEntitySAO *co = getluaobject(ref);
610 if(co == NULL) return 0;
612 float yaw = co->getYaw() * core::DEGTORAD;
613 lua_pushnumber(L, yaw);
617 // settexturemod(self, mod)
618 int ObjectRef::l_settexturemod(lua_State *L)
620 NO_MAP_LOCK_REQUIRED;
621 ObjectRef *ref = checkobject(L, 1);
622 LuaEntitySAO *co = getluaobject(ref);
623 if(co == NULL) return 0;
625 std::string mod = luaL_checkstring(L, 2);
626 co->setTextureMod(mod);
630 // setsprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
631 // select_horiz_by_yawpitch=false)
632 int ObjectRef::l_setsprite(lua_State *L)
634 NO_MAP_LOCK_REQUIRED;
635 ObjectRef *ref = checkobject(L, 1);
636 LuaEntitySAO *co = getluaobject(ref);
637 if(co == NULL) return 0;
641 p = read_v2s16(L, 2);
644 num_frames = lua_tonumber(L, 3);
645 float framelength = 0.2;
647 framelength = lua_tonumber(L, 4);
648 bool select_horiz_by_yawpitch = false;
650 select_horiz_by_yawpitch = lua_toboolean(L, 5);
651 co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
656 // get_entity_name(self)
657 int ObjectRef::l_get_entity_name(lua_State *L)
659 NO_MAP_LOCK_REQUIRED;
660 ObjectRef *ref = checkobject(L, 1);
661 LuaEntitySAO *co = getluaobject(ref);
662 log_deprecated(L,"Deprecated call to \"get_entity_name");
663 if(co == NULL) return 0;
665 std::string name = co->getName();
666 lua_pushstring(L, name.c_str());
670 // get_luaentity(self)
671 int ObjectRef::l_get_luaentity(lua_State *L)
673 NO_MAP_LOCK_REQUIRED;
674 ObjectRef *ref = checkobject(L, 1);
675 LuaEntitySAO *co = getluaobject(ref);
676 if(co == NULL) return 0;
678 luaentity_get(L, co->getId());
685 int ObjectRef::l_is_player(lua_State *L)
687 NO_MAP_LOCK_REQUIRED;
688 ObjectRef *ref = checkobject(L, 1);
689 Player *player = getplayer(ref);
690 lua_pushboolean(L, (player != NULL));
694 // is_player_connected(self)
695 int ObjectRef::l_is_player_connected(lua_State *L)
697 NO_MAP_LOCK_REQUIRED;
698 ObjectRef *ref = checkobject(L, 1);
699 Player *player = getplayer(ref);
700 lua_pushboolean(L, (player != NULL && player->peer_id != 0));
704 // get_player_name(self)
705 int ObjectRef::l_get_player_name(lua_State *L)
707 NO_MAP_LOCK_REQUIRED;
708 ObjectRef *ref = checkobject(L, 1);
709 Player *player = getplayer(ref);
711 lua_pushlstring(L, "", 0);
715 lua_pushstring(L, player->getName());
719 // get_look_dir(self)
720 int ObjectRef::l_get_look_dir(lua_State *L)
722 NO_MAP_LOCK_REQUIRED;
723 ObjectRef *ref = checkobject(L, 1);
724 Player *player = getplayer(ref);
725 if(player == NULL) return 0;
727 float pitch = player->getRadPitch();
728 float yaw = player->getRadYaw();
729 v3f v(cos(pitch)*cos(yaw), sin(pitch), cos(pitch)*sin(yaw));
734 // get_look_pitch(self)
735 int ObjectRef::l_get_look_pitch(lua_State *L)
737 NO_MAP_LOCK_REQUIRED;
738 ObjectRef *ref = checkobject(L, 1);
739 Player *player = getplayer(ref);
740 if(player == NULL) return 0;
742 lua_pushnumber(L, player->getRadPitch());
746 // get_look_yaw(self)
747 int ObjectRef::l_get_look_yaw(lua_State *L)
749 NO_MAP_LOCK_REQUIRED;
750 ObjectRef *ref = checkobject(L, 1);
751 Player *player = getplayer(ref);
752 if(player == NULL) return 0;
754 lua_pushnumber(L, player->getRadYaw());
758 // set_look_pitch(self, radians)
759 int ObjectRef::l_set_look_pitch(lua_State *L)
761 NO_MAP_LOCK_REQUIRED;
762 ObjectRef *ref = checkobject(L, 1);
763 PlayerSAO* co = getplayersao(ref);
764 if(co == NULL) return 0;
765 float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
771 // set_look_yaw(self, radians)
772 int ObjectRef::l_set_look_yaw(lua_State *L)
774 NO_MAP_LOCK_REQUIRED;
775 ObjectRef *ref = checkobject(L, 1);
776 PlayerSAO* co = getplayersao(ref);
777 if(co == NULL) return 0;
778 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
784 // set_breath(self, breath)
785 int ObjectRef::l_set_breath(lua_State *L)
787 NO_MAP_LOCK_REQUIRED;
788 ObjectRef *ref = checkobject(L, 1);
789 PlayerSAO* co = getplayersao(ref);
790 if(co == NULL) return 0;
791 u16 breath = luaL_checknumber(L, 2);
793 co->setBreath(breath);
794 co->m_breath_not_sent = true;
799 int ObjectRef::l_get_breath(lua_State *L)
801 NO_MAP_LOCK_REQUIRED;
802 ObjectRef *ref = checkobject(L, 1);
803 PlayerSAO* co = getplayersao(ref);
804 if(co == NULL) return 0;
806 u16 breath = co->getBreath();
807 lua_pushinteger (L, breath);
811 // set_inventory_formspec(self, formspec)
812 int ObjectRef::l_set_inventory_formspec(lua_State *L)
814 NO_MAP_LOCK_REQUIRED;
815 ObjectRef *ref = checkobject(L, 1);
816 Player *player = getplayer(ref);
817 if(player == NULL) return 0;
818 std::string formspec = luaL_checkstring(L, 2);
820 player->inventory_formspec = formspec;
821 getServer(L)->reportInventoryFormspecModified(player->getName());
822 lua_pushboolean(L, true);
826 // get_inventory_formspec(self) -> formspec
827 int ObjectRef::l_get_inventory_formspec(lua_State *L)
829 NO_MAP_LOCK_REQUIRED;
830 ObjectRef *ref = checkobject(L, 1);
831 Player *player = getplayer(ref);
832 if(player == NULL) return 0;
834 std::string formspec = player->inventory_formspec;
835 lua_pushlstring(L, formspec.c_str(), formspec.size());
839 // get_player_control(self)
840 int ObjectRef::l_get_player_control(lua_State *L)
842 NO_MAP_LOCK_REQUIRED;
843 ObjectRef *ref = checkobject(L, 1);
844 Player *player = getplayer(ref);
846 lua_pushlstring(L, "", 0);
850 PlayerControl control = player->getPlayerControl();
852 lua_pushboolean(L, control.up);
853 lua_setfield(L, -2, "up");
854 lua_pushboolean(L, control.down);
855 lua_setfield(L, -2, "down");
856 lua_pushboolean(L, control.left);
857 lua_setfield(L, -2, "left");
858 lua_pushboolean(L, control.right);
859 lua_setfield(L, -2, "right");
860 lua_pushboolean(L, control.jump);
861 lua_setfield(L, -2, "jump");
862 lua_pushboolean(L, control.aux1);
863 lua_setfield(L, -2, "aux1");
864 lua_pushboolean(L, control.sneak);
865 lua_setfield(L, -2, "sneak");
866 lua_pushboolean(L, control.LMB);
867 lua_setfield(L, -2, "LMB");
868 lua_pushboolean(L, control.RMB);
869 lua_setfield(L, -2, "RMB");
873 // get_player_control_bits(self)
874 int ObjectRef::l_get_player_control_bits(lua_State *L)
876 NO_MAP_LOCK_REQUIRED;
877 ObjectRef *ref = checkobject(L, 1);
878 Player *player = getplayer(ref);
880 lua_pushlstring(L, "", 0);
884 lua_pushnumber(L, player->keyPressed);
888 // hud_add(self, form)
889 int ObjectRef::l_hud_add(lua_State *L)
891 ObjectRef *ref = checkobject(L, 1);
892 Player *player = getplayer(ref);
896 HudElement *elem = new HudElement;
898 elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type",
899 es_HudElementType, HUD_ELEM_TEXT);
901 lua_getfield(L, 2, "position");
902 elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
905 lua_getfield(L, 2, "scale");
906 elem->scale = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
909 lua_getfield(L, 2, "size");
910 elem->size = lua_istable(L, -1) ? read_v2s32(L, -1) : v2s32();
913 elem->name = getstringfield_default(L, 2, "name", "");
914 elem->text = getstringfield_default(L, 2, "text", "");
915 elem->number = getintfield_default(L, 2, "number", 0);
916 elem->item = getintfield_default(L, 2, "item", 0);
917 elem->dir = getintfield_default(L, 2, "dir", 0);
919 lua_getfield(L, 2, "alignment");
920 elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
923 lua_getfield(L, 2, "offset");
924 elem->offset = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
927 lua_getfield(L, 2, "world_pos");
928 elem->world_pos = lua_istable(L, -1) ? read_v3f(L, -1) : v3f();
931 /* check for known deprecated element usage */
932 if ((elem->type == HUD_ELEM_STATBAR) && (elem->size == v2s32())) {
933 log_deprecated(L,"Deprecated usage of statbar without size!");
936 u32 id = getServer(L)->hudAdd(player, elem);
942 lua_pushnumber(L, id);
946 // hud_remove(self, id)
947 int ObjectRef::l_hud_remove(lua_State *L)
949 ObjectRef *ref = checkobject(L, 1);
950 Player *player = getplayer(ref);
955 if (!lua_isnil(L, 2))
956 id = lua_tonumber(L, 2);
958 if (!getServer(L)->hudRemove(player, id))
961 lua_pushboolean(L, true);
965 // hud_change(self, id, stat, data)
966 int ObjectRef::l_hud_change(lua_State *L)
968 ObjectRef *ref = checkobject(L, 1);
969 Player *player = getplayer(ref);
973 u32 id = lua_isnumber(L, 2) ? lua_tonumber(L, 2) : -1;
975 HudElement *e = player->getHud(id);
979 HudElementStat stat = HUD_STAT_NUMBER;
980 if (lua_isstring(L, 3)) {
982 std::string statstr = lua_tostring(L, 3);
983 stat = string_to_enum(es_HudElementStat, statint, statstr) ?
984 (HudElementStat)statint : HUD_STAT_NUMBER;
990 e->pos = read_v2f(L, 4);
994 e->name = luaL_checkstring(L, 4);
998 e->scale = read_v2f(L, 4);
1002 e->text = luaL_checkstring(L, 4);
1005 case HUD_STAT_NUMBER:
1006 e->number = luaL_checknumber(L, 4);
1010 e->item = luaL_checknumber(L, 4);
1014 e->dir = luaL_checknumber(L, 4);
1017 case HUD_STAT_ALIGN:
1018 e->align = read_v2f(L, 4);
1021 case HUD_STAT_OFFSET:
1022 e->offset = read_v2f(L, 4);
1025 case HUD_STAT_WORLD_POS:
1026 e->world_pos = read_v3f(L, 4);
1027 value = &e->world_pos;
1030 e->size = read_v2s32(L, 4);
1035 getServer(L)->hudChange(player, id, stat, value);
1037 lua_pushboolean(L, true);
1041 // hud_get(self, id)
1042 int ObjectRef::l_hud_get(lua_State *L)
1044 ObjectRef *ref = checkobject(L, 1);
1045 Player *player = getplayer(ref);
1049 u32 id = lua_tonumber(L, -1);
1051 HudElement *e = player->getHud(id);
1057 lua_pushstring(L, es_HudElementType[(u8)e->type].str);
1058 lua_setfield(L, -2, "type");
1060 push_v2f(L, e->pos);
1061 lua_setfield(L, -2, "position");
1063 lua_pushstring(L, e->name.c_str());
1064 lua_setfield(L, -2, "name");
1066 push_v2f(L, e->scale);
1067 lua_setfield(L, -2, "scale");
1069 lua_pushstring(L, e->text.c_str());
1070 lua_setfield(L, -2, "text");
1072 lua_pushnumber(L, e->number);
1073 lua_setfield(L, -2, "number");
1075 lua_pushnumber(L, e->item);
1076 lua_setfield(L, -2, "item");
1078 lua_pushnumber(L, e->dir);
1079 lua_setfield(L, -2, "dir");
1081 push_v3f(L, e->world_pos);
1082 lua_setfield(L, -2, "world_pos");
1087 // hud_set_flags(self, flags)
1088 int ObjectRef::l_hud_set_flags(lua_State *L)
1090 ObjectRef *ref = checkobject(L, 1);
1091 Player *player = getplayer(ref);
1099 const EnumString *esp = es_HudBuiltinElement;
1100 for (int i = 0; esp[i].str; i++) {
1101 if (getboolfield(L, 2, esp[i].str, flag)) {
1102 flags |= esp[i].num * flag;
1106 if (!getServer(L)->hudSetFlags(player, flags, mask))
1109 lua_pushboolean(L, true);
1113 int ObjectRef::l_hud_get_flags(lua_State *L)
1115 ObjectRef *ref = checkobject(L, 1);
1116 Player *player = getplayer(ref);
1121 lua_pushboolean(L, player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE);
1122 lua_setfield(L, -2, "hotbar");
1123 lua_pushboolean(L, player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE);
1124 lua_setfield(L, -2, "healthbar");
1125 lua_pushboolean(L, player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE);
1126 lua_setfield(L, -2, "crosshair");
1127 lua_pushboolean(L, player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE);
1128 lua_setfield(L, -2, "wielditem");
1129 lua_pushboolean(L, player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE);
1130 lua_setfield(L, -2, "breathbar");
1135 // hud_set_hotbar_itemcount(self, hotbar_itemcount)
1136 int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L)
1138 ObjectRef *ref = checkobject(L, 1);
1139 Player *player = getplayer(ref);
1143 s32 hotbar_itemcount = lua_tonumber(L, 2);
1145 if (!getServer(L)->hudSetHotbarItemcount(player, hotbar_itemcount))
1148 lua_pushboolean(L, true);
1152 // hud_set_hotbar_image(self, name)
1153 int ObjectRef::l_hud_set_hotbar_image(lua_State *L)
1155 ObjectRef *ref = checkobject(L, 1);
1156 Player *player = getplayer(ref);
1160 std::string name = lua_tostring(L, 2);
1162 getServer(L)->hudSetHotbarImage(player, name);
1166 // hud_set_hotbar_selected_image(self, name)
1167 int ObjectRef::l_hud_set_hotbar_selected_image(lua_State *L)
1169 ObjectRef *ref = checkobject(L, 1);
1170 Player *player = getplayer(ref);
1174 std::string name = lua_tostring(L, 2);
1176 getServer(L)->hudSetHotbarSelectedImage(player, name);
1180 // set_sky(self, bgcolor, type, list)
1181 int ObjectRef::l_set_sky(lua_State *L)
1183 ObjectRef *ref = checkobject(L, 1);
1184 Player *player = getplayer(ref);
1188 video::SColor bgcolor(255,255,255,255);
1189 if (!lua_isnil(L, 2))
1190 bgcolor = readARGB8(L, 2);
1192 std::string type = luaL_checkstring(L, 3);
1194 std::vector<std::string> params;
1195 if (lua_istable(L, 4)) {
1196 int table = lua_gettop(L);
1198 while (lua_next(L, table) != 0) {
1199 // key at index -2 and value at index -1
1200 if (lua_isstring(L, -1))
1201 params.push_back(lua_tostring(L, -1));
1203 params.push_back("");
1204 // removes value, keeps key for next iteration
1209 if (type == "skybox" && params.size() != 6)
1210 throw LuaError("skybox expects 6 textures");
1212 if (!getServer(L)->setSky(player, bgcolor, type, params))
1215 lua_pushboolean(L, true);
1219 // override_day_night_ratio(self, brightness=0...1)
1220 int ObjectRef::l_override_day_night_ratio(lua_State *L)
1222 ObjectRef *ref = checkobject(L, 1);
1223 Player *player = getplayer(ref);
1227 bool do_override = false;
1229 if (!lua_isnil(L, 2)){
1231 ratio = luaL_checknumber(L, 2);
1234 if (!getServer(L)->overrideDayNightRatio(player, do_override, ratio))
1237 lua_pushboolean(L, true);
1241 ObjectRef::ObjectRef(ServerActiveObject *object):
1244 //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
1247 ObjectRef::~ObjectRef()
1250 infostream<<"ObjectRef destructing for id="
1251 <<m_object->getId()<<std::endl;
1253 infostream<<"ObjectRef destructing for id=unknown"<<std::endl;*/
1256 // Creates an ObjectRef and leaves it on top of stack
1257 // Not callable from Lua; all references are created on the C side.
1258 void ObjectRef::create(lua_State *L, ServerActiveObject *object)
1260 ObjectRef *o = new ObjectRef(object);
1261 //infostream<<"ObjectRef::create: o="<<o<<std::endl;
1262 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1263 luaL_getmetatable(L, className);
1264 lua_setmetatable(L, -2);
1267 void ObjectRef::set_null(lua_State *L)
1269 ObjectRef *o = checkobject(L, -1);
1273 void ObjectRef::Register(lua_State *L)
1276 int methodtable = lua_gettop(L);
1277 luaL_newmetatable(L, className);
1278 int metatable = lua_gettop(L);
1280 lua_pushliteral(L, "__metatable");
1281 lua_pushvalue(L, methodtable);
1282 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
1284 lua_pushliteral(L, "__index");
1285 lua_pushvalue(L, methodtable);
1286 lua_settable(L, metatable);
1288 lua_pushliteral(L, "__gc");
1289 lua_pushcfunction(L, gc_object);
1290 lua_settable(L, metatable);
1292 lua_pop(L, 1); // drop metatable
1294 luaL_openlib(L, 0, methods, 0); // fill methodtable
1295 lua_pop(L, 1); // drop methodtable
1297 // Cannot be created from Lua
1298 //lua_register(L, className, create_object);
1301 const char ObjectRef::className[] = "ObjectRef";
1302 const luaL_reg ObjectRef::methods[] = {
1303 // ServerActiveObject
1304 luamethod(ObjectRef, remove),
1305 luamethod(ObjectRef, getpos),
1306 luamethod(ObjectRef, setpos),
1307 luamethod(ObjectRef, moveto),
1308 luamethod(ObjectRef, punch),
1309 luamethod(ObjectRef, right_click),
1310 luamethod(ObjectRef, set_hp),
1311 luamethod(ObjectRef, get_hp),
1312 luamethod(ObjectRef, get_inventory),
1313 luamethod(ObjectRef, get_wield_list),
1314 luamethod(ObjectRef, get_wield_index),
1315 luamethod(ObjectRef, get_wielded_item),
1316 luamethod(ObjectRef, set_wielded_item),
1317 luamethod(ObjectRef, set_armor_groups),
1318 luamethod(ObjectRef, set_physics_override),
1319 luamethod(ObjectRef, set_animation),
1320 luamethod(ObjectRef, set_bone_position),
1321 luamethod(ObjectRef, set_attach),
1322 luamethod(ObjectRef, set_detach),
1323 luamethod(ObjectRef, set_properties),
1324 // LuaEntitySAO-only
1325 luamethod(ObjectRef, setvelocity),
1326 luamethod(ObjectRef, getvelocity),
1327 luamethod(ObjectRef, setacceleration),
1328 luamethod(ObjectRef, getacceleration),
1329 luamethod(ObjectRef, setyaw),
1330 luamethod(ObjectRef, getyaw),
1331 luamethod(ObjectRef, settexturemod),
1332 luamethod(ObjectRef, setsprite),
1333 luamethod(ObjectRef, get_entity_name),
1334 luamethod(ObjectRef, get_luaentity),
1336 luamethod(ObjectRef, is_player),
1337 luamethod(ObjectRef, is_player_connected),
1338 luamethod(ObjectRef, get_player_name),
1339 luamethod(ObjectRef, get_look_dir),
1340 luamethod(ObjectRef, get_look_pitch),
1341 luamethod(ObjectRef, get_look_yaw),
1342 luamethod(ObjectRef, set_look_yaw),
1343 luamethod(ObjectRef, set_look_pitch),
1344 luamethod(ObjectRef, get_breath),
1345 luamethod(ObjectRef, set_breath),
1346 luamethod(ObjectRef, set_inventory_formspec),
1347 luamethod(ObjectRef, get_inventory_formspec),
1348 luamethod(ObjectRef, get_player_control),
1349 luamethod(ObjectRef, get_player_control_bits),
1350 luamethod(ObjectRef, hud_add),
1351 luamethod(ObjectRef, hud_remove),
1352 luamethod(ObjectRef, hud_change),
1353 luamethod(ObjectRef, hud_get),
1354 luamethod(ObjectRef, hud_set_flags),
1355 luamethod(ObjectRef, hud_get_flags),
1356 luamethod(ObjectRef, hud_set_hotbar_itemcount),
1357 luamethod(ObjectRef, hud_set_hotbar_image),
1358 luamethod(ObjectRef, hud_set_hotbar_selected_image),
1359 luamethod(ObjectRef, set_sky),
1360 luamethod(ObjectRef, override_day_night_ratio),
1361 luamethod(ObjectRef, set_local_animation),
1362 luamethod(ObjectRef, set_eye_offset),