SirSheepe

Untitled

Feb 26th, 2018
90
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --2GvRRG3c
  2. -- Utilities
  3.  
  4. function sleep(s)
  5. local ntime = os.time() + s
  6. repeat until os.time() > ntime
  7. end
  8.  
  9. function countItem(id, damage)
  10. damage = damage or 0
  11. local count = 0
  12. for i = 1, 16 do
  13. local data = turtle.getItemDetail(i)
  14. if data then
  15. if data.name == id and data.damage == damage then
  16. count = count + data.count
  17. end
  18. end
  19. end
  20. return count
  21. end
  22.  
  23. function findItem(id, damage)
  24. damage = damage or 0
  25. for i = 1, 16 do
  26. local data = turtle.getItemDetail(i)
  27. if data then
  28. if data.name == id and data.damage == damage then
  29. return i
  30. end
  31. end
  32. end
  33. return -1
  34. end
  35.  
  36. local fuelSource = "minecraft:coal"
  37. local donePercent = 0
  38. local c = 0
  39.  
  40. -- Turtle Wrapper
  41.  
  42. local Turtle = {}
  43.  
  44. function Turtle.refuel()
  45. local a = turtle.getFuelLevel()
  46. turtle.select(findItem(fuelSource))
  47. turtle.refuel(1)
  48. local b = turtle.getFuelLevel()
  49. local increase = b - a
  50.  
  51. for i = 1, math.floor((100-increase)/increase) do
  52. if countItem(fuelSource) > 0 then
  53. turtle.refuel(1)
  54. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;4; ;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  55. end
  56. end
  57. end
  58.  
  59. function Turtle.up()
  60. if turtle.getFuelLevel() >= 1 then
  61. local success = turtle.up()
  62. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;1; ;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  63. return success
  64. else
  65. local fuelCount = countItem(fuelSource)
  66.  
  67. if fuelCount >= 1 then
  68. Turtle.refuel()
  69. return Turtle.up()
  70. else
  71. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;3;Fuel Needed;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  72. exit()
  73. return false
  74. end
  75. end
  76. end
  77.  
  78. function Turtle.down()
  79. if turtle.getFuelLevel() >= 1 then
  80. local success = turtle.down()
  81. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;1; ;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  82. return success
  83. else
  84. local fuelCount = countItem(fuelSource)
  85.  
  86. if fuelCount > 0 then
  87. Turtle.refuel()
  88. return Turtle.down()
  89. else
  90. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;3;Fuel Needed;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  91. exit()
  92. return false
  93. end
  94. end
  95. end
  96.  
  97. function Turtle.forward()
  98. if turtle.getFuelLevel() >= 1 then
  99. local success = turtle.forward()
  100. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;1; ;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  101. return success
  102. else
  103. local fuelCount = countItem(fuelSource)
  104.  
  105. if fuelCount > 0 then
  106. Turtle.refuel()
  107. return Turtle.forward()
  108. else
  109. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;3;Fuel Needed;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  110. exit()
  111. return false
  112. end
  113. end
  114. end
  115.  
  116. function Turtle.back()
  117. if turtle.getFuelLevel() >= 1 then
  118. local success = turtle.back()
  119. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;1; ;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  120. return success
  121. else
  122. local fuelCount = countItem(fuelSource)
  123.  
  124. if fuelCount > 0 then
  125. Turtle.refuel()
  126. return Turtle.back()
  127. else
  128. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;3;Fuel Needed;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  129. exit()
  130. return false
  131. end
  132. end
  133. end
  134.  
  135. function Turtle.right()
  136. if turtle.getFuelLevel() >= 1 then
  137. local success = turtle.turnRight()
  138. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;1; ;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  139. if success and Turtle.forward() then
  140. turtle.turnLeft()
  141. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;1; ;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  142. return true
  143. elseif not success then
  144. return false
  145. else
  146. turtle.turnLeft()
  147. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;1; ;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  148. return false
  149. end
  150. else
  151. local fuelCount = countItem(fuelSource)
  152.  
  153. if fuelCount > 0 then
  154. Turtle.refuel()
  155. return Turtle.right()
  156. else
  157. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;3;Fuel Needed;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  158. exit()
  159. return false
  160. end
  161. end
  162. end
  163.  
  164. function Turtle.left()
  165. if turtle.getFuelLevel() >= 1 then
  166. local success = turtle.turnLeft()
  167. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;1; ;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  168. if success and Turtle.forward() then
  169. turtle.turnRight()
  170. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;1; ;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  171. return true
  172. elseif not success then
  173. return false
  174. else
  175. turtle.turnRight()
  176. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;1; ;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  177. return false
  178. end
  179. else
  180. local fuelCount = countItem(fuelSource)
  181.  
  182. if fuelCount > 0 then
  183. Turtle.refuel()
  184. return Turtle.left()
  185. else
  186. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;3;Fuel Needed;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  187. exit()
  188. return false
  189. end
  190. end
  191. end
  192.  
  193.  
  194. function Turtle.placeUp(id, countUp, damage)
  195. if countItem(id, damage) > 0 then
  196. local slot = findItem(id, damage)
  197. turtle.select(slot)
  198. local success = turtle.placeUp()
  199. if success and countUp then
  200. c = c + 1
  201. end
  202. return success
  203. else
  204. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;3;Not enough "..id.." ("..damage..");", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  205. exit()
  206. return false
  207. end
  208. end
  209.  
  210. function Turtle.placeDown(id, countUp, damage)
  211. if countItem(id, damage) > 0 then
  212. local slot = findItem(id, damage)
  213. turtle.select(slot)
  214. local success = turtle.placeDown()
  215. if success and countUp then
  216. c = c + 1
  217. end
  218. return success
  219. else
  220. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;3;Not enough "..id.." ("..damage..");", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  221. exit()
  222. return false
  223. end
  224. end
  225.  
  226.  
  227. function Turtle.repeatMethod(method, n)
  228. if turtle.getFuelLevel() >= 1 then
  229. for i = 1, n do
  230. local success = Turtle[method]()
  231. if not success then
  232. break
  233. end
  234. end
  235. else
  236. local fuelCount = countItem(fuelSource)
  237.  
  238. if fuelCount > 0 then
  239. Turtle.refuel()
  240. return Turtle.repeatMethod(method, n)
  241. else
  242. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;3;Fuel Needed;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  243. exit()
  244. return false
  245. end
  246. end
  247. end
  248.  
  249. -- Main thread
  250.  
  251. function plantCropsticks(n)
  252.  
  253. if countItem("AgriCraft:cropsItem") < (80 * (n or 1)) then
  254. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;3;Not enough crop sticks (%d Missing);", turtle.getFuelLevel(), turtle.getFuelLimit(), (80 * (n or 1)) - countItem("AgriCraft:cropsItem"))..tostring(donePercent))
  255. return
  256. end
  257.  
  258. local function proceed()
  259. turtle.digDown()
  260. Turtle.placeDown("AgriCraft:cropsItem", true)
  261. end
  262.  
  263. c = 0
  264.  
  265. for i = 1, (n or 1) do
  266.  
  267. if i == 1 then
  268. turtle.turnRight()
  269. end
  270. Turtle.repeatMethod("forward", 4)
  271. turtle.turnLeft()
  272.  
  273. Turtle.down()
  274.  
  275. Turtle.forward()
  276. proceed()
  277.  
  278. for i = 1, 5 do
  279.  
  280. for i = 1, 8 do
  281. Turtle.forward()
  282. proceed()
  283. donePercent = c / (81 * (n or 1))
  284. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;1; ;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  285. end
  286.  
  287. turtle.turnLeft()
  288. Turtle.forward()
  289. turtle.turnLeft()
  290.  
  291. if i ~= 5 then
  292.  
  293. proceed()
  294.  
  295. for i = 1, 8 do
  296. Turtle.forward()
  297. proceed()
  298. donePercent = c / (81 * (n or 1))
  299. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;1; ;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  300. end
  301.  
  302. turtle.turnRight()
  303. Turtle.forward()
  304. turtle.turnRight()
  305.  
  306. proceed()
  307.  
  308. end
  309.  
  310. end
  311.  
  312. Turtle.repeatMethod("forward", 9)
  313. turtle.turnLeft()
  314. Turtle.up()
  315. Turtle.repeatMethod("forward", 5)
  316.  
  317. if i == (n or 1) then
  318. turtle.turnLeft()
  319. Turtle.repeatMethod("down", 2)
  320. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;2; ;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  321. else
  322. peripheral.call("right", "transmit", 3, 1, string.format("%d;%d;5; ;", turtle.getFuelLevel(), turtle.getFuelLimit())..tostring(donePercent))
  323. Turtle.repeatMethod("forward", 12)
  324. end
  325.  
  326. end
  327.  
  328. end
  329.  
  330. local modem = peripheral.wrap("right")
  331. if not modem.isOpen(5) then
  332. modem.open(5)
  333. end
  334.  
  335. local msg
  336.  
  337. repeat
  338. msg = select(5, os.pullEvent("modem_message"))
  339. until msg == "start"
  340.  
  341. plantCropsticks(8)
RAW Paste Data