Advertisement
Te-ki

CCSeedBreeder

Oct 7th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.32 KB | None | 0 0
  1. --CCSeedBreeder by Teki
  2.  
  3. local tArgs = { ... }
  4. local monitor = term.current()
  5. local sizeX,sizeY = monitor.getSize()
  6.  
  7. monitor.clear()
  8. monitor.setCursorPos(1,1)
  9.  
  10. local fuelChest = {1,3}
  11. local cropSticksChest = {1,2}
  12. local boneMealChest = {2,1}
  13. local seedsChest = {1,4}
  14. local trashChest = {2,4}
  15. local productionChest = {2,5}
  16.  
  17. if #tArgs == 1 then
  18.     if tArgs[1] == "del" then
  19.         fs.delete(filePath)
  20.         return 0
  21.     elseif tArgs[1] == "help" then
  22.     end
  23. else
  24.     monitor.setCursorPos(1,1)
  25.     monitor.write("A : Autonomous activator facing soil")
  26.     monitor.setCursorPos(1,2)
  27.     monitor.write("S : Soil (dirt must be tilled!)")
  28.     monitor.setCursorPos(1,3)
  29.     monitor.write("C : Computer controlled seed analyzer")
  30.     monitor.setCursorPos(1,4)
  31.     monitor.write("f : Fuel chest")
  32.     monitor.setCursorPos(1,5)
  33.     monitor.write("c : Crop sticks chest")
  34.     monitor.setCursorPos(1,6)
  35.     monitor.write("b : Bone meal chest")
  36.     monitor.setCursorPos(1,7)
  37.     monitor.write("s : Seeds chest")
  38.     monitor.setCursorPos(1,8)
  39.     monitor.write("t : Trash chest")
  40.     monitor.setCursorPos(1,9)
  41.     monitor.write("p : Production chest")
  42.    
  43.     monitor.setCursorPos(30,5)
  44.     monitor.write("  A  ")
  45.     monitor.setCursorPos(30,6)
  46.     monitor.write("ASSSA")
  47.     monitor.setCursorPos(30,7)
  48.     -- monitor.write("b Ctp")
  49.     monitor.write("  C ")
  50.     monitor.setCursorPos(30,8)
  51.     -- monitor.write(" cfs ")
  52.     monitor.write("     ")
  53.     monitor.setCursorPos(29+fuelChest[2],9-fuelChest[1])
  54.     monitor.write("f")
  55.     monitor.setCursorPos(29+cropSticksChest[2],9-cropSticksChest[1])
  56.     monitor.write("c")
  57.     monitor.setCursorPos(29+boneMealChest[2],9-boneMealChest[1])
  58.     monitor.write("b")
  59.     monitor.setCursorPos(29+seedsChest[2],9-seedsChest[1])
  60.     monitor.write("s")
  61.     monitor.setCursorPos(29+trashChest[2],9-trashChest[1])
  62.     monitor.write("t")
  63.     monitor.setCursorPos(29+productionChest[2],9-productionChest[1])
  64.     monitor.write("p")
  65.    
  66.     monitor.setCursorPos(1,10)
  67.     monitor.write("Put the turtle on the fuel chest fuel")
  68.     monitor.setCursorPos(1,11)
  69.     monitor.write("seeds, crops sticks and bone meal in")
  70.     monitor.setCursorPos(1,12)
  71.     monitor.write("appropriate chests then start.")
  72.     monitor.setCursorPos(1,13)
  73.     monitor.write("Press Q to quit, S to start.")
  74.     while true do
  75.         local event, side, xPos, yPos = os.pullEvent()
  76.         if event == "char" then
  77.             if side == "Q" or side == "q" then
  78.                 return 0
  79.             elseif side == "S" or side == "s" then
  80.                 break
  81.             end
  82.         end
  83.     end
  84. end
  85.  
  86. local savedValues
  87. local savedFile
  88. local filePath = "seedbreeder.save"
  89.  
  90. local curX = fuelChest[1]
  91. local curY = fuelChest[2]
  92. local dir = 0
  93. local direction = nil
  94. local step = 0
  95.  
  96. local seed1 = {0,0,0}
  97. local seed2 = {0,0,0}
  98. local seed3 = {0,0,0}
  99.  
  100. local function roundTo(num, n)
  101.   local mult = 10^(n or 0)
  102.   return math.floor(num * mult + 0.5) / mult
  103. end
  104.  
  105. function pmsg(msg)
  106.     monitor.setCursorPos(1,13)
  107.     monitor.clearLine()
  108.     monitor.write(msg)
  109. end
  110.  
  111. function Save()
  112.     savedFile = fs.open(filePath, "w")
  113.     savedValues = {
  114.         [1] = curX,
  115.         [2] = curY,
  116.         [3] = dir,
  117.         [4] = step,
  118.         [5] = seed1,
  119.         [6] = seed2,
  120.         [7] = seed3
  121.     }
  122.     savedFile.write(textutils.serialize(savedValues))
  123.     savedFile.flush()
  124.     savedFile.close()
  125. end
  126.  
  127. function tForward(toX, toY)
  128.     if dir == 0 then
  129.         if curX < toX then
  130.             if turtle.forward() then
  131.                 curX = curX + 1
  132.                 Save()
  133.             elseif not turtle.detect() then
  134.                 turtle.attack()
  135.             end
  136.         else
  137.             if curY < toY then
  138.                 turtle.turnRight()
  139.                 dir = 1
  140.                 Save()
  141.             else
  142.                 turtle.turnLeft()
  143.                 dir = 3
  144.                 Save()
  145.             end
  146.         end
  147.     elseif dir == 1 then
  148.         if curY < toY then
  149.             if turtle.forward() then
  150.                 curY = curY + 1
  151.                 Save()
  152.             elseif not turtle.detect() then
  153.                 turtle.attack()
  154.             end
  155.         else
  156.             if curX < toX then
  157.                 turtle.turnLeft()
  158.                 dir = 0
  159.                 Save()
  160.             else
  161.                 turtle.turnRight()
  162.                 dir = 2
  163.                 Save()
  164.             end
  165.         end
  166.     elseif dir == 2 then
  167.         if curX > toX then
  168.             if turtle.forward() then
  169.                 curX = curX - 1
  170.                 Save()
  171.             elseif not turtle.detect() then
  172.                 turtle.attack()
  173.             end
  174.         else
  175.             if curY < toY then
  176.                 turtle.turnLeft()
  177.                 dir = 1
  178.                 Save()
  179.             else
  180.                 turtle.turnRight()
  181.                 dir = 3
  182.                 Save()
  183.             end
  184.         end
  185.     elseif dir == 3 then
  186.         if curY > toY then
  187.             if turtle.forward() then
  188.                 curY = curY - 1
  189.                 Save()
  190.             elseif not turtle.detect() then
  191.                 turtle.attack()
  192.             end
  193.         else
  194.             if curX < toX then
  195.                 turtle.turnRight()
  196.                 dir = 0
  197.                 Save()
  198.             else
  199.                 turtle.turnLeft()
  200.                 dir = 2
  201.                 Save()
  202.             end
  203.         end
  204.     end
  205. end
  206.  
  207. function goTo(toX, toY)
  208.     toX = tonumber(toX)
  209.     toY = tonumber(toY)
  210.     while curX ~= toX or curY ~= toY do
  211.         tForward(toX, toY)
  212.     end
  213. end
  214.  
  215. function activateAutonomous()
  216.     redstone.setOutput("bottom", true)
  217.     sleep(1)
  218.     redstone.setOutput("bottom", false)
  219. end
  220.  
  221. function findDirection()
  222.     local ret, val = pcall(function() peripheral.call("bottom", "hasPlant", "NORTH") end)
  223.     if ret then
  224.         direction = "NORTH"
  225.     end
  226.     ret, val = pcall(function() peripheral.call("bottom", "hasPlant", "EAST") end)
  227.     if ret then
  228.         direction = "EAST"
  229.     end
  230.     ret, val = pcall(function() peripheral.call("bottom", "hasPlant", "SOUTH") end)
  231.     if ret then
  232.         direction = "SOUTH"
  233.     end
  234.     ret, val = pcall(function() peripheral.call("bottom", "hasPlant", "WEST") end)
  235.     if ret then
  236.         direction = "WEST"
  237.     end
  238. end
  239.  
  240. function stepLoop()
  241.     pmsg("Fuel check ...")
  242.     if turtle.getFuelLevel() <= 30 then
  243.         goTo(fuelChest[1],fuelChest[2])
  244.         while turtle.getFuelLevel() <= 26 do
  245.             turtle.select(3)
  246.             while turtle.suckDown(1) == false or turtle.getFuelLevel() <= 26 do
  247.                 pmsg("Waiting for fuel ...")
  248.                 sleep(1)
  249.             end
  250.             turtle.refuel(1)
  251.         end
  252.     end
  253.     pmsg("Crop sticks check ...")
  254.     turtle.select(1)
  255.     if turtle.getItemCount() < 4 then
  256.         goTo(cropSticksChest[1],cropSticksChest[2])
  257.         while turtle.getItemCount() < 5 do
  258.             turtle.suckDown(64 - turtle.getItemCount())
  259.             pmsg("Waiting for crop sticks ...")
  260.             sleep(1)
  261.         end
  262.     end
  263.     pmsg("Bone Meal Check ...")
  264.     turtle.select(2)
  265.     if turtle.getItemCount() < 4 then
  266.         goTo(boneMealChest[1],boneMealChest[2])
  267.         while turtle.getItemCount() < 3 do
  268.             turtle.suckDown(64 - turtle.getItemCount())
  269.             pmsg("Waiting for bone meal ...")
  270.             sleep(1)
  271.         end
  272.     end
  273. end
  274.  
  275. function stepStart()
  276.     step = 0
  277.     pmsg("Step start ...")
  278.     goTo(seedsChest[1],seedsChest[2])
  279.     turtle.select(3)
  280.     while turtle.suckDown(1) == false do
  281.         pmsg("Waiting for a seed ...")
  282.         sleep(1)
  283.     end
  284.     goTo(2,3)
  285.     turtle.select(3)
  286.     turtle.dropDown(1)
  287.     peripheral.call("bottom", "analyze")
  288.     while peripheral.call("bottom", "isAnalyzed") == false do
  289.         sleep(0.05)
  290.     end
  291.     seed1[1],seed1[2],seed1[3] = peripheral.call("bottom", "getSpecimenStats")
  292.     turtle.suckDown(1)
  293.     stepA()
  294.     goTo(seedsChest[1],seedsChest[2])
  295.     turtle.select(3)
  296.     while turtle.suckDown(1) == false do
  297.         pmsg("Waiting for a seed ...")
  298.         sleep(1)
  299.     end
  300.     goTo(2,3)
  301.     turtle.dropDown(1)
  302.     peripheral.call("bottom", "analyze")
  303.     while peripheral.call("bottom", "isAnalyzed") == false do
  304.         sleep(0.05)
  305.     end
  306.     seed3[1],seed3[2],seed3[3] = peripheral.call("bottom", "getSpecimenStats")
  307.     turtle.suckDown(1)
  308.     stepB()
  309. end
  310.  
  311. function stepA()
  312.     step = 1
  313.     pmsg("Step 1 ...")
  314.     goTo(3,2)
  315.     turtle.select(1)
  316.     turtle.digDown()
  317.     turtle.placeDown(1)
  318.     goTo(3,1)
  319.     turtle.select(3)
  320.     turtle.dropDown(1)
  321.     activateAutonomous()
  322.     turtle.select(2)
  323.     turtle.dropDown(1)
  324.     activateAutonomous()
  325. end
  326.  
  327. function stepB()
  328.     step = 2
  329.     pmsg("Step 2 ...")
  330.     goTo(3,4)
  331.     turtle.select(1)
  332.     turtle.digDown()
  333.     turtle.placeDown(1)
  334.     goTo(3,5)
  335.     turtle.select(3)
  336.     turtle.dropDown(1)
  337.     activateAutonomous()
  338.     turtle.select(2)
  339.     turtle.dropDown(1)
  340.     activateAutonomous()
  341. end
  342.  
  343. function stepC()
  344.     step = 3
  345.     pmsg("Step 3 ...")
  346.     goTo(3,3)
  347.     turtle.select(1)
  348.     turtle.digDown()
  349.     goTo(4,3)
  350.     turtle.select(1)
  351.     turtle.dropDown(2)
  352.     activateAutonomous()
  353.     goTo(2,3)
  354.     if direction == nil then
  355.         findDirection()
  356.     end
  357.     while peripheral.call("bottom", "hasPlant", direction) == false do
  358.         pmsg("Waiting for a seed to appear ... ")
  359.         sleep(1)
  360.         if peripheral.call("bottom", "hasPlant", direction) then break end
  361.     end
  362. end
  363.  
  364. function stepD()
  365.     step = 4
  366.     pmsg("Step 4 ...")
  367.     goTo(3,3)
  368.     turtle.select(1)
  369.     turtle.digDown()
  370.     -- turtle.select(3)
  371.     -- turtle.transferTo(1,1)
  372.     -- turtle.select(4)
  373.     -- turtle.transferTo(3,1)
  374.     goTo(2,3)
  375.     turtle.select(3)
  376.     turtle.dropDown(1)
  377.     peripheral.call("bottom", "analyze")
  378.     while peripheral.call("bottom", "isAnalyzed") == false do
  379.         sleep(0.05)
  380.     end
  381.     seed2[1],seed2[2],seed2[3] = peripheral.call("bottom", "getSpecimenStats")
  382.     turtle.suckDown()
  383.    
  384.     if seed2[1] + seed2[2] + seed2[3] == 30 then
  385.         goTo(fuelChest[1],fuelChest[2])
  386.         step = "done"
  387.     elseif seed2[1] + seed2[2] + seed2[3] > seed1[1] + seed1[2] + seed1[3] or seed2[1] + seed2[2] + seed2[3] > seed3[1] + seed3[2] + seed3[3] then
  388.         pmsg("Found better seed !")
  389.         if seed1[1] + seed1[2] + seed1[3] > seed3[1] + seed3[2] + seed3[3] then
  390.             stepB()
  391.             seed3[1] = seed2[1]
  392.             seed3[2] = seed2[2]
  393.             seed3[3] = seed2[3]
  394.         else
  395.             stepA()
  396.             seed1[1] = seed2[1]
  397.             seed1[2] = seed2[2]
  398.             seed1[3] = seed2[3]
  399.         end
  400.     end
  401. end
  402.  
  403. function stepTrash()
  404.     step = 5
  405.     pmsg("Step trash ...")
  406.     goTo(trashChest[1],trashChest[2])
  407.     turtle.select(3)
  408.     turtle.dropDown(1)
  409.     turtle.select(4)
  410.     turtle.dropDown(1)
  411.     goTo(productionChest[1],productionChest[2])
  412.     turtle.select(5)
  413.     turtle.dropDown()
  414. end
  415.  
  416. stepLoop()
  417. stepStart()
  418.  
  419. while true do
  420.     stepC()
  421.     stepD()
  422.     if step == "done" then
  423.         break
  424.     end
  425.     stepTrash()
  426.     stepLoop()
  427. end
  428. pmsg("Done !")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement