if you use a brush-based level the engine should do it for you (at least A7 does so...). If not you have to get the color of the lightmap under the entity and set it's ambient to that. I'm not sure if it'S possible with a normal trace, but there was a plugin floating around in the forum, which did exactly that.