Advertisement
samuelask

Kino Script optimized

Mar 9th, 2025 (edited)
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.57 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.  
  10. modem = modem and component.proxy(modem)
  11. drone = drone and component.proxy(drone)
  12. geolyzer = geolyzer and component.proxy(geolyzer)
  13. radar = radar and component.proxy(radar)
  14.  
  15. local base_address = "a676e3d0-282e-401b-8848-bd6ee3b3fa98"
  16. local tablet_address = "3998d9fd-c974-4254-8237-bcbb434d7e64"
  17. local scan_mode = "normal"
  18.  
  19. -- Define the scan range (adjust these to match your actual scan parameters)
  20. local scan_x = -2 -- Start X offset relative to Kino
  21. local scan_y = -2-- Start Y offset relative to Kino
  22. local scan_z = -2 -- Start Z offset relative to Kino
  23.  
  24.  
  25. local scan_width = 4-- Width of the scan area
  26. local scan_height = 4-- Height of the scan area
  27. local scan_depth = 4-- Depth of the scan area
  28. local scan_radius = 16 -- ✅ **Total scanning area**
  29. local scan_step = scan_width -- ✅ **Step size for moving between scans**
  30.  
  31. local fine_motor_control = false
  32.  
  33. local kino_x, kino_y, kino_z = 0, 0, 0
  34.  
  35. local SCAN_RADIUS = 30 -- 30 blocks in all directions
  36. local SCAN_STEP = 4 -- How many blocks per step
  37. local MIN_Y = 1 -- Minimum Y level to prevent falling too low
  38.  
  39. local function debug(msg)
  40. modem.send(base_address, 123, "debug", msg)
  41. end
  42.  
  43. modem.open(123)
  44. drone.setStatusText("Ready")
  45.  
  46. if not modem or not drone then
  47. debug("Missing modem or drone component!")
  48. end
  49.  
  50. local function sleep(time)
  51. local start = computer.uptime()
  52. while computer.uptime() - start < time do
  53. computer.pullSignal(0) -- ✅ Allows OpenComputers to process events
  54. end
  55. end
  56. local function can_move(direction)
  57. local blocked, blockType = drone.detect(direction)
  58.  
  59. if not blocked then
  60. return true -- ✅ Path is clear, movement allowed
  61. elseif blockType == "passable" or blockType == "air" or blockType == "replaceable" then
  62. return true -- ✅ Movement allowed through these block types
  63. else
  64. return false -- ❌ Movement blocked
  65. end
  66. end
  67. -- **Optimized Movement Function with Position Tracking**
  68. local function move(dx, dy, dz)
  69. drone.move(dx, dy, dz)
  70. sleep(0.5) -- ✅ **Wait for movement to complete**
  71. kino_x = kino_x + dx
  72. kino_y = kino_y + dy
  73. kino_z = kino_z + dz
  74. debug("📍 Normal Move: X=" .. kino_x .. " Y=" .. kino_y .. " Z=" .. kino_z)
  75. end
  76. -- **Function to Move with Pathfinding & Skip Blocked Chunks**
  77. local function smart_move(dx, dy, dz)
  78. local direction = nil
  79.  
  80. -- **Determine Movement Direction**
  81. if dx > 0 then direction = 5 -- Right
  82. elseif dx < 0 then direction = 4 -- Left
  83. elseif dz > 0 then direction = 3 -- Forward
  84. elseif dz < 0 then direction = 2 -- Backward
  85. elseif dy > 0 then direction = 1 -- Up
  86. elseif dy < 0 then direction = 0 -- Down
  87. end
  88. debug("🚀 Attempting move: DX=" .. dx .. " DY=" .. dy .. " DZ=" .. dz)
  89. -- **Step 1: Check if Path is Clear**
  90. if direction and can_move(direction) then
  91. move(dx, dy, dz) -- ✅ Move normally if the path is clear
  92. debug("✅ Move successful!")
  93. return true
  94. else
  95. debug("❌ Move blocked! Cannot reach target.")
  96. return false
  97. end
  98.  
  99. -- **Step 2: Try Moving Up if Blocked**
  100. if can_move(1) then
  101. move(0, 1, 0)
  102. return smart_move(dx, dy, dz) -- Retry after moving up
  103. end
  104.  
  105. -- **Step 3: Try Moving Sideways if Blocked**
  106. if can_move(5) then
  107. move(1, 0, 0) -- Try Right
  108. return smart_move(dx, dy, dz)
  109. elseif can_move(4) then
  110. move(-1, 0, 0) -- Try Left
  111. return smart_move(dx, dy, dz)
  112. end
  113.  
  114. -- **Step 4: Try Moving Down (if not already low)**
  115. if kino_y > 1 and can_move(0) then
  116. move(0, -1, 0)
  117. return smart_move(dx, dy, dz)
  118. end
  119.  
  120. -- **Step 5: Completely Blocked - Return False**
  121. debug("⚠ Movement blocked! Unable to reach scan position.")
  122. return false -- **Returns false so `scan_full_area()` skips this chunk**
  123. end
  124. -- **Chunk-Based Scanning with Absolute Position Adjustments**
  125. local function scan_chunk(x_offset, z_offset)
  126. if not geolyzer then
  127. debug("No Geolyzer found, skipping scan.")
  128. return
  129. end
  130.  
  131. debug("🔍 Scanning chunk at offset X=" .. x_offset .. " Z=" .. z_offset)
  132.  
  133. local scan_data = {}
  134.  
  135. local success, result = pcall(geolyzer.scan, -2, -2, -2, scan_width, scan_height, scan_depth)
  136.  
  137. if success and type(result) == "table" then
  138. for i, density in ipairs(result) do
  139. local x = ((i - 1) % scan_width) + kino_x + x_offset
  140. local z = (((i - 1) // scan_width) % scan_depth) + kino_z + z_offset
  141. local y = (((i - 1) // (scan_width * scan_depth)) % scan_height) + kino_y - 2
  142.  
  143. if density > 0 then
  144. table.insert(scan_data, x)
  145. table.insert(scan_data, y)
  146. table.insert(scan_data, z)
  147. table.insert(scan_data, density)
  148. end
  149. end
  150. debug("✅ Chunk scan complete at X=" .. kino_x + x_offset .. " Z=" .. kino_z + z_offset)
  151. else
  152. debug("❌ Scan failed at X=" .. kino_x + x_offset .. " Z=" .. kino_z + z_offset)
  153. end
  154.  
  155. -- **Send scan data immediately**
  156. for i = 1, #scan_data, 20 do
  157. modem.send(base_address, 123, "scan_data", table.concat(scan_data, ",", i, math.min(i + 19, #scan_data)))
  158. sleep(0.1) -- ✅ **Prevent overloading the network**
  159. end
  160. end
  161.  
  162. -- **Full Area Scan: Kino Moves & Scans Each Chunk**
  163. local function scan_full_area()
  164. debug("🔄 Starting full area scan..(" .. SCAN_STEP .. ")")
  165.  
  166. -- **Verify SCAN_RADIUS is set correctly**
  167. if SCAN_RADIUS <= 0 then
  168. debug("❌ ERROR: SCAN_RADIUS is invalid (" .. SCAN_RADIUS .. ")! Aborting scan.")
  169. return
  170. end
  171.  
  172. -- **Loop through Y, X, and Z to cover the scan area**
  173. for y_offset = 0, -SCAN_RADIUS, -SCAN_STEP do
  174. if kino_y + y_offset < MIN_Y then break end -- Prevent going too low
  175.  
  176. for x_offset = -SCAN_RADIUS, SCAN_RADIUS, SCAN_STEP do
  177. for z_offset = -SCAN_RADIUS, SCAN_RADIUS, SCAN_STEP do
  178.  
  179. -- ✅ Debug: Confirm Loop Execution
  180. debug("📍 Loop Iteration: X=" .. x_offset .. " Y=" .. y_offset .. " Z=" .. z_offset)
  181.  
  182. -- ✅ Step 1: Verify Position Is Within Bounds
  183. local target_x = kino_x + x_offset
  184. local target_y = kino_y + y_offset
  185. local target_z = kino_z + z_offset
  186.  
  187. if math.abs(target_x) <= SCAN_RADIUS and math.abs(target_z) <= SCAN_RADIUS then
  188. -- ✅ Step 2: Try Moving Before Scanning
  189. local moved = smart_move(x_offset, y_offset, z_offset)
  190.  
  191. if moved then
  192. debug("✅ Move successful! Scanning chunk at X=" .. target_x .. " Y=" .. target_y .. " Z=" .. target_z)
  193. scan_chunk(x_offset, y_offset, z_offset) -- ✅ Scan the chunk only if movement succeeded
  194. else
  195. debug("⚠ Skipping blocked chunk at X=" .. target_x .. " Y=" .. target_y .. " Z=" .. target_z)
  196. end
  197. end
  198. end
  199. end
  200. end
  201.  
  202. debug("✅ Full area scan completed.")
  203. drone.setStatusText("Ready")
  204. end
  205.  
  206. -- Confirm script upload to sender
  207. modem.broadcast(123, "kino_update_success")
  208.  
  209. -- **Main Loop: Handles movement + scan mode**
  210. while true do
  211. local _, _, sender, _, _, cmd, value = computer.pullSignal(5)
  212.  
  213. if sender == tablet_address then
  214. if cmd == "move" then
  215. if value == "forward" then move(scan_step, 0, 0)
  216. elseif value == "backward" then move(-scan_step, 0, 0)
  217. elseif value == "left" then move(0, 0, -scan_step)
  218. elseif value == "right" then move(0, 0, scan_step)
  219. elseif value == "up" then move(0, scan_step, 0)
  220. elseif value == "down" then move(0, -scan_step, 0)
  221. end
  222. elseif cmd == "set_scan_mode" then scan_mode = value
  223. elseif cmd == "scan" then
  224. scan_full_area()
  225. drone.setStatusText("Scanning")
  226. elseif cmd == "fine_control" then
  227. if value == "on" then
  228. fine_motor_control = true
  229. scan_step = 1
  230. debug("🔧 Fine Motor Control ENABLED")
  231. elseif value == "off" then
  232. fine_motor_control = false
  233. scan_step = 4
  234. debug("🔧 Fine Motor Control DISABLED")
  235. end
  236. end
  237. end
  238. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement