name: Red-Eyes
card_text: If a Level 7 or lower "Red-Eyes" monster(s) you control is destroyed by your opponent's attack or card effect and sent to your GY, while this card is in your hand: You can Special Summon this card in Defense Position, and if you do, Special Summon as many of those destroyed monsters as possible in the same position they were in when destroyed. You can Tribute this card; you can Normal Summon 1 "Red-Eyes" monster during your Main Phase this turn in addition to your Normal Summon/Set. (You can only gain this effect once per turn.)
external_ids: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local SETCARD_ID_0 = 0x3b --Red-Eyes
  
lua: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local SETCARD_ID_0 = 0x3b --Red-Eyes
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_GRAVE_SPSUMMON)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_CUSTOM + id)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	aux.RegisterMergedDelayedEvent(c, id, EVENT_TO_GRAVE)
  	--extra summon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCondition(s.sumcon)
  	e2:SetCost(s.sumcost)
  	e2:SetTarget(s.sumtg)
  	e2:SetOperation(s.sumop)
  	c:RegisterEffect(e2)
  end
  function s.spfilter(c, e, tp)
  	return c:IsPreviousPosition(POS_FACEUP)
  		and c:IsPreviousControler(tp)
  		and c:IsPreviousLocation(LOCATION_MZONE)
  		and c:IsReason(REASON_DESTROY)
  		and (c:IsReason(REASON_EFFECT) and c:GetReasonPlayer() == 1 - tp or c:IsReason(REASON_BATTLE) and Duel.GetAttacker():IsControler(1 - tp))
  		and c:IsSetCard(SETCARD_ID_0)
  		and c:IsLevelBelow(7)
  		and c:IsControler(tp)
  		and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, c:GetPreviousPosition())
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return not Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0)
  			and Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  			and e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP_DEFENSE)
  			and eg:IsExists(s.spfilter, 1, nil, e, tp)
  	end
  	local g = eg:Filter(s.spfilter, nil, e, tp)
  	Duel.SetTargetCard(g)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, e:GetHandler(), 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_LEAVE_GRAVE, g, g:GetCount(), 0, 0)
  end
  function s.filter(c, e, tp)
  	return c:IsRelateToEffect(e) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, c:GetPreviousPosition())
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS):Filter(s.filter, nil, e, tp)
  	if c:IsRelateToEffect(e) then
  		if Duel.SpecialSummonStep(c, 0, tp, tp, false, false, POS_FACEUP_DEFENSE) then
  			local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  			if ft > 0 and g:GetCount() > 0 and not Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  				if g:GetCount() > ft then
  					Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  					g = g:Select(tp, ft, ft, nil)
  				end
  				local tc = g:GetFirst()
  				while tc do
  					Duel.SpecialSummonStep(tc, 0, tp, tp, false, false, tc:GetPreviousPosition())
  					tc = g:GetNext()
  				end
  			end
  		end
  		Duel.SpecialSummonComplete()
  	end
  end
  function s.sumcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFlagEffect(tp, id) == 0
  end
  function s.sumcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsReleasable()
  	end
  	Duel.Release(e:GetHandler(), REASON_COST)
  end
  function s.sumtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanSummon(tp) and Duel.IsPlayerCanAdditionalSummon(tp)
  	end
  end
  function s.sumop(e, tp, eg, ep, ev, re, r, rp)
  	local e1 = Effect.CreateEffect(e:GetHandler())
  	e1:SetDescription(aux.Stringid(id, 2))
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_EXTRA_SUMMON_COUNT)
  	e1:SetTargetRange(LOCATION_HAND + LOCATION_MZONE, 0)
  	e1:SetTarget(aux.TargetBoolFunction(Card.IsSetCard, SETCARD_ID_0))
  	e1:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e1, tp)
  	Duel.RegisterFlagEffect(tp, id, RESET_PHASE + PHASE_END, 0, 1)
  end
  
  
source_text: Red-Eyes
  If a Level 7 or lower "Red-Eyes" monster(s) you control is destroyed by your opponent's attack or card effect and sent to your GY, while this card is in your hand: You can Special Summon this card in Defense Position, and if you do, Special Summon as many of those destroyed monsters as possible in the same position they were in when destroyed. You can Tribute this card; you can Normal Summon 1 "Red-Eyes" monster during your Main Phase this turn in addition to your Normal Summon/Set. (You can only gain this effect once per turn.)
target_text: -- Red-Eyes
  local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local SETCARD_ID_0 = 0x3b --Red-Eyes
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_GRAVE_SPSUMMON)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_CUSTOM + id)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	aux.RegisterMergedDelayedEvent(c, id, EVENT_TO_GRAVE)
  	--extra summon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCondition(s.sumcon)
  	e2:SetCost(s.sumcost)
  	e2:SetTarget(s.sumtg)
  	e2:SetOperation(s.sumop)
  	c:RegisterEffect(e2)
  end
  function s.spfilter(c, e, tp)
  	return c:IsPreviousPosition(POS_FACEUP)
  		and c:IsPreviousControler(tp)
  		and c:IsPreviousLocation(LOCATION_MZONE)
  		and c:IsReason(REASON_DESTROY)
  		and (c:IsReason(REASON_EFFECT) and c:GetReasonPlayer() == 1 - tp or c:IsReason(REASON_BATTLE) and Duel.GetAttacker():IsControler(1 - tp))
  		and c:IsSetCard(SETCARD_ID_0)
  		and c:IsLevelBelow(7)
  		and c:IsControler(tp)
  		and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, c:GetPreviousPosition())
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return not Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0)
  			and Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  			and e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP_DEFENSE)
  			and eg:IsExists(s.spfilter, 1, nil, e, tp)
  	end
  	local g = eg:Filter(s.spfilter, nil, e, tp)
  	Duel.SetTargetCard(g)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, e:GetHandler(), 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_LEAVE_GRAVE, g, g:GetCount(), 0, 0)
  end
  function s.filter(c, e, tp)
  	return c:IsRelateToEffect(e) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, c:GetPreviousPosition())
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS):Filter(s.filter, nil, e, tp)
  	if c:IsRelateToEffect(e) then
  		if Duel.SpecialSummonStep(c, 0, tp, tp, false, false, POS_FACEUP_DEFENSE) then
  			local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  			if ft > 0 and g:GetCount() > 0 and not Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  				if g:GetCount() > ft then
  					Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  					g = g:Select(tp, ft, ft, nil)
  				end
  				local tc = g:GetFirst()
  				while tc do
  					Duel.SpecialSummonStep(tc, 0, tp, tp, false, false, tc:GetPreviousPosition())
  					tc = g:GetNext()
  				end
  			end
  		end
  		Duel.SpecialSummonComplete()
  	end
  end
  function s.sumcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFlagEffect(tp, id) == 0
  end
  function s.sumcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsReleasable()
  	end
  	Duel.Release(e:GetHandler(), REASON_COST)
  end
  function s.sumtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanSummon(tp) and Duel.IsPlayerCanAdditionalSummon(tp)
  	end
  end
  function s.sumop(e, tp, eg, ep, ev, re, r, rp)
  	local e1 = Effect.CreateEffect(e:GetHandler())
  	e1:SetDescription(aux.Stringid(id, 2))
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_EXTRA_SUMMON_COUNT)
  	e1:SetTargetRange(LOCATION_HAND + LOCATION_MZONE, 0)
  	e1:SetTarget(aux.TargetBoolFunction(Card.IsSetCard, SETCARD_ID_0))
  	e1:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e1, tp)
  	Duel.RegisterFlagEffect(tp, id, RESET_PHASE + PHASE_END, 0, 1)
  end
  
  