Barnet

MakeMap

Jun 17th, 2021 (edited)
215
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. map = {}
  2. x = 1
  3. y = 1
  4. direction = "+y"
  5.  
  6. filepath = arg[1]
  7. if filepath == nil then filepath = "map" end
  8. maxX = 1
  9. maxY = 1
  10.  
  11. function FaceDirection(d)
  12.   if direction == d then return end
  13.   if direction == "+y" then
  14.     if d == "+x" then
  15.       turtle.turnRight()
  16.     elseif d == "-x" then
  17.       turtle.turnLeft()
  18.     elseif d == "-y" then
  19.       turtle.turnRight()
  20.       turtle.turnRight()
  21.     end
  22.   elseif direction == "+x" then
  23.     if d == "+y" then
  24.       turtle.turnLeft()
  25.     elseif d == "-x" then
  26.       turtle.turnRight()
  27.       turtle.turnRight()
  28.     elseif d == "-y" then
  29.       turtle.turnRight()
  30.     end
  31.   elseif direction == "-x" then
  32.     if d == "+y" then
  33.       turtle.turnRight()
  34.     elseif d == "+x" then
  35.       turtle.turnRight()
  36.       turtle.turnRight()
  37.     elseif d == "-y" then
  38.       turtle.turnLeft()
  39.     end
  40.   elseif direction == "-y" then
  41.     if d == "+y" then
  42.       turtle.turnRight()
  43.       turtle.turnRight()
  44.     elseif d == "+x" then
  45.       turtle.turnLeft()
  46.     elseif d == "-x" then
  47.       turtle.turnRight()
  48.     end
  49.   end
  50.   direction = d
  51. end
  52.  
  53. function Checkblock()
  54.   local success, block = turtle.inspectDown()
  55.   if success then
  56.     if block.name == "botania:livingrock" or block.name == "botania:livingwood" or block.name == "botania:pure_daisy" or block.name == "minecraft:stone" or string.find(block.name, "log") ~= nil then
  57.       return true
  58.     end
  59.   end
  60.   return false
  61. end
  62.  
  63. function Move(d)
  64.   FaceDirection(d)
  65.   turtle.forward()
  66.   map[x .. " " .. y] = d
  67.  
  68.   if direction == "+x" then
  69.     x = x + 1
  70.   elseif direction == "-x" then
  71.     x = x - 1
  72.   elseif direction == "+y" then
  73.     y = y + 1
  74.   elseif direction == "-y" then
  75.     y = y - 1
  76.   end
  77.  
  78.   if x > maxX then
  79.     maxX = x
  80.   end
  81.  
  82.   if y > maxY then
  83.     maxY = y
  84.   end
  85. end
  86.  
  87. function Turn()
  88.   if direction == "+y" then
  89.     map[x .. " " .. y - 1] = "+x"
  90.     FaceDirection("+x")
  91.     turtle.forward()
  92.     x = x + 1
  93.     FaceDirection("-y")
  94.     turtle.forward()   
  95.     y = y - 1
  96.   elseif direction == "-y" then
  97.     map[x .. " " .. y + 1] = "+x"
  98.     FaceDirection("+x")
  99.     turtle.forward()
  100.     x = x + 1
  101.     FaceDirection("+y")
  102.     turtle.forward()   
  103.     y = y + 1
  104.   end
  105. end
  106.  
  107. while true do
  108.   if Checkblock() then
  109.     Move(direction)
  110.   else
  111.     Turn()
  112.     if not Checkblock() then
  113.       map[maxX .. " " .. y] = direction:sub(1,1) .. "E"
  114.       break
  115.     end
  116.   end
  117. end
  118.  
  119. maxY = maxY - 1
  120. file = fs.open(filepath, "w")
  121. file.writeLine(maxX .. " " .. maxY)
  122. for j = maxY, 1, -1 do
  123.   for i = 1, maxX, 1 do
  124.     v = map[i .. " " .. j]
  125.     if v == nil then
  126.       print(i .. " " .. j .. " doesn't exist")
  127.     else
  128.       if i == 1 and j == 1 then
  129.         file.write("+S ")
  130.       else
  131.         file.write(v .. " ")
  132.       end
  133.     end  
  134.   end
  135.   file.writeLine()
  136. end
  137. file.close()
RAW Paste Data