Advertisement
kssr3951

tinyWormminer

Aug 31st, 2014
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.65 KB | None | 0 0
  1. -- ---------------------------------------------------------
  2. -- config
  3. -- ---------------------------------------------------------
  4. local HEIGHT = -4 -- even number only
  5. local DEPTH  =  4 -- even number only
  6.  
  7. -- ---------------------------------------------------------
  8. -- utilities
  9. -- ---------------------------------------------------------
  10. local function fileReadAll(filePath)
  11.   local hFile = fs.open(filePath, "r")
  12.   local txt = hFile.readAll()
  13.   hFile.close()
  14.   return txt
  15. end
  16.  
  17. local function fileOverwrite(fileName, text)
  18.   local hFile = fs.open(fileName, "w")
  19.   hFile.writeLine(text)
  20.   hFile.close()
  21. end
  22.  
  23. -- ---------------------------------------------------------
  24. -- application
  25. -- ---------------------------------------------------------
  26. local ARMSG_INIT    = 1
  27. local ARMSG_SIGNAL  = 2
  28. local ARMSG_GO_HOME = 3
  29. local ARMSG_HALF    = 4
  30.  
  31. local status = { depth = 0, height = 0 }
  32.  
  33. -- -----------------------------
  34. -- app utils
  35. -- -----------------------------
  36. local STATUS_FILENAME = "tinywm.status"
  37. local function loadStatus()
  38.   local staTmp = fileReadAll(STATUS_FILENAME)
  39.   status = textutils.unserialize(staTmp)
  40. end
  41. local function saveStatus()
  42.   fileOverwrite(STATUS_FILENAME, textutils.serialize(status))
  43. end
  44.  
  45. -- -----------------------------
  46. -- cutTail
  47. -- -----------------------------
  48. local function createCutTailFunc(holizontal, suckFunc, digFunc)
  49.   return function()
  50.       if holizontal then
  51.         turtle.turnLeft()
  52.         turtle.turnLeft()
  53.       end
  54.       for i = 1, 16 do
  55.         suckFunc()
  56.       end
  57.       for i = 1, 16 do
  58.         if 0 == turtle.getItemCount(i) then
  59.           notFound = false
  60.           turtle.select(1)
  61.           turtle.transferTo(i)
  62.           break
  63.         end
  64.       end
  65.       if notFound then
  66.         turtle.select(1)
  67.         turtle.drop()
  68.       end
  69.       turtle.select(1)
  70.       digFunc()
  71.       if holizontal then
  72.         turtle.turnLeft()
  73.         turtle.turnLeft()
  74.       end
  75.     end
  76. end
  77. local cutTail       = createCutTailFunc(true , turtle.suck    , turtle.dig    )
  78. local cutTailTop    = createCutTailFunc(false, turtle.suckUp  , turtle.digUp  )
  79. local cutTailBottom = createCutTailFunc(false, turtle.suckDown, turtle.digDown)
  80.  
  81. -- -----------------------------
  82. -- go
  83. -- -----------------------------
  84. local function createGoFunc(cutTailFunc, digFunc, dir, placeFunc, arMsg, debugMsg)
  85.   return function(half)
  86.       while digFunc() do end
  87.       if half then
  88.         rs.setAnalogOutput(dir, arMsg + ARMSG_HALF)
  89.       else
  90.         rs.setAnalogOutput(dir, arMsg)
  91.       end
  92.       turtle.select(1)
  93.       while not placeFunc() do end
  94.       peripheral.call(dir, "turnOn")
  95.     end
  96. end
  97. local goForward = createGoFunc(cutTail      , turtle.dig    ,"front" , turtle.place    , ARMSG_SIGNAL , "goForward()")
  98. local goDown    = createGoFunc(cutTailTop   , turtle.digDown,"bottom", turtle.placeDown, ARMSG_SIGNAL , "goDown()")
  99. local goUp      = createGoFunc(cutTailBottom, turtle.digUp  ,"top"   , turtle.placeUp  , ARMSG_SIGNAL , "goUp()")
  100. local goHome    = createGoFunc(cutTail      , turtle.dig    ,"front" , turtle.place    , ARMSG_GO_HOME, "goHome()")
  101.  
  102. -- -----------------------------
  103. -- main
  104. -- -----------------------------
  105. local args = {...}
  106.  
  107. if #args == 0 then
  108.   local backInput   = rs.getAnalogInput("back")
  109.   local topInput    = rs.getAnalogInput("top")
  110.   local bottomInput = rs.getAnalogInput("bottom")
  111.   local frontInput  = rs.getAnalogInput("front")
  112.  
  113.   if 0 < frontInput then
  114.     turtle.turnLeft()
  115.     turtle.turnLeft()
  116.     backInput = frontInput
  117.   end
  118.  
  119.   if backInput == ARMSG_INIT then
  120.     saveStatus()
  121.   end
  122.   if backInput == ARMSG_INIT or
  123.      backInput == ARMSG_SIGNAL then
  124.     loadStatus()
  125.     if backInput == ARMSG_INIT then
  126.       status.depth = 1
  127.     elseif backInput == ARMSG_SIGNAL then
  128.       status.depth = status.depth + 2
  129.     end
  130.     saveStatus()
  131.     cutTail()
  132.     if status.depth == DEPTH then
  133.       goDown(true)
  134.     else
  135.       goForward(false)
  136.     end
  137.  
  138.   elseif topInput == ARMSG_SIGNAL or
  139.          topInput == ARMSG_SIGNAL + ARMSG_HALF then
  140.  
  141.     loadStatus()
  142.     if topInput == ARMSG_SIGNAL + ARMSG_HALF then
  143.       status.height = status.height - 1
  144.     else
  145.       status.height = status.height - 2
  146.     end
  147.     saveStatus()
  148.     cutTailTop()
  149.     for i = 1, 4 do
  150.       turtle.dig()
  151.       turtle.turnLeft()
  152.     end
  153.     if status.height == HEIGHT then
  154.       goUp(true)
  155.     else
  156.       goDown(false)
  157.     end
  158.  
  159.   elseif bottomInput == ARMSG_SIGNAL or
  160.          bottomInput == ARMSG_SIGNAL + ARMSG_HALF then
  161.     loadStatus()
  162.  
  163.     if topInput == ARMSG_SIGNAL + ARMSG_HALF then
  164.       status.height = status.height + 1
  165.     else
  166.       status.height = status.height + 2
  167.     end
  168.     saveStatus()
  169.     cutTailBottom()
  170.     if status.height == 0 then
  171.       turtle.turnLeft()
  172.       turtle.turnLeft()
  173.       goHome(true)
  174.     else
  175.       goUp(false)
  176.     end
  177.  
  178.   elseif backInput == ARMSG_GO_HOME or
  179.          backInput == ARMSG_GO_HOME + ARMSG_HALF then
  180.     loadStatus()
  181.  
  182.     if topInput == ARMSG_GO_HOME + ARMSG_HALF then
  183.       status.depth = status.depth - 1
  184.     else
  185.       status.depth = status.depth - 2
  186.     end
  187.     saveStatus()
  188.     cutTail()
  189.     if 0 == status.depth then
  190.       turtle.turnLeft()
  191.       turtle.turnLeft()
  192.       print("finished.")
  193.     else
  194.       goHome(false)
  195.     end
  196.   else
  197.     error("no signal.")
  198.   end
  199.  
  200. elseif #args == 1 and "-start" == args[1] then
  201.   turtle.turnLeft()
  202.   turtle.turnLeft()
  203.   turtle.select(1)
  204.   turtle.dig()
  205.   turtle.turnLeft()
  206.   turtle.turnLeft()
  207.  
  208.   saveStatus()
  209.   turtle.select(1)
  210.   rs.setAnalogOutput("front", ARMSG_INIT)
  211.   turtle.place()
  212.   peripheral.call("front", "turnOn")
  213.  
  214. else
  215.   error("bad args")
  216. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement