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"},
44 struct EnumString es_HudElementStat[] =
46 {HUD_STAT_POS, "pos"},
47 {HUD_STAT_NAME, "name"},
48 {HUD_STAT_SCALE, "scale"},
49 {HUD_STAT_TEXT, "text"},
50 {HUD_STAT_NUMBER, "number"},
51 {HUD_STAT_ITEM, "item"},
52 {HUD_STAT_DIR, "direction"},
53 {HUD_STAT_ALIGN, "alignment"},
54 {HUD_STAT_OFFSET, "offset"},
58 struct EnumString es_HudBuiltinElement[] =
60 {HUD_FLAG_HOTBAR_VISIBLE, "hotbar"},
61 {HUD_FLAG_HEALTHBAR_VISIBLE, "healthbar"},
62 {HUD_FLAG_CROSSHAIR_VISIBLE, "crosshair"},
63 {HUD_FLAG_WIELDITEM_VISIBLE, "wielditem"},
64 {HUD_FLAG_BREATHBAR_VISIBLE, "breathbar"},
73 ObjectRef* ObjectRef::checkobject(lua_State *L, int narg)
75 luaL_checktype(L, narg, LUA_TUSERDATA);
76 void *ud = luaL_checkudata(L, narg, className);
77 if(!ud) luaL_typerror(L, narg, className);
78 return *(ObjectRef**)ud; // unbox pointer
81 ServerActiveObject* ObjectRef::getobject(ObjectRef *ref)
83 ServerActiveObject *co = ref->m_object;
87 LuaEntitySAO* ObjectRef::getluaobject(ObjectRef *ref)
89 ServerActiveObject *obj = getobject(ref);
92 if(obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY)
94 return (LuaEntitySAO*)obj;
97 PlayerSAO* ObjectRef::getplayersao(ObjectRef *ref)
99 ServerActiveObject *obj = getobject(ref);
102 if(obj->getType() != ACTIVEOBJECT_TYPE_PLAYER)
104 return (PlayerSAO*)obj;
107 Player* ObjectRef::getplayer(ObjectRef *ref)
109 PlayerSAO *playersao = getplayersao(ref);
110 if(playersao == NULL)
112 return playersao->getPlayer();
115 // Exported functions
118 int ObjectRef::gc_object(lua_State *L) {
119 ObjectRef *o = *(ObjectRef **)(lua_touserdata(L, 1));
120 //infostream<<"ObjectRef::gc_object: o="<<o<<std::endl;
126 int ObjectRef::l_remove(lua_State *L)
128 NO_MAP_LOCK_REQUIRED;
129 ObjectRef *ref = checkobject(L, 1);
130 ServerActiveObject *co = getobject(ref);
131 if(co == NULL) return 0;
132 verbosestream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
133 co->m_removed = true;
138 // returns: {x=num, y=num, z=num}
139 int ObjectRef::l_getpos(lua_State *L)
141 NO_MAP_LOCK_REQUIRED;
142 ObjectRef *ref = checkobject(L, 1);
143 ServerActiveObject *co = getobject(ref);
144 if(co == NULL) return 0;
145 v3f pos = co->getBasePosition() / BS;
147 lua_pushnumber(L, pos.X);
148 lua_setfield(L, -2, "x");
149 lua_pushnumber(L, pos.Y);
150 lua_setfield(L, -2, "y");
151 lua_pushnumber(L, pos.Z);
152 lua_setfield(L, -2, "z");
157 int ObjectRef::l_setpos(lua_State *L)
159 NO_MAP_LOCK_REQUIRED;
160 ObjectRef *ref = checkobject(L, 1);
161 //LuaEntitySAO *co = getluaobject(ref);
162 ServerActiveObject *co = getobject(ref);
163 if(co == NULL) return 0;
165 v3f pos = checkFloatPos(L, 2);
171 // moveto(self, pos, continuous=false)
172 int ObjectRef::l_moveto(lua_State *L)
174 NO_MAP_LOCK_REQUIRED;
175 ObjectRef *ref = checkobject(L, 1);
176 //LuaEntitySAO *co = getluaobject(ref);
177 ServerActiveObject *co = getobject(ref);
178 if(co == NULL) return 0;
180 v3f pos = checkFloatPos(L, 2);
182 bool continuous = lua_toboolean(L, 3);
184 co->moveTo(pos, continuous);
188 // punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
189 int ObjectRef::l_punch(lua_State *L)
191 NO_MAP_LOCK_REQUIRED;
192 ObjectRef *ref = checkobject(L, 1);
193 ObjectRef *puncher_ref = checkobject(L, 2);
194 ServerActiveObject *co = getobject(ref);
195 ServerActiveObject *puncher = getobject(puncher_ref);
196 if(co == NULL) return 0;
197 if(puncher == NULL) return 0;
199 if(lua_type(L, 5) != LUA_TTABLE)
200 dir = co->getBasePosition() - puncher->getBasePosition();
202 dir = read_v3f(L, 5);
203 float time_from_last_punch = 1000000;
204 if(lua_isnumber(L, 3))
205 time_from_last_punch = lua_tonumber(L, 3);
206 ToolCapabilities toolcap = read_tool_capabilities(L, 4);
209 co->punch(dir, &toolcap, puncher, time_from_last_punch);
213 // right_click(self, clicker); clicker = an another ObjectRef
214 int ObjectRef::l_right_click(lua_State *L)
216 NO_MAP_LOCK_REQUIRED;
217 ObjectRef *ref = checkobject(L, 1);
218 ObjectRef *ref2 = checkobject(L, 2);
219 ServerActiveObject *co = getobject(ref);
220 ServerActiveObject *co2 = getobject(ref2);
221 if(co == NULL) return 0;
222 if(co2 == NULL) return 0;
229 // hp = number of hitpoints (2 * number of hearts)
231 int ObjectRef::l_set_hp(lua_State *L)
233 NO_MAP_LOCK_REQUIRED;
234 ObjectRef *ref = checkobject(L, 1);
235 luaL_checknumber(L, 2);
236 ServerActiveObject *co = getobject(ref);
237 if(co == NULL) return 0;
238 int hp = lua_tonumber(L, 2);
239 /*infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
240 <<" hp="<<hp<<std::endl;*/
248 // returns: number of hitpoints (2 * number of hearts)
249 // 0 if not applicable to this type of object
250 int ObjectRef::l_get_hp(lua_State *L)
252 NO_MAP_LOCK_REQUIRED;
253 ObjectRef *ref = checkobject(L, 1);
254 ServerActiveObject *co = getobject(ref);
257 lua_pushnumber(L, 1);
260 int hp = co->getHP();
261 /*infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
262 <<" hp="<<hp<<std::endl;*/
264 lua_pushnumber(L, hp);
268 // get_inventory(self)
269 int ObjectRef::l_get_inventory(lua_State *L)
271 NO_MAP_LOCK_REQUIRED;
272 ObjectRef *ref = checkobject(L, 1);
273 ServerActiveObject *co = getobject(ref);
274 if(co == NULL) return 0;
276 InventoryLocation loc = co->getInventoryLocation();
277 if(getServer(L)->getInventory(loc) != NULL)
278 InvRef::create(L, loc);
280 lua_pushnil(L); // An object may have no inventory (nil)
284 // get_wield_list(self)
285 int ObjectRef::l_get_wield_list(lua_State *L)
287 NO_MAP_LOCK_REQUIRED;
288 ObjectRef *ref = checkobject(L, 1);
289 ServerActiveObject *co = getobject(ref);
290 if(co == NULL) return 0;
292 lua_pushstring(L, co->getWieldList().c_str());
296 // get_wield_index(self)
297 int ObjectRef::l_get_wield_index(lua_State *L)
299 NO_MAP_LOCK_REQUIRED;
300 ObjectRef *ref = checkobject(L, 1);
301 ServerActiveObject *co = getobject(ref);
302 if(co == NULL) return 0;
304 lua_pushinteger(L, co->getWieldIndex() + 1);
308 // get_wielded_item(self)
309 int ObjectRef::l_get_wielded_item(lua_State *L)
311 NO_MAP_LOCK_REQUIRED;
312 ObjectRef *ref = checkobject(L, 1);
313 ServerActiveObject *co = getobject(ref);
316 LuaItemStack::create(L, ItemStack());
320 LuaItemStack::create(L, co->getWieldedItem());
324 // set_wielded_item(self, itemstack or itemstring or table or nil)
325 int ObjectRef::l_set_wielded_item(lua_State *L)
327 NO_MAP_LOCK_REQUIRED;
328 ObjectRef *ref = checkobject(L, 1);
329 ServerActiveObject *co = getobject(ref);
330 if(co == NULL) return 0;
332 ItemStack item = read_item(L, 2, getServer(L));
333 bool success = co->setWieldedItem(item);
334 lua_pushboolean(L, success);
338 // set_armor_groups(self, groups)
339 int ObjectRef::l_set_armor_groups(lua_State *L)
341 NO_MAP_LOCK_REQUIRED;
342 ObjectRef *ref = checkobject(L, 1);
343 ServerActiveObject *co = getobject(ref);
344 if(co == NULL) return 0;
346 ItemGroupList groups;
347 read_groups(L, 2, groups);
348 co->setArmorGroups(groups);
352 // set_physics_override(self, physics_override_speed, physics_override_jump,
353 // physics_override_gravity, sneak, sneak_glitch)
354 int ObjectRef::l_set_physics_override(lua_State *L)
356 ObjectRef *ref = checkobject(L, 1);
357 PlayerSAO *co = (PlayerSAO *) getobject(ref);
358 if(co == NULL) return 0;
360 if(!lua_isnil(L, 2)){
361 co->m_physics_override_speed = lua_tonumber(L, 2);
362 co->m_physics_override_sent = false;
364 if(!lua_isnil(L, 3)){
365 co->m_physics_override_jump = lua_tonumber(L, 3);
366 co->m_physics_override_sent = false;
368 if(!lua_isnil(L, 4)){
369 co->m_physics_override_gravity = lua_tonumber(L, 4);
370 co->m_physics_override_sent = false;
372 if (lua_isboolean(L, 5)) {
373 co->m_physics_override_sneak = lua_toboolean(L, 5);
374 co->m_physics_override_sent = false;
376 if (lua_isboolean(L, 6)) {
377 co->m_physics_override_sneak_glitch = lua_toboolean(L, 6);
378 co->m_physics_override_sent = false;
383 // set_animation(self, frame_range, frame_speed, frame_blend)
384 int ObjectRef::l_set_animation(lua_State *L)
386 NO_MAP_LOCK_REQUIRED;
387 ObjectRef *ref = checkobject(L, 1);
388 ServerActiveObject *co = getobject(ref);
389 if(co == NULL) return 0;
391 v2f frames = v2f(1, 1);
393 frames = read_v2f(L, 2);
394 float frame_speed = 15;
396 frame_speed = lua_tonumber(L, 3);
397 float frame_blend = 0;
399 frame_blend = lua_tonumber(L, 4);
400 co->setAnimation(frames, frame_speed, frame_blend);
404 // set_bone_position(self, std::string bone, v3f position, v3f rotation)
405 int ObjectRef::l_set_bone_position(lua_State *L)
407 NO_MAP_LOCK_REQUIRED;
408 ObjectRef *ref = checkobject(L, 1);
409 ServerActiveObject *co = getobject(ref);
410 if(co == NULL) return 0;
412 std::string bone = "";
414 bone = lua_tostring(L, 2);
415 v3f position = v3f(0, 0, 0);
417 position = read_v3f(L, 3);
418 v3f rotation = v3f(0, 0, 0);
420 rotation = read_v3f(L, 4);
421 co->setBonePosition(bone, position, rotation);
425 // set_attach(self, parent, bone, position, rotation)
426 int ObjectRef::l_set_attach(lua_State *L)
428 NO_MAP_LOCK_REQUIRED;
429 ObjectRef *ref = checkobject(L, 1);
430 ObjectRef *parent_ref = checkobject(L, 2);
431 ServerActiveObject *co = getobject(ref);
432 ServerActiveObject *parent = getobject(parent_ref);
433 if(co == NULL) return 0;
434 if(parent == NULL) return 0;
436 std::string bone = "";
438 bone = lua_tostring(L, 3);
439 v3f position = v3f(0, 0, 0);
441 position = read_v3f(L, 4);
442 v3f rotation = v3f(0, 0, 0);
444 rotation = read_v3f(L, 5);
445 co->setAttachment(parent->getId(), bone, position, rotation);
450 int ObjectRef::l_set_detach(lua_State *L)
452 NO_MAP_LOCK_REQUIRED;
453 ObjectRef *ref = checkobject(L, 1);
454 ServerActiveObject *co = getobject(ref);
455 if(co == NULL) return 0;
457 co->setAttachment(0, "", v3f(0,0,0), v3f(0,0,0));
461 // set_properties(self, properties)
462 int ObjectRef::l_set_properties(lua_State *L)
464 NO_MAP_LOCK_REQUIRED;
465 ObjectRef *ref = checkobject(L, 1);
466 ServerActiveObject *co = getobject(ref);
467 if(co == NULL) return 0;
468 ObjectProperties *prop = co->accessObjectProperties();
471 read_object_properties(L, 2, prop);
472 co->notifyObjectPropertiesModified();
476 /* LuaEntitySAO-only */
478 // setvelocity(self, {x=num, y=num, z=num})
479 int ObjectRef::l_setvelocity(lua_State *L)
481 NO_MAP_LOCK_REQUIRED;
482 ObjectRef *ref = checkobject(L, 1);
483 LuaEntitySAO *co = getluaobject(ref);
484 if(co == NULL) return 0;
485 v3f pos = checkFloatPos(L, 2);
487 co->setVelocity(pos);
492 int ObjectRef::l_getvelocity(lua_State *L)
494 NO_MAP_LOCK_REQUIRED;
495 ObjectRef *ref = checkobject(L, 1);
496 LuaEntitySAO *co = getluaobject(ref);
497 if(co == NULL) return 0;
499 v3f v = co->getVelocity();
504 // setacceleration(self, {x=num, y=num, z=num})
505 int ObjectRef::l_setacceleration(lua_State *L)
507 NO_MAP_LOCK_REQUIRED;
508 ObjectRef *ref = checkobject(L, 1);
509 LuaEntitySAO *co = getluaobject(ref);
510 if(co == NULL) return 0;
512 v3f pos = checkFloatPos(L, 2);
514 co->setAcceleration(pos);
518 // getacceleration(self)
519 int ObjectRef::l_getacceleration(lua_State *L)
521 NO_MAP_LOCK_REQUIRED;
522 ObjectRef *ref = checkobject(L, 1);
523 LuaEntitySAO *co = getluaobject(ref);
524 if(co == NULL) return 0;
526 v3f v = co->getAcceleration();
531 // setyaw(self, radians)
532 int ObjectRef::l_setyaw(lua_State *L)
534 NO_MAP_LOCK_REQUIRED;
535 ObjectRef *ref = checkobject(L, 1);
536 LuaEntitySAO *co = getluaobject(ref);
537 if(co == NULL) return 0;
538 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
545 int ObjectRef::l_getyaw(lua_State *L)
547 NO_MAP_LOCK_REQUIRED;
548 ObjectRef *ref = checkobject(L, 1);
549 LuaEntitySAO *co = getluaobject(ref);
550 if(co == NULL) return 0;
552 float yaw = co->getYaw() * core::DEGTORAD;
553 lua_pushnumber(L, yaw);
557 // settexturemod(self, mod)
558 int ObjectRef::l_settexturemod(lua_State *L)
560 NO_MAP_LOCK_REQUIRED;
561 ObjectRef *ref = checkobject(L, 1);
562 LuaEntitySAO *co = getluaobject(ref);
563 if(co == NULL) return 0;
565 std::string mod = luaL_checkstring(L, 2);
566 co->setTextureMod(mod);
570 // setsprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
571 // select_horiz_by_yawpitch=false)
572 int ObjectRef::l_setsprite(lua_State *L)
574 NO_MAP_LOCK_REQUIRED;
575 ObjectRef *ref = checkobject(L, 1);
576 LuaEntitySAO *co = getluaobject(ref);
577 if(co == NULL) return 0;
581 p = read_v2s16(L, 2);
584 num_frames = lua_tonumber(L, 3);
585 float framelength = 0.2;
587 framelength = lua_tonumber(L, 4);
588 bool select_horiz_by_yawpitch = false;
590 select_horiz_by_yawpitch = lua_toboolean(L, 5);
591 co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
596 // get_entity_name(self)
597 int ObjectRef::l_get_entity_name(lua_State *L)
599 NO_MAP_LOCK_REQUIRED;
600 ObjectRef *ref = checkobject(L, 1);
601 LuaEntitySAO *co = getluaobject(ref);
602 if(co == NULL) return 0;
604 std::string name = co->getName();
605 lua_pushstring(L, name.c_str());
609 // get_luaentity(self)
610 int ObjectRef::l_get_luaentity(lua_State *L)
612 NO_MAP_LOCK_REQUIRED;
613 ObjectRef *ref = checkobject(L, 1);
614 LuaEntitySAO *co = getluaobject(ref);
615 if(co == NULL) return 0;
617 luaentity_get(L, co->getId());
624 int ObjectRef::l_is_player(lua_State *L)
626 NO_MAP_LOCK_REQUIRED;
627 ObjectRef *ref = checkobject(L, 1);
628 Player *player = getplayer(ref);
629 lua_pushboolean(L, (player != NULL));
633 // is_player_connected(self)
634 int ObjectRef::l_is_player_connected(lua_State *L)
636 NO_MAP_LOCK_REQUIRED;
637 ObjectRef *ref = checkobject(L, 1);
638 Player *player = getplayer(ref);
639 lua_pushboolean(L, (player != NULL && player->peer_id != 0));
643 // get_player_name(self)
644 int ObjectRef::l_get_player_name(lua_State *L)
646 NO_MAP_LOCK_REQUIRED;
647 ObjectRef *ref = checkobject(L, 1);
648 Player *player = getplayer(ref);
650 lua_pushlstring(L, "", 0);
654 lua_pushstring(L, player->getName());
658 // get_look_dir(self)
659 int ObjectRef::l_get_look_dir(lua_State *L)
661 NO_MAP_LOCK_REQUIRED;
662 ObjectRef *ref = checkobject(L, 1);
663 Player *player = getplayer(ref);
664 if(player == NULL) return 0;
666 float pitch = player->getRadPitch();
667 float yaw = player->getRadYaw();
668 v3f v(cos(pitch)*cos(yaw), sin(pitch), cos(pitch)*sin(yaw));
673 // get_look_pitch(self)
674 int ObjectRef::l_get_look_pitch(lua_State *L)
676 NO_MAP_LOCK_REQUIRED;
677 ObjectRef *ref = checkobject(L, 1);
678 Player *player = getplayer(ref);
679 if(player == NULL) return 0;
681 lua_pushnumber(L, player->getRadPitch());
685 // get_look_yaw(self)
686 int ObjectRef::l_get_look_yaw(lua_State *L)
688 NO_MAP_LOCK_REQUIRED;
689 ObjectRef *ref = checkobject(L, 1);
690 Player *player = getplayer(ref);
691 if(player == NULL) return 0;
693 lua_pushnumber(L, player->getRadYaw());
697 // set_look_pitch(self, radians)
698 int ObjectRef::l_set_look_pitch(lua_State *L)
700 NO_MAP_LOCK_REQUIRED;
701 ObjectRef *ref = checkobject(L, 1);
702 PlayerSAO* co = getplayersao(ref);
703 if(co == NULL) return 0;
704 float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
710 // set_look_yaw(self, radians)
711 int ObjectRef::l_set_look_yaw(lua_State *L)
713 NO_MAP_LOCK_REQUIRED;
714 ObjectRef *ref = checkobject(L, 1);
715 PlayerSAO* co = getplayersao(ref);
716 if(co == NULL) return 0;
717 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
723 // set_breath(self, breath)
724 int ObjectRef::l_set_breath(lua_State *L)
726 NO_MAP_LOCK_REQUIRED;
727 ObjectRef *ref = checkobject(L, 1);
728 PlayerSAO* co = getplayersao(ref);
729 if(co == NULL) return 0;
730 u16 breath = luaL_checknumber(L, 2);
732 co->setBreath(breath);
733 co->m_breath_not_sent = true;
738 int ObjectRef::l_get_breath(lua_State *L)
740 NO_MAP_LOCK_REQUIRED;
741 ObjectRef *ref = checkobject(L, 1);
742 PlayerSAO* co = getplayersao(ref);
743 if(co == NULL) return 0;
745 u16 breath = co->getBreath();
746 lua_pushinteger (L, breath);
750 // set_inventory_formspec(self, formspec)
751 int ObjectRef::l_set_inventory_formspec(lua_State *L)
753 NO_MAP_LOCK_REQUIRED;
754 ObjectRef *ref = checkobject(L, 1);
755 Player *player = getplayer(ref);
756 if(player == NULL) return 0;
757 std::string formspec = luaL_checkstring(L, 2);
759 player->inventory_formspec = formspec;
760 getServer(L)->reportInventoryFormspecModified(player->getName());
761 lua_pushboolean(L, true);
765 // get_inventory_formspec(self) -> formspec
766 int ObjectRef::l_get_inventory_formspec(lua_State *L)
768 NO_MAP_LOCK_REQUIRED;
769 ObjectRef *ref = checkobject(L, 1);
770 Player *player = getplayer(ref);
771 if(player == NULL) return 0;
773 std::string formspec = player->inventory_formspec;
774 lua_pushlstring(L, formspec.c_str(), formspec.size());
778 // get_player_control(self)
779 int ObjectRef::l_get_player_control(lua_State *L)
781 NO_MAP_LOCK_REQUIRED;
782 ObjectRef *ref = checkobject(L, 1);
783 Player *player = getplayer(ref);
785 lua_pushlstring(L, "", 0);
789 PlayerControl control = player->getPlayerControl();
791 lua_pushboolean(L, control.up);
792 lua_setfield(L, -2, "up");
793 lua_pushboolean(L, control.down);
794 lua_setfield(L, -2, "down");
795 lua_pushboolean(L, control.left);
796 lua_setfield(L, -2, "left");
797 lua_pushboolean(L, control.right);
798 lua_setfield(L, -2, "right");
799 lua_pushboolean(L, control.jump);
800 lua_setfield(L, -2, "jump");
801 lua_pushboolean(L, control.aux1);
802 lua_setfield(L, -2, "aux1");
803 lua_pushboolean(L, control.sneak);
804 lua_setfield(L, -2, "sneak");
805 lua_pushboolean(L, control.LMB);
806 lua_setfield(L, -2, "LMB");
807 lua_pushboolean(L, control.RMB);
808 lua_setfield(L, -2, "RMB");
812 // get_player_control_bits(self)
813 int ObjectRef::l_get_player_control_bits(lua_State *L)
815 NO_MAP_LOCK_REQUIRED;
816 ObjectRef *ref = checkobject(L, 1);
817 Player *player = getplayer(ref);
819 lua_pushlstring(L, "", 0);
823 lua_pushnumber(L, player->keyPressed);
827 // hud_add(self, form)
828 int ObjectRef::l_hud_add(lua_State *L)
830 ObjectRef *ref = checkobject(L, 1);
831 Player *player = getplayer(ref);
835 HudElement *elem = new HudElement;
837 elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type",
838 es_HudElementType, HUD_ELEM_TEXT);
840 lua_getfield(L, 2, "position");
841 elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
844 lua_getfield(L, 2, "scale");
845 elem->scale = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
848 elem->name = getstringfield_default(L, 2, "name", "");
849 elem->text = getstringfield_default(L, 2, "text", "");
850 elem->number = getintfield_default(L, 2, "number", 0);
851 elem->item = getintfield_default(L, 2, "item", 0);
852 elem->dir = getintfield_default(L, 2, "dir", 0);
854 lua_getfield(L, 2, "alignment");
855 elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
858 lua_getfield(L, 2, "offset");
859 elem->offset = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
862 u32 id = getServer(L)->hudAdd(player, elem);
868 lua_pushnumber(L, id);
872 // hud_remove(self, id)
873 int ObjectRef::l_hud_remove(lua_State *L)
875 ObjectRef *ref = checkobject(L, 1);
876 Player *player = getplayer(ref);
881 if (!lua_isnil(L, 2))
882 id = lua_tonumber(L, 2);
884 if (!getServer(L)->hudRemove(player, id))
887 lua_pushboolean(L, true);
891 // hud_change(self, id, stat, data)
892 int ObjectRef::l_hud_change(lua_State *L)
894 ObjectRef *ref = checkobject(L, 1);
895 Player *player = getplayer(ref);
899 u32 id = !lua_isnil(L, 2) ? lua_tonumber(L, 2) : -1;
900 if (id >= player->hud.size())
903 HudElementStat stat = HUD_STAT_NUMBER;
904 if (!lua_isnil(L, 3)) {
906 std::string statstr = lua_tostring(L, 3);
907 stat = string_to_enum(es_HudElementStat, statint, statstr) ?
908 (HudElementStat)statint : HUD_STAT_NUMBER;
912 HudElement *e = player->hud[id];
918 e->pos = read_v2f(L, 4);
922 e->name = lua_tostring(L, 4);
926 e->scale = read_v2f(L, 4);
930 e->text = lua_tostring(L, 4);
933 case HUD_STAT_NUMBER:
934 e->number = lua_tonumber(L, 4);
938 e->item = lua_tonumber(L, 4);
942 e->dir = lua_tonumber(L, 4);
945 e->align = read_v2f(L, 4);
947 case HUD_STAT_OFFSET:
948 e->offset = read_v2f(L, 4);
952 getServer(L)->hudChange(player, id, stat, value);
954 lua_pushboolean(L, true);
959 int ObjectRef::l_hud_get(lua_State *L)
961 ObjectRef *ref = checkobject(L, 1);
962 Player *player = getplayer(ref);
966 u32 id = lua_tonumber(L, -1);
967 if (id >= player->hud.size())
970 HudElement *e = player->hud[id];
976 lua_pushstring(L, es_HudElementType[(u8)e->type].str);
977 lua_setfield(L, -2, "type");
980 lua_setfield(L, -2, "position");
982 lua_pushstring(L, e->name.c_str());
983 lua_setfield(L, -2, "name");
985 push_v2f(L, e->scale);
986 lua_setfield(L, -2, "scale");
988 lua_pushstring(L, e->text.c_str());
989 lua_setfield(L, -2, "text");
991 lua_pushnumber(L, e->number);
992 lua_setfield(L, -2, "number");
994 lua_pushnumber(L, e->item);
995 lua_setfield(L, -2, "item");
997 lua_pushnumber(L, e->dir);
998 lua_setfield(L, -2, "dir");
1003 // hud_set_flags(self, flags)
1004 int ObjectRef::l_hud_set_flags(lua_State *L)
1006 ObjectRef *ref = checkobject(L, 1);
1007 Player *player = getplayer(ref);
1015 const EnumString *esp = es_HudBuiltinElement;
1016 for (int i = 0; esp[i].str; i++) {
1017 if (getboolfield(L, 2, esp[i].str, flag)) {
1018 flags |= esp[i].num * flag;
1022 if (!getServer(L)->hudSetFlags(player, flags, mask))
1025 lua_pushboolean(L, true);
1029 // hud_set_hotbar_itemcount(self, hotbar_itemcount)
1030 int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L)
1032 ObjectRef *ref = checkobject(L, 1);
1033 Player *player = getplayer(ref);
1037 s32 hotbar_itemcount = lua_tonumber(L, 2);
1039 if (!getServer(L)->hudSetHotbarItemcount(player, hotbar_itemcount))
1042 lua_pushboolean(L, true);
1046 // hud_set_hotbar_image(self, name)
1047 int ObjectRef::l_hud_set_hotbar_image(lua_State *L)
1049 ObjectRef *ref = checkobject(L, 1);
1050 Player *player = getplayer(ref);
1054 std::string name = lua_tostring(L, 2);
1056 getServer(L)->hudSetHotbarImage(player, name);
1060 // hud_set_hotbar_selected_image(self, name)
1061 int ObjectRef::l_hud_set_hotbar_selected_image(lua_State *L)
1063 ObjectRef *ref = checkobject(L, 1);
1064 Player *player = getplayer(ref);
1068 std::string name = lua_tostring(L, 2);
1070 getServer(L)->hudSetHotbarSelectedImage(player, name);
1074 ObjectRef::ObjectRef(ServerActiveObject *object):
1077 //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
1080 ObjectRef::~ObjectRef()
1083 infostream<<"ObjectRef destructing for id="
1084 <<m_object->getId()<<std::endl;
1086 infostream<<"ObjectRef destructing for id=unknown"<<std::endl;*/
1089 // Creates an ObjectRef and leaves it on top of stack
1090 // Not callable from Lua; all references are created on the C side.
1091 void ObjectRef::create(lua_State *L, ServerActiveObject *object)
1093 ObjectRef *o = new ObjectRef(object);
1094 //infostream<<"ObjectRef::create: o="<<o<<std::endl;
1095 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1096 luaL_getmetatable(L, className);
1097 lua_setmetatable(L, -2);
1100 void ObjectRef::set_null(lua_State *L)
1102 ObjectRef *o = checkobject(L, -1);
1106 void ObjectRef::Register(lua_State *L)
1109 int methodtable = lua_gettop(L);
1110 luaL_newmetatable(L, className);
1111 int metatable = lua_gettop(L);
1113 lua_pushliteral(L, "__metatable");
1114 lua_pushvalue(L, methodtable);
1115 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
1117 lua_pushliteral(L, "__index");
1118 lua_pushvalue(L, methodtable);
1119 lua_settable(L, metatable);
1121 lua_pushliteral(L, "__gc");
1122 lua_pushcfunction(L, gc_object);
1123 lua_settable(L, metatable);
1125 lua_pop(L, 1); // drop metatable
1127 luaL_openlib(L, 0, methods, 0); // fill methodtable
1128 lua_pop(L, 1); // drop methodtable
1130 // Cannot be created from Lua
1131 //lua_register(L, className, create_object);
1134 const char ObjectRef::className[] = "ObjectRef";
1135 const luaL_reg ObjectRef::methods[] = {
1136 // ServerActiveObject
1137 luamethod(ObjectRef, remove),
1138 luamethod(ObjectRef, getpos),
1139 luamethod(ObjectRef, setpos),
1140 luamethod(ObjectRef, moveto),
1141 luamethod(ObjectRef, punch),
1142 luamethod(ObjectRef, right_click),
1143 luamethod(ObjectRef, set_hp),
1144 luamethod(ObjectRef, get_hp),
1145 luamethod(ObjectRef, get_inventory),
1146 luamethod(ObjectRef, get_wield_list),
1147 luamethod(ObjectRef, get_wield_index),
1148 luamethod(ObjectRef, get_wielded_item),
1149 luamethod(ObjectRef, set_wielded_item),
1150 luamethod(ObjectRef, set_armor_groups),
1151 luamethod(ObjectRef, set_physics_override),
1152 luamethod(ObjectRef, set_animation),
1153 luamethod(ObjectRef, set_bone_position),
1154 luamethod(ObjectRef, set_attach),
1155 luamethod(ObjectRef, set_detach),
1156 luamethod(ObjectRef, set_properties),
1157 // LuaEntitySAO-only
1158 luamethod(ObjectRef, setvelocity),
1159 luamethod(ObjectRef, getvelocity),
1160 luamethod(ObjectRef, setacceleration),
1161 luamethod(ObjectRef, getacceleration),
1162 luamethod(ObjectRef, setyaw),
1163 luamethod(ObjectRef, getyaw),
1164 luamethod(ObjectRef, settexturemod),
1165 luamethod(ObjectRef, setsprite),
1166 luamethod(ObjectRef, get_entity_name),
1167 luamethod(ObjectRef, get_luaentity),
1169 luamethod(ObjectRef, is_player),
1170 luamethod(ObjectRef, is_player_connected),
1171 luamethod(ObjectRef, get_player_name),
1172 luamethod(ObjectRef, get_look_dir),
1173 luamethod(ObjectRef, get_look_pitch),
1174 luamethod(ObjectRef, get_look_yaw),
1175 luamethod(ObjectRef, set_look_yaw),
1176 luamethod(ObjectRef, set_look_pitch),
1177 luamethod(ObjectRef, get_breath),
1178 luamethod(ObjectRef, set_breath),
1179 luamethod(ObjectRef, set_inventory_formspec),
1180 luamethod(ObjectRef, get_inventory_formspec),
1181 luamethod(ObjectRef, get_player_control),
1182 luamethod(ObjectRef, get_player_control_bits),
1183 luamethod(ObjectRef, hud_add),
1184 luamethod(ObjectRef, hud_remove),
1185 luamethod(ObjectRef, hud_change),
1186 luamethod(ObjectRef, hud_get),
1187 luamethod(ObjectRef, hud_set_flags),
1188 luamethod(ObjectRef, hud_set_hotbar_itemcount),
1189 luamethod(ObjectRef, hud_set_hotbar_image),
1190 luamethod(ObjectRef, hud_set_hotbar_selected_image),