]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/scriptapi.cpp
Add get_wielded_itemstring, get_wielded_item, damage_wielded_item and make getacceler...
[dragonfireclient.git] / src / scriptapi.cpp
index 06cf38d1e370f2493f2abd5e23af8ba11f198648..fbdbf4a124df373e2b0030c141b6259436e6c4eb 100644 (file)
@@ -1642,44 +1642,80 @@ class ObjectRef
                return 0;
        }
 
-       // setvelocity(self, velocity)
-       static int l_setvelocity(lua_State *L)
+       // get_wielded_itemstring(self)
+       static int l_get_wielded_itemstring(lua_State *L)
        {
                ObjectRef *ref = checkobject(L, 1);
-               LuaEntitySAO *co = getluaobject(ref);
+               ServerActiveObject *co = getobject(ref);
                if(co == NULL) return 0;
-               // pos
-               v3f pos = readFloatPos(L, 2);
                // Do it
-               co->setVelocity(pos);
-               return 0;
+               InventoryItem *item = co->getWieldedItem();
+               if(item == NULL){
+                       lua_pushnil(L);
+                       return 1;
+               }
+               lua_pushstring(L, item->getItemString().c_str());
+               return 1;
        }
-       
-       // setacceleration(self, acceleration)
-       static int l_setacceleration(lua_State *L)
+
+       // get_wielded_item(self)
+       static int l_get_wielded_item(lua_State *L)
        {
                ObjectRef *ref = checkobject(L, 1);
-               LuaEntitySAO *co = getluaobject(ref);
+               ServerActiveObject *co = getobject(ref);
                if(co == NULL) return 0;
-               // pos
-               v3f pos = readFloatPos(L, 2);
                // Do it
-               co->setAcceleration(pos);
-               return 0;
+               InventoryItem *item0 = co->getWieldedItem();
+               if(item0 == NULL){
+                       lua_pushnil(L);
+                       return 1;
+               }
+               if(std::string("MaterialItem") == item0->getName()){
+                       MaterialItem *item = (MaterialItem*)item0;
+                       lua_newtable(L);
+                       lua_pushstring(L, "NodeItem");
+                       lua_setfield(L, -2, "type");
+                       lua_pushstring(L, item->getNodeName().c_str());
+                       lua_setfield(L, -2, "name");
+               }
+               else if(std::string("CraftItem") == item0->getName()){
+                       CraftItem *item = (CraftItem*)item0;
+                       lua_newtable(L);
+                       lua_pushstring(L, "CraftItem");
+                       lua_setfield(L, -2, "type");
+                       lua_pushstring(L, item->getSubName().c_str());
+                       lua_setfield(L, -2, "name");
+               }
+               else if(std::string("ToolItem") == item0->getName()){
+                       ToolItem *item = (ToolItem*)item0;
+                       lua_newtable(L);
+                       lua_pushstring(L, "ToolItem");
+                       lua_setfield(L, -2, "type");
+                       lua_pushstring(L, item->getToolName().c_str());
+                       lua_setfield(L, -2, "name");
+                       lua_pushstring(L, itos(item->getWear()).c_str());
+                       lua_setfield(L, -2, "wear");
+               }
+               else{
+                       errorstream<<"l_get_wielded_item: Unknown item name: \""
+                                       <<item0->getName()<<"\""<<std::endl;
+                       lua_pushnil(L);
+               }
+               return 1;
        }
-       
-       // getacceleration(self)
-       static int l_getacceleration(lua_State *L)
+
+       // damage_wielded_item(self, amount)
+       static int l_damage_wielded_item(lua_State *L)
        {
                ObjectRef *ref = checkobject(L, 1);
-               LuaEntitySAO *co = getluaobject(ref);
+               ServerActiveObject *co = getobject(ref);
                if(co == NULL) return 0;
                // Do it
-               v3f v = co->getAcceleration();
-               pushFloatPos(L, v);
-               return 1;
+               int amount = lua_tonumber(L, 2);
+               co->damageWieldedItem(amount);
+               return 0;
        }
-       
+
        // add_to_inventory(self, itemstring)
        // returns: true if item was added, (false, "reason") otherwise
        static int l_add_to_inventory(lua_State *L)
@@ -1740,6 +1776,24 @@ class ObjectRef
                return 0;
        }
 
+       // set_hp(self, hp)
+       // hp = number of hitpoints (2 * number of hearts)
+       // returns: nil
+       static int l_set_hp(lua_State *L)
+       {
+               ObjectRef *ref = checkobject(L, 1);
+               luaL_checknumber(L, 2);
+               ServerActiveObject *co = getobject(ref);
+               if(co == NULL) return 0;
+               int hp = lua_tonumber(L, 2);
+               infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
+                               <<" hp="<<hp<<std::endl;
+               // Do it
+               co->setHP(hp);
+               // Return
+               return 0;
+       }
+
        // get_hp(self)
        // returns: number of hitpoints (2 * number of hearts)
        // 0 if not applicable to this type of object
@@ -1756,24 +1810,46 @@ class ObjectRef
                return 1;
        }
 
-       // set_hp(self, hp)
-       // hp = number of hitpoints (2 * number of hearts)
-       // returns: nil
-       static int l_set_hp(lua_State *L)
+       /* LuaEntitySAO-only */
+
+       // setvelocity(self, {x=num, y=num, z=num})
+       static int l_setvelocity(lua_State *L)
        {
                ObjectRef *ref = checkobject(L, 1);
-               luaL_checknumber(L, 2);
-               ServerActiveObject *co = getobject(ref);
+               LuaEntitySAO *co = getluaobject(ref);
                if(co == NULL) return 0;
-               int hp = lua_tonumber(L, 2);
-               infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
-                               <<" hp="<<hp<<std::endl;
+               // pos
+               v3f pos = readFloatPos(L, 2);
                // Do it
-               co->setHP(hp);
-               // Return
+               co->setVelocity(pos);
                return 0;
        }
-
+       
+       // setacceleration(self, {x=num, y=num, z=num})
+       static int l_setacceleration(lua_State *L)
+       {
+               ObjectRef *ref = checkobject(L, 1);
+               LuaEntitySAO *co = getluaobject(ref);
+               if(co == NULL) return 0;
+               // pos
+               v3f pos = readFloatPos(L, 2);
+               // Do it
+               co->setAcceleration(pos);
+               return 0;
+       }
+       
+       // getacceleration(self)
+       static int l_getacceleration(lua_State *L)
+       {
+               ObjectRef *ref = checkobject(L, 1);
+               LuaEntitySAO *co = getluaobject(ref);
+               if(co == NULL) return 0;
+               // Do it
+               v3f v = co->getAcceleration();
+               pushFloatPos(L, v);
+               return 1;
+       }
+       
        // settexturemod(self, mod)
        static int l_settexturemod(lua_State *L)
        {
@@ -1873,16 +1949,22 @@ class ObjectRef
 };
 const char ObjectRef::className[] = "ObjectRef";
 const luaL_reg ObjectRef::methods[] = {
+       // ServerActiveObject
        method(ObjectRef, remove),
        method(ObjectRef, getpos),
        method(ObjectRef, setpos),
        method(ObjectRef, moveto),
-       method(ObjectRef, setvelocity),
-       method(ObjectRef, setacceleration),
+       method(ObjectRef, get_wielded_itemstring),
+       method(ObjectRef, get_wielded_item),
+       method(ObjectRef, damage_wielded_item),
        method(ObjectRef, add_to_inventory),
        method(ObjectRef, add_to_inventory_later),
-       method(ObjectRef, get_hp),
        method(ObjectRef, set_hp),
+       method(ObjectRef, get_hp),
+       // LuaEntitySAO-only
+       method(ObjectRef, setvelocity),
+       method(ObjectRef, setacceleration),
+       method(ObjectRef, getacceleration),
        method(ObjectRef, settexturemod),
        method(ObjectRef, setsprite),
        {0,0}