Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- autonav.lua
- -- Put this in /startup (or run it directly). Assumes single warpdriveShipCore.
- local ship = peripheral.find("warpdriveShipCore")
- assert(ship, "No warpdriveShipCore found!")
- local lever = "front"
- local navfile = ".navdata"
- local backupfile = ".navbackup"
- -- read/write 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 one hop and reboot
- local function runOneHop(nav)
- local move = nav[1]
- -- remove first, persist rest (or delete if none)
- if #nav > 1 then
- table.remove(nav,1)
- saveNav(nav)
- else
- clearNav()
- end
- backupNav()
- -- fire the jump
- ship.command("MANUAL", false)
- ship.movement(move[1], move[2], move[3])
- ship.rotationSteps(0)
- ship.command("MANUAL", true)
- print(("→ Executed hop %d/%d FB:%d UD:%d LR:%d")
- :format(1, #nav+1, move[1], move[2], move[3]))
- sleep(5) -- give warpdrive time
- os.reboot() -- next boot will pick up the next hop
- end
- -- if there's a pending nav list, resume it
- local pending = loadNav()
- if pending then
- print("Resuming auto-nav with "..#pending.." hops remaining…")
- runOneHop(pending)
- end
- -- otherwise, listen for a broadcast
- for _,side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side)=="modem" then rednet.open(side) end
- end
- print("Awaiting ONJMcoordBroadcast (lever '"..lever.."’ HIGH)…")
- while true do
- if redstone.getInput(lever) then
- local sender,msg = rednet.receive("ONJMcoordBroadcast", 0.5)
- if sender and type(msg)=="table" and msg.x and msg.y and msg.z then
- local tx,ty,tz = tonumber(msg.x), tonumber(msg.y), tonumber(msg.z)
- print(("Received target from %d → X=%d Y=%d Z=%d")
- :format(sender,tx,ty,tz))
- -- fetch dims & orientation
- local front, right, up = ship.dim_positive()
- local back, left, down = ship.dim_negative()
- local orix,_,oriz = ship.getOrientation()
- local mx,my,mz = ship.getLocalPosition()
- -- world deltas
- local dx, dy, dz = tx-mx, ty-my, tz-mz
- -- ship-local axes
- local shipdeltafront, shipdeltaright
- if orix== 1 then shipdeltafront, shipdeltaright = dx, dz
- elseif orix==-1 then shipdeltafront, shipdeltaright = -dx, -dz
- elseif oriz== 1 then shipdeltafront, shipdeltaright = dz, -dx
- elseif oriz==-1 then shipdeltafront, shipdeltaright = -dz, dx
- else shipdeltafront, shipdeltaright = 0,0 end
- local shipdeltaup = dy
- -- minimal clearance
- local minFB = front+back
- local minUD = up+down
- local minLR = right+left
- -- how far we can jump
- local ok, maxJ = ship.getMaxJumpDistance()
- if not ok or maxJ<=0 then
- maxJ = math.sqrt(shipdeltafront^2 + shipdeltaup^2 + shipdeltaright^2)
- end
- -- compute one-axis move, like main.lua’s computeMove()
- 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 navdata
- local nav = {}
- repeat
- local m = {}
- -- Y first
- m[2] = computeMove(minUD+1, shipdeltaup, true)
- shipdeltaup = shipdeltaup - m[2]
- -- X (forward/back)
- m[1] = computeMove(minFB+1, shipdeltafront,
- math.abs(m[2])>minUD)
- if not (math.abs(m[2])>minUD)
- and shipdeltafront==0 and shipdeltaup~=0 and shipdeltaright==0
- and m[1]==0
- then
- m[1] = minFB+1
- end
- shipdeltafront = shipdeltafront - m[1]
- -- Z (left/right)
- m[3] = computeMove(minLR+1, shipdeltaright,
- math.abs(m[2])>minUD or math.abs(m[1])>minFB)
- shipdeltaright = shipdeltaright - m[3]
- table.insert(nav, m)
- until shipdeltafront==0 and shipdeltaup==0 and shipdeltaright==0
- -- persist and run first hop
- saveNav(nav)
- print("Computed "..#nav.." hop(s); starting…")
- runOneHop(nav)
- end
- else
- sleep(0.1)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement