Advertisement
samuelask

Kino Script

Mar 8th, 2025 (edited)
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.51 KB | None | 0 0
  1. -- Kino Drone Script for OpenComputers (Drone-Compatible)
  2. local component = component or {} -- Fallback for EEPROM environment
  3. local computer = computer or {}
  4.  
  5. -- Find Components (Manually, since require() doesn't work)
  6. local modemAddress = component.list and component.list("modem")()
  7. local droneAddress = component.list and component.list("drone")()
  8. local geolyzerAddress = component.list and component.list("geolyzer")()
  9. local radarAddress = component.list and component.list("radar")()
  10.  
  11. -- Check if the required components exist before using them
  12. local modem = modemAddress and component.proxy(modemAddress) or nil
  13. local drone = droneAddress and component.proxy(droneAddress) or nil
  14. local geolyzer = geolyzerAddress and component.proxy(geolyzerAddress) or nil
  15. local radar = radarAddress and component.proxy(radarAddress) or nil
  16.  
  17. -- Ensure we have a modem and drone; otherwise, exit
  18. if not modem then
  19.     error("No modem found! The Kino drone requires a wireless modem to function.")
  20. end
  21. if not drone then
  22.     error("No drone component found! This script must run on an OpenComputers drone.")
  23. end
  24.  
  25. -- Configuration
  26. local scan_interval = 5 -- Seconds between scans
  27. local scan_range = 8 -- Radius for scanning (must be <= 8)
  28. local base_address = "a676e3d0-282e-401b-8848-bd6ee3b3fa98" -- Address of the base station
  29. local tablet_address = "3998d9fd-c974-4254-8237-bcbb434d7e64" -- Address of the tablet
  30. local detected_players = {}
  31. local scan_mode = "normal" -- Default scan mode
  32.  
  33. -- Function to send debug messages to base station
  34. local function debug(message)
  35.     modem.send(base_address, 123, "debug", message)
  36. end
  37.  
  38. -- Function to move the Kino
  39. drone.setStatusText("Ready")
  40. modem.open(123) -- Open communication port
  41.  
  42. local function move(direction)
  43.     local moveAmount = 0.5 -- Drones move in small increments
  44.  
  45.     if direction == "forward" then drone.move(moveAmount, 0, 0)
  46.     elseif direction == "backward" then drone.move(-moveAmount, 0, 0)
  47.     elseif direction == "left" then drone.move(0, 0, -moveAmount)
  48.     elseif direction == "right" then drone.move(0, 0, moveAmount)
  49.     elseif direction == "up" then drone.move(0, moveAmount, 0)
  50.     elseif direction == "down" then drone.move(0, -moveAmount, 0)
  51.     else debug("Invalid move command: " .. tostring(direction))
  52.     end
  53. end
  54. -- Function to serialize a table manually (since drones can't use require)
  55. local function serialize_table(tbl)
  56.     local str = "{"
  57.     for k, v in pairs(tbl) do
  58.         if type(v) == "table" then
  59.             str = str .. k .. "=" .. serialize_table(v) .. ", "
  60.         elseif type(v) == "string" then
  61.             str = str .. k .. "='" .. v .. "', "
  62.         else
  63.             str = str .. k .. "=" .. tostring(v) .. ", "
  64.         end
  65.     end
  66.     return str .. "}"
  67.     debug("Serialized data...")
  68. end
  69. -- Function to scan environment
  70. local function scan_environment()
  71.     local scan_data = {mode = scan_mode, blocks = {}, entities = {}}
  72.  
  73.     -- Scan nearby blocks (if geolyzer is available)
  74.     if geolyzer then
  75.         debug("Scanning terrain...")
  76.  
  77.         -- Corrected geolyzer scan usage (added `{}` as the fifth argument)
  78.         local success, result = pcall(function() return geolyzer.scan(-4, -4, 0, 4, 4, 4)  -- Corrected Y offset
  79.         end)
  80.  
  81.         if success and type(result) == "table" then
  82.             for i, density in ipairs(result) do
  83.                 -- Convert 1D table index to 3D coordinates
  84.                 local x = (i - 1) % 4 - 4
  85.                 local z = ((i - 1) // 4) % 4 - 4
  86.                 local y = ((i - 1) // 16) % 4  -- Height coordinate
  87.                    
  88.                 table.insert(scan_data.blocks, {x = x, y = y, z = z, value = density})
  89.             end
  90.             debug("Geolyzer scan successful!")
  91.         else
  92.             debug("Geolyzer scan failed! Error: " .. tostring(result))
  93.         end
  94.     else
  95.         debug("Warning: No Geolyzer found, skipping block scanning.")
  96.     end
  97.  
  98.     -- Scan for entities (players) (if radar is available)
  99.     if radar then
  100.         debug("Scanning for entities...")
  101.         local entities = radar.getEntities(scan_range)
  102.         for _, entity in ipairs(entities) do
  103.             if entity.type == "player" and not detected_players[entity.name] then
  104.                 detected_players[entity.name] = true
  105.                 modem.send(tablet_address, 123, "player_detected", entity.name)
  106.             end
  107.             table.insert(scan_data.entities, {name=entity.name, type=entity.type, x=entity.x, y=entity.y, z=entity.z})
  108.         end
  109.     else
  110.         debug("Warning: No Radar found, skipping entity scanning.")
  111.     end
  112.  
  113.  
  114.     -- Convert `scan_data` into a string manually (since require("serialization") doesn't work)
  115.     local scan_data_str = serialize_table(scan_data)
  116.    
  117.     -- Send scan data to base station
  118.     modem.send(base_address, 123, "scan_data", scan_data_str)
  119. end
  120.  
  121. -- Confirm script upload to sender
  122. modem.broadcast(123, "kino_update_success")
  123.  
  124. -- Main Loop: Handles both movement commands and scanning
  125. while true do
  126.     -- Scan environment at intervals
  127.     local scan_timer = computer.uptime() + scan_interval
  128.    
  129.     while computer.uptime() < scan_timer do
  130.         -- Listen for modem messages (since drones can't use event.listen)
  131.         local event, _, sender, _, _, command, value = computer.pullSignal(scan_interval)
  132.  
  133.         if event == "modem_message" and sender == tablet_address then
  134.             if command == "move" then
  135.                 move(value)
  136.             elseif command == "set_scan_mode" then
  137.                 scan_mode = value
  138.             end
  139.         end
  140.     end
  141.  
  142.     scan_environment()
  143. end
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement