name: Fiendish
card_text: Target 1 face-up monster your opponent controls; Special Summon 1 monster with that name from your Deck or Extra Deck, but shuffle it into the Deck during the End Phase of the next turn, also you cannot Special Summon monsters from the Deck or Extra Deck for the rest of this turn after this card resolves. You can only activate 1 "Fiendish Portrait" per turn.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	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:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.cfilter(c, e, tp)
  	return c:IsFaceup() and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_DECK + LOCATION_EXTRA, 0, 1, nil, e, tp, c:GetCode())
  end
  function s.spfilter(c, e, tp, code)
  	return c:IsCode(code)
  		and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  		and (c:IsLocation(LOCATION_DECK) and Duel.GetMZoneCount(tp) > 0 or c:IsLocation(LOCATION_EXTRA) and Duel.GetLocationCountFromEx(tp, tp, nil, c) > 0)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(1 - tp) and s.cfilter(chkc, e, tp)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.cfilter, tp, 0, LOCATION_MZONE, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_FACEUP)
  	local g = Duel.SelectTarget(tp, s.cfilter, tp, 0, LOCATION_MZONE, 1, 1, nil, e, tp)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_DECK + LOCATION_EXTRA)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsFaceup() and tc:IsRelateToEffect(e) then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  		local g = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_DECK + LOCATION_EXTRA, 0, 1, 1, nil, e, tp, tc:GetCode())
  		local sc = g:GetFirst()
  		if sc and Duel.SpecialSummonStep(sc, 0, tp, tp, false, false, POS_FACEUP) then
  			sc: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:SetCountLimit(1)
  			e1:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  			e1:SetLabel(Duel.GetTurnCount())
  			e1:SetLabelObject(sc)
  			e1:SetCondition(s.tdcon)
  			e1:SetOperation(s.tdop)
  			e1:SetReset(RESET_PHASE + PHASE_END, 2)
  			Duel.RegisterEffect(e1, tp)
  		end
  		Duel.SpecialSummonComplete()
  	end
  	if e:IsHasType(EFFECT_TYPE_ACTIVATE) then
  		local e2 = Effect.CreateEffect(e:GetHandler())
  		e2:SetType(EFFECT_TYPE_FIELD)
  		e2:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
  		e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  		e2:SetTargetRange(1, 0)
  		e2:SetTarget(s.splimit)
  		e2:SetReset(RESET_PHASE + PHASE_END)
  		Duel.RegisterEffect(e2, tp)
  	end
  end
  function s.tdcon(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.tdop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = e:GetLabelObject()
  	Duel.SendtoDeck(tc, nil, SEQ_DECKSHUFFLE, REASON_EFFECT)
  end
  function s.splimit(e, c)
  	return c:IsLocation(LOCATION_DECK) or c:IsLocation(LOCATION_EXTRA)
  end
  
  
source_text: Fiendish
  Target 1 face-up monster your opponent controls; Special Summon 1 monster with that name from your Deck or Extra Deck, but shuffle it into the Deck during the End Phase of the next turn, also you cannot Special Summon monsters from the Deck or Extra Deck for the rest of this turn after this card resolves. You can only activate 1 "Fiendish Portrait" per turn.
target_text: -- Fiendish
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	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:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.cfilter(c, e, tp)
  	return c:IsFaceup() and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_DECK + LOCATION_EXTRA, 0, 1, nil, e, tp, c:GetCode())
  end
  function s.spfilter(c, e, tp, code)
  	return c:IsCode(code)
  		and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  		and (c:IsLocation(LOCATION_DECK) and Duel.GetMZoneCount(tp) > 0 or c:IsLocation(LOCATION_EXTRA) and Duel.GetLocationCountFromEx(tp, tp, nil, c) > 0)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(1 - tp) and s.cfilter(chkc, e, tp)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.cfilter, tp, 0, LOCATION_MZONE, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_FACEUP)
  	local g = Duel.SelectTarget(tp, s.cfilter, tp, 0, LOCATION_MZONE, 1, 1, nil, e, tp)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_DECK + LOCATION_EXTRA)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsFaceup() and tc:IsRelateToEffect(e) then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  		local g = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_DECK + LOCATION_EXTRA, 0, 1, 1, nil, e, tp, tc:GetCode())
  		local sc = g:GetFirst()
  		if sc and Duel.SpecialSummonStep(sc, 0, tp, tp, false, false, POS_FACEUP) then
  			sc: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:SetCountLimit(1)
  			e1:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  			e1:SetLabel(Duel.GetTurnCount())
  			e1:SetLabelObject(sc)
  			e1:SetCondition(s.tdcon)
  			e1:SetOperation(s.tdop)
  			e1:SetReset(RESET_PHASE + PHASE_END, 2)
  			Duel.RegisterEffect(e1, tp)
  		end
  		Duel.SpecialSummonComplete()
  	end
  	if e:IsHasType(EFFECT_TYPE_ACTIVATE) then
  		local e2 = Effect.CreateEffect(e:GetHandler())
  		e2:SetType(EFFECT_TYPE_FIELD)
  		e2:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
  		e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  		e2:SetTargetRange(1, 0)
  		e2:SetTarget(s.splimit)
  		e2:SetReset(RESET_PHASE + PHASE_END)
  		Duel.RegisterEffect(e2, tp)
  	end
  end
  function s.tdcon(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.tdop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = e:GetLabelObject()
  	Duel.SendtoDeck(tc, nil, SEQ_DECKSHUFFLE, REASON_EFFECT)
  end
  function s.splimit(e, c)
  	return c:IsLocation(LOCATION_DECK) or c:IsLocation(LOCATION_EXTRA)
  end
  
  