name: Malefic
card_text: If a face-up "Malefic" monster(s) you control is destroyed by battle or an opponent's card effect: Draw 2 cards. If a face-up "Malefic" monster(s) you control is destroyed, while this card is in your GY (except during the Damage Step): You can banish this card from your GY; add 1 "Malefic" monster from your Deck to your hand. You can only use each effect of "Malefic Tune" once per turn.
external_ids: local SETCARD_ID_0 = 0x23 --Malefic
  
lua: local SETCARD_ID_0 = 0x23 --Malefic
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_DRAW)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_DESTROYED)
  	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--search
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_DESTROYED)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCost(aux.bfgcost)
  	e2:SetCondition(s.thcon)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  end
  function s.cfilter(c, tp)
  	return c:IsPreviousControler(tp)
  		and c:IsPreviousLocation(LOCATION_MZONE)
  		and c:IsPreviousPosition(POS_FACEUP)
  		and c:IsSetCard(SETCARD_ID_0)
  		and (c:IsReason(REASON_BATTLE) or c:IsReason(REASON_EFFECT) and c:GetReasonPlayer() == 1 - tp)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanDraw(tp, 2)
  	end
  	Duel.SetTargetPlayer(tp)
  	Duel.SetTargetParam(2)
  	Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, 2)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local p, d = Duel.GetChainInfo(0, CHAININFO_TARGET_PLAYER, CHAININFO_TARGET_PARAM)
  	Duel.Draw(p, d, REASON_EFFECT)
  end
  function s.cfilter2(c, tp)
  	return c:IsPreviousControler(tp) and c:IsPreviousLocation(LOCATION_MZONE) and c:IsPreviousPosition(POS_FACEUP) and c:IsSetCard(SETCARD_ID_0) and not c:IsReason(REASON_BATTLE)
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter2, 1, nil, tp)
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  
  
source_text: Malefic
  If a face-up "Malefic" monster(s) you control is destroyed by battle or an opponent's card effect: Draw 2 cards. If a face-up "Malefic" monster(s) you control is destroyed, while this card is in your GY (except during the Damage Step): You can banish this card from your GY; add 1 "Malefic" monster from your Deck to your hand. You can only use each effect of "Malefic Tune" once per turn.
target_text: -- Malefic
  local SETCARD_ID_0 = 0x23 --Malefic
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_DRAW)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_DESTROYED)
  	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--search
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_DESTROYED)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCost(aux.bfgcost)
  	e2:SetCondition(s.thcon)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  end
  function s.cfilter(c, tp)
  	return c:IsPreviousControler(tp)
  		and c:IsPreviousLocation(LOCATION_MZONE)
  		and c:IsPreviousPosition(POS_FACEUP)
  		and c:IsSetCard(SETCARD_ID_0)
  		and (c:IsReason(REASON_BATTLE) or c:IsReason(REASON_EFFECT) and c:GetReasonPlayer() == 1 - tp)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanDraw(tp, 2)
  	end
  	Duel.SetTargetPlayer(tp)
  	Duel.SetTargetParam(2)
  	Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, 2)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local p, d = Duel.GetChainInfo(0, CHAININFO_TARGET_PLAYER, CHAININFO_TARGET_PARAM)
  	Duel.Draw(p, d, REASON_EFFECT)
  end
  function s.cfilter2(c, tp)
  	return c:IsPreviousControler(tp) and c:IsPreviousLocation(LOCATION_MZONE) and c:IsPreviousPosition(POS_FACEUP) and c:IsSetCard(SETCARD_ID_0) and not c:IsReason(REASON_BATTLE)
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter2, 1, nil, tp)
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  
  