name: Performapal
card_text: At the start of your Battle Phase: You can place 1 counter on this card (max. 2). This card gains the following effect, based on the number of counters on it.
  ● 1: Once per turn, when an opponent's monster declares an attack: You can negate that attack.
  ● 2: You can Tribute this card; banish all monsters on the field, until your opponent's 2nd End Phase.
external_ids: local COUNTER_ID_0 = 0x50 --Counter(Performapal
  
lua: local COUNTER_ID_0 = 0x50 --Counter(Performapal
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableCounterPermit(0x50)
  	c:SetCounterLimit(0x50, 2)
  	--add counter
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_COUNTER)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_PHASE + PHASE_BATTLE_START)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCountLimit(1)
  	e1:SetCondition(s.ctcon)
  	e1:SetTarget(s.cttg)
  	e1:SetOperation(s.ctop)
  	c:RegisterEffect(e1)
  	--negate attack
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_ATTACK_ANNOUNCE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1)
  	e2:SetCondition(s.negcon)
  	e2:SetOperation(s.negop)
  	c:RegisterEffect(e2)
  	--remove
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 2))
  	e3:SetCategory(CATEGORY_REMOVE)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCondition(s.rmcon)
  	e3:SetCost(s.rmcost)
  	e3:SetTarget(s.rmtg)
  	e3:SetOperation(s.rmop)
  	c:RegisterEffect(e3)
  end
  function s.ctcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() == tp
  end
  function s.cttg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsCanAddCounter(COUNTER_ID_0, 1)
  	end
  end
  function s.ctop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) then
  		c:AddCounter(COUNTER_ID_0, 1)
  	end
  end
  function s.negcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetAttacker():GetControler() ~= tp and e:GetHandler():GetCounter(COUNTER_ID_0) == 1
  end
  function s.negop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.NegateAttack()
  end
  function s.rmcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():GetCounter(COUNTER_ID_0) == 2
  end
  function s.rmcost(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.rmtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(Card.IsAbleToRemove, tp, LOCATION_MZONE, LOCATION_MZONE, 1, e:GetHandler())
  	end
  	local g = Duel.GetMatchingGroup(Card.IsAbleToRemove, tp, LOCATION_MZONE, LOCATION_MZONE, nil)
  	Duel.SetOperationInfo(0, CATEGORY_REMOVE, g, g:GetCount(), 0, 0)
  end
  function s.rmop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(Card.IsAbleToRemove, tp, LOCATION_MZONE, LOCATION_MZONE, nil)
  	if Duel.Remove(g, 0, REASON_EFFECT + REASON_TEMPORARY) ~= 0 then
  		local c = e:GetHandler()
  		local og = Duel.GetOperatedGroup()
  		local fid = c:GetFieldID()
  		local tc = og:GetFirst()
  		while tc do
  			tc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END + RESET_OPPO_TURN, 0, 2, fid)
  			tc = og:GetNext()
  		end
  		c:SetTurnCounter(0)
  		og:KeepAlive()
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		e1:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  		e1:SetCode(EVENT_PHASE + PHASE_END)
  		e1:SetCountLimit(1)
  		e1:SetLabel(fid)
  		e1:SetLabelObject(og)
  		e1:SetCondition(s.retcon)
  		e1:SetOperation(s.retop)
  		e1:SetReset(RESET_PHASE + PHASE_END + RESET_OPPO_TURN, 2)
  		Duel.RegisterEffect(e1, tp)
  	end
  end
  function s.retfilter(c, fid)
  	return c:GetFlagEffectLabel(id) == fid
  end
  function s.retcon(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetTurnPlayer() == tp then
  		return false
  	end
  	local g = e:GetLabelObject()
  	if not g:IsExists(s.retfilter, 1, nil, e:GetLabel()) then
  		g:DeleteGroup()
  		e:Reset()
  		return false
  	else
  		return true
  	end
  end
  function s.retop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local ct = c:GetTurnCounter()
  	ct = ct + 1
  	c:SetTurnCounter(ct)
  	if ct == 2 then
  		local g = e:GetLabelObject()
  		local sg = g:Filter(s.retfilter, nil, e:GetLabel())
  		g:DeleteGroup()
  		local tc = sg:GetFirst()
  		while tc do
  			Duel.ReturnToField(tc)
  			tc = sg:GetNext()
  		end
  	end
  end
  
  
source_text: Performapal
  At the start of your Battle Phase: You can place 1 counter on this card (max. 2). This card gains the following effect, based on the number of counters on it.
  ● 1: Once per turn, when an opponent's monster declares an attack: You can negate that attack.
  ● 2: You can Tribute this card; banish all monsters on the field, until your opponent's 2nd End Phase.
target_text: -- Performapal
  local COUNTER_ID_0 = 0x50 --Counter(Performapal
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableCounterPermit(0x50)
  	c:SetCounterLimit(0x50, 2)
  	--add counter
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_COUNTER)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_PHASE + PHASE_BATTLE_START)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCountLimit(1)
  	e1:SetCondition(s.ctcon)
  	e1:SetTarget(s.cttg)
  	e1:SetOperation(s.ctop)
  	c:RegisterEffect(e1)
  	--negate attack
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_ATTACK_ANNOUNCE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1)
  	e2:SetCondition(s.negcon)
  	e2:SetOperation(s.negop)
  	c:RegisterEffect(e2)
  	--remove
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 2))
  	e3:SetCategory(CATEGORY_REMOVE)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCondition(s.rmcon)
  	e3:SetCost(s.rmcost)
  	e3:SetTarget(s.rmtg)
  	e3:SetOperation(s.rmop)
  	c:RegisterEffect(e3)
  end
  function s.ctcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() == tp
  end
  function s.cttg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsCanAddCounter(COUNTER_ID_0, 1)
  	end
  end
  function s.ctop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) then
  		c:AddCounter(COUNTER_ID_0, 1)
  	end
  end
  function s.negcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetAttacker():GetControler() ~= tp and e:GetHandler():GetCounter(COUNTER_ID_0) == 1
  end
  function s.negop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.NegateAttack()
  end
  function s.rmcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():GetCounter(COUNTER_ID_0) == 2
  end
  function s.rmcost(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.rmtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(Card.IsAbleToRemove, tp, LOCATION_MZONE, LOCATION_MZONE, 1, e:GetHandler())
  	end
  	local g = Duel.GetMatchingGroup(Card.IsAbleToRemove, tp, LOCATION_MZONE, LOCATION_MZONE, nil)
  	Duel.SetOperationInfo(0, CATEGORY_REMOVE, g, g:GetCount(), 0, 0)
  end
  function s.rmop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(Card.IsAbleToRemove, tp, LOCATION_MZONE, LOCATION_MZONE, nil)
  	if Duel.Remove(g, 0, REASON_EFFECT + REASON_TEMPORARY) ~= 0 then
  		local c = e:GetHandler()
  		local og = Duel.GetOperatedGroup()
  		local fid = c:GetFieldID()
  		local tc = og:GetFirst()
  		while tc do
  			tc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END + RESET_OPPO_TURN, 0, 2, fid)
  			tc = og:GetNext()
  		end
  		c:SetTurnCounter(0)
  		og:KeepAlive()
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		e1:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  		e1:SetCode(EVENT_PHASE + PHASE_END)
  		e1:SetCountLimit(1)
  		e1:SetLabel(fid)
  		e1:SetLabelObject(og)
  		e1:SetCondition(s.retcon)
  		e1:SetOperation(s.retop)
  		e1:SetReset(RESET_PHASE + PHASE_END + RESET_OPPO_TURN, 2)
  		Duel.RegisterEffect(e1, tp)
  	end
  end
  function s.retfilter(c, fid)
  	return c:GetFlagEffectLabel(id) == fid
  end
  function s.retcon(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetTurnPlayer() == tp then
  		return false
  	end
  	local g = e:GetLabelObject()
  	if not g:IsExists(s.retfilter, 1, nil, e:GetLabel()) then
  		g:DeleteGroup()
  		e:Reset()
  		return false
  	else
  		return true
  	end
  end
  function s.retop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local ct = c:GetTurnCounter()
  	ct = ct + 1
  	c:SetTurnCounter(ct)
  	if ct == 2 then
  		local g = e:GetLabelObject()
  		local sg = g:Filter(s.retfilter, nil, e:GetLabel())
  		g:DeleteGroup()
  		local tc = sg:GetFirst()
  		while tc do
  			Duel.ReturnToField(tc)
  			tc = sg:GetNext()
  		end
  	end
  end
  
  