name: Reasoning
card_text: Your opponent declares a monster Level from 1 to 12. Excavate cards from the top of your Deck until you excavate a monster that can be Normal Summoned/Set, then, if that monster is the same Level as the one declared by your opponent, send all excavated cards to the GY. If not, Special Summon the excavated monster, also send the remaining cards to the GY.
external_ids: local CARD_ID_0 = 63060238 --Elemental HERO Blazeman
  local CARD_ID_1 = 97148796 --Drytron Alpha Thuban
  
lua: local CARD_ID_0 = 63060238 --Elemental HERO Blazeman
  local CARD_ID_1 = 97148796 --Drytron Alpha Thuban
  local s, id, o = GetID()
  function s.initial_effect(c)
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  			and Duel.IsPlayerCanSpecialSummon(tp)
  			and not Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0)
  			and not Duel.IsPlayerAffectedByEffect(tp, CARD_ID_1)
  			and Duel.IsExistingMatchingCard(Card.IsSummonableCard, tp, LOCATION_DECK, 0, 1, nil)
  			and Duel.IsPlayerCanDiscardDeck(tp, 1)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, 0, LOCATION_DECK)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	if not Duel.IsPlayerCanSpecialSummon(tp) or not Duel.IsPlayerCanDiscardDeck(tp, 1) then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, 1 - tp, HINGMSG_LVRANK)
  	local lv = Duel.AnnounceLevel(1 - tp)
  	local g = Duel.GetMatchingGroup(Card.IsSummonableCard, tp, LOCATION_DECK, 0, nil)
  	local dcount = Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0)
  	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
  	if seq == -1 then
  		Duel.ConfirmDecktop(tp, dcount)
  		Duel.ShuffleDeck(tp)
  		return
  	end
  	Duel.ConfirmDecktop(tp, dcount - seq)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and not spcard:IsLevel(lv) and spcard:IsCanBeSpecialSummoned(e, 0, tp, false, false) then
  		Duel.DisableShuffleCheck()
  		if dcount - seq == 1 then
  			Duel.SpecialSummon(spcard, 0, tp, tp, false, false, POS_FACEUP)
  		else
  			Duel.SpecialSummonStep(spcard, 0, tp, tp, false, false, POS_FACEUP)
  			Duel.DiscardDeck(tp, dcount - seq - 1, REASON_EFFECT)
  			Duel.SpecialSummonComplete()
  		end
  	else
  		Duel.DiscardDeck(tp, dcount - seq, REASON_EFFECT + REASON_REVEAL)
  	end
  end
  
  
source_text: Reasoning
  Your opponent declares a monster Level from 1 to 12. Excavate cards from the top of your Deck until you excavate a monster that can be Normal Summoned/Set, then, if that monster is the same Level as the one declared by your opponent, send all excavated cards to the GY. If not, Special Summon the excavated monster, also send the remaining cards to the GY.
target_text: -- Reasoning
  local CARD_ID_0 = 63060238 --Elemental HERO Blazeman
  local CARD_ID_1 = 97148796 --Drytron Alpha Thuban
  local s, id, o = GetID()
  function s.initial_effect(c)
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  			and Duel.IsPlayerCanSpecialSummon(tp)
  			and not Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0)
  			and not Duel.IsPlayerAffectedByEffect(tp, CARD_ID_1)
  			and Duel.IsExistingMatchingCard(Card.IsSummonableCard, tp, LOCATION_DECK, 0, 1, nil)
  			and Duel.IsPlayerCanDiscardDeck(tp, 1)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, 0, LOCATION_DECK)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	if not Duel.IsPlayerCanSpecialSummon(tp) or not Duel.IsPlayerCanDiscardDeck(tp, 1) then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, 1 - tp, HINGMSG_LVRANK)
  	local lv = Duel.AnnounceLevel(1 - tp)
  	local g = Duel.GetMatchingGroup(Card.IsSummonableCard, tp, LOCATION_DECK, 0, nil)
  	local dcount = Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0)
  	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
  	if seq == -1 then
  		Duel.ConfirmDecktop(tp, dcount)
  		Duel.ShuffleDeck(tp)
  		return
  	end
  	Duel.ConfirmDecktop(tp, dcount - seq)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and not spcard:IsLevel(lv) and spcard:IsCanBeSpecialSummoned(e, 0, tp, false, false) then
  		Duel.DisableShuffleCheck()
  		if dcount - seq == 1 then
  			Duel.SpecialSummon(spcard, 0, tp, tp, false, false, POS_FACEUP)
  		else
  			Duel.SpecialSummonStep(spcard, 0, tp, tp, false, false, POS_FACEUP)
  			Duel.DiscardDeck(tp, dcount - seq - 1, REASON_EFFECT)
  			Duel.SpecialSummonComplete()
  		end
  	else
  		Duel.DiscardDeck(tp, dcount - seq, REASON_EFFECT + REASON_REVEAL)
  	end
  end
  
  