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 "scriptapi.h"
21 #include "scriptapi_object.h"
24 #include "scriptapi_types.h"
25 #include "scriptapi_inventory.h"
26 #include "scriptapi_item.h"
27 #include "scriptapi_entity.h"
28 #include "scriptapi_common.h"
32 struct EnumString es_HudElementType[] =
34 {HUD_ELEM_IMAGE, "image"},
35 {HUD_ELEM_TEXT, "text"},
36 {HUD_ELEM_STATBAR, "statbar"},
37 {HUD_ELEM_INVENTORY, "inventory"},
41 struct EnumString es_HudElementStat[] =
43 {HUD_STAT_POS, "pos"},
44 {HUD_STAT_NAME, "name"},
45 {HUD_STAT_SCALE, "scale"},
46 {HUD_STAT_TEXT, "text"},
47 {HUD_STAT_NUMBER, "number"},
48 {HUD_STAT_ITEM, "item"},
49 {HUD_STAT_DIR, "direction"},
50 {HUD_STAT_ALIGN, "alignment"},
51 {HUD_STAT_OFFSET, "offset"},
55 struct EnumString es_HudBuiltinElement[] =
57 {HUD_FLAG_HOTBAR_VISIBLE, "hotbar"},
58 {HUD_FLAG_HEALTHBAR_VISIBLE, "healthbar"},
59 {HUD_FLAG_CROSSHAIR_VISIBLE, "crosshair"},
60 {HUD_FLAG_WIELDITEM_VISIBLE, "wielditem"},
70 ObjectRef* ObjectRef::checkobject(lua_State *L, int narg)
72 luaL_checktype(L, narg, LUA_TUSERDATA);
73 void *ud = luaL_checkudata(L, narg, className);
74 if(!ud) luaL_typerror(L, narg, className);
75 return *(ObjectRef**)ud; // unbox pointer
78 ServerActiveObject* ObjectRef::getobject(ObjectRef *ref)
80 ServerActiveObject *co = ref->m_object;
84 LuaEntitySAO* ObjectRef::getluaobject(ObjectRef *ref)
86 ServerActiveObject *obj = getobject(ref);
89 if(obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY)
91 return (LuaEntitySAO*)obj;
94 PlayerSAO* ObjectRef::getplayersao(ObjectRef *ref)
96 ServerActiveObject *obj = getobject(ref);
99 if(obj->getType() != ACTIVEOBJECT_TYPE_PLAYER)
101 return (PlayerSAO*)obj;
104 Player* ObjectRef::getplayer(ObjectRef *ref)
106 PlayerSAO *playersao = getplayersao(ref);
107 if(playersao == NULL)
109 return playersao->getPlayer();
112 // Exported functions
115 int ObjectRef::gc_object(lua_State *L) {
116 ObjectRef *o = *(ObjectRef **)(lua_touserdata(L, 1));
117 //infostream<<"ObjectRef::gc_object: o="<<o<<std::endl;
123 int ObjectRef::l_remove(lua_State *L)
125 ObjectRef *ref = checkobject(L, 1);
126 ServerActiveObject *co = getobject(ref);
127 if(co == NULL) return 0;
128 verbosestream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
129 co->m_removed = true;
134 // returns: {x=num, y=num, z=num}
135 int ObjectRef::l_getpos(lua_State *L)
137 ObjectRef *ref = checkobject(L, 1);
138 ServerActiveObject *co = getobject(ref);
139 if(co == NULL) return 0;
140 v3f pos = co->getBasePosition() / BS;
142 lua_pushnumber(L, pos.X);
143 lua_setfield(L, -2, "x");
144 lua_pushnumber(L, pos.Y);
145 lua_setfield(L, -2, "y");
146 lua_pushnumber(L, pos.Z);
147 lua_setfield(L, -2, "z");
152 int ObjectRef::l_setpos(lua_State *L)
154 ObjectRef *ref = checkobject(L, 1);
155 //LuaEntitySAO *co = getluaobject(ref);
156 ServerActiveObject *co = getobject(ref);
157 if(co == NULL) return 0;
159 v3f pos = checkFloatPos(L, 2);
165 // moveto(self, pos, continuous=false)
166 int ObjectRef::l_moveto(lua_State *L)
168 ObjectRef *ref = checkobject(L, 1);
169 //LuaEntitySAO *co = getluaobject(ref);
170 ServerActiveObject *co = getobject(ref);
171 if(co == NULL) return 0;
173 v3f pos = checkFloatPos(L, 2);
175 bool continuous = lua_toboolean(L, 3);
177 co->moveTo(pos, continuous);
181 // punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
182 int ObjectRef::l_punch(lua_State *L)
184 ObjectRef *ref = checkobject(L, 1);
185 ServerActiveObject *co = getobject(ref);
186 if(co == NULL) return 0;
188 ServerActiveObject *puncher = 0;
191 if (!lua_isnil(L,2)) {
192 ObjectRef *puncher_ref = checkobject(L, 2);
193 puncher = getobject(puncher_ref);
194 if(puncher == NULL) return 0;
196 dir = co->getBasePosition() - puncher->getBasePosition();
199 float time_from_last_punch = 1000000;
200 if(lua_isnumber(L, 3))
201 time_from_last_punch = lua_tonumber(L, 3);
203 ToolCapabilities toolcap = read_tool_capabilities(L, 4);
205 if(lua_type(L, 5) == LUA_TTABLE)
206 dir = read_v3f(L, 5);
210 co->punch(dir, &toolcap, puncher, time_from_last_punch);
214 // right_click(self, clicker); clicker = an another ObjectRef
215 int ObjectRef::l_right_click(lua_State *L)
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 ObjectRef *ref = checkobject(L, 1);
234 luaL_checknumber(L, 2);
235 ServerActiveObject *co = getobject(ref);
236 if(co == NULL) return 0;
237 int hp = lua_tonumber(L, 2);
238 /*infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
239 <<" hp="<<hp<<std::endl;*/
247 // returns: number of hitpoints (2 * number of hearts)
248 // 0 if not applicable to this type of object
249 int ObjectRef::l_get_hp(lua_State *L)
251 ObjectRef *ref = checkobject(L, 1);
252 ServerActiveObject *co = getobject(ref);
255 lua_pushnumber(L, 1);
258 int hp = co->getHP();
259 /*infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
260 <<" hp="<<hp<<std::endl;*/
262 lua_pushnumber(L, hp);
266 // get_inventory(self)
267 int ObjectRef::l_get_inventory(lua_State *L)
269 ObjectRef *ref = checkobject(L, 1);
270 ServerActiveObject *co = getobject(ref);
271 if(co == NULL) return 0;
273 InventoryLocation loc = co->getInventoryLocation();
274 if(get_server(L)->getInventory(loc) != NULL)
275 InvRef::create(L, loc);
277 lua_pushnil(L); // An object may have no inventory (nil)
281 // get_wield_list(self)
282 int ObjectRef::l_get_wield_list(lua_State *L)
284 ObjectRef *ref = checkobject(L, 1);
285 ServerActiveObject *co = getobject(ref);
286 if(co == NULL) return 0;
288 lua_pushstring(L, co->getWieldList().c_str());
292 // get_wield_index(self)
293 int ObjectRef::l_get_wield_index(lua_State *L)
295 ObjectRef *ref = checkobject(L, 1);
296 ServerActiveObject *co = getobject(ref);
297 if(co == NULL) return 0;
299 lua_pushinteger(L, co->getWieldIndex() + 1);
303 // get_wielded_item(self)
304 int ObjectRef::l_get_wielded_item(lua_State *L)
306 ObjectRef *ref = checkobject(L, 1);
307 ServerActiveObject *co = getobject(ref);
310 LuaItemStack::create(L, ItemStack());
314 LuaItemStack::create(L, co->getWieldedItem());
318 // set_wielded_item(self, itemstack or itemstring or table or nil)
319 int ObjectRef::l_set_wielded_item(lua_State *L)
321 ObjectRef *ref = checkobject(L, 1);
322 ServerActiveObject *co = getobject(ref);
323 if(co == NULL) return 0;
325 ItemStack item = read_item(L, 2);
326 bool success = co->setWieldedItem(item);
327 lua_pushboolean(L, success);
331 // set_armor_groups(self, groups)
332 int ObjectRef::l_set_armor_groups(lua_State *L)
334 ObjectRef *ref = checkobject(L, 1);
335 ServerActiveObject *co = getobject(ref);
336 if(co == NULL) return 0;
338 ItemGroupList groups;
339 read_groups(L, 2, groups);
340 co->setArmorGroups(groups);
344 // set_physics_override(self, physics_override_speed, physics_override_jump, physics_override_gravity)
345 int ObjectRef::l_set_physics_override(lua_State *L)
347 ObjectRef *ref = checkobject(L, 1);
348 PlayerSAO *co = (PlayerSAO *) getobject(ref);
349 if(co == NULL) return 0;
351 if(!lua_isnil(L, 2)){
352 co->m_physics_override_speed = lua_tonumber(L, 2);
353 co->m_physics_override_sent = false;
355 if(!lua_isnil(L, 3)){
356 co->m_physics_override_jump = lua_tonumber(L, 3);
357 co->m_physics_override_sent = false;
359 if(!lua_isnil(L, 4)){
360 co->m_physics_override_gravity = lua_tonumber(L, 4);
361 co->m_physics_override_sent = false;
366 // set_animation(self, frame_range, frame_speed, frame_blend)
367 int ObjectRef::l_set_animation(lua_State *L)
369 ObjectRef *ref = checkobject(L, 1);
370 ServerActiveObject *co = getobject(ref);
371 if(co == NULL) return 0;
373 v2f frames = v2f(1, 1);
375 frames = read_v2f(L, 2);
376 float frame_speed = 15;
378 frame_speed = lua_tonumber(L, 3);
379 float frame_blend = 0;
381 frame_blend = lua_tonumber(L, 4);
382 co->setAnimation(frames, frame_speed, frame_blend);
386 // set_bone_position(self, std::string bone, v3f position, v3f rotation)
387 int ObjectRef::l_set_bone_position(lua_State *L)
389 ObjectRef *ref = checkobject(L, 1);
390 ServerActiveObject *co = getobject(ref);
391 if(co == NULL) return 0;
393 std::string bone = "";
395 bone = lua_tostring(L, 2);
396 v3f position = v3f(0, 0, 0);
398 position = read_v3f(L, 3);
399 v3f rotation = v3f(0, 0, 0);
401 rotation = read_v3f(L, 4);
402 co->setBonePosition(bone, position, rotation);
406 // set_attach(self, parent, bone, position, rotation)
407 int ObjectRef::l_set_attach(lua_State *L)
409 ObjectRef *ref = checkobject(L, 1);
410 ObjectRef *parent_ref = checkobject(L, 2);
411 ServerActiveObject *co = getobject(ref);
412 ServerActiveObject *parent = getobject(parent_ref);
413 if(co == NULL) return 0;
414 if(parent == NULL) return 0;
416 std::string bone = "";
418 bone = lua_tostring(L, 3);
419 v3f position = v3f(0, 0, 0);
421 position = read_v3f(L, 4);
422 v3f rotation = v3f(0, 0, 0);
424 rotation = read_v3f(L, 5);
425 co->setAttachment(parent->getId(), bone, position, rotation);
430 int ObjectRef::l_set_detach(lua_State *L)
432 ObjectRef *ref = checkobject(L, 1);
433 ServerActiveObject *co = getobject(ref);
434 if(co == NULL) return 0;
436 co->setAttachment(0, "", v3f(0,0,0), v3f(0,0,0));
440 // set_properties(self, properties)
441 int ObjectRef::l_set_properties(lua_State *L)
443 ObjectRef *ref = checkobject(L, 1);
444 ServerActiveObject *co = getobject(ref);
445 if(co == NULL) return 0;
446 ObjectProperties *prop = co->accessObjectProperties();
449 read_object_properties(L, 2, prop);
450 co->notifyObjectPropertiesModified();
454 /* LuaEntitySAO-only */
456 // setvelocity(self, {x=num, y=num, z=num})
457 int ObjectRef::l_setvelocity(lua_State *L)
459 ObjectRef *ref = checkobject(L, 1);
460 LuaEntitySAO *co = getluaobject(ref);
461 if(co == NULL) return 0;
462 v3f pos = checkFloatPos(L, 2);
464 co->setVelocity(pos);
469 int ObjectRef::l_getvelocity(lua_State *L)
471 ObjectRef *ref = checkobject(L, 1);
472 LuaEntitySAO *co = getluaobject(ref);
473 if(co == NULL) return 0;
475 v3f v = co->getVelocity();
480 // setacceleration(self, {x=num, y=num, z=num})
481 int ObjectRef::l_setacceleration(lua_State *L)
483 ObjectRef *ref = checkobject(L, 1);
484 LuaEntitySAO *co = getluaobject(ref);
485 if(co == NULL) return 0;
487 v3f pos = checkFloatPos(L, 2);
489 co->setAcceleration(pos);
493 // getacceleration(self)
494 int ObjectRef::l_getacceleration(lua_State *L)
496 ObjectRef *ref = checkobject(L, 1);
497 LuaEntitySAO *co = getluaobject(ref);
498 if(co == NULL) return 0;
500 v3f v = co->getAcceleration();
505 // setyaw(self, radians)
506 int ObjectRef::l_setyaw(lua_State *L)
508 ObjectRef *ref = checkobject(L, 1);
509 LuaEntitySAO *co = getluaobject(ref);
510 if(co == NULL) return 0;
511 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
518 int ObjectRef::l_getyaw(lua_State *L)
520 ObjectRef *ref = checkobject(L, 1);
521 LuaEntitySAO *co = getluaobject(ref);
522 if(co == NULL) return 0;
524 float yaw = co->getYaw() * core::DEGTORAD;
525 lua_pushnumber(L, yaw);
529 // settexturemod(self, mod)
530 int ObjectRef::l_settexturemod(lua_State *L)
532 ObjectRef *ref = checkobject(L, 1);
533 LuaEntitySAO *co = getluaobject(ref);
534 if(co == NULL) return 0;
536 std::string mod = luaL_checkstring(L, 2);
537 co->setTextureMod(mod);
541 // setsprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
542 // select_horiz_by_yawpitch=false)
543 int ObjectRef::l_setsprite(lua_State *L)
545 ObjectRef *ref = checkobject(L, 1);
546 LuaEntitySAO *co = getluaobject(ref);
547 if(co == NULL) return 0;
551 p = read_v2s16(L, 2);
554 num_frames = lua_tonumber(L, 3);
555 float framelength = 0.2;
557 framelength = lua_tonumber(L, 4);
558 bool select_horiz_by_yawpitch = false;
560 select_horiz_by_yawpitch = lua_toboolean(L, 5);
561 co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
566 // get_entity_name(self)
567 int ObjectRef::l_get_entity_name(lua_State *L)
569 ObjectRef *ref = checkobject(L, 1);
570 LuaEntitySAO *co = getluaobject(ref);
571 if(co == NULL) return 0;
573 std::string name = co->getName();
574 lua_pushstring(L, name.c_str());
578 // get_luaentity(self)
579 int ObjectRef::l_get_luaentity(lua_State *L)
581 ObjectRef *ref = checkobject(L, 1);
582 LuaEntitySAO *co = getluaobject(ref);
583 if(co == NULL) return 0;
585 luaentity_get(L, co->getId());
592 int ObjectRef::l_is_player(lua_State *L)
594 ObjectRef *ref = checkobject(L, 1);
595 Player *player = getplayer(ref);
596 lua_pushboolean(L, (player != NULL));
600 // get_player_name(self)
601 int ObjectRef::l_get_player_name(lua_State *L)
603 ObjectRef *ref = checkobject(L, 1);
604 Player *player = getplayer(ref);
606 lua_pushlstring(L, "", 0);
610 lua_pushstring(L, player->getName());
614 // get_look_dir(self)
615 int ObjectRef::l_get_look_dir(lua_State *L)
617 ObjectRef *ref = checkobject(L, 1);
618 Player *player = getplayer(ref);
619 if(player == NULL) return 0;
621 float pitch = player->getRadPitch();
622 float yaw = player->getRadYaw();
623 v3f v(cos(pitch)*cos(yaw), sin(pitch), cos(pitch)*sin(yaw));
628 // get_look_pitch(self)
629 int ObjectRef::l_get_look_pitch(lua_State *L)
631 ObjectRef *ref = checkobject(L, 1);
632 Player *player = getplayer(ref);
633 if(player == NULL) return 0;
635 lua_pushnumber(L, player->getRadPitch());
639 // get_look_yaw(self)
640 int ObjectRef::l_get_look_yaw(lua_State *L)
642 ObjectRef *ref = checkobject(L, 1);
643 Player *player = getplayer(ref);
644 if(player == NULL) return 0;
646 lua_pushnumber(L, player->getRadYaw());
650 // set_look_pitch(self, radians)
651 int ObjectRef::l_set_look_pitch(lua_State *L)
653 ObjectRef *ref = checkobject(L, 1);
654 PlayerSAO* co = getplayersao(ref);
655 if(co == NULL) return 0;
656 float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
662 // set_look_yaw(self, radians)
663 int ObjectRef::l_set_look_yaw(lua_State *L)
665 ObjectRef *ref = checkobject(L, 1);
666 PlayerSAO* co = getplayersao(ref);
667 if(co == NULL) return 0;
668 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
674 // set_inventory_formspec(self, formspec)
675 int ObjectRef::l_set_inventory_formspec(lua_State *L)
677 ObjectRef *ref = checkobject(L, 1);
678 Player *player = getplayer(ref);
679 if(player == NULL) return 0;
680 std::string formspec = luaL_checkstring(L, 2);
682 player->inventory_formspec = formspec;
683 get_server(L)->reportInventoryFormspecModified(player->getName());
684 lua_pushboolean(L, true);
688 // get_inventory_formspec(self) -> formspec
689 int ObjectRef::l_get_inventory_formspec(lua_State *L)
691 ObjectRef *ref = checkobject(L, 1);
692 Player *player = getplayer(ref);
693 if(player == NULL) return 0;
695 std::string formspec = player->inventory_formspec;
696 lua_pushlstring(L, formspec.c_str(), formspec.size());
700 // get_player_control(self)
701 int ObjectRef::l_get_player_control(lua_State *L)
703 ObjectRef *ref = checkobject(L, 1);
704 Player *player = getplayer(ref);
706 lua_pushlstring(L, "", 0);
710 PlayerControl control = player->getPlayerControl();
712 lua_pushboolean(L, control.up);
713 lua_setfield(L, -2, "up");
714 lua_pushboolean(L, control.down);
715 lua_setfield(L, -2, "down");
716 lua_pushboolean(L, control.left);
717 lua_setfield(L, -2, "left");
718 lua_pushboolean(L, control.right);
719 lua_setfield(L, -2, "right");
720 lua_pushboolean(L, control.jump);
721 lua_setfield(L, -2, "jump");
722 lua_pushboolean(L, control.aux1);
723 lua_setfield(L, -2, "aux1");
724 lua_pushboolean(L, control.sneak);
725 lua_setfield(L, -2, "sneak");
726 lua_pushboolean(L, control.LMB);
727 lua_setfield(L, -2, "LMB");
728 lua_pushboolean(L, control.RMB);
729 lua_setfield(L, -2, "RMB");
733 // get_player_control_bits(self)
734 int ObjectRef::l_get_player_control_bits(lua_State *L)
736 ObjectRef *ref = checkobject(L, 1);
737 Player *player = getplayer(ref);
739 lua_pushlstring(L, "", 0);
743 lua_pushnumber(L, player->keyPressed);
747 // hud_add(self, form)
748 int ObjectRef::l_hud_add(lua_State *L)
750 ObjectRef *ref = checkobject(L, 1);
751 Player *player = getplayer(ref);
755 HudElement *elem = new HudElement;
757 elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type",
758 es_HudElementType, HUD_ELEM_TEXT);
760 lua_getfield(L, 2, "position");
761 elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
764 lua_getfield(L, 2, "scale");
765 elem->scale = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
768 elem->name = getstringfield_default(L, 2, "name", "");
769 elem->text = getstringfield_default(L, 2, "text", "");
770 elem->number = getintfield_default(L, 2, "number", 0);
771 elem->item = getintfield_default(L, 2, "item", 0);
772 elem->dir = getintfield_default(L, 2, "direction", 0);
774 lua_getfield(L, 2, "alignment");
775 elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
778 lua_getfield(L, 2, "offset");
779 elem->offset = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
782 u32 id = get_server(L)->hudAdd(player, elem);
788 lua_pushnumber(L, id);
792 // hud_remove(self, id)
793 int ObjectRef::l_hud_remove(lua_State *L)
795 ObjectRef *ref = checkobject(L, 1);
796 Player *player = getplayer(ref);
801 if (!lua_isnil(L, 2))
802 id = lua_tonumber(L, 2);
804 if (!get_server(L)->hudRemove(player, id))
807 lua_pushboolean(L, true);
811 // hud_change(self, id, stat, data)
812 int ObjectRef::l_hud_change(lua_State *L)
814 ObjectRef *ref = checkobject(L, 1);
815 Player *player = getplayer(ref);
819 u32 id = !lua_isnil(L, 2) ? lua_tonumber(L, 2) : -1;
820 if (id >= player->hud.size())
823 HudElementStat stat = HUD_STAT_NUMBER;
824 if (!lua_isnil(L, 3)) {
826 std::string statstr = lua_tostring(L, 3);
827 stat = string_to_enum(es_HudElementStat, statint, statstr) ?
828 (HudElementStat)statint : HUD_STAT_NUMBER;
832 HudElement *e = player->hud[id];
838 e->pos = read_v2f(L, 4);
842 e->name = lua_tostring(L, 4);
846 e->scale = read_v2f(L, 4);
850 e->text = lua_tostring(L, 4);
853 case HUD_STAT_NUMBER:
854 e->number = lua_tonumber(L, 4);
858 e->item = lua_tonumber(L, 4);
862 e->dir = lua_tonumber(L, 4);
865 e->align = read_v2f(L, 4);
867 case HUD_STAT_OFFSET:
868 e->offset = read_v2f(L, 4);
872 get_server(L)->hudChange(player, id, stat, value);
874 lua_pushboolean(L, true);
879 int ObjectRef::l_hud_get(lua_State *L)
881 ObjectRef *ref = checkobject(L, 1);
882 Player *player = getplayer(ref);
886 u32 id = lua_tonumber(L, -1);
887 if (id >= player->hud.size())
890 HudElement *e = player->hud[id];
896 lua_pushstring(L, es_HudElementType[(u8)e->type].str);
897 lua_setfield(L, -2, "type");
900 lua_setfield(L, -2, "position");
902 lua_pushstring(L, e->name.c_str());
903 lua_setfield(L, -2, "name");
905 push_v2f(L, e->scale);
906 lua_setfield(L, -2, "scale");
908 lua_pushstring(L, e->text.c_str());
909 lua_setfield(L, -2, "text");
911 lua_pushnumber(L, e->number);
912 lua_setfield(L, -2, "number");
914 lua_pushnumber(L, e->item);
915 lua_setfield(L, -2, "item");
917 lua_pushnumber(L, e->dir);
918 lua_setfield(L, -2, "dir");
923 // hud_set_flags(self, flags)
924 int ObjectRef::l_hud_set_flags(lua_State *L)
926 ObjectRef *ref = checkobject(L, 1);
927 Player *player = getplayer(ref);
935 const EnumString *esp = es_HudBuiltinElement;
936 for (int i = 0; esp[i].str; i++) {
937 if (getboolfield(L, 2, esp[i].str, flag)) {
938 flags |= esp[i].num * flag;
942 if (!get_server(L)->hudSetFlags(player, flags, mask))
945 lua_pushboolean(L, true);
949 ObjectRef::ObjectRef(ServerActiveObject *object):
952 //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
955 ObjectRef::~ObjectRef()
958 infostream<<"ObjectRef destructing for id="
959 <<m_object->getId()<<std::endl;
961 infostream<<"ObjectRef destructing for id=unknown"<<std::endl;*/
964 // Creates an ObjectRef and leaves it on top of stack
965 // Not callable from Lua; all references are created on the C side.
966 void ObjectRef::create(lua_State *L, ServerActiveObject *object)
968 ObjectRef *o = new ObjectRef(object);
969 //infostream<<"ObjectRef::create: o="<<o<<std::endl;
970 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
971 luaL_getmetatable(L, className);
972 lua_setmetatable(L, -2);
975 void ObjectRef::set_null(lua_State *L)
977 ObjectRef *o = checkobject(L, -1);
981 void ObjectRef::Register(lua_State *L)
984 int methodtable = lua_gettop(L);
985 luaL_newmetatable(L, className);
986 int metatable = lua_gettop(L);
988 lua_pushliteral(L, "__metatable");
989 lua_pushvalue(L, methodtable);
990 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
992 lua_pushliteral(L, "__index");
993 lua_pushvalue(L, methodtable);
994 lua_settable(L, metatable);
996 lua_pushliteral(L, "__gc");
997 lua_pushcfunction(L, gc_object);
998 lua_settable(L, metatable);
1000 lua_pop(L, 1); // drop metatable
1002 luaL_openlib(L, 0, methods, 0); // fill methodtable
1003 lua_pop(L, 1); // drop methodtable
1005 // Cannot be created from Lua
1006 //lua_register(L, className, create_object);
1009 const char ObjectRef::className[] = "ObjectRef";
1010 const luaL_reg ObjectRef::methods[] = {
1011 // ServerActiveObject
1012 luamethod(ObjectRef, remove),
1013 luamethod(ObjectRef, getpos),
1014 luamethod(ObjectRef, setpos),
1015 luamethod(ObjectRef, moveto),
1016 luamethod(ObjectRef, punch),
1017 luamethod(ObjectRef, right_click),
1018 luamethod(ObjectRef, set_hp),
1019 luamethod(ObjectRef, get_hp),
1020 luamethod(ObjectRef, get_inventory),
1021 luamethod(ObjectRef, get_wield_list),
1022 luamethod(ObjectRef, get_wield_index),
1023 luamethod(ObjectRef, get_wielded_item),
1024 luamethod(ObjectRef, set_wielded_item),
1025 luamethod(ObjectRef, set_armor_groups),
1026 luamethod(ObjectRef, set_physics_override),
1027 luamethod(ObjectRef, set_animation),
1028 luamethod(ObjectRef, set_bone_position),
1029 luamethod(ObjectRef, set_attach),
1030 luamethod(ObjectRef, set_detach),
1031 luamethod(ObjectRef, set_properties),
1032 // LuaEntitySAO-only
1033 luamethod(ObjectRef, setvelocity),
1034 luamethod(ObjectRef, getvelocity),
1035 luamethod(ObjectRef, setacceleration),
1036 luamethod(ObjectRef, getacceleration),
1037 luamethod(ObjectRef, setyaw),
1038 luamethod(ObjectRef, getyaw),
1039 luamethod(ObjectRef, settexturemod),
1040 luamethod(ObjectRef, setsprite),
1041 luamethod(ObjectRef, get_entity_name),
1042 luamethod(ObjectRef, get_luaentity),
1044 luamethod(ObjectRef, is_player),
1045 luamethod(ObjectRef, get_player_name),
1046 luamethod(ObjectRef, get_look_dir),
1047 luamethod(ObjectRef, get_look_pitch),
1048 luamethod(ObjectRef, get_look_yaw),
1049 luamethod(ObjectRef, set_look_yaw),
1050 luamethod(ObjectRef, set_look_pitch),
1051 luamethod(ObjectRef, set_inventory_formspec),
1052 luamethod(ObjectRef, get_inventory_formspec),
1053 luamethod(ObjectRef, get_player_control),
1054 luamethod(ObjectRef, get_player_control_bits),
1055 luamethod(ObjectRef, hud_add),
1056 luamethod(ObjectRef, hud_remove),
1057 luamethod(ObjectRef, hud_change),
1058 luamethod(ObjectRef, hud_get),
1059 luamethod(ObjectRef, hud_set_flags),
1063 // Creates a new anonymous reference if cobj=NULL or id=0
1064 void objectref_get_or_create(lua_State *L,
1065 ServerActiveObject *cobj)
1067 if(cobj == NULL || cobj->getId() == 0){
1068 ObjectRef::create(L, cobj);
1070 objectref_get(L, cobj->getId());
1074 void objectref_get(lua_State *L, u16 id)
1076 // Get minetest.object_refs[i]
1077 lua_getglobal(L, "minetest");
1078 lua_getfield(L, -1, "object_refs");
1079 luaL_checktype(L, -1, LUA_TTABLE);
1080 lua_pushnumber(L, id);
1081 lua_gettable(L, -2);
1082 lua_remove(L, -2); // object_refs
1083 lua_remove(L, -2); // minetest
1090 void read_object_properties(lua_State *L, int index,
1091 ObjectProperties *prop)
1094 index = lua_gettop(L) + 1 + index;
1095 if(!lua_istable(L, index))
1098 prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
1100 getboolfield(L, -1, "physical", prop->physical);
1102 getfloatfield(L, -1, "weight", prop->weight);
1104 lua_getfield(L, -1, "collisionbox");
1105 if(lua_istable(L, -1))
1106 prop->collisionbox = read_aabb3f(L, -1, 1.0);
1109 getstringfield(L, -1, "visual", prop->visual);
1111 getstringfield(L, -1, "mesh", prop->mesh);
1113 lua_getfield(L, -1, "visual_size");
1114 if(lua_istable(L, -1))
1115 prop->visual_size = read_v2f(L, -1);
1118 lua_getfield(L, -1, "textures");
1119 if(lua_istable(L, -1)){
1120 prop->textures.clear();
1121 int table = lua_gettop(L);
1123 while(lua_next(L, table) != 0){
1124 // key at index -2 and value at index -1
1125 if(lua_isstring(L, -1))
1126 prop->textures.push_back(lua_tostring(L, -1));
1128 prop->textures.push_back("");
1129 // removes value, keeps key for next iteration
1135 lua_getfield(L, -1, "colors");
1136 if(lua_istable(L, -1)){
1137 prop->colors.clear();
1138 int table = lua_gettop(L);
1140 while(lua_next(L, table) != 0){
1141 // key at index -2 and value at index -1
1142 if(lua_isstring(L, -1))
1143 prop->colors.push_back(readARGB8(L, -1));
1145 prop->colors.push_back(video::SColor(255, 255, 255, 255));
1146 // removes value, keeps key for next iteration
1152 lua_getfield(L, -1, "spritediv");
1153 if(lua_istable(L, -1))
1154 prop->spritediv = read_v2s16(L, -1);
1157 lua_getfield(L, -1, "initial_sprite_basepos");
1158 if(lua_istable(L, -1))
1159 prop->initial_sprite_basepos = read_v2s16(L, -1);
1162 getboolfield(L, -1, "is_visible", prop->is_visible);
1163 getboolfield(L, -1, "makes_footstep_sound", prop->makes_footstep_sound);
1164 getfloatfield(L, -1, "automatic_rotate", prop->automatic_rotate);
1171 void scriptapi_add_object_reference(lua_State *L, ServerActiveObject *cobj)
1174 assert(lua_checkstack(L, 20));
1175 //infostream<<"scriptapi_add_object_reference: id="<<cobj->getId()<<std::endl;
1176 StackUnroller stack_unroller(L);
1178 // Create object on stack
1179 ObjectRef::create(L, cobj); // Puts ObjectRef (as userdata) on stack
1180 int object = lua_gettop(L);
1182 // Get minetest.object_refs table
1183 lua_getglobal(L, "minetest");
1184 lua_getfield(L, -1, "object_refs");
1185 luaL_checktype(L, -1, LUA_TTABLE);
1186 int objectstable = lua_gettop(L);
1188 // object_refs[id] = object
1189 lua_pushnumber(L, cobj->getId()); // Push id
1190 lua_pushvalue(L, object); // Copy object to top of stack
1191 lua_settable(L, objectstable);
1194 void scriptapi_rm_object_reference(lua_State *L, ServerActiveObject *cobj)
1197 assert(lua_checkstack(L, 20));
1198 //infostream<<"scriptapi_rm_object_reference: id="<<cobj->getId()<<std::endl;
1199 StackUnroller stack_unroller(L);
1201 // Get minetest.object_refs table
1202 lua_getglobal(L, "minetest");
1203 lua_getfield(L, -1, "object_refs");
1204 luaL_checktype(L, -1, LUA_TTABLE);
1205 int objectstable = lua_gettop(L);
1207 // Get object_refs[id]
1208 lua_pushnumber(L, cobj->getId()); // Push id
1209 lua_gettable(L, objectstable);
1210 // Set object reference to NULL
1211 ObjectRef::set_null(L);
1212 lua_pop(L, 1); // pop object
1214 // Set object_refs[id] = nil
1215 lua_pushnumber(L, cobj->getId()); // Push id
1217 lua_settable(L, objectstable);