name: Crystron
card_text: "Crystron" cards you control cannot be banished by your opponent's card effects. You can target 1 face-up card on the field, or you can target 2 if you control a "Crystron" Synchro Monster; shuffle 1 "Crystron" card from your GY or banishment into the Deck, except "Crystron Cluster", and if you do, destroy the targeted card(s). You can only use this effect of "Crystron Cluster" once per turn.
external_ids: local SETCARD_ID_0 = 0xea --Crystron
  
lua: local SETCARD_ID_0 = 0xea --Crystron
  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)
  	c:RegisterEffect(e1)
  	--Cannot Banish
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_CANNOT_REMOVE)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e2:SetTargetRange(1, 1)
  	e2:SetTarget(s.rmlimit)
  	c:RegisterEffect(e2)
  	--destroy
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 0))
  	e3:SetCategory(CATEGORY_TODECK + CATEGORY_DESTROY)
  	e3:SetType(EFFECT_TYPE_QUICK_O)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetCode(EVENT_FREE_CHAIN)
  	e3:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e3:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e3:SetCountLimit(1, id)
  	e3:SetTarget(s.destg)
  	e3:SetOperation(s.desop)
  	c:RegisterEffect(e3)
  end
  function s.rmlimit(e, c, rp, r, re)
  	local tp = e:GetHandlerPlayer()
  	return c:IsControler(tp) and c:IsOnField() and c:IsSetCard(SETCARD_ID_0) and c:IsFaceup() and r & REASON_EFFECT ~= 0 and r & REASON_REDIRECT == 0 and rp == 1 - tp
  end
  function s.cfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SYNCHRO)
  end
  function s.tdfilter(c)
  	return not c:IsCode(id) and c:IsFaceupEx() and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToDeck()
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsOnField() and chkc:IsFaceup()
  	end
  	local xg = nil
  	if not e:GetHandler():IsStatus(STATUS_EFFECT_ENABLED) then
  		xg = e:GetHandler()
  	end
  	local ct = 1
  	if Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 1, nil) then
  		ct = 2
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(Card.IsFaceup, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, xg) and Duel.IsExistingMatchingCard(s.tdfilter, tp, LOCATION_GRAVE + LOCATION_REMOVED, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  	local g = Duel.SelectTarget(tp, Card.IsFaceup, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, ct, xg)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, g, g:GetCount(), 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, nil, 1, tp, LOCATION_GRAVE + LOCATION_REMOVED)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS)
  	local tg = g:Filter(Card.IsRelateToEffect, nil, e)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TODECK)
  	local dg = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.tdfilter), tp, LOCATION_GRAVE + LOCATION_REMOVED, 0, 1, 1, nil)
  	if dg:GetCount() > 0 then
  		Duel.HintSelection(dg)
  		if Duel.SendtoDeck(dg, nil, SEQ_DECKSHUFFLE, REASON_EFFECT) ~= 0 and dg:FilterCount(Card.IsLocation, nil, LOCATION_DECK + LOCATION_EXTRA) > 0 and tg:GetCount() > 0 then
  			Duel.Destroy(tg, REASON_EFFECT)
  		end
  	end
  end
  
  
source_text: Crystron
  "Crystron" cards you control cannot be banished by your opponent's card effects. You can target 1 face-up card on the field, or you can target 2 if you control a "Crystron" Synchro Monster; shuffle 1 "Crystron" card from your GY or banishment into the Deck, except "Crystron Cluster", and if you do, destroy the targeted card(s). You can only use this effect of "Crystron Cluster" once per turn.
target_text: -- Crystron
  local SETCARD_ID_0 = 0xea --Crystron
  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)
  	c:RegisterEffect(e1)
  	--Cannot Banish
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_CANNOT_REMOVE)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e2:SetTargetRange(1, 1)
  	e2:SetTarget(s.rmlimit)
  	c:RegisterEffect(e2)
  	--destroy
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 0))
  	e3:SetCategory(CATEGORY_TODECK + CATEGORY_DESTROY)
  	e3:SetType(EFFECT_TYPE_QUICK_O)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetCode(EVENT_FREE_CHAIN)
  	e3:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e3:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e3:SetCountLimit(1, id)
  	e3:SetTarget(s.destg)
  	e3:SetOperation(s.desop)
  	c:RegisterEffect(e3)
  end
  function s.rmlimit(e, c, rp, r, re)
  	local tp = e:GetHandlerPlayer()
  	return c:IsControler(tp) and c:IsOnField() and c:IsSetCard(SETCARD_ID_0) and c:IsFaceup() and r & REASON_EFFECT ~= 0 and r & REASON_REDIRECT == 0 and rp == 1 - tp
  end
  function s.cfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SYNCHRO)
  end
  function s.tdfilter(c)
  	return not c:IsCode(id) and c:IsFaceupEx() and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToDeck()
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsOnField() and chkc:IsFaceup()
  	end
  	local xg = nil
  	if not e:GetHandler():IsStatus(STATUS_EFFECT_ENABLED) then
  		xg = e:GetHandler()
  	end
  	local ct = 1
  	if Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 1, nil) then
  		ct = 2
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(Card.IsFaceup, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, xg) and Duel.IsExistingMatchingCard(s.tdfilter, tp, LOCATION_GRAVE + LOCATION_REMOVED, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  	local g = Duel.SelectTarget(tp, Card.IsFaceup, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, ct, xg)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, g, g:GetCount(), 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, nil, 1, tp, LOCATION_GRAVE + LOCATION_REMOVED)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS)
  	local tg = g:Filter(Card.IsRelateToEffect, nil, e)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TODECK)
  	local dg = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.tdfilter), tp, LOCATION_GRAVE + LOCATION_REMOVED, 0, 1, 1, nil)
  	if dg:GetCount() > 0 then
  		Duel.HintSelection(dg)
  		if Duel.SendtoDeck(dg, nil, SEQ_DECKSHUFFLE, REASON_EFFECT) ~= 0 and dg:FilterCount(Card.IsLocation, nil, LOCATION_DECK + LOCATION_EXTRA) > 0 and tg:GetCount() > 0 then
  			Duel.Destroy(tg, REASON_EFFECT)
  		end
  	end
  end
  
  