name: Dirge
card_text: Activate this card by declaring 1 card name. Each time the declared card, or its effect, is activated, halve its owner's LP immediately after it resolves. During the End Phase, if this card halved a player's LP this turn, send this card to the GY. While your opponent controls a monster, this card cannot be destroyed by card effects.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetProperty(EFFECT_FLAG_CONTINUOUS_TARGET)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--halve LP
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetCode(EVENT_CHAINING)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e2:SetLabelObject(e1)
  	e2:SetOperation(s.regop)
  	c:RegisterEffect(e2)
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e3:SetCode(EVENT_CHAIN_SOLVED)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetLabelObject(e2)
  	e3:SetCondition(s.lpcon)
  	e3:SetOperation(s.lpop)
  	c:RegisterEffect(e3)
  	--indes
  	local e5 = Effect.CreateEffect(c)
  	e5:SetType(EFFECT_TYPE_SINGLE)
  	e5:SetCode(EFFECT_INDESTRUCTABLE_EFFECT)
  	e5:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e5:SetRange(LOCATION_SZONE)
  	e5:SetCondition(s.indcon)
  	e5:SetValue(1)
  	c:RegisterEffect(e5)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_CODE)
  	local ac = Duel.AnnounceCard(tp)
  	Duel.SetTargetParam(ac)
  	Duel.SetOperationInfo(0, CATEGORY_ANNOUNCE, nil, 0, tp, 0)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local ac = Duel.GetChainInfo(0, CHAININFO_TARGET_PARAM)
  	e:SetLabel(ac)
  	e:GetHandler():SetHint(CHINT_CARD, ac)
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	if eg:GetFirst():IsCode(e:GetLabelObject():GetLabel()) then
  		e:GetHandler():RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD - RESET_TURN_SET + RESET_CHAIN, 0, 1)
  		e:SetLabel(eg:GetFirst():GetOwner())
  	end
  end
  function s.lpcon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	return c:GetFlagEffect(id) ~= 0 and re:GetHandler():IsCode(e:GetLabelObject():GetLabelObject():GetLabel())
  end
  function s.lpop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local p = e:GetLabelObject():GetLabel()
  	Duel.Hint(HINT_CARD, 0, id)
  	Duel.SetLP(p, math.ceil(Duel.GetLP(p) / 2))
  	if c:GetFlagEffect(40971262) == 0 then
  		local fid = c:GetFieldID()
  		c:RegisterFlagEffect(40971262, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END, 0, 1, fid)
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		e1:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  		e1:SetCode(EVENT_PHASE + PHASE_END)
  		e1:SetCountLimit(1)
  		e1:SetLabel(fid)
  		e1:SetCondition(s.tgcon)
  		e1:SetOperation(s.tgop)
  		e1:SetReset(RESET_PHASE + PHASE_END)
  		Duel.RegisterEffect(e1, tp)
  	end
  end
  function s.tgcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():GetFlagEffectLabel(40971262) == e:GetLabel()
  end
  function s.tgop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.SendtoGrave(e:GetHandler(), REASON_EFFECT)
  end
  function s.indcon(e)
  	return Duel.GetFieldGroupCount(e:GetOwnerPlayer(), 0, LOCATION_MZONE) > 0
  end
  
  
source_text: Dirge
  Activate this card by declaring 1 card name. Each time the declared card, or its effect, is activated, halve its owner's LP immediately after it resolves. During the End Phase, if this card halved a player's LP this turn, send this card to the GY. While your opponent controls a monster, this card cannot be destroyed by card effects.
target_text: -- Dirge
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetProperty(EFFECT_FLAG_CONTINUOUS_TARGET)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--halve LP
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetCode(EVENT_CHAINING)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e2:SetLabelObject(e1)
  	e2:SetOperation(s.regop)
  	c:RegisterEffect(e2)
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e3:SetCode(EVENT_CHAIN_SOLVED)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetLabelObject(e2)
  	e3:SetCondition(s.lpcon)
  	e3:SetOperation(s.lpop)
  	c:RegisterEffect(e3)
  	--indes
  	local e5 = Effect.CreateEffect(c)
  	e5:SetType(EFFECT_TYPE_SINGLE)
  	e5:SetCode(EFFECT_INDESTRUCTABLE_EFFECT)
  	e5:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e5:SetRange(LOCATION_SZONE)
  	e5:SetCondition(s.indcon)
  	e5:SetValue(1)
  	c:RegisterEffect(e5)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_CODE)
  	local ac = Duel.AnnounceCard(tp)
  	Duel.SetTargetParam(ac)
  	Duel.SetOperationInfo(0, CATEGORY_ANNOUNCE, nil, 0, tp, 0)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local ac = Duel.GetChainInfo(0, CHAININFO_TARGET_PARAM)
  	e:SetLabel(ac)
  	e:GetHandler():SetHint(CHINT_CARD, ac)
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	if eg:GetFirst():IsCode(e:GetLabelObject():GetLabel()) then
  		e:GetHandler():RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD - RESET_TURN_SET + RESET_CHAIN, 0, 1)
  		e:SetLabel(eg:GetFirst():GetOwner())
  	end
  end
  function s.lpcon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	return c:GetFlagEffect(id) ~= 0 and re:GetHandler():IsCode(e:GetLabelObject():GetLabelObject():GetLabel())
  end
  function s.lpop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local p = e:GetLabelObject():GetLabel()
  	Duel.Hint(HINT_CARD, 0, id)
  	Duel.SetLP(p, math.ceil(Duel.GetLP(p) / 2))
  	if c:GetFlagEffect(40971262) == 0 then
  		local fid = c:GetFieldID()
  		c:RegisterFlagEffect(40971262, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END, 0, 1, fid)
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		e1:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  		e1:SetCode(EVENT_PHASE + PHASE_END)
  		e1:SetCountLimit(1)
  		e1:SetLabel(fid)
  		e1:SetCondition(s.tgcon)
  		e1:SetOperation(s.tgop)
  		e1:SetReset(RESET_PHASE + PHASE_END)
  		Duel.RegisterEffect(e1, tp)
  	end
  end
  function s.tgcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():GetFlagEffectLabel(40971262) == e:GetLabel()
  end
  function s.tgop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.SendtoGrave(e:GetHandler(), REASON_EFFECT)
  end
  function s.indcon(e)
  	return Duel.GetFieldGroupCount(e:GetOwnerPlayer(), 0, LOCATION_MZONE) > 0
  end
  
  