--直播☆双子愿望单
local s,id,o=GetID()
function s.initial_effect(c)
	local g=Group.CreateGroup()
	g:KeepAlive()
	--Activate
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetTarget(s.target)
	e1:SetOperation(s.activate)
	e1:SetLabelObject(g)
	c:RegisterEffect(e1)  
	--salvage
	local e3=Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(id,0))
	e3:SetCategory(CATEGORY_TOHAND)
	e3:SetProperty(EFFECT_FLAG_DELAY)
	e3:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_O)
	e3:SetCode(EVENT_CUSTOM+id)
	e3:SetRange(LOCATION_SZONE)
	e3:SetCondition(s.con)
	e3:SetTarget(s.thtg)
	e3:SetOperation(s.thop)
	e3:SetLabelObject(g)
	c:RegisterEffect(e3)
	if not s.global_check then
		s.global_check=true
		if not recg1 then recg1=0 end
		if not recg2 then recg2=0 end
		local ge1=Effect.CreateEffect(c)
		ge1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
		ge1:SetCode(EVENT_RECOVER)
		ge1:SetLabelObject(g)
		ge1:SetOperation(s.checkop)
		Duel.RegisterEffect(ge1,0)
	end
end
function s.ffilter1(c)
	return c:IsFaceup() and c:IsSetCard(0x152)
end
function s.ffilter2(c)
	return c:IsFaceup() and c:IsSetCard(0x153)
end
function s.checkop(e,tp,eg,ep,ev,re,r,rp)
	local eg=e:GetLabelObject()
	local a=Duel.IsExistingMatchingCard(s.ffilter1,ep,LOCATION_ONFIELD,0,1,nil)
	local b=Duel.IsExistingMatchingCard(s.ffilter2,ep,LOCATION_ONFIELD,0,1,nil)
	if ep==0 and (a or b) then
		recg1=recg1+ev
		if recg1>1100 then
			Duel.RaiseEvent(eg,EVENT_CUSTOM+id,re,r,rp,ep,0)
			recg1=recg1-1100
		end
	elseif ep==1 and (a or b) then
		recg2=recg2+ev
		if recg2>1100 then
			Duel.RaiseEvent(eg,EVENT_CUSTOM+id,re,r,rp,ep,1)
			recg2=recg2-1100
		end
	end
end
function s.filter(c)
	return c:IsAbleToRemove()
end
function s.target(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsExistingMatchingCard(s.filter,tp,LOCATION_DECK,0,5,nil) end
	Duel.SetOperationInfo(0,CATEGORY_REMOVE,nil,3,tp,LOCATION_DECK)
end
function s.activate(e,tp,eg,ep,ev,re,r,rp)
	local g=Duel.GetMatchingGroup(s.filter,tp,LOCATION_DECK,0,nil)
	if g:GetCount()<3 then return end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVE)
	local rg=g:Select(tp,3,3,nil)
	Duel.ConfirmCards(1-tp,rg)
	Duel.Remove(rg,POS_FACEDOWN,REASON_EFFECT)
	local tc=rg:GetFirst()
	while tc do
		tc:RegisterFlagEffect(id,RESET_EVENT+RESETS_STANDARD,0,0)
		tc=rg:GetNext()
	end
	e:GetLabelObject():Clear()
	e:GetLabelObject():Merge(rg)
end
function s.con(e,tp,eg,ep,ev,re,r,rp)
	local a=Duel.IsExistingMatchingCard(s.ffilter1,tp,LOCATION_ONFIELD,0,1,nil)
	local b=Duel.IsExistingMatchingCard(s.ffilter2,tp,LOCATION_ONFIELD,0,1,nil)
	return ev==tp and (a or b)
end
function s.thfilter(c)
	return c:GetFlagEffect(id)~=0 and c:IsAbleToHand()
end
function s.thtg(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	if chkc then return e:GetLabelObject():IsContains(chkc) and s.thfilter(chkc) end
	if chk==0 then return e:GetLabelObject():IsExists(s.thfilter,1,nil) end
	Duel.Hint(HINT_SELECTMSG,1-tp,HINTMSG_ATOHAND)
	local g=e:GetLabelObject():FilterSelect(1-tp,s.thfilter,1,1,nil)
	e:GetLabelObject():Sub(g)
	Duel.SetTargetCard(g)
	Duel.SetOperationInfo(0,CATEGORY_TOHAND,nil,1,tp,LOCATION_REMOVED)
end
function s.thop(e,tp,eg,ep,ev,re,r,rp)
	local tc=Duel.GetFirstTarget()
	if tc:IsRelateToEffect(e) then
		Duel.SendtoHand(tc,nil,REASON_EFFECT)
		Duel.ConfirmCards(1-tp,tc)
	end
end