name: Thunder
card_text: 2+ Thunder monsters
  During your Main Phase, if you control this Link Summoned card: You can target 1 of your "Thunder Dragon" monsters that is banished or in your GY; apply that monster's effect that discards itself to activate, then place that monster on the top or bottom of your Deck. You can only use this effect of "Thunder Dragon Thunderstormech" once per turn. If a Thunder monster(s) you control would be destroyed by battle or card effect, you can banish 3 cards from your GY instead.
external_ids: local SETCARD_ID_0 = 0x11c --Thunder
  
lua: local SETCARD_ID_0 = 0x11c --Thunder
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--link summon
  	c:EnableReviveLimit()
  	aux.AddLinkProcedure(c, aux.FilterBoolFunction(Card.IsLinkRace, RACE_THUNDER), 2)
  	--apply effect
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TODECK)
  	e1:SetType(EFFECT_TYPE_IGNITION)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.effcon)
  	e1:SetTarget(s.efftg)
  	e1:SetOperation(s.effop)
  	c:RegisterEffect(e1)
  	--destroy replace
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_DESTROY_REPLACE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetTarget(s.reptg)
  	e2:SetValue(s.repval)
  	c:RegisterEffect(e2)
  end
  function s.effcon(e)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_LINK)
  end
  function s.efffilter(c, e, tp, eg, ep, ev, re, r, rp)
  	if not (c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToDeck() and (c:IsLocation(LOCATION_GRAVE) or c:IsFaceup())) then
  		return false
  	end
  	local te = c.discard_effect
  	if not te then
  		return false
  	end
  	local tg = te:GetTarget()
  	return not tg or tg and tg(e, tp, eg, ep, ev, re, r, rp, 0)
  end
  function s.efftg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE + LOCATION_REMOVED) and chkc:IsControler(tp) and s.efffilter(chkc, e, tp, eg, ep, ev, re, r, rp)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.efffilter, tp, LOCATION_GRAVE + LOCATION_REMOVED, 0, 1, nil, e, tp, eg, ep, ev, re, r, rp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	local g = Duel.SelectTarget(tp, s.efffilter, tp, LOCATION_GRAVE + LOCATION_REMOVED, 0, 1, 1, nil, e, tp, eg, ep, ev, re, r, rp)
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, g, 1, 0, 0)
  	local tc = g:GetFirst()
  	Duel.ClearTargetCard()
  	tc:CreateEffectRelation(e)
  	e:SetLabelObject(tc)
  	local te = tc.discard_effect
  	local tg = te:GetTarget()
  	if tg then
  		tg(e, tp, eg, ep, ev, re, r, rp, 1)
  	end
  end
  function s.effop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = e:GetLabelObject()
  	if tc:IsRelateToEffect(e) then
  		local te = tc.discard_effect
  		local op = te:GetOperation()
  		if op then
  			op(e, tp, eg, ep, ev, re, r, rp)
  		end
  		Duel.BreakEffect()
  		if tc:IsExtraDeckMonster() or Duel.SelectOption(tp, aux.Stringid(id, 1), aux.Stringid(id, 2)) == 0 then
  			Duel.SendtoDeck(tc, nil, SEQ_DECKTOP, REASON_EFFECT)
  		else
  			Duel.SendtoDeck(tc, nil, SEQ_DECKBOTTOM, REASON_EFFECT)
  		end
  	end
  end
  function s.repfilter(c, tp)
  	return c:IsFaceup() and c:IsControler(tp) and c:IsLocation(LOCATION_MZONE) and c:IsRace(RACE_THUNDER) and c:IsReason(REASON_BATTLE + REASON_EFFECT) and not c:IsReason(REASON_REPLACE)
  end
  function s.reptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return eg:IsExists(s.repfilter, 1, nil, tp) and Duel.IsExistingMatchingCard(Card.IsAbleToRemove, tp, LOCATION_GRAVE, 0, 3, nil)
  	end
  	if Duel.SelectEffectYesNo(tp, e:GetHandler(), 96) then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  		local g = Duel.SelectMatchingCard(tp, Card.IsAbleToRemove, tp, LOCATION_GRAVE, 0, 3, 3, nil)
  		Duel.Remove(g, POS_FACEUP, REASON_EFFECT + REASON_REPLACE)
  		return true
  	end
  	return false
  end
  function s.repval(e, c)
  	return s.repfilter(c, e:GetHandlerPlayer())
  end
  
  
source_text: Thunder
  2+ Thunder monsters
  During your Main Phase, if you control this Link Summoned card: You can target 1 of your "Thunder Dragon" monsters that is banished or in your GY; apply that monster's effect that discards itself to activate, then place that monster on the top or bottom of your Deck. You can only use this effect of "Thunder Dragon Thunderstormech" once per turn. If a Thunder monster(s) you control would be destroyed by battle or card effect, you can banish 3 cards from your GY instead.
target_text: -- Thunder
  local SETCARD_ID_0 = 0x11c --Thunder
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--link summon
  	c:EnableReviveLimit()
  	aux.AddLinkProcedure(c, aux.FilterBoolFunction(Card.IsLinkRace, RACE_THUNDER), 2)
  	--apply effect
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TODECK)
  	e1:SetType(EFFECT_TYPE_IGNITION)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.effcon)
  	e1:SetTarget(s.efftg)
  	e1:SetOperation(s.effop)
  	c:RegisterEffect(e1)
  	--destroy replace
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_DESTROY_REPLACE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetTarget(s.reptg)
  	e2:SetValue(s.repval)
  	c:RegisterEffect(e2)
  end
  function s.effcon(e)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_LINK)
  end
  function s.efffilter(c, e, tp, eg, ep, ev, re, r, rp)
  	if not (c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToDeck() and (c:IsLocation(LOCATION_GRAVE) or c:IsFaceup())) then
  		return false
  	end
  	local te = c.discard_effect
  	if not te then
  		return false
  	end
  	local tg = te:GetTarget()
  	return not tg or tg and tg(e, tp, eg, ep, ev, re, r, rp, 0)
  end
  function s.efftg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE + LOCATION_REMOVED) and chkc:IsControler(tp) and s.efffilter(chkc, e, tp, eg, ep, ev, re, r, rp)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.efffilter, tp, LOCATION_GRAVE + LOCATION_REMOVED, 0, 1, nil, e, tp, eg, ep, ev, re, r, rp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	local g = Duel.SelectTarget(tp, s.efffilter, tp, LOCATION_GRAVE + LOCATION_REMOVED, 0, 1, 1, nil, e, tp, eg, ep, ev, re, r, rp)
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, g, 1, 0, 0)
  	local tc = g:GetFirst()
  	Duel.ClearTargetCard()
  	tc:CreateEffectRelation(e)
  	e:SetLabelObject(tc)
  	local te = tc.discard_effect
  	local tg = te:GetTarget()
  	if tg then
  		tg(e, tp, eg, ep, ev, re, r, rp, 1)
  	end
  end
  function s.effop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = e:GetLabelObject()
  	if tc:IsRelateToEffect(e) then
  		local te = tc.discard_effect
  		local op = te:GetOperation()
  		if op then
  			op(e, tp, eg, ep, ev, re, r, rp)
  		end
  		Duel.BreakEffect()
  		if tc:IsExtraDeckMonster() or Duel.SelectOption(tp, aux.Stringid(id, 1), aux.Stringid(id, 2)) == 0 then
  			Duel.SendtoDeck(tc, nil, SEQ_DECKTOP, REASON_EFFECT)
  		else
  			Duel.SendtoDeck(tc, nil, SEQ_DECKBOTTOM, REASON_EFFECT)
  		end
  	end
  end
  function s.repfilter(c, tp)
  	return c:IsFaceup() and c:IsControler(tp) and c:IsLocation(LOCATION_MZONE) and c:IsRace(RACE_THUNDER) and c:IsReason(REASON_BATTLE + REASON_EFFECT) and not c:IsReason(REASON_REPLACE)
  end
  function s.reptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return eg:IsExists(s.repfilter, 1, nil, tp) and Duel.IsExistingMatchingCard(Card.IsAbleToRemove, tp, LOCATION_GRAVE, 0, 3, nil)
  	end
  	if Duel.SelectEffectYesNo(tp, e:GetHandler(), 96) then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  		local g = Duel.SelectMatchingCard(tp, Card.IsAbleToRemove, tp, LOCATION_GRAVE, 0, 3, 3, nil)
  		Duel.Remove(g, POS_FACEUP, REASON_EFFECT + REASON_REPLACE)
  		return true
  	end
  	return false
  end
  function s.repval(e, c)
  	return s.repfilter(c, e:GetHandlerPlayer())
  end
  
  