local matRefraction	= Material("refract_ring")
local tMats = {}
tMats.Glow1 = Material("sprites/nuke_light_glow02")
tMats.Glow2 = Material("sprites/nuke_yellowflare")
tMats.Glow3 = Material("sprites/nuke_redglow2")

function EFFECT:Init(data)
	self.Position = data:GetOrigin()
	self.Position.z = self.Position.z + 4
	self.Yield = data:GetMagnitude()
	self.YieldFast = self.Yield^1.4
	self.YieldSlow = self.Yield^0.75
	self.YieldSlowest = self.Yield^0.5
	self.YieldInverse = self.Yield^-1
	self.TimeLeft = CurTime() + 24
	self.FAlpha = 255
	self.GAlpha = 254
	self.GSize = 100*self.YieldSlow
	self.CloudHeight = data:GetScale()
	self.Refract = 0
	self.Size = 24
	if render.GetDXLevel() <= 81 then
		matRefraction = Material("effects/strider_pinch_dudv")
	end
	local Pos = self.Position
	self.smokeparticles = {}
	self.Emitter = ParticleEmitter(Pos)
	for i=1, 280 do
		local vecang = Vector(math.Rand(-32,32),math.Rand(-32,32),math.Rand(-18,18)):GetNormalized()
		local Flame = self.Emitter:Add("particles/flamelet"..math.random(1,5), Pos + self.YieldSlow*(vecang*(math.Rand(250,690))))
		vecang.z = vecang.z + self.YieldSlowest*1.4
		Flame:SetVelocity(math.Rand(30,33)*vecang)
		Flame:SetDieTime(math.Rand(23, 26))
		Flame:SetStartAlpha(math.Rand(230, 250))
		Flame:SetStartSize(self.YieldSlow*math.Rand(280, 360))
		Flame:SetEndSize(self.YieldSlow*math.Rand(540, 630))
		Flame:SetRoll(math.Rand(480, 540))
		Flame:SetRollDelta(self.YieldInverse*math.Rand(-1, 1))
		Flame:SetColor(math.Rand(150,255), math.Rand(100,150), 100)
	end
	for i=1, 27 do
		local vecang = VectorRand()*8
		local spawnpos = Pos + self.YieldSlowest*256*vecang
		for k=5,26 do
			local Flame2 = self.Emitter:Add("particles/flamelet"..math.random(1,5), spawnpos - vecang*9*k)
			Flame2:SetVelocity(vecang*math.Rand(2,3) + Vector(0,0,math.Rand(15,20)))
			Flame2:SetDieTime(math.Rand(22, 24))
			Flame2:SetStartAlpha(math.Rand(230, 250))
			Flame2:SetStartSize(self.YieldSlow*(k*math.Rand(3, 4))^1.2)
			Flame2:SetEndSize(self.YieldSlow*(k*math.Rand(5, 6))^1.2)
			Flame2:SetRoll(math.Rand(20, 80))
			Flame2:SetRollDelta(self.YieldInverse*math.Rand(-1, 1))
			Flame2:SetColor(math.Rand(150,255), math.Rand(100,150), 100)
		end
	end
	for i=1, 320 do
		local vecang = Vector(math.Rand(-32,32),math.Rand(-32,32),math.Rand(-18,18)):GetNormalized()
		local Smoke = self.Emitter:Add("particles/smokey", Pos + self.YieldSlow*(vecang*(math.Rand(2,665))))
		local startalpha = math.Rand(0, 7)
		vecang.z = vecang.z + self.YieldSlowest*1.5
		Smoke:SetVelocity(math.Rand(40,44)*vecang)
		Smoke:SetLifeTime(math.Rand(-16, -10))
		Smoke:SetDieTime(23)
		Smoke:SetStartAlpha(startalpha)
		Smoke:SetEndAlpha(20 + startalpha)
		Smoke:SetStartSize(self.YieldSlow*math.Rand(300, 400))
		Smoke:SetEndSize(self.YieldSlow*math.Rand(600, 800))
		Smoke:SetRoll(math.Rand(480, 540))
		Smoke:SetRollDelta(self.YieldInverse*math.Rand(-1, 1))
		Smoke:SetColor(230,230,230)
		table.insert(self.smokeparticles, Smoke)
	end
end

function EFFECT:Think()
	local timeleft = self.TimeLeft - CurTime()
	if timeleft > 0 then
		local ftime = FrameTime()
		if self.FAlpha > 0 then
			self.FAlpha = self.FAlpha - 150*ftime
		end
		self.GAlpha = self.GAlpha - 10.5*ftime
		self.GSize = self.GSize - 0.12*timeleft*ftime*self.YieldSlow
		self.Size = self.Size + 1200*ftime
		self.Refract = self.Refract + 1.3*ftime
		return true
	else
		self.Emitter:Finish()
		for _,particle in pairs(self.smokeparticles) do
			particle:SetStartAlpha(20)
			particle:SetEndAlpha(0)
		end
		return false
	end
end

function EFFECT:Render()
	local startpos = self.Position
	render.SetMaterial(tMats.Glow1)
	render.DrawSprite(startpos, 400*self.GSize,90*self.GSize,Color(255,240,220,self.GAlpha))
	render.DrawSprite(startpos, 70*self.GSize,280*self.GSize,Color(255,240,220,0.7*self.GAlpha))
	if self.FAlpha > 0 then
		render.DrawSprite(startpos + Vector(0,0,256),50*(self.GSize^2),35*(self.GSize^2),Color(255,245,235,self.FAlpha))
	end
	render.SetMaterial(tMats.Glow2)
	render.DrawSprite(startpos, 700*self.GSize,550*self.GSize,Color(255,50,10,self.GAlpha))
	render.SetMaterial(tMats.Glow3)
	render.DrawSprite(startpos, 56*self.GSize,600*self.GSize,Color(130,120,240,0.5*self.GAlpha))
	render.DrawSprite(startpos, 700*self.GSize,70*self.GSize,Color(80,73,255,self.GAlpha))
	if self.Size < 32768 then
		local Distance = EyePos():Distance(self.Entity:GetPos())
		local Pos = self.Entity:GetPos() + (EyePos() - self.Entity:GetPos()):GetNormal() * Distance * (self.Refract^(0.3)) * 0.8
		matRefraction:SetFloat("$refractamount", math.sin(self.Refract * math.pi) * 0.1)
		render.SetMaterial(matRefraction)
		render.UpdateRefractTexture()
		render.DrawSprite(Pos, self.Size, self.Size)
	end
end