Archer30, i will do it soon.
It hasn't been tested yet.
I've added a check to see if the position has a defensive moat (thanks to XEPOMAHT) and a hex in front of the town gate (95) that is inaccessible to the attacker.
The post above has been updated. Missing functions added.
The idea is that the positions are checked in this order, as shown in the figure: