name: Zexal
card_text: If a face-up Xyz Monster(s) you control is destroyed by battle or an opponent's card effect: Pay LP so that you only have 10 left; Special Summon 1 "Utopia" monster from your GY, and if you do, take 1 card from your Deck and place it on top of your Deck. That Special Summoned monster's ATK becomes doubled, also it cannot be destroyed by battle, except by battle with a "Number" monster, nor by card effects.
external_ids: local SETCARD_ID_0 = 0x107f --Utopia
  local SETCARD_ID_1 = 0x48 --Number
  
lua: local SETCARD_ID_0 = 0x107f --Utopia
  local SETCARD_ID_1 = 0x48 --Number
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY)
  	e1:SetCode(EVENT_DESTROYED)
  	e1:SetCondition(s.spcon)
  	e1:SetCost(s.spcost)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  end
  function s.cfilter(c, tp)
  	return c:IsPreviousControler(tp) and c:IsPreviousLocation(LOCATION_MZONE) and c:IsType(TYPE_XYZ) and (c:IsReason(REASON_BATTLE) or c:IsReason(REASON_EFFECT) and c:GetReasonPlayer() == 1 - tp)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, e:GetHandler(), tp)
  end
  function s.spcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local lp = Duel.GetLP(tp)
  	if chk == 0 then
  		return Duel.CheckLPCost(tp, lp - 10)
  	end
  	Duel.PayLPCost(tp, lp - 10)
  end
  function s.spfilter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_1) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  			and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
  			and Duel.IsExistingMatchingCard(aux.TRUE, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_GRAVE)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	local tc = g:GetFirst()
  	local ss = false
  	if tc and Duel.SpecialSummonStep(tc, 0, tp, tp, false, false, POS_FACEUP) then
  		ss = true
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_SET_ATTACK)
  		e1:SetValue(tc:GetAttack() * 2)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  		tc:RegisterEffect(e1)
  		local e2 = e1:Clone()
  		e2:SetCode(EFFECT_INDESTRUCTABLE_EFFECT)
  		e2:SetValue(1)
  		tc:RegisterEffect(e2)
  		local e3 = e2:Clone()
  		e3:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
  		e3:SetValue(s.indval)
  		tc:RegisterEffect(e3)
  		tc:RegisterFlagEffect(0, RESET_EVENT + RESETS_STANDARD, EFFECT_FLAG_CLIENT_HINT, 1, 0, aux.Stringid(id, 1))
  	end
  	Duel.SpecialSummonComplete()
  	if ss then
  		--to deck top
  		Duel.Hint(HINT_SELECTMSG, tp, aux.Stringid(id, 2))
  		local g2 = Duel.SelectMatchingCard(tp, aux.TRUE, tp, LOCATION_DECK, 0, 1, 1, nil)
  		local tc2 = g2:GetFirst()
  		if tc2 then
  			Duel.ShuffleDeck(tp)
  			Duel.MoveSequence(tc2, SEQ_DECKTOP)
  			Duel.ConfirmDecktop(tp, 1)
  		end
  	end
  end
  function s.indval(e, c)
  	return not c:IsSetCard(SETCARD_ID_1)
  end
  
  
source_text: Zexal
  If a face-up Xyz Monster(s) you control is destroyed by battle or an opponent's card effect: Pay LP so that you only have 10 left; Special Summon 1 "Utopia" monster from your GY, and if you do, take 1 card from your Deck and place it on top of your Deck. That Special Summoned monster's ATK becomes doubled, also it cannot be destroyed by battle, except by battle with a "Number" monster, nor by card effects.
target_text: -- Zexal
  local SETCARD_ID_0 = 0x107f --Utopia
  local SETCARD_ID_1 = 0x48 --Number
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY)
  	e1:SetCode(EVENT_DESTROYED)
  	e1:SetCondition(s.spcon)
  	e1:SetCost(s.spcost)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  end
  function s.cfilter(c, tp)
  	return c:IsPreviousControler(tp) and c:IsPreviousLocation(LOCATION_MZONE) and c:IsType(TYPE_XYZ) and (c:IsReason(REASON_BATTLE) or c:IsReason(REASON_EFFECT) and c:GetReasonPlayer() == 1 - tp)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, e:GetHandler(), tp)
  end
  function s.spcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local lp = Duel.GetLP(tp)
  	if chk == 0 then
  		return Duel.CheckLPCost(tp, lp - 10)
  	end
  	Duel.PayLPCost(tp, lp - 10)
  end
  function s.spfilter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_1) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  			and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
  			and Duel.IsExistingMatchingCard(aux.TRUE, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_GRAVE)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	local tc = g:GetFirst()
  	local ss = false
  	if tc and Duel.SpecialSummonStep(tc, 0, tp, tp, false, false, POS_FACEUP) then
  		ss = true
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_SET_ATTACK)
  		e1:SetValue(tc:GetAttack() * 2)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  		tc:RegisterEffect(e1)
  		local e2 = e1:Clone()
  		e2:SetCode(EFFECT_INDESTRUCTABLE_EFFECT)
  		e2:SetValue(1)
  		tc:RegisterEffect(e2)
  		local e3 = e2:Clone()
  		e3:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
  		e3:SetValue(s.indval)
  		tc:RegisterEffect(e3)
  		tc:RegisterFlagEffect(0, RESET_EVENT + RESETS_STANDARD, EFFECT_FLAG_CLIENT_HINT, 1, 0, aux.Stringid(id, 1))
  	end
  	Duel.SpecialSummonComplete()
  	if ss then
  		--to deck top
  		Duel.Hint(HINT_SELECTMSG, tp, aux.Stringid(id, 2))
  		local g2 = Duel.SelectMatchingCard(tp, aux.TRUE, tp, LOCATION_DECK, 0, 1, 1, nil)
  		local tc2 = g2:GetFirst()
  		if tc2 then
  			Duel.ShuffleDeck(tp)
  			Duel.MoveSequence(tc2, SEQ_DECKTOP)
  			Duel.ConfirmDecktop(tp, 1)
  		end
  	end
  end
  function s.indval(e, c)
  	return not c:IsSetCard(SETCARD_ID_1)
  end
  
  