name: Sangen
card_text: Once per turn, when a monster is destroyed by battle involving your FIRE Dragon monster: You can target 1 of those destroyed monsters; Special Summon it to your field in Defense Position. During your opponent's End Phase: You can pay 1000 LP, then target 1 "Sangen" Spell/Trap in your GY; Set it, but banish it when it leaves the field. You can only use this effect of "Sangen Furo" once per turn.
external_ids: local SETCARD_ID_0 = 0x1a9 --Sangen
  
lua: local SETCARD_ID_0 = 0x1a9 --Sangen
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e0 = Effect.CreateEffect(c)
  	e0:SetType(EFFECT_TYPE_ACTIVATE)
  	e0:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e0)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e1:SetRange(LOCATION_SZONE)
  	e1:SetCode(EVENT_BATTLE_DESTROYED)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetCountLimit(1)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--set
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_PHASE + PHASE_END)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetCondition(s.setcon)
  	e2:SetCost(s.setcost)
  	e2:SetTarget(s.settg)
  	e2:SetOperation(s.setop)
  	c:RegisterEffect(e2)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	local a = Duel.GetBattleMonster(tp)
  	return a
  		and (
  			a:IsLocation(LOCATION_MZONE) and a:IsRace(RACE_DRAGON) and a:IsAttribute(ATTRIBUTE_FIRE)
  			or not a:IsLocation(LOCATION_MZONE) and a:IsPreviousControler(tp) and a:GetPreviousRaceOnField() & RACE_DRAGON ~= 0 and a:GetPreviousAttributeOnField() & ATTRIBUTE_FIRE ~= 0
  		)
  end
  function s.tgfilter(c, e, tp)
  	return not c:IsType(TYPE_TOKEN) and c:IsFaceupEx() and c:IsType(TYPE_MONSTER) and c:IsCanBeEffectTarget(e) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP_DEFENSE)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return false
  	end
  	local g = eg:Filter(s.tgfilter, nil, e, tp)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and g:GetCount() > 0
  	end
  	local bc = g:GetFirst()
  	if g:GetCount() > 1 then
  		bc = g:FilterSelect(tp, s.tgfilter, 1, 1, nil, e, tp):GetFirst()
  	end
  	Duel.SetTargetCard(bc)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, bc, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and aux.NecroValleyFilter()(tc) then
  		Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP_DEFENSE)
  	end
  end
  function s.setcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() == 1 - tp
  end
  function s.setcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.CheckLPCost(tp, 1000)
  	end
  	Duel.PayLPCost(tp, 1000)
  end
  function s.sfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SPELL + TYPE_TRAP) and c:IsSSetable()
  end
  function s.settg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.sfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.sfilter, tp, LOCATION_GRAVE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SET)
  	local g = Duel.SelectTarget(tp, s.sfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_LEAVE_GRAVE, g, 1, 0, 0)
  end
  function s.setop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and aux.NecroValleyFilter()(tc) and Duel.SSet(tp, tc) then
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_LEAVE_FIELD_REDIRECT)
  		e1:SetReset(RESET_EVENT + RESETS_REDIRECT)
  		e1:SetValue(LOCATION_REMOVED)
  		tc:RegisterEffect(e1)
  	end
  end
  
  
source_text: Sangen
  Once per turn, when a monster is destroyed by battle involving your FIRE Dragon monster: You can target 1 of those destroyed monsters; Special Summon it to your field in Defense Position. During your opponent's End Phase: You can pay 1000 LP, then target 1 "Sangen" Spell/Trap in your GY; Set it, but banish it when it leaves the field. You can only use this effect of "Sangen Furo" once per turn.
target_text: -- Sangen
  local SETCARD_ID_0 = 0x1a9 --Sangen
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e0 = Effect.CreateEffect(c)
  	e0:SetType(EFFECT_TYPE_ACTIVATE)
  	e0:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e0)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e1:SetRange(LOCATION_SZONE)
  	e1:SetCode(EVENT_BATTLE_DESTROYED)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetCountLimit(1)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--set
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_PHASE + PHASE_END)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetCondition(s.setcon)
  	e2:SetCost(s.setcost)
  	e2:SetTarget(s.settg)
  	e2:SetOperation(s.setop)
  	c:RegisterEffect(e2)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	local a = Duel.GetBattleMonster(tp)
  	return a
  		and (
  			a:IsLocation(LOCATION_MZONE) and a:IsRace(RACE_DRAGON) and a:IsAttribute(ATTRIBUTE_FIRE)
  			or not a:IsLocation(LOCATION_MZONE) and a:IsPreviousControler(tp) and a:GetPreviousRaceOnField() & RACE_DRAGON ~= 0 and a:GetPreviousAttributeOnField() & ATTRIBUTE_FIRE ~= 0
  		)
  end
  function s.tgfilter(c, e, tp)
  	return not c:IsType(TYPE_TOKEN) and c:IsFaceupEx() and c:IsType(TYPE_MONSTER) and c:IsCanBeEffectTarget(e) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP_DEFENSE)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return false
  	end
  	local g = eg:Filter(s.tgfilter, nil, e, tp)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and g:GetCount() > 0
  	end
  	local bc = g:GetFirst()
  	if g:GetCount() > 1 then
  		bc = g:FilterSelect(tp, s.tgfilter, 1, 1, nil, e, tp):GetFirst()
  	end
  	Duel.SetTargetCard(bc)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, bc, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and aux.NecroValleyFilter()(tc) then
  		Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP_DEFENSE)
  	end
  end
  function s.setcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() == 1 - tp
  end
  function s.setcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.CheckLPCost(tp, 1000)
  	end
  	Duel.PayLPCost(tp, 1000)
  end
  function s.sfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SPELL + TYPE_TRAP) and c:IsSSetable()
  end
  function s.settg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.sfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.sfilter, tp, LOCATION_GRAVE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SET)
  	local g = Duel.SelectTarget(tp, s.sfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_LEAVE_GRAVE, g, 1, 0, 0)
  end
  function s.setop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and aux.NecroValleyFilter()(tc) and Duel.SSet(tp, tc) then
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_LEAVE_FIELD_REDIRECT)
  		e1:SetReset(RESET_EVENT + RESETS_REDIRECT)
  		e1:SetValue(LOCATION_REMOVED)
  		tc:RegisterEffect(e1)
  	end
  end
  
  