Advertisement
PHOBOSS

pseudoSoldier.lua

Apr 9th, 2022 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.37 KB | None | 0 0
  1. local component = require("component")
  2. local term = require("term")
  3. local computer = require("computer")
  4. local modem = component.modem
  5. gpsChannel = 2
  6. gpsRequestChannel = 20
  7. --trgChannel = 1
  8. SoldierChannel = 2413
  9.  
  10. modem.open(gpsChannel)
  11. --modem.open(trgChannel)
  12. modem.open(SoldierChannel)
  13. modem.setStrength(math.huge)
  14. gpsSats={}
  15. cmdTRGPos={}
  16.  
  17. drone_inv = "inv_s"
  18. ResponseChannel = 2403
  19. isFree = true
  20. function replyInv(add)
  21. print("replying to add: ",add)
  22. modem.send(add,ResponseChannel,"stats",isFree,false)--Queens send "true"
  23. end
  24.  
  25. function length(a)
  26. local c = 0
  27. for k,_ in pairs(a) do c=c+1 end
  28. return c
  29. end
  30.  
  31. function add2GPSTable(r_addr,x,y,z,dist)
  32. if length(gpsSats) < 7 then gpsSats[r_addr] = {x=x,y=y,z=z,d=dist} end
  33. end
  34.  
  35. acts = {
  36. [drone_inv] = function(r_add) replyInv(r_add) end,
  37. ["commit"] = function() isFree = false end,
  38. ["uncommit"] = function() isFree = true end,
  39. ["gps"] = function(r_addr,x,y,z,dist,_) add2GPSTable(r_addr,x,y,z,dist) end,
  40. ["trg"] = function(_,x,y,z) cmdTRGPos={c={x,y,z},d=dist} end,
  41.  
  42. ["formup"] = function(_,x,y,z,_,trgC) print(gpsMoveToTarget({x=x,y=y,z=z},trgC)) end,
  43.  
  44. ["HUSH"] = function() sleep(1) computer.shutdown() end
  45. }
  46.  
  47.  
  48. local floor, sqrt, abs = math.floor, math.sqrt, math.abs
  49.  
  50. local 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
  51. local 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
  52. local function len(v) return sqrt(v.x^2+v.y^2+v.z^2) end
  53. local function dot(v, b) return v.x*b.x+v.y*b.y+v.z*b.z end
  54. local function add(v, b) return {x=v.x+b.x, y=v.y+b.y, z=v.z+b.z} end
  55. local function sub(v, b) return {x=v.x-b.x, y=v.y-b.y, z=v.z-b.z} end
  56. local function mul(v, m) return {x=v.x*m, y=v.y*m, z=v.z*m} end
  57. local function norm(v) return mul(v, 1/len(v)) end
  58. local function trunc(v) local t = math.modf(v) return t end
  59. local function vec_trunc(A)
  60. if A then
  61. return {x=trunc(A.x),y=trunc(A.y),z=trunc(A.z)}
  62. end
  63. return nil
  64. end
  65.  
  66. local function trilaterate(A, B, C)
  67. local a2b = {x=B.x-A.x, y=B.y-A.y, z=B.z-A.z}
  68. local a2c = {x=C.x-A.x, y=C.y-A.y, z=C.z-A.z}
  69. if abs(dot(norm(a2b), norm(a2c))) > 0.999 then return nil end
  70. local d, ex = len(a2b), norm(a2b)
  71. local i = dot(ex, a2c)
  72. local exMi = mul(ex, i)
  73. local ey = norm(sub(a2c, mul(ex, i)))
  74. local j, ez = dot(ey, a2c), cross(ex, ey)
  75. local r1, r2, r3 = A.d, B.d, C.d
  76. local x = (r1^2 - r2^2 + d^2) / (2*d)
  77. local y = (r1^2 - r3^2 - x^2 + (x-i)^2 + j^2) / (2*j)
  78. local result = add(A, add(mul(ex, x), mul(ey, y)))
  79. local zSquared = r1^2 - x^2 - y^2
  80. if zSquared > 0 then
  81. local z = sqrt( zSquared )
  82. local result1 = add(result, mul(ez, z))
  83. local result2 = sub(result, mul(ez, z))
  84. local rnd1, rnd2 = result1,result2
  85. if rnd1.x ~= rnd2.x or rnd1.y ~= rnd2.y or rnd1.z ~= rnd2.z then
  86. --print("rnd1: ",rnd1.x,rnd1.y,rnd1.z)
  87. --print("rnd2: ",rnd2.x,rnd2.y,rnd2.z)
  88. return rnd1, rnd2
  89. else
  90. --print("rnd1: ",rnd1.x,rnd1.y,rnd1.z)
  91. return rnd1
  92. end
  93. end
  94. --print("result: ",result.x,result.y,result.z)
  95. return result
  96. end
  97.  
  98. local function narrow(p1, p2, fix)
  99. local d1 = abs(len(sub(p1, fix))-fix.d)
  100. local d2 = abs(len(sub(p2, fix))-fix.d)
  101. if abs(d1-d2) < 0.01 then
  102. return p1, p2
  103. elseif d1 < d2 then
  104. return p1,nil
  105. else
  106. return p2,nil
  107. end
  108. end
  109.  
  110. local function getGPSlocation()
  111. modem.open(gpsChannel)
  112. local fixes = {}
  113. local pos1, pos2 = nil, nil
  114. local deadline = computer.uptime()+2
  115. for addr,fix in pairs(gpsSats) do
  116. if fix.d == 0 then
  117. pos1, pos2 = {x=fix.x, y=fix.y, z=fix.z}, nil
  118. else
  119. table.insert(fixes, fix)
  120. end
  121. end
  122. if #fixes >= 3 then
  123. if fixes[1].z then
  124. if not pos1 then
  125. pos1, pos2 = trilaterate(fixes[1], fixes[2], fixes[3])
  126. end
  127. if pos1 and pos2 then
  128. for f=4,#fixes do
  129. pos1, pos2 = narrow(pos1, pos2, fixes[f])
  130. if pos1 and not pos2 then break end
  131. end
  132. end
  133. end
  134. end
  135.  
  136. if pos1 and pos2 then
  137. return nil
  138. elseif pos1 then
  139. local c = round(pos1,1)
  140. return {x=c.x,y=c.y,z=c.z}
  141. else
  142. return nil
  143. end
  144. end
  145.  
  146.  
  147. local refreshGPSInterval = 0
  148. function refreshGPSTable()
  149. if refreshGPSInterval >= 60 then gpsSats={} refreshGPSInterval = 0 end
  150. refreshGPSInterval = refreshGPSInterval + 1
  151. end
  152.  
  153. function getTRGPos()
  154. return cmdTRGPos
  155. end
  156.  
  157. function printGPSSats() -- temp
  158. print("gpsSats:")
  159. for addr,c in pairs(gpsSats) do
  160. print(addr,":: {",c.x,c.y,c.z,c.d,"}")
  161. end
  162. end
  163. function printTRGPos() -- temp
  164. if trgPos then
  165. print("trgPos: {",trgPos[1],trgPos[2],trgPos[3],"}")
  166. else
  167. print("trgPos:")
  168. end
  169. end
  170.  
  171. function printGPSTRG() -- temp
  172. term.clear()
  173. printGPSSats()
  174. printTRGPos()
  175. end
  176.  
  177. actsWhileMoving = {
  178. [drone_inv] = function(r_add) replyInv(r_add) end,
  179. ["commit"] = function() d.setLightColor(0x0077FF) isFree = false end,
  180. ["uncommit"] = function() isFree = true end,
  181. ["gps"] = function(r_addr,x,y,z,dist,_) add2GPSTable(r_addr,x,y,z,dist) end,
  182. ["trg"] = function(_,x,y,z,dist) cmdTRGPos={c={x=x,y=y,z=z},d=dist} end,
  183. ["HUSH"] = function() d.setLightColor(0xFF0000) sleep(1) computer.shutdown() end
  184. }
  185.  
  186. function gpsMoveToTarget(offset,trgChannel)
  187. checkArg(1,e_name,"string","nil")
  188. local ctrlTRGPos = nil
  189. modem.open(trgChannel)
  190. repeat
  191. term.clear()
  192. print("phase1")
  193. printGPSTRG()
  194. if length(gpsSats)>=3 then
  195. ctrlTRGPos = getGPSlocation()
  196. end
  197.  
  198. if ctrlTRGPos then ctrlTRGPos = vec_trunc(ctrlTRGPos)
  199. --[[else d.setLightColor(0xFF0000) d.setStatusText("No GPS") end]]
  200. else print("No GPS") end
  201.  
  202. _,_,r_add,_,dist,msg,x,y,z,_ = computer.pullSignal(0.5)
  203. if actsWhileMoving[msg] then
  204. actsWhileMoving[msg](r_add,x,y,z,dist)
  205. end
  206. until msg == "stop" or ctrlTRGPos
  207.  
  208. local mv = {0,0,0},msg,r_add,dist,x,y,z
  209.  
  210. repeat
  211. _,_,r_add,_,dist,msg,x,y,z,_ = computer.pullSignal(0.5)
  212. if actsWhileMoving[msg] then
  213. actsWhileMoving[msg](r_add,x,y,z,dist)
  214. end
  215.  
  216. term.clear()
  217. print("phase2")
  218. printGPSTRG()
  219. print("ctrlTRGPos: ",ctrlTRGPos.x,ctrlTRGPos.y,ctrlTRGPos.z)
  220. local trgPos = getTRGPos()
  221. if trgPos.d and trgPos.d < 50 then
  222. trgPos.c = vec_trunc(trgPos.c)
  223. print("trgPos: ",trgPos.c.x,trgPos.c.y,trgPos.c.z)
  224. print("Offset: ",offset.x,offset.y,offset.z)
  225.  
  226. local trgPosOffset = add(trgPos.c, offset)
  227. print("trgPosOffset: ",trgPosOffset.x,trgPosOffset.y,trgPosOffset.z)
  228.  
  229. mv = sub(trgPosOffset,ctrlTRGPos)
  230. --d.move(mv.x,mv.y,mv.z)
  231. print("mv: ",mv.x,mv.y,mv.z)
  232.  
  233. ctrlTRGPos = trgPosOffset
  234. else
  235. --[[d.setLightColor(0xFF0000)
  236. d.setStatusText("Out Of\nRange")
  237. d.move(-mv.x,-mv.y,-mv.z)]]
  238. print("Out Of Range")
  239. end
  240. refreshGPSTable()
  241. until msg == "stop"
  242. modem.close(trgChannel)
  243. return "S1"
  244. end
  245.  
  246.  
  247. --gpsMoveToTarget({x=10,y=23,z=35})
  248. while true do
  249. _,_,r_addr,_,dist,msg,x,y,z,trgCh = computer.pullSignal(0.5)
  250. --_,_,r_addr,_,dist,msg,trgCh,x,y,z = computer.pullSignal()
  251. if acts[msg] then
  252. acts[msg](r_addr,x,y,z,dist,trgCh)
  253. end
  254. end
  255.  
  256. --[[
  257. while true do
  258. _,_,r_addr,_,dist,msg,trgCh,x,y,z = computer.pullSignal(0.5)
  259.  
  260. if acts[msg] then
  261. acts[msg](r_addr,x,y,z,dist,trgCh)
  262. end
  263.  
  264. --printGPSTRG()
  265. local current_pos
  266. if length(gpsSats)>=3 then
  267. current_pos = getGPSlocation()
  268. end
  269.  
  270. term.clear()
  271. if current_pos then
  272. print("current_pos: ",current_pos.x,current_pos.y,current_pos.z)
  273. else
  274. print("current_pos:")
  275. end
  276.  
  277. refreshGPSTable()
  278. end
  279. ]]
  280.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement