Advertisement
Guest User

Drone

a guest
Dec 17th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 40.39 KB | None | 0 0
  1. --------------------------------------
  2. ---------|| Drone v.3.2 ||------------
  3. --------------------------------------
  4. --Requires a server 4 best experience!
  5. --------------------------------------
  6. --------------Notes-------------------
  7. --[[
  8. -------Controls and such--------
  9.     ?0¢ Arrow up - Go up
  10.     ?0¢ Arrow Down - Go down
  11.     ?0¢ Enter - Main action
  12.     ?0¢ Backspace - Secondary action
  13.     ?0¢ 't' - Close program
  14.     ?0¢ 'r' - Clear log file
  15. --------------------------------
  16. System pastebin code: 8jwwB9i4
  17. Drone pastebin code: AtWZRS07
  18.  
  19.     --Changes To Be Done-- 
  20.     ?0¢ Be able to quarry and such without connection with system - offline
  21.    
  22.     --Changes Made--
  23.     ?0¢ Implemented Bomb Job functionality
  24.     ?0¢ Implemented Anti Mob functionality
  25.     ?0¢ Implemented Toggle inventory functionality
  26.     ?0¢ Inv. variables updates on status report
  27.     ?0¢ Rewrote entire quarry function (MUCH smaller and easier to understand now)
  28.     ?0¢ Made Quarry size changeable
  29.     ?0¢ Made Depth of quarries changeable
  30.     ?0¢ Added a proper GUI
  31.     ?0¢ Updated and improved save&load functionality
  32.     ?0¢ Made turtle verify coordinates each start up
  33.     ?0¢ Made turtle ask for a job to do
  34.     ?0¢ Added some delays to prevent 'too long without yielding'
  35.     ?0¢ Made text change when updateTerm is false
  36.     ?0¢ Fixed unresponsive turtle when starting program with not enough fuel
  37.     ?0¢ Fixed an issue when starting turtle with autoRequest set to True
  38.     ?0¢ Made variable-loader compatible with most versions from 3.1 and newer
  39.     ?0¢ Select empty slot before sucking fuel from chest
  40.     ?0¢ Added more 'sleep(0)' to prevent crashes
  41.     ?0¢ Now checks if drop-of chest is full
  42.     ?0¢ Will now give the turtle a chance to escape before mining a fellow turtle, if told to
  43.     ?0¢ Will now dump cobble/dirt/gravel, if told to
  44.     ?0¢ Will now generate a logfile for debug, if told to
  45.     ?0¢ [BUG] Fixed random freezing in code
  46.     ?0¢ Will now drop coal in fuel chest, if told to
  47.    
  48.     --Bug List--
  49.     ?0¢ [MOD BUG] Sometimes just freezes and reboots while quarrying
  50.         -Reason: Lag causes code to freeze and then terminate with an "Too long without yielding" error
  51.         OR the chunk the turtle is currently in gets unloaded
  52. --]]
  53.  
  54. -- North >> -Z
  55. -- South >> +Z
  56. -- East >> +X
  57. -- West >> -X
  58. -------------/Notes-------------------
  59. ---------Variables'n'stuff------------
  60. local TurtleID = 1 --Make sure be unused by others!
  61.  
  62. local lowFuel = 1000 --At which point to head home
  63. local refuelAmount = 500 -- How much over lowFuel to refuel
  64. local unload = true --To empty inv. when refueling
  65. local updateTerm = true --Keep term with fresh variables(Heavier to process prog.!)
  66. local isWireless = true
  67. local isAggressive = true -- To attack entities in-front, under and over turtle if stuck
  68. local saveData = true
  69. local loadPriorData = true
  70.  
  71. local supplyFuelChest = true
  72. local dumpCobblestone = false
  73. local dumpDirt = false
  74. local dumpGravel = false
  75. local checkIfTurtle = true
  76. local doDebugLog = false
  77.  
  78. local preHomeX = 0 -- And these
  79. local preHomeZ = 0
  80. local preHomeY = 1
  81.  
  82. local HomeX = 0 --Make sure to set these, as well as the below to the turtle position
  83. local HomeZ = 0
  84. local HomeY = 1
  85. local HomeFacing = 0
  86.  
  87. local TravelY = 70 --What height for turtle to travel at (e.x To/From quarry)
  88.  
  89. local TurtleX = 0 --(  These are what I ment with 'below' )
  90. local TurtleZ = 0
  91. local TurtleY = 1
  92. local TurtleDirection = 0 -- Use the table below, 'Directions', as guide
  93.  
  94. local Directions = {} --Do not edit !
  95. Directions[0] = "South" --Do not edit !
  96. Directions[1] = "West" --Do not edit !
  97. Directions[2] = "North" --Do not edit !
  98. Directions[3] = "East" --Do not edit !
  99.  
  100. local TargetX = 0
  101. local TargetZ = 0
  102. local TargetY = 0
  103.  
  104. local stName1 = ("Quarry Turtle 1 :: Fuel") --Name in statList
  105. local stMax1 = 20000 --Full bar in statList
  106. local stName2 = ("Quarry Turtle 1 :: Inventory") -- -||-
  107. local stMax2 = 16 -- -||-
  108.  
  109. local turtleInvFull = 0
  110. local turtleInvExact = 0
  111. local Debug = false
  112. local currentlyDoing = "Nothing.."
  113. local keepGoing = true
  114.  
  115. -- Job Variables
  116. local jobs = {"Quarry", "Bomb"}
  117. local curJob = 1
  118. local autoJobRequest = false
  119. local sentRequest = false
  120.  
  121. -- Menu Variables
  122. local curMenu = 1
  123. local curSelection = 1
  124. local nextAction = ""
  125.  
  126. local menus = {
  127. {"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"}},
  128. {"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"}},
  129. {"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"}},
  130. {"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"}},
  131. {"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"}},
  132.  
  133. {"overview", {3, 13, "Switch to 'jobSetup'", "menuJobSetup"}},
  134. {"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"}}
  135. }
  136. --exactInv==true and 5 or 4
  137. --updateTerm==true and "Update Terminal: "..tostring(updateTerm).." (Slower)" or "Update Terminal: "..tostring(updateTerm).." (Faster)"
  138. -- Bombing Variables (function)
  139. local bombDetonationTiming = 15 --How many blocks the tnt falls before it explodes
  140.  
  141. --Quarry Variables (function)
  142. local quarrySize = 16
  143. local depth = 60 --Edit to set the depth of quarries
  144.  
  145. local lastX = 0
  146. local lastZ = 0
  147. local lastY = 0
  148. local lastDirection = 0
  149.  
  150. local quarryNum = -1
  151. --Modem Variables
  152. local modem = nil
  153. if isWireless then modem = peripheral.wrap("right") end
  154.  
  155. local listenCommonChannel = 99
  156. local officialHangoutChannel = 222
  157. --Stuff to prevent future problems
  158. local event = nil
  159. local mSide = nil
  160. local sChannel = nil
  161. local rChannel = nil
  162. local message = nil
  163. local sDis = nil
  164. --------/Variables'n'stuff------------
  165. -------------Pre-launch---------------
  166. if isWireless then modem.open(officialHangoutChannel) end
  167. --modem.open(listenCommonChannel)
  168. ------------/Pre-launch---------------
  169. -------------Functions----------------
  170. function ScreenCleanUp()
  171.   term.clear()
  172.   term.setCursorPos(1,1)
  173.   textColor("red")
  174.   print("Running 'Drone' v.3.2")
  175.   print()
  176.   textColor("cyan")
  177. end
  178.  
  179. function UpdateMenus()
  180. 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"}}
  181. 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"}}
  182. 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"}}
  183. 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"}}
  184. 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"}}
  185. menus[6] = {"overview", {3, 13, "Switch to 'jobSetup'", "menuJobSetup"}}
  186. 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"}}
  187.  
  188.     for x=2, #menus[curMenu] do
  189.         term.setCursorPos(menus[curMenu][x][1], menus[curMenu][x][2])
  190.         textColor("green")
  191.         write(menus[curMenu][x][3])
  192.     end
  193.     term.setCursorPos(1,(menus[curMenu][curSelection+1][2]))
  194.     write(">")
  195. end
  196.  
  197. function ScreenUpdate()
  198.     UpdateMenus()
  199.     term.setCursorPos(1,2)
  200.     textColor("cyan")
  201.     if curMenu == 1 then
  202.         print("     Variable Setup")
  203.     elseif curMenu == 2 then
  204.         print("     Variable 2 Setup")
  205.     elseif curMenu == 3 then
  206.         print("     Turtle Setup")
  207.     elseif curMenu == 4 then
  208.         print("     Home Setup")
  209.     elseif curMenu == 5 then
  210.         print("     Pre-Home Setup")
  211.     elseif curMenu == 6 then
  212.     print()
  213.     print("Fuel Level: "..tostring(turtle.getFuelLevel()))
  214.     print("My X: "..tostring(TurtleX).." || Home X: "..tostring(HomeX))--.." || Going X: "..tostring(TargetX))
  215.     print("My Z: "..tostring(TurtleZ).." || Home Z: "..tostring(HomeZ))--.." || Going Z: "..tostring(TargetZ))
  216.     print("My Y: "..tostring(TurtleY).." || Home Y: "..tostring(HomeY))--.." || Going Y: "..tostring(TargetY))
  217.     print("Im facing: "..tostring(Directions[TurtleDirection]))
  218.     print()
  219.     print("Currently doing: "..currentlyDoing)
  220.     print()
  221.    
  222.     elseif curMenu == 7 then
  223.         print("     Job Setup")
  224.     end
  225.    
  226. end
  227.  
  228. function saveData(updatePosition)
  229. if updatePosition then
  230.     if not fs.exists("data") then fs.makeDir("data") end
  231.     local h = fs.open("data/position", "w")
  232.     h.writeLine("TurtleX") h.writeLine(TurtleX)
  233.     h.writeLine("TurtleZ") h.writeLine(TurtleZ)
  234.     h.writeLine("TurtleY") h.writeLine(TurtleY)
  235.     h.writeLine("TurtleDirection") h.writeLine(TurtleDirection)
  236.     h.close()
  237. end
  238.  
  239. if not fs.exists("data") then fs.makeDir("data") end
  240.     local h = fs.open("data/variables", "w")
  241.     h.writeLine("HomeX") h.writeLine(HomeX)
  242.     h.writeLine("HomeZ") h.writeLine(HomeZ)
  243.     h.writeLine("HomeY") h.writeLine(HomeY)
  244.     h.writeLine("HomeFacing") h.writeLine(HomeFacing)
  245.     h.writeLine("preHomeX") h.writeLine(preHomeX)
  246.     h.writeLine("preHomeZ") h.writeLine(preHomeZ)
  247.     h.writeLine("preHomeY") h.writeLine(preHomeY)
  248.     h.writeLine("TurtleID") h.writeLine(TurtleID)
  249.     h.writeLine("TravelY") h.writeLine(TravelY)
  250.     h.writeLine("lowFuel") h.writeLine(lowFuel)
  251.     h.writeLine("curJob") h.writeLine(curJob)
  252.     h.writeLine("autoJobRequest") h.writeLine(autoJobRequest)
  253.     h.writeLine("updateTerm") h.writeLine(updateTerm)
  254.     h.writeLine("isWireless") h.writeLine(isWireless)
  255.     h.writeLine("isAggressive") h.writeLine(isAggressive)
  256.     h.writeLine("supplyFuelChest") h.writeLine(supplyFuelChest)
  257.     h.writeLine("dumpCobblestone") h.writeLine(dumpCobblestone)
  258.     h.writeLine("dumpDirt") h.writeLine(dumpDirt)
  259.     h.writeLine("dumpGravel") h.writeLine(dumpGravel)
  260.     h.writeLine("checkIfTurtle") h.writeLine(checkIfTurtle)
  261.     h.writeLine("doDebugLog") h.writeLine(doDebugLog)
  262.     h.close()
  263. end
  264.  
  265. function loadData()
  266. local temp = nil
  267. if Debug then print("Loading data") sleep(3) end
  268.     if fs.exists("data/position") then
  269.         local h = fs.open("data/position", "r")
  270.         if h.readLine() == "TurtleX" then
  271.         TurtleX = math.floor(tonumber(h.readLine())) else h.readLine() end
  272.         if h.readLine() == "TurtleZ" then
  273.         TurtleZ = math.floor(tonumber(h.readLine())) else h.readLine() end
  274.         if h.readLine() == "TurtleY" then
  275.         TurtleY = math.floor(tonumber(h.readLine())) else h.readLine() end
  276.         if h.readLine() == "TurtleDirection" then
  277.         TurtleDirection = math.floor(tonumber(h.readLine())) end
  278.         h.close()
  279.     end
  280.  
  281.     if fs.exists("data/variables") then
  282.         local h = fs.open("data/variables", "r")
  283.         if h.readLine() == "HomeX" then
  284.         HomeX = math.floor(tonumber(h.readLine())) else h.readLine() end
  285.         if h.readLine() == "HomeZ" then
  286.         HomeZ = math.floor(tonumber(h.readLine())) else h.readLine() end
  287.         if h.readLine() == "HomeY" then
  288.         HomeY = math.floor(tonumber(h.readLine())) else h.readLine() end
  289.         if h.readLine() == "HomeFacing" then
  290.         HomeFacing = math.floor(tonumber(h.readLine())) else h.readLine() end
  291.         if h.readLine() == "preHomeX" then
  292.         preHomeX = math.floor(tonumber(h.readLine())) else h.readLine() end
  293.         if h.readLine() == "preHomeZ" then
  294.         preHomeZ = math.floor(tonumber(h.readLine())) else h.readLine() end
  295.         if h.readLine() == "preHomeY" then
  296.         preHomeY = math.floor(tonumber(h.readLine())) else h.readLine() end
  297.         if h.readLine() == "TurtleID" then
  298.         TurtleID = math.floor(tonumber(h.readLine())) else h.readLine() end
  299.         if h.readLine() == "TravelY" then
  300.         TravelY = math.floor(tonumber(h.readLine())) else h.readLine() end
  301.         if h.readLine() == "lowFuel" then
  302.         lowFuel = math.floor(tonumber(h.readLine())) else h.readLine() end
  303.         if h.readLine() == "curJob" then
  304.         curJob = math.floor(tonumber(h.readLine())) else h.readLine() end
  305.        
  306.         if h.readLine() == "autoJobRequest" then
  307.         autoJobRequest = h.readLine()
  308.         autoJobRequest = autoJobRequest == "true" else h.readLine() end
  309.         if h.readLine() == "updateTerm" then
  310.         updateTerm = h.readLine()
  311.         updateTerm = updateTerm == "true" else h.readLine() end
  312.         if h.readLine() == "isWireless" then
  313.         isWireless = h.readLine()
  314.         isWireless = isWireless == "true" else h.readLine() end
  315.         if h.readLine() == "isAggressive" then
  316.         isAggressive = h.readLine()
  317.         isAggressive = isAggressive == "true" else h.readLine() end
  318.         if h.readLine() == "supplyFuelChest" then
  319.         supplyFuelChest = h.readLine()
  320.         supplyFuelChest = supplyFuelChest == "true" else h.readLine() end
  321.         if h.readLine() == "dumpCobblestone" then
  322.         dumpCobblestone = h.readLine()
  323.         dumpCobblestone = dumpCobblestone == "true" else h.readLine() end
  324.         if h.readLine() == "dumpDirt" then
  325.         dumpDirt = h.readLine()
  326.         dumpDirt = dumpDirt == "true" else h.readLine() end
  327.         if h.readLine() == "dumpGravel" then
  328.         dumpGravel = h.readLine()
  329.         dumpGravel = dumpGravel == "true" else h.readLine() end
  330.         if h.readLine() == "checkIfTurtle" then
  331.         checkIfTurtle = h.readLine()
  332.         checkIfTurtle = checkIfTurtle == "true" else h.readLine() end
  333.         if h.readLine() == "doDebugLog" then
  334.         doDebugLog = h.readLine()
  335.         doDebugLog = doDebugLog == "true" end
  336.        
  337.         h.close()
  338.     end
  339.     stName1 = ("Quarry Turtle "..tostring(TurtleID).." :: Fuel") --Name in statList
  340.     stName2 = ("Quarry Turtle "..tostring(TurtleID).." :: Inventory") -- -||-
  341. end
  342.  
  343. function generateLog(text)
  344.     if doDebugLog then
  345.         if not fs.exists("data") then fs.makeDir("data") end
  346.         local h = fs.open("data/Log", "a") --'a' - Add to previous
  347.         h.writeLine(text)
  348.         h.close()
  349.     end
  350. end
  351.  
  352. function textColor(color)
  353.     if term.isColor() then
  354.         if color == "white" then
  355.             term.setTextColor(1)
  356.         elseif color == "orange" then
  357.             term.setTextColor(2)
  358.         elseif color == "magneta" then
  359.             term.setTextColor(4)
  360.         elseif color == "lightBlue" then
  361.             term.setTextColor(8)
  362.         elseif color == "yellow" then
  363.             term.setTextColor(16)
  364.         elseif color == "lime" then
  365.             term.setTextColor(32)
  366.         elseif color == "pink" then
  367.             term.setTextColor(64)
  368.         elseif color == "gray" then
  369.             term.setTextColor(150)
  370.         elseif color == "lightGray" then
  371.             term.setTextColor(256)
  372.         elseif color == "cyan" then
  373.             term.setTextColor(512)
  374.         elseif color == "purple" then
  375.             term.setTextColor(1024)
  376.         elseif color == "blue" then
  377.             term.setTextColor(2048)
  378.         elseif color == "brown" then
  379.             term.setTextColor(4096)
  380.         elseif color == "green" then
  381.             term.setTextColor(8192)
  382.         elseif color == "red" then
  383.             term.setTextColor(16384)
  384.         elseif color == "black" then
  385.             term.setTextColor(32768)
  386.         end
  387.     end
  388. end
  389.  
  390. function valueEdit(buttonNum, variable, minValue, maxValue)
  391.     local x = (#menus[curMenu][buttonNum][3]-#tostring(variable))+3
  392.     local y = menus[curMenu][buttonNum][2]
  393.     term.setCursorPos(x,y)
  394.     for u=1,#tostring(variable) do
  395.         write(" ")
  396.         sleep(0)
  397.     end
  398.     term.setCursorPos(x,y)
  399.     local temp = read()
  400.  
  401.     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
  402. return variable
  403. end
  404.  
  405. function requestJob()
  406.     if jobs[curJob] == "Quarry" then
  407.         --print("Quarry") sleep(3)
  408.         sendToServer("quarry", "request")
  409.         sentRequest = true
  410.     elseif jobs[curJob] == "Bomb" then
  411.         --print("Bomb") sleep(3)
  412.         sendToServer("bomb")
  413.         sentRequest = true
  414.     end
  415. end
  416.  
  417. function handleButtons()
  418.     if nextAction == "menuVariableSetup" then
  419.         curMenu = 1
  420.         curSelection = 1
  421.     elseif nextAction == "menuVariable2Setup" then
  422.         curMenu = 2
  423.         curSelection = 2
  424.     elseif nextAction == "menuTurtleSetup" then
  425.         curMenu = 3
  426.         curSelection = 2
  427.         stName1 = ("Quarry Turtle "..tostring(TurtleID).." :: Fuel") --Name in statList
  428.         stName2 = ("Quarry Turtle "..tostring(TurtleID).." :: Inventory") -- -||-
  429.     elseif nextAction == "menuHomeSetup" then
  430.         curMenu = 4
  431.         curSelection = 2
  432.     elseif nextAction == "menuPreHomeSetup" then
  433.         curMenu = 5
  434.         curSelection = 2
  435.     elseif nextAction == "menuOverview" then
  436.         curMenu = 6
  437.         curSelection = 1
  438.     elseif nextAction == "menuJobSetup" then
  439.         curMenu = 7
  440.         curSelection = 1
  441.     elseif nextAction == "funEditTurtleID" then
  442.         TurtleID = valueEdit(3, TurtleID, 1, 9999)
  443.         if saveData then saveData() end
  444.     elseif nextAction == "funClearTurtleID" then
  445.         TurtleID = 1
  446.         if saveData then saveData() end
  447.     elseif nextAction == "funEditTravelY" then
  448.         TravelY = valueEdit(4, TravelY, 1, 999)
  449.         if saveData then saveData() end
  450.     elseif nextAction == "funClearTravelY" then
  451.         TravelY = 0
  452.         if saveData then saveData() end
  453.     elseif nextAction == "funEditLowFuel" then
  454.         lowFuel = valueEdit(5, lowFuel, 100, 10000)
  455.         if saveData then saveData() end
  456.     elseif nextAction == "funClearLowFuel" then
  457.         lowFuel = 1000
  458.         if saveData then saveData() end
  459.     elseif nextAction == "funSwitchUpdateTerm" then
  460.         updateTerm = not updateTerm
  461.         if saveData then saveData() end
  462.     elseif nextAction == "funSwitchIsWireless" then
  463.         isWireless = not isWireless
  464.         if saveData then saveData() end
  465.     elseif nextAction == "funSwitchSupplyFuelChest" then
  466.         supplyFuelChest = not supplyFuelChest
  467.         if saveData then saveData() end
  468.     elseif nextAction == "funSwitchIsAggressive" then
  469.         isAggressive = not isAggressive
  470.         if saveData then saveData() end
  471.     elseif nextAction == "funSwitchDumpCobblestone" then
  472.         dumpCobblestone = not dumpCobblestone
  473.         if saveData then saveData() end
  474.     elseif nextAction == "funSwitchDumpDirt" then
  475.         dumpDirt = not dumpDirt
  476.         if saveData then saveData() end
  477.     elseif nextAction == "funSwitchDumpGravel" then
  478.         dumpGravel = not dumpGravel
  479.         if saveData then saveData() end
  480.     elseif nextAction == "funSwitchCheckIfTurtle" then
  481.         checkIfTurtle = not checkIfTurtle
  482.         if saveData then saveData() end
  483.     elseif nextAction == "funSwitchDoDebugLog" then
  484.         doDebugLog = not doDebugLog
  485.         if saveData then saveData() end
  486.     elseif nextAction == "funEditTurtleX" then
  487.         TurtleX = valueEdit(4, TurtleX, -99999, 99999)
  488.         if saveData then saveData(true) end
  489.     elseif nextAction == "funClearTurtleX" then
  490.         TurtleX = 0
  491.         if saveData then saveData(true) end
  492.     elseif nextAction == "funEditTurtleZ" then
  493.         TurtleZ = valueEdit(5, TurtleZ, -99999, 99999)
  494.         if saveData then saveData(true) end
  495.     elseif nextAction == "funClearTurtleZ" then
  496.         TurtleZ = 0
  497.         if saveData then saveData(true) end
  498.     elseif nextAction == "funEditTurtleY" then
  499.         TurtleY = valueEdit(6, TurtleY, 1, 999)
  500.         if saveData then saveData(true) end
  501.     elseif nextAction == "funClearTurtleY" then
  502.         TurtleY = 0
  503.         if saveData then saveData(true) end
  504.     elseif nextAction == "funNextTurtleDirection" then
  505.         if TurtleDirection+1 > 3 then
  506.             TurtleDirection = 0
  507.         else
  508.             TurtleDirection = TurtleDirection+1
  509.         end
  510.         if saveData then saveData(true) end
  511.     elseif nextAction == "funPreviousTurtleDirection" then
  512.         if TurtleDirection-1 < 0 then
  513.             TurtleDirection = 3
  514.         else
  515.             TurtleDirection = TurtleDirection-1
  516.         end
  517.         if saveData then saveData(true) end
  518.     elseif nextAction == "funEditHomeX" then
  519.         HomeX = valueEdit(4, HomeX, -99999, 99999)
  520.         if saveData then saveData() end
  521.     elseif nextAction == "funClearHomeX" then
  522.         HomeX = 0
  523.         if saveData then saveData() end
  524.     elseif nextAction == "funEditHomeZ" then
  525.         HomeZ = valueEdit(5, HomeZ, -99999, 99999)
  526.         if saveData then saveData() end
  527.     elseif nextAction == "funClearHomeZ" then
  528.         HomeZ = 0
  529.         if saveData then saveData() end
  530.     elseif nextAction == "funEditHomeY" then
  531.         HomeY = valueEdit(6, HomeY, 1, 999)
  532.         if saveData then saveData() end
  533.     elseif nextAction == "funClearHomeY" then
  534.         HomeY = 0
  535.         if saveData then saveData() end
  536.     elseif nextAction == "funNextHomeFacing" then
  537.         if HomeFacing+1 > 3 then
  538.             HomeFacing = 0
  539.         else
  540.             HomeFacing = HomeFacing+1
  541.         end
  542.         if saveData then saveData() end
  543.     elseif nextAction == "funPreviousHomeFacing" then
  544.         if HomeFacing-1 < 0 then
  545.             HomeFacing = 3
  546.         else
  547.             HomeFacing = HomeFacing-1
  548.         end
  549.         if saveData then saveData() end    
  550.     elseif nextAction == "funEditPreHomeX" then
  551.         preHomeX = valueEdit(4, preHomeX, -99999, 99999)
  552.         if saveData then saveData() end
  553.     elseif nextAction == "funClearPreHomeX" then
  554.         preHomeX = 0
  555.         if saveData then saveData() end
  556.     elseif nextAction == "funEditPreHomeZ" then
  557.         preHomeZ = valueEdit(5, preHomeZ, -99999, 99999)
  558.         if saveData then saveData() end
  559.     elseif nextAction == "funClearPreHomeZ" then
  560.         preHomeZ = 0
  561.         if saveData then saveData() end
  562.     elseif nextAction == "funEditPreHomeY" then
  563.         preHomeY = valueEdit(6, preHomeY, 1, 999)
  564.         if saveData then saveData() end
  565.     elseif nextAction == "funClearPreHomeY" then
  566.         preHomeY = 0
  567.         if saveData then saveData() end
  568.     elseif nextAction == "funNextCurJob" then
  569.         if curJob+1 > #jobs then
  570.             curJob = 1
  571.         else
  572.             curJob = curJob+1
  573.         end
  574.         saveData(false) -- Don't save position
  575.     elseif nextAction == "funPreviousCurJob" then
  576.         if curJob-1 < 1 then
  577.             curJob = #jobs
  578.         else
  579.             curJob = curJob-1
  580.         end
  581.         saveData(false) -- Don't save position
  582.     elseif nextAction == "funSwitchAutoJobRequest" then
  583.         autoJobRequest = not autoJobRequest
  584.         saveData(false) -- Don't save position
  585.         if autoJobRequest then requestJob() end
  586.     elseif nextAction == "funRequestJob" then
  587.         sentRequest = false
  588.         requestJob()
  589.     end
  590. end
  591.  
  592. function isEvenSize()
  593.   if math.floor((quarrySize/2)+0.99) > (quarrySize/2) then
  594.     return false
  595.   else
  596.     return true
  597.   end
  598. end
  599.  
  600. function emptyInv()--supplyFuelChest
  601.     faceDirection(Directions[HomeFacing+1])
  602.     for u=1, 16 do
  603.         turtle.select(u)
  604.         item = turtle.getItemDetail()
  605.         if item then --If not nil
  606.             if  item.name == "minecraft:coal" and supplyFuelChest then turtle.dropDown() end -- If fail then drop in normal chest
  607.         while not turtle.drop() do if turtle.getItemSpace() == 64 then break end sleep(5) end
  608.         sleep(0)
  609.         end
  610.     end
  611.     turtle.select(1)
  612.     turtleInvFull = 0
  613.     faceDirection(Directions[HomeFacing])
  614.     sendToServer("status", false)
  615. end
  616.  
  617. function goHome(ifUnload)
  618.     goto(preHomeX, preHomeZ, preHomeY)
  619.     goto(HomeX, HomeZ, HomeY, false)
  620.     faceDirection(Directions[HomeFacing])
  621.     if ifUnload then emptyInv() end
  622. end
  623.  
  624. function turtleRefuel()
  625.     if unload then emptyInv() end
  626.     for i=1, 16 do
  627.         turtle.select(i)
  628.         if turtle.getItemSpace() == 64 then
  629.             break
  630.         end
  631.         sleep(0)
  632.     end
  633.     while true do
  634.         if not turtle.suckDown(8) then sleep(10) turtle.suckDown(8) end
  635.         turtle.refuel()
  636.         if turtle.getFuelLevel() > lowFuel+refuelAmount then
  637.             break
  638.         end
  639.         sleep(0)
  640.     end
  641.     faceDirection(Directions[HomeFacing])
  642.     generateLog("Refueled")
  643.     sendToServer("status", false)
  644. end
  645.  
  646. function checkFuel()
  647. generateLog("Checking fuel level")
  648.     if turtle.getFuelLevel() <= lowFuel then
  649.         lastX = TurtleX
  650.         lastZ = TurtleZ
  651.         lastY = TurtleY
  652.         lastDirection = TurtleDirection
  653.         goHome()
  654.         turtleRefuel()
  655.         generateLog("Returning to home")
  656.         sendToServer("status", false)
  657.         goto(preHomeX, preHomeZ, preHomeY, false)
  658.         generateLog("Heading to last coord")
  659.         goto(lastX, lastZ, lastY)
  660.         generateLog("Arrived at last coord")
  661.         faceDirection(Directions[lastDirection])
  662.         sendToServer("status", false)
  663.     end
  664. generateLog("Checked fuel")
  665. end
  666.  
  667. function turtleInv(empty)
  668.     local temp = 0
  669.     local tempPrecise = 0
  670.     for i=1, 16 do
  671.         turtle.select(i)
  672.         item = turtle.getItemDetail()
  673.         if item then --If not nil
  674.             if  item.name == "minecraft:cobblestone" and dumpCobblestone then turtle.drop()
  675.             elseif item.name == "minecraft:dirt" and dumpDirt then turtle.drop()
  676.             elseif item.name == "minecraft:gravel" and dumpGravel then turtle.drop()
  677.             else
  678.             if turtle.getItemSpace() < 64 then temp=temp+1 end
  679.             tempPrecise = tempPrecise + turtle.getItemCount()
  680.             sleep(0)
  681.             end
  682.         end
  683.     end
  684.     turtle.select(1)
  685.     turtleInvFull = temp
  686.     turtleInvExact = tempPrecise
  687.     if turtleInvFull == 16 and empty == true then
  688.         lastX = TurtleX
  689.         lastZ = TurtleZ
  690.         lastY = TurtleY
  691.         lastDirection = TurtleDirection
  692.         generateLog("Full inventory, will go home to dump")
  693.         sendToServer("status", false)
  694.         goHome()
  695.         sendToServer("status", false)
  696.         emptyInv()
  697.         goto(preHomeX, preHomeZ, preHomeY, false)
  698.         goto(lastX, lastZ, lastY)
  699.         faceDirection(Directions[lastDirection])
  700.         generateLog("Emptied inv and back in place - is now ready to go again")
  701.         sendToServer("status", false)
  702.     end
  703. end
  704.  
  705. function waitForEvent()
  706. generateLog("Waiting for answer/event")
  707. currentlyDoing = "Waiting for a job.."
  708. ScreenCleanUp()
  709. ScreenUpdate()
  710.     event, p1, sChannel, rChannel, message, sDis = os.pullEvent()
  711.     if event == "modem_message" then   
  712.         local tempMessage = textutils.unserialize(message)
  713.         if tempMessage == nil then
  714.             generateLog("Message received gave nil after .unserialize(message)")
  715.         else
  716.         generateLog("Received valid message!")
  717.             if tempMessage[1] == TurtleID then --If refered to
  718.                 generateLog("Server talking to me!")
  719.                 if tempMessage[2] == "newquarry" then
  720.                     generateLog("NewQuarry order received")
  721.                     depth = tempMessage[6]
  722.                     quarryNum = tempMessage[7]
  723.                     quarrySize = tempMessage[8]
  724.                     quarry(tempMessage[3], tempMessage[4], tempMessage[5])
  725.                 elseif tempMessage[2] == "bomb" then
  726.                     bomb(tempMessage[3],tempMessage[4],tempMessage[5],tempMessage[6],tempMessage[7],tempMessage[8])
  727.                
  728.                 --elseif tempMessage[2] == "finished" then
  729.                     --Nuthin'
  730.                 end
  731.             else
  732.                 generateLog("Server not talking to me :<") generateLog("My ID: "..TurtleID..", Message ID: "..tostring(tempMessage[1]))
  733.             end
  734.         end
  735.     -- elseif event == "char" then
  736.         -- if p1 == "t" then
  737.             -- keepGoing = false
  738.         -- end
  739.     elseif event == "key" then  --  x=2, #menus[curMenu]
  740.         if p1 == 20 then -- "t"
  741.             keepGoing = false
  742.         elseif p1 == 19 then -- "r"
  743.             if fs.exists("data/Log") then fs.delete("data/Log") end
  744.         elseif p1 == 28 then -- Enter
  745.             --if not menus[curMenu][curSelection+1][4] == nil then
  746.                 nextAction = menus[curMenu][curSelection+1][4]
  747.             --end
  748.         elseif p1 == 14 then -- Backspace
  749.             --if not menus[curMenu][curSelection+1][5] == nil then
  750.                 nextAction = menus[curMenu][curSelection+1][5]
  751.             --end
  752.         elseif p1 == 200 then -- Arrow Up
  753.             if curSelection-1 < 1 then
  754.                 curSelection = #menus[curMenu]-1
  755.             else
  756.                 curSelection = curSelection-1
  757.             end        
  758.         elseif p1 == 208 then -- Arrow Down
  759.             if curSelection+1 > #menus[curMenu]-1 then
  760.                 curSelection = 1
  761.             else
  762.                 curSelection = curSelection+1
  763.             end
  764.         --else
  765.             --print("Key pressed: "..tostring(p1))
  766.             --sleep(2)
  767.         end
  768.         handleButtons()
  769.         nextAction = ""
  770.     end
  771. end
  772.  
  773. function sendToServer(mode, param)
  774. generateLog("Entering sendToServer("..mode..", "..tostring(param)..")")
  775.     if mode == "quarry" then
  776.         generateLog("Quarry request message setting up")
  777.         local dataTable = {}
  778.         dataTable[1] = TurtleID
  779.         dataTable[2] = "Quarry"
  780.         dataTable[3] = TurtleX
  781.         dataTable[4] = TurtleZ
  782.         dataTable[5] = TurtleY
  783.         if param == "request" then
  784.             dataTable[6] = "request"
  785.         elseif param == "finished" then
  786.             dataTable[6] = "finished"
  787.             dataTable[7] = quarryNum
  788.         end
  789.         message = textutils.serialize(dataTable)
  790.         if isWireless then modem.transmit(listenCommonChannel, officialHangoutChannel, message) end
  791.         generateLog("Sent quarry request message")
  792.     elseif mode == "status" then
  793.         local dataTable = {}
  794.         dataTable[1] = TurtleID
  795.         dataTable[2] = "Status"
  796.         dataTable[3] = TurtleX
  797.         dataTable[4] = TurtleZ
  798.         dataTable[5] = TurtleY
  799.     generateLog("Checking FuelLevel() for status message")
  800.         dataTable[6] = turtle.getFuelLevel()
  801.         dataTable[7] = stMax1
  802.         dataTable[8] = stName1
  803.     generateLog("Checking turtleInv(param) for status message")
  804.         turtleInv(param) --Updates inv. and if true -unloads it
  805.         dataTable[9] = turtleInvFull
  806.         dataTable[10] = turtleInvExact
  807.         dataTable[11] = stMax2
  808.         dataTable[12] = stName2
  809.     generateLog("About to send status message")
  810.         local messageToSend = textutils.serialize(dataTable)
  811.         if isWireless then modem.transmit(listenCommonChannel, officialHangoutChannel, messageToSend) end
  812.     generateLog("Sent status message")
  813.         --print()
  814.         --print("Sent status")
  815.     elseif mode == "bomb" then
  816.         local dataTable = {}
  817.         dataTable[1] = TurtleID
  818.         dataTable[2] = "Bomb"
  819.         dataTable[3] = TurtleX
  820.         dataTable[4] = TurtleZ
  821.         dataTable[5] = TurtleY
  822.         message = textutils.serialize(dataTable)
  823.         if isWireless then modem.transmit(listenCommonChannel, officialHangoutChannel, message) end
  824.     end
  825.     generateLog("Returning from sendToServer()")
  826. end
  827.  
  828. function faceDirection(direction)
  829. local temp = 0
  830.     while true do
  831.         if tostring(Directions[temp]) == tostring(direction) then
  832.             break
  833.         else
  834.             temp=(temp+1)
  835.         end
  836.         sleep(0)
  837.     end
  838.     if Debug then
  839.         print("Searched: "..tostring(direction))
  840.         print("Found: "..tostring(Directions[temp]))
  841.     end
  842.    
  843.     while true do
  844.         if tostring(Directions[TurtleDirection]) == tostring(Directions[temp]) then
  845.             break
  846.         else
  847.             if turtle.turnRight() then
  848.                 TurtleDirection = (TurtleDirection + 1)
  849.                 if TurtleDirection > 3 then
  850.                     TurtleDirection = 0
  851.                 end
  852.             end
  853.         end
  854.         sleep(0)
  855.     end
  856.     if savePositionData then if saveData then saveData() end end
  857.     if Debug then
  858.         print("Orderd: "..tostring(Directions[temp]))
  859.         print("Did: "..tostring(Directions[TurtleDirection]))
  860.     end
  861. end
  862.  
  863. function goForward()
  864.     if checkIfTurtle then
  865.         local block = turtle.inspect()
  866.         if not block == nil then
  867.             if block.name == "ComputerCraft:CC-Turtle" or block.name == "ComputerCraft:CC-TurtleAdvanced" then
  868.                 sleep(2)
  869.             end
  870.         end
  871.     end
  872.     while not turtle.forward() do
  873.         turtle.dig()
  874.         if isAggressive then turtle.attack() end
  875.         sleep(0)
  876.     end
  877.     if TurtleDirection == 0 then --South
  878.         TurtleZ = (TurtleZ + 1)
  879.     elseif TurtleDirection == 1 then --West
  880.         TurtleX = (TurtleX - 1)
  881.     elseif TurtleDirection == 2 then --North
  882.         TurtleZ = (TurtleZ - 1)
  883.     elseif TurtleDirection == 3 then --East
  884.         TurtleX = (TurtleX + 1)
  885.     end
  886.     if updateTerm then ScreenCleanUp() ScreenUpdate() end
  887.     if savePositionData then if saveData then saveData() end end
  888. end
  889.  
  890. function goUp()
  891.     if checkIfTurtle then
  892.         local block = turtle.inspectUp()
  893.         if not block == nil then
  894.             if block.name == "ComputerCraft:CC-Turtle" or block.name == "ComputerCraft:CC-TurtleAdvanced" then
  895.                 sleep(2)
  896.             end
  897.         end
  898.     end
  899.     while not turtle.up() do
  900.         turtle.digUp()
  901.         if isAggressive then turtle.attackUp() end
  902.         sleep(0)
  903.     end
  904.     TurtleY = (TurtleY + 1)
  905.     if updateTerm then ScreenCleanUp() ScreenUpdate() end
  906.     if savePositionData then if saveData then saveData() end end
  907. end
  908.  
  909. function goDown()
  910.     if checkIfTurtle then
  911.         local block = turtle.inspectDown()
  912.         if not block == nil then
  913.             if block.name == "ComputerCraft:CC-Turtle" or block.name == "ComputerCraft:CC-TurtleAdvanced" then
  914.                 sleep(2)
  915.             end
  916.         end
  917.     end
  918.     while not turtle.down() do
  919.         turtle.digDown()
  920.         if isAggressive then turtle.attackDown() end
  921.         sleep(0)
  922.     end
  923.     TurtleY = (TurtleY - 1)
  924.     if updateTerm then ScreenCleanUp() ScreenUpdate() end
  925.     if savePositionData then if saveData then saveData() end
  926.     end
  927. end
  928.  
  929. function bomb(bombX, bombZ, bombY, bombDirection,bombRunLength, numRebomb)
  930. currentlyDoing = "Bombing.."
  931. sendToServer("status")
  932. checkFuel()
  933. goto(preHomeX, preHomeZ, preHomeY, false) --Do not use TravelHeight
  934. goto(bombX, bombZ, (bombY+bombDetonationTiming))
  935. if bombDirection == nil then bombDirection = "North" end --Prevents error if value is forgotten to be assigned
  936. faceDirection(bombDirection)
  937. if numRebomb == nil then numRebomb =  1 end --Prevents error if value is forgotten to be assigned
  938. if bombRunLength == nil then bombRunLength = 1 end --Prevents error if value is forgotten to be assigned
  939. local bombRun = 0
  940. while true do
  941. sendToServer("status")
  942.     bombRun=bombRun+1
  943.     if  bombRun > numRebomb then --If done, then head home
  944.         break
  945.     else
  946.         -- Going for a bombing run
  947.         turtle.select(1)
  948.         local temp = 1 -- Inv. selection variable
  949.         for i=1, bombRunLength do
  950.             while true do
  951.                 if turtle.getItemSpace() == 64 then
  952.                     temp=temp+1
  953.                     if temp > 16 then
  954.                         break
  955.                     else
  956.                         turtle.select(temp)
  957.                     end
  958.                 else
  959.                     break
  960.                 end
  961.                 sleep(0)
  962.             end
  963.             if temp > 16 then
  964.                 break
  965.             else
  966.                 turtle.place()
  967.                 redstone.setOutput("front", true)
  968.                 sleep(2)
  969.                 goForward()
  970.                 goForward()
  971.             end
  972.             sleep(0)
  973.         end
  974.         if temp > 16 then
  975.             break
  976.         else
  977.             redstone.setOutput("front", false)
  978.             turtle.select(1)
  979.             if TurtleDirection+1 > 3 then faceDirection(Directions[0]) --Turn right
  980.             else faceDirection(Directions[TurtleDirection+1]) end
  981.             if TurtleDirection+1 > 3 then faceDirection(Directions[0]) --Turn right
  982.             else faceDirection(Directions[TurtleDirection+1]) end      
  983.         end
  984.         sleep(0)
  985.     end
  986. end
  987.  
  988. if sentRequest then
  989.     sentRequest = false
  990. end
  991. goHome(false)--Will NOT empty inv when home
  992. currentlyDoing = "Waiting for job.."
  993. sendToServer("status")
  994. end
  995.  
  996. function quarry(QuarryX, QuarryZ, QuarryY)
  997. currentlyDoing = "Quarrying.."
  998. local dig = true
  999. checkFuel()
  1000. goto(preHomeX, preHomeZ, preHomeY, false)
  1001. goto(QuarryX, QuarryZ, QuarryY+1)
  1002. faceDirection("North")
  1003. local turnRight = true
  1004. checkFuel()
  1005. turtle.select(1)
  1006. turtle.digUp()
  1007. turtle.digDown()
  1008. while dig do
  1009.         for x=1, quarrySize do --j
  1010.                 for z=1, quarrySize-1 do --i
  1011.                     goForward()
  1012.                     while turtle.digUp() do
  1013.                     --Just to get rid of all sand/gravel
  1014.                     end
  1015.                     turtle.digDown()
  1016.                     sleep(0)
  1017.                 end
  1018.                 generateLog("SendToServer() | X: "..tostring(x).." Y: "..tostring(z))
  1019.             sendToServer("status", true)
  1020.             generateLog("Going to check fuel now: "..tostring(turtle.getFuelLevel()))
  1021.             checkFuel()
  1022.             generateLog("MADE IT")
  1023.             if turnRight then
  1024.                 if TurtleDirection+1 > 3 then
  1025.                     faceDirection(Directions[0])
  1026.                 else
  1027.                     faceDirection(Directions[TurtleDirection+1])
  1028.                 end
  1029.                
  1030.                
  1031.                 if quarrySize > x then goForward() end
  1032.                 while turtle.digUp() do
  1033.                     --Just to get rid of all sand/gravel
  1034.                 end
  1035.                 turtle.digDown()
  1036.                 if TurtleDirection+1 > 3 then
  1037.                     faceDirection(Directions[0])
  1038.                 else
  1039.                     faceDirection(Directions[TurtleDirection+1])
  1040.                 end
  1041.                 turnRight = false
  1042.             else
  1043.                 if TurtleDirection-1 < 0 then
  1044.                     faceDirection(Directions[3])
  1045.                 else
  1046.                     faceDirection(Directions[TurtleDirection-1])
  1047.                 end
  1048.                 if quarrySize > x then goForward() end
  1049.                 while turtle.digUp() do
  1050.                 --Just to get rid of all sand/gravel
  1051.                 end
  1052.                 turtle.digDown()
  1053.                 if TurtleDirection-1 < 0 then
  1054.                     faceDirection(Directions[3])
  1055.                 else
  1056.                     faceDirection(Directions[TurtleDirection-1])
  1057.                 end
  1058.                 turnRight = true
  1059.             end
  1060.             sleep(0)
  1061.         end
  1062.         if isEvenSize() then faceDirection("North") end
  1063.         turnRight = not turnRight
  1064.         --checkFuel()
  1065.         if TurtleY-1 <= depth then
  1066.             dig = false
  1067.             break
  1068.         elseif TurtleY-4 <= depth then
  1069.             for i=1, 4 do --    70 - 67
  1070.                 if TurtleY-i <= depth then
  1071.                     for i=1, (i-1) do
  1072.                         goDown()
  1073.                         turtle.digDown()
  1074.                         sleep(0)
  1075.                     end
  1076.                     break
  1077.                 end
  1078.                 sleep(0)
  1079.             end
  1080.         else
  1081.             for i=1, 3 do
  1082.                 goDown()
  1083.                 turtle.digDown()
  1084.                 sleep(0)
  1085.             end
  1086.         end
  1087.     end
  1088.     sleep(0)
  1089.     if quarryNum > -1 then sendToServer("quarry", "finished") quarryNum = -1 end --Just to make sure order came from server
  1090.     currentlyDoing = "Heading home.."
  1091.     if sentRequest then
  1092.         sentRequest = false
  1093.     end
  1094.     goHome(true)--Will empty inv when home
  1095.     currentlyDoing = "Waiting.."
  1096. end
  1097.  
  1098. function goto(TargetX,TargetZ,TargetY, ifTravelHeight)
  1099.   --Traveling Y-Coord
  1100.   if ifTravelHeight == nil then
  1101.     while true do
  1102.         if TurtleY < TravelY then
  1103.             goUp()
  1104.             --TurtleY = (TurtleY + 1)
  1105.         elseif TurtleY > TravelY then
  1106.             goDown()
  1107.             --TurtleY = (TurtleY - 1)
  1108.         end
  1109.         if TurtleY == TravelY then
  1110.             break
  1111.         end
  1112.         sleep(0)
  1113.     end
  1114.   end
  1115.   --Go X-Coord
  1116.   while true do
  1117.       if TurtleX < TargetX then
  1118.         faceDirection("East")
  1119.         goForward()
  1120.         --TurtleX = (TurtleX + 1)
  1121.       elseif TurtleX > TargetX then
  1122.         faceDirection("West")
  1123.         goForward()
  1124.         --TurtleX = (TurtleX - 1)
  1125.       end
  1126.         if TurtleX == TargetX then
  1127.             break
  1128.         end
  1129.         sleep(0)
  1130.   end
  1131.   --Go Z-Coord
  1132.     while true do
  1133.         if TurtleZ < TargetZ then
  1134.             faceDirection("South")
  1135.             goForward()
  1136.             --TurtleZ = (TurtleZ + 1)
  1137.         elseif TurtleZ > TargetZ then
  1138.             faceDirection("North")
  1139.             goForward()
  1140.             --TurtleZ = (TurtleZ - 1)
  1141.         end
  1142.         if TurtleZ == TargetZ then
  1143.             break
  1144.         end
  1145.         sleep(0)
  1146.   end
  1147.   --Go Y-Coord
  1148.     while true do
  1149.         if TurtleY < TargetY then
  1150.             goUp()
  1151.             --TurtleY = (TurtleY + 1)
  1152.         elseif TurtleY > TargetY then
  1153.             goDown()
  1154.             --TurtleY = (TurtleY - 1)
  1155.         end
  1156.         if TurtleY == TargetY then
  1157.             break
  1158.         end
  1159.         sleep(0)
  1160.     end
  1161. end
  1162.  
  1163. ------------/Functions----------------
  1164. ------------Launching Code------------
  1165. if loadPriorData then loadData() end
  1166. generateLog("Starting program")
  1167. sendToServer("status", true)
  1168.  
  1169.  
  1170. while keepGoing do
  1171.  
  1172. ScreenCleanUp()
  1173. ScreenUpdate()
  1174. if turtle.getFuelLevel() <= lowFuel then turtleRefuel() end
  1175. if autoJobRequest and curMenu >= 5 then
  1176.     if not sentRequest then
  1177.         requestJob()
  1178.     end
  1179. end
  1180. waitForEvent()
  1181. sleep(0)
  1182. end
  1183. generateLog("Program shutdown")
  1184. generateLog("----------------")
  1185. ScreenCleanUp()
  1186. print("Drone program has been shutdown..")
  1187. print()
  1188.  
  1189. -----------/Launching Code------------
  1190. ---------____________________---------
  1191. -------//  | Drone v.3.2  |  \\-------
  1192. -------|| Made By: RedCowboy ||-------
  1193. --------\\_______2014_______//--------
  1194. --------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement