Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------------------------------
- ---------|| Drone v.3.2 ||------------
- --------------------------------------
- --Requires a server 4 best experience!
- --------------------------------------
- --------------Notes-------------------
- --[[
- -------Controls and such--------
- ?0¢ Arrow up - Go up
- ?0¢ Arrow Down - Go down
- ?0¢ Enter - Main action
- ?0¢ Backspace - Secondary action
- ?0¢ 't' - Close program
- ?0¢ 'r' - Clear log file
- --------------------------------
- System pastebin code: 8jwwB9i4
- Drone pastebin code: AtWZRS07
- --Changes To Be Done--
- ?0¢ Be able to quarry and such without connection with system - offline
- --Changes Made--
- ?0¢ Implemented Bomb Job functionality
- ?0¢ Implemented Anti Mob functionality
- ?0¢ Implemented Toggle inventory functionality
- ?0¢ Inv. variables updates on status report
- ?0¢ Rewrote entire quarry function (MUCH smaller and easier to understand now)
- ?0¢ Made Quarry size changeable
- ?0¢ Made Depth of quarries changeable
- ?0¢ Added a proper GUI
- ?0¢ Updated and improved save&load functionality
- ?0¢ Made turtle verify coordinates each start up
- ?0¢ Made turtle ask for a job to do
- ?0¢ Added some delays to prevent 'too long without yielding'
- ?0¢ Made text change when updateTerm is false
- ?0¢ Fixed unresponsive turtle when starting program with not enough fuel
- ?0¢ Fixed an issue when starting turtle with autoRequest set to True
- ?0¢ Made variable-loader compatible with most versions from 3.1 and newer
- ?0¢ Select empty slot before sucking fuel from chest
- ?0¢ Added more 'sleep(0)' to prevent crashes
- ?0¢ Now checks if drop-of chest is full
- ?0¢ Will now give the turtle a chance to escape before mining a fellow turtle, if told to
- ?0¢ Will now dump cobble/dirt/gravel, if told to
- ?0¢ Will now generate a logfile for debug, if told to
- ?0¢ [BUG] Fixed random freezing in code
- ?0¢ Will now drop coal in fuel chest, if told to
- --Bug List--
- ?0¢ [MOD BUG] Sometimes just freezes and reboots while quarrying
- -Reason: Lag causes code to freeze and then terminate with an "Too long without yielding" error
- OR the chunk the turtle is currently in gets unloaded
- --]]
- -- North >> -Z
- -- South >> +Z
- -- East >> +X
- -- West >> -X
- -------------/Notes-------------------
- ---------Variables'n'stuff------------
- local TurtleID = 1 --Make sure be unused by others!
- local lowFuel = 1000 --At which point to head home
- local refuelAmount = 500 -- How much over lowFuel to refuel
- local unload = true --To empty inv. when refueling
- local updateTerm = true --Keep term with fresh variables(Heavier to process prog.!)
- local isWireless = true
- local isAggressive = true -- To attack entities in-front, under and over turtle if stuck
- local saveData = true
- local loadPriorData = true
- local supplyFuelChest = true
- local dumpCobblestone = false
- local dumpDirt = false
- local dumpGravel = false
- local checkIfTurtle = true
- local doDebugLog = false
- local preHomeX = 0 -- And these
- local preHomeZ = 0
- local preHomeY = 1
- local HomeX = 0 --Make sure to set these, as well as the below to the turtle position
- local HomeZ = 0
- local HomeY = 1
- local HomeFacing = 0
- local TravelY = 70 --What height for turtle to travel at (e.x To/From quarry)
- local TurtleX = 0 --( These are what I ment with 'below' )
- local TurtleZ = 0
- local TurtleY = 1
- local TurtleDirection = 0 -- Use the table below, 'Directions', as guide
- local Directions = {} --Do not edit !
- Directions[0] = "South" --Do not edit !
- Directions[1] = "West" --Do not edit !
- Directions[2] = "North" --Do not edit !
- Directions[3] = "East" --Do not edit !
- local TargetX = 0
- local TargetZ = 0
- local TargetY = 0
- local stName1 = ("Quarry Turtle 1 :: Fuel") --Name in statList
- local stMax1 = 20000 --Full bar in statList
- local stName2 = ("Quarry Turtle 1 :: Inventory") -- -||-
- local stMax2 = 16 -- -||-
- local turtleInvFull = 0
- local turtleInvExact = 0
- local Debug = false
- local currentlyDoing = "Nothing.."
- local keepGoing = true
- -- Job Variables
- local jobs = {"Quarry", "Bomb"}
- local curJob = 1
- local autoJobRequest = false
- local sentRequest = false
- -- Menu Variables
- local curMenu = 1
- local curSelection = 1
- local nextAction = ""
- local menus = {
- {"VariableSetup", {3, 12, "Next->", "menuVariable2Setup"}, {3, 4, ("Turtle ID: "..tostring(TurtleID)), "funEditTurtleID", "funClearTurtleID"}, {3,5, ("Travel Y: "..tostring(TravelY)), "funEditTravelY", "funClearTravelY"}, {3, 6, ("Low-Fuel Limit: "..tostring(lowFuel)), "funEditLowFuel", "funClearLowFuel"}, {3,7, (updateTerm==true and "Update Terminal: "..tostring(updateTerm).." (Slower)" or "Update Terminal: "..tostring(updateTerm).." (Faster)"), "funSwitchUpdateTerm"}, {3, 8, ("Is Wireless: "..tostring(isWireless)), "funSwitchIsWireless"}, {3, 9, ("Is Aggressive: "..tostring(isAggressive).." (Kill Entities)"), "funSwitchIsAggressive"}, {3, 10, ("Coal to fuel-chest: "..tostring(supplyFuelChest)), "funSwitchSupplyFuelChest"}},
- {"Variable2Setup", {3, 10, "<-Previous", "menuVariableSetup"}, {3, 11, "Next->", "menuTurtleSetup"}, {3, 4, ("Dump Cobblestone: "..tostring(dumpCobblestone)), "funSwitchDumpCobblestone"}, {3, 5, ("Dump Dirt: "..tostring(dumpDirt)), "funSwitchDumpDirt"}, {3, 6, ("Dump Gravel: "..tostring(dumpGravel)), "funSwitchDumpGravel"}, {3, 7, ("Check if Turtle: "..tostring(checkIfTurtle)), "funSwitchCheckIfTurtle"}, {3, 8, ("Keep log: "..tostring(doDebugLog)), "funSwitchDoDebugLog"}},
- {"TurtleSetup", {3, 9, "<-Previous", "menuVariable2Setup"}, {3, 10, "Next->", "menuHomeSetup"}, {3, 4, ("Turtle X: "..tostring(TurtleX)), "funEditTurtleX", "funClearTurtleX"}, {3,5, ("Turtle Z: "..tostring(TurtleZ)), "funEditTurtleZ", "funClearTurtleZ"}, {3,6, ("Turtle Y: "..tostring(TurtleY)), "funEditTurtleY", "funClearTurtleY"}, {3,7, ("Turtle Direction: "..tostring(Directions[TurtleDirection])), "funNextTurtleDirection", "funPreviousTurtleDirection"}},
- {"HomeSetup", {3, 9, "<-Previous", "menuTurtleSetup"}, {3, 10, "Next->", "menuPreHomeSetup"}, {3, 4, ("Home X: "..tostring(HomeX)), "funEditHomeX", "funClearHomeX"}, {3,5, ("Home Z: "..tostring(HomeZ)), "funEditHomeZ", "funClearHomeZ"}, {3,6, ("Home Y: "..tostring(HomeY)), "funEditHomeY", "funClearHomeY"}, {3,7, ("Home Facing: "..tostring(Directions[HomeFacing])), "funNextHomeFacing", "funPreviousHomeFacing"}},
- {"PreHomeSetup", {3, 9, "<-Previous", "menuHomeSetup"}, {3, 10, "Continue", "menuOverview"}, {3, 4, ("pre-Home X: "..tostring(preHomeX)), "funEditPreHomeX", "funClearPreHomeX"}, {3,5, ("pre-Home Z: "..tostring(preHomeZ)), "funEditPreHomeZ", "funClearPreHomeZ"}, {3,6, ("pre-Home Y: "..tostring(preHomeY)), "funEditPreHomeY", "funClearPreHomeY"}},
- {"overview", {3, 13, "Switch to 'jobSetup'", "menuJobSetup"}},
- {"jobSetup", {3, 10, "Switch to 'overview'", "menuOverview"}, {3, 4, ("Current Job Request: "..tostring(jobs[curJob])), "funNextCurJob", "funPreviousCurJob"}, {3, 5, ("Auto Job Request: "..tostring(autoJobRequest)), "funSwitchAutoJobRequest"}, {3, 8, "Request Job", "funRequestJob"}}
- }
- --exactInv==true and 5 or 4
- --updateTerm==true and "Update Terminal: "..tostring(updateTerm).." (Slower)" or "Update Terminal: "..tostring(updateTerm).." (Faster)"
- -- Bombing Variables (function)
- local bombDetonationTiming = 15 --How many blocks the tnt falls before it explodes
- --Quarry Variables (function)
- local quarrySize = 16
- local depth = 60 --Edit to set the depth of quarries
- local lastX = 0
- local lastZ = 0
- local lastY = 0
- local lastDirection = 0
- local quarryNum = -1
- --Modem Variables
- local modem = nil
- if isWireless then modem = peripheral.wrap("right") end
- local listenCommonChannel = 99
- local officialHangoutChannel = 222
- --Stuff to prevent future problems
- local event = nil
- local mSide = nil
- local sChannel = nil
- local rChannel = nil
- local message = nil
- local sDis = nil
- --------/Variables'n'stuff------------
- -------------Pre-launch---------------
- if isWireless then modem.open(officialHangoutChannel) end
- --modem.open(listenCommonChannel)
- ------------/Pre-launch---------------
- -------------Functions----------------
- function ScreenCleanUp()
- term.clear()
- term.setCursorPos(1,1)
- textColor("red")
- print("Running 'Drone' v.3.2")
- print()
- textColor("cyan")
- end
- function UpdateMenus()
- menus[1] = {"VariableSetup", {3, 12, "Next->", "menuVariable2Setup"}, {3, 4, ("Turtle ID: "..tostring(TurtleID)), "funEditTurtleID", "funClearTurtleID"}, {3,5, ("Travel Y: "..tostring(TravelY)), "funEditTravelY", "funClearTravelY"}, {3, 6, ("Low-Fuel Limit: "..tostring(lowFuel)), "funEditLowFuel", "funClearLowFuel"}, {3,7, (updateTerm==true and "Update Terminal: "..tostring(updateTerm).." (Slower)" or "Update Terminal: "..tostring(updateTerm).." (Faster)"), "funSwitchUpdateTerm"}, {3, 8, ("Is Wireless: "..tostring(isWireless)), "funSwitchIsWireless"}, {3, 9, ("Is Aggressive: "..tostring(isAggressive).." (Kill Entities)"), "funSwitchIsAggressive"}, {3, 10, ("Coal to fuel-chest: "..tostring(supplyFuelChest)), "funSwitchSupplyFuelChest"}}
- menus[2] = {"Variable2Setup", {3, 10, "<-Previous", "menuVariableSetup"}, {3, 11, "Next->", "menuTurtleSetup"}, {3, 4, ("Dump Cobblestone: "..tostring(dumpCobblestone)), "funSwitchDumpCobblestone"}, {3, 5, ("Dump Dirt: "..tostring(dumpDirt)), "funSwitchDumpDirt"}, {3, 6, ("Dump Gravel: "..tostring(dumpGravel)), "funSwitchDumpGravel"}, {3, 7, ("Check if Turtle: "..tostring(checkIfTurtle)), "funSwitchCheckIfTurtle"}, {3, 8, ("Keep log: "..tostring(doDebugLog)), "funSwitchDoDebugLog"}}
- menus[3] = {"TurtleSetup", {3, 9, "<-Previous", "menuVariable2Setup"}, {3, 10, "Next->", "menuHomeSetup"}, {3, 4, ("Turtle X: "..tostring(TurtleX)), "funEditTurtleX", "funClearTurtleX"}, {3,5, ("Turtle Z: "..tostring(TurtleZ)), "funEditTurtleZ", "funClearTurtleZ"}, {3,6, ("Turtle Y: "..tostring(TurtleY)), "funEditTurtleY", "funClearTurtleY"}, {3,7, ("Turtle Direction: "..tostring(Directions[TurtleDirection])), "funNextTurtleDirection", "funPreviousTurtleDirection"}}
- menus[4] = {"HomeSetup", {3, 9, "<-Previous", "menuTurtleSetup"}, {3, 10, "Next->", "menuPreHomeSetup"}, {3, 4, ("Home X: "..tostring(HomeX)), "funEditHomeX", "funClearHomeX"}, {3,5, ("Home Z: "..tostring(HomeZ)), "funEditHomeZ", "funClearHomeZ"}, {3,6, ("Home Y: "..tostring(HomeY)), "funEditHomeY", "funClearHomeY"}, {3,7, ("Home Facing: "..tostring(Directions[HomeFacing])), "funNextHomeFacing", "funPreviousHomeFacing"}}
- menus[5] = {"PreHomeSetup", {3, 9, "<-Previous", "menuHomeSetup"}, {3, 10, "Continue", "menuJobSetup"}, {3, 4, ("pre-Home X: "..tostring(preHomeX)), "funEditPreHomeX", "funClearPreHomeX"}, {3,5, ("pre-Home Z: "..tostring(preHomeZ)), "funEditPreHomeZ", "funClearPreHomeZ"}, {3,6, ("pre-Home Y: "..tostring(preHomeY)), "funEditPreHomeY", "funClearPreHomeY"}}
- menus[6] = {"overview", {3, 13, "Switch to 'jobSetup'", "menuJobSetup"}}
- menus[7] = {"jobSetup", {3, 10, "Switch to 'overview'", "menuOverview"}, {3, 4, ("Current Job Request: "..tostring(jobs[curJob])), "funNextCurJob", "funPreviousCurJob"}, {3, 5, ("Auto Job Request: "..tostring(autoJobRequest)), "funSwitchAutoJobRequest"}, {3, 8, "Request Job", "funRequestJob"}}
- for x=2, #menus[curMenu] do
- term.setCursorPos(menus[curMenu][x][1], menus[curMenu][x][2])
- textColor("green")
- write(menus[curMenu][x][3])
- end
- term.setCursorPos(1,(menus[curMenu][curSelection+1][2]))
- write(">")
- end
- function ScreenUpdate()
- UpdateMenus()
- term.setCursorPos(1,2)
- textColor("cyan")
- if curMenu == 1 then
- print(" Variable Setup")
- elseif curMenu == 2 then
- print(" Variable 2 Setup")
- elseif curMenu == 3 then
- print(" Turtle Setup")
- elseif curMenu == 4 then
- print(" Home Setup")
- elseif curMenu == 5 then
- print(" Pre-Home Setup")
- elseif curMenu == 6 then
- print()
- print("Fuel Level: "..tostring(turtle.getFuelLevel()))
- print("My X: "..tostring(TurtleX).." || Home X: "..tostring(HomeX))--.." || Going X: "..tostring(TargetX))
- print("My Z: "..tostring(TurtleZ).." || Home Z: "..tostring(HomeZ))--.." || Going Z: "..tostring(TargetZ))
- print("My Y: "..tostring(TurtleY).." || Home Y: "..tostring(HomeY))--.." || Going Y: "..tostring(TargetY))
- print("Im facing: "..tostring(Directions[TurtleDirection]))
- print()
- print("Currently doing: "..currentlyDoing)
- print()
- elseif curMenu == 7 then
- print(" Job Setup")
- end
- end
- function saveData(updatePosition)
- if updatePosition then
- if not fs.exists("data") then fs.makeDir("data") end
- local h = fs.open("data/position", "w")
- h.writeLine("TurtleX") h.writeLine(TurtleX)
- h.writeLine("TurtleZ") h.writeLine(TurtleZ)
- h.writeLine("TurtleY") h.writeLine(TurtleY)
- h.writeLine("TurtleDirection") h.writeLine(TurtleDirection)
- h.close()
- end
- if not fs.exists("data") then fs.makeDir("data") end
- local h = fs.open("data/variables", "w")
- h.writeLine("HomeX") h.writeLine(HomeX)
- h.writeLine("HomeZ") h.writeLine(HomeZ)
- h.writeLine("HomeY") h.writeLine(HomeY)
- h.writeLine("HomeFacing") h.writeLine(HomeFacing)
- h.writeLine("preHomeX") h.writeLine(preHomeX)
- h.writeLine("preHomeZ") h.writeLine(preHomeZ)
- h.writeLine("preHomeY") h.writeLine(preHomeY)
- h.writeLine("TurtleID") h.writeLine(TurtleID)
- h.writeLine("TravelY") h.writeLine(TravelY)
- h.writeLine("lowFuel") h.writeLine(lowFuel)
- h.writeLine("curJob") h.writeLine(curJob)
- h.writeLine("autoJobRequest") h.writeLine(autoJobRequest)
- h.writeLine("updateTerm") h.writeLine(updateTerm)
- h.writeLine("isWireless") h.writeLine(isWireless)
- h.writeLine("isAggressive") h.writeLine(isAggressive)
- h.writeLine("supplyFuelChest") h.writeLine(supplyFuelChest)
- h.writeLine("dumpCobblestone") h.writeLine(dumpCobblestone)
- h.writeLine("dumpDirt") h.writeLine(dumpDirt)
- h.writeLine("dumpGravel") h.writeLine(dumpGravel)
- h.writeLine("checkIfTurtle") h.writeLine(checkIfTurtle)
- h.writeLine("doDebugLog") h.writeLine(doDebugLog)
- h.close()
- end
- function loadData()
- local temp = nil
- if Debug then print("Loading data") sleep(3) end
- if fs.exists("data/position") then
- local h = fs.open("data/position", "r")
- if h.readLine() == "TurtleX" then
- TurtleX = math.floor(tonumber(h.readLine())) else h.readLine() end
- if h.readLine() == "TurtleZ" then
- TurtleZ = math.floor(tonumber(h.readLine())) else h.readLine() end
- if h.readLine() == "TurtleY" then
- TurtleY = math.floor(tonumber(h.readLine())) else h.readLine() end
- if h.readLine() == "TurtleDirection" then
- TurtleDirection = math.floor(tonumber(h.readLine())) end
- h.close()
- end
- if fs.exists("data/variables") then
- local h = fs.open("data/variables", "r")
- if h.readLine() == "HomeX" then
- HomeX = math.floor(tonumber(h.readLine())) else h.readLine() end
- if h.readLine() == "HomeZ" then
- HomeZ = math.floor(tonumber(h.readLine())) else h.readLine() end
- if h.readLine() == "HomeY" then
- HomeY = math.floor(tonumber(h.readLine())) else h.readLine() end
- if h.readLine() == "HomeFacing" then
- HomeFacing = math.floor(tonumber(h.readLine())) else h.readLine() end
- if h.readLine() == "preHomeX" then
- preHomeX = math.floor(tonumber(h.readLine())) else h.readLine() end
- if h.readLine() == "preHomeZ" then
- preHomeZ = math.floor(tonumber(h.readLine())) else h.readLine() end
- if h.readLine() == "preHomeY" then
- preHomeY = math.floor(tonumber(h.readLine())) else h.readLine() end
- if h.readLine() == "TurtleID" then
- TurtleID = math.floor(tonumber(h.readLine())) else h.readLine() end
- if h.readLine() == "TravelY" then
- TravelY = math.floor(tonumber(h.readLine())) else h.readLine() end
- if h.readLine() == "lowFuel" then
- lowFuel = math.floor(tonumber(h.readLine())) else h.readLine() end
- if h.readLine() == "curJob" then
- curJob = math.floor(tonumber(h.readLine())) else h.readLine() end
- if h.readLine() == "autoJobRequest" then
- autoJobRequest = h.readLine()
- autoJobRequest = autoJobRequest == "true" else h.readLine() end
- if h.readLine() == "updateTerm" then
- updateTerm = h.readLine()
- updateTerm = updateTerm == "true" else h.readLine() end
- if h.readLine() == "isWireless" then
- isWireless = h.readLine()
- isWireless = isWireless == "true" else h.readLine() end
- if h.readLine() == "isAggressive" then
- isAggressive = h.readLine()
- isAggressive = isAggressive == "true" else h.readLine() end
- if h.readLine() == "supplyFuelChest" then
- supplyFuelChest = h.readLine()
- supplyFuelChest = supplyFuelChest == "true" else h.readLine() end
- if h.readLine() == "dumpCobblestone" then
- dumpCobblestone = h.readLine()
- dumpCobblestone = dumpCobblestone == "true" else h.readLine() end
- if h.readLine() == "dumpDirt" then
- dumpDirt = h.readLine()
- dumpDirt = dumpDirt == "true" else h.readLine() end
- if h.readLine() == "dumpGravel" then
- dumpGravel = h.readLine()
- dumpGravel = dumpGravel == "true" else h.readLine() end
- if h.readLine() == "checkIfTurtle" then
- checkIfTurtle = h.readLine()
- checkIfTurtle = checkIfTurtle == "true" else h.readLine() end
- if h.readLine() == "doDebugLog" then
- doDebugLog = h.readLine()
- doDebugLog = doDebugLog == "true" end
- h.close()
- end
- stName1 = ("Quarry Turtle "..tostring(TurtleID).." :: Fuel") --Name in statList
- stName2 = ("Quarry Turtle "..tostring(TurtleID).." :: Inventory") -- -||-
- end
- function generateLog(text)
- if doDebugLog then
- if not fs.exists("data") then fs.makeDir("data") end
- local h = fs.open("data/Log", "a") --'a' - Add to previous
- h.writeLine(text)
- h.close()
- end
- end
- function textColor(color)
- if term.isColor() then
- if color == "white" then
- term.setTextColor(1)
- elseif color == "orange" then
- term.setTextColor(2)
- elseif color == "magneta" then
- term.setTextColor(4)
- elseif color == "lightBlue" then
- term.setTextColor(8)
- elseif color == "yellow" then
- term.setTextColor(16)
- elseif color == "lime" then
- term.setTextColor(32)
- elseif color == "pink" then
- term.setTextColor(64)
- elseif color == "gray" then
- term.setTextColor(150)
- elseif color == "lightGray" then
- term.setTextColor(256)
- elseif color == "cyan" then
- term.setTextColor(512)
- elseif color == "purple" then
- term.setTextColor(1024)
- elseif color == "blue" then
- term.setTextColor(2048)
- elseif color == "brown" then
- term.setTextColor(4096)
- elseif color == "green" then
- term.setTextColor(8192)
- elseif color == "red" then
- term.setTextColor(16384)
- elseif color == "black" then
- term.setTextColor(32768)
- end
- end
- end
- function valueEdit(buttonNum, variable, minValue, maxValue)
- local x = (#menus[curMenu][buttonNum][3]-#tostring(variable))+3
- local y = menus[curMenu][buttonNum][2]
- term.setCursorPos(x,y)
- for u=1,#tostring(variable) do
- write(" ")
- sleep(0)
- end
- term.setCursorPos(x,y)
- local temp = read()
- if tonumber(temp) then if tonumber(temp) > maxValue then temp = maxValue elseif tonumber(temp) < minValue then temp = minValue end variable = math.floor(tonumber(temp)) end
- return variable
- end
- function requestJob()
- if jobs[curJob] == "Quarry" then
- --print("Quarry") sleep(3)
- sendToServer("quarry", "request")
- sentRequest = true
- elseif jobs[curJob] == "Bomb" then
- --print("Bomb") sleep(3)
- sendToServer("bomb")
- sentRequest = true
- end
- end
- function handleButtons()
- if nextAction == "menuVariableSetup" then
- curMenu = 1
- curSelection = 1
- elseif nextAction == "menuVariable2Setup" then
- curMenu = 2
- curSelection = 2
- elseif nextAction == "menuTurtleSetup" then
- curMenu = 3
- curSelection = 2
- stName1 = ("Quarry Turtle "..tostring(TurtleID).." :: Fuel") --Name in statList
- stName2 = ("Quarry Turtle "..tostring(TurtleID).." :: Inventory") -- -||-
- elseif nextAction == "menuHomeSetup" then
- curMenu = 4
- curSelection = 2
- elseif nextAction == "menuPreHomeSetup" then
- curMenu = 5
- curSelection = 2
- elseif nextAction == "menuOverview" then
- curMenu = 6
- curSelection = 1
- elseif nextAction == "menuJobSetup" then
- curMenu = 7
- curSelection = 1
- elseif nextAction == "funEditTurtleID" then
- TurtleID = valueEdit(3, TurtleID, 1, 9999)
- if saveData then saveData() end
- elseif nextAction == "funClearTurtleID" then
- TurtleID = 1
- if saveData then saveData() end
- elseif nextAction == "funEditTravelY" then
- TravelY = valueEdit(4, TravelY, 1, 999)
- if saveData then saveData() end
- elseif nextAction == "funClearTravelY" then
- TravelY = 0
- if saveData then saveData() end
- elseif nextAction == "funEditLowFuel" then
- lowFuel = valueEdit(5, lowFuel, 100, 10000)
- if saveData then saveData() end
- elseif nextAction == "funClearLowFuel" then
- lowFuel = 1000
- if saveData then saveData() end
- elseif nextAction == "funSwitchUpdateTerm" then
- updateTerm = not updateTerm
- if saveData then saveData() end
- elseif nextAction == "funSwitchIsWireless" then
- isWireless = not isWireless
- if saveData then saveData() end
- elseif nextAction == "funSwitchSupplyFuelChest" then
- supplyFuelChest = not supplyFuelChest
- if saveData then saveData() end
- elseif nextAction == "funSwitchIsAggressive" then
- isAggressive = not isAggressive
- if saveData then saveData() end
- elseif nextAction == "funSwitchDumpCobblestone" then
- dumpCobblestone = not dumpCobblestone
- if saveData then saveData() end
- elseif nextAction == "funSwitchDumpDirt" then
- dumpDirt = not dumpDirt
- if saveData then saveData() end
- elseif nextAction == "funSwitchDumpGravel" then
- dumpGravel = not dumpGravel
- if saveData then saveData() end
- elseif nextAction == "funSwitchCheckIfTurtle" then
- checkIfTurtle = not checkIfTurtle
- if saveData then saveData() end
- elseif nextAction == "funSwitchDoDebugLog" then
- doDebugLog = not doDebugLog
- if saveData then saveData() end
- elseif nextAction == "funEditTurtleX" then
- TurtleX = valueEdit(4, TurtleX, -99999, 99999)
- if saveData then saveData(true) end
- elseif nextAction == "funClearTurtleX" then
- TurtleX = 0
- if saveData then saveData(true) end
- elseif nextAction == "funEditTurtleZ" then
- TurtleZ = valueEdit(5, TurtleZ, -99999, 99999)
- if saveData then saveData(true) end
- elseif nextAction == "funClearTurtleZ" then
- TurtleZ = 0
- if saveData then saveData(true) end
- elseif nextAction == "funEditTurtleY" then
- TurtleY = valueEdit(6, TurtleY, 1, 999)
- if saveData then saveData(true) end
- elseif nextAction == "funClearTurtleY" then
- TurtleY = 0
- if saveData then saveData(true) end
- elseif nextAction == "funNextTurtleDirection" then
- if TurtleDirection+1 > 3 then
- TurtleDirection = 0
- else
- TurtleDirection = TurtleDirection+1
- end
- if saveData then saveData(true) end
- elseif nextAction == "funPreviousTurtleDirection" then
- if TurtleDirection-1 < 0 then
- TurtleDirection = 3
- else
- TurtleDirection = TurtleDirection-1
- end
- if saveData then saveData(true) end
- elseif nextAction == "funEditHomeX" then
- HomeX = valueEdit(4, HomeX, -99999, 99999)
- if saveData then saveData() end
- elseif nextAction == "funClearHomeX" then
- HomeX = 0
- if saveData then saveData() end
- elseif nextAction == "funEditHomeZ" then
- HomeZ = valueEdit(5, HomeZ, -99999, 99999)
- if saveData then saveData() end
- elseif nextAction == "funClearHomeZ" then
- HomeZ = 0
- if saveData then saveData() end
- elseif nextAction == "funEditHomeY" then
- HomeY = valueEdit(6, HomeY, 1, 999)
- if saveData then saveData() end
- elseif nextAction == "funClearHomeY" then
- HomeY = 0
- if saveData then saveData() end
- elseif nextAction == "funNextHomeFacing" then
- if HomeFacing+1 > 3 then
- HomeFacing = 0
- else
- HomeFacing = HomeFacing+1
- end
- if saveData then saveData() end
- elseif nextAction == "funPreviousHomeFacing" then
- if HomeFacing-1 < 0 then
- HomeFacing = 3
- else
- HomeFacing = HomeFacing-1
- end
- if saveData then saveData() end
- elseif nextAction == "funEditPreHomeX" then
- preHomeX = valueEdit(4, preHomeX, -99999, 99999)
- if saveData then saveData() end
- elseif nextAction == "funClearPreHomeX" then
- preHomeX = 0
- if saveData then saveData() end
- elseif nextAction == "funEditPreHomeZ" then
- preHomeZ = valueEdit(5, preHomeZ, -99999, 99999)
- if saveData then saveData() end
- elseif nextAction == "funClearPreHomeZ" then
- preHomeZ = 0
- if saveData then saveData() end
- elseif nextAction == "funEditPreHomeY" then
- preHomeY = valueEdit(6, preHomeY, 1, 999)
- if saveData then saveData() end
- elseif nextAction == "funClearPreHomeY" then
- preHomeY = 0
- if saveData then saveData() end
- elseif nextAction == "funNextCurJob" then
- if curJob+1 > #jobs then
- curJob = 1
- else
- curJob = curJob+1
- end
- saveData(false) -- Don't save position
- elseif nextAction == "funPreviousCurJob" then
- if curJob-1 < 1 then
- curJob = #jobs
- else
- curJob = curJob-1
- end
- saveData(false) -- Don't save position
- elseif nextAction == "funSwitchAutoJobRequest" then
- autoJobRequest = not autoJobRequest
- saveData(false) -- Don't save position
- if autoJobRequest then requestJob() end
- elseif nextAction == "funRequestJob" then
- sentRequest = false
- requestJob()
- end
- end
- function isEvenSize()
- if math.floor((quarrySize/2)+0.99) > (quarrySize/2) then
- return false
- else
- return true
- end
- end
- function emptyInv()--supplyFuelChest
- faceDirection(Directions[HomeFacing+1])
- for u=1, 16 do
- turtle.select(u)
- item = turtle.getItemDetail()
- if item then --If not nil
- if item.name == "minecraft:coal" and supplyFuelChest then turtle.dropDown() end -- If fail then drop in normal chest
- while not turtle.drop() do if turtle.getItemSpace() == 64 then break end sleep(5) end
- sleep(0)
- end
- end
- turtle.select(1)
- turtleInvFull = 0
- faceDirection(Directions[HomeFacing])
- sendToServer("status", false)
- end
- function goHome(ifUnload)
- goto(preHomeX, preHomeZ, preHomeY)
- goto(HomeX, HomeZ, HomeY, false)
- faceDirection(Directions[HomeFacing])
- if ifUnload then emptyInv() end
- end
- function turtleRefuel()
- if unload then emptyInv() end
- for i=1, 16 do
- turtle.select(i)
- if turtle.getItemSpace() == 64 then
- break
- end
- sleep(0)
- end
- while true do
- if not turtle.suckDown(8) then sleep(10) turtle.suckDown(8) end
- turtle.refuel()
- if turtle.getFuelLevel() > lowFuel+refuelAmount then
- break
- end
- sleep(0)
- end
- faceDirection(Directions[HomeFacing])
- generateLog("Refueled")
- sendToServer("status", false)
- end
- function checkFuel()
- generateLog("Checking fuel level")
- if turtle.getFuelLevel() <= lowFuel then
- lastX = TurtleX
- lastZ = TurtleZ
- lastY = TurtleY
- lastDirection = TurtleDirection
- goHome()
- turtleRefuel()
- generateLog("Returning to home")
- sendToServer("status", false)
- goto(preHomeX, preHomeZ, preHomeY, false)
- generateLog("Heading to last coord")
- goto(lastX, lastZ, lastY)
- generateLog("Arrived at last coord")
- faceDirection(Directions[lastDirection])
- sendToServer("status", false)
- end
- generateLog("Checked fuel")
- end
- function turtleInv(empty)
- local temp = 0
- local tempPrecise = 0
- for i=1, 16 do
- turtle.select(i)
- item = turtle.getItemDetail()
- if item then --If not nil
- if item.name == "minecraft:cobblestone" and dumpCobblestone then turtle.drop()
- elseif item.name == "minecraft:dirt" and dumpDirt then turtle.drop()
- elseif item.name == "minecraft:gravel" and dumpGravel then turtle.drop()
- else
- if turtle.getItemSpace() < 64 then temp=temp+1 end
- tempPrecise = tempPrecise + turtle.getItemCount()
- sleep(0)
- end
- end
- end
- turtle.select(1)
- turtleInvFull = temp
- turtleInvExact = tempPrecise
- if turtleInvFull == 16 and empty == true then
- lastX = TurtleX
- lastZ = TurtleZ
- lastY = TurtleY
- lastDirection = TurtleDirection
- generateLog("Full inventory, will go home to dump")
- sendToServer("status", false)
- goHome()
- sendToServer("status", false)
- emptyInv()
- goto(preHomeX, preHomeZ, preHomeY, false)
- goto(lastX, lastZ, lastY)
- faceDirection(Directions[lastDirection])
- generateLog("Emptied inv and back in place - is now ready to go again")
- sendToServer("status", false)
- end
- end
- function waitForEvent()
- generateLog("Waiting for answer/event")
- currentlyDoing = "Waiting for a job.."
- ScreenCleanUp()
- ScreenUpdate()
- event, p1, sChannel, rChannel, message, sDis = os.pullEvent()
- if event == "modem_message" then
- local tempMessage = textutils.unserialize(message)
- if tempMessage == nil then
- generateLog("Message received gave nil after .unserialize(message)")
- else
- generateLog("Received valid message!")
- if tempMessage[1] == TurtleID then --If refered to
- generateLog("Server talking to me!")
- if tempMessage[2] == "newquarry" then
- generateLog("NewQuarry order received")
- depth = tempMessage[6]
- quarryNum = tempMessage[7]
- quarrySize = tempMessage[8]
- quarry(tempMessage[3], tempMessage[4], tempMessage[5])
- elseif tempMessage[2] == "bomb" then
- bomb(tempMessage[3],tempMessage[4],tempMessage[5],tempMessage[6],tempMessage[7],tempMessage[8])
- --elseif tempMessage[2] == "finished" then
- --Nuthin'
- end
- else
- generateLog("Server not talking to me :<") generateLog("My ID: "..TurtleID..", Message ID: "..tostring(tempMessage[1]))
- end
- end
- -- elseif event == "char" then
- -- if p1 == "t" then
- -- keepGoing = false
- -- end
- elseif event == "key" then -- x=2, #menus[curMenu]
- if p1 == 20 then -- "t"
- keepGoing = false
- elseif p1 == 19 then -- "r"
- if fs.exists("data/Log") then fs.delete("data/Log") end
- elseif p1 == 28 then -- Enter
- --if not menus[curMenu][curSelection+1][4] == nil then
- nextAction = menus[curMenu][curSelection+1][4]
- --end
- elseif p1 == 14 then -- Backspace
- --if not menus[curMenu][curSelection+1][5] == nil then
- nextAction = menus[curMenu][curSelection+1][5]
- --end
- elseif p1 == 200 then -- Arrow Up
- if curSelection-1 < 1 then
- curSelection = #menus[curMenu]-1
- else
- curSelection = curSelection-1
- end
- elseif p1 == 208 then -- Arrow Down
- if curSelection+1 > #menus[curMenu]-1 then
- curSelection = 1
- else
- curSelection = curSelection+1
- end
- --else
- --print("Key pressed: "..tostring(p1))
- --sleep(2)
- end
- handleButtons()
- nextAction = ""
- end
- end
- function sendToServer(mode, param)
- generateLog("Entering sendToServer("..mode..", "..tostring(param)..")")
- if mode == "quarry" then
- generateLog("Quarry request message setting up")
- local dataTable = {}
- dataTable[1] = TurtleID
- dataTable[2] = "Quarry"
- dataTable[3] = TurtleX
- dataTable[4] = TurtleZ
- dataTable[5] = TurtleY
- if param == "request" then
- dataTable[6] = "request"
- elseif param == "finished" then
- dataTable[6] = "finished"
- dataTable[7] = quarryNum
- end
- message = textutils.serialize(dataTable)
- if isWireless then modem.transmit(listenCommonChannel, officialHangoutChannel, message) end
- generateLog("Sent quarry request message")
- elseif mode == "status" then
- local dataTable = {}
- dataTable[1] = TurtleID
- dataTable[2] = "Status"
- dataTable[3] = TurtleX
- dataTable[4] = TurtleZ
- dataTable[5] = TurtleY
- generateLog("Checking FuelLevel() for status message")
- dataTable[6] = turtle.getFuelLevel()
- dataTable[7] = stMax1
- dataTable[8] = stName1
- generateLog("Checking turtleInv(param) for status message")
- turtleInv(param) --Updates inv. and if true -unloads it
- dataTable[9] = turtleInvFull
- dataTable[10] = turtleInvExact
- dataTable[11] = stMax2
- dataTable[12] = stName2
- generateLog("About to send status message")
- local messageToSend = textutils.serialize(dataTable)
- if isWireless then modem.transmit(listenCommonChannel, officialHangoutChannel, messageToSend) end
- generateLog("Sent status message")
- --print()
- --print("Sent status")
- elseif mode == "bomb" then
- local dataTable = {}
- dataTable[1] = TurtleID
- dataTable[2] = "Bomb"
- dataTable[3] = TurtleX
- dataTable[4] = TurtleZ
- dataTable[5] = TurtleY
- message = textutils.serialize(dataTable)
- if isWireless then modem.transmit(listenCommonChannel, officialHangoutChannel, message) end
- end
- generateLog("Returning from sendToServer()")
- end
- function faceDirection(direction)
- local temp = 0
- while true do
- if tostring(Directions[temp]) == tostring(direction) then
- break
- else
- temp=(temp+1)
- end
- sleep(0)
- end
- if Debug then
- print("Searched: "..tostring(direction))
- print("Found: "..tostring(Directions[temp]))
- end
- while true do
- if tostring(Directions[TurtleDirection]) == tostring(Directions[temp]) then
- break
- else
- if turtle.turnRight() then
- TurtleDirection = (TurtleDirection + 1)
- if TurtleDirection > 3 then
- TurtleDirection = 0
- end
- end
- end
- sleep(0)
- end
- if savePositionData then if saveData then saveData() end end
- if Debug then
- print("Orderd: "..tostring(Directions[temp]))
- print("Did: "..tostring(Directions[TurtleDirection]))
- end
- end
- function goForward()
- if checkIfTurtle then
- local block = turtle.inspect()
- if not block == nil then
- if block.name == "ComputerCraft:CC-Turtle" or block.name == "ComputerCraft:CC-TurtleAdvanced" then
- sleep(2)
- end
- end
- end
- while not turtle.forward() do
- turtle.dig()
- if isAggressive then turtle.attack() end
- sleep(0)
- end
- if TurtleDirection == 0 then --South
- TurtleZ = (TurtleZ + 1)
- elseif TurtleDirection == 1 then --West
- TurtleX = (TurtleX - 1)
- elseif TurtleDirection == 2 then --North
- TurtleZ = (TurtleZ - 1)
- elseif TurtleDirection == 3 then --East
- TurtleX = (TurtleX + 1)
- end
- if updateTerm then ScreenCleanUp() ScreenUpdate() end
- if savePositionData then if saveData then saveData() end end
- end
- function goUp()
- if checkIfTurtle then
- local block = turtle.inspectUp()
- if not block == nil then
- if block.name == "ComputerCraft:CC-Turtle" or block.name == "ComputerCraft:CC-TurtleAdvanced" then
- sleep(2)
- end
- end
- end
- while not turtle.up() do
- turtle.digUp()
- if isAggressive then turtle.attackUp() end
- sleep(0)
- end
- TurtleY = (TurtleY + 1)
- if updateTerm then ScreenCleanUp() ScreenUpdate() end
- if savePositionData then if saveData then saveData() end end
- end
- function goDown()
- if checkIfTurtle then
- local block = turtle.inspectDown()
- if not block == nil then
- if block.name == "ComputerCraft:CC-Turtle" or block.name == "ComputerCraft:CC-TurtleAdvanced" then
- sleep(2)
- end
- end
- end
- while not turtle.down() do
- turtle.digDown()
- if isAggressive then turtle.attackDown() end
- sleep(0)
- end
- TurtleY = (TurtleY - 1)
- if updateTerm then ScreenCleanUp() ScreenUpdate() end
- if savePositionData then if saveData then saveData() end
- end
- end
- function bomb(bombX, bombZ, bombY, bombDirection,bombRunLength, numRebomb)
- currentlyDoing = "Bombing.."
- sendToServer("status")
- checkFuel()
- goto(preHomeX, preHomeZ, preHomeY, false) --Do not use TravelHeight
- goto(bombX, bombZ, (bombY+bombDetonationTiming))
- if bombDirection == nil then bombDirection = "North" end --Prevents error if value is forgotten to be assigned
- faceDirection(bombDirection)
- if numRebomb == nil then numRebomb = 1 end --Prevents error if value is forgotten to be assigned
- if bombRunLength == nil then bombRunLength = 1 end --Prevents error if value is forgotten to be assigned
- local bombRun = 0
- while true do
- sendToServer("status")
- bombRun=bombRun+1
- if bombRun > numRebomb then --If done, then head home
- break
- else
- -- Going for a bombing run
- turtle.select(1)
- local temp = 1 -- Inv. selection variable
- for i=1, bombRunLength do
- while true do
- if turtle.getItemSpace() == 64 then
- temp=temp+1
- if temp > 16 then
- break
- else
- turtle.select(temp)
- end
- else
- break
- end
- sleep(0)
- end
- if temp > 16 then
- break
- else
- turtle.place()
- redstone.setOutput("front", true)
- sleep(2)
- goForward()
- goForward()
- end
- sleep(0)
- end
- if temp > 16 then
- break
- else
- redstone.setOutput("front", false)
- turtle.select(1)
- if TurtleDirection+1 > 3 then faceDirection(Directions[0]) --Turn right
- else faceDirection(Directions[TurtleDirection+1]) end
- if TurtleDirection+1 > 3 then faceDirection(Directions[0]) --Turn right
- else faceDirection(Directions[TurtleDirection+1]) end
- end
- sleep(0)
- end
- end
- if sentRequest then
- sentRequest = false
- end
- goHome(false)--Will NOT empty inv when home
- currentlyDoing = "Waiting for job.."
- sendToServer("status")
- end
- function quarry(QuarryX, QuarryZ, QuarryY)
- currentlyDoing = "Quarrying.."
- local dig = true
- checkFuel()
- goto(preHomeX, preHomeZ, preHomeY, false)
- goto(QuarryX, QuarryZ, QuarryY+1)
- faceDirection("North")
- local turnRight = true
- checkFuel()
- turtle.select(1)
- turtle.digUp()
- turtle.digDown()
- while dig do
- for x=1, quarrySize do --j
- for z=1, quarrySize-1 do --i
- goForward()
- while turtle.digUp() do
- --Just to get rid of all sand/gravel
- end
- turtle.digDown()
- sleep(0)
- end
- generateLog("SendToServer() | X: "..tostring(x).." Y: "..tostring(z))
- sendToServer("status", true)
- generateLog("Going to check fuel now: "..tostring(turtle.getFuelLevel()))
- checkFuel()
- generateLog("MADE IT")
- if turnRight then
- if TurtleDirection+1 > 3 then
- faceDirection(Directions[0])
- else
- faceDirection(Directions[TurtleDirection+1])
- end
- if quarrySize > x then goForward() end
- while turtle.digUp() do
- --Just to get rid of all sand/gravel
- end
- turtle.digDown()
- if TurtleDirection+1 > 3 then
- faceDirection(Directions[0])
- else
- faceDirection(Directions[TurtleDirection+1])
- end
- turnRight = false
- else
- if TurtleDirection-1 < 0 then
- faceDirection(Directions[3])
- else
- faceDirection(Directions[TurtleDirection-1])
- end
- if quarrySize > x then goForward() end
- while turtle.digUp() do
- --Just to get rid of all sand/gravel
- end
- turtle.digDown()
- if TurtleDirection-1 < 0 then
- faceDirection(Directions[3])
- else
- faceDirection(Directions[TurtleDirection-1])
- end
- turnRight = true
- end
- sleep(0)
- end
- if isEvenSize() then faceDirection("North") end
- turnRight = not turnRight
- --checkFuel()
- if TurtleY-1 <= depth then
- dig = false
- break
- elseif TurtleY-4 <= depth then
- for i=1, 4 do -- 70 - 67
- if TurtleY-i <= depth then
- for i=1, (i-1) do
- goDown()
- turtle.digDown()
- sleep(0)
- end
- break
- end
- sleep(0)
- end
- else
- for i=1, 3 do
- goDown()
- turtle.digDown()
- sleep(0)
- end
- end
- end
- sleep(0)
- if quarryNum > -1 then sendToServer("quarry", "finished") quarryNum = -1 end --Just to make sure order came from server
- currentlyDoing = "Heading home.."
- if sentRequest then
- sentRequest = false
- end
- goHome(true)--Will empty inv when home
- currentlyDoing = "Waiting.."
- end
- function goto(TargetX,TargetZ,TargetY, ifTravelHeight)
- --Traveling Y-Coord
- if ifTravelHeight == nil then
- while true do
- if TurtleY < TravelY then
- goUp()
- --TurtleY = (TurtleY + 1)
- elseif TurtleY > TravelY then
- goDown()
- --TurtleY = (TurtleY - 1)
- end
- if TurtleY == TravelY then
- break
- end
- sleep(0)
- end
- end
- --Go X-Coord
- while true do
- if TurtleX < TargetX then
- faceDirection("East")
- goForward()
- --TurtleX = (TurtleX + 1)
- elseif TurtleX > TargetX then
- faceDirection("West")
- goForward()
- --TurtleX = (TurtleX - 1)
- end
- if TurtleX == TargetX then
- break
- end
- sleep(0)
- end
- --Go Z-Coord
- while true do
- if TurtleZ < TargetZ then
- faceDirection("South")
- goForward()
- --TurtleZ = (TurtleZ + 1)
- elseif TurtleZ > TargetZ then
- faceDirection("North")
- goForward()
- --TurtleZ = (TurtleZ - 1)
- end
- if TurtleZ == TargetZ then
- break
- end
- sleep(0)
- end
- --Go Y-Coord
- while true do
- if TurtleY < TargetY then
- goUp()
- --TurtleY = (TurtleY + 1)
- elseif TurtleY > TargetY then
- goDown()
- --TurtleY = (TurtleY - 1)
- end
- if TurtleY == TargetY then
- break
- end
- sleep(0)
- end
- end
- ------------/Functions----------------
- ------------Launching Code------------
- if loadPriorData then loadData() end
- generateLog("Starting program")
- sendToServer("status", true)
- while keepGoing do
- ScreenCleanUp()
- ScreenUpdate()
- if turtle.getFuelLevel() <= lowFuel then turtleRefuel() end
- if autoJobRequest and curMenu >= 5 then
- if not sentRequest then
- requestJob()
- end
- end
- waitForEvent()
- sleep(0)
- end
- generateLog("Program shutdown")
- generateLog("----------------")
- ScreenCleanUp()
- print("Drone program has been shutdown..")
- print()
- -----------/Launching Code------------
- ---------____________________---------
- -------// | Drone v.3.2 | \\-------
- -------|| Made By: RedCowboy ||-------
- --------\\_______2014_______//--------
- --------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement