name: Valkyrie
card_text: During your Main Phase: You can excavate the top cards of your Deck equal to the number of other "Valkyrie" monsters you control, and if you do, add 1 excavated Normal Spell/Trap to your hand, also send the remaining cards to the GY. Otherwise, shuffle all excavated cards into the Deck. When this card is destroyed by battle and sent to the GY: You can Special Summon 1 "Valkyrie" monster from your Deck. You can only use each effect of "Valkyrie Vierte" once per turn.
external_ids: local SETCARD_ID_0 = 0x122 --Valkyrie
  
lua: local SETCARD_ID_0 = 0x122 --Valkyrie
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--deck search
  	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, id)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	--spsummon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_BATTLE_DESTROYED)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCondition(s.spcon)
  	e2:SetTarget(s.sptg)
  	e2:SetOperation(s.spop)
  	c:RegisterEffect(e2)
  end
  function s.ctfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		local ct = Duel.GetMatchingGroupCount(s.ctfilter, tp, LOCATION_MZONE, 0, e:GetHandler())
  		if ct == 0 or Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) < ct then
  			return false
  		end
  		local g = Duel.GetDecktopGroup(tp, ct)
  		return g:FilterCount(Card.IsAbleToHand, nil) > 0
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, 0, LOCATION_DECK)
  end
  function s.thfilter(c)
  	return c:GetType() == TYPE_SPELL or c:GetType() == TYPE_TRAP
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local ct = Duel.GetMatchingGroupCount(s.ctfilter, tp, LOCATION_MZONE, 0, e:GetHandler())
  	if ct == 0 then
  		return
  	end
  	local g = Duel.GetDecktopGroup(tp, ct)
  	Duel.ConfirmDecktop(tp, ct)
  	local tg = g:Filter(s.thfilter, nil)
  	if tg:GetCount() > 0 then
  		Duel.DisableShuffleCheck()
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local tc = tg:Select(tp, 1, 1, nil):GetFirst()
  		if tc:IsAbleToHand() then
  			Duel.SendtoHand(tc, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, tc)
  			Duel.ShuffleHand(tp)
  		else
  			Duel.SendtoGrave(tc, REASON_RULE)
  		end
  		g:RemoveCard(tc)
  		Duel.SendtoGrave(g, REASON_EFFECT + REASON_REVEAL)
  	else
  		Duel.ShuffleDeck(tp)
  	end
  end
  function s.spfilter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsLocation(LOCATION_GRAVE)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_DECK, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_DECK)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_DECK, 0, 1, 1, nil, e, tp)
  	if g:GetCount() > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  
source_text: Valkyrie
  During your Main Phase: You can excavate the top cards of your Deck equal to the number of other "Valkyrie" monsters you control, and if you do, add 1 excavated Normal Spell/Trap to your hand, also send the remaining cards to the GY. Otherwise, shuffle all excavated cards into the Deck. When this card is destroyed by battle and sent to the GY: You can Special Summon 1 "Valkyrie" monster from your Deck. You can only use each effect of "Valkyrie Vierte" once per turn.
target_text: -- Valkyrie
  local SETCARD_ID_0 = 0x122 --Valkyrie
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--deck search
  	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, id)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	--spsummon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_BATTLE_DESTROYED)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCondition(s.spcon)
  	e2:SetTarget(s.sptg)
  	e2:SetOperation(s.spop)
  	c:RegisterEffect(e2)
  end
  function s.ctfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		local ct = Duel.GetMatchingGroupCount(s.ctfilter, tp, LOCATION_MZONE, 0, e:GetHandler())
  		if ct == 0 or Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) < ct then
  			return false
  		end
  		local g = Duel.GetDecktopGroup(tp, ct)
  		return g:FilterCount(Card.IsAbleToHand, nil) > 0
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, 0, LOCATION_DECK)
  end
  function s.thfilter(c)
  	return c:GetType() == TYPE_SPELL or c:GetType() == TYPE_TRAP
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local ct = Duel.GetMatchingGroupCount(s.ctfilter, tp, LOCATION_MZONE, 0, e:GetHandler())
  	if ct == 0 then
  		return
  	end
  	local g = Duel.GetDecktopGroup(tp, ct)
  	Duel.ConfirmDecktop(tp, ct)
  	local tg = g:Filter(s.thfilter, nil)
  	if tg:GetCount() > 0 then
  		Duel.DisableShuffleCheck()
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local tc = tg:Select(tp, 1, 1, nil):GetFirst()
  		if tc:IsAbleToHand() then
  			Duel.SendtoHand(tc, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, tc)
  			Duel.ShuffleHand(tp)
  		else
  			Duel.SendtoGrave(tc, REASON_RULE)
  		end
  		g:RemoveCard(tc)
  		Duel.SendtoGrave(g, REASON_EFFECT + REASON_REVEAL)
  	else
  		Duel.ShuffleDeck(tp)
  	end
  end
  function s.spfilter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsLocation(LOCATION_GRAVE)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_DECK, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_DECK)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_DECK, 0, 1, 1, nil, e, tp)
  	if g:GetCount() > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  