From d5ba7ebaf0683560cc93b650c5b9499fbcdacff7 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Tue, 6 Dec 2011 17:23:58 +0200 Subject: [PATCH] Random fixes to InvRef related stuff and a broken luafurnace --- data/mods/experimental/init.lua | 134 +++++++++++++++++++++++++++++++- src/scriptapi.cpp | 64 +++++++++++++-- 2 files changed, 189 insertions(+), 9 deletions(-) diff --git a/data/mods/experimental/init.lua b/data/mods/experimental/init.lua index dd0d60f4e..2c28f89f9 100644 --- a/data/mods/experimental/init.lua +++ b/data/mods/experimental/init.lua @@ -56,6 +56,138 @@ minetest.register_on_placenode(function(pos, newnode, placer) end end) +minetest.register_abm({ + nodenames = {"experimental:luafurnace"}, + interval = 1.0, + chance = 1, + action = function(pos, node, active_object_count, active_object_count_wider) + local meta = minetest.env:get_meta(pos) + for i, name in ipairs({ + "fuel_totaltime", + "fuel_time", + "src_totaltime", + "src_time" + }) do + if not tonumber(meta:get_string(name)) then + meta:set_string(name, 0) + end + end + + local inv = meta:get_inventory() + + fuelitem = inv:get_stack("fuel", 1):peek_item() + srcitem = inv:get_stack("src", 1):peek_item() + + local cooked_something = false + + local src_cooktime = -1 + local result_stackstring = nil + + if srcitem then + local prop = nil + if srcitem.type == "node" then + prop = minetest.registered_nodes[srcitem.name] + elseif srcitem.type == "craft" then + prop = minetest.registered_craftitems[srcitem.name] + end + if prop and prop.cookresult_itemstring ~= "" then + result_stackstring = prop.cookresult_itemstring + src_cooktime = prop.furnace_cooktime or 3 + end + end + + if tonumber(meta:get_string("fuel_time")) < tonumber(meta:get_string("fuel_totaltime")) then + meta:set_string("fuel_time", tonumber(meta:get_string("fuel_time")) + 1) + meta:set_string("src_time", tonumber(meta:get_string("src_time")) + 1) + --print("result_stackstring="..dump(result_stackstring)) + --print('tonumber(meta:get_string("src_time"))='..dump(tonumber(meta:get_string("src_time")))) + --print("src_cooktime="..dump(src_cooktime)) + if result_stackstring and tonumber(meta:get_string("src_time")) >= src_cooktime and src_cooktime >= 0 then + for i=1,4 do + -- Put result in "dst" list + dststack = inv:get_stack("dst", i) + success = dststack:put_stackstring(result_stackstring) + inv:set_stack("dst", i, dststack) + -- If succeeded, take stuff from "src" list + if success then + srcstack = inv:get_stack("src", 1) + srcstack:take_item() + inv:set_stack("src", 1, srcstack) + break + end + end + meta:inventory_set_list("src", srclist) + meta:inventory_set_list("dst", dstlist) + meta:set_string("src_time", 0) + cooked_something = true + end + end + + if tonumber(meta:get_string("fuel_time")) < tonumber(meta:get_string("fuel_totaltime")) then + meta:set_infotext("Furnace active: "..(tonumber(meta:get_string("fuel_time"))/tonumber(meta:get_string("fuel_totaltime"))*100).."%") + return + end + + local srclist = meta:inventory_get_list("src") + _, srcitem = stackstring_take_item(srclist[1]) + + local src_cooktime = 0 + local result_stackstring = nil + + if srcitem then + if srcitem.type == "node" then + local prop = minetest.registered_nodes[srcitem.name] + if prop and prop.cookresult_itemstring ~= "" then + result_stackstring = prop.cookresult_itemstring + src_cooktime = prop.furnace_cooktime or 3 + end + elseif srcitem.type == "craft" then + local prop = minetest.registered_craftitems[srcitem.name] + if prop and prop.cookresult_itemstring ~= "" then + result_stackstring = prop.cookresult_itemstring + src_cooktime = prop.furnace_cooktime or 3 + end + end + end + + if not result_stackstring then + if cooked_something then + meta:set_infotext("Furnace is empty") + end + return + end + + local fuellist = meta:inventory_get_list("fuel") + _, fuelitem = stackstring_take_item(fuellist[1]) + + local burntime = -1 + if fuelitem then + if fuelitem.type == "node" then + local prop = minetest.registered_nodes[fuelitem.name] + if prop then + burntime = prop.furnace_burntime or -1 + end + elseif fuelitem.type == "craft" then + local prop = minetest.registered_craftitems[fuelitem.name] + if prop then + burntime = prop.furnace_burntime or -1 + end + end + end + + if burntime <= 0 then + meta:set_infotext("Furnace out of fuel") + return + end + + meta:set_string("fuel_totaltime", burntime) + meta:set_string("fuel_time", 0) + + fuellist[1], _ = stackstring_take_item(fuellist[1]) + meta:inventory_set_list("fuel", fuellist) + end, +}) +--[[ minetest.register_abm({ nodenames = {"experimental:luafurnace"}, interval = 1.0, @@ -110,7 +242,7 @@ minetest.register_abm({ meta:set_infotext("Lua Furnace: total cooked: "..total_cooked) end, }) - +--]] minetest.register_craft({ output = 'node "experimental:luafurnace" 1', recipe = { diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index 101e07894..2efb8203d 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -606,7 +606,7 @@ static void push_stack_item(lua_State *L, InventoryItem *item0) if(item0 == NULL){ lua_pushnil(L); } - if(std::string("MaterialItem") == item0->getName()){ + else if(std::string("MaterialItem") == item0->getName()){ MaterialItem *item = (MaterialItem*)item0; lua_newtable(L); lua_pushstring(L, "NodeItem"); @@ -837,6 +837,14 @@ private: return 0; } + // peek_item(self) + static int l_peek_item(lua_State *L) + { + ItemStack *o = checkobject(L, 1); + push_stack_item(L, o->m_stack); + return 1; + } + // take_item(self) static int l_take_item(lua_State *L) { @@ -870,6 +878,29 @@ private: return 1; } + // put_stackstring(self, stackstring) -> true/false + static int l_put_stackstring(lua_State *L) + { + ItemStack *o = checkobject(L, 1); + std::string stackstring = luaL_checkstring(L, 2); + try{ + InventoryItem *item = InventoryItem::deSerialize(stackstring, + get_server(L)); + if(!item->addableTo(o->m_stack)){ + lua_pushboolean(L, false); + return 1; + } + o->m_stack->add(1); + delete item; + lua_pushboolean(L, true); + return 1; + } + catch(SerializationError &e){ + lua_pushboolean(L, false); + return 1; + } + } + public: ItemStack(InventoryItem *item=NULL): m_stack(item) @@ -956,8 +987,10 @@ public: }; const char ItemStack::className[] = "ItemStack"; const luaL_reg ItemStack::methods[] = { + method(ItemStack, peek_item), method(ItemStack, take_item), method(ItemStack, put_item), + method(ItemStack, put_stackstring), {0,0} }; @@ -1317,7 +1350,6 @@ private: // set_infotext(self, text) static int l_set_infotext(lua_State *L) { - infostream<<__FUNCTION_NAME<m_p); + return 1; + } + + // deprecated: inventory_set_list(self, name, {item1, item2, ...}) static int l_inventory_set_list(lua_State *L) { + infostream<<"Deprecated: inventory_set_list"<