posicat

/cattech/common.lua

Sep 11th, 2024 (edited)
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.97 KB | None | 0 0
  1. --File: /cattech/common.lua
  2.  
  3. local lists = dofile("/cattech/lists.lua")
  4.  
  5. logLevels = {
  6.     off =   {level = 0, screen = false, file = false, web = false},
  7.     info =  {level = 1, screen = true, file = false, web = true},
  8.     error = {level = 2, screen = true, file = false, web = true},
  9.     warn =  {level = 3, screen = true, file = false, web = true},
  10.     debug = {level = 4, screen = true, file = false, web = true},
  11.     all =   {level = 999, screen = true, file = false, web = true},
  12. }
  13.  
  14. local logFile = "/log.txt"
  15. local logWeb = "https://pawz.cattech.org/~posicat/turtlelog/"
  16.  
  17. logLevel = logLevels["all"].level
  18.  
  19. function urlEncode(str)
  20.     if (str) then
  21.         str = string.gsub(str, "([^%w%-%.%_%~])", -- Only allow alphanumeric characters and few others
  22.             function(c)
  23.                 return string.format("%%%02X", string.byte(c))
  24.             end)
  25.     end
  26.     return str
  27. end
  28.  
  29.  
  30. function log(level, message)
  31.     local logLev = logLevels[level].level
  32.  
  33.     --print (level .. " " .. tostring(logLev) .. "<=?" .. tostring(logLevel))
  34.  
  35.     local id = os.getComputerID()
  36.     local name = os.getComputerLabel() or 'unnamed'
  37.     local time = textutils.formatTime(os.time(), true)
  38.     local logEntry = string.format("[%s] [%s] [%s] %s\n", level, time, name .. "(" .. id .. ")", message)
  39.  
  40.     if (logLev <= logLevel) then
  41.  
  42.         if (logLevels[level].web) then
  43.             local params = {
  44.                 id = tostring(id),
  45.                 msg = logEntry
  46.             }
  47.             local url = logWeb .. "?id=" .. id .. "&name=" .. name .. "&entry=" .. urlEncode(logEntry)  
  48.             local response = http.get(url)
  49.         end
  50.  
  51.         if (logLevels[level].file) then
  52.             local file = fs.open(logFile , "a")
  53.             file.write(logEntry)
  54.             file.close()
  55.         end
  56.  
  57.         if (logLevels[level].screen) then
  58.             print(level,":",message)
  59.         end
  60.     end
  61. end
  62.  
  63. function updateLine(msg)
  64.     local x, y = term.getCursorPos()
  65.     if y > 0 then
  66.         y=y-1
  67.     end
  68.     term.setCursorPos(1, y)
  69.     term.clearLine()
  70.     print(msg)
  71. end
  72.  
  73. function pause(msg)
  74.     print(msg)
  75.     print("Press any key.")
  76.     os.pullEvent("key")
  77. end
  78.  
  79. function isBlockInList(blockDetails,itemList)
  80.     if blockDetails and blockDetails.name then
  81.         local blockName = blockDetails.name -- Get the block's name
  82.         for _, item in ipairs(itemList) do
  83.             if blockName == item then
  84.                 return true
  85.             end
  86.         end
  87.     end
  88.     return false
  89. end
  90.  
  91. function isLiquid(blockDetails)
  92.     return(isBlockInList(blockDetails,lists.liquidBlocks))
  93. end
  94.  
  95. function isAir(blockDetails)
  96.     return(blockDetails.name == "minecraft:air")
  97. end
  98.  
  99. function refuelIfNeeded(minFuelLevel)
  100.     local fuelLevel = turtle.getFuelLevel()
  101.  
  102.     while fuelLevel < minFuelLevel do
  103.         local refueled = 0
  104.         for i = 1, 16 do
  105.             turtle.select(i)  -- Select the current slot
  106.             if turtle.getItemCount() > 0 then  -- Check if the slot has an item
  107.                 if turtle.refuel(1) then  -- Attempt to refuel with 1 item
  108.                     fuelLevel = turtle.getFuelLevel()
  109.                     refueled = 1
  110.                 end
  111.             end
  112.         end
  113.         if refueled == 0 then
  114.             pause("Fuel not found!")
  115.         end
  116.     end
  117. end
  118.  
  119. function selectTurtleItemByList(itemList)
  120.     for _, desiredItem in ipairs(itemList) do
  121.         for slot = 1, 16 do
  122.             turtle.select(slot) -- Select the slot
  123.             local itemDetails = turtle.getItemDetail() -- Get details of the item in the slot
  124.            
  125.             if itemDetails and itemDetails.name == desiredItem then
  126.                 log("debug","Found item: " .. itemDetails.name .. " in slot " .. slot)
  127.                 return itemDetails
  128.             end
  129.         end
  130.     end
  131.     return nil
  132. end
  133.  
  134. function placeItemByList(itemList,direction)
  135.     local placeItem = selectTurtleItemByList(itemList)
  136.     direction = direction or "forward"
  137.     if (placeItem == nil) then
  138.         print("Could not find item in list to place")
  139.         return false
  140.     else
  141.         log("debug","Placing " .. placeItem.name)
  142.         local success
  143.         if (direction == "forward") then
  144.             success = turtle.place()
  145.         end
  146.         if (direction == "down") then
  147.             success = turtle.placeDown()
  148.         end
  149.         if (direction == "up") then
  150.             success = turtle.placeUp()
  151.         end
  152.  
  153.         if success then
  154.             log("debug","Placed " .. placeItem.name)
  155.             return true
  156.         else
  157.             print("Place for " .. placeItem.name .. " failed")
  158.             return false
  159.         end
  160.     end
  161.     return false
  162. end
  163.  
  164. local inventory_totals = {}
  165.  
  166. function scanInventory()
  167.     local inventory_totals = {}
  168.  
  169.     inventory_totals["empty"] = 0
  170.  
  171.     for i = 1, 16 do
  172.         turtle.select(i)  -- Select slot i
  173.         local item = turtle.getItemDetail()
  174.        
  175.         local name
  176.         local count
  177.  
  178.         if item then
  179.             name = item.name
  180.             count = item.count
  181.         else
  182.             name = "empty"
  183.             count = 1
  184.         end
  185.        
  186.         if inventory_totals[name] then
  187.             inventory_totals[name] = inventory_totals[name] + count
  188.         else
  189.             inventory_totals[name] = count
  190.         end
  191.     end
  192.     return inventory_totals
  193. end
  194.  
  195. -- Search blocks around the Turtle to see if any need to be secured.
  196. function scanForNeedToSecure()
  197.     local secure = 0
  198.     local success,blockData = turtle.inspectDown()
  199.     if isLiquid(blockData) or (not success) then
  200.         secure = secure + 1
  201.     end
  202.  
  203.     success,blockData = turtle.inspectUp()
  204.     if isLiquid(blockData) then
  205.         secure = secure + 1
  206.     end
  207.  
  208.     for i=1,4 do
  209.         turtle.turnLeft()
  210.         success,blockData = turtle.inspect()
  211.         if isLiquid(blockData) then
  212.             secure = secure + 1
  213.         end
  214.     end
  215.     return secure
  216. end
  217.  
  218. function dropCommonItems(inv)
  219.     for slot = 1, 16 do  -- Turtle has 16 inventory slots
  220.         turtle.select(slot)  -- Select the slot to examine
  221.        
  222.         local blockDetails = turtle.getItemDetail()  -- Get item details in the current slot
  223.         if blockDetails then  -- Check if there is an item in this slot
  224.             if isBlockInList(blockDetails,lists.junkItems ) then
  225.                 turtle.drop()
  226.                 print("Dropped junk item: " .. blockDetails.name)
  227.             end
  228.         end
  229.     end
  230. end
Add Comment
Please, Sign In to add comment