name: Tearlaments
card_text: During the Main Phase (Quick Effect): You can Special Summon this card from your hand, and if you do, banish 1 "Kashtira" or "Tearlaments" card from your hand or GY. If this card is Normal or Special Summoned: You can send the top 3 cards of either player's Deck to the GY. If this card is sent to the GY by card effect: You can send the top 2 cards of your Deck to the GY. You can only use each effect of "Tearlaments Kashtira" once per turn.
external_ids: local SETCARD_ID_0 = 0x189 --Kashtira
  
lua: local SETCARD_ID_0 = 0x189 --Kashtira
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_REMOVE)
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_MAIN_END)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--deckdes
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_DECKDES)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCode(EVENT_SUMMON_SUCCESS)
  	e2:SetCountLimit(1, id + o)
  	e2:SetTarget(s.ddtg)
  	e2:SetOperation(s.ddop)
  	c:RegisterEffect(e2)
  	local e3 = e2:Clone()
  	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
  	c:RegisterEffect(e3)
  	--to grave deckdes
  	local e4 = Effect.CreateEffect(c)
  	e4:SetCategory(CATEGORY_DECKDES)
  	e4:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e4:SetCode(EVENT_TO_GRAVE)
  	e4:SetProperty(EFFECT_FLAG_DELAY)
  	e4:SetCountLimit(1, id + o * 2)
  	e4:SetCondition(s.discon)
  	e4:SetTarget(s.distg)
  	e4:SetOperation(s.disop)
  	c:RegisterEffect(e4)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	local ph = Duel.GetCurrentPhase()
  	return ph == PHASE_MAIN1 or ph == PHASE_MAIN2
  end
  function s.rmfilter(c)
  	return c:IsSetCard(SETCARD_ID_0, 0x181) and c:IsAbleToRemove()
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  			and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  			and Duel.IsExistingMatchingCard(s.rmfilter, tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, c)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, c, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_REMOVE, nil, 1, tp, LOCATION_HAND + LOCATION_GRAVE)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) and Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP) > 0 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  		local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.rmfilter), tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, 1, nil)
  		if #g > 0 then
  			Duel.Remove(g, POS_FACEUP, REASON_EFFECT)
  		end
  	end
  end
  function s.ddtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local b1 = Duel.IsPlayerCanDiscardDeck(tp, 3)
  	local b2 = Duel.IsPlayerCanDiscardDeck(1 - tp, 3)
  	if chk == 0 then
  		return b1 or b2
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DECKDES, nil, 0, PLAYER_ALL, 3)
  end
  function s.ddop(e, tp, eg, ep, ev, re, r, rp)
  	local b1 = Duel.IsPlayerCanDiscardDeck(tp, 3)
  	local b2 = Duel.IsPlayerCanDiscardDeck(1 - tp, 3)
  	if not b1 and not b2 then
  		return
  	end
  	local opt = 0
  	if b1 and not b2 then
  		opt = Duel.SelectOption(tp, aux.Stringid(id, 1))
  	end
  	if not b1 and b2 then
  		opt = Duel.SelectOption(tp, aux.Stringid(id, 2)) + 1
  	end
  	if b1 and b2 then
  		opt = Duel.SelectOption(tp, aux.Stringid(id, 1), aux.Stringid(id, 2))
  	end
  	if opt == 0 then
  		Duel.DiscardDeck(tp, 3, REASON_EFFECT)
  	else
  		Duel.DiscardDeck(1 - tp, 3, REASON_EFFECT)
  	end
  end
  function s.discon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsReason(REASON_EFFECT)
  end
  function s.distg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanDiscardDeck(tp, 2)
  	end
  	Duel.SetTargetPlayer(tp)
  	Duel.SetTargetParam(2)
  	Duel.SetOperationInfo(0, CATEGORY_DECKDES, nil, 0, tp, 2)
  end
  function s.disop(e, tp, eg, ep, ev, re, r, rp)
  	local p, d = Duel.GetChainInfo(0, CHAININFO_TARGET_PLAYER, CHAININFO_TARGET_PARAM)
  	Duel.DiscardDeck(p, d, REASON_EFFECT)
  end
  
  
source_text: Tearlaments
  During the Main Phase (Quick Effect): You can Special Summon this card from your hand, and if you do, banish 1 "Kashtira" or "Tearlaments" card from your hand or GY. If this card is Normal or Special Summoned: You can send the top 3 cards of either player's Deck to the GY. If this card is sent to the GY by card effect: You can send the top 2 cards of your Deck to the GY. You can only use each effect of "Tearlaments Kashtira" once per turn.
target_text: -- Tearlaments
  local SETCARD_ID_0 = 0x189 --Kashtira
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_REMOVE)
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_MAIN_END)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--deckdes
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_DECKDES)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCode(EVENT_SUMMON_SUCCESS)
  	e2:SetCountLimit(1, id + o)
  	e2:SetTarget(s.ddtg)
  	e2:SetOperation(s.ddop)
  	c:RegisterEffect(e2)
  	local e3 = e2:Clone()
  	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
  	c:RegisterEffect(e3)
  	--to grave deckdes
  	local e4 = Effect.CreateEffect(c)
  	e4:SetCategory(CATEGORY_DECKDES)
  	e4:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e4:SetCode(EVENT_TO_GRAVE)
  	e4:SetProperty(EFFECT_FLAG_DELAY)
  	e4:SetCountLimit(1, id + o * 2)
  	e4:SetCondition(s.discon)
  	e4:SetTarget(s.distg)
  	e4:SetOperation(s.disop)
  	c:RegisterEffect(e4)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	local ph = Duel.GetCurrentPhase()
  	return ph == PHASE_MAIN1 or ph == PHASE_MAIN2
  end
  function s.rmfilter(c)
  	return c:IsSetCard(SETCARD_ID_0, 0x181) and c:IsAbleToRemove()
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  			and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  			and Duel.IsExistingMatchingCard(s.rmfilter, tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, c)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, c, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_REMOVE, nil, 1, tp, LOCATION_HAND + LOCATION_GRAVE)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) and Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP) > 0 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  		local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.rmfilter), tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, 1, nil)
  		if #g > 0 then
  			Duel.Remove(g, POS_FACEUP, REASON_EFFECT)
  		end
  	end
  end
  function s.ddtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local b1 = Duel.IsPlayerCanDiscardDeck(tp, 3)
  	local b2 = Duel.IsPlayerCanDiscardDeck(1 - tp, 3)
  	if chk == 0 then
  		return b1 or b2
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DECKDES, nil, 0, PLAYER_ALL, 3)
  end
  function s.ddop(e, tp, eg, ep, ev, re, r, rp)
  	local b1 = Duel.IsPlayerCanDiscardDeck(tp, 3)
  	local b2 = Duel.IsPlayerCanDiscardDeck(1 - tp, 3)
  	if not b1 and not b2 then
  		return
  	end
  	local opt = 0
  	if b1 and not b2 then
  		opt = Duel.SelectOption(tp, aux.Stringid(id, 1))
  	end
  	if not b1 and b2 then
  		opt = Duel.SelectOption(tp, aux.Stringid(id, 2)) + 1
  	end
  	if b1 and b2 then
  		opt = Duel.SelectOption(tp, aux.Stringid(id, 1), aux.Stringid(id, 2))
  	end
  	if opt == 0 then
  		Duel.DiscardDeck(tp, 3, REASON_EFFECT)
  	else
  		Duel.DiscardDeck(1 - tp, 3, REASON_EFFECT)
  	end
  end
  function s.discon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsReason(REASON_EFFECT)
  end
  function s.distg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanDiscardDeck(tp, 2)
  	end
  	Duel.SetTargetPlayer(tp)
  	Duel.SetTargetParam(2)
  	Duel.SetOperationInfo(0, CATEGORY_DECKDES, nil, 0, tp, 2)
  end
  function s.disop(e, tp, eg, ep, ev, re, r, rp)
  	local p, d = Duel.GetChainInfo(0, CHAININFO_TARGET_PLAYER, CHAININFO_TARGET_PARAM)
  	Duel.DiscardDeck(p, d, REASON_EFFECT)
  end
  
  