Bunny_bt

Lazullia

Aug 2nd, 2022 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.18 KB | None | 0 0
  1. local isCalibrated = false
  2. local isFacingNormal = true
  3. local isRightward = false --Which way from the start do the crops go
  4. local fuelNeeded = 80 --Starts defined for calibration
  5. local timer = 45 --Seconds
  6. local height = 0
  7. local width = 1
  8.  
  9. --turtle.inspect [bool, content[name, metadata], error]  name=thaumicbases:lazullia
  10.  
  11. function Calibrate(crop)
  12.     if Refuel(fuelNeeded) == false then
  13.         turtle.turnLeft()
  14.         if RestockFuel(fuelNeeded) == true then
  15.             Refuel(fuelNeeded)
  16.             turtle.turnRight()
  17.         else
  18.             print(math.ceil(((fuelNeeded - turtle.getFuelLevel()) / 80)), ' coal required for calibration.')
  19.             turtle.turnRight()
  20.             return
  21.         end
  22.     end
  23.     print('Calibrating...')
  24.  
  25.     --Find height
  26.     turtle.up()
  27.     turtle.forward()
  28.     while true do
  29.         local inspected = {turtle.inspectDown()}
  30.         if inspected[1] ~= false and string.find(inspected[2].name, crop) then
  31.             height = height + 1
  32.             turtle.forward()
  33.         else
  34.             break
  35.         end
  36.     end
  37.  
  38.     --Position turtle for width
  39.     turtle.back()
  40.     turtle.turnLeft()
  41.     turtle.forward()
  42.     local inspected = {turtle.inspectDown()}
  43.     if inspected[1] == true and string.find(inspected[2].name, crop) == true then
  44.         isRightward = false
  45.     else
  46.         isRightward = true
  47.         turtle.back()
  48.         turtle.turnRight()
  49.         turtle.turnRight()
  50.         turtle.forward()
  51.     end
  52.  
  53.     --Find width
  54.     while true do
  55.         local inspected = {turtle.inspectDown()}
  56.         if inspected[1] ~= false and string.find(inspected[2].name, crop) then
  57.             width = width + 1
  58.             turtle.forward()
  59.         else
  60.             break
  61.         end
  62.     end
  63.  
  64.     --Return
  65.     turtle.turnLeft()
  66.     turtle.turnLeft()
  67.     for i = 1, width, 1 do
  68.         turtle.forward()
  69.     end
  70.     turtle.turnLeft()
  71.     for i = 1, height, 1 do
  72.         turtle.forward()
  73.     end
  74.     turtle.turnRight()
  75.     turtle.turnRight()
  76.     turtle.down()
  77.  
  78.     --Get required fuel
  79.     if math.floor(math.fmod(width,2)) == (width/2) then
  80.         fuelNeeded = height*width + width --if even
  81.     else
  82.         fuelNeeded = height*width + height+width --if odd
  83.     end
  84.  
  85.     --Display info
  86.     print('Calibration completed!\n')
  87.  
  88.     print('Height: ', height)
  89.     print('Width: ', width)
  90.     print('Expected ', crop, ': ', height*width)
  91.     print('Charcoal per run: ', fuelNeeded/80)
  92.  
  93.     isCalibrated = true
  94. end
  95.  
  96. function Refuel(RequiredFuel)
  97.     for i = 1, 16, 1 do
  98.         if turtle.getFuelLevel() >= RequiredFuel then --When refueled
  99.             print('Fuel checked')
  100.             return true
  101.         elseif turtle.getItemDetail(i) and turtle.getItemDetail(i).name == "minecraft:coal" then --Continue refill
  102.             turtle.select(i)
  103.             turtle.refuel(math.ceil(((RequiredFuel - turtle.getFuelLevel()) / 80)))
  104.             if turtle.getFuelLevel() >= RequiredFuel then --When refueled
  105.                 print('Fuel checked')
  106.                 return true
  107.             end
  108.         end
  109.     end
  110.     if turtle.getFuelLevel() < RequiredFuel then --When failed
  111.         print('Not enough fuel')
  112.         return false
  113.     end
  114. end
  115.  
  116. function Deposit(itemName)
  117.     local count = 0
  118.     for i = 1, 16, 1 do
  119.         turtle.select(i)
  120.         local selectedDetail = turtle.getItemDetail()
  121.         if selectedDetail and string.find(selectedDetail.name, itemName) then
  122.             turtle.drop()
  123.             count = count + selectedDetail.count
  124.         end
  125.     end
  126.     print(count, itemName, 'deposited.')
  127. end
  128.  
  129. function Harvest(maxGrowth, seedName)
  130.     local state, contents, err = turtle.inspectDown()
  131.     if contents.metadata == nil or contents.metadata < maxGrowth then --Skip ungrown crop
  132.         return
  133.     elseif contents.metadata >= maxGrowth then --Harvest grown crop
  134.         turtle.digDown()
  135.         for i = 1, 16, 1 do
  136.             local selectedDetail = turtle.getItemDetail(i)
  137.             if selectedDetail and string.find(selectedDetail.name, seedName) then --Replant seed
  138.                 turtle.select(i)
  139.                 turtle.placeDown()
  140.                 turtle.select(1)
  141.                 return
  142.             end
  143.         end
  144.     end
  145. end
  146.  
  147. function Idle(Timer)
  148.     os.startTimer(Timer) --45 sec
  149.     local evt = {os.pullEvent()}
  150.     if evt[1] == 'timer' and Refuel(fuelNeeded) then --On timer and fueled
  151.         isFacingNormal = true
  152.        
  153.         --Display
  154.         --term.clear()
  155.         --term.setCursorPos(1,1)
  156.  
  157.         return false
  158.     elseif evt[1] == 'timer' then --On timer, fuel required
  159.         turtle.turnLeft()
  160.         if RestockFuel(fuelNeeded) then --Restock fuel
  161.             turtle.turnRight()
  162.             if Refuel(fuelNeeded) then --Refuel
  163.                 return false
  164.             end
  165.         else --Restock failure
  166.             turtle.turnRight()
  167.             return true
  168.         end
  169.     else
  170.         return true
  171.     end
  172. end
  173.  
  174. function RestockFuel(RequiredFuel) --TODO: simplify this shit
  175.     print('Restocking fuel...')
  176.     local coalSlot
  177.     for i = 1, 16, 1 do
  178.         local detail = turtle.getItemDetail(i)
  179.         if (detail ~= nil and detail.name == "minecraft:coal") then
  180.             coalSlot = i
  181.             break
  182.         elseif detail == nil then
  183.             coalSlot = i
  184.             break
  185.         end
  186.     end
  187.     for i = 1, 16, 1 do
  188.         turtle.select(i)
  189.         if (turtle.getItemDetail() ~= nil) and (turtle.getItemDetail().name == "minecraft:coal") then
  190.             turtle.transferTo(coalSlot)
  191.         end
  192.     end
  193.     turtle.select(coalSlot)
  194.     turtle.suck(turtle.getItemSpace())
  195.     if turtle.getItemCount(coalSlot) >= math.ceil(((RequiredFuel - turtle.getFuelLevel()) / 80)) then
  196.         print('Restocking successful')
  197.         return true
  198.     else
  199.         print('Restocking failed!')
  200.         return false
  201.     end
  202. end
  203.  
  204. function ReturnHome()
  205.     if isFacingNormal and isRightward then --O:1
  206.         print('Running O:1')
  207.         turtle.turnLeft()
  208.         for i = 1, width-1, 1 do
  209.             turtle.forward()
  210.         end
  211.         turtle.turnLeft()
  212.         for i = 1, height, 1 do
  213.             turtle.forward()
  214.         end
  215.         turtle.down()
  216.     elseif isFacingNormal and isRightward == false then --O:2
  217.         print('Running O:2')
  218.         turtle.turnRight()
  219.         for i = 1, width-1, 1 do
  220.             turtle.forward()
  221.         end
  222.         turtle.turnRight()
  223.         for i = 1, height, 1 do
  224.             turtle.forward()
  225.         end
  226.         turtle.down()
  227.     elseif isFacingNormal == false and isRightward then --O:3
  228.         print('Running O:3')
  229.         turtle.turnRight()
  230.         for i = 1, width-1, 1 do
  231.             turtle.forward()
  232.         end
  233.         turtle.turnLeft()
  234.         turtle.forward()
  235.         turtle.down()
  236.     elseif isFacingNormal == false and isRightward == false then --O:4
  237.         print('Running O:4')
  238.         turtle.turnLeft()
  239.         for i = 1, width-1, 1 do
  240.             turtle.forward()
  241.         end
  242.         turtle.turnRight()
  243.         turtle.forward()
  244.         turtle.down()
  245.     end
  246. end
  247.  
  248. function Rows(seedName)
  249.     for i = 1, width, 1 do
  250.         for i = 1, height-1, 1 do
  251.             turtle.forward()
  252.             Harvest(7, seedName)
  253.         end
  254.         if i < width then
  255.             if isFacingNormal and isRightward then  --Turn right
  256.                 turtle.turnRight()
  257.                 turtle.forward()
  258.                 turtle.turnRight()
  259.                 isFacingNormal = not isFacingNormal
  260.                 print('Normal: ', isFacingNormal)
  261.                 Harvest(7, seedName)
  262.             elseif isFacingNormal and isRightward == false then  --Turn left
  263.                 turtle.turnLeft()
  264.                 turtle.forward()
  265.                 turtle.turnLeft()
  266.                 isFacingNormal = not isFacingNormal
  267.                 print('Normal: ', isFacingNormal)
  268.                 Harvest(7, seedName)
  269.             elseif isFacingNormal == false and isRightward then --Turn left
  270.                 turtle.turnLeft()
  271.                 turtle.forward()
  272.                 turtle.turnLeft()
  273.                 isFacingNormal = not isFacingNormal
  274.                 print('Normal: ', isFacingNormal)
  275.                 Harvest(7, seedName)
  276.             elseif isFacingNormal == false and isRightward == false then --Turn right
  277.                 turtle.turnRight()
  278.                 turtle.forward()
  279.                 turtle.turnRight()
  280.                 isFacingNormal = not isFacingNormal
  281.                 print('Normal: ', isFacingNormal)
  282.                 Harvest(7, seedName)
  283.             end
  284.         end
  285.        
  286.     end
  287. end
  288.  
  289. --Main
  290. while true do
  291.     if not isCalibrated then --Calibrate
  292.         Calibrate('lazullia')
  293.         if not isCalibrated then
  294.             sleep(timer)
  295.         end
  296.     elseif Idle(timer) == false and isCalibrated then --Harvest crops after timer
  297.         --This is a fucking mess
  298.         --This looping rapidly, Idle(timer) == false not yet tested
  299.         print('Harvesting...')
  300.         turtle.up()
  301.         turtle.forward()
  302.         Harvest(7, seedName)
  303.         Rows("seed")
  304.         ReturnHome()
  305.         Deposit("dye")
  306.         turtle.turnRight()
  307.         turtle.turnRight()
  308.         print('Harvest completed.')
  309.     end
  310. end
Add Comment
Please, Sign In to add comment