// Tint our skins!
// (inspired from Sharen's Details + [a sm64ex-coop mod])

// <[ Arguments ]>
// * player_t - Player to tint
// * color - Skincolor used by the tint
// * colorized - Do we colorize it?
// * frameflags - Frame flags (ej. FF_FULLBRIGHT)
// * time - Tint duration (tics)
// * fuse - Vanish?
// * customspr - The custom sprite to set - If omitted, uses player's sprite
// * customspr2 - The custom sprite2 to set - If omitted, uses player's sprite2
// * customframe - The custom frame to set - If omitted, uses player's frame
local D94XKZ_palleteTint = function(player_t, color, colorized, frameflags, time, fuse, customspr, customspr2, customframe)
	local d94xkztintghost = P_SpawnGhostMobj(player_t.mo)
	d94xkztintghost.d94xkztintremovable = removable
	d94xkztintghost.tics = time
	if fuse
		d94xkztintghost.fuse = time
	else
		d94xkztintghost.fuse = time+10
	end
	if customspr
		d94xkztintghost.sprite = customspr
	end
	if customspr2
		d94xkztintghost.sprite2 = customspr2
	end
	if customframe
		d94xkztintghost.frame = customframe
	end
	if frameflags
		d94xkztintghost.frame = $|frameflags
	end
	d94xkztintghost.color = color
	d94xkztintghost.colorized = colorized
	d94xkztintghost.d94xkztintflags = frameflags
	d94xkztintghost.dispoffset = 1
	player_t.d94xkztintghost = d94xkztintghost
	P_TeleportMove(d94xkztintghost, player_t.mo.x+player_t.mo.momx, player_t.mo.y+player_t.mo.momy, player_t.mo.z+player_t.mo.momz)
end

addHook("PlayerThink", function(player)
	if player.mo and player.mo.valid
		if player.d94xkztintghost
			if player.d94xkztintghost.valid
				P_TeleportMove(player.d94xkztintghost, player.mo.x+player.mo.momx, player.mo.y+player.mo.momy, player.mo.z+player.mo.momz)
			else
				player.d94xkztintghost = nil
			end
		end
		if player.mo.skin == "dark"
		or player.mo.skin == "xkz"
			if player.mo.eflags & MFE_UNDERWATER
				D94XKZ_palleteTint(player, SKINCOLOR_OCEAN, true, FF_TRANS70, 1)
			end
		end
	end
end)

addHook("MobjDamage", function(mo, inflictor, source, dmg, dmgt)
	if mo and mo.valid
		local player = mo.player
		if mo.skin == "dark"
		or mo.skin == "xkz"
			if dmgt == DMG_FIRE
			or dmgt == DMG_NUKE
				D94XKZ_palleteTint(player, SKINCOLOR_CARBON, true, nil, 70, true, SPR_PLAY, SPR2_PAIN, A)
			elseif dmgt == DMG_ELECTRIC
				D94XKZ_palleteTint(player, SKINCOLOR_SUPERSILVER4, true, FF_FULLBRIGHT, 10, true, SPR_PLAY, SPR2_PAIN, A)
			else
				D94XKZ_palleteTint(player, SKINCOLOR_PEPPER, true, nil, 10, true, SPR_PLAY, SPR2_PAIN, A)
			end
		end
	end
end)