ElijahCrafter

Holo Test

Dec 4th, 2025 (edited)
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.99 KB | Source Code | 0 0
  1. -- Camera to Hologram Sender with Recording
  2. -- Uses available APIs: read(), sleep(), parallel, fs
  3.  
  4. -- Configuration
  5. local cameraName = "camera_0"
  6. local hologramName = "hologram_0"
  7. local recordingFile = "recording.dat"
  8.  
  9. -- State
  10. local camera = nil
  11. local hologram = nil
  12. local isRunning = true
  13. local isRecording = false
  14. local isPlaying = false
  15. local recordedFrames = {}
  16. local frameIndex = 1
  17. local frameInterval = 0.1  -- 10 FPS
  18.  
  19. -- Colors
  20. local colors = colors or {
  21.     white = 1,
  22.     orange = 2,
  23.     magenta = 4,
  24.     lightBlue = 8,
  25.     yellow = 16,
  26.     lime = 32,
  27.     pink = 64,
  28.     gray = 128,
  29.     lightGray = 256,
  30.     cyan = 512,
  31.     purple = 1024,
  32.     blue = 2048,
  33.     brown = 4096,
  34.     green = 8192,
  35.     red = 16384,
  36.     black = 32768
  37. }
  38.  
  39. -- Find peripherals
  40. local function findPeripherals()
  41.     local names = peripheral.getNames()
  42.     for i, name in ipairs(names) do
  43.         local pType = peripheral.getType(name)
  44.         if pType == "camera" and not camera then
  45.             cameraName = name
  46.             camera = peripheral.wrap(name)
  47.             print("Found camera: " .. name)
  48.         elseif pType == "hologram" and not hologram then
  49.             hologramName = name
  50.             hologram = peripheral.wrap(name)
  51.             print("Found hologram: " .. name)
  52.             hologram.on()
  53.         end
  54.     end
  55.     return camera ~= nil and hologram ~= nil
  56. end
  57.  
  58. -- Save recording to file
  59. local function saveRecording()
  60.     if #recordedFrames == 0 then
  61.         print("No frames to save!")
  62.         return false
  63.     end
  64.    
  65.     local file = fs.open(recordingFile, "w")
  66.     if not file then
  67.         print("Failed to open file for writing")
  68.         return false
  69.     end
  70.    
  71.     -- Write frame count
  72.     file.writeLine(tostring(#recordedFrames))
  73.    
  74.     -- Write each frame
  75.     for i, frame in ipairs(recordedFrames) do
  76.         file.writeLine(textutils.serialize(frame))
  77.     end
  78.    
  79.     file.close()
  80.     print("Saved " .. #recordedFrames .. " frames to " .. recordingFile)
  81.     return true
  82. end
  83.  
  84. -- Load recording from file
  85. local function loadRecording()
  86.     if not fs.exists(recordingFile) then
  87.         print("Recording file not found: " .. recordingFile)
  88.         return false
  89.     end
  90.    
  91.     local file = fs.open(recordingFile, "r")
  92.     if not file then
  93.         print("Failed to open file for reading")
  94.         return false
  95.     end
  96.    
  97.     local countStr = file.readLine()
  98.     local count = tonumber(countStr) or 0
  99.    
  100.     recordedFrames = {}
  101.     for i = 1, count do
  102.         local line = file.readLine()
  103.         if line then
  104.             local frame = textutils.unserialize(line)
  105.             if frame then
  106.                 table.insert(recordedFrames, frame)
  107.             end
  108.         end
  109.     end
  110.    
  111.     file.close()
  112.     print("Loaded " .. #recordedFrames .. " frames from " .. recordingFile)
  113.     return #recordedFrames > 0
  114. end
  115.  
  116. -- Capture a single frame from camera (entities + center)
  117. local function captureFrame()
  118.     if not camera then return nil end
  119.    
  120.     local success, entities = pcall(function()
  121.         return camera.get3DEntityData()
  122.     end)
  123.    
  124.     local centerSuccess, center = pcall(function()
  125.         return camera.getNetworkCenter()
  126.     end)
  127.    
  128.     if success and entities and centerSuccess and center then
  129.         return { entities = entities, center = center }
  130.     end
  131.     return nil
  132. end
  133.  
  134. -- Send frame to hologram
  135. local function sendFrame(frameData)
  136.     if not hologram or not frameData then return end
  137.    
  138.     -- Send entity data with center position
  139.     pcall(function()
  140.         if frameData.entities and frameData.center then
  141.             hologram.setEntityData(frameData.entities, frameData.center)
  142.         end
  143.     end)
  144. end
  145.  
  146. -- Draw the main menu
  147. local function drawMenu()
  148.     term.clear()
  149.     term.setCursorPos(1, 1)
  150.     term.setTextColor(colors.yellow)
  151.     print("=== Camera Hologram Sender ===")
  152.     term.setTextColor(colors.white)
  153.     print("")
  154.    
  155.     if camera then
  156.         term.setTextColor(colors.lime)
  157.         print("Camera: " .. cameraName .. " [OK]")
  158.     else
  159.         term.setTextColor(colors.red)
  160.         print("Camera: NOT FOUND")
  161.     end
  162.    
  163.     if hologram then
  164.         term.setTextColor(colors.lime)
  165.         print("Hologram: " .. hologramName .. " [OK]")
  166.     else
  167.         term.setTextColor(colors.red)
  168.         print("Hologram: NOT FOUND")
  169.     end
  170.    
  171.     term.setTextColor(colors.white)
  172.     print("")
  173.     print("Recorded frames: " .. #recordedFrames)
  174.     print("")
  175.    
  176.     term.setTextColor(colors.cyan)
  177.     print("Commands:")
  178.     term.setTextColor(colors.white)
  179.     print("  1 = Live stream")
  180.     print("  2 = Start recording")
  181.     print("  3 = Stop recording")
  182.     print("  4 = Play recording")
  183.     print("  5 = Stop playback")
  184.     print("  6 = Save to file")
  185.     print("  7 = Load from file")
  186.     print("  8 = Clear recording")
  187.     print("  q = Quit")
  188.     print("")
  189.    
  190.     if isRecording then
  191.         term.setTextColor(colors.red)
  192.         print("[RECORDING] Frames: " .. #recordedFrames)
  193.     elseif isPlaying then
  194.         term.setTextColor(colors.lime)
  195.         print("[PLAYING] Frame: " .. frameIndex .. "/" .. #recordedFrames)
  196.     end
  197.    
  198.     term.setTextColor(colors.white)
  199.     print("")
  200.     print("Enter command: ")
  201. end
  202.  
  203. -- Live stream mode
  204. local function liveStream()
  205.     term.clear()
  206.     term.setCursorPos(1, 1)
  207.     term.setTextColor(colors.yellow)
  208.     print("=== LIVE STREAM MODE ===")
  209.     term.setTextColor(colors.white)
  210.     print("")
  211.     print("Enter duration in seconds (0 = 10 sec):")
  212.     local input = read()
  213.     local duration = tonumber(input) or 10
  214.     if duration <= 0 then duration = 10 end
  215.    
  216.     print("")
  217.     print("Streaming for " .. duration .. " seconds...")
  218.    
  219.     local frameCount = 0
  220.     local endTime = os.clock() + duration
  221.    
  222.     while os.clock() < endTime do
  223.         local frame = captureFrame()
  224.         if frame then
  225.             sendFrame(frame)
  226.             frameCount = frameCount + 1
  227.             term.setCursorPos(1, 6)
  228.             local remaining = math.floor(endTime - os.clock())
  229.             print("Frames: " .. frameCount .. " | Time left: " .. remaining .. "s   ")
  230.         end
  231.         sleep(frameInterval)
  232.     end
  233.    
  234.     print("")
  235.     print("Stream complete. Total frames: " .. frameCount)
  236.     sleep(1)
  237. end
  238.  
  239. -- Recording mode
  240. local function recordMode()
  241.     term.clear()
  242.     term.setCursorPos(1, 1)
  243.     term.setTextColor(colors.red)
  244.     print("=== RECORDING MODE ===")
  245.     term.setTextColor(colors.white)
  246.     print("")
  247.     print("Enter recording duration in seconds:")
  248.     local input = read()
  249.     local duration = tonumber(input) or 10
  250.     if duration <= 0 then duration = 10 end
  251.    
  252.     print("")
  253.     print("Recording for " .. duration .. " seconds...")
  254.    
  255.     isRecording = true
  256.     recordedFrames = {}
  257.     local endTime = os.clock() + duration
  258.    
  259.     while os.clock() < endTime do
  260.         local frame = captureFrame()
  261.         if frame then
  262.             table.insert(recordedFrames, frame)
  263.             -- Also send to hologram for preview
  264.             sendFrame(frame)
  265.             term.setCursorPos(1, 6)
  266.             local remaining = math.floor(endTime - os.clock())
  267.             print("Frames: " .. #recordedFrames .. " | Time left: " .. remaining .. "s   ")
  268.         end
  269.         sleep(frameInterval)
  270.     end
  271.    
  272.     isRecording = false
  273.     print("")
  274.     print("Recording complete! Total frames: " .. #recordedFrames)
  275.     sleep(1)
  276. end
  277.  
  278. -- Playback mode
  279. local function playbackMode()
  280.     if #recordedFrames == 0 then
  281.         print("No recording to play!")
  282.         sleep(1)
  283.         return
  284.     end
  285.    
  286.     term.clear()
  287.     term.setCursorPos(1, 1)
  288.     term.setTextColor(colors.lime)
  289.     print("=== PLAYBACK MODE ===")
  290.     term.setTextColor(colors.white)
  291.     print("")
  292.     print("Loop playback? (y/n):")
  293.     local loopInput = read()
  294.     local loopMode = (loopInput == "y" or loopInput == "Y")
  295.    
  296.     print("")
  297.     if loopMode then
  298.         print("Enter number of loops (0 = infinite):")
  299.         local loopCountInput = read()
  300.         local maxLoops = tonumber(loopCountInput) or 1
  301.        
  302.         print("")
  303.         print("Playing " .. #recordedFrames .. " frames...")
  304.         print("(Press Ctrl+T to terminate)")
  305.        
  306.         isPlaying = true
  307.         frameIndex = 1
  308.         local loopCount = 0
  309.        
  310.         while isPlaying do
  311.             if frameIndex <= #recordedFrames then
  312.                 sendFrame(recordedFrames[frameIndex])
  313.                 term.setCursorPos(1, 7)
  314.                 print("Frame: " .. frameIndex .. "/" .. #recordedFrames .. " | Loop: " .. (loopCount + 1) .. "   ")
  315.                 frameIndex = frameIndex + 1
  316.             else
  317.                 loopCount = loopCount + 1
  318.                 if maxLoops > 0 and loopCount >= maxLoops then
  319.                     break
  320.                 end
  321.                 frameIndex = 1
  322.             end
  323.             sleep(frameInterval)
  324.         end
  325.     else
  326.         print("Playing " .. #recordedFrames .. " frames...")
  327.        
  328.         isPlaying = true
  329.         for i, frame in ipairs(recordedFrames) do
  330.             sendFrame(frame)
  331.             term.setCursorPos(1, 6)
  332.             print("Frame: " .. i .. "/" .. #recordedFrames .. "   ")
  333.             sleep(frameInterval)
  334.         end
  335.     end
  336.    
  337.     isPlaying = false
  338.     print("")
  339.     print("Playback complete!")
  340.     sleep(1)
  341. end
  342.  
  343. -- Main program loop
  344. local function main()
  345.     term.clear()
  346.     term.setCursorPos(1, 1)
  347.     print("Initializing Camera Hologram Sender...")
  348.     print("")
  349.    
  350.     -- Find peripherals
  351.     if not findPeripherals() then
  352.         term.setTextColor(colors.red)
  353.         if not camera then
  354.             print("ERROR: No camera found!")
  355.         end
  356.         if not hologram then
  357.             print("ERROR: No hologram found!")
  358.         end
  359.         term.setTextColor(colors.white)
  360.         print("")
  361.         print("Connect peripherals and restart.")
  362.         return
  363.     end
  364.    
  365.     print("")
  366.     print("Press Enter to continue...")
  367.     read()
  368.    
  369.     -- Main loop
  370.     while isRunning do
  371.         drawMenu()
  372.        
  373.         local input = read()
  374.        
  375.         if input == "1" then
  376.             liveStream()
  377.         elseif input == "2" then
  378.             recordMode()
  379.         elseif input == "3" then
  380.             isRecording = false
  381.             print("Recording stopped.")
  382.             sleep(0.5)
  383.         elseif input == "4" then
  384.             playbackMode()
  385.         elseif input == "5" then
  386.             isPlaying = false
  387.             print("Playback stopped.")
  388.             sleep(0.5)
  389.         elseif input == "6" then
  390.             saveRecording()
  391.             sleep(1)
  392.         elseif input == "7" then
  393.             loadRecording()
  394.             sleep(1)
  395.         elseif input == "8" then
  396.             recordedFrames = {}
  397.             print("Recording cleared.")
  398.             sleep(0.5)
  399.         elseif input == "q" or input == "Q" then
  400.             isRunning = false
  401.             print("Goodbye!")
  402.         end
  403.     end
  404. end
  405.  
  406. -- Run the program
  407. main()
  408.  
Advertisement
Add Comment
Please, Sign In to add comment