summaryrefslogtreecommitdiff
path: root/gamemode/weather.lua
blob: 4ee0aab4cc3251b52a8cf5adcb886e93b8d4b08f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
GM.Weather = {}
GM.Weather.Rain = 0
GM.Weather.Thunder = 0
GM.Weather.Lightning = 0
GM.Weather.Wind = 0
GM.Weather.TransitionTime = 60 // 1 minute for weather transitions

GM.Weather.New = {}
GM.Weather.New.Rain = 0
GM.Weather.New.Thunder = 0
GM.Weather.New.Lightning = 0
GM.Weather.New.Wind = 0

function GM:EntityKeyValue( ent, key, val )

	if ent:GetClass() == "worldspawn" and key == "skyname" then

		SetGlobalString( "SkyName", val )

	end

end

function GM:WeatherInit()

	if CLIENT then

		RainEmitter = ParticleEmitter( Vector(0,0,0) )

	end

end

function GM:PlayerIndoors( ply )

	if SERVER then

		return ply:IsIndoors()

	else

		local tr = util.TraceLine( util.GetPlayerTrace( LocalPlayer(), Vector(0,0,1) ) )

		if tr.HitWorld and not tr.HitSky then

			GAMEMODE.PlayerIsIndoors = true

			return true

		end

	end

	GAMEMODE.PlayerIsIndoors = false

	return false

end

function GM:RandomizeWeather( force )

	GAMEMODE.Weather.New.Rain = 0
	GAMEMODE.Weather.New.Thunder = 0
	GAMEMODE.Weather.New.Lightning = 0
	GAMEMODE.Weather.New.Wind = 0

	if math.random(1,5) == 1 and not force then

		GAMEMODE:SynchWeather()

		return {0,0,0,0}

	end

	for k,v in pairs( GAMEMODE.Weather.New ) do

		if math.random(1,5) > 1 then

			GAMEMODE.Weather.New[k] = math.Rand(0,1)

			if math.random(1,3) == 1 then

				GAMEMODE.Weather.New[k] = 1

			end

		end

	end

	local count = 0

	for k,v in pairs( GAMEMODE.Weather.New ) do

		if v == 0 then

			count = count + 1

		end

	end

	if count == 4 or math.random(1,10) == 1 then

		GAMEMODE.Weather.New.Rain = 1
		GAMEMODE.Weather.New.Thunder = 1
		GAMEMODE.Weather.New.Lightning = 1
		GAMEMODE.Weather.New.Wind = 1

	end

	GAMEMODE:SynchWeather()

	return { GAMEMODE.Weather.New.Rain, GAMEMODE.Weather.New.Thunder, GAMEMODE.Weather.New.Lightning, GAMEMODE.Weather.New.Wind }

end

function GM:ManualWeather( rain, thunder, lightning, wind )

	GAMEMODE.Weather.New.Rain = rain
	GAMEMODE.Weather.New.Thunder = thunder
	GAMEMODE.Weather.New.Lightning = lightning
	GAMEMODE.Weather.New.Wind = wind

	GAMEMODE.Weather.NextShift = CurTime() + math.random( 240, 480 )

	GAMEMODE:SynchWeather()

end

function GM:ShiftWeather()

	for k,v in pairs( GAMEMODE.Weather.New ) do

		if v < 0.2 and math.random(1,2) == 1 then

			GAMEMODE.Weather.New[k] = 0

		else

			GAMEMODE.Weather.New[k] = math.Clamp( v + math.Rand( -0.2, math.Rand( 0.2, 0.3 ) ), 0, 1 )

		end

	end

	GAMEMODE:SynchWeather()

end

function GM:WeatherThink()

	if not GAMEMODE.Weather.NextShift then

		GAMEMODE.Weather.NextShift = CurTime() + math.random( 120, 600 )

	elseif GAMEMODE.Weather.NextShift < CurTime() then

		GAMEMODE.Weather.NextShift = nil

		GAMEMODE:ShiftWeather()

	end

end

function GM:SynchWeather()

	net.Start( "WeatherSynch" )

		net.WriteTable( GAMEMODE.Weather.New )

	net.Broadcast()

end

if SERVER then return end

net.Receive( "WeatherSynch", function( len )

	GAMEMODE.Weather.New = net.ReadTable()
	GAMEMODE.Weather.Transition = true
	GAMEMODE.Weather.Inc = 0

	//PrintTable( GAMEMODE.Weather.New )

end )

function GM:ProcessWeather()

	GAMEMODE:RainThink()
	GAMEMODE:ThunderThink()
	GAMEMODE:LightningThink()
	GAMEMODE:WindThink()

	if GAMEMODE.Weather.Transition then

		//local scale = 1 - ( ( GAMEMODE.Weather.Transition - CurTime() ) / GAMEMODE.Weather.TransitionTime )

		if GAMEMODE.Weather.Inc <= CurTime() then

			GAMEMODE.Weather.Inc = CurTime() + 1
			local count = 0

			for k,v in pairs( GAMEMODE.Weather.New ) do

				local diff = math.abs( GAMEMODE.Weather[k] - GAMEMODE.Weather.New[k] )
				local inc = diff / GAMEMODE.Weather.TransitionTime

				GAMEMODE.Weather[k] = math.Approach( GAMEMODE.Weather[k], GAMEMODE.Weather.New[k], inc )

				if GAMEMODE.Weather[k] == GAMEMODE.Weather.New[k] then

					count = count + 1

				end

			end

			if count == 4 then

				GAMEMODE.Weather.Transition = false

			end

		end

	end

end

RainMat = surface.GetTextureID( "effects/rain_warp" )

function GM:PaintWeather()

	if GAMEMODE.Weather.Rain > 0 and render.GetDXLevel() >= 90 then

		for k,v in pairs( GAMEMODE.RainDrops ) do

			local scale = math.Clamp( ( v.Time - CurTime() ) / v.Life, 0, 1 )

			surface.SetDrawColor( 200, 200, 220, 255 * scale )
			surface.SetTexture( RainMat )
			surface.DrawTexturedRect( v.X, v.Y - v.Movement * scale, v.Size, v.Size )

		end

	end

end

function GM:GetSky()

	local tr = util.TraceLine( util.GetPlayerTrace( LocalPlayer(), Vector(0,0,1) ) )

	if tr.HitSky then

		GAMEMODE.LastSkyPos = tr.HitPos
		GAMEMODE.PlayerIsIndoors = false

		GAMEMODE:ComputeSkyBounds()

		return tr.HitPos

	elseif GAMEMODE.LastSkyPos then

		GAMEMODE.PlayerIsIndoors = true

		return GAMEMODE.LastSkyPos

	else

		return LocalPlayer():GetPos()

	end

end

GM.RainDist = 1000
GM.RainDrops = {}

function GM:ComputeSkyBounds()

	local trace = {}
	trace.start = GAMEMODE.LastSkyPos
	trace.endpos = trace.start + Vector( GAMEMODE.RainDist, 0, 0 )

	local tr = util.TraceLine( trace )

	trace = {}
	trace.start = tr.HitPos
	trace.endpos = trace.start + Vector( 0, GAMEMODE.RainDist, 0 )

	tr = util.TraceLine( trace )

	GAMEMODE.RightSkyBound = tr.HitPos

	trace = {}
	trace.start = GAMEMODE.LastSkyPos
	trace.endpos = trace.start + Vector( GAMEMODE.RainDist * -1, 0, 0 )

	tr = util.TraceLine( trace )

	trace = {}
	trace.start = tr.HitPos
	trace.endpos = trace.start + Vector( 0, GAMEMODE.RainDist * -1, 0 )

	tr = util.TraceLine( trace )

	GAMEMODE.LeftSkyBound = tr.HitPos

end

function GM:GetClosestSkyPos()

	local skypos = GAMEMODE:GetSky()
	local pos = LocalPlayer():GetPos()

	local trace = {}
	trace.start = skypos
	trace.endpos = Vector( pos.x, pos.y, skypos.z )

	local tr = util.TraceLine( trace )

	GAMEMODE.LastSkyPos = tr.HitPos

	GAMEMODE:ComputeSkyBounds()

	return tr.HitPos

end

GM.RainSound = Sound( "ambient/weather/rumble_rain_nowind.wav" )

function GM:RainThink()

	for k,v in pairs( GAMEMODE.RainDrops ) do

		if v.Time < CurTime() then

			table.remove( GAMEMODE.RainDrops, k )

			break

		end

	end

	if GAMEMODE.Weather.Rain > 0 and ( GAMEMODE.NextRainDrop or 0 ) < CurTime() and not GAMEMODE.PlayerIsIndoors then

		GAMEMODE.NextRainDrop = CurTime() + math.Rand( 1 - GAMEMODE.Weather.Rain, ( 1.2 - GAMEMODE.Weather.Rain ) * 3 )

		for i=1, math.random( 1, math.floor( GAMEMODE.Weather.Rain * 4 ) ) do

			local tbl = {}

			tbl.Size = math.random( 40, 80 ) + math.random( 0, GAMEMODE.Weather.Rain * 20 )
			tbl.X = math.random( 0, ScrW() - tbl.Size )
			tbl.Y = math.random( 0, ScrH() - tbl.Size )
			tbl.Movement = math.random( 20, 80 )
			tbl.Life = math.Rand( 1.0, 5.0 )
			tbl.Time = CurTime() + tbl.Life

			if math.random(1,5) == 1 then

				tbl.Movement = 10

			end

			table.insert( GAMEMODE.RainDrops, tbl )

		end

	end

	if ( GAMEMODE.NextRain or 0 ) < CurTime() then

		GAMEMODE.NextRain = CurTime() + 0.2

		local amt = math.floor( GAMEMODE.Weather.Rain * 175 * CV_Density:GetFloat() )

		GAMEMODE:SpawnRain( amt )

		if not GAMEMODE.RainNoise then

			GAMEMODE.RainNoise = CreateSound( LocalPlayer(), GAMEMODE.RainSound )
			GAMEMODE.RainNoise:PlayEx( GAMEMODE.Weather.Rain * 0.3, 100 )
			GAMEMODE.RainVolume = GAMEMODE.Weather.Rain * 0.4

		else

			if GAMEMODE.PlayerIsIndoors then

				GAMEMODE.RainVolume = math.Approach( ( GAMEMODE.RainVolume or 0 ), math.max( GAMEMODE.Weather.Rain * 0.05, 0.02 ), 0.01 )

			elseif GAMEMODE.Weather.Rain == 0 then

				GAMEMODE.RainVolume = math.Approach( ( GAMEMODE.RainVolume or 0 ), 0, 0.002 )

			else

				GAMEMODE.RainVolume = math.Approach( ( GAMEMODE.RainVolume or 0 ), math.max( GAMEMODE.Weather.Rain * 0.4, 0.02 ), 0.002 )

			end

			GAMEMODE.RainNoise:ChangeVolume( GAMEMODE.RainVolume, GAMEMODE.RainVolume )

		end

	end

end

function GM:SpawnRain( amt )

	if amt == 0 and not GetGlobalBool( "Radiation", false ) then return end

	local function RainCollision( particle, pos, norm )

		particle:SetDieTime( 0 )

		if math.random(1,6) == 1 then

			local particle = RainEmitter:Add( "effects/blood", pos )
			particle:SetVelocity( Vector(0,0,0) )
			particle:SetLifeTime( 0 )
			particle:SetDieTime( 0.25 )
			particle:SetStartAlpha( 100 )
			particle:SetEndAlpha( 0 )
			particle:SetStartSize( 1 )
			particle:SetEndSize( math.Rand( 3, 10 + GAMEMODE.Weather.Rain * 5 ) )
			particle:SetRoll( math.Rand( -360, 360 ) )
			particle:SetAirResistance( 0 )
			particle:SetCollide( false )
			//particle:SetColor( Color( 200, 200, 250 ) )

		end

	end

	local function CloudCollision( particle, pos, norm )

		particle:SetDieTime( 0 )

	end

	local function RadCollision( particle, pos, norm )

		particle:SetDieTime( math.Rand( 1.0, 3.0 ) )

	end

	local pos = GAMEMODE:GetClosestSkyPos()

	if not pos then return end

	if GetGlobalBool( "Radiation", false ) then

		for i=1, 10 do

			local vec = Vector( math.random( GAMEMODE.LeftSkyBound.x, GAMEMODE.RightSkyBound.x ), math.random( GAMEMODE.LeftSkyBound.y, GAMEMODE.RightSkyBound.y ), pos.z )

			local particle = RainEmitter:Add( "effects/rain_cloud", vec )
			particle:SetVelocity( Vector( 0, 0, math.random( -1000, -800 ) ) + WindVector * math.Rand( 0, 2.0 ) )
			particle:SetLifeTime( 0 )
			particle:SetDieTime( 10 )
			particle:SetStartAlpha( math.random( 5, 10 ) )
			particle:SetEndAlpha( 5 )
			particle:SetStartSize( math.random( 150, 250 ) )
			particle:SetEndSize( 250 )
			particle:SetAirResistance( 0 )
			particle:SetCollide( true )
			particle:SetBounce( 0 )
			particle:SetColor( Color( 100, 250, 50 ) )
			particle:SetCollideCallback( CloudCollision )

			if i < 3 then

				local particle = RainEmitter:Add( "effects/rain_cloud", vec )
				particle:SetVelocity( Vector( 0, 0, math.random( -1000, -800 ) ) + WindVector * math.Rand( 0, 2.0 ) )
				particle:SetLifeTime( 0 )
				particle:SetDieTime( 10 )
				particle:SetStartAlpha( 255 )
				particle:SetEndAlpha( 0 )
				particle:SetStartSize( math.random( 1, 3 ) )
				particle:SetEndSize( 0 )
				particle:SetRoll( math.random( -180, 180 ) )
				particle:SetColor( 100, 200, 0 )
				particle:SetGravity( Vector( 0, 0, -300 ) + WindVector * ( math.sin( CurTime() * 0.01 ) * 20 ) )
				particle:SetCollide( true )
				particle:SetCollideCallback( RadCollision )
				particle:SetBounce( math.Rand( 0, 0.1 ) )

			end

		end

	end

	if amt == 0 then return end

	for i=1, amt do

		local vec = Vector( math.random( GAMEMODE.LeftSkyBound.x, GAMEMODE.RightSkyBound.x ), math.random( GAMEMODE.LeftSkyBound.y, GAMEMODE.RightSkyBound.y ), pos.z )
		local len = math.random( 40, 80 )

		local particle = RainEmitter:Add( "particle/Water/WaterDrop_001a", vec )
		particle:SetVelocity( Vector( 0, 0, math.random( -1000, -800 ) ) )
		particle:SetLifeTime( 0 )
		particle:SetDieTime( 10 )
		particle:SetStartAlpha( math.random( 5, 20 ) )
		particle:SetEndAlpha( 20 )
		particle:SetStartSize( 2.0 )
		particle:SetEndSize( math.Rand( 3.0, 4.0 + GAMEMODE.Weather.Rain ) )
		particle:SetStartLength( len )
		particle:SetEndLength( len + 5 )
		particle:SetAirResistance( 0 )
		particle:SetCollide( true )
		particle:SetBounce( 0 )
		//particle:SetColor( Color( 200, 200, 250 ) )
		particle:SetCollideCallback( RainCollision )

	end

	amt = math.floor( amt * 0.05 ) + 1

	for i=1, amt do

		local vec = Vector( math.random( GAMEMODE.LeftSkyBound.x, GAMEMODE.RightSkyBound.x ), math.random( GAMEMODE.LeftSkyBound.y, GAMEMODE.RightSkyBound.y ), pos.z )

		local particle = RainEmitter:Add( "effects/rain_cloud", vec )
		particle:SetVelocity( Vector( 0, 0, math.random( -1000, -800 ) ) + WindVector * math.Rand( 0, 2.0 ) )
		particle:SetLifeTime( 0 )
		particle:SetDieTime( 10 )
		particle:SetStartAlpha( 3 + math.Rand( 0, GAMEMODE.Weather.Rain * 5 ) )
		particle:SetEndAlpha( 5 )
		particle:SetStartSize( math.random( 100, 200 ) )
		particle:SetEndSize( 200 )
		particle:SetAirResistance( 0 )
		particle:SetCollide( true )
		particle:SetBounce( 0 )
		particle:SetColor( Color( 200, 200, 250 ) )
		particle:SetCollideCallback( CloudCollision )

	end

end

function GM:LightningThink()

	if GAMEMODE.Weather.Lightning == 0 then

		if GAMEMODE.NextLightning and GAMEMODE.NextLightning > CurTime() then

			GAMEMODE:LightUpSky( false )
			GAMEMODE.NextLightning = 0

		end

		return

	end

	local skyname = GetGlobalString( "SkyName" )

	if skyname and not GAMEMODE.SkyMat then

		GAMEMODE.SkyMat = {}
        GAMEMODE.SkyMat[1] = Material("skybox/" .. skyname .. "up")
        GAMEMODE.SkyMat[2] = Material("skybox/" .. skyname .. "dn")
        GAMEMODE.SkyMat[3] = Material("skybox/" .. skyname .. "lf")
        GAMEMODE.SkyMat[4] = Material("skybox/" .. skyname .. "rt")
        GAMEMODE.SkyMat[5] = Material("skybox/" .. skyname .. "bk")
        GAMEMODE.SkyMat[6] = Material("skybox/" .. skyname .. "ft")

		GAMEMODE.OldSky = {}
		GAMEMODE.LitUp = {}

		skyname = "sky_day01_01"

		GAMEMODE.NewSky = {}
		GAMEMODE.NewSky[1] = Material("skybox/" .. skyname .. "up")
        GAMEMODE.NewSky[2] = Material("skybox/" .. skyname .. "dn")
        GAMEMODE.NewSky[3] = Material("skybox/" .. skyname .. "lf")
        GAMEMODE.NewSky[4] = Material("skybox/" .. skyname .. "rt")
        GAMEMODE.NewSky[5] = Material("skybox/" .. skyname .. "bk")
        GAMEMODE.NewSky[6] = Material("skybox/" .. skyname .. "ft")

	end

	if ( GAMEMODE.NextLightning or 0 ) < CurTime() then

		if not GAMEMODE.FlashInterval then

			GAMEMODE.FlashInterval = CurTime() + math.Rand( 1.0, 2.5 )
			GAMEMODE.FlashTime = CurTime() + math.Rand( 0, 0.2 )
			GAMEMODE.FlashToggle = CurTime() + math.Rand( 0, 0.8 )

			sound.Play( table.Random( GAMEMODE.Thunder ), LocalPlayer():GetShootPos(), 150, math.random( 80, 110 ), 0.2 )

		end

		GAMEMODE:LightUpSky( GAMEMODE.FlashTime >= CurTime() )

		if GAMEMODE.FlashToggle < CurTime() then

			GAMEMODE.FlashTime = CurTime() + math.Rand( 0, 0.2 )
			GAMEMODE.FlashToggle = CurTime() + math.Rand( 0, 0.8 )

		end

		if GAMEMODE.FlashInterval < CurTime() then

			GAMEMODE.FlashInterval = nil
			GAMEMODE.NextLightning = CurTime() + math.random( 4, 8 + ( 1.0 - GAMEMODE.Weather.Lightning ) * 50 )
			GAMEMODE:LightUpSky( false )

		end

	end

end

function GM:LightUpSky( bool )

	if bool then

		for k,v in pairs( GAMEMODE.SkyMat ) do

			if not GAMEMODE.LitUp[k] then

				GAMEMODE.OldSky[k] = v:GetTexture( "$basetexture" )
				GAMEMODE.LitUp[k] = true

				v:SetTexture( "$basetexture", GAMEMODE.NewSky[k]:GetTexture( "$basetexture" ) )

			end

		end

	else

		for k,v in pairs( GAMEMODE.SkyMat ) do

			if GAMEMODE.LitUp and GAMEMODE.LitUp[k] then

				GAMEMODE.LitUp[k] = false

				v:SetTexture( "$basetexture", GAMEMODE.OldSky[k] )

			end

		end

	end

end

function GM:WindThink()

	if GAMEMODE.Weather.Wind == 0 then return end

	if ( GAMEMODE.NextWind or 0 ) < CurTime() then

		local vol = math.max( GAMEMODE.Weather.Wind, 0.2 )
		local snd = table.Random( GAMEMODE.Wind )

		if GAMEMODE.PlayerIsIndoors then

			vol = 0.1

		end

		sound.Play( snd, LocalPlayer():GetShootPos(), 150, math.random( 80, 110 ), vol )

		GAMEMODE.NextWind = CurTime() + math.random( 1, SoundDuration( snd ) + ( 1.0 - GAMEMODE.Weather.Wind ) * 30 + math.Rand( -2, 2 ) )

	end

end

function GM:ThunderThink()

	if GAMEMODE.Weather.Thunder == 0 then return end

	if ( GAMEMODE.NextThunder or 0 ) < CurTime() then

		GAMEMODE.NextThunder = CurTime() + math.random( 4, 8 + ( 1.0 - GAMEMODE.Weather.Thunder ) * 50 )

		local vol = math.max( GAMEMODE.Weather.Thunder, 0.2 )

		if GAMEMODE.PlayerIsIndoors then

			vol = 0.1

		end

		sound.Play( table.Random( GAMEMODE.Thunder ), LocalPlayer():GetShootPos(), 150, math.random( 80, 110 ), vol )

	end

end