Guest User

Fog of War

a guest
Jul 2nd, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. function widget:GetInfo()
  2. return {
  3. name = "Fog of War",
  4. desc = "Draw fog of war",
  5. author = "Knorke",
  6. date = "Mar 2011, 2011",
  7. license = "Public Domain",
  8. layer = -3,
  9. enabled = false
  10. }
  11. end
  12.  
  13. local DspLst=nil
  14. local res = 152 --75 50 to 200, multiple of 8 work better eg 64
  15. local TileMaxX = Game.mapSizeX/res +1
  16. local TileMaxZ = Game.mapSizeZ/res +1
  17. local unexplored = 0
  18. local visible = 1
  19. local explored = 2
  20. local FOG = {}
  21. local localAllyID = Spring.GetLocalAllyTeamID ()
  22. local updateRate = 15 --15 to 30,lower=faster updating
  23. local fogAlpha = 1 --0.7
  24. ---magical speedups---
  25. local Spring_GetGroundHeight = Spring.GetGroundHeight
  26. local Spring_IsPosInLos = Spring.IsPosInLos
  27. local gl_Vertex = gl.Vertex
  28. local gl_Color = gl.Color
  29. local gl_CreateList = gl.CreateList
  30. local gl_BeginEnd = gl.BeginEnd
  31. local gl_DepthMask = gl.DepthMask
  32. ----------------------
  33.  
  34. local function updateFOG()
  35. for x=1,TileMaxX do
  36. mx=x*res
  37. for z=1,TileMaxZ do
  38. mz=z*res
  39. local los = Spring_IsPosInLos (mx, Spring_GetGroundHeight(x,z), mz, localAllyID)
  40. if (los==true) then FOG[x][z] = 1 end
  41. if (los==false) then
  42. if (FOG[x][z]==visible) then FOG[x][z] = explored end
  43. --FOG[x][z] = 2
  44. end
  45. end
  46. end
  47. end
  48.  
  49. function widget:GameFrame (f)
  50. if (f%updateRate==0) then
  51. updateFOG()
  52. ForceRefresh()
  53. end
  54. end
  55.  
  56. function widget:Initialize (f)
  57. checkToDisable ()
  58. for x=1,TileMaxX do
  59. FOG[x] = {}
  60. for z=1,TileMaxZ do
  61. FOG[x][z] = unexplored --0
  62. end
  63. end
  64. updateFOG()
  65. end
  66.  
  67. local function TilesVerticesOutside(h)
  68. for x=1,TileMaxX,1 do
  69. for z=1,TileMaxZ,1 do
  70. if (x >= 1 and z >= 1 and x < TileMaxX and z < TileMaxZ) then
  71. --if (false) then
  72. if (FOG[x][z]~=nil) then
  73. if (FOG[x][z] == unexplored) then gl_Color(0,0,0,fogAlpha/2) end
  74. if (FOG[x][z] == explored) then gl_Color(0,0,0,fogAlpha/2) end
  75. if (FOG[x][z] ~= visible) then
  76. gl_Vertex(res*(x-1),h+Spring.GetGroundHeight(res*(x-1),res*z),res*z)
  77. gl_Vertex(res*x,h+Spring_GetGroundHeight(res*x,res*z),res*z)
  78. gl_Vertex(res*x,h+Spring_GetGroundHeight(res*x,res*(z-1)),res*(z-1))
  79. gl_Vertex(res*(x-1),h+Spring_GetGroundHeight(res*(x-1),res*(z-1)),res*(z-1))
  80. end
  81. end
  82. else
  83. local a = math.random (0,10)/64 --math.abs (math.sin (x*z))
  84. gl_Color(a,a,a,fogAlpha) --a,a,a for black&white
  85. gl_Vertex(res*(x-1),Spring.GetGroundHeight(res*(x-1),res*z),res*z)
  86. gl_Vertex(res*x,Spring_GetGroundHeight(res*x,res*z),res*z)
  87. gl_Vertex(res*x,Spring_GetGroundHeight(res*x,res*(z-1)),res*(z-1))
  88. gl_Vertex(res*(x-1),Spring_GetGroundHeight(res*(x-1),res*(z-1)),res*(z-1))
  89. end
  90. end
  91. end
  92. end
  93.  
  94. local function TilesVertices(h)
  95. for x=1,TileMaxX do
  96. for z=1,TileMaxZ do
  97. if (FOG[x][z]~=nil) then
  98. if (FOG[x][z] == unexplored) then gl_Color(0,0,0,fogAlpha) end
  99. --if (FOG[x][z] == visible) then gl_Color(0,0,0,0) end
  100. if (FOG[x][z] == explored) then gl_Color(0,0,0,fogAlpha/2) end
  101. if (FOG[x][z] ~= visible) then
  102. --if (x > TileMaxX or z > TileMaxZ) then
  103. local a = math.random (0,10)/10
  104. gl_Color(a,a,a,fogAlpha)
  105. --end
  106. gl_Vertex(res*(x-1),h+Spring.GetGroundHeight(res*(x-1),res*z),res*z)
  107. gl_Vertex(res*x,h+Spring_GetGroundHeight(res*x,res*z),res*z)
  108. gl_Vertex(res*x,h+Spring_GetGroundHeight(res*x,res*(z-1)),res*(z-1))
  109. gl_Vertex(res*(x-1),h+Spring_GetGroundHeight(res*(x-1),res*(z-1)),res*(z-1))
  110. end
  111. end
  112. end
  113. end
  114. end
  115.  
  116. local function DrawTiles()
  117. gl.PushAttrib(GL.ALL_ATTRIB_BITS)
  118. gl.DepthTest(true)
  119. gl_DepthMask(true)
  120. --gl.Texture(":a:foggy.png")
  121. --r = math.random(0,100)/100
  122. gl_Color(1,1,1,1)
  123. --gl_BeginEnd(GL.QUADS,TilesVertices,8)
  124. gl_BeginEnd(GL.QUADS,TilesVerticesOutside,50)
  125. --gl_BeginEnd(GL.TRIANGLES,TilesVertices,8)
  126. gl.Texture(false)
  127. gl_DepthMask(false)
  128. gl.DepthTest(false)
  129. gl_Color(1,1,1,1)
  130. gl.PopAttrib()
  131. end
  132.  
  133. --function widget:DrawWorldPreUnit()
  134. function widget:DrawWorld()
  135. --function widget:DrawInMiniMap()
  136. if not DspLst then
  137. DspLst=gl_CreateList(DrawTiles)
  138. end
  139. gl.CallList(DspLst)-- Or maybe you want to keep it cached but not draw it everytime.
  140. -- Maybe you want Spring.SetDrawGround(false) somewhere
  141. end
  142.  
  143.  
  144. function ForceRefresh()
  145. if DspLst then
  146. gl.DeleteList(DspLst)
  147. DspLst=nil
  148. end
  149. end
  150.  
  151.  
  152. function checkToDisable ()
  153. if (Spring.IsReplay() or Spring.GetSpectatingState ()) then
  154. widgetHandler:RemoveWidget()
  155. end
  156. end
  157.  
  158. function widget:PlayerChanged (playerID)
  159. checkToDisable ()
  160. end
Advertisement
Add Comment
Please, Sign In to add comment