name: Battlewasp
card_text: When your "Battlewasp" monster is targeted for an attack: Special Summon 1 "Battlewasp" monster from your hand or Deck, and if you do, place 1 counter on this card, then end the Battle Phase. You can only use this effect of "Battlewasp - Nest" once per turn. If this card has 2 or more counters during the End Phase, send it to the GY.
external_ids: local SETCARD_ID_0 = 0x12f --Battlewasp
  local COUNTER_ID_0 = 0x51 --Counter(Battlewasp
  
lua: local SETCARD_ID_0 = 0x12f --Battlewasp
  local COUNTER_ID_0 = 0x51 --Counter(Battlewasp
  local s, id, o = GetID()
  function s.initial_effect(c)
  	Duel.EnableGlobalFlag(GLOBALFLAG_SELF_TOGRAVE)
  	c:EnableCounterPermit(0x51)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--SpecialSummon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_BE_BATTLE_TARGET)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.condition)
  	e2:SetTarget(s.target)
  	e2:SetOperation(s.activate)
  	c:RegisterEffect(e2)
  	--to Grave
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_SINGLE)
  	e3:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e3:SetCode(EFFECT_SELF_TOGRAVE)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetCondition(s.tgcon)
  	c:RegisterEffect(e3)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	local tc = eg:GetFirst()
  	return tc:IsControler(tp) and tc:IsFaceup() and tc:IsSetCard(SETCARD_ID_0)
  end
  function s.filter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsCanAddCounter(tp, COUNTER_ID_0, 1, e:GetHandler())
  			and Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  			and Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_DECK + LOCATION_HAND, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_DECK + LOCATION_HAND)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_DECK, 0, 1, 1, nil, e, tp)
  	if #g > 0 and Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP) ~= 0 and e:GetHandler():AddCounter(COUNTER_ID_0, 1) ~= 0 then
  		Duel.BreakEffect()
  		Duel.SkipPhase(1 - tp, PHASE_BATTLE, RESET_PHASE + PHASE_BATTLE_STEP, 1)
  	end
  end
  function s.tgcon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	return Duel.GetCurrentPhase() == PHASE_END and c:GetCounter(COUNTER_ID_0) >= 2
  end
  
  
source_text: Battlewasp
  When your "Battlewasp" monster is targeted for an attack: Special Summon 1 "Battlewasp" monster from your hand or Deck, and if you do, place 1 counter on this card, then end the Battle Phase. You can only use this effect of "Battlewasp - Nest" once per turn. If this card has 2 or more counters during the End Phase, send it to the GY.
target_text: -- Battlewasp
  local SETCARD_ID_0 = 0x12f --Battlewasp
  local COUNTER_ID_0 = 0x51 --Counter(Battlewasp
  local s, id, o = GetID()
  function s.initial_effect(c)
  	Duel.EnableGlobalFlag(GLOBALFLAG_SELF_TOGRAVE)
  	c:EnableCounterPermit(0x51)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--SpecialSummon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_BE_BATTLE_TARGET)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.condition)
  	e2:SetTarget(s.target)
  	e2:SetOperation(s.activate)
  	c:RegisterEffect(e2)
  	--to Grave
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_SINGLE)
  	e3:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e3:SetCode(EFFECT_SELF_TOGRAVE)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetCondition(s.tgcon)
  	c:RegisterEffect(e3)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	local tc = eg:GetFirst()
  	return tc:IsControler(tp) and tc:IsFaceup() and tc:IsSetCard(SETCARD_ID_0)
  end
  function s.filter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsCanAddCounter(tp, COUNTER_ID_0, 1, e:GetHandler())
  			and Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  			and Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_DECK + LOCATION_HAND, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_DECK + LOCATION_HAND)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_DECK, 0, 1, 1, nil, e, tp)
  	if #g > 0 and Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP) ~= 0 and e:GetHandler():AddCounter(COUNTER_ID_0, 1) ~= 0 then
  		Duel.BreakEffect()
  		Duel.SkipPhase(1 - tp, PHASE_BATTLE, RESET_PHASE + PHASE_BATTLE_STEP, 1)
  	end
  end
  function s.tgcon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	return Duel.GetCurrentPhase() == PHASE_END and c:GetCounter(COUNTER_ID_0) >= 2
  end
  
  