name: Stardust
card_text: When this card you control is destroyed by your opponent's card (either by battle or by card effect) and sent to your Graveyard, you can select 1 "Stardust Dragon" in your Graveyard and Special Summon it in face-up Defense Position. You can remove from play this card in the Graveyard to select 1 face-up Dragon-Type Synchro Monster you control. Once per turn, that Synchro Monster cannot be destroyed by battle, but each time this effect is applied, it loses 800 ATK and DEF at the end of the Damage Step.
external_ids: local CARD_ID_0 = 44508094 --Stardust Dragon
  
lua: local CARD_ID_0 = 44508094 --Stardust Dragon
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_0)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DAMAGE_STEP)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_TO_GRAVE)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--indes
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCost(s.indcost)
  	e2:SetTarget(s.indtg)
  	e2:SetOperation(s.indop)
  	c:RegisterEffect(e2)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return rp == 1 - tp and e:GetHandler():IsPreviousLocation(LOCATION_ONFIELD) and e:GetHandler():IsReason(REASON_DESTROY)
  end
  function s.spfilter(c, e, tp)
  	return c:IsCode(CARD_ID_0) 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 chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.spfilter(chkc, e, tp)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.spfilter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectTarget(tp, s.spfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP_DEFENSE)
  	end
  end
  function s.indcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToRemoveAsCost()
  	end
  	Duel.Remove(e:GetHandler(), POS_FACEUP, REASON_COST)
  end
  function s.indfilter(c)
  	return c:IsFaceup() and c:IsRace(RACE_DRAGON) and c:IsType(TYPE_SYNCHRO)
  end
  function s.indtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.indfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.indfilter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	local g = Duel.SelectTarget(tp, s.indfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  end
  function s.indop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsFaceup() and tc:IsRelateToEffect(e) then
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  		e1:SetRange(LOCATION_MZONE)
  		e1:SetCode(EFFECT_INDESTRUCTABLE_COUNT)
  		e1:SetCountLimit(1)
  		e1:SetValue(s.valcon)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  		tc:RegisterEffect(e1)
  		local e2 = Effect.CreateEffect(e:GetHandler())
  		e2:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_SINGLE)
  		e2:SetCode(EVENT_DAMAGE_STEP_END)
  		e2:SetOperation(s.addown)
  		e2:SetReset(RESET_EVENT + RESETS_STANDARD)
  		tc:RegisterEffect(e2)
  	end
  end
  function s.valcon(e, re, r, rp)
  	if bit.band(r, REASON_BATTLE) ~= 0 then
  		e:GetHandler():RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_DAMAGE, 0, 1)
  		return true
  	else
  		return false
  	end
  end
  function s.addown(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetHandler():GetFlagEffect(id) == 0 then
  		return
  	end
  	local e1 = Effect.CreateEffect(e:GetOwner())
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetCode(EFFECT_UPDATE_ATTACK)
  	e1:SetValue(-800)
  	e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  	e:GetHandler():RegisterEffect(e1)
  	local e2 = e1:Clone()
  	e2:SetCode(EFFECT_UPDATE_DEFENSE)
  	e:GetHandler():RegisterEffect(e2)
  end
  
  
source_text: Stardust
  When this card you control is destroyed by your opponent's card (either by battle or by card effect) and sent to your Graveyard, you can select 1 "Stardust Dragon" in your Graveyard and Special Summon it in face-up Defense Position. You can remove from play this card in the Graveyard to select 1 face-up Dragon-Type Synchro Monster you control. Once per turn, that Synchro Monster cannot be destroyed by battle, but each time this effect is applied, it loses 800 ATK and DEF at the end of the Damage Step.
target_text: -- Stardust
  local CARD_ID_0 = 44508094 --Stardust Dragon
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_0)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DAMAGE_STEP)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_TO_GRAVE)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--indes
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCost(s.indcost)
  	e2:SetTarget(s.indtg)
  	e2:SetOperation(s.indop)
  	c:RegisterEffect(e2)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return rp == 1 - tp and e:GetHandler():IsPreviousLocation(LOCATION_ONFIELD) and e:GetHandler():IsReason(REASON_DESTROY)
  end
  function s.spfilter(c, e, tp)
  	return c:IsCode(CARD_ID_0) 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 chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.spfilter(chkc, e, tp)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.spfilter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectTarget(tp, s.spfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP_DEFENSE)
  	end
  end
  function s.indcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToRemoveAsCost()
  	end
  	Duel.Remove(e:GetHandler(), POS_FACEUP, REASON_COST)
  end
  function s.indfilter(c)
  	return c:IsFaceup() and c:IsRace(RACE_DRAGON) and c:IsType(TYPE_SYNCHRO)
  end
  function s.indtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.indfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.indfilter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	local g = Duel.SelectTarget(tp, s.indfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  end
  function s.indop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsFaceup() and tc:IsRelateToEffect(e) then
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  		e1:SetRange(LOCATION_MZONE)
  		e1:SetCode(EFFECT_INDESTRUCTABLE_COUNT)
  		e1:SetCountLimit(1)
  		e1:SetValue(s.valcon)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  		tc:RegisterEffect(e1)
  		local e2 = Effect.CreateEffect(e:GetHandler())
  		e2:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_SINGLE)
  		e2:SetCode(EVENT_DAMAGE_STEP_END)
  		e2:SetOperation(s.addown)
  		e2:SetReset(RESET_EVENT + RESETS_STANDARD)
  		tc:RegisterEffect(e2)
  	end
  end
  function s.valcon(e, re, r, rp)
  	if bit.band(r, REASON_BATTLE) ~= 0 then
  		e:GetHandler():RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_DAMAGE, 0, 1)
  		return true
  	else
  		return false
  	end
  end
  function s.addown(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetHandler():GetFlagEffect(id) == 0 then
  		return
  	end
  	local e1 = Effect.CreateEffect(e:GetOwner())
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetCode(EFFECT_UPDATE_ATTACK)
  	e1:SetValue(-800)
  	e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  	e:GetHandler():RegisterEffect(e1)
  	local e2 = e1:Clone()
  	e2:SetCode(EFFECT_UPDATE_DEFENSE)
  	e:GetHandler():RegisterEffect(e2)
  end
  
  