name: Munin
card_text: 2 "Runick" monsters
  If this card is Special Summoned from the Extra Deck: You can discard 1 card; add 1 "Runick" Continuous Spell from your Deck to your hand. When your opponent activates a card or effect that targets a "Runick" card(s) you control or a Set card(s) you control (Quick Effect): You can banish this card you control; negate the activation, and if you do, destroy that card. Once per turn, during the End Phase: Gain 1000 LP.
external_ids: local SETCARD_ID_0 = 0x17f --Runick
  
lua: local SETCARD_ID_0 = 0x17f --Runick
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--fusion material
  	c:EnableReviveLimit()
  	aux.AddFusionProcFunRep(c, aux.FilterBoolFunction(Card.IsFusionSetCard, SETCARD_ID_0), 2, true)
  	--tohand
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetProperty(EFFECT_FLAG_DELAY)
  	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e1:SetCost(s.thcost)
  	e1:SetCondition(s.thcon)
  	e1:SetTarget(s.thtg)
  	e1:SetOperation(s.thop)
  	c:RegisterEffect(e1)
  	--negate
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_NEGATE + CATEGORY_DESTROY)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DAMAGE_CAL)
  	e2:SetCode(EVENT_CHAINING)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCondition(s.discon)
  	e2:SetCost(aux.bfgcost)
  	e2:SetTarget(s.distg)
  	e2:SetOperation(s.disop)
  	c:RegisterEffect(e2)
  	--Recover
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 2))
  	e3:SetCategory(CATEGORY_RECOVER)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e3:SetCode(EVENT_PHASE + PHASE_END)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e3:SetCountLimit(1)
  	e3:SetTarget(s.rectg)
  	e3:SetOperation(s.recop)
  	c:RegisterEffect(e3)
  end
  function s.thcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(Card.IsDiscardable, tp, LOCATION_HAND, 0, 1, nil)
  	end
  	Duel.DiscardHand(tp, Card.IsDiscardable, 1, 1, REASON_COST + REASON_DISCARD)
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsSummonLocation(LOCATION_EXTRA)
  end
  function s.thfilter(c)
  	return c:IsType(TYPE_CONTINUOUS) and c:IsType(TYPE_SPELL) and c:IsSetCard(SETCARD_ID_0) 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 > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  function s.tfilter(c, tp)
  	return c:IsOnField() and c:IsControler(tp) and (c:IsSetCard(SETCARD_ID_0) or c:IsFacedown())
  end
  function s.discon(e, tp, eg, ep, ev, re, r, rp)
  	if rp == tp or e:GetHandler():IsStatus(STATUS_BATTLE_DESTROYED) then
  		return false
  	end
  	if not re:IsHasProperty(EFFECT_FLAG_CARD_TARGET) then
  		return false
  	end
  	local tg = Duel.GetChainInfo(ev, CHAININFO_TARGET_CARDS)
  	return tg and tg:IsExists(s.tfilter, 1, nil, tp) and Duel.IsChainNegatable(ev)
  end
  function s.distg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_NEGATE, eg, 1, 0, 0)
  	if re:GetHandler():IsDestructable() and re:GetHandler():IsRelateToEffect(re) then
  		Duel.SetOperationInfo(0, CATEGORY_DESTROY, eg, 1, 0, 0)
  	end
  end
  function s.disop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.NegateActivation(ev) and re:GetHandler():IsRelateToEffect(re) then
  		Duel.Destroy(eg, REASON_EFFECT)
  	end
  end
  function s.rectg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetTargetPlayer(tp)
  	Duel.SetTargetParam(1000)
  	Duel.SetOperationInfo(0, CATEGORY_RECOVER, nil, 0, tp, 1000)
  end
  function s.recop(e, tp, eg, ep, ev, re, r, rp)
  	local p, d = Duel.GetChainInfo(0, CHAININFO_TARGET_PLAYER, CHAININFO_TARGET_PARAM)
  	Duel.Recover(p, d, REASON_EFFECT)
  end
  
  
source_text: Munin
  2 "Runick" monsters
  If this card is Special Summoned from the Extra Deck: You can discard 1 card; add 1 "Runick" Continuous Spell from your Deck to your hand. When your opponent activates a card or effect that targets a "Runick" card(s) you control or a Set card(s) you control (Quick Effect): You can banish this card you control; negate the activation, and if you do, destroy that card. Once per turn, during the End Phase: Gain 1000 LP.
target_text: -- Munin
  local SETCARD_ID_0 = 0x17f --Runick
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--fusion material
  	c:EnableReviveLimit()
  	aux.AddFusionProcFunRep(c, aux.FilterBoolFunction(Card.IsFusionSetCard, SETCARD_ID_0), 2, true)
  	--tohand
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetProperty(EFFECT_FLAG_DELAY)
  	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e1:SetCost(s.thcost)
  	e1:SetCondition(s.thcon)
  	e1:SetTarget(s.thtg)
  	e1:SetOperation(s.thop)
  	c:RegisterEffect(e1)
  	--negate
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_NEGATE + CATEGORY_DESTROY)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DAMAGE_CAL)
  	e2:SetCode(EVENT_CHAINING)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCondition(s.discon)
  	e2:SetCost(aux.bfgcost)
  	e2:SetTarget(s.distg)
  	e2:SetOperation(s.disop)
  	c:RegisterEffect(e2)
  	--Recover
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 2))
  	e3:SetCategory(CATEGORY_RECOVER)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e3:SetCode(EVENT_PHASE + PHASE_END)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e3:SetCountLimit(1)
  	e3:SetTarget(s.rectg)
  	e3:SetOperation(s.recop)
  	c:RegisterEffect(e3)
  end
  function s.thcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(Card.IsDiscardable, tp, LOCATION_HAND, 0, 1, nil)
  	end
  	Duel.DiscardHand(tp, Card.IsDiscardable, 1, 1, REASON_COST + REASON_DISCARD)
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsSummonLocation(LOCATION_EXTRA)
  end
  function s.thfilter(c)
  	return c:IsType(TYPE_CONTINUOUS) and c:IsType(TYPE_SPELL) and c:IsSetCard(SETCARD_ID_0) 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 > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  function s.tfilter(c, tp)
  	return c:IsOnField() and c:IsControler(tp) and (c:IsSetCard(SETCARD_ID_0) or c:IsFacedown())
  end
  function s.discon(e, tp, eg, ep, ev, re, r, rp)
  	if rp == tp or e:GetHandler():IsStatus(STATUS_BATTLE_DESTROYED) then
  		return false
  	end
  	if not re:IsHasProperty(EFFECT_FLAG_CARD_TARGET) then
  		return false
  	end
  	local tg = Duel.GetChainInfo(ev, CHAININFO_TARGET_CARDS)
  	return tg and tg:IsExists(s.tfilter, 1, nil, tp) and Duel.IsChainNegatable(ev)
  end
  function s.distg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_NEGATE, eg, 1, 0, 0)
  	if re:GetHandler():IsDestructable() and re:GetHandler():IsRelateToEffect(re) then
  		Duel.SetOperationInfo(0, CATEGORY_DESTROY, eg, 1, 0, 0)
  	end
  end
  function s.disop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.NegateActivation(ev) and re:GetHandler():IsRelateToEffect(re) then
  		Duel.Destroy(eg, REASON_EFFECT)
  	end
  end
  function s.rectg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetTargetPlayer(tp)
  	Duel.SetTargetParam(1000)
  	Duel.SetOperationInfo(0, CATEGORY_RECOVER, nil, 0, tp, 1000)
  end
  function s.recop(e, tp, eg, ep, ev, re, r, rp)
  	local p, d = Duel.GetChainInfo(0, CHAININFO_TARGET_PLAYER, CHAININFO_TARGET_PARAM)
  	Duel.Recover(p, d, REASON_EFFECT)
  end
  
  