Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function widget:GetInfo() return {
- name = "Missile Pinger",
- desc = "Battlefield Awareness: Pings allied nukes, tacnukes, emps, and napalm strikes.",
- author = "_Shaman",
- date = "2020-04-16",
- license = "CC-0",
- layer = -1,
- enabled = true,
- } end
- local online = true
- local points = {}
- local circlehandler = {}
- local drawinglimit = 25 -- Spring has a limit on how many lines a widget can draw per ~15 (10? 12?) frames it seems.
- local recentallied = 0
- local watcheddefs = {}
- local silos = {}
- local lastsay = 0
- watcheddefs[UnitDefNames["staticnuke"].id] = {points = 60, radius = 910, type = "nuclear strike", life = 45}
- watcheddefs[UnitDefNames["tacnuke"].id] = {points = 16, radius = 192/2, type = "tactical nuke", life = 10}
- watcheddefs[UnitDefNames["napalmmissile"].id] = {points = 16, radius = 512/2, type = "napalm strike",life = 10}
- watcheddefs[UnitDefNames["empmissile"].id] = {points = 16, radius = 280/2, type = "EMP",life = 13}
- watcheddefs[UnitDefNames["subtacmissile"].id] = {points = 16, radius = 256/2, type = "tactical nuke",life = 13}
- local empty = {}
- local function EraseRadius(x,z,radius,points,stepnum,ending)
- local x2,y2 = 0
- local x1,y1 = 0
- local step = 360/points
- for i=stepnum, ending do
- x1 = radius * math.sin(math.rad(i*step)) + x
- y1 = radius * math.cos(math.rad(i*step)) + z
- Spring.MarkerErasePosition(x1,Spring.GetGroundHeight(x1,y1),y1)
- end
- end
- local function CreateRadius(x,z,radius,points,stepnum,ending)
- local x2,y2 = 0
- local x1,y1 = 0
- local step = 360/points
- local ox,oy = 0
- for i=stepnum, ending do
- if i == stepnum then
- x1 = radius * math.sin(math.rad(i*step)) + x
- y1 = radius * math.cos(math.rad(i*step)) + z
- if i > 1 then
- x2 = radius * math.sin(math.rad((i-1)*step)) + x
- y2 = radius * math.cos(math.rad((i-1)*step)) + z
- end
- else
- x2 = x1
- y2 = y1
- x1 = radius * math.sin(math.rad(i*step)) + x
- y1 = radius * math.cos(math.rad(i*step)) + z
- end
- if i>1 then
- Spring.MarkerAddLine(x1,Spring.GetGroundHeight(x1,y1)+700,y1,x2,Spring.GetGroundHeight(x1,y1)+700,y2)
- end
- if i == points then
- ox = radius * math.sin(math.rad(1*step)) + x
- oy = radius * math.cos(math.rad(1*step)) + z
- Spring.MarkerAddLine(x1,Spring.GetGroundHeight(x1,y1)+700,y1,ox,Spring.GetGroundHeight(x1,y1)+700,oy)
- end
- end
- end
- local function GetName(teamID)
- local name = "Unknown"
- if #Spring.GetPlayerList(teamID) > 1 then
- name = select(1,Spring.GetPlayerInfo(select(2,Spring.GetTeamInfo(teamID)))) .. "'s squad"
- else
- name = select(1,Spring.GetPlayerInfo(select(2,Spring.GetTeamInfo(teamID))))
- end
- return name
- end
- local function GetID(x,z) -- Assigns an ID to a missile strike.
- local id = x + z + Spring.GetGameFrame()
- if circlehandler[id] ~= nil then
- repeat
- id = id + 1
- until circlehandler[id] == nil
- end
- return id
- end
- function widget:UnitDestroyed(unitID)
- silos[unitID] = nil
- end
- function widget:UnitCommand(unitID, unitDefID, unitTeam, cmdID, cmdParams, cmdOpts, cmdTag, bypass)
- --Spring.Echo("Got " .. unitDefID .. "\nOnList: " .. tostring(watcheddefs[unitDefID] ~= nil))
- if watcheddefs[unitDefID] and cmdID == CMD.ATTACK then
- 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
- --Spring.Echo("added " .. unitID)
- silos[unitID] = {cmdParams[1], cmdParams[2], cmdParams[3]}
- return
- end
- local p = watcheddefs[unitDefID].points
- local type = watcheddefs[unitDefID].type
- local radius = watcheddefs[unitDefID].radius
- local life = watcheddefs[unitDefID].life
- local x,y,z = 0
- if #cmdParams == 1 then
- x,y,z = Spring.GetUnitPosition(cmdParams[1])
- else
- x = cmdParams[1]
- y = cmdParams[2]
- z = cmdParams[3]
- end
- local name = GetName(unitTeam)
- points[GetID(x,z)] = {x = x,y=y, z = z, timer = life,radius=radius,points=p}
- Spring.MarkerAddPoint(x,y,z,"Incoming allied " .. type .. "( " .. name .. ")",false)
- circlehandler[GetID(x,z)] = {x=x,z=z,points=p,current=0,radius=radius,erase=false}
- --Spring.Echo("It's a " .. UnitDefs[unitDefID].name)
- if unitDefID == UnitDefNames["staticnuke"].id then recentallied = Spring.GetGameFrame() end
- end
- end
- function widget:GameFrame(f)
- if Spring.GetGameRulesParam("recentNukeLaunch") == 1 and f < recentallied - 30 then
- Spring.SendCommands("say a:[info] WARNING: Possible enemy nuke launch detected.")
- end
- for id,_ in pairs(silos) do
- --Spring.Echo("NumCommands: " .. tostring(#Spring.GetUnitCommands(id,2)))
- 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
- --Spring.Echo("Calling UnitCommand")
- widget:UnitCommand (id, Spring.GetUnitDefID(id), Spring.GetUnitTeam(id), CMD.ATTACK, silos[id], empty, empty, true)
- silos[id] = nil
- end
- end
- drawinglimit = 25
- if f%30 == 0 then
- for id, data in pairs(points) do
- data.timer = data.timer - 1
- if data.timer == 0 then
- Spring.MarkerErasePosition(data.x,data.y,data.z)
- drawinglimit = drawinglimit - 1
- circlehandler[GetID(data.x,data.z)] = {x = data.x, z = data.z,points = data.points,radius=data.radius,current=0,erase=true}
- points[id] = nil
- end
- end
- end
- if f%10 == 0 then
- local result = 0
- for id,data in pairs(circlehandler) do
- if data.current ~= data.points then
- result = math.min(10,drawinglimit)
- result = math.min(result, data.points - data.current)
- drawinglimit = drawinglimit - result
- if not data.erase and drawinglimit > 0 then
- CreateRadius(data.x,data.z,data.radius,data.points,data.current,data.current+result)
- data.current = data.current + result
- elseif drawinglimit > 0 then
- EraseRadius(data.x,data.z,data.radius,data.points,data.current,data.current+result)
- data.current = data.current + result
- end
- else
- circlehandler[id] = nil
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement