name: Magical
card_text: FLIP: Excavate cards from the top of your Deck until you excavate a Spell/Trap, then add that card to your hand, also send the remaining cards to the GY.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--flip
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH + CATEGORY_DECKDES)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_FLIP)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(Card.IsType, tp, LOCATION_DECK, 0, nil, TYPE_SPELL + TYPE_TRAP)
  	local dcount = Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0)
  	if dcount == 0 then
  		return
  	end
  	if g:GetCount() == 0 then
  		Duel.ConfirmDecktop(tp, dcount)
  		Duel.ShuffleDeck(tp)
  		return
  	end
  	local seq = -1
  	local tc = g:GetFirst()
  	local spcard = nil
  	while tc do
  		if tc:GetSequence() > seq then
  			seq = tc:GetSequence()
  			spcard = tc
  		end
  		tc = g:GetNext()
  	end
  	Duel.ConfirmDecktop(tp, dcount - seq)
  	if spcard:IsAbleToHand() then
  		Duel.DisableShuffleCheck()
  		Duel.SendtoHand(spcard, nil, REASON_EFFECT)
  		Duel.DiscardDeck(tp, dcount - seq - 1, REASON_EFFECT + REASON_REVEAL)
  		Duel.ConfirmCards(1 - tp, spcard)
  		Duel.ShuffleHand(tp)
  	else
  		Duel.DiscardDeck(tp, dcount - seq, REASON_EFFECT + REASON_REVEAL)
  	end
  end
  
  
source_text: Magical
  FLIP: Excavate cards from the top of your Deck until you excavate a Spell/Trap, then add that card to your hand, also send the remaining cards to the GY.
target_text: -- Magical
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--flip
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH + CATEGORY_DECKDES)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_FLIP)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(Card.IsType, tp, LOCATION_DECK, 0, nil, TYPE_SPELL + TYPE_TRAP)
  	local dcount = Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0)
  	if dcount == 0 then
  		return
  	end
  	if g:GetCount() == 0 then
  		Duel.ConfirmDecktop(tp, dcount)
  		Duel.ShuffleDeck(tp)
  		return
  	end
  	local seq = -1
  	local tc = g:GetFirst()
  	local spcard = nil
  	while tc do
  		if tc:GetSequence() > seq then
  			seq = tc:GetSequence()
  			spcard = tc
  		end
  		tc = g:GetNext()
  	end
  	Duel.ConfirmDecktop(tp, dcount - seq)
  	if spcard:IsAbleToHand() then
  		Duel.DisableShuffleCheck()
  		Duel.SendtoHand(spcard, nil, REASON_EFFECT)
  		Duel.DiscardDeck(tp, dcount - seq - 1, REASON_EFFECT + REASON_REVEAL)
  		Duel.ConfirmCards(1 - tp, spcard)
  		Duel.ShuffleHand(tp)
  	else
  		Duel.DiscardDeck(tp, dcount - seq, REASON_EFFECT + REASON_REVEAL)
  	end
  end
  
  