name: Malefic
card_text: Cannot be Normal Summoned/Set. Must be Special Summoned by its own effect, and cannot be Special Summoned by other ways. If a "Malefic" monster you control, except "Malefic Truth Dragon", is destroyed by battle or by card effect: You can pay half your Life Points; Special Summon this card from your hand or Graveyard. There can only be 1 "Malefic" monster on the field. If there is no face-up Field Spell Card on the field, destroy this card. If this card destroys an opponent's monster by battle: Destroy all face-up monsters your opponent controls.
external_ids: local CARD_ID_0 = 75223115 --Malefic Territory
  local SETCARD_ID_0 = 0x23 --Malefic
  
lua: local CARD_ID_0 = 75223115 --Malefic Territory
  local SETCARD_ID_0 = 0x23 --Malefic
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableReviveLimit()
  	c:SetUniqueOnField(1, 1, s.uqfilter, LOCATION_MZONE)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY)
  	e1:SetCode(EVENT_DESTROYED)
  	e1:SetRange(LOCATION_HAND + LOCATION_GRAVE)
  	e1:SetCondition(s.spcon)
  	e1:SetCost(s.spcost)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--selfdes
  	local e7 = Effect.CreateEffect(c)
  	e7:SetType(EFFECT_TYPE_SINGLE)
  	e7:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e7:SetRange(LOCATION_MZONE)
  	e7:SetCode(EFFECT_SELF_DESTROY)
  	e7:SetCondition(s.descon)
  	c:RegisterEffect(e7)
  	--spson
  	local e8 = Effect.CreateEffect(c)
  	e8:SetType(EFFECT_TYPE_SINGLE)
  	e8:SetProperty(EFFECT_FLAG_CANNOT_DISABLE + EFFECT_FLAG_UNCOPYABLE)
  	e8:SetCode(EFFECT_SPSUMMON_CONDITION)
  	e8:SetValue(aux.FALSE)
  	c:RegisterEffect(e8)
  	--destroy
  	local e9 = Effect.CreateEffect(c)
  	e9:SetDescription(aux.Stringid(id, 1))
  	e9:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_F)
  	e9:SetCategory(CATEGORY_DESTROY)
  	e9:SetCode(EVENT_BATTLE_DESTROYING)
  	e9:SetCondition(aux.bdocon)
  	e9:SetTarget(s.detg)
  	e9:SetOperation(s.deop)
  	c:RegisterEffect(e9)
  end
  function s.uqfilter(c)
  	if Duel.IsPlayerAffectedByEffect(c:GetControler(), CARD_ID_0) then
  		return c:IsCode(id)
  	else
  		return c:IsSetCard(SETCARD_ID_0)
  	end
  end
  function s.cfilter(c, tp)
  	return c:IsPreviousControler(tp)
  		and c:IsPreviousLocation(LOCATION_MZONE)
  		and c:IsPreviousPosition(POS_FACEUP)
  		and c:IsPreviousSetCard(SETCARD_ID_0)
  		and c:GetPreviousCodeOnField() ~= id
  		and not c:IsReason(REASON_RULE)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp)
  end
  function s.spcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.PayLPCost(tp, math.floor(Duel.GetLP(tp) / 2))
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, true, true)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, e:GetHandler(), 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if not c:IsRelateToEffect(e) then
  		return
  	end
  	if Duel.SpecialSummon(c, 0, tp, tp, true, true, POS_FACEUP) ~= 0 then
  		c:CompleteProcedure()
  	end
  end
  function s.descon(e)
  	return not Duel.IsExistingMatchingCard(Card.IsFaceup, 0, LOCATION_FZONE, LOCATION_FZONE, 1, nil)
  end
  function s.detg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	local g = Duel.GetMatchingGroup(Card.IsFaceup, tp, 0, LOCATION_MZONE, nil)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, g, g:GetCount(), 0, 0)
  end
  function s.deop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(Card.IsFaceup, tp, 0, LOCATION_MZONE, nil)
  	Duel.Destroy(g, REASON_EFFECT)
  end
  
  
source_text: Malefic
  Cannot be Normal Summoned/Set. Must be Special Summoned by its own effect, and cannot be Special Summoned by other ways. If a "Malefic" monster you control, except "Malefic Truth Dragon", is destroyed by battle or by card effect: You can pay half your Life Points; Special Summon this card from your hand or Graveyard. There can only be 1 "Malefic" monster on the field. If there is no face-up Field Spell Card on the field, destroy this card. If this card destroys an opponent's monster by battle: Destroy all face-up monsters your opponent controls.
target_text: -- Malefic
  local CARD_ID_0 = 75223115 --Malefic Territory
  local SETCARD_ID_0 = 0x23 --Malefic
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableReviveLimit()
  	c:SetUniqueOnField(1, 1, s.uqfilter, LOCATION_MZONE)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY)
  	e1:SetCode(EVENT_DESTROYED)
  	e1:SetRange(LOCATION_HAND + LOCATION_GRAVE)
  	e1:SetCondition(s.spcon)
  	e1:SetCost(s.spcost)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--selfdes
  	local e7 = Effect.CreateEffect(c)
  	e7:SetType(EFFECT_TYPE_SINGLE)
  	e7:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e7:SetRange(LOCATION_MZONE)
  	e7:SetCode(EFFECT_SELF_DESTROY)
  	e7:SetCondition(s.descon)
  	c:RegisterEffect(e7)
  	--spson
  	local e8 = Effect.CreateEffect(c)
  	e8:SetType(EFFECT_TYPE_SINGLE)
  	e8:SetProperty(EFFECT_FLAG_CANNOT_DISABLE + EFFECT_FLAG_UNCOPYABLE)
  	e8:SetCode(EFFECT_SPSUMMON_CONDITION)
  	e8:SetValue(aux.FALSE)
  	c:RegisterEffect(e8)
  	--destroy
  	local e9 = Effect.CreateEffect(c)
  	e9:SetDescription(aux.Stringid(id, 1))
  	e9:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_F)
  	e9:SetCategory(CATEGORY_DESTROY)
  	e9:SetCode(EVENT_BATTLE_DESTROYING)
  	e9:SetCondition(aux.bdocon)
  	e9:SetTarget(s.detg)
  	e9:SetOperation(s.deop)
  	c:RegisterEffect(e9)
  end
  function s.uqfilter(c)
  	if Duel.IsPlayerAffectedByEffect(c:GetControler(), CARD_ID_0) then
  		return c:IsCode(id)
  	else
  		return c:IsSetCard(SETCARD_ID_0)
  	end
  end
  function s.cfilter(c, tp)
  	return c:IsPreviousControler(tp)
  		and c:IsPreviousLocation(LOCATION_MZONE)
  		and c:IsPreviousPosition(POS_FACEUP)
  		and c:IsPreviousSetCard(SETCARD_ID_0)
  		and c:GetPreviousCodeOnField() ~= id
  		and not c:IsReason(REASON_RULE)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp)
  end
  function s.spcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.PayLPCost(tp, math.floor(Duel.GetLP(tp) / 2))
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, true, true)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, e:GetHandler(), 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if not c:IsRelateToEffect(e) then
  		return
  	end
  	if Duel.SpecialSummon(c, 0, tp, tp, true, true, POS_FACEUP) ~= 0 then
  		c:CompleteProcedure()
  	end
  end
  function s.descon(e)
  	return not Duel.IsExistingMatchingCard(Card.IsFaceup, 0, LOCATION_FZONE, LOCATION_FZONE, 1, nil)
  end
  function s.detg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	local g = Duel.GetMatchingGroup(Card.IsFaceup, tp, 0, LOCATION_MZONE, nil)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, g, g:GetCount(), 0, 0)
  end
  function s.deop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(Card.IsFaceup, tp, 0, LOCATION_MZONE, nil)
  	Duel.Destroy(g, REASON_EFFECT)
  end
  
  