name: Forge
card_text: Once per Chain, if a Link-4 monster is Link Summoned: You can place 1 counter on this card (max. 3), then you can apply the following effect based on the number of counters on it.
  ● 1: Declare 1 card name, and if you do, the activated effects and effects on the field of cards with that original name, are negated this turn.
  ● 2: Special Summon 1 Level 4 or lower monster from your GY.
  ● 3: Send this card to the GY, and if you do, Special Summon 1 Link-4 monster from your Extra Deck.
  You can only activate 1 "Forge a New Future" per turn.
external_ids: local COUNTER_ID_0 = 0x61 --Counter(Prisoner
  
lua: local COUNTER_ID_0 = 0x61 --Counter(Prisoner
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableCounterPermit(0x61)
  	c:SetCounterLimit(0x61, 3)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	c:RegisterEffect(e1)
  	--counter
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_COUNTER + CATEGORY_SPECIAL_SUMMON + CATEGORY_TOGRAVE)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCondition(s.condition)
  	e2:SetTarget(s.target)
  	e2:SetOperation(s.operation)
  	c:RegisterEffect(e2)
  end
  function s.cfilter(c, tp)
  	return c:IsLink(4) and c:IsSummonType(SUMMON_TYPE_LINK) and c:IsFaceup()
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp)
  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())
  	end
  	Duel.SetOperationInfo(0, CATEGORY_COUNTER, nil, 1, 0, 0x61)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) then
  		c:AddCounter(COUNTER_ID_0, 1)
  		local ct = c:GetCounter(COUNTER_ID_0)
  		if ct == 1 and Duel.SelectYesNo(tp, aux.Stringid(id, 0)) then
  			Duel.BreakEffect()
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_CODE)
  			local ac = Duel.AnnounceCard(tp)
  			local e1 = Effect.CreateEffect(c)
  			e1:SetType(EFFECT_TYPE_FIELD)
  			e1:SetCode(EFFECT_DISABLE)
  			e1:SetTargetRange(LOCATION_ONFIELD, LOCATION_ONFIELD)
  			e1:SetTarget(s.distg1)
  			e1:SetLabel(ac)
  			e1:SetReset(RESET_PHASE + PHASE_END)
  			Duel.RegisterEffect(e1, tp)
  			local e2 = Effect.CreateEffect(c)
  			e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  			e2:SetCode(EVENT_CHAIN_SOLVING)
  			e2:SetCondition(s.discon)
  			e2:SetOperation(s.disop)
  			e2:SetLabel(ac)
  			e2:SetReset(RESET_PHASE + PHASE_END)
  			Duel.RegisterEffect(e2, tp)
  			local e3 = Effect.CreateEffect(c)
  			e3:SetType(EFFECT_TYPE_FIELD)
  			e3:SetCode(EFFECT_DISABLE_TRAPMONSTER)
  			e3:SetTargetRange(LOCATION_MZONE, LOCATION_MZONE)
  			e3:SetTarget(s.distg2)
  			e3:SetLabel(ac)
  			e3:SetReset(RESET_PHASE + PHASE_END)
  			Duel.RegisterEffect(e3, tp)
  		end
  		local g1 = Duel.GetMatchingGroup(aux.NecroValleyFilter(s.spfilter1), tp, LOCATION_GRAVE, 0, nil, e, tp)
  		if ct == 2 and g1:GetCount() > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 1)) then
  			Duel.BreakEffect()
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  			local sg1 = g1:Select(tp, 1, 1, nil)
  			Duel.SpecialSummon(sg1, 0, tp, tp, false, false, POS_FACEUP)
  		end
  		local g2 = Duel.GetMatchingGroup(s.spfilter2, tp, LOCATION_EXTRA, 0, nil, e, tp)
  		if ct == 3 and g2:GetCount() > 0 and c:IsAbleToGrave() and Duel.SelectYesNo(tp, aux.Stringid(id, 2)) then
  			Duel.BreakEffect()
  			if Duel.SendtoGrave(c, REASON_EFFECT) > 0 and c:IsLocation(LOCATION_GRAVE) then
  				Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  				local sg2 = g2:Select(tp, 1, 1, nil)
  				Duel.SpecialSummon(sg2, 0, tp, tp, false, false, POS_FACEUP)
  			end
  		end
  	end
  end
  function s.distg1(e, c)
  	local ac = e:GetLabel()
  	if c:IsType(TYPE_SPELL + TYPE_TRAP) then
  		return c:IsOriginalCodeRule(ac)
  	else
  		return c:IsOriginalCodeRule(ac) and (c:IsType(TYPE_EFFECT) or c:GetOriginalType() & TYPE_EFFECT ~= 0)
  	end
  end
  function s.distg2(e, c)
  	local ac = e:GetLabel()
  	return c:IsOriginalCodeRule(ac)
  end
  function s.discon(e, tp, eg, ep, ev, re, r, rp)
  	local ac = e:GetLabel()
  	return re:GetHandler():IsOriginalCodeRule(ac)
  end
  function s.disop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.NegateEffect(ev)
  end
  function s.spfilter1(c, e, tp)
  	return c:IsLevelBelow(4) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.spfilter2(c, e, tp)
  	return c:IsLink(4) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  
  
source_text: Forge
  Once per Chain, if a Link-4 monster is Link Summoned: You can place 1 counter on this card (max. 3), then you can apply the following effect based on the number of counters on it.
  ● 1: Declare 1 card name, and if you do, the activated effects and effects on the field of cards with that original name, are negated this turn.
  ● 2: Special Summon 1 Level 4 or lower monster from your GY.
  ● 3: Send this card to the GY, and if you do, Special Summon 1 Link-4 monster from your Extra Deck.
  You can only activate 1 "Forge a New Future" per turn.
target_text: -- Forge
  local COUNTER_ID_0 = 0x61 --Counter(Prisoner
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableCounterPermit(0x61)
  	c:SetCounterLimit(0x61, 3)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	c:RegisterEffect(e1)
  	--counter
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_COUNTER + CATEGORY_SPECIAL_SUMMON + CATEGORY_TOGRAVE)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCondition(s.condition)
  	e2:SetTarget(s.target)
  	e2:SetOperation(s.operation)
  	c:RegisterEffect(e2)
  end
  function s.cfilter(c, tp)
  	return c:IsLink(4) and c:IsSummonType(SUMMON_TYPE_LINK) and c:IsFaceup()
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp)
  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())
  	end
  	Duel.SetOperationInfo(0, CATEGORY_COUNTER, nil, 1, 0, 0x61)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) then
  		c:AddCounter(COUNTER_ID_0, 1)
  		local ct = c:GetCounter(COUNTER_ID_0)
  		if ct == 1 and Duel.SelectYesNo(tp, aux.Stringid(id, 0)) then
  			Duel.BreakEffect()
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_CODE)
  			local ac = Duel.AnnounceCard(tp)
  			local e1 = Effect.CreateEffect(c)
  			e1:SetType(EFFECT_TYPE_FIELD)
  			e1:SetCode(EFFECT_DISABLE)
  			e1:SetTargetRange(LOCATION_ONFIELD, LOCATION_ONFIELD)
  			e1:SetTarget(s.distg1)
  			e1:SetLabel(ac)
  			e1:SetReset(RESET_PHASE + PHASE_END)
  			Duel.RegisterEffect(e1, tp)
  			local e2 = Effect.CreateEffect(c)
  			e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  			e2:SetCode(EVENT_CHAIN_SOLVING)
  			e2:SetCondition(s.discon)
  			e2:SetOperation(s.disop)
  			e2:SetLabel(ac)
  			e2:SetReset(RESET_PHASE + PHASE_END)
  			Duel.RegisterEffect(e2, tp)
  			local e3 = Effect.CreateEffect(c)
  			e3:SetType(EFFECT_TYPE_FIELD)
  			e3:SetCode(EFFECT_DISABLE_TRAPMONSTER)
  			e3:SetTargetRange(LOCATION_MZONE, LOCATION_MZONE)
  			e3:SetTarget(s.distg2)
  			e3:SetLabel(ac)
  			e3:SetReset(RESET_PHASE + PHASE_END)
  			Duel.RegisterEffect(e3, tp)
  		end
  		local g1 = Duel.GetMatchingGroup(aux.NecroValleyFilter(s.spfilter1), tp, LOCATION_GRAVE, 0, nil, e, tp)
  		if ct == 2 and g1:GetCount() > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 1)) then
  			Duel.BreakEffect()
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  			local sg1 = g1:Select(tp, 1, 1, nil)
  			Duel.SpecialSummon(sg1, 0, tp, tp, false, false, POS_FACEUP)
  		end
  		local g2 = Duel.GetMatchingGroup(s.spfilter2, tp, LOCATION_EXTRA, 0, nil, e, tp)
  		if ct == 3 and g2:GetCount() > 0 and c:IsAbleToGrave() and Duel.SelectYesNo(tp, aux.Stringid(id, 2)) then
  			Duel.BreakEffect()
  			if Duel.SendtoGrave(c, REASON_EFFECT) > 0 and c:IsLocation(LOCATION_GRAVE) then
  				Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  				local sg2 = g2:Select(tp, 1, 1, nil)
  				Duel.SpecialSummon(sg2, 0, tp, tp, false, false, POS_FACEUP)
  			end
  		end
  	end
  end
  function s.distg1(e, c)
  	local ac = e:GetLabel()
  	if c:IsType(TYPE_SPELL + TYPE_TRAP) then
  		return c:IsOriginalCodeRule(ac)
  	else
  		return c:IsOriginalCodeRule(ac) and (c:IsType(TYPE_EFFECT) or c:GetOriginalType() & TYPE_EFFECT ~= 0)
  	end
  end
  function s.distg2(e, c)
  	local ac = e:GetLabel()
  	return c:IsOriginalCodeRule(ac)
  end
  function s.discon(e, tp, eg, ep, ev, re, r, rp)
  	local ac = e:GetLabel()
  	return re:GetHandler():IsOriginalCodeRule(ac)
  end
  function s.disop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.NegateEffect(ev)
  end
  function s.spfilter1(c, e, tp)
  	return c:IsLevelBelow(4) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.spfilter2(c, e, tp)
  	return c:IsLink(4) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  
  