Commit bcfb3e9a authored by Fedja Beader's avatar Fedja Beader
Browse files

reformer chest: simple & stupid & working 100%

parent a73142dc
......@@ -41,6 +41,16 @@ function find_item_index(list, name, start)
return nil
end
function find_item_index_rev(list, name, start)
for i = (start or #list), 1, -1 do
if list[i]:get_name() == name then
return i
end
end
return nil
end
function find_item(inv, name)
for list_name,list in pairs(inv) do
local index = find_item_index(list, name)
......
......@@ -21,6 +21,7 @@ When close to a designated chest, this CSM will swap all worn-down tools/armor
with repaired equivalents in the chest.
--]]
local l = Logger:new("repair chest")
-- Hardcoded for now. This is actually a chest node.
local reformer_pos = { y = 81, x = 11999, z = 11997 }
......@@ -99,7 +100,7 @@ end
-- Called from globalstep
function reformer:step(distance)
function reformer:deprecated_step(distance)
if self.take_index >= 0 then
print("Case >=0")
if distance >3 then self:reset(); return; end
......@@ -180,17 +181,74 @@ function reformer:step(distance)
end
end
--[[
core.register_globalstep(function(dtime)
if not core.localplayer then return end
local my_pos = core.localplayer:get_pos()
local distance = vector.distance(my_pos, reformer_pos)
reformer:step(distance)
end)
--]]
function reformer:step()
if not reformer.enable then return end
local my_pos = core.localplayer:get_pos()
if vector.distance(my_pos, reformer_pos) > 5 then
core.after(3, function() self:step() end)
return
end
local my_inv = core.get_inventory()
local chest_inv = core.get_inventory(reformer_pos)
for my_list_name,my_list in pairs(my_inv) do
for my_index,my_stack in pairs(my_list) do
if my_stack:get_wear() > 100 then
l:debug("Looking for repaired " .. my_stack:get_name()
.. " at " .. my_list_name .. "[" .. my_index .. "] with wear: " .. my_stack:get_wear())
for chest_list_name,chest_list in pairs(chest_inv) do
for chest_index,chest_stack in pairs(chest_list) do
if my_stack:get_name() == chest_stack:get_name() and chest_stack:get_wear() < 100 then
l:debug("Found suitable replacement in chest at index " .. chest_index .. ", wear: " .. chest_stack:get_wear())
-- Find empty index in chest to hold my damaged tool. In reverse, as whatever that is repairing
-- tools/armor will put repaired ones at start.
local empty_index = find_item_index_rev(chest_list, "")
if not empty_index then
l:err("No empty space left in chest")
core.after(5, function() self:step() end)
return
end
local empty_loc = ItemLoc:new(reformer_pos, chest_list_name, empty_index)
local repaired_tool_loc = ItemLoc:new(reformer_pos, chest_list_name, chest_index)
local damaged_tool_loc = ItemLoc:new(nil, my_list_name, my_index)
queue:append(function() return queued_move_stack(damaged_tool_loc, empty_loc, my_stack:get_count()) end)
queue:append(function() return queued_ensure_stack(damaged_tool_loc, "", 0, 5) end)
queue:append(function() return queued_move_stack(repaired_tool_loc, damaged_tool_loc, chest_stack:get_count()) end)
queue:append(function() return queued_ensure_stack(damaged_tool_loc, chest_stack:get_name(), chest_stack:get_count(), 5) end)
queue:append(function() l:info("Successfully repaired "..my_stack:get_name().." at "..my_list_name.."["..my_index.."]") end)
queue:append(function() return self:step() end)
queue:walk()
return
end
end
end
end
end
end
-- if nothing was repaired, then schedule another round of repairs
core.after(3, function() self:step() end)
end
core.register_chatcommand("reformer", {
func = function(param)
print("Punch a node to register it as reformer chest")
reformer.enable = true
reformer:step()
return true
end,
})
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment