From a244e367ea81aaa5507316dca7b5268078374864 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Wed, 30 Nov 2011 20:36:07 +0200 Subject: [PATCH] Add get_wielded_itemstring, get_wielded_item, damage_wielded_item and make getacceleration visible in API --- data/mods/default/init.lua | 4 + src/scriptapi.cpp | 158 ++++++++++++++++++++++++++++--------- 2 files changed, 124 insertions(+), 38 deletions(-) diff --git a/data/mods/default/init.lua b/data/mods/default/init.lua index f9c4c8231..20f3038c8 100644 --- a/data/mods/default/init.lua +++ b/data/mods/default/init.lua @@ -85,6 +85,10 @@ -- - add_to_inventory_later(itemstring): like above, but after callback returns (only allowed for craftitem callbacks) -- - get_hp(): returns number of hitpoints (2 * number of hearts) -- - set_hp(hp): set number of hitpoints (2 * number of hearts) +-- LuaEntitySAO-only: +-- - setvelocity(self, {x=num, y=num, z=num}) +-- - setacceleration(self, {x=num, y=num, z=num}) +-- - getacceleration(self) -- - settexturemod(mod) -- - setsprite(p={x=0,y=0}, num_frames=1, framelength=0.2, -- - select_horiz_by_yawpitch=false) diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index 06cf38d1e..fbdbf4a12 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -1642,44 +1642,80 @@ private: 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: \"" + <getName()<<"\""<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 @@ private: 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="<getId() + <<" hp="<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 @@ private: 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="<getId() - <<" hp="<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 @@ public: }; 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} -- 2.25.1