name: Zefra
card_text: 2+ monsters Special Summoned from the Extra Deck
  If a monster(s) this card points to that was Special Summoned from the Extra Deck is destroyed by battle or an opponent's card effect: Add 1 monster from your GY or 1 face-up Pendulum Monster from your Extra Deck to your hand. You can target 1 monster on each player's field that was Special Summoned from the Extra Deck, except this card; banish both until the End Phase. You can only use each effect of "Zefra Metaltron" once per turn.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableReviveLimit()
  	aux.AddLinkProcedure(c, s.matfilter, 2)
  	--to hand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_TOHAND)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetProperty(EFFECT_FLAG_DELAY + EFFECT_FLAG_DAMAGE_STEP)
  	e2:SetCode(EVENT_DESTROYED)
  	e2:SetCountLimit(1, id)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCondition(s.thcon)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  	--banish
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_REMOVE)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCountLimit(1, id + 1)
  	e3:SetTarget(s.rmtg)
  	e3:SetOperation(s.rmop)
  	c:RegisterEffect(e3)
  end
  function s.matfilter(c)
  	return c:IsSummonLocation(LOCATION_EXTRA)
  end
  function s.cfilter(c, tp, zone)
  	local seq = c:GetPreviousSequence()
  	return c:IsPreviousControler(tp)
  		and bit.extract(zone, seq) ~= 0
  		and c:IsSummonLocation(LOCATION_EXTRA)
  		and c:IsPreviousLocation(LOCATION_MZONE)
  		and (c:IsReason(REASON_BATTLE) or c:IsReason(REASON_EFFECT) and c:GetReasonPlayer() == 1 - tp)
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	local zone = e:GetHandler():GetLinkedZone()
  	return eg:IsExists(s.cfilter, 1, nil, tp, zone)
  end
  function s.thfilter(c)
  	return (c:IsLocation(LOCATION_GRAVE) or (c:IsFaceup() and c:IsType(TYPE_PENDULUM))) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_GRAVE + LOCATION_EXTRA, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_GRAVE + LOCATION_EXTRA)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.thfilter), tp, LOCATION_GRAVE + LOCATION_EXTRA, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  function s.rmfilter(c)
  	return c:IsFaceup() and c:IsSummonLocation(LOCATION_EXTRA) and c:IsAbleToRemove()
  end
  function s.rmtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return false
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.rmfilter, tp, LOCATION_MZONE, 0, 1, e:GetHandler()) and Duel.IsExistingTarget(s.rmfilter, tp, 0, LOCATION_MZONE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g1 = Duel.SelectTarget(tp, s.rmfilter, tp, LOCATION_MZONE, 0, 1, 1, e:GetHandler())
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g2 = Duel.SelectTarget(tp, s.rmfilter, tp, 0, LOCATION_MZONE, 1, 1, nil)
  	g1:Merge(g2)
  	Duel.SetOperationInfo(0, CATEGORY_REMOVE, g1, 2, 0, 0)
  end
  function s.rmop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetTargetsRelateToChain():Filter(Card.IsAbleToRemove, nil)
  	if g:GetCount() ~= 2 then
  		return
  	end
  	if Duel.Remove(g, 0, REASON_EFFECT + REASON_TEMPORARY) ~= 0 and g:IsExists(Card.IsLocation, 1, nil, LOCATION_REMOVED) then
  		local og = Duel.GetOperatedGroup():Filter(Card.IsLocation, nil, LOCATION_REMOVED)
  		local c = e:GetHandler()
  		for tc in aux.Next(og) do
  			tc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END, 0, 1)
  		end
  		og:KeepAlive()
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		e1:SetCode(EVENT_PHASE + PHASE_END)
  		e1:SetReset(RESET_PHASE + PHASE_END)
  		e1:SetLabelObject(og)
  		e1:SetCountLimit(1)
  		e1:SetCondition(s.retcon)
  		e1:SetOperation(s.retop)
  		Duel.RegisterEffect(e1, tp)
  	end
  end
  function s.retfilter(c)
  	return c:GetFlagEffect(id) ~= 0
  end
  function s.retcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetLabelObject():IsExists(s.retfilter, 1, nil)
  end
  function s.retop(e, tp, eg, ep, ev, re, r, rp)
  	local g = e:GetLabelObject():Filter(s.retfilter, nil)
  	for tc in aux.Next(g) do
  		Duel.ReturnToField(tc)
  	end
  end
  
  
source_text: Zefra
  2+ monsters Special Summoned from the Extra Deck
  If a monster(s) this card points to that was Special Summoned from the Extra Deck is destroyed by battle or an opponent's card effect: Add 1 monster from your GY or 1 face-up Pendulum Monster from your Extra Deck to your hand. You can target 1 monster on each player's field that was Special Summoned from the Extra Deck, except this card; banish both until the End Phase. You can only use each effect of "Zefra Metaltron" once per turn.
target_text: -- Zefra
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableReviveLimit()
  	aux.AddLinkProcedure(c, s.matfilter, 2)
  	--to hand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_TOHAND)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetProperty(EFFECT_FLAG_DELAY + EFFECT_FLAG_DAMAGE_STEP)
  	e2:SetCode(EVENT_DESTROYED)
  	e2:SetCountLimit(1, id)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCondition(s.thcon)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  	--banish
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_REMOVE)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCountLimit(1, id + 1)
  	e3:SetTarget(s.rmtg)
  	e3:SetOperation(s.rmop)
  	c:RegisterEffect(e3)
  end
  function s.matfilter(c)
  	return c:IsSummonLocation(LOCATION_EXTRA)
  end
  function s.cfilter(c, tp, zone)
  	local seq = c:GetPreviousSequence()
  	return c:IsPreviousControler(tp)
  		and bit.extract(zone, seq) ~= 0
  		and c:IsSummonLocation(LOCATION_EXTRA)
  		and c:IsPreviousLocation(LOCATION_MZONE)
  		and (c:IsReason(REASON_BATTLE) or c:IsReason(REASON_EFFECT) and c:GetReasonPlayer() == 1 - tp)
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	local zone = e:GetHandler():GetLinkedZone()
  	return eg:IsExists(s.cfilter, 1, nil, tp, zone)
  end
  function s.thfilter(c)
  	return (c:IsLocation(LOCATION_GRAVE) or (c:IsFaceup() and c:IsType(TYPE_PENDULUM))) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_GRAVE + LOCATION_EXTRA, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_GRAVE + LOCATION_EXTRA)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.thfilter), tp, LOCATION_GRAVE + LOCATION_EXTRA, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  function s.rmfilter(c)
  	return c:IsFaceup() and c:IsSummonLocation(LOCATION_EXTRA) and c:IsAbleToRemove()
  end
  function s.rmtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return false
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.rmfilter, tp, LOCATION_MZONE, 0, 1, e:GetHandler()) and Duel.IsExistingTarget(s.rmfilter, tp, 0, LOCATION_MZONE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g1 = Duel.SelectTarget(tp, s.rmfilter, tp, LOCATION_MZONE, 0, 1, 1, e:GetHandler())
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g2 = Duel.SelectTarget(tp, s.rmfilter, tp, 0, LOCATION_MZONE, 1, 1, nil)
  	g1:Merge(g2)
  	Duel.SetOperationInfo(0, CATEGORY_REMOVE, g1, 2, 0, 0)
  end
  function s.rmop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetTargetsRelateToChain():Filter(Card.IsAbleToRemove, nil)
  	if g:GetCount() ~= 2 then
  		return
  	end
  	if Duel.Remove(g, 0, REASON_EFFECT + REASON_TEMPORARY) ~= 0 and g:IsExists(Card.IsLocation, 1, nil, LOCATION_REMOVED) then
  		local og = Duel.GetOperatedGroup():Filter(Card.IsLocation, nil, LOCATION_REMOVED)
  		local c = e:GetHandler()
  		for tc in aux.Next(og) do
  			tc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END, 0, 1)
  		end
  		og:KeepAlive()
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		e1:SetCode(EVENT_PHASE + PHASE_END)
  		e1:SetReset(RESET_PHASE + PHASE_END)
  		e1:SetLabelObject(og)
  		e1:SetCountLimit(1)
  		e1:SetCondition(s.retcon)
  		e1:SetOperation(s.retop)
  		Duel.RegisterEffect(e1, tp)
  	end
  end
  function s.retfilter(c)
  	return c:GetFlagEffect(id) ~= 0
  end
  function s.retcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetLabelObject():IsExists(s.retfilter, 1, nil)
  end
  function s.retop(e, tp, eg, ep, ev, re, r, rp)
  	local g = e:GetLabelObject():Filter(s.retfilter, nil)
  	for tc in aux.Next(g) do
  		Duel.ReturnToField(tc)
  	end
  end
  
  