diff options
-rw-r--r-- | rules.js | 191 |
1 files changed, 103 insertions, 88 deletions
@@ -1809,6 +1809,92 @@ function find_valid_regroup_destinations(from, rommel) { // === WITHDRAWAL CHECKS === +function is_friendly_hexside(side) { + if (is_axis_player()) + return set_has(game.axis_sides, side) + return set_has(game.allied_sides, side) +} + +function is_enemy_hexside(side) { + if (is_allied_player()) + return set_has(game.axis_sides, side) + return set_has(game.allied_sides, side) +} + +function can_unit_withdraw(who) { + let result = false + if (is_unit_supplied(who)) { + let sline = unit_supply_line(who) + let sdist = unit_supply_distance(who) + let from = unit_hex(who) + for_each_adjacent_hex(from, to => { + let side = to_side_id(from, to) + if (!has_enemy_unit(to)) + if (sline[side] && sdist[to] <= sdist[from]) + result = true + }) + } + return result +} + +function can_unit_disengage_and_withdraw(who) { + let result = false + if (is_unit_supplied(who)) { + let sline = unit_supply_line(who) + let sdist = unit_supply_distance(who) + let from = unit_hex(who) + for_each_adjacent_hex(from, to => { + let side = to_side_id(from, to) + if (is_friendly_hexside(side) && !has_enemy_unit(to)) + if (sline[side] && sdist[to] <= sdist[from]) + result = true + }) + } + return result +} + +function can_unit_disengage_and_move(who) { + let from = unit_hex(who) + let result = false + for_each_adjacent_hex(from, to => { + let side = to_side_id(from, to) + if (is_friendly_hexside(side) && !has_enemy_unit(to)) + result = true + }) + return result +} + +function can_unit_disengage_and_withdraw_to(who, to, extra) { + if (is_unit_supplied(who)) { + search_withdraw_retreat(who, extra) + return can_move_to(to, unit_speed[who] + extra) + } + return false +} + +function can_unit_disengage_and_move_to(who, to, extra) { + search_move_retreat(unit_hex(who), unit_speed[who] + extra) + return can_move_to(to, unit_speed[who] + extra) +} + +function can_all_units_disengage_and_withdraw(from) { + let result = true + for_each_undisrupted_friendly_unit_in_hex(from, u => { + if (result === true && !can_unit_disengage_and_withdraw(u)) + result = false + }) + return result +} + +function can_all_units_withdraw(from) { + let result = true + for_each_undisrupted_friendly_unit_in_hex(from, u => { + if (!result === true && !can_unit_withdraw(u)) + result = false + }) + return result +} + function is_network_reduced(reference, candidate) { for (let x = first_hex; x <= last_hex; ++x) if (reference[x] - candidate[x] === 1) @@ -3246,6 +3332,23 @@ function is_valid_retreat_hex(from) { return can_any_retreat(from) } +function can_unit_retreat(who) { + let rommel1 = (game.rommel === 1) ? 1 : 0 + let rommel2 = (game.rommel === 2) ? 1 : 0 + let from = unit_hex(who) + if (!game.to1 && game.from1 === from) + return can_unit_retreat_group_move(who) + if (!game.to2 && game.from2 === from) + return can_unit_retreat_group_move(who) + if (game.to1 && is_hex_or_adjacent_to(from, game.from1)) + if (can_unit_retreat_regroup_move(who, game.to1, rommel1)) + return true + if (game.to2 && is_hex_or_adjacent_to(from, game.from2)) + if (can_unit_retreat_regroup_move(who, game.to2, rommel2)) + return true + return false +} + function can_any_retreat(from) { let result = false for_each_undisrupted_and_unmoved_friendly_unit_in_hex(from, u => { @@ -3268,23 +3371,6 @@ function can_all_retreat(from) { return result } -function can_unit_retreat(who) { - let rommel1 = (game.rommel === 1) ? 1 : 0 - let rommel2 = (game.rommel === 2) ? 1 : 0 - let from = unit_hex(who) - if (!game.to1 && game.from1 === from) - return can_unit_retreat_group_move(who) - if (!game.to2 && game.from2 === from) - return can_unit_retreat_group_move(who) - if (game.to1 && is_hex_or_adjacent_to(from, game.from1)) - if (can_unit_retreat_regroup_move(who, game.to1, rommel1)) - return true - if (game.to2 && is_hex_or_adjacent_to(from, game.from2)) - if (can_unit_retreat_regroup_move(who, game.to2, rommel2)) - return true - return false -} - function can_unit_retreat_group_move(who) { if (game.turn_option === 'pass') return can_unit_disengage_and_withdraw(who) @@ -3299,58 +3385,6 @@ function can_unit_retreat_regroup_move(who, to, rommel) { return can_unit_disengage_and_move_to(who, to, 1 + rommel) } -function is_friendly_hexside(side) { - if (is_axis_player()) - return set_has(game.axis_sides, side) - return set_has(game.allied_sides, side) -} - -function is_enemy_hexside(side) { - if (is_allied_player()) - return set_has(game.axis_sides, side) - return set_has(game.allied_sides, side) -} - -function can_unit_disengage_and_withdraw(who) { - let result = false - if (is_unit_supplied(who)) { - let sline = unit_supply_line(who) - let sdist = unit_supply_distance(who) - let from = unit_hex(who) - for_each_adjacent_hex(from, to => { - let side = to_side_id(from, to) - if (is_friendly_hexside(side) && !has_enemy_unit(to)) - if (sline[side] && sdist[to] <= sdist[from]) - result = true - }) - } - return result -} - -function can_unit_disengage_and_move(who) { - let from = unit_hex(who) - let result = false - for_each_adjacent_hex(from, to => { - let side = to_side_id(from, to) - if (is_friendly_hexside(side) && !has_enemy_unit(to)) - result = true - }) - return result -} - -function can_unit_disengage_and_withdraw_to(who, to, extra) { - if (is_unit_supplied(who)) { - search_withdraw_retreat(who, extra) - return can_move_to(to, unit_speed[who] + extra) - } - return false -} - -function can_unit_disengage_and_move_to(who, to, extra) { - search_move_retreat(unit_hex(who), unit_speed[who] + extra) - return can_move_to(to, unit_speed[who] + extra) -} - function can_select_retreat_hex() { if (!game.to1 && game.from1) if (is_valid_retreat_hex(game.from1)) @@ -3612,25 +3646,6 @@ function goto_refuse_battle() { } } -function can_all_units_disengage_and_withdraw(from) { - let result = true - for_each_undisrupted_friendly_unit_in_hex(from, u => { - if (result === true && !can_unit_disengage_and_withdraw(u)) - result = false - }) - return result -} - -function can_all_units_withdraw(from) { - let result = true - for_each_undisrupted_friendly_unit_in_hex(from, u => { - // No supply line to withdraw along! - if (!is_unit_supplied(u)) - result = false - }) - return result -} - states.refuse_battle = { inactive: "refuse battle", prompt() { |