name: Salamangreat
card_text: If a Cyberse monster(s) you control that was Special Summoned from the Extra Deck is destroyed by battle or an opponent's card effect: You can Special Summon this card from the GY (if it was there when the monster was destroyed) or hand (even if not), but banish it when it leaves the field. If this card is sent to the GY as material for the Link Summon of a Link-2 or higher Cyberse Link Monster: You can add 1 "Salamangreat" Normal Spell from your Deck to your hand. You can only use each effect of "Salamangreat Fennec" once per turn.
external_ids: local SETCARD_ID_0 = 0x119 --Salamangreat
  
lua: local SETCARD_ID_0 = 0x119 --Salamangreat
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY)
  	e1:SetCode(EVENT_DESTROYED)
  	e1:SetRange(LOCATION_HAND + LOCATION_GRAVE)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--search
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCode(EVENT_BE_MATERIAL)
  	e2:SetCountLimit(1, id + 1)
  	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 bit.band(c:GetPreviousRaceOnField(), RACE_CYBERSE) ~= 0
  		and c:IsSummonLocation(LOCATION_EXTRA)
  		and (c:IsReason(REASON_BATTLE) or c:IsReason(REASON_EFFECT) and c:GetReasonPlayer() == 1 - tp)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, e:GetHandler(), tp)
  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, false, false)
  	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 c:IsRelateToEffect(e) and Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP) > 0 then
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_LEAVE_FIELD_REDIRECT)
  		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  		e1:SetReset(RESET_EVENT + RESETS_REDIRECT)
  		e1:SetValue(LOCATION_REMOVED)
  		c:RegisterEffect(e1, true)
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local rc = c:GetReasonCard()
  	return c:IsLocation(LOCATION_GRAVE) and r == REASON_LINK and rc:IsRace(RACE_CYBERSE) and rc:IsLinkAbove(2)
  end
  function s.thfilter(c)
  	return c:GetType() == 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:GetCount() > 0 then
  		Duel.SendtoHand(g, tp, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  
  
source_text: Salamangreat
  If a Cyberse monster(s) you control that was Special Summoned from the Extra Deck is destroyed by battle or an opponent's card effect: You can Special Summon this card from the GY (if it was there when the monster was destroyed) or hand (even if not), but banish it when it leaves the field. If this card is sent to the GY as material for the Link Summon of a Link-2 or higher Cyberse Link Monster: You can add 1 "Salamangreat" Normal Spell from your Deck to your hand. You can only use each effect of "Salamangreat Fennec" once per turn.
target_text: -- Salamangreat
  local SETCARD_ID_0 = 0x119 --Salamangreat
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY)
  	e1:SetCode(EVENT_DESTROYED)
  	e1:SetRange(LOCATION_HAND + LOCATION_GRAVE)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--search
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCode(EVENT_BE_MATERIAL)
  	e2:SetCountLimit(1, id + 1)
  	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 bit.band(c:GetPreviousRaceOnField(), RACE_CYBERSE) ~= 0
  		and c:IsSummonLocation(LOCATION_EXTRA)
  		and (c:IsReason(REASON_BATTLE) or c:IsReason(REASON_EFFECT) and c:GetReasonPlayer() == 1 - tp)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, e:GetHandler(), tp)
  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, false, false)
  	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 c:IsRelateToEffect(e) and Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP) > 0 then
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_LEAVE_FIELD_REDIRECT)
  		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  		e1:SetReset(RESET_EVENT + RESETS_REDIRECT)
  		e1:SetValue(LOCATION_REMOVED)
  		c:RegisterEffect(e1, true)
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local rc = c:GetReasonCard()
  	return c:IsLocation(LOCATION_GRAVE) and r == REASON_LINK and rc:IsRace(RACE_CYBERSE) and rc:IsLinkAbove(2)
  end
  function s.thfilter(c)
  	return c:GetType() == 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:GetCount() > 0 then
  		Duel.SendtoHand(g, tp, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  
  