MtnMCG

compass

Jul 22nd, 2024 (edited)
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.22 KB | None | 0 0
  1. -- Enhanced Map Tracker for CC:Tweaked
  2. -- For Minecraft Forge 1.20.1
  3.  
  4. local monitor = peripheral.find("monitor") or term.current()
  5. local w, h = monitor.getSize()
  6.  
  7. local visited = {}
  8. local mapSize = math.min(w, h) - 4  -- Leave space for borders and buttons
  9. local zoomLevel = 1
  10. local mapOffsetX, mapOffsetY = 0, 0
  11. local lastKnownPosition = nil
  12.  
  13. -- Function to get GPS position
  14. local function getGPSPosition()
  15.     local x, y, z = gps.locate(2) -- 2 second timeout for reliable updates
  16.     if x then
  17.         return vector.new(math.floor(x), math.floor(y), math.floor(z))
  18.     else
  19.         return nil
  20.     end
  21. end
  22.  
  23. -- Function to mark visited location
  24. local function markVisited(position)
  25.     local key = tostring(position.x) .. "," .. tostring(position.z)
  26.     visited[key] = true
  27. end
  28.  
  29. -- Function to check if a location has been visited
  30. local function isVisited(x, z)
  31.     local key = tostring(x) .. "," .. tostring(z)
  32.     return visited[key] or false
  33. end
  34.  
  35. -- Function to draw the map
  36. local function drawMap()
  37.     if not lastKnownPosition then return end
  38.    
  39.     monitor.clear()
  40.     monitor.setCursorPos(1, 1)
  41.     monitor.write("Map Mode (Zoom: " .. zoomLevel .. "x)")
  42.  
  43.     local halfSize = math.floor(mapSize / 2)
  44.     local centerX, centerY = math.floor(w / 2), math.floor(h / 2)
  45.  
  46.     for dx = -halfSize, halfSize do
  47.         for dz = -halfSize, halfSize do
  48.             local x = lastKnownPosition.x + dx * zoomLevel + mapOffsetX
  49.             local z = lastKnownPosition.z + dz * zoomLevel + mapOffsetY
  50.             local screenX = centerX + dx
  51.             local screenY = centerY + dz
  52.  
  53.             if screenX > 0 and screenX <= w and screenY > 0 and screenY <= h - 2 then
  54.                 monitor.setCursorPos(screenX, screenY)
  55.                 if dx == 0 and dz == 0 and mapOffsetX == 0 and mapOffsetY == 0 then
  56.                     monitor.setBackgroundColor(colors.red)  -- Player position
  57.                 elseif isVisited(x, z) then
  58.                     monitor.setBackgroundColor(colors.green)  -- Visited block
  59.                 else
  60.                     monitor.setBackgroundColor(colors.gray)  -- Unvisited block
  61.                 end
  62.                 monitor.write(" ")
  63.                 monitor.setBackgroundColor(colors.black)  -- Reset background color
  64.             end
  65.         end
  66.     end
  67.  
  68.     -- Draw buttons
  69.     monitor.setCursorPos(1, h-1)
  70.     monitor.write("[-] [+] [<] [>] [^] [v] [C] [Scan]")
  71.  
  72.     -- Display current position
  73.     monitor.setCursorPos(1, h)
  74.     monitor.write(string.format("Pos: %d, %d, %d", lastKnownPosition.x, lastKnownPosition.y, lastKnownPosition.z))
  75. end
  76.  
  77. -- Function to handle button clicks
  78. local function handleClick(x, y)
  79.     if y ~= h-1 then return false end
  80.  
  81.     if x >= 1 and x <= 3 then
  82.         zoomLevel = math.max(1, zoomLevel - 1)  -- Zoom out
  83.     elseif x >= 5 and x <= 7 then
  84.         zoomLevel = zoomLevel + 1  -- Zoom in
  85.     elseif x >= 9 and x <= 11 then
  86.         mapOffsetX = mapOffsetX - zoomLevel  -- Move left
  87.     elseif x >= 13 and x <= 15 then
  88.         mapOffsetX = mapOffsetX + zoomLevel  -- Move right
  89.     elseif x >= 17 and x <= 19 then
  90.         mapOffsetY = mapOffsetY - zoomLevel  -- Move up
  91.     elseif x >= 21 and x <= 23 then
  92.         mapOffsetY = mapOffsetY + zoomLevel  -- Move down
  93.     elseif x >= 25 and x <= 27 then
  94.         mapOffsetX, mapOffsetY = 0, 0  -- Recenter
  95.     elseif x >= 29 and x <= 34 then
  96.         return "scan"  -- Switch to scan mode
  97.     else
  98.         return false
  99.     end
  100.  
  101.     return true
  102. end
  103.  
  104. -- Scanning mode function
  105. local function scanningMode()
  106.     while true do
  107.         monitor.clear()
  108.         monitor.setCursorPos(1, 1)
  109.         monitor.write("Scanning Mode")
  110.         monitor.setCursorPos(1, 3)
  111.         monitor.write("Touch screen to stop scanning")
  112.        
  113.         local position = getGPSPosition()
  114.         if position then
  115.             markVisited(position)
  116.             lastKnownPosition = position
  117.             monitor.setCursorPos(1, 5)
  118.             monitor.write(string.format("Current Pos: %d, %d, %d", position.x, position.y, position.z))
  119.         else
  120.             monitor.setCursorPos(1, 5)
  121.             monitor.write("Unable to get GPS position")
  122.         end
  123.        
  124.         local timer = os.startTimer(1)
  125.         local event = os.pullEvent()
  126.         if event == "monitor_touch" then
  127.             return "map"
  128.         end
  129.     end
  130. end
  131.  
  132. -- Map mode function
  133. local function mapMode()
  134.     if not lastKnownPosition then
  135.         monitor.clear()
  136.         monitor.setCursorPos(1, 1)
  137.         monitor.write("No position data available")
  138.         monitor.setCursorPos(1, 3)
  139.         monitor.write("Touch screen to start scanning")
  140.         os.pullEvent("monitor_touch")
  141.         return "scan"
  142.     end
  143.  
  144.     drawMap()
  145.  
  146.     while true do
  147.         local event, _, x, y = os.pullEvent("monitor_touch")
  148.         local result = handleClick(x, y)
  149.         if result == "scan" then
  150.             return "scan"
  151.         elseif result then
  152.             drawMap()
  153.         end
  154.     end
  155. end
  156.  
  157. -- Main function
  158. local function main()
  159.     local mode = "scan"
  160.     while true do
  161.         if mode == "scan" then
  162.             mode = scanningMode()
  163.         else
  164.             mode = mapMode()
  165.         end
  166.     end
  167. end
  168.  
  169. -- Run the main function
  170. pcall(main)
Advertisement
Add Comment
Please, Sign In to add comment