better chopping in superTreefarm

This commit is contained in:
kepler155c@gmail.com
2018-12-23 19:50:09 -05:00
parent 83a9aa46ab
commit 61fbbf5e3d
2 changed files with 59 additions and 66 deletions

View File

@@ -30,6 +30,8 @@ To start the program, run:
> superTreefarm > superTreefarm
A startup file is created automatically the first time the program is run (usr/autorun/superTreefarm.lua). A startup file is created automatically the first time the program is run (usr/autorun/superTreefarm.lua).
If the turtle does not get any saplings from the initial tree, place down another sapling in front of the turtle.
Tips Tips
==== ====
At any time, you can add additional saplings into the turtle's inventory. The modem is only needed for GPS. Wireless network can be disabled via System->Networking. At any time, you can add additional saplings into the turtle's inventory. The modem is only needed for GPS. Wireless network can be disabled via System->Networking.

View File

@@ -1,23 +1,3 @@
--[[
Requirements:
Place turtle against an oak tree or oak sapling
Area around turtle must be flat and can only be dirt or grass
(10 blocks in each direction from turtle)
Turtle must have: crafting table, chest
Turtle must have a pick equipped on the LEFT side
Optional:
Add additional sapling types that can grow with a single sapling
Notes:
If the turtle does not get any saplings from the initial tree, place
down another sapling in front of the turtle.
The program will be able to survive server restarts as long as it has
created the cobblestone line. If the program is stopped before that time,
place the turtle in the original position before restarting the program.
]]--
local GPS = require('gps') local GPS = require('gps')
local Peripheral = require('peripheral') local Peripheral = require('peripheral')
local Point = require('point') local Point = require('point')
@@ -67,7 +47,8 @@ local LOG = 'minecraft:log'
local LOG2 = 'minecraft:log2' local LOG2 = 'minecraft:log2'
local OAK_LOG = 'minecraft:log:0' local OAK_LOG = 'minecraft:log:0'
local OAK_PLANK = 'minecraft:planks:0' local OAK_PLANK = 'minecraft:planks:0'
local SAPLING = 'minecraft:sapling:0' local OAK_SAPLING = 'minecraft:sapling:0'
local SAPLING = 'minecraft:sapling'
local SCANNER = 'plethora:module:2' local SCANNER = 'plethora:module:2'
local SENSOR = 'plethora:module:3' local SENSOR = 'plethora:module:3'
local STICK = 'minecraft:stick:0' local STICK = 'minecraft:stick:0'
@@ -75,7 +56,7 @@ local STONE = 'minecraft:stone:0'
local TORCH = 'minecraft:torch:0' local TORCH = 'minecraft:torch:0'
local ALL_SAPLINGS = { local ALL_SAPLINGS = {
SAPLING OAK_SAPLING
} }
local state = Util.readTable('usr/config/superTreefarm') or { local state = Util.readTable('usr/config/superTreefarm') or {
@@ -334,7 +315,7 @@ local function createFurnace()
if turtle.has(FURNACE) or craftItem(FURNACE) then if turtle.has(FURNACE) or craftItem(FURNACE) then
turtle.drop(COBBLESTONE) turtle.drop(COBBLESTONE)
local furnacePt = { x = GRID.BL.x + 2, y = 1, z = GRID.BL.z + 2 } local furnacePt = { x = GRID.BL.x + 1, y = 1, z = GRID.BL.z + 1 }
turtle.placeAt(furnacePt, FURNACE) turtle.placeAt(furnacePt, FURNACE)
setState('furnace', furnacePt) setState('furnace', furnacePt)
end end
@@ -444,14 +425,14 @@ local function placeTorches()
end end
local function randomSapling() local function randomSapling()
local sapling = SAPLING local sapling = OAK_SAPLING
if #state.trees > 1 then if #state.trees > 1 then
ALL_SAPLINGS = { } ALL_SAPLINGS = { }
local slots = turtle.getFilledSlots() local slots = turtle.getFilledSlots()
for _, slot in pairs(slots) do for _, slot in pairs(slots) do
if slot.name == 'minecraft:sapling' then if slot.name == SAPLING then
table.insert(ALL_SAPLINGS, slot.key) table.insert(ALL_SAPLINGS, slot.key)
end end
end end
@@ -463,6 +444,10 @@ local function randomSapling()
return sapling return sapling
end end
local function makeKey(b)
return table.concat({ b.x, b.y, b.z }, ':')
end
local function sense(pt, filter, blocks) local function sense(pt, filter, blocks)
turtle.pathfind(pt, { blocks = Util.shallowCopy(state.trees) }) turtle.pathfind(pt, { blocks = Util.shallowCopy(state.trees) })
@@ -476,7 +461,7 @@ local function sense(pt, filter, blocks)
b.y = Util.round(b.y) + turtle.point.y b.y = Util.round(b.y) + turtle.point.y
b.z = Util.round(b.z) + turtle.point.z b.z = Util.round(b.z) + turtle.point.z
if filter(b) then if filter(b) then
table.insert(acc, b) acc[makeKey(b)] = b
end end
end, blocks) end, blocks)
@@ -496,33 +481,28 @@ local function scan(pt, filter)
b.y = b.y + turtle.point.y b.y = b.y + turtle.point.y
b.z = b.z + turtle.point.z b.z = b.z + turtle.point.z
if filter(b) then if filter(b) then
table.insert(acc, b) acc[makeKey(b)] = b
end end
end, { }) end, { })
return blocks return blocks
end end
local function plantTrees() local function plantTrees(blocks)
local blocks = scan(HOME_PT, function(b) Util.each(state.trees, function(sapling)
return b.name == 'minecraft:sapling' local key = makeKey(sapling)
end) local b = blocks[key]
if b then
local plant = Util.reduce(state.trees, function(acc, b) if b.name == SAPLING then
for _, sapling in pairs(blocks) do blocks[key] = nil
if sapling.x == b.x and sapling.z == b.z then else
return b.plant = randomSapling()
end end
return
end end
table.insert(acc, b) b = Util.shallowCopy(sapling)
end, { }) b.plant = randomSapling()
blocks[key] = b
Point.eachClosest(turtle.point, plant, function(pt)
if turtle.moveAgainst(pt, { blocks = Util.shallowCopy(state.trees) }) then
turtle.digAt(pt)
turtle.placeAt(pt, randomSapling())
end
turtle.select(1)
end) end)
end end
@@ -547,8 +527,15 @@ local function fellTrees(blocks)
turtle.up() turtle.up()
end end
for pt in Point.iterateClosest(turtle.point, blocks) do for pt in Point.iterateClosest(turtle.point, blocks) do
if pt.sapling then if pt.y == 0 then
repeat until not turtle.suckDownAt(pt) if pt.sapling then
repeat until not turtle.suckDownAt(pt)
else
turtle.digDownAt(pt)
if pt.plant then
turtle.placeDown(pt.plant)
end
end
else else
turtle.digAt(pt) turtle.digAt(pt)
end end
@@ -561,34 +548,41 @@ local function fellTrees(blocks)
end end
local function fell() local function fell()
local leaves = false
local slots = turtle.getSummedInventory()
for _, sapling in pairs(ALL_SAPLINGS) do
if not slots[sapling] or slots[sapling].count < MIN_SAPLINGS then
leaves = true
break
end
end
local function filter(b) local function filter(b)
return b.y > 0 and (b.name == LOG or b.name == LOG2) return b.y >= 0 and (b.name == LOG or b.name == LOG2 or b.name == SAPLING)
end end
local function saplingFilter(b) local function saplingFilter(b)
if b.y == 0 and string.find(b.displayName, 'sapling', 1, true) then -- b.x > -6 so we don't take out the furnace
if b.y == 0 and b.x > -6 and string.find(b.displayName, 'sapling', 1, true) then
b.sapling = true b.sapling = true
return true return true
end end
end end
-- low scan
local blocks = scan(HOME_PT, filter) local blocks = scan(HOME_PT, filter)
if leaves then if not Util.every(blocks, function(b) return b.y < 6 end) then
-- tree might be above low scan range, do a scan higher up
turtle.setPolicy("digAttack")
blocks = scan(HIGH_PT, filter)
turtle.setPolicy("attackOnly")
end
-- determine if we need saplings
local slots = turtle.getSummedInventory()
if not Util.every(ALL_SAPLINGS, function(sapling)
return slots[sapling] and slots[sapling].count >= MIN_SAPLINGS
end) then
-- need saplings add sapling entities
sense(HOME_PT, saplingFilter, blocks) sense(HOME_PT, saplingFilter, blocks)
end end
if #blocks > 0 then -- add any locations that need saplings
plantTrees(blocks)
if not Util.empty(blocks) then
print('Chopping') print('Chopping')
local fuel = turtle.getFuelLevel() local fuel = turtle.getFuelLevel()
@@ -596,14 +590,11 @@ local function fell()
turtle.setPolicy("digAttack") turtle.setPolicy("digAttack")
fellTrees(blocks) fellTrees(blocks)
fellTrees(scan(HIGH_PT, filter))
turtle.pathfind(HOME_PT, { blocks = Util.shallowCopy(state.trees) }) turtle.pathfind(HOME_PT, { blocks = Util.shallowCopy(state.trees) })
turtle.setPolicy("attackOnly") turtle.setPolicy("attackOnly")
print('Used ' .. (fuel - turtle.getFuelLevel()) .. ' fuel') print('Used ' .. (fuel - turtle.getFuelLevel()) .. ' fuel')
plantTrees()
end end
return true return true
@@ -614,7 +605,7 @@ local function moreTrees()
return return
end end
if not state.chest or turtle.getItemCount('minecraft:sapling') < 15 then if not state.chest or turtle.getItemCount(OAK_SAPLING) < 15 then
return true return true
end end