Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.33 KB | None | 0 0
  1.  
  2. SLOT_CROPS = 1
  3. SLOT_ANALYSER = 2
  4. SLOT_SEEDS = 3
  5.  
  6. ACTIVATOR_PULSE_DURATION = 0.85
  7.  
  8. -- Interval at which the turtle checks if a plant has matured (in seconds)
  9. MATURE_SLEEP_DURATION = 5
  10. SPREAD_SLEEP_DURATION = 5
  11. ANALYSE_SLEEP_DURATION = 1 -- interval for which to check if analyse process is finsished
  12.  
  13. -- If "true", excess non-10/10/10 seeds will get trashed
  14. TRASH_EXCESS_SEEDS = true
  15.  
  16. -- the seed analyser uses absolute facing directions, adjust these depending on your setup
  17. ANALYSER_LOWER_FACE = "WEST"
  18. ANALYSER_UPPER_FACE = "EAST"
  19.  
  20. STATE_NORMAL = "normal"
  21. STATE_ALTERNATE = "alternate"
  22.  
  23. CROP_LOWER_RIGHT = { x = 2, y = 1 }
  24. CROP_LOWER_LEFT = { x = 1, y = 1 }
  25. CROP_UPPER_RIGHT = { x = 2, y = 2 }
  26. CROP_UPPER_LEFT = { x = 1, y = 2 }
  27.  
  28. ANALYSER_LOWER_RIGHT = { x = 2, y = 0 }
  29. ANALYSER_LOWER_LEFT = { x = 1, y = 0 }
  30. ANALYSER_UPPER_RIGHT = { x = 2, y = 3 }
  31. ANALYSER_UPPER_LEFT = { x = 1, y = 3 }
  32.  
  33. ACTIVATOR_LOWER_RIGHT = { x = 3, y = 1 }
  34. ACTIVATOR_LOWER_LEFT = { x = 0, y = 1 }
  35. ACTIVATOR_UPPER_RIGHT = { x = 3, y = 2 }
  36. ACTIVATOR_UPPER_LEFT = { x = 0, y = 2 }
  37.  
  38. ANALYSER_MAIN = { x = 2, y = -1 }
  39. CHEST = { x = 1, y = -1 }
  40. TRASH = { x = 3, y = -1 }
  41.  
  42. LOWER_RIGHT = {
  43.   crop = CROP_LOWER_RIGHT,
  44.   analyser = ANALYSER_LOWER_RIGHT,
  45.   analyserFace = ANALYSER_LOWER_FACE,
  46.   activator = ACTIVATOR_LOWER_RIGHT
  47. }
  48.  
  49. LOWER_LEFT = {
  50.   crop = CROP_LOWER_LEFT,
  51.   analyser = ANALYSER_LOWER_LEFT,
  52.   analyserFace = ANALYSER_LOWER_FACE,
  53.   activator = ACTIVATOR_LOWER_LEFT
  54. }
  55.  
  56. UPPER_RIGHT = {
  57.   crop = CROP_UPPER_RIGHT,
  58.   analyser = ANALYSER_UPPER_RIGHT,
  59.   analyserFace = ANALYSER_UPPER_FACE,
  60.   activator = ACTIVATOR_UPPER_RIGHT
  61. }
  62.  
  63. UPPER_LEFT = {
  64.   crop = CROP_UPPER_LEFT,
  65.   analyser = ANALYSER_UPPER_LEFT,
  66.   analyserFace = ANALYSER_UPPER_FACE,
  67.   activator = ACTIVATOR_UPPER_LEFT
  68. }
  69.  
  70. POS_Y = 1
  71. POS_X = 2
  72. NEG_Y = 3
  73. NEG_X = 4
  74.  
  75. currentFace = POS_Y
  76. currentPos = { x = 1, y = -1 } -- same as the chest
  77. currentState = STATE_NORMAL
  78.  
  79. STATES = {
  80.   normal = { LOWER_RIGHT, UPPER_LEFT },
  81.   alternate = { LOWER_LEFT, UPPER_RIGHT }
  82. }
  83.  
  84. --
  85. -- State management
  86. --
  87. function flipCurrentState()
  88.   if currentState == STATE_NORMAL then
  89.     currentState = STATE_ALTERNATE
  90.   else
  91.     currentState = STATE_NORMAL
  92.   end
  93. end
  94.  
  95. function currentField1()
  96.   return STATES[currentState][1]
  97. end
  98.  
  99. function currentField2()
  100.   return STATES[currentState][2]
  101. end
  102.  
  103. function oppositeState()
  104.   return currentState == STATE_NORMAL and STATE_ALTERNATE or STATE_NORMAL
  105. end
  106.  
  107. function oppositeField1()
  108.   return STATES[oppositeState()][1]
  109. end
  110.  
  111. function oppositeField2()
  112.   return STATES[oppositeState()][2]
  113. end
  114.  
  115. --
  116. -- Movement and orientation
  117. --
  118. function turnLeft()
  119.   turtle.turnLeft()
  120.   currentFace = currentFace - 1
  121.   if currentFace < POS_Y then
  122.     currentFace = NEG_X
  123.   end
  124. end
  125.  
  126. function turnRight()
  127.   turtle.turnRight()
  128.   currentFace = currentFace + 1
  129.   if currentFace > NEG_X then
  130.     currentFace = POS_Y
  131.   end
  132. end
  133.  
  134. function turnToFace(face)
  135.   local diff = currentFace - face
  136.   if diff == 0 then
  137.     return
  138.   end
  139.  
  140.   if math.abs(diff) == 2 then
  141.     turnLeft()
  142.     turnLeft()
  143.   elseif diff == 1 or diff == -3 then
  144.     turnLeft()
  145.   elseif diff == -1 or diff == 3 then
  146.     turnRight()
  147.   else
  148.     error("Invalid difference: " + diff)
  149.   end
  150. end
  151.  
  152. function moveForward(times)
  153.   while times > 0 do
  154.     turtle.forward()
  155.     adjustCurrentPos()
  156.     times = times - 1
  157.   end
  158. end
  159.  
  160. function adjustCurrentPos()
  161.   if currentFace == POS_Y then
  162.     currentPos.y = currentPos.y + 1
  163.   elseif currentFace == POS_X then
  164.     currentPos.x = currentPos.x + 1
  165.   elseif currentFace == NEG_Y then
  166.     currentPos.y = currentPos.y - 1
  167.   elseif currentFace == NEG_X then
  168.     currentPos.x = currentPos.x - 1
  169.   else
  170.     error("Invald face: " + currentFace)
  171.   end
  172. end
  173.  
  174. function moveTo(pos)
  175.   local diffx = currentPos.x - pos.x
  176.   local diffy = currentPos.y - pos.y
  177.   local facex = diffx > 0 and NEG_X or POS_X
  178.   local facey = diffy > 0 and NEG_Y or POS_Y
  179.   local absx = math.abs(diffx)
  180.   local absy = math.abs(diffy)
  181.  
  182.   if currentFace == facex then
  183.     moveForward(absx)
  184.     if absy > 0 then
  185.       turnToFace(facey)
  186.       moveForward(absy)
  187.     end
  188.   elseif currentFace == facey then
  189.     moveForward(absy)
  190.     if absx > 0 then
  191.       turnToFace(facex)
  192.       moveForward(absx)
  193.     end
  194.   else
  195.     if absx > 0 then
  196.       turnToFace(facex)
  197.       moveForward(absx)
  198.     end
  199.     if absy > 0 then
  200.       turnToFace(facey)
  201.       moveForward(absy)
  202.     end
  203.   end
  204. end
  205.  
  206. --
  207. -- Planting
  208. --
  209.  
  210. function placeCropAndSeed(field)
  211.   moveTo(field.crop)
  212.   placeSingleCrop()
  213.   moveTo(field.activator)
  214.   placeSeed()
  215. end
  216.  
  217. function placeCropAndCrossCrop(field)
  218.   moveTo(field.crop)
  219.   placeSingleCrop()
  220.   moveTo(field.activator)
  221.   placeCrossCrop()
  222. end
  223.  
  224. function placeSingleCrop()
  225.   turtle.select(SLOT_CROPS)
  226.   turtle.placeDown()
  227. end
  228.  
  229. function placeViaActivator(slot)
  230.   turtle.select(slot)
  231.   turtle.dropDown(1)
  232.  
  233.   rs.setOutput("bottom", true)
  234.   os.sleep(ACTIVATOR_PULSE_DURATION)
  235.   rs.setOutput("bottom", false)
  236. end
  237.  
  238. function placeCrossCrop()
  239.   placeViaActivator(SLOT_CROPS)
  240. end
  241.  
  242. function placeSeed()
  243.   placeViaActivator(SLOT_SEEDS)
  244. end
  245.  
  246. function placeAnalyser()
  247.   turtle.select(SLOT_ANALYSER)
  248.   turtle.placeDown()
  249.  
  250.   return peripheral.wrap("bottom")
  251. end
  252.  
  253. function waitForPlantToMature(face)
  254.   analyser = placeAnalyser()
  255.   while not analyser.isMature(face) do
  256.     sleep(MATURE_SLEEP_DURATION)
  257.   end
  258.  
  259.   turtle.digDown()
  260. end
  261.  
  262. function waitForPlantToSpread(face)
  263.   analyser = placeAnalyser()
  264.   while not analyser.hasPlant(face) do
  265.     sleep(SPREAD_SLEEP_DURATION)
  266.   end
  267.  
  268.   turtle.digDown()
  269. end
  270.  
  271. function waitForMaturePlant(field)
  272.   moveTo(field.analyser)
  273.   waitForPlantToMature(field.analyserFace)
  274. end
  275.  
  276. function waitForSpread(field)
  277.   moveTo(field.analyser)
  278.   waitForPlantToSpread(field.analyserFace)
  279. end
  280.  
  281. function harvest(field)
  282.   moveTo(field.crop)
  283.   turtle.select(SLOT_CROPS)
  284.   turtle.digDown()
  285. end
  286.  
  287. -- true if item at slot i should get dumped
  288. -- returns true for everything that doesnt contain the word "seed" in the technical name
  289. function dumpItem(i)
  290.   local item = turtle.getItemDetail(i)
  291.   if item == nil then
  292.     return false
  293.   end
  294.  
  295.   return string.find(string.lower(item.name), "seed") == nil
  296. end
  297.  
  298. -- dumps all items that are not seeds into chest below, also returns slot numbers for the 2 collected seeds
  299. function dumpHarvest()
  300.   moveTo(CHEST)
  301.  
  302.   local s1 = nil
  303.   local s2 = nil
  304.  
  305.   for i=3,16 do
  306.     if dumpItem(i) then
  307.       turtle.select(i)
  308.       turtle.dropDown()
  309.     elseif turtle.getItemDetail(i) ~= nil then
  310.       if s1 == nil then
  311.         s1 = i
  312.       else
  313.         s2 = i
  314.       end
  315.     end
  316.   end
  317.   return s1, s2
  318. end
  319.  
  320. function analyseSlotGetStats(slot)
  321.   analyser = placeAnalyser()
  322.   turtle.select(slot)
  323.   turtle.dropDown()
  324.   analyser.analyze()
  325.  
  326.   while not analyser.isAnalyzed() do
  327.     sleep(ANALYSER_SLEEP_DURATION)
  328.   end
  329.  
  330.   gr, ga, str = analyser.getSpecimenStats()
  331.  
  332.   turtle.suckDown()
  333.   turtle.select(SLOT_ANALYSER)
  334.   turtle.digDown()
  335.  
  336.   return ((gr + ga + str) == 30)
  337. end
  338.  
  339.  
  340. function analyseSlotAndStore(slot)
  341.   if slot == nil then
  342.     return false
  343.   end
  344.  
  345.   moveTo(ANALYSER_MAIN)
  346.   local has30 = analyseSlotGetStats(slot)
  347.  
  348.   if has30 then
  349.     moveTo(CHEST)
  350.   else
  351.     moveTo(TRASH)
  352.   end
  353.  
  354.   turtle.select(slot)
  355.   turtle.dropDown()
  356.   return has30
  357. end
  358.  
  359. function analyseHarvest(s1, s2)
  360.   local s1Has30 = analyseSlotAndStore(s1)
  361.   local s2Has30 = analyseSlotAndStore(s2)
  362.  
  363.   return s1Has30 or s2Has30
  364. end
  365.  
  366. function main()
  367.   placeCropAndSeed(currentField1())
  368.   placeCropAndSeed(currentField2())
  369.  
  370.   local finishedBreeding = false
  371.  
  372.   while not finishedBreeding do
  373.     waitForMaturePlant(currentField1())
  374.     waitForMaturePlant(currentField2())
  375.  
  376.     placeCropAndCrossCrop(oppositeField1())
  377.     placeCropAndCrossCrop(oppositeField2())
  378.  
  379.     waitForSpread(oppositeField1())
  380.     waitForSpread(oppositeField2())
  381.  
  382.     harvest(currentField1())
  383.     harvest(currentField2())
  384.  
  385.     s1, s2 = dumpHarvest()
  386.     finishedBreeding = analyseHarvest(s1, s2)
  387.  
  388.     flipCurrentState()
  389.   end
  390.  
  391.   -- harvest remaining 2 and store them
  392.   harvest(currentField1())
  393.   harvest(currentField2())
  394.   s1, s2 = dumpHarvest()
  395.   analyseHarvest(s1, s2)
  396.  
  397.   -- reset position
  398.   moveTo(CHEST)
  399.   turnToFace(POS_Y)
  400. end
  401.  
  402. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement