mariebu

WallBuilder

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