forked from mtcontrib/pipeworks
simplify autocrafterCache, remove cache entry on node dig and disable debug output
This commit is contained in:
parent
b9bbfbb754
commit
6a9fdae4a1
|
@ -1,18 +1,4 @@
|
||||||
local autocrafterCache = {}
|
local autocrafterCache = {} -- caches some recipe data to avoid to call the slow function minetest.get_craft_result() every second
|
||||||
|
|
||||||
function addCacheEntry(pos, recipe, result, new)
|
|
||||||
if autocrafterCache[pos.x] == nil then autocrafterCache[pos.x] = {} end
|
|
||||||
if autocrafterCache[pos.x][pos.y] == nil then autocrafterCache[pos.x][pos.y] = {} end
|
|
||||||
if autocrafterCache[pos.x][pos.y][pos.z] == nil then autocrafterCache[pos.x][pos.z] = {} end
|
|
||||||
autocrafterCache[pos.x][pos.y][pos.z] = {["recipe"] = recipe, ["result"] = result, ["new"] = new}
|
|
||||||
end
|
|
||||||
|
|
||||||
function getCacheEntry(pos)
|
|
||||||
if autocrafterCache[pos.x] == nil then return nil, nil, nil end
|
|
||||||
if autocrafterCache[pos.x][pos.y] == nil then return nil, nil, nil end
|
|
||||||
if autocrafterCache[pos.x][pos.y][pos.z] == nil then return nil, nil, nil end
|
|
||||||
return autocrafterCache[pos.x][pos.y][pos.z]["recipe"], autocrafterCache[pos.x][pos.y][pos.z]["result"], autocrafterCache[pos.x][pos.y][pos.z]["new"]
|
|
||||||
end
|
|
||||||
|
|
||||||
function autocraft(inventory, pos)
|
function autocraft(inventory, pos)
|
||||||
local recipe = inventory:get_list("recipe")
|
local recipe = inventory:get_list("recipe")
|
||||||
|
@ -20,15 +6,23 @@ function autocraft(inventory, pos)
|
||||||
local result
|
local result
|
||||||
local new
|
local new
|
||||||
|
|
||||||
recipe_last, result, new = getCacheEntry(pos)
|
if autocrafterCache[minetest.hash_node_position(pos)] == nil then
|
||||||
if recipe_last ~= nil then
|
--print("first call for this autocrafter at pos("..pos.x..","..pos.y..","..pos.z..")")
|
||||||
|
recipe_last = {}
|
||||||
|
for i = 1, 9 do
|
||||||
|
recipe_last[i] = recipe[i]
|
||||||
|
--print (recipe_last[i]:get_name())
|
||||||
|
recipe[i] = ItemStack({name = recipe[i]:get_name(), count = 1})
|
||||||
|
end
|
||||||
|
result, new = minetest.get_craft_result({method = "normal", width = 3, items = recipe})
|
||||||
|
autocrafterCache[minetest.hash_node_position(pos)] = {["recipe"] = recipe, ["result"] = result, ["new"] = new}
|
||||||
|
else
|
||||||
|
local autocrafterCacheEntry = autocrafterCache[minetest.hash_node_position(pos)]
|
||||||
|
recipe_last = autocrafterCacheEntry["recipe"]
|
||||||
|
result = autocrafterCacheEntry["result"]
|
||||||
|
new = autocrafterCacheEntry["new"]
|
||||||
local recipeUnchanged = true
|
local recipeUnchanged = true
|
||||||
for i = 1, 9 do
|
for i = 1, 9 do
|
||||||
--print ("recipe_base"..i.." ")
|
|
||||||
--print (recipe[i]:get_name())
|
|
||||||
--print (recipe[i]:get_count())
|
|
||||||
--print (recipe_last[i]:get_name())
|
|
||||||
--print (recipe_last[i]:get_count())
|
|
||||||
if recipe[i]:get_name() ~= recipe_last[i]:get_name() then
|
if recipe[i]:get_name() ~= recipe_last[i]:get_name() then
|
||||||
recipeUnchanged = False
|
recipeUnchanged = False
|
||||||
break
|
break
|
||||||
|
@ -39,26 +33,16 @@ function autocraft(inventory, pos)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if recipeUnchanged then
|
if recipeUnchanged then
|
||||||
-- print("autocrafter recipe unchanged")
|
--print("autocrafter recipe unchanged")
|
||||||
else
|
else
|
||||||
print("autocrafter recipe changed at pos("..pos.x..","..pos.y..","..pos.z..")")
|
--print("autocrafter recipe changed at pos("..pos.x..","..pos.y..","..pos.z..")")
|
||||||
for i = 1, 9 do
|
for i = 1, 9 do
|
||||||
recipe_last[i] = recipe[i]
|
recipe_last[i] = recipe[i]
|
||||||
recipe[i] = ItemStack({name = recipe[i]:get_name(), count = 1})
|
recipe[i] = ItemStack({name = recipe[i]:get_name(), count = 1})
|
||||||
end
|
end
|
||||||
result, new = minetest.get_craft_result({method = "normal", width = 3, items = recipe})
|
result, new = minetest.get_craft_result({method = "normal", width = 3, items = recipe})
|
||||||
addCacheEntry(pos, recipe_last, result, new)
|
autocrafterCache[minetest.hash_node_position(pos)] = {["recipe"] = recipe, ["result"] = result, ["new"] = new}
|
||||||
end
|
end
|
||||||
else
|
|
||||||
print("first call for this autocrafter at pos("..pos.x..","..pos.y..","..pos.z..")")
|
|
||||||
recipe_last = {}
|
|
||||||
for i = 1, 9 do
|
|
||||||
recipe_last[i] = recipe[i]
|
|
||||||
--print (recipe_last[i]:get_name())
|
|
||||||
recipe[i] = ItemStack({name = recipe[i]:get_name(), count = 1})
|
|
||||||
end
|
|
||||||
result, new = minetest.get_craft_result({method = "normal", width = 3, items = recipe})
|
|
||||||
addCacheEntry(pos, recipe_last, result, new)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local input = inventory:get_list("input")
|
local input = inventory:get_list("input")
|
||||||
|
@ -126,8 +110,11 @@ minetest.register_node("pipeworks:autocrafter", {
|
||||||
local inv = meta:get_inventory()
|
local inv = meta:get_inventory()
|
||||||
return (inv:is_empty("src") and inv:is_empty("recipe") and inv:is_empty("dst"))
|
return (inv:is_empty("src") and inv:is_empty("recipe") and inv:is_empty("dst"))
|
||||||
end,
|
end,
|
||||||
after_place_node = tube_scanforobjects,
|
after_place_node = tube_scanforobjects(pos),
|
||||||
after_dig_node = tube_scanforobjects,
|
after_dig_node = function(pos)
|
||||||
|
tube_scanforobjects(pos)
|
||||||
|
autocrafterCache[minetest.hash_node_position(pos)] = nil
|
||||||
|
end
|
||||||
})
|
})
|
||||||
|
|
||||||
minetest.register_abm({nodenames = {"pipeworks:autocrafter"}, interval = 1, chance = 1,
|
minetest.register_abm({nodenames = {"pipeworks:autocrafter"}, interval = 1, chance = 1,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user