Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --File: /cattech/common.lua
- local lists = dofile("/cattech/lists.lua")
- logLevels = {
- off = {level = 0, screen = false, file = false, web = false},
- info = {level = 1, screen = true, file = false, web = true},
- error = {level = 2, screen = true, file = false, web = true},
- warn = {level = 3, screen = true, file = false, web = true},
- debug = {level = 4, screen = true, file = false, web = true},
- all = {level = 999, screen = true, file = false, web = true},
- }
- local logFile = "/log.txt"
- local logWeb = "https://pawz.cattech.org/~posicat/turtlelog/"
- logLevel = logLevels["all"].level
- function urlEncode(str)
- if (str) then
- str = string.gsub(str, "([^%w%-%.%_%~])", -- Only allow alphanumeric characters and few others
- function(c)
- return string.format("%%%02X", string.byte(c))
- end)
- end
- return str
- end
- function log(level, message)
- local logLev = logLevels[level].level
- --print (level .. " " .. tostring(logLev) .. "<=?" .. tostring(logLevel))
- local id = os.getComputerID()
- local name = os.getComputerLabel() or 'unnamed'
- local time = textutils.formatTime(os.time(), true)
- local logEntry = string.format("[%s] [%s] [%s] %s\n", level, time, name .. "(" .. id .. ")", message)
- if (logLev <= logLevel) then
- if (logLevels[level].web) then
- local params = {
- id = tostring(id),
- msg = logEntry
- }
- local url = logWeb .. "?id=" .. id .. "&name=" .. name .. "&entry=" .. urlEncode(logEntry)
- local response = http.get(url)
- end
- if (logLevels[level].file) then
- local file = fs.open(logFile , "a")
- file.write(logEntry)
- file.close()
- end
- if (logLevels[level].screen) then
- print(level,":",message)
- end
- end
- end
- function updateLine(msg)
- local x, y = term.getCursorPos()
- if y > 0 then
- y=y-1
- end
- term.setCursorPos(1, y)
- term.clearLine()
- print(msg)
- end
- function pause(msg)
- print(msg)
- print("Press any key.")
- os.pullEvent("key")
- end
- function isBlockInList(blockDetails,itemList)
- if blockDetails and blockDetails.name then
- local blockName = blockDetails.name -- Get the block's name
- for _, item in ipairs(itemList) do
- if blockName == item then
- return true
- end
- end
- end
- return false
- end
- function isLiquid(blockDetails)
- return(isBlockInList(blockDetails,lists.liquidBlocks))
- end
- function isAir(blockDetails)
- return(blockDetails.name == "minecraft:air")
- end
- function refuelIfNeeded(minFuelLevel)
- local fuelLevel = turtle.getFuelLevel()
- while fuelLevel < minFuelLevel do
- local refueled = 0
- for i = 1, 16 do
- turtle.select(i) -- Select the current slot
- if turtle.getItemCount() > 0 then -- Check if the slot has an item
- if turtle.refuel(1) then -- Attempt to refuel with 1 item
- fuelLevel = turtle.getFuelLevel()
- refueled = 1
- end
- end
- end
- if refueled == 0 then
- pause("Fuel not found!")
- end
- end
- end
- function selectTurtleItemByList(itemList)
- for _, desiredItem in ipairs(itemList) do
- for slot = 1, 16 do
- turtle.select(slot) -- Select the slot
- local itemDetails = turtle.getItemDetail() -- Get details of the item in the slot
- if itemDetails and itemDetails.name == desiredItem then
- log("debug","Found item: " .. itemDetails.name .. " in slot " .. slot)
- return itemDetails
- end
- end
- end
- return nil
- end
- function placeItemByList(itemList,direction)
- local placeItem = selectTurtleItemByList(itemList)
- direction = direction or "forward"
- if (placeItem == nil) then
- print("Could not find item in list to place")
- return false
- else
- log("debug","Placing " .. placeItem.name)
- local success
- if (direction == "forward") then
- success = turtle.place()
- end
- if (direction == "down") then
- success = turtle.placeDown()
- end
- if (direction == "up") then
- success = turtle.placeUp()
- end
- if success then
- log("debug","Placed " .. placeItem.name)
- return true
- else
- print("Place for " .. placeItem.name .. " failed")
- return false
- end
- end
- return false
- end
- local inventory_totals = {}
- function scanInventory()
- local inventory_totals = {}
- inventory_totals["empty"] = 0
- for i = 1, 16 do
- turtle.select(i) -- Select slot i
- local item = turtle.getItemDetail()
- local name
- local count
- if item then
- name = item.name
- count = item.count
- else
- name = "empty"
- count = 1
- end
- if inventory_totals[name] then
- inventory_totals[name] = inventory_totals[name] + count
- else
- inventory_totals[name] = count
- end
- end
- return inventory_totals
- end
- -- Search blocks around the Turtle to see if any need to be secured.
- function scanForNeedToSecure()
- local secure = 0
- local success,blockData = turtle.inspectDown()
- if isLiquid(blockData) or (not success) then
- secure = secure + 1
- end
- success,blockData = turtle.inspectUp()
- if isLiquid(blockData) then
- secure = secure + 1
- end
- for i=1,4 do
- turtle.turnLeft()
- success,blockData = turtle.inspect()
- if isLiquid(blockData) then
- secure = secure + 1
- end
- end
- return secure
- end
- function dropCommonItems(inv)
- for slot = 1, 16 do -- Turtle has 16 inventory slots
- turtle.select(slot) -- Select the slot to examine
- local blockDetails = turtle.getItemDetail() -- Get item details in the current slot
- if blockDetails then -- Check if there is an item in this slot
- if isBlockInList(blockDetails,lists.junkItems ) then
- turtle.drop()
- print("Dropped junk item: " .. blockDetails.name)
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment