if endofgame == 1 then
	endGame()
	stopTimerEvent(timer_event1)
	stopTimerEvent(timer_event2)
	stopTimerEvent(aiTimer)
elseif triggeredTimerEventId() == selection_timer then
	dofile(getSystemMacroValue("$SCENARIO_PATH").."load_lanes.lua")
elseif triggeredTimerEventId() == timer_event1 then
	if firstspawn == 1 then
		if timerEventSecondsElapsed(timer_event1) - lastspawntimer > 0 then
			lastspawntimer = timerEventSecondsElapsed(timer_event1)
			displayFormattedLangText ("secleft",tostring(starttime - lastspawntimer ))
		end
	elseif (respawntime-timerEventSecondsElapsed(timer_event1)) % 15 == 0 then
		lastspawntimer = timerEventSecondsElapsed(timer_event1)
		if timeleft == respawntime - lastspawntimer then
		else
			timeleft = respawntime - lastspawntimer
			if timeleft == 0 then
				addConsoleText("spawnnow")
			else
				addConsoleLangText("secleftnext",tostring(timeleft))
			end
		end
	end
	if timerEventSecondsElapsed(timer_event1) >= respawntime + ((starttime - respawntime) * firstspawn) then
		if firstspawn == 1 then
			firstspawn = 0
			displayStatus()
			--displayFormattedLangText("score1",kills[1],kills[2],towersalive[1],towersalive[2])
		end	
		i = 0
		while i < ( creepsPerSpawn / 3 ) do
			local j = 1
			while j <= table.getn(waypoints) do
				local creepDB = unit:new{}
				creepDB:createDCreep(0,j)
				creepDB:aimCreep()
				local creepDR = unit:new{}
				creepDR:createDCreep(4,j)
				creepDR:aimCreep()
				j = j + 1
			end
			i = i+1
		end	
		i = 0
		while i < ( creepsPerSpawn / 3 * 2 ) do
			local j = 1
			while j <= table.getn(waypoints) do
				local creepB = unit:new{}
				creepB:createCreep(0,j)
				creepB:aimCreep()
				local creepR = unit:new{}
				creepR:createCreep(4,j)
				creepR:aimCreep()

				j = j + 1
			end
			i = i+1
		end	
		if ultracreeps[1] > 0 or towersalive[2] == 0 then
			ultracreeps[1] = ultracreeps[1] - 1
			local j = 1
			while j <= table.getn(waypoints) do
				local creepUB = unit:new{}
				creepUB:createUCreep(0,j)
				creepUB:aimCreep()
				j = j + 1
			end
		end
		if ultracreeps[2] > 0 or towersalive[1] == 0 then
			ultracreeps[2] = ultracreeps[2] - 1
			local j = 1
			while j <= table.getn(waypoints) do
				local creepUB = unit:new{}
				creepUB:createUCreep(4,j)
				creepUB:aimCreep()
				j = j + 1
			end
		end
		
		resetTimerEvent(timer_event1)
	end

elseif triggeredTimerEventId() == timer_gold then
	if ( timerEventSecondsElapsed(timer_gold)-goldLoop >= 1 ) and  ( timerEventSecondsElapsed(timer_gold) >= 3) then
		giveResource("gold", 1, 2)
		giveResource("gold", 2, 2)
		giveResource("gold", 5, 2)
		giveResource("gold", 6, 2)
		resetTimerEvent(timer_gold)
		goldLoop = timerEventSecondsElapsed(timer_gold)
	elseif ( timerEventSecondsElapsed(timer_gold)-goldLoop >= 1 ) then
		giveResource("gold", 1, 1)
		giveResource("gold", 2, 1)
		giveResource("gold", 5, 1)
		giveResource("gold", 6, 1)
		goldLoop = timerEventSecondsElapsed(timer_gold)
		
	end
	
elseif triggeredTimerEventId() == timer_event2 then
	--if timerEventSecondsElapsed(timer_event2)-aiTimeLoop >= 1 then
		herotimer = herotimer + 1
		for i,v in ipairs(herosAI) do 
			if units[v] == nil then
				table.remove(herosAI,i)
				print("removed "..i)
			elseif units[v].isAlive == 0 then
				table.remove(herosAI,i)
				print("removed "..i.." "..units[v].name)
			end
		end
		
		local v = herosAI[math.ceil((herotimer) % (table.getn(herosAI)+1))+1] --timerEventSecondsElapsed(timer_event2)
		if units[v] == nil then
		else
			if units[v].isAIcontroled == 1 then
				if units[v].isAlive == 1 then
					if debugAI == 1 then
						addConsoleLangText("test","run",units[v].id)
					end
					units[v]:aimHero()
				end
			end
			--aiTimeLoop = timerEventSecondsElapsed(timer_event2)
		end
	--end


elseif triggeredTimerEventId() == aiTimer then
	if timerEventSecondsElapsed(aiTimer) >= 3 then
		if aiIsWorking == 0 then
			aiIsWorking = 1
			for a,n in Loop:unitsForAll("creep") do
				units[n]:aimCreep()
			end
			resetTimerEvent(aiTimer)
			aiIsWorking = 0
		end
	end
elseif triggeredTimerEventId() == timer_respawn then
	if timerEventSecondsElapsed(timer_respawn) >= 1 then
		for i,v in ipairs(respawn_units) do 
			if v == nil then print("nil unit "..i)else
				if v.timeleft > 0 then
					v.timeleft = v.timeleft - 1
					giveResource("my_deathtime", v.faction, (-1))
					--print(v.name.." time left "..v.timeleft)
				elseif v.timeleft == 0 then
					v.timeleft = v.timeleft - 1
					v:respawn()
				else
					table.remove(respawn_units,i)
				end
			end
		end
		resetTimerEvent(timer_respawn)
	end
end