# User:Phol ende wodan/FlankingProposal
This is a mini-proposal for a flanking algorithm which could be used in to-hit calculations. It relies only on the state of the map: which monsters are around a defender, whether they are hostile, and what terrain is around the defender. It produces a value from 0 (not flanked at all) to 7 (surrounded on all 8 sides by hostile monsters).

Mechanics
A defender being flanked by multiple attackers will be easier to hit. This applies primarily to the player, but also to monsters that may be flanked by pets. When a defending creature is under attack, every monster is eligible to contribute to flanking if:

it is hostile to the defending creature, and
if it is a pet, it is willing (subject to the normal HP and level checks) to attack the defending creature on its turn, and
it is currently attacking, or
it is not currently helpless, not caught in a trap, not fleeing, has at least one attack that is not AT_NONE, is not a long worm tail segment, and is adjacent to the target. (I'd like to have ranged flanking threats and polearm-range flanking threats as well, but it seems hard to define - a monster wielding a launcher or carrying several daggers or something, and in line with the player?)
There are only eight primary directions of attack in NetHack. The amount by which a defender is flanked can be quantified by the maximum arc in which the defender is not threatened by anything.
Balancing out the flanking bonus slightly are walls or other spaces adjacent to the defender that the defender cannot move to; a defender in the open can dodge more easily than if they are cornered or backed up against a wall. The accessibility of terrain is determined by whether the defender can move there without consequence (water or lava are accessible if the defender is levitating, for example.)
Specifically, flanking advantage is calculated as the following, and given as a bonus to the attacker:

Compute the largest arc in which no monsters are threatening the defender, and subtract it from 7 (there is always an attack coming from one direction, so 7 is the maximum possible angle).
If the space behind the defender - in the opposite direction of the attack - is blocked by a trap or terrain, add 1. (This has the odd edge case that a player surrounded by xorns or ghosts embedded in walls may give the attacker a larger bonus than 7, which is normally not possible).
If the above case happens and the defender's movement to either of the space radially adjacent to the one "behind" the defender is blocked (by monsters as well as traps and terrain), add 1/2 for each square.
The strategy implications of flanking mean that players will be incentivized to fight in doorways, corridors, and other choke points where they can't get flanked very easily. Open levels like the Big Room and the Gnomish Mines and things like magic traps and werecreatures become more dangerous. In vanilla, experienced players already use this strategy because it means fewer things can attack you at the same time. This might exacerbate its dominance as a melee combat strategy, but it's also tactically the right thing to do in real life, so I'm ambivalent on the issue. It also means that if you plan to run through hordes of enemies, like the standard strategy for the ascension run, you will likely suffer many large flanking penalties, which is probably good.
Monster AI might also benefit from a change where intelligent enemies may hang back and let their compatriots catch up so they can all attack you at once and flank you.

Examples
The attacking monster in all of these examples is the dingo.

.d..@....
The largest arc with no threat is 7 (northeast clockwise to northwest). Flanking bonus = (7 - 7) + 0 + 0/2 = 0.

.dd.@....
The largest arc with no threat is 6 (east clockwise to northwest). Flanking bonus = (7 - 6) + 0 + 0/2 = 1.

.d..@...d
The largest arc with no threat is 4 (south clockwise to northwest). Flanking bonus = (7 - 4) + 0 + 0/2 = 3.

.d..@d.d.
The largest arc with no threat is 3 (southwest clockwise to northwest). The space behind the defender is not a trap or terrain, so it does not get any extra bonus. Flanking bonus = (7 - 3) + 0 + 0/2 = 4.

dddd@dddd
The largest arc with no threat is 0 (there is no direction with no threat). The space behind the defender is not a trap or terrain, so it does not get any extra bonus. Flanking bonus = (7 - 0) + 0 + 0/2 = 7.

dddd@d.dd
The largest arc with no threat is 1 (an "arc" consisting only of southwest). The space behind the defender is not a trap or terrain, so it does not get any extra bonus. Flanking bonus = (7 - 1) + 0 + 0/2 = 6.

.d..@.---
The largest arc with no threat is 7 (northeast clockwise to northwest), and the space behind the defender is blocked, and both spaces on either side of it are also blocked. Flanking bonus = (7 - 7) + 1 + 2/2 = 2.

.d..@.--.
The largest arc with no threat is 7 (northeast clockwise to northwest), and the space behind the defender is blocked, and one of the spaces on either side of it is also blocked. Flanking bonus = (7 - 7) + 1 + 1/2 = 1.5.

.d.d@d---
The largest arc with no threat is 3 (southeast clockwise to southwest), and the space behind the defender is blocked, and both of the spaces on either side of it are also blocked. Flanking bonus = (7 - 3) + 1 + 2/2 = 6.

.d.d@d---
The largest arc with no threat is 3 (southeast clockwise to southwest). The space behind the defender is not a trap or terrain, so it does not get any extra bonus. Flanking bonus = (7 - 3) + 0 + 0/2 = 4.

.ddd@.---
The largest arc with no threat is 4 (east clockwise to southwest). The space behind the defender is not a trap or terrain, so it does not get any extra bonus. Flanking bonus = (7 - 4) + 0 + 0/2 = 3. The flanking bonus seems kind of low for a player so hemmed in, but it's worthwhile to point out that both of the jackals will get the full +2 when they attack for a flanking bonus of 5.

---.@d---
The largest arc with no threat is 7 (southeast clockwise to northeast). The space behind the defender is not a trap or terrain, so it does not get any extra bonus. Flanking bonus = (7 - 7) + 0 + 0/2 = 0.