Jason Rohrer's Castle Doctrine.  Rohrer put the code in the public domain.  I'm putting it here for safe keeping.