name: CXyz
card_text: 3 Level 6 monsters
  You can also Xyz Summon this card by using "N.As.H. Knight" you control (transfer its materials to this card). Cannot be destroyed by card effects. You can detach 1 material from this card; Special Summon from your Extra Deck, 1 "Number" Xyz Monster that has a number between "101" and "107" in its name, using this face-up card you control (this is treated as an Xyz Summon, transfer its materials to the Summoned monster), but destroy it during your opponent's next End Phase. You can only use this effect of "CXyz N.As.Ch. Knight" once per turn.
external_ids: local CARD_ID_0 = 34876719 --N.As.H. Knight
  local SETCARD_ID_0 = 0x48 --Number
  
lua: local CARD_ID_0 = 34876719 --N.As.H. Knight
  local SETCARD_ID_0 = 0x48 --Number
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--xyz summon
  	aux.AddXyzProcedure(c, nil, 6, 3, s.ovfilter, aux.Stringid(id, 0))
  	c:EnableReviveLimit()
  	--immune
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetCode(EFFECT_INDESTRUCTABLE_EFFECT)
  	e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetValue(1)
  	c:RegisterEffect(e1)
  	--xyz spsummon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetCost(s.spcost)
  	e2:SetTarget(s.sptg)
  	e2:SetOperation(s.spop)
  	c:RegisterEffect(e2)
  end
  function s.ovfilter(c)
  	return c:IsCode(CARD_ID_0) and c:IsFaceup()
  end
  function s.spcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():CheckRemoveOverlayCard(tp, 1, REASON_COST)
  	end
  	e:GetHandler():RemoveOverlayCard(tp, 1, 1, REASON_COST)
  end
  function s.spfilter(c, e, tp, mc)
  	local no = aux.GetXyzNumber(c)
  	return no
  		and no >= 101
  		and no <= 107
  		and c:IsSetCard(SETCARD_ID_0)
  		and c:IsType(TYPE_XYZ)
  		and mc:IsCanBeXyzMaterial(c)
  		and c:IsCanBeSpecialSummoned(e, SUMMON_TYPE_XYZ, tp, false, false)
  		and Duel.GetLocationCountFromEx(tp, tp, mc, c) > 0
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return aux.MustMaterialCheck(c, tp, EFFECT_MUST_BE_XMATERIAL) and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_EXTRA, 0, 1, nil, e, tp, c)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_EXTRA)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if not aux.MustMaterialCheck(c, tp, EFFECT_MUST_BE_XMATERIAL) then
  		return
  	end
  	if c:IsFaceup() and c:IsRelateToEffect(e) and c:IsControler(tp) and not c:IsImmuneToEffect(e) then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  		local g = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_EXTRA, 0, 1, 1, nil, e, tp, c)
  		local sc = g:GetFirst()
  		if sc then
  			local mg = c:GetOverlayGroup()
  			if mg:GetCount() ~= 0 then
  				Duel.Overlay(sc, mg)
  			end
  			sc:SetMaterial(Group.FromCards(c))
  			Duel.Overlay(sc, Group.FromCards(c))
  			if Duel.SpecialSummon(sc, SUMMON_TYPE_XYZ, tp, tp, false, false, POS_FACEUP) ~= 0 then
  				sc:CompleteProcedure()
  				local e1 = Effect.CreateEffect(c)
  				e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  				e1:SetCode(EVENT_PHASE + PHASE_END)
  				e1:SetCountLimit(1)
  				e1:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  				e1:SetLabelObject(sc)
  				e1:SetCondition(s.descon)
  				e1:SetOperation(s.desop)
  				if Duel.GetTurnPlayer() == 1 - tp and Duel.GetCurrentPhase() == PHASE_END then
  					e1:SetReset(RESET_PHASE + PHASE_END + RESET_OPPO_TURN, 2)
  					e1:SetLabel(Duel.GetTurnCount())
  					sc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END + RESET_OPPO_TURN, 0, 2)
  				else
  					e1:SetReset(RESET_PHASE + PHASE_END + RESET_OPPO_TURN)
  					e1:SetLabel(0)
  					sc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END + RESET_OPPO_TURN, 0, 1)
  				end
  				Duel.RegisterEffect(e1, tp)
  			end
  		end
  	end
  end
  function s.descon(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetTurnPlayer() == tp or Duel.GetTurnCount() == e:GetLabel() then
  		return false
  	end
  	return e:GetLabelObject():GetFlagEffect(id) > 0
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Destroy(e:GetLabelObject(), REASON_EFFECT)
  end
  
  
source_text: CXyz
  3 Level 6 monsters
  You can also Xyz Summon this card by using "N.As.H. Knight" you control (transfer its materials to this card). Cannot be destroyed by card effects. You can detach 1 material from this card; Special Summon from your Extra Deck, 1 "Number" Xyz Monster that has a number between "101" and "107" in its name, using this face-up card you control (this is treated as an Xyz Summon, transfer its materials to the Summoned monster), but destroy it during your opponent's next End Phase. You can only use this effect of "CXyz N.As.Ch. Knight" once per turn.
target_text: -- CXyz
  local CARD_ID_0 = 34876719 --N.As.H. Knight
  local SETCARD_ID_0 = 0x48 --Number
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--xyz summon
  	aux.AddXyzProcedure(c, nil, 6, 3, s.ovfilter, aux.Stringid(id, 0))
  	c:EnableReviveLimit()
  	--immune
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetCode(EFFECT_INDESTRUCTABLE_EFFECT)
  	e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetValue(1)
  	c:RegisterEffect(e1)
  	--xyz spsummon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetCost(s.spcost)
  	e2:SetTarget(s.sptg)
  	e2:SetOperation(s.spop)
  	c:RegisterEffect(e2)
  end
  function s.ovfilter(c)
  	return c:IsCode(CARD_ID_0) and c:IsFaceup()
  end
  function s.spcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():CheckRemoveOverlayCard(tp, 1, REASON_COST)
  	end
  	e:GetHandler():RemoveOverlayCard(tp, 1, 1, REASON_COST)
  end
  function s.spfilter(c, e, tp, mc)
  	local no = aux.GetXyzNumber(c)
  	return no
  		and no >= 101
  		and no <= 107
  		and c:IsSetCard(SETCARD_ID_0)
  		and c:IsType(TYPE_XYZ)
  		and mc:IsCanBeXyzMaterial(c)
  		and c:IsCanBeSpecialSummoned(e, SUMMON_TYPE_XYZ, tp, false, false)
  		and Duel.GetLocationCountFromEx(tp, tp, mc, c) > 0
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return aux.MustMaterialCheck(c, tp, EFFECT_MUST_BE_XMATERIAL) and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_EXTRA, 0, 1, nil, e, tp, c)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_EXTRA)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if not aux.MustMaterialCheck(c, tp, EFFECT_MUST_BE_XMATERIAL) then
  		return
  	end
  	if c:IsFaceup() and c:IsRelateToEffect(e) and c:IsControler(tp) and not c:IsImmuneToEffect(e) then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  		local g = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_EXTRA, 0, 1, 1, nil, e, tp, c)
  		local sc = g:GetFirst()
  		if sc then
  			local mg = c:GetOverlayGroup()
  			if mg:GetCount() ~= 0 then
  				Duel.Overlay(sc, mg)
  			end
  			sc:SetMaterial(Group.FromCards(c))
  			Duel.Overlay(sc, Group.FromCards(c))
  			if Duel.SpecialSummon(sc, SUMMON_TYPE_XYZ, tp, tp, false, false, POS_FACEUP) ~= 0 then
  				sc:CompleteProcedure()
  				local e1 = Effect.CreateEffect(c)
  				e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  				e1:SetCode(EVENT_PHASE + PHASE_END)
  				e1:SetCountLimit(1)
  				e1:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  				e1:SetLabelObject(sc)
  				e1:SetCondition(s.descon)
  				e1:SetOperation(s.desop)
  				if Duel.GetTurnPlayer() == 1 - tp and Duel.GetCurrentPhase() == PHASE_END then
  					e1:SetReset(RESET_PHASE + PHASE_END + RESET_OPPO_TURN, 2)
  					e1:SetLabel(Duel.GetTurnCount())
  					sc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END + RESET_OPPO_TURN, 0, 2)
  				else
  					e1:SetReset(RESET_PHASE + PHASE_END + RESET_OPPO_TURN)
  					e1:SetLabel(0)
  					sc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END + RESET_OPPO_TURN, 0, 1)
  				end
  				Duel.RegisterEffect(e1, tp)
  			end
  		end
  	end
  end
  function s.descon(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetTurnPlayer() == tp or Duel.GetTurnCount() == e:GetLabel() then
  		return false
  	end
  	return e:GetLabelObject():GetFlagEffect(id) > 0
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Destroy(e:GetLabelObject(), REASON_EFFECT)
  end
  
  