--冥鲲之影灵衣
function c114514775.initial_effect(c)
	c:EnableReviveLimit()
	--cannot special summon
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_SINGLE)
	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
	e1:SetCode(EFFECT_SPSUMMON_CONDITION)
	e1:SetValue(aux.ritlimit)
	c:RegisterEffect(e1)
	--tohand
	local e2=Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(114514775,0))
	e2:SetCategory(CATEGORY_TOHAND)
	e2:SetType(EFFECT_TYPE_IGNITION)
	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
	e2:SetRange(LOCATION_HAND)
	e2:SetCountLimit(1,114514775)
	e2:SetCost(c114514775.thcost)
	e2:SetTarget(c114514775.thtg)
	e2:SetOperation(c114514775.thop)
	c:RegisterEffect(e2)
	--ritual summon
	local e3=Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(114514775,1))
	e3:SetCategory(CATEGORY_SPECIAL_SUMMON+CATEGORY_REMOVE)
	e3:SetType(EFFECT_TYPE_QUICK_O)
	e3:SetCode(EVENT_FREE_CHAIN)
	e3:SetRange(LOCATION_MZONE)
	e3:SetCountLimit(1,114514775*2)
	e3:SetTarget(c114514775.sstg)
	e3:SetOperation(c114514775.ssop)
	c:RegisterEffect(e3)
end
function c114514775.mat_filter(c)
	return not c:IsCode(114514775)
end
function c114514775.thcost(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return e:GetHandler():IsDiscardable() end
	Duel.SendtoGrave(e:GetHandler(),REASON_COST+REASON_DISCARD)
end
function c114514775.thfilter(c)
	return c:IsSetCard(0xb4) and (c:IsType(TYPE_SPELL) or c:IsType(TYPE_TRAP)) and c:IsAbleToHand() and c:IsFaceup()
end
function c114514775.thtg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chkc then return chkc:IsControler(tp) and chkc:IsLocation(LOCATION_REMOVED) and c36733451.filter(chkc) end
	if chk==0 then return Duel.IsExistingTarget(c114514775.thfilter,tp,LOCATION_REMOVED,0,1,nil) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_ATOHAND)
	local g=Duel.SelectTarget(tp,c114514775.thfilter,tp,LOCATION_REMOVED,0,1,1,nil)
	Duel.SetOperationInfo(0,CATEGORY_TOHAND,g,g:GetCount(),0,0)
end
function c114514775.thop(e,tp,eg,ep,ev,re,r,rp)
	local tc=Duel.GetFirstTarget()
	if tc and tc:IsRelateToEffect(e) then
		Duel.SendtoHand(tc,nil,REASON_EFFECT)
		Duel.ConfirmCards(1-tp,tc)
	end
end

function c114514775.sstg(e,tp,eg,ep,ev,re,r,rp,chk)
	local eg=Duel.GetMatchingGroup(c114514775.ssfilter,tp,0,LOCATION_EXTRA+LOCATION_GRAVE,nil)
	local tg=Duel.GetMatchingGroup(c114514775.ssfilter2,tp,LOCATION_HAND+LOCATION_GRAVE,0,nil)
	if chk==0 then return #eg > 0 and #tg > 0 end
	Duel.SetOperationInfo(0,CATEGORY_REMOVE,nil,1,1-tp,LOCATION_EXTRA+LOCATION_GRAVE)
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,LOCATION_HAND+LOCATION_GRAVE)
end

function c114514775.ssfilter(c)
	return c:IsType(TYPE_MONSTER) and c:IsAbleToRemove()
end

function c114514775.ssfilter2(c)
	return c:IsType(TYPE_MONSTER) and c:IsType(TYPE_RITUAL) and c:IsAttribute(ATTRIBUTE_WATER)
end

function c114514775.ssop(e,tp,eg,ep,ev,re,r,rp)
	local gg=Duel.GetFieldGroup(tp,0,LOCATION_EXTRA)
	local eg=Duel.GetMatchingGroup(c114514775.ssfilter,tp,0,LOCATION_GRAVE,nil)
	local g
	if #gg>0 or #eg>0 then
		local off=1
		local ops,opval={},{}
		if #gg>0 then
			ops[off]=aux.Stringid(114514775,3)
			opval[off]=0
			off=off+1
		end
		if #eg>0 then
			ops[off]=aux.Stringid(114514775,4)
			opval[off]=1
			off=off+1
		end
		local op=Duel.SelectOption(tp,table.unpack(ops))+1
		local sel=opval[op]
		if sel==0 then
			Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVE)
			Duel.ConfirmCards(tp,gg)
			g=Duel.SelectMatchingCard(tp,c114514775.ssfilter,tp,0,LOCATION_EXTRA,1,1,nil)
		else
			Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVE)
			g=Duel.SelectMatchingCard(tp,c114514775.ssfilter,tp,0,LOCATION_GRAVE,1,1,nil)
		end
	end
	if g:GetCount()==0 then return end
	Duel.HintSelection(g)
	Duel.Remove(g,POS_FACEUP,REASON_EFFECT)
	local tg=Duel.SelectMatchingCard(tp,c114514775.ssfilter2,tp,LOCATION_HAND+LOCATION_GRAVE,0,1,1,nil)
	local tc=tg:GetFirst()
	Duel.SpecialSummon(tc,SUMMON_TYPE_RITUAL,tp,tp,false,true,POS_FACEUP)
	tc:CompleteProcedure()
end