Jameelo

Turtle Common Utility Functions

Nov 11th, 2023 (edited)
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.98 KB | Gaming | 0 0
  1. --[[
  2.     This file will be an API that holds general-purpose functions cuz I don't wanna have to re-write them across programs. Saves s+
  3.  
  4.     to include these function in a program, type:
  5.     os.loadAPI("myApiFile")
  6.     myApiFile.functionName()
  7.     Add an error function with infinite while loop & error message to ensure the robot doesn't go nuts
  8.  
  9.     DEPRECATED, I now will use the Lib files that are more recent cuz I wanted to split this file up more
  10. ]]
  11.  
  12. CHESTS = {"minecraft:chest", "minecraft:barrel", "minecraft:trapped_chest", "aether:treasure_chest", "twilightforest:twilight_oak_chest", "twilightforest:canopy_chest", "twilightforest:mangrove_chest",
  13. "twilightforest:dark_chest", "twilightforest:time_chest", "twilightforest:transformation_chest",
  14. "twilightforest:mining_chest", "twilightforest:sorting_chest"}
  15. ECHESTS = {"enderstorage:ender_chest", "minecraft:ender_chest", "enderchests:ender_chest"}
  16.  
  17. function apiExists(path) -- Check if an API is real or not
  18.     if os.loadAPI(path) ~= false then
  19.         return true
  20.     end
  21.     return false
  22. end
  23.  
  24. function dumpItems() -- Empty the inventory, prioritising ender chest usage
  25.     local chestIndex = findChest(ECHESTS) -- Get the index of the ender chest in the inventory, if it exists.
  26.     local eChest = false
  27.     local leftCount = 0
  28.  
  29.     if chestIndex > 0 then -- Echest Present
  30.         eChest = true
  31.     else
  32.         chestIndex = findChest(CHESTS)
  33.     end
  34.  
  35.     -- Direction determination
  36.     if chestIndex > 0 then
  37.         turtle.select(chestIndex)
  38.         if not turtle.detectUp() then
  39.             -- We can place the chest above us
  40.             turtle.placeUp()
  41.             emptyInv("up",eChest)
  42.             if eChest then
  43.                 turtle.digUp()
  44.             end
  45.             return
  46.         else
  47.             -- If you cannot place the chest above you
  48.             for _ = 1,4,1 do
  49.                 turtle.turnLeft()
  50.                 leftCount = leftCount + 1
  51.                 if not turtle.detect() then
  52.                     -- Can place the chest in this direction
  53.                     turtle.place()
  54.                     emptyInv("front",eChest)
  55.                     if eChest then
  56.                         turtle.dig()
  57.                     end
  58.                     for _ = 1,leftCount,1 do
  59.                         turtle.turnRight()
  60.                     end
  61.                     return
  62.                 end
  63.             end
  64.             shell.run("os.shutdown()")
  65.         end
  66.     else
  67.         -- If there are no chests whatsoever
  68.         printError("No chests available!",0)
  69.     end
  70. end
  71.  
  72. function emptyInv(direction, EChestPlaced) -- Dump everything except chests.
  73.     local directions = {up    = turtle.dropUp,
  74.                         down  = turtle.dropDown,
  75.                         front = turtle.drop}
  76.     local drop = true
  77.  
  78.     for n = 1,16,1 do -- for all inventory cells
  79.         drop = true
  80.         if turtle.getItemCount(n) ~= 0 then -- if the item count in this cell is more than zero
  81.             if not EChestPlaced and contains(CHESTS,turtle.getItemDetail(n).name) == true then -- If the item is a chest (also why do I need == true here??? Thanks Lua)
  82.                 drop = false
  83.             end
  84.             if drop then
  85.                 turtle.select(n)
  86.                 directions[direction]()
  87.             end
  88.         end
  89.     end
  90. end
  91.  
  92. function findChest(chestArray) -- loops through
  93.     local currChest
  94.     for _,chestID in pairs(chestArray) do
  95.         currChest = findItemBF(chestID)
  96.         if currChest > 0 then
  97.             return currChest
  98.         end
  99.     end
  100.     return 0
  101. end
  102.  
  103. function findItemBF(ID) -- brute force finds any item passed to it, otherwise returns 0
  104.     for n = 1,16,1 do
  105.         if turtle.getItemCount(n) ~= 0 then
  106.             if turtle.getItemDetail(n).name == ID then
  107.                 return n
  108.             end
  109.         end
  110.     end
  111.     return 0
  112. end
  113.  
  114. function refuelChestSafe() -- Refuel without comsuming any chests
  115.     local isRefueled
  116.     for index = 1,16,1 do
  117.         turtle.select(index)
  118.         local isFuel, _ = turtle.refuel(0) -- See if there's any fuel in this slot
  119.         if isFuel then
  120.             if not contains(CHESTS,turtle.getItemDetail(index).name) then
  121.                 isRefueled = turtle.refuel() -- Om nom nom
  122.             end
  123.         end
  124.     end
  125.     return isRefueled
  126. end
  127.  
  128. function contains(table,element) -- Check to see if element is in table
  129.     for _, value in pairs(table) do
  130.         if value == element then
  131.           return true
  132.         end
  133.     end
  134.     return false
  135. end
  136.  
  137. function digForward(length) -- Variable length dig forward command.
  138.     if length == nil then
  139.         length = 1 -- default
  140.     end
  141.     for _ = 1,length,1 do        
  142.         -- keep digging until nothing remains in front of you
  143.         repeat
  144.             turtle.dig()
  145.             os.sleep(0.1)
  146.         until not turtle.detect()
  147.  
  148.         turtle.forward()
  149.         if everySlotTaken() == true then
  150.             print("Storage full! Dumping items...")
  151.             dumpItems()
  152.         end
  153.     end
  154. end
  155.  
  156. function everySlotTaken()
  157.     --Cycle through all the slots and get the inventory size, if every cell has at least 1 item in it then there's no space left for new items
  158.     for n = 1,16,1 do
  159.         if turtle.getItemCount(n) == 0 then
  160.             return false
  161.         end
  162.     end
  163.     return true
  164. end
  165.  
  166. function dictLookup(dict,item) -- Checks to see if item is a key in dict, and return its value
  167.     for k,v in pairs(dict) do
  168.         if k == item then
  169.             return v
  170.         end
  171.     end
  172.     return false
  173. end
  174.  
  175. function placeMoveForward(length, block) -- places the currently selected block & moves forward.
  176.     --[[
  177.         True - returned as length was reached
  178.         False - returned due to lack of items
  179.     ]]
  180.  
  181.     for _ = 1,length,1 do        
  182.         if turtle.getItemCount() == 0 then -- if no blocks are left, reload.
  183.             newSlot = findItemBF(block)
  184.             if newSlot > 0 then -- if there is another stack in the inventory
  185.                 turtle.select(newSlot) -- select another instance of the block
  186.             else
  187.                 return false -- ran outta blocks
  188.             end
  189.         end
  190.  
  191.         if turtle.detect() then -- if there is an obstacle, even though there shouldn't be.
  192.             turtle.dig() -- This opens up the possibility of filling the inventory I guess? Not an issue atm.
  193.         end
  194.  
  195.         if turtle.detectDown() then
  196.             turtle.forward()
  197.         else
  198.             turtle.placeDown()
  199.             turtle.forward()
  200.         end
  201.     end
  202.  
  203.     return true
  204. end
  205.  
  206. function saveFile(table,saveFileID)
  207.     local tableString = textutils.serialize(table)
  208.     --save tableString to file
  209.     local tFile = fs.open(saveFileID, "w")
  210.     tFile.write(tableString)
  211.     tFile.close()
  212. end
  213.  
  214. function loadFile(fileID) -- read table from file
  215.     local tFile = fs.open(fileID, "r")
  216.     local fileContents = tFile.readAll()
  217.     local readTable = textutils.unserialize(fileContents)
  218.     return readTable
  219. end
Add Comment
Please, Sign In to add comment