name: Sunavalon
card_text: 2+ Link Monsters
  If this card is Link Summoned: You can add 1 "Sunavalon" Spell/Trap from your Deck to your hand. This card cannot be destroyed by your opponent's card effects, and cannot be targeted for attacks, but does not prevent your opponent from attacking you directly. Once per turn: You can Tribute 1 Link Monster this card points to; destroy any number of cards your opponent controls, up to that monster's Link Rating.
external_ids: local SETCARD_ID_0 = 0x2158 --Sunavalon
  
lua: local SETCARD_ID_0 = 0x2158 --Sunavalon
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--link summon
  	aux.AddLinkProcedure(c, s.mfilter, 2, 99)
  	c:EnableReviveLimit()
  	--search
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetProperty(EFFECT_FLAG_DELAY)
  	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e1:SetCondition(s.thcon)
  	e1:SetTarget(s.thtg)
  	e1:SetOperation(s.thop)
  	c:RegisterEffect(e1)
  	--cannot be battle target and destroyed
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_SINGLE)
  	e2:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e2:SetCode(EFFECT_IGNORE_BATTLE_TARGET)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetValue(1)
  	c:RegisterEffect(e2)
  	local e4 = e2:Clone()
  	e4:SetCode(EFFECT_INDESTRUCTABLE_EFFECT)
  	e4:SetValue(aux.indoval)
  	c:RegisterEffect(e4)
  	--destroy
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_DESTROY)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCountLimit(1)
  	e3:SetCost(s.cost)
  	e3:SetTarget(s.destg)
  	e3:SetOperation(s.desop)
  	c:RegisterEffect(e3)
  end
  function s.mfilter(c)
  	return c:IsLinkType(TYPE_LINK)
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	return c:IsSummonType(SUMMON_TYPE_LINK)
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SPELL + TYPE_TRAP) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if #g > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	e:SetLabel(1)
  	return true
  end
  function s.desfilter(c, tc, ec)
  	return c:GetEquipTarget() ~= tc and c ~= ec
  end
  function s.costfilter(c, ec, tp, g)
  	return Duel.IsExistingTarget(s.desfilter, tp, 0, LOCATION_ONFIELD, 1, c, c, ec) and g:IsContains(c) and c:IsType(TYPE_LINK) and c:IsLinkAbove(1)
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	local lg = c:GetLinkedGroup()
  	local ct = 0
  	if chk == 0 then
  		if e:GetLabel() == 1 then
  			e:SetLabel(0)
  			return Duel.CheckReleaseGroup(tp, s.costfilter, 1, c, c, tp, lg)
  		else
  			return Duel.IsExistingMatchingCard(nil, tp, 0, LOCATION_ONFIELD, 1, nil)
  		end
  	end
  	if e:GetLabel() == 1 then
  		e:SetLabel(0)
  		local g = Duel.SelectReleaseGroup(tp, s.costfilter, 1, 1, c, c, tp, lg)
  		ct = g:GetFirst():GetLink()
  		Duel.Release(g, REASON_COST)
  	end
  	e:SetValue(ct)
  	local sg = Duel.GetMatchingGroup(nil, tp, 0, LOCATION_ONFIELD, nil)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, sg, 1, 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local ct = e:GetValue()
  	local sg = Duel.GetMatchingGroup(nil, tp, 0, LOCATION_ONFIELD, nil)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  	local g = sg:Select(tp, 1, ct, nil)
  	if #g > 0 then
  		Duel.HintSelection(g)
  		Duel.Destroy(g, REASON_EFFECT)
  	end
  end
  
  
source_text: Sunavalon
  2+ Link Monsters
  If this card is Link Summoned: You can add 1 "Sunavalon" Spell/Trap from your Deck to your hand. This card cannot be destroyed by your opponent's card effects, and cannot be targeted for attacks, but does not prevent your opponent from attacking you directly. Once per turn: You can Tribute 1 Link Monster this card points to; destroy any number of cards your opponent controls, up to that monster's Link Rating.
target_text: -- Sunavalon
  local SETCARD_ID_0 = 0x2158 --Sunavalon
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--link summon
  	aux.AddLinkProcedure(c, s.mfilter, 2, 99)
  	c:EnableReviveLimit()
  	--search
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetProperty(EFFECT_FLAG_DELAY)
  	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e1:SetCondition(s.thcon)
  	e1:SetTarget(s.thtg)
  	e1:SetOperation(s.thop)
  	c:RegisterEffect(e1)
  	--cannot be battle target and destroyed
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_SINGLE)
  	e2:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e2:SetCode(EFFECT_IGNORE_BATTLE_TARGET)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetValue(1)
  	c:RegisterEffect(e2)
  	local e4 = e2:Clone()
  	e4:SetCode(EFFECT_INDESTRUCTABLE_EFFECT)
  	e4:SetValue(aux.indoval)
  	c:RegisterEffect(e4)
  	--destroy
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_DESTROY)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCountLimit(1)
  	e3:SetCost(s.cost)
  	e3:SetTarget(s.destg)
  	e3:SetOperation(s.desop)
  	c:RegisterEffect(e3)
  end
  function s.mfilter(c)
  	return c:IsLinkType(TYPE_LINK)
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	return c:IsSummonType(SUMMON_TYPE_LINK)
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SPELL + TYPE_TRAP) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if #g > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	e:SetLabel(1)
  	return true
  end
  function s.desfilter(c, tc, ec)
  	return c:GetEquipTarget() ~= tc and c ~= ec
  end
  function s.costfilter(c, ec, tp, g)
  	return Duel.IsExistingTarget(s.desfilter, tp, 0, LOCATION_ONFIELD, 1, c, c, ec) and g:IsContains(c) and c:IsType(TYPE_LINK) and c:IsLinkAbove(1)
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	local lg = c:GetLinkedGroup()
  	local ct = 0
  	if chk == 0 then
  		if e:GetLabel() == 1 then
  			e:SetLabel(0)
  			return Duel.CheckReleaseGroup(tp, s.costfilter, 1, c, c, tp, lg)
  		else
  			return Duel.IsExistingMatchingCard(nil, tp, 0, LOCATION_ONFIELD, 1, nil)
  		end
  	end
  	if e:GetLabel() == 1 then
  		e:SetLabel(0)
  		local g = Duel.SelectReleaseGroup(tp, s.costfilter, 1, 1, c, c, tp, lg)
  		ct = g:GetFirst():GetLink()
  		Duel.Release(g, REASON_COST)
  	end
  	e:SetValue(ct)
  	local sg = Duel.GetMatchingGroup(nil, tp, 0, LOCATION_ONFIELD, nil)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, sg, 1, 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local ct = e:GetValue()
  	local sg = Duel.GetMatchingGroup(nil, tp, 0, LOCATION_ONFIELD, nil)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  	local g = sg:Select(tp, 1, ct, nil)
  	if #g > 0 then
  		Duel.HintSelection(g)
  		Duel.Destroy(g, REASON_EFFECT)
  	end
  end
  
  