name: Kelbek
card_text: If a card(s) is sent from the hand or Deck to your opponent's GY (except during the Damage Step): You can target 1 Special Summoned monster your opponent controls; Special Summon this card from your hand, then return that monster to the hand. If this card is sent from the hand or Deck to the GY: You can activate this effect; each player sends the top 5 cards of their Deck to the GY (or their entire Deck, if less than 5), then, if "Exchange of the Spirit" is in your GY, you can Set 1 Trap from your GY. You can only use each effect of "Kelbek the Ancient Vanguard" once per turn.
external_ids: local CARD_ID_0 = 17484499 --Exchange of the Spirit
  
lua: local CARD_ID_0 = 17484499 --Exchange of the Spirit
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_0)
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_TOHAND)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_TO_GRAVE)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id)
  	e1:SetProperty(EFFECT_FLAG_DELAY + EFFECT_FLAG_CARD_TARGET)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--deckdes
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_DECKDES)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetCountLimit(1, id + o)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCondition(s.tgcon)
  	e2:SetTarget(s.tgtg)
  	e2:SetOperation(s.tgop)
  	c:RegisterEffect(e2)
  end
  function s.cfilter(c, tp)
  	return c:IsPreviousLocation(LOCATION_DECK + LOCATION_HAND) and c:IsControler(1 - tp)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp)
  end
  function s.filter(c)
  	return c:IsSummonType(SUMMON_TYPE_SPECIAL) and c:IsAbleToHand()
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(1 - tp) and s.filter(chkc)
  	end
  	local c = e:GetHandler()
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.filter, tp, 0, LOCATION_MZONE, 1, nil) and Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_RTOHAND)
  	local g = Duel.SelectTarget(tp, s.filter, tp, 0, LOCATION_MZONE, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, c, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, g, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) and Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP) > 0 then
  		local tc = Duel.GetFirstTarget()
  		if tc:IsRelateToEffect(e) and tc:IsAbleToHand() then
  			Duel.BreakEffect()
  			Duel.SendtoHand(tc, nil, REASON_EFFECT)
  		end
  	end
  end
  function s.tgcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsPreviousLocation(LOCATION_DECK + LOCATION_HAND)
  end
  function s.tgtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanDiscardDeck(tp, 5) and Duel.IsPlayerCanDiscardDeck(1 - tp, 5)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DECKDES, nil, 0, PLAYER_ALL, 5)
  end
  function s.sfilter(c)
  	return c:IsType(TYPE_TRAP) and c:IsSSetable()
  end
  function s.tgop(e, tp, eg, ep, ev, re, r, rp)
  	local g1 = Duel.GetDecktopGroup(tp, 5)
  	local g2 = Duel.GetDecktopGroup(1 - tp, 5)
  	g1:Merge(g2)
  	Duel.DisableShuffleCheck()
  	if Duel.SendtoGrave(g1, REASON_EFFECT) ~= 0 and g1:IsExists(Card.IsLocation, 1, nil, LOCATION_GRAVE) and Duel.IsExistingMatchingCard(Card.IsCode, tp, LOCATION_GRAVE, 0, 1, nil, CARD_ID_0) then
  		local g = Duel.GetMatchingGroup(aux.NecroValleyFilter(s.sfilter), tp, LOCATION_GRAVE, 0, nil)
  		if #g > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 2)) then
  			Duel.BreakEffect()
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SET)
  			local sg = g:Select(tp, 1, 1, nil)
  			Duel.SSet(tp, sg)
  		end
  	end
  end
  
  
source_text: Kelbek
  If a card(s) is sent from the hand or Deck to your opponent's GY (except during the Damage Step): You can target 1 Special Summoned monster your opponent controls; Special Summon this card from your hand, then return that monster to the hand. If this card is sent from the hand or Deck to the GY: You can activate this effect; each player sends the top 5 cards of their Deck to the GY (or their entire Deck, if less than 5), then, if "Exchange of the Spirit" is in your GY, you can Set 1 Trap from your GY. You can only use each effect of "Kelbek the Ancient Vanguard" once per turn.
target_text: -- Kelbek
  local CARD_ID_0 = 17484499 --Exchange of the Spirit
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_0)
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_TOHAND)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_TO_GRAVE)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id)
  	e1:SetProperty(EFFECT_FLAG_DELAY + EFFECT_FLAG_CARD_TARGET)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--deckdes
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_DECKDES)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetCountLimit(1, id + o)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCondition(s.tgcon)
  	e2:SetTarget(s.tgtg)
  	e2:SetOperation(s.tgop)
  	c:RegisterEffect(e2)
  end
  function s.cfilter(c, tp)
  	return c:IsPreviousLocation(LOCATION_DECK + LOCATION_HAND) and c:IsControler(1 - tp)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp)
  end
  function s.filter(c)
  	return c:IsSummonType(SUMMON_TYPE_SPECIAL) and c:IsAbleToHand()
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(1 - tp) and s.filter(chkc)
  	end
  	local c = e:GetHandler()
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.filter, tp, 0, LOCATION_MZONE, 1, nil) and Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_RTOHAND)
  	local g = Duel.SelectTarget(tp, s.filter, tp, 0, LOCATION_MZONE, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, c, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, g, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) and Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP) > 0 then
  		local tc = Duel.GetFirstTarget()
  		if tc:IsRelateToEffect(e) and tc:IsAbleToHand() then
  			Duel.BreakEffect()
  			Duel.SendtoHand(tc, nil, REASON_EFFECT)
  		end
  	end
  end
  function s.tgcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsPreviousLocation(LOCATION_DECK + LOCATION_HAND)
  end
  function s.tgtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanDiscardDeck(tp, 5) and Duel.IsPlayerCanDiscardDeck(1 - tp, 5)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DECKDES, nil, 0, PLAYER_ALL, 5)
  end
  function s.sfilter(c)
  	return c:IsType(TYPE_TRAP) and c:IsSSetable()
  end
  function s.tgop(e, tp, eg, ep, ev, re, r, rp)
  	local g1 = Duel.GetDecktopGroup(tp, 5)
  	local g2 = Duel.GetDecktopGroup(1 - tp, 5)
  	g1:Merge(g2)
  	Duel.DisableShuffleCheck()
  	if Duel.SendtoGrave(g1, REASON_EFFECT) ~= 0 and g1:IsExists(Card.IsLocation, 1, nil, LOCATION_GRAVE) and Duel.IsExistingMatchingCard(Card.IsCode, tp, LOCATION_GRAVE, 0, 1, nil, CARD_ID_0) then
  		local g = Duel.GetMatchingGroup(aux.NecroValleyFilter(s.sfilter), tp, LOCATION_GRAVE, 0, nil)
  		if #g > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 2)) then
  			Duel.BreakEffect()
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SET)
  			local sg = g:Select(tp, 1, 1, nil)
  			Duel.SSet(tp, sg)
  		end
  	end
  end
  
  