There's no trick. It's just a simple problem to solve. Every character just runs a breadth-first search, starting from their own position, until they find what they're looking for. Since the map is so densely populated, the search will never have to run for long before finding a target, so it's cheap. Effective use of data structures helps too.
One way in which you can make searches like these cheaper is to calculate influence maps and/or potential fields in the level for each unit type. Then pathfinding can be easily done just by moving from each tile to the most attractive tile next to it. More information here: http://aigamedev.com/open/tutorials/potential-fields/Using Potential Fields in a Real-time Strategy Game Scenario (Tutorial).
But if you're having specific problems, you will need to state clearly what exactly you're trying (probably showing some code) and in what way it doesn't work.