Guest User

WallBuilder

a guest
Jun 27th, 2015
1,277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.06 KB | None | 0 0
  1. local tArgs={...}
  2. local currentlevel = 1
  3. local maxlevel = 1
  4. local program = ""
  5. local PC=0
  6. local materialCount=0
  7.  
  8. local function clear()
  9.     term.clear()
  10.     term.setCursorPos(1, 1)
  11. end
  12.  
  13.  
  14. function Update()
  15.     clear()
  16.     print("Building level: "..currentlevel.."/"..maxlevel)
  17.     print("Fuel remaining: "..turtle.getFuelLevel())
  18.     print("")
  19.     local data = turtle.getItemDetail()
  20.     if data then
  21.         print("Builing material: {"..materialCount.."}")
  22.         print(data.name)
  23.         print("")
  24.     end
  25.    
  26.     print("Program: {"..PC.."/"..string.len(program).."}")
  27.     print("["..program.."]")
  28.     if PC>0 then
  29.         local t= " "
  30.         for i = 1 ,PC-1 do
  31.             t=t.." "
  32.         end
  33.         print(t.."^")
  34.     end
  35.    
  36. end
  37.  
  38. function selectBlock()
  39.     if turtle.getItemCount(16) ==0 then
  40.         print("Waiting for material")
  41.         while turtle.getItemCount(16) ==0 do
  42.             sleep(.5)
  43.         end
  44.         turtle.select(2)
  45.     end
  46.    
  47.     if turtle.getItemCount() >0 and turtle.compareTo(16) then
  48.         return true
  49.     end
  50.     materialCount=0
  51.     local selected = -1  
  52.     for i = 2, 16 do
  53.         if turtle.getItemCount(i) >0 then
  54.             turtle.select(i)
  55.             if turtle.compareTo(16) then
  56.                 materialCount= materialCount+turtle.getItemCount(i)
  57.                 if selected == -1 then
  58.                     selected = i  
  59.                 end
  60.             end
  61.         end
  62.     end
  63.     if selected==-1 then
  64.         return false
  65.         else
  66.         turtle.select(selected)
  67.     end
  68. end
  69.  
  70.  
  71. function placeBlock()
  72.     if not refuel(5) then
  73.         print("Waiting for fuel")
  74.         while not refuel(5) do
  75.             sleep(.5)
  76.         end
  77.     end
  78.     selectBlock()
  79.     turtle.place()
  80.     materialCount=materialCount-1
  81.     Update()
  82. end
  83.  
  84. function placeBlockDown()
  85.     if not refuel(5) then
  86.         print("Waiting for fuel")
  87.         while not refuel(5) do
  88.             sleep(.5)
  89.         end
  90.     end
  91.     selectBlock()
  92.     turtle.placeDown()
  93.     materialCount=materialCount-1
  94.     Update()
  95. end
  96.  
  97.  
  98. function turn180()
  99.     turtle.turnLeft()
  100.     turtle.turnLeft()
  101. end
  102.  
  103. function findnext(  )
  104.     if turtle.detect() then
  105.         program = program.."F"
  106.         return false
  107.     end
  108.    
  109.     turtle.forward()
  110.     if turtle.detectDown() then
  111.         program = program.."F"
  112.         return true
  113.     end
  114.     turtle.back()
  115.     turtle.turnLeft()
  116.     if turtle.detect() then
  117.         program = program.."L"
  118.         return false
  119.     end
  120.    
  121.     turtle.forward()
  122.    
  123.     if turtle.detectDown() then
  124.         program = program.."L"
  125.         return true
  126.     end
  127.    
  128.    
  129.     turtle.back()
  130.     turtle.turnLeft()
  131.     turtle.turnLeft()
  132.     if turtle.detect() then
  133.         program = program.."R"
  134.         return false
  135.     end
  136.    
  137.     turtle.forward()
  138.    
  139.    
  140.    
  141.     if turtle.detectDown() then
  142.         program = program.."R"
  143.         return true
  144.     end
  145.    
  146.     turtle.back()
  147.     turtle.turnLeft()
  148.     return false
  149.    
  150. end
  151.  
  152. function refuel( ammount )
  153.     local fuelLevel = turtle.getFuelLevel()
  154.     if fuelLevel == "unlimited" then
  155.         return true
  156.     end
  157.    
  158.     local needed = ammount or (xPos + zPos + depth + 2)
  159.     if turtle.getFuelLevel() < needed then
  160.         local fueled = false
  161.         for n=1,16 do
  162.             if turtle.getItemCount(n) > 0 then
  163.                 turtle.select(n)
  164.                 if turtle.refuel(1) then
  165.                     while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  166.                         turtle.refuel(1)
  167.                     end
  168.                     if turtle.getFuelLevel() >= needed then
  169.                         turtle.select(1)
  170.                         return true
  171.                     end
  172.                 end
  173.             end
  174.         end
  175.         turtle.select(1)
  176.         return false
  177.     end
  178.    
  179.     return true
  180. end
  181.  
  182. function recordOneLevel()
  183.     while true do
  184.         if turtle.detect() then
  185.             return true
  186.         end
  187.         if findnext() then
  188.             turn180()
  189.             placeBlock()
  190.             turn180()
  191.             else
  192.             return true
  193.         end
  194.     end
  195. end
  196.  
  197.  
  198. function playOneLevel()
  199.     for i=1, string.len(program) do
  200.         local x = string.sub(program, i, i)
  201.         PC=i
  202.         if x=="F" then
  203.             turtle.back()
  204.             else
  205.             if x=="L" then
  206.                
  207.                 turtle.turnLeft()
  208.                 turtle.back()
  209.                 else
  210.                 if x=="R" then
  211.                     turtle.turnRight() 
  212.                     turtle.back()
  213.                 end
  214.             end
  215.         end
  216.         placeBlock()
  217.     end
  218. end
  219.  
  220.  
  221. if tArgs[1] == nil then
  222.   clear()
  223.   print("How how many levels?")
  224.   tArgs[1] = read()
  225. end
  226.  
  227. maxlevel = tArgs[1]
  228.  
  229. currentlevel = 1
  230. refuel(5)
  231. recordOneLevel()
  232. turtle.up()
  233. placeBlockDown()
  234. turtle.forward()
  235.  
  236.  
  237. turn180()
  238. for i = 2, tArgs[1] do
  239.     currentlevel = i
  240.     refuel(5)
  241.     playOneLevel()
  242.     turtle.up()
  243.     placeBlockDown()
  244.     turtle.back()
  245. end
  246. turn180()
Advertisement
Add Comment
Please, Sign In to add comment