--モディファイ・ディープブルー
--Modify Deep Blue
local s,id=GetID()
function s.initial_effect(c)
	--Banish 1 Xyz Monster and Xyz Summon using Materials from the Deck
	local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetLabel(0)
	e1:SetCost(s.cost)
	e1:SetTarget(s.target)
	e1:SetOperation(s.activate)
	c:RegisterEffect(e1)
end
function s.filter(c,rk)
	return c:IsLevel(rk)
end
function s.xyzfilter(c,mg)
	return c:IsXyzSummonable(nil,mg)
end
function s.cfilter(c,e,tp)
	local rk=c:GetRank()
	local mg=Duel.GetMatchingGroup(s.filter,tp,LOCATION_DECK,0,nil,rk)
	return c:IsFaceup() and c:IsType(TYPE_XYZ) and Duel.GetMZoneCount(tp,c,tp)>0 and c:IsAbleToRemoveAsCost()
		and #mg>1 and Duel.IsExistingMatchingCard(s.xyzfilter,tp,LOCATION_EXTRA,0,1,nil,mg)
end
function s.cost(e,tp,eg,ep,ev,re,r,rp,chk)
	e:SetLabel(1)
	return true
end
function s.target(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then
		if e:GetLabel()~=1 then return false end
		e:SetLabel(0)
		return Duel.IsExistingMatchingCard(s.cfilter,tp,LOCATION_MZONE,0,1,nil,e,tp)
	end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVE)
	local rg=Duel.SelectMatchingCard(tp,s.cfilter,tp,LOCATION_MZONE,0,1,1,nil,e,tp)
	Duel.Remove(rg,POS_FACEUP,REASON_COST)
	Duel.SetTargetParam(rg:GetFirst():GetRank())
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,LOCATION_EXTRA)
end
function s.activate(e,tp,eg,ep,ev,re,r,rp)
	if Duel.GetLocationCount(tp,LOCATION_MZONE)<=0 then return end
	local rk=Duel.GetChainInfo(0,CHAININFO_TARGET_PARAM)
	local mg=Duel.GetMatchingGroup(s.filter,tp,LOCATION_DECK,0,nil,rk)
	local xyzg=Duel.GetMatchingGroup(s.xyzfilter,tp,LOCATION_EXTRA,0,nil,mg)
	if #xyzg>0 then
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
		local xyz=xyzg:Select(tp,1,1,nil):GetFirst()
		Duel.XyzSummon(tp,xyz,nil,mg)
	end
end