Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2021
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.10 KB | None | 0 0
  1. function widget:GetInfo() return {
  2.     name    = "Missile Pinger",
  3.     desc    = "Battlefield Awareness: Pings allied nukes, tacnukes, emps, and napalm strikes.",
  4.     author  = "_Shaman",
  5.     date    = "2020-04-16",
  6.     license = "CC-0",
  7.     layer   = -1,
  8.     enabled = true,
  9. } end
  10.  
  11. local online = true
  12. local points = {}
  13. local circlehandler = {}
  14. local drawinglimit = 25 -- Spring has a limit on how many lines a widget can draw per ~15 (10? 12?) frames it seems.
  15. local recentallied = 0
  16. local watcheddefs = {}
  17. local silos = {}
  18. local lastsay = 0
  19. watcheddefs[UnitDefNames["staticnuke"].id] = {points = 60, radius = 910, type = "nuclear strike", life = 45}
  20. watcheddefs[UnitDefNames["tacnuke"].id] = {points = 16, radius = 192/2, type = "tactical nuke", life = 10}
  21. watcheddefs[UnitDefNames["napalmmissile"].id] = {points = 16, radius = 512/2, type = "napalm strike",life = 10}
  22. watcheddefs[UnitDefNames["empmissile"].id] = {points = 16, radius = 280/2, type = "EMP",life = 13}
  23. watcheddefs[UnitDefNames["subtacmissile"].id] = {points = 16, radius = 256/2, type = "tactical nuke",life = 13}
  24. local empty = {}
  25.  
  26. local function EraseRadius(x,z,radius,points,stepnum,ending)
  27.     local x2,y2 = 0
  28.     local x1,y1 = 0
  29.     local step = 360/points
  30.     for i=stepnum, ending do
  31.         x1 = radius * math.sin(math.rad(i*step)) + x
  32.         y1 = radius * math.cos(math.rad(i*step)) + z
  33.         Spring.MarkerErasePosition(x1,Spring.GetGroundHeight(x1,y1),y1)
  34.     end
  35. end
  36.  
  37.  
  38.  
  39. local function CreateRadius(x,z,radius,points,stepnum,ending)
  40.     local x2,y2 = 0
  41.     local x1,y1 = 0
  42.     local step = 360/points
  43.     local ox,oy = 0
  44.     for i=stepnum, ending do
  45.         if i == stepnum then
  46.             x1 = radius * math.sin(math.rad(i*step)) + x
  47.             y1 = radius * math.cos(math.rad(i*step)) + z
  48.             if i > 1 then
  49.                 x2 = radius * math.sin(math.rad((i-1)*step)) + x
  50.                 y2 = radius * math.cos(math.rad((i-1)*step)) + z
  51.             end
  52.         else
  53.             x2 = x1
  54.             y2 = y1
  55.             x1 = radius * math.sin(math.rad(i*step)) + x
  56.             y1 = radius * math.cos(math.rad(i*step)) + z
  57.         end
  58.         if i>1 then
  59.             Spring.MarkerAddLine(x1,Spring.GetGroundHeight(x1,y1)+700,y1,x2,Spring.GetGroundHeight(x1,y1)+700,y2)
  60.         end
  61.         if i == points then
  62.             ox = radius * math.sin(math.rad(1*step)) + x
  63.             oy = radius * math.cos(math.rad(1*step)) + z
  64.             Spring.MarkerAddLine(x1,Spring.GetGroundHeight(x1,y1)+700,y1,ox,Spring.GetGroundHeight(x1,y1)+700,oy)
  65.         end
  66.     end
  67. end
  68.  
  69. local function GetName(teamID)
  70.     local name = "Unknown"
  71.     if #Spring.GetPlayerList(teamID) > 1 then
  72.         name = select(1,Spring.GetPlayerInfo(select(2,Spring.GetTeamInfo(teamID)))) .. "'s squad"
  73.     else
  74.         name = select(1,Spring.GetPlayerInfo(select(2,Spring.GetTeamInfo(teamID))))
  75.     end
  76.     return name
  77. end
  78.  
  79. local function GetID(x,z) -- Assigns an ID to a missile strike.
  80.     local id = x + z + Spring.GetGameFrame()
  81.     if circlehandler[id] ~= nil then
  82.         repeat
  83.             id = id + 1
  84.         until circlehandler[id] == nil
  85.     end
  86.     return id
  87. end
  88.  
  89. function widget:UnitDestroyed(unitID)
  90.     silos[unitID] = nil
  91. end
  92.  
  93. function widget:UnitCommand(unitID, unitDefID, unitTeam, cmdID, cmdParams, cmdOpts, cmdTag, bypass)
  94.     --Spring.Echo("Got " .. unitDefID .. "\nOnList: " .. tostring(watcheddefs[unitDefID] ~= nil))
  95.     if watcheddefs[unitDefID] and cmdID == CMD.ATTACK then
  96.         if ((unitDefID == UnitDefNames["staticnuke"].id or unitDefID == UnitDefNames["subtacmissile"].id) and (select(1,Spring.GetUnitStockpile(unitID)) == 0 and type(bypass) ~= "boolean")) or select(5,Spring.GetUnitHealth(unitID)) < 1.0 then
  97.             --Spring.Echo("added " .. unitID)
  98.             silos[unitID] = {cmdParams[1], cmdParams[2], cmdParams[3]}
  99.             return
  100.         end
  101.        
  102.         local p = watcheddefs[unitDefID].points
  103.         local type = watcheddefs[unitDefID].type
  104.         local radius = watcheddefs[unitDefID].radius
  105.         local life = watcheddefs[unitDefID].life
  106.         local x,y,z = 0
  107.         if #cmdParams == 1 then
  108.             x,y,z = Spring.GetUnitPosition(cmdParams[1])
  109.         else
  110.             x = cmdParams[1]
  111.             y = cmdParams[2]
  112.             z = cmdParams[3]
  113.         end
  114.         local name = GetName(unitTeam)
  115.         points[GetID(x,z)] = {x = x,y=y, z = z, timer = life,radius=radius,points=p}
  116.         Spring.MarkerAddPoint(x,y,z,"Incoming allied " .. type .. "( " .. name .. ")",false)
  117.         circlehandler[GetID(x,z)] = {x=x,z=z,points=p,current=0,radius=radius,erase=false}
  118.         --Spring.Echo("It's a " .. UnitDefs[unitDefID].name)
  119.         if unitDefID == UnitDefNames["staticnuke"].id then recentallied = Spring.GetGameFrame() end
  120.     end
  121. end
  122.  
  123. function widget:GameFrame(f)
  124.     if Spring.GetGameRulesParam("recentNukeLaunch") == 1 and f < recentallied - 30 then
  125.         Spring.SendCommands("say a:[info] WARNING: Possible enemy nuke launch detected.")
  126.     end
  127.     for id,_ in pairs(silos) do
  128.         --Spring.Echo("NumCommands: " .. tostring(#Spring.GetUnitCommands(id,2)))
  129.         if ((UnitDefNames["staticnuke"].id == Spring.GetUnitDefID(id) or UnitDefNames["subtacmissile"].id) and #Spring.GetUnitCommands(id,1) == 0) or (UnitDefNames["staticnuke"].id ~=  Spring.GetUnitDefID(id) and select(5,Spring.GetUnitHealth(id))) == 1.0 then
  130.             --Spring.Echo("Calling UnitCommand")
  131.             widget:UnitCommand (id, Spring.GetUnitDefID(id), Spring.GetUnitTeam(id), CMD.ATTACK, silos[id], empty, empty, true)
  132.             silos[id] = nil
  133.         end
  134.     end
  135.     drawinglimit = 25
  136.     if f%30 == 0 then
  137.         for id, data in pairs(points) do
  138.             data.timer = data.timer - 1
  139.             if data.timer == 0 then
  140.                 Spring.MarkerErasePosition(data.x,data.y,data.z)
  141.                 drawinglimit = drawinglimit - 1
  142.                 circlehandler[GetID(data.x,data.z)] = {x = data.x, z = data.z,points = data.points,radius=data.radius,current=0,erase=true}
  143.                 points[id] = nil
  144.             end
  145.         end
  146.     end
  147.     if f%10 == 0 then
  148.         local result = 0
  149.         for id,data in pairs(circlehandler) do
  150.             if data.current ~= data.points then
  151.                 result = math.min(10,drawinglimit)
  152.                 result = math.min(result, data.points - data.current)
  153.                 drawinglimit = drawinglimit - result
  154.                 if not data.erase and drawinglimit > 0 then
  155.                     CreateRadius(data.x,data.z,data.radius,data.points,data.current,data.current+result)
  156.                     data.current = data.current + result
  157.                 elseif drawinglimit > 0 then
  158.                     EraseRadius(data.x,data.z,data.radius,data.points,data.current,data.current+result)
  159.                     data.current = data.current + result
  160.                 end
  161.             else
  162.                 circlehandler[id] = nil
  163.             end
  164.         end
  165.     end
  166. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement