wrong box

This commit is contained in:
kepler155c
2017-10-11 16:27:51 -04:00
parent f5dede540e
commit 5232b5a115

View File

@@ -1,13 +1,13 @@
requireInjector(getfenv(1)) _G.requireInjector()
local Logger = require('logger')
local Pathing = require('turtle.pathfind') local Pathing = require('turtle.pathfind')
local Point = require('point') local Point = require('point')
local Util = require('util') local Util = require('util')
if device and device.wireless_modem then local fs = _G.fs
Logger.setWirelessLogging() local read = _G.read
end local os = _G.os
local turtle = _G.turtle
local args = { ... } local args = { ... }
local options = { local options = {
@@ -37,25 +37,20 @@ local fortuneBlocks = {
local MIN_FUEL = 7500 local MIN_FUEL = 7500
local LOW_FUEL = 1500 local LOW_FUEL = 1500
local MAX_FUEL = 100000 local MAX_FUEL = turtle.getFuelLimit()
local PROGRESS_FILE = 'usr/config/mining.progress' local PROGRESS_FILE = 'usr/config/mining.progress'
local TRASH_FILE = 'usr/config/mining.trash' local TRASH_FILE = 'usr/config/mining.trash'
if not term.isColor() then
MAX_FUEL = 20000
end
local mining = { local mining = {
diameter = 1, diameter = 1,
chunkIndex = 0, chunkIndex = 0,
chunks = -1, chunks = -1,
} }
local trash local trash, boreDirection, unload
local boreDirection
function getChunkCoordinates(diameter, index, x, z) local function getChunkCoordinates(diameter, index, x, z)
local dirs = { -- circumference of grid local dirs = { -- circumference of grid
{ xd = 0, zd = 1, heading = 1 }, -- south { xd = 0, zd = 1, heading = 1 }, -- south
{ xd = -1, zd = 0, heading = 2 }, { xd = -1, zd = 0, heading = 2 },
@@ -68,13 +63,13 @@ function getChunkCoordinates(diameter, index, x, z)
dirs[4].z = z dirs[4].z = z
return dirs[4] return dirs[4]
end end
dir = dirs[math.floor(index / (diameter - 1)) + 1] local dir = dirs[math.floor(index / (diameter - 1)) + 1]
dir.x = x + dir.xd * 16 dir.x = x + dir.xd * 16
dir.z = z + dir.zd * 16 dir.z = z + dir.zd * 16
return dir return dir
end end
function getBoreLocations(x, z) local function getBoreLocations(x, z)
local locations = {} local locations = {}
@@ -116,7 +111,7 @@ function getBoreLocations(x, z)
end end
-- get the bore location closest to the miner -- get the bore location closest to the miner
local function getClosestLocation(points, b) local function getClosestLocation(points)
local key = 1 local key = 1
local leastMoves = 9000 local leastMoves = 9000
for k,pt in pairs(points) do for k,pt in pairs(points) do
@@ -134,11 +129,11 @@ local function getClosestLocation(points, b)
return table.remove(points, key) return table.remove(points, key)
end end
function getCornerOf(c) local function getCornerOf(c)
return math.floor(c.x / 16) * 16, math.floor(c.z / 16) * 16 return math.floor(c.x / 16) * 16, math.floor(c.z / 16) * 16
end end
function nextChunk() local function nextChunk()
local x, z = getCornerOf({ x = mining.x, z = mining.z }) local x, z = getCornerOf({ x = mining.x, z = mining.z })
local points = math.pow(mining.diameter, 2) - math.pow(mining.diameter-2, 2) local points = math.pow(mining.diameter, 2) - math.pow(mining.diameter-2, 2)
@@ -168,7 +163,7 @@ function nextChunk()
return true return true
end end
function addTrash() local function addTrash()
if not trash then if not trash then
trash = { } trash = { }
@@ -176,7 +171,7 @@ function addTrash()
local slots = turtle.getFilledSlots() local slots = turtle.getFilledSlots()
for k,slot in pairs(slots) do for _,slot in pairs(slots) do
trash[slot.iddmg] = true trash[slot.iddmg] = true
end end
@@ -184,17 +179,16 @@ function addTrash()
Util.writeTable(TRASH_FILE, trash) Util.writeTable(TRASH_FILE, trash)
end end
function log(text) local function log(text)
print(text) print(text)
Logger.log('mineWorker', text)
end end
function status(status) local function status(newStatus)
turtle.status = status turtle.status = newStatus
log(status) log(newStatus)
end end
function refuel() local function refuel()
if turtle.getFuelLevel() < MIN_FUEL then if turtle.getFuelLevel() < MIN_FUEL then
local oldStatus = turtle.status local oldStatus = turtle.status
status('refueling') status('refueling')
@@ -221,7 +215,51 @@ function refuel()
turtle.select(1) turtle.select(1)
end end
function enderChestUnload() local function safeGoto(x, z, y, h)
local oldStatus = turtle.status
-- only pathfind above or around other turtles (never down)
Pathing.setBox({ x = turtle.point.x, y = turtle.point.y, z = turtle.point.z, ex = x, ey = y, ez = z })
while not turtle.pathfind({ x = x, z = z, y = y or turtle.point.y, heading = h }) do
--status('stuck')
if turtle.abort then
return false
end
os.sleep(3)
end
turtle.status = oldStatus
return true
end
local function safeGotoY(y)
local oldStatus = turtle.status
while not turtle.gotoY(y) do
status('stuck')
if turtle.abort then
return false
end
os.sleep(1)
end
turtle.status = oldStatus
return true
end
local function makeWalkableTunnel(action, tpt, pt)
if action ~= 'turn' and not Point.compare(tpt, { x = 0, z = 0 }) then -- not at source
if not Point.compare(tpt, pt) then -- not at dest
local r, block = turtle.inspectUp()
if r and not turtle.isTurtleAtSide('top') then
if block.name ~= 'minecraft:cobblestone' and
block.name ~= 'minecraft:chest' then
turtle.digUp()
end
end
end
end
end
--[[
local function enderChestUnload()
log('unloading') log('unloading')
turtle.select(1) turtle.select(1)
if not Util.tryTimed(5, function() if not Util.tryTimed(5, function()
@@ -237,51 +275,9 @@ function enderChestUnload()
turtle.digDown() turtle.digDown()
end end
end end
]]
function safeGoto(x, z, y, h) local function normalChestUnload()
local oldStatus = turtle.status
-- only pathfind above or around other turtles (never down)
Pathing.setBox({ x = 0, y = 0, z = 0, ex = x, ey = y + 1, ez = z })
while not turtle.pathfind({ x = x, z = z, y = y or turtle.point.y, heading = h }) do
--status('stuck')
if turtle.abort then
return false
end
--os.sleep(1)
end
turtle.status = oldStatus
return true
end
function safeGotoY(y)
local oldStatus = turtle.status
while not turtle.gotoY(y) do
status('stuck')
if turtle.abort then
return false
end
os.sleep(1)
end
turtle.status = oldStatus
return true
end
function makeWalkableTunnel(action, tpt, pt)
if action ~= 'turn' and not Point.compare(tpt, { x = 0, z = 0 }) then -- not at source
if not Point.compare(tpt, pt) then -- not at dest
local r, block = turtle.inspectUp()
if r and not turtle.isTurtleAtSide('top') then
if block.name ~= 'minecraft:cobblestone' and
block.name ~= 'minecraft:chest' then
turtle.digUp()
end
end
end
end
end
function normalChestUnload()
local oldStatus = turtle.status local oldStatus = turtle.status
status('unloading') status('unloading')
local pt = Util.shallowCopy(turtle.point) local pt = Util.shallowCopy(turtle.point)
@@ -317,7 +313,7 @@ function normalChestUnload()
status(oldStatus) status(oldStatus)
end end
function ejectTrash() local function ejectTrash()
local cobbleSlotCount = 0 local cobbleSlotCount = 0
@@ -340,7 +336,35 @@ function ejectTrash()
end) end)
end end
function mineable(action) local function checkSpace()
if turtle.getItemCount(16) > 0 then
refuel()
local oldStatus = turtle.status
status('condensing')
ejectTrash()
turtle.condense()
local lastSlot = 16
if boreDirection == 'down' then
lastSlot = 15
end
if turtle.getItemCount(lastSlot) > 0 then
unload()
end
status(oldStatus)
turtle.select(1)
end
end
local function collectDrops(suckAction)
for _ = 1, 50 do
if not suckAction() then
break
end
checkSpace()
end
end
local function mineable(action)
local r, block = action.inspect() local r, block = action.inspect()
if not r then if not r then
return false return false
@@ -375,12 +399,12 @@ function mineable(action)
return block.name return block.name
end end
function fortuneDig(action, blockName) local function fortuneDig(action, blockName)
if options.fortunePick.value and fortuneBlocks[blockName] then if options.fortunePick.value and fortuneBlocks[blockName] then
turtle.select('cctweaks:toolHost') turtle.select('cctweaks:toolHost')
turtle.equipRight() turtle.equipRight()
turtle.select(options.fortunePick.value) turtle.select(options.fortunePick.value)
repeat until not turtle.dig() repeat until not action.dig()
turtle.select('minecraft:diamond_pickaxe') turtle.select('minecraft:diamond_pickaxe')
turtle.equipRight() turtle.equipRight()
turtle.select(1) turtle.select(1)
@@ -388,7 +412,7 @@ function fortuneDig(action, blockName)
end end
end end
function mine(action) local function mine(action)
local blockName = mineable(action) local blockName = mineable(action)
if blockName then if blockName then
checkSpace() checkSpace()
@@ -399,7 +423,7 @@ function mine(action)
end end
end end
function bore() local function bore()
local loc = turtle.point local loc = turtle.point
local level = loc.y local level = loc.y
@@ -417,10 +441,6 @@ function bore()
break break
end end
if turtle.point.y < -2 then
-- turtle.setDigPolicy(turtle.digPolicies.turtleSafe)
end
mine(turtle.getAction('down')) mine(turtle.getAction('down'))
if not Util.tryTimed(3, turtle.down) then if not Util.tryTimed(3, turtle.down) then
break break
@@ -450,10 +470,6 @@ function bore()
return false return false
end end
if turtle.point.y > -2 then
-- turtle.setDigPolicy(turtle.digPolicies.turtleSafe)
end
while not Util.tryTimed(3, turtle.up) do while not Util.tryTimed(3, turtle.up) do
status('stuck') status('stuck')
end end
@@ -482,34 +498,6 @@ function bore()
return true return true
end end
function checkSpace()
if turtle.getItemCount(16) > 0 then
refuel()
local oldStatus = turtle.status
status('condensing')
ejectTrash()
turtle.condense()
local lastSlot = 16
if boreDirection == 'down' then
lastSlot = 15
end
if turtle.getItemCount(lastSlot) > 0 then
unload()
end
status(oldStatus)
turtle.select(1)
end
end
function collectDrops(suckAction)
for i = 1, 50 do
if not suckAction() then
break
end
checkSpace()
end
end
function Point.compare(pta, ptb) function Point.compare(pta, ptb)
if pta.x == ptb.x and pta.z == ptb.z then if pta.x == ptb.x and pta.z == ptb.z then
if pta.y and ptb.y then if pta.y and ptb.y then
@@ -520,15 +508,15 @@ function Point.compare(pta, ptb)
return false return false
end end
function inspect(action, name) local function inspect(action, name)
local r, block = action.inspect() local r, block = action.inspect()
if r and block.name == name then if r and block.name == name then
return true return true
end end
end end
function boreCommand() local function boreCommand()
local pt = getClosestLocation(mining.locations, turtle.point) local pt = getClosestLocation(mining.locations)
turtle.setMoveCallback(function(action, tpt) turtle.setMoveCallback(function(action, tpt)
makeWalkableTunnel(action, tpt, pt) makeWalkableTunnel(action, tpt, pt)
@@ -628,15 +616,13 @@ turtle.run(function()
turtle.reset() turtle.reset()
turtle.setPolicy(turtle.policies.digAttack) turtle.setPolicy(turtle.policies.digAttack)
turtle.setDigPolicy(turtle.digPolicies.turtleSafe) turtle.setDigPolicy(turtle.digPolicies.turtleSafe)
unload() unload()
status('mining') status('mining')
local s, m = pcall(function() main() end) local s, m = pcall(function() main() end)
if not s and m then if not s and m then
printError(m) _G.printError(m)
end end
turtle.abort = false turtle.abort = false
safeGotoY(0) safeGotoY(0)
safeGoto(0, 0, 0, 0) safeGoto(0, 0, 0, 0)