Advertisement
Jirek

Computercraft - Turtle - ScanDestroyArea

Dec 31st, 2013
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. --EN:
  2. --This program does the same thing like ScanArea, but it
  3. --also destroys the area.
  4. --
  5. --Download ScanArea from http://pastebin.com/1hFiZ7Ub
  6.  
  7. --CZ:
  8. --Tenhle program dela to same jako ScanArea,
  9. --ale danou oblast znici.
  10. --
  11. --Stahnete si ScanArea z http://pastebin.com/1hFiZ7Ub
  12.  
  13. --
  14. --Input ------------------------------------------
  15. --
  16.  
  17. local tArgs = { ... }
  18. if #tArgs ~= 3 and #tArgs ~= 4 then
  19.   print("Usage: ScanArea <x> <y> <map name>")
  20.   return
  21. end
  22.  
  23. if fs.exists(tArgs[3]) then
  24.   print("Current file already exists")
  25.   return
  26. end
  27.  
  28. --Runned by another program?
  29. local runByProgram = tArgs[4] == "T"
  30.  
  31. --Clear screen
  32. if runByProgram == false then
  33.   term.clear()
  34.   term.setCursorPos(1, 1)
  35.   print("Info: Fuel place only in the last slot")
  36.   print("      Run only on mining turtle")
  37.   print("")
  38. end
  39.  
  40. --
  41. --Main variables ---------------------------------
  42. --
  43.  
  44. local VirtualMap = {}
  45. local X = tonumber(tArgs[1])
  46. local Y = tonumber(tArgs[2])
  47.  
  48. --
  49. --Methods ----------------------------------------
  50. --
  51.  
  52. local function CheckForAll()
  53.   --fuel
  54.   if turtle.getFuelLevel() == 0 then
  55.     turtle.select(16)
  56.     local bFirst = true
  57.     while turtle.refuel(1) == false do
  58.       if bFirst then
  59.         print("Out of fuel, waiting...")
  60.       end
  61.       os.sleep(1)
  62.       bFirst = false
  63.     end
  64.     print("Thanks!")
  65.     turtle.select(1)
  66.   end
  67. end
  68.  
  69. local function ScanLine(index, inverted)
  70.   local LineMap = {}
  71.   local i = 1
  72.   while i <= X do
  73.     if inverted then
  74.       LineMap[X - (i - 1)] = turtle.detectDown()
  75.     else
  76.       LineMap[i] = turtle.detectDown()
  77.     end
  78.     turtle.digDown()
  79.     if i ~= X then
  80.       CheckForAll()
  81.       shell.run("/rom/programs/turtle/go forward")
  82.     end
  83.     i = i + 1
  84.   end
  85.   VirtualMap[index] = LineMap
  86. end
  87.  
  88. --
  89. --Code -------------------------------------------
  90. --
  91.  
  92. local invertedLine = false
  93. local i = 1
  94. while i <= Y do
  95.   ScanLine(i, invertedLine)
  96.   if i ~= Y then
  97.     CheckForAll()
  98.     if invertedLine then
  99.       shell.run("/rom/programs/turtle/go left forward left")
  100.       invertedLine = false
  101.     else
  102.       shell.run("/rom/programs/turtle/go right forward right")
  103.       invertedLine = true
  104.     end
  105.   end
  106.   i = i + 1
  107. end
  108.  
  109. --Return
  110. if invertedLine then
  111.   shell.run("/rom/programs/turtle/go right")
  112.   for n=1, (Y - 1) do
  113.     CheckForAll()
  114.     shell.run("/rom/programs/turtle/go forward")
  115.   end
  116.   shell.run("/rom/programs/turtle/go right")
  117. else
  118.   shell.run("/rom/programs/turtle/go left")
  119.   for n=1, (Y - 1) do
  120.     CheckForAll()
  121.     shell.run("/rom/programs/turtle/go forward")
  122.   end
  123.   shell.run("/rom/programs/turtle/go left")
  124.   for n=1, (X - 1) do
  125.     CheckForAll()
  126.     shell.run("/rom/programs/turtle/go forward")
  127.   end
  128.   shell.run("/rom/programs/turtle/go left 2")
  129. end
  130.  
  131. --
  132. --Save result ------------------------------------
  133. --
  134.  
  135. local file = fs.open(tArgs[3], "w")
  136. for y=1, Y do
  137.   local line = ""
  138.   for x=1, X do
  139.     if VirtualMap[y][x] then
  140.       line = line.."1"
  141.     else
  142.       line = line.."0"
  143.     end
  144.   end
  145.   file.writeLine(line)
  146. end
  147. file.close()
  148.  
  149. if runByProgram == false then
  150.   print("Scanning done, result saved")
  151. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement