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

local PB = {ringParticles = {},glowParticles = {}, smokeParticles = {}, columnParticles = {}} --PB stands for "pre-built"
local sunR, sunG, sunB
if (gadgetHandler:IsSyncedCode()) then
--------------------------------------------------------------------------------
--  SYNCED
--------------------------------------------------------------------------------


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 BuildSmokeParticles()
	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 = 0.0
			color1G = 0.0
			color1B = 0.0
			color1A = 0.0

			color2R = 1.0
			color2G = (math.random(1,5) / 10)
			color2B = 0.0
			color2A = 0.3

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

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

			size = math_random(1000,6000)/1000 / distScale

			myType = math_random(1,4) / distScale

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

			velX = math_random(-1000,1000)/1000
			velY = math_random(-1000,1000)/1000
			velZ = math_random(-1000,1000)/1000
	
			sizeGrowth = math_random(50,300) / 1000 / distScale
			if sizeGrowth < 0 then sizeGrowth = sizeGrowth * -1 + 0.5 end

			myVelocity = math_random(1,7) -- VELOCITY MIN/MAX!
			startTime = 0 -- shrink enough to fit into color space
			finishTime = math_random(3000,7000) / 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.smokeParticles[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 BuildColumnParticles()
	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 = 0.0
			color1G = 0.0
			color1B = 0.0
			color1A = 0.0

			color2R = 1.0
			color2G = (math.random(1,5) / 10)
			color2B = 0.0
			color2A = 0.3

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

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

			size = math_random(1000,6000)/1000 / distScale

			myType = math_random(1,4) / distScale

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

			velX = math_random(-1000,1000)/1000
			velY = math_random(-1000,1000)/1000
			velZ = math_random(-1000,1000)/1000
	
			sizeGrowth = math_random(50,300) / 1000 / distScale
			if sizeGrowth < 0 then sizeGrowth = sizeGrowth * -1 + 0.5 end

			myVelocity = math_random(1,7) -- VELOCITY MIN/MAX!
			startTime = 0 -- shrink enough to fit into color space
			finishTime = math_random(3000,7000) / 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.columnParticles[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 BuildRingParticles()
	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.ringParticles[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 BuildGlowParticles()
	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.5

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

			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 = 48 / distScale
			sizeGrowth = math_random(25,50) / 1000 /distScale

			myType = 64 / distScale

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

			velX = math_random(-1000,1000) / 1000
			velY = math_random(-1000,1000) / 1000
			velZ = math_random(-1000,1000) / 1000
	
			myVelocity = math_random(4,10) -- VELOCITY MIN/MAX!
			startTime = 0 -- shrink enough to fit into color space
			finishTime = math_random(1500,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, 9000 == 90 degrees

			PB.glowParticles[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

function NukePOPS(unitID, ud, team, pieceID, popsType, popsPercent)
	local myPiece = Spring.GetUnitScriptPiece(unitID, pieceID)
	local pX, pY, pZ, vecX, vecY, vecZ = Spring.GetUnitPiecePosDir(unitID,myPiece)

	local math_abs = math.abs
	local math_random = math.random
	local distScale = 1000000
	local index

	--Spring.Echo(pX,pY,pZ)

	pX = pX / distScale
	if pX < 0 then pX = -pX + 0.5 end
	pY = pY / distScale
	if pY < 0 then pY = -pY + 0.5 end
	pZ = pZ / distScale
	if pZ < 0 then pZ = -pZ + 0.5 end


------------------------------------------GLOW
	if popsType == 1 then
		for i = 1,16 do
			index = math_random(1,10000)

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

	end

------------------------------------------EJECTA
	if popsType == 2 then
			for i = 1,200 do
				index = math_random(1,10000)

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

------------------------------------------RINGS
	if popsType == 3 then
		for i = 1,3 do
			index = math_random(1,10000)

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

------------------------------------------MUSHROOM
	if popsType == 4 then
		for i = 1,3 do
			index = math_random(1,10000)

			SendToUnsynced("pops_event", pX, pY, pZ,PB.smokeParticles[index].gravity,PB.smokeParticles[index].velX,PB.smokeParticles[index].velY,PB.smokeParticles[index].velZ,PB.smokeParticles[index].size,PB.smokeParticles[index].drag,PB.smokeParticles[index].sizeGrowth, PB.smokeParticles[index].myVelocity,PB.smokeParticles[index].startTime,PB.smokeParticles[index].finishTime,PB.smokeParticles[index].myRand,PB.smokeParticles[index].myType,PB.smokeParticles[index].myCone,PB.smokeParticles[index].color1R, PB.smokeParticles[index].color1G, PB.smokeParticles[index].color1B, PB.smokeParticles[index].color1A, PB.smokeParticles[index].color2R, PB.smokeParticles[index].color2G, PB.smokeParticles[index].color2B, PB.smokeParticles[index].color2A, PB.smokeParticles[index].color3R, PB.smokeParticles[index].color3G, PB.smokeParticles[index].color3B, PB.smokeParticles[index].color3A, PB.smokeParticles[index].color4R, PB.smokeParticles[index].color4G, PB.smokeParticles[index].color4B, PB.smokeParticles[index].color4A)
		end
	end
	return true
end
gadgetHandler:RegisterGlobal("NukePOPS", NukePOPS)

function DestroyEverything512(unitID, ud, team)
	local posX,posY,posZ = Spring.GetUnitPosition(unitID)
	local deathList = Spring.GetUnitsInSphere(posX,posY,posZ,512)
	for i,k in ipairs(deathList) do
		if k ~= unitID and UnitDefs[Spring.GetUnitDefID(k)].customParams.invincible ~= "yes" then
			Spring.DestroyUnit(k,false,false)
		end
	end
end
gadgetHandler:RegisterGlobal("DestroyEverything512", DestroyEverything512)


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

	BuildSmokeParticles()

	BuildColumnParticles()

	BuildRingParticles()

	BuildGlowParticles()
	end

	gadgetHandler:RemoveCallIn("GameFrame")
end

end --END OF SYNCED