--万古的仪式
local s,id,o=GetID()
function s.initial_effect(c)
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetOperation(s.op)
	c:RegisterEffect(e1)
end

function s.op(e,tp,eg,ep,ev,re,r,rp)
	local ct=Duel.GetFieldGroupCount(tp,LOCATION_DECK,0)
	while ct>0 do
		local g=Duel.GetDecktopGroup(tp,ct)
		local count=math.min(ct,15)
		local dg=g:Select(tp,1,count,nil)
		local c=dg:GetFirst()
		while c do
			Duel.MoveSequence(c,SEQ_DECKTOP)
			c=dg:GetNext()
		end
		Duel.SortDecktop(tp,tp,#dg)
		count=#dg
		while count>0 do
			g=Duel.GetDecktopGroup(tp,1)
			Duel.MoveSequence(g:GetFirst(),SEQ_DECKBOTTOM)
			count=count-1
		end
		ct=ct-#dg
	end
	
	local e1=Effect.CreateEffect(e:GetHandler())
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetCode(EFFECT_CHANGE_DAMAGE)
	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e1:SetTargetRange(1,0)
	e1:SetValue(s.val)
	e1:SetReset(RESET_PHASE+PHASE_END) 
	Duel.RegisterEffect(e1,1-tp)
	local e2=Effect.CreateEffect(e:GetHandler())
	e2:SetType(EFFECT_TYPE_FIELD)
	e2:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
	e2:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
	e2:SetTargetRange(LOCATION_MZONE,0)
	e2:SetReset(RESET_PHASE+PHASE_END)
	e2:SetValue(1)
	Duel.RegisterEffect(e2,1-tp)
	
	-- Validate deck order
	--ct=Duel.GetFieldGroupCount(tp,LOCATION_DECK,0)
	--s.drawall(tp,ct)
end

function s.val(e,re,val,r,rp,rc)
	if e:GetHandlerPlayer()==1-rp then
		return 0
	else return val end
end

function s.drawall(tp,n)
	while n>0 do
		Duel.Draw(tp,1,REASON_EFFECT)
		Duel.SelectYesNo(tp,aux.Stringid(id,0))
		n=n-1
	end
end