krom

Minecraft - Wallbuilder - Simple

Mar 29th, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.16 KB | None | 0 0
  1. -- ------------------------------------------------------
  2. --
  3. -- Wall builder
  4. -- Author: KROM
  5. -- goes forward and down. then steps back and places block in front
  6. -- end of row, move up, turn around and continue.
  7. --
  8. -- simple and untested, use at own risk ;p
  9. --
  10. -- ------------------------------------------------------
  11. local nStepsBackHome = 0
  12. local bDone = false
  13. local bAtHome = true
  14. local nEnergy = 0
  15. local nActiveSlot = 0
  16. local nItemsLeft = 0
  17.  
  18. local vOffset = vector.new(0,0,0)
  19. local vDir    = vector.new(0,0,1)
  20. -- ------------------------------------------------------
  21. local function goForward()
  22.     if turtle.forward() == true then
  23.         if vDir.z == 1 then vOffset.z = vOffset.z + 1 else vOffset.z = vOffset.z - 1 end
  24.         nEnergy = nEnergy - 1;
  25.         return true
  26.     end
  27.     return false
  28. end
  29. -- ------------------------------------------------------
  30. local function goBack()
  31.     if turtle.back() == true then
  32.         if vDir.z == 1 then vOffset.z = vOffset.z - 1 else vOffset.z = vOffset.z + 1 end
  33.         nEnergy = nEnergy - 1;
  34.         return true
  35.     end
  36.     return false
  37. end
  38. -- ------------------------------------------------------
  39. local function goUp()
  40.     if turtle.up() == true then
  41.         vOffset.y = vOffset.y + 1
  42.         nEnergy = nEnergy - 1;
  43.         return true
  44.     end
  45.     return false
  46. end
  47. -- ------------------------------------------------------
  48. local function goDown()
  49.     if turtle.down() == true then
  50.         vOffset.y = vOffset.y - 1
  51.         nEnergy = nEnergy - 1;
  52.         return true
  53.     end
  54.     return false
  55. end
  56. -- ------------------------------------------------------
  57.  
  58. local function goHome()
  59.     print("Going home")
  60.     while vOffset.y > 0 do
  61.         if goUp() == false then
  62.             print("ERROR: Return home: no way up")
  63.         end
  64.     end
  65.     if not vDir.z == 1 then
  66.         turtle.turnLeft()
  67.         turtle.turnLeft()
  68.         nEnergy = nEnergy - 2;
  69.         vDir.z = 1
  70.     end
  71.     while vOffset.z > 0 do
  72.         if goForward() == false then
  73.             print("ERROR: Return home: no way forward")
  74.         end
  75.     end
  76.     if vOffset.y == 0 and vOffset.z == 0 then
  77.         bHome = true
  78.         print("Arrived at home")
  79.     end
  80. end
  81. -- ------------------------------------------------------
  82. local function refuel(amount)
  83.     local fuelLevel = turtle.getFuelLevel()
  84.     if fuelLevel == "unlimited" then
  85.         nEnergy = 9999
  86.         return true
  87.     end
  88.    
  89.     --refuel
  90.     local needed             = amount or 600
  91.     if turtle.getFuelLevel() < needed then
  92.         for n=1,16 do
  93.             if turtle.getItemCount(n) > 0 then
  94.                 turtle.select(n)
  95.                 if turtle.refuel(1) then
  96.                     needed = needed - 1
  97.                     while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  98.                         turtle.refuel(1)
  99.                     end
  100.                     if turtle.getFuelLevel() >= needed then
  101.                         turtle.select(1)
  102.                         nEnergy = turtle.getFuelLevel()
  103.                         return true
  104.                     end
  105.                 end
  106.             end
  107.         end
  108.         nEnergy = turtle.getFuelLevel()
  109.         turtle.select(1)
  110.         return false
  111.     end
  112.     nEnergy = turtle.getFuelLevel()
  113.     turtle.select(1)
  114.     return true
  115. end
  116. -- ------------------------------------------------------
  117. local function getBlock()
  118.     for n=2,16 do
  119.         if turtle.getItemCount(n) > 0 then
  120.             turtle.select(n)
  121.             nItemsLeft = turtle.getItemCount(n)
  122.             nActiveSlot = n
  123.             return true
  124.         end
  125.     end
  126.     return false
  127. end
  128. -- ------------------------------------------------------
  129. local function placeBlock()  
  130.     if nItemsLeft > 0 then
  131.         turtle.place()
  132.         nItemsLeft = nItemsLeft - 1
  133.         return true
  134.     else
  135.         if getBlock() == true then
  136.             return placeBlock()
  137.         else
  138.             return false
  139.         end
  140.     end
  141. end
  142. -- ------------------------------------------------------
  143. local function placeBlockDown()  
  144.     if nItemsLeft > 0 then
  145.         turtle.placeDown()
  146.         nItemsLeft = nItemsLeft - 1
  147.         return true
  148.     else
  149.         if getBlock() == true then
  150.             return placeBlock()
  151.         else
  152.             return false
  153.         end
  154.     end
  155. end
  156.  
  157. -- ------------------------------------------------------
  158.  
  159.  
  160. while not bDone do
  161.    
  162.    
  163.     if bAtHome == true then
  164.         -- if at home
  165.  
  166.         -- check energy
  167.         if not refuel() or nEnergy < 60 then
  168.             print "Fuel less than 60 and no fuel left. Abort."
  169.             bDone = true
  170.             break
  171.         end
  172.  
  173.         -- check inventory
  174.         if not getBlock() then
  175.             print "No blocks to place. Abort."
  176.             bDone = true
  177.             break
  178.         end
  179.  
  180.         while goForward() == true do
  181.         end
  182.  
  183.         while goDown() == true do
  184.         end
  185.  
  186.         bAtHome = false
  187.     else
  188.  
  189.         -- check energy
  190.         if not refuel() or nEnergy < 60 then
  191.             print "Fuel less than 60 and no fuel left. Abort."
  192.             goHome()
  193.             bDone = true
  194.             break
  195.         end
  196.  
  197.         -- check inventory
  198.         if not getBlock() then
  199.             print "No blocks to place. Abort."
  200.             bDone = true
  201.             goHome()
  202.             break
  203.         end
  204.  
  205.         -- if on duty
  206.         if goBack() == true then
  207.             if not placeBlock() then
  208.                 print("could not place block")
  209.                 goHome()
  210.                 bDone = true
  211.                 break
  212.             end
  213.         else
  214.             if goUp() == true then
  215.                 if not placeBlockDown() then
  216.                     print("could not place block below")
  217.                     goHome()
  218.                     bDone = true
  219.                     break
  220.                 else
  221.                     -- check if top layer
  222.                     if vOffset.y == 0 then
  223.                         print("Reached home layer. Returning")
  224.                         goHome()
  225.                         bDone = true
  226.                         break
  227.                     end
  228.  
  229.                     -- check if we can move backwards now
  230.                     if goBack() == true then
  231.                         if not placeBlock() then
  232.                             print("could not place block")
  233.                             goHome()
  234.                             bDone = true
  235.                             break
  236.                         end
  237.                     else
  238.                         -- no we can not. turn around
  239.                         turtle.turnLeft()
  240.                         turtle.turnLeft()
  241.                         nEnergy = nEnergy - 2;
  242.                         vDir.z = 1 - vDir.z
  243.                     end
  244.  
  245.  
  246.                 end
  247.             else
  248.                 print("FATAL ERROR: could not go up")
  249.                 bDone = true
  250.                 break
  251.             end
  252.         end
  253.     end
  254. end
  255. print("Wall: All done!")
  256. -- ------------------------------------------------------
  257. -- ------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment