Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- DefragBot v0.4 - Merged Version (Fixes + Mult command)
- Combines movement library and bot logic into a single file.
- Provides chat commands to test movement functions.
- Fixes:
- - Peripheral name for End Automata to "endAutomata".
- - Nil concatenation error during initialization log message.
- - Better handling of automata peripheral not being found.
- - Refined argument parsing for the 'cmd' subcommand.
- New Features:
- - Added 'mult' command for sequential basic movements (e.g., @defrag mult lffr).
- ]]
- --#region Configuration (Bot Specific)
- local CHAT_BOX_PERIPHERAL_NAME = "chatBox"
- local COMMAND_PREFIX = "@defrag"
- local CHAT_BOT_NAME = "DefragBot"
- local CHAT_BOT_BRACKETS = "[]"
- local CHAT_BOT_BRACKET_COLOR = "&3" -- Cyan
- --#endregion
- --#region Movement Library Code (Integrated)
- --##region Movement Library Configuration and State
- local AUTOMATA_PERIPHERAL_NAME = "endAutomata"
- local POSITION_FILE = "turtle_pos.json"
- local REFUEL_SLOT = 16
- local FUEL_ITEM_NAME_PART = "coal"
- local automata = nil
- local current_pos = { x = nil, y = nil, z = nil }
- local current_dir = nil
- local DIR_VECTORS = {
- [0] = {x = 0, y = 0, z = 1}, [1] = {x = 1, y = 0, z = 0},
- [2] = {x = 0, y = 0, z = -1}, [3] = {x = -1, y = 0, z = 0}
- }
- local DIR_NAMES = {
- [0] = "North (+Z)", [1] = "East (+X)",
- [2] = "South (-Z)", [3] = "West (-X)"
- }
- --##endregion
- --##region Movement Library Helper Functions (Internal)
- local function moveLog(message) print("[MoveLib] " .. message) end
- local function savePosition()
- if current_pos.x == nil or current_dir == nil then moveLog("Cannot save position, state is unknown."); return end
- local data = { x = current_pos.x, y = current_pos.y, z = current_pos.z, dir = current_dir }
- local file, err = fs.open(POSITION_FILE, "w")
- if file then
- file.write(textutils.serialiseJSON(data)); file.close()
- moveLog("Position saved: X:" .. data.x .. " Y:" .. data.y .. " Z:" .. data.z .. " Dir:" .. (DIR_NAMES[current_dir] or "Unknown"))
- else moveLog("Error saving position: " .. (err or "unknown")) end
- end
- local function loadPosition()
- if fs.exists(POSITION_FILE) then
- local file, err = fs.open(POSITION_FILE, "r")
- if file then
- local sData = file.readAll(); file.close()
- local success, data = pcall(textutils.unserialiseJSON, sData)
- if success and data and data.x ~= nil and data.y ~= nil and data.z ~= nil and data.dir ~= nil then
- current_pos.x = data.x; current_pos.y = data.y; current_pos.z = data.z; current_dir = data.dir
- moveLog("Position loaded: X:" .. current_pos.x .. " Y:" .. current_pos.y .. " Z:" .. current_pos.z .. " Dir:" .. (DIR_NAMES[current_dir] or "Unknown"))
- return true
- else moveLog("Failed to parse position file or data invalid: " .. tostring(data)) end
- else moveLog("Error opening position file: " .. (err or "unknown")) end
- else moveLog("Position file not found.") end
- return false
- end
- local function dirNameToNumber(name)
- name = string.lower(name or ""); if name == "n" or name == "north" then return 0
- elseif name == "e" or name == "east" then return 1 elseif name == "s" or name == "south" then return 2
- elseif name == "w" or name == "west" then return 3 end
- moveLog("Warning: Invalid direction name '" .. (name or "nil") .. "'. Defaulting to North."); return 0
- end
- --##endregion
- --##region Movement Library Core Movement and Action Functions
- local function turnLeft()
- if current_dir == nil then moveLog("Cannot turn: direction unknown."); return false end
- if turtle.turnLeft() then
- current_dir = (current_dir - 1 + 4) % 4
- moveLog("Turned Left. New Dir: " .. (DIR_NAMES[current_dir] or "Unknown")); savePosition(); return true
- else moveLog("Failed to turn left."); return false end
- end
- local function turnRight()
- if current_dir == nil then moveLog("Cannot turn: direction unknown."); return false end
- if turtle.turnRight() then
- current_dir = (current_dir + 1) % 4
- moveLog("Turned Right. New Dir: " .. (DIR_NAMES[current_dir] or "Unknown")); savePosition(); return true
- else moveLog("Failed to turn right."); return false end
- end
- local function turnAround()
- if current_dir == nil then moveLog("Cannot turn around: direction unknown."); return false end
- moveLog("Turning around..."); if turnRight() and turnRight() then
- moveLog("Turned Around. New Dir: " .. (DIR_NAMES[current_dir] or "Unknown")); return true
- else moveLog("Failed to turn around completely."); return false end
- end
- local function forward()
- if current_pos.x == nil then moveLog("Cannot move: position unknown."); return false end
- if turtle.forward() then
- local vec = DIR_VECTORS[current_dir]
- current_pos.x = current_pos.x + vec.x; current_pos.y = current_pos.y + vec.y
- current_pos.z = current_pos.z + vec.z
- moveLog("Moved Forward. New Pos: X:" .. current_pos.x .. " Y:" .. current_pos.y .. " Z:" .. current_pos.z); savePosition(); return true
- else moveLog("Failed to move forward (blocked)."); return false end
- end
- local function back()
- if current_pos.x == nil then moveLog("Cannot move: position unknown."); return false end
- if turtle.back() then
- local vec = DIR_VECTORS[current_dir]
- current_pos.x = current_pos.x - vec.x; current_pos.y = current_pos.y - vec.y
- current_pos.z = current_pos.z - vec.z
- moveLog("Moved Back. New Pos: X:" .. current_pos.x .. " Y:" .. current_pos.y .. " Z:" .. current_pos.z); savePosition(); return true
- else moveLog("Failed to move back (blocked)."); return false end
- end
- local function up()
- if current_pos.y == nil then moveLog("Cannot move up: Y position unknown."); return false end
- if turtle.up() then
- current_pos.y = current_pos.y + 1
- moveLog("Moved Up. New Pos: X:" .. current_pos.x .. " Y:" .. current_pos.y .. " Z:" .. current_pos.z); savePosition(); return true
- else moveLog("Failed to move up (blocked or no fuel)."); return false end
- end
- local function down()
- if current_pos.y == nil then moveLog("Cannot move down: Y position unknown."); return false end
- if turtle.down() then
- current_pos.y = current_pos.y - 1
- moveLog("Moved Down. New Pos: X:" .. current_pos.x .. " Y:" .. current_pos.y .. " Z:" .. current_pos.z); savePosition(); return true
- else moveLog("Failed to move down (blocked)."); return false end
- end
- local function home()
- if not automata then automata = peripheral.find(AUTOMATA_PERIPHERAL_NAME)
- if not automata then moveLog("Error: End Automata ('"..AUTOMATA_PERIPHERAL_NAME.."') not found."); return false end
- end
- moveLog("Attempting to warp to 'home'...")
- local success, result = pcall(function() return automata.warpToPoint("home") end)
- if success and result then
- moveLog("Warped to home successfully.")
- current_pos.x = 0; current_pos.y = 0; current_pos.z = 0; current_dir = 0
- moveLog("Position reset to home (0,0,0,N)."); savePosition(); return true
- else moveLog("Failed to warp to home: " .. tostring(result or "pcall error or peripheral method failed")); return false end
- end
- local function refuel()
- moveLog("Starting refuel process..."); local initialFuel = turtle.getFuelLevel()
- moveLog("Initial fuel level: " .. initialFuel); turtle.select(REFUEL_SLOT)
- local fuelInSlot = turtle.getItemCount(REFUEL_SLOT)
- if fuelInSlot > 0 then moveLog("Fuel in slot " .. REFUEL_SLOT .. ": " .. fuelInSlot) end
- local suckedCount = 0; local maxStackSize
- local currentSpace = turtle.getItemSpace(REFUEL_SLOT)
- if currentSpace == 0 and fuelInSlot > 0 then maxStackSize = 64 - fuelInSlot
- elseif currentSpace == 0 and fuelInSlot == 0 then maxStackSize = 64
- else maxStackSize = currentSpace end
- moveLog("Sucking fuel. Slot " .. REFUEL_SLOT .. ". Space: " .. maxStackSize)
- while turtle.getItemSpace(REFUEL_SLOT) > 0 do
- local itemDetail = turtle.getItemDetail(REFUEL_SLOT)
- if itemDetail and not string.find(string.lower(itemDetail.name), FUEL_ITEM_NAME_PART) then
- moveLog("Item in slot " .. REFUEL_SLOT .. " (" .. itemDetail.name .. ") not fuel. Stop."); break
- end
- if turtle.suck() then
- suckedCount = suckedCount + 1; -- moveLog("Sucked 1. Total: " .. suckedCount) -- Reduce log spam
- if turtle.getItemCount(REFUEL_SLOT) >= 64 then moveLog("Slot " .. REFUEL_SLOT .. " full."); break end
- else moveLog("Failed to suck/chest empty. Sucked: " .. suckedCount); break end
- sleep(0.1)
- end
- if suckedCount > 0 then moveLog("Total sucked: " .. suckedCount) end
- turtle.select(REFUEL_SLOT)
- if turtle.getItemCount(REFUEL_SLOT) > 0 then
- moveLog("Refueling from slot " .. REFUEL_SLOT .. "...")
- if turtle.refuel(0) then moveLog("Successfully refueled.")
- else moveLog("Refuel command failed/no fuel items in slot " .. REFUEL_SLOT .. ".") end
- else moveLog("No fuel items in slot " .. REFUEL_SLOT .. " to refuel.") end
- moveLog("Refuel finished. Fuel: " .. turtle.getFuelLevel())
- return turtle.getFuelLevel() == "unlimited" or turtle.getFuelLevel() > initialFuel
- end
- local function setPos(x, y, z, dir_name_or_num)
- if type(x)~="number" or type(y)~="number" or type(z)~="number" then moveLog("Err: setPos xyz num."); return false end
- current_pos.x = x; current_pos.y = y; current_pos.z = z
- if type(dir_name_or_num) == "number" then current_dir = dir_name_or_num % 4
- else current_dir = dirNameToNumber(dir_name_or_num) end
- moveLog("Pos set: X:"..x.." Y:"..y.." Z:"..z.." Dir:"..(DIR_NAMES[current_dir] or "Unk")); savePosition(); return true
- end
- --##endregion
- --##region Movement Library Advanced Movement (Pathfinding)
- local function turnToDir(target_dir_num)
- if current_dir == nil then moveLog("Cannot turnToDir: dir unknown."); return false end
- if current_dir == target_dir_num then return true end
- moveLog("Turning to target dir: " .. (DIR_NAMES[target_dir_num] or "Unk"))
- local diff = (target_dir_num - current_dir + 4) % 4
- if diff == 1 then return turnRight() elseif diff == 2 then return turnAround()
- elseif diff == 3 then return turnLeft() end; return false
- end
- local function moveTo(targetX, targetY, targetZ, targetDir_name_or_num)
- if current_pos.x == nil then moveLog("Cannot moveTo: current pos unknown."); return false end
- moveLog(string.format("MoveTo: Target X:%s Y:%s Z:%s Dir:%s", tostring(targetX),tostring(targetY),tostring(targetZ),tostring(targetDir_name_or_num)))
- local target_dir_num
- if type(targetDir_name_or_num)=="number" then target_dir_num=targetDir_name_or_num%4 else target_dir_num=dirNameToNumber(targetDir_name_or_num) end
- local attempts=0; local max_total_attempts=100; local max_stuck_attempts_per_axis=3
- while (current_pos.x~=targetX or current_pos.y~=targetY or current_pos.z~=targetZ) and attempts<max_total_attempts do
- attempts=attempts+1; local moved_this_cycle=false; local current_axis_stuck_attempts=0
- -- Y movement
- if current_pos.y < targetY then
- if up() then moved_this_cycle=true else
- current_axis_stuck_attempts=current_axis_stuck_attempts+1
- moveLog("moveTo: Blocked UP ("..current_axis_stuck_attempts..")")
- if (current_pos.x~=targetX or current_pos.z~=targetZ) and current_axis_stuck_attempts<max_stuck_attempts_per_axis then -- try other axes
- elseif current_axis_stuck_attempts<max_stuck_attempts_per_axis then if turnToDir(math.random(0,3)) and forward() then moved_this_cycle=true end
- else moveLog("moveTo: Stuck Y (up). Abort Y."); targetY=current_pos.y end
- end
- elseif current_pos.y > targetY then
- if down() then moved_this_cycle=true else
- current_axis_stuck_attempts=current_axis_stuck_attempts+1
- moveLog("moveTo: Blocked DOWN ("..current_axis_stuck_attempts..")")
- if (current_pos.x~=targetX or current_pos.z~=targetZ) and current_axis_stuck_attempts<max_stuck_attempts_per_axis then
- elseif current_axis_stuck_attempts<max_stuck_attempts_per_axis then if turnToDir(math.random(0,3)) and forward() then moved_this_cycle=true end
- else moveLog("moveTo: Stuck Y (down). Abort Y."); targetY=current_pos.y end
- end
- end
- if moved_this_cycle then goto continue_main_loop end; current_axis_stuck_attempts=0
- -- X movement
- if current_pos.x < targetX then
- if turnToDir(1) and forward() then moved_this_cycle=true else
- current_axis_stuck_attempts=current_axis_stuck_attempts+1; moveLog("moveTo: Blocked EAST ("..current_axis_stuck_attempts..")")
- if current_pos.z~=targetZ and current_axis_stuck_attempts<max_stuck_attempts_per_axis then
- elseif current_axis_stuck_attempts<max_stuck_attempts_per_axis then if turnToDir((math.random(0,1)*2)) and forward() then moved_this_cycle=true end
- else moveLog("moveTo: Stuck X (east). Abort X."); targetX=current_pos.x end
- end
- elseif current_pos.x > targetX then
- if turnToDir(3) and forward() then moved_this_cycle=true else
- current_axis_stuck_attempts=current_axis_stuck_attempts+1; moveLog("moveTo: Blocked WEST ("..current_axis_stuck_attempts..")")
- if current_pos.z~=targetZ and current_axis_stuck_attempts<max_stuck_attempts_per_axis then
- elseif current_axis_stuck_attempts<max_stuck_attempts_per_axis then if turnToDir((math.random(0,1)*2)) and forward() then moved_this_cycle=true end
- else moveLog("moveTo: Stuck X (west). Abort X."); targetX=current_pos.x end
- end
- end
- if moved_this_cycle then goto continue_main_loop end; current_axis_stuck_attempts=0
- -- Z movement
- if current_pos.z < targetZ then
- if turnToDir(0) and forward() then moved_this_cycle=true else
- current_axis_stuck_attempts=current_axis_stuck_attempts+1; moveLog("moveTo: Blocked NORTH ("..current_axis_stuck_attempts..")")
- if current_pos.x~=targetX and current_axis_stuck_attempts<max_stuck_attempts_per_axis then
- elseif current_axis_stuck_attempts<max_stuck_attempts_per_axis then if turnToDir((math.random(0,1)*2)+1) and forward() then moved_this_cycle=true end
- else moveLog("moveTo: Stuck Z (north). Abort Z."); targetZ=current_pos.z end
- end
- elseif current_pos.z > targetZ then
- if turnToDir(2) and forward() then moved_this_cycle=true else
- current_axis_stuck_attempts=current_axis_stuck_attempts+1; moveLog("moveTo: Blocked SOUTH ("..current_axis_stuck_attempts..")")
- if current_pos.x~=targetX and current_axis_stuck_attempts<max_stuck_attempts_per_axis then
- elseif current_axis_stuck_attempts<max_stuck_attempts_per_axis then if turnToDir((math.random(0,1)*2)+1) and forward() then moved_this_cycle=true end
- else moveLog("moveTo: Stuck Z (south). Abort Z."); targetZ=current_pos.z end
- end
- end
- if moved_this_cycle then goto continue_main_loop end
- if not moved_this_cycle then moveLog("moveTo: No move this cycle. Attempts: "..attempts); if current_axis_stuck_attempts>=max_stuck_attempts_per_axis then break end end
- ::continue_main_loop::
- sleep(0.05)
- end
- if attempts>=max_total_attempts then moveLog("moveTo: Max total attempts.") end
- if not turnToDir(target_dir_num) then moveLog("moveTo: Failed final turn.") end
- local success=current_pos.x==targetX and current_pos.y==targetY and current_pos.z==targetZ and current_dir==target_dir_num
- if success then moveLog("moveTo: Success.") else moveLog(string.format("moveTo: Finished. Pos X:%s Y:%s Z:%s Dir:%s.",tostring(current_pos.x),tostring(current_pos.y),tostring(current_pos.z),(DIR_NAMES[current_dir]or"Unk"))) end
- return success
- end
- --##endregion
- --##region Movement Library Initialization
- local function initMoveLibrary()
- moveLog("Initializing Integrated Move Library...")
- automata = peripheral.find(AUTOMATA_PERIPHERAL_NAME)
- if not automata then moveLog("CRIT WARN: End Automata ('"..AUTOMATA_PERIPHERAL_NAME.."') missing. Home warp fail.") end
- if not loadPosition() then
- moveLog("Pos unknown/load fail. Warp home & set default.")
- if home() then moveLog("Init at home success.")
- else moveLog("CRIT: Fail init at home. Pos unknown."); current_pos.x=nil;current_pos.y=nil;current_pos.z=nil;current_dir=nil end
- end
- moveLog("MoveLib Init. Pos: X:"..tostring(current_pos.x).." Y:"..tostring(current_pos.y).." Z:"..tostring(current_pos.z).." Dir:"..(current_dir and DIR_NAMES[current_dir]or"Unk"))
- end
- local function getPosition() return current_pos end
- local function getDirection() return current_dir end
- local function getDirectionName() return current_dir and DIR_NAMES[current_dir] or "Unknown" end
- local ml = {
- l=turnLeft,turnLeft=turnLeft,r=turnRight,turnRight=turnRight,f=forward,forward=forward,b=back,back=back,
- a=turnAround,turnAround=turnAround,u=up,up=up,d=down,down=down,h=home,home=home,
- refuel=refuel,setPos=setPos,m=moveTo,moveTo=moveTo,
- getPosition=getPosition,getDirection=getDirection,getDirectionName=getDirectionName,
- init=initMoveLibrary
- }
- initMoveLibrary()
- --##endregion End of Movement Library Code
- --#endregion End of Integrated Movement Library
- --#region DefragBot Peripherals and Helpers
- local chatBox = peripheral.find(CHAT_BOX_PERIPHERAL_NAME)
- local function botLog(message) print("["..CHAT_BOT_NAME.."-Debug] "..message) end
- local function sendFormattedChat(messageComponents, recipientUsername)
- if not chatBox then local pt="";for _,c in ipairs(messageComponents)do pt=pt..(c.text or"")end;print("["..CHAT_BOT_NAME.."-NoChat] "..pt);return end
- local jm=textutils.serialiseJSON(messageComponents)
- if recipientUsername then chatBox.sendFormattedMessageToPlayer(jm,recipientUsername,CHAT_BOT_NAME,CHAT_BOT_BRACKETS,CHAT_BOT_BRACKET_COLOR)
- else chatBox.sendFormattedMessage(jm,CHAT_BOT_NAME,CHAT_BOT_BRACKETS,CHAT_BOT_BRACKET_COLOR) end; sleep(0.2)
- end
- local function announce(mc) sendFormattedChat(mc) end
- local COLORS={GOLD="gold",AQUA="aqua",GRAY="gray",RED="red",GREEN="green",YELLOW="yellow",WHITE="white"}
- --#endregion
- --#region DefragBot Command Handlers
- local commandHandlers = {}
- commandHandlers.help = function(username, _)
- announce({{text="--- DefragBot Commands ("..COMMAND_PREFIX..") ---",color=COLORS.GOLD,bold=true}})
- announce({{text=COMMAND_PREFIX.." help",color=COLORS.AQUA},{text=" - Shows this help.",color=COLORS.GRAY}})
- announce({{text=COMMAND_PREFIX.." cmd <ml.func_call_string>",color=COLORS.AQUA},{text=" - Executes a movement function.",color=COLORS.GRAY}})
- announce({{text=" Ex: "..COMMAND_PREFIX.." cmd ml.f()",color=COLORS.GRAY}})
- announce({{text=" Ex: "..COMMAND_PREFIX.." cmd ml.moveTo(10,0,0,'N')",color=COLORS.GRAY}})
- announce({{text=COMMAND_PREFIX.." mult <sequence>",color=COLORS.AQUA},{text=" - Executes sequence of l,r,f,b,u,d,a.",color=COLORS.GRAY}})
- announce({{text=" Ex: "..COMMAND_PREFIX.." mult lffr",color=COLORS.GRAY}})
- announce({{text=COMMAND_PREFIX.." pos",color=COLORS.AQUA},{text=" - Current position and direction.",color=COLORS.GRAY}})
- announce({{text=COMMAND_PREFIX.." fuel",color=COLORS.AQUA},{text=" - Current fuel level.",color=COLORS.GRAY}})
- end
- commandHandlers.cmd = function(username, args)
- if #args == 0 then announce({{text="Usage: "..COMMAND_PREFIX.." cmd <ml.function_call_string>",color=COLORS.YELLOW}});return end
- -- Correctly join all parts after "cmd" to form the command string
- local cmdStringParts = {}
- for i = 1, #args do -- Start from the first element of args
- table.insert(cmdStringParts, args[i])
- end
- local cmdString = table.concat(cmdStringParts, " ")
- botLog("User "..username.." trying to execute: "..cmdString)
- local func,err=load("return "..cmdString,"user_cmd","t",{ml=ml})
- if not func then announce({{text="Err parsing: ",color=COLORS.RED},{text=tostring(err),color=COLORS.YELLOW}});botLog("Parse err: "..tostring(err));return end
- local s,r=pcall(func)
- if s then announce({{text="Cmd exec. Result: ",color=COLORS.GREEN},{text=tostring(r),color=COLORS.WHITE}});botLog("Cmd result: "..tostring(r))
- else announce({{text="Err exec: ",color=COLORS.RED},{text=tostring(r),color=COLORS.YELLOW}});botLog("Exec err: "..tostring(r)) end
- end
- commandHandlers.mult = function(username, args)
- if #args ~= 1 or type(args[1]) ~= "string" then
- announce({{text="Usage: "..COMMAND_PREFIX.." mult <sequence_string>",color=COLORS.YELLOW}});
- announce({{text="Sequence can contain l,r,f,b,u,d,a. Example: lffr",color=COLORS.GRAY}}); return
- end
- local sequence = string.lower(args[1])
- botLog("User "..username.." requested multi-sequence: "..sequence)
- announce({{text="Executing sequence: ",color=COLORS.AQUA},{text=sequence,color=COLORS.WHITE}})
- local success = true
- for i = 1, #sequence do
- local moveChar = string.sub(sequence, i, i)
- local moveFunc = nil
- if moveChar == "l" then moveFunc = ml.l
- elseif moveChar == "r" then moveFunc = ml.r
- elseif moveChar == "f" then moveFunc = ml.f
- elseif moveChar == "b" then moveFunc = ml.b
- elseif moveChar == "u" then moveFunc = ml.u
- elseif moveChar == "d" then moveFunc = ml.d
- elseif moveChar == "a" then moveFunc = ml.a
- end
- if moveFunc then
- announce({{text="Executing: ",color=COLORS.GRAY},{text=moveChar,color=COLORS.YELLOW}})
- if not moveFunc() then
- announce({{text="Move '",color=COLORS.RED},{text=moveChar,color=COLORS.YELLOW},{text="' failed. Stopping sequence.",color=COLORS.RED}})
- success = false
- break
- end
- sleep(0.2) -- Small delay between multi-moves
- else
- announce({{text="Unknown move character '",color=COLORS.RED},{text=moveChar,color=COLORS.YELLOW},{text="' in sequence. Stopping.",color=COLORS.RED}})
- success = false
- break
- end
- end
- if success then announce({{text="Sequence finished.",color=COLORS.GREEN}}) end
- end
- commandHandlers.pos = function(username, _)
- local pD=ml.getPosition(); local dNS=ml.getDirectionName()
- if pD and pD.x~=nil then announce({{text="Pos: ",color=COLORS.AQUA},{text="X:"..tostring(pD.x).." Y:"..tostring(pD.y).." Z:"..tostring(pD.z),color=COLORS.WHITE},{text=" Facing: "..dNS,color=COLORS.WHITE}})
- else announce({{text="Position is unknown.",color=COLORS.YELLOW}}) end
- end
- commandHandlers.fuel = function(username, _)
- local l, lim = turtle.getFuelLevel(), turtle.getFuelLimit()
- announce({{text="Fuel: ",color=COLORS.AQUA},{text=tostring(l)..(lim>0 and (" / "..tostring(lim))or""),color=COLORS.WHITE}})
- end
- --#endregion
- --#region DefragBot Main Loop
- local function run()
- term.clear();term.setCursorPos(1,1); botLog("DefragBot initializing...")
- if not chatBox then botLog("WARN: ChatBox missing!");print("WARN: ChatBox missing!") end
- botLog(CHAT_BOT_NAME.." online. MoveLib init.")
- print(CHAT_BOT_NAME.." online. '"..COMMAND_PREFIX.." help' or '@all'.")
- if chatBox then announce({{text=CHAT_BOT_NAME.." online, defrag duties (test)!",color=COLORS.GREEN}}) end
- while true do
- local eventData={os.pullEvent()}
- local eventType=eventData[1]
- if eventType=="chat" then
- local eU,eM=eventData[2],eventData[3]
- if eM then
- if string.lower(eM)=="@all" then
- botLog("@all from "..eU); announce({{text="Use '",color=COLORS.GREEN},{text=COMMAND_PREFIX.." help",color=COLORS.AQUA},{text="' for my move test cmds.",color=COLORS.GREEN}})
- elseif string.sub(eM,1,#COMMAND_PREFIX)==COMMAND_PREFIX then
- botLog("Chat cmd from "..eU..": "..eM)
- local parts={};for part in string.gmatch(eM,"[^%s]+")do table.insert(parts,part)end
- local cmdName="";if parts[2]then cmdName=string.lower(parts[2])end
- -- Corrected argument collection for all commands
- local cmdArgs = {}
- for i = 3, #parts do table.insert(cmdArgs, parts[i]) end
- if commandHandlers[cmdName] then commandHandlers[cmdName](eU,cmdArgs)
- elseif cmdName~="" then announce({{text="Unk DefragBot cmd: '",color=COLORS.RED},{text=cmdName,color=COLORS.YELLOW},{text="'.",color=COLORS.RED}}) end
- end
- end
- elseif eventType=="terminate" then
- botLog("Terminate. Shutdown.");if chatBox then announce({{text=CHAT_BOT_NAME.." shut down.",color=COLORS.YELLOW}})end;return
- end
- end
- end
- run()
- --#endregion
Add Comment
Please, Sign In to add comment