imrskull

quarry_144

Oct 11th, 2025 (edited)
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. -- random_fly_20.lua (OC Robot + hover)
  2. -- Стартуй на зарядке: это (0,0,0) и "база". Смотрим на север.
  3. -- Летает случайно в кубе 20x20x20 по +X/+Y/+Z, пикает, при <30% энергии возвращается на базу и ждёт до 95%.
  4.  
  5. local robot = require("robot")
  6. local computer = require("computer")
  7. local os = require("os")
  8.  
  9. ----------------------------------------------------------------
  10. -- Настройки
  11. ----------------------------------------------------------------
  12. local BOUNDS = {x=20,y=20,z=20}
  13. local ENERGY_LOW = 0.30
  14. local ENERGY_FULL = 0.95
  15. local STEP_DELAY = 0.05
  16. local BEEP_EVERY = 25 -- шагов
  17.  
  18. -- Направления (старт см. на Север). Север=-Z, Восток=+X, Юг=+Z, Запад=-X
  19. local DIRS = {N=0,E=1,S=2,W=3}
  20. local dir = DIRS.N
  21.  
  22. -- Координаты (от базы)
  23. local pos = {x=0,y=0,z=0}
  24. local base = {x=0,y=0,z=0}
  25.  
  26. ----------------------------------------------------------------
  27. -- Безопасные бипы (не падают на старых версиях OC)
  28. ----------------------------------------------------------------
  29. local function safeBeep(a,b)
  30. if type(a)=="table" then
  31. for i=1,#a do
  32. local n=a[i]; local f=tonumber(n[1]) or 0; local d=tonumber(n[2]) or 0.1
  33. pcall(computer.beep,f,d); os.sleep(d+0.01)
  34. end
  35. else
  36. pcall(computer.beep, tonumber(a) or 0, tonumber(b) or 0.1)
  37. end
  38. end
  39.  
  40. local function beep_tick() safeBeep(1200,0.03) end
  41. local function beep_warn() safeBeep({{659,0.12},{0,0.05},{659,0.12}}) end
  42. local function beep_ok() safeBeep({{1046,0.06},{1318,0.08}}) end
  43. local function beep_start() safeBeep({{880,0.08},{988,0.08},{1046,0.12}}) end
  44. local function beep_home() safeBeep({{784,0.08},{659,0.08},{523,0.12}}) end
  45. local function beep_err() safeBeep({{220,0.20},{196,0.20}}) end
  46.  
  47. ----------------------------------------------------------------
  48. -- Энергия
  49. ----------------------------------------------------------------
  50. local function energy()
  51. local m = computer.maxEnergy() or 1
  52. local e = computer.energy() or 0
  53. if m<=0 then return 1 end
  54. return e/m
  55. end
  56.  
  57. ----------------------------------------------------------------
  58. -- Повороты/шаги (минимум вложенности)
  59. ----------------------------------------------------------------
  60. local function turnLeft() local ok=robot.turnLeft(); if ok then dir=(dir+3)%4 end; return ok end
  61. local function turnRight() local ok=robot.turnRight(); if ok then dir=(dir+1)%4 end; return ok end
  62.  
  63. local function face(t)
  64. t = t % 4
  65. local n=0
  66. while dir ~= t and n < 4 do
  67. turnRight(); n = n + 1
  68. end
  69. end
  70.  
  71. local function stepForward()
  72. if robot.forward() then
  73. if dir==DIRS.N then pos.z=pos.z-1
  74. elseif dir==DIRS.S then pos.z=pos.z+1
  75. elseif dir==DIRS.E then pos.x=pos.x+1
  76. else pos.x=pos.x-1
  77. end
  78. return true
  79. end
  80. return false
  81. end
  82.  
  83. local function stepBack()
  84. if robot.back() then
  85. if dir==DIRS.N then pos.z=pos.z+1
  86. elseif dir==DIRS.S then pos.z=pos.z-1
  87. elseif dir==DIRS.E then pos.x=pos.x-1
  88. else pos.x=pos.x+1
  89. end
  90. return true
  91. end
  92. return false
  93. end
  94.  
  95. local function stepUp() if robot.up() then pos.y=pos.y+1 return true end return false end
  96. local function stepDown() if robot.down() then pos.y=pos.y-1 return true end return false end
  97.  
  98. ----------------------------------------------------------------
  99. -- Навигация
  100. ----------------------------------------------------------------
  101. local function clamp(p)
  102. if p.x<0 then p.x=0 elseif p.x>=BOUNDS.x then p.x=BOUNDS.x-1 end
  103. if p.y<0 then p.y=0 elseif p.y>=BOUNDS.y then p.y=BOUNDS.y-1 end
  104. if p.z<0 then p.z=0 elseif p.z>=BOUNDS.z then p.z=BOUNDS.z-1 end
  105. end
  106.  
  107. local function moveTo(t)
  108. clamp(t)
  109. -- Y
  110. while pos.y < t.y do
  111. if energy()<ENERGY_LOW then return false,"low_energy" end
  112. if not stepUp() then return false,"blocked_y_up" end
  113. os.sleep(STEP_DELAY)
  114. end
  115. while pos.y > t.y do
  116. if energy()<ENERGY_LOW then return false,"low_energy" end
  117. if not stepDown() then return false,"blocked_y_down" end
  118. os.sleep(STEP_DELAY)
  119. end
  120. -- X
  121. while pos.x < t.x do
  122. if energy()<ENERGY_LOW then return false,"low_energy" end
  123. face(DIRS.E); if not stepForward() then return false,"blocked_x_pos" end
  124. os.sleep(STEP_DELAY)
  125. end
  126. while pos.x > t.x do
  127. if energy()<ENERGY_LOW then return false,"low_energy" end
  128. face(DIRS.W); if not stepForward() then return false,"blocked_x_neg" end
  129. os.sleep(STEP_DELAY)
  130. end
  131. -- Z
  132. while pos.z < t.z do
  133. if energy()<ENERGY_LOW then return false,"low_energy" end
  134. face(DIRS.S); if not stepForward() then return false,"blocked_z_pos" end
  135. os.sleep(STEP_DELAY)
  136. end
  137. while pos.z > t.z do
  138. if energy()<ENERGY_LOW then return false,"low_energy" end
  139. face(DIRS.N); if not stepForward() then return false,"blocked_z_neg" end
  140. os.sleep(STEP_DELAY)
  141. end
  142. return true
  143. end
  144.  
  145. local function goHome()
  146. io.write(string.format("[HOME] -> (%d,%d,%d)\n", base.x,base.y,base.z))
  147. local ok,why = moveTo(base)
  148. if not ok then io.write("[HOME] fail: "..tostring(why).."\n"); beep_err(); return false end
  149. beep_home(); io.write("[HOME] charging...\n")
  150. while energy() < ENERGY_FULL do os.sleep(0.5) end
  151. io.write("[HOME] charged\n"); beep_ok(); return true
  152. end
  153.  
  154. ----------------------------------------------------------------
  155. -- Цель и основной цикл
  156. ----------------------------------------------------------------
  157. local function randomTarget()
  158. return {x=math.random(0,BOUNDS.x-1), y=math.random(0,BOUNDS.y-1), z=math.random(0,BOUNDS.z-1)}
  159. end
  160.  
  161. math.randomseed(os.time())
  162. beep_start()
  163. io.write("[INIT] Куб 20x20x20 от базы по +X/+Y/+Z. Старт на зарядке, смотрим на Север.\n")
  164.  
  165. local steps=0
  166. while true do
  167. if energy() < ENERGY_LOW then beep_warn(); goHome() end
  168. local t = randomTarget()
  169. io.write(string.format("[MOVE] -> (%d,%d,%d)\n", t.x,t.y,t.z))
  170. local ok,why = moveTo(t)
  171. if ok then
  172. io.write(string.format("[MOVE] at (%d,%d,%d)\n", pos.x,pos.y,pos.z))
  173. else
  174. io.write("[WARN] move fail: "..tostring(why).."\n")
  175. end
  176. steps=steps+1
  177. if steps % BEEP_EVERY == 0 then beep_tick() end
  178. os.sleep(0.05)
  179. end
  180.  
Advertisement
Add Comment
Please, Sign In to add comment