Random fixes to InvRef related stuff and a broken luafurnace

This commit is contained in:
Perttu Ahola 2011-12-06 17:23:58 +02:00
parent 270a482740
commit d5ba7ebaf0
2 changed files with 189 additions and 9 deletions

@ -56,6 +56,138 @@ minetest.register_on_placenode(function(pos, newnode, placer)
end end
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({ minetest.register_abm({
nodenames = {"experimental:luafurnace"}, nodenames = {"experimental:luafurnace"},
interval = 1.0, interval = 1.0,
@ -110,7 +242,7 @@ minetest.register_abm({
meta:set_infotext("Lua Furnace: total cooked: "..total_cooked) meta:set_infotext("Lua Furnace: total cooked: "..total_cooked)
end, end,
}) })
--]]
minetest.register_craft({ minetest.register_craft({
output = 'node "experimental:luafurnace" 1', output = 'node "experimental:luafurnace" 1',
recipe = { recipe = {

@ -606,7 +606,7 @@ static void push_stack_item(lua_State *L, InventoryItem *item0)
if(item0 == NULL){ if(item0 == NULL){
lua_pushnil(L); lua_pushnil(L);
} }
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, "NodeItem");
@ -837,6 +837,14 @@ private:
return 0; 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) // take_item(self)
static int l_take_item(lua_State *L) static int l_take_item(lua_State *L)
{ {
@ -870,6 +878,29 @@ private:
return 1; 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: public:
ItemStack(InventoryItem *item=NULL): ItemStack(InventoryItem *item=NULL):
m_stack(item) m_stack(item)
@ -956,8 +987,10 @@ public:
}; };
const char ItemStack::className[] = "ItemStack"; const char ItemStack::className[] = "ItemStack";
const luaL_reg ItemStack::methods[] = { const luaL_reg ItemStack::methods[] = {
method(ItemStack, peek_item),
method(ItemStack, take_item), method(ItemStack, take_item),
method(ItemStack, put_item), method(ItemStack, put_item),
method(ItemStack, put_stackstring),
{0,0} {0,0}
}; };
@ -1317,7 +1350,6 @@ private:
// set_infotext(self, text) // set_infotext(self, text)
static int l_set_infotext(lua_State *L) static int l_set_infotext(lua_State *L)
{ {
infostream<<__FUNCTION_NAME<<std::endl;
NodeMetaRef *ref = checkobject(L, 1); NodeMetaRef *ref = checkobject(L, 1);
NodeMetadata *meta = getmeta(ref); NodeMetadata *meta = getmeta(ref);
if(meta == NULL) return 0; if(meta == NULL) return 0;
@ -1328,9 +1360,21 @@ private:
return 0; return 0;
} }
// inventory_set_list(self, name, {item1, item2, ...}) // get_inventory(self)
static int l_get_inventory(lua_State *L)
{
NodeMetaRef *ref = checkobject(L, 1);
NodeMetadata *meta = getmeta(ref);
if(meta == NULL) return 0;
// Do it
InvRef::createNodeMeta(L, ref->m_p);
return 1;
}
// deprecated: inventory_set_list(self, name, {item1, item2, ...})
static int l_inventory_set_list(lua_State *L) static int l_inventory_set_list(lua_State *L)
{ {
infostream<<"Deprecated: inventory_set_list"<<std::endl;
NodeMetaRef *ref = checkobject(L, 1); NodeMetaRef *ref = checkobject(L, 1);
NodeMetadata *meta = getmeta(ref); NodeMetadata *meta = getmeta(ref);
if(meta == NULL) return 0; if(meta == NULL) return 0;
@ -1343,9 +1387,10 @@ private:
return 0; return 0;
} }
// inventory_get_list(self, name) // deprecated: inventory_get_list(self, name)
static int l_inventory_get_list(lua_State *L) static int l_inventory_get_list(lua_State *L)
{ {
infostream<<"Deprecated: inventory_get_list"<<std::endl;
NodeMetaRef *ref = checkobject(L, 1); NodeMetaRef *ref = checkobject(L, 1);
NodeMetadata *meta = getmeta(ref); NodeMetadata *meta = getmeta(ref);
if(meta == NULL) return 0; if(meta == NULL) return 0;
@ -1541,8 +1586,9 @@ const luaL_reg NodeMetaRef::methods[] = {
method(NodeMetaRef, get_text), method(NodeMetaRef, get_text),
method(NodeMetaRef, get_owner), method(NodeMetaRef, get_owner),
method(NodeMetaRef, set_infotext), method(NodeMetaRef, set_infotext),
method(NodeMetaRef, inventory_set_list), method(NodeMetaRef, get_inventory),
method(NodeMetaRef, inventory_get_list), method(NodeMetaRef, inventory_set_list), // deprecated
method(NodeMetaRef, inventory_get_list), // deprecated
method(NodeMetaRef, set_inventory_draw_spec), method(NodeMetaRef, set_inventory_draw_spec),
method(NodeMetaRef, set_allow_text_input), method(NodeMetaRef, set_allow_text_input),
method(NodeMetaRef, set_allow_removal), method(NodeMetaRef, set_allow_removal),
@ -3204,10 +3250,12 @@ void scriptapi_export(lua_State *L, Server *server)
luaL_register(L, NULL, minetest_entity_m); luaL_register(L, NULL, minetest_entity_m);
// Put other stuff in metatable // Put other stuff in metatable
// Register reference wrappers // Register wrappers
ItemStack::Register(L);
InvRef::Register(L);
NodeMetaRef::Register(L); NodeMetaRef::Register(L);
EnvRef::Register(L);
ObjectRef::Register(L); ObjectRef::Register(L);
EnvRef::Register(L);
} }
bool scriptapi_loadmod(lua_State *L, const std::string &scriptpath, bool scriptapi_loadmod(lua_State *L, const std::string &scriptpath,