--影灵衣的伏魔镜
function c114514774.initial_effect(c)
	--Activate
	local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetTarget(c114514774.rstarget)
	e1:SetOperation(c114514774.rsop)
	c:RegisterEffect(e1)
	--search
	local e2=Effect.CreateEffect(c)
	e2:SetCategory(CATEGORY_SEARCH+CATEGORY_TOHAND)
	e2:SetType(EFFECT_TYPE_IGNITION)
	e2:SetRange(LOCATION_GRAVE)
	e2:SetCondition(c114514774.thcon)
	e2:SetCost(c114514774.thcost)
	e2:SetTarget(c114514774.thtg)
	e2:SetOperation(c114514774.thop)
	c:RegisterEffect(e2)
end

function c114514774.rstarget(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then
		local mg1=Duel.GetRitualMaterial(tp)
		local mg2=Duel.GetMatchingGroup(c114514774.exfilter,tp,0,LOCATION_MZONE,nil,e)
		local mg3=Duel.GetMatchingGroup(c114514774.filter,tp,LOCATION_HAND,0,nil)
		return Duel.IsExistingMatchingCard(aux.RitualUltimateFilter,tp,LOCATION_HAND,0,1,nil,c114514774.filter,e,tp,mg1,nil,Card.GetLevel,"Equal") or (#mg2>0 and #mg3>0)
	end
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,LOCATION_HAND)
end

function c114514774.rsop(e,tp,eg,ep,ev,re,r,rp)
	local b=Duel.GetMatchingGroup(c114514774.filter,tp,LOCATION_HAND,0,nil)
	if b==0 then return end
	local mg1=Duel.GetMatchingGroup(c114514774.exfilter,tp,0,LOCATION_MZONE,nil,e)
	local b1=#mg1 > 0	
	local mg=Duel.GetRitualMaterial(tp)
	local mg2=Duel.GetMatchingGroup(aux.RitualUltimateFilter,tp,LOCATION_HAND,0,nil,c114514774.filter,e,tp,mg,nil,Card.GetLevel,"Equal")
	local b2=#mg2 > 0
	if b1 and (not b2 or Duel.SelectYesNo(tp,aux.Stringid(114514774,0))) then
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
		local tg=Duel.SelectMatchingCard(tp,c114514774.rsfilter,tp,LOCATION_HAND,0,1,1,nil)	
		local tc=tg:GetFirst()
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_RELEASE)
		local mat=Duel.SelectMatchingCard(tp,c114514774.exfilter,tp,0,LOCATION_MZONE,1,1,nil)	
		
		Duel.ReleaseRitualMaterial(mat)
		Duel.SpecialSummon(tc,SUMMON_TYPE_RITUAL,tp,tp,false,true,POS_FACEUP)	
		tc:CompleteProcedure()
	else
		-- normal ritual
		local op=aux.RitualUltimateOperation(c114514774.filter,Card.GetLevel,"Equal",LOCATION_HAND)
		op(e,tp,eg,ep,ev,re,r,rp)
	end
end

function c114514774.exfilter(c)
	return c:IsSummonLocation(LOCATION_EXTRA)
end
function c114514774.filter(c)
	return c:IsSetCard(0xb4) and c:IsType(TYPE_RITUAL) and c:IsType(TYPE_MONSTER)
end
function c114514774.rsfilter(c)
	return c114514774.filter(c) and bit.band(c:GetType(),0x81)==0x81
end
function c114514774.thcon(e,tp,eg,ep,ev,re,r,rp)
	return Duel.GetFieldGroupCount(tp,LOCATION_MZONE,0)==0
end
function c114514774.cfilter(c)
	return c:IsSetCard(0xb4) and c:IsType(TYPE_MONSTER) and c:IsAbleToRemoveAsCost()
end
function c114514774.thcost(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return e:GetHandler():IsAbleToRemoveAsCost()
		and Duel.IsExistingMatchingCard(c114514774.cfilter,tp,LOCATION_GRAVE,0,1,nil) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVE)
	local g=Duel.SelectMatchingCard(tp,c114514774.cfilter,tp,LOCATION_GRAVE,0,1,1,nil)
	g:AddCard(e:GetHandler())
	Duel.Remove(g,POS_FACEUP,REASON_COST)
end
function c114514774.thfilter(c)
	return c:IsSetCard(0xb4) and c:IsType(TYPE_SPELL) and c:IsAbleToHand()
end
function c114514774.thtg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsExistingMatchingCard(c114514774.thfilter,tp,LOCATION_DECK,0,1,nil) end
	Duel.SetOperationInfo(0,CATEGORY_TOHAND,nil,1,tp,LOCATION_DECK)
end
function c114514774.thop(e,tp,eg,ep,ev,re,r,rp)
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_ATOHAND)
	local g=Duel.SelectMatchingCard(tp,c114514774.thfilter,tp,LOCATION_DECK,0,1,1,nil)
	if g:GetCount()>0 then
		Duel.SendtoHand(g,nil,REASON_EFFECT)
		Duel.ConfirmCards(1-tp,g)
	end
end