--SP-奇迹同调融合
function c87593003.initial_effect(c)
	--spsummon
	local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetCondition(c87593003.condition)
	e1:SetTarget(c87593003.target)
	e1:SetOperation(c87593003.operation)
	c:RegisterEffect(e1)
end
function c87593003.condition(e,tp,eg,ep,ev,re,r,rp)
	return Duel.IsExistingMatchingCard(c87593003.ofilter,tp,LOCATION_ONFIELD,LOCATION_ONFIELD,1,nil,e,tp)
end
function c87593003.ofilter(c,e,tp)
	if c:GetControler()==tp then cou=0x871 else cou=0x872 end
	return c:IsCode(87593200) and c:GetCounter(cou)>=7
end
function c87593003.filter2(c,fc)
	local fd=c:GetCode()
	if not c:IsCanBeFusionMaterial() or not c:IsAbleToRemove() then return false end
	for i=1,fc.material_count do
		if fd==fc.material[i] then return true end
	end
	return false
end
function c87593003.filter1(c,e,tp)
	local ct=c.material_count
	return ct~=nil and Duel.IsExistingMatchingCard(c87593003.filter2,tp,LOCATION_MZONE+LOCATION_GRAVE,0,1,nil,c)
	 and c:IsCanBeSpecialSummoned(e,SUMMON_TYPE_FUSION,tp,false,false)
end
function c87593003.target(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsExistingMatchingCard(c87593003.filter1,tp,LOCATION_EXTRA,0,1,nil,e,tp) and
	Duel.IsExistingMatchingCard(c87593003.filter3,tp,LOCATION_MZONE+LOCATION_GRAVE,0,1,nil) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_CONFIRM)
	local g=Duel.SelectTarget(tp,c87593003.filter1,tp,LOCATION_EXTRA,0,1,1,nil,e,tp)
	Duel.ConfirmCards(1-tp,g)
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVE)
	local cg=Duel.SelectMatchingCard(tp,c87593003.filter2,tp,LOCATION_MZONE+LOCATION_GRAVE,0,1,1,nil,g:GetFirst())
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVE)
	local sg=Duel.SelectMatchingCard(tp,c87593003.filter3,tp,LOCATION_MZONE+LOCATION_GRAVE,0,1,1,nil)
	e:SetLabel(sg:GetFirst():GetLevel())
	cg:Merge(sg)
	Duel.SetOperationInfo(0,CATEGORY_REMOVE,cg,1,0,0)
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,g,1,tp,LOCATION_EXTRA)
end
function c87593003.filter3(c)
	return c:IsType(TYPE_SYNCHRO) and c:IsCanBeFusionMaterial() and c:IsAbleToRemove()
end
function c87593003.operation(e,tp,eg,ep,ev,re,r,rp)
	local ex,mg=Duel.GetOperationInfo(0,CATEGORY_REMOVE)
	local ex,g=Duel.GetOperationInfo(0,CATEGORY_SPECIAL_SUMMON)
	local tc=g:GetFirst()
	if tc and Duel.Remove(mg,POS_FACEUP,REASON_EFFECT+REASON_MATERIAL+REASON_FUSION) then
		tc:SetMaterial(mg)
		Duel.SpecialSummon(tc,SUMMON_TYPE_FUSION,tp,tp,false,false,POS_FACEUP)
	local e1=Effect.CreateEffect(e:GetHandler())
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetRange(LOCATION_MZONE)
	e1:SetTargetRange(0,LOCATION_MZONE)
	e1:SetTarget(c87593003.dis)
	e1:SetCode(EFFECT_DISABLE)
	e1:SetReset(RESET_EVENT+0x1fe0000)
	e1:SetLabel(e:GetLabel())
	tc:RegisterEffect(e1)
		tc:CompleteProcedure()
	end
end
function c87593003.dis(e,c)
	return c:GetLevel()<=e:GetLabel() or c:GetRank()<=e:GetLabel()
end