Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function track()
- convertedl = 0
- while radars == radar.getEntities() do
- flag = false
- for _ in pairs(radars) do
- flag = true
- end
- if flag then break end
- end
- radars = radar.getEntities()
- timeDif = os.clock() - lasttime
- lasttime = os.clock()
- print("timeDif", timeDif)
- print("hiCount", hiCount)
- converted = convertRadar(radars)
- for i, val in pairs(Contacts) do
- if convertedl > 0 then
- n = getClosest(converted, getProjected(i))
- Certainty = Contacts[i]["Certainty"]
- DivXY = getDivXY(i,n,converted)
- DivXYZ = getDivXYZ(i,n,converted)
- print("DivXZ", DivXY)
- print("DivXYZ", DivXYZ)
- else
- DivXY = 10000000000
- DivXYZ = 10000000000
- end
- print("comp", DivXY<accXY)
- if (DivXY == nan) then
- ContactsUpdate(n,i,converted,0)
- print("updateL")
- elseif ((DivXY<accXY) and (DivXYZ<(accXYZ-Certainty)) and (not Contacts[i]["new"])) then
- ContactsUpdate(n,i,converted,0.01)
- print("updateA")
- elseif ((DivXY<accXY) and (DivXYZ>=(accXYZ-Certainty)) and (not Contacts[i]["new"])) then
- ContactsUpdate(n,i,converted,-0.01)
- print("updateB")
- elseif Contacts[i]["new"] then
- if not (convertedl == 0) then
- ContactsUpdate(n,i,converted,0.01)
- Contacts[i]["new"] = false
- print("updateN")
- else
- Contacts[i] = nil
- print("updateDel")
- end
- else
- print("NoUpdate")
- if Contacts[i]["lastUpdated"] < thresholdTime then
- Contacts[i]["lastUpdated"] = Contacts[i]["lastUpdated"] + timeDif
- else
- Contacts[i]=nil
- print("del")
- end
- end
- end
- contactsEmpty = false
- while not contactsEmpty do
- contactsEmpty = true
- for iter, val in pairs(converted) do
- contactsEmpty = false
- num = string.sub(iter, 3)
- newContact(num, converted)
- break
- end
- end
- end
- function setNotUpdated()
- for i,v in pairs(Contacts) do
- Contacts[i]["updated"] = false
- end
- end
- function getDivXYZ(i,n,inList)
- PosX = Contacts[i]["PosX"]
- PosY = Contacts[i]["PosY"]
- PosZ = Contacts[i]["PosZ"]
- VectX = Contacts[i]["VectX"]
- VectY = Contacts[i]["VectY"]
- VectZ = Contacts[i]["VectZ"]
- VectXa = inList["x_"..n]-PosX
- VectYa = inList["y_"..n]-PosY
- VectZa = inList["z_"..n]-PosZ
- cos = (VectX*VectXa+VectY*VectYa+VectZ*VectZa)/(math.sqrt((VectX^2)+(VectY^2)+(VectZ^2))*math.sqrt((VectXa^2)+(VectYa^2)+(VectZa^2)))
- angle = math.acos(cos)
- return angle
- end
- function getDivXY(i,n,inList)
- PosX = Contacts[i]["PosX"]
- PosY = Contacts[i]["PosZ"]
- VectX = Contacts[i]["VectX"]
- VectY = Contacts[i]["VectZ"]
- VectXa = inList["x_"..n]-PosX
- VectYa = inList["z_"..n]-PosY
- angle = math.acos((VectX*VectXa+VectY*VectYa)/(math.sqrt((VectX^2)+(VectY^2))*math.sqrt((VectXa^2)+(VectYa^2))))
- return angle
- end
- function com(cmd)
- print(cmd)
- if cmd == "getContacts" then
- local contacts = radar.getBlocks()
- os.sleep(0.1)
- tunnel.send(serialization.serialize(contacts))
- elseif cmd == "detonateEmp" then
- if lastDetonated then
- rs.setBundledOutput(sides.front, colors.green, 255)
- os.sleep(0.1)
- rs.setBundledOutput(sides.front, colors.green, 0)
- else
- rs.setBundledOutput(sides.front, 3, 255)
- os.sleep(0.1)
- rs.setBundledOutput(sides.front, 3, 0)
- end
- lastDetonated = not lastDetonated
- tunnel.send("true")
- elseif cmd == "launchMissile" then
- local success = false
- rs.setBundledOutput(sides.front, colors.blue, 0)
- local _,_,_,_,_, x = event.pull("modem_message")
- local _,_,_,_,_, y = event.pull("modem_message")
- launchers[lastLaunched].setTarget(x,y)
- local lastLauncedStart = lastLaunched
- while (not launchers[LastLaunched].canLaunch()) do
- if lastLaunched == lastLaunchedFirst then
- break
- end
- if lastLaunched < n then
- lastLaunched = lastLaunched + 1
- else
- lastLaunched = 0
- end
- launchers[lastLaunched].setTarget(x,y)
- end
- if launchers[lastLaunched].canLaunch() then
- launchers[lastLaunched].launch()
- success = true
- end
- if lastLaunched < n then
- lastLaunched = lastLaunched + 1
- else
- lastLaunched = 0
- end
- tunnel.send(success)
- rs.setBundledOutput(sides.front, colors.blue, 255)
- elseif cmd == "setTubes" then
- local _,_,_,_,_,args = event.pull("modem_message")
- if args == "open" then
- rs.setBundledOutput(sides.front, colors.blue, 0)
- else
- rs.setBundledOutput(sides.front, colors.blue, 255)
- end
- tunnel.send("true")
- end
- com = nil
- end
- function convertRadar(inList)
- local ActualPositions = {}
- convertedl = 0
- for u,i in pairs(inList) do
- ActualPositions[u] = i
- convertedl = convertedl+1
- end
- return ActualPositions
- end
- function getClosest(inList, PosX, PosY, PosZ)
- smlDist=1000000
- for iv, v in pairs(inList) do
- i = string.sub(iv, 3)
- X = inList["x_"..i]
- Y = inList["y_"..i]
- Z = inList["z_"..i]
- dist = math.sqrt((X-PosX)^2+(Y-PosY)^2+(Z-PosZ)^2)
- if dist<smlDist then
- smlDist=dist
- smlI = i
- end
- end
- return smlI
- end
- function getProjected(n)
- PosX = Contacts[n]["PosX"]
- PosY = Contacts[n]["PosY"]
- PosZ = Contacts[n]["PosZ"]
- VectX = Contacts[n]["VectX"]
- VectY = Contacts[n]["VectY"]
- VectZ = Contacts[n]["VectZ"]
- lastUpdate = Contacts[n]["lastUpdated"]
- X = PosX + VectX*lastUpdate
- Y = PosY + VectY*lastUpdate
- Z = PosZ + VectZ*lastUpdate
- return X, Y, Z
- end
- function ContactsUpdate(im, ln, inList, crtntyIn)
- X = inList["x_"..im]
- Y = inList["y_"..im]
- Z = inList["z_"..im]
- inList["x_"..im] = nil
- inList["y_"..im] = nil
- inList["z_"..im] = nil
- convertedl = convertedl - 3
- PosX = Contacts[ln]["PosX"]
- PosY = Contacts[ln]["PosY"]
- PosZ = Contacts[ln]["PosZ"]
- crtnty = Contacts[ln]["Certainty"]
- VectX = (X-PosX)/timeDif
- VectY = (Y-PosY)/timeDif
- VectZ = (Z-PosZ)/timeDif
- PosX=X
- PosY=Y
- PosZ=Z
- Contacts[ln]["Certainty"] = crtnty + crtntyIn
- Contacts[ln]["PosX"] = PosX
- Contacts[ln]["PosY"] = PosY
- Contacts[ln]["PosZ"] = PosZ
- Contacts[ln]["VectX"] = VectX
- Contacts[ln]["VectY"] = VectY
- Contacts[ln]["VectZ"] = VectZ
- Contacts[ln]["updated"]=true
- Contacts[ln]["lastUpdated"] = 0
- end
- function newContact(n, inList)
- X = inList["x_"..n]
- Y = inList["y_"..n]
- Z = inList["z_"..n]
- nxt = tostring(hiCount)
- Contacts[nxt] = {}
- Contacts[nxt]["PosX"] = X
- Contacts[nxt]["PosY"] = Y
- Contacts[nxt]["PosZ"] = Z
- Contacts[nxt]["VectX"] = 0.0001
- Contacts[nxt]["VectY"] = 0.0001
- Contacts[nxt]["VectZ"] = 0.0001
- Contacts[nxt]["Certainty"] = initCertainty
- Contacts[nxt]["updated"]=true
- Contacts[nxt]["lastUpdated"] = 0
- Contacts[nxt]["new"] = true
- hiCount = hiCount + 1
- inList["x_"..n] = nil
- inList["y_"..n] = nil
- inList["z_"..n] = nil
- end
- local addrLauncher1 = "331255f5"
- local addrLauncher2 = "f45f0841"
- local lastLaunched = 0
- local lastDetonated = true
- Contacts={}
- hiCount = 0
- accXY = 0.17
- accXYZ = 1.5
- thresholdTime = 0.05
- lasttime = os.clock()
- initCertainty = 0.1
- math = require("math")
- component = require("component")
- event = require("event")
- sides = require("sides")
- colors = require("colors")
- serialization = require("serialization")
- tunnel = component.tunnel
- rs = component.redstone
- radar = component.radar_station
- local addrLstLauncher = component.list("defense")
- local n = 0
- local launchers = {}
- for i,v in pairs(addrLstLauncher) do
- launchers[n] = component.get(i)
- n=n+1
- end
- print(n)
- radars = radar.getBlocks()
- print("done1")
- converted = convertRadar(radars)
- print(converted["x_0"])
- print("done2")
- print(converted)
- rs.setBundledOutput(sides.front, colors.blue, 255)
- while true do
- local _,_,_,_,_,cmd = event.pull(0.2,"modem_message")
- if not cmd == nil then
- com(cmd)
- end
- track()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement