name: Tenyinfinity
card_text: When this card is activated: You can add 1 "Vessel for the Dragon Cycle" or "Heavenly Dragon Circle" from your Deck to your hand. You can Tribute 1 Wyrm monster, then target 1 monster on the field; destroy it. You can only use this effect of "Tenyinfinity" once per turn. Once per turn, during your End Phase: You can send this card to the GY, and if you do, place 1 "Flawless Perfection of the Tenyi" from your Deck or GY, face-up in your Field Zone. You can only activate 1 "Tenyinfinity" per turn.
external_ids: local CARD_ID_0 = 39730727 --Flawless Perfection of the Tenyi
  local CARD_ID_1 = 51684157 --Heavenly Dragon Circle
  local CARD_ID_2 = 65124425 --Vessel for the Dragon Cycle
  
lua: local CARD_ID_0 = 39730727 --Flawless Perfection of the Tenyi
  local CARD_ID_1 = 51684157 --Heavenly Dragon Circle
  local CARD_ID_2 = 65124425 --Vessel for the Dragon Cycle
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_1, CARD_ID_2, CARD_ID_0)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--destroy
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_DESTROY)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_FZONE)
  	e2:SetCountLimit(1, id + o)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetCost(s.descost)
  	e2:SetTarget(s.destg)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  	--set
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 2))
  	e3:SetCategory(CATEGORY_TOGRAVE)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e3:SetCode(EVENT_PHASE + PHASE_END)
  	e3:SetRange(LOCATION_FZONE)
  	e3:SetCountLimit(1)
  	e3:SetCondition(s.setcon)
  	e3:SetTarget(s.settg)
  	e3:SetOperation(s.setop)
  	c:RegisterEffect(e3)
  end
  function s.thfilter(c)
  	return c:IsCode(CARD_ID_1, CARD_ID_2) and c:IsAbleToHand()
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(s.thfilter, tp, LOCATION_DECK, 0, nil)
  	if g:GetCount() > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 3)) then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local sg = g:Select(tp, 1, 1, nil)
  		Duel.SendtoHand(sg, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, sg)
  	end
  end
  function s.cfilter(c, tp)
  	return c:IsRace(RACE_WYRM) and (c:IsControler(tp) or c:IsFaceup()) and Duel.IsExistingTarget(nil, tp, LOCATION_MZONE, LOCATION_MZONE, 1, c)
  end
  function s.descost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.CheckReleaseGroup(tp, s.cfilter, 1, nil, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_RELEASE)
  	local g = Duel.SelectReleaseGroup(tp, s.cfilter, 1, 1, nil, tp)
  	Duel.Release(g, REASON_COST)
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(nil, tp, LOCATION_MZONE, LOCATION_MZONE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  	local g = Duel.SelectTarget(tp, nil, tp, LOCATION_MZONE, LOCATION_MZONE, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, g, 1, 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and tc:IsType(TYPE_MONSTER) then
  		Duel.Destroy(tc, REASON_EFFECT)
  	end
  end
  function s.setcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() == tp
  end
  function s.setfilter(c)
  	return c:IsCode(CARD_ID_0) and not c:IsForbidden()
  end
  function s.settg(e, tp, eg, ep, ev, re, r, rp, chk, chkc, exc)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToGrave() and Duel.IsExistingMatchingCard(s.setfilter, tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, e:GetHandler(), 1, 0, 0)
  end
  function s.setop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) and Duel.SendtoGrave(c, REASON_EFFECT) ~= 0 and c:IsLocation(LOCATION_GRAVE) then
  		local sc = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.setfilter), tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, 1, nil):GetFirst()
  		if sc then
  			local fc = Duel.GetFieldCard(tp, LOCATION_SZONE, 5)
  			if fc then
  				Duel.SendtoGrave(fc, REASON_RULE)
  				Duel.BreakEffect()
  			end
  			Duel.MoveToField(sc, tp, tp, LOCATION_FZONE, POS_FACEUP, true)
  		end
  	end
  end
  
  
source_text: Tenyinfinity
  When this card is activated: You can add 1 "Vessel for the Dragon Cycle" or "Heavenly Dragon Circle" from your Deck to your hand. You can Tribute 1 Wyrm monster, then target 1 monster on the field; destroy it. You can only use this effect of "Tenyinfinity" once per turn. Once per turn, during your End Phase: You can send this card to the GY, and if you do, place 1 "Flawless Perfection of the Tenyi" from your Deck or GY, face-up in your Field Zone. You can only activate 1 "Tenyinfinity" per turn.
target_text: -- Tenyinfinity
  local CARD_ID_0 = 39730727 --Flawless Perfection of the Tenyi
  local CARD_ID_1 = 51684157 --Heavenly Dragon Circle
  local CARD_ID_2 = 65124425 --Vessel for the Dragon Cycle
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_1, CARD_ID_2, CARD_ID_0)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--destroy
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_DESTROY)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_FZONE)
  	e2:SetCountLimit(1, id + o)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetCost(s.descost)
  	e2:SetTarget(s.destg)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  	--set
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 2))
  	e3:SetCategory(CATEGORY_TOGRAVE)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e3:SetCode(EVENT_PHASE + PHASE_END)
  	e3:SetRange(LOCATION_FZONE)
  	e3:SetCountLimit(1)
  	e3:SetCondition(s.setcon)
  	e3:SetTarget(s.settg)
  	e3:SetOperation(s.setop)
  	c:RegisterEffect(e3)
  end
  function s.thfilter(c)
  	return c:IsCode(CARD_ID_1, CARD_ID_2) and c:IsAbleToHand()
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(s.thfilter, tp, LOCATION_DECK, 0, nil)
  	if g:GetCount() > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 3)) then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local sg = g:Select(tp, 1, 1, nil)
  		Duel.SendtoHand(sg, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, sg)
  	end
  end
  function s.cfilter(c, tp)
  	return c:IsRace(RACE_WYRM) and (c:IsControler(tp) or c:IsFaceup()) and Duel.IsExistingTarget(nil, tp, LOCATION_MZONE, LOCATION_MZONE, 1, c)
  end
  function s.descost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.CheckReleaseGroup(tp, s.cfilter, 1, nil, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_RELEASE)
  	local g = Duel.SelectReleaseGroup(tp, s.cfilter, 1, 1, nil, tp)
  	Duel.Release(g, REASON_COST)
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(nil, tp, LOCATION_MZONE, LOCATION_MZONE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  	local g = Duel.SelectTarget(tp, nil, tp, LOCATION_MZONE, LOCATION_MZONE, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, g, 1, 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and tc:IsType(TYPE_MONSTER) then
  		Duel.Destroy(tc, REASON_EFFECT)
  	end
  end
  function s.setcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() == tp
  end
  function s.setfilter(c)
  	return c:IsCode(CARD_ID_0) and not c:IsForbidden()
  end
  function s.settg(e, tp, eg, ep, ev, re, r, rp, chk, chkc, exc)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToGrave() and Duel.IsExistingMatchingCard(s.setfilter, tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, e:GetHandler(), 1, 0, 0)
  end
  function s.setop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) and Duel.SendtoGrave(c, REASON_EFFECT) ~= 0 and c:IsLocation(LOCATION_GRAVE) then
  		local sc = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.setfilter), tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, 1, nil):GetFirst()
  		if sc then
  			local fc = Duel.GetFieldCard(tp, LOCATION_SZONE, 5)
  			if fc then
  				Duel.SendtoGrave(fc, REASON_RULE)
  				Duel.BreakEffect()
  			end
  			Duel.MoveToField(sc, tp, tp, LOCATION_FZONE, POS_FACEUP, true)
  		end
  	end
  end
  
  