name: Cipher
card_text: Target 1 "Galaxy-Eyes" or "Cipher" Xyz Monster you control; attach this card to it as an Xyz Material, and if you do, the next time it would be destroyed by battle or card effect this turn, it is not destroyed.
external_ids: local SETCARD_ID_0 = 0x107b --Galaxy-Eyes
  
lua: local SETCARD_ID_0 = 0x107b --Galaxy-Eyes
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.filter(c)
  	return c:IsFaceup() and c:IsType(TYPE_XYZ) and c:IsSetCard(SETCARD_ID_0, 0xe5)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsControler(tp) and chkc:IsLocation(LOCATION_MZONE) and s.filter(chkc)
  	end
  	if chk == 0 then
  		return e:IsHasType(EFFECT_TYPE_ACTIVATE) and e:GetHandler():IsCanOverlay() and Duel.IsExistingTarget(s.filter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.filter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and not tc:IsImmuneToEffect(e) and c:IsRelateToEffect(e) and c:IsCanOverlay() then
  		c:CancelToGrave()
  		Duel.Overlay(tc, Group.FromCards(c))
  		--
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_INDESTRUCTABLE_COUNT)
  		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  		e1:SetCountLimit(1)
  		e1:SetValue(s.indval)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  		tc:RegisterEffect(e1)
  	end
  end
  function s.indval(e, re, r, rp)
  	return bit.band(r, REASON_BATTLE + REASON_EFFECT) ~= 0
  end
  
  
source_text: Cipher
  Target 1 "Galaxy-Eyes" or "Cipher" Xyz Monster you control; attach this card to it as an Xyz Material, and if you do, the next time it would be destroyed by battle or card effect this turn, it is not destroyed.
target_text: -- Cipher
  local SETCARD_ID_0 = 0x107b --Galaxy-Eyes
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.filter(c)
  	return c:IsFaceup() and c:IsType(TYPE_XYZ) and c:IsSetCard(SETCARD_ID_0, 0xe5)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsControler(tp) and chkc:IsLocation(LOCATION_MZONE) and s.filter(chkc)
  	end
  	if chk == 0 then
  		return e:IsHasType(EFFECT_TYPE_ACTIVATE) and e:GetHandler():IsCanOverlay() and Duel.IsExistingTarget(s.filter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.filter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and not tc:IsImmuneToEffect(e) and c:IsRelateToEffect(e) and c:IsCanOverlay() then
  		c:CancelToGrave()
  		Duel.Overlay(tc, Group.FromCards(c))
  		--
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_INDESTRUCTABLE_COUNT)
  		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  		e1:SetCountLimit(1)
  		e1:SetValue(s.indval)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  		tc:RegisterEffect(e1)
  	end
  end
  function s.indval(e, re, r, rp)
  	return bit.band(r, REASON_BATTLE + REASON_EFFECT) ~= 0
  end
  
  