diff --git a/apis/blocks.lua b/apis/builder/blocks.lua similarity index 100% rename from apis/blocks.lua rename to apis/builder/blocks.lua diff --git a/apis/builder/builder.lua b/apis/builder/builder.lua index 90fbd12..79dd95f 100644 --- a/apis/builder/builder.lua +++ b/apis/builder/builder.lua @@ -1,4 +1,4 @@ -local Blocks = require('blocks') +local Blocks = require('builder.blocks') local class = require('class') local Message = require('message') local Util = require('util') @@ -10,13 +10,9 @@ local turtle = _G.turtle local Builder = class() Util.merge(Builder, { isCommandComputer = not turtle, - slots = { }, loc = { }, index = 1, mode = 'build', - fuelItem = { id = 'minecraft:coal', dmg = 0 }, - resourceSlots = 14, - facing = 'south', }) local BUILDER_DIR = 'usr/builder' @@ -87,7 +83,7 @@ end function Builder:saveProgress(index) Util.writeTable( fs.combine(BUILDER_DIR, self.schematic.filename .. '.progress'), - { index = index, facing = self.facing, loc = self.loc } + { index = index, loc = self.loc } ) end @@ -98,7 +94,6 @@ function Builder:loadProgress(filename) if self.index > #self.schematic.blocks then self.index = 1 end - self.facing = progress.facing or 'south' self.loc = progress.loc or { } end end diff --git a/apis/builder/commands.lua b/apis/builder/commands.lua index 99c2b32..f300bae 100644 --- a/apis/builder/commands.lua +++ b/apis/builder/commands.lua @@ -12,6 +12,11 @@ function Builder:begin() local last = #self.schematic.blocks 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 direction = -1 last = 1 @@ -32,12 +37,10 @@ function Builder:begin() end local function placeBlock(bid, dmg, x, y, z) - local cx, _, cz = commands.getBlockPosition() - local command = table.concat({ "setblock", cx + x + 1, - "~" .. y, + cy + y, cz + z + 1, bid, dmg, diff --git a/apis/schematic.lua b/apis/builder/schematic.lua similarity index 97% rename from apis/schematic.lua rename to apis/builder/schematic.lua index ac3d398..b530ee2 100644 --- a/apis/schematic.lua +++ b/apis/builder/schematic.lua @@ -5,31 +5,11 @@ local Point = require('point') local bit = _G.bit local fs = _G.fs +local os = _G.os 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) - if heading and type(heading) == 'string' then - return namedHeadings[heading] - end - return headings[heading] + return Point.headings[heading] end --[[ @@ -86,7 +66,7 @@ function Spinner:stop(text) end local Schematic = class() -function Schematic:init(args) +function Schematic:init() self.blocks = { } self.damages = { } self.originalBlocks = { } @@ -122,7 +102,7 @@ function Schematic:readname(h) local n = n1*256 + n2 local str = "" - for i=1,n do + for _=1,n do local c = h:readbyte(h) if c == nil then return diff --git a/apis/builder/turtle.lua b/apis/builder/turtle.lua index 33f583e..9fd7fdf 100644 --- a/apis/builder/turtle.lua +++ b/apis/builder/turtle.lua @@ -15,7 +15,13 @@ local read = _G.read local rs = _G.rs local turtle = _G.turtle +local RESOURCE_SLOTS = 14 +local FUEL_ITEM = { id = 'minecraft:coal', dmg = 0 } + local TurtleBuilder = class(Builder) +Util.merge(TurtleBuilder, { + slots = { }, +}) -- Temp functions until conversion to new adapters is complete local function convertSingleForward(item) @@ -92,7 +98,7 @@ function supplyPage:eventHandler(event) self.builder:dumpInventory() --Builder.status = 'idle' UI:setPage('start') - turtle.status = 'idle' + turtle.setStatus('waiting') elseif event.type == 'grid_focus_row' then self.statusBar:setValue('help', event.selected.id .. ':' .. event.selected.dmg) @@ -164,7 +170,7 @@ function TurtleBuilder:getBlockCounts() wrench.need = 1 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.need = 1 blocks[fuel.id .. ':' .. fuel.dmg] = fuel @@ -200,7 +206,7 @@ function TurtleBuilder:getAirResupplyList(blockIndex) local slots = { } if self.mode == 'destroy' then - for i = 1, self.resourceSlots do + for i = 1, RESOURCE_SLOTS do slots[i] = { qty = 0, need = 0, @@ -211,7 +217,7 @@ function TurtleBuilder:getAirResupplyList(blockIndex) slots = self:getGenericSupplyList(blockIndex) 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] = { id = 'minecraft:chest', @@ -260,7 +266,7 @@ end function TurtleBuilder:getGenericSupplyList(blockIndex) local slots = { } - for i = 1, self.resourceSlots do + for i = 1, RESOURCE_SLOTS do slots[i] = { qty = 0, need = 0, @@ -397,13 +403,13 @@ function TurtleBuilder:refuel() print('Refueling') 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) if turtle.getItemCount(1) == 0 then print('Out of fuel, add fuel to chest/ME system') turtle.setHeading(0) - turtle.status = 'waiting' + turtle.setStatus('waiting') os.sleep(5) else turtle.refuel(64) @@ -426,7 +432,7 @@ function TurtleBuilder:inAirDropoff() return false end - turtle.status = 'waiting' + turtle.setStatus('waiting') if msg.contents.point then local pt = msg.contents.point @@ -488,7 +494,7 @@ function TurtleBuilder:inAirResupply() return false end - turtle.status = 'waiting' + turtle.setStatus('waiting') if msg.contents.point then local pt = msg.contents.point @@ -609,7 +615,7 @@ function TurtleBuilder:resupply() return end - turtle.status = 'resupplying' + turtle.setStatus('resupplying') self:log('Resupplying') self:gotoSupplyPoint() @@ -653,11 +659,11 @@ end -- figure out our orientation in the world function TurtleBuilder:getTurtleFacing() - local directions = { -- reversed directions - [5] = 'west', - [3] = 'north', - [4] = 'east', - [2] = 'south', + local directions = { + [5] = 2, + [3] = 3, + [4] = 0, + [2] = 1, } local function getItem(item) @@ -730,7 +736,7 @@ function TurtleBuilder:wrenchBlock(side, facing, cache) } if turtle.getHeadingInfo(facing).heading < 4 then - local offsetDirection = (turtle.getHeadingInfo(self.facing).heading + + local offsetDirection = (self.facing + turtle.getHeadingInfo(facing).heading) % 4 facing = turtle.getHeadingInfo(offsetDirection).direction end @@ -754,17 +760,12 @@ function TurtleBuilder:wrenchBlock(side, facing, cache) end function TurtleBuilder:rotateBlock(side, facing) - local s = self:getWrenchSlot() - - if not s then - return false + if self:getWrenchSlot() then + for _ = 1, facing do + turtle.getAction(side).place() + end + return true end - - for _ = 1, facing do - turtle.getAction(side).place() - end - - return true end -- place piston, wrench piston to face downward, extend, remove piston @@ -813,7 +814,7 @@ function TurtleBuilder:_goto(x, z, y, heading) print('stuck') print('Press enter to continue') os.sleep(1) - turtle.status = 'stuck' + turtle.setStatus('stuck') read() end end @@ -902,7 +903,7 @@ function TurtleBuilder:placeDirectionalBlock(b, slot, travelPlane) [ 'west-up' ] = 'east' } if stairUpDirections[d] then - local isSouth = (turtle.getHeadingInfo(self.facing).heading + + local isSouth = (self.facing + turtle.getHeadingInfo(stairUpDirections[d]).heading) % 4 == 1 if not self.stairBug then @@ -1091,10 +1092,10 @@ function TurtleBuilder:build() if self.mode == 'destroy' then direction = -1 last = 1 - turtle.status = 'destroying' + turtle.setStatus('destroying') else travelPlane = self:findTravelPlane(self.index) - turtle.status = 'building' + turtle.setStatus('building') end local pt = self:getBuildingCorner() @@ -1120,13 +1121,13 @@ function TurtleBuilder:build() -- 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 self:gotoSupplyPoint() self:dumpInventoryWithCheck() self:refuel() end - turtle.status = 'destroying' + turtle.setStatus('destroying') end else -- Build mode @@ -1177,9 +1178,9 @@ function TurtleBuilder:build() throttle() -- sleep in case there are a large # of skipped blocks end - if turtle.abort then - turtle.status = 'aborting' - turtle.abort = false + if turtle.isAborted() then + turtle.setStatus('aborting') + turtle.abort(false) self:gotoTravelPlane(travelPlane) self:gotoSupplyPoint() turtle.setHeading(0) @@ -1224,8 +1225,7 @@ function TurtleBuilder:begin() self:refuel() self:getTurtleFacing() - local facing = turtle.getHeadingInfo(self.facing).heading - Point.rotate(self.supplyPoint, facing) + Point.rotate(self.supplyPoint, self.facing) turtle.setPoint(self.supplyPoint) -- reset piston cache in case wrench was substituted diff --git a/apis/turtle/level.lua b/apis/turtle/level.lua index 93bbbb9..9e86ae2 100644 --- a/apis/turtle/level.lua +++ b/apis/turtle/level.lua @@ -161,7 +161,7 @@ return function(startPt, endPt, firstPt, verbose) if not turtle.gotoPoint(node) then break end - until turtle.abort + until turtle.isAborted() turtle.resetState() turtle.setMoveCallback(oldCallback) diff --git a/apps/builder.lua b/apps/builder.lua index d66f2e7..73189d9 100644 --- a/apps/builder.lua +++ b/apps/builder.lua @@ -8,7 +8,7 @@ local Adapter = require('inventoryAdapter') local Event = require('event') local GPS = require('gps') local itemDB = require('itemDB') -local Schematic = require('schematic') +local Schematic = require('builder.schematic') local TableDB = require('tableDB') local UI = require('ui') local Util = require('util') @@ -16,7 +16,6 @@ local Util = require('util') local colors = _G.colors local fs = _G.fs local multishell = _ENV.multishell -local turtle = _G.turtle local BUILDER_DIR = 'usr/builder' @@ -637,11 +636,15 @@ function startPage:eventHandler(event) elseif event.type == 'startPoint' then local loc = Util.shallowCopy(Builder.loc) if not loc.x then - local pt = GPS.getPoint() - if pt then - loc.x = pt.x - loc.y = pt.y - loc.z = pt.z + if _G.turtle then + local pt = GPS.getPoint() + if pt then + loc.x = pt.x + loc.y = pt.y + loc.z = pt.z + end + elseif _G.commands then + loc.x, loc.y, loc.z = _G.commands.getBlockPosition() end end @@ -775,12 +778,4 @@ UI:setPages({ UI:setPage('start') -if Builder.isCommandComputer then - UI:pullEvents() -else - UI:pullEvents() --- turtle.run(function() --- turtle.reset() --- UI:pullEvents() --- end) -end +UI:pullEvents() diff --git a/apps/music.lua b/apps/music.lua index 32a058b..57d5768 100644 --- a/apps/music.lua +++ b/apps/music.lua @@ -252,9 +252,9 @@ page:setVolume(page.volume, true) UI:setPage(page) -turtle.status = 'Jamming' +turtle.setStatus('Jamming') UI:pullEvents() -turtle.status = 'idle' +turtle.setStatus('idle') page:play(false) UI.term:reset() diff --git a/apps/pickup.lua b/apps/pickup.lua index 3de66da..f0aa9bb 100644 --- a/apps/pickup.lua +++ b/apps/pickup.lua @@ -1,12 +1,17 @@ -requireInjector(getfenv(1)) +_G.requireInjector() local Event = require('event') -local GPS = require('gps') local ChestAdapter = require('chestAdapter18') local Point = require('point') local Socket = require('socket') 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 error('Modem is required') end @@ -35,7 +40,7 @@ local fuel = { local slots -turtle.setMoveCallback(function(action, pt) +turtle.setMoveCallback(function() if slots then for _,slot in pairs(slots) do if turtle.getItemCount(slot.index) ~= slot.qty then @@ -46,10 +51,38 @@ turtle.setMoveCallback(function(action, pt) 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 print('refueling') - turtle.status = 'refueling' + turtle.setStatus('refueling') gotoPoint(locations.dropPt, true) dropOff(locations.dropPt) local chestAdapter = ChestAdapter({ @@ -66,8 +99,8 @@ function refuel() end end -function pickUp(pt) - turtle.status = 'picking up' +local function pickUp(pt) + turtle.setStatus('picking up') gotoPoint(pt, true) while true do if not turtle.selectOpenSlot() then @@ -81,41 +114,13 @@ function pickUp(pt) end end -function dropOff(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) +local function checkCell(pt) if not turtle.selectOpenSlot() then dropOff(locations.dropPt) end print('checking cell') - turtle.status = 'recharging' + turtle.setStatus('recharging') gotoPoint(pt, true) local c = peripheral.wrap('bottom') local energy = c.getMaxEnergyStored() - @@ -136,9 +141,9 @@ function checkCell(pt) end end -function fluid(points) +local function fluid(points) print('checking fluid') - turtle.status = 'fluiding' + turtle.setStatus('fluiding') gotoPoint(points.source, true) turtle.select(1) turtle.digDown() @@ -152,10 +157,10 @@ function fluid(points) turtle.placeDown() end -function refill(entry) +local function refill(entry) dropOff(locations.dropPt) - turtle.status = 'refilling' + turtle.setStatus('refilling') gotoPoint(locations.dropPt) local chestAdapter = ChestAdapter() for _,item in pairs(entry.items) do @@ -169,21 +174,6 @@ function refill(entry) 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) return string.format('%d:%d:%d', pt.x, pt.y, pt.z) end @@ -198,7 +188,7 @@ local function pickupHost(socket) end print('command: ' .. data.type) - + if data.type == 'pickup' then local key = makeKey(data.point) locations.pickups[key] = data.point @@ -207,7 +197,7 @@ local function pickupHost(socket) elseif data.type == 'items' then socket:write( { type = "response", response = items }) - + elseif data.type == 'refill' then local key = makeKey(data.entry.point) locations.refills[key] = data.entry @@ -223,14 +213,12 @@ local function pickupHost(socket) locations.chargePt = data.point Util.writeTable('/usr/config/pickup', locations) socket:write( { type = "response", response = 'Location set' }) - + elseif data.type == 'charge' then local key = makeKey(data.point) locations.cells[key] = data.point Util.writeTable('/usr/config/pickup', locations) socket:write( { type = "response", response = 'added' }) - - elseif data.type == 'fluid' then elseif data.type == 'clear' then local key = makeKey(data.point) @@ -240,7 +228,7 @@ local function pickupHost(socket) locations.pickups[key] = nil Util.writeTable('/usr/config/pickup', locations) - + socket:write( { type = "response", response = 'cleared' }) else print('unknown command') @@ -264,7 +252,7 @@ local function eachEntry(t, fn) local keys = Util.keys(t) for _,key in pairs(keys) do if t[key] then - if turtle.abort then + if turtle.isAborted() then return end fn(t[key]) @@ -283,7 +271,7 @@ local function eachClosestEntry(t, fn) while not Util.empty(points) do local closest = Point.closest(turtle.point, points) - if turtle.abort then + if turtle.isAborted() then return end if t[closest.key] then @@ -299,7 +287,6 @@ local function eachClosestEntry(t, fn) end Event.addRoutine(function() - if not turtle.enableGPS() then error('turtle: No GPS found') end @@ -318,8 +305,8 @@ Event.addRoutine(function() eachEntry(locations.cells, checkCell) end print('sleeping') - turtle.status = 'sleeping' - if turtle.abort then + turtle.setStatus('sleeping') + if turtle.isAborted() then printError('aborted') break end diff --git a/apps/shapes.lua b/apps/shapes.lua index 69cc81d..c501bee 100644 --- a/apps/shapes.lua +++ b/apps/shapes.lua @@ -46,7 +46,7 @@ local function refuel() itemAdapter:provide({ name = 'minecraft:coal', damage = 0 }, 64, 1) if turtle.getItemCount(1) == 0 then print('Out of fuel, add fuel to chest/ME system') - turtle.status = 'waiting' + turtle.setStatus('waiting') os.sleep(5) else turtle.refuel(64) @@ -212,12 +212,12 @@ local function clear() if sy > osy then turtle.digDown() end - if turtle.abort then + if turtle.isAborted() then break end end - if turtle.abort then + if turtle.isAborted() then break end if sy + 1 >= ey then @@ -231,7 +231,7 @@ local function clear() end turtle.run(function() - turtle.status = 'Clearing' + turtle.setStatus('Clearing') if turtle.enableGPS() then @@ -255,7 +255,7 @@ local Level = require('turtle.level') local Util = require('util') local s, m = turtle.run(function() - turtle.status = 'Leveling' + turtle.setStatus('Leveling') if turtle.enableGPS() then @@ -379,7 +379,7 @@ function page:eventHandler(event) end self.statusBar:setStatus('') elseif event.type == 'cancel' then - self:runFunction(turtleId, 'turtle.abortAction()') + self:runFunction(turtleId, 'turtle.abort(true)') self.statusBar:setStatus('') else return UI.Page.eventHandler(self, event) diff --git a/apps/simpleMiner.lua b/apps/simpleMiner.lua index 12cd93c..8c726b2 100644 --- a/apps/simpleMiner.lua +++ b/apps/simpleMiner.lua @@ -184,13 +184,13 @@ local function log(text) end local function status(newStatus) - turtle.status = newStatus + turtle.setStatus(newStatus) log(newStatus) end local function refuel() if turtle.getFuelLevel() < MIN_FUEL then - local oldStatus = turtle.status + local oldStatus = turtle.getStatus() status('refueling') if turtle.select('minecraft:coal:0') then @@ -216,31 +216,31 @@ local function refuel() end local function safeGoto(x, z, y, h) - local oldStatus = turtle.status + local oldStatus = turtle.getStatus() -- 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 + if turtle.isAborted() then return false end os.sleep(3) end - turtle.status = oldStatus + turtle.setStatus(oldStatus) return true end local function safeGotoY(y) - local oldStatus = turtle.status + local oldStatus = turtle.getStatus() while not turtle.gotoY(y) do status('stuck') - if turtle.abort then + if turtle.isAborted() then return false end os.sleep(1) end - turtle.status = oldStatus + turtle.setStatus(oldStatus) return true end @@ -278,7 +278,7 @@ end ]] local function normalChestUnload() - local oldStatus = turtle.status + local oldStatus = turtle.getStatus() status('unloading') local pt = Util.shallowCopy(turtle.point) safeGotoY(0) @@ -339,7 +339,7 @@ end local function checkSpace() if turtle.getItemCount(16) > 0 then refuel() - local oldStatus = turtle.status + local oldStatus = turtle.getStatus() status('condensing') ejectTrash() turtle.condense() @@ -433,7 +433,7 @@ local function bore() boreDirection = 'down' while true do - if turtle.abort then + if turtle.isAborted() then status('aborting') return false end @@ -465,7 +465,7 @@ local function bore() turtle.turnLeft() while true do - if turtle.abort then + if turtle.isAborted() then status('aborting') return false end @@ -473,7 +473,7 @@ local function bore() while not Util.tryTimed(3, turtle.up) do status('stuck') end - if turtle.status == 'stuck' then + if turtle.getStatus() == 'stuck' then status('boring up') end @@ -624,7 +624,7 @@ turtle.run(function() if not s and m then _G.printError(m) end - turtle.abort = false + turtle.abort(false) safeGotoY(0) safeGoto(0, 0, 0, 0) unload() diff --git a/apps/supplier.lua b/apps/supplier.lua index 779c049..2b8dcee 100644 --- a/apps/supplier.lua +++ b/apps/supplier.lua @@ -1,4 +1,4 @@ -requireInjector(getfenv(1)) +_G.requireInjector() local Event = require('event') local Logger = require('logger') @@ -8,6 +8,10 @@ local Point = require('point') local TableDB = require('tableDB') 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 ender modems. @@ -57,7 +61,7 @@ local maxStackDB = TableDB({ } } }) - + function maxStackDB:get(id, dmg) return self.data[id .. ':' .. dmg] or 64 end @@ -87,7 +91,7 @@ function Builder:dumpInventoryWithCheck() print('Press enter to continue') --turtle.setHeading(0) self.ready = false - read() + _G.read() end self.ready = true end @@ -95,7 +99,7 @@ end function Builder:autocraft(supplies) local t = { } - for i,s in pairs(supplies) do + for _,s in pairs(supplies) do local key = s.id .. ':' .. s.dmg local item = t[key] if not item then @@ -108,7 +112,7 @@ function Builder:autocraft(supplies) end item.qty = item.qty + (s.need-s.qty) end - + Builder.itemProvider:craftItems(t) end @@ -133,9 +137,9 @@ function Builder:log(...) end function Builder:getSupplies() - + Builder.itemProvider:refresh() - + local t = { } for _,s in ipairs(self.slots) do if s.need > 0 then @@ -171,7 +175,7 @@ function Builder:getSupplies() Builder:log('Need %d %s', s.need - s.qty, name) end end - + return t end @@ -179,11 +183,11 @@ local function moveTowardsX(dx) local direction = dx - turtle.point.x local move - + if direction == 0 then return false end - + if direction > 0 and turtle.point.heading == 0 or direction < 0 and turtle.point.heading == 2 then move = turtle.forward @@ -202,7 +206,7 @@ local function moveTowardsZ(dz) if direction == 0 then return false end - + if direction > 0 and turtle.point.heading == 1 or direction < 0 and turtle.point.heading == 3 then move = turtle.forward @@ -214,7 +218,6 @@ local function moveTowardsZ(dz) end function Builder:finish() - Builder.resupplying = true Builder.ready = false if turtle.gotoLocation('supplies') then @@ -227,9 +230,8 @@ function Builder:finish() end function Builder:gotoBuilder() - if Builder.lastPoint then - turtle.status = 'tracking' + turtle.setStatus('tracking') while true do local pt = Point.copy(Builder.lastPoint) pt.y = pt.y + 3 @@ -260,140 +262,137 @@ function Builder:gotoBuilder() end end -Message.addHandler('builder', - function(h, id, msg, distance) - if not id or id ~= __BUILDER_ID then - return - end +Message.addHandler('builder', + function(_, id, msg) + if not id or id ~= __BUILDER_ID then + return + end if not Builder.resupplying then - local pt = msg.contents - pt.y = pt.y + 3 + local pt = msg.contents + pt.y = pt.y + 3 - turtle.status = 'supervising' - turtle.gotoYfirst(pt) - end + turtle.setStatus('supervising') + turtle.gotoYfirst(pt) + end end) -Message.addHandler('supplyList', - function(h, id, msg, distance) - if not id or id ~= __BUILDER_ID then - return - end +Message.addHandler('supplyList', + function(_, id, msg) + if not id or id ~= __BUILDER_ID then + return + end - turtle.status = 'resupplying' - Builder.resupplying = true - Builder.slots = msg.contents.slots - Builder.slotUid = msg.contents.uid + turtle.setStatus('resupplying') + Builder.resupplying = true + Builder.slots = msg.contents.slots + Builder.slotUid = msg.contents.uid Builder:log('Received supply list ' .. Builder.slotUid) - os.sleep(0) - if not turtle.gotoLocation('supplies') then - Builder:log('Failed to go to supply location') - self.ready = false - Event.exitPullEvents() - end + os.sleep(0) + if not turtle.gotoLocation('supplies') then + Builder:log('Failed to go to supply location') + Builder.ready = false + Event.exitPullEvents() + end turtle.setHeading(1) os.sleep(.2) -- random 'Computer is not connected' error... Builder:dumpInventoryWithCheck() Builder:refuel() while true do - local supplies = Builder:getSupplies() - if #supplies == 0 then - break - end - Builder:autocraft(supplies) - turtle.status = 'waiting' - os.sleep(5) - end - Builder:log('Got all supplies') - os.sleep(0) - Builder:gotoBuilder() - Builder.resupplying = false + local supplies = Builder:getSupplies() + if #supplies == 0 then + break + end + Builder:autocraft(supplies) + turtle.setStatus('waiting') + os.sleep(5) + end + Builder:log('Got all supplies') + os.sleep(0) + Builder:gotoBuilder() + Builder.resupplying = false end) -Message.addHandler('needSupplies', - function(h, id, msg, distance) - if not id or id ~= __BUILDER_ID then - return - end +Message.addHandler('needSupplies', + function(_, id, msg) + if not id or id ~= __BUILDER_ID then + return + end - if Builder.resupplying or msg.contents.uid ~= Builder.slotUid then - - Builder:log('No supplies ready') + if Builder.resupplying or msg.contents.uid ~= Builder.slotUid then + 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') - else - turtle.status = 'supplying' - Builder:log('Supplying') - os.sleep(0) - - local pt = msg.contents.point - pt.y = turtle.getPoint().y - pt.heading = nil - if not turtle.gotoYfirst(pt) then -- location of builder - Builder.resupplying = true - 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 + local pt = msg.contents.point + pt.y = turtle.getPoint().y + pt.heading = nil + if not turtle.gotoYfirst(pt) then -- location of builder + Builder.resupplying = true + Message.send(__BUILDER_ID, 'gotSupplies') + os.sleep(0) + if not turtle.gotoLocation('supplies') then + Builder:log('failed to go to supply location') + Event.exitPullEvents() + end turtle.setHeading(1) - return - end - pt.y = pt.y - 2 -- location where builder should go for the chest to be above + return + end + pt.y = pt.y - 2 -- location where builder should go for the chest to be above - turtle.select(15) - turtle.placeDown() - os.sleep(.1) -- random computer not connected error + turtle.select(15) + turtle.placeDown() + os.sleep(.1) -- random computer not connected error local p = ChestProvider({ direction = 'up', wrapSide = 'bottom' }) - for i = 1, 16 do - p:insert(i, 64) - end + for i = 1, 16 do + p:insert(i, 64) + 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) - --os.sleep(0) + Message.waitForMessage('thanks', 5, __BUILDER_ID) + --os.sleep(0) - --p.condenseItems() - for i = 1, 16 do - p:extract(i, 64) - end - turtle.digDown() - turtle.status = 'waiting' - end + --p.condenseItems() + for i = 1, 16 do + p:extract(i, 64) + end + turtle.digDown() + turtle.setStatus('waiting') + end end) Message.addHandler('finished', - function(h, id) - if not id or id ~= __BUILDER_ID then - return - end + function(_, id) + if not id or id ~= __BUILDER_ID then + return + end Builder:finish() end) Event.on('turtle_abort', function() - turtle.abort = false - turtle.status = 'aborting' + turtle.abort(false) + turtle.setStatus('aborting') Builder:finish() end) local function onTheWay() -- parallel routine 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 id == __BUILDER_ID and msg and msg.type then - if msg.type == 'needSupplies' then - Message.send(__BUILDER_ID, 'gotSupplies', { supplies = true }) - elseif msg.type == 'builder' then - Builder.lastPoint = msg.contents - end + if msg.type == 'needSupplies' then + Message.send(__BUILDER_ID, 'gotSupplies', { supplies = true }) + elseif msg.type == 'builder' then + Builder.lastPoint = msg.contents + end end end end diff --git a/apps/treefarm.lua b/apps/treefarm.lua index 3475cf6..18b61f2 100644 --- a/apps/treefarm.lua +++ b/apps/treefarm.lua @@ -732,11 +732,11 @@ local s, m = turtle.run(function() turtle.setPolicy("attack") - while not turtle.abort do + while not turtle.isAborted() do print('fuel: ' .. turtle.getFuelLevel()) for _,task in ipairs(Util.shallowCopy(tasks)) do --print(task.desc) - turtle.status = task.desc + turtle.setStatus(task.desc) turtle.select(1) if not task.fn() then Util.filterInplace(tasks, function(v) return v.fn ~= task.fn end) diff --git a/etc/scripts/abort b/etc/scripts/abort index a2b446e..891b17a 100644 --- a/etc/scripts/abort +++ b/etc/scripts/abort @@ -1 +1 @@ -turtle.abortAction() \ No newline at end of file +turtle.abort(true) \ No newline at end of file diff --git a/etc/scripts/follow b/etc/scripts/follow index 4853ec5..868d195 100644 --- a/etc/scripts/follow +++ b/etc/scripts/follow @@ -1,12 +1,15 @@ +local os = _G.os +local turtle = _G.turtle + local function follow(id) - requireInjector(getfenv(1)) + _G.requireInjector() local Event = require('event') local Point = require('point') local Socket = require('socket') - turtle.status = 'follow ' .. id + turtle.setStatus('follow ' .. id) if not turtle.enableGPS() then error('turtle: No GPS found') @@ -55,7 +58,7 @@ local function follow(id) Event.onInterval(.5, function() local function getRemotePoint() - if not turtle.abort then + if not turtle.isAborted() then if socket:write({ type = 'gps' }) then return socket:read(3) end @@ -65,7 +68,7 @@ local function follow(id) -- sometimes gps will fail if moving local pt, d - for i = 1, 3 do + for _ = 1, 3 do pt, d = getRemotePoint() if pt then break @@ -73,18 +76,18 @@ local function follow(id) os.sleep(.5) end - if not pt or turtle.abort then + if not pt or turtle.isAborted() then error('Did not receive GPS location') end if not lastPoint or (lastPoint.x ~= pt.x or lastPoint.y ~= pt.y or lastPoint.z ~= pt.z) then if following then - turtle.abort = true + turtle.getState().abort = true while following do os.sleep(.1) end - turtle.abort = false + turtle.getState().abort = false end -- check if gps is inaccurate (player moving too fast) diff --git a/etc/scripts/obsidian b/etc/scripts/obsidian index 99150ae..99ff542 100644 --- a/etc/scripts/obsidian +++ b/etc/scripts/obsidian @@ -70,7 +70,7 @@ local function findObsidian() if not turtle.gotoPoint(node) then break end - until turtle.abort + until turtle.isAborted() end turtle.run(function() @@ -97,7 +97,7 @@ turtle.run(function() turtle.placeDown() turtle.down() turtle.select(1) - until turtle.abort + until turtle.isAborted() end) if not s and m then diff --git a/etc/scripts/summon b/etc/scripts/summon index e841622..159b1a3 100644 --- a/etc/scripts/summon +++ b/etc/scripts/summon @@ -6,7 +6,7 @@ local function summon(id) local Point = require('point') local Socket = require('socket') - turtle.status = 'GPSing' + turtle.setStatus('GPSing') turtle.setPoint({ x = 0, y = 0, z = 0, heading = 0 }) local pts = { @@ -64,7 +64,7 @@ local function summon(id) local pt = { x = pos.x, y = pos.y, z = pos.z } local _, h = Point.calculateMoves(turtle.getPoint(), pt) 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 }) else error("turtle: Could not determine position")