Advertisement
samuelask

Kino Script optimized

Mar 9th, 2025 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.00 KB | None | 0 0
  1. -- Kino Drone Script for OpenComputers (Optimized for Memory)
  2. local component = component
  3. local computer = computer
  4.  
  5. local modem = component.list("modem")()
  6. local drone = component.list("drone")()
  7. local geolyzer = component.list("geolyzer")()
  8. local radar = component.list("radar")()
  9. local chunkloader = component.list("chunkloader")()
  10.  
  11. modem = modem and component.proxy(modem)
  12. drone = drone and component.proxy(drone)
  13. geolyzer = geolyzer and component.proxy(geolyzer)
  14. radar = radar and component.proxy(radar)
  15. chunkloader = chunkloader and component.proxy(chunkloader)
  16. confirmed_move = false
  17. got_address = false
  18.  
  19. local Version = "1.0.0"
  20. local base_address = ""
  21. local tablet_address = ""
  22. local scan_mode = "normal"
  23.  
  24. local SCAN_HEIGHT = 10
  25. local scan_step = 1
  26. local scan_x, scan_y, scan_z = 0, -3, 0 -- Start at Kino's position
  27. local step_size = 4 -- Scan size (4x4x4)
  28. local max_radius = 10 -- Maximum scan range
  29. local layer = 0 -- Current expansion layer
  30. local direction = 1 -- 1 = right, 2 = down, 3 = left, 4 = up
  31. local moves_per_layer = 1
  32. local move_count = 0
  33. local no_geo = false
  34. local batch_scan_data = {} -- Buffer to store multiple scan results
  35. local batch_threshold = 5 -- Number of scans before sending
  36. local low_energy = false
  37. local fine_motor_control = false
  38.  
  39. local kino_x, kino_y, kino_z = 0, 0, 0
  40.  
  41. local scanned_positions = {}
  42.  
  43. local function debug(msg)
  44. modem.send(base_address, 123, "debug", msg)
  45. end
  46.  
  47. modem.open(123)
  48.  
  49. local function sleep(time)
  50. local start = computer.uptime()
  51. while computer.uptime() - start < time do
  52. computer.pullSignal(0) -- ✅ Allows OpenComputers to process events
  53. end
  54. end
  55. local function can_move(direction)
  56. local blocked, blockType = drone.detect(direction)
  57.  
  58. if not blocked then
  59. return true -- ✅ Path is clear, movement allowed
  60. elseif blockType == "passable" or blockType == "air" or blockType == "replaceable" then
  61. return true -- ✅ Movement allowed through these block types
  62. else
  63. return false -- ❌ Movement blocked
  64. end
  65. end
  66. -- **Optimized Movement Function with Position Tracking**
  67. local function move(dx, dy, dz)
  68. drone.move(dx, dy, dz)
  69. sleep(0.5) -- ✅ **Wait for movement to complete**
  70. kino_x = kino_x + dx
  71. kino_y = kino_y + dy
  72. kino_z = kino_z + dz
  73. end
  74. -- Function to send all accumulated data
  75. function send_scan_data()
  76. if #batch_scan_data == 0 then return end
  77.  
  78. -- ✅ Check energy level
  79. local energy_level = computer.energy() / computer.maxEnergy()
  80. if energy_level < 0.07 then
  81. low_energy = true -- ⚡ Mark for shutdown
  82. end
  83.  
  84. for i = 1, #batch_scan_data, 20 do
  85. modem.send(base_address, 123, "scan_data", table.concat(batch_scan_data, ",", i, math.min(i + 19, #batch_scan_data)))
  86. sleep(0.1)
  87. end
  88. batch_scan_data = {} -- Clear buffer after sending
  89. end
  90. local function perform_scan(x, y, z)
  91.  
  92. -- Avoid scanning the same position twice
  93. local scan_key = x .. "," .. y .. "," .. z
  94. if scanned_positions[scan_key] then
  95. return
  96. end
  97. scanned_positions[scan_key] = true
  98.  
  99. local success, result = pcall(geolyzer.scan, x, z, y, 4, 4, 4)
  100.  
  101. if success and type(result) == "table" then
  102. for i, density in ipairs(result) do
  103. local sx = ((i - 1) % 4) + x
  104. local sz = (((i - 1) // 4) % 4) + z
  105. local sy = (((i - 1) // 16) % 4) + y
  106. if density > 0 then
  107. table.insert(batch_scan_data, sx)
  108. table.insert(batch_scan_data, sy)
  109. table.insert(batch_scan_data, sz)
  110. table.insert(batch_scan_data, density)
  111. end
  112. end
  113. else
  114. debug("❌ Scan failed at X=" .. x .. " Y=" .. y .. " Z=" .. z)
  115. end
  116.  
  117. -- Send data if batch threshold is reached
  118. if #batch_scan_data >= batch_threshold * 20 then
  119. send_scan_data()
  120. end
  121. end
  122. local function calculate_scan_coords()
  123. if low_energy then
  124. debug("[⚠️] Low energy detected! Sending last scan data and shutting down.")
  125. send_scan_data() -- Ensure all data is sent before shutdown
  126. modem.send(tablet_address, 123, "shutdownenergy")
  127. computer.shutdown()
  128. end
  129.  
  130. -- Scan the Kino’s position first
  131. perform_scan(scan_x, scan_y, scan_z)
  132.  
  133. -- Adjust X scanning (Alternate Positive/Negative)
  134. if scan_x == 0 and scan_z == 0 then
  135. scan_x = -step_size -- Start negative first
  136. elseif scan_x < 0 then
  137. scan_x = -scan_x -- Flip to positive side
  138. else
  139. scan_x = -(scan_x + step_size) -- Move further negative
  140. end
  141.  
  142. -- Ensure Z alternates properly
  143. if math.abs(scan_x) > max_radius then
  144. scan_x = 0
  145. if scan_z >= 0 then
  146. scan_z = -scan_z - step_size -- Move to negative Z if not already
  147. else
  148. scan_z = -scan_z -- Flip to positive
  149. end
  150. end
  151.  
  152. -- Move upwards if we've fully scanned the X-Z plane
  153. if math.abs(scan_z) > max_radius then
  154. scan_x, scan_z = 0, 0
  155. scan_y = scan_y + step_size
  156. end
  157.  
  158. -- Stop if max height is reached
  159. if scan_y > SCAN_HEIGHT then
  160. debug("[✔] Full area scan completed.")
  161. return false
  162. end
  163.  
  164. return true
  165. end
  166. local function scan_entire_area()
  167. debug("[🔄] Starting full area scan...")
  168. drone.setStatusText("Scanning")
  169. drone.setLightColor(008000)
  170.  
  171. while calculate_scan_coords() do
  172. sleep(0.2) -- Small delay to prevent CPU overload
  173. end
  174.  
  175. send_scan_data()
  176. debug("[✔] Full area scan completed.")
  177. scan_x, scan_y, scan_z = 0, -4, 0 -- Start at Kino's position
  178. step_size = 4 -- Scan size (4x4x4)
  179. scanned_positions = {}
  180. modem.send(tablet_address, 123, "finish")
  181. drone.setStatusText("Ready")
  182. drone.setLightColor(32768)
  183. end
  184. -- Confirm script upload to sender
  185. modem.broadcast(123, "kino_update_success")
  186. drone.setStatusText("Waiting")
  187. while not got_address do
  188. local _, _, sender, _, _, cmd, value, value2 = computer.pullSignal(5)
  189. if cmd == "adresses" and value then
  190. tablet_address = value
  191. base_address = value2
  192. got_address = true
  193. debug("[✔] Tablet address updated: " .. tablet_address)
  194. debug("[✔] Base address updated: " .. base_address)
  195. end
  196. end
  197.  
  198. if not modem or not drone then
  199. debug("[❌] Missing modem or drone component!")
  200. end
  201. if not geolyzer then
  202. debug("[❌] No Geolyzer found, stopping.")
  203. no_geo = true
  204. end
  205. if not chunkloader then
  206. debug("[❌] No Chunkloader found, Kino wont scan correctly.")
  207. elseif chunkloader then
  208. chunkloader.setActive(true)
  209. end
  210.  
  211. drone.setStatusText("Ready")
  212. drone.setLightColor(32768)
  213. -- **Main Loop: Handles movement + scan mode**
  214. while got_address and not no_geo do
  215. local _, _, sender, _, _, cmd, value = computer.pullSignal(5)
  216.  
  217. if sender == tablet_address then
  218. if cmd == "move" then
  219. if value == "forward" and not confirmed_move then
  220. move(scan_step, 0, 0)
  221. elseif value == "forward" and confirmed_move then
  222. drone.move(scan_step, 0, 0)
  223. sleep(0.2)
  224. computer.shutdown()
  225. elseif value == "backward" and not confirmed_move then
  226. move(-scan_step, 0, 0)
  227. elseif value == "backward" and confirmed_move then
  228. drone.move(-scan_step, 0, 0)
  229. sleep(0.2)
  230. computer.shutdown()
  231. elseif value == "left" and not confirmed_move then
  232. move(0, 0, -scan_step)
  233. elseif value == "left" and confirmed_move then
  234. drone.move(0, 0, -scan_step)
  235. sleep(0.2)
  236. computer.shutdown()
  237. elseif value == "right" and not confirmed_move then
  238. move(0, 0, scan_step)
  239. elseif value == "right" and confirmed_move then
  240. drone.move(0, 0, scan_step)
  241. sleep(0.2)
  242. computer.shutdown()
  243. elseif value == "up" and not confirmed_move then
  244. move(0, scan_step, 0)
  245. elseif value == "down" and not confirmed_move then
  246. move(0, -scan_step, 0)
  247. end
  248. elseif cmd == "set_scan_mode" then scan_mode = value
  249. elseif cmd == "scanconfirm" then
  250. modem.send(base_address, 123, "newscan")
  251. modem.send(tablet_address, 123, "newscan")
  252. elseif cmd == "scan" then
  253. if scan_mode == "normal" and not confirmed_move then
  254. modem.send(tablet_address, 123, "moveme")
  255. elseif scan_mode == "normal" and confirmed_move then
  256. -- why are we here, just to suffer
  257. elseif scan_mode == "manual" then
  258. scan_entire_area()
  259. end
  260. elseif cmd == "fine_control" then
  261. if value == "on" then
  262. fine_motor_control = true
  263. scan_step = 4
  264. debug("[🔧] Fine Motor Control ENABLED")
  265. elseif value == "off" then
  266. fine_motor_control = false
  267. scan_step = 1
  268. debug("[🔧] Fine Motor Control DISABLED")
  269. end
  270. elseif cmd == "confirmed_move" then
  271. confirmed_move = true
  272. elseif cmd == "autoscan" then
  273. modem.send(tablet_address, 123, "autoscanning")
  274. scan_entire_area()
  275. elseif cmd == "shutdown" then
  276. debug("Shutting Down")
  277. modem.send(tablet_address, 123, "shutok")
  278. sleep(1)
  279. computer.shutdown()
  280. end
  281. end
  282. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement