Advertisement
Guest User

[ICMB] fialed code with notes

a guest
Nov 6th, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.43 KB | None | 0 0
  1. --[[
  2.     real time parth prediction
  3. ]]--
  4.  
  5. local targets = {}
  6.  
  7. local function addFile(fileParth,sDATA) -- saves to file
  8.     file = fs.open(fileParth,"a")
  9.     if file then
  10.         file.write(sDATA.."\n")
  11.         file:close()
  12.         return true
  13.     end
  14. end
  15. local function openDevice(sType) -- finds and activats a device like radar or launcher then returns functions
  16.     for i,v in pairs(rs.getSides()) do
  17.         if peripheral.isPresent(v) and peripheral.getType(v) == sType then
  18.             return peripheral.wrap(v),v
  19.         end
  20.     end
  21. end
  22.  
  23. local radar = openDevice("ICBMRadar")
  24.  
  25. 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.
  26.    
  27.     local gravity = 9.80665
  28.    
  29.     local lastV
  30.    
  31.     local count = 0
  32.    
  33.     local totalX = 0
  34.     local totalY = 0
  35.     local tabZ = {}
  36.    
  37.     for i,v in pairs(data) do -- gets the speed of X and Y movment also Z but z it the probblem
  38.         if i ~= "t" then
  39.             if lastV then -- v[1] v[2] v[3]
  40.                 totalX = totalX + (v[1] - lastV[1])
  41.                 totalY = totalY + (v[3] - lastV[3])
  42.                 table.insert(tabZ,v[2] - lastV[2])
  43.                 lastV = v
  44.                 count = count + 1
  45.             else
  46.                 lastV = v
  47.             end
  48.         end
  49.     end
  50.    
  51.     local avrZ = 0
  52.    
  53.     for i,v in pairs(tabZ) do
  54.         -- avrZ = avrZ - (v - ((i-1)*(gravity * 0.1)))
  55.         avrZ = avrZ + v
  56.     end
  57.    
  58.     local driftX = totalX/count --  gets average by deviding
  59.     local driftY = totalY/count
  60.     local driftZ = avrZ/count
  61.    
  62.     -- print("drift X: "..driftX.." Y: "..driftY.." Z: "..driftZ)
  63.    
  64.     -- print("Running simulation")
  65.    
  66.     local deBug = false -- turns on deBug mode
  67.    
  68.     local mid = math.ceil(#data/2)
  69.    
  70.     local mis = {}
  71.     mis.x = data[mid][1] -- these add the data into the simulation
  72.     mis.y = data[mid][3]
  73.     mis.z = data[mid][2]
  74.     mis.rateX = driftX
  75.     mis.rateY = driftY
  76.     mis.accZ = driftZ
  77.    
  78.     local nTime = 0
  79.    
  80.     local lineX,lineY = term.getCursorPos()
  81.    
  82.     if deBug then
  83.         addFile("chk","--- START ---")
  84.     end
  85.    
  86.     while mis.z > 0 do -- this runs a simulation of flight parth based on know data
  87.         mis.x = mis.x + mis.rateX
  88.         mis.y = mis.y + mis.rateY
  89.         mis.z = mis.z + mis.accZ
  90.         mis.accZ = mis.accZ - ((gravity * 0.1)/60) -- this increments the Z acceleration appling gravity PROBBLEM IS HERE
  91.         if deBug then
  92.             term.setCursorPos(lineX,lineY)
  93.             write("T: "..nTime.." X: "..mis.x.." Y: "..mis.y.." Z: "..mis.z)
  94.             addFile("chk",mis.x.." "..mis.y.." "..mis.z)
  95.             sleep(0.1)
  96.         end
  97.        
  98.         nTime = nTime + 1 -- keeps track of loop N.O
  99.        
  100.     end
  101.    
  102.     if deBug then -- prints and saves to file
  103.         print()
  104.         addFile("chk","---  END  ---")
  105.     end
  106.    
  107.     return mis.x,mis.y,mis.z -- returns the position of impact
  108.    
  109. end
  110.  
  111.  
  112.  
  113. local posX,posY = term.getCursorPos()
  114.  
  115. while true do -- loops collecting data and cleaning it up then sending it into the above code to for parth prediction
  116.     local data = {pcall(radar.getMissiles)} -- radar.getMissiles causes crash if there is no power this bypasses that using pcall
  117.     if data[1] == true then
  118.         table.remove(data,1)
  119.         if math.fmod(#data,3) == 0 then
  120.             for i = 0,#data-1,3 do
  121.                 if #targets == 0 then
  122.                     targets[1] = {{data[i+1],data[i+2],data[i+3]},t = os.clock()}
  123.                 else
  124.                     local flag = false
  125.                     for a = 1,#targets do -- 10 meater tollerance
  126.                         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
  127.                             table.insert(targets[a],{data[i+1],data[i+2],data[i+3],t = os.clock()})
  128.                             flag = true
  129.                             break
  130.                         end
  131.                     end
  132.                     if not flag then
  133.                         table.insert(targets,{{data[i+1],data[i+2],data[i+3],t = os.clock()}})
  134.                     end
  135.                 end
  136.             end
  137.         end
  138.        
  139.         local now = os.clock()
  140.         local loop = 1
  141.        
  142.         while true do -- removes old entries in the target table
  143.             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
  144.                 table.remove(targets,loop)
  145.             else
  146.                 if targets[loop] then
  147.                     print(table.concat({target(targets[loop])}," "))
  148.                 end
  149.                 loop = loop + 1
  150.             end
  151.             if loop > #targets then
  152.                 break
  153.             end
  154.             loop = loop + 1
  155.         end
  156.     else
  157.         term.setCursorPos(0,posY)
  158.         term.clearLine()
  159.         print("Power LOW") -- tells you there is a probblem with power
  160.     end
  161.    
  162.     print("targets "..#targets)
  163.     for i = 1,#targets do
  164.         write(#targets[i].." ")
  165.     end
  166.     print()
  167.    
  168.     sleep(0.1)
  169. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement