tikimyster

excavateb

Sep 10th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.58 KB | None | 0 0
  1. local size = 10
  2. local currentPosition   = {x = 0, y = 0, z =0, d  = 0}
  3. local savePostion       = {x = 0, y = 0, z =0, d  = 0}
  4. local homePosition      = {x = 0, y = 0, z =0, d  = 0}
  5. x, y, z, d = 0, 0, 0, 0
  6.  
  7. -- dig
  8. function excavate(size, depth)
  9.     local isFinished = false
  10.     local useDepth = depth ~= nil and depth > 0
  11.     local depthCount = 0
  12.  
  13.     if useDepth then
  14.         if startNextLevel() then
  15.             depthCount = depthCount + 1
  16.             isFinished = depth >= depthCount
  17.         end
  18.     else
  19.         isFinished = startNextLevel()
  20.     end
  21.  
  22.     while isFinished do
  23.         --up and down or column
  24.         for i = 0, size - 1 do
  25.             --row or left to right
  26.             for j = 0, size - 1 do
  27.                 dig("forward")
  28.                 go("forward")
  29.             end
  30.             if i ~= size - 1 then
  31.                 --even turns right
  32.                 if i % 2 == 0 then
  33.                     turn("right")
  34.                     dig("forward")
  35.                     go("forward")
  36.                     turn("right")
  37.                 --odd turns left
  38.                 else
  39.                     turn("left")
  40.                     dig("forward")
  41.                     go("forward")
  42.                     turn("left")
  43.                 end
  44.             end
  45.         end
  46.         if size % 2 == 1 then --if the size is odd  180 if not 900
  47.             turn("left")
  48.             turn("left")
  49.         else
  50.             turn("left")
  51.         end
  52.         if useDepth then
  53.             if startNextLevel() then
  54.                 depthCount = depthCount + 1
  55.                 isFinished = depth >= depthCount
  56.             end
  57.         else
  58.             isFinished = startNextLevel()
  59.         end
  60.     end
  61.  
  62.     local function startNextLevel()
  63.         local atNext = go("down")
  64.         if not atNext then
  65.             dig("down")
  66.             atNext = go("down")
  67.         end
  68.         return atNext
  69.     end
  70. end
  71.  
  72. function dig(str)
  73.     local success = false
  74.     if string.lower(str) == "forward" or string.lower(str) == "f" then
  75.         success = turtle.dig()
  76.     elseif string.lower(str) == "up" or string.lower(str) == "u" then
  77.         success = turtle.digUp()
  78.     elseif string.lower(str) == "down" or string.lower(str) == "down" then
  79.         success = turtle.digDown()
  80.     end
  81.     return success
  82. end
  83.  
  84. --check fuel
  85. function checkfuel()
  86. end
  87.  
  88. function go(str)
  89.     local success = false
  90.     if string.lower(str) == "down" or string.lower(str) == "d" then
  91.         if turtle.down() then
  92.             currentPosition["y"] = currentPosition["y"] - 1
  93.             success = true
  94.         else
  95.             term.write("I'm stuck!")
  96.         end
  97.     end
  98.     if string.lower(str) == "up" or string.lower(str) == "u" then
  99.         currentPosition["y"] = currentPosition["y"] + 1
  100.         success = true
  101.     end
  102.     if string.lower(str) == "forward" or sting.lower(str) == "f" then
  103.         if d == 0 then  currentPosition["z"] = currentPosition["z"] + 1 end
  104.         if d == 1 then  currentPosition["x"] = currentPosition["x"] - 1 end
  105.         if d == 2 then  currentPosition["z"] = currentPosition["z"] - 1 end
  106.         if d == 3 then  currentPosition["x"] = currentPosition["x"] + 1 end
  107.         success = true
  108.     end
  109.     if string.lower(str) == "back" or string.lower(str) == "b" then
  110.         if d == 0 then currentPosition["z"] = currentPosition["z"] - 1 end
  111.         if d == 1 then currentPosition["x"] = currentPosition["x"] + 1 end
  112.         if d == 2 then currentPosition["z"] = currentPosition["z"] + 1 end
  113.         if d == 3 then currentPosition["x"] = currentPosition["x"] - 1 end
  114.         success = true
  115.     end
  116.     return success
  117. end
  118.  
  119. function turn(str)
  120.     if string.lower(str) == "right" or string.lower(str) == "r" then
  121.         turtle.turnright()
  122.         currentPosition["d"] = (currentPosition["d"] + 1) % 4
  123.     end
  124.     if string.lower(str) == "left" or string.lower(str) == "l" then
  125.         turtle.turnleft()
  126.         currentPosition["d"] = (currentPosition["d"] - 1) % 4
  127.     end
  128. end
  129.  
  130. -- track positon
  131. function trackPosition()
  132. end
  133.  
  134. --track Inventory
  135. function trackInventory()
  136. end
  137.  
  138. function goToPosition(position, order, canDig)
  139.     local isFinished = false
  140.     if order == nil then order = {"x", "z", "y"} end
  141.     if canDig == nil then canDig = false end
  142. end
  143.  
  144. excavate(size,depth)
Add Comment
Please, Sign In to add comment