name: Marincess
card_text: If a "Marincess" Link Monster(s) you control is destroyed by battle or card effect: Target 1 of those monsters; Special Summon 1 "Marincess" Link Monster from your Extra Deck with a lower Link Rating than that monster. (This is treated as a Link Summon.) It is unaffected by your opponent's card effects while you control it. You can only activate 1 "Marincess Snow" per turn.
external_ids: local SETCARD_ID_0 = 0x12b --Marincess
  
lua: local SETCARD_ID_0 = 0x12b --Marincess
  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:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY + EFFECT_FLAG_CARD_TARGET)
  	e1:SetCode(EVENT_DESTROYED)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.spfilter1(c, e, tp)
  	local lk = c:GetLink()
  	return c:IsReason(REASON_BATTLE + REASON_EFFECT)
  		and c:IsPreviousLocation(LOCATION_MZONE)
  		and c:IsPreviousControler(tp)
  		and c:IsPreviousSetCard(SETCARD_ID_0)
  		and bit.band(c:GetPreviousTypeOnField(), TYPE_LINK) ~= 0
  		and c:IsLocation(LOCATION_GRAVE + LOCATION_REMOVED)
  		and c:IsCanBeEffectTarget(e)
  		and lk > 0
  		and Duel.IsExistingMatchingCard(s.spfilter2, tp, LOCATION_EXTRA, 0, 1, nil, e, tp, lk)
  end
  function s.spfilter2(c, e, tp, lk)
  	return c:IsSetCard(SETCARD_ID_0)
  		and c:IsType(TYPE_LINK)
  		and c:GetLink() < lk
  		and c:IsCanBeSpecialSummoned(e, SUMMON_TYPE_LINK, tp, false, false)
  		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 eg:IsContains(chkc) and s.spfilter1(chkc, e, tp)
  	end
  	if chk == 0 then
  		return aux.MustMaterialCheck(nil, tp, EFFECT_MUST_BE_LMATERIAL) and eg:IsExists(s.spfilter1, 1, nil, e, tp)
  	end
  	if eg:GetCount() == 1 then
  		Duel.SetTargetCard(eg)
  	else
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  		local g = eg:FilterSelect(tp, s.spfilter1, 1, 1, nil, e, tp)
  		Duel.SetTargetCard(g)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_EXTRA)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if not tc:IsRelateToEffect(e) or not aux.MustMaterialCheck(nil, tp, EFFECT_MUST_BE_LMATERIAL) then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.spfilter2, tp, LOCATION_EXTRA, 0, 1, 1, nil, e, tp, tc:GetLink())
  	local tc = g:GetFirst()
  	if not tc then
  		return
  	end
  	tc:SetMaterial(nil)
  	if Duel.SpecialSummon(tc, SUMMON_TYPE_LINK, tp, tp, false, false, POS_FACEUP) ~= 0 then
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  		e1:SetCode(EFFECT_IMMUNE_EFFECT)
  		e1:SetRange(LOCATION_MZONE)
  		e1:SetCondition(s.immcon)
  		e1:SetValue(s.efilter)
  		e1:SetOwnerPlayer(tp)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_CONTROL)
  		tc:RegisterEffect(e1, true)
  		tc:CompleteProcedure()
  	end
  end
  function s.immcon(e)
  	return e:GetHandler():IsControler(e:GetOwnerPlayer())
  end
  function s.efilter(e, te)
  	return e:GetOwnerPlayer() ~= te:GetOwnerPlayer()
  end
  
  
source_text: Marincess
  If a "Marincess" Link Monster(s) you control is destroyed by battle or card effect: Target 1 of those monsters; Special Summon 1 "Marincess" Link Monster from your Extra Deck with a lower Link Rating than that monster. (This is treated as a Link Summon.) It is unaffected by your opponent's card effects while you control it. You can only activate 1 "Marincess Snow" per turn.
target_text: -- Marincess
  local SETCARD_ID_0 = 0x12b --Marincess
  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:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY + EFFECT_FLAG_CARD_TARGET)
  	e1:SetCode(EVENT_DESTROYED)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.spfilter1(c, e, tp)
  	local lk = c:GetLink()
  	return c:IsReason(REASON_BATTLE + REASON_EFFECT)
  		and c:IsPreviousLocation(LOCATION_MZONE)
  		and c:IsPreviousControler(tp)
  		and c:IsPreviousSetCard(SETCARD_ID_0)
  		and bit.band(c:GetPreviousTypeOnField(), TYPE_LINK) ~= 0
  		and c:IsLocation(LOCATION_GRAVE + LOCATION_REMOVED)
  		and c:IsCanBeEffectTarget(e)
  		and lk > 0
  		and Duel.IsExistingMatchingCard(s.spfilter2, tp, LOCATION_EXTRA, 0, 1, nil, e, tp, lk)
  end
  function s.spfilter2(c, e, tp, lk)
  	return c:IsSetCard(SETCARD_ID_0)
  		and c:IsType(TYPE_LINK)
  		and c:GetLink() < lk
  		and c:IsCanBeSpecialSummoned(e, SUMMON_TYPE_LINK, tp, false, false)
  		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 eg:IsContains(chkc) and s.spfilter1(chkc, e, tp)
  	end
  	if chk == 0 then
  		return aux.MustMaterialCheck(nil, tp, EFFECT_MUST_BE_LMATERIAL) and eg:IsExists(s.spfilter1, 1, nil, e, tp)
  	end
  	if eg:GetCount() == 1 then
  		Duel.SetTargetCard(eg)
  	else
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  		local g = eg:FilterSelect(tp, s.spfilter1, 1, 1, nil, e, tp)
  		Duel.SetTargetCard(g)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_EXTRA)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if not tc:IsRelateToEffect(e) or not aux.MustMaterialCheck(nil, tp, EFFECT_MUST_BE_LMATERIAL) then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.spfilter2, tp, LOCATION_EXTRA, 0, 1, 1, nil, e, tp, tc:GetLink())
  	local tc = g:GetFirst()
  	if not tc then
  		return
  	end
  	tc:SetMaterial(nil)
  	if Duel.SpecialSummon(tc, SUMMON_TYPE_LINK, tp, tp, false, false, POS_FACEUP) ~= 0 then
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  		e1:SetCode(EFFECT_IMMUNE_EFFECT)
  		e1:SetRange(LOCATION_MZONE)
  		e1:SetCondition(s.immcon)
  		e1:SetValue(s.efilter)
  		e1:SetOwnerPlayer(tp)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_CONTROL)
  		tc:RegisterEffect(e1, true)
  		tc:CompleteProcedure()
  	end
  end
  function s.immcon(e)
  	return e:GetHandler():IsControler(e:GetOwnerPlayer())
  end
  function s.efilter(e, te)
  	return e:GetOwnerPlayer() ~= te:GetOwnerPlayer()
  end
  
  