name: Xyz
card_text: Tribute 1 Xyz Monster; Special Summon from your Extra Deck, 1 monster with the same Type, Attribute, and Rank as that Tributed monster had on the field, but with a different name, and if you do, attach this card to it as an Xyz Material. During the End Phase, send it to the Graveyard. You can only activate 1 "Xyz Shift" per turn.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetLabel(0)
  	e1:SetCost(s.cost)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	e:SetLabel(100)
  	if chk == 0 then
  		return true
  	end
  end
  function s.cfilter(c, e, tp)
  	local rk = c:GetRank()
  	return rk > 0 and Duel.IsExistingMatchingCard(s.spfilter1, tp, LOCATION_EXTRA, 0, 1, nil, rk, c:GetRace(), c:GetAttribute(), c:GetCode(), e, tp, c)
  end
  function s.spfilter1(c, rk, race, att, code, e, tp, mc)
  	return c:IsRank(rk) and c:IsRace(race) and c:IsAttribute(att) and not c:IsCode(code) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false) and Duel.GetLocationCountFromEx(tp, tp, mc, c) > 0
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		if e:GetLabel() ~= 100 then
  			return false
  		end
  		e:SetLabel(0)
  		return e:IsHasType(EFFECT_TYPE_ACTIVATE) and e:GetHandler():IsCanOverlay() and Duel.CheckReleaseGroup(tp, s.cfilter, 1, nil, e, tp)
  	end
  	e:SetLabel(0)
  	local g = Duel.SelectReleaseGroup(tp, s.cfilter, 1, 1, nil, e, tp)
  	e:SetLabel(g:GetFirst():GetRank(), g:GetFirst():GetRace(), g:GetFirst():GetAttribute(), g:GetFirst():GetCode())
  	Duel.Release(g, REASON_COST)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_EXTRA)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local rk, race, att, code = e:GetLabel()
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.spfilter1, tp, LOCATION_EXTRA, 0, 1, 1, nil, rk, race, att, code, e, tp, nil)
  	local sc = g:GetFirst()
  	if sc and Duel.SpecialSummon(sc, 0, tp, tp, false, false, POS_FACEUP) ~= 0 then
  		if c:IsRelateToEffect(e) then
  			c:CancelToGrave()
  			Duel.Overlay(sc, Group.FromCards(c))
  		end
  		local fid = c:GetFieldID()
  		sc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD, 0, 1, fid)
  		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:SetLabel(fid)
  		e1:SetLabelObject(sc)
  		e1:SetCondition(s.tgcon)
  		e1:SetOperation(s.tgop)
  		Duel.RegisterEffect(e1, tp)
  	end
  end
  function s.tgcon(e, tp, eg, ep, ev, re, r, rp)
  	local tc = e:GetLabelObject()
  	if tc:GetFlagEffectLabel(id) ~= e:GetLabel() then
  		e:Reset()
  		return false
  	else
  		return true
  	end
  end
  function s.tgop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.SendtoGrave(e:GetLabelObject(), REASON_EFFECT)
  end
  
  
source_text: Xyz
  Tribute 1 Xyz Monster; Special Summon from your Extra Deck, 1 monster with the same Type, Attribute, and Rank as that Tributed monster had on the field, but with a different name, and if you do, attach this card to it as an Xyz Material. During the End Phase, send it to the Graveyard. You can only activate 1 "Xyz Shift" per turn.
target_text: -- Xyz
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetLabel(0)
  	e1:SetCost(s.cost)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	e:SetLabel(100)
  	if chk == 0 then
  		return true
  	end
  end
  function s.cfilter(c, e, tp)
  	local rk = c:GetRank()
  	return rk > 0 and Duel.IsExistingMatchingCard(s.spfilter1, tp, LOCATION_EXTRA, 0, 1, nil, rk, c:GetRace(), c:GetAttribute(), c:GetCode(), e, tp, c)
  end
  function s.spfilter1(c, rk, race, att, code, e, tp, mc)
  	return c:IsRank(rk) and c:IsRace(race) and c:IsAttribute(att) and not c:IsCode(code) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false) and Duel.GetLocationCountFromEx(tp, tp, mc, c) > 0
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		if e:GetLabel() ~= 100 then
  			return false
  		end
  		e:SetLabel(0)
  		return e:IsHasType(EFFECT_TYPE_ACTIVATE) and e:GetHandler():IsCanOverlay() and Duel.CheckReleaseGroup(tp, s.cfilter, 1, nil, e, tp)
  	end
  	e:SetLabel(0)
  	local g = Duel.SelectReleaseGroup(tp, s.cfilter, 1, 1, nil, e, tp)
  	e:SetLabel(g:GetFirst():GetRank(), g:GetFirst():GetRace(), g:GetFirst():GetAttribute(), g:GetFirst():GetCode())
  	Duel.Release(g, REASON_COST)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_EXTRA)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local rk, race, att, code = e:GetLabel()
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.spfilter1, tp, LOCATION_EXTRA, 0, 1, 1, nil, rk, race, att, code, e, tp, nil)
  	local sc = g:GetFirst()
  	if sc and Duel.SpecialSummon(sc, 0, tp, tp, false, false, POS_FACEUP) ~= 0 then
  		if c:IsRelateToEffect(e) then
  			c:CancelToGrave()
  			Duel.Overlay(sc, Group.FromCards(c))
  		end
  		local fid = c:GetFieldID()
  		sc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD, 0, 1, fid)
  		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:SetLabel(fid)
  		e1:SetLabelObject(sc)
  		e1:SetCondition(s.tgcon)
  		e1:SetOperation(s.tgop)
  		Duel.RegisterEffect(e1, tp)
  	end
  end
  function s.tgcon(e, tp, eg, ep, ev, re, r, rp)
  	local tc = e:GetLabelObject()
  	if tc:GetFlagEffectLabel(id) ~= e:GetLabel() then
  		e:Reset()
  		return false
  	else
  		return true
  	end
  end
  function s.tgop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.SendtoGrave(e:GetLabelObject(), REASON_EFFECT)
  end
  
  