name: Endymion,
card_text: You can Special Summon this card (from your hand or GY) by removing 6 Spell Counters from a "Magical Citadel of Endymion" you control. If Summoned this way: Target 1 Spell in your GY; add that target to your hand. Once per turn: You can discard 1 Spell, then target 1 card on the field; destroy that target.
external_ids: local CARD_ID_0 = 39910367 --Magical Citadel of Endymion
  local COUNTER_ID_0 = 0x1 --Spell
  
lua: local CARD_ID_0 = 39910367 --Magical Citadel of Endymion
  local COUNTER_ID_0 = 0x1 --Spell
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_SPSUMMON_PROC)
  	e1:SetProperty(EFFECT_FLAG_UNCOPYABLE)
  	e1:SetRange(LOCATION_HAND + LOCATION_GRAVE)
  	e1:SetCondition(s.spcon)
  	e1:SetOperation(s.spop)
  	e1:SetValue(SUMMON_VALUE_SELF)
  	c:RegisterEffect(e1)
  	--to hand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_TOHAND)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_F)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e2:SetCondition(s.condition)
  	e2:SetTarget(s.target)
  	e2:SetOperation(s.operation)
  	c:RegisterEffect(e2)
  	--destroy
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_DESTROY)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCountLimit(1)
  	e3:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e3:SetCost(s.descost)
  	e3:SetTarget(s.destg)
  	e3:SetOperation(s.desop)
  	c:RegisterEffect(e3)
  end
  function s.spcfilter(c, tp)
  	return c:IsCode(CARD_ID_0) and c:IsCanRemoveCounter(tp, COUNTER_ID_0, 1, REASON_COST)
  end
  function s.spcon(e, c)
  	if c == nil then
  		return true
  	end
  	if c:IsHasEffect(EFFECT_NECRO_VALLEY) then
  		return false
  	end
  	local tp = c:GetControler()
  	local g = Duel.GetMatchingGroup(s.spcfilter, tp, LOCATION_ONFIELD, 0, nil, tp)
  	local ct = 0
  	for tc in aux.Next(g) do
  		ct = ct + tc:GetCounter(COUNTER_ID_0)
  	end
  	return ct >= 6 and Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp, c)
  	local g = Duel.GetMatchingGroup(s.spcfilter, tp, LOCATION_ONFIELD, 0, nil, tp)
  	if #g == 1 then
  		g:GetFirst():RemoveCounter(tp, COUNTER_ID_0, 6, REASON_COST)
  	else
  		for i = 1, 6 do
  			Duel.Hint(HINT_SELECTMSG, tp, aux.Stringid(id, 2))
  			local tg = Duel.SelectMatchingCard(tp, s.spcfilter, tp, LOCATION_ONFIELD, 0, 1, 1, nil, tp)
  			tg:GetFirst():RemoveCounter(tp, COUNTER_ID_0, 1, REASON_COST)
  		end
  	end
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():GetSummonType() == SUMMON_TYPE_SPECIAL + SUMMON_VALUE_SELF
  end
  function s.filter(c)
  	return c:IsType(TYPE_SPELL) and c:IsAbleToHand()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.filter(chkc)
  	end
  	if chk == 0 then
  		return true
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectTarget(tp, s.filter, tp, LOCATION_GRAVE, 0, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, g, g:GetCount(), 0, 0)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc and tc:IsRelateToEffect(e) then
  		Duel.SendtoHand(tc, nil, REASON_EFFECT)
  		Duel.ShuffleHand(tp)
  	end
  end
  function s.cfilter(c)
  	return c:IsType(TYPE_SPELL) and c:IsDiscardable()
  end
  function s.descost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_HAND, 0, 1, e:GetHandler())
  	end
  	Duel.DiscardHand(tp, s.cfilter, 1, 1, REASON_COST + REASON_DISCARD)
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsOnField()
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(aux.TRUE, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  	local g = Duel.SelectTarget(tp, aux.TRUE, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, g, 1, 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.Destroy(tc, REASON_EFFECT)
  	end
  end
  
  
source_text: Endymion,
  You can Special Summon this card (from your hand or GY) by removing 6 Spell Counters from a "Magical Citadel of Endymion" you control. If Summoned this way: Target 1 Spell in your GY; add that target to your hand. Once per turn: You can discard 1 Spell, then target 1 card on the field; destroy that target.
target_text: -- Endymion,
  local CARD_ID_0 = 39910367 --Magical Citadel of Endymion
  local COUNTER_ID_0 = 0x1 --Spell
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_SPSUMMON_PROC)
  	e1:SetProperty(EFFECT_FLAG_UNCOPYABLE)
  	e1:SetRange(LOCATION_HAND + LOCATION_GRAVE)
  	e1:SetCondition(s.spcon)
  	e1:SetOperation(s.spop)
  	e1:SetValue(SUMMON_VALUE_SELF)
  	c:RegisterEffect(e1)
  	--to hand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_TOHAND)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_F)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e2:SetCondition(s.condition)
  	e2:SetTarget(s.target)
  	e2:SetOperation(s.operation)
  	c:RegisterEffect(e2)
  	--destroy
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_DESTROY)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCountLimit(1)
  	e3:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e3:SetCost(s.descost)
  	e3:SetTarget(s.destg)
  	e3:SetOperation(s.desop)
  	c:RegisterEffect(e3)
  end
  function s.spcfilter(c, tp)
  	return c:IsCode(CARD_ID_0) and c:IsCanRemoveCounter(tp, COUNTER_ID_0, 1, REASON_COST)
  end
  function s.spcon(e, c)
  	if c == nil then
  		return true
  	end
  	if c:IsHasEffect(EFFECT_NECRO_VALLEY) then
  		return false
  	end
  	local tp = c:GetControler()
  	local g = Duel.GetMatchingGroup(s.spcfilter, tp, LOCATION_ONFIELD, 0, nil, tp)
  	local ct = 0
  	for tc in aux.Next(g) do
  		ct = ct + tc:GetCounter(COUNTER_ID_0)
  	end
  	return ct >= 6 and Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp, c)
  	local g = Duel.GetMatchingGroup(s.spcfilter, tp, LOCATION_ONFIELD, 0, nil, tp)
  	if #g == 1 then
  		g:GetFirst():RemoveCounter(tp, COUNTER_ID_0, 6, REASON_COST)
  	else
  		for i = 1, 6 do
  			Duel.Hint(HINT_SELECTMSG, tp, aux.Stringid(id, 2))
  			local tg = Duel.SelectMatchingCard(tp, s.spcfilter, tp, LOCATION_ONFIELD, 0, 1, 1, nil, tp)
  			tg:GetFirst():RemoveCounter(tp, COUNTER_ID_0, 1, REASON_COST)
  		end
  	end
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():GetSummonType() == SUMMON_TYPE_SPECIAL + SUMMON_VALUE_SELF
  end
  function s.filter(c)
  	return c:IsType(TYPE_SPELL) and c:IsAbleToHand()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.filter(chkc)
  	end
  	if chk == 0 then
  		return true
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectTarget(tp, s.filter, tp, LOCATION_GRAVE, 0, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, g, g:GetCount(), 0, 0)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc and tc:IsRelateToEffect(e) then
  		Duel.SendtoHand(tc, nil, REASON_EFFECT)
  		Duel.ShuffleHand(tp)
  	end
  end
  function s.cfilter(c)
  	return c:IsType(TYPE_SPELL) and c:IsDiscardable()
  end
  function s.descost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_HAND, 0, 1, e:GetHandler())
  	end
  	Duel.DiscardHand(tp, s.cfilter, 1, 1, REASON_COST + REASON_DISCARD)
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsOnField()
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(aux.TRUE, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  	local g = Duel.SelectTarget(tp, aux.TRUE, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, g, 1, 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.Destroy(tc, REASON_EFFECT)
  	end
  end
  
  