--神影依·阿库帕拉
function c114514390.initial_effect(c)
	c:EnableReviveLimit()
	local e0=Effect.CreateEffect(c)
	e0:SetType(EFFECT_TYPE_SINGLE)
	e0:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
	e0:SetCode(EFFECT_FUSION_MATERIAL)
	e0:SetCondition(c114514390.FShaddollCondition())
	e0:SetOperation(c114514390.FShaddollOperation())
	c:RegisterEffect(e0)
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_SINGLE)
	e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE+EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
	e1:SetCode(EFFECT_SPSUMMON_CONDITION)
	e1:SetRange(LOCATION_EXTRA)
	e1:SetValue(c114514390.splimit)
	c:RegisterEffect(e1)
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_FIELD)
	e2:SetCode(EFFECT_CANNOT_BE_EFFECT_TARGET)
	e2:SetRange(LOCATION_MZONE)
	e2:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE+EFFECT_FLAG_SET_AVAILABLE)
	e2:SetTargetRange(LOCATION_ONFIELD,0)
	e2:SetTarget(c114514390.ctfilter)
	e2:SetValue(aux.tgoval)
	c:RegisterEffect(e2)
	local e3=Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(114514390,0))
	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e3:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_O)
	e3:SetCode(EVENT_TO_GRAVE)
	e3:SetProperty(EFFECT_FLAG_CARD_TARGET+EFFECT_FLAG_DELAY)
	e3:SetCountLimit(1,114514390)
	e3:SetTarget(c114514390.sptg)
	e3:SetOperation(c114514390.spop)
	c:RegisterEffect(e3)
end
function c114514390.FShaddollFilter(c,fc)
	return c:IsCanBeFusionMaterial(fc)
end
function c114514390.FShaddollExFilter(c,fc,fe)
	return c:IsFaceup() and not c:IsImmuneToEffect(fe) and c114514390.FShaddollFilter(c,fc)
end
function c114514390.FShaddollFilter1(c,g)
	return c:IsFusionSetCard(0x9d) and g:IsExists(c114514390.EXFShaddollFilter,1,nil)
end
function c114514390.EXFShaddollFilter(c)
	return bit.band(TYPE_RITUAL+TYPE_FUSION+TYPE_SYNCHRO+TYPE_XYZ+TYPE_LINK,c:GetFusionType())~=0
end
function c114514390.FShaddollSpFilter1(c,fc,tp,mg,exg,chkf)
	return mg:IsExists(c114514390.FShaddollSpFilter2,1,c,fc,tp,c,chkf)
		or (exg and exg:IsExists(c114514390.FShaddollSpFilter2,1,c,fc,tp,c,chkf))
end
function c114514390.FShaddollSpFilter2(c,fc,tp,mc,chkf)
	local sg=Group.FromCards(c,mc)
	if sg:IsExists(aux.TuneMagicianCheckX,1,nil,sg,EFFECT_TUNE_MAGICIAN_F) then return false end
	if not aux.MustMaterialCheck(sg,tp,EFFECT_MUST_BE_FMATERIAL) then return false end
	if aux.FCheckAdditional and not aux.FCheckAdditional(tp,sg,fc)
		or aux.FGoalCheckAdditional and not aux.FGoalCheckAdditional(tp,sg,fc) then return false end
	return ((c114514390.FShaddollFilter1(c,sg) and c114514390.EXFShaddollFilter(mc)) or (c114514390.FShaddollFilter1(mc,sg) and c114514390.EXFShaddollFilter(c))) and (chkf==PLAYER_NONE or Duel.GetLocationCountFromEx(tp,tp,sg,fc)>0)
end
function c114514390.FShaddollCondition()
	return  function(e,g,gc,chkf)
			if g==nil then return aux.MustMaterialCheck(nil,e:GetHandlerPlayer(),EFFECT_MUST_BE_FMATERIAL) end
			local c=e:GetHandler()
			local mg=g:Filter(c114514390.FShaddollFilter,nil,c)
			local tp=e:GetHandlerPlayer()
			local fc=Duel.GetFieldCard(tp,LOCATION_FZONE,0)
			local exg=nil
			if fc and fc:IsHasEffect(81788994) and fc:IsCanRemoveCounter(tp,0x16,3,REASON_EFFECT) then
				local fe=fc:IsHasEffect(81788994)
				exg=Duel.GetMatchingGroup(c114514390.FShaddollExFilter,tp,0,LOCATION_MZONE,mg,c,fe)
			end
			if gc then
				if not mg:IsContains(gc) then return false end
				return c114514390.FShaddollSpFilter1(gc,c,tp,mg,exg,chkf)
			end
			return mg:IsExists(c114514390.FShaddollSpFilter1,1,nil,c,tp,mg,exg,chkf)
		end
end
function c114514390.FShaddollOperation()
	return  function(e,tp,eg,ep,ev,re,r,rp,gc,chkf)
			local c=e:GetHandler()
			local mg=eg:Filter(c114514390.FShaddollFilter,nil,c)
			local fc=Duel.GetFieldCard(tp,LOCATION_FZONE,0)
			local exg=nil
			if fc and fc:IsHasEffect(81788994) and fc:IsCanRemoveCounter(tp,0x16,3,REASON_EFFECT) then
				local fe=fc:IsHasEffect(81788994)
				exg=Duel.GetMatchingGroup(c114514390.FShaddollExFilter,tp,0,LOCATION_MZONE,mg,c,fe)
			end
			local g=nil
			if gc then
				g=Group.FromCards(gc)
				mg:RemoveCard(gc)
			else
				Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_FMATERIAL)
				g=mg:FilterSelect(tp,c114514390.FShaddollSpFilter1,1,1,nil,c,tp,mg,exg,chkf)
				mg:Sub(g)
			end
			if exg and exg:IsExists(c114514390.FShaddollSpFilter2,1,nil,c,tp,g:GetFirst(),chkf)
				and (mg:GetCount()==0 or (exg:GetCount()>0 and Duel.SelectYesNo(tp,aux.Stringid(81788994,0)))) then
				fc:RemoveCounter(tp,0x16,3,REASON_EFFECT)
				Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_FMATERIAL)
				local sg=exg:FilterSelect(tp,c114514390.FShaddollSpFilter2,1,1,nil,c,tp,g:GetFirst(),chkf)
				g:Merge(sg)
			else
				Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_FMATERIAL)
				local sg=mg:FilterSelect(tp,c114514390.FShaddollSpFilter2,1,1,nil,c,tp,g:GetFirst(),chkf)
				g:Merge(sg)
			end
			Duel.SetFusionMaterial(g)
		end
end
function c114514390.splimit(e,se,sp,st)
	return bit.band(st,SUMMON_TYPE_FUSION)==SUMMON_TYPE_FUSION
end
function c114514390.ctfilter(e,c)
	return c:IsPosition(POS_FACEDOWN) or (c:IsPosition(POS_FACEUP) and c:IsSetCard(0x9d) and c:IsType(TYPE_MONSTER))
end
function c114514390.filter(c,e,tp)
	return c:IsSetCard(0x9d) and c:IsLevelAbove(5) and c:IsCanBeSpecialSummoned(e,0,tp,false,false)
end
function c114514390.sptg(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	if chkc then return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and c114514390.filter(chkc,e,tp) end
	if chk==0 then return Duel.GetLocationCount(tp,LOCATION_MZONE)>0
		and Duel.IsExistingTarget(c114514390.filter,tp,LOCATION_GRAVE,0,1,nil,e,tp) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
	local g=Duel.SelectTarget(tp,c114514390.filter,tp,LOCATION_GRAVE,0,1,1,nil,e,tp)
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,g,1,0,0)
end
function c114514390.spop(e,tp,eg,ep,ev,re,r,rp)
	local tc=Duel.GetFirstTarget()
	if tc:IsRelateToEffect(e) then
		Duel.SpecialSummon(tc,0,tp,tp,false,false,POS_FACEUP)
	end
end