name: Chronomaly
card_text: If you control no monsters, or all monsters you control are "Chronomaly" monsters: You can pay 500 LP; immediately after this effect resolves, Normal Summon 1 "Chronomaly" monster from your hand. If a "Chronomaly" or "Number" Xyz Monster you control would activate an effect by detaching material, you can send this card to the GY instead of 1 of those materials. You can only use each effect of "Chronomaly Temple - Trilithon" once per turn.
external_ids: local SETCARD_ID_0 = 0x70 --Chronomaly
  
lua: local SETCARD_ID_0 = 0x70 --Chronomaly
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--summon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_SUMMON)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetCost(s.sumcost)
  	e2:SetCondition(s.sumcon)
  	e2:SetTarget(s.sumtg)
  	e2:SetOperation(s.sumop)
  	c:RegisterEffect(e2)
  	--remove overlay replace
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_FIELD)
  	e3:SetCode(EFFECT_OVERLAY_REMOVE_REPLACE)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetCountLimit(1, id + 1)
  	e3:SetCondition(s.rcon)
  	e3:SetOperation(s.rop)
  	c:RegisterEffect(e3)
  end
  function s.sumcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.CheckLPCost(tp, 500)
  	end
  	Duel.PayLPCost(tp, 500)
  end
  function s.cfilter(c)
  	return c:IsFacedown() or not c:IsSetCard(SETCARD_ID_0)
  end
  function s.sumcon(e, tp, eg, ep, ev, re, r, rp)
  	return not Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 1, nil)
  end
  function s.sumfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsSummonable(true, nil)
  end
  function s.sumtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.sumfilter, tp, LOCATION_HAND, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SUMMON, nil, 1, 0, LOCATION_HAND)
  end
  function s.sumop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.sumfilter, tp, LOCATION_HAND, 0, 1, 1, nil)
  	local tc = g:GetFirst()
  	if tc then
  		Duel.Summon(tp, tc, true, nil)
  	end
  end
  function s.rcon(e, tp, eg, ep, ev, re, r, rp)
  	local rc = re:GetHandler()
  	return bit.band(r, REASON_COST) ~= 0
  		and re:IsActivated()
  		and re:IsActiveType(TYPE_XYZ)
  		and rc:GetOverlayCount() >= ev - 1
  		and (rc:IsSetCard(SETCARD_ID_0) or rc:IsSetCard(0x48))
  		and e:GetHandler():IsAbleToGraveAsCost()
  		and ep == e:GetOwnerPlayer()
  end
  function s.rop(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.SendtoGrave(e:GetHandler(), REASON_COST)
  end
  
  
source_text: Chronomaly
  If you control no monsters, or all monsters you control are "Chronomaly" monsters: You can pay 500 LP; immediately after this effect resolves, Normal Summon 1 "Chronomaly" monster from your hand. If a "Chronomaly" or "Number" Xyz Monster you control would activate an effect by detaching material, you can send this card to the GY instead of 1 of those materials. You can only use each effect of "Chronomaly Temple - Trilithon" once per turn.
target_text: -- Chronomaly
  local SETCARD_ID_0 = 0x70 --Chronomaly
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--summon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_SUMMON)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetCost(s.sumcost)
  	e2:SetCondition(s.sumcon)
  	e2:SetTarget(s.sumtg)
  	e2:SetOperation(s.sumop)
  	c:RegisterEffect(e2)
  	--remove overlay replace
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_FIELD)
  	e3:SetCode(EFFECT_OVERLAY_REMOVE_REPLACE)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetCountLimit(1, id + 1)
  	e3:SetCondition(s.rcon)
  	e3:SetOperation(s.rop)
  	c:RegisterEffect(e3)
  end
  function s.sumcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.CheckLPCost(tp, 500)
  	end
  	Duel.PayLPCost(tp, 500)
  end
  function s.cfilter(c)
  	return c:IsFacedown() or not c:IsSetCard(SETCARD_ID_0)
  end
  function s.sumcon(e, tp, eg, ep, ev, re, r, rp)
  	return not Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 1, nil)
  end
  function s.sumfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsSummonable(true, nil)
  end
  function s.sumtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.sumfilter, tp, LOCATION_HAND, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SUMMON, nil, 1, 0, LOCATION_HAND)
  end
  function s.sumop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.sumfilter, tp, LOCATION_HAND, 0, 1, 1, nil)
  	local tc = g:GetFirst()
  	if tc then
  		Duel.Summon(tp, tc, true, nil)
  	end
  end
  function s.rcon(e, tp, eg, ep, ev, re, r, rp)
  	local rc = re:GetHandler()
  	return bit.band(r, REASON_COST) ~= 0
  		and re:IsActivated()
  		and re:IsActiveType(TYPE_XYZ)
  		and rc:GetOverlayCount() >= ev - 1
  		and (rc:IsSetCard(SETCARD_ID_0) or rc:IsSetCard(0x48))
  		and e:GetHandler():IsAbleToGraveAsCost()
  		and ep == e:GetOwnerPlayer()
  end
  function s.rop(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.SendtoGrave(e:GetHandler(), REASON_COST)
  end
  
  