name: Final
card_text: If a monster(s) with an original Level of 7 or higher is sent from each player's field to the Graveyard this turn: Banish as many monsters from the Graveyards as possible, then you can Special Summon the 1 highest-Level Spellcaster-Type monster among those banished monsters to your field (your choice, if tied).
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_REMOVE)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(0, TIMING_END_PHASE + TIMING_TOGRAVE)
  	e1:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	if not s.global_check then
  		s.global_check = true
  		s[0] = false
  		s[1] = false
  		local ge1 = Effect.CreateEffect(c)
  		ge1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		ge1:SetCode(EVENT_TO_GRAVE)
  		ge1:SetOperation(s.checkop)
  		Duel.RegisterEffect(ge1, 0)
  		local ge2 = Effect.CreateEffect(c)
  		ge2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		ge2:SetCode(EVENT_PHASE_START + PHASE_DRAW)
  		ge2:SetOperation(s.clear)
  		Duel.RegisterEffect(ge2, 0)
  	end
  end
  function s.checkop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = eg:GetFirst()
  	while tc do
  		if tc:GetOriginalLevel() >= 7 and tc:IsPreviousLocation(LOCATION_MZONE) then
  			s[tc:GetPreviousControler()] = true
  		end
  		tc = eg:GetNext()
  	end
  end
  function s.clear(e, tp, eg, ep, ev, re, r, rp)
  	s[0] = false
  	s[1] = false
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return s[0] and s[1]
  end
  function s.filter(c)
  	return c:IsType(TYPE_MONSTER) and c:IsAbleToRemove()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_GRAVE, LOCATION_GRAVE, 1, nil)
  	end
  	local g = Duel.GetMatchingGroup(s.filter, tp, LOCATION_GRAVE, LOCATION_GRAVE, nil)
  	Duel.SetOperationInfo(0, CATEGORY_REMOVE, g, g:GetCount(), 0, 0)
  end
  function s.spfilter(c, e, tp)
  	return c:IsRace(RACE_SPELLCASTER) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false) and c:IsLocation(LOCATION_REMOVED) and c:GetLevel() > 0
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(s.filter, tp, LOCATION_GRAVE, LOCATION_GRAVE, nil)
  	if Duel.Remove(g, POS_FACEUP, REASON_EFFECT) > 0 then
  		local og = Duel.GetOperatedGroup():Filter(s.spfilter, nil, e, tp)
  		if og:GetCount() > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 0)) then
  			Duel.BreakEffect()
  			local sg = og:GetMaxGroup(Card.GetLevel)
  			if sg:GetCount() > 1 then
  				Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  				sg = sg:Select(tp, 1, 1, nil)
  			end
  			Duel.SpecialSummon(sg, 0, tp, tp, false, false, POS_FACEUP)
  		end
  	end
  end
  
  
source_text: Final
  If a monster(s) with an original Level of 7 or higher is sent from each player's field to the Graveyard this turn: Banish as many monsters from the Graveyards as possible, then you can Special Summon the 1 highest-Level Spellcaster-Type monster among those banished monsters to your field (your choice, if tied).
target_text: -- Final
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_REMOVE)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(0, TIMING_END_PHASE + TIMING_TOGRAVE)
  	e1:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	if not s.global_check then
  		s.global_check = true
  		s[0] = false
  		s[1] = false
  		local ge1 = Effect.CreateEffect(c)
  		ge1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		ge1:SetCode(EVENT_TO_GRAVE)
  		ge1:SetOperation(s.checkop)
  		Duel.RegisterEffect(ge1, 0)
  		local ge2 = Effect.CreateEffect(c)
  		ge2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		ge2:SetCode(EVENT_PHASE_START + PHASE_DRAW)
  		ge2:SetOperation(s.clear)
  		Duel.RegisterEffect(ge2, 0)
  	end
  end
  function s.checkop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = eg:GetFirst()
  	while tc do
  		if tc:GetOriginalLevel() >= 7 and tc:IsPreviousLocation(LOCATION_MZONE) then
  			s[tc:GetPreviousControler()] = true
  		end
  		tc = eg:GetNext()
  	end
  end
  function s.clear(e, tp, eg, ep, ev, re, r, rp)
  	s[0] = false
  	s[1] = false
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return s[0] and s[1]
  end
  function s.filter(c)
  	return c:IsType(TYPE_MONSTER) and c:IsAbleToRemove()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_GRAVE, LOCATION_GRAVE, 1, nil)
  	end
  	local g = Duel.GetMatchingGroup(s.filter, tp, LOCATION_GRAVE, LOCATION_GRAVE, nil)
  	Duel.SetOperationInfo(0, CATEGORY_REMOVE, g, g:GetCount(), 0, 0)
  end
  function s.spfilter(c, e, tp)
  	return c:IsRace(RACE_SPELLCASTER) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false) and c:IsLocation(LOCATION_REMOVED) and c:GetLevel() > 0
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(s.filter, tp, LOCATION_GRAVE, LOCATION_GRAVE, nil)
  	if Duel.Remove(g, POS_FACEUP, REASON_EFFECT) > 0 then
  		local og = Duel.GetOperatedGroup():Filter(s.spfilter, nil, e, tp)
  		if og:GetCount() > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 0)) then
  			Duel.BreakEffect()
  			local sg = og:GetMaxGroup(Card.GetLevel)
  			if sg:GetCount() > 1 then
  				Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  				sg = sg:Select(tp, 1, 1, nil)
  			end
  			Duel.SpecialSummon(sg, 0, tp, tp, false, false, POS_FACEUP)
  		end
  	end
  end
  
  