name: Purrelyeap!?
card_text: Target 1 "Purrely" Xyz Monster you control; Special Summon from your Extra Deck, 1 "Purrely" Xyz Monster with a different Rank, by using that target as material, but return it to the Extra Deck during the End Phase of the next turn. (This is treated as an Xyz Summon. Transfer its materials to the Summoned monster.) You can banish this card from your GY, then target up to 3 "Purrely" monsters in your GY; shuffle them into the Deck. You can only activate 1 "Purrelyeap!?" per turn.
external_ids: local SETCARD_ID_0 = 0x18c --Purrely
  
lua: local SETCARD_ID_0 = 0x18c --Purrely
  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:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--Effect 2
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_TODECK)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCost(aux.bfgcost)
  	e2:SetTarget(s.tdtg)
  	e2:SetOperation(s.tdop)
  	c:RegisterEffect(e2)
  end
  function s.filter1(c, e, tp)
  	return c:IsFaceup()
  		and c:IsSetCard(SETCARD_ID_0)
  		and c:IsType(TYPE_XYZ)
  		and aux.MustMaterialCheck(c, tp, EFFECT_MUST_BE_XMATERIAL)
  		and Duel.IsExistingMatchingCard(s.filter2, tp, LOCATION_EXTRA, 0, 1, nil, e, tp, c, c:GetRank())
  end
  function s.filter2(c, e, tp, mc, rk)
  	return not c:IsRank(rk)
  		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.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsControler(tp) and chkc:IsLocation(LOCATION_MZONE) and s.filter1(chkc, e, tp)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.filter1, tp, LOCATION_MZONE, 0, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	local g = Duel.SelectTarget(tp, s.filter1, tp, LOCATION_MZONE, 0, 1, 1, nil, e, tp)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_EXTRA)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if not aux.MustMaterialCheck(tc, tp, EFFECT_MUST_BE_XMATERIAL) or tc:IsFacedown() or not tc:IsRelateToEffect(e) or tc:IsControler(1 - tp) or tc:IsImmuneToEffect(e) then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.filter2, tp, LOCATION_EXTRA, 0, 1, 1, nil, e, tp, tc, tc:GetRank())
  	local sc = g:GetFirst()
  	if sc then
  		local mg = tc:GetOverlayGroup()
  		if mg:GetCount() ~= 0 then
  			Duel.Overlay(sc, mg)
  		end
  		sc:SetMaterial(Group.FromCards(tc))
  		Duel.Overlay(sc, Group.FromCards(tc))
  		if Duel.SpecialSummon(sc, SUMMON_TYPE_XYZ, tp, tp, false, false, POS_FACEUP) > 0 then
  			sc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END, 0, 2)
  			sc:CompleteProcedure()
  		end
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		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(Duel.GetTurnCount())
  		e1:SetLabelObject(sc)
  		e1:SetCondition(s.tdcon3)
  		e1:SetOperation(s.tdop3)
  		e1:SetReset(RESET_PHASE + PHASE_END, 2)
  		Duel.RegisterEffect(e1, tp)
  	end
  end
  function s.tdcon3(e, tp, eg, ep, ev, re, r, rp)
  	local tc = e:GetLabelObject()
  	return Duel.GetTurnCount() ~= e:GetLabel() and tc:GetFlagEffect(id) ~= 0
  end
  function s.tdop3(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_CARD, 0, id)
  	local tc = e:GetLabelObject()
  	Duel.SendtoDeck(tc, nil, SEQ_DECKSHUFFLE, REASON_EFFECT)
  end
  function s.drfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToDeck()
  end
  function s.tdtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.drfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.drfilter, tp, LOCATION_GRAVE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TODECK)
  	local g = Duel.SelectTarget(tp, s.drfilter, tp, LOCATION_GRAVE, 0, 1, 3, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, g, 1, 0, 0)
  end
  function s.tdop(e, tp, eg, ep, ev, re, r, rp)
  	local tg = Duel.GetTargetsRelateToChain()
  	if #tg == 0 then
  		return
  	end
  	Duel.SendtoDeck(tg, nil, SEQ_DECKSHUFFLE, REASON_EFFECT)
  end
  
  
source_text: Purrelyeap!?
  Target 1 "Purrely" Xyz Monster you control; Special Summon from your Extra Deck, 1 "Purrely" Xyz Monster with a different Rank, by using that target as material, but return it to the Extra Deck during the End Phase of the next turn. (This is treated as an Xyz Summon. Transfer its materials to the Summoned monster.) You can banish this card from your GY, then target up to 3 "Purrely" monsters in your GY; shuffle them into the Deck. You can only activate 1 "Purrelyeap!?" per turn.
target_text: -- Purrelyeap!?
  local SETCARD_ID_0 = 0x18c --Purrely
  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:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--Effect 2
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_TODECK)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCost(aux.bfgcost)
  	e2:SetTarget(s.tdtg)
  	e2:SetOperation(s.tdop)
  	c:RegisterEffect(e2)
  end
  function s.filter1(c, e, tp)
  	return c:IsFaceup()
  		and c:IsSetCard(SETCARD_ID_0)
  		and c:IsType(TYPE_XYZ)
  		and aux.MustMaterialCheck(c, tp, EFFECT_MUST_BE_XMATERIAL)
  		and Duel.IsExistingMatchingCard(s.filter2, tp, LOCATION_EXTRA, 0, 1, nil, e, tp, c, c:GetRank())
  end
  function s.filter2(c, e, tp, mc, rk)
  	return not c:IsRank(rk)
  		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.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsControler(tp) and chkc:IsLocation(LOCATION_MZONE) and s.filter1(chkc, e, tp)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.filter1, tp, LOCATION_MZONE, 0, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	local g = Duel.SelectTarget(tp, s.filter1, tp, LOCATION_MZONE, 0, 1, 1, nil, e, tp)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_EXTRA)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if not aux.MustMaterialCheck(tc, tp, EFFECT_MUST_BE_XMATERIAL) or tc:IsFacedown() or not tc:IsRelateToEffect(e) or tc:IsControler(1 - tp) or tc:IsImmuneToEffect(e) then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.filter2, tp, LOCATION_EXTRA, 0, 1, 1, nil, e, tp, tc, tc:GetRank())
  	local sc = g:GetFirst()
  	if sc then
  		local mg = tc:GetOverlayGroup()
  		if mg:GetCount() ~= 0 then
  			Duel.Overlay(sc, mg)
  		end
  		sc:SetMaterial(Group.FromCards(tc))
  		Duel.Overlay(sc, Group.FromCards(tc))
  		if Duel.SpecialSummon(sc, SUMMON_TYPE_XYZ, tp, tp, false, false, POS_FACEUP) > 0 then
  			sc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END, 0, 2)
  			sc:CompleteProcedure()
  		end
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		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(Duel.GetTurnCount())
  		e1:SetLabelObject(sc)
  		e1:SetCondition(s.tdcon3)
  		e1:SetOperation(s.tdop3)
  		e1:SetReset(RESET_PHASE + PHASE_END, 2)
  		Duel.RegisterEffect(e1, tp)
  	end
  end
  function s.tdcon3(e, tp, eg, ep, ev, re, r, rp)
  	local tc = e:GetLabelObject()
  	return Duel.GetTurnCount() ~= e:GetLabel() and tc:GetFlagEffect(id) ~= 0
  end
  function s.tdop3(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_CARD, 0, id)
  	local tc = e:GetLabelObject()
  	Duel.SendtoDeck(tc, nil, SEQ_DECKSHUFFLE, REASON_EFFECT)
  end
  function s.drfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToDeck()
  end
  function s.tdtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.drfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.drfilter, tp, LOCATION_GRAVE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TODECK)
  	local g = Duel.SelectTarget(tp, s.drfilter, tp, LOCATION_GRAVE, 0, 1, 3, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, g, 1, 0, 0)
  end
  function s.tdop(e, tp, eg, ep, ev, re, r, rp)
  	local tg = Duel.GetTargetsRelateToChain()
  	if #tg == 0 then
  		return
  	end
  	Duel.SendtoDeck(tg, nil, SEQ_DECKSHUFFLE, REASON_EFFECT)
  end
  
  