name: Alsei,
card_text: 2 Level 8 monsters
  Once per turn: You can declare 1 card name; excavate the top card of your Deck, and if it is the declared card, add it to your hand. Otherwise, send it to the GY. If a card(s) is sent from your Deck to the GY by a card effect (except during the Damage Step): You can detach 1 material from this card, then target 1 card on the field; place that target on either the top or bottom of the Deck. You can only use this effect of "Alsei, the Sylvan High Protector" once per turn.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--xyz summon
  	aux.AddXyzProcedure(c, nil, 8, 2)
  	c:EnableReviveLimit()
  	--announce
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH + CATEGORY_DECKDES)
  	e1:SetType(EFFECT_TYPE_IGNITION)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCountLimit(1)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	--to deck
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TODECK)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DELAY)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.tdcon)
  	e2:SetCost(s.tdcost)
  	e2:SetTarget(s.tdtg)
  	e2:SetOperation(s.tdop)
  	c:RegisterEffect(e2)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanDiscardDeck(tp, 1)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_CODE)
  	getmetatable(e:GetHandler()).announce_filter = { TYPE_FUSION + TYPE_SYNCHRO + TYPE_XYZ + TYPE_LINK, OPCODE_ISTYPE, OPCODE_NOT }
  	local ac = Duel.AnnounceCard(tp, table.unpack(getmetatable(e:GetHandler()).announce_filter))
  	Duel.SetTargetParam(ac)
  	Duel.SetOperationInfo(0, CATEGORY_ANNOUNCE, nil, 0, tp, 0)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	if not Duel.IsPlayerCanDiscardDeck(tp, 1) then
  		return
  	end
  	Duel.ConfirmDecktop(tp, 1)
  	local g = Duel.GetDecktopGroup(tp, 1)
  	local tc = g:GetFirst()
  	local ac = Duel.GetChainInfo(0, CHAININFO_TARGET_PARAM)
  	if tc:IsCode(ac) and tc:IsAbleToHand() then
  		Duel.DisableShuffleCheck()
  		Duel.SendtoHand(tc, nil, REASON_EFFECT)
  		Duel.ShuffleHand(tp)
  	else
  		Duel.DisableShuffleCheck()
  		Duel.SendtoGrave(tc, REASON_EFFECT + REASON_REVEAL)
  	end
  end
  function s.cfilter(c, tp)
  	return c:IsReason(REASON_EFFECT) and c:IsPreviousLocation(LOCATION_DECK) and c:IsPreviousControler(tp)
  end
  function s.tdcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp)
  end
  function s.tdcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():CheckRemoveOverlayCard(tp, 1, REASON_COST)
  	end
  	e:GetHandler():RemoveOverlayCard(tp, 1, 1, REASON_COST)
  end
  function s.tdtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsOnField() and chkc:IsAbleToDeck()
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(Card.IsAbleToDeck, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TODECK)
  	local g = Duel.SelectTarget(tp, Card.IsAbleToDeck, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, g, 1, 0, 0)
  end
  function s.tdop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		if tc:IsExtraDeckMonster() or Duel.SelectOption(tp, aux.Stringid(id, 2), aux.Stringid(id, 3)) == 0 then
  			Duel.SendtoDeck(tc, nil, SEQ_DECKTOP, REASON_EFFECT)
  		else
  			Duel.SendtoDeck(tc, nil, SEQ_DECKBOTTOM, REASON_EFFECT)
  		end
  	end
  end
  
  
source_text: Alsei,
  2 Level 8 monsters
  Once per turn: You can declare 1 card name; excavate the top card of your Deck, and if it is the declared card, add it to your hand. Otherwise, send it to the GY. If a card(s) is sent from your Deck to the GY by a card effect (except during the Damage Step): You can detach 1 material from this card, then target 1 card on the field; place that target on either the top or bottom of the Deck. You can only use this effect of "Alsei, the Sylvan High Protector" once per turn.
target_text: -- Alsei,
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--xyz summon
  	aux.AddXyzProcedure(c, nil, 8, 2)
  	c:EnableReviveLimit()
  	--announce
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH + CATEGORY_DECKDES)
  	e1:SetType(EFFECT_TYPE_IGNITION)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCountLimit(1)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	--to deck
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TODECK)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DELAY)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.tdcon)
  	e2:SetCost(s.tdcost)
  	e2:SetTarget(s.tdtg)
  	e2:SetOperation(s.tdop)
  	c:RegisterEffect(e2)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanDiscardDeck(tp, 1)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_CODE)
  	getmetatable(e:GetHandler()).announce_filter = { TYPE_FUSION + TYPE_SYNCHRO + TYPE_XYZ + TYPE_LINK, OPCODE_ISTYPE, OPCODE_NOT }
  	local ac = Duel.AnnounceCard(tp, table.unpack(getmetatable(e:GetHandler()).announce_filter))
  	Duel.SetTargetParam(ac)
  	Duel.SetOperationInfo(0, CATEGORY_ANNOUNCE, nil, 0, tp, 0)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	if not Duel.IsPlayerCanDiscardDeck(tp, 1) then
  		return
  	end
  	Duel.ConfirmDecktop(tp, 1)
  	local g = Duel.GetDecktopGroup(tp, 1)
  	local tc = g:GetFirst()
  	local ac = Duel.GetChainInfo(0, CHAININFO_TARGET_PARAM)
  	if tc:IsCode(ac) and tc:IsAbleToHand() then
  		Duel.DisableShuffleCheck()
  		Duel.SendtoHand(tc, nil, REASON_EFFECT)
  		Duel.ShuffleHand(tp)
  	else
  		Duel.DisableShuffleCheck()
  		Duel.SendtoGrave(tc, REASON_EFFECT + REASON_REVEAL)
  	end
  end
  function s.cfilter(c, tp)
  	return c:IsReason(REASON_EFFECT) and c:IsPreviousLocation(LOCATION_DECK) and c:IsPreviousControler(tp)
  end
  function s.tdcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp)
  end
  function s.tdcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():CheckRemoveOverlayCard(tp, 1, REASON_COST)
  	end
  	e:GetHandler():RemoveOverlayCard(tp, 1, 1, REASON_COST)
  end
  function s.tdtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsOnField() and chkc:IsAbleToDeck()
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(Card.IsAbleToDeck, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TODECK)
  	local g = Duel.SelectTarget(tp, Card.IsAbleToDeck, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, g, 1, 0, 0)
  end
  function s.tdop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		if tc:IsExtraDeckMonster() or Duel.SelectOption(tp, aux.Stringid(id, 2), aux.Stringid(id, 3)) == 0 then
  			Duel.SendtoDeck(tc, nil, SEQ_DECKTOP, REASON_EFFECT)
  		else
  			Duel.SendtoDeck(tc, nil, SEQ_DECKBOTTOM, REASON_EFFECT)
  		end
  	end
  end
  
  