Advertisement
KVT36

Quarry

Aug 11th, 2015
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.35 KB | None | 0 0
  1. local robot = require("robot")
  2. local computer = require("computer")
  3. local component = require("component")
  4. local sides = require("sides")
  5. local ic = component.inventory_controller
  6.  
  7. inventorySize = robot.inventorySize()
  8.  
  9. toolSlots = {
  10. shovel = {1, 2, 3, 4},
  11. pickaxe = {5, 6, 7, 8}
  12. }
  13.  
  14. currentTool = "none"
  15. currentToolSlot = -1
  16.  
  17. dropFirstSlot = 9
  18. isInverted = false
  19.  
  20. saveCoords = {x = 0, y = 0, z = 0, side = 1}
  21. baseCoords = {x = 0, y = 0, z = 0, side = 1}
  22. chardgeCoords = {x = 0, y = 0, z = 0, side = 1}
  23. dropCoords = {x = 0, y = 0, z = 0, side = 3}
  24. loadToolShovelCoords = {x = 1, y = 0, z = 0, side = 3}
  25. loadToolPickaxeCoords = {x = 2, y = 0, z = 0, side = 3}
  26. curCoords = {x = 0, y = 0, z = 0, side = 1}
  27. dx = {0, 1, 0, -1}
  28. dz = {1, 0, -1, 0}
  29.  
  30. -- 1
  31. -- Z
  32. --
  33. -- 4 0 X2
  34. --
  35. --
  36. -- 3
  37.  
  38. function getEnergy()
  39. return computer.energy() / computer.maxEnergy()
  40. end
  41.  
  42. function getItemCount(slotList)
  43. itemCount = 0
  44. for i = 1,#slotList do
  45. itemCount = itemCount + robot.count(slotList[i])
  46. end
  47. return itemCount
  48. end
  49.  
  50. function getFirstNotEmptySlot(slotList)
  51. for i = 1,#slotList do
  52. if robot.count(slotList[i]) > 0 then
  53. return slotList[i]
  54. end
  55. end
  56. return nil
  57. end
  58.  
  59. function toolEquip(slot)
  60. t = robot.select()
  61. robot.select(slot)
  62. ic.equip()
  63. robot.select(t)
  64. end
  65.  
  66. function equipTool(tool)
  67. if currentTool == "breaking" then
  68. toolEquip(dropFirstSlot)
  69. currentTool = "none"
  70. end
  71. if currentTool == "none" then
  72. currentToolSlot = getFirstNotEmptySlot(toolSlots[tool])
  73. toolEquip(currentToolSlot)
  74. currentTool = tool
  75. else
  76. toolEquip(currentToolSlot)
  77. currentToolSlot = getFirstNotEmptySlot(toolSlots[tool])
  78. toolEquip(currentToolSlot)
  79. currentTool = tool
  80. end
  81. end
  82.  
  83. function changeTool(side)
  84. data = geolyzer.analyze(side)
  85. tool = data.harvestTool
  86. v, s = robot.durability()
  87. if v == nill or s == "no tool equipped" then
  88. currentTool = "none"
  89. end
  90. if v == 0 then
  91. currentTool = "breaking"
  92. end
  93. if tool == "axe" or tool == nil then
  94. equipTool("shovel")
  95. else
  96. if tool == "shovel" or tool == "pickaxe" then
  97. if currentTool ~= tool then
  98. equipTool(tool)
  99. end
  100. else
  101. equipTool("pickaxe")
  102. end
  103. end
  104. end
  105.  
  106. function returnToBaseWithError(messageID)
  107. if messageID == 1 then
  108. message = "ERROR: Can't break block: x = " .. curCoords.x .. ", y = " .. curCoords.y .. ", z = " .. curCoords.z
  109. print(message)
  110. file = io.open("error", "w")
  111. io.output(file)
  112. io.write(message)
  113. io.close(file)
  114. end
  115. goTo(baseCoords)
  116. turnTo(1)
  117. while true do
  118. computer.shutdown(false)
  119. end
  120. end
  121.  
  122. function dig()
  123. conter = 0
  124. while robot.detect() do
  125. changeTool(sides.front)
  126. robot.swing()
  127. conter = conter + 1
  128. if conter > 10 then
  129. returnToBaseWithError(1)
  130. end
  131. os.sleep(0)
  132. end
  133. end
  134.  
  135. function digUp()
  136. conter = 0
  137. while robot.detectUp() do
  138. changeTool(sides.top)
  139. robot.swingUp()
  140. conter = conter + 1
  141. if conter > 10 then
  142. returnToBaseWithError(1)
  143. end
  144. os.sleep(0)
  145. end
  146. end
  147.  
  148. function digDown()
  149. conter = 0
  150. while robot.detectDown() do
  151. changeTool(sides.bottom)
  152. robot.swingDown()
  153. conter = conter + 1
  154. if conter > 10 then
  155. returnToBaseWithError(1)
  156. end
  157. os.sleep(0)
  158. end
  159. end
  160.  
  161. function forward(isDigUp, isDigDown)
  162. if robot.detect() then
  163. dig()
  164. end
  165. while not robot.forward() do
  166. dig()
  167. end
  168. if isDigUp then
  169. digUp()
  170. end
  171. if isDigDown then
  172. digDown()
  173. end
  174. curCoords.x = curCoords.x + dx[curCoords.side]
  175. curCoords.z = curCoords.z + dz[curCoords.side]
  176. os.sleep(0)
  177. end
  178.  
  179. function up(isDigUp)
  180. if robot.detectUp() then
  181. digUp()
  182. end
  183. while not robot.up() do
  184. digUp()
  185. end
  186. if isDigUp then
  187. digUp()
  188. end
  189. curCoords.y = curCoords.y + 1
  190. os.sleep(0)
  191. end
  192.  
  193. function down(isDigDown)
  194. if robot.detectDown() then
  195. digDown()
  196. end
  197. while not robot.down() do
  198. digDown()
  199. end
  200. if isDigDown then
  201. digDown()
  202. end
  203. curCoords.y = curCoords.y - 1
  204. os.sleep(0)
  205. end
  206.  
  207. function turnLeft()
  208. robot.turnLeft()
  209. curCoords.side = curCoords.side - 1;
  210. if curCoords.side == 0 then
  211. curCoords.side = 4
  212. end
  213. os.sleep(0)
  214. end
  215.  
  216. function turnRight()
  217. robot.turnRight()
  218. curCoords.side = curCoords.side + 1;
  219. if curCoords.side == 5 then
  220. curCoords.side = 1
  221. end
  222. os.sleep(0)
  223. end
  224.  
  225. function turnTo(side)
  226. if curCoords.side == side then
  227. return
  228. end
  229.  
  230. if (curCoords.side - side) % 2 == 0 then
  231. turnLeft()
  232. turnLeft()
  233. return
  234. end
  235.  
  236. if (curCoords.side + 1) % 4 == side % 4 then
  237. turnRight()
  238. return
  239. end
  240.  
  241. if (curCoords.side - 1) % 4 == side % 4 then
  242. turnLeft()
  243. return
  244. end
  245. end
  246.  
  247. function goTo(coord, digUp, digDown)
  248. while curCoords.y ~= coord.y do
  249. if curCoords.y > coord.y then
  250. down(digDown)
  251. else
  252. up(digUp)
  253. end
  254. end
  255.  
  256. if curCoords.x ~= coord.x then
  257. if curCoords.x > coord.x then
  258. turnTo(4)
  259. else
  260. turnTo(2)
  261. end
  262. while curCoords.x ~= coord.x do
  263. forward(digUp, digDown)
  264. end
  265. end
  266.  
  267. if curCoords.z ~= coord.z then
  268. if curCoords.z > coord.z then
  269. turnTo(3)
  270. else
  271. turnTo(1)
  272. end
  273. while curCoords.z ~= coord.z do
  274. forward(digUp, digDown)
  275. end
  276. end
  277. end
  278.  
  279. function checkEnergy(value)
  280. return getEnergy() >= value
  281. end
  282.  
  283. function checkTool()
  284. return getItemCount(toolSlots["shovel"]) > 0 and getItemCount(toolSlots["pickaxe"]) > 0
  285. end
  286.  
  287. function checkDrop()
  288. firstEmptySlot = inventorySize
  289. for i = dropFirstSlot,inventorySize do
  290. if robot.count(i) == 0 then
  291. firstEmptySlot = i
  292. break
  293. end
  294. end
  295.  
  296. return firstEmptySlot + 2 <= inventorySize
  297. end
  298.  
  299. function charging()
  300. goTo(chardgeCoords)
  301. while not checkEnergy(0.95) do
  302. os.sleep(1)
  303. end
  304. end
  305.  
  306. function drop()
  307. goTo(dropCoords)
  308. turnTo(dropCoords.side)
  309. for i = dropFirstSlot,inventorySize do
  310. robot.select(i)
  311. robot.drop()
  312. end
  313.  
  314. robot.select(dropFirstSlot)
  315. end
  316.  
  317. function loadTool()
  318. if currentTool ~= "none" then
  319. if currentTool == "shovel" then
  320. toolEquip(toolSlots["shovel"][1])
  321. currentTool = "none"
  322. end
  323. if currentTool == "pickaxe" then
  324. toolEquip(toolSlots["pickaxe"][1])
  325. currentTool = "none"
  326. end
  327. end
  328.  
  329. goTo(loadToolShovelCoords)
  330. turnTo(loadToolShovelCoords.side)
  331. for i = 1,#toolSlots["shovel"] do
  332. if robot.count(toolSlots["shovel"][i]) == 0 then
  333. robot.select(toolSlots["shovel"][i])
  334. robot.suck()
  335. end
  336. end
  337.  
  338. goTo(loadToolPickaxeCoords)
  339. turnTo(loadToolPickaxeCoords.side)
  340. for i = 1,#toolSlots["pickaxe"] do
  341. if robot.count(toolSlots["pickaxe"][i]) == 0 then
  342. robot.select(toolSlots["pickaxe"][i])
  343. robot.suck()
  344. end
  345. end
  346.  
  347. robot.select(dropFirstSlot)
  348. end
  349.  
  350. function returnToBase()
  351. saveCoords.x = curCoords.x
  352. saveCoords.y = curCoords.y
  353. saveCoords.z = curCoords.z
  354. goTo(baseCoords)
  355. if not checkEnergy(0.95) then
  356. charging()
  357. end
  358. if not checkDrop() then
  359. drop()
  360. end
  361. if not checkTool() then
  362. loadTool()
  363. end
  364.  
  365. goTo(baseCoords)
  366. turnTo(1)
  367. forward(true)
  368. goTo(saveCoords)
  369. turnTo(saveCoords.side)
  370. end
  371.  
  372. function check()
  373. if not checkEnergy(0.2) or not checkTool() or not checkDrop() then
  374. returnToBase()
  375. end
  376. end
  377.  
  378. function main(x, z, y, rx, rz, ry)
  379. if component.isAvailable("geolyzer") then
  380. geolyzer = component.geolyzer
  381. else
  382. geolyzer = nil
  383. print("Need geolyzer component")
  384. return
  385. end
  386.  
  387. if rx ~= nil and ry ~= nil and rz ~= nil then
  388. isRev = true
  389. rx = tonumber(rx)
  390. ry = tonumber(ry)
  391. rz = tonumber(rz)
  392. end
  393.  
  394. returnToBase()
  395. forward(true)
  396. coordTo = {x = 0, y = 0, z = 0, side = 1}
  397. robot.select(dropFirstSlot)
  398. for k = 0, y / 3 do
  399. for j = 0, x - 1 do
  400. for i = 1, z do
  401. if k % 2 == 0 then
  402. coordTo.x = j
  403. if j % 2 == 0 then
  404. coordTo.z = i
  405. else
  406. coordTo.z = z - i + 1
  407. end
  408. else
  409. coordTo.x = x - j - 1
  410. if (x % 2 == 0) then
  411. if j % 2 == 0 then
  412. coordTo.z = i
  413. else
  414. coordTo.z = z - i + 1
  415. end
  416. else
  417. if j % 2 == 0 then
  418. coordTo.z = z - i + 1
  419. else
  420. coordTo.z = i
  421. end
  422. end
  423. end
  424.  
  425. coordTo.y = -k * 3 - 1
  426. if coordTo.y < -y then
  427. coordTo.y = -y
  428. end
  429.  
  430. if isRev then
  431. if coordTo.x == rx and coordTo.y == ry and coordTo.z == rz then
  432. goTo(coordTo, coordTo.y < 0 and coordTo.z > 0, coordTo.y > -y and coordTo.z > 0)
  433. isRev = false
  434. end
  435. else
  436. goTo(coordTo, coordTo.y < 0 and coordTo.z > 0, coordTo.y > -y and coordTo.z > 0)
  437.  
  438. check()
  439. end
  440. end
  441. end
  442. end
  443.  
  444. goTo(baseCoords)
  445. drop()
  446. goTo(baseCoords)
  447. turnTo(1)
  448. if currentToolSlot > 0 then
  449. toolEquip(currentToolSlot)
  450. end
  451. end
  452.  
  453. main(...)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement