Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- autonav.lua
- -- Place this as /startup (or make sure it's run on boot).
- local ship = peripheral.find("warpdriveShipCore")
- assert(ship, "No warpdriveShipCore found!")
- local navfile = ".navdata"
- local backupfile = ".navbackup"
- -- load / save helpers
- local function loadNav()
- if fs.exists(navfile) then
- local f = fs.open(navfile, "r")
- local t = textutils.unserialize(f.readAll())
- f.close()
- return t
- end
- end
- local function saveNav(t)
- local f = fs.open(navfile, "w")
- f.write(textutils.serialize(t))
- f.close()
- end
- local function backupNav()
- if fs.exists(navfile) then
- if fs.exists(backupfile) then fs.delete(backupfile) end
- fs.copy(navfile, backupfile)
- end
- end
- local function clearNav()
- if fs.exists(navfile) then fs.delete(navfile) end
- end
- -- execute just one hop then reboot
- local function runOneHop(nav)
- local move = table.remove(nav, 1)
- if #nav > 0 then
- saveNav(nav)
- else
- clearNav()
- end
- backupNav()
- -- fire the jump
- ship.command("MANUAL", false)
- ship.movement(move[1], 0, move[2]) -- [1]=forward/back, [2]=left/right
- ship.rotationSteps(0)
- ship.command("MANUAL", true)
- print(string.format(
- "→ Executed hop. Remaining: %d", #nav
- ))
- sleep(5)
- os.reboot()
- end
- -- if there’s a pending nav list, resume it
- local pending = loadNav()
- if pending then
- print("Resuming auto-nav, hops left:", #pending)
- runOneHop(pending)
- end
- -- otherwise wait for a broadcast
- for _, side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side) == "modem" then
- rednet.open(side)
- end
- end
- print("Waiting for ONJMcoordBroadcast…")
- local _, msg = rednet.receive("ONJMcoordBroadcast")
- assert(type(msg)=="table" and msg.x and msg.z,
- "Invalid broadcast payload")
- local tx, tz = tonumber(msg.x), tonumber(msg.z)
- print(("Target received → X:%d Z:%d"):format(tx, tz))
- -- fetch dims & orientation
- local front, right = ship.dim_positive()
- local back, left = ship.dim_negative()
- local rx,_,rz = ship.getOrientation()
- local mx,_,mz = ship.getLocalPosition()
- -- world deltas
- local dx, dz = tx-mx, tz-mz
- -- convert into ship-local axes
- local fb, lr = 0,0
- if rx== 1 then fb, lr = dx, dz
- elseif rx==-1 then fb, lr = -dx, -dz
- elseif rz== 1 then fb, lr = dz, -dx
- elseif rz==-1 then fb, lr = -dz, dx
- end
- -- clearance + max jump
- local minFB = front+back
- local minLR = right+left
- local ok, maxJ = ship.getMaxJumpDistance()
- if not ok or maxJ<=0 then
- error("Could not get max jump distance")
- end
- -- one-axis “computeMove” from your main.lua
- local function computeMove(mindist, rem, unconstrained)
- if rem==0 then return 0 end
- local s = rem<0 and -1 or 1
- if math.abs(rem)<mindist and not unconstrained then
- return -s*mindist
- end
- return s * math.min(math.abs(rem), maxJ)
- end
- -- build the full list of (forward/back, left/right) hops
- local nav = {}
- repeat
- local m_fb = computeMove(minFB+1, fb, false)
- fb = fb - m_fb
- local m_lr = computeMove(minLR+1, lr, math.abs(m_fb)>minFB)
- lr = lr - m_lr
- table.insert(nav, { m_fb, m_lr })
- until fb==0 and lr==0
- assert(#nav>0, "Computed zero hops?")
- -- save & run first hop
- saveNav(nav)
- print("Computed "..#nav.." hops; starting…")
- runOneHop(nav)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement