name: Kashtira
card_text: If you control no monsters, you can Special Summon this card (from your hand). You can only use each of the following effects of "Kashtira Ogre" once per turn. During your Main Phase: You can add 1 "Kashtira" Trap from your Deck to your hand. When this card declares an attack, or if your opponent activates a monster effect (except during the Damage Step): You can excavate up to 5 cards from the top of your opponent's Deck, banish 1 excavated card, face-down, and place the rest on the top of the Deck in the same order.
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)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_SPSUMMON_PROC)
  	e1:SetProperty(EFFECT_FLAG_UNCOPYABLE)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCondition(s.spcon)
  	c:RegisterEffect(e1)
  	--tohand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  	--remove when attack
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 2))
  	e3:SetCategory(CATEGORY_REMOVE)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e3:SetCode(EVENT_ATTACK_ANNOUNCE)
  	e3:SetCountLimit(1, id + 1)
  	e3:SetTarget(s.rmtg)
  	e3:SetOperation(s.rmop)
  	c:RegisterEffect(e3)
  	--remove when chaining
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 2))
  	e4:SetCategory(CATEGORY_REMOVE)
  	e4:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e4:SetCode(EVENT_CHAINING)
  	e4:SetProperty(EFFECT_FLAG_DELAY)
  	e4:SetRange(LOCATION_MZONE)
  	e4:SetCountLimit(1, id + 1)
  	e4:SetCondition(s.rmcon)
  	e4:SetTarget(s.rmtg2)
  	e4:SetOperation(s.rmop)
  	c:RegisterEffect(e4)
  end
  function s.spcon(e, c)
  	if c == nil then
  		return true
  	end
  	return Duel.GetFieldGroupCount(c:GetControler(), LOCATION_MZONE, 0) == 0 and Duel.GetLocationCount(c:GetControler(), LOCATION_MZONE) > 0
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(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.rmfilter(c, tp)
  	return c:IsAbleToRemove(tp, POS_FACEDOWN)
  end
  function s.rmtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local g = Duel.GetDecktopGroup(1 - tp, 1)
  	if chk == 0 then
  		return #g > 0 and g:GetFirst():IsAbleToRemove(tp, POS_FACEDOWN)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_REMOVE, nil, 1, 1 - tp, LOCATION_DECK)
  end
  function s.rmop(e, tp, eg, ep, ev, re, r, rp)
  	if not Duel.IsPlayerCanRemove(tp) then
  		return
  	end
  	local ct = Duel.GetFieldGroupCount(tp, 0, LOCATION_DECK)
  	if ct > 5 then
  		ct = 5
  	end
  	if ct > 1 then
  		local tbl = {}
  		for i = 1, ct do
  			table.insert(tbl, i)
  		end
  		Duel.Hint(HINT_SELECTMSG, tp, aux.Stringid(id, 3))
  		ct = Duel.AnnounceNumber(tp, table.unpack(tbl))
  	end
  	Duel.ConfirmDecktop(1 - tp, ct)
  	local g = Duel.GetDecktopGroup(1 - tp, ct)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	Duel.RevealSelectDeckSequence(true)
  	local sg = g:FilterSelect(tp, s.rmfilter, 1, 1, nil, tp)
  	Duel.RevealSelectDeckSequence(false)
  	if #sg > 0 then
  		Duel.DisableShuffleCheck(true)
  		Duel.Remove(sg, POS_FACEDOWN, REASON_EFFECT)
  	end
  end
  function s.rmcon(e, tp, eg, ep, ev, re, r, rp)
  	return rp == 1 - tp and re:IsActiveType(TYPE_MONSTER)
  end
  function s.rmtg2(e, tp, eg, ep, ev, re, r, rp, chk)
  	local g = Duel.GetDecktopGroup(1 - tp, 1)
  	if chk == 0 then
  		return rp == 1 - tp and #g > 0 and g:GetFirst():IsAbleToRemove(tp, POS_FACEDOWN)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_REMOVE, nil, 1, 1 - tp, LOCATION_DECK)
  end
  
  
source_text: Kashtira
  If you control no monsters, you can Special Summon this card (from your hand). You can only use each of the following effects of "Kashtira Ogre" once per turn. During your Main Phase: You can add 1 "Kashtira" Trap from your Deck to your hand. When this card declares an attack, or if your opponent activates a monster effect (except during the Damage Step): You can excavate up to 5 cards from the top of your opponent's Deck, banish 1 excavated card, face-down, and place the rest on the top of the Deck in the same order.
target_text: -- Kashtira
  local SETCARD_ID_0 = 0x189 --Kashtira
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_SPSUMMON_PROC)
  	e1:SetProperty(EFFECT_FLAG_UNCOPYABLE)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCondition(s.spcon)
  	c:RegisterEffect(e1)
  	--tohand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  	--remove when attack
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 2))
  	e3:SetCategory(CATEGORY_REMOVE)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e3:SetCode(EVENT_ATTACK_ANNOUNCE)
  	e3:SetCountLimit(1, id + 1)
  	e3:SetTarget(s.rmtg)
  	e3:SetOperation(s.rmop)
  	c:RegisterEffect(e3)
  	--remove when chaining
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 2))
  	e4:SetCategory(CATEGORY_REMOVE)
  	e4:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e4:SetCode(EVENT_CHAINING)
  	e4:SetProperty(EFFECT_FLAG_DELAY)
  	e4:SetRange(LOCATION_MZONE)
  	e4:SetCountLimit(1, id + 1)
  	e4:SetCondition(s.rmcon)
  	e4:SetTarget(s.rmtg2)
  	e4:SetOperation(s.rmop)
  	c:RegisterEffect(e4)
  end
  function s.spcon(e, c)
  	if c == nil then
  		return true
  	end
  	return Duel.GetFieldGroupCount(c:GetControler(), LOCATION_MZONE, 0) == 0 and Duel.GetLocationCount(c:GetControler(), LOCATION_MZONE) > 0
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(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.rmfilter(c, tp)
  	return c:IsAbleToRemove(tp, POS_FACEDOWN)
  end
  function s.rmtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local g = Duel.GetDecktopGroup(1 - tp, 1)
  	if chk == 0 then
  		return #g > 0 and g:GetFirst():IsAbleToRemove(tp, POS_FACEDOWN)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_REMOVE, nil, 1, 1 - tp, LOCATION_DECK)
  end
  function s.rmop(e, tp, eg, ep, ev, re, r, rp)
  	if not Duel.IsPlayerCanRemove(tp) then
  		return
  	end
  	local ct = Duel.GetFieldGroupCount(tp, 0, LOCATION_DECK)
  	if ct > 5 then
  		ct = 5
  	end
  	if ct > 1 then
  		local tbl = {}
  		for i = 1, ct do
  			table.insert(tbl, i)
  		end
  		Duel.Hint(HINT_SELECTMSG, tp, aux.Stringid(id, 3))
  		ct = Duel.AnnounceNumber(tp, table.unpack(tbl))
  	end
  	Duel.ConfirmDecktop(1 - tp, ct)
  	local g = Duel.GetDecktopGroup(1 - tp, ct)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	Duel.RevealSelectDeckSequence(true)
  	local sg = g:FilterSelect(tp, s.rmfilter, 1, 1, nil, tp)
  	Duel.RevealSelectDeckSequence(false)
  	if #sg > 0 then
  		Duel.DisableShuffleCheck(true)
  		Duel.Remove(sg, POS_FACEDOWN, REASON_EFFECT)
  	end
  end
  function s.rmcon(e, tp, eg, ep, ev, re, r, rp)
  	return rp == 1 - tp and re:IsActiveType(TYPE_MONSTER)
  end
  function s.rmtg2(e, tp, eg, ep, ev, re, r, rp, chk)
  	local g = Duel.GetDecktopGroup(1 - tp, 1)
  	if chk == 0 then
  		return rp == 1 - tp and #g > 0 and g:GetFirst():IsAbleToRemove(tp, POS_FACEDOWN)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_REMOVE, nil, 1, 1 - tp, LOCATION_DECK)
  end
  
  