--ナンバーズ・エヴァイユ
--Numbers Eveil (Manga)
--Rescripted by edo9300
local s,id=GetID()
function s.initial_effect(c)
	--Activate
	local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetTarget(s.target)
	e1:SetOperation(s.activate)
	c:RegisterEffect(e1)
end
s.listed_series={0x48}
function s.spfilter(c,e,p,mg)
	return Duel.GetLocationCountFromEx(p,p,mg,c)>0 and c:IsCanBeSpecialSummoned(e,SUMMON_TYPE_XYZ,p,false,false)
end
function s.filter(c,mg,mg0,p,e)
	if not c:IsSetCard(SET_NUMBER) or not c:IsType(TYPE_XYZ) or type(c.xyz_number)~="number" or not s.spfilter(c,e,p,mg-c) then return false end
	if c.xyz_number==0 then
		return #mg0>0
	else
		return (mg-c):CheckWithSumEqual(function(c)return c.xyz_number end,c.xyz_number,1,#mg)
	end
end
function s.target(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then
		local pg=aux.GetMustBeMaterialGroup(tp,Group.CreateGroup(),tp,nil,nil,REASON_XYZ)
		local mg,mg0=Duel.GetMatchingGroup(function(c) return type(c.xyz_number)=="number" end,tp,LOCATION_EXTRA,0,nil):Split(function(c)return c.xyz_number>0 end,nil)
		if (#pg-(#mg+#mg0))>0 then return false end
		return Duel.IsExistingMatchingCard(s.filter,tp,LOCATION_EXTRA,0,1,nil,mg,mg0,tp,e)
	end
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,0,LOCATION_EXTRA)
end
function s.checkvalid(c,mg,sg,mg0,cc,p,e)
	Duel.SetSelectedCard((sg+cc)-mg0)
	return s.spfilter(c,e,p,mg-c) and (mg-c):CheckWithSumEqual(function(c)return c.xyz_number end,c.xyz_number,0,99999)
end
function s.rescon(sg,e,tp,mg)
	return mg:IsExists(s.spfilter,1,nil,e,tp,sg)
end
function s.activate(e,tp,eg,ep,ev,re,r,rp)
	local pg=aux.GetMustBeMaterialGroup(tp,Group.CreateGroup(),tp,nil,nil,REASON_XYZ)
	local mg,mg0=Duel.GetMatchingGroup(function(c) return type(c.xyz_number)=="number" end,tp,LOCATION_EXTRA,0,nil):Split(function(c)return c.xyz_number>0 end,nil)
	if (#pg-(#mg+#mg0))>0 then return false end
	local mat=Group.CreateGroup()
	if #mg==0 and #mg0>1 then
		mat=aux.SelectUnselectGroup(mg0,e,tp,1,#mg0-1,s.rescon,1,tp,HINTMSG_XMATERIAL)
	else
		while true do
			local cg=mg:Filter(function(c,mg,sg)
				return mg:IsExists(s.checkvalid,1,c+sg,mg,sg,mg0,c,tp,e)
			end,mat,mg,mat)+(mg0-mat)
			if #cg==0 then break end
			local cancel = ((mg0:Includes(mat) and #(mg0-mat)>0) or mg:IsExists(function(c,mg,sg)
				local without_num0=sg-mg0
				Duel.SetSelectedCard(without_num0)
				return without_num0:CheckWithSumEqual(function(c)return c.xyz_number end,c.xyz_number,0,0)
			end,1,mat,mg,mat)) and #mat>0
			Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_XMATERIAL)
			local tc=cg:SelectUnselect(mat,tp,cancel,cancel)
			if not tc then break end
			if mat:IsContains(tc) then
				mat=mat-tc
			else
				mat=mat+tc
			end
		end
	end
	local spc
	if mg0:Includes(mat) and #(mg0-mat)>0 then
		spc=mg0:Select(tp,1,1,mat):GetFirst()
	else
		spc=mg:FilterSelect(tp,function(c,mat)
			Duel.SetSelectedCard(mat-mg0)
			return s.spfilter(c,e,tp,mat) and Group.CreateGroup():CheckWithSumEqual(function(c)return c.xyz_number end,c.xyz_number,0,0)
		end,1,1,mat,mat):GetFirst()
	end
	if spc and Duel.SpecialSummon(spc,SUMMON_TYPE_XYZ,tp,tp,false,false,POS_FACEUP)>0 then
		Duel.Overlay(spc,mat)
		spc:CompleteProcedure()
	end
end
