robocyclone

Turtlefarm 4 NF/DP

Feb 27th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.52 KB | None | 0 0
  1. if not fs.exists("/.persistance") then
  2.     print("Persistant variable folder not found. \n Creating...")
  3.     fs.makeDir("/.persistance")
  4.     print("Created")
  5. end
  6.  
  7. function store(sName, stuff)
  8.     local filePath = fs.combine("/.persistance", sName)
  9.     if stuff == nil then
  10.         return fs.delete(filePath)
  11.     end
  12.     local handle = fs.open(filePath, "w")
  13.     handle.write(textutils.serialize(stuff))
  14.     handle.close()
  15. end
  16.  
  17. function pull(sName)
  18.     local filePath = fs.combine("/.persistance", sName)
  19.     local handle = fs.open(filePath, "r")
  20.     local stuff = handle.readAll()
  21.     handle.close()
  22.     return textutils.unserialize(stuff)
  23. end
  24.  
  25. if not fs.open(".persistance/farmCoords", "r") then
  26.     coords = { }
  27.     coords["x"] = 0
  28.     coords["y"] = 0
  29.     store("farmCoords", coords)
  30. else
  31.     coords = pull("farmCoords")
  32. end
  33. if not fs.open(".persistance/farmDir", "r") then
  34.     direction = "north"
  35.     store("farmDir", direction)
  36. else
  37.     direction = pull("farmDir")
  38. end
  39.  
  40. if coords["x"] == 0 and coords["y"] == 0 then
  41.     local success, idtable = turtle.inspectDown()
  42.     if success and idtable.name == "minecraft:chest" then
  43.         print("Home chest found")
  44.     else
  45.         error("Need chest below turtle to start farming!")
  46.     end
  47. end
  48.  
  49. --local fuelTC = 0
  50.  
  51. --[[function testForAvailableFuel()
  52.     for i = 1, 16 do
  53.         turtle.select(i)
  54.         if turtle.refuel(0) then
  55.             return true
  56.         end
  57.     end
  58.     return false
  59. end
  60.  
  61. function fuelTestCount()
  62.     if fuelTC > 10 then
  63.         fuelTC = fuelTC + 1
  64.     else
  65.         fuelTC = 0
  66.         fuel()
  67.     end
  68. end
  69.  
  70.  
  71. function fuel()
  72.     local fLimit = turtle.getFuelLimit()
  73.     local fCurr = turtle.getFuelLevel()
  74.     local fSucc = testForAvailableFuel()
  75.     if fCurr >= 5000 then return end
  76.     if fCurr <= math.floor(fLimit/2) and fSucc == true then
  77.         for i = 1, 16 do
  78.             turtle.select(i)
  79.             if turtle.refuel(0) then
  80.                 local halfStack = math.floor(turtle.getItemCount(i)/2)
  81.                 turtle.refuel(halfStack)
  82.             end
  83.         end
  84.     end
  85. end]]
  86.  
  87. function forward(dirToGo)
  88.     if dirToGo == "north" then
  89.         coords["y"] = coords["y"] + 1
  90.         if direction == "north" then
  91.             turtle.forward()
  92.         elseif direction == "south" then
  93.             turtle.turnLeft()
  94.             turtle.turnLeft()
  95.             turtle.forward()
  96.         elseif direction == "west" then
  97.             turtle.turnRight()
  98.             turtle.forward()
  99.         elseif direction == "east" then
  100.             turtle.turnLeft()
  101.             turtle.forward()
  102.         end
  103.         store("farmCoords", coords)
  104.         direction = "north"
  105.         store("farmDir", direction)
  106.     elseif dirToGo == "east" then
  107.         coords["x"] = coords["x"] + 1
  108.         if direction == "north" then
  109.             turtle.turnRight()
  110.             turtle.forward()
  111.         elseif direction == "west" then
  112.             turtle.turnLeft()
  113.             turtle.turnLeft()
  114.             turtle.forward()
  115.         elseif direction == "east" then
  116.             turtle.forward()
  117.         elseif direction == "south" then
  118.             turtle.turnLeft()
  119.             turtle.forward()
  120.         end
  121.         store("farmCoords", coords)
  122.         direction = "east"
  123.         store("farmDir", direction)
  124.     elseif dirToGo == "south" then
  125.         coords["y"] = coords["y"] - 1
  126.         if direction == "south" then
  127.             turtle.forward()
  128.         elseif direction == "north" then
  129.             turtle.turnLeft()
  130.             turtle.turnLeft()
  131.             turtle.forward()
  132.         elseif direction == "east" then
  133.             turtle.turnRight()
  134.             turtle.forward()
  135.         elseif direction == "west" then
  136.             turtle.turnLeft()
  137.             turtle.forward()
  138.         end
  139.         store("farmCoords", coords)
  140.         direction = "south"
  141.         store("farmDir", direction)
  142.     elseif dirToGo == "west" then
  143.         coords["x"] = coords["x"] - 1
  144.         if direction == "west" then
  145.             turtle.forward()
  146.         elseif direction == "east" then
  147.             turtle.turnLeft()
  148.             turtle.turnLeft()
  149.             turtle.forward()
  150.         elseif direction == "south" then
  151.             turtle.turnRight()
  152.             turtle.forward()
  153.         elseif direction == "north" then
  154.             turtle.turnLeft()
  155.             turtle.forward()
  156.         end
  157.         store("farmCoords", coords)
  158.         direction = "west"
  159.         store("farmDir", direction)
  160.     end
  161.     --fuelTestCount()
  162. end
  163.  
  164. function moveTo(x2, y2)
  165.     if x2 > coords["x"] then
  166.         local x3 = x2 - coords["x"]
  167.         for f = 1, x3 do
  168.             forward("east")
  169.         end
  170.     elseif x2 < coords["x"] then
  171.         local x3 = math.abs((math.abs(x2)) - coords["x"])
  172.         for f = 1, x3 do
  173.             forward("west")
  174.         end
  175.     end
  176.     if y2 > coords["y"] then
  177.         local y3 = y2 - coords["y"]
  178.         for g = 1, y3 do
  179.             forward("north")
  180.         end
  181.     elseif y2 < coords["y"] then
  182.         local y3 = math.abs((math.abs(y2)) - coords["y"])
  183.         for g = 1, y3 do
  184.             forward("south")
  185.         end
  186.     end
  187. end
  188.  
  189. function faceTo(dirToTurn)
  190.     if dirToTurn == "north" then
  191.         if direction == "south" then
  192.             for i = 1, 2 do turtle.turnRight() end
  193.         elseif direction == "east" then
  194.             turtle.turnLeft()
  195.         elseif direction == "west" then
  196.             turtle.turnRight()
  197.         end
  198.         direction = "north"
  199.         store("farmDir", direction)
  200.     elseif dirToTurn == "south" then
  201.         if direction == "north" then
  202.             for i = 1, 2 do turtle.turnRight() end
  203.         elseif direction == "east" then
  204.             turtle.turnRight()
  205.         elseif direction == "west" then
  206.             turtle.turnLeft()
  207.         end
  208.         direction = "south"
  209.         store("farmDir", direction)
  210.     elseif dirToTurn == "east" then
  211.         if direction == "west" then
  212.             for i = 1, 2 do turtle.turnRight() end
  213.         elseif direction == "north" then
  214.             turtle.turnRight()
  215.         elseif direction == "south" then
  216.             turtle.turnLeft()
  217.         end
  218.         direction = "east"
  219.         store("farmDir", direction)
  220.     elseif dirToTurn == "west" then
  221.         if direction == "east" then
  222.             for i = 1, 2 do turtle.turnRight() end
  223.         elseif direction == "north" then
  224.             turtle.turnLeft()
  225.         elseif direction == "south" then
  226.             turtle.turnRight()
  227.         end
  228.         direction = "west"
  229.         store("farmDir", direction)
  230.     end
  231. end
  232.  
  233. function dropOff()
  234.     moveTo(0,0)
  235.     faceTo("south")
  236.     for i = 1, 16 do
  237.         turtle.select(i)
  238.         turtle.dropDown()
  239.     end
  240.     turtle.select(1)
  241.     turtle.suck()
  242. end
  243.  
  244. function findSeeds()
  245.     for i = 1, 16 do
  246.         turtle.select(i)
  247.         local data = turtle.getItemDetail()
  248.         if data then
  249.             if data.name == "minecraft:wheat_seeds" then
  250.                 return i
  251.             end
  252.         end
  253.     end
  254. end
  255.  
  256. function harvestGrownAndPlant()
  257.     local under, wheatCheck = turtle.inspectDown()
  258.     if under then
  259.         if wheatCheck.name == "minecraft:wheat" and wheatCheck.state.age == 7 then
  260.             turtle.digDown()
  261.             if not tonumber(findSeeds()) then return end
  262.             local seeds = tonumber(findSeeds())
  263.             if seeds then
  264.                 turtle.select(seeds)
  265.                 turtle.placeDown()
  266.             else
  267.                 dropOff()
  268.             end
  269.         end
  270.     else
  271.         local seeds = tonumber(findSeeds())
  272.         if seeds then
  273.             turtle.select(seeds)
  274.             turtle.placeDown()
  275.         else
  276.             dropOff()
  277.         end
  278.     end
  279. end
  280.  
  281. function harvestAll()
  282.     for k = 0, 26 do
  283.         for p = 1, 28 do
  284.             moveTo(k, p)
  285.             harvestGrownAndPlant()
  286.         end
  287.     end
  288.     moveTo(0,0)
  289. end
  290.  
  291. moveTo(0,0)
  292. faceTo("north")
  293.  
  294. while true do
  295.     dropOff()
  296.     harvestAll()
  297.     sleep(600)
  298. end
Advertisement
Add Comment
Please, Sign In to add comment