Fixed a potential issue whereby NPCs being held onto by a script would cause a crash when the AI server disconnects (because the data's freed) by making it atomically reference counted. SMDFile class is the same. Also, we now wait until scripts have finished executing before proceeding with shutting the server down (NPC container is using CSTLMap which forces a delete, so it's really quite hacky).