From e6ee432997852d8dce336fd0d20e8e36eb1c1c44 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Wed, 24 May 2017 19:48:48 -0400 Subject: [PATCH] relative-gps coord system --- sys/apis/gps.lua | 8 ++++---- sys/apis/ui.lua | 14 +++++++++++++- sys/apps/builder.lua | 15 ++++++++------- sys/apps/supplier.lua | 8 +++++--- sys/autorun/gps.lua | 23 ++++++----------------- sys/etc/scripts/follow | 6 +----- sys/etc/scripts/moveTo | 5 +---- sys/etc/scripts/summon | 1 + sys/extensions/scheduler.lua | 1 + sys/extensions/tgps.lua | 27 +++++++++++---------------- sys/extensions/tl3.lua | 11 +++++++++++ 11 files changed, 62 insertions(+), 57 deletions(-) diff --git a/sys/apis/gps.lua b/sys/apis/gps.lua index 57d9246..4abaafd 100644 --- a/sys/apis/gps.lua +++ b/sys/apis/gps.lua @@ -31,13 +31,13 @@ function GPS.getPoint(timeout, debug) return pt end -function GPS.getHeading() +function GPS.getHeading(timeout) if not turtle then return end - local apt = GPS.locate() + local apt = GPS.locate(timeout) if not apt then return end @@ -67,8 +67,8 @@ function GPS.getHeading() return 3 end -function GPS.getPointAndHeading() - local heading = GPS.getHeading() +function GPS.getPointAndHeading(timeout) + local heading = GPS.getHeading(timeout) if heading then local pt = GPS.getPoint() if pt then diff --git a/sys/apis/ui.lua b/sys/apis/ui.lua index 4322592..35358e3 100644 --- a/sys/apis/ui.lua +++ b/sys/apis/ui.lua @@ -1260,6 +1260,8 @@ function UI.Device:runTransitions(transitions, canvas) canvas:blitClipped(self.device) -- and blit the remainder canvas:reset() + local queue = { } -- don't miss events while transition is running + -- especially timers while true do for _,k in ipairs(Util.keys(transitions)) do local transition = transitions[k] @@ -1270,7 +1272,17 @@ function UI.Device:runTransitions(transitions, canvas) if Util.empty(transitions) then break end - os.sleep() -- ? + local timerId = os.startTimer(0) + while true do + local e = { os.pullEvent() } + if e[1] == 'timer' and e[2] == timerId then + break + end + table.insert(queue, e) + end + end + for _, e in ipairs(queue) do + Event.processEvent(e) end end diff --git a/sys/apps/builder.lua b/sys/apps/builder.lua index f51a4ab..0bd3b74 100644 --- a/sys/apps/builder.lua +++ b/sys/apps/builder.lua @@ -2143,13 +2143,14 @@ UI:setPages({ }) UI:setPage('start') -turtle.setPolicy(turtle.policies.digAttack) -turtle.setPoint({ x = -1, z = -1, y = 0, heading = 0 }) -turtle.saveLocation('supplies') -turtle.status = 'idle' -turtle.abort = false -Event.pullEvents() +turtle.run(function() + turtle.setPolicy(turtle.policies.digAttack) + turtle.setPoint({ x = -1, z = -1, y = 0, heading = 0 }) + turtle.getState().coordSystem = 'relative' + turtle.saveLocation('supplies') + Event.pullEvents() +end) UI.term:reset() -turtle.status = 'idle' +--turtle.status = 'idle' diff --git a/sys/apps/supplier.lua b/sys/apps/supplier.lua index be807d8..952a80c 100644 --- a/sys/apps/supplier.lua +++ b/sys/apps/supplier.lua @@ -387,9 +387,6 @@ __BUILDER_ID = tonumber(args[1]) maxStackDB:load() -turtle.setPoint({ x = -1, z = -2, y = 0, heading = 0 }) -turtle.saveLocation('supplies') - Builder.itemProvider = MEProvider() if not Builder.itemProvider:isValid() then Builder.itemProvider = ChestProvider() @@ -399,5 +396,10 @@ if not Builder.itemProvider:isValid() then end turtle.run(function() + turtle.setPoint({ x = -1, z = -2, y = 0, heading = 0 }) + turtle.getState().coordSystem = 'relative' + + turtle.saveLocation('supplies') + Event.pullEvents(onTheWay) end) diff --git a/sys/autorun/gps.lua b/sys/autorun/gps.lua index f9dec4c..6c5cbe9 100644 --- a/sys/autorun/gps.lua +++ b/sys/autorun/gps.lua @@ -12,21 +12,16 @@ if turtle and device.wireless_modem then } Config.load('gps', config) - local GPS = require('gps') - local pt - for i = 1, 3 do - pt = GPS.getPointAndHeading(2) - if pt then - break - end + local s = turtle.enableGPS(2) + if not s then + s = turtle.enableGPS(2) end - - if not pt and config.destructive then + if not s and config.destructive then turtle.setPolicy('turtleSafe') - pt = GPS.getPointAndHeading(2) + s = turtle.enableGPS(2) end - if not pt then + if not s then error('Unable to get GPS position') end @@ -34,18 +29,12 @@ if turtle and device.wireless_modem then turtle.setPolicy('turtleSafe') end - Util.print('Setting turtle point to %d %d %d', pt.x, pt.y, pt.z) - turtle.setPoint(pt) - turtle.getState().coordSystem = 'GPS' - if not turtle.pathfind(homePt) then error('Failed to return home') end end end) - turtle.setPolicy('none') - if not s and m then error(m) end diff --git a/sys/etc/scripts/follow b/sys/etc/scripts/follow index b7ba73c..287e8ee 100644 --- a/sys/etc/scripts/follow +++ b/sys/etc/scripts/follow @@ -1,20 +1,16 @@ local function follow(id) require = requireInjector(getfenv(1)) - local GPS = require('gps') local Socket = require('socket') local Point = require('point') local process = require('process') turtle.status = 'follow ' .. id - local pt = GPS.getPointAndHeading() - if not pt or not pt.heading then + if not turtle.enableGPS() then error('turtle: No GPS found') end - turtle.setPoint(pt) - local socket = Socket.connect(id, 161) if not socket then error('turtle: Unable to connect to ' .. id) diff --git a/sys/etc/scripts/moveTo b/sys/etc/scripts/moveTo index 0ca0caf..7db9c52 100644 --- a/sys/etc/scripts/moveTo +++ b/sys/etc/scripts/moveTo @@ -5,13 +5,10 @@ turtle.run(function() local id = {COMPUTER_ID} - local pt = GPS.getPointAndHeading() - if not pt or not pt.heading then + if not turtle.enableGPS() then error('turtle: No GPS found') end - turtle.setPoint(pt) - local socket = Socket.connect(id, 161) if not socket then error('turtle: Unable to connect to ' .. id) diff --git a/sys/etc/scripts/summon b/sys/etc/scripts/summon index 0c00e07..ff36ce2 100644 --- a/sys/etc/scripts/summon +++ b/sys/etc/scripts/summon @@ -7,6 +7,7 @@ local function summon(id) turtle.status = 'GPSing' turtle.setPoint({ x = 0, y = 0, z = 0, heading = 0 }) + turtle.getState().coordSystem = 'relative' local pts = { [ 1 ] = { x = 0, z = 0, y = 0 }, diff --git a/sys/extensions/scheduler.lua b/sys/extensions/scheduler.lua index 6fad73b..3a2546f 100644 --- a/sys/extensions/scheduler.lua +++ b/sys/extensions/scheduler.lua @@ -67,6 +67,7 @@ function turtle.run(fn, ...) end if id == ticketId then turtle.abort = false + turtle.resetState() local args = { ... } local s, m = pcall(function() fn(unpack(args)) end) turtle.abort = false diff --git a/sys/extensions/tgps.lua b/sys/extensions/tgps.lua index 81dee09..729ea2f 100644 --- a/sys/extensions/tgps.lua +++ b/sys/extensions/tgps.lua @@ -5,10 +5,15 @@ end require = requireInjector(getfenv(1)) local GPS = require('gps') -function turtle.enableGPS() - local pt = GPS.getPointAndHeading() +function turtle.enableGPS(timeout) + if turtle.getState().coordSystem == 'GPS' then + return turtle.point + end + + local pt = GPS.getPointAndHeading(timeout) if pt then turtle.setPoint(pt) + turtle.getState().coordSystem = 'GPS' return true end end @@ -16,25 +21,15 @@ end function turtle.gotoGPSHome() local homePt = turtle.loadLocation('gpsHome') if homePt then - local pt = GPS.getPointAndHeading() - if pt then - turtle.setPoint(pt) + if turtle.enableGPS() then turtle.pathfind(homePt) end end end function turtle.setGPSHome() - local GPS = require('gps') - - local pt = GPS.getPoint() - if pt then - turtle.setPoint(pt) - pt.heading = GPS.getHeading() - if pt.heading then - turtle.point.heading = pt.heading - turtle.storeLocation('gpsHome', pt) - turtle.gotoPoint(pt) - end + if turtle.enableGPS() then + turtle.storeLocation('gpsHome', turtle.point) + turtle.gotoPoint(tturtle.point) end end diff --git a/sys/extensions/tl3.lua b/sys/extensions/tl3.lua index 0f195d6..9bcf7b7 100644 --- a/sys/extensions/tl3.lua +++ b/sys/extensions/tl3.lua @@ -50,6 +50,17 @@ function turtle.reset() return true end +function turtle.resetState() + --turtle.abort = false -- should be part of state + --turtle.status = 'idle' -- should be part of state + state.moveAttack = noop + state.moveDig = noop + state.moveCallback = noop + state.locations = {} + + return true +end + local actions = { up = { detect = turtle.native.detectUp,