name: The
card_text: Activate 1 "The Sanctuary in the Sky" directly from your Deck, or add 1 monster that mentions "The Sanctuary in the Sky" from your Deck to your hand, then, if "The Sanctuary in the Sky" is on the field or in either GY, you can gain 500 LP for each "Hyperion" and "The Agent" monster you control. If your monster that mentions "The Sanctuary in the Sky" would be destroyed by battle, you can banish this card from your GY instead. You can only use each effect of "The Sacred Waters in the Sky" once per turn.
external_ids: local CARD_ID_0 = 4179255 --Ancient Pixie Dragon
  local CARD_ID_2 = 56433456 --The Sanctuary in the Sky
  local SETCARD_ID_0 = 0x44 --The
  
lua: local CARD_ID_0 = 4179255 --Ancient Pixie Dragon
  local CARD_ID_2 = 56433456 --The Sanctuary in the Sky
  local SETCARD_ID_0 = 0x44 --The
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_2)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCountLimit(1, id)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--destroy replace
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetCode(EFFECT_DESTROY_REPLACE)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetTarget(s.reptg)
  	e2:SetValue(s.repval)
  	e2:SetOperation(s.repop)
  	c:RegisterEffect(e2)
  end
  function s.actfilter(c, tp)
  	return c:IsCode(CARD_ID_2) and c:GetActivateEffect():IsActivatable(tp, true, true)
  end
  function s.thfilter(c)
  	return c:IsType(TYPE_MONSTER) and aux.IsCodeListed(c, CARD_ID_2) and c:IsAbleToHand()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local b1 = Duel.IsExistingMatchingCard(s.actfilter, tp, LOCATION_DECK, 0, 1, nil, tp)
  	local b2 = Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	if chk == 0 then
  		return b1 or b2
  	end
  end
  function s.recfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0, 0x16f)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local b1 = Duel.IsExistingMatchingCard(s.actfilter, tp, LOCATION_DECK, 0, 1, nil, tp)
  	local b2 = Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	local off = 1
  	local ops, opval = {}, {}
  	if b1 then
  		ops[off] = aux.Stringid(id, 0)
  		opval[off] = 0
  		off = off + 1
  	end
  	if b2 then
  		ops[off] = aux.Stringid(id, 1)
  		opval[off] = 1
  		off = off + 1
  	end
  	local op = Duel.SelectOption(tp, table.unpack(ops)) + 1
  	local sel = opval[op]
  	local resolve = false
  	if sel == 0 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_OPERATECARD)
  		local g = Duel.SelectMatchingCard(tp, s.actfilter, tp, LOCATION_DECK, 0, 1, 1, nil, tp)
  		local tc = g:GetFirst()
  		if tc then
  			local te = tc:GetActivateEffect()
  			local fc = Duel.GetFieldCard(tp, LOCATION_FZONE, 0)
  			if fc then
  				Duel.SendtoGrave(fc, REASON_RULE)
  				Duel.BreakEffect()
  			end
  			Duel.MoveToField(tc, tp, tp, LOCATION_FZONE, POS_FACEUP, true)
  			te:UseCountLimit(tp, 1, true)
  			local tep = tc:GetControler()
  			local cost = te:GetCost()
  			if cost then
  				cost(te, tep, eg, ep, ev, re, r, rp, 1)
  			end
  			Duel.RaiseEvent(tc, CARD_ID_0, te, 0, tp, tp, Duel.GetCurrentChain())
  			resolve = true
  		end
  	else
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.SendtoHand(g, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, g)
  			resolve = true
  		end
  	end
  	local check = Duel.IsEnvironment(CARD_ID_2, PLAYER_ALL, LOCATION_ONFIELD + LOCATION_GRAVE)
  	local ct = Duel.GetMatchingGroupCount(s.recfilter, tp, LOCATION_MZONE, 0, nil)
  	if resolve and check and ct > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 2)) then
  		Duel.BreakEffect()
  		Duel.Recover(tp, ct * 500, REASON_EFFECT)
  	end
  end
  function s.repfilter(c, tp)
  	return c:IsFaceup() and aux.IsCodeListed(c, CARD_ID_2) and c:IsLocation(LOCATION_MZONE) and c:IsControler(tp) and c:IsReason(REASON_BATTLE) and not c:IsReason(REASON_REPLACE)
  end
  function s.reptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToRemove() and eg:IsExists(s.repfilter, 1, nil, tp)
  	end
  	return Duel.SelectEffectYesNo(tp, e:GetHandler(), 96)
  end
  function s.repval(e, c)
  	return s.repfilter(c, e:GetHandlerPlayer())
  end
  function s.repop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Remove(e:GetHandler(), POS_FACEUP, REASON_EFFECT)
  end
  
  
source_text: The
  Activate 1 "The Sanctuary in the Sky" directly from your Deck, or add 1 monster that mentions "The Sanctuary in the Sky" from your Deck to your hand, then, if "The Sanctuary in the Sky" is on the field or in either GY, you can gain 500 LP for each "Hyperion" and "The Agent" monster you control. If your monster that mentions "The Sanctuary in the Sky" would be destroyed by battle, you can banish this card from your GY instead. You can only use each effect of "The Sacred Waters in the Sky" once per turn.
target_text: -- The
  local CARD_ID_0 = 4179255 --Ancient Pixie Dragon
  local CARD_ID_2 = 56433456 --The Sanctuary in the Sky
  local SETCARD_ID_0 = 0x44 --The
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_2)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCountLimit(1, id)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--destroy replace
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetCode(EFFECT_DESTROY_REPLACE)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetTarget(s.reptg)
  	e2:SetValue(s.repval)
  	e2:SetOperation(s.repop)
  	c:RegisterEffect(e2)
  end
  function s.actfilter(c, tp)
  	return c:IsCode(CARD_ID_2) and c:GetActivateEffect():IsActivatable(tp, true, true)
  end
  function s.thfilter(c)
  	return c:IsType(TYPE_MONSTER) and aux.IsCodeListed(c, CARD_ID_2) and c:IsAbleToHand()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local b1 = Duel.IsExistingMatchingCard(s.actfilter, tp, LOCATION_DECK, 0, 1, nil, tp)
  	local b2 = Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	if chk == 0 then
  		return b1 or b2
  	end
  end
  function s.recfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0, 0x16f)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local b1 = Duel.IsExistingMatchingCard(s.actfilter, tp, LOCATION_DECK, 0, 1, nil, tp)
  	local b2 = Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	local off = 1
  	local ops, opval = {}, {}
  	if b1 then
  		ops[off] = aux.Stringid(id, 0)
  		opval[off] = 0
  		off = off + 1
  	end
  	if b2 then
  		ops[off] = aux.Stringid(id, 1)
  		opval[off] = 1
  		off = off + 1
  	end
  	local op = Duel.SelectOption(tp, table.unpack(ops)) + 1
  	local sel = opval[op]
  	local resolve = false
  	if sel == 0 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_OPERATECARD)
  		local g = Duel.SelectMatchingCard(tp, s.actfilter, tp, LOCATION_DECK, 0, 1, 1, nil, tp)
  		local tc = g:GetFirst()
  		if tc then
  			local te = tc:GetActivateEffect()
  			local fc = Duel.GetFieldCard(tp, LOCATION_FZONE, 0)
  			if fc then
  				Duel.SendtoGrave(fc, REASON_RULE)
  				Duel.BreakEffect()
  			end
  			Duel.MoveToField(tc, tp, tp, LOCATION_FZONE, POS_FACEUP, true)
  			te:UseCountLimit(tp, 1, true)
  			local tep = tc:GetControler()
  			local cost = te:GetCost()
  			if cost then
  				cost(te, tep, eg, ep, ev, re, r, rp, 1)
  			end
  			Duel.RaiseEvent(tc, CARD_ID_0, te, 0, tp, tp, Duel.GetCurrentChain())
  			resolve = true
  		end
  	else
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.SendtoHand(g, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, g)
  			resolve = true
  		end
  	end
  	local check = Duel.IsEnvironment(CARD_ID_2, PLAYER_ALL, LOCATION_ONFIELD + LOCATION_GRAVE)
  	local ct = Duel.GetMatchingGroupCount(s.recfilter, tp, LOCATION_MZONE, 0, nil)
  	if resolve and check and ct > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 2)) then
  		Duel.BreakEffect()
  		Duel.Recover(tp, ct * 500, REASON_EFFECT)
  	end
  end
  function s.repfilter(c, tp)
  	return c:IsFaceup() and aux.IsCodeListed(c, CARD_ID_2) and c:IsLocation(LOCATION_MZONE) and c:IsControler(tp) and c:IsReason(REASON_BATTLE) and not c:IsReason(REASON_REPLACE)
  end
  function s.reptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToRemove() and eg:IsExists(s.repfilter, 1, nil, tp)
  	end
  	return Duel.SelectEffectYesNo(tp, e:GetHandler(), 96)
  end
  function s.repval(e, c)
  	return s.repfilter(c, e:GetHandlerPlayer())
  end
  function s.repop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Remove(e:GetHandler(), POS_FACEUP, REASON_EFFECT)
  end
  
  