xponen

radar_dot_line.lua

Mar 13th, 2012
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. local versionName = "v1.00"
  2. --------------------------------------------------------------------------------
  3. --------------------------------------------------------------------------------
  4. function widget:GetInfo()
  5. return {
  6. name = "Vertical Line on Radar Dots",
  7. desc = versionName .. " help you identify enemy units by adding vertical line on radar dots",
  8. author = "xponen",
  9. date = "March 14, 2012",
  10. license = "GNU GPL, v2 or later",
  11. layer = 20,
  12. enabled = false -- loaded by default?
  13. }
  14. end
  15.  
  16. local osClock = os.clock
  17. local mathCeil = math.ceil
  18. local spGetUnitPosition = Spring.GetUnitPosition
  19. local spGetGroundHeight = Spring.GetGroundHeight
  20. local spGetMyAllyTeamID = Spring.GetMyAllyTeamID
  21.  
  22. local glVertex = gl.Vertex
  23. local glPushAttrib = gl.PushAttrib
  24. local glLineStipple = gl.LineStipple
  25. local glDepthTest = gl.DepthTest
  26. local glLineWidth = gl.LineWidth
  27. local glColor = gl.Color
  28. local glBeginEnd = gl.BeginEnd
  29. local glPopAttrib = gl.PopAttrib
  30. local glCreateList = gl.CreateList
  31. --local glCallList = gl.CallList
  32. local glDeleteList = gl.DeleteList
  33. local GL_LINES = GL.LINES
  34.  
  35. local dots_gbl = {}
  36. local framePoll_gbl = {frame = 0, lastUpdate = 0} --// 1st number represent number of frame soo far, 2nd number represent the time of last update
  37. local updateAtFrame_gbl = 0
  38. local desiredDisplayInterval_gbl = 0.03
  39. local myAllyTeamID = -1
  40. -----------------------------------------
  41. function widget:Initialize()
  42. framePoll_gbl.frame = 0
  43. framePoll_gbl.lastUpdate = osClock()
  44. myAllyTeamID = spGetMyAllyTeamID()
  45. end
  46.  
  47. function widget:UnitEnteredRadar(unitID, allyTeam)
  48. if myAllyTeamID ~= allyTeam then
  49. if ( dots_gbl[unitID] == nil ) then --//a check to prevent crash in case 1: where enemy suddenly appear in LOS + Radar but LOS registered it first
  50. dots_gbl[unitID] = {position={0,0,0}, surface=0, isBelow = false, frame= 0, inLOS=false, wasInLOS = false}
  51. end
  52. end
  53. end
  54.  
  55. function widget:UnitLeftRadar(unitID, allyTeam)
  56. if myAllyTeamID ~= allyTeam then
  57. dots_gbl[unitID] = nil
  58. end
  59. end
  60.  
  61. function widget:UnitEnteredLos(unitID, allyTeam )
  62. if myAllyTeamID ~= allyTeam then
  63. if ( dots_gbl[unitID] ~= nil ) then --//a check to prevent crash in case 2: where enemy appear in LOS but ally has no radar
  64. dots_gbl[unitID].inLOS= true
  65. dots_gbl[unitID].wasInLOS = true
  66. else --//a check to prevent crash in case 1: where enemy suddenly appear in LOS + Radar but LOS registered it first
  67. dots_gbl[unitID] = {position={0,0,0}, surface=0, isBelow = false, frame= 0, inLOS=true , wasInLOS = true}
  68. end
  69. end
  70. end
  71.  
  72. function widget:UnitLeftLos(unitID, allyTeam)
  73. if myAllyTeamID ~= allyTeam then --//a check to prevent crash in case 3: where enemy died in LOS + Radar but Radar registered the death first.
  74. if ( dots_gbl[unitID] ~= nil ) then
  75. dots_gbl[unitID].inLOS= false
  76. end
  77. end
  78. end
  79. ------------------------------------------------------
  80. local function UpdateDotsContent (unitID, content) --//retrieve unit information.
  81. local x, y, z = spGetUnitPosition(unitID)
  82. if x == nil then return nil end
  83. local groundY = spGetGroundHeight(x,z)
  84. local surfaceY = mathCeil (groundY, 0) --//select surface of water, or select ground height depending on which is higher.
  85. if surfaceY > y then --//mark unit as submerged if it below surface
  86. content.isBelow = true
  87. else
  88. content.isBelow = false
  89. end
  90. content.surface = surfaceY
  91. content.position = {x,y,z}
  92.  
  93. return content
  94. end
  95.  
  96. local function GetAppropriateUpdateInterval (framePoll, updateAtFrame) --//poll 60 frame to check how long is 1 frame took
  97. local desiredDisplayInterval = desiredDisplayInterval_gbl
  98. ---- localized global variable
  99.  
  100. local currentTime = osClock()
  101. local secondPerFrame = (currentTime - framePoll.lastUpdate)/framePoll.frame
  102. framePoll.lastUpdate = currentTime
  103. local numberOfFrameForDesiredDisplayInterval = desiredDisplayInterval/secondPerFrame
  104. updateAtFrame = mathCeil(numberOfFrameForDesiredDisplayInterval,1) --//either the number of frame needed to satisfy the desired interval, or at every 1 frame (in case frame interval is longer than needed). To prevent high FPS from using too much CPU
  105.  
  106. return framePoll, updateAtFrame
  107. end
  108.  
  109. function widget:DrawWorld()
  110. local framePoll = framePoll_gbl
  111. local updateAtFrame = updateAtFrame_gbl
  112. local dots = dots_gbl
  113.  
  114. framePoll.frame = framePoll.frame +1
  115. if framePoll.frame >= 60 then --//calculate the appropriate update interval by polling delay after 60 frame
  116. framePoll, updateAtFrame = GetAppropriateUpdateInterval (framePoll, updateAtFrame)
  117. framePoll.frame = 0
  118. end
  119.  
  120. glPushAttrib(GL.LINE_BITS) --//reference: "unit_target_on_the_move.lua" (by Google Frog), ZK gadget
  121. glDepthTest(false)
  122. glLineWidth(1.4)
  123. glColor(1, 0.75, 0, 1)
  124. for unitID, content in pairs(dots) do --//iterate over all dots and draw them
  125. if content ~= nil then
  126. if content.inLOS ==false then
  127. content.frame = content.frame +1
  128. if content.frame >= updateAtFrame then --//retrieve dots info at every interval. Prevent updating more than necessary
  129. content.frame = 0
  130. content = UpdateDotsContent (unitID, content)
  131. end
  132. if content ~= nil then
  133. if content.isBelow then --//use stipple when unit is below surface. Indicate moving away from map's plane/away from user
  134. glLineStipple(1, 2047)
  135. elseif content.wasInLOS then --//add occlusion to the vertical line (when enemy was identified) for asthetic purposes
  136. glDepthTest(true)
  137. end
  138. local x,y,z = content.position[1],content.position[2],content.position[3]
  139. local surfaceY = content.surface
  140. glBeginEnd(GL_LINES, function() glVertex(x,surfaceY,z) glVertex(x,y,z) end)
  141. --glCallList(drawList)
  142. glLineStipple(false)
  143. glDepthTest(false)
  144. end
  145. dots[unitID] = content --//commit any changes to dots.
  146. end
  147. end
  148. end
  149. glColor(1,1,1,1)
  150. glPopAttrib()
  151.  
  152. ---- update global variable
  153. framePoll_gbl = framePoll
  154. updateAtFrame_gbl = updateAtFrame
  155. dots_gbl = dots
  156. end
  157. ----------------------
  158. --//reference1: "unit_ghostRadar.lua" (by very_bad_soldier), http://widgets.springrts.de/
  159. --//reference2: "unit_target_on_the_move.lua" (by Google Frog), ZK gadget
Advertisement
Add Comment
Please, Sign In to add comment