name: Bug
card_text: Target 2 Level 3 Insect-Type monsters in your Graveyard; Special Summon them, but their effects are negated. You can banish this card and 1 Xyz Monster from your Graveyard; the Levels of all Level 3 Insect-Type monsters you currently control become equal to the banished monster's Rank until the end of this turn, also you cannot Special Summon monsters for the rest of this turn, except Insect-Type monsters. You can only activate 1 "Bug Emergency" 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
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--Change level
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCost(s.cost)
  	e2:SetTarget(s.target)
  	e2:SetOperation(s.operation)
  	c:RegisterEffect(e2)
  end
  function s.spfilter(c, e, tp)
  	return c:IsLevel(3) and c:IsRace(RACE_INSECT) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.spfilter(chkc, e, tp)
  	end
  	if chk == 0 then
  		return not Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) and Duel.GetLocationCount(tp, LOCATION_MZONE) > 1 and Duel.IsExistingTarget(s.spfilter, tp, LOCATION_GRAVE, 0, 2, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectTarget(tp, s.spfilter, tp, LOCATION_GRAVE, 0, 2, 2, nil, e, tp)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 2, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS):Filter(Card.IsRelateToEffect, nil, e)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) < g:GetCount() or (g:GetCount() > 1 and Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0)) then
  		return
  	end
  	local tc = g:GetFirst()
  	while tc do
  		Duel.SpecialSummonStep(tc, 0, tp, tp, false, false, POS_FACEUP)
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_DISABLE)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  		tc:RegisterEffect(e1, true)
  		local e2 = Effect.CreateEffect(e:GetHandler())
  		e2:SetType(EFFECT_TYPE_SINGLE)
  		e2:SetCode(EFFECT_DISABLE_EFFECT)
  		e2:SetValue(RESET_TURN_SET)
  		e2:SetReset(RESET_EVENT + RESETS_STANDARD)
  		tc:RegisterEffect(e2, true)
  		tc = g:GetNext()
  	end
  	Duel.SpecialSummonComplete()
  end
  function s.cfilter(c, e, tp)
  	return c:IsType(TYPE_XYZ) and c:GetRank() ~= 3 and c:IsAbleToRemoveAsCost()
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToRemoveAsCost() and Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g = Duel.SelectMatchingCard(tp, s.cfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	e:SetLabel(g:GetFirst():GetRank())
  	g:AddCard(e:GetHandler())
  	Duel.Remove(g, POS_FACEUP, REASON_COST)
  end
  function s.filter(c)
  	return c:IsFaceup() and c:IsLevel(3) and c:IsRace(RACE_INSECT)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local g = Duel.GetMatchingGroup(s.filter, tp, LOCATION_MZONE, 0, nil)
  	local tc = g:GetFirst()
  	while tc do
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_CHANGE_LEVEL)
  		e1:SetValue(e:GetLabel())
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  		tc:RegisterEffect(e1)
  		tc = g:GetNext()
  	end
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
  	e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e2:SetTargetRange(1, 0)
  	e2:SetTarget(s.splimit)
  	e2:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e2, tp)
  end
  function s.splimit(e, c, sump, sumtype, sumpos, targetp, se)
  	return not c:IsRace(RACE_INSECT)
  end
  
  
source_text: Bug
  Target 2 Level 3 Insect-Type monsters in your Graveyard; Special Summon them, but their effects are negated. You can banish this card and 1 Xyz Monster from your Graveyard; the Levels of all Level 3 Insect-Type monsters you currently control become equal to the banished monster's Rank until the end of this turn, also you cannot Special Summon monsters for the rest of this turn, except Insect-Type monsters. You can only activate 1 "Bug Emergency" per turn.
target_text: -- Bug
  local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--Change level
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCost(s.cost)
  	e2:SetTarget(s.target)
  	e2:SetOperation(s.operation)
  	c:RegisterEffect(e2)
  end
  function s.spfilter(c, e, tp)
  	return c:IsLevel(3) and c:IsRace(RACE_INSECT) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.spfilter(chkc, e, tp)
  	end
  	if chk == 0 then
  		return not Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) and Duel.GetLocationCount(tp, LOCATION_MZONE) > 1 and Duel.IsExistingTarget(s.spfilter, tp, LOCATION_GRAVE, 0, 2, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectTarget(tp, s.spfilter, tp, LOCATION_GRAVE, 0, 2, 2, nil, e, tp)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 2, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS):Filter(Card.IsRelateToEffect, nil, e)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) < g:GetCount() or (g:GetCount() > 1 and Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0)) then
  		return
  	end
  	local tc = g:GetFirst()
  	while tc do
  		Duel.SpecialSummonStep(tc, 0, tp, tp, false, false, POS_FACEUP)
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_DISABLE)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  		tc:RegisterEffect(e1, true)
  		local e2 = Effect.CreateEffect(e:GetHandler())
  		e2:SetType(EFFECT_TYPE_SINGLE)
  		e2:SetCode(EFFECT_DISABLE_EFFECT)
  		e2:SetValue(RESET_TURN_SET)
  		e2:SetReset(RESET_EVENT + RESETS_STANDARD)
  		tc:RegisterEffect(e2, true)
  		tc = g:GetNext()
  	end
  	Duel.SpecialSummonComplete()
  end
  function s.cfilter(c, e, tp)
  	return c:IsType(TYPE_XYZ) and c:GetRank() ~= 3 and c:IsAbleToRemoveAsCost()
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToRemoveAsCost() and Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g = Duel.SelectMatchingCard(tp, s.cfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	e:SetLabel(g:GetFirst():GetRank())
  	g:AddCard(e:GetHandler())
  	Duel.Remove(g, POS_FACEUP, REASON_COST)
  end
  function s.filter(c)
  	return c:IsFaceup() and c:IsLevel(3) and c:IsRace(RACE_INSECT)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local g = Duel.GetMatchingGroup(s.filter, tp, LOCATION_MZONE, 0, nil)
  	local tc = g:GetFirst()
  	while tc do
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_CHANGE_LEVEL)
  		e1:SetValue(e:GetLabel())
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  		tc:RegisterEffect(e1)
  		tc = g:GetNext()
  	end
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
  	e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e2:SetTargetRange(1, 0)
  	e2:SetTarget(s.splimit)
  	e2:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e2, tp)
  end
  function s.splimit(e, c, sump, sumtype, sumpos, targetp, se)
  	return not c:IsRace(RACE_INSECT)
  end
  
  