--now, load the zone & music data into the script


local zones = {}
local music = {}
local globali

if settings.UseMusicZones == true then
	for i,zone in pairs(zonesfolder:GetChildren()) do
		if zone:IsA("Model") and zone:FindFirstChild("Music") then
			music[i] = {}
			for _,sound in pairs(zone.Music:GetChildren()) do --save all of the music data for this zone
				if sound:IsA("Sound") then
					table.insert(music[i], {["SoundId"] = sound.SoundId, ["Volume"] = sound.Volume, ["Pitch"] = sound.Pitch})
				end
			end

			zones[i] = {
				["Priority"] = zone:FindFirstChild("Priority") and zone.Priority.Value or 1,
				["Parts"] = {},
				["Music"] = music[i]
			}

			for _,part in pairs(zone:GetChildren()) do
				if part:IsA("Part") then

					if IsCleanRotation({part.Rotation.X,part.Rotation.Y,part.Rotation.Z}) == true then --store the min and max X/Y/Z position coordinates of this part
						local lx,ly,lz = math.huge,math.huge,math.huge
						local mx,my,mz = -math.huge,-math.huge,-math.huge
						local function ApplyValues(p)
							if p.x < lx then lx = p.x end
							if p.x > mx then mx = p.x end
							if p.y < ly then ly = p.y end
							if p.y > my then my = p.y end
							if p.z < lz then lz = p.z end
							if p.z > mz then mz = p.z end
						end
						local points = {}
						--a bunch of sloppy math stuff to calculate the coordinates on each of the part's faces so we can figure out the min and max values. mathematicians, pls don't kill me if i'm doing this terribly
						local cf1 = part.CFrame
						local cf2 = (part.CFrame * CFrame.Angles(0,math.pi/2,0))
						local cf3 = (part.CFrame * CFrame.Angles(math.pi/2,0,0))
						local lvz = (cf1.lookVector * (part.Size.Z/2))
						local lvx = (cf2.lookVector * (part.Size.X/2))
						local lvy = (cf3.lookVector * (part.Size.Y/2))
						points[1] = cf1+lvz
						points[2] = cf1-lvz
						points[3] = cf2+lvx
						points[4] = cf2-lvx
						points[5] = cf3+lvy
						points[6] = cf3-lvy
						for _,p in pairs(points) do
							ApplyValues(p)
						end 
						table.insert(zones[i]["Parts"],{["Coordinates"] = {lx=lx,ly=ly,lz=lz, mx=mx,my=my,mz=mz}})
					else --save a reference to the part itself to be used for GetTouchingParts(), since it has a complex rotation that can't be solved with my own feeble calculations
						table.insert(zones[i]["Parts"],{["Part"] = part})
					end

				end
			end
		end
	end
end

if settings.UseGlobalBackgroundMusic == true then
	globali = #music + 1
	music[globali] = {}
	for _,sound in pairs(globalfolder:GetChildren()) do
		if sound:IsA("Sound") then
			table.insert(music[globali], {["SoundId"] = sound.SoundId, ["Volume"] = sound.Volume, ["Pitch"] = sound.Pitch})
		end
	end
end