Someone make a good observation that I hadn't considered. There's really two separate "trapping" problems being considered in this thread:
- One, trapping the builder, due to standing on the wrong side of a wall while building it;
- two, trapping unconstructed wall tiles, due to building wall tiles in the wrong order.
My previous suggestion was aimed only at the former problem. I still like it, despite Draco's objections, and I think it would work most of the time if tweaked enough. (If you're in the situation of *trying* to trap a dwarf in an emergency, you should not be relying on the AI to read your mind anyway.)
Here's my suggestion for the second case, avoiding the annoyance of trapping unconstructed "build wall" designations such that construction of some tiles cannot proceed until other tiles are deconstructed:
Each wall tile designated to be built will be surrounded by differing numbers of existing unwalkable tiles, and it's these unwalkable surrounding tiles *already* present which are the real culprit in occasionally blocking part of your construction order. Therefore, within the wall-construction job category as a whole, each build-a-wall tile must carry a construction priority, with hard-to-reach tiles constructed first. This necessarily breaks the last-in-first-out (LIFO) order, which must be abandoned.
Any time a batch of wall tiles is placed, each tile in that batch must be assigned a priority level before any more wall construction jobs can be accpted by dwarves. Thus, individual tiles in the current batch may or may not be higher priority than tiles in previously-designated batches.
How are the priority levels assigned?
We first group each tile according to how many sides were unwalkable when the construction was ordered. Group 0 are walkable on all four sides; Group 1 are up against a wall; Group 2 are in a corner; Group 3 are in an "alcove." Group 3 tiles clearly must have the highest priority, then group 2 tiles, and so on. Within a given priority, the order of construction will not matter; LIFO can be used. The lowest priority of all, call it Group -1, are tiles with two non-walkable tiles on opposite sides. These will be built last, because doing so will seal off a corridor. Within this group, order clearly matters, but deciding on the first to be built is non-trivial.
Here's the kicker, though: Any time any tile's construction is completed, the priorities of all neighboring tiles (if any) must be recalculated, as though those tiles had just been placed.
There are cases which this method will miss, such as any tiles built in the middle of a corridor. But I think these cases are exceptional, and should be handled with care by the player in any case. There are also cases, particularly emergencies, where LIFO order is advantageous. In this system, emergency constructions would have to be handled with mass suspension of existing build orders.
As far as I know, the solutions to both problems can work at the same time, on the same job.