--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Riiak Shi Nal's Magic The Gathering Functions -------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

-- /////////////////////////////////////////////////////////////////////////////
-- Choosing Functions //////////////////////////////////////////////////////////
-- /////////////////////////////////////////////////////////////////////////////
function RSNChooseNCardsOfSubTypeFromHand( pPlayer_hand, pPlayer_choosing, pType, pTags )
	local count = #pTags
	Object():GetFilter():Clear()
	Object():GetFilter():SetPlayer( pPlayer_hand )
	Object():GetFilter():AddSubType( pType )
	Object():GetFilter():SetZone( ZONE_HAND )
	pPlayer_choosing:SetTargetCount( count )
	local index = 1
	while index < count + 1 do
		pPlayer_choosing:SetTargetPrompt( index - 1, pTags[index] )
		index = index + 1
	end
	pPlayer_choosing:ChooseTargets()
end

function RSNChooseNCreaturesOfSubType( pPlayer, pType, pTags )
	local count = #pTags
	Object():GetFilter():Clear()
	Object():GetFilter():SetPlayer( pPlayer )
	Object():GetFilter():SetZone( ZONE_IN_PLAY )
	Object():GetFilter():AddCardType( CARD_TYPE_CREATURE )
	Object():GetFilter():AddSubType( pType )
	Object():GetPlayer():SetTargetCount( count )
	local index = 1
	while index < count + 1 do
		Object():GetPlayer():SetTargetPrompt( index - 1, pTags[index] )
		index = index + 1
	end
	Object():GetPlayer():ChooseTargets()
end

function RSNChooseNPermanentCardsOfSubTypeFromHand( pPlayer_hand, pPlayer_choosing, pType, pTags )
	local count = #pTags
	Object():GetFilter():Clear()
	Object():GetFilter():SetPlayer( pPlayer_hand )
	Object():GetFilter():AddCardType( CARD_TYPE_ARTIFACT )
	Object():GetFilter():AddCardType( CARD_TYPE_CREATURE )
	Object():GetFilter():AddCardType( CARD_TYPE_ENCHANTMENT )
	Object():GetFilter():AddCardType( CARD_TYPE_LAND )
	Object():GetFilter():AddCardType( CARD_TYPE_PLANESWALKER )
	Object():GetFilter():AddSubType( pType )
	Object():GetFilter():SetZone( ZONE_HAND )
	pPlayer_choosing:SetTargetCount( count )
	local index = 1
	while index < count + 1 do
		pPlayer_choosing:SetTargetPrompt( index - 1, pTags[index] )
		index = index + 1
	end
	pPlayer_choosing:ChooseTargets()
end

function RSNChooseNUntappedCreaturesOfSubType( pPlayer, pType, pTags )
	local count = #pTags
	Object():GetFilter():Clear()
	Object():GetFilter():SetPlayer( pPlayer )
	Object():GetFilter():SetZone( ZONE_IN_PLAY )
	Object():GetFilter():AddCardType( CARD_TYPE_CREATURE )
	Object():GetFilter():AddSubType( pType )
	Object():GetFilter():AddExtra( FILTER_EXTRA_CREATURE_UNTAPPED )
	Object():GetPlayer():SetTargetCount( count )
	local index = 1
	while index < count + 1 do
		Object():GetPlayer():SetTargetPrompt( index - 1, pTags[index] )
		index = index + 1
	end
	Object():GetPlayer():ChooseTargets()
end

-- /////////////////////////////////////////////////////////////////////////////
-- Counting Functions //////////////////////////////////////////////////////////
-- /////////////////////////////////////////////////////////////////////////////
function RSNCountArtifactsInYourGraveyard()
	Object():GetFilter():Clear()
	Object():GetFilter():SetZone( ZONE_GRAVEYARD )
	Object():GetFilter():AddCardType( CARD_TYPE_ARTIFACT )
	Object():GetFilter():SetController( Object():GetController() )
	Object():GetFilter():NotTargetted()
	return (Object():GetFilter():Count())
end

function RSNCountOpponentLandsOfType( pType )
	Object():GetFilter():Clear()
	Object():GetFilter():SetZone( ZONE_IN_PLAY )
	Object():GetFilter():AddCardType( CARD_TYPE_LAND )
	Object():GetFilter():AddSubType( pType )
	Object():GetFilter():SetControllersTeam( Object():GetController():GetTeam() )
	Object():GetFilter():AddExtra( FILTER_EXTRA_FLIP_TEAM )
	Object():GetFilter():NotTargetted()
	return (Object():GetFilter():Count())
end

function RSNCountPlayerAttackingCreatures( pPlayer )
	Object():GetFilter():Clear()
	Object():GetFilter():AddCardType( CARD_TYPE_CREATURE )
	Object():GetFilter():SetController( pPlayer )
	Object():GetFilter():AddExtra( FILTER_EXTRA_CREATURE_ATTACKING )
	Object():GetFilter():NotTargetted()
	return (Object():GetFilter():Count())
end

function RSNCountPlayerCreaturesOfBothSubTypes( pPlayer, pType1, pType2 )
	Object():GetFilter():Clear()
	Object():GetFilter():SetZone( ZONE_IN_PLAY )
	Object():GetFilter():AddCardType( CARD_TYPE_CREATURE )
	Object():GetFilter():SetController( pPlayer )
	Object():GetFilter():AddSubType( pType1 )
	Object():GetFilter():AddSubType( pType2 )
	Object():GetFilter():NotTargetted()
	return (Object():GetFilter():Count())
end

function RSNCountPlayerCreaturesOfTwoSubTypes( pPlayer, pType1, pType2 )
	local nCountBoth = RSNCountPlayerCreaturesOfBothSubTypes( pPlayer, pType1, pType2 )
	local nCountType1 = CountPlayersCreaturesOfType( pPlayer, pType1 )
	local nCountType2 = CountPlayersCreaturesOfType( pPlayer, pType2 )
	return ((nCountType1 + nCountType2) - nCountBoth)
end

function RSNCountPlayerLandsOfType( pPlayer, pType )
	Object():GetFilter():Clear()
	Object():GetFilter():SetZone( ZONE_IN_PLAY )
	Object():GetFilter():AddCardType( CARD_TYPE_LAND )
	Object():GetFilter():AddSubType( pType )
	Object():GetFilter():SetController( pPlayer )
	Object():GetFilter():NotTargetted()
	return (Object():GetFilter():Count())
end

function RSNCountOtherPlayerPermanentsOfColour( pPlayer, pColour )
	Object():GetFilter():Clear()
	Object():GetFilter():SetZone( ZONE_IN_PLAY )
	Object():GetFilter():AddColour( pColour )
	Object():GetFilter():SetController( pPlayer )
	Object():GetFilter():SetCardInstance( Object() )
	Object():GetFilter():AddExtra( FILTER_EXTRA_FLIP_CARD_INSTANCE )
	Object():GetFilter():NotTargetted()
	return (Object():GetFilter():Count())
end

function RSNCountOtherPlayerTwoColoredPermanents( pPlayer, pColour1, pColour2 )
	local nColouredCount1 = RSNCountOtherPlayerPermanentsOfColour( pPlayer, pColour1 )
	local nColouredCount2 = RSNCountOtherPlayerPermanentsOfColour( pPlayer, pColour2 )
	Object():GetFilter():Clear()
	Object():GetFilter():SetZone( ZONE_IN_PLAY )
	Object():GetFilter():AddColour( pColour1 )
	Object():GetFilter():AddColour( pColour2 )
	Object():GetFilter():SetController( pPlayer )
	Object():GetFilter():SetCardInstance( Object() )
	Object():GetFilter():AddExtra( FILTER_EXTRA_FLIP_CARD_INSTANCE )
	Object():GetFilter():NotTargetted()
	local nColouredCountBoth = Object():GetFilter():Count()
	return ((nColouredCount1 + nColouredCount2) - nColouredCountBoth)
end

function RSNCountOtherPlayerMultiColoredPermanents( pPlayer )
	local nCount = RSNCountOtherPlayerTwoColoredPermanents( pPlayer, COLOUR_BLACK, COLOUR_BLUE )
	nCount = nCount + RSNCountOtherPlayerTwoColoredPermanents( pPlayer, COLOUR_BLACK, COLOUR_GREEN )
	nCount = nCount + RSNCountOtherPlayerTwoColoredPermanents( pPlayer, COLOUR_BLACK, COLOUR_RED )
	nCount = nCount + RSNCountOtherPlayerTwoColoredPermanents( pPlayer, COLOUR_BLACK, COLOUR_WHITE )
	nCount = nCount + RSNCountOtherPlayerTwoColoredPermanents( pPlayer, COLOUR_BLUE, COLOUR_GREEN )
	nCount = nCount + RSNCountOtherPlayerTwoColoredPermanents( pPlayer, COLOUR_BLUE, COLOUR_RED )
	nCount = nCount + RSNCountOtherPlayerTwoColoredPermanents( pPlayer, COLOUR_BLUE, COLOUR_WHITE )
	nCount = nCount + RSNCountOtherPlayerTwoColoredPermanents( pPlayer, COLOUR_GREEN, COLOUR_RED )
	nCount = nCount + RSNCountOtherPlayerTwoColoredPermanents( pPlayer, COLOUR_GREEN, COLOUR_WHITE )
	nCount = nCount + RSNCountOtherPlayerTwoColoredPermanents( pPlayer, COLOUR_RED, COLOUR_WHITE )
	return nCount
end

function RSNCountYourCreaturesOfTwoSubTypes( pType1, pType2 )
	return RSNCountPlayerCreaturesOfTwoSubTypes( Object():GetController(), pType1, pType2 )
end

function RSNCountYourTappedLands()
	Object():GetFilter():Clear()
	Object():GetFilter():SetZone( ZONE_IN_PLAY )
	Object():GetFilter():AddCardType( CARD_TYPE_LAND )
	Object():GetFilter():AddExtra( FILTER_EXTRA_CREATURE_TAPPED )
	Object():GetFilter():SetController( Object():GetController() )
	Object():GetFilter():NotTargetted()
	return (Object():GetFilter():Count())
end

function RSNCountYourOtherCreatures()
	Object():GetFilter():Clear()
	Object():GetFilter():SetZone( ZONE_IN_PLAY )
	Object():GetFilter():AddCardType( CARD_TYPE_CREATURE )
	Object():GetFilter():SetController( Object():GetController() )
	Object():GetFilter():SetCardInstance( Object() )
	Object():GetFilter():AddExtra( FILTER_EXTRA_FLIP_CARD_INSTANCE )
	Object():GetFilter():NotTargetted()
	return (Object():GetFilter():Count())
end

-- /////////////////////////////////////////////////////////////////////////////
-- Filtering Functions /////////////////////////////////////////////////////////
-- /////////////////////////////////////////////////////////////////////////////
function RSNCreaturesYouDontControl()
	return (CreaturesInPlay() and YouDontControl())
end

-- This is here so that if I start doing mana abilities I can change this test without affecting anything, but my cards.
function RSNLandFall()
	return  ((TriggerObject():GetCardType():Test( CARD_TYPE_LAND ) ~= 0) and (TriggerObject():GetPlayer() == Object():GetPlayer()))
end

function RSNYourGraveyard()
	return (InGraveyard() and OwnedByYou())
end

-- /////////////////////////////////////////////////////////////////////////////
-- Targeting Functions /////////////////////////////////////////////////////////
-- /////////////////////////////////////////////////////////////////////////////
function RSNTargetAllGoodF()
	Object():GetFilter():PlayerHint( Object():GetPlayer() )

	if (Object():GetFilter():CountStopAt( Object():GetPlayer():GetTargetCount() ) == Object():GetPlayer():GetTargetCount()) then
		return (TARGET_DETERMINATION_ALL)
	else
		return (TARGET_DETERMINATION_NONE)
	end
end

function RSNTargetArtifactCreatureInGraveyard( pPlayer )
	Object():GetFilter():Clear()
	Object():GetFilter():AddCardType( CARD_TYPE_CREATURE )
	Object():GetFilter():AddSecondCardType( CARD_TYPE_ARTIFACT )
	Object():GetFilter():SetZone( ZONE_GRAVEYARD )
	Object():GetFilter():SetPlayer( pPlayer )
	return TargetGoodF()
end

function RSNTargetArtifactCreatureInYourGraveyard()
	return RSNTargetArtifactCreatureInGraveyard( Object():GetPlayer() )
end

-- /////////////////////////////////////////////////////////////////////////////
-- Trigger Functions ///////////////////////////////////////////////////////////
-- /////////////////////////////////////////////////////////////////////////////
function RSNDuringOpponentsTurn()
	if (MTG():GetNumberOfPlayers() > 2) then
		if (MTG():GetNumberOfPlayers() > 3) then
			return (((Object():GetPlayer():GetTeam() ~= MTG():GetNthPlayer( 0 ):GetTeam()) and (MTG():GetNthPlayer( 0 ):MyTurn() ~= 0))
				or ((Object():GetPlayer():GetTeam() ~= MTG():GetNthPlayer( 1 ):GetTeam()) and (MTG():GetNthPlayer( 1 ):MyTurn() ~= 0))
				or ((Object():GetPlayer():GetTeam() ~= MTG():GetNthPlayer( 2 ):GetTeam()) and (MTG():GetNthPlayer( 2 ):MyTurn() ~= 0))
				or ((Object():GetPlayer():GetTeam() ~= MTG():GetNthPlayer( 3 ):GetTeam()) and (MTG():GetNthPlayer( 3 ):MyTurn() ~= 0)))
		else
			return (((Object():GetPlayer():GetTeam() ~= MTG():GetNthPlayer( 0 ):GetTeam()) and (MTG():GetNthPlayer( 0 ):MyTurn() ~= 0))
				or ((Object():GetPlayer():GetTeam() ~= MTG():GetNthPlayer( 1 ):GetTeam()) and (MTG():GetNthPlayer( 1 ):MyTurn() ~= 0))
				or ((Object():GetPlayer():GetTeam() ~= MTG():GetNthPlayer( 2 ):GetTeam()) and (MTG():GetNthPlayer( 2 ):MyTurn() ~= 0)))
		end
	else
		return (((Object():GetPlayer():GetTeam() ~= MTG():GetNthPlayer( 0 ):GetTeam()) and (MTG():GetNthPlayer( 0 ):MyTurn() ~= 0))
			or ((Object():GetPlayer():GetTeam() ~= MTG():GetNthPlayer( 1 ):GetTeam()) and (MTG():GetNthPlayer( 1 ):MyTurn() ~= 0)))
	end
end