name: Purrely
card_text: Choose 1 card on the field, and until the end of the next turn, the first time it would be destroyed by card effect, it is not destroyed. After choosing a card, then you can apply the following effect.
  ● Discard 1 card, and if you do, Special Summon 1 Level 1 "Purrely" monster from your Deck.
  A "Purrely" Xyz Monster that has this card as material gains the following effect.
  ● This card can attack monsters a number of times each Battle Phase, up to the number of "Purrely Happy Memory" attached to it +1.
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 + CATEGORY_DECKDES + CATEGORY_HANDES)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetTarget(s.tg)
  	e1:SetOperation(s.op)
  	c:RegisterEffect(e1)
  	--get effect
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCode(EFFECT_EXTRA_ATTACK_MONSTER)
  	e2:SetType(EFFECT_TYPE_XMATERIAL)
  	e2:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCondition(s.xcon)
  	e2:SetValue(s.xval)
  	c:RegisterEffect(e2)
  end
  function s.tg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(nil, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, e:GetHandler())
  	end
  end
  function s.filter(c, e, tp)
  	return c:IsLevel(1) and c:IsSetCard(SETCARD_ID_0) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.op(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_OPERATECARD)
  	local g = Duel.SelectMatchingCard(tp, nil, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, 1, aux.ExceptThisCard(e))
  	local tc = g:GetFirst()
  	if not tc then
  		return
  	end
  	Duel.HintSelection(g)
  	if not tc:IsImmuneToEffect(e) then
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_INDESTRUCTABLE_COUNT)
  		e1:SetProperty(EFFECT_FLAG_SET_AVAILABLE + EFFECT_FLAG_NO_TURN_RESET)
  		e1:SetCountLimit(1)
  		e1:SetValue(s.valcon)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END, 2)
  		tc:RegisterEffect(e1)
  		if
  			Duel.IsExistingMatchingCard(Card.IsDiscardable, tp, LOCATION_HAND, 0, 1, nil)
  			and Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  			and Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_DECK, 0, 1, nil, e, tp)
  			and Duel.SelectYesNo(tp, aux.Stringid(id, 0))
  		then
  			Duel.BreakEffect()
  			if Duel.DiscardHand(tp, nil, 1, 1, REASON_EFFECT + REASON_DISCARD) > 0 then
  				Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  				local sg = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_DECK, 0, 1, 1, nil, e, tp)
  				if sg:GetCount() > 0 then
  					Duel.SpecialSummon(sg, 0, tp, tp, false, false, POS_FACEUP)
  				end
  			end
  		end
  	end
  end
  function s.valcon(e, re, r, rp)
  	return bit.band(r, REASON_EFFECT) ~= 0
  end
  function s.xcon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	return c:IsSetCard(SETCARD_ID_0)
  end
  function s.xval(e)
  	local c = e:GetHandler()
  	return c:GetOverlayGroup():FilterCount(Card.IsCode, nil, id)
  end
  
  
source_text: Purrely
  Choose 1 card on the field, and until the end of the next turn, the first time it would be destroyed by card effect, it is not destroyed. After choosing a card, then you can apply the following effect.
  ● Discard 1 card, and if you do, Special Summon 1 Level 1 "Purrely" monster from your Deck.
  A "Purrely" Xyz Monster that has this card as material gains the following effect.
  ● This card can attack monsters a number of times each Battle Phase, up to the number of "Purrely Happy Memory" attached to it +1.
target_text: -- Purrely
  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 + CATEGORY_DECKDES + CATEGORY_HANDES)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetTarget(s.tg)
  	e1:SetOperation(s.op)
  	c:RegisterEffect(e1)
  	--get effect
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCode(EFFECT_EXTRA_ATTACK_MONSTER)
  	e2:SetType(EFFECT_TYPE_XMATERIAL)
  	e2:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCondition(s.xcon)
  	e2:SetValue(s.xval)
  	c:RegisterEffect(e2)
  end
  function s.tg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(nil, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, e:GetHandler())
  	end
  end
  function s.filter(c, e, tp)
  	return c:IsLevel(1) and c:IsSetCard(SETCARD_ID_0) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.op(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_OPERATECARD)
  	local g = Duel.SelectMatchingCard(tp, nil, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, 1, aux.ExceptThisCard(e))
  	local tc = g:GetFirst()
  	if not tc then
  		return
  	end
  	Duel.HintSelection(g)
  	if not tc:IsImmuneToEffect(e) then
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_INDESTRUCTABLE_COUNT)
  		e1:SetProperty(EFFECT_FLAG_SET_AVAILABLE + EFFECT_FLAG_NO_TURN_RESET)
  		e1:SetCountLimit(1)
  		e1:SetValue(s.valcon)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END, 2)
  		tc:RegisterEffect(e1)
  		if
  			Duel.IsExistingMatchingCard(Card.IsDiscardable, tp, LOCATION_HAND, 0, 1, nil)
  			and Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  			and Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_DECK, 0, 1, nil, e, tp)
  			and Duel.SelectYesNo(tp, aux.Stringid(id, 0))
  		then
  			Duel.BreakEffect()
  			if Duel.DiscardHand(tp, nil, 1, 1, REASON_EFFECT + REASON_DISCARD) > 0 then
  				Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  				local sg = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_DECK, 0, 1, 1, nil, e, tp)
  				if sg:GetCount() > 0 then
  					Duel.SpecialSummon(sg, 0, tp, tp, false, false, POS_FACEUP)
  				end
  			end
  		end
  	end
  end
  function s.valcon(e, re, r, rp)
  	return bit.band(r, REASON_EFFECT) ~= 0
  end
  function s.xcon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	return c:IsSetCard(SETCARD_ID_0)
  end
  function s.xval(e)
  	local c = e:GetHandler()
  	return c:GetOverlayGroup():FilterCount(Card.IsCode, nil, id)
  end
  
  