PHOBOSS

QUEEN_FIRMWARE.lua

Apr 5th, 2022 (edited)
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1. local component = require 'component'
  2. local modem = component.modem
  3.  
  4. local QUEEN = {}
  5.  
  6.  
  7. local FIRMWARE = {
  8. [[
  9. r= component.proxy(component.list('radar')())
  10. n= component.proxy(component.list('navigation')())
  11. d= component.proxy(component.list('drone')())
  12. m= component.proxy(component.list('modem')())
  13. Channel = 2412
  14. ResponseChannel = 2402
  15. m.open(Channel)
  16. m.setStrength(math.huge)
  17. drone_inv = "inv_q"
  18. isDroneQueen = true
  19. isFree = true
  20. broadcastGPS = false
  21. gpsChannel = 2
  22. gpsRequestChannel = 20
  23. m.open(gpsRequestChannel)
  24. ]]
  25. ,
  26. [[
  27. function sleep(timeout)
  28. checkArg(1, timeout, 'number', 'nil')
  29. local deadline = computer.uptime() + (timeout or 0)
  30. repeat
  31. computer.pullSignal(deadline - computer.uptime())
  32. until computer.uptime() >= deadline
  33. end
  34. ]]
  35. ,
  36. [[
  37. function sub_vec(A,B) return {A[1]-B[1],A[2]-B[2],A[3]-B[3]} end
  38. function math.trunc(v)
  39. local t = math.modf(v)
  40. return t
  41. end
  42. function vec_trunc(A)
  43. if A then
  44. return {math.trunc(A[1]),math.trunc(A[2]),math.trunc(A[3])}
  45. end
  46. return nil
  47. end
  48. ]]
  49. ,
  50. [[
  51. function getPlayerCoord(e_name)
  52. checkArg(1,e_name,'string','nil')
  53. for k,v in ipairs(r.getPlayers()) do
  54. if v.name == e_name then
  55. return {v.x,v.y,v.z},v.distance
  56. end
  57. end
  58. return nil
  59. end
  60. ]]
  61. ,
  62. [[
  63. function move(x,y,z)
  64. checkArg(1,x,'number','nil')
  65. checkArg(1,y,'number','nil')
  66. checkArg(1,z,'number','nil')
  67. if x and y and z then
  68. d.setLightColor(0x00FFAF)
  69. d.move(x,y,z)
  70. end
  71. end
  72. ]]
  73. ,
  74. [[
  75. function replyInv(add)
  76. m.send(add,ResponseChannel,"stats",isFree,isDroneQueen)--Queens send "true"
  77. end
  78. ]]
  79. ,
  80. [[
  81.  
  82. function QgpsBroadcast(c)
  83. if broadcastGPS and c[1] then
  84. --m.broadcast(gpsChannel,"gps",-c[1],-(c[2]+1),-c[3])
  85. m.broadcast(gpsChannel,"gps",c[1],c[2],c[3])
  86. d.setLightColor(0x00FFCC)
  87. end
  88. end
  89. ]]
  90. ,
  91. [[
  92. function gpsReply(addr)
  93. if gpsSatPos[1] then
  94. m.send(addr,gpsChannel,"gps",gpsSatPos[1],gpsSatPos[2],gpsSatPos[3])
  95. end
  96. end
  97. ]]
  98. ,
  99. [[
  100. acts = {
  101. ["go"] = function(_,tag) d.setLightColor(0x00FF00) d.setStatusText(navMoveToPlayer(tag)) end,
  102. ["bzz"] = function(_,tag) d.setLightColor(0x0000FF) d.setStatusText(navSwarmPlayer(tag)) end,
  103. ["move"] = function(_,_,x,y,z) move(x,y,z) end,
  104. [drone_inv] = function(r_add) d.setLightColor(0xFF00BB) replyInv(r_add) end,
  105. ["commit"] = function() d.setLightColor(0x0077FF) isFree = false end,
  106. ["uncommit"] = function() isFree = true end,
  107. ["formup"] = function(_,tag,x,y,z) d.setStatusText(navMoveToPlayerWOffset(tag,x,y,z)) end,
  108. ["setgpspos"] = function(_,_,x,y,z) gpsSatPos = {x,y,z} end,
  109. ["startgps"] = function() broadcastGPS = true end,
  110. ["stopgps"] = function() broadcastGPS = false end,
  111. ["reqgps"] = function(r_add) gpsReply(r_add) end,
  112. ["HUSH"] = function() computer.shutdown() end
  113. }
  114. ]]
  115. ,
  116. [[
  117. actsWhileMoving = {
  118. [drone_inv] = function(r_add) replyInv(r_add) end,
  119. ["commit"] = function() d.setLightColor(0x0077FF) isFree = false end,
  120. ["uncommit"] = function() isFree = true end,
  121. ["setgpspos"] = function(_,_,x,y,z) gpsSatPos = {x,y,z} end,
  122. ["startgps"] = function() broadcastGPS = true end,
  123. ["stopgps"] = function() broadcastGPS = false end,
  124. ["HUSH"] = function() d.setLightColor(0xFF0000) sleep(1) computer.shutdown() end
  125. }
  126. ]]
  127. ,
  128. [[
  129. function navMoveToPlayer(e_name)
  130. checkArg(1,e_name,"string","nil")
  131. local trgPos = {n.getPosition()},msg,r_add
  132.  
  133. if not trgPos[1] then d.setLightColor(0xFF0000) return "Out Of\nRange" end
  134.  
  135. trgPos = vec_trunc(trgPos)
  136. local mv = {0,0,0}
  137. local mapRange = n.getRange()
  138.  
  139. repeat
  140. local v = getPlayerCoord(e_name)
  141. if not v then
  142. d.setLightColor(0xFF0000)
  143. d.setStatusText("Out Of\nRange")
  144. else
  145. v = vec_trunc(v)
  146. local npos = {n.getPosition()}
  147. if npos[1] then
  148. npos = vec_trunc(npos)
  149. local Qpn = {npos[1] + v[1], npos[2] + v[2] + 2, npos[3] + v[3]}
  150. mv = sub_vec(Qpn,trgPos)
  151. if math.abs(Qpn[1]) < mapRange-5 and math.abs(Qpn[3]) < mapRange-5 then
  152. d.move(mv[1],mv[2],mv[3])
  153. trgPos = Qpn
  154. end
  155. else
  156. d.setLightColor(0xFF0000)
  157. d.setStatusText("Out Of\nRange")
  158. d.move(-mv[1],-mv[2],-mv[3])
  159. end
  160. end
  161. _,_,r_add,_,_,msg = computer.pullSignal(0.5)
  162. if actsWhileMoving[msg] then
  163. actsWhileMoving[msg](r_add)
  164. end
  165. until msg == "stop"
  166. return d.name()
  167. end
  168. ]]
  169. ,
  170. [[
  171. function navSwarmPlayer(e_name)
  172. checkArg(1,e_name,"string","nil")
  173. local trgPos = {n.getPosition()},msg,r_add
  174.  
  175. if not trgPos[1] then d.setLightColor(0xFF0000) return "Out Of\nRange" end
  176.  
  177. trgPos = vec_trunc(trgPos)
  178. local mv = {0,0,0}
  179. local mapRange = n.getRange()
  180.  
  181. repeat
  182. local v = getPlayerCoord(e_name)
  183. if not v then
  184. d.setLightColor(0xFF0000)
  185. d.setStatusText("Out Of\nRange")
  186. else
  187. v = vec_trunc(v)
  188. local npos = {n.getPosition()}
  189. if npos[1] then
  190. npos = vec_trunc(npos)
  191. local Qpn = {npos[1] + v[1] +math.random(-3,3), npos[2] + v[2] +math.random(-3,3), npos[3] + v[3]+math.random(-3,3)}
  192. mv = sub_vec(Qpn,trgPos)
  193. if math.abs(Qpn[1]) < mapRange-5 and math.abs(Qpn[3]) < mapRange-5 then
  194. d.move(mv[1],mv[2],mv[3])
  195. trgPos = Qpn
  196. end
  197. else
  198. d.setLightColor(0xFF0000)
  199. d.setStatusText("Out Of\nRange")
  200. d.move(-mv[1],-mv[2],-mv[3])
  201. end
  202. end
  203. _,_,r_add,_,_,msg = computer.pullSignal(0.5)
  204. if actsWhileMoving[msg] then
  205. actsWhileMoving[msg](r_add)
  206. end
  207. until msg == "stop"
  208. return d.name()
  209. end
  210. ]]
  211. ,
  212. [[
  213. gpsSatPos={}
  214. function navMoveToPlayerWOffset(e_name,xo,yo,zo)
  215. checkArg(1,e_name,"string","nil")
  216. local trgPos = {n.getPosition()}
  217. if not trgPos[1] then d.setLightColor(0xFF0000) return "Out Of\nRange" end
  218. trgPos = vec_trunc(trgPos)
  219. local mv = {0,0,0},msg,r_add
  220. local mapRange = n.getRange()
  221. repeat
  222. local v = getPlayerCoord(e_name)
  223. if not v then
  224. d.setLightColor(0xFF0000)
  225. d.setStatusText("Out Of\nRange")
  226. else
  227. v = vec_trunc(v)
  228. --QgpsBroadcast(v)
  229.  
  230. --gpsSatPos = {-v[1],-(v[2]+1),-v[3]} --offset origin to tablet position (above player's head)
  231. --gpsSatPos = {-v[1],-v[2],-v[3]}
  232.  
  233. local npos = {n.getPosition()}
  234. if npos[1] then
  235. npos = vec_trunc(npos)
  236. local Qpn = {npos[1] + v[1] + xo, npos[2] + v[2] + yo, npos[3] + v[3] + zo}
  237. mv = sub_vec(Qpn,trgPos)
  238. if math.abs(Qpn[1]) < mapRange-5 and math.abs(Qpn[3]) < mapRange-5 then
  239. d.move(mv[1],mv[2],mv[3])
  240. trgPos = Qpn
  241. end
  242. else
  243. d.setLightColor(0xFF0000)
  244. d.setStatusText("Out Of\nRange")
  245. d.move(-mv[1],-mv[2],-mv[3])
  246. end
  247. end
  248. _,_,r_add,_,_,msg = computer.pullSignal(0.5)
  249. if actsWhileMoving[msg] then
  250. actsWhileMoving[msg](r_add)
  251. end
  252. until msg == "stop"
  253. return d.name()
  254. end
  255. ]]
  256. ,
  257. [[
  258. d.setAcceleration(100)
  259. local cmd,tag,x,y,z
  260. d.setLightColor(0xFFAF00)
  261. while true do
  262. _,_,r_add,_,_,cmd,tag,x,y,z = computer.pullSignal(0.5)
  263. if d.name():match("^Q%d+$") then
  264. if acts[cmd] then
  265. acts[cmd](r_add,tag,x,y,z)
  266. end
  267. QgpsBroadcast(gpsSatPos)
  268. end
  269. --d.setLightColor(0xFFAF00)
  270. --d.setLightColor(0x77FF77)
  271. end
  272. ]]
  273. }
  274.  
  275. function QUEEN.broadcastFirmWare(port)
  276. for _,part in ipairs(FIRMWARE) do
  277. modem.broadcast(port,part)
  278. end
  279. end
  280.  
  281. return QUEEN
Add Comment
Please, Sign In to add comment