immibis

mine

Apr 13th, 2013
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.31 KB | None | 0 0
  1. print("Press x in 5 seconds to cancel startup")
  2. do
  3. local timer = os.startTimer(5)
  4. local evt, p
  5. repeat
  6. evt, p = os.pullEvent()
  7. until (evt == "char" and p == "x") or (evt == "timer" and p == timer)
  8. if evt == "char" then return end
  9. end
  10.  
  11. local startX, startZ -- world coords of centre of mine
  12. local state
  13. local miningX, miningZ -- centre-relative coords of next block to mine
  14. local turtX, turtZ -- centre-relative turtle coords
  15. local faceX, faceZ = 0, 0
  16.  
  17. print("Attempting self-update")
  18. pcall(function()
  19. local c = http.get("http://immibis.com/ccfiles/rrts1/mine.lua")
  20. if not c then print("Update failed") return end
  21. local d = c.readAll()
  22. c.close()
  23.  
  24. local f = fs.open("startup", "r")
  25. local d2 = f.readAll()
  26. f.close()
  27.  
  28. if d ~= d2 then
  29. f = fs.open("startup", "w")
  30. f.write(d)
  31. f.close()
  32. print("Update successful")
  33. os.reboot()
  34. while true do coroutine.yield() end
  35. else
  36. print("No update required")
  37. end
  38. end)
  39.  
  40. function onDumpFull()
  41. -- rednet.send(8, "wrs16 full") -- notify shuttle via long distance relay
  42. end
  43.  
  44. function onFuelEmpty()
  45. -- rednet.send(8, "wrs16 fuel") -- notify shuttle via long distance relay
  46. end
  47.  
  48. function onDumpFinished()
  49. -- rednet.send(8, "wrs16 call") -- notify shuttle via long distance relay
  50. end
  51.  
  52. --rednet.open("right")
  53.  
  54. local function save()
  55. if fs.exists("/minestate") then
  56. if fs.exists("/msbak") then
  57. fs.delete("/msbak")
  58. end
  59. fs.move("/minestate", "/msbak")
  60. end
  61. local f = fs.open("/minestate", "w")
  62. f.writeLine(tostring(startX))
  63. f.writeLine(tostring(startZ))
  64. f.writeLine(state)
  65. f.writeLine(tostring(miningX))
  66. f.writeLine(tostring(miningZ))
  67. f.writeLine(tostring(turtX))
  68. f.writeLine(tostring(turtZ))
  69. f.writeLine(tostring(faceX))
  70. f.writeLine(tostring(faceZ))
  71. f.close()
  72. end
  73.  
  74. local function checkGPS()
  75. write("GPS is ")
  76. local x = gps.locate(1)
  77. if x == nil then
  78. print("down")
  79. return false
  80. else
  81. print("up")
  82. return true
  83. end
  84. end
  85.  
  86. local function gpslocate()
  87. local x, y, z
  88. repeat
  89. x, y, z = gps.locate(2)
  90. if not x then
  91. print("GPS failure!")
  92. end
  93. until x ~= nil
  94. return x, z
  95. end
  96.  
  97. local function f()
  98. while not turtle.forward() do
  99. if peripheral.getType("front") ~= nil then
  100. -- Don't mine other turtles
  101. sleep(0.5)
  102. else
  103. if not turtle.dig() then
  104. turtle.attack()
  105. end
  106. end
  107. end
  108. turtX = turtX + faceX
  109. turtZ = turtZ + faceZ
  110. end
  111.  
  112. local function forwardNoDig()
  113. if turtle.forward() then
  114. turtX = turtX + faceX
  115. turtZ = turtZ + faceZ
  116. save()
  117. return true
  118. else
  119. return false
  120. end
  121. end
  122.  
  123. local function r()
  124. turtle.turnRight()
  125. faceX, faceZ = -faceZ, faceX
  126. end
  127.  
  128. local function l()
  129. turtle.turnLeft()
  130. faceX, faceZ = faceZ, -faceX
  131. end
  132.  
  133. local function gpsgetfacing()
  134. local x1, z1 = gpslocate()
  135. f()
  136. local x2, z2 = gpslocate()
  137. r()
  138. r()
  139. f()
  140. r()
  141. r()
  142. return x2 - x1, z2 - z1
  143. end
  144.  
  145. function manualCoordReset()
  146. print("Enter current turtle coordinates.")
  147. write("X: ")
  148. turtX = tonumber(read()) or error("Invalid number", 0)
  149. write("Z: ")
  150. turtZ = tonumber(read()) or error("Invalid number", 0)
  151.  
  152. turtX = turtX - startX
  153. turtZ = turtZ - startZ
  154.  
  155. print("Enter current turtle direction (+X/-X/+Z/-Z)")
  156. local dir = read():lower()
  157. if dir == "-x" then faceX, faceZ = -1, 0
  158. elseif dir == "+x" then faceX, faceZ = 1, 0
  159. elseif dir == "-z" then faceX, faceZ = 0, -1
  160. elseif dir == "+z" then faceX, faceZ = 0, 1
  161. else error("Invalid direction", 0)
  162. end
  163. end
  164.  
  165. if fs.exists("/minestate") or fs.exists("/msbak") then
  166. function readState(filename)
  167. local f = fs.open(filename,"r")
  168. startX = tonumber(f.readLine())
  169. startZ = tonumber(f.readLine())
  170. state = f.readLine()
  171. miningX = tonumber(f.readLine())
  172. miningZ = tonumber(f.readLine())
  173. turtX = tonumber(f.readLine())
  174. turtZ = tonumber(f.readLine())
  175. faceX = tonumber(f.readLine())
  176. faceZ = tonumber(f.readLine())
  177. f.close()
  178. end
  179. if not pcall(readState, "/minestate") then
  180. readState("/msbak")
  181. end
  182.  
  183. if checkGPS() then
  184. turtX, turtZ = gpslocate()
  185. turtX = turtX - startX
  186. turtZ = turtZ - startZ
  187. faceX, faceZ = gpsgetfacing()
  188. else
  189. state = "resetpos"
  190. end
  191. else
  192. if not checkGPS() then
  193. startX, startZ = 0, 0
  194. manualCoordReset()
  195. startX, startZ = turtX, turtZ
  196. else
  197. startX, startZ = gpslocate()
  198. turtX, turtZ = 0, 0
  199. faceX, faceZ = gpsgetfacing()
  200. end
  201. turtX, turtZ = 0, 0
  202. state = "mine"
  203. miningX, miningZ = -1, -1
  204. end
  205.  
  206. local function face(dx, dz)
  207. if dx == faceX and dz == faceZ then
  208. -- do nothing
  209. elseif dx == faceZ and dz == -faceX then
  210. l()
  211. elseif dx == -faceZ and dz == faceX then
  212. r()
  213. elseif dx == -faceX and dz == -faceZ then
  214. l() l()
  215. else
  216. error("invalid orientation "..dx..","..dz, 2)
  217. end
  218. end
  219.  
  220. function go(x, z)
  221. --print("goto ",x,",",z," from ",turtX,",",turtZ)
  222. if x > turtX then
  223. face(1, 0)
  224. while x > turtX do f() save() end
  225. elseif x < turtX then
  226. face(-1, 0)
  227. while x < turtX do f() save() end
  228. end
  229. if z > turtZ then
  230. face(0, 1)
  231. while z > turtZ do f() save() end
  232. elseif z < turtZ then
  233. face(0, -1)
  234. while z < turtZ do f() save() end
  235. end
  236. assert(x == turtX and z == turtZ, "something fucked up")
  237. end
  238.  
  239. print("At ",turtX,",",turtZ," facing ",faceX,",",faceZ)
  240. save()
  241.  
  242. turtle.select(1)
  243.  
  244. while true do
  245. if state == "mine" then
  246. go(miningX, miningZ)
  247. local radius = math.max(math.abs(miningX), math.abs(miningZ))
  248.  
  249. --[[
  250.  
  251. -- --> +-
  252. <<
  253. ^ v
  254.  
  255. -+ <-- ++
  256.  
  257.  
  258. ]]
  259.  
  260. if miningZ == -radius and miningX ~= radius then
  261. face(1, 0)
  262. elseif miningX == radius and miningZ ~= radius then
  263. face(0, 1)
  264. elseif miningZ == radius and miningX ~= -radius then
  265. face(-1, 0)
  266. elseif miningX == -radius and miningZ ~= -radius then
  267. face(0, -1)
  268. else
  269. error("shouldn't get here, position "..miningX..","..miningZ.." (radius is "..radius..")")
  270. end
  271.  
  272. -- leave a 5x5 platform
  273. if math.abs(turtX) > 2 or math.abs(turtZ) > 2 then
  274. turtle.digUp()
  275. turtle.digDown()
  276. end
  277. f()
  278. if turtX == -radius and turtZ == -radius + 1 then
  279. -- expand radius
  280. miningX = turtX - 1
  281. miningZ = turtZ - 2
  282.  
  283. -- otherwise there's a diagonal line of unmined blocks
  284. if math.abs(turtX) > 2 or math.abs(turtZ) > 2 then
  285. turtle.digUp()
  286. turtle.digDown()
  287. end
  288. else
  289. miningX, miningZ = turtX, turtZ
  290. end
  291.  
  292. if turtle.getFuelLevel() < 600 or turtle.getItemCount(16) > 0 then
  293. if math.abs(turtX) > math.abs(turtZ) then
  294. state = "dropoff_zfirst"
  295. else
  296. state = "dropoff_xfirst"
  297. end
  298. end
  299.  
  300. save()
  301.  
  302. elseif state == "dropoff_zfirst" then
  303. go(turtX, 0)
  304. state = "dropoff_last"
  305. save()
  306. elseif state == "dropoff_xfirst" then
  307. go(0, turtZ)
  308. state = "dropoff_last"
  309. save()
  310. elseif state == "dropoff_last" then
  311. go(0, 0)
  312.  
  313. for k=1,16 do
  314. while turtle.getItemCount(k) > 0 do
  315. turtle.select(k)
  316. while not turtle.dropUp() do
  317. onDumpFull()
  318. print("Dump chest full")
  319. sleep(5)
  320. end
  321. end
  322. end
  323. onDumpFinished()
  324.  
  325. while turtle.getFuelLevel() < 2000 do
  326. turtle.select(1)
  327. while not turtle.suckDown() do
  328. print("No fuel in chest!")
  329. onFuelEmpty()
  330. sleep(5)
  331. end
  332. if not turtle.refuel() then
  333. print("Found non-fuel item in fuel chest")
  334. while not turtle.dropUp() do
  335. print("Dump chest full")
  336. onDumpFull()
  337. sleep(5)
  338. end
  339. end
  340. end
  341.  
  342. turtle.select(1)
  343. state = "resume"
  344. save()
  345.  
  346. elseif state == "resume" then
  347. if math.abs(miningX) > math.abs(miningZ) then
  348. state = "resume_xfirst"
  349. else
  350. state = "resume_zfirst"
  351. end
  352. save()
  353.  
  354. elseif state == "resume_xfirst" then
  355. go(miningX, turtZ)
  356. state = "mine"
  357. save()
  358.  
  359. elseif state == "resume_zfirst" then
  360. go(turtX, miningZ)
  361. state = "mine"
  362. save()
  363.  
  364. elseif state == "resetpos" then
  365. if math.abs(turtX) > math.abs(turtZ) then
  366. go(turtX, 0)
  367. go(0, 0)
  368. else
  369. go(0, turtZ)
  370. go(0, 0)
  371. end
  372. if checkGPS() then
  373. turtX, turtZ = gpslocate()
  374. turtX = turtX - startX
  375. turtZ = turtZ - startZ
  376. faceX, faceZ = 0, 0
  377. faceX, faceZ = gpsgetfacing()
  378. state = "resume"
  379. save()
  380.  
  381. print("Recovered position from GPS, resuming mining")
  382.  
  383. else
  384. print("No GPS signal to reset position")
  385. manualCoordReset()
  386. state = "resume"
  387. save()
  388. end
  389.  
  390. else
  391. error("unknown state! "..state)
  392. end
  393. end
Advertisement
Add Comment
Please, Sign In to add comment