name: Fiendish
card_text: Target 1 monster on the field with 2000 or more ATK; banish it (until the End Phase of the next turn), then if you activated this card while you controlled "Red Dragon Archfiend" or a Synchro Monster that mentions it, you can Set 1 "Fiendish Chain" directly from your Deck or GY.
external_ids: local CARD_ID_0 = 70902743 --Red Dragon Archfiend
  local CARD_ID_1 = 50078509 --Fiendish Chain
  
lua: local CARD_ID_0 = 70902743 --Red Dragon Archfiend
  local CARD_ID_1 = 50078509 --Fiendish Chain
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_0)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_REMOVE)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.rmfilter(c)
  	return c:IsAttackAbove(2000) and c:IsFaceup() and c:IsAbleToRemove()
  end
  function s.cfilter(c)
  	return (c:IsCode(CARD_ID_0) or (aux.IsCodeListed(c, CARD_ID_0) and c:IsType(TYPE_SYNCHRO) and c:IsLocation(LOCATION_MZONE))) and c:IsFaceup()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and s.rmfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.rmfilter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g = Duel.SelectTarget(tp, s.rmfilter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_REMOVE, g, 1, 0, 0)
  	local ct = Duel.GetMatchingGroupCount(s.cfilter, tp, LOCATION_ONFIELD, 0, nil)
  	e:SetLabel(ct)
  end
  function s.stfilter(c)
  	return c:IsCode(CARD_ID_1) and c:IsSSetable()
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and tc:IsType(TYPE_MONSTER) and Duel.Remove(tc, 0, REASON_EFFECT + REASON_TEMPORARY) ~= 0 then
  		tc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END, 0, 2)
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		e1:SetCode(EVENT_PHASE + PHASE_END)
  		e1:SetReset(RESET_PHASE + PHASE_END, 2)
  		e1:SetLabelObject(tc)
  		e1:SetCountLimit(1)
  		e1:SetCondition(s.retcon)
  		e1:SetOperation(s.retop)
  		e1:SetLabel(Duel.GetTurnCount())
  		Duel.RegisterEffect(e1, tp)
  		local g = Duel.GetMatchingGroup(aux.NecroValleyFilter(s.stfilter), tp, LOCATION_DECK + LOCATION_GRAVE, 0, nil)
  		if e:GetLabel() > 0 and g:GetCount() > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 0)) then
  			Duel.BreakEffect()
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SET)
  			local sc = g:Select(tp, 1, 1, nil)
  			Duel.SSet(tp, sc)
  		end
  	end
  end
  function s.retcon(e, tp, eg, ep, ev, re, r, rp)
  	local tc = e:GetLabelObject()
  	return Duel.GetTurnCount() ~= e:GetLabel() and tc:GetFlagEffect(id) ~= 0
  end
  function s.retop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.ReturnToField(e:GetLabelObject())
  end
  
  
source_text: Fiendish
  Target 1 monster on the field with 2000 or more ATK; banish it (until the End Phase of the next turn), then if you activated this card while you controlled "Red Dragon Archfiend" or a Synchro Monster that mentions it, you can Set 1 "Fiendish Chain" directly from your Deck or GY.
target_text: -- Fiendish
  local CARD_ID_0 = 70902743 --Red Dragon Archfiend
  local CARD_ID_1 = 50078509 --Fiendish Chain
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_0)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_REMOVE)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.rmfilter(c)
  	return c:IsAttackAbove(2000) and c:IsFaceup() and c:IsAbleToRemove()
  end
  function s.cfilter(c)
  	return (c:IsCode(CARD_ID_0) or (aux.IsCodeListed(c, CARD_ID_0) and c:IsType(TYPE_SYNCHRO) and c:IsLocation(LOCATION_MZONE))) and c:IsFaceup()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and s.rmfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.rmfilter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g = Duel.SelectTarget(tp, s.rmfilter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_REMOVE, g, 1, 0, 0)
  	local ct = Duel.GetMatchingGroupCount(s.cfilter, tp, LOCATION_ONFIELD, 0, nil)
  	e:SetLabel(ct)
  end
  function s.stfilter(c)
  	return c:IsCode(CARD_ID_1) and c:IsSSetable()
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and tc:IsType(TYPE_MONSTER) and Duel.Remove(tc, 0, REASON_EFFECT + REASON_TEMPORARY) ~= 0 then
  		tc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END, 0, 2)
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		e1:SetCode(EVENT_PHASE + PHASE_END)
  		e1:SetReset(RESET_PHASE + PHASE_END, 2)
  		e1:SetLabelObject(tc)
  		e1:SetCountLimit(1)
  		e1:SetCondition(s.retcon)
  		e1:SetOperation(s.retop)
  		e1:SetLabel(Duel.GetTurnCount())
  		Duel.RegisterEffect(e1, tp)
  		local g = Duel.GetMatchingGroup(aux.NecroValleyFilter(s.stfilter), tp, LOCATION_DECK + LOCATION_GRAVE, 0, nil)
  		if e:GetLabel() > 0 and g:GetCount() > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 0)) then
  			Duel.BreakEffect()
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SET)
  			local sc = g:Select(tp, 1, 1, nil)
  			Duel.SSet(tp, sc)
  		end
  	end
  end
  function s.retcon(e, tp, eg, ep, ev, re, r, rp)
  	local tc = e:GetLabelObject()
  	return Duel.GetTurnCount() ~= e:GetLabel() and tc:GetFlagEffect(id) ~= 0
  end
  function s.retop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.ReturnToField(e:GetLabelObject())
  end
  
  