Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. --- Invariants:
  2. ---- Keep seeds in first slot
  3. --- Jobs:
  4. ---- 1: Plant
  5. ---- 2: Harvest and plant
  6.  
  7. local drone = component.proxy(component.list("drone")())
  8. local sides = require('sides')
  9.  
  10. local FARM_DIRECTION_NS = 0
  11. local FARM_DIRECTION_EW = -1
  12.  
  13. local function sleep(timeout)
  14. checkArg(1, timeout, "number", "nil")
  15. local deadline = computer.uptime() + (timeout or 0)
  16. repeat
  17. computer.pullSignal(deadline - computer.uptime())
  18. until computer.uptime() >= deadline
  19. end
  20.  
  21. local function checkE()
  22. local en = computer.energy()
  23. return en
  24. end
  25.  
  26. local function drop()
  27. drone.drop(2)
  28. end
  29.  
  30. local function dropAll()
  31. for i=4,2,-1
  32. do
  33. drone.select(i)
  34. drop()
  35. end
  36. drone.select(1)
  37. end
  38.  
  39. local function move(x,y,z)
  40. drone.move(x,y,z)
  41. travelx = travelx + x
  42. travely = travely + y
  43. travelz = travelz + z
  44. end
  45.  
  46. local function beep()
  47. computer.beep(261, 0.6)
  48. computer.beep(329, 0.5)
  49. computer.beep(392, 0.3)
  50. computer.beep(523, 0.7)
  51. end
  52.  
  53. local function swing()
  54. for i=5,0,-1 do
  55. drone.swing(i)
  56. end
  57. sleep(0.2)
  58. end
  59.  
  60. local function swingHor()
  61. for i=1,5,1 do
  62. drone.swing(i)
  63. end
  64. sleep(0.2)
  65. end
  66.  
  67. local function suck(x)
  68. drone.suck(x)
  69. end
  70.  
  71. local function suckAll()
  72. for i=0,5,1 do
  73. suck(i)
  74. end
  75. end
  76.  
  77. local function layer(n)
  78. for i=n,1,-1 do
  79.  
  80. for i=3,1,-1 do
  81. suckAll()
  82. swingHor()
  83. move(-1,0,0)
  84. end
  85.  
  86. for i=3,1,-1 do
  87. suckAll()
  88. swingHor()
  89. move(0,0,-1)
  90. end
  91.  
  92. for i=3,1,-1 do
  93. suckAll()
  94. swingHor()
  95. move(1,0,0)
  96. end
  97.  
  98. for i=3,1,-1 do
  99. suckAll()
  100. swing()
  101. move(0,0,1)
  102. end
  103.  
  104. swing()
  105. sleep(1)
  106. move(0,-1,0)
  107. sleep(0.3)
  108. suckAll()
  109. end
  110.  
  111. for i=3,1,-1 do
  112. suckAll()
  113. swingHor()
  114. move(-1,0,0)
  115. end
  116.  
  117. for i=3,1,-1 do
  118. suckAll()
  119. swingHor()
  120. move(0,0,-1)
  121. end
  122.  
  123. for i=3,1,-1 do
  124. suckAll()
  125. swingHor()
  126. move(1,0,0)
  127. end
  128.  
  129. for i=3,1,-1 do
  130. suckAll()
  131. swingHor()
  132. move(0,0,1)
  133. end
  134. end
  135.  
  136. local function placeD()
  137. drone.place(0)
  138. end
  139.  
  140. local function plant()
  141. drone.setStatusText("PLANTING")
  142. drone.select(1)
  143. move(-8,1,0)
  144. sleep(2)
  145. placeD()
  146.  
  147. move(-1,0,0)
  148. sleep(0.3)
  149. placeD()
  150.  
  151. move(0,0,-1)
  152. sleep(0.3)
  153. placeD()
  154.  
  155. move(1,0,0)
  156. sleep(0.3)
  157. placeD()
  158. returnHome()
  159. end
  160.  
  161. local function harvest()
  162. drone.setStatusText("CUTTING")
  163. move(0,30,0)
  164. sleep(3)
  165. move(-7,0,1)
  166. sleep(2)
  167.  
  168. layer(30)
  169.  
  170. swingHor()
  171.  
  172. move(0,0,-1)
  173. for i=1,4,1 do
  174. move(1,0,0)
  175. swingHor()
  176. suckAll()
  177. end
  178. returnHome()
  179. dropAll()
  180. sleep(2)
  181. end
  182.  
  183. local function resetP()
  184. travelx, travely, travelz = 0,0,0
  185. end
  186.  
  187. function returnHome()
  188. travelx = travelx * -1
  189. travely = travely * -1
  190. travelz = travelz * -1
  191. sleep(1)
  192. move(travelx,travely,travelz)
  193. sleep(2)
  194. resetP()
  195. end
  196.  
  197. local function getToPosition()
  198. for i=1,20,1 do
  199. local somethingInFront = drone.detect(sides.front)
  200. if somethingInFront then
  201. drone.setStatusText("Found the fence!")
  202. -- Move on top of the fence
  203. move(0, 0, 1)
  204. move(FARM_DIRECTION_EW, FARM_DIRECTION_NS, 1)
  205.  
  206. -- Move to the left corner of the farm
  207. for i=1,20,1 do
  208. local somethingBelow = drone.detect(sides.bottom)
  209. -- Then we went past the fence, go back in
  210. if not somethingBelow then
  211. drone.setStatusText("Hit the end")
  212. sleep(0.5)
  213. if FARM_DIRECTION_EW != 0 then
  214. move(0, -2, 0)
  215. else
  216. move(-2, 0, 0)
  217. end
  218. move(FARM_DIRECTION_EW, FARM_DIRECTION_NS, 1)
  219. sleep(0.5)
  220. end
  221.  
  222. -- Go left until we find the end
  223. sleep(0.5)
  224. if FARM_DIRECTION_EW != 0 then
  225. move(0, 1, 0)
  226. else
  227. move(1, 0, 0)
  228. end
  229. sleep(0.5)
  230. end
  231. end
  232.  
  233. sleep(0.5)
  234. move(FARM_DIRECTION_EW, FARM_DIRECTION_NS, 0)
  235. sleep(0.5)
  236. end
  237. returnHome()
  238. end
  239.  
  240. local function checkPlants()
  241. resetP()
  242. job = 0
  243. drone.setStatusText("CHECKING")
  244.  
  245. --
  246. -- drone.select(1)
  247. -- local result = drone.compare(sides.bottom)
  248. --
  249. for i=1,20,1 do
  250. local somethingInFront = drone.detect(4)
  251. if result then
  252. job = 2
  253. break
  254. end
  255. sleep(0.5)
  256. move(FARM_DIRECTION_EW, FARM_DIRECTION_NS, 0)
  257. sleep(0.5)
  258. end
  259.  
  260. local resultA = drone.detect(4)
  261. move(0,1,0)
  262. sleep(0.5)
  263. local resultB = drone.detect(4)
  264.  
  265. if not resultA then
  266. job = 1
  267. end
  268.  
  269. if resultA == true and resultB == true then
  270. job = 2
  271. end
  272. returnHome()
  273. sleep(1)
  274. end
  275.  
  276. --start
  277. computer.beep(80, 2)
  278. sleep(0.5)
  279. resetP()
  280. job = 0
  281. beep()
  282. drone.setStatusText("ROM\nOK.v4.14.")
  283. sleep(1)
  284.  
  285. -- Main loop
  286. while true do
  287. while true do
  288. local energy = checkE()
  289. if energy < 1000 then
  290. drone.setLightColor(0xFF9969)
  291. drone.setStatusText("LOW\nENERGY")
  292. computer.beep(878, 1)
  293. sleep(22)
  294. end
  295. if energy >= 1000 then
  296. --pass
  297. break
  298. end
  299. end
  300. drone.setLightColor(0xD0DE58)
  301. sleep(1)
  302.  
  303. checkPlants()
  304. drone.setStatusText(tostring(job))
  305. sleep(0.3)
  306. if job == 1 then
  307. plant()
  308. end
  309. if job == 2 then
  310. harvest()
  311. plant()
  312. end
  313. drone.setStatusText("zZzZzZz")
  314. sleep(30)
  315. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement