name: Linkmail
card_text: 2+ monsters, including a Ritual, Fusion, Synchro, or Xyz Monster
  If this card is Special Summoned: You can target 1 Ritual, Fusion, Synchro, or Xyz Monster you control or in your GY; all monsters your opponent currently controls lose ATK equal to the targeted monster's ATK, until the end of this turn. Your opponent cannot target monsters you control, that were Special Summoned from the Extra Deck, with monster effects. If this card would be destroyed by battle or card effect, you can banish 1 Ritual, Fusion, Synchro, or Xyz Monster from your GY instead.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--link summon
  	aux.AddLinkProcedure(c, nil, 2, nil, s.lcheck)
  	c:EnableReviveLimit()
  	--atkdown
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_ATKCHANGE)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DELAY)
  	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e1:SetTarget(s.atktg)
  	e1:SetOperation(s.atkop)
  	c:RegisterEffect(e1)
  	--cannot be target
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_CANNOT_BE_EFFECT_TARGET)
  	e2:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetTargetRange(LOCATION_MZONE, 0)
  	e2:SetTarget(s.tgtg)
  	e2:SetValue(s.tgval)
  	c:RegisterEffect(e2)
  	--destroy replace
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_SINGLE)
  	e3:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e3:SetCode(EFFECT_DESTROY_REPLACE)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetTarget(s.reptg)
  	c:RegisterEffect(e3)
  end
  function s.lcheck(g)
  	return g:IsExists(Card.IsLinkType, 1, nil, TYPE_RITUAL + TYPE_FUSION + TYPE_SYNCHRO + TYPE_XYZ)
  end
  function s.atkfilter(c)
  	return c:GetAttack() > 0 and (c:IsLocation(LOCATION_GRAVE) or c:IsFaceup()) and c:IsType(TYPE_RITUAL + TYPE_FUSION + TYPE_SYNCHRO + TYPE_XYZ)
  end
  function s.atktg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE + LOCATION_GRAVE) and chkc:IsControler(tp) and s.atkfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.atkfilter, tp, LOCATION_MZONE + LOCATION_GRAVE, 0, 1, nil) and Duel.IsExistingMatchingCard(Card.IsFaceup, tp, 0, LOCATION_MZONE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.atkfilter, tp, LOCATION_MZONE + LOCATION_GRAVE, 0, 1, 1, nil)
  end
  function s.atkop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if not tc:IsRelateToEffect(e) or (tc:IsLocation(LOCATION_MZONE) and tc:IsFacedown()) then
  		return
  	end
  	local atk = tc:GetAttack()
  	local g = Duel.GetMatchingGroup(Card.IsFaceup, tp, 0, LOCATION_MZONE, nil)
  	local sc = g:GetFirst()
  	while sc do
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_UPDATE_ATTACK)
  		e1:SetValue(-atk)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  		sc:RegisterEffect(e1)
  		sc = g:GetNext()
  	end
  end
  function s.tgtg(e, c)
  	return c:IsSummonLocation(LOCATION_EXTRA)
  end
  function s.tgval(e, re, rp)
  	return rp == 1 - e:GetHandlerPlayer() and re:IsActiveType(TYPE_MONSTER)
  end
  function s.repfilter(c)
  	return c:IsType(TYPE_MONSTER) and c:IsType(TYPE_RITUAL + TYPE_FUSION + TYPE_SYNCHRO + TYPE_XYZ) and c:IsAbleToRemove()
  end
  function s.reptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return c:IsReason(REASON_BATTLE + REASON_EFFECT) and not c:IsReason(REASON_REPLACE) and Duel.IsExistingMatchingCard(s.repfilter, tp, LOCATION_GRAVE, 0, 1, nil)
  	end
  	if Duel.SelectEffectYesNo(tp, c, 96) then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESREPLACE)
  		local g = Duel.SelectMatchingCard(tp, s.repfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil)
  		Duel.Remove(g, POS_FACEUP, REASON_EFFECT + REASON_REPLACE)
  		return true
  	else
  		return false
  	end
  end
  
  
source_text: Linkmail
  2+ monsters, including a Ritual, Fusion, Synchro, or Xyz Monster
  If this card is Special Summoned: You can target 1 Ritual, Fusion, Synchro, or Xyz Monster you control or in your GY; all monsters your opponent currently controls lose ATK equal to the targeted monster's ATK, until the end of this turn. Your opponent cannot target monsters you control, that were Special Summoned from the Extra Deck, with monster effects. If this card would be destroyed by battle or card effect, you can banish 1 Ritual, Fusion, Synchro, or Xyz Monster from your GY instead.
target_text: -- Linkmail
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--link summon
  	aux.AddLinkProcedure(c, nil, 2, nil, s.lcheck)
  	c:EnableReviveLimit()
  	--atkdown
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_ATKCHANGE)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DELAY)
  	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e1:SetTarget(s.atktg)
  	e1:SetOperation(s.atkop)
  	c:RegisterEffect(e1)
  	--cannot be target
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_CANNOT_BE_EFFECT_TARGET)
  	e2:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetTargetRange(LOCATION_MZONE, 0)
  	e2:SetTarget(s.tgtg)
  	e2:SetValue(s.tgval)
  	c:RegisterEffect(e2)
  	--destroy replace
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_SINGLE)
  	e3:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e3:SetCode(EFFECT_DESTROY_REPLACE)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetTarget(s.reptg)
  	c:RegisterEffect(e3)
  end
  function s.lcheck(g)
  	return g:IsExists(Card.IsLinkType, 1, nil, TYPE_RITUAL + TYPE_FUSION + TYPE_SYNCHRO + TYPE_XYZ)
  end
  function s.atkfilter(c)
  	return c:GetAttack() > 0 and (c:IsLocation(LOCATION_GRAVE) or c:IsFaceup()) and c:IsType(TYPE_RITUAL + TYPE_FUSION + TYPE_SYNCHRO + TYPE_XYZ)
  end
  function s.atktg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE + LOCATION_GRAVE) and chkc:IsControler(tp) and s.atkfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.atkfilter, tp, LOCATION_MZONE + LOCATION_GRAVE, 0, 1, nil) and Duel.IsExistingMatchingCard(Card.IsFaceup, tp, 0, LOCATION_MZONE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.atkfilter, tp, LOCATION_MZONE + LOCATION_GRAVE, 0, 1, 1, nil)
  end
  function s.atkop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if not tc:IsRelateToEffect(e) or (tc:IsLocation(LOCATION_MZONE) and tc:IsFacedown()) then
  		return
  	end
  	local atk = tc:GetAttack()
  	local g = Duel.GetMatchingGroup(Card.IsFaceup, tp, 0, LOCATION_MZONE, nil)
  	local sc = g:GetFirst()
  	while sc do
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_UPDATE_ATTACK)
  		e1:SetValue(-atk)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  		sc:RegisterEffect(e1)
  		sc = g:GetNext()
  	end
  end
  function s.tgtg(e, c)
  	return c:IsSummonLocation(LOCATION_EXTRA)
  end
  function s.tgval(e, re, rp)
  	return rp == 1 - e:GetHandlerPlayer() and re:IsActiveType(TYPE_MONSTER)
  end
  function s.repfilter(c)
  	return c:IsType(TYPE_MONSTER) and c:IsType(TYPE_RITUAL + TYPE_FUSION + TYPE_SYNCHRO + TYPE_XYZ) and c:IsAbleToRemove()
  end
  function s.reptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return c:IsReason(REASON_BATTLE + REASON_EFFECT) and not c:IsReason(REASON_REPLACE) and Duel.IsExistingMatchingCard(s.repfilter, tp, LOCATION_GRAVE, 0, 1, nil)
  	end
  	if Duel.SelectEffectYesNo(tp, c, 96) then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESREPLACE)
  		local g = Duel.SelectMatchingCard(tp, s.repfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil)
  		Duel.Remove(g, POS_FACEUP, REASON_EFFECT + REASON_REPLACE)
  		return true
  	else
  		return false
  	end
  end
  
  