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"},
61 ObjectRef* ObjectRef::checkobject(lua_State *L, int narg)
63 luaL_checktype(L, narg, LUA_TUSERDATA);
64 void *ud = luaL_checkudata(L, narg, className);
65 if(!ud) luaL_typerror(L, narg, className);
66 return *(ObjectRef**)ud; // unbox pointer
69 ServerActiveObject* ObjectRef::getobject(ObjectRef *ref)
71 ServerActiveObject *co = ref->m_object;
75 LuaEntitySAO* ObjectRef::getluaobject(ObjectRef *ref)
77 ServerActiveObject *obj = getobject(ref);
80 if(obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY)
82 return (LuaEntitySAO*)obj;
85 PlayerSAO* ObjectRef::getplayersao(ObjectRef *ref)
87 ServerActiveObject *obj = getobject(ref);
90 if(obj->getType() != ACTIVEOBJECT_TYPE_PLAYER)
92 return (PlayerSAO*)obj;
95 Player* ObjectRef::getplayer(ObjectRef *ref)
97 PlayerSAO *playersao = getplayersao(ref);
100 return playersao->getPlayer();
103 // Exported functions
106 int ObjectRef::gc_object(lua_State *L) {
107 ObjectRef *o = *(ObjectRef **)(lua_touserdata(L, 1));
108 //infostream<<"ObjectRef::gc_object: o="<<o<<std::endl;
114 int ObjectRef::l_remove(lua_State *L)
116 ObjectRef *ref = checkobject(L, 1);
117 ServerActiveObject *co = getobject(ref);
118 if(co == NULL) return 0;
119 verbosestream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
120 co->m_removed = true;
125 // returns: {x=num, y=num, z=num}
126 int ObjectRef::l_getpos(lua_State *L)
128 ObjectRef *ref = checkobject(L, 1);
129 ServerActiveObject *co = getobject(ref);
130 if(co == NULL) return 0;
131 v3f pos = co->getBasePosition() / BS;
133 lua_pushnumber(L, pos.X);
134 lua_setfield(L, -2, "x");
135 lua_pushnumber(L, pos.Y);
136 lua_setfield(L, -2, "y");
137 lua_pushnumber(L, pos.Z);
138 lua_setfield(L, -2, "z");
143 int ObjectRef::l_setpos(lua_State *L)
145 ObjectRef *ref = checkobject(L, 1);
146 //LuaEntitySAO *co = getluaobject(ref);
147 ServerActiveObject *co = getobject(ref);
148 if(co == NULL) return 0;
150 v3f pos = checkFloatPos(L, 2);
156 // moveto(self, pos, continuous=false)
157 int ObjectRef::l_moveto(lua_State *L)
159 ObjectRef *ref = checkobject(L, 1);
160 //LuaEntitySAO *co = getluaobject(ref);
161 ServerActiveObject *co = getobject(ref);
162 if(co == NULL) return 0;
164 v3f pos = checkFloatPos(L, 2);
166 bool continuous = lua_toboolean(L, 3);
168 co->moveTo(pos, continuous);
172 // punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
173 int ObjectRef::l_punch(lua_State *L)
175 ObjectRef *ref = checkobject(L, 1);
176 ObjectRef *puncher_ref = checkobject(L, 2);
177 ServerActiveObject *co = getobject(ref);
178 ServerActiveObject *puncher = getobject(puncher_ref);
179 if(co == NULL) return 0;
180 if(puncher == NULL) return 0;
182 if(lua_type(L, 5) != LUA_TTABLE)
183 dir = co->getBasePosition() - puncher->getBasePosition();
185 dir = read_v3f(L, 5);
186 float time_from_last_punch = 1000000;
187 if(lua_isnumber(L, 3))
188 time_from_last_punch = lua_tonumber(L, 3);
189 ToolCapabilities toolcap = read_tool_capabilities(L, 4);
192 co->punch(dir, &toolcap, puncher, time_from_last_punch);
196 // right_click(self, clicker); clicker = an another ObjectRef
197 int ObjectRef::l_right_click(lua_State *L)
199 ObjectRef *ref = checkobject(L, 1);
200 ObjectRef *ref2 = checkobject(L, 2);
201 ServerActiveObject *co = getobject(ref);
202 ServerActiveObject *co2 = getobject(ref2);
203 if(co == NULL) return 0;
204 if(co2 == NULL) return 0;
211 // hp = number of hitpoints (2 * number of hearts)
213 int ObjectRef::l_set_hp(lua_State *L)
215 ObjectRef *ref = checkobject(L, 1);
216 luaL_checknumber(L, 2);
217 ServerActiveObject *co = getobject(ref);
218 if(co == NULL) return 0;
219 int hp = lua_tonumber(L, 2);
220 /*infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
221 <<" hp="<<hp<<std::endl;*/
229 // returns: number of hitpoints (2 * number of hearts)
230 // 0 if not applicable to this type of object
231 int ObjectRef::l_get_hp(lua_State *L)
233 ObjectRef *ref = checkobject(L, 1);
234 ServerActiveObject *co = getobject(ref);
237 lua_pushnumber(L, 1);
240 int hp = co->getHP();
241 /*infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
242 <<" hp="<<hp<<std::endl;*/
244 lua_pushnumber(L, hp);
248 // get_inventory(self)
249 int ObjectRef::l_get_inventory(lua_State *L)
251 ObjectRef *ref = checkobject(L, 1);
252 ServerActiveObject *co = getobject(ref);
253 if(co == NULL) return 0;
255 InventoryLocation loc = co->getInventoryLocation();
256 if(get_server(L)->getInventory(loc) != NULL)
257 InvRef::create(L, loc);
259 lua_pushnil(L); // An object may have no inventory (nil)
263 // get_wield_list(self)
264 int ObjectRef::l_get_wield_list(lua_State *L)
266 ObjectRef *ref = checkobject(L, 1);
267 ServerActiveObject *co = getobject(ref);
268 if(co == NULL) return 0;
270 lua_pushstring(L, co->getWieldList().c_str());
274 // get_wield_index(self)
275 int ObjectRef::l_get_wield_index(lua_State *L)
277 ObjectRef *ref = checkobject(L, 1);
278 ServerActiveObject *co = getobject(ref);
279 if(co == NULL) return 0;
281 lua_pushinteger(L, co->getWieldIndex() + 1);
285 // get_wielded_item(self)
286 int ObjectRef::l_get_wielded_item(lua_State *L)
288 ObjectRef *ref = checkobject(L, 1);
289 ServerActiveObject *co = getobject(ref);
292 LuaItemStack::create(L, ItemStack());
296 LuaItemStack::create(L, co->getWieldedItem());
300 // set_wielded_item(self, itemstack or itemstring or table or nil)
301 int ObjectRef::l_set_wielded_item(lua_State *L)
303 ObjectRef *ref = checkobject(L, 1);
304 ServerActiveObject *co = getobject(ref);
305 if(co == NULL) return 0;
307 ItemStack item = read_item(L, 2);
308 bool success = co->setWieldedItem(item);
309 lua_pushboolean(L, success);
313 // set_armor_groups(self, groups)
314 int ObjectRef::l_set_armor_groups(lua_State *L)
316 ObjectRef *ref = checkobject(L, 1);
317 ServerActiveObject *co = getobject(ref);
318 if(co == NULL) return 0;
320 ItemGroupList groups;
321 read_groups(L, 2, groups);
322 co->setArmorGroups(groups);
326 // set_physics_override(self, physics_override_speed, physics_override_jump, physics_override_gravity)
327 int ObjectRef::l_set_physics_override(lua_State *L)
329 ObjectRef *ref = checkobject(L, 1);
330 PlayerSAO *co = (PlayerSAO *) getobject(ref);
331 if(co == NULL) return 0;
333 if(!lua_isnil(L, 2)){
334 co->m_physics_override_speed = lua_tonumber(L, 2);
335 co->m_physics_override_sent = false;
337 if(!lua_isnil(L, 3)){
338 co->m_physics_override_jump = lua_tonumber(L, 3);
339 co->m_physics_override_sent = false;
341 if(!lua_isnil(L, 4)){
342 co->m_physics_override_gravity = lua_tonumber(L, 4);
343 co->m_physics_override_sent = false;
348 // set_animation(self, frame_range, frame_speed, frame_blend)
349 int ObjectRef::l_set_animation(lua_State *L)
351 ObjectRef *ref = checkobject(L, 1);
352 ServerActiveObject *co = getobject(ref);
353 if(co == NULL) return 0;
355 v2f frames = v2f(1, 1);
357 frames = read_v2f(L, 2);
358 float frame_speed = 15;
360 frame_speed = lua_tonumber(L, 3);
361 float frame_blend = 0;
363 frame_blend = lua_tonumber(L, 4);
364 co->setAnimation(frames, frame_speed, frame_blend);
368 // set_bone_position(self, std::string bone, v3f position, v3f rotation)
369 int ObjectRef::l_set_bone_position(lua_State *L)
371 ObjectRef *ref = checkobject(L, 1);
372 ServerActiveObject *co = getobject(ref);
373 if(co == NULL) return 0;
375 std::string bone = "";
377 bone = lua_tostring(L, 2);
378 v3f position = v3f(0, 0, 0);
380 position = read_v3f(L, 3);
381 v3f rotation = v3f(0, 0, 0);
383 rotation = read_v3f(L, 4);
384 co->setBonePosition(bone, position, rotation);
388 // set_attach(self, parent, bone, position, rotation)
389 int ObjectRef::l_set_attach(lua_State *L)
391 ObjectRef *ref = checkobject(L, 1);
392 ObjectRef *parent_ref = checkobject(L, 2);
393 ServerActiveObject *co = getobject(ref);
394 ServerActiveObject *parent = getobject(parent_ref);
395 if(co == NULL) return 0;
396 if(parent == NULL) return 0;
398 std::string bone = "";
400 bone = lua_tostring(L, 3);
401 v3f position = v3f(0, 0, 0);
403 position = read_v3f(L, 4);
404 v3f rotation = v3f(0, 0, 0);
406 rotation = read_v3f(L, 5);
407 co->setAttachment(parent->getId(), bone, position, rotation);
412 int ObjectRef::l_set_detach(lua_State *L)
414 ObjectRef *ref = checkobject(L, 1);
415 ServerActiveObject *co = getobject(ref);
416 if(co == NULL) return 0;
418 co->setAttachment(0, "", v3f(0,0,0), v3f(0,0,0));
422 // set_properties(self, properties)
423 int ObjectRef::l_set_properties(lua_State *L)
425 ObjectRef *ref = checkobject(L, 1);
426 ServerActiveObject *co = getobject(ref);
427 if(co == NULL) return 0;
428 ObjectProperties *prop = co->accessObjectProperties();
431 read_object_properties(L, 2, prop);
432 co->notifyObjectPropertiesModified();
436 /* LuaEntitySAO-only */
438 // setvelocity(self, {x=num, y=num, z=num})
439 int ObjectRef::l_setvelocity(lua_State *L)
441 ObjectRef *ref = checkobject(L, 1);
442 LuaEntitySAO *co = getluaobject(ref);
443 if(co == NULL) return 0;
444 v3f pos = checkFloatPos(L, 2);
446 co->setVelocity(pos);
451 int ObjectRef::l_getvelocity(lua_State *L)
453 ObjectRef *ref = checkobject(L, 1);
454 LuaEntitySAO *co = getluaobject(ref);
455 if(co == NULL) return 0;
457 v3f v = co->getVelocity();
462 // setacceleration(self, {x=num, y=num, z=num})
463 int ObjectRef::l_setacceleration(lua_State *L)
465 ObjectRef *ref = checkobject(L, 1);
466 LuaEntitySAO *co = getluaobject(ref);
467 if(co == NULL) return 0;
469 v3f pos = checkFloatPos(L, 2);
471 co->setAcceleration(pos);
475 // getacceleration(self)
476 int ObjectRef::l_getacceleration(lua_State *L)
478 ObjectRef *ref = checkobject(L, 1);
479 LuaEntitySAO *co = getluaobject(ref);
480 if(co == NULL) return 0;
482 v3f v = co->getAcceleration();
487 // setyaw(self, radians)
488 int ObjectRef::l_setyaw(lua_State *L)
490 ObjectRef *ref = checkobject(L, 1);
491 LuaEntitySAO *co = getluaobject(ref);
492 if(co == NULL) return 0;
493 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
500 int ObjectRef::l_getyaw(lua_State *L)
502 ObjectRef *ref = checkobject(L, 1);
503 LuaEntitySAO *co = getluaobject(ref);
504 if(co == NULL) return 0;
506 float yaw = co->getYaw() * core::DEGTORAD;
507 lua_pushnumber(L, yaw);
511 // settexturemod(self, mod)
512 int ObjectRef::l_settexturemod(lua_State *L)
514 ObjectRef *ref = checkobject(L, 1);
515 LuaEntitySAO *co = getluaobject(ref);
516 if(co == NULL) return 0;
518 std::string mod = luaL_checkstring(L, 2);
519 co->setTextureMod(mod);
523 // setsprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
524 // select_horiz_by_yawpitch=false)
525 int ObjectRef::l_setsprite(lua_State *L)
527 ObjectRef *ref = checkobject(L, 1);
528 LuaEntitySAO *co = getluaobject(ref);
529 if(co == NULL) return 0;
533 p = read_v2s16(L, 2);
536 num_frames = lua_tonumber(L, 3);
537 float framelength = 0.2;
539 framelength = lua_tonumber(L, 4);
540 bool select_horiz_by_yawpitch = false;
542 select_horiz_by_yawpitch = lua_toboolean(L, 5);
543 co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
548 // get_entity_name(self)
549 int ObjectRef::l_get_entity_name(lua_State *L)
551 ObjectRef *ref = checkobject(L, 1);
552 LuaEntitySAO *co = getluaobject(ref);
553 if(co == NULL) return 0;
555 std::string name = co->getName();
556 lua_pushstring(L, name.c_str());
560 // get_luaentity(self)
561 int ObjectRef::l_get_luaentity(lua_State *L)
563 ObjectRef *ref = checkobject(L, 1);
564 LuaEntitySAO *co = getluaobject(ref);
565 if(co == NULL) return 0;
567 luaentity_get(L, co->getId());
574 int ObjectRef::l_is_player(lua_State *L)
576 ObjectRef *ref = checkobject(L, 1);
577 Player *player = getplayer(ref);
578 lua_pushboolean(L, (player != NULL));
582 // get_player_name(self)
583 int ObjectRef::l_get_player_name(lua_State *L)
585 ObjectRef *ref = checkobject(L, 1);
586 Player *player = getplayer(ref);
588 lua_pushlstring(L, "", 0);
592 lua_pushstring(L, player->getName());
596 // get_look_dir(self)
597 int ObjectRef::l_get_look_dir(lua_State *L)
599 ObjectRef *ref = checkobject(L, 1);
600 Player *player = getplayer(ref);
601 if(player == NULL) return 0;
603 float pitch = player->getRadPitch();
604 float yaw = player->getRadYaw();
605 v3f v(cos(pitch)*cos(yaw), sin(pitch), cos(pitch)*sin(yaw));
610 // get_look_pitch(self)
611 int ObjectRef::l_get_look_pitch(lua_State *L)
613 ObjectRef *ref = checkobject(L, 1);
614 Player *player = getplayer(ref);
615 if(player == NULL) return 0;
617 lua_pushnumber(L, player->getRadPitch());
621 // get_look_yaw(self)
622 int ObjectRef::l_get_look_yaw(lua_State *L)
624 ObjectRef *ref = checkobject(L, 1);
625 Player *player = getplayer(ref);
626 if(player == NULL) return 0;
628 lua_pushnumber(L, player->getRadYaw());
632 // set_look_pitch(self, radians)
633 int ObjectRef::l_set_look_pitch(lua_State *L)
635 ObjectRef *ref = checkobject(L, 1);
636 PlayerSAO* co = getplayersao(ref);
637 if(co == NULL) return 0;
638 float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
644 // set_look_yaw(self, radians)
645 int ObjectRef::l_set_look_yaw(lua_State *L)
647 ObjectRef *ref = checkobject(L, 1);
648 PlayerSAO* co = getplayersao(ref);
649 if(co == NULL) return 0;
650 float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
656 // set_inventory_formspec(self, formspec)
657 int ObjectRef::l_set_inventory_formspec(lua_State *L)
659 ObjectRef *ref = checkobject(L, 1);
660 Player *player = getplayer(ref);
661 if(player == NULL) return 0;
662 std::string formspec = luaL_checkstring(L, 2);
664 player->inventory_formspec = formspec;
665 get_server(L)->reportInventoryFormspecModified(player->getName());
666 lua_pushboolean(L, true);
670 // get_inventory_formspec(self) -> formspec
671 int ObjectRef::l_get_inventory_formspec(lua_State *L)
673 ObjectRef *ref = checkobject(L, 1);
674 Player *player = getplayer(ref);
675 if(player == NULL) return 0;
677 std::string formspec = player->inventory_formspec;
678 lua_pushlstring(L, formspec.c_str(), formspec.size());
682 // get_player_control(self)
683 int ObjectRef::l_get_player_control(lua_State *L)
685 ObjectRef *ref = checkobject(L, 1);
686 Player *player = getplayer(ref);
688 lua_pushlstring(L, "", 0);
692 PlayerControl control = player->getPlayerControl();
694 lua_pushboolean(L, control.up);
695 lua_setfield(L, -2, "up");
696 lua_pushboolean(L, control.down);
697 lua_setfield(L, -2, "down");
698 lua_pushboolean(L, control.left);
699 lua_setfield(L, -2, "left");
700 lua_pushboolean(L, control.right);
701 lua_setfield(L, -2, "right");
702 lua_pushboolean(L, control.jump);
703 lua_setfield(L, -2, "jump");
704 lua_pushboolean(L, control.aux1);
705 lua_setfield(L, -2, "aux1");
706 lua_pushboolean(L, control.sneak);
707 lua_setfield(L, -2, "sneak");
708 lua_pushboolean(L, control.LMB);
709 lua_setfield(L, -2, "LMB");
710 lua_pushboolean(L, control.RMB);
711 lua_setfield(L, -2, "RMB");
715 // get_player_control_bits(self)
716 int ObjectRef::l_get_player_control_bits(lua_State *L)
718 ObjectRef *ref = checkobject(L, 1);
719 Player *player = getplayer(ref);
721 lua_pushlstring(L, "", 0);
725 lua_pushnumber(L, player->keyPressed);
729 // hud_add(self, form)
730 int ObjectRef::l_hud_add(lua_State *L)
732 ObjectRef *ref = checkobject(L, 1);
733 Player *player = getplayer(ref);
737 HudElement *elem = new HudElement;
739 elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type",
740 es_HudElementType, HUD_ELEM_TEXT);
742 lua_getfield(L, 2, "position");
743 elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
746 lua_getfield(L, 2, "scale");
747 elem->scale = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
750 elem->name = getstringfield_default(L, 2, "name", "");
751 elem->text = getstringfield_default(L, 2, "text", "");
752 elem->number = getintfield_default(L, 2, "number", 0);
753 elem->item = getintfield_default(L, 2, "item", 0);
754 elem->dir = getintfield_default(L, 2, "direction", 0);
756 lua_getfield(L, 2, "alignment");
757 elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
760 lua_getfield(L, 2, "offset");
761 elem->offset = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
764 u32 id = get_server(L)->hudAdd(player, elem);
770 lua_pushnumber(L, id);
774 // hud_remove(self, id)
775 int ObjectRef::l_hud_remove(lua_State *L)
777 ObjectRef *ref = checkobject(L, 1);
778 Player *player = getplayer(ref);
783 if (!lua_isnil(L, 2))
784 id = lua_tonumber(L, 2);
786 if (!get_server(L)->hudRemove(player, id))
789 lua_pushboolean(L, true);
793 // hud_change(self, id, stat, data)
794 int ObjectRef::l_hud_change(lua_State *L)
796 ObjectRef *ref = checkobject(L, 1);
797 Player *player = getplayer(ref);
801 u32 id = !lua_isnil(L, 2) ? lua_tonumber(L, 2) : -1;
802 if (id >= player->hud.size())
805 HudElementStat stat = HUD_STAT_NUMBER;
806 if (!lua_isnil(L, 3)) {
808 std::string statstr = lua_tostring(L, 3);
809 stat = string_to_enum(es_HudElementStat, statint, statstr) ?
810 (HudElementStat)statint : HUD_STAT_NUMBER;
814 HudElement *e = player->hud[id];
820 e->pos = read_v2f(L, 4);
824 e->name = lua_tostring(L, 4);
828 e->scale = read_v2f(L, 4);
832 e->text = lua_tostring(L, 4);
835 case HUD_STAT_NUMBER:
836 e->number = lua_tonumber(L, 4);
840 e->item = lua_tonumber(L, 4);
844 e->dir = lua_tonumber(L, 4);
847 e->align = read_v2f(L, 4);
849 case HUD_STAT_OFFSET:
850 e->offset = read_v2f(L, 4);
854 get_server(L)->hudChange(player, id, stat, value);
856 lua_pushboolean(L, true);
861 int ObjectRef::l_hud_get(lua_State *L)
863 ObjectRef *ref = checkobject(L, 1);
864 Player *player = getplayer(ref);
868 u32 id = lua_tonumber(L, -1);
869 if (id >= player->hud.size())
872 HudElement *e = player->hud[id];
878 lua_pushstring(L, es_HudElementType[(u8)e->type].str);
879 lua_setfield(L, -2, "type");
882 lua_setfield(L, -2, "position");
884 lua_pushstring(L, e->name.c_str());
885 lua_setfield(L, -2, "name");
887 push_v2f(L, e->scale);
888 lua_setfield(L, -2, "scale");
890 lua_pushstring(L, e->text.c_str());
891 lua_setfield(L, -2, "text");
893 lua_pushnumber(L, e->number);
894 lua_setfield(L, -2, "number");
896 lua_pushnumber(L, e->item);
897 lua_setfield(L, -2, "item");
899 lua_pushnumber(L, e->dir);
900 lua_setfield(L, -2, "dir");
905 ObjectRef::ObjectRef(ServerActiveObject *object):
908 //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
911 ObjectRef::~ObjectRef()
914 infostream<<"ObjectRef destructing for id="
915 <<m_object->getId()<<std::endl;
917 infostream<<"ObjectRef destructing for id=unknown"<<std::endl;*/
920 // Creates an ObjectRef and leaves it on top of stack
921 // Not callable from Lua; all references are created on the C side.
922 void ObjectRef::create(lua_State *L, ServerActiveObject *object)
924 ObjectRef *o = new ObjectRef(object);
925 //infostream<<"ObjectRef::create: o="<<o<<std::endl;
926 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
927 luaL_getmetatable(L, className);
928 lua_setmetatable(L, -2);
931 void ObjectRef::set_null(lua_State *L)
933 ObjectRef *o = checkobject(L, -1);
937 void ObjectRef::Register(lua_State *L)
940 int methodtable = lua_gettop(L);
941 luaL_newmetatable(L, className);
942 int metatable = lua_gettop(L);
944 lua_pushliteral(L, "__metatable");
945 lua_pushvalue(L, methodtable);
946 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
948 lua_pushliteral(L, "__index");
949 lua_pushvalue(L, methodtable);
950 lua_settable(L, metatable);
952 lua_pushliteral(L, "__gc");
953 lua_pushcfunction(L, gc_object);
954 lua_settable(L, metatable);
956 lua_pop(L, 1); // drop metatable
958 luaL_openlib(L, 0, methods, 0); // fill methodtable
959 lua_pop(L, 1); // drop methodtable
961 // Cannot be created from Lua
962 //lua_register(L, className, create_object);
965 const char ObjectRef::className[] = "ObjectRef";
966 const luaL_reg ObjectRef::methods[] = {
967 // ServerActiveObject
968 luamethod(ObjectRef, remove),
969 luamethod(ObjectRef, getpos),
970 luamethod(ObjectRef, setpos),
971 luamethod(ObjectRef, moveto),
972 luamethod(ObjectRef, punch),
973 luamethod(ObjectRef, right_click),
974 luamethod(ObjectRef, set_hp),
975 luamethod(ObjectRef, get_hp),
976 luamethod(ObjectRef, get_inventory),
977 luamethod(ObjectRef, get_wield_list),
978 luamethod(ObjectRef, get_wield_index),
979 luamethod(ObjectRef, get_wielded_item),
980 luamethod(ObjectRef, set_wielded_item),
981 luamethod(ObjectRef, set_armor_groups),
982 luamethod(ObjectRef, set_physics_override),
983 luamethod(ObjectRef, set_animation),
984 luamethod(ObjectRef, set_bone_position),
985 luamethod(ObjectRef, set_attach),
986 luamethod(ObjectRef, set_detach),
987 luamethod(ObjectRef, set_properties),
989 luamethod(ObjectRef, setvelocity),
990 luamethod(ObjectRef, getvelocity),
991 luamethod(ObjectRef, setacceleration),
992 luamethod(ObjectRef, getacceleration),
993 luamethod(ObjectRef, setyaw),
994 luamethod(ObjectRef, getyaw),
995 luamethod(ObjectRef, settexturemod),
996 luamethod(ObjectRef, setsprite),
997 luamethod(ObjectRef, get_entity_name),
998 luamethod(ObjectRef, get_luaentity),
1000 luamethod(ObjectRef, is_player),
1001 luamethod(ObjectRef, get_player_name),
1002 luamethod(ObjectRef, get_look_dir),
1003 luamethod(ObjectRef, get_look_pitch),
1004 luamethod(ObjectRef, get_look_yaw),
1005 luamethod(ObjectRef, set_look_yaw),
1006 luamethod(ObjectRef, set_look_pitch),
1007 luamethod(ObjectRef, set_inventory_formspec),
1008 luamethod(ObjectRef, get_inventory_formspec),
1009 luamethod(ObjectRef, get_player_control),
1010 luamethod(ObjectRef, get_player_control_bits),
1011 luamethod(ObjectRef, hud_add),
1012 luamethod(ObjectRef, hud_remove),
1013 luamethod(ObjectRef, hud_change),
1014 luamethod(ObjectRef, hud_get),
1015 //luamethod(ObjectRef, hud_lock_next_bar),
1016 //luamethod(ObjectRef, hud_unlock_bar),
1020 // Creates a new anonymous reference if cobj=NULL or id=0
1021 void objectref_get_or_create(lua_State *L,
1022 ServerActiveObject *cobj)
1024 if(cobj == NULL || cobj->getId() == 0){
1025 ObjectRef::create(L, cobj);
1027 objectref_get(L, cobj->getId());
1031 void objectref_get(lua_State *L, u16 id)
1033 // Get minetest.object_refs[i]
1034 lua_getglobal(L, "minetest");
1035 lua_getfield(L, -1, "object_refs");
1036 luaL_checktype(L, -1, LUA_TTABLE);
1037 lua_pushnumber(L, id);
1038 lua_gettable(L, -2);
1039 lua_remove(L, -2); // object_refs
1040 lua_remove(L, -2); // minetest
1047 void read_object_properties(lua_State *L, int index,
1048 ObjectProperties *prop)
1051 index = lua_gettop(L) + 1 + index;
1052 if(!lua_istable(L, index))
1055 prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
1057 getboolfield(L, -1, "physical", prop->physical);
1059 getfloatfield(L, -1, "weight", prop->weight);
1061 lua_getfield(L, -1, "collisionbox");
1062 if(lua_istable(L, -1))
1063 prop->collisionbox = read_aabb3f(L, -1, 1.0);
1066 getstringfield(L, -1, "visual", prop->visual);
1068 getstringfield(L, -1, "mesh", prop->mesh);
1070 lua_getfield(L, -1, "visual_size");
1071 if(lua_istable(L, -1))
1072 prop->visual_size = read_v2f(L, -1);
1075 lua_getfield(L, -1, "textures");
1076 if(lua_istable(L, -1)){
1077 prop->textures.clear();
1078 int table = lua_gettop(L);
1080 while(lua_next(L, table) != 0){
1081 // key at index -2 and value at index -1
1082 if(lua_isstring(L, -1))
1083 prop->textures.push_back(lua_tostring(L, -1));
1085 prop->textures.push_back("");
1086 // removes value, keeps key for next iteration
1092 lua_getfield(L, -1, "colors");
1093 if(lua_istable(L, -1)){
1094 prop->colors.clear();
1095 int table = lua_gettop(L);
1097 while(lua_next(L, table) != 0){
1098 // key at index -2 and value at index -1
1099 if(lua_isstring(L, -1))
1100 prop->colors.push_back(readARGB8(L, -1));
1102 prop->colors.push_back(video::SColor(255, 255, 255, 255));
1103 // removes value, keeps key for next iteration
1109 lua_getfield(L, -1, "spritediv");
1110 if(lua_istable(L, -1))
1111 prop->spritediv = read_v2s16(L, -1);
1114 lua_getfield(L, -1, "initial_sprite_basepos");
1115 if(lua_istable(L, -1))
1116 prop->initial_sprite_basepos = read_v2s16(L, -1);
1119 getboolfield(L, -1, "is_visible", prop->is_visible);
1120 getboolfield(L, -1, "makes_footstep_sound", prop->makes_footstep_sound);
1121 getfloatfield(L, -1, "automatic_rotate", prop->automatic_rotate);
1128 void scriptapi_add_object_reference(lua_State *L, ServerActiveObject *cobj)
1131 assert(lua_checkstack(L, 20));
1132 //infostream<<"scriptapi_add_object_reference: id="<<cobj->getId()<<std::endl;
1133 StackUnroller stack_unroller(L);
1135 // Create object on stack
1136 ObjectRef::create(L, cobj); // Puts ObjectRef (as userdata) on stack
1137 int object = lua_gettop(L);
1139 // Get minetest.object_refs table
1140 lua_getglobal(L, "minetest");
1141 lua_getfield(L, -1, "object_refs");
1142 luaL_checktype(L, -1, LUA_TTABLE);
1143 int objectstable = lua_gettop(L);
1145 // object_refs[id] = object
1146 lua_pushnumber(L, cobj->getId()); // Push id
1147 lua_pushvalue(L, object); // Copy object to top of stack
1148 lua_settable(L, objectstable);
1151 void scriptapi_rm_object_reference(lua_State *L, ServerActiveObject *cobj)
1154 assert(lua_checkstack(L, 20));
1155 //infostream<<"scriptapi_rm_object_reference: id="<<cobj->getId()<<std::endl;
1156 StackUnroller stack_unroller(L);
1158 // Get minetest.object_refs table
1159 lua_getglobal(L, "minetest");
1160 lua_getfield(L, -1, "object_refs");
1161 luaL_checktype(L, -1, LUA_TTABLE);
1162 int objectstable = lua_gettop(L);
1164 // Get object_refs[id]
1165 lua_pushnumber(L, cobj->getId()); // Push id
1166 lua_gettable(L, objectstable);
1167 // Set object reference to NULL
1168 ObjectRef::set_null(L);
1169 lua_pop(L, 1); // pop object
1171 // Set object_refs[id] = nil
1172 lua_pushnumber(L, cobj->getId()); // Push id
1174 lua_settable(L, objectstable);