Guest User

Untitled

a guest
Sep 3rd, 2023
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.85 KB | Source Code | 0 0
  1. gN = input.getNumber
  2. m = math
  3. sin, cos = m.sin, m.cos
  4. s = screen
  5. DT = s.drawTriangle
  6.  
  7. Sensi = property.getNumber("Sensitivity") * 0.001
  8. S_Compensation = property.getBool("Speed Compensation")
  9. A_Compensation = property.getBool("Acceleration Compensation")
  10. ticks = property.getNumber("Ticks compensation")
  11. GPSbuttonToggle = property.getBool("GPS targeting mode")
  12. w, h = 32, 32
  13. o = { 0, 0, 0 }
  14. zoom, Output, offset, pointABS, point = 0, { 0, 0 }, { 0, 0, 0.892 }, o, o
  15. angOld, gpsOld, speedOld, angspeedOld, AimAngle = o, o, o, o, o
  16.  
  17. function onTick()
  18.     --input
  19.     inX, inY = gN(18), gN(19)
  20.     dist = gN(20)
  21.     ScreenClicked = input.getBool(1)
  22.     targetModule = input.getBool(2)
  23.  
  24.     -- angular stuff
  25.     local ang = { gN(4), gN(5), gN(6) }
  26.     local angspeed = VectorSub(ang, angOld)
  27.     local angacceleration = VectorSub(angspeed, angspeedOld)
  28.  
  29.     -- x y z stuff
  30.     local gps = { gN(1), gN(2), gN(3) }
  31.  
  32.     --buttons
  33.  
  34.     if targetModule then
  35.         if GPSbuttonToggle then
  36.             if hit(-1, 23, 6, 6) and noClickLastTick then GPStargeting = not GPStargeting end
  37.         else
  38.             GPStargeting = hit(-1, 23, 6, 6)
  39.         end
  40.  
  41.         pointABS = GPStargeting and { gN(21), gN(22), gN(23) } or pointABS
  42.     end
  43.  
  44.     if noClickLastTick then
  45.         if hit(-1, 13, 6, 6) then IR = not IR end
  46.         if hit(-1, 18, 6, 6) then LaserOn = not LaserOn end
  47.         if hit(-1, 8, 6, 6) then tracking = not tracking end
  48.     end
  49.     Zup, Zdown = hit(0, -1, 6, 6), hit(0, 4, 6, 6)
  50.     zoom = Counter(zoom, Zup, Zdown, 0.005, 0, 1)
  51.  
  52.     -- tick compensation
  53.     -- D = 1/2 a * t^2 + v0 * t + D0
  54.     angCompensated = ang
  55.     if S_Compensation then
  56.         angCompensated = VectorAdd(angCompensated, VectorScall(angspeed, ticks))
  57.     end
  58.     if A_Compensation then
  59.         angCompensated = VectorAdd(angCompensated, VectorScall(angacceleration, (ticks ^ 2) / 2))
  60.     end
  61.  
  62.     --the real deal
  63.     if tracking or GPStargeting then
  64.         point = VectorSub(VectorRot(XYZ(angCompensated), VectorSub(pointABS, gps)), offset)
  65.         --[[    - Substracts pod coordinates to the point coordinates (-> gets the vector in the global frame)
  66.         - Apply reversed rotation to get the vector in the local frame
  67.         - Adjust the offset between the laser and the sensor
  68. ]]
  69.         dist2 = (point[1] ^ 2 + point[3] ^ 2 + point[2] ^ 2) ^ 0.5
  70.         AimAngle = { m.atan(point[1], point[3]), m.asin(point[2] / dist2), m.asin((point[2] + 0.25) / dist2) }
  71.         --- Azimuth, Elevation, adjusted Elevation for the camera
  72.  
  73.         Output = VectorScall(AimAngle, 4 / m.pi)
  74.         --- convertion from radians to gimbal parts rotation
  75.     end
  76.     canAim = ScreenClicked and (inX > 6)
  77.     --- locks aiming if a button is pressed
  78.     if canAim or not tracking then
  79.         if canAim then
  80.             local r = Sensi / (zoom * 5 + 1)
  81.             Output = { clamp(Output[1] + ((inX * 2 - w) / w * r), -1, 1),
  82.                 clamp(Output[2] - ((inY * 2 - h) / h * r), -1, 1) }
  83.             --- adds or substracts to the Azimut (1) or Elevation (2) based on the distance to the center of the screen and zooming
  84.  
  85.             AimAngle = VectorScall(Output, m.pi / 4)
  86.             --- convertion from gimbal parts rotation to radians
  87.         end
  88.  
  89.         point    = { sin(AimAngle[1]) * cos(AimAngle[2]) * dist, sin(AimAngle[2]) * dist,
  90.             cos(AimAngle[1]) * cos(AimAngle[2]) * dist }
  91.         pointABS = VectorAdd(VectorRot(ZYX(angCompensated), VectorAdd(point, offset)), gps)
  92.         --[[    -  Adjust the offset between the laser and the sensor
  93.         - Apply rotation to get the vector in the global frame
  94.         - Add pod coordinates to the vector (-> gets the point gps)
  95. ]]
  96.         Output[3] = LaserOn and m.atan(point[2], point[3]+0.25) * 4 / m.pi or Output[2]
  97.         --- Adjusted Elevation for the camera
  98.     end
  99.  
  100.     -- outputs
  101.     Out = { Output[1], Output[2], Output[3], pointABS[1], pointABS[2], pointABS[3], zoom }
  102.     for i = 1, #Out do
  103.         output.setNumber(i, Out[i])
  104.     end
  105.  
  106.     output.setBool(1, LaserOn)
  107.     output.setBool(2, IR)
  108.  
  109.     --tick variables
  110.     angOld = ang
  111.     angspeedOld = angspeed
  112.  
  113.  
  114.     noClickLastTick = not ScreenClicked
  115. end
  116.  
  117. function onDraw()
  118.     w = s.getWidth()
  119.     h = s.getHeight()
  120.  
  121.     s.setColor(0, 255, 0, 225)
  122.     s.drawLine(13, 15, 15, 15)
  123.     s.drawLine(18, 15, 20, 15)
  124.     s.drawLine(16, 12, 16, 14)
  125.     s.drawLine(16, 17, 16, 19)
  126.  
  127.     s.drawLine(28, 10, 28, 30)
  128.     s.drawLine(28, 10, 30, 10)
  129.     s.drawLine(28, 30, 30, 30)
  130.  
  131.     zoomlevel = 29 - zoom * 18
  132.     s.drawLine(29, zoomlevel, 30, zoomlevel)
  133.  
  134.     dist_display = math.floor(dist)
  135.     dStr(32 - string.len(dist_display) * 4, 1, dist_display)
  136.  
  137.     s.setColor(10, 10, 10)
  138.     s.drawRectF(0, 0, 5, 32)
  139.     s.setColor(5, 5, 5)
  140.     s.drawRectF(0, 1, 5, 3)
  141.     s.drawRectF(0, 5, 5, 3)
  142.     for i = 1, targetModule and 4 or 3 do s.drawRectF(0, 4 + 5 * i, 5, 4) end
  143.  
  144.     s.setColor(50, 50, 50, Zup and 150 or 255) --Z UP
  145.     s.drawLine(2, 1, 2, 4)
  146.     s.drawLine(1, 2, 4, 2)
  147.  
  148.     s.setColor(50, 50, 50, Zdown and 150 or 255) -- ZDown
  149.     s.drawLine(1, 6, 4, 6)
  150.  
  151.     s.setColor(50, 50, 50, tracking and 150 or 255) --tracking
  152.     dStr(1, 9, "T")
  153.  
  154.     s.setColor(50, 50, 50, IR and 150 or 255) --IR
  155.     dStr(2, 14, "R")
  156.     s.drawLine(0, 14, 0, 18)
  157.  
  158.     s.setColor(50, 50, 50, LaserOn and 150 or 255) --Laser
  159.     dStr(1, 19, "L")
  160.  
  161.     if targetModule then
  162.         s.setColor(50, 50, 50, GPStargeting and 150 or 255)
  163.         dStr(1, 24, "C")
  164.     end
  165.  
  166.     s.setColor(50, 50, 50, ScreenClicked and (inX > 6) and 200 or 0)
  167.     s.drawCircle(inX, inY, 2)
  168. end
  169.  
  170. --- Rotation Matrix
  171. function ZYX(angtable)
  172.     local sx, sy, sz, cx, cy, cz = m.sin(angtable[1]), m.sin(angtable[2]), m.sin(angtable[3]), m.cos(angtable[1]),
  173.         m.cos(angtable[2]), m.cos(angtable[3])
  174.     rotationMatrix = {
  175.         { cz * cy, -sz * cx + cz * sx * sy, sz * sx + cz * sy * cx },
  176.         { sz * cy, cz * cx + sz * sy * sx,  -sx * cz + sz * sy * cx },
  177.         { -sy,     cy * sx,                 cx * cy }
  178.     }
  179.     return rotationMatrix
  180. end
  181.  
  182. function XYZ(angtable)
  183.     local sx, sy, sz, cx, cy, cz = m.sin(angtable[1]), m.sin(angtable[2]), m.sin(angtable[3]), m.cos(angtable[1]),
  184.         m.cos(angtable[2]), m.cos(angtable[3])
  185.     rotationMatrix = {
  186.         { cz * cy,                 cy * sz,                 -sy },
  187.         { -sz * cx + cz * sy * sx, cz * cx + sz * sy * sx,  sx * cy },
  188.         { sx * sz + cx * sy * cz,  -sx * cz + cx * sy * sz, cx * cy }
  189.     }
  190.     return rotationMatrix
  191. end
  192.  
  193. --- Vector Stuff
  194. VectorRot = function(rotmatrix, vector) --Assuming matrix multiplication is possible
  195.     local res = {}
  196.  
  197.     for i = 1, #rotmatrix do
  198.         res[i] = 0
  199.         for k = 1, #vector do
  200.             res[i] = res[i] + rotmatrix[i][k] * vector[k]
  201.         end
  202.     end
  203.  
  204.     return res
  205. end
  206.  
  207. function VectorAdd(Vec1, Vec2)
  208.     return { Vec1[1] + Vec2[1], Vec1[2] + Vec2[2], Vec1[3] + Vec2[3] }
  209. end
  210.  
  211. function VectorSub(Vec1, Vec2)
  212.     return { Vec1[1] - Vec2[1], Vec1[2] - Vec2[2], Vec1[3] - Vec2[3] }
  213. end
  214.  
  215. function VectorScall(Vec, factor)
  216.     return { Vec[1] * factor, Vec[2] * factor, Vec[3] and Vec[3] * factor or nil }
  217. end
  218.  
  219. --- User interface stuff
  220. function hit(x, y, w, h)
  221.     return inX > x and inY > y and inX < x + w and inY < y + h and ScreenClicked
  222. end
  223.  
  224. function Counter(value, Up, Down, Reason, min, max)
  225.     value = value + (Up and Reason or 0) - (Down and Reason or 0)
  226.     return clamp(value, min, max)
  227. end
  228.  
  229. --- mini Text function
  230. font34 = {
  231.     0x0D0, 0xC0C, 0xFAF, 0x2F4, 0xB2D, 0x6F5, 0x0C0, 0x690, 0x096, 0xAEA, 0x4E4, 0x560, 0x444, 0x010, 0x168, 0x79E,
  232.     0x5F1,
  233.     0x9B5, 0x9DA, 0x6F2, 0xDDA, 0x6DA, 0x9AC, 0x3FC, 0x4A7, 0x050, 0x1A0, 0x44A, 0xAAA, 0xA44, 0xA41, 0x69D, 0x7A7,
  234.     0xFD6,
  235.     0x699, 0xF96, 0xFD9, 0xFA8, 0x69B, 0xF2F, 0x9F9, 0x19E, 0xF4B, 0xF11, 0xF4F, 0xF6F, 0x696, 0xFA4, 0x6B7, 0xFA5,
  236.     0x5BA,
  237.     0x8F8, 0xE1E, 0xC3C, 0xF5F, 0x969, 0xC7C, 0xBD9, 0xF90, 0x861, 0x09F, 0x484, 0x111, 0x084, 0x2F9, 0x0F0, 0x9F4, 0x462 }
  238. function dDot(x, y)
  239.     s.drawLine(x, y, x, y + 1)
  240. end
  241.  
  242. function dChar(x, y, char)
  243.     c = string.byte(string.upper(char)) - 32
  244.     if c > 64 then c = c - 26 end
  245.     if c > 0 and c < 69 then
  246.         for i = 0, 11 do
  247.             if font34[c] & (1 << (11 - i)) > 0 then dDot(x + i // 4, y + i % 4) end
  248.         end
  249.     end
  250. end
  251.  
  252. function dStr(x, y, str)
  253.     cs = string.len(str)
  254.     for i = 0, cs - 1 do
  255.         dChar(x + 4 * i, y, string.sub(str, i + 1, i + 1))
  256.     end
  257. end
  258.  
  259. function clamp(value, min, max)
  260.     return math.min(math.max(value, min), max)
  261. end
  262.  
Advertisement
Add Comment
Please, Sign In to add comment