Guest User

Untitled

a guest
Apr 25th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.17 KB | None | 0 0
  1. ///////////////////////////////
  2. // © 2009-2010 Pulsar Effect //
  3. // All rights reserved //
  4. ///////////////////////////////
  5. // This material may not be //
  6. // reproduced, displayed, //
  7. // modified or distributed //
  8. // without the express prior //
  9. // written permission of the //
  10. // the copyright holder. //
  11. ///////////////////////////////
  12.  
  13.  
  14. include('shared.lua')
  15.  
  16. local matLight = Material( "sprites/light_ignorez" )
  17.  
  18. ENT.RenderGroup = RENDERGROUP_BOTH
  19.  
  20. function ENT:Draw ( )
  21. self.lastDraw = CurTime() + 0.1
  22. self:DrawTranslucent()
  23. end
  24.  
  25. function ENT:Think ( )
  26. if (!self.lastDraw || self.lastDraw < CurTime()) then
  27. self:DrawTranslucent()
  28. end
  29. end
  30.  
  31. function ENT:DrawTranslucent ( )
  32. if (!self.ourParent) then
  33. local closestDist = 10000;
  34. local closest;
  35.  
  36.  
  37. for k, v in pairs(ents.FindByClass("prop_vehicle_jeep")) do
  38. local dist = v:GetPos():Distance(self:GetPos());
  39. if (dist < closestDist) then
  40. closest = v;
  41. closestDist = dist;
  42. end
  43. end
  44.  
  45. if (!closest) then return; end
  46.  
  47. self.ourParent = closest;
  48.  
  49.  
  50. local vT = lookForVT(self.ourParent);
  51.  
  52.  
  53. local vehicleID = self.ourParent:GetNetworkedString("vehicletableid", "error");
  54.  
  55.  
  56. if (vehicleID != "error" && vT.ID != vehicleID) then
  57.  
  58. vT = VEHICLE_DATABASE[vehicleID];
  59. end
  60.  
  61.  
  62. self.vehicleTable = vT;
  63.  
  64. if (self.vehicleTable.SirenNoise) then
  65. self.sirenNoise = CreateSound(self.ourParent, self.vehicleTable.SirenNoise);
  66. self.sirenNoise_Duration = SoundDuration(self.vehicleTable.SirenNoise) * .98;
  67. end
  68.  
  69. if (self.vehicleTable.SirenNoise_Alt) then
  70. self.sirenNoise_Alt = CreateSound(self.ourParent, self.vehicleTable.SirenNoise_Alt);
  71. self.sirenNoise_Alt_Duration = SoundDuration(self.vehicleTable.SirenNoise_Alt) * .98;
  72. end
  73. end
  74.  
  75. if (!self.ourParent || !IsValid(self.ourParent)) then return; end
  76.  
  77. local percChange = 1;
  78. local percChange_Siren = 1;
  79. if (GAMEMODE.CurrentTime > NOON) then
  80.  
  81. if (GAMEMODE.CurrentTime < DUSK_START) then
  82. percChange = .25;
  83. percChange_Siren = 0.5
  84. elseif (GAMEMODE.CurrentTime < DUSK_END) then
  85. PercentOf = (GAMEMODE.CurrentTime - DUSK_START) / (DUSK_END - DUSK_START);
  86. percChange = .25 + (.75 * PercentOf);
  87. percChange_Siren = .5 + (.5 * PercentOf);
  88. end
  89. else
  90.  
  91. if (GAMEMODE.CurrentTime > DAWN_END) then
  92. percChange = .25;
  93. percChange_Siren = .5;
  94. elseif (GAMEMODE.CurrentTime > DAWN_START) then
  95. PercentOf = (GAMEMODE.CurrentTime - DAWN_START) / (DAWN_END - DAWN_START);
  96. percChange = 1 - (.75 * PercentOf);
  97. percChange_Siren = 1 - (.5 * PercentOf);
  98. end
  99. end
  100.  
  101. if (self.ourParent:GetNetworkedBool("hl", false)) then
  102. self:DrawHeadlights(percChange);
  103. end
  104.  
  105. if (!self.vehicleTable || !self.vehicleTable.RequiredClass) then return; end
  106.  
  107. if (self.ourParent:GetNetworkedBool("siren", false)) then
  108. if (self.ourParent:GetNetworkedBool("siren_loud", false)) then
  109. if (self.LastSirenPlay) then
  110. self.sirenNoise:Stop();
  111. self.LastSirenPlay = nil;
  112. end
  113.  
  114. if (!self.LastSirenPlay_Alt || self.LastSirenPlay_Alt <= CurTime()) then
  115. self.LastSirenPlay_Alt = CurTime() + self.sirenNoise_Alt_Duration;
  116. self.sirenNoise_Alt:Stop();
  117. self.sirenNoise_Alt:Play();
  118. end
  119. else
  120. if (self.LastSirenPlay_Alt) then
  121. self.sirenNoise_Alt:Stop();
  122. self.LastSirenPlay_Alt = nil;
  123. end
  124.  
  125. if (!self.LastSirenPlay || self.LastSirenPlay <= CurTime()) then
  126. self.LastSirenPlay = CurTime() + self.sirenNoise_Duration;
  127. self.sirenNoise:Stop();
  128. self.sirenNoise:Play();
  129. end
  130. end
  131. elseif (self.LastSirenPlay) then
  132. self.sirenNoise:Stop();
  133. self.LastSirenPlay = nil;
  134. elseif (self.LastSirenPlay_Alt) then
  135. self.sirenNoise_Alt:Stop();
  136. self.LastSirenPlay_Alt = nil;
  137. end
  138.  
  139. if (self.ourParent:GetNetworkedBool("slights", false)) then
  140. self.curSlight = 0
  141. if (table.Count(self.vehicleTable.SirenColors) == 2) then
  142. self:DrawDualSiren(percChange_Siren)
  143. else
  144. self:DrawMultiSiren(percChange_Siren)
  145. end
  146. end
  147. end
  148.  
  149. function ENT:DrawMultiSiren ( percChange )
  150. local numPhases = math.ceil(table.Count(self.vehicleTable.SirenColors) / 2)
  151. local timePerPhase = 0.5 / numPhases
  152.  
  153. local curPhase = math.ceil((CurTime() - math.floor(CurTime())) / timePerPhase)
  154.  
  155. if (curPhase > numPhases) then
  156. curPhase = curPhase - numPhases
  157. end
  158.  
  159. local lightStart = curPhase * 2
  160.  
  161. for curLight = lightStart - 1, lightStart do
  162. if (self.vehicleTable.SirenColors[curLight]) then
  163. self:DrawSirenSprite(self.vehicleTable.SirenColors[curLight][2], self.vehicleTable.SirenColors[curLight][1], percChange)
  164. end
  165. end
  166. end
  167.  
  168. function ENT:DrawSirenSprite ( pos, col, percChange )
  169. self.curSlight = self.curSlight + 1
  170. local truePos = self.ourParent:LocalToWorld(pos + Vector(0, 0, 5))
  171.  
  172. if (!self.PixVis["SLights" .. self.curSlight]) then
  173. self.PixVis["SLights" .. self.curSlight] = util.GetPixelVisibleHandle();
  174. end
  175.  
  176. local trueAng = Angle(90, -90, 0) + self.ourParent:GetAngles();
  177.  
  178. local LightNrm = trueAng:Up()
  179. local ViewNormal = truePos - EyePos()
  180. local Distance = ViewNormal:Length()
  181. ViewNormal:Normalize()
  182. local ViewDot = ViewNormal:Dot( LightNrm )
  183. local LightPos = truePos + ViewNormal * -6
  184.  
  185. local visCheck = util.PixelVisible(LightPos, 32, self.PixVis["SLights" .. self.curSlight])
  186.  
  187. if (!visCheck || visCheck == 0) then return end
  188.  
  189. render.SetMaterial( matLight )
  190. local Size = math.Clamp(Distance * visCheck * 2, 32, 256) * percChange
  191.  
  192. Distance = math.Clamp(Distance, 32, 800);
  193. local Alpha = math.Clamp(math.Clamp((1000 - Distance) * visCheck, 0, 75) * percChange, 0, 255);
  194.  
  195. render.DrawSprite(LightPos, Size, Size, Color(col.r, col.g, col.b, Alpha), visCheck)
  196.  
  197. local dlight = DynamicLight( self.ourParent:EntIndex() )
  198. if ( dlight ) then
  199. dlight.Pos = LightPos;
  200. dlight.r = col.r
  201. dlight.g = col.g
  202. dlight.b = col.b
  203. dlight.Brightness = 5 * percChange
  204. dlight.Decay = 256
  205. dlight.Size = 512
  206. dlight.DieTime = CurTime() + 0.05
  207. end
  208. end
  209.  
  210. function ENT:DrawDualSiren ( percChange )
  211. local shouldBeOn_1, shouldBeOn_2;
  212.  
  213. local shouldBeOn_C = math.sin(CurTime() * 8);
  214. if (shouldBeOn_C > .4 && shouldBeOn_C < .85) then
  215. shouldBeOn_1 = true;
  216. elseif (shouldBeOn_C > -0.85 && shouldBeOn_C < -0.4) then
  217. shouldBeOn_2 = true;
  218. end
  219.  
  220. local curLight = 3;
  221. if (shouldBeOn_1) then curLight = 1; end
  222. if (shouldBeOn_2) then curLight = 2; end
  223.  
  224. if (curLight != 3) then
  225. self:DrawSirenSprite(self.vehicleTable.SirenColors[curLight][2], self.vehicleTable.SirenColors[curLight][1], percChange)
  226. end
  227. end
  228.  
  229. function ENT:DrawHeadlights ( percChange )
  230. local parentTable = lookForVT(self.ourParent);
  231.  
  232. if (!parentTable.HeadlightPositions) then return; end
  233.  
  234. for k, v in pairs(parentTable.HeadlightPositions) do
  235. if (self.ourParent:GetNetworkedInt("fl", 0) == 0 || self.ourParent:GetNetworkedInt("fl", 0) == k) then
  236. local truePos = self.ourParent:LocalToWorld(Vector(v[1].x, v[1].y, v[1].z));
  237. local trueAng = Angle(0, v[2].y, v[2].r) - Angle(-90, 90, 0) + self.ourParent:GetAngles();
  238.  
  239. local LightNrm = trueAng:Up()
  240. local ViewNormal = truePos - EyePos()
  241. local Distance = ViewNormal:Length()
  242. ViewNormal:Normalize()
  243. local ViewDot = ViewNormal:Dot( LightNrm )
  244. local LightPos = truePos + ViewNormal * -6
  245.  
  246. if ( ViewDot >= 0 ) then
  247. render.SetMaterial( matLight )
  248.  
  249. if (!self.PixVis["H" .. tostring(k)]) then
  250. self.PixVis["H" .. tostring(k)] = util.GetPixelVisibleHandle();
  251. end
  252.  
  253. local Visibile = util.PixelVisible(LightPos, 16, self.PixVis["H" .. tostring(k)])
  254. if (!Visibile) then return end
  255.  
  256. local Size = math.Clamp(Distance * Visibile * ViewDot * 2, 64, 512) * percChange
  257.  
  258. Distance = math.Clamp(Distance, 32, 800);
  259. local Alpha = math.Clamp((1000 - Distance) * Visibile * ViewDot, 0, 100) * percChange;
  260. local r, g, b = self:GetColor();
  261. local Col = Color(r, g, b, Alpha);
  262.  
  263. render.DrawSprite(LightPos, Size, Size, Col, Visibile * ViewDot)
  264. render.DrawSprite(LightPos, Size * 0.4, Size * 0.4, Col, Visibile * ViewDot);
  265. end
  266. end
  267. end
  268. end
  269.  
  270. function ENT:Initialize ( )
  271. self:SetNotSolid(true);
  272. self:DrawShadow(false);
  273.  
  274. self.PixVis = {};
  275. end
Add Comment
Please, Sign In to add comment