ForAbby

FloodFill

Aug 13th, 2025 (edited)
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.29 KB | Source Code | 0 0
  1.  
  2.  
  3. -- local turtle = require("turtle")
  4. -- local math = require("math")
  5.  
  6. -- ### World ###
  7.  
  8. --  structure of a block:
  9. --  {
  10. --      name = "minecraft:oak_log",
  11. --      state = { axis = "x" },
  12. --      tags = { ["minecraft:logs"] = true, ... },
  13. --  }
  14.  
  15. local block_map = {}
  16.  
  17. function ClearMap()
  18.     block_map = {}
  19. end
  20.  
  21. function GetBlock(pos)
  22.     return block_map[tostring(pos)]
  23. end
  24.  
  25. function RegisterBlock(pos, block)
  26.     if block == nil then
  27.         block_map[tostring(pos)] = {name="minecraft:air"}
  28.     else
  29.         block_map[tostring(pos)] = block
  30.     end
  31. end
  32.  
  33.  
  34. -- ### Vector
  35. Vector = {__type = "Vector"}
  36. VectorMT = {__index = Vector}
  37.  
  38. function VectorMT.__add(a, b)
  39.     if type(a) == "number" then
  40.         if type(b) == "Vector" then
  41.             return Vector.new(a + b.x, a + b.y)
  42.         else
  43.             return Vector.new()
  44.         end
  45.     elseif type(a) == "Vector" then
  46.         if type(b) == "Vector" then
  47.             return Vector.new(a.x + b.x, a.y + b.y)
  48.         elseif type(b) == "number" then
  49.             return Vector.new(a.x + b, a.y + b)
  50.         else
  51.             return Vector.new()
  52.         end
  53.     end
  54.     return Vector.new()
  55. end
  56.  
  57. function VectorMT.__tostring(t)
  58.     return t.x .. ", " .. t.y
  59. end
  60.  
  61. function Vector.new(x, y)
  62.     local self = setmetatable({}, VectorMT)
  63.     self.x = x or 0
  64.     self.y = y or 0
  65.     return self
  66. end
  67.  
  68. function Vector:copy()
  69.     return Vector.new(self.x, self.y)
  70. end
  71.  
  72.  
  73. -- ### Movement ###
  74. ORIENTATION = {
  75.     [0] = Vector.new(1, 0),
  76.     [1] = Vector.new(0, 1),
  77.     [2] = Vector.new(-1, 0),
  78.     [3] = Vector.new(0, -1)}
  79.  
  80. local position = Vector.new(0, 0)
  81. local orientation = 0
  82.  
  83. function Move(distance)
  84.     RegisterLeft()
  85.     local is_block, block_infront = RegisterFront()
  86.     RegisterRight()
  87.     while is_block == false and distance > 0 do
  88.         turtle.forward()
  89.         print("Before move - Position: " .. tostring(position))
  90.         print("Current orientation: " .. orientation)
  91.         print("Adding vector: " .. tostring(ORIENTATION[orientation]))
  92.         position = position + ORIENTATION[orientation]
  93.         print("After move - Position: " .. tostring(position))
  94. RegisterLeft()
  95.         is_block, block_infront = RegisterFront()
  96.         RegisterRight()
  97.         distance = distance - 1
  98.     end
  99. end
  100.  
  101. function RegisterFront()
  102.     local front_position = position + ORIENTATION[orientation]
  103.     local is_block, block_infront = turtle.inspect()
  104.     RegisterBlock(front_position, block_infront)
  105.     return is_block, block_infront
  106. end
  107.  
  108. function RegisterLeft()
  109.     turtle.turnLeft()
  110.     local o = orientation - 1
  111.     if o == 0 then
  112.         o = 4
  113.     end
  114.     local is_block, block = RegisterFront()
  115.     turtle.turnRight()
  116.     return is_block, block
  117. end
  118.  
  119. function RegisterRight()
  120.     turtle.turnRight()
  121.     local o = orientation + 1
  122.     if o == 5 then
  123.         o = 1
  124.     end
  125.     local is_block, block = RegisterFront()
  126.     turtle.turnLeft()
  127.     return is_block, block
  128. end
  129.  
  130. ClearMap()
  131. -- local block = {name = "computercraft:advanced_turtle"}
  132. -- RegisterBlock(Vector.new(0,0), block)
  133. Move(10)
  134.  
  135.  
  136.  
  137. print("Now printing moves")
  138. local count = 0
  139. for k, v in pairs(block_map) do
  140.     count = count + 1
  141.     print(" - Block at "..k.." is "..textutils.serialise(v.name))
  142. end
  143. print("Block Map length:" .. count)
  144.  
Advertisement
Add Comment
Please, Sign In to add comment