if CLIENT then //only executes on the clinet (noone wil see it on the server, so why would you run it there?)
	
local parts = {}

local Power = true
local sound = false

local MaxFlakes
local Col
//Add: Perhaps "parenting" to hit entity?

//Controlls the snoflakes as they fall
function SnowControll()
	MaxFlakes = GetConVarNumber("snow_max_flakes") //get max flakes
	
	if(Power) then
		//only play sound once when you trun it on
		if !sound then
			LocalPlayer():EmitSound("ambient/wind/wind_gusty1.wav", 35, 100)
			sound = true
		end
		
		// only spawn flakes if we can
		if(MaxFlakes == -1 or table.Count(parts) < MaxFlakes) then
			local low
			
			//Vel based switch that spawns the parts lower if moving (better effect)
			if(LocalPlayer():GetVelocity():Length() <= 100) then
				low = 350
			else
				low  = 50
			end
			
			//set the emiiters pos (this doesnt really matter, but I like it near where I'm spawning the particles)
			local EmmiterPos = LocalPlayer():GetPos()+LocalPlayer():GetVelocity()
			local ParticlePos = EmmiterPos+Vector(math.random(-300,300),math.random(-300,300),math.random(low,450))
			
			Add_Snowflake(EmmiterPos,ParticlePos)
		end	
		
		//loop throguh all flakes
		for i=1, table.Count(parts) do	
			//if we find that there isnt a flake in the table slot, remvoe slot
			if(parts[i] == nil) then
				table.remove(parts,i)
			else
				local part = parts[i]
				
				//if the particle is still alive
				if(part:GetLifeTime() < part:GetDieTime()) then
				
					local dist = part:GetPos():Distance(LocalPlayer():GetPos()) // distance to you
					local NewVel = part:GetVelocity() + Vector(math.random(-2,2),math.random(-2,2),0) // calculate a random velocity
					local maxa = GetConVarNumber("snow_alpha") //maximum alpha based on what the user set
					
					local Trace = util.QuickTrace(part:GetPos(),Vector(0,0,-1.5)) //a downward trace to detect normal collitions
					local VelTrace = util.QuickTrace(part:GetPos(),part:GetVelocity():Normalize()*1.5) // a velocity based trace todo thesame
					
					part:SetVelocity(NewVel) //set the new vel
					//part:SetColor(Col.r,Col.g,Col.b)
					
					// if the article hit something
					if(Trace.Hit or VelTrace.Hit) then
						part:SetVelocity(Vector(0,0,0)) //stop it
						part:SetEndSize(0)
						
						if(part:GetDieTime() == 20) then //make it so it takes 4 secodns to melt (will reaplce with a new, parented particle evetualy.
							part:SetDieTime(part:GetLifeTime()+4)
							table.remove(parts,i)
						end						
					end
					
					//distance based fade. Will probaby make it configurable
					if(dist > 300 and dist < 300 + maxa) then
						part:SetStartAlpha(maxa-(dist-300))
					elseif(dist > 300 + maxa) then
						part:SetStartAlpha(10)
					else
						part:SetStartAlpha(maxa)
					end
					
					//kills the particle if it's to far away (have ot set it this high otherwsie they dont properly fall when moving)
					if(part:GetPos():Distance(LocalPlayer():GetPos()) >= 1000) then
						part:SetDieTime(part:GetLifeTime()-0.25)
						table.remove(parts,i)
					end
				else
					table.remove(parts,i)
				end
			end
		end
	else
		//kills the sound and flushes the table when we turn it off
		sound = false
		table.Empty(parts)
		FlakeCount = 0
	end
end

// particle/cloud
function Add_Snowflake(EmmiterPos, ParticlePos)
	//had ot have this in there, because even though i had it up top, it was still spawning snow
	if(Power and (MaxFlakes == -1 or table.Count(parts) < MaxFlakes)) then
		//gets the set color
		Col = Color(GetConVarNumber("snow_colr"),GetConVarNumber("snow_colg"),GetConVarNumber("snow_colb"),GetConVarNumber("snow_alpha"))
		
		local em = ParticleEmitter(EmmiterPos)
		local part = em:Add("particle/snow",ParticlePos)
		addToTable(part) //ads the particle to our handy-dandy table.

		if part then
			part:SetColor(Col.r,Col.g,Col.b)
			part:SetRoll(math.random(0, 2*3.141596))
			part:SetStartSize(math.random(0.5, 1))
			
			part:SetCollide(1) //bult-in world based collition, a backup if mine fails so you dont overload your game with particles
			part:SetLifeTime(0)
			part:SetDieTime(20)
			part:SetEndSize(part:GetStartSize())
			part:SetEndAlpha(25)
			
			part:SetVelocity(Vector(0,0,-math.random(33,38)))
			
		end
		//kills the emmiter
		em:Finish()
	end
end

//Adds particles to table, this is the safest way tha ti could think of and what fixed the collition bug.
function addToTable(part)
	local added = false
	local count = table.Count(parts)
	for i=1, count do
		if(!added and parts[i] == nil) then
			parts[i] = part
			added = true
		end
	end
	if (!added) then
		parts[count] = part
	end
end

// this turns is on and off
concommand.Add("snow_power",function()
	Power = not Power
	if(Power)then
		chat.AddText(Color(255,255,255,255),"Snow Script: ", Color(0,255,0,255),"Enabled")
	else
		chat.AddText(Color(255,255,255,255),"Snow Script: ", Color(255,0,0,255),"Disabled")
	end
end)
end