--舞焰鬼
local s,id,o=GetID()
function s.initial_effect(c)
	 --destory
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e2:SetCode(EVENT_CUSTOM+id)
	e2:SetRange(LOCATION_MZONE)
	e2:SetCondition(s.ctcon)
	e2:SetOperation(s.ctop)
	c:RegisterEffect(e2)
	if not s.global_check then
		s.global_check=true
		if not recg1 then recg1=0 end
		if not recg2 then recg2=0 end
		local ge1=Effect.CreateEffect(c)
		ge1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
		ge1:SetCode(EVENT_DAMAGE)
		ge1:SetOperation(s.checkop)
		Duel.RegisterEffect(ge1,0)
	end
end
function s.checkop(e,tp,eg,ep,ev,re,r,rp)
	local oldev=Duel.GetFlagEffectLabel(ep,id)
	if not oldev then oldev=0 end
	Duel.ResetFlagEffect(ep,id)
	Duel.RegisterFlagEffect(ep,id,RESET_PHASE+PHASE_END,0,1,ev+oldev)
	if ep==0 then
		Duel.RaiseEvent(e:GetHandler(),EVENT_CUSTOM+id,re,r,rp,ep,0)
	elseif ep==1 then
		Duel.RaiseEvent(e:GetHandler(),EVENT_CUSTOM+id,re,r,rp,ep,1)
	end
end
function s.ctcon(e,tp,eg,ep,ev,re,r,rp)
	return ep~=tp and bit.band(r,REASON_EFFECT)~=0
end
function s.filter(c,ep)
	return c:IsDefenseBelow(Duel.GetFlagEffectLabel(ep,id)) or c:IsType(TYPE_LINK)
end
function s.ctop(e,tp,eg,ep,ev,re,r,rp)
	if not Duel.IsExistingMatchingCard(s.filter,tp,LOCATION_MZONE,LOCATION_MZONE,1,nil,ep) then return end
	Duel.Hint(HINT_CARD,0,id)
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_DESTROY)
	local g=Duel.SelectMatchingCard(tp,s.filter,tp,LOCATION_MZONE,LOCATION_MZONE,1,1,nil,ep)
	if #g>0 then
		Duel.Destroy(g,REASON_EFFECT)
	end
	Duel.Draw(tp,1,REASON_EFFECT)
	local e1=Effect.CreateEffect(e:GetHandler())
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e1:SetCode(EFFECT_SKIP_DP)
	e1:SetTargetRange(1,0)
	if Duel.GetTurnPlayer()==tp and Duel.GetCurrentPhase()==PHASE_DRAW then
		e1:SetReset(RESET_PHASE+PHASE_DRAW+RESET_SELF_TURN,2)
	else
		e1:SetReset(RESET_PHASE+PHASE_DRAW+RESET_SELF_TURN)
	end
	Duel.RegisterEffect(e1,tp)
end