name: Stardust
card_text: 1 Tuner + 1+ non-Tuner monsters
  When a card or effect is activated that would destroy a card(s) on the field (Quick Effect): You can Tribute this card; negate the activation, and if you do, destroy it. During the End Phase, if this effect was activated this turn (and was not negated): You can Special Summon this card from your GY.
external_ids: local CARD_ID_0 = 84012625 --Cosmic Flare
  
lua: local CARD_ID_0 = 84012625 --Cosmic Flare
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--synchro summon
  	aux.AddSynchroProcedure(c, nil, aux.NonTuner(nil), 1)
  	c:EnableReviveLimit()
  	--Negate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_NEGATE + CATEGORY_DESTROY)
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetCode(EVENT_CHAINING)
  	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DAMAGE_CAL)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCondition(s.condition)
  	e1:SetCost(s.cost)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	--Revive
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_TRIGGER_O + EFFECT_TYPE_FIELD)
  	e2:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e2:SetCode(EVENT_PHASE + PHASE_END)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1)
  	e2:SetTarget(s.sumtg)
  	e2:SetOperation(s.sumop)
  	c:RegisterEffect(e2)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetHandler():IsStatus(STATUS_BATTLE_DESTROYED) or not Duel.IsChainNegatable(ev) then
  		return false
  	end
  	if re:IsHasCategory(CATEGORY_NEGATE) and Duel.GetChainInfo(ev - 1, CHAININFO_TRIGGERING_EFFECT):IsHasType(EFFECT_TYPE_ACTIVATE) then
  		return false
  	end
  	local ex, tg, tc = Duel.GetOperationInfo(ev, CATEGORY_DESTROY)
  	return ex and tg ~= nil and tc + tg:FilterCount(Card.IsOnField, nil) - tg:GetCount() > 0
  end
  function s.excostfilter(c, tp)
  	return c:IsAbleToRemoveAsCost() and c:IsHasEffect(CARD_ID_0, tp)
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local g = Duel.GetMatchingGroup(s.excostfilter, tp, LOCATION_GRAVE, 0, nil, tp)
  	if e:GetHandler():IsReleasable() then
  		g:AddCard(e:GetHandler())
  	end
  	if chk == 0 then
  		return #g > 0
  	end
  	local tc
  	if #g > 1 then
  		Duel.Hint(HINT_SELECTMSG, tp, aux.Stringid(CARD_ID_0, 0))
  		tc = g:Select(tp, 1, 1, nil):GetFirst()
  	else
  		tc = g:GetFirst()
  	end
  	local te = tc:IsHasEffect(CARD_ID_0, tp)
  	if te then
  		Duel.Remove(tc, POS_FACEUP, REASON_COST + REASON_REPLACE)
  	else
  		Duel.Release(tc, REASON_COST)
  	end
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_NEGATE, eg, 1, 0, 0)
  	if re:GetHandler():IsDestructable() and re:GetHandler():IsRelateToEffect(re) then
  		Duel.SetOperationInfo(0, CATEGORY_DESTROY, eg, 1, 0, 0)
  	end
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.NegateActivation(ev) and re:GetHandler():IsRelateToEffect(re) then
  		Duel.Destroy(eg, REASON_EFFECT)
  	end
  	e:GetHandler():RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END, 0, 0)
  end
  function s.sumtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and c:GetFlagEffect(id) > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, c, 1, 0, 0)
  end
  function s.sumop(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetHandler():IsRelateToEffect(e) then
  		Duel.SpecialSummon(e:GetHandler(), 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  
source_text: Stardust
  1 Tuner + 1+ non-Tuner monsters
  When a card or effect is activated that would destroy a card(s) on the field (Quick Effect): You can Tribute this card; negate the activation, and if you do, destroy it. During the End Phase, if this effect was activated this turn (and was not negated): You can Special Summon this card from your GY.
target_text: -- Stardust
  local CARD_ID_0 = 84012625 --Cosmic Flare
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--synchro summon
  	aux.AddSynchroProcedure(c, nil, aux.NonTuner(nil), 1)
  	c:EnableReviveLimit()
  	--Negate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_NEGATE + CATEGORY_DESTROY)
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetCode(EVENT_CHAINING)
  	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DAMAGE_CAL)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCondition(s.condition)
  	e1:SetCost(s.cost)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	--Revive
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_TRIGGER_O + EFFECT_TYPE_FIELD)
  	e2:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e2:SetCode(EVENT_PHASE + PHASE_END)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1)
  	e2:SetTarget(s.sumtg)
  	e2:SetOperation(s.sumop)
  	c:RegisterEffect(e2)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetHandler():IsStatus(STATUS_BATTLE_DESTROYED) or not Duel.IsChainNegatable(ev) then
  		return false
  	end
  	if re:IsHasCategory(CATEGORY_NEGATE) and Duel.GetChainInfo(ev - 1, CHAININFO_TRIGGERING_EFFECT):IsHasType(EFFECT_TYPE_ACTIVATE) then
  		return false
  	end
  	local ex, tg, tc = Duel.GetOperationInfo(ev, CATEGORY_DESTROY)
  	return ex and tg ~= nil and tc + tg:FilterCount(Card.IsOnField, nil) - tg:GetCount() > 0
  end
  function s.excostfilter(c, tp)
  	return c:IsAbleToRemoveAsCost() and c:IsHasEffect(CARD_ID_0, tp)
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local g = Duel.GetMatchingGroup(s.excostfilter, tp, LOCATION_GRAVE, 0, nil, tp)
  	if e:GetHandler():IsReleasable() then
  		g:AddCard(e:GetHandler())
  	end
  	if chk == 0 then
  		return #g > 0
  	end
  	local tc
  	if #g > 1 then
  		Duel.Hint(HINT_SELECTMSG, tp, aux.Stringid(CARD_ID_0, 0))
  		tc = g:Select(tp, 1, 1, nil):GetFirst()
  	else
  		tc = g:GetFirst()
  	end
  	local te = tc:IsHasEffect(CARD_ID_0, tp)
  	if te then
  		Duel.Remove(tc, POS_FACEUP, REASON_COST + REASON_REPLACE)
  	else
  		Duel.Release(tc, REASON_COST)
  	end
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_NEGATE, eg, 1, 0, 0)
  	if re:GetHandler():IsDestructable() and re:GetHandler():IsRelateToEffect(re) then
  		Duel.SetOperationInfo(0, CATEGORY_DESTROY, eg, 1, 0, 0)
  	end
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.NegateActivation(ev) and re:GetHandler():IsRelateToEffect(re) then
  		Duel.Destroy(eg, REASON_EFFECT)
  	end
  	e:GetHandler():RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END, 0, 0)
  end
  function s.sumtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and c:GetFlagEffect(id) > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, c, 1, 0, 0)
  end
  function s.sumop(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetHandler():IsRelateToEffect(e) then
  		Duel.SpecialSummon(e:GetHandler(), 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  