name: Traptrix
card_text: 2 Level 4 monsters
  While this card has material, it is unaffected by Trap effects and the activated effects of other monsters with the same Type as this card's material. You can only use each of the following effects of "Traptrix Pinguicula" once per turn. You can detach 1 material from this card; add 1 "Traptrix" monster from your Deck to your hand. If a monster(s) owned by your opponent is sent to the GY, or banished, by a card effect (except during the Damage Step): You can attach 1 of those monsters to this card as material.
external_ids: local SETCARD_ID_0 = 0x108a --Traptrix
  
lua: local SETCARD_ID_0 = 0x108a --Traptrix
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--xyz summon
  	aux.AddXyzProcedure(c, nil, 4, 2)
  	c:EnableReviveLimit()
  	--immune
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCode(EFFECT_IMMUNE_EFFECT)
  	e1:SetCondition(s.imcon)
  	e1:SetValue(s.efilter)
  	c:RegisterEffect(e1)
  	--Search
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetCost(s.thcost)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  	--material
  	local custom_code = aux.RegisterMergedDelayedEvent_ToSingleCard(c, id, { EVENT_TO_GRAVE, EVENT_REMOVE })
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e3:SetProperty(EFFECT_FLAG_DELAY)
  	e3:SetCode(custom_code)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCountLimit(1, id + o)
  	e3:SetTarget(s.mttg)
  	e3:SetOperation(s.mtop)
  	c:RegisterEffect(e3)
  end
  function s.imcon(e)
  	local c = e:GetHandler()
  	return c:GetOverlayCount() > 0
  end
  function s.efilter(e, re)
  	if re:IsActiveType(TYPE_TRAP) then
  		return true
  	end
  	local g = e:GetHandler():GetOverlayGroup():Filter(Card.IsType, nil, TYPE_MONSTER)
  	local race = 0
  	for tc in aux.Next(g) do
  		race = race | tc:GetOriginalRace()
  	end
  	local rc = re:GetHandler()
  	return re:GetOwner() ~= e:GetOwner() and race ~= 0 and rc:IsRace(race) and re:IsActivated()
  end
  function s.thcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():CheckRemoveOverlayCard(tp, 1, REASON_COST)
  	end
  	e:GetHandler():RemoveOverlayCard(tp, 1, 1, REASON_COST)
  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
  function s.cfilter(c, tp)
  	return not c:IsType(TYPE_TOKEN)
  		and c:IsType(TYPE_MONSTER)
  		and c:GetOwner() == 1 - tp
  		and c:IsReason(REASON_EFFECT + REASON_REDIRECT)
  		and c:IsLocation(LOCATION_GRAVE + LOCATION_REMOVED)
  		and c:IsFaceupEx()
  		and c:IsCanOverlay()
  end
  function s.mttg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local g = eg:Filter(s.cfilter, nil, tp)
  	if chk == 0 then
  		return e:GetHandler():IsType(TYPE_XYZ) and #g > 0
  	end
  	Duel.SetTargetCard(g)
  end
  function s.mtop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local g = eg:Filter(s.cfilter, nil, tp)
  	local mg = g:Filter(aux.NecroValleyFilter(Card.IsRelateToChain), nil)
  	if #mg > 0 and c:IsRelateToChain() then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_XMATERIAL)
  		local og = mg:Select(tp, 1, 1, nil)
  		Duel.Overlay(c, og)
  	end
  end
  
  
source_text: Traptrix
  2 Level 4 monsters
  While this card has material, it is unaffected by Trap effects and the activated effects of other monsters with the same Type as this card's material. You can only use each of the following effects of "Traptrix Pinguicula" once per turn. You can detach 1 material from this card; add 1 "Traptrix" monster from your Deck to your hand. If a monster(s) owned by your opponent is sent to the GY, or banished, by a card effect (except during the Damage Step): You can attach 1 of those monsters to this card as material.
target_text: -- Traptrix
  local SETCARD_ID_0 = 0x108a --Traptrix
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--xyz summon
  	aux.AddXyzProcedure(c, nil, 4, 2)
  	c:EnableReviveLimit()
  	--immune
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCode(EFFECT_IMMUNE_EFFECT)
  	e1:SetCondition(s.imcon)
  	e1:SetValue(s.efilter)
  	c:RegisterEffect(e1)
  	--Search
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetCost(s.thcost)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  	--material
  	local custom_code = aux.RegisterMergedDelayedEvent_ToSingleCard(c, id, { EVENT_TO_GRAVE, EVENT_REMOVE })
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e3:SetProperty(EFFECT_FLAG_DELAY)
  	e3:SetCode(custom_code)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCountLimit(1, id + o)
  	e3:SetTarget(s.mttg)
  	e3:SetOperation(s.mtop)
  	c:RegisterEffect(e3)
  end
  function s.imcon(e)
  	local c = e:GetHandler()
  	return c:GetOverlayCount() > 0
  end
  function s.efilter(e, re)
  	if re:IsActiveType(TYPE_TRAP) then
  		return true
  	end
  	local g = e:GetHandler():GetOverlayGroup():Filter(Card.IsType, nil, TYPE_MONSTER)
  	local race = 0
  	for tc in aux.Next(g) do
  		race = race | tc:GetOriginalRace()
  	end
  	local rc = re:GetHandler()
  	return re:GetOwner() ~= e:GetOwner() and race ~= 0 and rc:IsRace(race) and re:IsActivated()
  end
  function s.thcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():CheckRemoveOverlayCard(tp, 1, REASON_COST)
  	end
  	e:GetHandler():RemoveOverlayCard(tp, 1, 1, REASON_COST)
  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
  function s.cfilter(c, tp)
  	return not c:IsType(TYPE_TOKEN)
  		and c:IsType(TYPE_MONSTER)
  		and c:GetOwner() == 1 - tp
  		and c:IsReason(REASON_EFFECT + REASON_REDIRECT)
  		and c:IsLocation(LOCATION_GRAVE + LOCATION_REMOVED)
  		and c:IsFaceupEx()
  		and c:IsCanOverlay()
  end
  function s.mttg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local g = eg:Filter(s.cfilter, nil, tp)
  	if chk == 0 then
  		return e:GetHandler():IsType(TYPE_XYZ) and #g > 0
  	end
  	Duel.SetTargetCard(g)
  end
  function s.mtop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local g = eg:Filter(s.cfilter, nil, tp)
  	local mg = g:Filter(aux.NecroValleyFilter(Card.IsRelateToChain), nil)
  	if #mg > 0 and c:IsRelateToChain() then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_XMATERIAL)
  		local og = mg:Select(tp, 1, 1, nil)
  		Duel.Overlay(c, og)
  	end
  end
  
  