freeslot("MT_SMSTARCOIN", "sfx_scsfxz")
--Far reaching sound.
sfxinfo[sfx_scsfxz] = {flags = SF_X8AWAYSOUND|SF_X2AWAYSOUND|SF_X4AWAYSOUND,}

--Assign numbers to each emblem on load.
addHook("MapLoad", function()
	for mt in mapthings.iterate do
		if mt.type == mobjinfo[MT_EMBLEM].doomednum
			local ghs = P_SpawnMobj(mt.x*FRACUNIT, mt.y*FRACUNIT, 0, MT_SMSTARCOIN)
			if mt.angle != nil
				ghs.smstarcoin = mt.angle+1 
			else
				ghs.smstarcoin = 10
			end
			ghs.smstarcoinztime = 2
			ghs.smstarcoinz = mt.z
			ghs.scale = 110000
			ghs.smstarcoinangle = 0
		end
	end
end)

--The emblem itself
addHook("MobjThinker", function(s)
	if s.valid and s.smstarcoin and (s.smstarcoin != nil)
	
		if s.smstarcoinztime
		s.smstarcoinztime = $-1
		--After 2 tics, move yourself up to your correct Z location and become collectible.
			if not s.smstarcoinztime
				if s.smstarcoinz != nil
				s.flags = MF_NOGRAVITY|MF_NOCLIPHEIGHT|MF_SPECIAL
				s.z = s.floorz+(s.smstarcoinz*FRACUNIT)
				s.spriteyoffset = 19*FRACUNIT
				s.smstarcoinz = nil
				end
			end
		end
		--speen
		if not s.health
			if not s.fuse
				s.fuse = 45
			elseif s.fuse < 36
				s.frame = A|FF_PAPERSPRITE|TR_TRANS90-((s.fuse*FRACUNIT)/4)
			end
			s.smstarcoinangle = $+ANGLE_45
			s.angle = s.smstarcoinangle
		else
			s.smstarcoinangle = $+ANG10
			s.angle = s.smstarcoinangle
		end	
	end
end, MT_SMSTARCOIN)


--Hide yourself, they can't know we're secretly still here.
addHook("MobjThinker", function(s)
	if s.valid
	s.flags = $|MF2_DONTDRAW
	end
end, MT_EMBLEM)


--Broadcast serverwide message. You got one, you absolute chad!

addHook("TouchSpecial", function(emblem, s)
	if emblem and emblem.valid and emblem.health
	and s and s.valid and s.player
		emblem.health = 0
		emblem.state = S_SMSTARCOINGET
		emblem.fuse = 45
		emblem.destscale = $*99
		S_StartSound(s, sfx_scsfxz)
		P_AddPlayerScore(s.player, 5000)
			if emblem.smstarcoin
			print("\x82"+s.player.name+" has collected Emblem\x87 "+emblem.smstarcoin+"\x82"+"!")
			end
		return true
	end
end, MT_SMSTARCOIN)
