The 18-year-old German-Iranian gunman who killed nine people in Munich on Friday began planning the attack a year ago after visiting the German city of Winnenden where another teenager killed 15 people in 2009, Bavarian officials said on Sunday.
Materials found at the gunman's home also showed he had been hospitalised for psychiatric care for three months around the same time, and was an avid player of violent video games, the officials told a news conference.
One of the games was "Counter Strike: Source", Robert Heimberger, president of the state crime office, said. He said it was "a game played by nearly every known rampage killer".
The gunman - identified by sources as David Sonboly - called himself "Ali" and once described the 17-year-old killer in the 2009 attack as a good person, according to a 16-year-old youth with whom he played video games in an online club, the German magazine Spiegel reported.
He frequently used online video handles such as "Hate" and "Rampage Killer", expressed nationalistic views, and was eventually excluded from the club because the other members were afraid of him, the magazine cited the other youth as saying.
"We always expected something like what happened, but we never thought he could get a gun and then use it," he said.
The gunman was also evidently inspired by Norwegian mass killer Anders Breivik, and likely deliberately staged the shooting on the fifth anniversary of Breivik's massacre of 77 people, Heimberger said.
Bavaria's chief prosecutor Thomas Steinhaus-Koch said the gunman was treated for anxiety and been hospitalised for psychiatric treatment from July to September 2015, followed by out-patient treatment as recently as last month.
Investigators also found prescription medicines at his home, where he lived with his parents and younger brother.
He had made reference to his hospitalization in a heated exchange that was videotaped immediately after the shooting.
"I am German," he yelled at a bystander in a nearby apartment house after he yelled a racial slur and threw a beer bottle. "Because of you I was bullied for seven years."
Officials said authorities had investigated allegations in 2012 that the gunman was bullied by three other children, but none of those individuals - or any other classmates of the gunman - were among the shooting victims.
Nor did the victims include anyone who commented on a fake Facebook page created by the gunman in May, using photographs and the name of a young Turkish woman, the officials said. He had invited people to free food at the McDonald's restaurant at 4 p.m. local time, shortly before the shooting began.
Heimberger said the gunman likely purchased his weapon - a reactivated Glock 17 pistol - on the shadowy dark net area of the Internet.
He said it was unclear when he bought the gun, or how he paid for it, adding that the parents of the gunman remained in shock and were not able to be interviewed.