Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Enhanced Map Tracker for CC:Tweaked
- -- For Minecraft Forge 1.20.1
- local monitor = peripheral.find("monitor") or term.current()
- local w, h = monitor.getSize()
- local visited = {}
- local mapSize = math.min(w, h) - 4 -- Leave space for borders and buttons
- local zoomLevel = 1
- local mapOffsetX, mapOffsetY = 0, 0
- local lastKnownPosition = nil
- -- Function to get GPS position
- local function getGPSPosition()
- local x, y, z = gps.locate(2) -- 2 second timeout for reliable updates
- if x then
- return vector.new(math.floor(x), math.floor(y), math.floor(z))
- else
- return nil
- end
- end
- -- Function to mark visited location
- local function markVisited(position)
- local key = tostring(position.x) .. "," .. tostring(position.z)
- visited[key] = true
- end
- -- Function to check if a location has been visited
- local function isVisited(x, z)
- local key = tostring(x) .. "," .. tostring(z)
- return visited[key] or false
- end
- -- Function to draw the map
- local function drawMap()
- if not lastKnownPosition then return end
- monitor.clear()
- monitor.setCursorPos(1, 1)
- monitor.write("Map Mode (Zoom: " .. zoomLevel .. "x)")
- local halfSize = math.floor(mapSize / 2)
- local centerX, centerY = math.floor(w / 2), math.floor(h / 2)
- for dx = -halfSize, halfSize do
- for dz = -halfSize, halfSize do
- local x = lastKnownPosition.x + dx * zoomLevel + mapOffsetX
- local z = lastKnownPosition.z + dz * zoomLevel + mapOffsetY
- local screenX = centerX + dx
- local screenY = centerY + dz
- if screenX > 0 and screenX <= w and screenY > 0 and screenY <= h - 2 then
- monitor.setCursorPos(screenX, screenY)
- if dx == 0 and dz == 0 and mapOffsetX == 0 and mapOffsetY == 0 then
- monitor.setBackgroundColor(colors.red) -- Player position
- elseif isVisited(x, z) then
- monitor.setBackgroundColor(colors.green) -- Visited block
- else
- monitor.setBackgroundColor(colors.gray) -- Unvisited block
- end
- monitor.write(" ")
- monitor.setBackgroundColor(colors.black) -- Reset background color
- end
- end
- end
- -- Draw buttons
- monitor.setCursorPos(1, h-1)
- monitor.write("[-] [+] [<] [>] [^] [v] [C] [Scan]")
- -- Display current position
- monitor.setCursorPos(1, h)
- monitor.write(string.format("Pos: %d, %d, %d", lastKnownPosition.x, lastKnownPosition.y, lastKnownPosition.z))
- end
- -- Function to handle button clicks
- local function handleClick(x, y)
- if y ~= h-1 then return false end
- if x >= 1 and x <= 3 then
- zoomLevel = math.max(1, zoomLevel - 1) -- Zoom out
- elseif x >= 5 and x <= 7 then
- zoomLevel = zoomLevel + 1 -- Zoom in
- elseif x >= 9 and x <= 11 then
- mapOffsetX = mapOffsetX - zoomLevel -- Move left
- elseif x >= 13 and x <= 15 then
- mapOffsetX = mapOffsetX + zoomLevel -- Move right
- elseif x >= 17 and x <= 19 then
- mapOffsetY = mapOffsetY - zoomLevel -- Move up
- elseif x >= 21 and x <= 23 then
- mapOffsetY = mapOffsetY + zoomLevel -- Move down
- elseif x >= 25 and x <= 27 then
- mapOffsetX, mapOffsetY = 0, 0 -- Recenter
- elseif x >= 29 and x <= 34 then
- return "scan" -- Switch to scan mode
- else
- return false
- end
- return true
- end
- -- Scanning mode function
- local function scanningMode()
- while true do
- monitor.clear()
- monitor.setCursorPos(1, 1)
- monitor.write("Scanning Mode")
- monitor.setCursorPos(1, 3)
- monitor.write("Touch screen to stop scanning")
- local position = getGPSPosition()
- if position then
- markVisited(position)
- lastKnownPosition = position
- monitor.setCursorPos(1, 5)
- monitor.write(string.format("Current Pos: %d, %d, %d", position.x, position.y, position.z))
- else
- monitor.setCursorPos(1, 5)
- monitor.write("Unable to get GPS position")
- end
- local timer = os.startTimer(1)
- local event = os.pullEvent()
- if event == "monitor_touch" then
- return "map"
- end
- end
- end
- -- Map mode function
- local function mapMode()
- if not lastKnownPosition then
- monitor.clear()
- monitor.setCursorPos(1, 1)
- monitor.write("No position data available")
- monitor.setCursorPos(1, 3)
- monitor.write("Touch screen to start scanning")
- os.pullEvent("monitor_touch")
- return "scan"
- end
- drawMap()
- while true do
- local event, _, x, y = os.pullEvent("monitor_touch")
- local result = handleClick(x, y)
- if result == "scan" then
- return "scan"
- elseif result then
- drawMap()
- end
- end
- end
- -- Main function
- local function main()
- local mode = "scan"
- while true do
- if mode == "scan" then
- mode = scanningMode()
- else
- mode = mapMode()
- end
- end
- end
- -- Run the main function
- pcall(main)
Advertisement
Add Comment
Please, Sign In to add comment