ElijahCrafter

Holo modem test 2

Dec 3rd, 2025 (edited)
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.73 KB | Source Code | 0 0
  1. -- sender.lua
  2. -- Camera data transmitter with recording
  3.  
  4. local camera = peripheral.find("camera")
  5. local modem = peripheral.find("modem")
  6.  
  7. if not camera then print("No camera!") return end
  8. if not modem then print("No modem!") return end
  9.  
  10. -- Config
  11. local CHANNEL = 1
  12. local NETWORK = "security"
  13.  
  14. -- State
  15. local isRecording = false
  16. local isPlaying = false
  17. local frames = {}
  18. local recStart = 0
  19. local playStart = 0
  20. local playIndex = 1
  21. local playSpeed = 1.0
  22. local doLoop = false
  23. local tick = 0
  24.  
  25. -- Safe method caller
  26. local function call(method, ...)
  27.     if camera[method] then
  28.         return camera[method](...)
  29.     end
  30.     return nil
  31. end
  32.  
  33. -- Setup
  34. call("setNetwork", NETWORK)
  35. call("start")
  36. modem.open(100)
  37.  
  38. print("=== Camera Sender ===")
  39. print("Commands: rec, stop, play, loop, save, load, test, quit")
  40. write("> ")
  41.  
  42. -- Capture frame
  43. local function capture(ents, center)
  44.     if not isRecording then return end
  45.     if #frames >= 6000 then
  46.         isRecording = false
  47.         print("\n[REC] Max!")
  48.         write("> ")
  49.         return
  50.     end
  51.    
  52.     local f = {
  53.         t = tick - recStart,
  54.         e = {},
  55.         c = {
  56.             x = center and center.x or 0,
  57.             y = center and center.y or 0,
  58.             z = center and center.z or 0
  59.         }
  60.     }
  61.    
  62.     if ents then
  63.         for i = 1, #ents do
  64.             local e = ents[i]
  65.             if e then
  66.                 f.e[#f.e + 1] = {
  67.                     id = e.id or "",
  68.                     name = e.name or "",
  69.                     entityType = e.entityType or "",
  70.                     isPlayer = e.isPlayer or false,
  71.                     x = e.x or 0,
  72.                     y = e.y or 0,
  73.                     z = e.z or 0,
  74.                     yaw = e.yaw or 0,
  75.                     pitch = e.pitch or 0,
  76.                     bodyYaw = e.bodyYaw or 0
  77.                 }
  78.             end
  79.         end
  80.     end
  81.    
  82.     frames[#frames + 1] = f
  83. end
  84.  
  85. -- Get playback frame
  86. local function getFrame()
  87.     if not isPlaying or #frames == 0 then
  88.         return nil, nil
  89.     end
  90.    
  91.     local elapsed = (tick - playStart) * playSpeed
  92.    
  93.     while playIndex < #frames do
  94.         local nxt = frames[playIndex + 1]
  95.         if nxt and nxt.t and elapsed >= nxt.t then
  96.             playIndex = playIndex + 1
  97.         else
  98.             break
  99.         end
  100.     end
  101.    
  102.     local last = frames[#frames]
  103.     if last and last.t and elapsed >= last.t then
  104.         if doLoop then
  105.             playStart = tick
  106.             playIndex = 1
  107.         else
  108.             isPlaying = false
  109.             playIndex = 1
  110.             print("\n[PLAY] Done")
  111.             write("> ")
  112.             return nil, nil
  113.         end
  114.     end
  115.    
  116.     local f = frames[playIndex]
  117.     if f then
  118.         return f.e or {}, f.c or {x=0,y=0,z=0}
  119.     end
  120.     return {}, {x=0,y=0,z=0}
  121. end
  122.  
  123. -- Get live camera data
  124. local function getLiveData()
  125.     local ents = call("get3DEntityData") or {}
  126.     local center = call("getNetworkCenter") or {x=0,y=0,z=0}
  127.    
  128.     -- Filter to tables only
  129.     local filtered = {}
  130.     if type(ents) == "table" then
  131.         for i = 1, #ents do
  132.             if type(ents[i]) == "table" then
  133.                 filtered[#filtered + 1] = ents[i]
  134.             end
  135.         end
  136.     end
  137.    
  138.     return filtered, center
  139. end
  140.  
  141. -- Test camera
  142. local function testCamera()
  143.     print("Testing...")
  144.     local ents = call("get3DEntityData")
  145.     local center = call("getNetworkCenter")
  146.     print("Entities: " .. (ents and #ents or "nil"))
  147.     print("Center: " .. (center and "yes" or "nil"))
  148.     if center then
  149.         print("  x=" .. (center.x or "?"))
  150.         print("  y=" .. (center.y or "?"))
  151.         print("  z=" .. (center.z or "?"))
  152.     end
  153. end
  154.  
  155. -- Commands
  156. local function cmd(input)
  157.     local c = ((input or ""):match("%S+") or ""):lower()
  158.     local a = (input or ""):match("%S+%s+(%S+)")
  159.    
  160.     if c == "rec" then
  161.         if isPlaying then print("Stop play first!")
  162.         elseif isRecording then print("Already rec!")
  163.         else
  164.             frames = {}
  165.             recStart = tick
  166.             isRecording = true
  167.             print("[REC] Go...")
  168.         end
  169.        
  170.     elseif c == "stop" then
  171.         if isRecording then
  172.             isRecording = false
  173.             print("[REC] " .. #frames .. " frames")
  174.         elseif isPlaying then
  175.             isPlaying = false
  176.             playIndex = 1
  177.             print("[PLAY] Stopped")
  178.         else
  179.             print("Nothing")
  180.         end
  181.        
  182.     elseif c == "play" then
  183.         if isRecording then print("Stop rec first!")
  184.         elseif #frames == 0 then print("No recording!")
  185.         else
  186.             isPlaying = true
  187.             playIndex = 1
  188.             playStart = tick
  189.             print("[PLAY] " .. #frames .. " frames")
  190.         end
  191.        
  192.     elseif c == "loop" then
  193.         doLoop = not doLoop
  194.         print("Loop: " .. (doLoop and "ON" or "OFF"))
  195.        
  196.     elseif c == "speed" and a then
  197.         local s = tonumber(a)
  198.         if s then playSpeed = s print("Speed: " .. s) end
  199.        
  200.     elseif c == "save" then
  201.         local name = a or "rec.dat"
  202.         if #frames == 0 then print("Empty!") return false end
  203.         local file = fs.open(name, "w")
  204.         if file then
  205.             file.write(textutils.serialize(frames))
  206.             file.close()
  207.             print("Saved " .. #frames)
  208.         end
  209.        
  210.     elseif c == "load" then
  211.         local name = a or "rec.dat"
  212.         if not fs.exists(name) then print("Not found!") return false end
  213.         local file = fs.open(name, "r")
  214.         if file then
  215.             local d = textutils.unserialize(file.readAll())
  216.             file.close()
  217.             if d then frames = d print("Loaded " .. #frames) end
  218.         end
  219.        
  220.     elseif c == "status" then
  221.         print("Rec:" .. (isRecording and "Y" or "N") .. " Play:" .. (isPlaying and "Y" or "N") .. " Frames:" .. #frames)
  222.        
  223.     elseif c == "test" then
  224.         testCamera()
  225.        
  226.     elseif c == "quit" or c == "exit" then
  227.         return true
  228.     end
  229.    
  230.     return false
  231. end
  232.  
  233. -- Main loop
  234. local buf = ""
  235. local timer = os.startTimer(0.05)
  236. local statCnt = 0
  237.  
  238. while true do
  239.     local ev, p1 = os.pullEvent()
  240.    
  241.     if ev == "timer" and p1 == timer then
  242.         tick = tick + 1
  243.        
  244.         local ents, center
  245.        
  246.         if isPlaying then
  247.             ents, center = getFrame()
  248.         else
  249.             ents, center = getLiveData()
  250.             if isRecording then
  251.                 capture(ents, center)
  252.             end
  253.         end
  254.        
  255.         -- Ensure we have valid data
  256.         ents = ents or {}
  257.         center = center or {x=0,y=0,z=0}
  258.        
  259.         if #ents > 0 then
  260.             modem.transmit(CHANNEL, 100, {
  261.                 type = "entities",
  262.                 entities = ents,
  263.                 center = center,
  264.                 isPlayback = isPlaying
  265.             })
  266.         end
  267.        
  268.         statCnt = statCnt + 1
  269.         if statCnt >= 40 and isRecording then
  270.             statCnt = 0
  271.             print("\n[REC] " .. #frames)
  272.             write("> " .. buf)
  273.         end
  274.        
  275.         timer = os.startTimer(0.05)
  276.        
  277.     elseif ev == "char" then
  278.         buf = buf .. p1
  279.         write(p1)
  280.        
  281.     elseif ev == "key" then
  282.         if p1 == keys.enter then
  283.             print()
  284.             if cmd(buf) then break end
  285.             buf = ""
  286.             write("> ")
  287.         elseif p1 == keys.backspace and #buf > 0 then
  288.             buf = buf:sub(1, -2)
  289.             local x, y = term.getCursorPos()
  290.             term.setCursorPos(x-1, y)
  291.             write(" ")
  292.             term.setCursorPos(x-1, y)
  293.         end
  294.        
  295.     elseif ev == "terminate" then
  296.         break
  297.     end
  298. end
  299.  
  300. call("stop")
  301. print("Done.")
Advertisement
Add Comment
Please, Sign In to add comment