Advertisement
Jirek

Computercraft - Turtle - BuildArea

Dec 30th, 2013
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.61 KB | None | 0 0
  1. --EN:
  2. --This program uses virtual maps from ScanArea
  3. --program. It will build exactly what's saved
  4. --in the map.
  5. --
  6. --Download ScanArea from http://pastebin.com/1hFiZ7Ub
  7.  
  8. --CZ:
  9. --Tenhle program pouziva virtualni mapy vytvorene
  10. --programem ScanArea a stavi podle nich.
  11. --
  12. --Stahnete si ScanArea z http://pastebin.com/1hFiZ7Ub
  13.  
  14. --
  15. --Input ------------------------------------------
  16. --
  17.  
  18. local tArgs = { ... }
  19. if #tArgs ~= 1 and #tArgs ~= 2 then
  20.   print("Usage: BuildArea <map name>")
  21.   return
  22. end
  23.  
  24. if fs.exists(tArgs[1]) == false then
  25.   print("Current file does not exist")
  26.   return
  27. end
  28.  
  29. --Runned by another program?
  30. local runByProgram = tArgs[2] == "T"
  31.  
  32. --Clear screen
  33. if runByProgram == false then
  34.   term.clear()
  35.   term.setCursorPos(1, 1)
  36.   print("Info: Fuel place only in the last slot")
  37.   print("      Material place anywhere except the last slot")
  38.   print("")
  39. end
  40.  
  41. --
  42. --Main variables ---------------------------------
  43. --
  44.  
  45. local VirtualMap = {}
  46. local X
  47. local Y
  48. local WeirdFile = false
  49. local nSelected = 1
  50.  
  51. --
  52. --Methods ----------------------------------------
  53. --
  54.  
  55. local function CheckForAll()
  56.   --material
  57.   local bCheck = true
  58.   while bCheck do
  59.     bCheck = false
  60.     if turtle.getItemCount(nSelected) == 0 then
  61.       nSelected = nSelected + 1
  62.       turtle.select(nSelected)
  63.       bCheck = true
  64.     end
  65.     if nSelected == 16 then
  66.       bCheck = false
  67.       nSelected = 1
  68.       turtle.select(nSelected)
  69.       print("Out of material, fill me Im waiting...")
  70.       while turtle.getItemCount(1) == 0 do
  71.         os.sleep(1)
  72.       end
  73.       print("Thanks!")
  74.     end
  75.   end
  76.    
  77.   --fuel
  78.   if turtle.getFuelLevel() == 0 then
  79.     turtle.select(16)
  80.     local bFirst = true
  81.     while turtle.refuel(1) == false do
  82.       if bFirst then
  83.         print("Out of fuel, waiting...")
  84.       end
  85.       os.sleep(1)
  86.       bFirst = false
  87.     end
  88.     print("Thanks!")
  89.     turtle.select(nSelected)
  90.   end
  91. end
  92.  
  93. local function BuildLine(index, inverted)
  94.   local x = 1
  95.   while x <= X do
  96.     if inverted then
  97.       if VirtualMap[index][X - (x - 1)] then
  98.         CheckForAll()
  99.         turtle.placeDown()
  100.       end
  101.     else
  102.       if VirtualMap[index][x] then
  103.         CheckForAll()
  104.         turtle.placeDown()
  105.       end
  106.     end
  107.     if x ~= X then
  108.       CheckForAll()
  109.       shell.run("/rom/programs/turtle/go forward")
  110.     end
  111.     x = x + 1
  112.   end
  113. end
  114.  
  115. --
  116. --Code -------------------------------------------
  117. --
  118.  
  119. --Read file
  120. file = fs.open(tArgs[1], "r")
  121. local l = 0
  122. local ch = 1
  123. local line = file.readLine()
  124. if line == nil then
  125.   WeirdFile = true
  126. end
  127. while line ~= nil do
  128.   l = l + 1
  129.   VirtualMap[l] = {}
  130.   ch = 1
  131.   while ch <= #line do
  132.     if string.sub(line, ch, ch) == "1" then
  133.       VirtualMap[l][ch] = true
  134.     elseif string.sub(line, ch, ch) == "0" then
  135.       VirtualMap[l][ch] = false
  136.     else
  137.       WeirdFile = true
  138.     end
  139.     ch = ch + 1
  140.   end
  141.   line = file.readLine()
  142. end
  143. file.close()
  144.  
  145. for n=2, l do
  146.   if #VirtualMap[n] ~= #VirtualMap[1] then
  147.     WeirdFile = true
  148.   end
  149. end
  150.  
  151. if WeirdFile then
  152.   print("This file is weird! Is it really a map?")
  153.   return
  154. end
  155.  
  156. --Get area size
  157. X = #VirtualMap[1]
  158. Y = #VirtualMap
  159.  
  160. --Ask for continue
  161. if runByProgram == false then
  162.   local bAgain = true
  163.   while bAgain do
  164.     print("The area is "..tostring(X).."x"..tostring(Y))
  165.     print("Do you want to continue? (y/n) ")
  166.     local input = read()
  167.     if input == "y" then
  168.       bAgain = false
  169.     elseif input == "n" then
  170.       print("Ok, ending...")
  171.       return
  172.     else
  173.       print("Wrong answer!")
  174.       print("")
  175.     end
  176.   end
  177. end
  178.  
  179. --Build
  180. turtle.select(1)
  181. local invertedLine = false
  182. local y = 1
  183. while y <= Y do
  184.   BuildLine(y, invertedLine)
  185.   if y ~= Y then
  186.     if invertedLine then
  187.       CheckForAll()
  188.       shell.run("/rom/programs/turtle/go left forward left")
  189.       invertedLine = false
  190.     else
  191.       CheckForAll()
  192.       shell.run("/rom/programs/turtle/go right forward right")
  193.       invertedLine = true
  194.     end
  195.   end
  196.   y = y + 1
  197. end
  198.  
  199. --Return
  200. if invertedLine then
  201.   shell.run("/rom/programs/turtle/go right")
  202.   for n=1, (Y - 1) do
  203.     CheckForAll()
  204.     shell.run("/rom/programs/turtle/go forward")
  205.   end
  206.   shell.run("/rom/programs/turtle/go right")
  207. else
  208.   shell.run("/rom/programs/turtle/go left")
  209.   for n=1, (Y - 1) do
  210.     CheckForAll()
  211.     shell.run("/rom/programs/turtle/go forward")
  212.   end
  213.   shell.run("/rom/programs/turtle/go left")
  214.   for n=1, (X - 1) do
  215.     CheckForAll()
  216.     shell.run("/rom/programs/turtle/go forward")
  217.   end
  218.   shell.run("/rom/programs/turtle/go left 2")
  219. end
  220.  
  221. if runByProgram == false then
  222.   print("All done!")
  223. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement