Advertisement
Guest User

GuardianFin.lua

a guest
May 20th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.47 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 == nan) 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.     local contacts = radar.getBlocks()
  105.     os.sleep(0.1)
  106.     tunnel.send(serialization.serialize(contacts))
  107.  
  108.   elseif cmd == "detonateEmp" then
  109.     if lastDetonated then
  110.       rs.setBundledOutput(sides.front, colors.green, 255)
  111.       os.sleep(0.1)
  112.       rs.setBundledOutput(sides.front, colors.green, 0)
  113.     else
  114.       rs.setBundledOutput(sides.front, 3, 255)
  115.       os.sleep(0.1)
  116.       rs.setBundledOutput(sides.front, 3, 0)
  117.     end
  118.     lastDetonated = not lastDetonated
  119.     tunnel.send("true")
  120.  
  121.   elseif cmd == "launchMissile" then
  122.     local success = false
  123.     rs.setBundledOutput(sides.front, colors.blue, 0)
  124.     local _,_,_,_,_, x = event.pull("modem_message")
  125.     local _,_,_,_,_, y = event.pull("modem_message")
  126.     launchers[lastLaunched].setTarget(x,y)
  127.     local lastLauncedStart = lastLaunched
  128.     while (not launchers[LastLaunched].canLaunch()) do
  129.       if lastLaunched == lastLaunchedFirst then
  130.         break
  131.       end
  132.       if lastLaunched < n then
  133.         lastLaunched = lastLaunched + 1
  134.       else
  135.         lastLaunched = 0
  136.       end
  137.       launchers[lastLaunched].setTarget(x,y)
  138.     end
  139.     if launchers[lastLaunched].canLaunch() then
  140.       launchers[lastLaunched].launch()
  141.       success = true
  142.     end
  143.     if lastLaunched < n then
  144.       lastLaunched = lastLaunched + 1
  145.     else
  146.       lastLaunched = 0
  147.     end
  148.     tunnel.send(success)
  149.     rs.setBundledOutput(sides.front, colors.blue, 255)
  150.  
  151.   elseif cmd == "setTubes" then
  152.     local _,_,_,_,_,args = event.pull("modem_message")
  153.     if args == "open" then
  154.       rs.setBundledOutput(sides.front, colors.blue, 0)
  155.     else
  156.       rs.setBundledOutput(sides.front, colors.blue, 255)
  157.     end
  158.     tunnel.send("true")
  159.   end
  160.   com = nil
  161. end
  162.  
  163. function convertRadar(inList)
  164.   local ActualPositions = {}
  165.   convertedl = 0
  166.   for u,i in pairs(inList) do
  167.     ActualPositions[u] = i
  168.     convertedl = convertedl+1
  169.   end
  170.   return ActualPositions
  171. end
  172.  
  173. function getClosest(inList, PosX, PosY, PosZ)
  174.   smlDist=1000000
  175.   for iv, v in pairs(inList) do
  176.     i = string.sub(iv, 3)
  177.     X = inList["x_"..i]
  178.     Y = inList["y_"..i]
  179.     Z = inList["z_"..i]
  180.     dist = math.sqrt((X-PosX)^2+(Y-PosY)^2+(Z-PosZ)^2)
  181.     if dist<smlDist then
  182.       smlDist=dist
  183.       smlI = i
  184.     end
  185.   end
  186.   return smlI
  187. end
  188.  
  189. function getProjected(n)
  190.   PosX = Contacts[n]["PosX"]
  191.   PosY = Contacts[n]["PosY"]
  192.   PosZ = Contacts[n]["PosZ"]
  193.   VectX = Contacts[n]["VectX"]
  194.   VectY = Contacts[n]["VectY"]
  195.   VectZ = Contacts[n]["VectZ"]
  196.   lastUpdate = Contacts[n]["lastUpdated"]
  197.   X = PosX + VectX*lastUpdate
  198.   Y = PosY + VectY*lastUpdate
  199.   Z = PosZ + VectZ*lastUpdate
  200.   return X, Y, Z
  201. end
  202.  
  203. function ContactsUpdate(im, ln, inList, crtntyIn)
  204.   X = inList["x_"..im]
  205.   Y = inList["y_"..im]
  206.   Z = inList["z_"..im]
  207.   inList["x_"..im] = nil
  208.   inList["y_"..im] = nil
  209.   inList["z_"..im] = nil
  210.   convertedl = convertedl - 3
  211.   PosX = Contacts[ln]["PosX"]
  212.   PosY = Contacts[ln]["PosY"]
  213.   PosZ = Contacts[ln]["PosZ"]
  214.   crtnty = Contacts[ln]["Certainty"]
  215.   VectX = (X-PosX)/timeDif
  216.   VectY = (Y-PosY)/timeDif
  217.   VectZ = (Z-PosZ)/timeDif
  218.   PosX=X
  219.   PosY=Y
  220.   PosZ=Z
  221.   Contacts[ln]["Certainty"] = crtnty + crtntyIn
  222.   Contacts[ln]["PosX"] = PosX
  223.   Contacts[ln]["PosY"] = PosY
  224.   Contacts[ln]["PosZ"] = PosZ
  225.   Contacts[ln]["VectX"] = VectX
  226.   Contacts[ln]["VectY"] = VectY
  227.   Contacts[ln]["VectZ"] = VectZ
  228.   Contacts[ln]["updated"]=true
  229.   Contacts[ln]["lastUpdated"] = 0
  230. end
  231.  
  232. function newContact(n, inList)
  233.   X = inList["x_"..n]
  234.   Y = inList["y_"..n]
  235.   Z = inList["z_"..n]
  236.   nxt = tostring(hiCount)
  237.   Contacts[nxt] = {}
  238.   Contacts[nxt]["PosX"] = X
  239.   Contacts[nxt]["PosY"] = Y
  240.   Contacts[nxt]["PosZ"] = Z
  241.   Contacts[nxt]["VectX"] = 0.0001
  242.   Contacts[nxt]["VectY"] = 0.0001
  243.   Contacts[nxt]["VectZ"] = 0.0001
  244.   Contacts[nxt]["Certainty"] = initCertainty
  245.   Contacts[nxt]["updated"]=true
  246.   Contacts[nxt]["lastUpdated"] = 0
  247.   Contacts[nxt]["new"] = true
  248.   hiCount = hiCount + 1
  249.   inList["x_"..n] = nil
  250.   inList["y_"..n] = nil
  251.   inList["z_"..n] = nil
  252. end
  253.  
  254. local addrLauncher1 = "331255f5"
  255. local addrLauncher2 = "f45f0841"
  256.  
  257. local lastLaunched = 0
  258. local lastDetonated = true
  259. Contacts={}
  260. hiCount = 0
  261. accXY = 0.17
  262. accXYZ = 1.5
  263. thresholdTime = 0.05
  264.  
  265. lasttime = os.clock()
  266. initCertainty = 0.1
  267.  
  268. math = require("math")
  269. component = require("component")
  270. event = require("event")
  271. sides = require("sides")
  272. colors = require("colors")
  273. serialization = require("serialization")
  274.  
  275. tunnel = component.tunnel
  276. rs = component.redstone
  277. radar = component.radar_station
  278. local addrLstLauncher = component.list("defense")
  279. local n = 0
  280. local launchers = {}
  281. for i,v in pairs(addrLstLauncher) do
  282.   launchers[n] = component.get(i)
  283.   n=n+1
  284. end
  285.  
  286. print(n)
  287.  
  288. radars = radar.getBlocks()
  289. print("done1")
  290. converted = convertRadar(radars)
  291. print(converted["x_0"])
  292. print("done2")
  293. print(converted)
  294.  
  295. rs.setBundledOutput(sides.front, colors.blue, 255)
  296.  
  297. while true do
  298.   local _,_,_,_,_,cmd = event.pull(0.2,"modem_message")
  299.   if not cmd == nil then
  300.     com(cmd)  
  301.   end
  302.  track()
  303. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement