refactor + cleanup

This commit is contained in:
kepler155c
2017-10-27 20:25:16 -04:00
parent 9939c75fc3
commit d97a9d7468
17 changed files with 261 additions and 299 deletions

View File

@@ -1,4 +1,4 @@
local Blocks = require('blocks') local Blocks = require('builder.blocks')
local class = require('class') local class = require('class')
local Message = require('message') local Message = require('message')
local Util = require('util') local Util = require('util')
@@ -10,13 +10,9 @@ local turtle = _G.turtle
local Builder = class() local Builder = class()
Util.merge(Builder, { Util.merge(Builder, {
isCommandComputer = not turtle, isCommandComputer = not turtle,
slots = { },
loc = { }, loc = { },
index = 1, index = 1,
mode = 'build', mode = 'build',
fuelItem = { id = 'minecraft:coal', dmg = 0 },
resourceSlots = 14,
facing = 'south',
}) })
local BUILDER_DIR = 'usr/builder' local BUILDER_DIR = 'usr/builder'
@@ -87,7 +83,7 @@ end
function Builder:saveProgress(index) function Builder:saveProgress(index)
Util.writeTable( Util.writeTable(
fs.combine(BUILDER_DIR, self.schematic.filename .. '.progress'), fs.combine(BUILDER_DIR, self.schematic.filename .. '.progress'),
{ index = index, facing = self.facing, loc = self.loc } { index = index, loc = self.loc }
) )
end end
@@ -98,7 +94,6 @@ function Builder:loadProgress(filename)
if self.index > #self.schematic.blocks then if self.index > #self.schematic.blocks then
self.index = 1 self.index = 1
end end
self.facing = progress.facing or 'south'
self.loc = progress.loc or { } self.loc = progress.loc or { }
end end
end end

View File

@@ -12,6 +12,11 @@ function Builder:begin()
local last = #self.schematic.blocks local last = #self.schematic.blocks
local throttle = Util.throttle() local throttle = Util.throttle()
local cx, cy, cz = commands.getBlockPosition()
if self.loc.x then
cx, cy, cz = self.loc.rx, self.loc.ry, self.loc.rz
end
if self.mode == 'destroy' then if self.mode == 'destroy' then
direction = -1 direction = -1
last = 1 last = 1
@@ -32,12 +37,10 @@ function Builder:begin()
end end
local function placeBlock(bid, dmg, x, y, z) local function placeBlock(bid, dmg, x, y, z)
local cx, _, cz = commands.getBlockPosition()
local command = table.concat({ local command = table.concat({
"setblock", "setblock",
cx + x + 1, cx + x + 1,
"~" .. y, cy + y,
cz + z + 1, cz + z + 1,
bid, bid,
dmg, dmg,

View File

@@ -5,31 +5,11 @@ local Point = require('point')
local bit = _G.bit local bit = _G.bit
local fs = _G.fs local fs = _G.fs
local os = _G.os
local term = _G.term local term = _G.term
local headings = {
[ 0 ] = { xd = 1, zd = 0, yd = 0, heading = 0, direction = 'east' },
[ 1 ] = { xd = 0, zd = 1, yd = 0, heading = 1, direction = 'south' },
[ 2 ] = { xd = -1, zd = 0, yd = 0, heading = 2, direction = 'west' },
[ 3 ] = { xd = 0, zd = -1, yd = 0, heading = 3, direction = 'north' },
[ 4 ] = { xd = 0, zd = 0, yd = 1, heading = 4, direction = 'up' },
[ 5 ] = { xd = 0, zd = 0, yd = -1, heading = 5, direction = 'down' }
}
local namedHeadings = {
east = headings[0],
south = headings[1],
west = headings[2],
north = headings[3],
up = headings[4],
down = headings[5]
}
local function getHeadingInfo(heading) local function getHeadingInfo(heading)
if heading and type(heading) == 'string' then return Point.headings[heading]
return namedHeadings[heading]
end
return headings[heading]
end end
--[[ --[[
@@ -86,7 +66,7 @@ function Spinner:stop(text)
end end
local Schematic = class() local Schematic = class()
function Schematic:init(args) function Schematic:init()
self.blocks = { } self.blocks = { }
self.damages = { } self.damages = { }
self.originalBlocks = { } self.originalBlocks = { }
@@ -122,7 +102,7 @@ function Schematic:readname(h)
local n = n1*256 + n2 local n = n1*256 + n2
local str = "" local str = ""
for i=1,n do for _=1,n do
local c = h:readbyte(h) local c = h:readbyte(h)
if c == nil then if c == nil then
return return

View File

@@ -15,7 +15,13 @@ local read = _G.read
local rs = _G.rs local rs = _G.rs
local turtle = _G.turtle local turtle = _G.turtle
local RESOURCE_SLOTS = 14
local FUEL_ITEM = { id = 'minecraft:coal', dmg = 0 }
local TurtleBuilder = class(Builder) local TurtleBuilder = class(Builder)
Util.merge(TurtleBuilder, {
slots = { },
})
-- Temp functions until conversion to new adapters is complete -- Temp functions until conversion to new adapters is complete
local function convertSingleForward(item) local function convertSingleForward(item)
@@ -92,7 +98,7 @@ function supplyPage:eventHandler(event)
self.builder:dumpInventory() self.builder:dumpInventory()
--Builder.status = 'idle' --Builder.status = 'idle'
UI:setPage('start') UI:setPage('start')
turtle.status = 'idle' turtle.setStatus('waiting')
elseif event.type == 'grid_focus_row' then elseif event.type == 'grid_focus_row' then
self.statusBar:setValue('help', event.selected.id .. ':' .. event.selected.dmg) self.statusBar:setValue('help', event.selected.id .. ':' .. event.selected.dmg)
@@ -164,7 +170,7 @@ function TurtleBuilder:getBlockCounts()
wrench.need = 1 wrench.need = 1
blocks[wrench.id .. ':' .. wrench.dmg] = wrench blocks[wrench.id .. ':' .. wrench.dmg] = wrench
local fuel = self.subDB:getSubstitutedItem(Builder.fuelItem.id, Builder.fuelItem.dmg) local fuel = self.subDB:getSubstitutedItem(FUEL_ITEM.id, FUEL_ITEM.dmg)
fuel.qty = 0 fuel.qty = 0
fuel.need = 1 fuel.need = 1
blocks[fuel.id .. ':' .. fuel.dmg] = fuel blocks[fuel.id .. ':' .. fuel.dmg] = fuel
@@ -200,7 +206,7 @@ function TurtleBuilder:getAirResupplyList(blockIndex)
local slots = { } local slots = { }
if self.mode == 'destroy' then if self.mode == 'destroy' then
for i = 1, self.resourceSlots do for i = 1, RESOURCE_SLOTS do
slots[i] = { slots[i] = {
qty = 0, qty = 0,
need = 0, need = 0,
@@ -211,7 +217,7 @@ function TurtleBuilder:getAirResupplyList(blockIndex)
slots = self:getGenericSupplyList(blockIndex) slots = self:getGenericSupplyList(blockIndex)
end end
local fuel = self.subDB:getSubstitutedItem(self.fuelItem.id, self.fuelItem.dmg) local fuel = self.subDB:getSubstitutedItem(FUEL_ITEM.id, FUEL_ITEM.dmg)
slots[15] = { slots[15] = {
id = 'minecraft:chest', id = 'minecraft:chest',
@@ -260,7 +266,7 @@ end
function TurtleBuilder:getGenericSupplyList(blockIndex) function TurtleBuilder:getGenericSupplyList(blockIndex)
local slots = { } local slots = { }
for i = 1, self.resourceSlots do for i = 1, RESOURCE_SLOTS do
slots[i] = { slots[i] = {
qty = 0, qty = 0,
need = 0, need = 0,
@@ -397,13 +403,13 @@ function TurtleBuilder:refuel()
print('Refueling') print('Refueling')
turtle.select(1) turtle.select(1)
local fuel = self.subDB:getSubstitutedItem(self.fuelItem.id, self.fuelItem.dmg) local fuel = self.subDB:getSubstitutedItem(FUEL_ITEM.id, FUEL_ITEM.dmg)
self.itemAdapter:provide(convertSingleForward(fuel), 64, 1) self.itemAdapter:provide(convertSingleForward(fuel), 64, 1)
if turtle.getItemCount(1) == 0 then if turtle.getItemCount(1) == 0 then
print('Out of fuel, add fuel to chest/ME system') print('Out of fuel, add fuel to chest/ME system')
turtle.setHeading(0) turtle.setHeading(0)
turtle.status = 'waiting' turtle.setStatus('waiting')
os.sleep(5) os.sleep(5)
else else
turtle.refuel(64) turtle.refuel(64)
@@ -426,7 +432,7 @@ function TurtleBuilder:inAirDropoff()
return false return false
end end
turtle.status = 'waiting' turtle.setStatus('waiting')
if msg.contents.point then if msg.contents.point then
local pt = msg.contents.point local pt = msg.contents.point
@@ -488,7 +494,7 @@ function TurtleBuilder:inAirResupply()
return false return false
end end
turtle.status = 'waiting' turtle.setStatus('waiting')
if msg.contents.point then if msg.contents.point then
local pt = msg.contents.point local pt = msg.contents.point
@@ -609,7 +615,7 @@ function TurtleBuilder:resupply()
return return
end end
turtle.status = 'resupplying' turtle.setStatus('resupplying')
self:log('Resupplying') self:log('Resupplying')
self:gotoSupplyPoint() self:gotoSupplyPoint()
@@ -653,11 +659,11 @@ end
-- figure out our orientation in the world -- figure out our orientation in the world
function TurtleBuilder:getTurtleFacing() function TurtleBuilder:getTurtleFacing()
local directions = { -- reversed directions local directions = {
[5] = 'west', [5] = 2,
[3] = 'north', [3] = 3,
[4] = 'east', [4] = 0,
[2] = 'south', [2] = 1,
} }
local function getItem(item) local function getItem(item)
@@ -730,7 +736,7 @@ function TurtleBuilder:wrenchBlock(side, facing, cache)
} }
if turtle.getHeadingInfo(facing).heading < 4 then if turtle.getHeadingInfo(facing).heading < 4 then
local offsetDirection = (turtle.getHeadingInfo(self.facing).heading + local offsetDirection = (self.facing +
turtle.getHeadingInfo(facing).heading) % 4 turtle.getHeadingInfo(facing).heading) % 4
facing = turtle.getHeadingInfo(offsetDirection).direction facing = turtle.getHeadingInfo(offsetDirection).direction
end end
@@ -754,17 +760,12 @@ function TurtleBuilder:wrenchBlock(side, facing, cache)
end end
function TurtleBuilder:rotateBlock(side, facing) function TurtleBuilder:rotateBlock(side, facing)
local s = self:getWrenchSlot() if self:getWrenchSlot() then
for _ = 1, facing do
if not s then turtle.getAction(side).place()
return false end
return true
end end
for _ = 1, facing do
turtle.getAction(side).place()
end
return true
end end
-- place piston, wrench piston to face downward, extend, remove piston -- place piston, wrench piston to face downward, extend, remove piston
@@ -813,7 +814,7 @@ function TurtleBuilder:_goto(x, z, y, heading)
print('stuck') print('stuck')
print('Press enter to continue') print('Press enter to continue')
os.sleep(1) os.sleep(1)
turtle.status = 'stuck' turtle.setStatus('stuck')
read() read()
end end
end end
@@ -902,7 +903,7 @@ function TurtleBuilder:placeDirectionalBlock(b, slot, travelPlane)
[ 'west-up' ] = 'east' [ 'west-up' ] = 'east'
} }
if stairUpDirections[d] then if stairUpDirections[d] then
local isSouth = (turtle.getHeadingInfo(self.facing).heading + local isSouth = (self.facing +
turtle.getHeadingInfo(stairUpDirections[d]).heading) % 4 == 1 turtle.getHeadingInfo(stairUpDirections[d]).heading) % 4 == 1
if not self.stairBug then if not self.stairBug then
@@ -1091,10 +1092,10 @@ function TurtleBuilder:build()
if self.mode == 'destroy' then if self.mode == 'destroy' then
direction = -1 direction = -1
last = 1 last = 1
turtle.status = 'destroying' turtle.setStatus('destroying')
else else
travelPlane = self:findTravelPlane(self.index) travelPlane = self:findTravelPlane(self.index)
turtle.status = 'building' turtle.setStatus('building')
end end
local pt = self:getBuildingCorner() local pt = self:getBuildingCorner()
@@ -1120,13 +1121,13 @@ function TurtleBuilder:build()
-- if no supplier, then should fill all slots -- if no supplier, then should fill all slots
if turtle.getItemCount(self.resourceSlots) > 0 or turtle.getFuelLevel() < minFuel then if turtle.getItemCount(RESOURCE_SLOTS) > 0 or turtle.getFuelLevel() < minFuel then
if turtle.getFuelLevel() < minFuel or not self:inAirDropoff() then if turtle.getFuelLevel() < minFuel or not self:inAirDropoff() then
self:gotoSupplyPoint() self:gotoSupplyPoint()
self:dumpInventoryWithCheck() self:dumpInventoryWithCheck()
self:refuel() self:refuel()
end end
turtle.status = 'destroying' turtle.setStatus('destroying')
end end
else -- Build mode else -- Build mode
@@ -1177,9 +1178,9 @@ function TurtleBuilder:build()
throttle() -- sleep in case there are a large # of skipped blocks throttle() -- sleep in case there are a large # of skipped blocks
end end
if turtle.abort then if turtle.isAborted() then
turtle.status = 'aborting' turtle.setStatus('aborting')
turtle.abort = false turtle.abort(false)
self:gotoTravelPlane(travelPlane) self:gotoTravelPlane(travelPlane)
self:gotoSupplyPoint() self:gotoSupplyPoint()
turtle.setHeading(0) turtle.setHeading(0)
@@ -1224,8 +1225,7 @@ function TurtleBuilder:begin()
self:refuel() self:refuel()
self:getTurtleFacing() self:getTurtleFacing()
local facing = turtle.getHeadingInfo(self.facing).heading Point.rotate(self.supplyPoint, self.facing)
Point.rotate(self.supplyPoint, facing)
turtle.setPoint(self.supplyPoint) turtle.setPoint(self.supplyPoint)
-- reset piston cache in case wrench was substituted -- reset piston cache in case wrench was substituted

View File

@@ -161,7 +161,7 @@ return function(startPt, endPt, firstPt, verbose)
if not turtle.gotoPoint(node) then if not turtle.gotoPoint(node) then
break break
end end
until turtle.abort until turtle.isAborted()
turtle.resetState() turtle.resetState()
turtle.setMoveCallback(oldCallback) turtle.setMoveCallback(oldCallback)

View File

@@ -8,7 +8,7 @@ local Adapter = require('inventoryAdapter')
local Event = require('event') local Event = require('event')
local GPS = require('gps') local GPS = require('gps')
local itemDB = require('itemDB') local itemDB = require('itemDB')
local Schematic = require('schematic') local Schematic = require('builder.schematic')
local TableDB = require('tableDB') local TableDB = require('tableDB')
local UI = require('ui') local UI = require('ui')
local Util = require('util') local Util = require('util')
@@ -16,7 +16,6 @@ local Util = require('util')
local colors = _G.colors local colors = _G.colors
local fs = _G.fs local fs = _G.fs
local multishell = _ENV.multishell local multishell = _ENV.multishell
local turtle = _G.turtle
local BUILDER_DIR = 'usr/builder' local BUILDER_DIR = 'usr/builder'
@@ -637,11 +636,15 @@ function startPage:eventHandler(event)
elseif event.type == 'startPoint' then elseif event.type == 'startPoint' then
local loc = Util.shallowCopy(Builder.loc) local loc = Util.shallowCopy(Builder.loc)
if not loc.x then if not loc.x then
local pt = GPS.getPoint() if _G.turtle then
if pt then local pt = GPS.getPoint()
loc.x = pt.x if pt then
loc.y = pt.y loc.x = pt.x
loc.z = pt.z loc.y = pt.y
loc.z = pt.z
end
elseif _G.commands then
loc.x, loc.y, loc.z = _G.commands.getBlockPosition()
end end
end end
@@ -775,12 +778,4 @@ UI:setPages({
UI:setPage('start') UI:setPage('start')
if Builder.isCommandComputer then UI:pullEvents()
UI:pullEvents()
else
UI:pullEvents()
-- turtle.run(function()
-- turtle.reset()
-- UI:pullEvents()
-- end)
end

View File

@@ -252,9 +252,9 @@ page:setVolume(page.volume, true)
UI:setPage(page) UI:setPage(page)
turtle.status = 'Jamming' turtle.setStatus('Jamming')
UI:pullEvents() UI:pullEvents()
turtle.status = 'idle' turtle.setStatus('idle')
page:play(false) page:play(false)
UI.term:reset() UI.term:reset()

View File

@@ -1,12 +1,17 @@
requireInjector(getfenv(1)) _G.requireInjector()
local Event = require('event') local Event = require('event')
local GPS = require('gps')
local ChestAdapter = require('chestAdapter18') local ChestAdapter = require('chestAdapter18')
local Point = require('point') local Point = require('point')
local Socket = require('socket') local Socket = require('socket')
local Util = require('util') local Util = require('util')
local device = _G.device
local os = _G.os
local peripheral = _G.peripheral
local printError = _G.printError
local turtle = _G.turtle
if not device.wireless_modem then if not device.wireless_modem then
error('Modem is required') error('Modem is required')
end end
@@ -35,7 +40,7 @@ local fuel = {
local slots local slots
turtle.setMoveCallback(function(action, pt) turtle.setMoveCallback(function()
if slots then if slots then
for _,slot in pairs(slots) do for _,slot in pairs(slots) do
if turtle.getItemCount(slot.index) ~= slot.qty then if turtle.getItemCount(slot.index) ~= slot.qty then
@@ -46,10 +51,38 @@ turtle.setMoveCallback(function(action, pt)
end end
end) end)
function refuel() local function gotoPoint(pt, doDetect)
slots = turtle.getInventory()
while not turtle.pathfind(pt, { blocks = blocks }) do
if turtle.isAborted() then
error('aborted')
end
turtle.setStatus('blocked')
os.sleep(5)
end
if doDetect and not turtle.detectDown() then
printError('Missing target')
Event.exitPullEvents()
end
end
local function dropOff(pt)
if turtle.selectSlotWithItems() then
gotoPoint(pt, true)
turtle.emptyInventory(turtle.dropDown)
if pt == locations.dropPt then
print('refreshing items')
local chestAdapter = ChestAdapter()
items = chestAdapter:refresh()
end
end
end
local function refuel()
if turtle.getFuelLevel() < 5000 and locations.dropPt then if turtle.getFuelLevel() < 5000 and locations.dropPt then
print('refueling') print('refueling')
turtle.status = 'refueling' turtle.setStatus('refueling')
gotoPoint(locations.dropPt, true) gotoPoint(locations.dropPt, true)
dropOff(locations.dropPt) dropOff(locations.dropPt)
local chestAdapter = ChestAdapter({ local chestAdapter = ChestAdapter({
@@ -66,8 +99,8 @@ function refuel()
end end
end end
function pickUp(pt) local function pickUp(pt)
turtle.status = 'picking up' turtle.setStatus('picking up')
gotoPoint(pt, true) gotoPoint(pt, true)
while true do while true do
if not turtle.selectOpenSlot() then if not turtle.selectOpenSlot() then
@@ -81,41 +114,13 @@ function pickUp(pt)
end end
end end
function dropOff(pt) local function checkCell(pt)
if turtle.selectSlotWithItems() then
gotoPoint(pt, true)
turtle.emptyInventory(turtle.dropDown)
if pt == locations.dropPt then
print('refreshing items')
chestAdapter = ChestAdapter()
items = chestAdapter:refresh()
end
end
end
function gotoPoint(pt, doDetect)
slots = turtle.getInventory()
while not turtle.pathfind(pt, { blocks = blocks }) do
if turtle.abort then
error('aborted')
end
turtle.status = 'blocked'
os.sleep(5)
end
if doDetect and not turtle.detectDown() then
printError('Missing target')
Event.exitPullEvents()
end
end
function checkCell(pt)
if not turtle.selectOpenSlot() then if not turtle.selectOpenSlot() then
dropOff(locations.dropPt) dropOff(locations.dropPt)
end end
print('checking cell') print('checking cell')
turtle.status = 'recharging' turtle.setStatus('recharging')
gotoPoint(pt, true) gotoPoint(pt, true)
local c = peripheral.wrap('bottom') local c = peripheral.wrap('bottom')
local energy = c.getMaxEnergyStored() - local energy = c.getMaxEnergyStored() -
@@ -136,9 +141,9 @@ function checkCell(pt)
end end
end end
function fluid(points) local function fluid(points)
print('checking fluid') print('checking fluid')
turtle.status = 'fluiding' turtle.setStatus('fluiding')
gotoPoint(points.source, true) gotoPoint(points.source, true)
turtle.select(1) turtle.select(1)
turtle.digDown() turtle.digDown()
@@ -152,10 +157,10 @@ function fluid(points)
turtle.placeDown() turtle.placeDown()
end end
function refill(entry) local function refill(entry)
dropOff(locations.dropPt) dropOff(locations.dropPt)
turtle.status = 'refilling' turtle.setStatus('refilling')
gotoPoint(locations.dropPt) gotoPoint(locations.dropPt)
local chestAdapter = ChestAdapter() local chestAdapter = ChestAdapter()
for _,item in pairs(entry.items) do for _,item in pairs(entry.items) do
@@ -169,21 +174,6 @@ function refill(entry)
end end
end end
function oldRefill(points)
gotoPoint(points.source)
repeat until not turtle.suckDown(64)
if points.target then
dropOff(points.target)
end
if points.targets then
for k,target in pairs(points.targets) do
dropOff(target)
end
end
dropOff(points.source)
dropOff(locations.dropPt)
end
local function makeKey(pt) local function makeKey(pt)
return string.format('%d:%d:%d', pt.x, pt.y, pt.z) return string.format('%d:%d:%d', pt.x, pt.y, pt.z)
end end
@@ -198,7 +188,7 @@ local function pickupHost(socket)
end end
print('command: ' .. data.type) print('command: ' .. data.type)
if data.type == 'pickup' then if data.type == 'pickup' then
local key = makeKey(data.point) local key = makeKey(data.point)
locations.pickups[key] = data.point locations.pickups[key] = data.point
@@ -207,7 +197,7 @@ local function pickupHost(socket)
elseif data.type == 'items' then elseif data.type == 'items' then
socket:write( { type = "response", response = items }) socket:write( { type = "response", response = items })
elseif data.type == 'refill' then elseif data.type == 'refill' then
local key = makeKey(data.entry.point) local key = makeKey(data.entry.point)
locations.refills[key] = data.entry locations.refills[key] = data.entry
@@ -223,14 +213,12 @@ local function pickupHost(socket)
locations.chargePt = data.point locations.chargePt = data.point
Util.writeTable('/usr/config/pickup', locations) Util.writeTable('/usr/config/pickup', locations)
socket:write( { type = "response", response = 'Location set' }) socket:write( { type = "response", response = 'Location set' })
elseif data.type == 'charge' then elseif data.type == 'charge' then
local key = makeKey(data.point) local key = makeKey(data.point)
locations.cells[key] = data.point locations.cells[key] = data.point
Util.writeTable('/usr/config/pickup', locations) Util.writeTable('/usr/config/pickup', locations)
socket:write( { type = "response", response = 'added' }) socket:write( { type = "response", response = 'added' })
elseif data.type == 'fluid' then
elseif data.type == 'clear' then elseif data.type == 'clear' then
local key = makeKey(data.point) local key = makeKey(data.point)
@@ -240,7 +228,7 @@ local function pickupHost(socket)
locations.pickups[key] = nil locations.pickups[key] = nil
Util.writeTable('/usr/config/pickup', locations) Util.writeTable('/usr/config/pickup', locations)
socket:write( { type = "response", response = 'cleared' }) socket:write( { type = "response", response = 'cleared' })
else else
print('unknown command') print('unknown command')
@@ -264,7 +252,7 @@ local function eachEntry(t, fn)
local keys = Util.keys(t) local keys = Util.keys(t)
for _,key in pairs(keys) do for _,key in pairs(keys) do
if t[key] then if t[key] then
if turtle.abort then if turtle.isAborted() then
return return
end end
fn(t[key]) fn(t[key])
@@ -283,7 +271,7 @@ local function eachClosestEntry(t, fn)
while not Util.empty(points) do while not Util.empty(points) do
local closest = Point.closest(turtle.point, points) local closest = Point.closest(turtle.point, points)
if turtle.abort then if turtle.isAborted() then
return return
end end
if t[closest.key] then if t[closest.key] then
@@ -299,7 +287,6 @@ local function eachClosestEntry(t, fn)
end end
Event.addRoutine(function() Event.addRoutine(function()
if not turtle.enableGPS() then if not turtle.enableGPS() then
error('turtle: No GPS found') error('turtle: No GPS found')
end end
@@ -318,8 +305,8 @@ Event.addRoutine(function()
eachEntry(locations.cells, checkCell) eachEntry(locations.cells, checkCell)
end end
print('sleeping') print('sleeping')
turtle.status = 'sleeping' turtle.setStatus('sleeping')
if turtle.abort then if turtle.isAborted() then
printError('aborted') printError('aborted')
break break
end end

View File

@@ -46,7 +46,7 @@ local function refuel()
itemAdapter:provide({ name = 'minecraft:coal', damage = 0 }, 64, 1) itemAdapter:provide({ name = 'minecraft:coal', damage = 0 }, 64, 1)
if turtle.getItemCount(1) == 0 then if turtle.getItemCount(1) == 0 then
print('Out of fuel, add fuel to chest/ME system') print('Out of fuel, add fuel to chest/ME system')
turtle.status = 'waiting' turtle.setStatus('waiting')
os.sleep(5) os.sleep(5)
else else
turtle.refuel(64) turtle.refuel(64)
@@ -212,12 +212,12 @@ local function clear()
if sy > osy then if sy > osy then
turtle.digDown() turtle.digDown()
end end
if turtle.abort then if turtle.isAborted() then
break break
end end
end end
if turtle.abort then if turtle.isAborted() then
break break
end end
if sy + 1 >= ey then if sy + 1 >= ey then
@@ -231,7 +231,7 @@ local function clear()
end end
turtle.run(function() turtle.run(function()
turtle.status = 'Clearing' turtle.setStatus('Clearing')
if turtle.enableGPS() then if turtle.enableGPS() then
@@ -255,7 +255,7 @@ local Level = require('turtle.level')
local Util = require('util') local Util = require('util')
local s, m = turtle.run(function() local s, m = turtle.run(function()
turtle.status = 'Leveling' turtle.setStatus('Leveling')
if turtle.enableGPS() then if turtle.enableGPS() then
@@ -379,7 +379,7 @@ function page:eventHandler(event)
end end
self.statusBar:setStatus('') self.statusBar:setStatus('')
elseif event.type == 'cancel' then elseif event.type == 'cancel' then
self:runFunction(turtleId, 'turtle.abortAction()') self:runFunction(turtleId, 'turtle.abort(true)')
self.statusBar:setStatus('') self.statusBar:setStatus('')
else else
return UI.Page.eventHandler(self, event) return UI.Page.eventHandler(self, event)

View File

@@ -184,13 +184,13 @@ local function log(text)
end end
local function status(newStatus) local function status(newStatus)
turtle.status = newStatus turtle.setStatus(newStatus)
log(newStatus) log(newStatus)
end end
local function refuel() local function refuel()
if turtle.getFuelLevel() < MIN_FUEL then if turtle.getFuelLevel() < MIN_FUEL then
local oldStatus = turtle.status local oldStatus = turtle.getStatus()
status('refueling') status('refueling')
if turtle.select('minecraft:coal:0') then if turtle.select('minecraft:coal:0') then
@@ -216,31 +216,31 @@ local function refuel()
end end
local function safeGoto(x, z, y, h) local function safeGoto(x, z, y, h)
local oldStatus = turtle.status local oldStatus = turtle.getStatus()
-- only pathfind above or around other turtles (never down) -- 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 }) 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 while not turtle.pathfind({ x = x, z = z, y = y or turtle.point.y, heading = h }) do
--status('stuck') --status('stuck')
if turtle.abort then if turtle.isAborted() then
return false return false
end end
os.sleep(3) os.sleep(3)
end end
turtle.status = oldStatus turtle.setStatus(oldStatus)
return true return true
end end
local function safeGotoY(y) local function safeGotoY(y)
local oldStatus = turtle.status local oldStatus = turtle.getStatus()
while not turtle.gotoY(y) do while not turtle.gotoY(y) do
status('stuck') status('stuck')
if turtle.abort then if turtle.isAborted() then
return false return false
end end
os.sleep(1) os.sleep(1)
end end
turtle.status = oldStatus turtle.setStatus(oldStatus)
return true return true
end end
@@ -278,7 +278,7 @@ end
]] ]]
local function normalChestUnload() local function normalChestUnload()
local oldStatus = turtle.status local oldStatus = turtle.getStatus()
status('unloading') status('unloading')
local pt = Util.shallowCopy(turtle.point) local pt = Util.shallowCopy(turtle.point)
safeGotoY(0) safeGotoY(0)
@@ -339,7 +339,7 @@ end
local function checkSpace() local function checkSpace()
if turtle.getItemCount(16) > 0 then if turtle.getItemCount(16) > 0 then
refuel() refuel()
local oldStatus = turtle.status local oldStatus = turtle.getStatus()
status('condensing') status('condensing')
ejectTrash() ejectTrash()
turtle.condense() turtle.condense()
@@ -433,7 +433,7 @@ local function bore()
boreDirection = 'down' boreDirection = 'down'
while true do while true do
if turtle.abort then if turtle.isAborted() then
status('aborting') status('aborting')
return false return false
end end
@@ -465,7 +465,7 @@ local function bore()
turtle.turnLeft() turtle.turnLeft()
while true do while true do
if turtle.abort then if turtle.isAborted() then
status('aborting') status('aborting')
return false return false
end end
@@ -473,7 +473,7 @@ local function bore()
while not Util.tryTimed(3, turtle.up) do while not Util.tryTimed(3, turtle.up) do
status('stuck') status('stuck')
end end
if turtle.status == 'stuck' then if turtle.getStatus() == 'stuck' then
status('boring up') status('boring up')
end end
@@ -624,7 +624,7 @@ turtle.run(function()
if not s and m then if not s and m then
_G.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)
unload() unload()

View File

@@ -1,4 +1,4 @@
requireInjector(getfenv(1)) _G.requireInjector()
local Event = require('event') local Event = require('event')
local Logger = require('logger') local Logger = require('logger')
@@ -8,6 +8,10 @@ local Point = require('point')
local TableDB = require('tableDB') local TableDB = require('tableDB')
local Util = require('util') local Util = require('util')
local device = _G.device
local os = _G.os
local turtle = _G.turtle
--[[ --[[
A supplier turtle for the builder turtle. For larger builds, use A supplier turtle for the builder turtle. For larger builds, use
ender modems. ender modems.
@@ -57,7 +61,7 @@ local maxStackDB = TableDB({
} }
} }
}) })
function maxStackDB:get(id, dmg) function maxStackDB:get(id, dmg)
return self.data[id .. ':' .. dmg] or 64 return self.data[id .. ':' .. dmg] or 64
end end
@@ -87,7 +91,7 @@ function Builder:dumpInventoryWithCheck()
print('Press enter to continue') print('Press enter to continue')
--turtle.setHeading(0) --turtle.setHeading(0)
self.ready = false self.ready = false
read() _G.read()
end end
self.ready = true self.ready = true
end end
@@ -95,7 +99,7 @@ end
function Builder:autocraft(supplies) function Builder:autocraft(supplies)
local t = { } local t = { }
for i,s in pairs(supplies) do for _,s in pairs(supplies) do
local key = s.id .. ':' .. s.dmg local key = s.id .. ':' .. s.dmg
local item = t[key] local item = t[key]
if not item then if not item then
@@ -108,7 +112,7 @@ function Builder:autocraft(supplies)
end end
item.qty = item.qty + (s.need-s.qty) item.qty = item.qty + (s.need-s.qty)
end end
Builder.itemProvider:craftItems(t) Builder.itemProvider:craftItems(t)
end end
@@ -133,9 +137,9 @@ function Builder:log(...)
end end
function Builder:getSupplies() function Builder:getSupplies()
Builder.itemProvider:refresh() Builder.itemProvider:refresh()
local t = { } local t = { }
for _,s in ipairs(self.slots) do for _,s in ipairs(self.slots) do
if s.need > 0 then if s.need > 0 then
@@ -171,7 +175,7 @@ function Builder:getSupplies()
Builder:log('Need %d %s', s.need - s.qty, name) Builder:log('Need %d %s', s.need - s.qty, name)
end end
end end
return t return t
end end
@@ -179,11 +183,11 @@ local function moveTowardsX(dx)
local direction = dx - turtle.point.x local direction = dx - turtle.point.x
local move local move
if direction == 0 then if direction == 0 then
return false return false
end end
if direction > 0 and turtle.point.heading == 0 or if direction > 0 and turtle.point.heading == 0 or
direction < 0 and turtle.point.heading == 2 then direction < 0 and turtle.point.heading == 2 then
move = turtle.forward move = turtle.forward
@@ -202,7 +206,7 @@ local function moveTowardsZ(dz)
if direction == 0 then if direction == 0 then
return false return false
end end
if direction > 0 and turtle.point.heading == 1 or if direction > 0 and turtle.point.heading == 1 or
direction < 0 and turtle.point.heading == 3 then direction < 0 and turtle.point.heading == 3 then
move = turtle.forward move = turtle.forward
@@ -214,7 +218,6 @@ local function moveTowardsZ(dz)
end end
function Builder:finish() function Builder:finish()
Builder.resupplying = true Builder.resupplying = true
Builder.ready = false Builder.ready = false
if turtle.gotoLocation('supplies') then if turtle.gotoLocation('supplies') then
@@ -227,9 +230,8 @@ function Builder:finish()
end end
function Builder:gotoBuilder() function Builder:gotoBuilder()
if Builder.lastPoint then if Builder.lastPoint then
turtle.status = 'tracking' turtle.setStatus('tracking')
while true do while true do
local pt = Point.copy(Builder.lastPoint) local pt = Point.copy(Builder.lastPoint)
pt.y = pt.y + 3 pt.y = pt.y + 3
@@ -260,140 +262,137 @@ function Builder:gotoBuilder()
end end
end end
Message.addHandler('builder', Message.addHandler('builder',
function(h, id, msg, distance) function(_, id, msg)
if not id or id ~= __BUILDER_ID then if not id or id ~= __BUILDER_ID then
return return
end end
if not Builder.resupplying then if not Builder.resupplying then
local pt = msg.contents local pt = msg.contents
pt.y = pt.y + 3 pt.y = pt.y + 3
turtle.status = 'supervising' turtle.setStatus('supervising')
turtle.gotoYfirst(pt) turtle.gotoYfirst(pt)
end end
end) end)
Message.addHandler('supplyList', Message.addHandler('supplyList',
function(h, id, msg, distance) function(_, id, msg)
if not id or id ~= __BUILDER_ID then if not id or id ~= __BUILDER_ID then
return return
end end
turtle.status = 'resupplying' turtle.setStatus('resupplying')
Builder.resupplying = true Builder.resupplying = true
Builder.slots = msg.contents.slots Builder.slots = msg.contents.slots
Builder.slotUid = msg.contents.uid Builder.slotUid = msg.contents.uid
Builder:log('Received supply list ' .. Builder.slotUid) Builder:log('Received supply list ' .. Builder.slotUid)
os.sleep(0) os.sleep(0)
if not turtle.gotoLocation('supplies') then if not turtle.gotoLocation('supplies') then
Builder:log('Failed to go to supply location') Builder:log('Failed to go to supply location')
self.ready = false Builder.ready = false
Event.exitPullEvents() Event.exitPullEvents()
end end
turtle.setHeading(1) turtle.setHeading(1)
os.sleep(.2) -- random 'Computer is not connected' error... os.sleep(.2) -- random 'Computer is not connected' error...
Builder:dumpInventoryWithCheck() Builder:dumpInventoryWithCheck()
Builder:refuel() Builder:refuel()
while true do while true do
local supplies = Builder:getSupplies() local supplies = Builder:getSupplies()
if #supplies == 0 then if #supplies == 0 then
break break
end end
Builder:autocraft(supplies) Builder:autocraft(supplies)
turtle.status = 'waiting' turtle.setStatus('waiting')
os.sleep(5) os.sleep(5)
end end
Builder:log('Got all supplies') Builder:log('Got all supplies')
os.sleep(0) os.sleep(0)
Builder:gotoBuilder() Builder:gotoBuilder()
Builder.resupplying = false Builder.resupplying = false
end) end)
Message.addHandler('needSupplies', Message.addHandler('needSupplies',
function(h, id, msg, distance) function(_, id, msg)
if not id or id ~= __BUILDER_ID then if not id or id ~= __BUILDER_ID then
return return
end end
if Builder.resupplying or msg.contents.uid ~= Builder.slotUid then if Builder.resupplying or msg.contents.uid ~= Builder.slotUid then
Builder:log('No supplies ready')
Builder:log('No supplies ready') Message.send(__BUILDER_ID, 'gotSupplies')
else
turtle.setStatus('supplying')
Builder:log('Supplying')
os.sleep(0)
Message.send(__BUILDER_ID, 'gotSupplies') local pt = msg.contents.point
else pt.y = turtle.getPoint().y
turtle.status = 'supplying' pt.heading = nil
Builder:log('Supplying') if not turtle.gotoYfirst(pt) then -- location of builder
os.sleep(0) Builder.resupplying = true
Message.send(__BUILDER_ID, 'gotSupplies')
local pt = msg.contents.point os.sleep(0)
pt.y = turtle.getPoint().y if not turtle.gotoLocation('supplies') then
pt.heading = nil Builder:log('failed to go to supply location')
if not turtle.gotoYfirst(pt) then -- location of builder Event.exitPullEvents()
Builder.resupplying = true end
Message.send(__BUILDER_ID, 'gotSupplies')
os.sleep(0)
if not turtle.gotoLocation('supplies') then
Builder:log('failed to go to supply location')
--self.ready = false
Event.exitPullEvents()
end
turtle.setHeading(1) turtle.setHeading(1)
return return
end end
pt.y = pt.y - 2 -- location where builder should go for the chest to be above pt.y = pt.y - 2 -- location where builder should go for the chest to be above
turtle.select(15) turtle.select(15)
turtle.placeDown() turtle.placeDown()
os.sleep(.1) -- random computer not connected error os.sleep(.1) -- random computer not connected error
local p = ChestProvider({ direction = 'up', wrapSide = 'bottom' }) local p = ChestProvider({ direction = 'up', wrapSide = 'bottom' })
for i = 1, 16 do for i = 1, 16 do
p:insert(i, 64) p:insert(i, 64)
end end
Message.send(__BUILDER_ID, 'gotSupplies', { supplies = true, point = pt }) Message.send(__BUILDER_ID, 'gotSupplies', { supplies = true, point = pt })
Message.waitForMessage('thanks', 5, __BUILDER_ID) Message.waitForMessage('thanks', 5, __BUILDER_ID)
--os.sleep(0) --os.sleep(0)
--p.condenseItems() --p.condenseItems()
for i = 1, 16 do for i = 1, 16 do
p:extract(i, 64) p:extract(i, 64)
end end
turtle.digDown() turtle.digDown()
turtle.status = 'waiting' turtle.setStatus('waiting')
end end
end) end)
Message.addHandler('finished', Message.addHandler('finished',
function(h, id) function(_, id)
if not id or id ~= __BUILDER_ID then if not id or id ~= __BUILDER_ID then
return return
end end
Builder:finish() Builder:finish()
end) end)
Event.on('turtle_abort', Event.on('turtle_abort',
function() function()
turtle.abort = false turtle.abort(false)
turtle.status = 'aborting' turtle.setStatus('aborting')
Builder:finish() Builder:finish()
end) end)
local function onTheWay() -- parallel routine local function onTheWay() -- parallel routine
while true do while true do
local e, side, _id, id, msg, distance = os.pullEvent('modem_message') local _, _, _, id, msg, _ = os.pullEvent('modem_message')
if Builder.ready then if Builder.ready then
if id == __BUILDER_ID and msg and msg.type then if id == __BUILDER_ID and msg and msg.type then
if msg.type == 'needSupplies' then if msg.type == 'needSupplies' then
Message.send(__BUILDER_ID, 'gotSupplies', { supplies = true }) Message.send(__BUILDER_ID, 'gotSupplies', { supplies = true })
elseif msg.type == 'builder' then elseif msg.type == 'builder' then
Builder.lastPoint = msg.contents Builder.lastPoint = msg.contents
end end
end end
end end
end end

View File

@@ -732,11 +732,11 @@ local s, m = turtle.run(function()
turtle.setPolicy("attack") turtle.setPolicy("attack")
while not turtle.abort do while not turtle.isAborted() do
print('fuel: ' .. turtle.getFuelLevel()) print('fuel: ' .. turtle.getFuelLevel())
for _,task in ipairs(Util.shallowCopy(tasks)) do for _,task in ipairs(Util.shallowCopy(tasks)) do
--print(task.desc) --print(task.desc)
turtle.status = task.desc turtle.setStatus(task.desc)
turtle.select(1) turtle.select(1)
if not task.fn() then if not task.fn() then
Util.filterInplace(tasks, function(v) return v.fn ~= task.fn end) Util.filterInplace(tasks, function(v) return v.fn ~= task.fn end)

View File

@@ -1 +1 @@
turtle.abortAction() turtle.abort(true)

View File

@@ -1,12 +1,15 @@
local os = _G.os
local turtle = _G.turtle
local function follow(id) local function follow(id)
requireInjector(getfenv(1)) _G.requireInjector()
local Event = require('event') local Event = require('event')
local Point = require('point') local Point = require('point')
local Socket = require('socket') local Socket = require('socket')
turtle.status = 'follow ' .. id turtle.setStatus('follow ' .. id)
if not turtle.enableGPS() then if not turtle.enableGPS() then
error('turtle: No GPS found') error('turtle: No GPS found')
@@ -55,7 +58,7 @@ local function follow(id)
Event.onInterval(.5, function() Event.onInterval(.5, function()
local function getRemotePoint() local function getRemotePoint()
if not turtle.abort then if not turtle.isAborted() then
if socket:write({ type = 'gps' }) then if socket:write({ type = 'gps' }) then
return socket:read(3) return socket:read(3)
end end
@@ -65,7 +68,7 @@ local function follow(id)
-- sometimes gps will fail if moving -- sometimes gps will fail if moving
local pt, d local pt, d
for i = 1, 3 do for _ = 1, 3 do
pt, d = getRemotePoint() pt, d = getRemotePoint()
if pt then if pt then
break break
@@ -73,18 +76,18 @@ local function follow(id)
os.sleep(.5) os.sleep(.5)
end end
if not pt or turtle.abort then if not pt or turtle.isAborted() then
error('Did not receive GPS location') error('Did not receive GPS location')
end end
if not lastPoint or (lastPoint.x ~= pt.x or lastPoint.y ~= pt.y or lastPoint.z ~= pt.z) then if not lastPoint or (lastPoint.x ~= pt.x or lastPoint.y ~= pt.y or lastPoint.z ~= pt.z) then
if following then if following then
turtle.abort = true turtle.getState().abort = true
while following do while following do
os.sleep(.1) os.sleep(.1)
end end
turtle.abort = false turtle.getState().abort = false
end end
-- check if gps is inaccurate (player moving too fast) -- check if gps is inaccurate (player moving too fast)

View File

@@ -70,7 +70,7 @@ local function findObsidian()
if not turtle.gotoPoint(node) then if not turtle.gotoPoint(node) then
break break
end end
until turtle.abort until turtle.isAborted()
end end
turtle.run(function() turtle.run(function()
@@ -97,7 +97,7 @@ turtle.run(function()
turtle.placeDown() turtle.placeDown()
turtle.down() turtle.down()
turtle.select(1) turtle.select(1)
until turtle.abort until turtle.isAborted()
end) end)
if not s and m then if not s and m then

View File

@@ -6,7 +6,7 @@ local function summon(id)
local Point = require('point') local Point = require('point')
local Socket = require('socket') local Socket = require('socket')
turtle.status = 'GPSing' turtle.setStatus('GPSing')
turtle.setPoint({ x = 0, y = 0, z = 0, heading = 0 }) turtle.setPoint({ x = 0, y = 0, z = 0, heading = 0 })
local pts = { local pts = {
@@ -64,7 +64,7 @@ local function summon(id)
local pt = { x = pos.x, y = pos.y, z = pos.z } local pt = { x = pos.x, y = pos.y, z = pos.z }
local _, h = Point.calculateMoves(turtle.getPoint(), pt) local _, h = Point.calculateMoves(turtle.getPoint(), pt)
local hi = turtle.getHeadingInfo(h) local hi = turtle.getHeadingInfo(h)
turtle.status = 'recalling' turtle.setStatus('recalling')
turtle.pathfind({ x = pt.x - hi.xd, z = pt.z - hi.zd, y = pt.y - hi.yd, heading = h }) turtle.pathfind({ x = pt.x - hi.xd, z = pt.z - hi.zd, y = pt.y - hi.yd, heading = h })
else else
error("turtle: Could not determine position") error("turtle: Could not determine position")