--幻光阵 祭坛
local s,id,o=GetID()
function s.initial_effect(c)
	--activate
	local e0=Effect.CreateEffect(c)
	e0:SetType(EFFECT_TYPE_ACTIVATE)
	e0:SetCode(EVENT_FREE_CHAIN)
	c:RegisterEffect(e0)
	--link summon
	local e1=Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(id,0))
	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e1:SetType(EFFECT_TYPE_QUICK_O)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetRange(LOCATION_SZONE)
	e1:SetHintTiming(0,TIMING_END_PHASE)
	e1:SetCountLimit(2,id)
	e1:SetTarget(s.linktg)
	e1:SetOperation(s.linkop)
	c:RegisterEffect(e1)
	--move
	local e2=Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id,1))
	e2:SetType(EFFECT_TYPE_QUICK_O)
	e2:SetCode(EVENT_FREE_CHAIN)
	e2:SetRange(LOCATION_SZONE)
	e2:SetHintTiming(0,TIMING_END_PHASE)
	e2:SetCountLimit(2,id+1)
	e2:SetTarget(s.movetg)
	e2:SetOperation(s.moveop)
	c:RegisterEffect(e2)
	--special
	local e3=Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(id,3))
	e3:SetCategory(CATEGORY_SPECIAL_SUMMON+CATEGORY_TOGRAVE)
	e3:SetType(EFFECT_TYPE_QUICK_O)
	e3:SetCode(EVENT_FREE_CHAIN)
	e3:SetRange(LOCATION_SZONE)
	e3:SetCost(s.spcost)
	e3:SetTarget(s.sptg)
	e3:SetOperation(s.spop)
	c:RegisterEffect(e3)
end
--连接召唤
function s.lfilter(c)
	return c:IsLinkSummonable(nil) and c:IsSetCard(0x3ca5)
end
function s.linktg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsExistingMatchingCard(s.lfilter,tp,LOCATION_EXTRA,0,1,nil) end
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,0,0)
end
function s.linkop(e,tp,eg,ep,ev,re,r,rp)
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
	local g=Duel.SelectMatchingCard(tp,s.lfilter,tp,LOCATION_EXTRA,0,1,1,nil)
	local tc=g:GetFirst()
	if tc then
		Duel.LinkSummon(tp,tc,nil)
	end
end
--位置移动
function s.seqfilter(c)
	local tp=c:GetControler()
	if not c:IsSetCard(0x3ca5) or not c:IsFaceup() then return false end
	return Duel.GetLocationCount(tp,LOCATION_MZONE,PLAYER_NONE,0)>0
end
function s.movetg(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	if chkc then return chkc:IsLocation(LOCATION_MZONE) end
	if chk==0 then return Duel.IsExistingTarget(s.seqfilter,tp,LOCATION_MZONE,0,1,nil) end
	Duel.Hint(HINT_SELECTMSG,tp,aux.Stringid(id,2))
	local g=Duel.SelectTarget(tp,s.seqfilter,tp,LOCATION_MZONE,0,1,1,nil)
end
function s.moveop(e,tp,eg,ep,ev,re,r,rp)
	local tc=Duel.GetFirstTarget()
	if tc:IsRelateToEffect(e) and Duel.GetLocationCount(tp,LOCATION_MZONE,PLAYER_NONE,0)>0 then
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOZONE)
        local fd=Duel.SelectDisableField(tp,1,LOCATION_MZONE,0,0)
        local nseq=math.log(fd,2)
        Duel.MoveSequence(tc,nseq)
    end
end
--虹光神的手续
function s.spcost(e,tp,eg,ep,ev,re,r,rp,chk)
	e:SetLabel(100)
	if chk==0 then return true end
end
function s.lkfilter(c)
	return c:IsSetCard(0xca5) and c:IsAbleToGraveAsCost()
end
function s.lkfilter2(c)
	return c:IsLocation(0x04) and c:IsType(TYPE_LINK)
end
function s.lkfilter3(c)
	return c:IsSetCard(0xca5) and c:IsAbleToGraveAsCost()
end
function s.spfilterg(g,tp)
	return g:Filter(s.lkfilter2,nil):GetClassCount(Card.GetCode)>=6 and Duel.GetMZoneCount(tp,g)>0
end
function s.spfilter(c,e,tp)
	return c:IsCode(116307199) and c:IsCanBeSpecialSummoned(e,0,tp,true,false)
end
function s.sptg(e,tp,eg,ep,ev,re,r,rp,chk)
	local c=e:GetHandler()
	local g=Duel.GetMatchingGroup(s.lkfilter,tp,0x0c,0,c)
	local tg=Duel.GetFieldGroup(tp,0,0x0c)
	if chk==0 then
		if e:GetLabel()~=100 then return false end
			e:SetLabel(0)
		return c:IsAbleToGraveAsCost() and g:CheckSubGroup(s.spfilterg,9,9,tp)
		and Duel.IsExistingMatchingCard(s.spfilter,tp,0x03,0,1,nil,e,tp) and #tg>=0
	end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOGRAVE)
	g=g:SelectSubGroup(tp,s.spfilterg,false,9,9,tp)
	g:AddCard(c)
	Duel.SendtoGrave(g,REASON_COST)
	Duel.SetChainLimit(s.chainlm)
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,LOCATION_DECK+LOCATION_HAND)
	Duel.SetOperationInfo(0,CATEGORY_TOGRAVE,tg,#tg,nil,nil)
end
function s.chainlm(e,ep,tp)
	return tp==ep
end
function s.spop(e,tp,eg,ep,ev,re,r,rp)
	if Duel.GetLocationCount(tp,LOCATION_MZONE)<=0 then return end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
	local g=Duel.SelectMatchingCard(tp,s.spfilter,tp,LOCATION_DECK+LOCATION_HAND,0,1,1,nil,e,tp)
	local tg=Duel.GetMatchingGroup(Card.IsAbleToGrave,tp,0,LOCATION_ONFIELD,nil)
	if g:GetCount()>0 then
		Duel.SpecialSummon(g,0,tp,tp,true,false,POS_FACEUP)
		local tc=g:GetFirst()
		tc:CompleteProcedure()
		if #tg>0 then
		Duel.SendtoGrave(tg,REASON_EFFECT)
		end
	end
end