name: Purrely
card_text: If this card is Normal or Special Summoned: You can excavate the top 3 cards of your Deck, and if you do, you can add 1 excavated "Purrely" Spell/Trap to your hand, also place the rest on the bottom of your Deck in any order. Once per turn, during your Main Phase: You can reveal 1 "Purrely" Quick-Play Spell in your hand, Special Summon 1 Xyz Monster from your Extra Deck that mentions that card, by using this card you control as material, and attach the revealed card to the Summoned monster. (This is treated as an Xyz Summon.)
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)
  	--search
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetProperty(EFFECT_FLAG_DELAY)
  	e1:SetCode(EVENT_SUMMON_SUCCESS)
  	e1:SetTarget(s.tdtg)
  	e1:SetOperation(s.tdop)
  	c:RegisterEffect(e1)
  	local e2 = e1:Clone()
  	e2:SetCode(EVENT_SPSUMMON_SUCCESS)
  	c:RegisterEffect(e2)
  	--spsummon
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCountLimit(1)
  	e3:SetTarget(s.sptg)
  	e3:SetOperation(s.spop)
  	c:RegisterEffect(e3)
  end
  function s.tdtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		if Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) < 3 then
  			return false
  		end
  		local g = Duel.GetDecktopGroup(tp, 3)
  		local result = g:FilterCount(Card.IsAbleToHand, nil) > 0
  		return result
  	end
  	Duel.SetTargetPlayer(tp)
  end
  function s.tdfilter(c)
  	return c:IsAbleToHand() and c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SPELL + TYPE_TRAP)
  end
  function s.tdop(e, tp, eg, ep, ev, re, r, rp)
  	local p = Duel.GetChainInfo(0, CHAININFO_TARGET_PLAYER)
  	Duel.ConfirmDecktop(p, 3)
  	local g = Duel.GetDecktopGroup(p, 3)
  	if not g or #g < 3 then
  		return
  	end
  	g = g:Filter(s.tdfilter, nil)
  	local ct = 3
  	if #g > 0 and Duel.SelectYesNo(p, aux.Stringid(id, 2)) then
  		Duel.Hint(HINT_SELECTMSG, p, HINTMSG_ATOHAND)
  		local sg = g:Select(p, 1, 1, nil)
  		Duel.DisableShuffleCheck()
  		Duel.SendtoHand(sg, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - p, sg)
  		Duel.ShuffleHand(p)
  		ct = ct - 1
  	end
  	Duel.SortDecktop(p, p, ct)
  	for i = 1, ct do
  		local mg = Duel.GetDecktopGroup(p, 1)
  		Duel.MoveSequence(mg:GetFirst(), SEQ_DECKBOTTOM)
  	end
  end
  function s.sptgexfilter(c, e, tp, code)
  	local sc = e:GetHandler()
  	return aux.IsCodeListed(c, code)
  		and c:IsCanBeSpecialSummoned(e, SUMMON_TYPE_XYZ, tp, false, false)
  		and c:IsType(TYPE_XYZ)
  		and sc:IsCanBeXyzMaterial(c)
  		and Duel.GetLocationCountFromEx(tp, tp, sc, c) > 0
  end
  function s.sptgfilter(c, e, tp)
  	return not c:IsPublic()
  		and c:IsType(TYPE_QUICKPLAY)
  		and c:IsSetCard(SETCARD_ID_0)
  		and c:IsCanOverlay()
  		and Duel.IsExistingMatchingCard(s.sptgexfilter, tp, LOCATION_EXTRA, 0, 1, nil, e, tp, c:GetCode())
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return aux.MustMaterialCheck(e:GetHandler(), tp, EFFECT_MUST_BE_XMATERIAL) and Duel.IsExistingMatchingCard(s.sptgfilter, tp, LOCATION_HAND, 0, 1, nil, e, tp)
  	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 not c:IsRelateToChain() or c:IsImmuneToEffect(e) or c:IsFacedown() or c:IsControler(1 - tp) then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_CONFIRM)
  	local g = Duel.SelectMatchingCard(tp, s.sptgfilter, tp, LOCATION_HAND, 0, 1, 1, nil, e, tp)
  	if #g < 1 then
  		return
  	end
  	Duel.ConfirmCards(1 - tp, g)
  	local sc = g:GetFirst()
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local sg = Duel.SelectMatchingCard(tp, s.sptgexfilter, tp, LOCATION_EXTRA, 0, 1, 1, nil, e, tp, sc:GetCode())
  	local tc = sg:GetFirst()
  	tc:SetMaterial(Group.FromCards(c))
  	Duel.Overlay(tc, Group.FromCards(c))
  	Duel.SpecialSummon(tc, SUMMON_TYPE_XYZ, tp, tp, false, false, POS_FACEUP)
  	tc:CompleteProcedure()
  	if not sc:IsImmuneToEffect(e) and sc:IsCanOverlay() then
  		Duel.Overlay(tc, g)
  	end
  end
  
  
source_text: Purrely
  If this card is Normal or Special Summoned: You can excavate the top 3 cards of your Deck, and if you do, you can add 1 excavated "Purrely" Spell/Trap to your hand, also place the rest on the bottom of your Deck in any order. Once per turn, during your Main Phase: You can reveal 1 "Purrely" Quick-Play Spell in your hand, Special Summon 1 Xyz Monster from your Extra Deck that mentions that card, by using this card you control as material, and attach the revealed card to the Summoned monster. (This is treated as an Xyz Summon.)
target_text: -- Purrely
  local SETCARD_ID_0 = 0x18c --Purrely
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--search
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetProperty(EFFECT_FLAG_DELAY)
  	e1:SetCode(EVENT_SUMMON_SUCCESS)
  	e1:SetTarget(s.tdtg)
  	e1:SetOperation(s.tdop)
  	c:RegisterEffect(e1)
  	local e2 = e1:Clone()
  	e2:SetCode(EVENT_SPSUMMON_SUCCESS)
  	c:RegisterEffect(e2)
  	--spsummon
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCountLimit(1)
  	e3:SetTarget(s.sptg)
  	e3:SetOperation(s.spop)
  	c:RegisterEffect(e3)
  end
  function s.tdtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		if Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) < 3 then
  			return false
  		end
  		local g = Duel.GetDecktopGroup(tp, 3)
  		local result = g:FilterCount(Card.IsAbleToHand, nil) > 0
  		return result
  	end
  	Duel.SetTargetPlayer(tp)
  end
  function s.tdfilter(c)
  	return c:IsAbleToHand() and c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SPELL + TYPE_TRAP)
  end
  function s.tdop(e, tp, eg, ep, ev, re, r, rp)
  	local p = Duel.GetChainInfo(0, CHAININFO_TARGET_PLAYER)
  	Duel.ConfirmDecktop(p, 3)
  	local g = Duel.GetDecktopGroup(p, 3)
  	if not g or #g < 3 then
  		return
  	end
  	g = g:Filter(s.tdfilter, nil)
  	local ct = 3
  	if #g > 0 and Duel.SelectYesNo(p, aux.Stringid(id, 2)) then
  		Duel.Hint(HINT_SELECTMSG, p, HINTMSG_ATOHAND)
  		local sg = g:Select(p, 1, 1, nil)
  		Duel.DisableShuffleCheck()
  		Duel.SendtoHand(sg, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - p, sg)
  		Duel.ShuffleHand(p)
  		ct = ct - 1
  	end
  	Duel.SortDecktop(p, p, ct)
  	for i = 1, ct do
  		local mg = Duel.GetDecktopGroup(p, 1)
  		Duel.MoveSequence(mg:GetFirst(), SEQ_DECKBOTTOM)
  	end
  end
  function s.sptgexfilter(c, e, tp, code)
  	local sc = e:GetHandler()
  	return aux.IsCodeListed(c, code)
  		and c:IsCanBeSpecialSummoned(e, SUMMON_TYPE_XYZ, tp, false, false)
  		and c:IsType(TYPE_XYZ)
  		and sc:IsCanBeXyzMaterial(c)
  		and Duel.GetLocationCountFromEx(tp, tp, sc, c) > 0
  end
  function s.sptgfilter(c, e, tp)
  	return not c:IsPublic()
  		and c:IsType(TYPE_QUICKPLAY)
  		and c:IsSetCard(SETCARD_ID_0)
  		and c:IsCanOverlay()
  		and Duel.IsExistingMatchingCard(s.sptgexfilter, tp, LOCATION_EXTRA, 0, 1, nil, e, tp, c:GetCode())
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return aux.MustMaterialCheck(e:GetHandler(), tp, EFFECT_MUST_BE_XMATERIAL) and Duel.IsExistingMatchingCard(s.sptgfilter, tp, LOCATION_HAND, 0, 1, nil, e, tp)
  	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 not c:IsRelateToChain() or c:IsImmuneToEffect(e) or c:IsFacedown() or c:IsControler(1 - tp) then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_CONFIRM)
  	local g = Duel.SelectMatchingCard(tp, s.sptgfilter, tp, LOCATION_HAND, 0, 1, 1, nil, e, tp)
  	if #g < 1 then
  		return
  	end
  	Duel.ConfirmCards(1 - tp, g)
  	local sc = g:GetFirst()
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local sg = Duel.SelectMatchingCard(tp, s.sptgexfilter, tp, LOCATION_EXTRA, 0, 1, 1, nil, e, tp, sc:GetCode())
  	local tc = sg:GetFirst()
  	tc:SetMaterial(Group.FromCards(c))
  	Duel.Overlay(tc, Group.FromCards(c))
  	Duel.SpecialSummon(tc, SUMMON_TYPE_XYZ, tp, tp, false, false, POS_FACEUP)
  	tc:CompleteProcedure()
  	if not sc:IsImmuneToEffect(e) and sc:IsCanOverlay() then
  		Duel.Overlay(tc, g)
  	end
  end
  
  