From 3e201b997a8f5f6957c57a2af3db8a206e653b26 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Wed, 27 Mar 2024 00:15:38 +0100 Subject: More robust lone leader retreat checks. Handle cases when (defending) units retreat or avoid battle to spaces with lone leaders. --- rules.js | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/rules.js b/rules.js index 2c2dbe5..6341afa 100644 --- a/rules.js +++ b/rules.js @@ -2954,20 +2954,33 @@ function resume_move() { let who = moving_piece() let where = moving_piece_space() - let enemy_units = has_unbesieged_enemy_units(where) - let enemy_leader = has_unbesieged_enemy_leader(where) - let friendly_units = has_unbesieged_friendly_units(where) - let friendly_leader = has_unbesieged_friendly_leader(where) + // First retreat enemy lone leaders with our units. + // Can happen if defender retreats into space(s) with lone enemy leaders. + for (let p = first_enemy_leader; p <= last_enemy_leader; ++p) { + let s = piece_space(p) + let enemy_units = has_unbesieged_enemy_units(s) + let enemy_leader = has_unbesieged_enemy_leader(s) + let friendly_units = has_unbesieged_friendly_units(s) - if (enemy_leader && !enemy_units && friendly_units) { - return goto_retreat_lone_leader(where, 'move') + if (enemy_leader && !enemy_units && friendly_units) { + return goto_retreat_lone_leader(s, 'move') + } } - // Can happen if enemy intercepted into moving piece and lone friendly leaders remain after battle. - if (friendly_leader && !friendly_units && enemy_units) { - game.state = 'retreat_lone_leader' - game.retreat = { from: where, reason: 'friendly_move' } - return + // Then retreat our lone leaders with enemy units. + // Can happen if enemy intercepted into moving piece and + // lone friendly leaders remain after battle. + for (let p = first_friendly_leader; p <= last_friendly_leader; ++p) { + let s = piece_space(p) + let enemy_units = has_unbesieged_enemy_units(s) + let friendly_units = has_unbesieged_friendly_units(s) + let friendly_leader = has_unbesieged_friendly_leader(s) + + if (friendly_leader && !friendly_units && enemy_units) { + game.state = 'retreat_lone_leader' + game.retreat = { from: s, reason: 'friendly_move' } + return + } } // Interrupt for Foul Weather response at first opportunity to move. -- cgit v1.2.3