Advertisement
Guest User

startup

a guest
Feb 25th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.86 KB | None | 0 0
  1. -- Auto Turtle Farm (by Jake)
  2.  
  3. local logFile = false
  4. local sizeX = 9
  5. local sizeY = 9
  6. local waitTime = 120
  7.  
  8. -- Logging Function
  9.  
  10. local w,h = term.getSize()
  11. local function log(str)
  12.     if logFile then
  13.         f = fs.open("/log","a")
  14.         f.writeLine(str)
  15.         f.close()
  16.     end
  17.     local curX,curY = term.getCursorPos()
  18.     term.setCursorPos(1,1)
  19.     write(string.rep(" ",w))
  20.     term.setCursorPos(1,1)
  21.     local percent = (turtle.getFuelLevel()/turtle.getFuelLimit())*100
  22.     write("Gardener Turtle OS | Fuel: "..tostring(math.floor(percent)).."%")
  23.     term.setCursorPos(curX,curY)
  24.     write(str)
  25.     term.setCursorPos(curX,curY+1)
  26. end
  27.  
  28. -- Saving Data
  29.  
  30. local function saveData()
  31.     if data then
  32.         f = fs.open("/.data","w")
  33.         f.writeLine(textutils.serialise(data))
  34.         f.close()
  35.         return true
  36.     else
  37.         return false
  38.     end
  39. end
  40.  
  41. -- Recalling Data
  42.  
  43. if fs.exists("/.data") then
  44.     f = fs.open("/.data","r")
  45.     data = textutils.unserialise(f.readAll())
  46.     f.close()
  47.     log("Data loaded")
  48. else
  49.     data = {}
  50.     data.x = 0
  51.     data.y = 0
  52.     data.f = 2
  53.     data.z = 0
  54.     saveData()
  55. end
  56.  
  57. -- Sorting Inventory
  58.  
  59. local openSlot = 16
  60. local invSlots = {
  61.     [15] = "minecraft:wheat_seeds",
  62.     [14] = "minecraft:sapling",
  63.     [13] = "minecraft:bonemeal",
  64.     [12] = "minecraft:pumpkin",
  65.     [11] = "minecraft:melon",
  66.     [10] = "minecraft:carrot",
  67.     [9] = "minecraft:potato",
  68.     [8] = "minecraft:wheat",
  69.     [7] = "minecraft:log",
  70.     [6] = "minecraft:reeds",
  71.     [5] = "minecraft:coal",
  72. }
  73. function sortInv()
  74.     for i=1,16 do
  75.         local itemData = turtle.getItemDetail(i)
  76.         if itemData then
  77.             local destSlot = nil
  78.             for i,v in pairs(invSlots) do
  79.                 if v == itemData.name then
  80.                     destSlot = i
  81.                 end
  82.             end
  83.             if destSlot then
  84.                 if destSlot ~= i then
  85.                     turtle.select(i)
  86.                     turtle.transferTo(destSlot)
  87.                 end
  88.             else
  89.                 turtle.select(i)
  90.                 turtle.drop()
  91.             end
  92.         end
  93.     end
  94.     turtle.select(1)
  95. end
  96. function selectItem(name)
  97.     for i,v in pairs(invSlots) do
  98.         if v == name then
  99.             turtle.select(i)
  100.         end
  101.     end
  102. end
  103.  
  104. -- Facing Directions
  105.  
  106. local hdg = {
  107.     [1] = {0,-1},
  108.     [2] = {1,0},
  109.     [3] = {0,1},
  110.     [4] = {-1,0},
  111. }
  112.  
  113. -- Turtle Functions
  114.  
  115. local t = {}
  116. for i,v in pairs(turtle) do
  117.     t[i] = v
  118. end
  119. function fuelCheck()
  120.     if turtle.getFuelLevel() <= 0 then
  121.         log("Oh no! I'm out of fuel!")
  122.         log("Searching for a fuel source..")
  123.         while turtle.getFuelLevel() <= 0 do
  124.             for i=1,16 do
  125.                 turtle.select(i)
  126.                 if turtle.refuel(4) then
  127.                     break
  128.                 end
  129.             end
  130.             sleep(5)
  131.         end
  132.         log("Back in business!")
  133.         turtle.select(1)
  134.     end
  135. end
  136. function t.forward()
  137.     fuelCheck()
  138.     while true do
  139.         if turtle.forward() then
  140.             break
  141.         else
  142.             turtle.attack()
  143.             turtle.dig()
  144.         end
  145.         sleep(0)
  146.     end
  147.     data.x = data.x + hdg[data.f][1]
  148.     data.y = data.y + hdg[data.f][2]
  149.     saveData()
  150. end
  151. function t.back()
  152.     fuelCheck()
  153.     while true do
  154.         if turtle.back() then
  155.             break
  156.         else
  157.             data.f = data.f + 1
  158.             turtle.turnRight()
  159.             data.f = data.f + 1
  160.             turtle.turnRight()
  161.             turtle.attack()
  162.             while not turtle.dig() do
  163.                 sleep(30)
  164.             end
  165.             data.f = data.f + 1
  166.             turtle.turnRight()
  167.             data.f = data.f + 1
  168.             turtle.turnRight()
  169.         end
  170.         sleep(0)
  171.     end
  172.     data.x = data.x - hdg[data.f][1]
  173.     data.y = data.y - hdg[data.f][1]
  174.     saveData()
  175. end
  176. function t.turnLeft()
  177.     data.f = data.f - 1
  178.     if data.f < 1 then
  179.         data.f = 4
  180.     end
  181.     saveData()
  182.     turtle.turnLeft()
  183. end
  184. function t.turnRight()
  185.     data.f = data.f + 1
  186.     if data.f > 4 then
  187.         data.f = 1
  188.     end
  189.     saveData()
  190.     turtle.turnRight()
  191. end
  192. function t.up()
  193.     fuelCheck()
  194.     data.z = data.z + 1
  195.     saveData()
  196.     turtle.up()
  197. end
  198. function t.down()
  199.     fuelCheck()
  200.     data.z = data.z - 1
  201.     saveData()
  202.     turtle.down()
  203. end
  204. local function face(heading)
  205.     while heading ~= data.f do
  206.         t.turnRight()
  207.     end
  208. end
  209. local function goTo(tX,tY)
  210.     if data.x < tX then
  211.         face(2)
  212.     end
  213.     while data.x < tX do
  214.         t.forward()
  215.     end
  216.     if data.y < tY then
  217.         face(3)
  218.     end
  219.     while data.y < tY do
  220.         t.forward()
  221.     end
  222.     if data.x > tX then
  223.         face(4)
  224.     end
  225.     while data.x > tX do
  226.         t.forward()
  227.     end
  228.     if data.y > tY then
  229.         face(1)
  230.     end
  231.     while data.y > tY do
  232.         t.forward()
  233.     end
  234. end
  235.  
  236. -- Cleanup Screen
  237.  
  238. term.clear()
  239. term.setCursorPos(1,2)
  240. if shell.getRunningProgram() ~= "startup" and shell.getRunningProgram() ~= "startup.lua" then
  241.     log("Warning! Not running as startup.")
  242. end
  243. while data.z > 0 do
  244.     t.down()
  245. end
  246.  
  247. -- Dump Function
  248.  
  249. local holdItems = {
  250.     ["minecraft:log"] = 24,
  251.     ["minecraft:wheat_seeds"] = 16,
  252.     ["minecraft:sapling"] = 8,
  253. }
  254. local dumpItems = {
  255.     ["minecraft:carrot"] = true,
  256.     ["minecraft:log"] = true,
  257.     ["minecraft:wheat"] = true,
  258.     ["minecraft:wheat_seeds"] = true,
  259.     ["minecraft:potato"] = true,
  260.     ["minecraft:reeds"] = true,
  261.     ["minecraft:pumpkin"] = true,
  262.     ["minecraft:melon"] = true,
  263.     ["minecraft:sapling"] = true,
  264. }
  265. function dump(resume)
  266.     log("Returning to dump point")
  267.     if resume then
  268.         local callback = {
  269.             ["x"] = data.x,
  270.             ["y"] = data.y,
  271.             ["f"] = data.f,
  272.         }
  273.     end
  274.     goTo(0,0)
  275.     face(2)
  276.     log("Dumping items...")
  277.     local _,meta = turtle.inspectDown()
  278.     if meta then
  279.         if meta.name == "minecraft:chest" then
  280.             -- All good
  281.         else
  282.             log("Error! Chest not found")
  283.             return false
  284.         end
  285.     end
  286.     for i=1,16 do
  287.         turtle.select(i)
  288.         local itemData = turtle.getItemDetail(i)
  289.         if itemData then
  290.             if dumpItems[itemData.name] then
  291.                 turtle.select(i)
  292.                 local dropAmount = nil
  293.                 if holdItems[itemData.name] then
  294.                     dropAmount = itemData.count - holdItems[itemData.name]
  295.                 end
  296.                 if dropAmount then
  297.                     if dropAmount > 0 then
  298.                         turtle.dropDown(dropAmount)
  299.                     end
  300.                 else
  301.                     turtle.dropDown()
  302.                 end
  303.             end
  304.         end
  305.     end
  306.     log("Done!")
  307.     turtle.select(1)
  308.     if resume then
  309.         log("Returning to traverse")
  310.         goTo(callback.x,callback.y)
  311.         face(callback.f)
  312.     end
  313. end
  314.  
  315. -- Return Home
  316.  
  317. while data.z > 0 do
  318.     t.down()
  319. end
  320. sortInv()
  321. dump(false)
  322. local _,meta = turtle.inspectDown()
  323. if meta then
  324.     if meta.name == "minecraft:chest" then
  325.         log("In position, ready to go!")
  326.     else
  327.         log("Cannot find home point!")
  328.         return    
  329.     end
  330. end
  331.  
  332. -- Traverse Garden
  333.  
  334. log("All systems operational")
  335. while true do
  336.     log("Begin traverse")
  337.     local dir = true
  338.     for x=1,sizeY do
  339.         if dir then
  340.             face(2)
  341.         else
  342.             face(4)
  343.         end
  344.         for y=1,sizeX do
  345.             local ok,meta = t.inspectDown()
  346.             if ok then
  347.                 if meta.name == "minecraft:log" then
  348.                     log("Tree! Harvesting..")
  349.                     t.digDown()
  350.                     while t.detectUp() do
  351.                         t.digUp()
  352.                         t.up()
  353.                         t.dig()
  354.                         t.turnRight()
  355.                         t.dig()
  356.                         t.turnRight()
  357.                         t.dig()
  358.                         t.turnRight()
  359.                         t.dig()
  360.                         t.turnRight()
  361.                     end
  362.                     while data.z > 0 do
  363.                         t.down()
  364.                     end
  365.                     sortInv()
  366.                     selectItem("minecraft:sapling")
  367.                     t.placeDown()
  368.                     t.select(1)
  369.                 elseif meta.name == "minecraft:wheat" then
  370.                     if meta.metadata >= 7 then
  371.                         t.digDown()
  372.                         sortInv()
  373.                         selectItem("minecraft:wheat_seeds")
  374.                         t.placeDown()
  375.                         t.select(1)
  376.                     end
  377.                 elseif meta.name == "minecraft:potatoes" then
  378.                     if meta.metadata >= 7 then
  379.                         t.digDown()
  380.                         sortInv()
  381.                         selectItem("minecraft:potato")
  382.                         t.placeDown()
  383.                         t.select(1)
  384.                     end
  385.                 elseif meta.name == "minecraft:carrots" then
  386.                     if meta.metadata >= 7 then
  387.                         t.digDown()
  388.                         sortInv()
  389.                         selectItem("minecraft:carrot")
  390.                         t.placeDown()
  391.                         t.select(1)
  392.                     end    
  393.                 end
  394.             end
  395.             t.forward()
  396.         end
  397.         if x ~= sizeX then
  398.             if dir then
  399.                 t.turnRight()
  400.                 t.forward()
  401.                 t.turnRight()
  402.             else
  403.                 t.turnLeft()
  404.                 t.forward()
  405.                 t.turnLeft()
  406.             end
  407.         end
  408.         dir = not dir
  409.     end
  410.     dump(false)
  411.     log("Traverse complete")
  412.     log("Waiting for "..tostring(waitTime).." seconds")
  413.     sleep(waitTime)
  414. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement