name: Tenyi
card_text: If you control no Effect Monsters: You can Special Summon this card from your hand. If a face-up non-Effect Monster(s) you control is destroyed by battle or card effect: You can banish this card from your hand or GY, then target 1 of those destroyed monsters; Special Summon it, then you can destroy 1 monster your opponent controls. You can only use each effect of "Tenyi Spirit - Shthana" once per turn.
lua: 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_IGNITION)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--special summon and destory
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_DESTROY)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetRange(LOCATION_HAND + LOCATION_GRAVE)
  	e2:SetCode(EVENT_DESTROYED)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCondition(s.descon)
  	e2:SetCost(aux.bfgcost)
  	e2:SetTarget(s.destg)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  end
  function s.spcfilter(c)
  	return c:IsType(TYPE_EFFECT) and c:IsFaceup()
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return not Duel.IsExistingMatchingCard(s.spcfilter, tp, LOCATION_MZONE, 0, 1, nil)
  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 e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, false, false)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, e:GetHandler(), 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) then
  		Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  function s.descfilter(c, tp)
  	return c:IsReason(REASON_BATTLE + REASON_EFFECT)
  		and bit.band(c:GetPreviousTypeOnField(), TYPE_EFFECT) == 0
  		and c:IsPreviousLocation(LOCATION_MZONE)
  		and c:IsPreviousPosition(POS_FACEUP)
  		and c:IsPreviousControler(tp)
  end
  function s.descon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.descfilter, 1, nil, tp)
  end
  function s.tgfilter(c, e, tp)
  	return c:IsCanBeSpecialSummoned(e, 0, tp, false, false) and s.descfilter(c, tp) and c:IsCanBeEffectTarget(e) and c:IsLocation(LOCATION_GRAVE + LOCATION_REMOVED)
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local g = eg:Filter(s.tgfilter, nil, e, tp)
  	if chkc then
  		return eg:IsContains(chkc) and s.tgfilter(chkc, e, tp)
  	end
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and g:GetCount() > 0
  	end
  	local c = nil
  	if g:GetCount() > 1 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  		c = g:Select(tp, 1, 1, nil):GetFirst()
  	else
  		c = g:GetFirst()
  	end
  	Duel.SetTargetCard(c)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, c, 1, 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if
  		tc:IsRelateToEffect(e)
  		and Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP) > 0
  		and Duel.GetFieldGroupCount(tp, 0, LOCATION_MZONE) > 0
  		and Duel.SelectYesNo(tp, aux.Stringid(id, 2))
  	then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  		local g = Duel.SelectMatchingCard(tp, nil, tp, 0, LOCATION_MZONE, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.BreakEffect()
  			Duel.HintSelection(g)
  			Duel.Destroy(g, REASON_EFFECT)
  		end
  	end
  end
  
  
source_text: Tenyi
  If you control no Effect Monsters: You can Special Summon this card from your hand. If a face-up non-Effect Monster(s) you control is destroyed by battle or card effect: You can banish this card from your hand or GY, then target 1 of those destroyed monsters; Special Summon it, then you can destroy 1 monster your opponent controls. You can only use each effect of "Tenyi Spirit - Shthana" once per turn.
target_text: -- Tenyi
  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_IGNITION)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--special summon and destory
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_DESTROY)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetRange(LOCATION_HAND + LOCATION_GRAVE)
  	e2:SetCode(EVENT_DESTROYED)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCondition(s.descon)
  	e2:SetCost(aux.bfgcost)
  	e2:SetTarget(s.destg)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  end
  function s.spcfilter(c)
  	return c:IsType(TYPE_EFFECT) and c:IsFaceup()
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return not Duel.IsExistingMatchingCard(s.spcfilter, tp, LOCATION_MZONE, 0, 1, nil)
  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 e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, false, false)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, e:GetHandler(), 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) then
  		Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  function s.descfilter(c, tp)
  	return c:IsReason(REASON_BATTLE + REASON_EFFECT)
  		and bit.band(c:GetPreviousTypeOnField(), TYPE_EFFECT) == 0
  		and c:IsPreviousLocation(LOCATION_MZONE)
  		and c:IsPreviousPosition(POS_FACEUP)
  		and c:IsPreviousControler(tp)
  end
  function s.descon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.descfilter, 1, nil, tp)
  end
  function s.tgfilter(c, e, tp)
  	return c:IsCanBeSpecialSummoned(e, 0, tp, false, false) and s.descfilter(c, tp) and c:IsCanBeEffectTarget(e) and c:IsLocation(LOCATION_GRAVE + LOCATION_REMOVED)
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local g = eg:Filter(s.tgfilter, nil, e, tp)
  	if chkc then
  		return eg:IsContains(chkc) and s.tgfilter(chkc, e, tp)
  	end
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and g:GetCount() > 0
  	end
  	local c = nil
  	if g:GetCount() > 1 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  		c = g:Select(tp, 1, 1, nil):GetFirst()
  	else
  		c = g:GetFirst()
  	end
  	Duel.SetTargetCard(c)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, c, 1, 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if
  		tc:IsRelateToEffect(e)
  		and Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP) > 0
  		and Duel.GetFieldGroupCount(tp, 0, LOCATION_MZONE) > 0
  		and Duel.SelectYesNo(tp, aux.Stringid(id, 2))
  	then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  		local g = Duel.SelectMatchingCard(tp, nil, tp, 0, LOCATION_MZONE, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.BreakEffect()
  			Duel.HintSelection(g)
  			Duel.Destroy(g, REASON_EFFECT)
  		end
  	end
  end
  
  