In the winding hallways and stuffy rooms of an old factory in Brooklyn, under bulbs flickering as if in a horror movie, an elite new police unit prepares, over and over again, for the attack it knows is coming.

Day after day, the officers comb the highest floor of the building, looking for witnesses to point them to the right door and listening for gunshots like those that have echoed all over the world in recent months. They are conducting exercises to help them hunt down an “active shooter.”

“It’s going to happen,” said Chief James R. Waters, who leads the New York Police Department’s Counterterrorism Bureau. “Something like Orlando’s going to happen.”

Last year, the police announced the creation of a heavily armed and armored regiment called the Critical Response Command. Teams of officers work all over the city and are trained to respond to many locations in three to five minutes.