Advertisement
SirDimples

PrepForQuarry_TurtleMC

Jun 3rd, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.00 KB | None | 0 0
  1. -- ********************************************************************
  2. -- ** Prep for Quarry - Turtle (Tekkit/Minecraft) by SirDimples      **
  3. -- ** ComputerCraft 1.58                                             **
  4. -- **                                                                **
  5. -- ** Features:                                                      **
  6. -- ** - TODO                                                         **
  7. -- **                                                                **
  8. -- ** Usage:                                                         **
  9. -- **       PrepQuarry width length startheight                  **
  10. -- **                                                                **
  11. -- ** Versions/Changelog:                                            **
  12. -- ** [0.0] First submit                                             **
  13. -- **                                                                **
  14. -- ** Warning: script is work in progress                            **
  15. -- ********************************************************************
  16.  
  17. local fuelLevelToRefuelAt = 5
  18. local currentlySelectedSlot = 0 -- The slot that the last noise block was found in
  19. local fuelLevelToRefuelAt = 5
  20. local refuelItemsToUseWhenRefuelling = 63
  21. local emergencyFuelToRetain = 0
  22. local maximumGravelStackSupported = 25
  23. local returningToStart = false
  24.  
  25. -- Enum: Direction
  26. direction = { FORWARD=0, RIGHT=1, BACK=2, LEFT=3, UP=4, DOWN=5 }
  27.  
  28. -- Enum: State
  29. state = { START=0, EMPTYINVENTORY=1 }
  30.  
  31. -- Location information
  32. local currX
  33. local currY
  34. local currZ
  35. local currOrient
  36. local currState = state.START
  37.  
  38. -- Turtle information
  39. local isWirelessTurtle
  40.  
  41. local startHeight -- Represents the height (y co-ord) that the turtle started at
  42. local quarryWidth -- Represents the length of the mines that the turtle will dig
  43. local quarryLength -- (Optional) the length if it's not the same as width
  44.  
  45. messageLevel = { DEBUG=0, INFO=1, WARNING=2, ERROR=3, FATAL=4 }
  46. local messageOutputLevel = messageLevel.INFO
  47.  
  48. function writeMessage(message, msgLevel)
  49.   if (msgLevel >= messageOutputLevel) then
  50.     print(message)
  51.  
  52.     -- If this turtle has a modem, then write the message to red net
  53.     if (isWirelessTurtle == true) then
  54.       if (turtleId == nil) then
  55.         rednet.broadcast(message)
  56.       else
  57.         -- Broadcast the message (prefixed with the turtle's id)
  58.         rednet.broadcast("[".. turtleId.."] "..message)
  59.       end
  60.     end
  61.   end
  62. end
  63.  
  64. function ensureFuel()
  65.   -- Determine whether a refuel is required
  66.   local fuelLevel = turtle.getFuelLevel()
  67.   if (fuelLevel ~= "unlimited") then
  68.     if (fuelLevel < fuelLevelToRefuelAt) then
  69.       -- Need to refuel
  70.       turtle.select(16)
  71.       currentlySelectedSlot = 16
  72.       local fuelItems = turtle.getItemCount(16)
  73.  
  74.       -- Do we need to impact the emergency fuel to continue? (always  
  75.       -- keep one fuel item in slot 16)
  76.       if (fuelItems == 0) then
  77.         writeMessage("Completely out of fuel!", messageLevel.FATAL)
  78.       elseif (fuelItems == 1) then
  79.         writeMessage("Out of Fuel!", messageLevel.ERROR)
  80.         turtle.refuel()
  81.       elseif (fuelItems <= (emergencyFuelToRetain + 1)) then
  82.         writeMessage("Consuming emergency fuel supply. "..(fuelItems - 2).." emergency fuel items remain", messageLevel.WARNING)
  83.         turtle.refuel(1)
  84.       else
  85.         -- Refuel the lesser of the refuelItemsToUseWhenRefuelling and the number of items more than
  86.         -- the emergency fuel level
  87.         if (fuelItems - (emergencyFuelToRetain + 1) < refuelItemsToUseWhenRefuelling) then
  88.           turtle.refuel(fuelItems - (emergencyFuelToRetain + 1))
  89.         else
  90.           turtle.refuel(refuelItemsToUseWhenRefuelling)
  91.         end
  92.       end
  93.     end
  94.   end
  95. end
  96.  
  97. function ensureInventorySpace()
  98.  
  99.   -- If already returning to start, then don't need to do anything
  100.   if (returningToStart == false) then
  101.  
  102.     -- If the last inventory slot is full, then need to return to the start and empty
  103.     if (turtle.getItemCount(lastEmptySlot) > 0) then
  104.  
  105.       -- Return to the starting point and empty the inventory, then go back to mining
  106.       returnToStartAndUnload(true)
  107.     end
  108.   end
  109. end
  110.  
  111. function returnToStartAndUnload(returnBackToMiningPoint)
  112.   writeMessage("returnToStartAndUnload called", messageLevel.DEBUG)
  113.   returningToStart = true
  114.   local storedX, storedY, storedZ, storedOrient
  115.   local prevMiningState = currMiningState
  116.   storedX = currX
  117.   storedY = currY
  118.   storedZ = currZ
  119.   storedOrient = currOrient
  120.  
  121.   -- Store the current location and orientation so that it can be returned to
  122.   currState = state.EMPTYINVENTORY
  123.   writeMessage("last item count = "..turtle.getItemCount(lastEmptySlot), messageLevel.DEBUG)
  124. end
  125.  
  126. function prepQuarry()
  127.   -- Dig 4 high rectangular tunnel
  128.   writeMessage("Prep Quarry placeholder, quarry dimensions: "..quarryWidth.."x"..quarryLength, messageLevel.INFO)
  129. end
  130.  
  131. -- Process the input arguments - storing them to global variables
  132. local args = { ... }
  133. local paramsOK = true
  134.  
  135. -- Detect whether this is a wireless turtle, and if so, open the modem
  136. local peripheralConnected = peripheral.getType("right")
  137. if (peripheralConnected == "modem") then
  138.   isWirelessTurtle = true
  139. end
  140.  
  141. -- If a wireless turtle, open the modem
  142. if (isWirelessTurtle == true) then
  143.   turtleId = os.getComputerLabel()
  144.   rednet.open("right")
  145. end
  146.  
  147. if (#args == 0) then
  148.   -- TODO Resume support
  149. elseif (#args == 1) then
  150.   quarryWidth = tonumber(args[1])
  151.   quarryLength = quarryWidth
  152.   local x, y, z = gps.locate(5)
  153.   startHeight = y
  154.   if (startHeight == nil) then
  155.     writeMessage("Can't locate GPS", messageLevel.FATAL)
  156.     paramsOK = false
  157.   end
  158. elseif (#args == 2) then
  159.   if (args[2] == "/r") then
  160.     quarryWidth = tonumber(args[1])
  161.     quarryLength = tonumber(args[2])
  162.   else
  163.     quarryWidth = tonumber(args[1])
  164.     quarryLength = tonumber(args[2])
  165.     startHeight = tonumber(args[3])
  166.   end
  167. elseif (#args == 3) then
  168.   if(args[3] == "/r") then
  169.     quarryWidth = tonumber(args[1])
  170.     quarryLength = tonumber(agrs[2])
  171.   else
  172.     quarryWidth = tonumber(args[1])
  173.     quarryLength = tonumber(args[2])
  174.     startHeight = tonumber(args[3])
  175.   end
  176. end
  177.  
  178. if (paramsOK == false) then
  179.   writeMessage("Usage: "..shell.getRunningProgram().." <diameter> [turtleY] [/r]", messageLevel.FATAL)
  180.   paramsOK = false
  181. end
  182.  
  183. if (paramsOK == true) then
  184.   if ((startHeight < 6) or (startHeight > 128)) then
  185.     writeMessage("turtleY must be between 6 and 128", messageLevel.FATAL)
  186.     paramsOK = false
  187.   end
  188.  
  189.   if ((quarryWidth < 2) or (quarryWidth > 64)) then
  190.     writeMessage("diameter must be between 2 and 64", messageLevel.FATAL)
  191.     paramsOK = false
  192.   end
  193.  
  194.   writeMessage("----------------", messageLevel.INFO)
  195.   writeMessage("** QuarryPrep **", messageLevel.INFO)
  196.   writeMessage("----------------", messageLevel.INFO)
  197.  
  198.   -- Set the turtle's starting position
  199.   currX = 0
  200.   currY = startHeight
  201.   currZ = 0
  202.   currOrient = direction.FORWARD
  203.  
  204.   -- Create a Quarry
  205.   turtle.select(1)
  206.   currentlySelectedSlot = 1
  207.   prepQuarry()
  208. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement