name: Alien
card_text: If this card destroys a monster with an A-Counter by battle and sends it to the Graveyard, Special Summon the destroyed monster to your side of the field at the end of the Battle Phase. When this card is removed from the field, all monsters that were Special Summoned by this card's effect are destroyed.
external_ids: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local COUNTER_ID_0 = 0x100e --A-Counter
  
lua: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local COUNTER_ID_0 = 0x100e --A-Counter
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--check
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_CONTINUOUS)
  	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e1:SetCode(EVENT_BATTLED)
  	e1:SetOperation(s.checkop)
  	c:RegisterEffect(e1)
  	local g = Group.CreateGroup()
  	e1:SetLabelObject(g)
  	g:KeepAlive()
  	--special summon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_CONTINUOUS)
  	e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e2:SetCode(EVENT_BATTLE_DESTROYING)
  	e2:SetOperation(s.checkop2)
  	e2:SetLabelObject(e1)
  	c:RegisterEffect(e2)
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 0))
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e3:SetCode(EVENT_PHASE + PHASE_BATTLE)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCountLimit(1)
  	e3:SetCondition(s.spcon)
  	e3:SetTarget(s.sptg)
  	e3:SetOperation(s.spop)
  	e3:SetLabelObject(e2)
  	c:RegisterEffect(e3)
  	--destroy
  	local e4 = Effect.CreateEffect(c)
  	e4:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_CONTINUOUS)
  	e4:SetCode(EVENT_LEAVE_FIELD)
  	e4:SetOperation(s.desop)
  	c:RegisterEffect(e4)
  end
  function s.checkop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local t = Duel.GetAttackTarget()
  	if t and t ~= c and t:GetCounter(COUNTER_ID_0) > 0 then
  		e:SetLabel(1)
  	else
  		e:SetLabel(0)
  	end
  end
  function s.checkop2(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if e:GetLabelObject():GetLabel() == 0 then
  		return
  	end
  	local t = c:GetBattleTarget()
  	local g = e:GetLabelObject():GetLabelObject()
  	if c:GetFieldID() ~= e:GetLabel() then
  		g:Clear()
  		e:SetLabel(c:GetFieldID())
  	end
  	if aux.bdgcon(e, tp, eg, ep, ev, re, r, rp) then
  		g:AddCard(t)
  		t:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_BATTLE, 0, 1)
  	end
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():GetFieldID() == e:GetLabelObject():GetLabel()
  end
  function s.filter(c, e, tp)
  	return c:GetFlagEffect(id) ~= 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local g = e:GetLabelObject():GetLabelObject():GetLabelObject()
  	if chk == 0 then
  		return g:IsExists(s.filter, 1, nil, e, tp)
  	end
  	local dg = g:Filter(s.filter, nil, e, tp)
  	g:Clear()
  	Duel.SetTargetCard(dg)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, dg, dg:GetCount(), 0, 0)
  end
  function s.sfilter(c, e, tp)
  	return c:IsRelateToEffect(e) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.spop(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
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS)
  	local sg = g:Filter(s.sfilter, nil, e, tp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	if sg:GetCount() > ft then
  		sg = sg:Select(tp, ft, ft, nil)
  	end
  	local tc = sg:GetFirst()
  	local c = e:GetHandler()
  	while tc do
  		Duel.SpecialSummonStep(tc, 0, tp, tp, false, false, POS_FACEUP)
  		tc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD, 0, 0)
  		c:CreateRelation(tc, RESET_EVENT + 0x1020000)
  		tc = sg:GetNext()
  	end
  	Duel.SpecialSummonComplete()
  end
  function s.desfilter(c, rc)
  	return c:GetFlagEffect(id) ~= 0 and rc:IsRelateToCard(c)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local dg = Duel.GetMatchingGroup(s.desfilter, tp, LOCATION_MZONE, LOCATION_MZONE, nil, e:GetHandler())
  	Duel.Destroy(dg, REASON_EFFECT)
  end
  
  
source_text: Alien
  If this card destroys a monster with an A-Counter by battle and sends it to the Graveyard, Special Summon the destroyed monster to your side of the field at the end of the Battle Phase. When this card is removed from the field, all monsters that were Special Summoned by this card's effect are destroyed.
target_text: -- Alien
  local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local COUNTER_ID_0 = 0x100e --A-Counter
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--check
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_CONTINUOUS)
  	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e1:SetCode(EVENT_BATTLED)
  	e1:SetOperation(s.checkop)
  	c:RegisterEffect(e1)
  	local g = Group.CreateGroup()
  	e1:SetLabelObject(g)
  	g:KeepAlive()
  	--special summon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_CONTINUOUS)
  	e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e2:SetCode(EVENT_BATTLE_DESTROYING)
  	e2:SetOperation(s.checkop2)
  	e2:SetLabelObject(e1)
  	c:RegisterEffect(e2)
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 0))
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e3:SetCode(EVENT_PHASE + PHASE_BATTLE)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCountLimit(1)
  	e3:SetCondition(s.spcon)
  	e3:SetTarget(s.sptg)
  	e3:SetOperation(s.spop)
  	e3:SetLabelObject(e2)
  	c:RegisterEffect(e3)
  	--destroy
  	local e4 = Effect.CreateEffect(c)
  	e4:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_CONTINUOUS)
  	e4:SetCode(EVENT_LEAVE_FIELD)
  	e4:SetOperation(s.desop)
  	c:RegisterEffect(e4)
  end
  function s.checkop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local t = Duel.GetAttackTarget()
  	if t and t ~= c and t:GetCounter(COUNTER_ID_0) > 0 then
  		e:SetLabel(1)
  	else
  		e:SetLabel(0)
  	end
  end
  function s.checkop2(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if e:GetLabelObject():GetLabel() == 0 then
  		return
  	end
  	local t = c:GetBattleTarget()
  	local g = e:GetLabelObject():GetLabelObject()
  	if c:GetFieldID() ~= e:GetLabel() then
  		g:Clear()
  		e:SetLabel(c:GetFieldID())
  	end
  	if aux.bdgcon(e, tp, eg, ep, ev, re, r, rp) then
  		g:AddCard(t)
  		t:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_BATTLE, 0, 1)
  	end
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():GetFieldID() == e:GetLabelObject():GetLabel()
  end
  function s.filter(c, e, tp)
  	return c:GetFlagEffect(id) ~= 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local g = e:GetLabelObject():GetLabelObject():GetLabelObject()
  	if chk == 0 then
  		return g:IsExists(s.filter, 1, nil, e, tp)
  	end
  	local dg = g:Filter(s.filter, nil, e, tp)
  	g:Clear()
  	Duel.SetTargetCard(dg)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, dg, dg:GetCount(), 0, 0)
  end
  function s.sfilter(c, e, tp)
  	return c:IsRelateToEffect(e) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.spop(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
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS)
  	local sg = g:Filter(s.sfilter, nil, e, tp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	if sg:GetCount() > ft then
  		sg = sg:Select(tp, ft, ft, nil)
  	end
  	local tc = sg:GetFirst()
  	local c = e:GetHandler()
  	while tc do
  		Duel.SpecialSummonStep(tc, 0, tp, tp, false, false, POS_FACEUP)
  		tc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD, 0, 0)
  		c:CreateRelation(tc, RESET_EVENT + 0x1020000)
  		tc = sg:GetNext()
  	end
  	Duel.SpecialSummonComplete()
  end
  function s.desfilter(c, rc)
  	return c:GetFlagEffect(id) ~= 0 and rc:IsRelateToCard(c)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local dg = Duel.GetMatchingGroup(s.desfilter, tp, LOCATION_MZONE, LOCATION_MZONE, nil, e:GetHandler())
  	Duel.Destroy(dg, REASON_EFFECT)
  end
  
  