reduce table lookups and hash_node_position calculations

This commit is contained in:
Tim 2015-01-26 18:34:05 +01:00
parent 52cfe9f277
commit 7b9571912d

View File

@ -8,6 +8,11 @@ local function make_inventory_cache(invlist)
return l return l
end end
local function get_cached_craft(pos)
local hash = minetest.hash_node_position(pos)
return hash, autocrafterCache[hash]
end
local function autocraft(inventory, pos) local function autocraft(inventory, pos)
if not inventory then return end if not inventory then return end
local recipe = inventory:get_list("recipe") local recipe = inventory:get_list("recipe")
@ -16,38 +21,33 @@ local function autocraft(inventory, pos)
local result local result
local new local new
if autocrafterCache[minetest.hash_node_position(pos)] == nil then local hash, craft = get_cached_craft(pos)
if craft == nil then
recipe_last = {} recipe_last = {}
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})
autocrafterCache[minetest.hash_node_position(pos)] = {["recipe"] = recipe, ["result"] = result, ["new"] = new} autocrafterCache[hash] = {["recipe"] = recipe, ["result"] = result, ["new"] = new}
else else
local autocrafterCacheEntry = autocrafterCache[minetest.hash_node_position(pos)] recipe_last, result, new = craft.recipe, craft.result, craft.new
recipe_last = autocrafterCacheEntry["recipe"] local recipe_changed = false
result = autocrafterCacheEntry["result"]
new = autocrafterCacheEntry["new"]
local recipeUnchanged = true
for i = 1, 9 do for i = 1, 9 do
if recipe[i]:get_name() ~= recipe_last[i]:get_name() then local recipe_entry, recipe_last_entry = recipe[i], recipe_last[i]
recipeUnchanged = false if recipe_entry:get_name() ~= recipe_last_entry:get_name()
break or recipe_entry:get_count() ~= recipe_last_entry:get_count() then
end recipe_changed = true
if recipe[i]:get_count() ~= recipe_last[i]:get_count() then
recipeUnchanged = false
break break
end end
end end
if recipeUnchanged then if recipe_changed then
else
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})
autocrafterCache[minetest.hash_node_position(pos)] = {["recipe"] = recipe, ["result"] = result, ["new"] = new} autocrafterCache[hash] = {["recipe"] = recipe, ["result"] = result, ["new"] = new}
end end
end end
@ -57,10 +57,11 @@ local function autocraft(inventory, pos)
local to_use = {} local to_use = {}
for _, item in ipairs(recipe) do for _, item in ipairs(recipe) do
if item~= nil and not item:is_empty() then if item~= nil and not item:is_empty() then
if to_use[item:get_name()] == nil then local item_name = item:get_name()
to_use[item:get_name()] = 1 if to_use[item_name] == nil then
to_use[item_name] = 1
else else
to_use[item:get_name()] = to_use[item:get_name()]+1 to_use[item_name] = to_use[item_name]+1
end end
end end
end end