-- clean_shot.levelgen
function setAllLines(teamIndex)
	for _, zoneJunk in ipairs(zoneData) do
		for _, line in ipairs(zoneJunk["lines"]) do
			line:setTeam(teamIndex)
		end
	end
end


function updateLines()
	for _, zoneJunk in ipairs(zoneData) do
		local lines = zoneJunk["lines"]
		local zoneTeam = zoneJunk["zone"]:getTeamIndex()
		
		-- If the zone's team is different than it's lineitems, we need to
		-- update the lineitems
		if zoneTeam ~= lines[1]:getTeamIndex() then
			for _, line in ipairs(lines) do
				line:setTeam(zoneTeam)
			end
		end
	end
end


function onScoreChanged(scoreChange, teamIndex, player)
	local game = bf:getGameInfo()
	local team = game:getTeam(teamIndex)
	local teamScore = team:getScore()

	-- If we've made a touchdown, change everything back to neutral team
	if teamScore % zoneCount == 0 and scoreChange > 0 then
		setAllLines(Team.Neutral)

	-- Else update the line artwork to reflect the proper teams
	else
		-- We have to delay the update one tick because the zones don't seem to
		-- reflect the correct teams when this even is fired.  Bug?
		Timer:scheduleOnce(updateLines, 1)  -- 1 ms is all that's needed
	end
end


function main()
	bf:subscribe(Event.ScoreChanged)
	
	-- Globals
	-- How many zones we have
	zoneCount = 8
	
	-- Load up all of our zoneData into a complex object for fast lookup later
	zoneData = {}
	
	-- Each of the indexes correspond to a goalzone ID. The members:
	--    zone  - the zone object itself
	--    lines - the lineitem objects that will change team when the zone changes
	--            team
	zoneData = 
	{
		{
			["zone"] = bf:findObjectById(72),
			["lines"] = {
				bf:findObjectById(1),
				bf:findObjectById(2),
				bf:findObjectById(3),
				bf:findObjectById(4),
				bf:findObjectById(5),
				bf:findObjectById(6),
				bf:findObjectById(7),
				bf:findObjectById(8),
				bf:findObjectById(9),
				bf:findObjectById(10),
			}
		},
		{
			["zone"] = bf:findObjectById(73),
			["lines"] = {
				bf:findObjectById(11),
				bf:findObjectById(12),
				bf:findObjectById(13),
				bf:findObjectById(14),
				bf:findObjectById(15),
				bf:findObjectById(16),
				bf:findObjectById(17),
				bf:findObjectById(18),
				bf:findObjectById(19),
				bf:findObjectById(20),
			}
		},
		{
			["zone"] = bf:findObjectById(74),
			["lines"] = {
				bf:findObjectById(21),
				bf:findObjectById(22),
				bf:findObjectById(23),
				bf:findObjectById(24),
				bf:findObjectById(25),
				bf:findObjectById(26),
				bf:findObjectById(80),
				bf:findObjectById(81),
				bf:findObjectById(82),
			}
		},
		{
			["zone"] = bf:findObjectById(75),
			["lines"] = {
				bf:findObjectById(27),
				bf:findObjectById(28),
				bf:findObjectById(29),
				bf:findObjectById(30),
				bf:findObjectById(31),
				bf:findObjectById(32),
				bf:findObjectById(33),
				bf:findObjectById(34),
				bf:findObjectById(35),
				bf:findObjectById(36),
			}
		},
		{
			["zone"] = bf:findObjectById(76),
			["lines"] = {
				bf:findObjectById(37),
				bf:findObjectById(38),
				bf:findObjectById(39),
				bf:findObjectById(40),
				bf:findObjectById(41),
				bf:findObjectById(42),
				bf:findObjectById(43),
				bf:findObjectById(44),
				bf:findObjectById(45),
				bf:findObjectById(46),
				bf:findObjectById(47),
			}
		},
		{
			["zone"] = bf:findObjectById(77),
			["lines"] = {
				bf:findObjectById(48),
				bf:findObjectById(49),
				bf:findObjectById(50),
				bf:findObjectById(51),
				bf:findObjectById(52),
				bf:findObjectById(53),
				bf:findObjectById(54),
				bf:findObjectById(55),
				bf:findObjectById(56),
			}
		},
		{
			["zone"] = bf:findObjectById(78),
			["lines"] = {
				bf:findObjectById(57),
				bf:findObjectById(58),
				bf:findObjectById(59),
				bf:findObjectById(60),
				bf:findObjectById(61),
				bf:findObjectById(62),
				bf:findObjectById(63),
			}
		},
		{
			["zone"] = bf:findObjectById(79),
			["lines"] = {
				bf:findObjectById(64),
				bf:findObjectById(65),
				bf:findObjectById(66),
				bf:findObjectById(67),
				bf:findObjectById(68),
				bf:findObjectById(69),
				bf:findObjectById(70),
				bf:findObjectById(71),
			}
		}
	}
end