name: Altergeist
card_text: 2 "Altergeist" monsters
  Gains ATK equal to the original ATK of each "Altergeist" monster it points to. When a Spell/Trap Card or effect is activated (Quick Effect): You can Tribute 1 "Altergeist" monster this card points to; negate the activation, and if you do, destroy that card. If this card is sent from the field to the GY: You can add 1 "Altergeist" card from your Deck to your hand. You can only use this effect of "Altergeist Hexstia" once per turn.
external_ids: local SETCARD_ID_0 = 0x103 --Altergeist
  
lua: local SETCARD_ID_0 = 0x103 --Altergeist
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableReviveLimit()
  	aux.AddLinkProcedure(c, aux.FilterBoolFunction(Card.IsLinkSetCard, SETCARD_ID_0), 2, 2)
  	--atk
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetCode(EFFECT_UPDATE_ATTACK)
  	e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetValue(s.atkval)
  	c:RegisterEffect(e1)
  	--negate
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_NEGATE + CATEGORY_DESTROY)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_CHAINING)
  	e2:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DAMAGE_CAL)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCondition(s.discon)
  	e2:SetCost(s.discost)
  	e2:SetTarget(s.distg)
  	e2:SetOperation(s.disop)
  	c:RegisterEffect(e2)
  	--search
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e3:SetProperty(EFFECT_FLAG_DELAY)
  	e3:SetCode(EVENT_TO_GRAVE)
  	e3:SetCountLimit(1, id)
  	e3:SetCondition(s.thcon)
  	e3:SetTarget(s.thtg)
  	e3:SetOperation(s.thop)
  	c:RegisterEffect(e3)
  end
  function s.atkfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:GetBaseAttack() >= 0
  end
  function s.atkval(e, c)
  	local lg = c:GetLinkedGroup():Filter(s.atkfilter, nil)
  	return lg:GetSum(Card.GetBaseAttack)
  end
  function s.discon(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetHandler():IsStatus(STATUS_BATTLE_DESTROYED) then
  		return false
  	end
  	return re:IsActiveType(TYPE_SPELL + TYPE_TRAP) and Duel.IsChainNegatable(ev)
  end
  function s.cfilter(c, g)
  	return c:IsSetCard(SETCARD_ID_0) and g:IsContains(c) and not c:IsStatus(STATUS_BATTLE_DESTROYED)
  end
  function s.discost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local lg = e:GetHandler():GetLinkedGroup()
  	if chk == 0 then
  		return Duel.CheckReleaseGroup(tp, s.cfilter, 1, nil, lg)
  	end
  	local g = Duel.SelectReleaseGroup(tp, s.cfilter, 1, 1, nil, lg)
  	Duel.Release(g, REASON_COST)
  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.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsPreviousLocation(LOCATION_ONFIELD)
  end
  function s.thfilter(c)
  	return 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:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  
  
source_text: Altergeist
  2 "Altergeist" monsters
  Gains ATK equal to the original ATK of each "Altergeist" monster it points to. When a Spell/Trap Card or effect is activated (Quick Effect): You can Tribute 1 "Altergeist" monster this card points to; negate the activation, and if you do, destroy that card. If this card is sent from the field to the GY: You can add 1 "Altergeist" card from your Deck to your hand. You can only use this effect of "Altergeist Hexstia" once per turn.
target_text: -- Altergeist
  local SETCARD_ID_0 = 0x103 --Altergeist
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableReviveLimit()
  	aux.AddLinkProcedure(c, aux.FilterBoolFunction(Card.IsLinkSetCard, SETCARD_ID_0), 2, 2)
  	--atk
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetCode(EFFECT_UPDATE_ATTACK)
  	e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetValue(s.atkval)
  	c:RegisterEffect(e1)
  	--negate
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_NEGATE + CATEGORY_DESTROY)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_CHAINING)
  	e2:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DAMAGE_CAL)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCondition(s.discon)
  	e2:SetCost(s.discost)
  	e2:SetTarget(s.distg)
  	e2:SetOperation(s.disop)
  	c:RegisterEffect(e2)
  	--search
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e3:SetProperty(EFFECT_FLAG_DELAY)
  	e3:SetCode(EVENT_TO_GRAVE)
  	e3:SetCountLimit(1, id)
  	e3:SetCondition(s.thcon)
  	e3:SetTarget(s.thtg)
  	e3:SetOperation(s.thop)
  	c:RegisterEffect(e3)
  end
  function s.atkfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:GetBaseAttack() >= 0
  end
  function s.atkval(e, c)
  	local lg = c:GetLinkedGroup():Filter(s.atkfilter, nil)
  	return lg:GetSum(Card.GetBaseAttack)
  end
  function s.discon(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetHandler():IsStatus(STATUS_BATTLE_DESTROYED) then
  		return false
  	end
  	return re:IsActiveType(TYPE_SPELL + TYPE_TRAP) and Duel.IsChainNegatable(ev)
  end
  function s.cfilter(c, g)
  	return c:IsSetCard(SETCARD_ID_0) and g:IsContains(c) and not c:IsStatus(STATUS_BATTLE_DESTROYED)
  end
  function s.discost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local lg = e:GetHandler():GetLinkedGroup()
  	if chk == 0 then
  		return Duel.CheckReleaseGroup(tp, s.cfilter, 1, nil, lg)
  	end
  	local g = Duel.SelectReleaseGroup(tp, s.cfilter, 1, 1, nil, lg)
  	Duel.Release(g, REASON_COST)
  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.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsPreviousLocation(LOCATION_ONFIELD)
  end
  function s.thfilter(c)
  	return 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:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  
  