name: Illusion
card_text: If a monster(s) was destroyed during this turn, while you controlled it: Excavate the top 5 cards of your Deck, then you can Special Summon 1 excavated "Performapal" monster, also shuffle the rest back into your Deck.
external_ids: local SETCARD_ID_0 = 0x9f --Performapal
  
lua: local SETCARD_ID_0 = 0x9f --Performapal
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_DECKDES)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(0, TIMING_END_PHASE)
  	e1:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	if not s.global_check then
  		s.global_check = true
  		local ge1 = Effect.CreateEffect(c)
  		ge1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		ge1:SetCode(EVENT_DESTROYED)
  		ge1:SetOperation(s.checkop)
  		Duel.RegisterEffect(ge1, 0)
  	end
  end
  function s.checkop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = eg:GetFirst()
  	local p1 = false
  	local p2 = false
  	while tc do
  		if tc:IsPreviousLocation(LOCATION_MZONE) then
  			if tc:IsPreviousControler(0) then
  				p1 = true
  			else
  				p2 = true
  			end
  		end
  		tc = eg:GetNext()
  	end
  	if p1 then
  		Duel.RegisterFlagEffect(0, id, RESET_PHASE + PHASE_END, 0, 1)
  	end
  	if p2 then
  		Duel.RegisterFlagEffect(1, id, RESET_PHASE + PHASE_END, 0, 1)
  	end
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFlagEffect(tp, id) ~= 0
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanSpecialSummon(tp) and Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) > 4
  	end
  end
  function s.filter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) == 0 then
  		return
  	end
  	Duel.ConfirmDecktop(tp, 5)
  	local g = Duel.GetDecktopGroup(tp, 5):Filter(s.filter, nil, e, tp)
  	if g:GetCount() > 0 and Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 1)) then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  		local sg = g:Select(tp, 1, 1, nil)
  		Duel.SpecialSummon(sg, 0, tp, tp, false, false, POS_FACEUP)
  	end
  	Duel.ShuffleDeck(tp)
  end
  
  
source_text: Illusion
  If a monster(s) was destroyed during this turn, while you controlled it: Excavate the top 5 cards of your Deck, then you can Special Summon 1 excavated "Performapal" monster, also shuffle the rest back into your Deck.
target_text: -- Illusion
  local SETCARD_ID_0 = 0x9f --Performapal
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_DECKDES)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(0, TIMING_END_PHASE)
  	e1:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	if not s.global_check then
  		s.global_check = true
  		local ge1 = Effect.CreateEffect(c)
  		ge1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		ge1:SetCode(EVENT_DESTROYED)
  		ge1:SetOperation(s.checkop)
  		Duel.RegisterEffect(ge1, 0)
  	end
  end
  function s.checkop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = eg:GetFirst()
  	local p1 = false
  	local p2 = false
  	while tc do
  		if tc:IsPreviousLocation(LOCATION_MZONE) then
  			if tc:IsPreviousControler(0) then
  				p1 = true
  			else
  				p2 = true
  			end
  		end
  		tc = eg:GetNext()
  	end
  	if p1 then
  		Duel.RegisterFlagEffect(0, id, RESET_PHASE + PHASE_END, 0, 1)
  	end
  	if p2 then
  		Duel.RegisterFlagEffect(1, id, RESET_PHASE + PHASE_END, 0, 1)
  	end
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFlagEffect(tp, id) ~= 0
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanSpecialSummon(tp) and Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) > 4
  	end
  end
  function s.filter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) == 0 then
  		return
  	end
  	Duel.ConfirmDecktop(tp, 5)
  	local g = Duel.GetDecktopGroup(tp, 5):Filter(s.filter, nil, e, tp)
  	if g:GetCount() > 0 and Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 1)) then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  		local sg = g:Select(tp, 1, 1, nil)
  		Duel.SpecialSummon(sg, 0, tp, tp, false, false, POS_FACEUP)
  	end
  	Duel.ShuffleDeck(tp)
  end
  
  