Guest User

roomDigger.lua

a guest
Aug 10th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. local component = require("component")
  2. local sides = require("sides")
  3. local robot = require("robot")
  4. local computer = require("computer")
  5.  
  6. local turnRight = true
  7. local num1, num2 = 0, 0
  8.  
  9. print("Set the X")
  10. local roomX = tonumber(io.read())
  11. print("Set the Y")
  12. local roomY = tonumber(io.read())
  13. print("Set the Z")
  14. local roomZ = tonumber(io.read())
  15.  
  16. function digUp(roomZ)
  17. local var1, var2
  18. local i = 0
  19. while i ~= roomZ do
  20. os.sleep(0.5)
  21. var1, var2 = robot.detect()
  22. if var1 == true and var2 ~= "liquid" then
  23. repeat
  24. robot.swing()
  25. os.sleep(0.5)
  26. var1, var2 = robot.detect()
  27. until var2 == "liquid" or var1 == false
  28. end
  29. i = i + 1
  30. if i ~= roomZ then
  31. robot.up()
  32. end
  33. end
  34. while i ~= 0 do
  35. i = i - 1
  36. if i ~= 0 then
  37. robot.down()
  38. end
  39. end
  40. var1, var2 = robot.detect()
  41. if var1 == true and var2 ~= "liquid" then
  42. repeat
  43. robot.swing()
  44. os.sleep(0.5)
  45. var1, var2 = robot.detect()
  46. until var1 == false or var2 == "liquid"
  47. end
  48. end
  49. function digNL()
  50. if turnRight == true then
  51. robot.turnRight()
  52. digUp(roomZ)
  53. robot.forward()
  54. robot.turnRight()
  55. turnRight = false
  56. else
  57. robot.turnLeft()
  58. digUp(roomZ)
  59. robot.forward()
  60. robot.turnLeft()
  61. turnRight = true
  62. end
  63. end
  64.  
  65. function turn()
  66. if turnRight == true then
  67. robot.turnLeft()
  68. else
  69. robot.turnRight()
  70. end
  71. end
  72.  
  73. function returnHome()
  74. turn()
  75. local var3, var4 = robot.detect()
  76. while var3 == false or var4 == "liquid" do
  77. robot.forward()
  78. num1 = num1 + 1
  79. os.sleep(0.5)
  80. var3, var4 = robot.detect()
  81. end
  82. robot.turnLeft()
  83. var3, var4 = robot.detect()
  84. while var3 == false or var4 == "liquid" do
  85. robot.forward()
  86. num2 = num2 + 1
  87. os.sleep(0.5)
  88. var3, var4 = robot.detect()
  89. end
  90. end
  91.  
  92. function returnWork()
  93. robot.turnAround()
  94. for i = 0, num2 - 1, 1 do
  95. robot.forward()
  96. end
  97. robot.turnRight()
  98. for i = 0, num1 - 1, 1 do
  99. robot.forward()
  100. end
  101. turn()
  102. end
  103. function checkStat()
  104. if robot.count(16) ~= 0 then
  105. matinance()
  106. end
  107. if computer.energy() <= 5000 then
  108. matinance()
  109. end
  110. end
  111.  
  112. function storageLeft()
  113. robot.turnLeft()
  114. for i = 1, 3 , 1 do
  115. robot.forward()
  116. end
  117. robot.turnRight()
  118.  
  119. end
  120.  
  121. function fullInvDump()
  122. for i = 1, 16, 1 do
  123. robot.select(i)
  124. robot.drop()
  125. end
  126. local sum = 0
  127. for i = 1, 16, 1 do
  128. sum = sum + robot.count(i)
  129. end
  130. if sum ~= 0 then
  131. return false
  132. else
  133. robot.select(1)
  134. return true
  135. end
  136. end
  137. function storageDump()
  138. robot.turnLeft()
  139. for i = 1, 4 , 1 do
  140. robot.forward()
  141. end
  142. robot.turnRight()
  143. if fullInvDump() == false then
  144. robot.up()
  145. if fullInvDump() == false then
  146. storageLeft()
  147. if fullInvDump() == false then
  148. robot.down()
  149. end
  150. end
  151. end
  152. robot.turnRight()
  153. robot.down()
  154. for i = 1, 16, 1 do
  155. robot.forward()
  156. end
  157. robot.turnLeft()
  158. end
  159.  
  160. function matinance()
  161. returnHome()
  162. repeat
  163. if computer.energy() <= 10000 then
  164. repeat
  165. os.sleep(15)
  166. until computer.energy() >= computer.maxEnergy() - 500
  167. elseif robot.count(16) ~= 0 then
  168. storageDump()
  169. end
  170. until computer.energy() > computer.maxEnergy() - 1000 and robot.count(16) == 0
  171. returnWork()
  172. end
  173.  
  174. for i = 0, roomY - 1, 1 do
  175. for j = 0, roomX -1, 1 do
  176. digUp(roomZ)
  177. robot.forward()
  178. checkStat()
  179. end
  180. digNL()
  181. end
Advertisement
Add Comment
Please, Sign In to add comment