name: Gatchiri
card_text: The first time each Cyberse monster you control would be destroyed by a card effect each turn, it is not destroyed. You can only use each of the following effects of "Gatchiri @Ignister" once per turn.
  ● You can target 1 Cyberse Effect Monster you control; negate its effects, and if you do, Special Summon this card from your hand.
  ● If this card is sent from the field to your GY: You can target 1 face-up monster you control; that target is unaffected by your opponent's card effects until the end of your opponent's turn.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_DISABLE + CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_IGNITION)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--indes
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_INDESTRUCTABLE_COUNT)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetTargetRange(LOCATION_MZONE, 0)
  	e2:SetTarget(s.indtg)
  	e2:SetValue(s.indct)
  	c:RegisterEffect(e2)
  	--immune
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e3:SetCode(EVENT_TO_GRAVE)
  	e3:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DELAY)
  	e3:SetCountLimit(1, id + 1)
  	e3:SetCondition(s.imcon)
  	e3:SetTarget(s.imtg)
  	e3:SetOperation(s.imop)
  	c:RegisterEffect(e3)
  end
  function s.cfilter(c)
  	return c:IsRace(RACE_CYBERSE) and aux.NegateEffectMonsterFilter(c)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local c = e:GetHandler()
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.cfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false) and Duel.IsExistingTarget(s.cfilter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DISABLE)
  	Duel.SelectTarget(tp, s.cfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, c, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if tc:IsFaceup() and tc:IsRelateToEffect(e) and tc:IsCanBeDisabledByEffect(e) then
  		Duel.NegateRelatedChain(tc, RESET_TURN_SET)
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_DISABLE)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  		tc:RegisterEffect(e1)
  		local e2 = Effect.CreateEffect(c)
  		e2:SetType(EFFECT_TYPE_SINGLE)
  		e2:SetCode(EFFECT_DISABLE_EFFECT)
  		e2:SetValue(RESET_TURN_SET)
  		e2:SetReset(RESET_EVENT + RESETS_STANDARD)
  		tc:RegisterEffect(e2)
  		if c:IsRelateToEffect(e) then
  			Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP)
  		end
  	end
  end
  function s.indtg(e, c)
  	return c:IsFaceup() and c:IsRace(RACE_CYBERSE)
  end
  function s.indct(e, re, r, rp)
  	if bit.band(r, REASON_EFFECT) ~= 0 then
  		return 1
  	else
  		return 0
  	end
  end
  function s.imcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsPreviousLocation(LOCATION_ONFIELD)
  end
  function s.imtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and chkc:IsFaceup()
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(Card.IsFaceup, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, Card.IsFaceup, tp, LOCATION_MZONE, 0, 1, 1, nil)
  end
  function s.imop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsFaceup() and tc:IsControler(tp) and 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 + RESET_OPPO_TURN)
  		e1:SetOwnerPlayer(tp)
  		tc:RegisterEffect(e1)
  	end
  end
  function s.efilter(e, re)
  	return e:GetOwnerPlayer() ~= re:GetOwnerPlayer()
  end
  
  
source_text: Gatchiri
  The first time each Cyberse monster you control would be destroyed by a card effect each turn, it is not destroyed. You can only use each of the following effects of "Gatchiri @Ignister" once per turn.
  ● You can target 1 Cyberse Effect Monster you control; negate its effects, and if you do, Special Summon this card from your hand.
  ● If this card is sent from the field to your GY: You can target 1 face-up monster you control; that target is unaffected by your opponent's card effects until the end of your opponent's turn.
target_text: -- Gatchiri
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_DISABLE + CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_IGNITION)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--indes
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_INDESTRUCTABLE_COUNT)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetTargetRange(LOCATION_MZONE, 0)
  	e2:SetTarget(s.indtg)
  	e2:SetValue(s.indct)
  	c:RegisterEffect(e2)
  	--immune
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e3:SetCode(EVENT_TO_GRAVE)
  	e3:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DELAY)
  	e3:SetCountLimit(1, id + 1)
  	e3:SetCondition(s.imcon)
  	e3:SetTarget(s.imtg)
  	e3:SetOperation(s.imop)
  	c:RegisterEffect(e3)
  end
  function s.cfilter(c)
  	return c:IsRace(RACE_CYBERSE) and aux.NegateEffectMonsterFilter(c)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local c = e:GetHandler()
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.cfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false) and Duel.IsExistingTarget(s.cfilter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DISABLE)
  	Duel.SelectTarget(tp, s.cfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, c, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if tc:IsFaceup() and tc:IsRelateToEffect(e) and tc:IsCanBeDisabledByEffect(e) then
  		Duel.NegateRelatedChain(tc, RESET_TURN_SET)
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_DISABLE)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  		tc:RegisterEffect(e1)
  		local e2 = Effect.CreateEffect(c)
  		e2:SetType(EFFECT_TYPE_SINGLE)
  		e2:SetCode(EFFECT_DISABLE_EFFECT)
  		e2:SetValue(RESET_TURN_SET)
  		e2:SetReset(RESET_EVENT + RESETS_STANDARD)
  		tc:RegisterEffect(e2)
  		if c:IsRelateToEffect(e) then
  			Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP)
  		end
  	end
  end
  function s.indtg(e, c)
  	return c:IsFaceup() and c:IsRace(RACE_CYBERSE)
  end
  function s.indct(e, re, r, rp)
  	if bit.band(r, REASON_EFFECT) ~= 0 then
  		return 1
  	else
  		return 0
  	end
  end
  function s.imcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsPreviousLocation(LOCATION_ONFIELD)
  end
  function s.imtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and chkc:IsFaceup()
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(Card.IsFaceup, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, Card.IsFaceup, tp, LOCATION_MZONE, 0, 1, 1, nil)
  end
  function s.imop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsFaceup() and tc:IsControler(tp) and 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 + RESET_OPPO_TURN)
  		e1:SetOwnerPlayer(tp)
  		tc:RegisterEffect(e1)
  	end
  end
  function s.efilter(e, re)
  	return e:GetOwnerPlayer() ~= re:GetOwnerPlayer()
  end
  
  