aboutsummaryrefslogtreecommitdiff
path: root/gamemode/shared/stream.lua
blob: e4d45e82e1afb4430e14b9f7c8f1dfeea6e0bd2c (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
local meta = FindMetaTable("Player")

if (SERVER) then
	util.AddNetworkString("STREAM")
	util.AddNetworkString("ENDSTREAM")
	
	concommand.Add("StreamURL",function(pl,com,args)
		if (!IsValid(pl)) then return end
		
		pl:StreamSong(table.concat(args," "))
	end)
	
	concommand.Add("EndStream",function(pl,com,args)
		if (!IsValid(pl)) then return end
		
		pl:EndStream()
	end)
	
	function meta:StreamSong(URL)
		if (!self:IsAdmin()) then return end
		
		self.URL = URL
		
		net.Start("STREAM")
			net.WriteEntity(self)
			net.WriteString(URL)
		net.Broadcast()
	end
	
	function meta:EndStream()
		if (!self:IsAdmin()) then return end
		
		self.URL = nil
		
		net.Start("ENDSTREAM")
			net.WriteEntity(self)
		net.Broadcast()
	end
	
	function meta:UpdateStream(v)
		if (v.URL) then
			net.Start("STREAM")
				net.WriteEntity(v)
				net.WriteString(v.URL)
			net.Send(self)
		end
	end
else
	local Emitter 	= ParticleEmitter( Vector(0,0,0) )
	local Up		= Vector(0,0,20)
	local Retries	= 0
	local Streams 	= {}
	
	function TryURL(url,pl)
		if (!IsValid(pl)) then return end
		
		local ID = pl:UniqueID()
		if (Streams[ID]) then Streams[ID]:Stop() Streams[ID] = nil end
		
		if (Retries < 4) then
			sound.PlayURL( url, "3d mono noplay", function( chan )
				if (!chan) then TryURL(url,pl) Retries = Retries+1
				elseif (IsValid(pl)) then Streams[ID] = chan chan:Play() Retries = 0 end
			end)
		else
			Retries = 0
			Msg("Couldn't play "..url.." \n")
		end
	end
	
	net.Receive("STREAM",function()
		local pl = net.ReadEntity()
		local URL = net.ReadString()
		
		if (!IsValid(pl)) then return end
		
		TryURL(URL,pl)
	end)
	
	net.Receive("ENDSTREAM",function()
		local pl = net.ReadEntity()
		
		if (!IsValid(pl)) then return end
		
		local ID = pl:UniqueID()
		if (Streams[ID]) then Streams[ID]:Stop() Streams[ID] = nil end
	end)
		
	hook.Add("Think","Streamer",function()
		for k,st in pairs(Streams) do
			if (!st) then table.remove(Streams,k)
			else
				local v = player.GetByUniqueID(k)
				
				if (!IsValid(v)) then 
					st:Stop() 
					table.remove(Streams,k)
				else
					local pig = v:GetPigeon()
					local Pos = v:GetPos()
					
					if (IsValid(pig)) then Pos = pig:GetPos() end
					
					if (!v.PTime or v.PTime < CurTime()) then
						local particle = Emitter:Add( "lam/musicnotes/note"..math.random(1,2), Pos + VectorRand()*15)
						particle:SetDieTime( 1 )
						particle:SetVelocity( Up )
						
						particle:SetStartAlpha( 250 )
						particle:SetEndAlpha( 0 )
						
						particle:SetStartSize( 10 )
						particle:SetEndSize( 10 )
						
						particle:SetColor( math.random( 0, 250 ), math.random( 0, 250 ), math.random( 0, 250 ) )
						
						v.PTime = CurTime()+0.1
					end
					
					st:SetPos(Pos)
				end
			end
		end
	end)
end