Advertisement
Rnen10

Forestry WoodPile Charcoal Turtle

Jul 17th, 2022 (edited)
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.83 KB | None | 0 0
  1. -- Made by Rnen10
  2. -- If problems/questions feel free to contact me
  3. -- https://twitter.com/Rnen10
  4. -- rnen. (Evan#7181)  on discord
  5.  
  6. -- This automates a Forestry wood pile oven using a CC turtle. (Needs a pickaxe and access to flint to the right, wood piles below, output on top). Made for a 3x3 oven, start off the turtle on the middle. If the coordinate system gets broken, just run "rm coalData"
  7.  
  8. ---@diagnostic disable-next-line: undefined-global
  9. term.clear()
  10. term.setCursorPos(1, 1)
  11.  
  12. --[[The sleep time before closing it off after lighting]]
  13. local lightTime = 1
  14. --[[The minimum fuel required for one oven run]]
  15. local min_fuel = 40
  16. --[[The time to wait for the next batch. waitTime * 10 = seconds]]
  17. local waitTime = 150
  18.  
  19. local debug = false
  20. local brickSlot = 14
  21. local flintSlot = 15
  22. local woodSlot = 16
  23.  
  24. local function DEBUG()
  25.     return (fs.exists("coaldebug.lua") or debug)
  26. end
  27.  
  28. local done = false
  29. local turtleState = { ["x"] = 0, ["y"] = 0, ["rot"] = 1, ["action"] = 0 }
  30.  
  31. local function split(inputstr)
  32.     local t = {}
  33.     for str in string.gmatch(inputstr, "[^\r\n]+") do
  34.         table.insert(t, str)
  35.     end
  36.     return t
  37. end
  38.  
  39. --[[Refreshes the GUI with new text]]
  40. local function NewLine(Str)
  41.     local barLength = 40
  42.     local tempPercentage = 0
  43.     term.clear()
  44.     term.setCursorPos(1, 2)
  45.     print("-------------[CoalBot v1.0]------------")
  46.     term.setCursorPos(11, 5)
  47.     write("v {Current action} v")
  48.     local splString = split(Str)
  49.     local size1 = 0
  50.     for _ in pairs(splString) do size1 = size1 + 1 end
  51.     if size1 > 1 then
  52.         for i, v in ipairs(splString) do
  53.             term.setCursorPos(21 - (math.floor(string.len(v) / 2)), i + 7)
  54.             term.write(v)
  55.         end
  56.     else
  57.         term.setCursorPos(21 - (math.floor(string.len(Str) / 2)), 8)
  58.         term.write(Str)
  59.     end
  60.     --if DEBUG() then
  61.     --    term.setCursorPos(1, 11)
  62.     --   print("X: " ..
  63.     --        turtleState["x"] ..
  64.     --        " Y: " .. turtleState["y"] .. " Rot: " .. turtleState["rot"] .. " Action: " .. turtleState["action"])
  65.     --end
  66.     local fuelStr = "[Current Fuel - %" .. tostring((math.floor(turtle.getFuelLevel()))) .. "]"
  67.     term.setCursorPos(21 - (math.floor(string.len(fuelStr) / 2)), 12)
  68.     write(fuelStr)
  69.     term.setCursorPos(1, 13)
  70.     barLength = turtle.getFuelLevel() / 100 * 40
  71.     if barLength > 40 then barLength = 40 end
  72.     if term.isColor() then
  73.         term.setBackgroundColor(colors.green)
  74.     else
  75.         term.setBackgroundColor(colors.white)
  76.     end
  77.     for i = 1, (barLength) do
  78.         write(" ")
  79.     end
  80.     if barLength < 40 then
  81.         if term.isColor() then term.setBackgroundColor(colors.red) else term.setBackgroundColor(colors.gray) end
  82.         local remainingBarLength = (40 - barLength)
  83.         for i = 1, (remainingBarLength) do
  84.             write(" ")
  85.         end
  86.     end
  87.     term.setBackgroundColor(colors.black)
  88. end
  89.  
  90. --[[Reads the turtleState data from a file, or writes a new one if it doesn't exist]]
  91. local function CheckPosition()
  92.     if fs.exists("coalData") then
  93.         local file = fs.open("coalData", "r")
  94.         local data = file.readAll()
  95.         file.close()
  96.         turtleState = textutils.unserialize(data)
  97.     else
  98.         local file = fs.open("coalData", "w")
  99.         file.write(textutils.serialize(turtleState))
  100.         file.close()
  101.     end
  102. end
  103.  
  104. --[[Saves the turtleState data to a file]]
  105. local function SavePosition()
  106.     local file = fs.open("coalData", "w")
  107.     file.write(textutils.serialize(turtleState))
  108.     file.close()
  109. end
  110.  
  111. --[[Goes Forward. Saves change in cordinate]]
  112. local function Forward()
  113.     turtle.forward()
  114.     if turtleState["rot"] == 1 then
  115.         turtleState["x"] = turtleState["x"] + 1
  116.     elseif turtleState["rot"] == 3 then
  117.         turtleState["x"] = turtleState["x"] - 1
  118.     elseif turtleState["rot"] == 0 then
  119.         turtleState["y"] = turtleState["y"] - 1
  120.     elseif turtleState["rot"] == 2 then
  121.         turtleState["y"] = turtleState["y"] + 1
  122.     end
  123.     SavePosition()
  124. end
  125.  
  126. --[[Turns right. Saves change in cordinate]]
  127. local function Right()
  128.     turtle.turnRight()
  129.     turtleState["rot"] = turtleState["rot"] + 1
  130.     if turtleState["rot"] > 4 then turtleState["rot"] = 0 end
  131.     SavePosition()
  132. end
  133.  
  134. --[[Turns left. Saves change in cordinate]]
  135. local function Left()
  136.     turtle.turnLeft()
  137.     turtleState["rot"] = turtleState["rot"] - 1
  138.     if turtleState["rot"] < 0 then turtleState["rot"] = 4 end
  139.     SavePosition()
  140. end
  141.  
  142. --[[Goes back. Saves change in cordinate]]
  143. local function Back()
  144.     turtle.back()
  145.     if turtleState["rot"] == 1 then
  146.         turtleState["x"] = turtleState["x"] - 1
  147.     elseif turtleState["rot"] == 3 then
  148.         turtleState["x"] = turtleState["x"] + 1
  149.     elseif turtleState["rot"] == 0 then
  150.         turtleState["y"] = turtleState["y"] + 1
  151.     elseif turtleState["rot"] == 2 then
  152.         turtleState["y"] = turtleState["y"] - 1
  153.     end
  154.     SavePosition()
  155. end
  156.  
  157. --[[Empties the inventory into a chest above the turtle]]
  158. local function Empty()
  159.     if not done then NewLine("Emptying Inventory") end
  160.     for i = 1, 16 do
  161.         if i == flintSlot or i == woodSlot then
  162.         else
  163.             turtle.select(i)
  164.             if not turtle.dropUp() and (turtle.getItemCount() > 0) then
  165.                 NewLine("Chest is full!")
  166.                 done = true
  167.                 break
  168.             end
  169.         end
  170.     end
  171.     turtle.select(1)
  172. end
  173.  
  174. --[[Checks the fuel level, if below required level, returns false]]
  175. local function CheckFuel()
  176.     return turtle.getFuelLevel() > min_fuel
  177. end
  178.  
  179. local function CheckEmpty(bool)
  180.     local invItemNumber = 0
  181.     NewLine("Counting Items...")
  182.     for i = 1, 16 do
  183.         if bool and (i == flintSlot or i == woodSlot) then
  184.         else
  185.             turtle.select(i)
  186.             invItemNumber = invItemNumber + turtle.getItemCount(i)
  187.         end
  188.     end
  189.     turtle.select(1)
  190.     return invItemNumber < 1
  191. end
  192.  
  193. local function CheckFlint()
  194.     return turtle.getItemCount(flintSlot) > 0
  195. end
  196.  
  197. local function GetFlint()
  198.     Right()
  199.     turtle.select(flintSlot)
  200.     turtle.suck(1)
  201.     turtle.select(1)
  202.     Left()
  203.     return turtle.getItemCount(flintSlot) > 0
  204. end
  205.  
  206.  
  207. --[[Replaces blocks above and below]]
  208. local function ReplaceBlocks()
  209.     turtle.select(1)
  210.     turtle.digDown()
  211.     turtle.digUp()
  212.     turtle.select(woodSlot)
  213.     turtle.placeDown()
  214.     turtle.placeUp()
  215.     turtle.select(1)
  216. end
  217.  
  218. --[[Goes through the oven and replaces all the ash with new wood]]
  219. local function ResetOven()
  220.     NewLine("Replacing Ash/Wood")
  221.     if turtle.getItemCount(woodSlot) < 64 then
  222.         turtle.select(woodSlot)
  223.         turtle.suckDown(64 - turtle.getItemCount())
  224.         if turtle.getItemCount() < 27 then
  225.             NewLine("Not enough Wood Piles!!\nStopping...")
  226.             done = true
  227.             return false
  228.         end
  229.     end
  230.     turtle.select(brickSlot)
  231.     turtle.dig()
  232.     Forward()
  233.     turtle.dig()
  234.     Forward()
  235.     for i = 1, 3 do
  236.         ReplaceBlocks()
  237.         Left()
  238.         turtle.dig()
  239.         Forward()
  240.         ReplaceBlocks()
  241.         Back()
  242.         turtle.select(woodSlot)
  243.         turtle.place()
  244.         turtle.select(1)
  245.         Right()
  246.         Right()
  247.         turtle.dig()
  248.         Forward()
  249.         ReplaceBlocks()
  250.         Back()
  251.         turtle.select(woodSlot)
  252.         turtle.place()
  253.         turtle.select(1)
  254.         Left()
  255.         if i < 3 then
  256.             turtle.dig()
  257.             Forward()
  258.         end
  259.     end
  260.     for i = 1, 3 do
  261.         Back()
  262.         turtle.select(woodSlot)
  263.         turtle.place()
  264.     end
  265.     Back()
  266.     turtle.select(flintSlot)
  267.     turtle.place()
  268.     sleep(lightTime)
  269.     turtle.select(brickSlot)
  270.     turtle.place()
  271.     turtle.select(1)
  272.     return true
  273. end
  274.  
  275. -- ===========STARTUP SEQUENCE==========
  276. NewLine("Checking Fuel & Position...")
  277. --if turtle.getFuelLevel() > 20 then
  278. CheckPosition()
  279. --end
  280. if not CheckFuel() then
  281.     NewLine("Turtle needs to be primed with fuel!")
  282.     done = true
  283.     return
  284. end
  285. Empty()
  286. --if CheckEmpty() then
  287. --    NewLine("Inventory Empty!")
  288. --    done = true
  289. --    return
  290. --end
  291. -- ===========STARTUP SEQUENCE END======
  292.  
  293. -- ===========MAIN LOOP=================
  294. while not done do
  295.     if not CheckFlint() then
  296.         if not GetFlint() then
  297.             NewLine("Out of Flint & Steel!")
  298.             done = true
  299.             break
  300.         end
  301.     end
  302.     if not ResetOven() then
  303.         break
  304.     end
  305.     if not CheckFuel() then
  306.         NewLine("Refueling...")
  307.         while not CheckFuel() do
  308.             for i = 1, 2 do
  309.                 turtle.select(i)
  310.                 turtle.refuel(1)
  311.             end
  312.         end
  313.     end
  314.     Empty()
  315.     for i = 1, waitTime do
  316.         NewLine("Waiting For BurnCycle...\n" .. ((waitTime - i) * 10 + 10) .. "s")
  317.         sleep(10)
  318.     end
  319. end
  320. -- ===========MAIN LOOP END=============
  321.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement