Look here:
White - pathable, grey - non-pathable and that red square - our town hall. So, if we want to protect ourselves we should paint over that white passage on the right hand of town.
Universal method to do this is to draw a circle. As we know circle can have any size in Universe (Meta-Universe), so it can contain all our base. And sphere has the largest square with static perimeter among all figures while the base is usually built in a free piece of space. So, we have a circle with some radius:
As we see, it shows our wall perfectly except that blue point southward of town. It can be removed easily by checking neighbour points on making closed-loop figure. So, that 2 grey points can do this, and blue one is not needed. Removed.
edit
Forgot about other "blue points", just too lazy to change something. Do with them the same.
Then we should make an entrance. Just choose 2 (or 1 and make our "300") closest segments like this:
Saying with JASS terms, we have 2 boolean tables: first - terrain pathing table (0 - non-pathable, 1 - pathable), second - wall prediction table (0 - no wall prediction, 1 - wall prediction). Then we make 3rd table multiplying 1st and 2nd, remove "blue points", make entrance and gratz! We have wall-protected town.
Just make an algorithm choosing the best radius. And remember: not wall is built for towers, but towers for wall, so you don't need to think much about towers while making the wall.
edit
If you don't like walls similar to piece of circle, you can modify them easily by checking (with table with no entrance) your move on close-loop (or just on touch all neighbours if it's not outer, so bottom part of wall at illustration may become sharp vertical). (But rememeber: each line is the piece of infinite circle existing in the centre of Meta-Universe) Circle is just a way to define main shape.