Advertisement
Guest User

Guardian.lua

a guest
May 20th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.62 KB | None | 0 0
  1. function track()
  2.  convertedl = 0
  3.  while radars == radar.getEntities() do
  4.    flag = false
  5.    for _ in pairs(radars) do
  6.     flag = true
  7.    end
  8.    if flag then break end
  9.  end
  10.  radars = radar.getEntities()
  11.  timeDif = os.clock() - lasttime
  12.  lasttime = os.clock()
  13.  print("timeDif", timeDif)
  14.  print("hiCount", hiCount)
  15.  converted = convertRadar(radars)
  16.  for i, val in pairs(Contacts) do
  17.   if convertedl > 0 then
  18.     n = getClosest(converted, getProjected(i))
  19.     Certainty = Contacts[i]["Certainty"]
  20.     DivXY = getDivXY(i,n,converted)
  21.     DivXYZ = getDivXYZ(i,n,converted)
  22.     print("DivXZ", DivXY)
  23.     print("DivXYZ", DivXYZ)
  24.   else
  25.     DivXY = 10000000000
  26.     DivXYZ = 10000000000
  27.   end  
  28.   print("comp", DivXY<accXY)
  29.   if (DivXY ~= DivXY) then
  30.    ContactsUpdate(n,i,converted,0)
  31.    print("updateL")
  32.   elseif ((DivXY<accXY) and (DivXYZ<(accXYZ-Certainty)) and (not Contacts[i]["new"])) then
  33.    ContactsUpdate(n,i,converted,0.01)
  34.    print("updateA")
  35.   elseif ((DivXY<accXY) and (DivXYZ>=(accXYZ-Certainty)) and (not Contacts[i]["new"])) then
  36.    ContactsUpdate(n,i,converted,-0.01)
  37.    print("updateB")
  38.   elseif Contacts[i]["new"] then
  39.    if not (convertedl == 0) then
  40.     ContactsUpdate(n,i,converted,0.01)
  41.     Contacts[i]["new"] = false
  42.     print("updateN")
  43.    else
  44.     Contacts[i] = nil
  45.     print("updateDel")
  46.    end
  47.   else
  48.    print("NoUpdate")
  49.    if Contacts[i]["lastUpdated"] < thresholdTime then
  50.     Contacts[i]["lastUpdated"] = Contacts[i]["lastUpdated"] + timeDif
  51.    else
  52.     Contacts[i]=nil
  53.     print("del")
  54.    end
  55.   end
  56.  end
  57.  contactsEmpty = false
  58.  while not contactsEmpty do
  59.   contactsEmpty = true
  60.   for iter, val in pairs(converted) do
  61.    contactsEmpty = false
  62.    num = string.sub(iter, 3)
  63.    newContact(num, converted)
  64.    break
  65.   end
  66.  end
  67. end
  68.  
  69. function setNotUpdated()
  70.   for i,v in pairs(Contacts) do
  71.      Contacts[i]["updated"] = false
  72.   end
  73. end
  74.  
  75. function getDivXYZ(i,n,inList)
  76.  PosX = Contacts[i]["PosX"]
  77.  PosY = Contacts[i]["PosY"]
  78.  PosZ = Contacts[i]["PosZ"]
  79.  VectX = Contacts[i]["VectX"]
  80.  VectY = Contacts[i]["VectY"]
  81.  VectZ = Contacts[i]["VectZ"]
  82.  VectXa = inList["x_"..n]-PosX
  83.  VectYa = inList["y_"..n]-PosY
  84.  VectZa = inList["z_"..n]-PosZ
  85.  cos = (VectX*VectXa+VectY*VectYa+VectZ*VectZa)/(math.sqrt((VectX^2)+(VectY^2)+(VectZ^2))*math.sqrt((VectXa^2)+(VectYa^2)+(VectZa^2)))
  86.  angle = math.acos(cos)
  87.  return angle
  88. end
  89.  
  90. function getDivXY(i,n,inList)
  91.  PosX = Contacts[i]["PosX"]
  92.  PosY = Contacts[i]["PosZ"]
  93.  VectX = Contacts[i]["VectX"]
  94.  VectY = Contacts[i]["VectZ"]
  95.  VectXa = inList["x_"..n]-PosX
  96.  VectYa = inList["z_"..n]-PosY
  97.  angle = math.acos((VectX*VectXa+VectY*VectYa)/(math.sqrt((VectX^2)+(VectY^2))*math.sqrt((VectXa^2)+(VectYa^2))))
  98.  return angle
  99. end
  100.  
  101. function com(cmd)
  102.   print(cmd)
  103.   if     cmd == "getContacts" then
  104.     os.sleep(0.1)
  105.     tunnel.send(serialization.serialize(Contacts))
  106.  
  107.   elseif cmd == "resetContacts" then
  108.     Contacts = {}
  109.     hiCount = 0
  110.     tunnel.send("true")
  111.  
  112.   elseif cmd == "detonateEmp" then
  113.     if lastDetonated then
  114.       rs.setBundledOutput(sides.front, colors.green, 255)
  115.       os.sleep(0.1)
  116.       rs.setBundledOutput(sides.front, colors.green, 0)
  117.     else
  118.       rs.setBundledOutput(sides.front, 3, 255)
  119.       os.sleep(0.1)
  120.       rs.setBundledOutput(sides.front, 3, 0)
  121.     end
  122.     lastDetonated = not lastDetonated
  123.     tunnel.send("true")
  124.  
  125.   elseif cmd == "launchMissile" then
  126.     local success = false
  127.     rs.setBundledOutput(sides.front, colors.blue, 0)
  128.     local _,_,_,_,_, x = event.pull("modem_message")
  129.     local _,_,_,_,_, y = event.pull("modem_message")
  130.     launchers[lastLaunched].setTarget(x,y)
  131.     local lastLauncedStart = lastLaunched
  132.     while (not launchers[LastLaunched].canLaunch()) do
  133.       if lastLaunched == lastLaunchedFirst then
  134.         break
  135.       end
  136.       if lastLaunched < n then
  137.         lastLaunched = lastLaunched + 1
  138.       else
  139.         lastLaunched = 0
  140.       end
  141.       launchers[lastLaunched].setTarget(x,y)
  142.     end
  143.     if launchers[lastLaunched].canLaunch() then
  144.       launchers[lastLaunched].launch()
  145.       success = true
  146.     end
  147.     if lastLaunched < n then
  148.       lastLaunched = lastLaunched + 1
  149.     else
  150.       lastLaunched = 0
  151.     end
  152.     tunnel.send(success)
  153.     rs.setBundledOutput(sides.front, colors.blue, 255)
  154.  
  155.   elseif cmd == "setTubes" then
  156.     local _,_,_,_,_,args = event.pull("modem_message")
  157.     if args == "open" then
  158.       rs.setBundledOutput(sides.front, colors.blue, 0)
  159.     else
  160.       rs.setBundledOutput(sides.front, colors.blue, 255)
  161.     end
  162.     tunnel.send("true")
  163.   end
  164.   cmd = nil
  165. end
  166.  
  167. function convertRadar(inList)
  168.   local ActualPositions = {}
  169.   convertedl = 0
  170.   for u,i in pairs(inList) do
  171.     ActualPositions[u] = i
  172.     convertedl = convertedl+1
  173.   end
  174.   return ActualPositions
  175. end
  176.  
  177. function getClosest(inList, PosX, PosY, PosZ)
  178.   smlDist=1000000
  179.   for iv, v in pairs(inList) do
  180.     i = string.sub(iv, 3)
  181.     X = inList["x_"..i]
  182.     Y = inList["y_"..i]
  183.     Z = inList["z_"..i]
  184.     dist = math.sqrt(((X-PosX)^2)+((Y-PosY)^2)+((Z-PosZ)^2))
  185.     if dist<smlDist then
  186.       smlDist=dist
  187.       smlI = i
  188.     end
  189.   end
  190.   return smlI
  191. end
  192.  
  193. function getProjected(n)
  194.   PosX = Contacts[n]["PosX"]
  195.   PosY = Contacts[n]["PosY"]
  196.   PosZ = Contacts[n]["PosZ"]
  197.   VectX = Contacts[n]["VectX"]
  198.   VectY = Contacts[n]["VectY"]
  199.   VectZ = Contacts[n]["VectZ"]
  200.   lastUpdate = Contacts[n]["lastUpdated"]
  201.   X = PosX + VectX*lastUpdate
  202.   Y = PosY + VectY*lastUpdate
  203.   Z = PosZ + VectZ*lastUpdate
  204.   return X, Y, Z
  205. end
  206.  
  207. function ContactsUpdate(im, ln, inList, crtntyIn)
  208.   X = inList["x_"..im]
  209.   Y = inList["y_"..im]
  210.   Z = inList["z_"..im]
  211.   inList["x_"..im] = nil
  212.   inList["y_"..im] = nil
  213.   inList["z_"..im] = nil
  214.   convertedl = convertedl - 3
  215.   PosX = Contacts[ln]["PosX"]
  216.   PosY = Contacts[ln]["PosY"]
  217.   PosZ = Contacts[ln]["PosZ"]
  218.   crtnty = Contacts[ln]["Certainty"]
  219.   VectX = (X-PosX)/timeDif
  220.   VectY = (Y-PosY)/timeDif
  221.   VectZ = (Z-PosZ)/timeDif
  222.   PosX=X
  223.   PosY=Y
  224.   PosZ=Z
  225.   Contacts[ln]["Certainty"] = crtnty + crtntyIn
  226.   Contacts[ln]["PosX"] = PosX
  227.   Contacts[ln]["PosY"] = PosY
  228.   Contacts[ln]["PosZ"] = PosZ
  229.   Contacts[ln]["VectX"] = VectX
  230.   Contacts[ln]["VectY"] = VectY
  231.   Contacts[ln]["VectZ"] = VectZ
  232.   Contacts[ln]["updated"]=true
  233.   Contacts[ln]["lastUpdated"] = 0
  234. end
  235.  
  236. function newContact(n, inList)
  237.   X = inList["x_"..n]
  238.   Y = inList["y_"..n]
  239.   Z = inList["z_"..n]
  240.   nxt = tostring(hiCount)
  241.   Contacts[nxt] = {}
  242.   Contacts[nxt]["PosX"] = X
  243.   Contacts[nxt]["PosY"] = Y
  244.   Contacts[nxt]["PosZ"] = Z
  245.   Contacts[nxt]["VectX"] = 0.0001
  246.   Contacts[nxt]["VectY"] = 0.0001
  247.   Contacts[nxt]["VectZ"] = 0.0001
  248.   Contacts[nxt]["Certainty"] = initCertainty
  249.   Contacts[nxt]["updated"]=true
  250.   Contacts[nxt]["lastUpdated"] = 0
  251.   Contacts[nxt]["new"] = true
  252.   hiCount = hiCount + 1
  253.   inList["x_"..n] = nil
  254.   inList["y_"..n] = nil
  255.   inList["z_"..n] = nil
  256. end
  257.  
  258. local addrLauncher1 = "331255f5"
  259. local addrLauncher2 = "f45f0841"
  260.  
  261. local lastLaunched = 0
  262. local lastDetonated = true
  263. Contacts={}
  264. hiCount = 0
  265. accXY = 0.17
  266. accXYZ = 1.5
  267. thresholdTime = 0.05
  268.  
  269. lasttime = os.clock()
  270. initCertainty = 0.1
  271.  
  272. math = require("math")
  273. component = require("component")
  274. event = require("event")
  275. sides = require("sides")
  276. colors = require("colors")
  277. serialization = require("serialization")
  278.  
  279. tunnel = component.tunnel
  280. rs = component.redstone
  281. radar = component.radar_station
  282. local addrLstLauncher = component.list("defense")
  283. local n = 0
  284. local launchers = {}
  285. for i,v in pairs(addrLstLauncher) do
  286.   launchers[n] = component.get(i)
  287.   n=n+1
  288. end
  289.  
  290. print(n)
  291.  
  292. radars = radar.getBlocks()
  293. print("done1")
  294. converted = convertRadar(radars)
  295. print(converted["x_0"])
  296. print("done2")
  297. print(converted)
  298.  
  299. rs.setBundledOutput(sides.front, colors.blue, 255)
  300.  
  301. while true do
  302.   local _,_,_,_,_,cmd = event.pull(0.2,"modem_message")
  303.   if (not (cmd == nil)) then
  304.     com(cmd)
  305.     cmd = nil  
  306.   end
  307.  track()
  308.  if (rs.getBundledInput(sides.front, colors.white)>0)then
  309.   os.exit()
  310.  end
  311. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement