name: Sangen
card_text: During your Main Phase 1, FIRE Dragon monsters you control are unaffected by your opponent's activated effects. During your Main Phase: You can add 1 "Tenpai Dragon" monster from your Deck to your hand, then discard 1 card. You can only use this effect of "Sangen Summoning" once per turn. If this card is destroyed during the Battle Phase: You can target 1 Dragon Synchro Monster you control; double its ATK.
external_ids: local SETCARD_ID_0 = 0x1aa --Tenpai
  
lua: local SETCARD_ID_0 = 0x1aa --Tenpai
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_IMMUNE_EFFECT)
  	e2:SetRange(LOCATION_FZONE)
  	e2:SetTargetRange(LOCATION_MZONE, 0)
  	e2:SetCondition(s.indcon)
  	e2:SetTarget(s.indtg)
  	e2:SetValue(s.efilter)
  	c:RegisterEffect(e2)
  	--
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND + CATEGORY_HANDES)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_FZONE)
  	e3:SetCountLimit(1, id)
  	e3:SetTarget(s.thtg)
  	e3:SetOperation(s.thop)
  	c:RegisterEffect(e3)
  	--
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 2))
  	e4:SetCategory(CATEGORY_ATKCHANGE)
  	e4:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e4:SetCode(EVENT_DESTROYED)
  	e4:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY + EFFECT_FLAG_CARD_TARGET)
  	e4:SetCondition(s.atkcon)
  	e4:SetTarget(s.atktg)
  	e4:SetOperation(s.atkop)
  	c:RegisterEffect(e4)
  end
  function s.indcon(e)
  	return Duel.GetCurrentPhase() == PHASE_MAIN1 and Duel.GetTurnPlayer() == e:GetHandlerPlayer()
  end
  function s.indtg(e, c)
  	return c:IsRace(RACE_DRAGON) and c:IsAttribute(ATTRIBUTE_FIRE)
  end
  function s.efilter(e, re)
  	return e:GetHandlerPlayer() ~= re:GetOwnerPlayer() and re:IsActivated()
  end
  function s.filter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  	Duel.SetOperationInfo(0, CATEGORY_HANDES, nil, 0, tp, 1)
  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.filter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 and Duel.SendtoHand(g, nil, REASON_EFFECT) ~= 0 and g:GetFirst():IsLocation(LOCATION_HAND) then
  		Duel.ConfirmCards(1 - tp, g)
  		Duel.BreakEffect()
  		local dg = Duel.SelectMatchingCard(tp, aux.TRUE, tp, LOCATION_HAND, 0, 1, 1, nil)
  		Duel.ShuffleHand(tp)
  		Duel.SendtoGrave(dg, REASON_EFFECT + REASON_DISCARD)
  	end
  end
  function s.atkcon(e, tp, eg, ep, ev, re, r, rp)
  	local ph = Duel.GetCurrentPhase()
  	return ph >= PHASE_BATTLE_START and ph <= PHASE_BATTLE
  end
  function s.atkfilter(c)
  	return c:IsFaceup() and c:IsType(TYPE_SYNCHRO) and c:IsRace(RACE_DRAGON)
  end
  function s.atktg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.atkfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.atkfilter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.atkfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  end
  function s.atkop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and tc:IsFaceup() then
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_SET_ATTACK_FINAL)
  		e1:SetValue(tc:GetAttack() * 2)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  		tc:RegisterEffect(e1)
  	end
  end
  
  
source_text: Sangen
  During your Main Phase 1, FIRE Dragon monsters you control are unaffected by your opponent's activated effects. During your Main Phase: You can add 1 "Tenpai Dragon" monster from your Deck to your hand, then discard 1 card. You can only use this effect of "Sangen Summoning" once per turn. If this card is destroyed during the Battle Phase: You can target 1 Dragon Synchro Monster you control; double its ATK.
target_text: -- Sangen
  local SETCARD_ID_0 = 0x1aa --Tenpai
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_IMMUNE_EFFECT)
  	e2:SetRange(LOCATION_FZONE)
  	e2:SetTargetRange(LOCATION_MZONE, 0)
  	e2:SetCondition(s.indcon)
  	e2:SetTarget(s.indtg)
  	e2:SetValue(s.efilter)
  	c:RegisterEffect(e2)
  	--
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND + CATEGORY_HANDES)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_FZONE)
  	e3:SetCountLimit(1, id)
  	e3:SetTarget(s.thtg)
  	e3:SetOperation(s.thop)
  	c:RegisterEffect(e3)
  	--
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 2))
  	e4:SetCategory(CATEGORY_ATKCHANGE)
  	e4:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e4:SetCode(EVENT_DESTROYED)
  	e4:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY + EFFECT_FLAG_CARD_TARGET)
  	e4:SetCondition(s.atkcon)
  	e4:SetTarget(s.atktg)
  	e4:SetOperation(s.atkop)
  	c:RegisterEffect(e4)
  end
  function s.indcon(e)
  	return Duel.GetCurrentPhase() == PHASE_MAIN1 and Duel.GetTurnPlayer() == e:GetHandlerPlayer()
  end
  function s.indtg(e, c)
  	return c:IsRace(RACE_DRAGON) and c:IsAttribute(ATTRIBUTE_FIRE)
  end
  function s.efilter(e, re)
  	return e:GetHandlerPlayer() ~= re:GetOwnerPlayer() and re:IsActivated()
  end
  function s.filter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  	Duel.SetOperationInfo(0, CATEGORY_HANDES, nil, 0, tp, 1)
  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.filter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 and Duel.SendtoHand(g, nil, REASON_EFFECT) ~= 0 and g:GetFirst():IsLocation(LOCATION_HAND) then
  		Duel.ConfirmCards(1 - tp, g)
  		Duel.BreakEffect()
  		local dg = Duel.SelectMatchingCard(tp, aux.TRUE, tp, LOCATION_HAND, 0, 1, 1, nil)
  		Duel.ShuffleHand(tp)
  		Duel.SendtoGrave(dg, REASON_EFFECT + REASON_DISCARD)
  	end
  end
  function s.atkcon(e, tp, eg, ep, ev, re, r, rp)
  	local ph = Duel.GetCurrentPhase()
  	return ph >= PHASE_BATTLE_START and ph <= PHASE_BATTLE
  end
  function s.atkfilter(c)
  	return c:IsFaceup() and c:IsType(TYPE_SYNCHRO) and c:IsRace(RACE_DRAGON)
  end
  function s.atktg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.atkfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.atkfilter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.atkfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  end
  function s.atkop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and tc:IsFaceup() then
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_SET_ATTACK_FINAL)
  		e1:SetValue(tc:GetAttack() * 2)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  		tc:RegisterEffect(e1)
  	end
  end
  
  