name: Sylvan
card_text: 2 Plant monsters
  If this card is Link Summoned: You can choose a number from 1 to 3 and excavate that many cards from the top of your Deck, and if you do, you can Special Summon up to 2 excavated Plant monsters, but they cannot be used as Link Material, also send the remaining cards to the GY. You can target 1 Plant monster in your GY that has a Level; the Levels of monsters this card points to become that monster's Level, until the end of this turn. You can only use each effect of "Sylvan Dancepione" once per turn.
external_ids: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  
lua: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--link summon
  	c:EnableReviveLimit()
  	aux.AddLinkProcedure(c, aux.FilterBoolFunction(Card.IsLinkRace, RACE_PLANT), 2, 2)
  	--deck
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_TOGRAVE + CATEGORY_DECKDES)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetProperty(EFFECT_FLAG_DELAY)
  	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	--change lv
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id + o)
  	e2:SetTarget(s.lvtg)
  	e2:SetOperation(s.lvop)
  	c:RegisterEffect(e2)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_LINK)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanDiscardDeck(tp, 1)
  	end
  end
  function s.spfilter(c, e, tp)
  	return c:IsRace(RACE_PLANT) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if not Duel.IsPlayerCanDiscardDeck(tp, 1) then
  		return
  	end
  	local ct = Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0)
  	if ct == 0 then
  		return
  	end
  	local ac = 1
  	if ct > 1 then
  		if ct > 3 then
  			ct = 3
  		end
  		local t = {}
  		for i = 1, ct do
  			t[i] = i
  		end
  		Duel.Hint(HINT_SELECTMSG, tp, aux.Stringid(id, 2))
  		ac = Duel.AnnounceNumber(tp, table.unpack(t))
  	end
  	Duel.ConfirmDecktop(tp, ac)
  	local g = Duel.GetDecktopGroup(tp, ac)
  	local og = g:Filter(s.spfilter, nil, e, tp)
  	local ft = math.min((Duel.GetLocationCount(tp, LOCATION_MZONE)), 2)
  	if og:GetCount() > 0 and ft > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 3)) then
  		if ft > 1 and Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  			ft = 1
  		end
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  		local sg = og:Select(tp, 1, ft, nil)
  		for tc in aux.Next(sg) do
  			Duel.DisableShuffleCheck()
  			if Duel.SpecialSummonStep(tc, 0, tp, tp, false, false, POS_FACEUP) then
  				local e1 = Effect.CreateEffect(c)
  				e1:SetType(EFFECT_TYPE_SINGLE)
  				e1:SetCode(EFFECT_CANNOT_BE_LINK_MATERIAL)
  				e1:SetValue(1)
  				e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  				tc:RegisterEffect(e1)
  				g:RemoveCard(tc)
  			end
  		end
  		Duel.SpecialSummonComplete()
  	end
  	Duel.DisableShuffleCheck()
  	Duel.SendtoGrave(g, REASON_EFFECT + REASON_REVEAL)
  end
  function s.lvfilter1(c, tp, lg)
  	return c:IsRace(RACE_PLANT) and c:IsLevelAbove(1) and Duel.IsExistingMatchingCard(s.lvfilter2, tp, LOCATION_MZONE, LOCATION_MZONE, 1, nil, lg, c:GetLevel())
  end
  function s.lvfilter2(c, g, lv)
  	return c:IsFaceup() and c:IsLevelAbove(1) and g:IsContains(c) and not c:IsLevel(lv)
  end
  function s.lvtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local lg = e:GetHandler():GetLinkedGroup()
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.lvfilter1(chkc, tp, lg)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.lvfilter1, tp, LOCATION_GRAVE, 0, 1, nil, tp, lg)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.lvfilter1, tp, LOCATION_GRAVE, 0, 1, 1, nil, tp, lg)
  end
  function s.lvop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if not c:IsRelateToEffect(e) or not tc:IsRelateToEffect(e) then
  		return
  	end
  	local lg = c:GetLinkedGroup()
  	local lv = tc:GetLevel()
  	local g = Duel.GetMatchingGroup(s.lvfilter2, tp, LOCATION_MZONE, LOCATION_MZONE, nil, lg, lv)
  	for lc in aux.Next(g) do
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_CHANGE_LEVEL)
  		e1:SetValue(lv)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  		lc:RegisterEffect(e1)
  	end
  end
  
  
source_text: Sylvan
  2 Plant monsters
  If this card is Link Summoned: You can choose a number from 1 to 3 and excavate that many cards from the top of your Deck, and if you do, you can Special Summon up to 2 excavated Plant monsters, but they cannot be used as Link Material, also send the remaining cards to the GY. You can target 1 Plant monster in your GY that has a Level; the Levels of monsters this card points to become that monster's Level, until the end of this turn. You can only use each effect of "Sylvan Dancepione" once per turn.
target_text: -- Sylvan
  local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--link summon
  	c:EnableReviveLimit()
  	aux.AddLinkProcedure(c, aux.FilterBoolFunction(Card.IsLinkRace, RACE_PLANT), 2, 2)
  	--deck
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_TOGRAVE + CATEGORY_DECKDES)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetProperty(EFFECT_FLAG_DELAY)
  	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	--change lv
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id + o)
  	e2:SetTarget(s.lvtg)
  	e2:SetOperation(s.lvop)
  	c:RegisterEffect(e2)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_LINK)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanDiscardDeck(tp, 1)
  	end
  end
  function s.spfilter(c, e, tp)
  	return c:IsRace(RACE_PLANT) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if not Duel.IsPlayerCanDiscardDeck(tp, 1) then
  		return
  	end
  	local ct = Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0)
  	if ct == 0 then
  		return
  	end
  	local ac = 1
  	if ct > 1 then
  		if ct > 3 then
  			ct = 3
  		end
  		local t = {}
  		for i = 1, ct do
  			t[i] = i
  		end
  		Duel.Hint(HINT_SELECTMSG, tp, aux.Stringid(id, 2))
  		ac = Duel.AnnounceNumber(tp, table.unpack(t))
  	end
  	Duel.ConfirmDecktop(tp, ac)
  	local g = Duel.GetDecktopGroup(tp, ac)
  	local og = g:Filter(s.spfilter, nil, e, tp)
  	local ft = math.min((Duel.GetLocationCount(tp, LOCATION_MZONE)), 2)
  	if og:GetCount() > 0 and ft > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 3)) then
  		if ft > 1 and Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  			ft = 1
  		end
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  		local sg = og:Select(tp, 1, ft, nil)
  		for tc in aux.Next(sg) do
  			Duel.DisableShuffleCheck()
  			if Duel.SpecialSummonStep(tc, 0, tp, tp, false, false, POS_FACEUP) then
  				local e1 = Effect.CreateEffect(c)
  				e1:SetType(EFFECT_TYPE_SINGLE)
  				e1:SetCode(EFFECT_CANNOT_BE_LINK_MATERIAL)
  				e1:SetValue(1)
  				e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  				tc:RegisterEffect(e1)
  				g:RemoveCard(tc)
  			end
  		end
  		Duel.SpecialSummonComplete()
  	end
  	Duel.DisableShuffleCheck()
  	Duel.SendtoGrave(g, REASON_EFFECT + REASON_REVEAL)
  end
  function s.lvfilter1(c, tp, lg)
  	return c:IsRace(RACE_PLANT) and c:IsLevelAbove(1) and Duel.IsExistingMatchingCard(s.lvfilter2, tp, LOCATION_MZONE, LOCATION_MZONE, 1, nil, lg, c:GetLevel())
  end
  function s.lvfilter2(c, g, lv)
  	return c:IsFaceup() and c:IsLevelAbove(1) and g:IsContains(c) and not c:IsLevel(lv)
  end
  function s.lvtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local lg = e:GetHandler():GetLinkedGroup()
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.lvfilter1(chkc, tp, lg)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.lvfilter1, tp, LOCATION_GRAVE, 0, 1, nil, tp, lg)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.lvfilter1, tp, LOCATION_GRAVE, 0, 1, 1, nil, tp, lg)
  end
  function s.lvop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if not c:IsRelateToEffect(e) or not tc:IsRelateToEffect(e) then
  		return
  	end
  	local lg = c:GetLinkedGroup()
  	local lv = tc:GetLevel()
  	local g = Duel.GetMatchingGroup(s.lvfilter2, tp, LOCATION_MZONE, LOCATION_MZONE, nil, lg, lv)
  	for lc in aux.Next(g) do
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_CHANGE_LEVEL)
  		e1:SetValue(lv)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  		lc:RegisterEffect(e1)
  	end
  end
  
  