MtnMCG

geoturtle

Sep 5th, 2024 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. -- Turtle Program
  2.  
  3. local modem = peripheral.find("modem") or error("No modem found")
  4. modem.open(10000)
  5.  
  6. local scanData = {}
  7. local miningTargets = {}
  8.  
  9. -- Function to update position based on GPS
  10. local function updatePosition()
  11. local x, y, z = gps.locate()
  12. if not x then error("Could not get GPS signal") end
  13. return math.floor(x + 0.5), math.floor(y + 0.5), math.floor(z + 0.5)
  14. end
  15.  
  16. -- Function to determine facing direction
  17. local function getFacing()
  18. local x, y, z = updatePosition()
  19. for i = 0, 3 do
  20. if turtle.forward() then
  21. local newX, newY, newZ = updatePosition()
  22. turtle.back()
  23. if newX > x then return 1 -- East
  24. elseif newX < x then return 3 -- West
  25. elseif newZ > z then return 2 -- South
  26. elseif newZ < z then return 0 -- North
  27. end
  28. end
  29. turtle.turnRight()
  30. end
  31. error("Could not determine facing direction")
  32. end
  33.  
  34. -- Initialize position and facing
  35. local x, y, z = updatePosition()
  36. local facing = getFacing()
  37. print("Turtle initialized at:", x, y, z, "facing", facing)
  38.  
  39. -- Function to turn to a specific direction
  40. local function turnTo(targetFacing)
  41. while facing ~= targetFacing do
  42. turtle.turnRight()
  43. facing = (facing + 1) % 4
  44. end
  45. end
  46.  
  47. -- Function to move to adjacent block
  48. local function moveTo(tx, ty, tz)
  49. if ty > y then
  50. while not turtle.up() do turtle.digUp() end
  51. y = y + 1
  52. elseif ty < y then
  53. while not turtle.down() do turtle.digDown() end
  54. y = y - 1
  55. else
  56. local dx, dz = tx - x, tz - z
  57. if dx ~= 0 then
  58. turnTo(dx > 0 and 1 or 3)
  59. while not turtle.forward() do turtle.dig() end
  60. x = tx
  61. elseif dz ~= 0 then
  62. turnTo(dz > 0 and 2 or 0)
  63. while not turtle.forward() do turtle.dig() end
  64. z = tz
  65. end
  66. end
  67. end
  68.  
  69. -- A* pathfinding algorithm
  70. local function findPath(startX, startY, startZ, endX, endY, endZ)
  71. local function heuristic(x, y, z)
  72. return math.abs(x - endX) + math.abs(y - endY) + math.abs(z - endZ)
  73. end
  74.  
  75. local function getNeighbors(x, y, z)
  76. local neighbors = {}
  77. for _, offset in ipairs({{1,0,0}, {-1,0,0}, {0,1,0}, {0,-1,0}, {0,0,1}, {0,0,-1}}) do
  78. local nx, ny, nz = x + offset[1], y + offset[2], z + offset[3]
  79. if scanData[nx..","..ny..","..nz] ~= "minecraft:bedrock" then
  80. table.insert(neighbors, {nx, ny, nz})
  81. end
  82. end
  83. return neighbors
  84. end
  85.  
  86. local openSet = {{x = startX, y = startY, z = startZ, g = 0, h = heuristic(startX, startY, startZ)}}
  87. local closedSet = {}
  88. local cameFrom = {}
  89.  
  90. while #openSet > 0 do
  91. table.sort(openSet, function(a, b) return (a.g + a.h) < (b.g + b.h) end)
  92. local current = table.remove(openSet, 1)
  93.  
  94. if current.x == endX and current.y == endY and current.z == endZ then
  95. local path = {}
  96. while current do
  97. table.insert(path, 1, {x = current.x, y = current.y, z = current.z})
  98. current = cameFrom[current.x .. "," .. current.y .. "," .. current.z]
  99. end
  100. return path
  101. end
  102.  
  103. closedSet[current.x .. "," .. current.y .. "," .. current.z] = true
  104.  
  105. for _, neighbor in ipairs(getNeighbors(current.x, current.y, current.z)) do
  106. local nx, ny, nz = table.unpack(neighbor)
  107. if not closedSet[nx .. "," .. ny .. "," .. nz] then
  108. local tentative_g = current.g + 1
  109. local node = {x = nx, y = ny, z = nz, g = tentative_g, h = heuristic(nx, ny, nz)}
  110. local idx = nil
  111. for i, v in ipairs(openSet) do
  112. if v.x == nx and v.y == ny and v.z == nz then
  113. idx = i
  114. break
  115. end
  116. end
  117. if idx then
  118. if tentative_g < openSet[idx].g then
  119. openSet[idx] = node
  120. cameFrom[nx .. "," .. ny .. "," .. nz] = current
  121. end
  122. else
  123. table.insert(openSet, node)
  124. cameFrom[nx .. "," .. ny .. "," .. nz] = current
  125. end
  126. end
  127. end
  128. end
  129. return nil -- No path found
  130. end
  131.  
  132. -- Function to mine to specific coordinates
  133. local function mineTo(tx, ty, tz)
  134. local path = findPath(x, y, z, tx, ty, tz)
  135. if not path then
  136. print("No path found to", tx, ty, tz)
  137. return false
  138. end
  139.  
  140. for i = 2, #path do -- Start from 2 because 1 is the current position
  141. local node = path[i]
  142. moveTo(node.x, node.y, node.z)
  143. end
  144.  
  145. return true
  146. end
  147.  
  148. -- Function to mine all targets
  149. local function mineAllTargets()
  150. for _, target in ipairs(miningTargets) do
  151. if mineTo(target.x, target.y, target.z) then
  152. turtle.dig()
  153. modem.transmit(10000, 10000, {cmd = "ore_mined", x = target.x, y = target.y, z = target.z})
  154. else
  155. modem.transmit(10000, 10000, {cmd = "error", msg = "Could not reach target"})
  156. end
  157. end
  158. modem.transmit(10000, 10000, {cmd = "mining_complete"})
  159. end
  160.  
  161. -- Main program loop
  162. while true do
  163. local event, side, channel, replyChannel, message = os.pullEvent("modem_message")
  164. if channel == 10000 then
  165. if message.cmd == "scan_data" then
  166. scanData = message.data
  167. print("Received scan data")
  168. elseif message.cmd == "mine_targets" then
  169. miningTargets = message.data
  170. print("Received mining targets. Starting mining operation.")
  171. mineAllTargets()
  172. end
  173. end
  174. end
Advertisement
Add Comment
Please, Sign In to add comment