Advertisement
Jirek

Computercraft - Turtle - ScanArea

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