Advertisement
Fayo

OpenComputers Quarry

Sep 24th, 2014
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. if require("component").isAvailable("generator")
  2. then
  3. generator=require("component").generator
  4. end
  5. withg=(generator~=nil)
  6. slots=16
  7. slotstocheck=2
  8. robot=require("robot")
  9. coords={x=0,y=0,z=0,side=1}
  10. extcoords={x=0,y=0,z=0,side=1}
  11. sx={0,1,0,-1}
  12. sy={1,0,-1,0}
  13. falsemode=false
  14. counter=0
  15. tocount=0
  16. --1,2,3,4
  17. --tl -- coords.side = (coords.side + 3)%4
  18. --tr -- coords.side = (coords.side + 1)%4
  19. --ta -- coords.side = (coords.side + 2)%4
  20. --all -- if coords.side == 0 then coords.side =4 end
  21. function save()
  22. file = io.open("quarryrest","w")
  23. file:write(coords.x.."\n"..coords.y.."\n"..coords.z.."\n"..coords.side.."\n"..counter.."\n"..x.."\n"..y.."\n"..z.."\n")
  24. file:close()
  25. end
  26. function restore()
  27. local file = io.open("quarryrest","r")
  28. extcoords.x=file:read()+0
  29. extcoords.y=file:read()+0
  30. extcoords.z=file:read()+0
  31. extcoords.side=file:read()+0
  32. back()
  33. tocount=file:read()+0
  34. x=file:read()+0
  35. y=file:read()+0
  36. z=file:read()+0
  37. falsemode=true
  38. end
  39. function checkfalse()
  40. if counter>=tocount then falsemode=false end
  41. end
  42.  
  43. function sgo() while not robot.forward() do robot.swing()end coords.x=coords.x + sx[coords.side];coords.y = coords.y + sy[coords.side] end
  44. function sgodown() while not robot.down() do robot.swingDown()end coords.z = coords.z - 1 end
  45. function sgoup() while not robot.up() do robot.swingUp()end coords.z = coords.z + 1 end
  46. function sturnLeft() robot.turnLeft(); coords.side = (coords.side + 3)%4;if coords.side == 0 then coords.side =4 end; end
  47. function sturnRight() robot.turnRight(); coords.side = (coords.side + 1)%4; if coords.side == 0 then coords.side =4 end; end
  48.  
  49.  
  50. function go() if not falsemode then while not robot.forward() do robot.swing()end coords.x=coords.x + sx[coords.side];coords.y = coords.y + sy[coords.side] end counter=counter+1; save();checkfalse(); end
  51. function godown() if not falsemode then while not robot.down() do robot.swingDown()end coords.z = coords.z - 1 end counter=counter+1; save();checkfalse(); end
  52. function goup() if not falsemode then while not robot.up() do robot.swingUp()end coords.z = coords.z + 1 end counter=counter+1; save();checkfalse(); end
  53. function check() return robot.count(slots-(slotstocheck-1)) > 0 end
  54. function turnLeft() if not falsemode then robot.turnLeft(); coords.side = (coords.side + 3)%4;if coords.side == 0 then coords.side =4 end; end counter=counter+1; save();checkfalse(); end
  55. function turnRight() if not falsemode then robot.turnRight(); coords.side = (coords.side + 1)%4; if coords.side == 0 then coords.side =4 end; end counter=counter+1; save();checkfalse(); end
  56. function tosurf()
  57. extcoords.x=coords.x
  58. extcoords.y=coords.y
  59. extcoords.z=coords.z
  60. extcoords.side=coords.side
  61. while coords.z ~= 0 do sgoup() end
  62. while coords.side ~= 3 do sturnRight() end
  63. while coords.y ~= 0 do sgo() end
  64. sturnRight();
  65. while coords.x ~= 0 do sgo() end
  66. sturnLeft();
  67. end
  68. function back()
  69. while coords.z ~= extcoords.z do sgodown() end
  70. while coords.side~= 1 do sturnRight() end
  71. while coords.y ~= extcoords.y do sgo() end
  72. sturnRight();
  73. while coords.x ~= extcoords.x do sgo() end
  74. while coords.side ~= extcoords.side do sturnRight() end
  75. end
  76. function clearinv()
  77. alreadyhave=false
  78. for invcounter=1,slots do
  79. robot.select(invcounter)
  80. if withg then
  81. suc,error=generator.insert()
  82. if (error=="selected slot does not contain fuel" or alreadyhave) then robot.drop() else alreadyhave=true end
  83. else
  84. robot.drop()
  85. end
  86. end
  87. robot.select(1)
  88. sort()
  89. end
  90. function sort()
  91. curs=1
  92. for q=1,slots do
  93. if robot.count(q) > 0 then robot.select(q);robot.transferTo(curs);curs=curs+1 end
  94. end
  95. robot.select(1)
  96. end
  97. function prevent()
  98. if check() then
  99. tosurf()
  100. clearinv()
  101. back()
  102. end
  103. end
  104.  
  105. function digrow(n)
  106. prevent()
  107. robot.swingUp()
  108. prevent()
  109. robot.swingDown()
  110. for k=1,n do
  111. prevent()
  112. go()
  113. prevent()
  114. robot.swingUp()
  115. prevent()
  116. robot.swingDown()
  117. end
  118. end
  119. function digdim(x,y)
  120. godown()
  121. godown()
  122. for n=1,y do
  123. if tl then turnLeft() else turnRight() end
  124. digrow(ex)
  125. if tl then turnRight() else turnLeft() end
  126. tl = not tl
  127. if n==y then print("ok") else go() end
  128. end
  129. turnRight();
  130. turnRight();
  131. tl= not tl
  132. end
  133. x,y,z=...
  134. if x=="-restore" then
  135. restore()
  136. end
  137. tl=false
  138. x=x+0
  139. y=y+0
  140. z=z+0
  141. ex=x-1
  142. fullz=(z-z%3)/3
  143. print("Fullz = " .. fullz)
  144. for i=1,fullz do
  145. digdim(x,y)
  146. godown()
  147. end
  148. if fullz*3==z then elseif fullz*3+1==z then goup();goup();digdim(tl,x,y) else goup();digdim(tl,x,y) end
  149. tosurf()
  150. clearinv()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement