Auto-translated
I think that if it's a standard map without any custom elements, you can simply use a script to check all the heroes of all players in one day. If there's necromancy, it means it's a Necromancer; if there's summoning, it means it's a Demon, and so on, and store the results in a global table (key - player number, value - faction). Then you can use it for either heroes or castles.