A few problems:
- If you run out of enemies you'll start to capture buildings, but then if ranged units ungarrison and start attacking you, you'll ignore them and continue to capture. This is pretty annoying.
- There's an obsolete SortEntitiesByPriority function, which for example says that if you're attacking melee infantry and get attacked by melee cavalry, then switch to attacking the cavalry, but if you're attacking melee cavalry and get attacked by melee infantry, then continue to attack the cavalry. Instead in these situations we should always just continue attacking our current target.
- The chasing section is missing the Attacked part, so if you're attacking a unit and they run away, while you chase them melee units can attack you and you'll continue to chase instead of stopping and attacking the new enemy.
The fixes:
- When we're attacking, if we're capturing (unforced) and are attacked by some other unit, then we should stop capturing and instead attack the unit.
- When we're approaching or chasing (unforced) and are attacked by a melee unit, then we should stop and instead attack this unit, because our original target might be protected by melee units and otherwise we could die before ever reaching them. (We shouldn't respond to ranged attacks though, because those units could be farther away than our original target. But maybe we should add the capture cases here too.)
- targetAttackersPassive is always true so I don't see any point in keeping it around. I'm not sure I'm a fan of targetAttackersAlways (used in violent stance to override forced commands), but I left it in.
Ideally units should have attacking preferences, e.g. melee first then capture, so it wouldn't have to be hard-coded. (Maybe in the future we'd have capture specialists who have melee as a backup attack, for example.) We have target preference classes, but it seems like there's issues with that because it's per attack type and the Attack.js code doesn't really appreciate that. I think this stuff needs to be rethought, is what I'm trying to say. (Currently it doesn't matter since capture has special coding everywhere and no unit has both melee and ranged attacks, and those are the only three attack types.)
(It looks like walk-and-fight creates forced attacks, which might not be what we want. I'll try to look into that later.)