name: Crystal
card_text: Send 4 "Crystal Beast" cards from your Spell & Trap Zone to the GY; send as many cards on the field as possible to the GY, then Special Summon as many "Crystal Beast" monsters as possible from your GY, up to the number of cards sent from your opponent's field to the GY by this card's effect.
external_ids: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local SETCARD_ID_0 = 0x1034 --Crystal
  
lua: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local SETCARD_ID_0 = 0x1034 --Crystal
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_TOGRAVE + CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCost(s.cost)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.costfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToGraveAsCost() and c:GetSequence() < 5
  end
  function s.gcheck(g, tg)
  	return tg:FilterCount(aux.TRUE, g) > 0
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local g = Duel.GetMatchingGroup(s.costfilter, tp, LOCATION_SZONE, 0, nil)
  	local tg = Duel.GetMatchingGroup(aux.TRUE, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, e:GetHandler())
  	if chk == 0 then
  		return g:CheckSubGroup(s.gcheck, 4, 4, tg)
  	end
  	local sg = g
  	if #g ~= 4 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  		sg = g:SelectSubGroup(tp, s.gcheck, false, 4, 4, tg)
  	end
  	Duel.SendtoGrave(sg, REASON_COST)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local tg = Duel.GetMatchingGroup(aux.TRUE, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, e:GetHandler())
  	if chk == 0 then
  		return e:IsCostChecked() or #tg > 0
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, tg, #tg, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_GRAVE)
  end
  function s.ctfilter(c, tp)
  	return c:IsPreviousControler(tp) and c:IsLocation(LOCATION_GRAVE)
  end
  function s.spfilter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local tg = Duel.GetMatchingGroup(aux.TRUE, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, aux.ExceptThisCard(e))
  	Duel.SendtoGrave(tg, REASON_EFFECT)
  	local g = Duel.GetOperatedGroup()
  	local ct = g:FilterCount(s.ctfilter, nil, 1 - tp)
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	if ft > ct then
  		ft = ct
  	end
  	if ft <= 0 then
  		return
  	end
  	if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  		ft = 1
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local sg = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_GRAVE, 0, ft, ft, nil, e, tp)
  	if sg:GetCount() > 0 then
  		Duel.BreakEffect()
  		Duel.SpecialSummon(sg, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  
source_text: Crystal
  Send 4 "Crystal Beast" cards from your Spell & Trap Zone to the GY; send as many cards on the field as possible to the GY, then Special Summon as many "Crystal Beast" monsters as possible from your GY, up to the number of cards sent from your opponent's field to the GY by this card's effect.
target_text: -- Crystal
  local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local SETCARD_ID_0 = 0x1034 --Crystal
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_TOGRAVE + CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCost(s.cost)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.costfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToGraveAsCost() and c:GetSequence() < 5
  end
  function s.gcheck(g, tg)
  	return tg:FilterCount(aux.TRUE, g) > 0
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local g = Duel.GetMatchingGroup(s.costfilter, tp, LOCATION_SZONE, 0, nil)
  	local tg = Duel.GetMatchingGroup(aux.TRUE, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, e:GetHandler())
  	if chk == 0 then
  		return g:CheckSubGroup(s.gcheck, 4, 4, tg)
  	end
  	local sg = g
  	if #g ~= 4 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  		sg = g:SelectSubGroup(tp, s.gcheck, false, 4, 4, tg)
  	end
  	Duel.SendtoGrave(sg, REASON_COST)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local tg = Duel.GetMatchingGroup(aux.TRUE, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, e:GetHandler())
  	if chk == 0 then
  		return e:IsCostChecked() or #tg > 0
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, tg, #tg, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_GRAVE)
  end
  function s.ctfilter(c, tp)
  	return c:IsPreviousControler(tp) and c:IsLocation(LOCATION_GRAVE)
  end
  function s.spfilter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local tg = Duel.GetMatchingGroup(aux.TRUE, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, aux.ExceptThisCard(e))
  	Duel.SendtoGrave(tg, REASON_EFFECT)
  	local g = Duel.GetOperatedGroup()
  	local ct = g:FilterCount(s.ctfilter, nil, 1 - tp)
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	if ft > ct then
  		ft = ct
  	end
  	if ft <= 0 then
  		return
  	end
  	if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  		ft = 1
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local sg = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_GRAVE, 0, ft, ft, nil, e, tp)
  	if sg:GetCount() > 0 then
  		Duel.BreakEffect()
  		Duel.SpecialSummon(sg, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  