--[决斗编年史]抽卡的创造者
function c66666004.initial_effect(c)
	if c66666004.reg then return end
	c66666004.reg = true
	c66666004.active = {[0]=false,[1]=false}
	local e1=Effect.GlobalEffect()
	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e1:SetCode(EVENT_ADJUST)
	e1:SetOperation(c66666004.init)
	Duel.RegisterEffect(e1,0)
	local e2=Effect.GlobalEffect()
	e2:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_F)
	e2:SetCode(EVENT_PREDRAW)
	e2:SetCondition(c66666004.cfcon)
	e2:SetOperation(c66666004.cfop)
	Duel.RegisterEffect(e2,0)
end
c66666004.tag = true
function c66666004.init(e)
	for tp = 0,1 do
		local g = Duel.GetMatchingGroup(Card.IsCode,tp,LOCATION_HAND,0,nil,66666004)
		local i = g:GetCount()
		if i > 0 then
			c66666004.active[tp] = true
			c66666004.active[tp+10] = true
			Duel.Exile(g,REASON_RULE)
			local top = Duel.GetDecktopGroup(tp,i)
			top:ForEach(c66666004.exile)
			Duel.Exile(top,REASON_RULE)
		end
		local g2 = Duel.GetMatchingGroup(Card.IsCode,tp,LOCATION_DECK,0,nil,66666004)
		if g2:GetCount() > 0 then
			c66666004.active[tp] = true
			c66666004.active[tp+10] = true
			Duel.Exile(g2,REASON_RULE)
		end
	end
	e:Reset()
end

function c66666004.exile(c)
	local tp = c:GetOwner()
	Duel.SendtoHand(Duel.CreateToken(tp,c:GetOriginalCode()),tp,REASON_RULE)
end
function c66666004.cfcon(e,tp,eg,ep,ev,re,r,rp)
	tp = Duel.GetTurnPlayer()
	return c66666004.active[tp]
		and Duel.GetFieldGroupCount(tp,LOCATION_DECK,0)>0
end
c66666004.announcefilter = {TYPE_FUSION+TYPE_SYNCHRO+TYPE_XYZ+TYPE_LINK,OPCODE_ISTYPE,OPCODE_NOT}
function c66666004.filter(c,code)
	return c:GetOriginalCode()==code and c:GetOwner() == c:GetControler()
end
function c66666004.cfop(e,tp,eg,ep,ev,re,r,rp)
	tp = Duel.GetTurnPlayer()
	c66666004.active[tp+10] = not c66666004.active[tp+10]
	if c66666004.tag and c66666004.active[tp+10] then return end
	local top = Duel.GetDecktopGroup(tp,1):GetFirst()
	if not top or top:IsFaceup() then return end
	Duel.Hint(HINT_MESSAGE,tp,aux.Stringid(66666004,1))
	Duel.Hint(HINT_MESSAGE,1-tp,aux.Stringid(66666004,1))
	Duel.Hint(HINT_SELECTMSG,tp,aux.Stringid(66666004,0))
	local code = 66666000
	while code ~= 66666000 do
		code = Duel.AnnounceCardFilter(tp,table.unpack(c66666004.announcefilter))
	end
	Duel.Exile(top,REASON_RULE)
	local newc = Duel.CreateToken(tp,code)
	Duel.SendtoDeck(newc,nil,0,REASON_RULE)
	if Duel.GetMatchingGroupCount(c66666004.filter,tp,
		LOCATION_DECK+LOCATION_ONFIELD+LOCATION_EXTRA+LOCATION_GRAVE+LOCATION_HAND+LOCATION_OVERLAY+LOCATION_REMOVED
		,0,nil,code) and
		Duel.SelectYesNo(1-tp,aux.Stringid(66666004,2)) then
		Duel.Win(1-tp,0x0)
	end
end
