name: Knightmare
card_text: When this card is Normal Summoned: You can target 1 Link Monster in your GY; Special Summon it to your field so it points to this card, but change its ATK to 0, also negate its effects. You cannot Special Summon monsters, except Link Monsters. If this card in its owner's control is sent to the GY: You can Special Summon this card to your opponent's field in Defense Position. You can only use this effect of "Knightmare Corruptor Iblee" once per turn.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon link
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_SUMMON_SUCCESS)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DELAY)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--splimit
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
  	e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetTargetRange(1, 0)
  	e2:SetTarget(s.splimit)
  	c:RegisterEffect(e2)
  	--spsummon opp
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e3:SetCode(EVENT_TO_GRAVE)
  	e3:SetProperty(EFFECT_FLAG_DELAY)
  	e3:SetCountLimit(1, id)
  	e3:SetCondition(s.condition)
  	e3:SetTarget(s.target)
  	e3:SetOperation(s.operation)
  	c:RegisterEffect(e3)
  	--cant spsummon from main deck check
  	local e4 = Effect.CreateEffect(c)
  	e4:SetType(EFFECT_TYPE_FIELD)
  	e4:SetCode(63060238)
  	e4:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e4:SetRange(LOCATION_MZONE)
  	e4:SetTargetRange(1, 0)
  	c:RegisterEffect(e4)
  end
  function s.get_zone(c, seq)
  	local zone = 0
  	if seq < 4 and c:IsLinkMarker(LINK_MARKER_LEFT) then
  		zone = bit.replace(zone, 0x1, seq + 1)
  	end
  	if seq > 0 and seq < 5 and c:IsLinkMarker(LINK_MARKER_RIGHT) then
  		zone = bit.replace(zone, 0x1, seq - 1)
  	end
  	return zone
  end
  function s.spfilter(c, e, tp, seq)
  	local zone = s.get_zone(c, seq)
  	return zone ~= 0 and c:IsType(TYPE_LINK) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP, tp, zone)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local seq = e:GetHandler():GetSequence()
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.spfilter(chkc, e, tp, seq)
  	end
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingTarget(s.spfilter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp, seq)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectTarget(tp, s.spfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp, seq)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if c:IsRelateToEffect(e) and c:IsControler(tp) and tc:IsRelateToEffect(e) then
  		local zone = s.get_zone(tc, c:GetSequence())
  		if zone ~= 0 and Duel.SpecialSummonStep(tc, 0, tp, tp, false, false, POS_FACEUP, zone) then
  			local e1 = Effect.CreateEffect(c)
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_DISABLE)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  			tc:RegisterEffect(e1)
  			local e2 = Effect.CreateEffect(c)
  			e2:SetType(EFFECT_TYPE_SINGLE)
  			e2:SetCode(EFFECT_DISABLE_EFFECT)
  			e2:SetValue(RESET_TURN_SET)
  			e2:SetReset(RESET_EVENT + RESETS_STANDARD)
  			tc:RegisterEffect(e2)
  			local e3 = Effect.CreateEffect(c)
  			e3:SetType(EFFECT_TYPE_SINGLE)
  			e3:SetCode(EFFECT_SET_ATTACK)
  			e3:SetValue(0)
  			e3:SetReset(RESET_EVENT + RESETS_STANDARD)
  			tc:RegisterEffect(e3)
  		end
  		Duel.SpecialSummonComplete()
  	end
  end
  function s.splimit(e, c, sump, sumtype, sumpos, targetp)
  	return c:GetOriginalType() & TYPE_LINK ~= TYPE_LINK
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsPreviousLocation(LOCATION_ONFIELD) and e:GetHandler():IsPreviousControler(tp)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return Duel.GetLocationCount(1 - tp, LOCATION_MZONE) > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP_DEFENSE, 1 - tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, c, 1, 0, 0)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetHandler():IsRelateToEffect(e) then
  		Duel.SpecialSummon(e:GetHandler(), 0, tp, 1 - tp, false, false, POS_FACEUP_DEFENSE)
  	end
  end
  
  
source_text: Knightmare
  When this card is Normal Summoned: You can target 1 Link Monster in your GY; Special Summon it to your field so it points to this card, but change its ATK to 0, also negate its effects. You cannot Special Summon monsters, except Link Monsters. If this card in its owner's control is sent to the GY: You can Special Summon this card to your opponent's field in Defense Position. You can only use this effect of "Knightmare Corruptor Iblee" once per turn.
target_text: -- Knightmare
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon link
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_SUMMON_SUCCESS)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DELAY)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--splimit
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
  	e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetTargetRange(1, 0)
  	e2:SetTarget(s.splimit)
  	c:RegisterEffect(e2)
  	--spsummon opp
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e3:SetCode(EVENT_TO_GRAVE)
  	e3:SetProperty(EFFECT_FLAG_DELAY)
  	e3:SetCountLimit(1, id)
  	e3:SetCondition(s.condition)
  	e3:SetTarget(s.target)
  	e3:SetOperation(s.operation)
  	c:RegisterEffect(e3)
  	--cant spsummon from main deck check
  	local e4 = Effect.CreateEffect(c)
  	e4:SetType(EFFECT_TYPE_FIELD)
  	e4:SetCode(63060238)
  	e4:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e4:SetRange(LOCATION_MZONE)
  	e4:SetTargetRange(1, 0)
  	c:RegisterEffect(e4)
  end
  function s.get_zone(c, seq)
  	local zone = 0
  	if seq < 4 and c:IsLinkMarker(LINK_MARKER_LEFT) then
  		zone = bit.replace(zone, 0x1, seq + 1)
  	end
  	if seq > 0 and seq < 5 and c:IsLinkMarker(LINK_MARKER_RIGHT) then
  		zone = bit.replace(zone, 0x1, seq - 1)
  	end
  	return zone
  end
  function s.spfilter(c, e, tp, seq)
  	local zone = s.get_zone(c, seq)
  	return zone ~= 0 and c:IsType(TYPE_LINK) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP, tp, zone)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local seq = e:GetHandler():GetSequence()
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.spfilter(chkc, e, tp, seq)
  	end
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingTarget(s.spfilter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp, seq)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectTarget(tp, s.spfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp, seq)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if c:IsRelateToEffect(e) and c:IsControler(tp) and tc:IsRelateToEffect(e) then
  		local zone = s.get_zone(tc, c:GetSequence())
  		if zone ~= 0 and Duel.SpecialSummonStep(tc, 0, tp, tp, false, false, POS_FACEUP, zone) then
  			local e1 = Effect.CreateEffect(c)
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_DISABLE)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  			tc:RegisterEffect(e1)
  			local e2 = Effect.CreateEffect(c)
  			e2:SetType(EFFECT_TYPE_SINGLE)
  			e2:SetCode(EFFECT_DISABLE_EFFECT)
  			e2:SetValue(RESET_TURN_SET)
  			e2:SetReset(RESET_EVENT + RESETS_STANDARD)
  			tc:RegisterEffect(e2)
  			local e3 = Effect.CreateEffect(c)
  			e3:SetType(EFFECT_TYPE_SINGLE)
  			e3:SetCode(EFFECT_SET_ATTACK)
  			e3:SetValue(0)
  			e3:SetReset(RESET_EVENT + RESETS_STANDARD)
  			tc:RegisterEffect(e3)
  		end
  		Duel.SpecialSummonComplete()
  	end
  end
  function s.splimit(e, c, sump, sumtype, sumpos, targetp)
  	return c:GetOriginalType() & TYPE_LINK ~= TYPE_LINK
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsPreviousLocation(LOCATION_ONFIELD) and e:GetHandler():IsPreviousControler(tp)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return Duel.GetLocationCount(1 - tp, LOCATION_MZONE) > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP_DEFENSE, 1 - tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, c, 1, 0, 0)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetHandler():IsRelateToEffect(e) then
  		Duel.SpecialSummon(e:GetHandler(), 0, tp, 1 - tp, false, false, POS_FACEUP_DEFENSE)
  	end
  end
  
  