name: Salamangreat
card_text: Activate 1 of these effects;
  ● Add 1 "Salamangreat" monster from your Deck to your hand.
  ● Target 1 "Salamangreat" Link Monster you control that was Link Summoned using a monster with its same name as material; that Link Monster is unaffected by monster effects this turn, except its own.
  You can only activate 1 "Salamangreat Circle" 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)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetHintTiming(0, TIMING_END_PHASE)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	if not s.global_check then
  		s.global_check = true
  		local ge1 = Effect.CreateEffect(c)
  		ge1:SetType(EFFECT_TYPE_FIELD)
  		ge1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE + EFFECT_FLAG_SET_AVAILABLE + EFFECT_FLAG_IGNORE_RANGE)
  		ge1:SetCode(EFFECT_MATERIAL_CHECK)
  		ge1:SetValue(s.valcheck)
  		Duel.RegisterEffect(ge1, 0)
  	end
  end
  function s.valcheck(e, c)
  	local g = c:GetMaterial()
  	if g:IsExists(Card.IsLinkCode, 1, nil, c:GetCode()) then
  		c:RegisterFlagEffect(id, RESET_EVENT + 0x4fe0000, 0, 1)
  	end
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.immfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsSummonType(SUMMON_TYPE_LINK) and c:GetFlagEffect(id) ~= 0
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.immfilter(chkc)
  	end
  	local b1 = Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	local b2 = Duel.IsExistingTarget(s.immfilter, tp, LOCATION_MZONE, 0, 1, nil)
  	if chk == 0 then
  		return b1 or b2
  	end
  	local op = 0
  	if b1 and b2 then
  		op = Duel.SelectOption(tp, aux.Stringid(id, 0), aux.Stringid(id, 1))
  	elseif b1 then
  		op = Duel.SelectOption(tp, aux.Stringid(id, 0))
  	else
  		op = Duel.SelectOption(tp, aux.Stringid(id, 1)) + 1
  	end
  	e:SetLabel(op)
  	if op == 0 then
  		e:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  		e:SetProperty(0)
  		Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  	else
  		e:SetCategory(0)
  		e:SetProperty(EFFECT_FLAG_CARD_TARGET)
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_FACEUP)
  		Duel.SelectTarget(tp, s.immfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  	end
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local op = e:GetLabel()
  	if op == 0 then
  		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
  	else
  		local tc = Duel.GetFirstTarget()
  		if tc:IsRelateToEffect(e) then
  			local e1 = Effect.CreateEffect(e:GetHandler())
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_IMMUNE_EFFECT)
  			e1:SetValue(s.efilter)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  			tc:RegisterEffect(e1)
  		end
  	end
  end
  function s.efilter(e, re)
  	return e:GetHandler() ~= re:GetOwner() and re:IsActiveType(TYPE_MONSTER)
  end
  
  
source_text: Salamangreat
  Activate 1 of these effects;
  ● Add 1 "Salamangreat" monster from your Deck to your hand.
  ● Target 1 "Salamangreat" Link Monster you control that was Link Summoned using a monster with its same name as material; that Link Monster is unaffected by monster effects this turn, except its own.
  You can only activate 1 "Salamangreat Circle" per turn.
target_text: -- Salamangreat
  local SETCARD_ID_0 = 0x119 --Salamangreat
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetHintTiming(0, TIMING_END_PHASE)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	if not s.global_check then
  		s.global_check = true
  		local ge1 = Effect.CreateEffect(c)
  		ge1:SetType(EFFECT_TYPE_FIELD)
  		ge1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE + EFFECT_FLAG_SET_AVAILABLE + EFFECT_FLAG_IGNORE_RANGE)
  		ge1:SetCode(EFFECT_MATERIAL_CHECK)
  		ge1:SetValue(s.valcheck)
  		Duel.RegisterEffect(ge1, 0)
  	end
  end
  function s.valcheck(e, c)
  	local g = c:GetMaterial()
  	if g:IsExists(Card.IsLinkCode, 1, nil, c:GetCode()) then
  		c:RegisterFlagEffect(id, RESET_EVENT + 0x4fe0000, 0, 1)
  	end
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.immfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsSummonType(SUMMON_TYPE_LINK) and c:GetFlagEffect(id) ~= 0
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.immfilter(chkc)
  	end
  	local b1 = Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	local b2 = Duel.IsExistingTarget(s.immfilter, tp, LOCATION_MZONE, 0, 1, nil)
  	if chk == 0 then
  		return b1 or b2
  	end
  	local op = 0
  	if b1 and b2 then
  		op = Duel.SelectOption(tp, aux.Stringid(id, 0), aux.Stringid(id, 1))
  	elseif b1 then
  		op = Duel.SelectOption(tp, aux.Stringid(id, 0))
  	else
  		op = Duel.SelectOption(tp, aux.Stringid(id, 1)) + 1
  	end
  	e:SetLabel(op)
  	if op == 0 then
  		e:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  		e:SetProperty(0)
  		Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  	else
  		e:SetCategory(0)
  		e:SetProperty(EFFECT_FLAG_CARD_TARGET)
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_FACEUP)
  		Duel.SelectTarget(tp, s.immfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  	end
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local op = e:GetLabel()
  	if op == 0 then
  		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
  	else
  		local tc = Duel.GetFirstTarget()
  		if tc:IsRelateToEffect(e) then
  			local e1 = Effect.CreateEffect(e:GetHandler())
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_IMMUNE_EFFECT)
  			e1:SetValue(s.efilter)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  			tc:RegisterEffect(e1)
  		end
  	end
  end
  function s.efilter(e, re)
  	return e:GetHandler() ~= re:GetOwner() and re:IsActiveType(TYPE_MONSTER)
  end
  
  