Advertisement
PHOBOSS

SOLDIER_FIRMWARE.lua

Apr 9th, 2022 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require 'component'
  2. local modem = component.modem
  3.  
  4. local PAWN = {}
  5.  
  6.  
  7. local FIRMWARE = {
  8. [[
  9. d= component.proxy(component.list('drone')())
  10. m= component.proxy(component.list('modem')())
  11. Channel = 2413
  12. ResponseChannel = 2403
  13. gpsChannel = 2
  14.  
  15. m.open(Channel)
  16.  
  17.  
  18. drone_inv = "inv_s"
  19. isDroneQueen = false
  20. isFree = true
  21.  
  22. gpsSats={}
  23. cmdTRGPos={}
  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. floor, sqrt, abs = math.floor, math.sqrt, math.abs
  38.  
  39. function round(v, m) return {x = floor((v.x+(m*0.5))/m)*m, y = floor((v.y+(m*0.5))/m)*m, z = floor((v.z+(m*0.5))/m)*m} end
  40. function cross(v, b) return {x = v.y*b.z-v.z*b.y, y = v.z*b.x-v.x*b.z, z = v.x*b.y-v.y*b.x} end
  41. function len(v) return sqrt(v.x^2+v.y^2+v.z^2) end
  42. function dot(v, b) return v.x*b.x+v.y*b.y+v.z*b.z end
  43. function add(v, b) return {x=v.x+b.x, y=v.y+b.y, z=v.z+b.z} end
  44. function sub(v, b) return {x=v.x-b.x, y=v.y-b.y, z=v.z-b.z} end
  45. function mul(v, m) return {x=v.x*m, y=v.y*m, z=v.z*m} end
  46. function norm(v) return mul(v, 1/len(v)) end
  47.  
  48. function arr_length(a)
  49. local c = 0
  50. for k,_ in pairs(a) do c=c+1 end
  51. return c
  52. end
  53. function trunc(v) local t = math.modf(v) return t end
  54. function vec_trunc(A)
  55. if A then
  56. return {x=trunc(A.x),y=trunc(A.y),z=trunc(A.z)}
  57. end
  58. return nil
  59. end
  60. ]]
  61. ,
  62.  
  63. [[
  64. function add2GPSTable(r_addr,x,y,z,dist)
  65. if arr_length(gpsSats) < 7 then gpsSats[r_addr] = {x=x,y=y,z=z,d=dist} end
  66. end
  67. ]]
  68. ,
  69. [[
  70. function replyInv(add)
  71. m.send(add,ResponseChannel,"stats",isFree,isDroneQueen)--Queens send "true"
  72. end
  73. ]]
  74. ,
  75. [[
  76. acts = {
  77. [drone_inv] = function(r_add) d.setLightColor(0x00FFBB) replyInv(r_add) end,
  78. ["commit"] = function() d.setLightColor(0x5E00FF) isFree = false end,
  79. ["uncommit"] = function() isFree = true end,
  80.  
  81. ["gps"] = function(r_addr,x,y,z,dist) add2GPSTable(r_addr,x,y,z,dist) end,
  82. ["trg"] = function(_,x,y,z,dist) cmdTRGPos={c={x,y,z},d=dist} end,
  83. ["formup"] = function(_,x,y,z,_,trgC) d.setStatusText(gpsMoveToTarget({x=x,y=y,z=z},trgC)) end,
  84. ["orbit"] = function(_,x,y,z,_,trgC) d.setStatusText("orbiting") d.setStatusText(gpsOrbitTRG({x=x,y=y,z=z},trgC)) end,
  85. ["HUSH"] = function() computer.shutdown() end
  86. }
  87. ]]
  88. ,
  89. [[
  90. actsWhileMoving = {
  91. [drone_inv] = function(r_add) replyInv(r_add) end,
  92. ["commit"] = function() d.setLightColor(0x0077FF) isFree = false end,
  93. ["uncommit"] = function() isFree = true end,
  94. ["gps"] = function(r_addr,x,y,z,dist) add2GPSTable(r_addr,x,y,z,dist) end,
  95. --["trg"] = function(_,x,y,z,dist) cmdTRGPos={c={x=x,y=y,z=z},d=dist} d.setStatusText("trg2:"..tostring(cmdTRGPos.c.x)) end,
  96. ["HUSH"] = function() d.setLightColor(0xFF0000) sleep(1) computer.shutdown() end
  97. }
  98. ]]
  99. ,
  100. [[
  101. function trilaterate(A, B, C)
  102. local a2b = {x=B.x-A.x, y=B.y-A.y, z=B.z-A.z}
  103. local a2c = {x=C.x-A.x, y=C.y-A.y, z=C.z-A.z}
  104. if abs(dot(norm(a2b), norm(a2c))) > 0.999 then return nil end
  105. local d, ex = len(a2b), norm(a2b)
  106. local i = dot(ex, a2c)
  107. local exMi = mul(ex, i)
  108. local ey = norm(sub(a2c, mul(ex, i)))
  109. local j, ez = dot(ey, a2c), cross(ex, ey)
  110. local r1, r2, r3 = A.d, B.d, C.d
  111. local x = (r1^2 - r2^2 + d^2) / (2*d)
  112. local y = (r1^2 - r3^2 - x^2 + (x-i)^2 + j^2) / (2*j)
  113. local result = add(A, add(mul(ex, x), mul(ey, y)))
  114. local zSquared = r1^2 - x^2 - y^2
  115. if zSquared > 0 then
  116. local z = sqrt( zSquared )
  117. local result1 = add(result, mul(ez, z))
  118. local result2 = sub(result, mul(ez, z))
  119. local rnd1, rnd2 = result1,result2
  120. if rnd1.x ~= rnd2.x or rnd1.y ~= rnd2.y or rnd1.z ~= rnd2.z then
  121. return rnd1, rnd2
  122. else
  123. return rnd1
  124. end
  125. end
  126. return result
  127. end
  128. ]]
  129. ,
  130. [[
  131. function narrow(p1, p2, fix)
  132. local d1 = abs(len(sub(p1, fix))-fix.d)
  133. local d2 = abs(len(sub(p2, fix))-fix.d)
  134. if abs(d1-d2) < 0.01 then
  135. return p1, p2
  136. elseif d1 < d2 then
  137. return p1,nil
  138. else
  139. return p2,nil
  140. end
  141. end
  142. ]]
  143. ,
  144. [[
  145. function getGPSlocation()
  146. local fixes = {}
  147. local pos1, pos2 = nil, nil
  148. for addr,fix in pairs(gpsSats) do
  149. if fix.d == 0 then
  150. pos1, pos2 = {x=fix.x, y=fix.y, z=fix.z}, nil
  151. else
  152. table.insert(fixes, fix)
  153. end
  154. end
  155. if #fixes >= 3 then
  156. if not pos1 then
  157. pos1, pos2 = trilaterate(fixes[1], fixes[2], fixes[3])
  158. end
  159. if pos1 and pos2 then
  160. for f=4,#fixes do
  161. pos1, pos2 = narrow(pos1, pos2, fixes[f])
  162. if pos1 and not pos2 then break end
  163. end
  164. end
  165. end
  166.  
  167. if pos1 and pos2 then
  168. d.setStatusText("ps12")
  169. return nil
  170. elseif pos1 then
  171. local c = round(pos1,1)
  172. return {x=c.x,y=c.y,z=c.z}
  173. else
  174. d.setStatusText("else")
  175. return nil
  176. end
  177. end
  178. ]]
  179. ,
  180. [[
  181. refreshGPSInterval = 0
  182. function refreshGPSTable()
  183. if refreshGPSInterval >= 17 then gpsSats={} refreshGPSInterval = 0 end
  184. refreshGPSInterval = refreshGPSInterval + 1
  185. end
  186.  
  187. function getTRGPos()
  188. return cmdTRGPos
  189. end
  190. ]]
  191. ,
  192. [[
  193. function gpsMoveToTarget(offset,trgChannel)
  194. d.setLightColor(0xFFFFFF)
  195. gpsSats={}
  196. m.open(gpsChannel)
  197. local ctrlTRGPos = nil
  198. repeat
  199. if arr_length(gpsSats)>=3 then
  200. ctrlTRGPos = getGPSlocation()
  201. end
  202.  
  203. if ctrlTRGPos then ctrlTRGPos = vec_trunc(ctrlTRGPos)
  204. else d.setLightColor(0xFF0000) d.setStatusText("No GPS:") end
  205.  
  206. _,_,r_addr,_,dist,msg,x,y,z,trgCh = computer.pullSignal(0.5)
  207.  
  208. if actsWhileMoving[msg] then
  209. actsWhileMoving[msg](r_addr,x,y,z,dist)
  210. end
  211. refreshGPSTable()
  212. until msg == "stop" or ctrlTRGPos
  213.  
  214. if ctrlTRGPos then
  215. m.close(gpsChannel)
  216. d.setLightColor(0xFFFFFF)
  217. m.open(trgChannel)
  218. local mv = {x=0,y=0,z=0},msg,r_add,dist,x,y,z
  219. local trgUpdate = {}
  220. repeat
  221. _,_,r_addr,_,dist,msg,x,y,z,trgCh = computer.pullSignal(0.5)
  222.  
  223. if msg == "trg" then
  224. trgUpdate = {c={x=x,y=y,z=z},d=dist}
  225. end
  226. local trgPos = trgUpdate
  227.  
  228. if trgPos.d and trgPos.d < 50 then
  229. trgPos.c = vec_trunc(trgPos.c)
  230. local trgPosOffset = add(trgPos.c, offset)
  231. mv = sub(trgPosOffset,ctrlTRGPos)
  232. d.move(mv.x,mv.y,mv.z)
  233. ctrlTRGPos = trgPosOffset
  234. d.setLightColor(0x00FF00)
  235. d.setStatusText(d.name())
  236. else
  237. d.setLightColor(0xFF0000)
  238. d.setStatusText("Out Of\nRange")
  239. d.move(-mv.x,-mv.y,-mv.z)
  240. end
  241. if actsWhileMoving[msg] then
  242. actsWhileMoving[msg](r_addr,x,y,z,dist)
  243. end
  244. until msg == "stop"
  245. end
  246. m.close(trgChannel)
  247. return d.name()
  248. end
  249. ]]
  250. ,
  251. [[
  252. function rotatePoint(rad,point)
  253. local s = math.sin(rad);
  254. local c = math.cos(rad);
  255.  
  256. // rotate point
  257. float xnew = point.x * c - point.y * s;
  258. float ynew = point.x * s + point.y * c;
  259.  
  260. return point
  261. end
  262. ]]
  263. ,
  264. [[
  265. function gpsOrbitTRG(offset,trgChannel)
  266. d.setLightColor(0xFFFFFF)
  267. gpsSats={}
  268. m.open(gpsChannel)
  269. local ctrlTRGPos = nil
  270. repeat
  271. if arr_length(gpsSats)>=3 then
  272. ctrlTRGPos = getGPSlocation()
  273. end
  274.  
  275. if ctrlTRGPos then ctrlTRGPos = vec_trunc(ctrlTRGPos)
  276. else d.setLightColor(0xFF0000) d.setStatusText("No GPS:") end
  277.  
  278. _,_,r_addr,_,dist,msg,x,y,z,trgCh = computer.pullSignal(0.5)
  279.  
  280. if actsWhileMoving[msg] then
  281. actsWhileMoving[msg](r_addr,x,y,z,dist)
  282. end
  283. refreshGPSTable()
  284. until msg == "stop" or ctrlTRGPos
  285.  
  286. if ctrlTRGPos then
  287. m.close(gpsChannel)
  288. d.setLightColor(0xFFFFFF)
  289. m.open(trgChannel)
  290. local mv = {x=0,y=0,z=0},msg,r_add,dist,x,y,z
  291. local trgUpdate = {}
  292. local currentAngle = 0 -- in radians
  293. local rotationInterval = 0.785 -- PI/4
  294. repeat
  295. _,_,r_addr,_,dist,msg,x,y,z,trgCh = computer.pullSignal(0.5)
  296.  
  297. if msg == "trg" then
  298. trgUpdate = {c={x=x,y=y,z=z},d=dist}
  299. end
  300. local trgPos = trgUpdate
  301.  
  302. if trgPos.d and trgPos.d < 50 then
  303. trgPos.c = vec_trunc(trgPos.c)
  304.  
  305. if currentAngle >= 6.3832 then currentAngle = 0 end
  306. local rotatedOffset = rotatePoint(currentAngle,offset)
  307. currentAngle = currentAngle + rotationInterval
  308.  
  309. local trgPosOffset = add(trgPos.c, rotatedOffset)
  310.  
  311. mv = sub(trgPosOffset,ctrlTRGPos)
  312. d.move(mv.x,mv.y,mv.z)
  313. ctrlTRGPos = trgPosOffset
  314. d.setLightColor(0x00FF00)
  315. d.setStatusText(d.name())
  316. else
  317. d.setLightColor(0xFF0000)
  318. d.setStatusText("Out Of\nRange")
  319. d.move(-mv.x,-mv.y,-mv.z)
  320. end
  321. if actsWhileMoving[msg] then
  322. actsWhileMoving[msg](r_addr,x,y,z,dist)
  323. end
  324. until msg == "stop"
  325. end
  326. m.close(trgChannel)
  327. return d.name()
  328. end
  329. ]]
  330. ,
  331. [[
  332. d.setAcceleration(100)
  333. d.setLightColor(0x007B62)
  334. while true do
  335. _,_,r_addr,_,dist,msg,x,y,z,trgCh = computer.pullSignal(0.5)
  336. if d.name():match("^S%d+$") then
  337. if msg =="orbit"then d.setStatusText("orbitter") end
  338. if acts[msg] then
  339. acts[msg](r_addr,x,y,z,dist,trgCh)
  340. end
  341. end
  342. --d.setLightColor(0xFFAF00)
  343. --d.setLightColor(0x77FF77)
  344. end
  345. ]]
  346. }
  347.  
  348. PAWN.master = "" --control tablet modem address, set before broadcasting firmware
  349.  
  350. function PAWN.broadcastFirmWare(port)
  351. modem.broadcast(port,
  352. [[
  353. master = ]]..PAWN.master..[[
  354. ]]
  355. )
  356. for _,part in ipairs(FIRMWARE) do
  357. modem.broadcast(port,part)
  358. end
  359. end
  360.  
  361. return PAWN
  362.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement