function gadget:GetInfo()
	return {
		name = "P.O.P.S. Explosions",
		desc = "CEG Replacements",
		author = "Argh, thanks to Kloot, jK",
		date = "February 11, 2010",
		license = "GPL, v. 2 or later, 2010",
		layer = -16,--Layer assignment is critical!
		enabled = true,
	}
end

local PB = {flameParticles = {},fireParticles = {},sparkParticles = {}, dirtParticles = {}, plasmaParticles = {}} --PB stands for "pre-built"
local explosionTable = {}

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

function gadget:Explosion(weaponId, projX, projY, projZ, ownerID)
	local wd = WeaponDefs[weaponId]
	if wd.customParams.custom_explosion == "yes" then
		local distScale = 1000000
		local index
		local math_random = math.random
		local math_abs = math.abs
		local math_floor = math.floor

		local origX,origY,origZ = projX,projY,projZ

		projX = projX / distScale
		if projX < 0 then projX = -projX + 0.5 end
	
		projY = projY / distScale
		if projY < 0 then projY = -projY + 0.5 end

		projZ = projZ / distScale
		if projZ < 0 then projZ = -projZ + 0.5 end

		local events = tonumber(wd.customParams.explosion_events)
		local scale = tonumber(wd.customParams.explosion_scale)
		local type = tonumber(wd.customParams.explosion_type)
------------------------------------------------------------------------------------------------------------DEFAULT
		if type == 1 then

			for i = 1,events do
				index = math_random(1,10000)

				SendToUnsynced("pops_event",projX, projY, projZ,PB.flameParticles[index].gravity,PB.flameParticles[index].velX,PB.flameParticles[index].velY,PB.flameParticles[index].velZ,PB.flameParticles[index].size * scale,PB.flameParticles[index].drag, PB.flameParticles[index].sizeGrowth,PB.flameParticles[index].myVelocity * scale,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)

				SendToUnsynced("pops_event",projX, projY, projZ,PB.dirtParticles[index].gravity,PB.dirtParticles[index].velX,PB.dirtParticles[index].velY,PB.dirtParticles[index].velZ,PB.dirtParticles[index].size,PB.dirtParticles[index].drag, PB.dirtParticles[index].sizeGrowth,PB.dirtParticles[index].myVelocity * scale,PB.dirtParticles[index].startTime,PB.dirtParticles[index].finishTime,PB.dirtParticles[index].myRand,PB.dirtParticles[index].myType,PB.dirtParticles[index].myCone,PB.dirtParticles[index].color1R, PB.dirtParticles[index].color1G, PB.dirtParticles[index].color1B, PB.dirtParticles[index].color1A, PB.dirtParticles[index].color2R, PB.dirtParticles[index].color2G, PB.dirtParticles[index].color2B, PB.dirtParticles[index].color2A, PB.dirtParticles[index].color3R, PB.dirtParticles[index].color3G, PB.dirtParticles[index].color3B, PB.dirtParticles[index].color3A, PB.dirtParticles[index].color4R, PB.dirtParticles[index].color4G, PB.dirtParticles[index].color4B, PB.dirtParticles[index].color4A)

				SendToUnsynced("pops_event",projX, projY, projZ,PB.fireParticles[index].gravity,PB.fireParticles[index].velX,PB.fireParticles[index].velY,PB.fireParticles[index].velZ,PB.fireParticles[index].size * scale,PB.fireParticles[index].drag, PB.fireParticles[index].sizeGrowth,PB.fireParticles[index].myVelocity * scale,PB.fireParticles[index].startTime,PB.fireParticles[index].finishTime,PB.fireParticles[index].myRand,PB.fireParticles[index].myType,PB.fireParticles[index].myCone,PB.fireParticles[index].color1R, PB.fireParticles[index].color1G, PB.fireParticles[index].color1B, PB.fireParticles[index].color1A, PB.fireParticles[index].color2R, PB.fireParticles[index].color2G, PB.fireParticles[index].color2B, PB.fireParticles[index].color2A, PB.fireParticles[index].color3R, PB.fireParticles[index].color3G, PB.fireParticles[index].color3B, PB.fireParticles[index].color3A, PB.fireParticles[index].color4R, PB.fireParticles[index].color4G, PB.fireParticles[index].color4B, PB.fireParticles[index].color4A)
			end

			for i = 1,events*2 do
				index = math_random(1,10000)
				SendToUnsynced("pops_event",projX, projY, projZ,PB.sparkParticles[index].gravity,PB.sparkParticles[index].velX,PB.sparkParticles[index].velY,PB.sparkParticles[index].velZ,PB.sparkParticles[index].size,PB.sparkParticles[index].drag / scale, PB.sparkParticles[index].sizeGrowth,PB.sparkParticles[index].myVelocity * scale,PB.sparkParticles[index].startTime,PB.sparkParticles[index].finishTime,PB.sparkParticles[index].myRand,PB.sparkParticles[index].myType,PB.sparkParticles[index].myCone,PB.sparkParticles[index].color1R, PB.sparkParticles[index].color1G, PB.sparkParticles[index].color1B, PB.sparkParticles[index].color1A, PB.sparkParticles[index].color2R, PB.sparkParticles[index].color2G, PB.sparkParticles[index].color2B, PB.sparkParticles[index].color2A, PB.sparkParticles[index].color3R, PB.sparkParticles[index].color3G, PB.sparkParticles[index].color3B, PB.sparkParticles[index].color3A, PB.sparkParticles[index].color4R, PB.sparkParticles[index].color4G, PB.sparkParticles[index].color4B, PB.sparkParticles[index].color4A)
			end
		end
-----------------------------------------------------------------------------------------------------------END DEFAULT
-----------------------------------------------------------------------------------------------------------PLASMA
		if type == 2 then
			for i = 1,events do
				index = math_random(1,10000)

				SendToUnsynced("pops_event",projX, projY, projZ,PB.plasmaParticles[index].gravity,PB.plasmaParticles[index].velX,PB.plasmaParticles[index].velY,PB.plasmaParticles[index].velZ,PB.plasmaParticles[index].size * scale,PB.plasmaParticles[index].drag, PB.plasmaParticles[index].sizeGrowth,PB.plasmaParticles[index].myVelocity * scale,PB.plasmaParticles[index].startTime,PB.plasmaParticles[index].finishTime,PB.plasmaParticles[index].myRand,PB.plasmaParticles[index].myType,PB.plasmaParticles[index].myCone,PB.plasmaParticles[index].color1R, PB.plasmaParticles[index].color1G, PB.plasmaParticles[index].color1B, PB.plasmaParticles[index].color1A, PB.plasmaParticles[index].color2R, PB.plasmaParticles[index].color2G, PB.plasmaParticles[index].color2B, PB.plasmaParticles[index].color2A, PB.plasmaParticles[index].color3R, PB.plasmaParticles[index].color3G, PB.plasmaParticles[index].color3B, PB.plasmaParticles[index].color3A, PB.plasmaParticles[index].color4R, PB.plasmaParticles[index].color4G, PB.plasmaParticles[index].color4B, PB.plasmaParticles[index].color4A)

				SendToUnsynced("pops_event",projX, projY, projZ,PB.dirtParticles[index].gravity,PB.dirtParticles[index].velX,PB.dirtParticles[index].velY,PB.dirtParticles[index].velZ,PB.dirtParticles[index].size,PB.dirtParticles[index].drag, PB.dirtParticles[index].sizeGrowth,PB.dirtParticles[index].myVelocity * scale,PB.dirtParticles[index].startTime,PB.dirtParticles[index].finishTime,PB.dirtParticles[index].myRand,PB.dirtParticles[index].myType,PB.dirtParticles[index].myCone,PB.dirtParticles[index].color1R, PB.dirtParticles[index].color1G, PB.dirtParticles[index].color1B, PB.dirtParticles[index].color1A, PB.dirtParticles[index].color2R, PB.dirtParticles[index].color2G, PB.dirtParticles[index].color2B, PB.dirtParticles[index].color2A, PB.dirtParticles[index].color3R, PB.dirtParticles[index].color3G, PB.dirtParticles[index].color3B, PB.dirtParticles[index].color3A, PB.dirtParticles[index].color4R, PB.dirtParticles[index].color4G, PB.dirtParticles[index].color4B, PB.dirtParticles[index].color4A)
			end

			for i = 1,events*2 do
				index = math_random(1,10000)
				SendToUnsynced("pops_event",projX, projY, projZ,PB.sparkParticles[index].gravity,PB.sparkParticles[index].velX,PB.sparkParticles[index].velY,PB.sparkParticles[index].velZ,PB.sparkParticles[index].size,PB.sparkParticles[index].drag / scale, PB.sparkParticles[index].sizeGrowth,PB.sparkParticles[index].myVelocity * scale,PB.sparkParticles[index].startTime,PB.sparkParticles[index].finishTime,PB.sparkParticles[index].myRand,PB.sparkParticles[index].myType,PB.sparkParticles[index].myCone,PB.sparkParticles[index].color1R, PB.sparkParticles[index].color1G, PB.sparkParticles[index].color1B, PB.sparkParticles[index].color1A, PB.sparkParticles[index].color2R, PB.sparkParticles[index].color2G, PB.sparkParticles[index].color2B, PB.sparkParticles[index].color2A, PB.sparkParticles[index].color3R, PB.sparkParticles[index].color3G, PB.sparkParticles[index].color3B, PB.sparkParticles[index].color3A, PB.sparkParticles[index].color4R, PB.sparkParticles[index].color4G, PB.sparkParticles[index].color4B, PB.sparkParticles[index].color4A)
			end
		end
-----------------------------------------------------------------------------------------------------------END PLASMA
	return false
	end
	return false
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,10000 do
			color1R = 1.0
			color1G = 1.0
			color1B = 1.0
			color1A = 0.3

			color2R = 1.0
			color2G = 1.0
			color2B = 1.0
			color2A = 0.5

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

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

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

			myType = math_random(9,16) / distScale

			drag = 0.07 / distScale
			gravity = math_random(1,4) / distScale

			velX = 0
			velY = 1.0
			velZ = 0

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

			myVelocity = math_random(2500,5000) * 8 /1000 -- VELOCITY MIN/MAX!
			startTime = 0 -- shrink enough to fit into color space
			finishTime = math_random(300,750) / 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
	return
end

local function BuildFireParticles()
	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,10000 do

		local randChoice = math_random(1,2)

			color1R = 1.0
			color1G = 1.0
			color1B = 1.0
			color1A = 0.5

			color2R = 1.0
			color2G = 1.0
			color2B = 1.0
			color2A = 0.7

			local randColor = math_random(10,25)/100
			color3R = randColor * sunR
			color3G = randColor * sunG
			color3B = randColor * sunB
			color3A = 1.0

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

			size = math.random(350,700)/100 / distScale
			
			if randChoice == 1 then
				myType = 7 / distScale
			else
				myType = 25 / distScale
			end

			drag = 0.12 / distScale
			gravity = (math.random(1,5) / distScale) + 0.5

			velX = 0
			velY = 1.0
			velZ = 0

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

			myVelocity = math_random(2500,4500) * 8 /1000 -- VELOCITY MIN/MAX!
			startTime = 0 -- shrink enough to fit into color space
			finishTime = math_random(2000,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(-60000,60000)/1000 -- cone value, like CEG, 9000 == 90 degrees

			PB.fireParticles[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
	return
end

local function BuildSparkParticles()
	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,10000 do

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

			color2R = 1.0
			color2G = 1.0
			color2B = 0.1
			color2A = 1.0

			color3R = 1.0
			color3G = 0.3 
			color3B = 0.1
			color3A = 1.0

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

			drag = 0.01 / distScale
			gravity = math_random(30,60) / distScale 

			size = math_random(500,700)/1000 / distScale

			myType = 64 / distScale

			velX = 0
			velY = 1.0
			velZ = 0

			sizeGrowth = math_random(-5,-1) / 10000 / distScale
			if sizeGrowth < 0 then sizeGrowth = sizeGrowth * -1 + 0.5 end

			myVelocity = math_random(20000,25000) * 8 / 1000 -- VELOCITY MIN/MAX!
			startTime = 0 -- shrink enough to fit into color space
			finishTime = math_random(1000,3500) / 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, 90000 == 90 degrees

			PB.sparkParticles[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
	return
end

local function BuildDirtParticles()
	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,10000 do

			color1R = 1.0 * sunR
			color1G = 1.0 * sunG
			color1B = 1.0 * sunB
			color1A = 0.5

			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

			drag = 0.01 / distScale
			gravity = math_random(15,30) / distScale

			size = math_random(5000,10000)/1000 / distScale

			myType = math_random(5,6) / distScale

			velX = 0
			velY = 1
			velZ = 0

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

			myVelocity = math_random(4000,16000) * 8 / 1000 -- VELOCITY MIN/MAX!
			startTime = 0 -- shrink enough to fit into color space
			finishTime = math_random(1000,2000) / 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(-30000,30000)/1000 -- cone value, like CEG, 90000 == 90 degrees

			PB.dirtParticles[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
	return
end

local function BuildPlasmaParticles()
	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,10000 do

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

			color2R = 0.1
			color2G = 0.7
			color2B = 1.0
			color2A = 0.2

			color3R = 1.0
			color3G = 0.3
			color3B = 0.0
			color3A = 0.3

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

			drag = 0.05 / distScale
			gravity = math_random(1,2) / distScale

			size = math_random(5000,10000)/1000 / distScale

			myType = 17 / distScale

			velX = 0
			velY = 1
			velZ = 0

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

			myVelocity = math_random(4000,8000) * 8 / 1000 -- VELOCITY MIN/MAX!
			startTime = 0 -- shrink enough to fit into color space
			finishTime = math_random(300,500) / 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, 90000 == 90 degrees

			PB.plasmaParticles[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
	return
end

function gadget:Initialize()
	for weaponId, weaponDef in pairs (WeaponDefs) do
		if weaponDef.customParams.custom_explosion then
			--Spring.Echo(weaponDef.name) -- useful for debugging
			Script.SetWatchWeapon(weaponId, true)
		end
	end
end

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

	BuildFlameParticles()

	BuildSparkParticles()

	BuildDirtParticles()

	BuildFireParticles()

	BuildPlasmaParticles()

	gadgetHandler:RemoveCallIn("GameFrame")
	end

end
-------------------------------------------------------------------------------------------------END
end