function gadget:GetInfo()
	return {
		name = "CEG Replacer",
		desc = "Replaces CEGs with P.O.P.S. events",
		author = "Argh",
		date = "December 19th, 2009",
		license = "GPL, v. 2 or later, 2009",
		layer = 1,
		enabled = true,
	}
end

local PB = {fastDustParticles = {}, dustParticles = {}, flameParticles = {}, chunkParticles ={}} --PB stands for "pre-built"
if (gadgetHandler:IsSyncedCode()) then
--------------------------------------------------------------------------------
--  SYNCED
--------------------------------------------------------------------------------
local sunR, sunG, sunB

function gadget:RecvLuaMsg(msg, playerID)
	--Spring.Echo("message received, "..msg)
	if string.find(msg,"sunvalueR") then
		msg = msg:gsub("sunvalueR","")
		sunR = tonumber(msg)
	end
	if string.find(msg,"sunvalueG") then
		msg = msg:gsub("sunvalueG","")
		sunG = tonumber(msg)
	end
	if string.find(msg,"sunvalueB") then
		msg = msg:gsub("sunvalueB","")
		sunB = tonumber(msg)
	end
end

local function BuildChunkParticles()
	local myType, gravity, velX, velY, velZ, size, drag, color1R, color1G, color1B, color1A, color2R, color2G, color2B, color2A, color3R, color3G, color3B, color3A, color4R, color4G, color4B, color4A
	local math_random = math.random
	local distScale = 1000000

	for i = 1,1000 do
			color1R = 1.0 * sunR
			color1G = 1.0 * sunG
			color1B = 1.0 * sunB
			color1A = 1.0
			
			color2R = 1.0 * sunR
			color2G = 1.0 * sunG
			color2B = 1.0 * sunB
			color2A = 1.0

			color3R = 1.0 * sunR
			color3G = 1.0 * sunG
			color3B = 1.0 * sunB
			color3A = 1.0

			color4R = 0.0
			color4G = 0.0
			color4B = 0.0
			color4A = 0.0

			size = math_random(2,7) / distScale

			drag = 0.12 / distScale
			gravity = 30 / distScale

			velX = math_random(-1000,1000)/1000
			velY = math_random(-1000,1000)/1000
			velZ = math_random(-1000,1000)/1000

			myType = math_random(33,40) / distScale

			sizeGrowth = 0
			if sizeGrowth < 0 then sizeGrowth = sizeGrowth * -1 + 0.5 end

			myVelocity = math_random(5,20) -- VELOCITY MIN/MAX!
			startTime = 0 -- shrink enough to fit into color space
			finishTime = math_random(9000,10000) / 1000 -- shrink enough to fit into color space
			myRand = math_random() -- should be left alone, unless you really need to change this.
			myCone = math_random(-90000,90000)/1000 -- cone value, like CEG, 9000 == 90 degrees


			PB.chunkParticles[i] = {gravity = gravity,velX = velX,velY = velY,velZ = velZ,size = size,drag = drag,sizeGrowth = sizeGrowth,myVelocity = myVelocity,startTime = startTime, finishTime = finishTime,myRand = myRand,myType = myType, myCone = myCone, color1R = color1R, color1G = color1G, color1B = color1B, color1A = color1A, color2R = color2R, color2G = color2G, color2B = color2B, color2A = color2A, color3R = color3R, color3G = color3G, color3B = color3B, color3A = color3A, color4R = color4R, color4G = color4G, color4B = color4B, color4A = color4A}
	end
end

local function BuildFastDustParticles()
	local myType, gravity, velX, velY, velZ, size, drag, color1R, color1G, color1B, color1A, color2R, color2G, color2B, color2A, color3R, color3G, color3B, color3A, color4R, color4G, color4B, color4A
	local math_random = math.random
	local distScale = 1000000

	for i = 1,1000 do
			color1R = 1.0 * sunR
			color1G = 1.0 * sunG
			color1B = 1.0 * sunB
			color1A = 1.0
			
			color2R = 1.0 * sunR
			color2G = 1.0 * sunG
			color2B = 1.0 * sunB
			color2A = 1.0

			color3R = 1.0 * sunR
			color3G = 1.0 * sunG
			color3B = 1.0 * sunB
			color3A = 1.0

			color4R = 0.0
			color4G = 0.0
			color4B = 0.0
			color4A = 0.0

			size = math_random(5,15) / distScale

			drag = 0.12 / distScale
			gravity = 30 / distScale

			velX = math_random(-1000,1000)/1000
			velY = math_random(-1000,1000)/1000
			velZ = math_random(-1000,1000)/1000

			myType = math_random(5,6) / distScale

			sizeGrowth = 0
			if sizeGrowth < 0 then sizeGrowth = sizeGrowth * -1 + 0.5 end

			myVelocity = math_random(5,15) -- VELOCITY MIN/MAX!
			startTime = 0 -- shrink enough to fit into color space
			finishTime = math_random(1000,3000) / 1000 -- shrink enough to fit into color space
			myRand = math_random() -- should be left alone, unless you really need to change this.
			myCone = math_random(-90000,90000)/1000 -- cone value, like CEG, 9000 == 90 degrees


			PB.fastDustParticles[i] = {gravity = gravity,velX = velX,velY = velY,velZ = velZ,size = size,drag = drag,sizeGrowth = sizeGrowth,myVelocity = myVelocity,startTime = startTime, finishTime = finishTime,myRand = myRand,myType = myType, myCone = myCone, color1R = color1R, color1G = color1G, color1B = color1B, color1A = color1A, color2R = color2R, color2G = color2G, color2B = color2B, color2A = color2A, color3R = color3R, color3G = color3G, color3B = color3B, color3A = color3A, color4R = color4R, color4G = color4G, color4B = color4B, color4A = color4A}
	end
end

local function BuildDustParticles()
	local myType, gravity, velX, velY, velZ, size, drag, color1R, color1G, color1B, color1A, color2R, color2G, color2B, color2A, color3R, color3G, color3B, color3A, color4R, color4G, color4B, color4A
	local math_random = math.random
	local distScale = 1000000

	for i = 1,1000 do
			local randomVal = math.random(1000,2000) / 10000			

			color1R = 0.1 + randomVal * sunR
			color1G = 0.1 + randomVal * sunG
			color1B = 0.1 + randomVal * sunB
			color1A = 1.0
			
			color2R = 0.1 + randomVal * sunR
			color2G = 0.1 + randomVal * sunG
			color2B = 0.1 + randomVal * sunB
			color2A = 1.0

			color3R = 0.1  + randomVal * sunR
			color3G = 0.1 + randomVal * sunG
			color3B = 0.1 + randomVal * sunB
			color3A = 1.0

			color4R = 0.0
			color4G = 0.0
			color4B = 0.0
			color4A = 0.0

			size = math.random(1,15) / distScale

			drag = 0.5 / distScale
			gravity = (math_random(1,10) / distScale) + 0.5 --negative

			velX = 0
			velY = 1
			velZ = 0

			myType = math_random(1,4) / distScale

			sizeGrowth = math_random(10,200) / 1000 / distScale
			if sizeGrowth < 0 then sizeGrowth = sizeGrowth * -1 + 0.5 end

			myVelocity = math_random(1,35) -- VELOCITY MIN/MAX!
			startTime = 0 -- shrink enough to fit into color space
			finishTime = math_random(3000,15000) / 1000 -- shrink enough to fit into color space
			myRand = math_random() -- should be left alone, unless you really need to change this.
			myCone = math_random(-90000,90000)/1000 -- cone value, like CEG, 9000 == 90 degrees

			PB.dustParticles[i] = {gravity = gravity,velX = velX,velY = velY,velZ = velZ,size = size,drag = drag,sizeGrowth = sizeGrowth,myVelocity = myVelocity,startTime = startTime, finishTime = finishTime,myRand = myRand,myType = myType, myCone = myCone, color1R = color1R, color1G = color1G, color1B = color1B, color1A = color1A, color2R = color2R, color2G = color2G, color2B = color2B, color2A = color2A, color3R = color3R, color3G = color3G, color3B = color3B, color3A = color3A, color4R = color4R, color4G = color4G, color4B = color4B, color4A = color4A}
	end
end

local function BuildFlameParticles()
	local myType, gravity, velX, velY, velZ, size, drag, color1R, color1G, color1B, color1A, color2R, color2G, color2B, color2A, color3R, color3G, color3B, color3A, color4R, color4G, color4B, color4A
	local math_random = math.random
	local distScale = 1000000

	for i = 1,1000 do

			color1R = 1.0
			color1G = 1.0
			color1B = 1.0
			color1A = 0.1

			color2R = 1.0
			color2G = 1.0
			color2B = 1.0
			color2A = 0.3

			color3R = 1.0
			color3G = 1.0
			color3B = 1.0
			color3A = 0.5

			color4R = 0.0
			color4G = 0.0
			color4B = 0.0
			color4A = 0.0

			size = 10 / distScale

			drag = 0.9 / distScale
			gravity = (math_random(15,30) / distScale) + 0.5 --negative

			velX = math_random(-1000,1000)/1000
			velY = math_random(-1000,1000)/1000
			velZ = math_random(-1000,1000)/1000

			myType = math_random(9,16) / distScale

			sizeGrowth = math_random(1500,3500) / 1000 / distScale
			if sizeGrowth < 0 then sizeGrowth = sizeGrowth * -1 + 0.5 end

			myVelocity = math_random(5,15) -- VELOCITY MIN/MAX!
			startTime = 0 -- shrink enough to fit into color space
			finishTime = math_random(500,800) / 1000 -- shrink enough to fit into color space
			myRand = math_random() -- should be left alone, unless you really need to change this.
			myCone = math_random(-90000,90000)/1000 -- cone value, like CEG, 9000 == 90 degrees

			PB.flameParticles[i] = {gravity = gravity,velX = velX,velY = velY,velZ = velZ,size = size,drag = drag,sizeGrowth = sizeGrowth,myVelocity = myVelocity,startTime = startTime, finishTime = finishTime,myRand = myRand,myType = myType, myCone = myCone, color1R = color1R, color1G = color1G, color1B = color1B, color1A = color1A, color2R = color2R, color2G = color2G, color2B = color2B, color2A = color2A, color3R = color3R, color3G = color3G, color3B = color3B, color3A = color3A, color4R = color4R, color4G = color4G, color4B = color4B, color4A = color4A}
	end
end

local function buildPOP(pX, pY, pZ,POPType)
	local math_random = math.random
	local distScale = 1000000
	local index

	if POPType == 1 then
		for moveX = -64,64,32 do
		for moveZ = -64,64,32 do
			if moveX > 32 or moveX < -32 and moveZ >32 or moveZ < -32 then

				for i = 1,5 do
					index = math_random(1,1000)
SendToUnsynced("pops_event",(pX + moveX) / distScale,pY / distScale,(pZ + moveZ) / distScale,PB.fastDustParticles[index].gravity,PB.fastDustParticles[index].velX,PB.fastDustParticles[index].velY,PB.fastDustParticles[index].velZ,PB.fastDustParticles[index].size,PB.fastDustParticles[index].drag,PB.fastDustParticles[index].sizeGrowth,PB.fastDustParticles[index].myVelocity,PB.fastDustParticles[index].startTime,PB.fastDustParticles[index].finishTime,PB.fastDustParticles[index].myRand,PB.fastDustParticles[index].myType,PB.fastDustParticles[index].myCone,PB.fastDustParticles[index].color1R, PB.fastDustParticles[index].color1G, PB.fastDustParticles[index].color1B, PB.fastDustParticles[index].color1A, PB.fastDustParticles[index].color2R, PB.fastDustParticles[index].color2G, PB.fastDustParticles[index].color2B, PB.fastDustParticles[index].color2A, PB.fastDustParticles[index].color3R, PB.fastDustParticles[index].color3G, PB.fastDustParticles[index].color3B, PB.fastDustParticles[index].color3A, PB.fastDustParticles[index].color4R, PB.fastDustParticles[index].color4G, PB.fastDustParticles[index].color4B, PB.fastDustParticles[index].color4A)
				end

				for i = 1,5 do
					index = math_random(1,1000)
SendToUnsynced("pops_event",(pX + moveX) / distScale,pY / distScale,(pZ + moveZ) / distScale,PB.flameParticles[index].gravity,PB.flameParticles[index].velX,PB.flameParticles[index].velY,PB.flameParticles[index].velZ,PB.flameParticles[index].size,PB.flameParticles[index].drag,PB.flameParticles[index].sizeGrowth,PB.flameParticles[index].myVelocity,PB.flameParticles[index].startTime,PB.flameParticles[index].finishTime,PB.flameParticles[index].myRand,PB.flameParticles[index].myType,PB.flameParticles[index].myCone,PB.flameParticles[index].color1R, PB.flameParticles[index].color1G, PB.flameParticles[index].color1B, PB.flameParticles[index].color1A, PB.flameParticles[index].color2R, PB.flameParticles[index].color2G, PB.flameParticles[index].color2B, PB.flameParticles[index].color2A, PB.flameParticles[index].color3R, PB.flameParticles[index].color3G, PB.flameParticles[index].color3B, PB.flameParticles[index].color3A, PB.flameParticles[index].color4R, PB.flameParticles[index].color4G, PB.flameParticles[index].color4B, PB.flameParticles[index].color4A)
				end

				for i = 1,3 do
					index = math_random(1,1000)
SendToUnsynced("pops_event",(pX + moveX) / distScale,pY / distScale,(pZ + moveZ) / distScale,PB.chunkParticles[index].gravity,PB.chunkParticles[index].velX,PB.chunkParticles[index].velY,PB.chunkParticles[index].velZ,PB.chunkParticles[index].size,PB.chunkParticles[index].drag,PB.chunkParticles[index].sizeGrowth,PB.chunkParticles[index].myVelocity,PB.chunkParticles[index].startTime,PB.chunkParticles[index].finishTime,PB.chunkParticles[index].myRand,PB.chunkParticles[index].myType,PB.chunkParticles[index].myCone,PB.chunkParticles[index].color1R, PB.chunkParticles[index].color1G, PB.chunkParticles[index].color1B, PB.chunkParticles[index].color1A, PB.chunkParticles[index].color2R, PB.chunkParticles[index].color2G, PB.chunkParticles[index].color2B, PB.chunkParticles[index].color2A, PB.chunkParticles[index].color3R, PB.chunkParticles[index].color3G, PB.chunkParticles[index].color3B, PB.chunkParticles[index].color3A, PB.chunkParticles[index].color4R, PB.chunkParticles[index].color4G, PB.chunkParticles[index].color4B, PB.chunkParticles[index].color4A)
				end


				for i = 1,5 do
					index = math_random(1,1000)
SendToUnsynced("pops_slow_event",(pX + moveX) / distScale,pY / distScale,(pZ + moveZ) / distScale,PB.dustParticles[index].gravity,PB.dustParticles[index].velX,PB.dustParticles[index].velY,PB.dustParticles[index].velZ,PB.dustParticles[index].size,PB.dustParticles[index].drag,PB.dustParticles[index].sizeGrowth,PB.dustParticles[index].myVelocity,PB.dustParticles[index].startTime,PB.dustParticles[index].finishTime,PB.dustParticles[index].myRand,PB.dustParticles[index].myType,PB.dustParticles[index].myCone,PB.dustParticles[index].color1R, PB.dustParticles[index].color1G, PB.dustParticles[index].color1B, PB.dustParticles[index].color1A, PB.dustParticles[index].color2R, PB.dustParticles[index].color2G, PB.dustParticles[index].color2B, PB.dustParticles[index].color2A, PB.dustParticles[index].color3R, PB.dustParticles[index].color3G, PB.dustParticles[index].color3B, PB.dustParticles[index].color3A, PB.dustParticles[index].color4R, PB.dustParticles[index].color4G, PB.dustParticles[index].color4B, PB.dustParticles[index].color4A)
				end
			else

				for i = 1,10 do
					index = math_random(1,1000)
SendToUnsynced("pops_slow_event",(pX + moveX) / distScale,pY / distScale,(pZ + moveZ) / distScale,PB.dustParticles[index].gravity,PB.dustParticles[index].velX,PB.dustParticles[index].velY,PB.dustParticles[index].velZ,PB.dustParticles[index].size,PB.dustParticles[index].drag,PB.dustParticles[index].sizeGrowth,PB.dustParticles[index].myVelocity,PB.dustParticles[index].startTime,PB.dustParticles[index].finishTime,PB.dustParticles[index].myRand,PB.dustParticles[index].myType,PB.dustParticles[index].myCone,PB.dustParticles[index].color1R, PB.dustParticles[index].color1G, PB.dustParticles[index].color1B, PB.dustParticles[index].color1A, PB.dustParticles[index].color2R, PB.dustParticles[index].color2G, PB.dustParticles[index].color2B, PB.dustParticles[index].color2A, PB.dustParticles[index].color3R, PB.dustParticles[index].color3G, PB.dustParticles[index].color3B, PB.dustParticles[index].color3A, PB.dustParticles[index].color4R, PB.dustParticles[index].color4G, PB.dustParticles[index].color4B, PB.dustParticles[index].color4A)
				end
			end
		end
		end
	end
end

function makePOP(unitID, ud, team, location, POP)
	local GetUnitScriptPiece = Spring.GetUnitScriptPiece
	local GetUnitPiecePosDir = Spring.GetUnitPiecePosDir

	local p = GetUnitScriptPiece(unitID,location)
	local x,y,z = GetUnitPiecePosDir(unitID,p)

	buildPOP(x,y,z,POP)
end
gadgetHandler:RegisterGlobal("makePOP", makePOP)


function gadget:GameFrame(f)
	if f == 1 then
	if sunR == nil then
		sunR = 1.0
		sunG = 1.0
		sunB = 1.0
		return
	end

	BuildFastDustParticles()

	BuildFlameParticles()

	BuildDustParticles()

	BuildChunkParticles()

	gadgetHandler:RemoveCallIn("GameFrame")
	end
end
--------------------------------------------------------------------------------
--  END SYNCED
--------------------------------------------------------------------------------
end