Barnet

Livify

Jun 17th, 2021 (edited)
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.97 KB | None | 0 0
  1. map = {}
  2. x = 1
  3. y = 1
  4. direction = "+y"
  5. reversing = false
  6.  
  7. filepath = arg[1]
  8. if filepath == nil then filepath = "map" end
  9.  
  10. function FindStuff()
  11.   if CheckSlot() then
  12.     return true
  13.   end
  14.  
  15.   for i = 1, 16, 1 do
  16.     turtle.select(i)
  17.     if CheckSlot() then
  18.       return true
  19.     end
  20.   end
  21.  
  22.   return false
  23. end
  24.  
  25. function CheckSlot()
  26.   if turtle.getItemCount() > 0 then
  27.     item = turtle.getItemDetail()
  28.     if item.name == "minecraft:stone" or string.find(item.name, "log") ~= nil then
  29.       return true
  30.     end
  31.   end
  32.   return false
  33. end
  34.  
  35. function ReadMap()
  36.   local file = fs.open(filepath, "r")
  37.   if file == nil then
  38.     shell.run("MakeMap")
  39.     reversing = true
  40.     file = fs.open(filepath, "r")
  41.   end
  42.   local line = file.readLine()
  43.   local bounds = line:gmatch("%S+")
  44.   local maxX = tonumber(bounds())
  45.   local maxY = tonumber(bounds())
  46.   local i = 1
  47.   local j = maxY
  48.  
  49.   while j >= 1 do
  50.     line = file.readLine()
  51.     if line == nil then
  52.       break
  53.     end
  54.    
  55.     i = 1
  56.     for word in line:gmatch("%S+") do
  57.       map[i .. " " .. j] = word
  58.       i = i + 1
  59.     end
  60.     j = j - 1
  61.   end
  62.   file.close()
  63. end
  64.  
  65. function ReadLog()
  66.   local log = fs.open("log", "r")
  67.   if log == nil then
  68.     UpdateLog()
  69.   else
  70.     x = tonumber(log.readLine())
  71.     y = tonumber(log.readLine())
  72.     direction = log.readLine()
  73.        reversing = log.readLine() == "true"
  74.     log.close()
  75.   end
  76. end
  77.  
  78. function UpdateLog()
  79.   local log = fs.open("log", "w")
  80.   log.writeLine(x)
  81.   log.writeLine(y)
  82.   log.writeLine(direction)
  83.   log.writeLine(reversing)
  84.   log.close()
  85. end
  86.  
  87. function Checkblock()
  88.   local success, block = turtle.inspectDown()
  89.   if success then
  90.     if block.name == "botania:livingrock" or block.name == "botania:livingwood" then
  91.       return "living"
  92.     elseif block.name == "minecraft:stone" or string.find(block.name, "log") ~= nil then
  93.       return "block"
  94.     elseif block.name == "botania:pure_daisy" then
  95.       return "daisy"
  96.     end
  97.   end
  98.   return "air"
  99. end
  100.  
  101. function Move(d)
  102.   if d == "-E" or d == "+E" then
  103.     reversing = true
  104.     UpdateLog()
  105.     ReturnHome()
  106.        return
  107.   elseif d == "+S" then
  108.     d = "+y"
  109.     reversing = false
  110.   end
  111.  
  112.   FaceDirection(d)
  113.   if direction == "+x" then
  114.     x = x + 1
  115.   elseif direction == "-x" then
  116.     x = x - 1
  117.   elseif direction == "+y" then
  118.     y = y + 1
  119.   elseif direction == "-y" then
  120.     y = y - 1
  121.   end
  122.   UpdateLog()
  123.   turtle.forward()
  124. end
  125.  
  126. function FaceDirection(d)
  127.   if direction == d then return end
  128.   if direction == "+y" then
  129.     if d == "+x" then
  130.       turtle.turnRight()
  131.     elseif d == "-x" then
  132.       turtle.turnLeft()
  133.     elseif d == "-y" then
  134.       turtle.turnRight()
  135.       turtle.turnRight()
  136.     end
  137.   elseif direction == "+x" then
  138.     if d == "+y" then
  139.       turtle.turnLeft()
  140.     elseif d == "-x" then
  141.       turtle.turnRight()
  142.       turtle.turnRight()
  143.     elseif d == "-y" then
  144.       turtle.turnRight()
  145.     end
  146.   elseif direction == "-x" then
  147.     if d == "+y" then
  148.       turtle.turnRight()
  149.     elseif d == "+x" then
  150.       turtle.turnRight()
  151.       turtle.turnRight()
  152.     elseif d == "-y" then
  153.       turtle.turnLeft()
  154.     end
  155.   elseif direction == "-y" then
  156.     if d == "+y" then
  157.       turtle.turnRight()
  158.       turtle.turnRight()
  159.     elseif d == "+x" then
  160.       turtle.turnLeft()
  161.     elseif d == "-x" then
  162.       turtle.turnRight()
  163.     end
  164.   end
  165.   direction = d
  166.   UpdateLog()
  167. end
  168.  
  169. function ReturnHome()
  170.   while x ~= 1 or y ~= 1 do
  171.     if x > 1 then
  172.          Move("-x")
  173.        elseif y > 1 then
  174.          Move("-y")
  175.        end
  176.   end
  177.   reversing = false
  178.   UpdateLog()
  179. end
  180.  
  181. ReadLog()
  182. ReadMap()
  183. if reversing then ReturnHome() end
  184. while FindStuff() do
  185.   local block = Checkblock()
  186.   if block == "living" then
  187.     turtle.digDown()
  188.     turtle.placeDown()
  189.   elseif block == "block" then
  190.     repeat
  191.       sleep(1)
  192.     until Checkblock() == "living"
  193.     turtle.digDown()
  194.     turtle.placeDown()
  195.   elseif block == "air" then
  196.     turtle.placeDown()
  197.   end  
  198.   Move(map[x .. " " .. y])
  199. end
  200.  
  201. print("Done")
Add Comment
Please, Sign In to add comment