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"
36 ObjectRef* ObjectRef::checkobject(lua_State *L, int narg)
38 luaL_checktype(L, narg, LUA_TUSERDATA);
39 void *ud = luaL_checkudata(L, narg, className);
40 if(!ud) luaL_typerror(L, narg, className);
41 return *(ObjectRef**)ud; // unbox pointer
44 ServerActiveObject* ObjectRef::getobject(ObjectRef *ref)
46 ServerActiveObject *co = ref->m_object;
50 LuaEntitySAO* ObjectRef::getluaobject(ObjectRef *ref)
52 ServerActiveObject *obj = getobject(ref);
55 if(obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY)
57 return (LuaEntitySAO*)obj;
60 PlayerSAO* ObjectRef::getplayersao(ObjectRef *ref)
62 ServerActiveObject *obj = getobject(ref);
65 if(obj->getType() != ACTIVEOBJECT_TYPE_PLAYER)
67 return (PlayerSAO*)obj;
70 Player* ObjectRef::getplayer(ObjectRef *ref)
72 PlayerSAO *playersao = getplayersao(ref);
75 return playersao->getPlayer();
81 int ObjectRef::gc_object(lua_State *L) {
82 ObjectRef *o = *(ObjectRef **)(lua_touserdata(L, 1));
83 //infostream<<"ObjectRef::gc_object: o="<<o<<std::endl;
89 int ObjectRef::l_remove(lua_State *L)
91 ObjectRef *ref = checkobject(L, 1);
92 ServerActiveObject *co = getobject(ref);
93 if(co == NULL) return 0;
94 verbosestream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
100 // returns: {x=num, y=num, z=num}
101 int ObjectRef::l_getpos(lua_State *L)
103 ObjectRef *ref = checkobject(L, 1);
104 ServerActiveObject *co = getobject(ref);
105 if(co == NULL) return 0;
106 v3f pos = co->getBasePosition() / BS;
108 lua_pushnumber(L, pos.X);
109 lua_setfield(L, -2, "x");
110 lua_pushnumber(L, pos.Y);
111 lua_setfield(L, -2, "y");
112 lua_pushnumber(L, pos.Z);
113 lua_setfield(L, -2, "z");
118 int ObjectRef::l_setpos(lua_State *L)
120 ObjectRef *ref = checkobject(L, 1);
121 //LuaEntitySAO *co = getluaobject(ref);
122 ServerActiveObject *co = getobject(ref);
123 if(co == NULL) return 0;
125 v3f pos = checkFloatPos(L, 2);
131 // moveto(self, pos, continuous=false)
132 int ObjectRef::l_moveto(lua_State *L)
134 ObjectRef *ref = checkobject(L, 1);
135 //LuaEntitySAO *co = getluaobject(ref);
136 ServerActiveObject *co = getobject(ref);
137 if(co == NULL) return 0;
139 v3f pos = checkFloatPos(L, 2);
141 bool continuous = lua_toboolean(L, 3);
143 co->moveTo(pos, continuous);
147 // punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
148 int ObjectRef::l_punch(lua_State *L)
150 ObjectRef *ref = checkobject(L, 1);
151 ObjectRef *puncher_ref = checkobject(L, 2);
152 ServerActiveObject *co = getobject(ref);
153 ServerActiveObject *puncher = getobject(puncher_ref);
154 if(co == NULL) return 0;
155 if(puncher == NULL) return 0;
157 if(lua_type(L, 5) != LUA_TTABLE)
158 dir = co->getBasePosition() - puncher->getBasePosition();
160 dir = read_v3f(L, 5);
161 float time_from_last_punch = 1000000;
162 if(lua_isnumber(L, 3))
163 time_from_last_punch = lua_tonumber(L, 3);
164 ToolCapabilities toolcap = read_tool_capabilities(L, 4);
167 co->punch(dir, &toolcap, puncher, time_from_last_punch);
171 // right_click(self, clicker); clicker = an another ObjectRef
172 int ObjectRef::l_right_click(lua_State *L)
174 ObjectRef *ref = checkobject(L, 1);
175 ObjectRef *ref2 = checkobject(L, 2);
176 ServerActiveObject *co = getobject(ref);
177 ServerActiveObject *co2 = getobject(ref2);
178 if(co == NULL) return 0;
179 if(co2 == NULL) return 0;
186 // hp = number of hitpoints (2 * number of hearts)
188 int ObjectRef::l_set_hp(lua_State *L)
190 ObjectRef *ref = checkobject(L, 1);
191 luaL_checknumber(L, 2);
192 ServerActiveObject *co = getobject(ref);
193 if(co == NULL) return 0;
194 int hp = lua_tonumber(L, 2);
195 /*infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
196 <<" hp="<<hp<<std::endl;*/
204 // returns: number of hitpoints (2 * number of hearts)
205 // 0 if not applicable to this type of object
206 int ObjectRef::l_get_hp(lua_State *L)
208 ObjectRef *ref = checkobject(L, 1);
209 ServerActiveObject *co = getobject(ref);
212 lua_pushnumber(L, 1);
215 int hp = co->getHP();
216 /*infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
217 <<" hp="<<hp<<std::endl;*/
219 lua_pushnumber(L, hp);
223 // get_inventory(self)
224 int ObjectRef::l_get_inventory(lua_State *L)
226 ObjectRef *ref = checkobject(L, 1);
227 ServerActiveObject *co = getobject(ref);
228 if(co == NULL) return 0;
230 InventoryLocation loc = co->getInventoryLocation();
231 if(get_server(L)->getInventory(loc) != NULL)
232 InvRef::create(L, loc);
234 lua_pushnil(L); // An object may have no inventory (nil)
238 // get_wield_list(self)
239 int ObjectRef::l_get_wield_list(lua_State *L)
241 ObjectRef *ref = checkobject(L, 1);
242 ServerActiveObject *co = getobject(ref);
243 if(co == NULL) return 0;
245 lua_pushstring(L, co->getWieldList().c_str());
249 // get_wield_index(self)
250 int ObjectRef::l_get_wield_index(lua_State *L)
252 ObjectRef *ref = checkobject(L, 1);
253 ServerActiveObject *co = getobject(ref);
254 if(co == NULL) return 0;
256 lua_pushinteger(L, co->getWieldIndex() + 1);
260 // get_wielded_item(self)
261 int ObjectRef::l_get_wielded_item(lua_State *L)
263 ObjectRef *ref = checkobject(L, 1);
264 ServerActiveObject *co = getobject(ref);
267 LuaItemStack::create(L, ItemStack());
271 LuaItemStack::create(L, co->getWieldedItem());
275 // set_wielded_item(self, itemstack or itemstring or table or nil)
276 int ObjectRef::l_set_wielded_item(lua_State *L)
278 ObjectRef *ref = checkobject(L, 1);
279 ServerActiveObject *co = getobject(ref);
280 if(co == NULL) return 0;
282 ItemStack item = read_item(L, 2);
283 bool success = co->setWieldedItem(item);
284 lua_pushboolean(L, success);
288 // set_armor_groups(self, groups)
289 int ObjectRef::l_set_armor_groups(lua_State *L)
291 ObjectRef *ref = checkobject(L, 1);
292 ServerActiveObject *co = getobject(ref);
293 if(co == NULL) return 0;
295 ItemGroupList groups;
296 read_groups(L, 2, groups);
297 co->setArmorGroups(groups);
301 // set_animation(self, frame_range, frame_speed, frame_blend)
302 int ObjectRef::l_set_animation(lua_State *L)
304 ObjectRef *ref = checkobject(L, 1);
305 ServerActiveObject *co = getobject(ref);
306 if(co == NULL) return 0;
308 v2f frames = v2f(1, 1);
310 frames = read_v2f(L, 2);
311 float frame_speed = 15;
313 frame_speed = lua_tonumber(L, 3);
314 float frame_blend = 0;
316 frame_blend = lua_tonumber(L, 4);
317 co->setAnimation(frames, frame_speed, frame_blend);
321 // set_bone_position(self, std::string bone, v3f position, v3f rotation)
322 int ObjectRef::l_set_bone_position(lua_State *L)
324 ObjectRef *ref = checkobject(L, 1);
325 ServerActiveObject *co = getobject(ref);
326 if(co == NULL) return 0;
328 std::string bone = "";
330 bone = lua_tostring(L, 2);
331 v3f position = v3f(0, 0, 0);
333 position = read_v3f(L, 3);
334 v3f rotation = v3f(0, 0, 0);
336 rotation = read_v3f(L, 4);
337 co->setBonePosition(bone, position, rotation);
341 // set_attach(self, parent, bone, position, rotation)
342 int ObjectRef::l_set_attach(lua_State *L)
344 ObjectRef *ref = checkobject(L, 1);
345 ObjectRef *parent_ref = checkobject(L, 2);
346 ServerActiveObject *co = getobject(ref);
347 ServerActiveObject *parent = getobject(parent_ref);
348 if(co == NULL) return 0;
349 if(parent == NULL) return 0;
351 std::string bone = "";
353 bone = lua_tostring(L, 3);
354 v3f position = v3f(0, 0, 0);
356 position = read_v3f(L, 4);
357 v3f rotation = v3f(0, 0, 0);
359 rotation = read_v3f(L, 5);
360 co->setAttachment(parent->getId(), bone, position, rotation);
365 int ObjectRef::l_set_detach(lua_State *L)
367 ObjectRef *ref = checkobject(L, 1);
368 ServerActiveObject *co = getobject(ref);
369 if(co == NULL) return 0;
371 co->setAttachment(0, "", v3f(0,0,0), v3f(0,0,0));
375 // set_properties(self, properties)
376 int ObjectRef::l_set_properties(lua_State *L)
378 ObjectRef *ref = checkobject(L, 1);
379 ServerActiveObject *co = getobject(ref);
380 if(co == NULL) return 0;
381 ObjectProperties *prop = co->accessObjectProperties();
384 read_object_properties(L, 2, prop);
385 co->notifyObjectPropertiesModified();
389 /* LuaEntitySAO-only */
391 // setvelocity(self, {x=num, y=num, z=num})
392 int ObjectRef::l_setvelocity(lua_State *L)
394 ObjectRef *ref = checkobject(L, 1);
395 LuaEntitySAO *co = getluaobject(ref);
396 if(co == NULL) return 0;
397 v3f pos = checkFloatPos(L, 2);
399 co->setVelocity(pos);
404 int ObjectRef::l_getvelocity(lua_State *L)
406 ObjectRef *ref = checkobject(L, 1);
407 LuaEntitySAO *co = getluaobject(ref);
408 if(co == NULL) return 0;
410 v3f v = co->getVelocity();
415 // setacceleration(self, {x=num, y=num, z=num})
416 int ObjectRef::l_setacceleration(lua_State *L)
418 ObjectRef *ref = checkobject(L, 1);
419 LuaEntitySAO *co = getluaobject(ref);
420 if(co == NULL) return 0;
422 v3f pos = checkFloatPos(L, 2);
424 co->setAcceleration(pos);
428 // getacceleration(self)
429 int ObjectRef::l_getacceleration(lua_State *L)
431 ObjectRef *ref = checkobject(L, 1);
432 LuaEntitySAO *co = getluaobject(ref);
433 if(co == NULL) return 0;
435 v3f v = co->getAcceleration();
440 // setyaw(self, radians)
441 int ObjectRef::l_setyaw(lua_State *L)
443 ObjectRef *ref = checkobject(L, 1);
444 LuaEntitySAO *co = getluaobject(ref);
445 if(co == NULL) return 0;
446 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
453 int ObjectRef::l_getyaw(lua_State *L)
455 ObjectRef *ref = checkobject(L, 1);
456 LuaEntitySAO *co = getluaobject(ref);
457 if(co == NULL) return 0;
459 float yaw = co->getYaw() * core::DEGTORAD;
460 lua_pushnumber(L, yaw);
464 // settexturemod(self, mod)
465 int ObjectRef::l_settexturemod(lua_State *L)
467 ObjectRef *ref = checkobject(L, 1);
468 LuaEntitySAO *co = getluaobject(ref);
469 if(co == NULL) return 0;
471 std::string mod = luaL_checkstring(L, 2);
472 co->setTextureMod(mod);
476 // setsprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
477 // select_horiz_by_yawpitch=false)
478 int ObjectRef::l_setsprite(lua_State *L)
480 ObjectRef *ref = checkobject(L, 1);
481 LuaEntitySAO *co = getluaobject(ref);
482 if(co == NULL) return 0;
486 p = read_v2s16(L, 2);
489 num_frames = lua_tonumber(L, 3);
490 float framelength = 0.2;
492 framelength = lua_tonumber(L, 4);
493 bool select_horiz_by_yawpitch = false;
495 select_horiz_by_yawpitch = lua_toboolean(L, 5);
496 co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
501 // get_entity_name(self)
502 int ObjectRef::l_get_entity_name(lua_State *L)
504 ObjectRef *ref = checkobject(L, 1);
505 LuaEntitySAO *co = getluaobject(ref);
506 if(co == NULL) return 0;
508 std::string name = co->getName();
509 lua_pushstring(L, name.c_str());
513 // get_luaentity(self)
514 int ObjectRef::l_get_luaentity(lua_State *L)
516 ObjectRef *ref = checkobject(L, 1);
517 LuaEntitySAO *co = getluaobject(ref);
518 if(co == NULL) return 0;
520 luaentity_get(L, co->getId());
527 int ObjectRef::l_is_player(lua_State *L)
529 ObjectRef *ref = checkobject(L, 1);
530 Player *player = getplayer(ref);
531 lua_pushboolean(L, (player != NULL));
535 // get_player_name(self)
536 int ObjectRef::l_get_player_name(lua_State *L)
538 ObjectRef *ref = checkobject(L, 1);
539 Player *player = getplayer(ref);
541 lua_pushlstring(L, "", 0);
545 lua_pushstring(L, player->getName());
549 // get_look_dir(self)
550 int ObjectRef::l_get_look_dir(lua_State *L)
552 ObjectRef *ref = checkobject(L, 1);
553 Player *player = getplayer(ref);
554 if(player == NULL) return 0;
556 float pitch = player->getRadPitch();
557 float yaw = player->getRadYaw();
558 v3f v(cos(pitch)*cos(yaw), sin(pitch), cos(pitch)*sin(yaw));
563 // get_look_pitch(self)
564 int ObjectRef::l_get_look_pitch(lua_State *L)
566 ObjectRef *ref = checkobject(L, 1);
567 Player *player = getplayer(ref);
568 if(player == NULL) return 0;
570 lua_pushnumber(L, player->getRadPitch());
574 // get_look_yaw(self)
575 int ObjectRef::l_get_look_yaw(lua_State *L)
577 ObjectRef *ref = checkobject(L, 1);
578 Player *player = getplayer(ref);
579 if(player == NULL) return 0;
581 lua_pushnumber(L, player->getRadYaw());
585 // set_inventory_formspec(self, formspec)
586 int ObjectRef::l_set_inventory_formspec(lua_State *L)
588 ObjectRef *ref = checkobject(L, 1);
589 Player *player = getplayer(ref);
590 if(player == NULL) return 0;
591 std::string formspec = luaL_checkstring(L, 2);
593 player->inventory_formspec = formspec;
594 get_server(L)->reportInventoryFormspecModified(player->getName());
595 lua_pushboolean(L, true);
599 // get_inventory_formspec(self) -> formspec
600 int ObjectRef::l_get_inventory_formspec(lua_State *L)
602 ObjectRef *ref = checkobject(L, 1);
603 Player *player = getplayer(ref);
604 if(player == NULL) return 0;
606 std::string formspec = player->inventory_formspec;
607 lua_pushlstring(L, formspec.c_str(), formspec.size());
611 // get_player_control(self)
612 int ObjectRef::l_get_player_control(lua_State *L)
614 ObjectRef *ref = checkobject(L, 1);
615 Player *player = getplayer(ref);
617 lua_pushlstring(L, "", 0);
621 PlayerControl control = player->getPlayerControl();
623 lua_pushboolean(L, control.up);
624 lua_setfield(L, -2, "up");
625 lua_pushboolean(L, control.down);
626 lua_setfield(L, -2, "down");
627 lua_pushboolean(L, control.left);
628 lua_setfield(L, -2, "left");
629 lua_pushboolean(L, control.right);
630 lua_setfield(L, -2, "right");
631 lua_pushboolean(L, control.jump);
632 lua_setfield(L, -2, "jump");
633 lua_pushboolean(L, control.aux1);
634 lua_setfield(L, -2, "aux1");
635 lua_pushboolean(L, control.sneak);
636 lua_setfield(L, -2, "sneak");
637 lua_pushboolean(L, control.LMB);
638 lua_setfield(L, -2, "LMB");
639 lua_pushboolean(L, control.RMB);
640 lua_setfield(L, -2, "RMB");
644 // get_player_control_bits(self)
645 int ObjectRef::l_get_player_control_bits(lua_State *L)
647 ObjectRef *ref = checkobject(L, 1);
648 Player *player = getplayer(ref);
650 lua_pushlstring(L, "", 0);
654 lua_pushnumber(L, player->keyPressed);
659 ObjectRef::ObjectRef(ServerActiveObject *object):
662 //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
665 ObjectRef::~ObjectRef()
668 infostream<<"ObjectRef destructing for id="
669 <<m_object->getId()<<std::endl;
671 infostream<<"ObjectRef destructing for id=unknown"<<std::endl;*/
674 // Creates an ObjectRef and leaves it on top of stack
675 // Not callable from Lua; all references are created on the C side.
676 void ObjectRef::create(lua_State *L, ServerActiveObject *object)
678 ObjectRef *o = new ObjectRef(object);
679 //infostream<<"ObjectRef::create: o="<<o<<std::endl;
680 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
681 luaL_getmetatable(L, className);
682 lua_setmetatable(L, -2);
685 void ObjectRef::set_null(lua_State *L)
687 ObjectRef *o = checkobject(L, -1);
691 void ObjectRef::Register(lua_State *L)
694 int methodtable = lua_gettop(L);
695 luaL_newmetatable(L, className);
696 int metatable = lua_gettop(L);
698 lua_pushliteral(L, "__metatable");
699 lua_pushvalue(L, methodtable);
700 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
702 lua_pushliteral(L, "__index");
703 lua_pushvalue(L, methodtable);
704 lua_settable(L, metatable);
706 lua_pushliteral(L, "__gc");
707 lua_pushcfunction(L, gc_object);
708 lua_settable(L, metatable);
710 lua_pop(L, 1); // drop metatable
712 luaL_openlib(L, 0, methods, 0); // fill methodtable
713 lua_pop(L, 1); // drop methodtable
715 // Cannot be created from Lua
716 //lua_register(L, className, create_object);
719 const char ObjectRef::className[] = "ObjectRef";
720 const luaL_reg ObjectRef::methods[] = {
721 // ServerActiveObject
722 luamethod(ObjectRef, remove),
723 luamethod(ObjectRef, getpos),
724 luamethod(ObjectRef, setpos),
725 luamethod(ObjectRef, moveto),
726 luamethod(ObjectRef, punch),
727 luamethod(ObjectRef, right_click),
728 luamethod(ObjectRef, set_hp),
729 luamethod(ObjectRef, get_hp),
730 luamethod(ObjectRef, get_inventory),
731 luamethod(ObjectRef, get_wield_list),
732 luamethod(ObjectRef, get_wield_index),
733 luamethod(ObjectRef, get_wielded_item),
734 luamethod(ObjectRef, set_wielded_item),
735 luamethod(ObjectRef, set_armor_groups),
736 luamethod(ObjectRef, set_animation),
737 luamethod(ObjectRef, set_bone_position),
738 luamethod(ObjectRef, set_attach),
739 luamethod(ObjectRef, set_detach),
740 luamethod(ObjectRef, set_properties),
742 luamethod(ObjectRef, setvelocity),
743 luamethod(ObjectRef, getvelocity),
744 luamethod(ObjectRef, setacceleration),
745 luamethod(ObjectRef, getacceleration),
746 luamethod(ObjectRef, setyaw),
747 luamethod(ObjectRef, getyaw),
748 luamethod(ObjectRef, settexturemod),
749 luamethod(ObjectRef, setsprite),
750 luamethod(ObjectRef, get_entity_name),
751 luamethod(ObjectRef, get_luaentity),
753 luamethod(ObjectRef, is_player),
754 luamethod(ObjectRef, get_player_name),
755 luamethod(ObjectRef, get_look_dir),
756 luamethod(ObjectRef, get_look_pitch),
757 luamethod(ObjectRef, get_look_yaw),
758 luamethod(ObjectRef, set_inventory_formspec),
759 luamethod(ObjectRef, get_inventory_formspec),
760 luamethod(ObjectRef, get_player_control),
761 luamethod(ObjectRef, get_player_control_bits),
765 // Creates a new anonymous reference if cobj=NULL or id=0
766 void objectref_get_or_create(lua_State *L,
767 ServerActiveObject *cobj)
769 if(cobj == NULL || cobj->getId() == 0){
770 ObjectRef::create(L, cobj);
772 objectref_get(L, cobj->getId());
776 void objectref_get(lua_State *L, u16 id)
778 // Get minetest.object_refs[i]
779 lua_getglobal(L, "minetest");
780 lua_getfield(L, -1, "object_refs");
781 luaL_checktype(L, -1, LUA_TTABLE);
782 lua_pushnumber(L, id);
784 lua_remove(L, -2); // object_refs
785 lua_remove(L, -2); // minetest
792 void read_object_properties(lua_State *L, int index,
793 ObjectProperties *prop)
796 index = lua_gettop(L) + 1 + index;
797 if(!lua_istable(L, index))
800 prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
802 getboolfield(L, -1, "physical", prop->physical);
804 getfloatfield(L, -1, "weight", prop->weight);
806 lua_getfield(L, -1, "collisionbox");
807 if(lua_istable(L, -1))
808 prop->collisionbox = read_aabb3f(L, -1, 1.0);
811 getstringfield(L, -1, "visual", prop->visual);
813 getstringfield(L, -1, "mesh", prop->mesh);
815 lua_getfield(L, -1, "visual_size");
816 if(lua_istable(L, -1))
817 prop->visual_size = read_v2f(L, -1);
820 lua_getfield(L, -1, "textures");
821 if(lua_istable(L, -1)){
822 prop->textures.clear();
823 int table = lua_gettop(L);
825 while(lua_next(L, table) != 0){
826 // key at index -2 and value at index -1
827 if(lua_isstring(L, -1))
828 prop->textures.push_back(lua_tostring(L, -1));
830 prop->textures.push_back("");
831 // removes value, keeps key for next iteration
837 lua_getfield(L, -1, "colors");
838 if(lua_istable(L, -1)){
839 prop->colors.clear();
840 int table = lua_gettop(L);
842 while(lua_next(L, table) != 0){
843 // key at index -2 and value at index -1
844 if(lua_isstring(L, -1))
845 prop->colors.push_back(readARGB8(L, -1));
847 prop->colors.push_back(video::SColor(255, 255, 255, 255));
848 // removes value, keeps key for next iteration
854 lua_getfield(L, -1, "spritediv");
855 if(lua_istable(L, -1))
856 prop->spritediv = read_v2s16(L, -1);
859 lua_getfield(L, -1, "initial_sprite_basepos");
860 if(lua_istable(L, -1))
861 prop->initial_sprite_basepos = read_v2s16(L, -1);
864 getboolfield(L, -1, "is_visible", prop->is_visible);
865 getboolfield(L, -1, "makes_footstep_sound", prop->makes_footstep_sound);
866 getfloatfield(L, -1, "automatic_rotate", prop->automatic_rotate);
873 void scriptapi_add_object_reference(lua_State *L, ServerActiveObject *cobj)
876 assert(lua_checkstack(L, 20));
877 //infostream<<"scriptapi_add_object_reference: id="<<cobj->getId()<<std::endl;
878 StackUnroller stack_unroller(L);
880 // Create object on stack
881 ObjectRef::create(L, cobj); // Puts ObjectRef (as userdata) on stack
882 int object = lua_gettop(L);
884 // Get minetest.object_refs table
885 lua_getglobal(L, "minetest");
886 lua_getfield(L, -1, "object_refs");
887 luaL_checktype(L, -1, LUA_TTABLE);
888 int objectstable = lua_gettop(L);
890 // object_refs[id] = object
891 lua_pushnumber(L, cobj->getId()); // Push id
892 lua_pushvalue(L, object); // Copy object to top of stack
893 lua_settable(L, objectstable);
896 void scriptapi_rm_object_reference(lua_State *L, ServerActiveObject *cobj)
899 assert(lua_checkstack(L, 20));
900 //infostream<<"scriptapi_rm_object_reference: id="<<cobj->getId()<<std::endl;
901 StackUnroller stack_unroller(L);
903 // Get minetest.object_refs table
904 lua_getglobal(L, "minetest");
905 lua_getfield(L, -1, "object_refs");
906 luaL_checktype(L, -1, LUA_TTABLE);
907 int objectstable = lua_gettop(L);
909 // Get object_refs[id]
910 lua_pushnumber(L, cobj->getId()); // Push id
911 lua_gettable(L, objectstable);
912 // Set object reference to NULL
913 ObjectRef::set_null(L);
914 lua_pop(L, 1); // pop object
916 // Set object_refs[id] = nil
917 lua_pushnumber(L, cobj->getId()); // Push id
919 lua_settable(L, objectstable);