ascobol

Asco Farming

Feb 25th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.70 KB | None | 0 0
  1. --[[
  2.  
  3. Farming Turtle program
  4.  
  5. @author: Ascobol
  6.  
  7. a program for farming different plants in Minecraft FTB with help of ComputerCraft
  8.  
  9. !!I tried to keep the functions as variable as possible, but the program is still made for my setup!!
  10.  
  11. --]]
  12.  
  13. -- Default values
  14. local defaultWaitTime
  15. local defaultRedstoneInput
  16.  
  17. -- Global Values
  18. local farmMode = 1
  19. local waitTime = 0
  20. local rowLength = 0
  21. local distanceBetweenRows = 0
  22. local plantRows = 0
  23.  
  24. -- Header for each menu
  25. function baseMenu()
  26.     -- Clearing screen
  27.     term.clear()
  28.                
  29.     -- Program start
  30.     print ("Farming Turtle")
  31.     print ("==============")
  32. end
  33.  
  34. -- "Shows" a menu to select the farming mode
  35. function menuFarmMode()
  36.     baseMenu()
  37.    
  38.     --Inputs
  39.     print ("What do you want to farm?")
  40.     print ("  1 - Wheat, Barley etc")
  41.     print ("  2 - Melon, Pumpkin etc")
  42.     print ("  3 - Sugar Cane")
  43.     write ("Farm Mode: ")
  44.     farmMode = tonumber(io.read())
  45.     print()
  46.    
  47.     if(farmMode < 1 or farmMode > 3) then
  48.         error("Invalid farmMode Input")
  49.     end
  50. end
  51.  
  52. -- "Shows" a menu to input the waiting time, careful can throw errors
  53. function menuWaitingTime()
  54.     baseMenu()
  55.                
  56.     --Inputs
  57.     print ("How long shall the idle time be?")
  58.     print ("  <0 = No idling, manual handling")
  59.     print ("  >0 = Idle time in seconds")
  60.     print ("   0 = Default time (" .. defaultWaitTime .. " sec)")
  61.     write ("Idle Time: ")
  62.     waitTime = tonumber(io.read())
  63.     print()
  64.          
  65.     if ( waitTime == 0 ) then      
  66.         waitTime = defaultWaitTime
  67.     elseif ( waitTime < 0 ) then        
  68.         waitTime = tonumber(waitTime)
  69.     elseif ( waitTime > 0 ) then
  70.         waitTime = tonumber(waitTime)
  71.     else
  72.         error("Invalid IdleTime Input")
  73.     end
  74. end
  75.  
  76. -- "Shows" a menu to input the row length, careful can throw errors
  77. function menuRowLength()
  78.     baseMenu()
  79.                
  80.     --Inputs
  81.     print ("How long is one row?")    
  82.     write ("Row Length: ")
  83.     rowLength = tonumber(io.read())
  84.     print()
  85.          
  86.     if ( rowLength == nil ) then              
  87.         error("Invalid RowLength Input")
  88.     end
  89. end
  90.  
  91. -- "Shows" a menu to input the distance between rows, careful can throw errors
  92. function menuRowDistance()
  93.     baseMenu()
  94.                
  95.     --Inputs
  96.     print ("How far is it to the next row (incl. the next row)?")    
  97.     write("Row Distance: ")
  98.     distanceBetweenRows = tonumber(io.read())
  99.     print()
  100.          
  101.     if ( distanceBetweenRows == nil ) then              
  102.         error("Invalid RowDistance Input")
  103.     end
  104. end
  105.  
  106. -- "Shows" a menu to input the number of rows to plant, careful can throw errors
  107. function menuPlantRows()
  108.     baseMenu()
  109.                
  110.     --Inputs
  111.     print ("How many rows shall be planted/harvested?")    
  112.     write("Number of Rows: ")
  113.     plantRows = tonumber(io.read())
  114.     print()
  115.          
  116.     if ( plantRows == nil ) then              
  117.         error("Invalid NumberOfPlantRows Input")
  118.     end
  119. end
  120.  
  121. function menuShowInputs()
  122.     baseMenu()
  123.  
  124.     print("FarmMode:     " .. farmMode)
  125.     print("IdleTime:     " .. waitTime)
  126.     print("RowLength:    " .. rowLength)
  127.     print("RowDistance:  " .. distanceBetweenRows)
  128.     print("NumberOfRows: " .. plantRows)
  129.     print("")
  130.     write("Press any key to continue, F1 to cancel")
  131.     ascoTurtleApi.waitForInput()
  132.     print()
  133. end
  134.  
  135. function initProgram()
  136.    
  137.     -- Default values
  138.     defaultWaitTime = 7200
  139.     defaultRedstoneInput = "top"
  140.     defaultPlantRows = 4
  141.     defaultRowLength = 53
  142.     defaultDistanceBetweenRows = 2
  143.  
  144.     local validInput = false
  145.  
  146.     while(not validInput) do
  147.         validInput = pcall(menuFarmMode)
  148.     end
  149.  
  150.     validInput = false
  151.    
  152.     while(not validInput) do
  153.         validInput = pcall(menuWaitingTime)
  154.     end
  155.  
  156.     validInput = false
  157.  
  158.     while(not validInput) do
  159.         validInput = pcall(menuRowLength)
  160.     end
  161.  
  162.     validInput = false
  163.  
  164.     while(not validInput) do
  165.         validInput = pcall(menuRowDistance)
  166.     end
  167.  
  168.     validInput = false
  169.  
  170.     while(not validInput) do
  171.         validInput = pcall(menuPlantRows)
  172.     end
  173.    
  174.     menuShowInputs()    
  175.  
  176. end
  177.  
  178. -- Waits for an redstone input or a timer
  179. function idle()
  180.     print("Idle now for " .. waitTime .. " seconds.")
  181.     local waitInSeconds = waitTime
  182.     local redstoneInput = defaultRedstoneInput
  183.     local interuptted = false
  184.     local waitedSeconds = 0
  185.  
  186.     if (waitTime < 0) then
  187.         print("Manual Control active. Stop idling by redstone input at " .. defaultRedstoneInput .. " side.")
  188.         waitInSeconds = waitedSeconds + 1
  189.     end
  190.  
  191.     while (not(interuptted) and (waitedSeconds < waitInSeconds)) do
  192.         if (checkRedstone(redstoneInput)) then
  193.             interuptted = true
  194.             print "Interrupted by player"
  195.         else
  196.             os.sleep(1)
  197.             waitedSeconds = waitedSeconds + 1
  198.             local x,y = term.getCursorPos()
  199.             term.setCursorPos(x, y-1)
  200.             term.clearLine()
  201.                        
  202.             print("Waited " .. ascoUtilityApi.formatSeconds(waitedSeconds))
  203.                        
  204.             if (waitTime < 0) then
  205.                 waitInSeconds = waitedSeconds + 1
  206.             end
  207.         end
  208.     end
  209.     print "Finished idling, back to work..."
  210. end
  211.  
  212. --Moving around the field
  213. function harvestAndPlant(pRowLength, pPlantRows, pDistanceBetweenRows)
  214.     if(farmMode == 1) then
  215.         harvestingFarmMode1(pRowLength, pPlantRows, pDistanceBetweenRows)
  216.     elseif(farmMode == 2) then
  217.         harvestingFarmMode2(pRowLength, pPlantRows, pDistanceBetweenRows)
  218.     elseif(farmMode == 3) then
  219.         harvestingFarmMode3(pRowLength, pPlantRows, pDistanceBetweenRows)
  220.     end
  221.     print("Harvesting, planting and emptying done.")
  222. end
  223.  
  224. --Farming in Mode 1 (Wheat, Barley, Carrots, Potatoes ...)
  225. function harvestingFarmMode1(pRowLength, pPlantRows, pDistanceBetweenRows)
  226.     print("Start harvesting and planting...")
  227.     ascoFarmingApi.checkSeeds()
  228.     --Start in lower right corner with harvest/plant
  229.     turtle.turnLeft()
  230.     for row = 1, pPlantRows do
  231.         --Harvest
  232.         ascoFarmingApi.harvestDown()
  233.         for position = 1, pRowLength -1 do
  234.             ascoTurtleApi.moveForward()
  235.             ascoFarmingApi.harvestDown()
  236.         end
  237.         ascoTurtleApi.turnAround()
  238.         --Get stack of seeds
  239.         ascoTurtleApi.makeMaxStack(1)
  240.         --Plant
  241.         ascoFarmingApi.plant()
  242.         for position = 1, pRowLength -1 do
  243.             ascoTurtleApi.moveForward()
  244.             ascoFarmingApi.plant()
  245.         end
  246.         --Next row?
  247.         if(row ~= pPlantRows) then
  248.             turtle.turnLeft()
  249.             for move = 1, pDistanceBetweenRows do
  250.                 ascoTurtleApi.moveForward()
  251.             end
  252.             turtle.turnLeft()        
  253.         else
  254.             turtle.turnRight()
  255.         end
  256.     end
  257.    
  258.     --Go home
  259.     for moves = 1, ((pPlantRows - 1) * pDistanceBetweenRows) do
  260.         ascoTurtleApi.moveForward()
  261.     end
  262.     --Parking Position
  263.     ascoTurtleApi.turnAround()
  264.     --Emptying inventory
  265.     ascoTurtleApi.emptyInventory("right")
  266. end
  267.  
  268. --Farming in Mode 2 (Melon, Pumpkin ...)
  269. --No planting, only harvesting
  270. function harvestingFarmMode2(pRowLength, pPlantRows, pDistanceBetweenRows)
  271.     print("Start harvesting ...")    
  272.     --Start in lower right corner with harvest/plant
  273.     turtle.turnLeft()
  274.     for row = 1, pPlantRows do
  275.         --Harvest
  276.         ascoFarmingApi.harvestDown()
  277.         for position = 1, pRowLength -1 do
  278.             ascoTurtleApi.moveForward()
  279.             ascoFarmingApi.harvestDown()
  280.         end
  281.        
  282.         --Next row?
  283.         if(row ~= pPlantRows) then
  284.             --Turn Turtle and move to next row         
  285.             if(ascoUtilityApi.isEvenNumber(row)) then
  286.                 turtle.turnLeft()
  287.                 ascoTurtleApi.moveForward()
  288.                 ascoTurtleApi.moveForward()
  289.                 turtle.turnLeft()
  290.             else
  291.                 turtle.turnRight()
  292.                 for move = 1, pDistanceBetweenRows do
  293.                     ascoTurtleApi.moveForward()
  294.                 end
  295.                 turtle.turnRight()
  296.             end        
  297.         else
  298.             if(ascoUtilityApi.isEvenNumber(row)) then
  299.                 turtle.turnRight()
  300.             else
  301.                 ascoTurtleApi.turnAround()
  302.                 for position = 1, pRowLength -1 do
  303.                     ascoTurtleApi.moveForward()
  304.                 end
  305.                 turtle.turnRight()
  306.             end
  307.         end
  308.     end
  309.    
  310.     --Go home
  311.     local homedist = math.floor(pPlantRows/2)*pDistanceBetweenRows + 2 * math.ceil(pPlantRows/2) - 1
  312.     if(ascoUtilityApi.isEvenNumber(pPlantRows)) then
  313.         homedist = homedist - 1
  314.     else
  315.         homedist = homedist + 1
  316.     end
  317.     for moves = 1, homedist do
  318.         ascoTurtleApi.moveForward()
  319.     end
  320.     --Parking Position
  321.     ascoTurtleApi.turnAround()
  322.     --Emptying inventory
  323.     ascoTurtleApi.emptyInventory("right")
  324. end
  325.  
  326. --Farming in Mode 3 (Sugar Cane ...)
  327. --No planting, only harvesting
  328. function harvestingFarmMode3(pRowLength, pPlantRows, pDistanceBetweenRows)
  329.     print("Start harvesting ...")    
  330.     --Start in lower right corner with harvest/plant
  331.     turtle.turnLeft()
  332.     turtle.up()
  333.     for row = 1, pPlantRows do
  334.         --Harvest
  335.         --3rd level
  336.         ascoFarmingApi.harvestFront()
  337.         for position = 1, pRowLength + 1 do
  338.             ascoTurtleApi.moveForward()
  339.             ascoFarmingApi.harvestFront()
  340.             ascoFarmingApi.harvestDown()
  341.         end
  342.        
  343.         --Next row?
  344.         if(row ~= pPlantRows) then
  345.             --Turn Turtle and move to next row                     
  346.             if(ascoUtilityApi.isEvenNumber(row)) then
  347.                 turtle.turnLeft()
  348.                 for step = 1, pDistanceBetweenRows do
  349.                     ascoTurtleApi.moveForward()        
  350.                 end
  351.                 turtle.turnLeft()
  352.             else
  353.                 turtle.turnRight()
  354.                 for step = 1, pDistanceBetweenRows do
  355.                     ascoTurtleApi.moveForward()        
  356.                 end
  357.                 turtle.turnRight()
  358.             end                
  359.         else
  360.             if(ascoUtilityApi.isEvenNumber(row)) then
  361.                 turtle.turnRight()
  362.             else
  363.                 turtle.turnRight()
  364.                 ascoTurtleApi.moveForward()
  365.                 turtle.turnRight()
  366.                 for step = 1, pDistanceBetweenRows do
  367.                     ascoTurtleApi.moveForward()        
  368.                 end
  369.                 turtle.turnRight()
  370.                 ascoTurtleApi.moveForward()
  371.             end
  372.         end
  373.     end
  374.    
  375.     --Go home
  376.     local homedist = (pPlantRows * pDistanceBetweenRows) - pDistanceBetweenRows
  377.    
  378.     for moves = 1, homedist do
  379.         ascoTurtleApi.moveForward()
  380.     end
  381.     --Parking Position
  382.     ascoTurtleApi.turnAround()
  383.     turtle.down()
  384.     --Emptying inventory
  385.     ascoTurtleApi.emptyInventory("bottom")
  386. end
  387.  
  388. --Helper
  389. function checkRedstone(side)
  390.     return (redstone.getInput(side))
  391. end
  392.  
  393. -- Main function
  394. function Main()
  395.     --APIs are loaded in the downloader
  396.     initProgram()
  397.     while (true) do
  398.        harvestAndPlant(rowLength, plantRows, distanceBetweenRows)
  399.        ascoTurtleApi.printFuelStatus()
  400.        idle()    
  401.     end
  402. end
  403.  
  404. -- Call the main method
  405. Main()
Advertisement
Add Comment
Please, Sign In to add comment