name: Magic
card_text: Select 1 Spell Card that remains face-up on the field and put 1 counter on it. If the selected card would be destroyed, the counter is removed instead.
external_ids: local COUNTER_ID_0 = 0x102a --Counter(Magic
  
lua: local COUNTER_ID_0 = 0x102a --Counter(Magic
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--destroy rep
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_COUNTER)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetTarget(s.addct)
  	e1:SetOperation(s.addc)
  	c:RegisterEffect(e1)
  end
  function s.filter(c)
  	return c:IsFaceup() and c:IsType(TYPE_SPELL) and c:IsCanAddCounter(COUNTER_ID_0, 1)
  end
  function s.addct(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_SZONE) and chkc:IsControler(tp) and s.filter(chkc) and chkc ~= e:GetHandler()
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.filter, tp, LOCATION_SZONE, 0, 1, e:GetHandler())
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, aux.Stringid(id, 0))
  	Duel.SelectTarget(tp, s.filter, tp, LOCATION_SZONE, 0, 1, 1, e:GetHandler())
  end
  function s.addc(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsFaceup() and tc:IsRelateToEffect(e) then
  		tc:AddCounter(COUNTER_ID_0, 1)
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_CONTINUOUS)
  		e1:SetCode(EFFECT_DESTROY_REPLACE)
  		e1:SetTarget(s.reptg)
  		e1:SetOperation(s.repop)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  		tc:RegisterEffect(e1)
  	end
  end
  function s.reptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return bit.band(r, REASON_RULE) == 0 and e:GetHandler():GetCounter(COUNTER_ID_0) > 0
  	end
  	return true
  end
  function s.repop(e, tp, eg, ep, ev, re, r, rp)
  	e:GetHandler():RemoveCounter(tp, COUNTER_ID_0, 1, REASON_EFFECT)
  end
  
  
source_text: Magic
  Select 1 Spell Card that remains face-up on the field and put 1 counter on it. If the selected card would be destroyed, the counter is removed instead.
target_text: -- Magic
  local COUNTER_ID_0 = 0x102a --Counter(Magic
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--destroy rep
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_COUNTER)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetTarget(s.addct)
  	e1:SetOperation(s.addc)
  	c:RegisterEffect(e1)
  end
  function s.filter(c)
  	return c:IsFaceup() and c:IsType(TYPE_SPELL) and c:IsCanAddCounter(COUNTER_ID_0, 1)
  end
  function s.addct(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_SZONE) and chkc:IsControler(tp) and s.filter(chkc) and chkc ~= e:GetHandler()
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.filter, tp, LOCATION_SZONE, 0, 1, e:GetHandler())
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, aux.Stringid(id, 0))
  	Duel.SelectTarget(tp, s.filter, tp, LOCATION_SZONE, 0, 1, 1, e:GetHandler())
  end
  function s.addc(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsFaceup() and tc:IsRelateToEffect(e) then
  		tc:AddCounter(COUNTER_ID_0, 1)
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_CONTINUOUS)
  		e1:SetCode(EFFECT_DESTROY_REPLACE)
  		e1:SetTarget(s.reptg)
  		e1:SetOperation(s.repop)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  		tc:RegisterEffect(e1)
  	end
  end
  function s.reptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return bit.band(r, REASON_RULE) == 0 and e:GetHandler():GetCounter(COUNTER_ID_0) > 0
  	end
  	return true
  end
  function s.repop(e, tp, eg, ep, ev, re, r, rp)
  	e:GetHandler():RemoveCounter(tp, COUNTER_ID_0, 1, REASON_EFFECT)
  end
  
  