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, physics_override_gravity)
353 int ObjectRef::l_set_physics_override(lua_State *L)
355 ObjectRef *ref = checkobject(L, 1);
356 PlayerSAO *co = (PlayerSAO *) getobject(ref);
357 if(co == NULL) return 0;
359 if(!lua_isnil(L, 2)){
360 co->m_physics_override_speed = lua_tonumber(L, 2);
361 co->m_physics_override_sent = false;
363 if(!lua_isnil(L, 3)){
364 co->m_physics_override_jump = lua_tonumber(L, 3);
365 co->m_physics_override_sent = false;
367 if(!lua_isnil(L, 4)){
368 co->m_physics_override_gravity = lua_tonumber(L, 4);
369 co->m_physics_override_sent = false;
374 // set_animation(self, frame_range, frame_speed, frame_blend)
375 int ObjectRef::l_set_animation(lua_State *L)
377 NO_MAP_LOCK_REQUIRED;
378 ObjectRef *ref = checkobject(L, 1);
379 ServerActiveObject *co = getobject(ref);
380 if(co == NULL) return 0;
382 v2f frames = v2f(1, 1);
384 frames = read_v2f(L, 2);
385 float frame_speed = 15;
387 frame_speed = lua_tonumber(L, 3);
388 float frame_blend = 0;
390 frame_blend = lua_tonumber(L, 4);
391 co->setAnimation(frames, frame_speed, frame_blend);
395 // set_bone_position(self, std::string bone, v3f position, v3f rotation)
396 int ObjectRef::l_set_bone_position(lua_State *L)
398 NO_MAP_LOCK_REQUIRED;
399 ObjectRef *ref = checkobject(L, 1);
400 ServerActiveObject *co = getobject(ref);
401 if(co == NULL) return 0;
403 std::string bone = "";
405 bone = lua_tostring(L, 2);
406 v3f position = v3f(0, 0, 0);
408 position = read_v3f(L, 3);
409 v3f rotation = v3f(0, 0, 0);
411 rotation = read_v3f(L, 4);
412 co->setBonePosition(bone, position, rotation);
416 // set_attach(self, parent, bone, position, rotation)
417 int ObjectRef::l_set_attach(lua_State *L)
419 NO_MAP_LOCK_REQUIRED;
420 ObjectRef *ref = checkobject(L, 1);
421 ObjectRef *parent_ref = checkobject(L, 2);
422 ServerActiveObject *co = getobject(ref);
423 ServerActiveObject *parent = getobject(parent_ref);
424 if(co == NULL) return 0;
425 if(parent == NULL) return 0;
427 std::string bone = "";
429 bone = lua_tostring(L, 3);
430 v3f position = v3f(0, 0, 0);
432 position = read_v3f(L, 4);
433 v3f rotation = v3f(0, 0, 0);
435 rotation = read_v3f(L, 5);
436 co->setAttachment(parent->getId(), bone, position, rotation);
441 int ObjectRef::l_set_detach(lua_State *L)
443 NO_MAP_LOCK_REQUIRED;
444 ObjectRef *ref = checkobject(L, 1);
445 ServerActiveObject *co = getobject(ref);
446 if(co == NULL) return 0;
448 co->setAttachment(0, "", v3f(0,0,0), v3f(0,0,0));
452 // set_properties(self, properties)
453 int ObjectRef::l_set_properties(lua_State *L)
455 NO_MAP_LOCK_REQUIRED;
456 ObjectRef *ref = checkobject(L, 1);
457 ServerActiveObject *co = getobject(ref);
458 if(co == NULL) return 0;
459 ObjectProperties *prop = co->accessObjectProperties();
462 read_object_properties(L, 2, prop);
463 co->notifyObjectPropertiesModified();
467 /* LuaEntitySAO-only */
469 // setvelocity(self, {x=num, y=num, z=num})
470 int ObjectRef::l_setvelocity(lua_State *L)
472 NO_MAP_LOCK_REQUIRED;
473 ObjectRef *ref = checkobject(L, 1);
474 LuaEntitySAO *co = getluaobject(ref);
475 if(co == NULL) return 0;
476 v3f pos = checkFloatPos(L, 2);
478 co->setVelocity(pos);
483 int ObjectRef::l_getvelocity(lua_State *L)
485 NO_MAP_LOCK_REQUIRED;
486 ObjectRef *ref = checkobject(L, 1);
487 LuaEntitySAO *co = getluaobject(ref);
488 if(co == NULL) return 0;
490 v3f v = co->getVelocity();
495 // setacceleration(self, {x=num, y=num, z=num})
496 int ObjectRef::l_setacceleration(lua_State *L)
498 NO_MAP_LOCK_REQUIRED;
499 ObjectRef *ref = checkobject(L, 1);
500 LuaEntitySAO *co = getluaobject(ref);
501 if(co == NULL) return 0;
503 v3f pos = checkFloatPos(L, 2);
505 co->setAcceleration(pos);
509 // getacceleration(self)
510 int ObjectRef::l_getacceleration(lua_State *L)
512 NO_MAP_LOCK_REQUIRED;
513 ObjectRef *ref = checkobject(L, 1);
514 LuaEntitySAO *co = getluaobject(ref);
515 if(co == NULL) return 0;
517 v3f v = co->getAcceleration();
522 // setyaw(self, radians)
523 int ObjectRef::l_setyaw(lua_State *L)
525 NO_MAP_LOCK_REQUIRED;
526 ObjectRef *ref = checkobject(L, 1);
527 LuaEntitySAO *co = getluaobject(ref);
528 if(co == NULL) return 0;
529 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
536 int ObjectRef::l_getyaw(lua_State *L)
538 NO_MAP_LOCK_REQUIRED;
539 ObjectRef *ref = checkobject(L, 1);
540 LuaEntitySAO *co = getluaobject(ref);
541 if(co == NULL) return 0;
543 float yaw = co->getYaw() * core::DEGTORAD;
544 lua_pushnumber(L, yaw);
548 // settexturemod(self, mod)
549 int ObjectRef::l_settexturemod(lua_State *L)
551 NO_MAP_LOCK_REQUIRED;
552 ObjectRef *ref = checkobject(L, 1);
553 LuaEntitySAO *co = getluaobject(ref);
554 if(co == NULL) return 0;
556 std::string mod = luaL_checkstring(L, 2);
557 co->setTextureMod(mod);
561 // setsprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
562 // select_horiz_by_yawpitch=false)
563 int ObjectRef::l_setsprite(lua_State *L)
565 NO_MAP_LOCK_REQUIRED;
566 ObjectRef *ref = checkobject(L, 1);
567 LuaEntitySAO *co = getluaobject(ref);
568 if(co == NULL) return 0;
572 p = read_v2s16(L, 2);
575 num_frames = lua_tonumber(L, 3);
576 float framelength = 0.2;
578 framelength = lua_tonumber(L, 4);
579 bool select_horiz_by_yawpitch = false;
581 select_horiz_by_yawpitch = lua_toboolean(L, 5);
582 co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
587 // get_entity_name(self)
588 int ObjectRef::l_get_entity_name(lua_State *L)
590 NO_MAP_LOCK_REQUIRED;
591 ObjectRef *ref = checkobject(L, 1);
592 LuaEntitySAO *co = getluaobject(ref);
593 if(co == NULL) return 0;
595 std::string name = co->getName();
596 lua_pushstring(L, name.c_str());
600 // get_luaentity(self)
601 int ObjectRef::l_get_luaentity(lua_State *L)
603 NO_MAP_LOCK_REQUIRED;
604 ObjectRef *ref = checkobject(L, 1);
605 LuaEntitySAO *co = getluaobject(ref);
606 if(co == NULL) return 0;
608 luaentity_get(L, co->getId());
615 int ObjectRef::l_is_player(lua_State *L)
617 NO_MAP_LOCK_REQUIRED;
618 ObjectRef *ref = checkobject(L, 1);
619 Player *player = getplayer(ref);
620 lua_pushboolean(L, (player != NULL));
624 // get_player_name(self)
625 int ObjectRef::l_get_player_name(lua_State *L)
627 NO_MAP_LOCK_REQUIRED;
628 ObjectRef *ref = checkobject(L, 1);
629 Player *player = getplayer(ref);
631 lua_pushlstring(L, "", 0);
635 lua_pushstring(L, player->getName());
639 // get_look_dir(self)
640 int ObjectRef::l_get_look_dir(lua_State *L)
642 NO_MAP_LOCK_REQUIRED;
643 ObjectRef *ref = checkobject(L, 1);
644 Player *player = getplayer(ref);
645 if(player == NULL) return 0;
647 float pitch = player->getRadPitch();
648 float yaw = player->getRadYaw();
649 v3f v(cos(pitch)*cos(yaw), sin(pitch), cos(pitch)*sin(yaw));
654 // get_look_pitch(self)
655 int ObjectRef::l_get_look_pitch(lua_State *L)
657 NO_MAP_LOCK_REQUIRED;
658 ObjectRef *ref = checkobject(L, 1);
659 Player *player = getplayer(ref);
660 if(player == NULL) return 0;
662 lua_pushnumber(L, player->getRadPitch());
666 // get_look_yaw(self)
667 int ObjectRef::l_get_look_yaw(lua_State *L)
669 NO_MAP_LOCK_REQUIRED;
670 ObjectRef *ref = checkobject(L, 1);
671 Player *player = getplayer(ref);
672 if(player == NULL) return 0;
674 lua_pushnumber(L, player->getRadYaw());
678 // set_look_pitch(self, radians)
679 int ObjectRef::l_set_look_pitch(lua_State *L)
681 NO_MAP_LOCK_REQUIRED;
682 ObjectRef *ref = checkobject(L, 1);
683 PlayerSAO* co = getplayersao(ref);
684 if(co == NULL) return 0;
685 float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
691 // set_look_yaw(self, radians)
692 int ObjectRef::l_set_look_yaw(lua_State *L)
694 NO_MAP_LOCK_REQUIRED;
695 ObjectRef *ref = checkobject(L, 1);
696 PlayerSAO* co = getplayersao(ref);
697 if(co == NULL) return 0;
698 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
704 // set_breath(self, breath)
705 int ObjectRef::l_set_breath(lua_State *L)
707 NO_MAP_LOCK_REQUIRED;
708 ObjectRef *ref = checkobject(L, 1);
709 PlayerSAO* co = getplayersao(ref);
710 if(co == NULL) return 0;
711 u16 breath = luaL_checknumber(L, 2);
713 co->setBreath(breath);
714 co->m_breath_not_sent = true;
719 int ObjectRef::l_get_breath(lua_State *L)
721 NO_MAP_LOCK_REQUIRED;
722 ObjectRef *ref = checkobject(L, 1);
723 PlayerSAO* co = getplayersao(ref);
724 if(co == NULL) return 0;
726 u16 breath = co->getBreath();
727 lua_pushinteger (L, breath);
731 // set_inventory_formspec(self, formspec)
732 int ObjectRef::l_set_inventory_formspec(lua_State *L)
734 NO_MAP_LOCK_REQUIRED;
735 ObjectRef *ref = checkobject(L, 1);
736 Player *player = getplayer(ref);
737 if(player == NULL) return 0;
738 std::string formspec = luaL_checkstring(L, 2);
740 player->inventory_formspec = formspec;
741 getServer(L)->reportInventoryFormspecModified(player->getName());
742 lua_pushboolean(L, true);
746 // get_inventory_formspec(self) -> formspec
747 int ObjectRef::l_get_inventory_formspec(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 std::string formspec = player->inventory_formspec;
755 lua_pushlstring(L, formspec.c_str(), formspec.size());
759 // get_player_control(self)
760 int ObjectRef::l_get_player_control(lua_State *L)
762 NO_MAP_LOCK_REQUIRED;
763 ObjectRef *ref = checkobject(L, 1);
764 Player *player = getplayer(ref);
766 lua_pushlstring(L, "", 0);
770 PlayerControl control = player->getPlayerControl();
772 lua_pushboolean(L, control.up);
773 lua_setfield(L, -2, "up");
774 lua_pushboolean(L, control.down);
775 lua_setfield(L, -2, "down");
776 lua_pushboolean(L, control.left);
777 lua_setfield(L, -2, "left");
778 lua_pushboolean(L, control.right);
779 lua_setfield(L, -2, "right");
780 lua_pushboolean(L, control.jump);
781 lua_setfield(L, -2, "jump");
782 lua_pushboolean(L, control.aux1);
783 lua_setfield(L, -2, "aux1");
784 lua_pushboolean(L, control.sneak);
785 lua_setfield(L, -2, "sneak");
786 lua_pushboolean(L, control.LMB);
787 lua_setfield(L, -2, "LMB");
788 lua_pushboolean(L, control.RMB);
789 lua_setfield(L, -2, "RMB");
793 // get_player_control_bits(self)
794 int ObjectRef::l_get_player_control_bits(lua_State *L)
796 NO_MAP_LOCK_REQUIRED;
797 ObjectRef *ref = checkobject(L, 1);
798 Player *player = getplayer(ref);
800 lua_pushlstring(L, "", 0);
804 lua_pushnumber(L, player->keyPressed);
808 // hud_add(self, form)
809 int ObjectRef::l_hud_add(lua_State *L)
811 ObjectRef *ref = checkobject(L, 1);
812 Player *player = getplayer(ref);
816 HudElement *elem = new HudElement;
818 elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type",
819 es_HudElementType, HUD_ELEM_TEXT);
821 lua_getfield(L, 2, "position");
822 elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
825 lua_getfield(L, 2, "scale");
826 elem->scale = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
829 elem->name = getstringfield_default(L, 2, "name", "");
830 elem->text = getstringfield_default(L, 2, "text", "");
831 elem->number = getintfield_default(L, 2, "number", 0);
832 elem->item = getintfield_default(L, 2, "item", 0);
833 elem->dir = getintfield_default(L, 2, "dir", 0);
835 lua_getfield(L, 2, "alignment");
836 elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
839 lua_getfield(L, 2, "offset");
840 elem->offset = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
843 u32 id = getServer(L)->hudAdd(player, elem);
849 lua_pushnumber(L, id);
853 // hud_remove(self, id)
854 int ObjectRef::l_hud_remove(lua_State *L)
856 ObjectRef *ref = checkobject(L, 1);
857 Player *player = getplayer(ref);
862 if (!lua_isnil(L, 2))
863 id = lua_tonumber(L, 2);
865 if (!getServer(L)->hudRemove(player, id))
868 lua_pushboolean(L, true);
872 // hud_change(self, id, stat, data)
873 int ObjectRef::l_hud_change(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 HudElementStat stat = (HudElementStat)getenumfield(L, 3, "stat",
885 es_HudElementStat, HUD_STAT_NUMBER);
887 if (id >= player->hud.size())
891 HudElement *e = player->hud[id];
897 e->pos = read_v2f(L, 4);
901 e->name = lua_tostring(L, 4);
905 e->scale = read_v2f(L, 4);
909 e->text = lua_tostring(L, 4);
912 case HUD_STAT_NUMBER:
913 e->number = lua_tonumber(L, 4);
917 e->item = lua_tonumber(L, 4);
921 e->dir = lua_tonumber(L, 4);
924 e->align = read_v2f(L, 4);
926 case HUD_STAT_OFFSET:
927 e->offset = read_v2f(L, 4);
931 getServer(L)->hudChange(player, id, stat, value);
933 lua_pushboolean(L, true);
938 int ObjectRef::l_hud_get(lua_State *L)
940 ObjectRef *ref = checkobject(L, 1);
941 Player *player = getplayer(ref);
945 u32 id = lua_tonumber(L, -1);
946 if (id >= player->hud.size())
949 HudElement *e = player->hud[id];
955 lua_pushstring(L, es_HudElementType[(u8)e->type].str);
956 lua_setfield(L, -2, "type");
959 lua_setfield(L, -2, "position");
961 lua_pushstring(L, e->name.c_str());
962 lua_setfield(L, -2, "name");
964 push_v2f(L, e->scale);
965 lua_setfield(L, -2, "scale");
967 lua_pushstring(L, e->text.c_str());
968 lua_setfield(L, -2, "text");
970 lua_pushnumber(L, e->number);
971 lua_setfield(L, -2, "number");
973 lua_pushnumber(L, e->item);
974 lua_setfield(L, -2, "item");
976 lua_pushnumber(L, e->dir);
977 lua_setfield(L, -2, "dir");
982 // hud_set_flags(self, flags)
983 int ObjectRef::l_hud_set_flags(lua_State *L)
985 ObjectRef *ref = checkobject(L, 1);
986 Player *player = getplayer(ref);
994 const EnumString *esp = es_HudBuiltinElement;
995 for (int i = 0; esp[i].str; i++) {
996 if (getboolfield(L, 2, esp[i].str, flag)) {
997 flags |= esp[i].num * flag;
1001 if (!getServer(L)->hudSetFlags(player, flags, mask))
1004 lua_pushboolean(L, true);
1008 // hud_set_hotbar_itemcount(self, hotbar_itemcount)
1009 int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L)
1011 ObjectRef *ref = checkobject(L, 1);
1012 Player *player = getplayer(ref);
1016 s32 hotbar_itemcount = lua_tonumber(L, 2);
1018 if (!getServer(L)->hudSetHotbarItemcount(player, hotbar_itemcount))
1021 lua_pushboolean(L, true);
1025 ObjectRef::ObjectRef(ServerActiveObject *object):
1028 //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
1031 ObjectRef::~ObjectRef()
1034 infostream<<"ObjectRef destructing for id="
1035 <<m_object->getId()<<std::endl;
1037 infostream<<"ObjectRef destructing for id=unknown"<<std::endl;*/
1040 // Creates an ObjectRef and leaves it on top of stack
1041 // Not callable from Lua; all references are created on the C side.
1042 void ObjectRef::create(lua_State *L, ServerActiveObject *object)
1044 ObjectRef *o = new ObjectRef(object);
1045 //infostream<<"ObjectRef::create: o="<<o<<std::endl;
1046 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1047 luaL_getmetatable(L, className);
1048 lua_setmetatable(L, -2);
1051 void ObjectRef::set_null(lua_State *L)
1053 ObjectRef *o = checkobject(L, -1);
1057 void ObjectRef::Register(lua_State *L)
1060 int methodtable = lua_gettop(L);
1061 luaL_newmetatable(L, className);
1062 int metatable = lua_gettop(L);
1064 lua_pushliteral(L, "__metatable");
1065 lua_pushvalue(L, methodtable);
1066 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
1068 lua_pushliteral(L, "__index");
1069 lua_pushvalue(L, methodtable);
1070 lua_settable(L, metatable);
1072 lua_pushliteral(L, "__gc");
1073 lua_pushcfunction(L, gc_object);
1074 lua_settable(L, metatable);
1076 lua_pop(L, 1); // drop metatable
1078 luaL_openlib(L, 0, methods, 0); // fill methodtable
1079 lua_pop(L, 1); // drop methodtable
1081 // Cannot be created from Lua
1082 //lua_register(L, className, create_object);
1085 const char ObjectRef::className[] = "ObjectRef";
1086 const luaL_reg ObjectRef::methods[] = {
1087 // ServerActiveObject
1088 luamethod(ObjectRef, remove),
1089 luamethod(ObjectRef, getpos),
1090 luamethod(ObjectRef, setpos),
1091 luamethod(ObjectRef, moveto),
1092 luamethod(ObjectRef, punch),
1093 luamethod(ObjectRef, right_click),
1094 luamethod(ObjectRef, set_hp),
1095 luamethod(ObjectRef, get_hp),
1096 luamethod(ObjectRef, get_inventory),
1097 luamethod(ObjectRef, get_wield_list),
1098 luamethod(ObjectRef, get_wield_index),
1099 luamethod(ObjectRef, get_wielded_item),
1100 luamethod(ObjectRef, set_wielded_item),
1101 luamethod(ObjectRef, set_armor_groups),
1102 luamethod(ObjectRef, set_physics_override),
1103 luamethod(ObjectRef, set_animation),
1104 luamethod(ObjectRef, set_bone_position),
1105 luamethod(ObjectRef, set_attach),
1106 luamethod(ObjectRef, set_detach),
1107 luamethod(ObjectRef, set_properties),
1108 // LuaEntitySAO-only
1109 luamethod(ObjectRef, setvelocity),
1110 luamethod(ObjectRef, getvelocity),
1111 luamethod(ObjectRef, setacceleration),
1112 luamethod(ObjectRef, getacceleration),
1113 luamethod(ObjectRef, setyaw),
1114 luamethod(ObjectRef, getyaw),
1115 luamethod(ObjectRef, settexturemod),
1116 luamethod(ObjectRef, setsprite),
1117 luamethod(ObjectRef, get_entity_name),
1118 luamethod(ObjectRef, get_luaentity),
1120 luamethod(ObjectRef, is_player),
1121 luamethod(ObjectRef, get_player_name),
1122 luamethod(ObjectRef, get_look_dir),
1123 luamethod(ObjectRef, get_look_pitch),
1124 luamethod(ObjectRef, get_look_yaw),
1125 luamethod(ObjectRef, set_look_yaw),
1126 luamethod(ObjectRef, set_look_pitch),
1127 luamethod(ObjectRef, get_breath),
1128 luamethod(ObjectRef, set_breath),
1129 luamethod(ObjectRef, set_inventory_formspec),
1130 luamethod(ObjectRef, get_inventory_formspec),
1131 luamethod(ObjectRef, get_player_control),
1132 luamethod(ObjectRef, get_player_control_bits),
1133 luamethod(ObjectRef, hud_add),
1134 luamethod(ObjectRef, hud_remove),
1135 luamethod(ObjectRef, hud_change),
1136 luamethod(ObjectRef, hud_get),
1137 luamethod(ObjectRef, hud_set_flags),
1138 luamethod(ObjectRef, hud_set_hotbar_itemcount),