PonyKuu

modulemine v0.1

Mar 18th, 2013
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.42 KB | None | 0 0
  1. -- Mastermine is a Module part of turtle swarm quarry made by PonyKuu
  2. -- It uses my module API
  3.  
  4. -- version 0.1
  5.  
  6. os.loadAPI ("module")
  7.  
  8. local function turn (direction)
  9.     -- this is used to turn the turtle using numbers - it's useful in our layer-wiping code
  10.     -- 1 - right
  11.     -- -1 - left
  12.     -- other - around
  13.     if direction == 1 then
  14.         module.turn ("right")
  15.     elseif direction == -1 then
  16.         module.turn ("left")
  17.     else
  18.         module.turn ("around")
  19.     end
  20. end
  21.  
  22. -- A function to dig a block under the module and chech if it should dump all the mined goods into chest
  23. local function digUnder ()
  24.     turtle.digDown ()
  25.     module.checkSpace()
  26. end
  27. local function makeLine (length) --a simple line function
  28.     for i = 1, length-1 do
  29.         digUnder ()
  30.         if not module.move ("forward") then
  31.             return false    --return false if it cannot move forward. That typically means the turtle hit bedrock.
  32.         end
  33.     end
  34.     return true
  35. end
  36.  
  37. local function wipeLayer (diameter)  --cleans a square layer
  38.     local turnDirection = 1
  39.     --the main algorithm
  40.     for i = 1, diameter-1 do
  41.         if not makeLine (diameter) then
  42.             return false
  43.         end
  44.         turn (turnDirection)
  45.         digUnder ()
  46.         if not module.move ("forward") then
  47.             return false
  48.         end
  49.         turn (turnDirection)
  50.         turnDirection = -turnDirection
  51.     end
  52.     --finish the layer
  53.     if not makeLine (diameter) then
  54.         return false
  55.     end
  56.     if not module.move ("down") then
  57.         return false --try to move down
  58.     end
  59.     if diameter % 2 == 0 then
  60.         turn (1)    --turn right for the next layer if the diameter is even
  61.     else
  62.         turn (0)    --turn around if it is odd. A bit of magic here. That should work.
  63.     end
  64.     return true  --returns true if layer is done
  65. end
  66.  
  67. local function makeHole (diameter) -- a main mining function
  68.     local depth = 0
  69.     while wipeLayer (diameter) do
  70.         depth = depth+1
  71.     end
  72.     module.dumpStuff ()
  73.     for i=1, depth do
  74.         module.move ("up")
  75.     end
  76.     -- That's it! It's just wipes all the layers until it hits bedrock and returns to its initial depth
  77. end
  78.  
  79. -- set up our Mine task
  80. module.addTasks {
  81.     Mine = function (response)
  82.         makeHole (response.holeSize)
  83.     end
  84. }
  85. -- set up the module type to Miner
  86. module.setType ("Miner")
  87. module.init ()
  88. module.operate ()
Advertisement
Add Comment
Please, Sign In to add comment