Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- real time parth prediction
- ]]--
- local targets = {}
- local function addFile(fileParth,sDATA) -- saves to file
- file = fs.open(fileParth,"a")
- if file then
- file.write(sDATA.."\n")
- file:close()
- return true
- end
- end
- local function openDevice(sType) -- finds and activats a device like radar or launcher then returns functions
- for i,v in pairs(rs.getSides()) do
- if peripheral.isPresent(v) and peripheral.getType(v) == sType then
- return peripheral.wrap(v),v
- end
- end
- end
- local radar = openDevice("ICBMRadar")
- local function target(data) -- 1 = X 2 = Z 3 = Y -- take data set and works out trejectory jusigna simulation at 0.1 frmaes per second.
- local gravity = 9.80665
- local lastV
- local count = 0
- local totalX = 0
- local totalY = 0
- local tabZ = {}
- for i,v in pairs(data) do -- gets the speed of X and Y movment also Z but z it the probblem
- if i ~= "t" then
- if lastV then -- v[1] v[2] v[3]
- totalX = totalX + (v[1] - lastV[1])
- totalY = totalY + (v[3] - lastV[3])
- table.insert(tabZ,v[2] - lastV[2])
- lastV = v
- count = count + 1
- else
- lastV = v
- end
- end
- end
- local avrZ = 0
- for i,v in pairs(tabZ) do
- -- avrZ = avrZ - (v - ((i-1)*(gravity * 0.1)))
- avrZ = avrZ + v
- end
- local driftX = totalX/count -- gets average by deviding
- local driftY = totalY/count
- local driftZ = avrZ/count
- -- print("drift X: "..driftX.." Y: "..driftY.." Z: "..driftZ)
- -- print("Running simulation")
- local deBug = false -- turns on deBug mode
- local mid = math.ceil(#data/2)
- local mis = {}
- mis.x = data[mid][1] -- these add the data into the simulation
- mis.y = data[mid][3]
- mis.z = data[mid][2]
- mis.rateX = driftX
- mis.rateY = driftY
- mis.accZ = driftZ
- local nTime = 0
- local lineX,lineY = term.getCursorPos()
- if deBug then
- addFile("chk","--- START ---")
- end
- while mis.z > 0 do -- this runs a simulation of flight parth based on know data
- mis.x = mis.x + mis.rateX
- mis.y = mis.y + mis.rateY
- mis.z = mis.z + mis.accZ
- mis.accZ = mis.accZ - ((gravity * 0.1)/60) -- this increments the Z acceleration appling gravity PROBBLEM IS HERE
- if deBug then
- term.setCursorPos(lineX,lineY)
- write("T: "..nTime.." X: "..mis.x.." Y: "..mis.y.." Z: "..mis.z)
- addFile("chk",mis.x.." "..mis.y.." "..mis.z)
- sleep(0.1)
- end
- nTime = nTime + 1 -- keeps track of loop N.O
- end
- if deBug then -- prints and saves to file
- print()
- addFile("chk","--- END ---")
- end
- return mis.x,mis.y,mis.z -- returns the position of impact
- end
- local posX,posY = term.getCursorPos()
- while true do -- loops collecting data and cleaning it up then sending it into the above code to for parth prediction
- local data = {pcall(radar.getMissiles)} -- radar.getMissiles causes crash if there is no power this bypasses that using pcall
- if data[1] == true then
- table.remove(data,1)
- if math.fmod(#data,3) == 0 then
- for i = 0,#data-1,3 do
- if #targets == 0 then
- targets[1] = {{data[i+1],data[i+2],data[i+3]},t = os.clock()}
- else
- local flag = false
- for a = 1,#targets do -- 10 meater tollerance
- if data[i+1] < targets[a][#targets[a]][1] + 10 and data[i+1] < targets[a][#targets[a]][1] + 10 and data[i+2] < targets[a][#targets[a]][2] + 10 and data[i+3] > targets[a][#targets[a]][3] - 10 and data[i+2] > targets[a][#targets[a]][2] - 10 and data[i+3] > targets[a][#targets[a]][3] - 10 then
- table.insert(targets[a],{data[i+1],data[i+2],data[i+3],t = os.clock()})
- flag = true
- break
- end
- end
- if not flag then
- table.insert(targets,{{data[i+1],data[i+2],data[i+3],t = os.clock()}})
- end
- end
- end
- end
- local now = os.clock()
- local loop = 1
- while true do -- removes old entries in the target table
- if targets[loop] and targets[loop][#targets[loop]].t and (targets[loop][#targets[loop]].t+1 < now or targets[loop][#targets[loop]].t - 1 > now) then
- table.remove(targets,loop)
- else
- if targets[loop] then
- print(table.concat({target(targets[loop])}," "))
- end
- loop = loop + 1
- end
- if loop > #targets then
- break
- end
- loop = loop + 1
- end
- else
- term.setCursorPos(0,posY)
- term.clearLine()
- print("Power LOW") -- tells you there is a probblem with power
- end
- print("targets "..#targets)
- for i = 1,#targets do
- write(#targets[i].." ")
- end
- print()
- sleep(0.1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement