--转瞬即逝的噩梦
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)
    --quick activate
    local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_QUICK_O)
	e1:SetCode(EVENT_FREE_CHAIN)
    e1:SetRange(LOCATION_HAND)
    e1:SetCountLimit(1,EFFECT_COUNT_CODE_CHAIN)
    e1:SetCondition(s.qacon)
    e1:SetCost(s.cost)
    e1:SetOperation(s.qaop)
    c:RegisterEffect(e1)
    --destroy
    local e2=Effect.CreateEffect(c)
    e2:SetType(EFFECT_TYPE_QUICK_F)
	e2:SetCode(EVENT_CHAINING)
    e2:SetRange(LOCATION_SZONE)
	e2:SetProperty(EFFECT_FLAG_DAMAGE_STEP+EFFECT_FLAG_DAMAGE_CAL)
    e2:SetCost(s.cost)
    e2:SetTarget(s.destg)
    e2:SetOperation(s.desop)
    c:RegisterEffect(e2)
    local e2p=e2:Clone()
    e2p:SetCode(EVENT_CHAIN_SOLVED)
    c:RegisterEffect(e2p)
    --self destroy
	local e3=Effect.CreateEffect(c)
	e3:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e3:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
	e3:SetCode(EVENT_PHASE+PHASE_END)
	e3:SetRange(LOCATION_SZONE)
    e3:SetCategory(CATEGORY_DAMAGE)
	e3:SetCountLimit(1)
	e3:SetCondition(s.sdcon)
	e3:SetOperation(s.sdop)
	c:RegisterEffect(e3)
end

function s.cost(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.qacon(e,tp,eg,ep,ev,re,r,rp)
	local ph=Duel.GetCurrentPhase()
	return ph==PHASE_MAIN1 or ph==PHASE_MAIN2
end
function s.qaop(e,tp,eg,ep,ev,re,r,rp)
    local c=e:GetHandler()
    Duel.MoveToField(c,tp,tp,LOCATION_SZONE,POS_FACEUP,true)
end

function s.desfilter(c)
    return c:IsOnField() and c:IsType(TYPE_SPELL+TYPE_TRAP) and c:IsFaceup() and c:GetFlagEffect(id)==0
end
function s.destg(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
    if chkc then return s.desfilter(chkc) and chkc~=e:GetHandler() end
	if chk==0 then return Duel.IsExistingTarget(s.desfilter,tp,LOCATION_ONFIELD,LOCATION_ONFIELD,1,e:GetHandler()) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_DESTROY)
	local g=Duel.SelectTarget(tp,s.desfilter,tp,LOCATION_ONFIELD,LOCATION_ONFIELD,1,1,e:GetHandler())
    Duel.HintSelection(g)
    local tc=g:GetFirst()
    tc:RegisterFlagEffect(id,RESET_EVENT+RESET_CHAIN+RESET_LEAVE,0,1)
	Duel.SetOperationInfo(0,CATEGORY_DESTROY,tc,1,0,0)
end

function s.desop(e,tp,eg,ep,ev,re,r,rp)
	local tc=Duel.GetFirstTarget()
	Duel.Destroy(tc,REASON_EFFECT)
end

function s.sdcon(e,tp,eg,ep,ev,re,r,rp)
    return not Duel.IsExistingMatchingCard(Card.IsCode,tp,LOCATION_MZONE,LOCATION_MZONE,1,nil,22200403)
end
function s.sdop(e,tp,eg,ep,ev,re,r,rp)
    local c=e:GetHandler()
    if Duel.Damage(tp,3000,REASON_EFFECT)>0 then
        Duel.Destroy(c,REASON_EFFECT)
    end
end