robocyclone

Turtle Farm NOFUEL w/ Persistence

Feb 20th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.40 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.         store("farmCoords", coords)
  91.         if direction == "north" then
  92.             turtle.forward()
  93.         elseif direction == "south" then
  94.             turtle.turnLeft()
  95.             turtle.turnLeft()
  96.             turtle.forward()
  97.         elseif direction == "west" then
  98.             turtle.turnRight()
  99.             turtle.forward()
  100.         elseif direction == "east" then
  101.             turtle.turnLeft()
  102.             turtle.forward()
  103.         end
  104.         direction = "north"
  105.         store("farmDir", direction)
  106.     elseif dirToGo == "east" then
  107.         coords["x"] = coords["x"] + 1
  108.         store("farmCoords", coords)
  109.         if direction == "north" then
  110.             turtle.turnRight()
  111.             turtle.forward()
  112.         elseif direction == "west" then
  113.             turtle.turnLeft()
  114.             turtle.turnLeft()
  115.             turtle.forward()
  116.         elseif direction == "east" then
  117.             turtle.forward()
  118.         elseif direction == "south" then
  119.             turtle.turnLeft()
  120.             turtle.forward()
  121.         end
  122.         direction = "east"
  123.         store("farmDir", direction)
  124.     elseif dirToGo == "south" then
  125.         coords["y"] = coords["y"] - 1
  126.         store("farmCoords", coords)
  127.         if direction == "south" then
  128.             turtle.forward()
  129.         elseif direction == "north" then
  130.             turtle.turnLeft()
  131.             turtle.turnLeft()
  132.             turtle.forward()
  133.         elseif direction == "east" then
  134.             turtle.turnRight()
  135.             turtle.forward()
  136.         elseif direction == "west" then
  137.             turtle.turnLeft()
  138.             turtle.forward()
  139.         end
  140.         direction = "south"
  141.         store("farmDir", direction)
  142.     elseif dirToGo == "west" then
  143.         coords["x"] = coords["x"] - 1
  144.         store("farmCoords", coords)
  145.         if direction == "west" then
  146.             turtle.forward()
  147.         elseif direction == "east" then
  148.             turtle.turnLeft()
  149.             turtle.turnLeft()
  150.             turtle.forward()
  151.         elseif direction == "south" then
  152.             turtle.turnRight()
  153.             turtle.forward()
  154.         elseif direction == "north" then
  155.             turtle.turnLeft()
  156.             turtle.forward()
  157.         end
  158.         direction = "west"
  159.         store("farmDir", direction)
  160.     end
  161.     --fuelTestCount()
  162. end
  163.  
  164. function moveTo(x2, y2)
  165.     if x2 > x then
  166.         local x3 = x2 - x
  167.         for f = 1, x3 do
  168.             forward("east")
  169.         end
  170.     elseif x2 < x then
  171.         local x3 = math.abs((math.abs(x2)) - x)
  172.         for f = 1, x3 do
  173.             forward("west")
  174.         end
  175.     end
  176.     if y2 > y then
  177.         local y3 = y2 - y
  178.         for g = 1, y3 do
  179.             forward("north")
  180.         end
  181.     elseif y2 < y then
  182.         local y3 = math.abs((math.abs(y2)) - 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.             local seeds = tonumber(findSeeds())
  262.             if seeds then
  263.                 turtle.select(seeds)
  264.                 turtle.placeDown()
  265.             else
  266.                 dropOff()
  267.             end
  268.         end
  269.     else
  270.         local seeds = tonumber(findSeeds())
  271.         if seeds then
  272.             turtle.select(seeds)
  273.             turtle.placeDown()
  274.         else
  275.             dropOff()
  276.         end
  277.     end
  278. end
  279.  
  280. function harvestAll()
  281.     for k = 0, 26 do
  282.         for p = 1, 28 do
  283.             moveTo(k, p)
  284.             harvestGrownAndPlant()
  285.         end
  286.     end
  287.     moveTo(0,0)
  288. end
  289.  
  290. moveTo(0,0)
  291. faceTo("north")
  292.  
  293. while true do
  294.     dropOff()
  295.     harvestAll()
  296.     sleep(600)
  297. end
Advertisement
Add Comment
Please, Sign In to add comment