SHARE
TWEET

Untitled

a guest Nov 29th, 2011 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --------------------------------------------------------------------------------
  2. --------------------------------------------------------------------------------
  3. --
  4. --  author:  jK
  5. --
  6. --  Copyright (C) 2007,2008.
  7. --  Licensed under the terms of the GNU GPL, v2 or later.
  8. --
  9. --------------------------------------------------------------------------------
  10. --------------------------------------------------------------------------------
  11.  
  12. function widget:GetInfo()
  13.   return {
  14.     name      = "HealthBars",
  15.     desc      = "Gives various informations about units in form of bars.",
  16.     author    = "jK",
  17.     date      = "Jun, 2008",
  18.     license   = "GNU GPL, v2 or later",
  19.     layer     = -10,
  20.     enabled   = true  --  loaded by default?
  21.   }
  22. end
  23.  
  24. --------------------------------------------------------------------------------
  25. --------------------------------------------------------------------------------
  26.  
  27. local barHeight = 3
  28. local barWidth  = 14  --// (barWidth)x2 total width!!!
  29. local barAlpha  = 0.9
  30.  
  31. local featureBarHeight = 3
  32. local featureBarWidth  = 10
  33. local featureBarAlpha  = 0.6
  34.  
  35. local drawBarTitles = true
  36. local titlesAlpha   = 0.3*barAlpha
  37.  
  38. local drawFullHealthBars = false
  39.  
  40. local drawFeatureHealth  = true
  41. local featureTitlesAlpha = featureBarAlpha * titlesAlpha/barAlpha
  42. local featureHpThreshold = 0.85
  43.  
  44. local infoDistance = 700000
  45.  
  46. local minReloadTime = 4 --// in seconds
  47.  
  48. local drawStunnedOverlay = true
  49. local drawUnitsOnFire    = Spring.GetGameRulesParam("unitsOnFire")
  50. local drawJumpJet        = Spring.GetGameRulesParam("jumpJets")
  51.  
  52. --// this table is used to shows the hp of perimeter defence, and filter it for default wreckages
  53. local walls = {dragonsteeth=true,dragonsteeth_core=true,fortification=true,fortification_core=true,spike=true,floatingteeth=true,floatingteeth_core=true,spike=true}
  54.  
  55. local stockpileH = 24
  56. local stockpileW = 12
  57.  
  58. --------------------------------------------------------------------------------
  59. --------------------------------------------------------------------------------
  60.  
  61. --// colors
  62.  
  63. local bkBottom   = { 0.40,0.40,0.40,barAlpha }
  64. local bkTop      = { 0.10,0.10,0.10,barAlpha }
  65. local hpcolormap = { {0.8, 0.0, 0.0, barAlpha},  {0.8, 0.6, 0.0, barAlpha}, {0.0,0.70,0.0,barAlpha} }
  66. local empcolor   = { 0.50,0.50,1.00,barAlpha }
  67. local empcolor_p = { 0.40,0.40,0.80,barAlpha }
  68. local empcolor_b = { 0.60,0.60,0.90,barAlpha }
  69. local capcolor   = { 1.00,0.50,0.00,barAlpha }
  70. local buildcolor = { 0.75,0.75,0.75,barAlpha }
  71. local stockcolor = { 0.50,0.50,0.50,barAlpha }
  72. local reloadcolor= { 0.00,0.60,0.60,barAlpha }
  73. local jumpcolor  = { 0.00,0.60,0.60,barAlpha }
  74. local shieldcolor= { 0.20,0.60,0.60,barAlpha }
  75.  
  76. local fbkBottom   = { 0.40,0.40,0.40,featureBarAlpha }
  77. local fbkTop      = { 0.06,0.06,0.06,featureBarAlpha }
  78. local fhpcolormap = { {0.8, 0.0, 0.0, featureBarAlpha},  {0.8, 0.6, 0.0, featureBarAlpha}, {0.0,0.70,0.0,featureBarAlpha} }
  79. local rescolor    = { 1.00,0.50,0.00,featureBarAlpha }
  80. local reccolor    = { 0.75,0.75,0.75,featureBarAlpha }
  81.  
  82. --------------------------------------------------------------------------------
  83. --------------------------------------------------------------------------------
  84.  
  85. local blink = false;
  86. local gameFrame = 0;
  87.  
  88. local empDecline = 32/30/40;
  89.  
  90. local bfcolormap = {}; --// a buffered list for hpcolormap
  91.  
  92. local UnitDefsCount;
  93. local tmpUpdate;
  94.  
  95. local UnitMorphs = {};
  96.  
  97. local cx, cy, cz = 0,0,0;  --// camera pos
  98. local paraUnits   = {};
  99. local onFireUnits = {};
  100.  
  101. local barShader;
  102. local barColorLoc;
  103. local barEnabledLoc;
  104. local barProgressLoc;
  105. local barOffsetLoc;
  106. local barDList;
  107. local barFeatureDList;
  108.  
  109.  
  110. --------------------------------------------------------------------------------
  111. --------------------------------------------------------------------------------
  112.  
  113. -- speed-ups
  114.  
  115. Spring.GetTeamColor = Spring.GetTeamColor or function(teamID) local _,_,_,_,_,_,r,g,b = Spring.GetTeamInfo(teamID); return r,g,b end
  116.  
  117. local GL_QUADS = GL.QUADS
  118. local GL_TEXTURE_GEN_MODE = GL.TEXTURE_GEN_MODE
  119. local GL_EYE_PLANE  = GL.EYE_PLANE
  120. local GL_EYE_LINEAR = GL.EYE_LINEAR
  121. local GL_T   = GL.T
  122. local GL_S   = GL.S
  123. local GL_ONE = GL.ONE
  124. local GL_SRC_ALPHA           = GL.SRC_ALPHA
  125. local GL_ONE_MINUS_SRC_ALPHA = GL.ONE_MINUS_SRC_ALPHA
  126. local glVertex        = gl.Vertex
  127. local glBeginEnd      = gl.BeginEnd
  128. local glTexRect       = gl.TexRect
  129. local glTranslate     = gl.Translate
  130. local glColor         = gl.Color
  131. local glDrawFuncAtUnit= gl.DrawFuncAtUnit
  132. local glFog           = gl.Fog
  133. local glDepthTest     = gl.DepthTest
  134. local glTranslate     = gl.Translate
  135. local glTexture       = gl.Texture
  136. local glText          = gl.Text
  137. local glPushMatrix    = gl.PushMatrix
  138. local glPopMatrix     = gl.PopMatrix
  139. local glBillboard     = gl.Billboard
  140. local glDepthMask     = gl.DepthMask
  141. local glBlending      = gl.Blending
  142. local glTexCoord      = gl.TexCoord
  143. local glUnit          = gl.Unit
  144. local glTexGen        = gl.TexGen
  145. local glPolygonOffset = gl.PolygonOffset
  146. local glDepthTest     = gl.DepthTest
  147. local max,min,abs     = math.max,math.min,math.abs
  148. local ceil,floor      = math.ceil,math.floor
  149. local insert          = table.insert
  150. local GetUnitDefDimensions = Spring.GetUnitDefDimensions
  151. local GetUnitIsStunned     = Spring.GetUnitIsStunned
  152. local GetUnitHealth        = Spring.GetUnitHealth
  153. local GetFeatureHealth     = Spring.GetFeatureHealth
  154. local GetFeatureResources  = Spring.GetFeatureResources
  155. local GetCameraPosition    = Spring.GetCameraPosition
  156. local GetCameraVectors     = Spring.GetCameraVectors
  157. local GetUnitWeaponState   = Spring.GetUnitWeaponState
  158. local GetUnitShieldState   = Spring.GetUnitShieldState
  159. local GetTeamList          = Spring.GetTeamList
  160. local GetTeamUnits         = Spring.GetTeamUnits
  161. local IsUnitInView         = Spring.IsUnitInView
  162. local GetUnitViewPosition  = Spring.GetUnitViewPosition
  163. local GetUnitStockpile     = Spring.GetUnitStockpile
  164. local IsSphereInView       = Spring.IsSphereInView
  165. local GetAllFeatures       = Spring.GetAllFeatures
  166. local GetFeatureDefID      = Spring.GetFeatureDefID
  167. local GetFeaturePosition   = Spring.GetFeaturePosition
  168. local GetGameFrame         = Spring.GetGameFrame
  169. local GetUnitDefID         = Spring.GetUnitDefID
  170. local GetVisibleUnits      = Spring.GetVisibleUnits
  171. local GetUnitRulesParam    = Spring.GetUnitRulesParam
  172. local ALL_UNITS            = Spring.ALL_UNITS
  173.  
  174. --------------------------------------------------------------------------------
  175. --------------------------------------------------------------------------------
  176.  
  177. local deactivated = false
  178. function showhealthbars(cmd, line, words)
  179.   if ((words[1])and(words[1]~="0"))or(deactivated) then
  180.     widgetHandler:UpdateCallIn('DrawWorld')
  181.     deactivated = false
  182.   else
  183.     widgetHandler:RemoveCallIn('DrawWorld')
  184.     deactivated = true
  185.   end
  186. end
  187.  
  188.  
  189. function widget:Initialize()
  190.   --// catch f9
  191.   Spring.SendCommands({"showhealthbars 0"})
  192.   widgetHandler:AddAction("showhealthbars", showhealthbars)
  193.   Spring.SendCommands({"unbind f9 showhealthbars"})
  194.   Spring.SendCommands({"bind f9 luaui showhealthbars"})
  195.  
  196.   --// find real primary weapon and its reloadtime
  197.   for _,ud in pairs(UnitDefs) do
  198.     ud.reloadTime    = 0;
  199.     ud.primaryWeapon = 0;
  200.     ud.shieldPower   = 0;
  201.  
  202.     for i=1,#ud.weapons do
  203.       local WeaponDefID = ud.weapons[i].weaponDef;
  204.       local WeaponDef   = WeaponDefs[ WeaponDefID ];
  205.       if (WeaponDef.reload>ud.reloadTime) then
  206.         ud.reloadTime    = WeaponDef.reload;
  207.         ud.primaryWeapon = i;
  208.       end
  209.     end
  210.     local shieldDefID = ud.shieldWeaponDef
  211.     ud.shieldPower = ((shieldDefID)and(WeaponDefs[shieldDefID].shieldPower))or(-1)
  212.   end
  213.  
  214.   --// wow, using a buffered list can give 1-2 frames in extreme(!) situations :p
  215.   for hp=0,100 do
  216.     bfcolormap[hp] = {GetColor(hpcolormap,hp*0.01)}
  217.   end
  218.  
  219.   --// link morph callins
  220.   widgetHandler:RegisterGlobal('MorphUpdate', MorphUpdate)
  221.   widgetHandler:RegisterGlobal('MorphFinished', MorphFinished)
  222.   widgetHandler:RegisterGlobal('MorphStart', MorphStart)
  223.   widgetHandler:RegisterGlobal('MorphStop', MorphStop)
  224.  
  225.   --// deactivate cheesy progress text
  226.   widgetHandler:RegisterGlobal('MorphDrawProgress', function() return true end)
  227.  
  228.   --// create bar shader
  229.   if (gl.CreateShader) then
  230.     barShader = gl.CreateShader({
  231.       vertex = [[
  232.         uniform vec4  barColor;
  233.         uniform float offset;
  234.         uniform float progress;
  235.         uniform bool  enabled;
  236.  
  237.         void main()
  238.         {
  239.            if (!enabled) {
  240.              gl_TexCoord[0]= gl_TextureMatrix[0]*gl_MultiTexCoord0;
  241.              gl_FrontColor = gl_Color;
  242.              gl_Position   = ftransform();
  243.              return;
  244.            }
  245.            if (gl_Vertex.w>0) {
  246.              gl_FrontColor = gl_Color;
  247.              if (gl_Vertex.z>0.0) {
  248.                gl_Vertex.x -= (1.0-progress)*gl_Vertex.z;
  249.                gl_Vertex.z  = 0.0;
  250.              }
  251.            }else{
  252.              if (gl_Vertex.y>0.0) {
  253.                gl_FrontColor = vec4(barColor.rgb*1.5,barColor.a);
  254.              }else{
  255.                gl_FrontColor = barColor;
  256.              }
  257.              if (gl_Vertex.z>1.0) {
  258.                gl_Vertex.x += progress*gl_Vertex.z;
  259.                gl_Vertex.z  = 0.0;
  260.              }
  261.              gl_Vertex.w  = 1.0;
  262.            }
  263.  
  264.            gl_Vertex.y += offset;
  265.            gl_Position  = gl_ModelViewProjectionMatrix*gl_Vertex;
  266.          }
  267.       ]],
  268.       uniformInt = {enabled = 1}
  269.     });
  270.  
  271.     if (barShader) then
  272.       barColorLoc    = gl.GetUniformLocation(barShader,"barColor")
  273.       barOffsetLoc   = gl.GetUniformLocation(barShader,"offset")
  274.       barProgressLoc = gl.GetUniformLocation(barShader,"progress")
  275.       barEnabledLoc  = gl.GetUniformLocation(barShader,"enabled")
  276.  
  277.       barDList = gl.CreateList(function()
  278.         glBeginEnd(GL_QUADS,function()
  279.           glVertex(-barWidth,0,        0,0);
  280.           glVertex(-barWidth,0,        barWidth*2,0);
  281.           glVertex(-barWidth,barHeight,barWidth*2,0);
  282.           glVertex(-barWidth,barHeight,0,0);
  283.  
  284.           glColor(bkBottom);
  285.           glVertex(barWidth,0,        0,         1);
  286.           glVertex(barWidth,0,        barWidth*2,1);
  287.           glColor(bkTop);
  288.           glVertex(barWidth,barHeight,barWidth*2,1);
  289.           glVertex(barWidth,barHeight,0,         1);
  290.         end)
  291.       end)
  292.  
  293.       barFeatureDList = gl.CreateList(function()
  294.         glBeginEnd(GL_QUADS,function()
  295.           glVertex(-featureBarWidth,0,               0,0);
  296.           glVertex(-featureBarWidth,0,               featureBarWidth*2,0);
  297.           glVertex(-featureBarWidth,featureBarHeight,featureBarWidth*2,0);
  298.           glVertex(-featureBarWidth,featureBarHeight,0,0);
  299.  
  300.           glColor(fbkBottom);
  301.           glVertex(featureBarWidth,0,               0,         1);
  302.           glVertex(featureBarWidth,0,               featureBarWidth*2,1);
  303.           glColor(fbkTop);
  304.           glVertex(featureBarWidth,featureBarHeight,featureBarWidth*2,1);
  305.           glVertex(featureBarWidth,featureBarHeight,0,         1);
  306.         end)
  307.       end)
  308.     end
  309.   end
  310.  
  311. end
  312.  
  313. function widget:Shutdown()
  314.   --// catch f9
  315.   widgetHandler:RemoveAction("showhealthbars", showhealthbars)
  316.   Spring.SendCommands({"unbind f9 luaui"})
  317.   Spring.SendCommands({"bind f9 showhealthbars"})
  318.   Spring.SendCommands({"showhealthbars 1"})
  319.  
  320.   widgetHandler:DeregisterGlobal('MorphUpdate', MorphUpdate)
  321.   widgetHandler:DeregisterGlobal('MorphFinished', MorphFinished)
  322.   widgetHandler:DeregisterGlobal('MorphStart', MorphStart)
  323.   widgetHandler:DeregisterGlobal('MorphStop', MorphStop)
  324.  
  325.   widgetHandler:DeregisterGlobal('MorphDrawProgress')
  326.  
  327.   if (barShader) then
  328.     gl.DeleteShader(barShader)
  329.   end
  330.   if (barDList) then
  331.     gl.DeleteList(barDList)
  332.     gl.DeleteList(barFeatureDList)
  333.   end
  334. end
  335.  
  336. --------------------------------------------------------------------------------
  337. --------------------------------------------------------------------------------
  338.  
  339. function GetColor(colormap,slider)
  340.   local coln = #colormap
  341.   if (slider>=1) then
  342.     local col = colormap[coln]
  343.     return col[1],col[2],col[3],col[4]
  344.   end
  345.   if (slider<0) then slider=0 elseif(slider>1) then slider=1 end
  346.   local posn  = 1+(coln-1) * slider
  347.   local iposn = floor(posn)
  348.   local aa    = posn - iposn
  349.   local ia    = 1-aa
  350.  
  351.   local col1,col2 = colormap[iposn],colormap[iposn+1]
  352.  
  353.   return col1[1]*ia + col2[1]*aa, col1[2]*ia + col2[2]*aa,
  354.          col1[3]*ia + col2[3]*aa, col1[4]*ia + col2[4]*aa
  355. end
  356.  
  357. local function DrawGradient(left,top,right,bottom,topclr,bottomclr)
  358.   glColor(bottomclr)
  359.   glVertex(left,bottom)
  360.   glVertex(right,bottom)
  361.   glColor(topclr)
  362.   glVertex(right,top)
  363.   glVertex(left,top)
  364. end
  365.  
  366. local brightClr = {}
  367. local function DrawBar(offsetY,percent,color)
  368.   if (barShader) then
  369.     gl.Uniform(barColorLoc,color[1],color[2],color[3],color[4])
  370.     gl.Uniform(barProgressLoc,percent)
  371.     gl.Uniform(barOffsetLoc,offsetY)
  372.     gl.CallList(barDList)
  373.     return;
  374.   end
  375.  
  376.   brightClr[1] = color[1]*1.5; brightClr[2] = color[2]*1.5; brightClr[3] = color[3]*1.5; brightClr[4] = color[4]
  377.   local progress_pos= -barWidth+barWidth*2*percent
  378.   local bar_Height  = barHeight+offsetY
  379.   if percent<1 then glBeginEnd(GL_QUADS,DrawGradient,progress_pos, bar_Height, barWidth, offsetY, bkTop,bkBottom) end
  380.   glBeginEnd(GL_QUADS,DrawGradient,-barWidth, bar_Height, progress_pos, offsetY,brightClr,color)
  381. end
  382.  
  383. local function DrawFeatureBar(offsetY,percent,color)
  384.   if (barShader) then
  385.     gl.Uniform(barColorLoc,color[1],color[2],color[3],color[4])
  386.     gl.Uniform(barProgressLoc,percent)
  387.     gl.Uniform(barOffsetLoc,offsetY)
  388.     gl.CallList(barFeatureDList)
  389.     return;
  390.   end
  391.  
  392.   brightClr[1] = color[1]*1.5; brightClr[2] = color[2]*1.5; brightClr[3] = color[3]*1.5; brightClr[4] = color[4]
  393.   local progress_pos = -featureBarWidth+featureBarWidth*2*percent
  394.   glBeginEnd(GL_QUADS,DrawGradient,progress_pos, featureBarHeight+offsetY, featureBarWidth, offsetY, fbkTop,fbkBottom)
  395.   glBeginEnd(GL_QUADS,DrawGradient,-featureBarWidth, featureBarHeight+offsetY, progress_pos, offsetY, brightClr,color)
  396. end
  397.  
  398. local function DrawStockpile(numStockpiled,numStockpileQued)
  399.   --// DRAW STOCKPILED MISSLES
  400.   glColor(1,1,1,1)
  401.   glTexture("LuaUI/Images/nuke.png")
  402.   local xoffset = barWidth+16
  403.   for i=1,min(numStockpiled,3) do
  404.     glTexRect(xoffset,-(11*barHeight-2)-stockpileH,xoffset-stockpileW,-(11*barHeight-2))
  405.     xoffset = xoffset-8
  406.   end
  407.   glTexture(false)
  408.  
  409.   glText(numStockpiled..'/'..numStockpileQued,barWidth+1.7,-(11*barHeight-2)-16,6.5,"dcno")
  410. end
  411.  
  412. local teamColors = {}
  413. local function SetTeamColor(teamID,a)
  414.   local color = teamColors[teamID]
  415.   if (color) then
  416.     color[4]=a
  417.     glColor(color)
  418.     return
  419.   end
  420.   local r, g, b = Spring.GetTeamColor(teamID)
  421.   if (r and g and b) then
  422.     color = { r, g, b }
  423.     teamColors[teamID] = color
  424.     glColor(color)
  425.     return
  426.   end
  427. end
  428.  
  429. --------------------------------------------------------------------------------
  430. --------------------------------------------------------------------------------
  431.  
  432. local function DrawUnitInfos(unitID,height,canStockpile,reloadTime,primaryWeapon,MaxShieldPower)
  433.   local fullText = true
  434.   local ux, uy, uz = GetUnitViewPosition(unitID)
  435.   local dx, dy, dz = ux-cx, uy-cy, uz-cz
  436.   dist = dx*dx + dy*dy + dz*dz
  437.   if (dist>9000000) then
  438.     return
  439.   elseif (dist > infoDistance) then
  440.     fullText = false
  441.   end
  442.  
  443.   --// GET UNIT INFORMATION
  444.   local health,maxHealth,paralyzeDamage,capture,build = GetUnitHealth(unitID)
  445.   --if (health==nil)    then health=-1   elseif(health<1)    then health=1    end
  446.   if (maxHealth==nil)or(maxHealth<1) then maxHealth=1 end
  447.   if (paralyzeDamage==nil) then paralyzeDamage=0 end
  448.   if (capture==nil)   then capture=0 end
  449.   if (build==nil)     then build=1   end
  450.  
  451.   local emp = paralyzeDamage/(health or 1)
  452.   if (emp>1) then emp=1 end
  453.   local hp  = (health or 0)/maxHealth
  454.   local shieldOn,shieldPower
  455.   if (MaxShieldPower>0)  --//you can also add a shield with Lua, but as long as it isn't used we don't need to check it
  456.      then shieldOn,shieldPower = GetUnitShieldState(unitID) end
  457.   local morph = UnitMorphs[unitID]
  458.  
  459.   if (drawUnitsOnFire)and(GetUnitRulesParam(unitID,"on_fire")==1) then
  460.     onFireUnits[#onFireUnits+1]=unitID
  461.   end
  462.  
  463.   --// BARS //-----------------------------------------------------------------------------
  464.   local bars = {}
  465.   local n = 0
  466.  
  467.     --// Shield
  468.     if (shieldOn)and(build==1)and(shieldPower<MaxShieldPower) then
  469.       shieldPower = shieldPower / MaxShieldPower
  470.       n=n+1
  471.       bars[n]={title="shield",progress=shieldPower,color=shieldcolor,text=floor(shieldPower*100)..'%'}
  472.     end
  473.  
  474.     --// HEALTH
  475.     if (health)and( (build==1)or(build-hp>=0.01) ) then
  476.       local hp100   = ceil(hp*100)
  477.       if (hp100<0) then hp100=0 elseif (hp100>100) then hp100=100 end
  478.       if (drawFullHealthBars)or(hp100<100) then
  479.         local hpcolor = bfcolormap[hp100]
  480.         n=n+1
  481.         bars[n]={title="health",progress=hp,color=hpcolor,text=hp100..'%'}
  482.       end
  483.     end
  484.  
  485.     --// BUILD
  486.     if (build<1) then
  487.       n=n+1
  488.       bars[n]={title="building",progress=build,color=buildcolor,text=floor(build*100)..'%'}
  489.     end
  490.  
  491.     --// MORPHING
  492.     if (morph) then
  493.       local build = morph.progress
  494.       n=n+1
  495.       bars[n]={title="morph",progress=build,color=buildcolor,text=floor(build*100)..'%'}
  496.     end
  497.  
  498.     --// STOCKPILE
  499.     local numStockpiled,numStockpileQued,stockpileBuild;
  500.     if canStockpile then
  501.       numStockpiled,numStockpileQued,stockpileBuild = GetUnitStockpile(unitID)
  502.       if numStockpiled then
  503.         stockpileBuild = stockpileBuild or 0
  504.         if (stockpileBuild>0) then
  505.           n=n+1
  506.           bars[n]={title="stockpile",progress=stockpileBuild,color=stockcolor,text=floor(stockpileBuild*100)..'%'}
  507.         end
  508.       end
  509.     end
  510.  
  511.     --// PARALYZE
  512.     if (emp>0.01)and(hp>0.01)and(not morph)and(emp<1e8) then
  513.       local stunned = GetUnitIsStunned(unitID)
  514.       local infotext = ""
  515.       if (stunned) then
  516.         paraUnits[#paraUnits+1]=unitID
  517.         --table.insert(paraUnits,unitID)
  518.         infotext = floor((paralyzeDamage-health)/(maxHealth*empDecline)) .. 's'
  519.         emp = 1
  520.       else
  521.         infotext = floor(emp*100)..'%'
  522.       end
  523.       local paracolor = (stunned and ((blink and empcolor_b) or empcolor_p)) or (empcolor)
  524.       n=n+1
  525.       bars[n]={title="paralyze",progress=emp,color=paracolor,text=infotext}
  526.     end
  527.  
  528.     --// CAPTURE
  529.     if (capture>0) then
  530.       n=n+1
  531.       bars[n]={title="capture",progress=capture,color=capcolor,text=floor(capture*100)..'%'}
  532.     end
  533.  
  534.     --// RELOAD
  535.     if (reloadTime>=minReloadTime) then
  536.       local _,reloaded,reloadFrame = GetUnitWeaponState(unitID,primaryWeapon-1)
  537.       if (reloaded==false) then
  538.         local reload = 1 - ((reloadFrame-gameFrame)/30) / reloadTime;
  539.         n=n+1
  540.         bars[n]={title="reload",progress=reload,color=reloadcolor,text=floor(reload*100)..'%'}
  541.       end
  542.     end
  543.  
  544.     --// JUMPJET
  545.     if (drawJumpJet) then
  546.       local jumpReload = GetUnitRulesParam(unitID,"jumpReload")
  547.       if (jumpReload and (jumpReload>0) and (jumpReload<1)) then
  548.         n=n+1
  549.         bars[n]={title="jump",progress=jumpReload,color=jumpcolor,text=floor(jumpReload*100)..'%'}
  550.       end
  551.     end
  552.  
  553.  
  554.   if (n>0) then
  555.     glPushMatrix()
  556.     glTranslate(ux, uy+height, uz )
  557.     glBillboard()
  558.  
  559.     --// STOCKPILE ICON
  560.     if (numStockpiled) then
  561.       if (barShader) then gl.UniformInt(barEnabledLoc,0) end
  562.       DrawStockpile(numStockpiled,numStockpileQued)
  563.       if (barShader) then gl.UniformInt(barEnabledLoc,1) end
  564.     end
  565.  
  566.     --// DRAW BARS
  567.     local yoffset = 0
  568.     for i=1,n do
  569.       local barInfo = bars[i]
  570.       DrawBar(yoffset,barInfo.progress,barInfo.color)
  571.       if (fullText) then
  572.         if (barShader) then gl.UniformInt(barEnabledLoc,0) end
  573.         glColor(1,1,1,barAlpha)
  574.         glText(barInfo.text,-barWidth-1,yoffset-1,4,"drn")
  575.         if (drawBarTitles) then
  576.           glColor(1,1,1,titlesAlpha)
  577.           glText(barInfo.title,0,yoffset,2.5,"dcn")
  578.         end
  579.         if (barShader) then gl.UniformInt(barEnabledLoc,1) end
  580.       end
  581.       yoffset = yoffset - barHeight - 2
  582.     end
  583.  
  584.     glPopMatrix()
  585.   end
  586. end
  587.  
  588.  
  589.  
  590. local function DrawFeatureInfos(featureID,height,fullText,fx,fy,fz)
  591.   --// GET UNIT INFORMATION
  592.   local featureDefID = GetFeatureDefID(featureID)
  593.   local featureDef   = FeatureDefs[featureDefID or -1]
  594.  
  595.   local health,maxHealth,resurrect = GetFeatureHealth(featureID)
  596.   local _,_,_,_,reclaimLeft        = GetFeatureResources(featureID)
  597.   if (health==nil)      then health=1    end
  598.   if (maxHealth==nil)   then maxHealth=1 end
  599.   if (resurrect==nil)   then resurrect=0 end
  600.   if (reclaimLeft==nil) then reclaimLeft=0 end
  601.   local hp = health/maxHealth
  602.  
  603.   --// filter all none walls and none resurrecting features
  604.   if (health==nil)or
  605.      (featureDef==nil)or
  606.      ( (resurrect==0)and(reclaimLeft==1)and
  607.        ( (not walls[featureDef.name]) or (hp>featureHpThreshold) )
  608.      )
  609.   then return end
  610.  
  611.   --// BARS //-----------------------------------------------------------------------------
  612.   local bars = {}
  613.   local n = 0
  614.  
  615.     --// HEALTH
  616.     if (hp<featureHpThreshold)and(drawFeatureHealth) then
  617.       local hpcolor = {}
  618.       hpcolor[1],hpcolor[2],hpcolor[3],hpcolor[4] = GetColor(fhpcolormap,hp)
  619.       n=n+1
  620.       bars[n]={title="health",progress=hp,color=hpcolor,text=floor(hp*100)..'%'}
  621.     end
  622.  
  623.     --// RESURRECT
  624.     if (resurrect>0) then
  625.       n=n+1
  626.       bars[n]={title="resurrect",progress=resurrect,color=rescolor,text=floor(resurrect*100)..'%'}
  627.     end
  628.  
  629.  
  630.     --// RECLAIMING
  631.     if (reclaimLeft>0 and reclaimLeft<1) then
  632.       n=n+1
  633.       bars[n]={title="reclaim",progress=reclaimLeft,color=reccolor,text=floor(reclaimLeft*100)..'%'}
  634.     end
  635.  
  636.  
  637.   if (n>0) then
  638.     glPushMatrix()
  639.     glTranslate(fx,fy+featureDef.height+14,fz)
  640.     glBillboard()
  641.  
  642.     --// DRAW BARS
  643.     local yoffset = 0
  644.     for i=1,n do
  645.       local barInfo = bars[i]
  646.       DrawFeatureBar(yoffset,barInfo.progress,barInfo.color)
  647.       if (fullText) then
  648.         if (barShader) then gl.UniformInt(barEnabledLoc,0) end
  649.         glColor(1,1,1,featureBarAlpha)
  650.         glText(barInfo.text,-featureBarWidth-1,yoffset-1,4,"drn")
  651.         if (drawBarTitles) then
  652.           glColor(1,1,1,featureTitlesAlpha)
  653.           glText(barInfo.title,0,yoffset,2.5,"dcn")
  654.         end
  655.         if (barShader) then gl.UniformInt(barEnabledLoc,1) end
  656.       end
  657.       yoffset = yoffset - featureBarHeight - 2
  658.     end
  659.  
  660.     glPopMatrix()
  661.   end
  662. end
  663.  
  664. --------------------------------------------------------------------------------
  665. --------------------------------------------------------------------------------
  666.  
  667. local featureList  = {}
  668. local visibleUnits = {}
  669. local videoFrame   = 0
  670. local unitDefHeights = {}
  671.  
  672. function widget:DrawWorld()
  673.  
  674.   if Spring.IsGUIHidden() then
  675.     return
  676.   end
  677.  
  678.   --glFog(false)
  679.   --glDepthTest(true)
  680.   glDepthMask(true)
  681.  
  682.   cx, cy, cz = GetCameraPosition()
  683.  
  684.   videoFrame = videoFrame+1
  685.   if (videoFrame%4<1) then
  686.     visibleUnits = GetVisibleUnits(ALL_UNITS,nil,true)
  687.   end
  688.  
  689.   if (barShader) then gl.UseShader(barShader) end
  690.  
  691.   --// draw bars of units
  692.   for i=1,#visibleUnits do
  693.     local unitID    = visibleUnits[i]
  694.     local unitDefID = GetUnitDefID(unitID)
  695.     local unitDef   = UnitDefs[unitDefID or -1]
  696.     if (unitDef) then
  697.       if (not unitDefHeights[unitDefID]) then unitDefHeights[unitDefID] = unitDef.height+14 end
  698.       local height         = unitDefHeights[unitDefID]
  699.       local canStockpile   = unitDef.canStockpile
  700.       local reloadTime     = unitDef.reloadTime
  701.       local primaryWeapon  = unitDef.primaryWeapon
  702.       local MaxShieldPower = unitDef.shieldPower
  703.              
  704.       --glDrawFuncAtUnit(unitID, true, DrawUnitInfos, unitID, height, canStockpile, reloadTime, primaryWeapon, MaxShieldPower)
  705.       DrawUnitInfos(unitID, height, canStockpile, reloadTime, primaryWeapon, MaxShieldPower)
  706.     end
  707.   end
  708.  
  709.   --// draw bars for features
  710.   for featureID,featurePos in pairs(featureList) do
  711.     local wx, wy, wz = featurePos[1],featurePos[2],featurePos[3]
  712.     local dx, dy, dz = wx-cx, wy-cy, wz-cz
  713.     dist = dx*dx + dy*dy + dz*dz
  714.     if (dist < 6000000)and(IsSphereInView(wx,wy,wz)) then
  715.       if (dist < infoDistance) then
  716.         DrawFeatureInfos(featureID, 70, true, wx,wy,wz)
  717.       else
  718.         DrawFeatureInfos(featureID, 70, false, wx,wy,wz)
  719.       end
  720.     end
  721.   end
  722.  
  723.  
  724.   if (barShader) then gl.UseShader(0) end
  725.   glDepthMask(false)
  726.  
  727.  
  728.   --// draw an overlay for stunned units
  729.   if (drawStunnedOverlay)and(#paraUnits>0) then
  730.     glDepthTest(true)
  731.     glPolygonOffset(-2, -2)
  732.     glBlending(GL_SRC_ALPHA, GL_ONE)
  733.  
  734.     local alpha = ((5.5 * widgetHandler:GetHourTimer()) % 2) - 0.7
  735.     glColor(0,0.7,1,alpha/4)
  736.     for i=1,#paraUnits do
  737.       glUnit(paraUnits[i],true)
  738.     end
  739.     local shift = widgetHandler:GetHourTimer() / 20
  740.  
  741.     glTexCoord(0,0)
  742.     glTexGen(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR)
  743.     local cvs = GetCameraVectors()
  744.     local v = cvs.right
  745.     glTexGen(GL_T, GL_EYE_PLANE, v[1]*0.008,v[2]*0.008,v[3]*0.008, shift)
  746.     glTexGen(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR)
  747.     v = cvs.forward
  748.     glTexGen(GL_S, GL_EYE_PLANE, v[1]*0.008,v[2]*0.008,v[3]*0.008, shift)
  749.     glTexture("LuaUI/Images/paralyzed.png")
  750.  
  751.     glColor(0,1,1,alpha*1.1)
  752.     for i=1,#paraUnits do
  753.       glUnit(paraUnits[i],true)
  754.     end
  755.  
  756.     glTexture(false)
  757.     glTexGen(GL_T, false)
  758.     glTexGen(GL_S, false)
  759.     glBlending(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  760.     glPolygonOffset(false)
  761.     glDepthTest(false)
  762.  
  763.     paraUnits = {}
  764.   end
  765.  
  766.   --// overlay for units on fire
  767.   if (drawUnitsOnFire)and(onFireUnits) then
  768.     glDepthTest(true)
  769.     glPolygonOffset(-2, -2)
  770.     glBlending(GL_SRC_ALPHA, GL_ONE)
  771.  
  772.     local alpha = abs((widgetHandler:GetHourTimer() % 2)-1)
  773.     glColor(1,0.3,0,alpha/4)
  774.     for i=1,#onFireUnits do
  775.       glUnit(onFireUnits[i],true)
  776.     end
  777.  
  778.     glBlending(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  779.     glPolygonOffset(false)
  780.     glDepthTest(false)
  781.  
  782.     onFireUnits = {}
  783.   end
  784.  
  785.   glColor(1,1,1,1)
  786.   --glDepthTest(false)
  787. end
  788.  
  789.  
  790. local sec = 0
  791. function widget:Update(dt)
  792.   sec=sec+dt
  793.   blink = (sec%1)<0.5
  794.  
  795.   gameFrame = GetGameFrame()
  796.  
  797.   --// update feature list (huge speed improvement if we buffer it)
  798.   if (((gameFrame+1)%180)<1) then
  799.     featureList = {}
  800.     local allFeatures = GetAllFeatures()
  801.     for i=1,#allFeatures do
  802.       local featureID    = allFeatures[i]
  803.       local featureDefID = GetFeatureDefID(featureID) or -1
  804.       local featureDef   = FeatureDefs[featureDefID]
  805.       --// filter trees and none destructable features
  806.       if (featureDef)and(featureDef.drawType~=1)and(featureDef.destructable) then
  807.         featureList[featureID] = {GetFeaturePosition(featureID)}
  808.       end
  809.     end
  810.   end
  811. end
  812.  
  813. --------------------------------------------------------------------------------
  814. --------------------------------------------------------------------------------
  815.  
  816. --// not 100% finished!
  817.  
  818. function MorphUpdate(morphTable)
  819.   UnitMorphs = morphTable
  820. end
  821.  
  822. function MorphStart(unitID,morphDef)
  823.   --return false
  824. end
  825.  
  826. function MorphStop(unitID)
  827.   UnitMorphs[unitID] = nil
  828. end
  829.  
  830. function MorphFinished(unitID)
  831.   UnitMorphs[unitID] = nil
  832. end
  833.  
  834. --------------------------------------------------------------------------------
  835. --------------------------------------------------------------------------------
  836.  
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top