Skip to content

Instantly share code, notes, and snippets.

@Quader
Created August 25, 2014 20:55
Show Gist options
  • Save Quader/49d0b15c52d449e6e45e to your computer and use it in GitHub Desktop.
Save Quader/49d0b15c52d449e6e45e to your computer and use it in GitHub Desktop.
turtle scan
--EN:
--This program will create a virtual map of
--an specified area. This map can be used for
--copying this area somewhere else.
--
--Download BuildArea from http://pastebin.com/45dc4FG2
--CZ:
--Tenhle program naskenuje danou oblast a vytvori
--virtualni mapu. Tu muze pouzit program BuildArea
--aby podle ji znovu postavil nekde jinde.
--
--Stahnete si BuildArea z http://pastebin.com/45dc4FG2
print("Info: Fuel place only in the last slot")
--
--Input ------------------------------------------
--
local tArgs = { ... }
if #tArgs ~= 3 then
print("Usage: ScanArea <x> <y> <map name>")
return
end
if fs.exists(tArgs[3]) then
print("Current file already exists")
return
end
--
--Main variables ---------------------------------
--
local VirtualMap = {}
local X = tonumber(tArgs[1])
local Y = tonumber(tArgs[2])
--
--Methods ----------------------------------------
--
local function CheckForAll()
--fuel
if turtle.getFuelLevel() == 0 then
turtle.select(16)
local bFirst = true
while turtle.refuel(1) == false do
if bFirst then
print("Out of fuel, waiting...")
end
os.sleep(1)
bFirst = false
end
print("Thanks!")
turtle.select(1)
end
end
local function ScanLine(index, inverted)
local LineMap = {}
local i = 1
while i <= X do
if inverted then
LineMap[X - (i - 1)] = turtle.detectDown()
else
LineMap[i] = turtle.detectDown()
end
if i ~= X then
CheckForAll()
shell.run("/rom/programs/turtle/go forward")
end
i = i + 1
end
VirtualMap[index] = LineMap
end
--
--Code -------------------------------------------
--
local invertedLine = false
local i = 1
while i <= Y do
ScanLine(i, invertedLine)
if i ~= Y then
CheckForAll()
if invertedLine then
shell.run("/rom/programs/turtle/go left forward left")
invertedLine = false
else
shell.run("/rom/programs/turtle/go right forward right")
invertedLine = true
end
end
i = i + 1
end
--Return
if invertedLine then
shell.run("/rom/programs/turtle/go right")
for n=1, (Y - 1) do
CheckForAll()
shell.run("/rom/programs/turtle/go forward")
end
shell.run("/rom/programs/turtle/go right")
else
shell.run("/rom/programs/turtle/go left")
for n=1, (Y - 1) do
CheckForAll()
shell.run("/rom/programs/turtle/go forward")
end
shell.run("/rom/programs/turtle/go left")
for n=1, (X - 1) do
CheckForAll()
shell.run("/rom/programs/turtle/go forward")
end
shell.run("/rom/programs/turtle/go left 2")
end
--
--Save result ------------------------------------
--
local file = fs.open(tArgs[3], "w")
for y=1, Y do
local line = ""
for x=1, X do
if VirtualMap[y][x] then
line = line.."1"
else
line = line.."0"
end
end
file.writeLine(line)
end
file.close()
print("Scanning done, result saved")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment