name: Gagagaback
card_text: During the turn a "Gagaga" monster you control was destroyed by battle and sent to the Graveyard: Special Summon from your Graveyard as many monsters as possible that were destroyed by battle this turn, in face-up Defense Position, then take 600 damage for each monster Special Summoned by this effect. You can only activate 1 "Gagagaback" per turn.
external_ids: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local SETCARD_ID_0 = 0x54 --Gagaga
  
lua: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local SETCARD_ID_0 = 0x54 --Gagaga
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(0, TIMING_END_PHASE)
  	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_BATTLE_DESTROYED)
  		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:IsSetCard(SETCARD_ID_0) and tc:IsLocation(LOCATION_GRAVE) and tc:IsReason(REASON_BATTLE) and tc:GetControler() == tc:GetPreviousControler() then
  			s[tc:GetControler()] = 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.filter(c, id, e, tp)
  	return c:IsReason(REASON_BATTLE) and c:GetTurnID() == id and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP_DEFENSE)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return s[tp] and Duel.GetFlagEffect(tp, id) == 0
  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.IsExistingMatchingCard(s.filter, tp, LOCATION_GRAVE, 0, 1, nil, Duel.GetTurnCount(), e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_GRAVE)
  	Duel.SetOperationInfo(0, CATEGORY_DAMAGE, nil, 0, tp, 0)
  	Duel.RegisterFlagEffect(tp, id, RESET_PHASE + PHASE_END, 0, 1)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	if ft <= 0 then
  		return
  	end
  	if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  		ft = 1
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_GRAVE, 0, ft, ft, nil, Duel.GetTurnCount(), e, tp)
  	if g:GetCount() > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP_DEFENSE)
  		Duel.BreakEffect()
  		Duel.Damage(tp, g:GetCount() * 600, REASON_EFFECT)
  	end
  end
  
  
source_text: Gagagaback
  During the turn a "Gagaga" monster you control was destroyed by battle and sent to the Graveyard: Special Summon from your Graveyard as many monsters as possible that were destroyed by battle this turn, in face-up Defense Position, then take 600 damage for each monster Special Summoned by this effect. You can only activate 1 "Gagagaback" per turn.
target_text: -- Gagagaback
  local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local SETCARD_ID_0 = 0x54 --Gagaga
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(0, TIMING_END_PHASE)
  	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_BATTLE_DESTROYED)
  		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:IsSetCard(SETCARD_ID_0) and tc:IsLocation(LOCATION_GRAVE) and tc:IsReason(REASON_BATTLE) and tc:GetControler() == tc:GetPreviousControler() then
  			s[tc:GetControler()] = 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.filter(c, id, e, tp)
  	return c:IsReason(REASON_BATTLE) and c:GetTurnID() == id and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP_DEFENSE)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return s[tp] and Duel.GetFlagEffect(tp, id) == 0
  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.IsExistingMatchingCard(s.filter, tp, LOCATION_GRAVE, 0, 1, nil, Duel.GetTurnCount(), e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_GRAVE)
  	Duel.SetOperationInfo(0, CATEGORY_DAMAGE, nil, 0, tp, 0)
  	Duel.RegisterFlagEffect(tp, id, RESET_PHASE + PHASE_END, 0, 1)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	if ft <= 0 then
  		return
  	end
  	if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  		ft = 1
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_GRAVE, 0, ft, ft, nil, Duel.GetTurnCount(), e, tp)
  	if g:GetCount() > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP_DEFENSE)
  		Duel.BreakEffect()
  		Duel.Damage(tp, g:GetCount() * 600, REASON_EFFECT)
  	end
  end
  
  