feat: Implement comprehensive block scanning; report discovered blocks to server
This commit is contained in:
92
turtle.lua
92
turtle.lua
@@ -409,6 +409,92 @@ local function navigateTowards(target)
|
||||
return getDistance(state.position, target) < 2
|
||||
end
|
||||
|
||||
-- Comprehensive block scanning - scans all 6 directions around the turtle
|
||||
local function scanAllDirections()
|
||||
if not state.position then
|
||||
return {} -- Can't determine block positions without knowing turtle position
|
||||
end
|
||||
|
||||
local scannedBlocks = {}
|
||||
local myID = os.getComputerID()
|
||||
|
||||
-- Helper to add block with absolute world coordinates
|
||||
local function addBlock(relX, relY, relZ, blockData)
|
||||
if blockData and blockData.name then
|
||||
local worldPos = {
|
||||
x = state.position.x + relX,
|
||||
y = state.position.y + relY,
|
||||
z = state.position.z + relZ
|
||||
}
|
||||
table.insert(scannedBlocks, {
|
||||
x = worldPos.x,
|
||||
y = worldPos.y,
|
||||
z = worldPos.z,
|
||||
name = blockData.name,
|
||||
metadata = blockData.metadata or 0,
|
||||
discoveredBy = myID
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
-- Scan up and down (always accessible)
|
||||
local hasBlockUp, dataUp = turtle.inspectUp()
|
||||
if hasBlockUp and dataUp then
|
||||
addBlock(0, 1, 0, dataUp)
|
||||
end
|
||||
|
||||
local hasBlockDown, dataDown = turtle.inspectDown()
|
||||
if hasBlockDown and dataDown then
|
||||
addBlock(0, -1, 0, dataDown)
|
||||
end
|
||||
|
||||
-- Scan all 4 horizontal directions by rotating
|
||||
-- We'll scan forward, then rotate 4 times to get all sides
|
||||
local originalFacing = facing
|
||||
|
||||
for i = 0, 3 do
|
||||
local hasBlock, data = turtle.inspect()
|
||||
if hasBlock and data then
|
||||
-- Calculate relative position based on current facing
|
||||
local relX, relZ = 0, 0
|
||||
if facing == 0 then relZ = -1 -- North
|
||||
elseif facing == 1 then relX = 1 -- East
|
||||
elseif facing == 2 then relZ = 1 -- South
|
||||
elseif facing == 3 then relX = -1 -- West
|
||||
end
|
||||
addBlock(relX, 0, relZ, data)
|
||||
end
|
||||
|
||||
-- Rotate to next direction (unless this is the last iteration)
|
||||
if i < 3 then
|
||||
smartTurnRight()
|
||||
end
|
||||
end
|
||||
|
||||
-- We've rotated 3 times (270 degrees), rotate once more to get back to original facing
|
||||
-- Original facing is at position 0, we're now at position 3, so turn once more to get to 4 (same as 0)
|
||||
if facing ~= originalFacing then
|
||||
smartTurnRight()
|
||||
end
|
||||
|
||||
return scannedBlocks
|
||||
end
|
||||
|
||||
-- Send discovered blocks to server
|
||||
local function reportDiscoveredBlocks(blocks)
|
||||
if #blocks == 0 then return end
|
||||
|
||||
-- Send blocks to webbridge which will forward to server
|
||||
modem.transmit(CHANNEL_SEND, CHANNEL_RECEIVE, {
|
||||
type = "blocks_discovered",
|
||||
turtleID = os.getComputerID(),
|
||||
blocks = blocks,
|
||||
timestamp = os.epoch("utc")
|
||||
})
|
||||
|
||||
print("📦 Reported " .. #blocks .. " blocks to server")
|
||||
end
|
||||
|
||||
-- Status broadcasting
|
||||
function broadcastStatus()
|
||||
updateFuel()
|
||||
@@ -621,6 +707,12 @@ local function exploreStep()
|
||||
markVisited(state.position)
|
||||
end
|
||||
|
||||
-- Perform comprehensive scan of all surrounding blocks
|
||||
local discoveredBlocks = scanAllDirections()
|
||||
if #discoveredBlocks > 0 then
|
||||
reportDiscoveredBlocks(discoveredBlocks)
|
||||
end
|
||||
|
||||
-- Check if stuck in same position
|
||||
if isStuckInSamePosition() then
|
||||
state.stuckInHoleCounter = state.stuckInHoleCounter + 1
|
||||
|
||||
Reference in New Issue
Block a user