diff --git a/apps/farm.lua b/apps/farm.lua index 2d4fd17..cbdde54 100644 --- a/apps/farm.lua +++ b/apps/farm.lua @@ -31,76 +31,74 @@ end local function scan() local blocks = scanner.scan() + Util.filterInplace(blocks, function(v) if v.name == 'minecraft:reeds' then return v.y == 0 end + if v.name == 'minecraft:chest' then + return v.y == -1 + end return crops[v.name] and scanner.getBlockMeta(v.x, v.y, v.z).metadata == crops[v.name].mature - end) + end) - return blocks + local harvestCount = 0 + for _,v in pairs(blocks) do + if v.name ~= 'minecraft:chest' then + harvestCount = harvestCount + 1 + end + end + + return blocks, harvestCount end local function harvest(blocks) turtle.equip('right', 'minecraft:diamond_pickaxe') turtle.setPoint({ x = 0, y = 0, z = 0, heading = turtle.point.heading }) + turtle.select(1) Point.eachClosest(turtle.point, blocks, function(b) - Util.print(b) if b.name == 'minecraft:reeds' then turtle._goto(b) + elseif b.name == 'minecraft:chest' then + local summed = turtle.getSummedInventory() + for _,v in pairs(summed) do + if v.count > 32 then + if turtle._goto(Point.above(b)) then + for k,v2 in pairs(summed) do + if v2.count > 32 then + turtle.dropDown(k, v2.count - 32) + end + end + end + break + end + end else turtle._goto(Point.above(b)) turtle.digDown() turtle.placeDown(crops[b.name].seed) - turtle.select(1) end end) - turtle.equip('right', 'plethora:module:2') end -local function dropOff() - local blocks = scanner.scan() - local done - - Util.filterInplace(blocks, function(v) - if v.name == 'minecraft:chest' then - return v.y == -1 - end - end) - - turtle.setPoint({ x = 0, y = 0, z = 0, heading = turtle.point.heading }) - Point.eachClosest(turtle.point, blocks, function(b) - if not done then - if turtle._goto(Point.above(b)) then - for k,v in pairs(turtle.getSummedInventory()) do - if v.count > 32 then - turtle.dropDown(k, v.count - 32) - end - done = true - end - end - end - end) -end - turtle.run(function() local facing = scanner.getBlockMeta(0, 0, 0).state.facing turtle.point.heading = Point.facings[facing].heading turtle.setPolicy('digOnly') - repeat - local blocks = scan() - if #blocks > 0 then + local blocks, harvestCount = scan() + if harvestCount > 0 then turtle.setStatus('Harvesting') harvest(blocks) - turtle.setStatus('Storing') - dropOff() turtle.setStatus('Sleeping') end os.sleep(10) + if turtle.getFuelLevel() < 10 then + error('Out of fuel') + end until turtle.isAborted() end)