name: Selene,
card_text: 2+ monsters, including a Spellcaster monster
  If this card is Link Summoned: Place Spell Counters on this card, equal to the number of Spells on the field and in the GYs. While an "Endymion" card(s) is on the field, your opponent's monsters cannot target this card for attacks. Once per turn, during the Main Phase (Quick Effect): You can remove 3 Spell Counters from your field; Special Summon 1 Spellcaster monster from your hand or GY in Defense Position, to your zone this card points to.
external_ids: local SETCARD_ID_0 = 0x12a --Endymion
  local COUNTER_ID_0 = 0x1 --Spell
  
lua: local SETCARD_ID_0 = 0x12a --Endymion
  local COUNTER_ID_0 = 0x1 --Spell
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableCounterPermit(0x1)
  	--link summon
  	c:EnableReviveLimit()
  	aux.AddLinkProcedure(c, nil, 2, 3, s.lcheck)
  	--add counter
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_COUNTER)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_F)
  	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e1:SetCondition(s.ctcon)
  	e1:SetOperation(s.ctop)
  	c:RegisterEffect(e1)
  	--can not be attack target
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_SINGLE)
  	e2:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCode(EFFECT_CANNOT_BE_BATTLE_TARGET)
  	e2:SetCondition(s.atcon)
  	e2:SetValue(aux.imval1)
  	c:RegisterEffect(e2)
  	--spsummon
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetType(EFFECT_TYPE_QUICK_O)
  	e3:SetCode(EVENT_FREE_CHAIN)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_MAIN_END)
  	e3:SetCountLimit(1)
  	e3:SetCondition(s.spcon)
  	e3:SetCost(s.spcost)
  	e3:SetTarget(s.sptg)
  	e3:SetOperation(s.spop)
  	c:RegisterEffect(e3)
  end
  function s.lcheck(g, lc)
  	return g:IsExists(Card.IsLinkRace, 1, nil, RACE_SPELLCASTER)
  end
  function s.ctfilter(c)
  	return (c:IsLocation(LOCATION_GRAVE) or c:IsFaceup() or c:GetEquipTarget() or c:IsLocation(LOCATION_FZONE)) and c:IsType(TYPE_SPELL)
  end
  function s.ctcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_LINK)
  end
  function s.ctop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local ct = Duel.GetMatchingGroupCount(s.ctfilter, tp, LOCATION_ONFIELD + LOCATION_GRAVE, LOCATION_ONFIELD + LOCATION_GRAVE, nil)
  	if c:IsFaceup() and c:IsRelateToEffect(e) and ct > 0 then
  		c:AddCounter(COUNTER_ID_0, ct)
  	end
  end
  function s.atfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0)
  end
  function s.atcon(e)
  	return Duel.IsExistingMatchingCard(s.atfilter, e:GetHandlerPlayer(), LOCATION_ONFIELD, LOCATION_ONFIELD, 1, nil)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetCurrentPhase() == PHASE_MAIN1 or Duel.GetCurrentPhase() == PHASE_MAIN2
  end
  function s.spcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsCanRemoveCounter(tp, 1, 0, COUNTER_ID_0, 3, REASON_COST)
  	end
  	Duel.RemoveCounter(tp, 1, 0, COUNTER_ID_0, 3, REASON_COST)
  end
  function s.spfilter(c, e, tp, zone)
  	return c:IsRace(RACE_SPELLCASTER) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP_DEFENSE, tp, zone)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local zone = bit.band(e:GetHandler():GetLinkedZone(tp), 0x1f)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, nil, e, tp, zone)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_HAND + LOCATION_GRAVE)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if not c:IsRelateToEffect(e) then
  		return
  	end
  	local zone = bit.band(e:GetHandler():GetLinkedZone(tp), 0x1f)
  	if zone == 0 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.spfilter), tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, 1, nil, e, tp, zone)
  	if g:GetCount() > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP_DEFENSE, zone)
  	end
  end
  
  
source_text: Selene,
  2+ monsters, including a Spellcaster monster
  If this card is Link Summoned: Place Spell Counters on this card, equal to the number of Spells on the field and in the GYs. While an "Endymion" card(s) is on the field, your opponent's monsters cannot target this card for attacks. Once per turn, during the Main Phase (Quick Effect): You can remove 3 Spell Counters from your field; Special Summon 1 Spellcaster monster from your hand or GY in Defense Position, to your zone this card points to.
target_text: -- Selene,
  local SETCARD_ID_0 = 0x12a --Endymion
  local COUNTER_ID_0 = 0x1 --Spell
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableCounterPermit(0x1)
  	--link summon
  	c:EnableReviveLimit()
  	aux.AddLinkProcedure(c, nil, 2, 3, s.lcheck)
  	--add counter
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_COUNTER)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_F)
  	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e1:SetCondition(s.ctcon)
  	e1:SetOperation(s.ctop)
  	c:RegisterEffect(e1)
  	--can not be attack target
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_SINGLE)
  	e2:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCode(EFFECT_CANNOT_BE_BATTLE_TARGET)
  	e2:SetCondition(s.atcon)
  	e2:SetValue(aux.imval1)
  	c:RegisterEffect(e2)
  	--spsummon
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetType(EFFECT_TYPE_QUICK_O)
  	e3:SetCode(EVENT_FREE_CHAIN)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_MAIN_END)
  	e3:SetCountLimit(1)
  	e3:SetCondition(s.spcon)
  	e3:SetCost(s.spcost)
  	e3:SetTarget(s.sptg)
  	e3:SetOperation(s.spop)
  	c:RegisterEffect(e3)
  end
  function s.lcheck(g, lc)
  	return g:IsExists(Card.IsLinkRace, 1, nil, RACE_SPELLCASTER)
  end
  function s.ctfilter(c)
  	return (c:IsLocation(LOCATION_GRAVE) or c:IsFaceup() or c:GetEquipTarget() or c:IsLocation(LOCATION_FZONE)) and c:IsType(TYPE_SPELL)
  end
  function s.ctcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_LINK)
  end
  function s.ctop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local ct = Duel.GetMatchingGroupCount(s.ctfilter, tp, LOCATION_ONFIELD + LOCATION_GRAVE, LOCATION_ONFIELD + LOCATION_GRAVE, nil)
  	if c:IsFaceup() and c:IsRelateToEffect(e) and ct > 0 then
  		c:AddCounter(COUNTER_ID_0, ct)
  	end
  end
  function s.atfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0)
  end
  function s.atcon(e)
  	return Duel.IsExistingMatchingCard(s.atfilter, e:GetHandlerPlayer(), LOCATION_ONFIELD, LOCATION_ONFIELD, 1, nil)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetCurrentPhase() == PHASE_MAIN1 or Duel.GetCurrentPhase() == PHASE_MAIN2
  end
  function s.spcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsCanRemoveCounter(tp, 1, 0, COUNTER_ID_0, 3, REASON_COST)
  	end
  	Duel.RemoveCounter(tp, 1, 0, COUNTER_ID_0, 3, REASON_COST)
  end
  function s.spfilter(c, e, tp, zone)
  	return c:IsRace(RACE_SPELLCASTER) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP_DEFENSE, tp, zone)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local zone = bit.band(e:GetHandler():GetLinkedZone(tp), 0x1f)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, nil, e, tp, zone)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_HAND + LOCATION_GRAVE)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if not c:IsRelateToEffect(e) then
  		return
  	end
  	local zone = bit.band(e:GetHandler():GetLinkedZone(tp), 0x1f)
  	if zone == 0 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.spfilter), tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, 1, nil, e, tp, zone)
  	if g:GetCount() > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP_DEFENSE, zone)
  	end
  end
  
  