Inksaver

Kelp farm auto-smelter

Jan 22nd, 2024 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.26 KB | Source Code | 0 0
  1. local version = 20240122.1500
  2. -- https://www.youtube.com/watch?v=i-eBj0qLmRU
  3. --[[
  4.     |S|T|C|     S = smoker, T = turtle, C = chest/barrel (for crafted dried kelp blocks)
  5.     |H|C|       H = hopper, C= single or double chest (could also use barrel
  6.     Script for turtle placed next to a smoker / furnace
  7.     SuckDown output from smoker which is a chest below
  8.     Craft dried kelp into dried kelp blocks
  9.     if fuel required place into smoker on the right
  10.     else rotate 180 and place in chest on the left
  11. ]]
  12. local storage = 0
  13. local backup = false
  14. local totalStored = 0
  15. local slots = {1,2,3,5,6,7,9,10,11}
  16.  
  17. local function isItemOfType(direction, itemTable)
  18.     -- check if item in front of turtle is a specific type
  19.     local success, data = false, nil
  20.     if direction == "up" then
  21.         success, data= turtle.inspectUp()
  22.     elseif direction == "down" then
  23.         success, data= turtle.inspectDown()
  24.     else
  25.         success, data= turtle.inspect()                 -- check if anything in front
  26.     end
  27.     if success then                                     -- a block is in front
  28.         if type(itemTable) == "table" then              -- Are we checking for multiple items eg {"chest", "barrel"} ?
  29.             for _, item in ipairs(itemTable) do         -- iterate items
  30.                 if data.name:find(item) ~= nil then     -- match found
  31.                     return true
  32.                 end
  33.             end
  34.         elseif type(itemTable) == "string" then         -- checking for single item
  35.             if data.name:find(itemTable) ~= nil then    -- match found
  36.                 return true
  37.             end
  38.         end
  39.     end
  40.     return false                                        -- no match
  41. end
  42.  
  43. local function getStorage()
  44.     -- turtle has already been aligned with smoker/furnace. Now find chest/barrel
  45.     -- use no. of turns to base future rotations to face storage
  46.     print("Locating storage")
  47.     backup = isItemOfType("up", {"chest", "barrel"})    -- check for fuel backup storage
  48.     if backup then
  49.         print("Found backup fuel supply above")
  50.     end
  51.    
  52.     local turns = 0
  53.     repeat
  54.         if isItemOfType("forward", {"chest", "barrel"}) then    -- look for chest or barrel
  55.             storage = turns                             -- storage = current no of turns
  56.         else
  57.             turtle.turnRight()
  58.             turns = turns + 1
  59.         end
  60.     until storage > 0 or turns == 4                     -- limit to 4 in case no storage present
  61.    
  62.     if storage > 0 then                                 -- has the turtle rotated?
  63.         for i = 1, storage do                           -- rotate in opposite direction until facing smelter
  64.             turtle.turnLeft()
  65.         end
  66.     end
  67. end
  68.  
  69. local function orientate() 
  70.     print("Checking position")
  71.     while not isItemOfType("forward", {"smoker", "furnace"}) do -- true if facing a smoker or furnace
  72.         turtle.turnRight()                              -- not facing smoker so turn
  73.     end
  74. end
  75.  
  76. local function craftKelp()
  77.     turtle.select(16)                                   -- select a slot out of crafting area
  78.     turtle.suckDown()                                   -- remove items from chest below (dried kelp)
  79.     local quantity = turtle.getItemCount(16)            -- count items obtained
  80.     print(quantity.. " dried kelp found")
  81.     if quantity >= 9 then                               -- need min 9 to craft
  82.         local amount = math.floor(quantity / 9)         -- calculate how many to put in each of 9 slots
  83.         for slot = 1, 9 do
  84.             turtle.transferTo(slots[slot], amount)      -- fill slots with same amount each
  85.         end
  86.         turtle.dropDown()                               -- drop remaining back into chest
  87.         turtle.select(1)                                -- put crafted items in slot 1
  88.         if turtle.craft() then
  89.             return true
  90.         end
  91.     end
  92.     return false
  93. end
  94.  
  95. local function needsFuel()
  96.     -- check if smelter is in 'lit' state.
  97.     -- attempt to remove fuel from smelter if not lit
  98.     local success, data = turtle.inspect()
  99.     if success then                                         -- smelter found
  100.         if data.name:find("smoker") ~= nil or data.name:find("furnace") ~= nil then
  101.             if not data.state.lit then                      -- if not lit, check for unused fuel
  102.                 turtle.select(2)                            -- use spare slot
  103.                 if turtle.suck() then                       -- fuel present
  104.                     turtle.drop(1)                          -- replace fuel
  105.                     turtle.select(1)                        -- reset selected slot to 1
  106.                     return false
  107.                 end
  108.                 turtle.select(1)                            -- not lit, no fuel = refuel
  109.                 return true
  110.             end
  111.         end
  112.     end
  113.     turtle.select(1)
  114.     return false
  115. end
  116.  
  117. local function addFuelBackup()
  118.     -- make sure backup fuel store has 3 blocks in it
  119.     local retValue = false
  120.     turtle.select(2)                                        -- select empty slot
  121.     turtle.suckUp()                                         -- remove any stored fuel
  122.     print("Checking fuel backup")
  123.     if turtle.getItemCount(2) < 3 then                      -- check how much fuel was in storage
  124.         turtle.select(1)                                    -- add 1 more block from recently crafted
  125.         turtle.dropUp(1)
  126.     end
  127.     turtle.select(2)                                        -- replace blocks removed for counting
  128.     turtle.dropUp()
  129.    
  130.     return turtle.getItemCount(1)                           -- return any remaining blocks
  131. end
  132.  
  133. local function run()
  134.     -- infinite loop to constantly attend to kelp farm smelter
  135.     local slots = {1, 2, 3, 5, 6, 7, 9, 10, 11}             -- translate turtle 16 slots into equivalent crafting slots
  136.     while true do                                           -- infinite loop
  137.         if craftKelp() then                                 -- try crafting kelp block
  138.             -- at least 1 block crafted
  139.             if needsFuel() then                             -- check if needs fuel
  140.                 print("Refuelling smelter")
  141.                 turtle.select(1)
  142.                 turtle.drop(1)                              -- drop crafted blocks into smoker fuel slot
  143.             end
  144.             local count = turtle.getItemCount(1)
  145.             if count > 0 then                               -- spare dried kelp blocks available
  146.                 count = addFuelBackup()
  147.                 if count > 0 then                           -- still some kelp left
  148.                     for turns = 1, storage do
  149.                         turtle.turnRight()                  -- face storage
  150.                     end
  151.                     print("Storing "..count.." excess dried kelp blocks")
  152.                     totalStored = totalStored + count
  153.                     turtle.select(1)
  154.                     turtle.drop()                           -- drop into storage
  155.                     for turns = 1, storage do
  156.                         turtle.turnLeft()                   -- face smelter
  157.                     end
  158.                 end
  159.             end
  160.         else                                                -- not crafted: could be insufficient kelp or out of fuel
  161.             print("Waiting 10 seconds for further smelting")
  162.             sleep(10)
  163.             if craftKelp() then
  164.                 print("Refuelling smelter 2nd attempt")
  165.                 turtle.drop(1)                              -- drop crafted blocks into smoker fuel slot
  166.             else
  167.                 if backup then
  168.                     print("Refuelling smelter from backup")
  169.                     turtle.select(1)
  170.                     turtle.suckUp(1)
  171.                     turtle.drop()
  172.                 end
  173.             end
  174.         end
  175.  
  176.         for time = 60, 1, -1 do                         -- wait 1 minute
  177.             term.clear()
  178.             term.setCursorPos(1, 1)
  179.             print("Stored so far: "..totalStored.." blocks")
  180.             print("Next kelp craft cycle: "..time.." secs")
  181.             sleep(1)
  182.         end        
  183.     end
  184. end
  185.  
  186. local function main()
  187.     orientate()
  188.     getStorage()
  189.     run()
  190. end
  191.  
  192. main()
Tags: ccTweaked
Add Comment
Please, Sign In to add comment