name: Archfiend
card_text: If exactly 1 face-up DARK Fiend monster on the field, except this card, would be destroyed, you can banish 1 DARK Fiend monster from your GY, instead. When this card is destroyed and sent from the field to the GY: You can target 1 Level 6 or higher DARK Fiend monster in your GY, except "Archfiend Empress"; Special Summon it.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--destroy replace
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_DESTROY_REPLACE)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetTarget(s.destg)
  	e1:SetValue(1)
  	c:RegisterEffect(e1)
  	--special summon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DAMAGE_STEP)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetCondition(s.spcon)
  	e2:SetTarget(s.sptg)
  	e2:SetOperation(s.spop)
  	c:RegisterEffect(e2)
  end
  function s.rfilter(c)
  	return c:IsRace(RACE_FIEND) and c:IsAttribute(ATTRIBUTE_DARK) and c:IsAbleToRemove()
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local dc = eg:GetFirst()
  	if chk == 0 then
  		return eg:GetCount() == 1
  			and dc ~= e:GetHandler()
  			and dc:IsFaceup()
  			and dc:IsLocation(LOCATION_MZONE)
  			and dc:IsRace(RACE_FIEND)
  			and dc:IsAttribute(ATTRIBUTE_DARK)
  			and not dc:IsReason(REASON_REPLACE)
  			and Duel.IsExistingMatchingCard(s.rfilter, tp, LOCATION_GRAVE, 0, 1, nil)
  	end
  	if Duel.SelectEffectYesNo(tp, e:GetHandler(), 96) then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  		local g = Duel.SelectMatchingCard(tp, s.rfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil)
  		Duel.Remove(g, POS_FACEUP, REASON_EFFECT)
  		return true
  	else
  		return false
  	end
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsReason(REASON_DESTROY) and e:GetHandler():IsPreviousLocation(LOCATION_ONFIELD)
  end
  function s.filter(c, e, tp)
  	return c:IsRace(RACE_FIEND) and c:IsAttribute(ATTRIBUTE_DARK) and c:IsLevelAbove(6) and not c:IsCode(id) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.filter(chkc, e, tp)
  	end
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingTarget(s.filter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectTarget(tp, s.filter, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  
source_text: Archfiend
  If exactly 1 face-up DARK Fiend monster on the field, except this card, would be destroyed, you can banish 1 DARK Fiend monster from your GY, instead. When this card is destroyed and sent from the field to the GY: You can target 1 Level 6 or higher DARK Fiend monster in your GY, except "Archfiend Empress"; Special Summon it.
target_text: -- Archfiend
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--destroy replace
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_DESTROY_REPLACE)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetTarget(s.destg)
  	e1:SetValue(1)
  	c:RegisterEffect(e1)
  	--special summon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DAMAGE_STEP)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetCondition(s.spcon)
  	e2:SetTarget(s.sptg)
  	e2:SetOperation(s.spop)
  	c:RegisterEffect(e2)
  end
  function s.rfilter(c)
  	return c:IsRace(RACE_FIEND) and c:IsAttribute(ATTRIBUTE_DARK) and c:IsAbleToRemove()
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local dc = eg:GetFirst()
  	if chk == 0 then
  		return eg:GetCount() == 1
  			and dc ~= e:GetHandler()
  			and dc:IsFaceup()
  			and dc:IsLocation(LOCATION_MZONE)
  			and dc:IsRace(RACE_FIEND)
  			and dc:IsAttribute(ATTRIBUTE_DARK)
  			and not dc:IsReason(REASON_REPLACE)
  			and Duel.IsExistingMatchingCard(s.rfilter, tp, LOCATION_GRAVE, 0, 1, nil)
  	end
  	if Duel.SelectEffectYesNo(tp, e:GetHandler(), 96) then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  		local g = Duel.SelectMatchingCard(tp, s.rfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil)
  		Duel.Remove(g, POS_FACEUP, REASON_EFFECT)
  		return true
  	else
  		return false
  	end
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsReason(REASON_DESTROY) and e:GetHandler():IsPreviousLocation(LOCATION_ONFIELD)
  end
  function s.filter(c, e, tp)
  	return c:IsRace(RACE_FIEND) and c:IsAttribute(ATTRIBUTE_DARK) and c:IsLevelAbove(6) and not c:IsCode(id) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.filter(chkc, e, tp)
  	end
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingTarget(s.filter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectTarget(tp, s.filter, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  