Working implementation of experimental:luafurnace

This commit is contained in:
Perttu Ahola 2011-12-06 18:07:13 +02:00
parent d5ba7ebaf0
commit 6be5441176
3 changed files with 102 additions and 64 deletions

@ -301,7 +301,7 @@ end
test_stackstring() test_stackstring()
-- --
-- nodeitem helpers -- NodeItem helpers
-- --
minetest.inventorycube = function(img1, img2, img3) minetest.inventorycube = function(img1, img2, img3)
@ -314,7 +314,7 @@ minetest.inventorycube = function(img1, img2, img3)
end end
-- --
-- craftitem helpers -- CraftItem helpers
-- --
minetest.craftitem_place_item = function(item, placer, pos) minetest.craftitem_place_item = function(item, placer, pos)

@ -56,6 +56,15 @@ minetest.register_on_placenode(function(pos, newnode, placer)
end end
end) end)
local get_item_definition = function(item)
if not item then return nil end
if item.type == "node" then
return minetest.registered_nodes[item.name]
elseif item.type == "craft" then
return minetest.registered_craftitems[item.name]
end
end
minetest.register_abm({ minetest.register_abm({
nodenames = {"experimental:luafurnace"}, nodenames = {"experimental:luafurnace"},
interval = 1.0, interval = 1.0,
@ -68,58 +77,54 @@ minetest.register_abm({
"src_totaltime", "src_totaltime",
"src_time" "src_time"
}) do }) do
if not tonumber(meta:get_string(name)) then if not meta:get_string(name) then
meta:set_string(name, 0) meta:set_string(name, 0)
end end
end end
local inv = meta:get_inventory() local inv = meta:get_inventory()
fuelitem = inv:get_stack("fuel", 1):peek_item() local fuelitem = inv:get_stack("fuel", 1):peek_item()
srcitem = inv:get_stack("src", 1):peek_item() local srcitem = inv:get_stack("src", 1):peek_item()
--print("fuelitem="..dump(fuelitem))
--print("srcitem="..dump(srcitem))
local cooked_something = false local was_active = false
local src_cooktime = -1 local src_cooktime = -1
local result_stackstring = nil local result_stackstring = nil
if srcitem then if srcitem then
local prop = nil local prop = get_item_definition(srcitem)
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 if prop and prop.cookresult_itemstring ~= "" then
result_stackstring = prop.cookresult_itemstring result_stackstring = prop.cookresult_itemstring
src_cooktime = prop.furnace_cooktime or 3 src_cooktime = prop.furnace_cooktime or 3
end end
end end
print("src_cooktime="..dump(src_cooktime))
print("result_stackstring="..dump(result_stackstring))
if tonumber(meta:get_string("fuel_time")) < tonumber(meta:get_string("fuel_totaltime")) then if tonumber(meta:get_string("fuel_time")) < tonumber(meta:get_string("fuel_totaltime")) then
was_active = true
meta:set_string("fuel_time", tonumber(meta:get_string("fuel_time")) + 1) meta:set_string("fuel_time", tonumber(meta:get_string("fuel_time")) + 1)
meta:set_string("src_time", tonumber(meta:get_string("src_time")) + 1) meta:set_string("src_time", tonumber(meta:get_string("src_time")) + 1)
--print("result_stackstring="..dump(result_stackstring)) --print("result_stackstring="..dump(result_stackstring))
--print('tonumber(meta:get_string("src_time"))='..dump(tonumber(meta:get_string("src_time")))) --print('tonumber(meta:get_string("src_time"))='..dump(tonumber(meta:get_string("src_time"))))
--print("src_cooktime="..dump(src_cooktime)) --print("src_cooktime="..dump(src_cooktime))
if result_stackstring and tonumber(meta:get_string("src_time")) >= src_cooktime and src_cooktime >= 0 then 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
-- Put result in "dst" list success = inv:autoinsert_stackstring("dst", result_stackstring)
dststack = inv:get_stack("dst", i) if not success then
success = dststack:put_stackstring(result_stackstring) print("Could not autoinsert '"..result_stackstring.."'")
inv:set_stack("dst", i, dststack) end
-- If succeeded, take stuff from "src" list -- If succeeded, take stuff from "src" list
if success then if success then
srcstack = inv:get_stack("src", 1) srcstack = inv:get_stack("src", 1)
srcstack:take_item() srcstack:take_item()
inv:set_stack("src", 1, srcstack) inv:set_stack("src", 1, srcstack)
break
end
end end
meta:inventory_set_list("src", srclist)
meta:inventory_set_list("dst", dstlist)
meta:set_string("src_time", 0) meta:set_string("src_time", 0)
cooked_something = true
end end
end end
@ -128,50 +133,33 @@ minetest.register_abm({
return return
end end
local srclist = meta:inventory_get_list("src") local srcitem = inv:get_stack("src", 1):peek_item()
_, srcitem = stackstring_take_item(srclist[1])
local src_cooktime = 0 local src_cooktime = 0
local result_stackstring = nil local result_stackstring = nil
if srcitem then if srcitem then
if srcitem.type == "node" then local prop = get_item_definition(srcitem)
local prop = minetest.registered_nodes[srcitem.name] if prop and prop.cookresult_itemstring ~= "" then
if prop and prop.cookresult_itemstring ~= "" then result_stackstring = prop.cookresult_itemstring
result_stackstring = prop.cookresult_itemstring src_cooktime = prop.furnace_cooktime or 3
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
end end
if not result_stackstring then local fuelitem = inv:get_stack("fuel", 1):peek_item()
if cooked_something then
if not result_stackstring or not fuelitem then
if was_active then
meta:set_infotext("Furnace is empty") meta:set_infotext("Furnace is empty")
end end
return return
end end
local fuellist = meta:inventory_get_list("fuel")
_, fuelitem = stackstring_take_item(fuellist[1])
local burntime = -1 local burntime = -1
if fuelitem then if fuelitem then
if fuelitem.type == "node" then local prop = get_item_definition(fuelitem)
local prop = minetest.registered_nodes[fuelitem.name] if prop then
if prop then burntime = prop.furnace_burntime or -1
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
end end
@ -182,9 +170,10 @@ minetest.register_abm({
meta:set_string("fuel_totaltime", burntime) meta:set_string("fuel_totaltime", burntime)
meta:set_string("fuel_time", 0) meta:set_string("fuel_time", 0)
fuellist[1], _ = stackstring_take_item(fuellist[1]) local stack = inv:get_stack("fuel", 1)
meta:inventory_set_list("fuel", fuellist) stack:take_item()
inv:set_stack("fuel", 1, stack)
end, end,
}) })
--[[ --[[

@ -609,7 +609,7 @@ static void push_stack_item(lua_State *L, InventoryItem *item0)
else if(std::string("MaterialItem") == item0->getName()){ else if(std::string("MaterialItem") == item0->getName()){
MaterialItem *item = (MaterialItem*)item0; MaterialItem *item = (MaterialItem*)item0;
lua_newtable(L); lua_newtable(L);
lua_pushstring(L, "NodeItem"); lua_pushstring(L, "node");
lua_setfield(L, -2, "type"); lua_setfield(L, -2, "type");
lua_pushstring(L, item->getNodeName().c_str()); lua_pushstring(L, item->getNodeName().c_str());
lua_setfield(L, -2, "name"); lua_setfield(L, -2, "name");
@ -617,7 +617,7 @@ static void push_stack_item(lua_State *L, InventoryItem *item0)
else if(std::string("CraftItem") == item0->getName()){ else if(std::string("CraftItem") == item0->getName()){
CraftItem *item = (CraftItem*)item0; CraftItem *item = (CraftItem*)item0;
lua_newtable(L); lua_newtable(L);
lua_pushstring(L, "CraftItem"); lua_pushstring(L, "craft");
lua_setfield(L, -2, "type"); lua_setfield(L, -2, "type");
lua_pushstring(L, item->getSubName().c_str()); lua_pushstring(L, item->getSubName().c_str());
lua_setfield(L, -2, "name"); lua_setfield(L, -2, "name");
@ -625,7 +625,7 @@ static void push_stack_item(lua_State *L, InventoryItem *item0)
else if(std::string("ToolItem") == item0->getName()){ else if(std::string("ToolItem") == item0->getName()){
ToolItem *item = (ToolItem*)item0; ToolItem *item = (ToolItem*)item0;
lua_newtable(L); lua_newtable(L);
lua_pushstring(L, "ToolItem"); lua_pushstring(L, "tool");
lua_setfield(L, -2, "type"); lua_setfield(L, -2, "type");
lua_pushstring(L, item->getToolName().c_str()); lua_pushstring(L, item->getToolName().c_str());
lua_setfield(L, -2, "name"); lua_setfield(L, -2, "name");
@ -1093,7 +1093,7 @@ private:
{ {
InvRef *ref = checkobject(L, 1); InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2); const char *listname = luaL_checkstring(L, 2);
int i = luaL_checknumber(L, 3); int i = luaL_checknumber(L, 3) - 1;
InventoryItem *item = getitem(L, ref, listname, i); InventoryItem *item = getitem(L, ref, listname, i);
if(!item){ if(!item){
ItemStack::create(L, NULL); ItemStack::create(L, NULL);
@ -1108,7 +1108,7 @@ private:
{ {
InvRef *ref = checkobject(L, 1); InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2); const char *listname = luaL_checkstring(L, 2);
int i = luaL_checknumber(L, 3); int i = luaL_checknumber(L, 3) - 1;
ItemStack *stack = ItemStack::checkobject(L, 4); ItemStack *stack = ItemStack::checkobject(L, 4);
InventoryList *list = getlist(L, ref, listname); InventoryList *list = getlist(L, ref, listname);
if(!list){ if(!list){
@ -1151,6 +1151,53 @@ private:
return 0; return 0;
} }
// autoinsert_stack(self, listname, stack)
static int l_autoinsert_stack(lua_State *L)
{
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
ItemStack *stack = ItemStack::checkobject(L, 3);
InventoryList *list = getlist(L, ref, listname);
if(!list){
lua_pushboolean(L, false);
return 1;
}
InventoryItem *item = stack->getItemCopy();
if(list->roomForItem(item)){
delete list->addItem(item);
lua_pushboolean(L, true);
reportInventoryChange(L, ref);
} else {
delete item;
lua_pushboolean(L, false);
}
return 1;
}
// autoinsert_stackstring(self, listname, stackstring)
static int l_autoinsert_stackstring(lua_State *L)
{
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
const char *stackstring = luaL_checkstring(L, 3);
InventoryList *list = getlist(L, ref, listname);
if(!list){
lua_pushboolean(L, false);
return 1;
}
InventoryItem *item = InventoryItem::deSerialize(stackstring,
get_server(L));
if(list->roomForItem(item)){
delete list->addItem(item);
lua_pushboolean(L, true);
reportInventoryChange(L, ref);
} else {
delete item;
lua_pushboolean(L, false);
}
return 1;
}
public: public:
InvRef(const InventoryLocation &loc): InvRef(const InventoryLocation &loc):
m_loc(loc) m_loc(loc)
@ -1219,6 +1266,8 @@ const luaL_reg InvRef::methods[] = {
method(InvRef, set_stack), method(InvRef, set_stack),
method(InvRef, get_list), method(InvRef, get_list),
method(InvRef, set_list), method(InvRef, set_list),
method(InvRef, autoinsert_stack),
method(InvRef, autoinsert_stackstring),
{0,0} {0,0}
}; };