I was doing some idle thinking about making the AI set up ambushes for the player. I wrote up some possible steps for how to achieve this, as a starting point for discussion. It is certainly not intended as a "tutorial" or something.
So,
* Do you think the steps below is a viable way to achieve it? (Implementation, execution speed and result)
* Are there better/simpler ways?
The scenario: There's a group of monsters that due to some AI choice decides they want to set up an ambush (they are aware of the player coming). I assume the game's AI is capable of telling them to go to a specified position and wait there until further notice.
A) Identify suitable position for '1' (pre-ambush position) and '2' (attack position). This step could be a whole point of discussion in itself, but assume we can identify a corridor shape leading into a room.
########
.......#
.......#
.......##
.......21 @
.......##
.......#
.......#
########
B) Run FOV from '1'
########
.......#
XXXX...#
XXXXXX.##
XXXXXXXX1 @
XXXXXX.##
XXXX...#
.......#
########
C) Run FOV from '2'
########
XXXXX..#
XXXXXX.#
XXXXXXX##
XXXXXXX2 @
XXXXXXX##
XXXXXX.#
XXXXX..#
########
D) Find positions reached by FOV '2', but not by '1'
########
XXXXX..#
....XX.#
......X##
.......2
......X##
....XX.#
XXXXX..#
########
E) If monsters have (prefer) ranged attacks, remove positions in melee range of '2'.
Otherwise (if they prefer melee), remove cells NOT in melee range of '2' (though this example would only leave two such cells).
Sort the remaining cells according to distance from '2'. Start with the furthest cell and place a monster there. For the next position, if it blocks LOS from any previous position, earase it, otherwise place a monster there. Repeat until no more monsters or no more positions (if there are more monsters than positions, it needs to be decided what to do with the remaining monsters)
########
M.M.M..#
.......#
.......##
.......2
.......##
.......#
M.M.M..#
########