Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Advanced OpenGlasses 2 AR Control System
- -- Version 2.0 - Enhanced Edition
- local component = require("component")
- local event = require("event")
- local term = require("term")
- local computer = require("computer")
- local serialization = require("serialization")
- local filesystem = require("filesystem")
- -- Components
- local gpu = component.gpu
- local glasses = component.glasses
- local nav = component.navigation
- local radar = component.radar
- local geo = component.geolyzer or nil
- -- Configuration
- local config = {
- radarEnabled = false,
- radarRange = 64,
- radarUpdateInterval = 0.5,
- radarShowPlayers = true,
- radarShowMobs = true,
- fullbrightEnabled = false,
- fullbrightOpacity = 0.3,
- geolyserEnabled = false,
- geolyserRadius = 8,
- geolyserDepth = 32,
- hudEnabled = true,
- showCoordinates = true,
- showCompass = true,
- showFPS = false,
- colors = {
- player = 0x00FF00,
- hostile = 0xFF0000,
- passive = 0x00FFFF,
- item = 0xFFFF00,
- waypoint = 0xFFFFFF,
- ore = 0xFFAA00
- }
- }
- -- State
- local widgets = {
- radar = {},
- fullbright = nil,
- geolyser = {},
- waypoints = {},
- hud = {}
- }
- local running = true
- local playerPos = {x=0, y=0, z=0}
- local playerFacing = 0
- local savedWaypoints = {}
- -- Utility Functions
- local function saveConfig()
- local file = io.open("/home/ar_config.cfg", "w")
- if file then
- file:write(serialization.serialize(config))
- file:close()
- end
- end
- local function loadConfig()
- if filesystem.exists("/home/ar_config.cfg") then
- local file = io.open("/home/ar_config.cfg", "r")
- if file then
- local content = file:read("*a")
- file:close()
- local success, loaded = pcall(serialization.unserialize, content)
- if success then
- for k, v in pairs(loaded) do
- config[k] = v
- end
- end
- end
- end
- end
- local function saveWaypoints()
- local file = io.open("/home/ar_waypoints.dat", "w")
- if file then
- file:write(serialization.serialize(savedWaypoints))
- file:close()
- end
- end
- local function loadWaypoints()
- if filesystem.exists("/home/ar_waypoints.dat") then
- local file = io.open("/home/ar_waypoints.dat", "r")
- if file then
- local content = file:read("*a")
- file:close()
- local success, loaded = pcall(serialization.unserialize, content)
- if success then
- savedWaypoints = loaded
- end
- end
- end
- end
- local function updatePlayerPos()
- local x, y, z = nav.getPosition()
- playerPos = {x=math.floor(x), y=math.floor(y), z=math.floor(z)}
- playerFacing = nav.getFacing()
- end
- local function getDistance(x1, y1, z1, x2, y2, z2)
- return math.sqrt((x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2)
- end
- local function getCardinalDirection(yaw)
- yaw = yaw % 360
- if yaw < 0 then yaw = yaw + 360 end
- if yaw >= 337.5 or yaw < 22.5 then return "S"
- elseif yaw >= 22.5 and yaw < 67.5 then return "SW"
- elseif yaw >= 67.5 and yaw < 112.5 then return "W"
- elseif yaw >= 112.5 and yaw < 157.5 then return "NW"
- elseif yaw >= 157.5 and yaw < 202.5 then return "N"
- elseif yaw >= 202.5 and yaw < 247.5 then return "NE"
- elseif yaw >= 247.5 and yaw < 292.5 then return "E"
- elseif yaw >= 292.5 and yaw < 337.5 then return "SE"
- end
- return "?"
- end
- -- Widget Management
- local function clearAllWidgets()
- glasses.removeAll()
- widgets.radar = {}
- widgets.fullbright = nil
- widgets.geolyser = {}
- widgets.waypoints = {}
- widgets.hud = {}
- end
- local function clearRadar()
- for _, widget in pairs(widgets.radar) do
- if widget and widget.removeWidget then
- widget.removeWidget()
- end
- end
- widgets.radar = {}
- end
- local function clearGeolyser()
- for _, widget in pairs(widgets.geolyser) do
- if widget and widget.removeWidget then
- widget.removeWidget()
- end
- end
- widgets.geolyser = {}
- end
- local function clearHUD()
- for _, widget in pairs(widgets.hud) do
- if widget and widget.removeWidget then
- widget.removeWidget()
- end
- end
- widgets.hud = {}
- end
- -- Fullbright
- local function enableFullbright()
- if widgets.fullbright then
- widgets.fullbright.removeWidget()
- end
- widgets.fullbright = glasses.addCube3D()
- widgets.fullbright.set3DPos(0, 0, 0)
- widgets.fullbright.setColor(255, 255, 255)
- widgets.fullbright.setAlpha(config.fullbrightOpacity)
- widgets.fullbright.setScale(200)
- widgets.fullbright.setVisibleThroughObjects(true)
- config.fullbrightEnabled = true
- end
- local function disableFullbright()
- if widgets.fullbright then
- widgets.fullbright.removeWidget()
- widgets.fullbright = nil
- end
- config.fullbrightEnabled = false
- end
- -- HUD
- local function updateHUD()
- if not config.hudEnabled then return end
- clearHUD()
- updatePlayerPos()
- local yOffset = -90
- -- Coordinates
- if config.showCoordinates then
- local coordText = glasses.addTextLabel()
- coordText.setText(string.format("XYZ: %d, %d, %d", playerPos.x, playerPos.y, playerPos.z))
- coordText.setPosition(5, yOffset)
- coordText.setColor(255, 255, 255)
- coordText.setScale(1.0)
- table.insert(widgets.hud, coordText)
- yOffset = yOffset + 10
- end
- -- Compass
- if config.showCompass then
- local direction = getCardinalDirection(playerFacing)
- local compassText = glasses.addTextLabel()
- compassText.setText(string.format("Facing: %s (%.1f°)", direction, playerFacing))
- compassText.setPosition(5, yOffset)
- compassText.setColor(255, 255, 0)
- compassText.setScale(1.0)
- table.insert(widgets.hud, compassText)
- yOffset = yOffset + 10
- end
- -- Status indicators
- local statusText = glasses.addTextLabel()
- local status = {}
- if config.radarEnabled then table.insert(status, "RADAR") end
- if config.fullbrightEnabled then table.insert(status, "BRIGHT") end
- if config.geolyserEnabled then table.insert(status, "GEO") end
- if #status > 0 then
- statusText.setText("Active: " .. table.concat(status, " | "))
- else
- statusText.setText("All systems standby")
- end
- statusText.setPosition(5, yOffset)
- statusText.setColor(0, 255, 0)
- statusText.setScale(0.8)
- table.insert(widgets.hud, statusText)
- end
- -- Radar
- local function updateRadar()
- if not config.radarEnabled then return end
- clearRadar()
- updatePlayerPos()
- local entities = radar.getPlayers(config.radarRange)
- for _, ent in ipairs(entities) do
- local dist = getDistance(playerPos.x, playerPos.y, playerPos.z, ent.x, ent.y, ent.z)
- -- Skip if too close (probably the player)
- if dist > 1 then
- local color = config.colors.player
- local isHostile = false
- -- Classify entity
- local name = ent.name or "Unknown"
- if name:match("Creeper") or name:match("Zombie") or name:match("Skeleton") or
- name:match("Spider") or name:match("Witch") or name:match("Enderman") then
- color = config.colors.hostile
- isHostile = true
- elseif name:match("Cow") or name:match("Sheep") or name:match("Pig") or
- name:match("Chicken") or name:match("Horse") then
- color = config.colors.passive
- end
- -- Create marker
- local dot = glasses.addDot3D()
- dot.set3DPos(ent.x, ent.y + 1, ent.z)
- dot.setColor((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF)
- dot.setSize(isHostile and 3 or 2)
- dot.setAlpha(0.9)
- -- Create label
- local label = glasses.addFloatingText()
- label.set3DPos(ent.x, ent.y + 2, ent.z)
- label.setText(string.format("%s\n%.1fm", name, dist))
- label.setColor((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF)
- label.setScale(0.5)
- label.setAlpha(0.8)
- -- Add warning for hostile mobs nearby
- if isHostile and dist < 10 then
- local warning = glasses.addFloatingText()
- warning.set3DPos(ent.x, ent.y + 2.5, ent.z)
- warning.setText("⚠ DANGER ⚠")
- warning.setColor(255, 0, 0)
- warning.setScale(0.7)
- warning.setAlpha(1.0)
- table.insert(widgets.radar, warning)
- end
- table.insert(widgets.radar, dot)
- table.insert(widgets.radar, label)
- end
- end
- end
- -- Geolyser
- local function isOre(hardness)
- return hardness >= 3 and hardness <= 5
- end
- local function updateGeolyser()
- if not config.geolyserEnabled or not geo then return end
- clearGeolyser()
- updatePlayerPos()
- local radius = config.geolyserRadius
- local depth = config.geolyserDepth
- local foundOres = 0
- -- Scan in layers
- for y = 0, -depth, -2 do
- local scan = geo.scan(playerPos.x, playerPos.y + y, playerPos.z, radius, radius, 1)
- local idx = 1
- for dx = -radius, radius-1 do
- for dz = -radius, radius-1 do
- local hardness = scan[idx]
- idx = idx + 1
- if hardness and isOre(hardness) then
- local wx = playerPos.x + dx
- local wy = playerPos.y + y
- local wz = playerPos.z + dz
- local cube = glasses.addCube3D()
- cube.set3DPos(wx + 0.5, wy + 0.5, wz + 0.5)
- cube.setColor(255, 170, 0)
- cube.setAlpha(0.4)
- cube.setScale(0.8)
- cube.setVisibleThroughObjects(true)
- table.insert(widgets.geolyser, cube)
- foundOres = foundOres + 1
- end
- end
- end
- end
- -- Update HUD with ore count
- if foundOres > 0 then
- local oreLabel = glasses.addTextLabel()
- oreLabel.setText(string.format("Ores detected: %d", foundOres))
- oreLabel.setPosition(5, -70)
- oreLabel.setColor(255, 170, 0)
- oreLabel.setScale(1.0)
- table.insert(widgets.hud, oreLabel)
- end
- end
- -- Waypoint Management
- local function clearWaypoints()
- for _, widget in pairs(widgets.waypoints) do
- if widget and widget.removeWidget then
- widget.removeWidget()
- end
- end
- widgets.waypoints = {}
- end
- local function addWaypoint(name, x, y, z, color)
- color = color or config.colors.waypoint
- local marker = glasses.addDot3D()
- marker.set3DPos(x, y, z)
- marker.setColor((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF)
- marker.setSize(4)
- marker.setAlpha(1.0)
- local label = glasses.addFloatingText()
- label.set3DPos(x, y + 1, z)
- updatePlayerPos()
- local dist = getDistance(playerPos.x, playerPos.y, playerPos.z, x, y, z)
- label.setText(string.format("%s\n%.1fm", name, dist))
- label.setColor((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF)
- label.setScale(0.8)
- table.insert(widgets.waypoints, marker)
- table.insert(widgets.waypoints, label)
- end
- local function refreshWaypoints()
- clearWaypoints()
- for _, wp in ipairs(savedWaypoints) do
- addWaypoint(wp.name, wp.x, wp.y, wp.z, wp.color)
- end
- end
- -- UI Drawing
- local function drawMenu()
- term.clear()
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- print("╔═════════════════════════════════════════════╗")
- print("║ OpenGlasses 2 AR Control System v2.0 ║")
- print("╚═════════════════════════════════════════════╝")
- print()
- updatePlayerPos()
- print(string.format("Position: %d, %d, %d | Facing: %s",
- playerPos.x, playerPos.y, playerPos.z, getCardinalDirection(playerFacing)))
- print()
- print("╔═══ FEATURES ═══════════════════════════════╗")
- print(string.format("║ [1] Radar: %-7s Range: %2dm ║",
- config.radarEnabled and "ON" or "OFF", config.radarRange))
- print(string.format("║ [2] Fullbright: %-7s Opacity: %3d%% ║",
- config.fullbrightEnabled and "ON" or "OFF", math.floor(config.fullbrightOpacity * 100)))
- if geo then
- print(string.format("║ [3] Geolyser: %-7s R:%2d D:%2d ║",
- config.geolyserEnabled and "ON" or "OFF", config.geolyserRadius, config.geolyserDepth))
- else
- print("║ [3] Geolyser: N/A (no geolyzer) ║")
- end
- print(string.format("║ [4] HUD: %-7s ║",
- config.hudEnabled and "ON" or "OFF"))
- print("╚════════════════════════════════════════════╝")
- print()
- print("╔═══ WAYPOINTS ══════════════════════════════╗")
- print("║ [W] Add Waypoint at Current Position ║")
- print("║ [L] List Waypoints ║")
- print("║ [D] Delete Waypoint ║")
- print(string.format("║ Total Waypoints: %-3d ║", #savedWaypoints))
- print("╚════════════════════════════════════════════╝")
- print()
- print("╔═══ SETTINGS ═══════════════════════════════╗")
- print("║ [R] Adjust Radar Range ║")
- print("║ [F] Adjust Fullbright Opacity ║")
- if geo then
- print("║ [G] Adjust Geolyser Settings ║")
- end
- print("║ [H] Toggle HUD Elements ║")
- print("╚════════════════════════════════════════════╝")
- print()
- print("╔═══ SYSTEM ═════════════════════════════════╗")
- print("║ [C] Clear All Widgets ║")
- print("║ [S] Save Configuration ║")
- print("║ [Q] Quit ║")
- print("╚════════════════════════════════════════════╝")
- print()
- print("Press a key to select option...")
- end
- local function listWaypoints()
- term.clear()
- print("╔═══ WAYPOINTS ══════════════════════════════╗")
- if #savedWaypoints == 0 then
- print("║ No waypoints saved ║")
- else
- for i, wp in ipairs(savedWaypoints) do
- print(string.format("║ %2d. %-20s (%d,%d,%d) ║",
- i, wp.name:sub(1,20), wp.x, wp.y, wp.z))
- end
- end
- print("╚════════════════════════════════════════════╝")
- print("\nPress any key to continue...")
- event.pull("key_down")
- end
- -- Event Handlers
- local function handleInput(char, code)
- if char == 0 then return end
- local key = string.char(char):lower()
- if key == "1" then
- config.radarEnabled = not config.radarEnabled
- if config.radarEnabled then
- updateRadar()
- else
- clearRadar()
- end
- updateHUD()
- elseif key == "2" then
- if config.fullbrightEnabled then
- disableFullbright()
- else
- enableFullbright()
- end
- updateHUD()
- elseif key == "3" and geo then
- config.geolyserEnabled = not config.geolyserEnabled
- if config.geolyserEnabled then
- updateGeolyser()
- else
- clearGeolyser()
- end
- updateHUD()
- elseif key == "4" then
- config.hudEnabled = not config.hudEnabled
- if config.hudEnabled then
- updateHUD()
- else
- clearHUD()
- end
- elseif key == "w" then
- term.clear()
- print("Add Waypoint at Current Position")
- updatePlayerPos()
- print(string.format("Position: %d, %d, %d", playerPos.x, playerPos.y, playerPos.z))
- print("\nEnter waypoint name:")
- local name = io.read()
- if name and name ~= "" then
- table.insert(savedWaypoints, {
- name = name,
- x = playerPos.x,
- y = playerPos.y,
- z = playerPos.z,
- color = config.colors.waypoint
- })
- saveWaypoints()
- refreshWaypoints()
- print("\nWaypoint added!")
- os.sleep(1)
- end
- elseif key == "l" then
- listWaypoints()
- elseif key == "d" then
- if #savedWaypoints == 0 then
- term.clear()
- print("No waypoints to delete!")
- os.sleep(1)
- return
- end
- listWaypoints()
- print("\nEnter waypoint number to delete (or 0 to cancel):")
- local num = tonumber(io.read())
- if num and num > 0 and num <= #savedWaypoints then
- table.remove(savedWaypoints, num)
- saveWaypoints()
- refreshWaypoints()
- print("Waypoint deleted!")
- os.sleep(1)
- end
- elseif key == "r" then
- term.clear()
- print(string.format("Current radar range: %dm", config.radarRange))
- print("Enter new range (1-64):")
- local range = tonumber(io.read())
- if range and range >= 1 and range <= 64 then
- config.radarRange = range
- if config.radarEnabled then
- updateRadar()
- end
- end
- elseif key == "f" then
- term.clear()
- print(string.format("Current fullbright opacity: %d%%",
- math.floor(config.fullbrightOpacity * 100)))
- print("Enter new opacity (0-100):")
- local opacity = tonumber(io.read())
- if opacity and opacity >= 0 and opacity <= 100 then
- config.fullbrightOpacity = opacity / 100
- if config.fullbrightEnabled then
- disableFullbright()
- enableFullbright()
- end
- end
- elseif key == "g" and geo then
- term.clear()
- print(string.format("Current radius: %dm", config.geolyserRadius))
- print("Enter new radius (1-16):")
- local radius = tonumber(io.read())
- if radius and radius >= 1 and radius <= 16 then
- config.geolyserRadius = radius
- end
- print(string.format("Current depth: %dm", config.geolyserDepth))
- print("Enter new depth (1-64):")
- local depth = tonumber(io.read())
- if depth and depth >= 1 and depth <= 64 then
- config.geolyserDepth = depth
- end
- if config.geolyserEnabled then
- updateGeolyser()
- end
- elseif key == "h" then
- term.clear()
- print("HUD Element Toggles:")
- print(string.format("[1] Coordinates: %s", config.showCoordinates and "ON" or "OFF"))
- print(string.format("[2] Compass: %s", config.showCompass and "ON" or "OFF"))
- print("\nEnter option (1-2):")
- local opt = io.read()
- if opt == "1" then
- config.showCoordinates = not config.showCoordinates
- elseif opt == "2" then
- config.showCompass = not config.showCompass
- end
- updateHUD()
- elseif key == "c" then
- clearAllWidgets()
- config.radarEnabled = false
- config.fullbrightEnabled = false
- config.geolyserEnabled = false
- config.hudEnabled = false
- elseif key == "s" then
- saveConfig()
- term.clear()
- print("Configuration saved!")
- os.sleep(1)
- elseif key == "q" then
- running = false
- end
- end
- -- Main Loop
- local function mainLoop()
- local lastRadarUpdate = 0
- local lastGeoUpdate = 0
- local lastHUDUpdate = 0
- while running do
- drawMenu()
- local currentTime = computer.uptime()
- -- Update radar
- if config.radarEnabled and (currentTime - lastRadarUpdate) > config.radarUpdateInterval then
- updateRadar()
- lastRadarUpdate = currentTime
- end
- -- Update geolyser
- if config.geolyserEnabled and (currentTime - lastGeoUpdate) > 5 then
- updateGeolyser()
- lastGeoUpdate = currentTime
- end
- -- Update HUD
- if config.hudEnabled and (currentTime - lastHUDUpdate) > 1 then
- updateHUD()
- lastHUDUpdate = currentTime
- end
- -- Input handling
- local eventType, _, char, code = event.pull(0.1, "key_down")
- if eventType == "key_down" then
- handleInput(char, code)
- end
- end
- end
- -- Initialization
- term.clear()
- print("╔═══════════════════════════════════════════╗")
- print("║ OpenGlasses 2 AR Control System v2.0 ║")
- print("║ Initializing... ║")
- print("╚═══════════════════════════════════════════╝")
- print()
- -- Check components
- print("Checking components...")
- if not component.isAvailable("glasses") then
- print("✗ ERROR: OpenGlasses Terminal not found!")
- return
- else
- print("✓ OpenGlasses Terminal found")
- end
- if not component.isAvailable("navigation") then
- print("✗ ERROR: Navigation upgrade not found!")
- return
- else
- print("✓ Navigation upgrade found")
- end
- if not component.isAvailable("radar") then
- print("✗ ERROR: Radar upgrade not found!")
- return
- else
- print("✓ Radar upgrade found")
- end
- if not component.isAvailable("geolyzer") then
- print("⚠ WARNING: Geolyzer not found (feature disabled)")
- else
- print("✓ Geolyzer found")
- end
- print()
- print("Loading configuration...")
- loadConfig()
- loadWaypoints()
- print("✓ Configuration loaded")
- print()
- print("Refreshing waypoints...")
- refreshWaypoints()
- if config.hudEnabled then
- updateHUD()
- end
- print()
- print("✓ All systems ready!")
- os.sleep(2)
- -- Run
- clearAllWidgets()
- mainLoop()
- -- Cleanup
- clearAllWidgets()
- saveConfig()
- saveWaypoints()
- term.clear()
- print("AR Control System shutdown complete.")
- print("Configuration and waypoints saved.")
Advertisement
Add Comment
Please, Sign In to add comment