name: Level
card_text: If a monster(s) you control is destroyed by battle or an opponent's card effect: Target 1 of those monsters; Special Summon monster(s) from your Deck in Defense Position so their combined Levels equal the Level of that monster, but negate their effects. You can only activate 1 "Level Resist Wall" per turn.
external_ids: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  
lua: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate/Special Summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_DESTROYED)
  	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY + EFFECT_FLAG_CARD_TARGET)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.tgfilter(c, e, tp, rp, g, ft)
  	local lv = c:GetLevel()
  	if
  		not (
  			(c:IsReason(REASON_BATTLE) or (rp == 1 - tp and c:IsReason(REASON_EFFECT)))
  			and c:IsPreviousLocation(LOCATION_MZONE)
  			and c:IsPreviousControler(tp)
  			and c:IsLocation(LOCATION_GRAVE + LOCATION_REMOVED)
  			and c:IsCanBeEffectTarget(e)
  			and lv > 0
  		)
  	then
  		return false
  	end
  	local sg = g:Filter(Card.IsLevelBelow, nil, lv)
  	aux.GCheckAdditional = s.gcheck(lv)
  	local res = sg:CheckSubGroup(s.fgoal, 1, ft, lv)
  	aux.GCheckAdditional = nil
  	return res
  end
  function s.gcheck(lv)
  	return function(sg)
  		return sg:GetSum(Card.GetLevel) <= lv
  	end
  end
  function s.fgoal(sg, lv)
  	return sg:GetSum(Card.GetLevel) == lv
  end
  function s.spfilter(c, e, tp)
  	return c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP_DEFENSE)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	if ft > 1 and Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  		ft = 1
  	end
  	local g = Duel.GetMatchingGroup(s.spfilter, tp, LOCATION_DECK, 0, nil, e, tp)
  	if chkc then
  		return eg:IsContains(chkc) and s.tgfilter(chkc, e, tp, rp, g, ft)
  	end
  	if chk == 0 then
  		return ft > 0 and eg:IsExists(s.tgfilter, 1, nil, e, tp, rp, g, ft)
  	end
  	local tg
  	if #eg == 1 then
  		tg = eg
  	else
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  		tg = eg:FilterSelect(tp, s.tgfilter, 1, 1, nil, e, tp, rp, g, ft)
  	end
  	Duel.SetTargetCard(tg)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_DECK)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	if ft <= 0 then
  		return
  	end
  	if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  		ft = 1
  	end
  	local g = Duel.GetMatchingGroup(s.spfilter, tp, LOCATION_DECK, 0, nil, e, tp)
  	local lv = tc:GetLevel()
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	aux.GCheckAdditional = s.gcheck(lv)
  	local sg = g:SelectSubGroup(tp, s.fgoal, false, 1, ft, lv)
  	aux.GCheckAdditional = nil
  	if not sg then
  		return
  	end
  	local tc = sg:GetFirst()
  	while tc do
  		Duel.SpecialSummonStep(tc, 0, tp, tp, false, false, POS_FACEUP_DEFENSE)
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_DISABLE)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  		tc:RegisterEffect(e1)
  		local e2 = e1:Clone()
  		e2:SetCode(EFFECT_DISABLE_EFFECT)
  		e2:SetValue(RESET_TURN_SET)
  		tc:RegisterEffect(e2)
  		tc = sg:GetNext()
  	end
  	Duel.SpecialSummonComplete()
  end
  
  
source_text: Level
  If a monster(s) you control is destroyed by battle or an opponent's card effect: Target 1 of those monsters; Special Summon monster(s) from your Deck in Defense Position so their combined Levels equal the Level of that monster, but negate their effects. You can only activate 1 "Level Resist Wall" per turn.
target_text: -- Level
  local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate/Special Summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_DESTROYED)
  	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY + EFFECT_FLAG_CARD_TARGET)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.tgfilter(c, e, tp, rp, g, ft)
  	local lv = c:GetLevel()
  	if
  		not (
  			(c:IsReason(REASON_BATTLE) or (rp == 1 - tp and c:IsReason(REASON_EFFECT)))
  			and c:IsPreviousLocation(LOCATION_MZONE)
  			and c:IsPreviousControler(tp)
  			and c:IsLocation(LOCATION_GRAVE + LOCATION_REMOVED)
  			and c:IsCanBeEffectTarget(e)
  			and lv > 0
  		)
  	then
  		return false
  	end
  	local sg = g:Filter(Card.IsLevelBelow, nil, lv)
  	aux.GCheckAdditional = s.gcheck(lv)
  	local res = sg:CheckSubGroup(s.fgoal, 1, ft, lv)
  	aux.GCheckAdditional = nil
  	return res
  end
  function s.gcheck(lv)
  	return function(sg)
  		return sg:GetSum(Card.GetLevel) <= lv
  	end
  end
  function s.fgoal(sg, lv)
  	return sg:GetSum(Card.GetLevel) == lv
  end
  function s.spfilter(c, e, tp)
  	return c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP_DEFENSE)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	if ft > 1 and Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  		ft = 1
  	end
  	local g = Duel.GetMatchingGroup(s.spfilter, tp, LOCATION_DECK, 0, nil, e, tp)
  	if chkc then
  		return eg:IsContains(chkc) and s.tgfilter(chkc, e, tp, rp, g, ft)
  	end
  	if chk == 0 then
  		return ft > 0 and eg:IsExists(s.tgfilter, 1, nil, e, tp, rp, g, ft)
  	end
  	local tg
  	if #eg == 1 then
  		tg = eg
  	else
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  		tg = eg:FilterSelect(tp, s.tgfilter, 1, 1, nil, e, tp, rp, g, ft)
  	end
  	Duel.SetTargetCard(tg)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_DECK)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	if ft <= 0 then
  		return
  	end
  	if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  		ft = 1
  	end
  	local g = Duel.GetMatchingGroup(s.spfilter, tp, LOCATION_DECK, 0, nil, e, tp)
  	local lv = tc:GetLevel()
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	aux.GCheckAdditional = s.gcheck(lv)
  	local sg = g:SelectSubGroup(tp, s.fgoal, false, 1, ft, lv)
  	aux.GCheckAdditional = nil
  	if not sg then
  		return
  	end
  	local tc = sg:GetFirst()
  	while tc do
  		Duel.SpecialSummonStep(tc, 0, tp, tp, false, false, POS_FACEUP_DEFENSE)
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_DISABLE)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  		tc:RegisterEffect(e1)
  		local e2 = e1:Clone()
  		e2:SetCode(EFFECT_DISABLE_EFFECT)
  		e2:SetValue(RESET_TURN_SET)
  		tc:RegisterEffect(e2)
  		tc = sg:GetNext()
  	end
  	Duel.SpecialSummonComplete()
  end
  
  