name: Endymion's
card_text: This card's name becomes "Magical Citadel of Endymion" while in the Spell & Trap Zone. Each time a Spell Card is activated, place 1 Spell Counter on this card when that Spell resolves. Once per turn, after damage calculation, if your Spellcaster monster is destroyed by battle: You can remove 6 Spell Counters from your field; Special Summon 1 Level 7 or higher Spellcaster monster from your hand or Deck. You can only activate 1 "Endymion's Lab" per turn.
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)
  	c:EnableCounterPermit(0x1)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	c:RegisterEffect(e1)
  	--Code Change
  	aux.EnableChangeCode(c, CARD_ID_0)
  	--Add Counter
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_FIELD)
  	e3:SetCode(EVENT_CHAINING)
  	e3:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetOperation(aux.chainreg)
  	c:RegisterEffect(e3)
  	local e4 = Effect.CreateEffect(c)
  	e4:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_FIELD)
  	e4:SetCode(EVENT_CHAIN_SOLVED)
  	e4:SetRange(LOCATION_SZONE)
  	e4:SetOperation(s.counterop)
  	c:RegisterEffect(e4)
  	--Special Summon
  	local e5 = Effect.CreateEffect(c)
  	e5:SetDescription(aux.Stringid(id, 0))
  	e5:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e5:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e5:SetCode(EVENT_BATTLED)
  	e5:SetRange(LOCATION_SZONE)
  	e5:SetCountLimit(1)
  	e5:SetCondition(s.spcon)
  	e5:SetCost(s.spcost)
  	e5:SetTarget(s.sptg)
  	e5:SetOperation(s.spop)
  	c:RegisterEffect(e5)
  end
  function s.counterop(e, tp, eg, ep, ev, re, r, rp)
  	if re:IsHasType(EFFECT_TYPE_ACTIVATE) and re:IsActiveType(TYPE_SPELL) and e:GetHandler():GetFlagEffect(FLAG_ID_CHAINING) > 0 then
  		e:GetHandler():AddCounter(COUNTER_ID_0, 1)
  	end
  end
  function s.spconcheck(c, tp)
  	return c and c:IsRace(RACE_SPELLCASTER) and c:IsControler(tp) and c:IsStatus(STATUS_BATTLE_DESTROYED)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetAttackTarget() ~= nil and (s.spconcheck(Duel.GetAttacker(), tp) or s.spconcheck(Duel.GetAttackTarget(), tp))
  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, 6, REASON_COST)
  	end
  	Duel.RemoveCounter(tp, 1, 0, COUNTER_ID_0, 6, REASON_COST)
  end
  function s.spfilter(c, e, tp)
  	return c:IsRace(RACE_SPELLCASTER) and c:IsLevelAbove(7) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_DECK + LOCATION_HAND, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_DECK)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_DECK + LOCATION_HAND, 0, 1, 1, nil, e, tp)
  	if g:GetCount() > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  
source_text: Endymion's
  This card's name becomes "Magical Citadel of Endymion" while in the Spell & Trap Zone. Each time a Spell Card is activated, place 1 Spell Counter on this card when that Spell resolves. Once per turn, after damage calculation, if your Spellcaster monster is destroyed by battle: You can remove 6 Spell Counters from your field; Special Summon 1 Level 7 or higher Spellcaster monster from your hand or Deck. You can only activate 1 "Endymion's Lab" per turn.
target_text: -- Endymion's
  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)
  	c:EnableCounterPermit(0x1)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	c:RegisterEffect(e1)
  	--Code Change
  	aux.EnableChangeCode(c, CARD_ID_0)
  	--Add Counter
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_FIELD)
  	e3:SetCode(EVENT_CHAINING)
  	e3:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetOperation(aux.chainreg)
  	c:RegisterEffect(e3)
  	local e4 = Effect.CreateEffect(c)
  	e4:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_FIELD)
  	e4:SetCode(EVENT_CHAIN_SOLVED)
  	e4:SetRange(LOCATION_SZONE)
  	e4:SetOperation(s.counterop)
  	c:RegisterEffect(e4)
  	--Special Summon
  	local e5 = Effect.CreateEffect(c)
  	e5:SetDescription(aux.Stringid(id, 0))
  	e5:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e5:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e5:SetCode(EVENT_BATTLED)
  	e5:SetRange(LOCATION_SZONE)
  	e5:SetCountLimit(1)
  	e5:SetCondition(s.spcon)
  	e5:SetCost(s.spcost)
  	e5:SetTarget(s.sptg)
  	e5:SetOperation(s.spop)
  	c:RegisterEffect(e5)
  end
  function s.counterop(e, tp, eg, ep, ev, re, r, rp)
  	if re:IsHasType(EFFECT_TYPE_ACTIVATE) and re:IsActiveType(TYPE_SPELL) and e:GetHandler():GetFlagEffect(FLAG_ID_CHAINING) > 0 then
  		e:GetHandler():AddCounter(COUNTER_ID_0, 1)
  	end
  end
  function s.spconcheck(c, tp)
  	return c and c:IsRace(RACE_SPELLCASTER) and c:IsControler(tp) and c:IsStatus(STATUS_BATTLE_DESTROYED)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetAttackTarget() ~= nil and (s.spconcheck(Duel.GetAttacker(), tp) or s.spconcheck(Duel.GetAttackTarget(), tp))
  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, 6, REASON_COST)
  	end
  	Duel.RemoveCounter(tp, 1, 0, COUNTER_ID_0, 6, REASON_COST)
  end
  function s.spfilter(c, e, tp)
  	return c:IsRace(RACE_SPELLCASTER) and c:IsLevelAbove(7) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_DECK + LOCATION_HAND, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_DECK)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_DECK + LOCATION_HAND, 0, 1, 1, nil, e, tp)
  	if g:GetCount() > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  