MtnMCG

Geocomputer

Sep 5th, 2024 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. -- Computer Program
  2.  
  3. local modem = peripheral.find("modem") or error("No modem found")
  4. local geoScanner = peripheral.find("geoScanner") or error("No geo scanner found")
  5.  
  6. modem.open(10000)
  7.  
  8. -- Get computer's GPS coordinates
  9. local compX, compY, compZ = gps.locate()
  10. if not compX then error("GPS signal not found") end
  11.  
  12. print("Computer position:", compX, compY, compZ)
  13.  
  14. -- Function to convert scanner coordinates to world coordinates
  15. local function toWorldCoords(relX, relY, relZ)
  16. return compX + relX, compY + relY, compZ + relZ
  17. end
  18.  
  19. -- Function to perform chunked scanning
  20. local function chunkedScan(radius)
  21. local scanData = {}
  22. local chunkSize = 8 -- GeoScanner's maximum scan size
  23. local scansPerformed = 0
  24. local totalScans = math.ceil((radius * 2 + 1) ^ 3 / chunkSize ^ 3)
  25.  
  26. for x = -radius, radius, chunkSize do
  27. for y = -radius, radius, chunkSize do
  28. for z = -radius, radius, chunkSize do
  29. local chunkRadius = math.min(chunkSize / 2, radius - math.max(math.abs(x), math.abs(y), math.abs(z)))
  30. if chunkRadius > 0 then
  31. scansPerformed = scansPerformed + 1
  32. print(string.format("Performing scan %d of %d", scansPerformed, totalScans))
  33.  
  34. local scan = geoScanner.scan(chunkRadius, x + chunkRadius, y + chunkRadius, z + chunkRadius)
  35. if scan then
  36. for _, block in ipairs(scan) do
  37. local wx, wy, wz = toWorldCoords(x + block.x, y + block.y, z + block.z)
  38. scanData[wx..","..wy..","..wz] = block.name
  39. end
  40. print(string.format("Chunk scan successful. Found %d blocks.", #scan))
  41. else
  42. print(string.format("Chunk scan failed at (%d, %d, %d)", x, y, z))
  43. end
  44. end
  45. end
  46. end
  47. end
  48. return scanData
  49. end
  50.  
  51. -- Function to send command to turtle
  52. local function sendCommand(cmd, data)
  53. modem.transmit(10000, 10000, {command = cmd, data = data})
  54. end
  55.  
  56. -- Main program
  57. print("Enter scan radius:")
  58. local radius = tonumber(read())
  59. if not radius or radius <= 0 then
  60. error("Invalid scan radius")
  61. end
  62.  
  63. print("Scanning with radius " .. radius .. "...")
  64. local scanData = chunkedScan(radius)
  65.  
  66. local blockCount = 0
  67. for _ in pairs(scanData) do blockCount = blockCount + 1 end
  68. print("Scan complete. Found " .. blockCount .. " blocks.")
  69.  
  70. print("Enter the ore to mine (e.g., minecraft:diamond_ore):")
  71. local oreToMine = read()
  72.  
  73. local oreLocations = {}
  74. for coords, blockName in pairs(scanData) do
  75. if blockName == oreToMine then
  76. local x, y, z = coords:match("(-?%d+),(-?%d+),(-?%d+)")
  77. table.insert(oreLocations, {x = tonumber(x), y = tonumber(y), z = tonumber(z)})
  78. end
  79. end
  80.  
  81. if #oreLocations == 0 then
  82. print("No " .. oreToMine .. " found in scan area.")
  83. else
  84. print("Found " .. #oreLocations .. " " .. oreToMine .. " deposits.")
  85. print("Sending data to turtle...")
  86. sendCommand("scan_data", scanData)
  87. sendCommand("mine_targets", oreLocations)
  88.  
  89. print("Waiting for mining operation to complete...")
  90. while true do
  91. local event, side, channel, replyChannel, message = os.pullEvent("modem_message")
  92. if channel == 10000 then
  93. if message.command == "target_mined" then
  94. print("Ore mined at:", message.x, message.y, message.z)
  95. elseif message.command == "mining_complete" then
  96. print("All ores mined. Operation complete.")
  97. break
  98. elseif message.command == "mining_failed" then
  99. print("Mining operation failed:", message.reason)
  100. break
  101. end
  102. end
  103. end
  104. end
  105.  
  106. print("Program complete.")
Advertisement
Add Comment
Please, Sign In to add comment