From 1ab4efcc25fb98a9d88a7e5e61e1f0863d0276d5 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Tue, 14 Nov 2023 15:34:26 +0100 Subject: Forbidden hexes are only forbidden while units remain to enter. Code updated as per the latest clarification from Mark Herman. --- play.js | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++------------- rules.js | 57 +++++++++++++++++++++++++++++++++------ 2 files changed, 124 insertions(+), 26 deletions(-) diff --git a/play.js b/play.js index 9dc7f40..1558960 100644 --- a/play.js +++ b/play.js @@ -16,18 +16,66 @@ const DICE = { D6: '', } -const FORBIDDEN = [ - [ - 2800, 2801, 2900, 2901, 3000, 3001, 3002, 3040, 3041, - 3100, 3101, 3139, 3140, 3141, 3200, 3201, 3239, 3240, - 3241, 3339, 3340, 3341, 3440, 3441, 3814, 3815, 3816, - 3913, 3914, 3915, 3916, 4013, 4014, 4015, 4016, 4017, - ], - [ - 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, - 1022, 1114, 1115, 1116, 1117, 1120, 1121, 1215, 1216, - 1218, 1221, - ], +const FORBIDDEN1 = [ + 2800, [ 15, 17 ], + 2801, [ 15, 17 ], + 2900, [ 15, 17 ], + 2901, [ 15, 17 ], + 3000, [ 15, 17 ], + 3001, [ 15, 17 ], + 3002, [ 15, 17 ], + 3040, [ 21 ], + 3041, [ 21 ], + 3100, [ 15, 17 ], + 3101, [ 15, 17 ], + 3139, [ 21 ], + 3140, [ 21 ], + 3141, [ 21 ], + 3200, [ 15, 17 ], + 3201, [ 15, 17 ], + 3239, [ 21 ], + 3240, [ 21 ], + 3241, [ 21 ], + 3339, [ 21 ], + 3340, [ 21 ], + 3341, [ 21 ], + 3440, [ 21 ], + 3441, [ 21 ], + 3814, [ 16 ], + 3815, [ 16 ], + 3816, [ 16 ], + 3913, [ 16 ], + 3914, [ 16 ], + 3915, [ 16 ], + 3916, [ 16 ], + 4013, [ 16 ], + 4014, [ 16 ], + 4015, [ 16 ], + 4016, [ 16 ], + 4017, [ 16 ], +] + +const FORBIDDEN2 = [ + 1013, [ 5, 6 ], + 1014, [ 5, 6 ], + 1015, [ 5, 6, 7, 8, 9, 12 ], + 1016, [ 5, 6, 7, 8, 9, 12 ], + 1017, [ 5, 6, 7, 8, 9, 12 ], + 1018, [ 7, 8, 9, 10, 11, 12 ], + 1019, [ 7, 8, 9, 10, 11, 12 ], + 1020, [ 7, 8, 9, 10, 11, 12 ], + 1021, [ 10, 11 ], + 1022, [ 10, 11 ], + 1114, [ 5, 6 ], + 1115, [ 5, 6, 7, 8, 9, 12 ], + 1116, [ 5, 6, 7, 8, 9, 12 ], + 1117, [ 7, 8, 9, 12 ], + 1120, [ 10, 11 ], + 1121, [ 10, 11 ], + 1215, [ 5, 6 ], + 1216, [ 5, 6, 7, 8, 9, 12 ], + 1218, [ 7, 8, 9, 12 ], + 1221, [ 10, 11 ], ] const yoff = 1555 @@ -197,12 +245,6 @@ for (let row = 0; row < data.map.rows; ++row) { hex.my_name = String(hex_id) + " (" + data.map.names[hex_id] + ")" else hex.my_name = String(hex_id) - - if (set_has(FORBIDDEN[0], hex_id)) - hex.classList.add("p1forbidden") - if (set_has(FORBIDDEN[1], hex_id)) - hex.classList.add("p2forbidden") - document.getElementById("hexes").appendChild(hex) } } @@ -414,6 +456,19 @@ function is_piece_support(id) { return false } +function is_forbidden_hex_for(map, x) { + let reinf = map_get(map, x, null) + if (reinf) { + // check if reinforcements affecting this hex are yet to enter + for (let p of reinf) { + let x = piece_hex(p) + if (x === REINFORCEMENTS || (x >= 1 && x <= 20)) + return true + } + } + return false +} + function on_update() { ui.stack.fill(0) @@ -440,6 +495,8 @@ function on_update() { ui.hexes[id].classList.toggle("p2zoi", is_p2_zoi(id)) ui.hexes[id].classList.toggle("p1hq", is_in_range(id, 0) || is_in_range(id, 1) || is_in_range(id, 2)) ui.hexes[id].classList.toggle("p2hq", is_in_range(id, 3) || is_in_range(id, 4)) + ui.hexes[id].classList.toggle("p1forbidden", is_forbidden_hex_for(FORBIDDEN1, id)) + ui.hexes[id].classList.toggle("p2forbidden", is_forbidden_hex_for(FORBIDDEN2, id)) } } diff --git a/rules.js b/rules.js index d10a4f7..b521fd1 100644 --- a/rules.js +++ b/rules.js @@ -165,23 +165,37 @@ for (let road_id = 0; road_id < data.map.roads.length; ++road_id) { const p1_forbidden = [] const p2_forbidden = [] -function calc_forbidden(set, a) { - set_add(set, a) +function add_forbidden(map, hex, list) { + let set = map_get(map, hex, null) + if (!set) + map_set(map, hex, set=[]) + for (let u of list) + set_add(set, u) +} + +function calc_forbidden(side, map, a) { + let units = null + for (let info of data.reinforcements) { + if (info.side === side) + if (info.hex === a || (Array.isArray(info.hex) && info.hex.includes(a))) + units = info.list + } + add_forbidden(map, a, units) for_each_adjacent(a, b => { if (!is_river(a, b)) { - set_add(set, b) + add_forbidden(map, b, units) for_each_adjacent(b, c => { if (!is_river(b, c)) - set_add(set, c) + add_forbidden(map, c, units) }) } }) } for (let entry of p1_forbidden_entry) - calc_forbidden(p1_forbidden, entry) + calc_forbidden("Coalition", p1_forbidden, entry) for (let entry of p2_forbidden_entry) - calc_forbidden(p2_forbidden, entry) + calc_forbidden("French", p2_forbidden, entry) function make_piece_list(f) { let list = [] @@ -310,9 +324,20 @@ function piece_is_in_zoc_of_hex(p, x) { } function is_forbidden_hex(x) { + let reinf = null if (game.active === P1) - return set_has(p1_forbidden, x) - return set_has(p2_forbidden, x) + reinf = map_get(p1_forbidden, x, null) + else + reinf = map_get(p2_forbidden, x, null) + if (reinf) { + // check if reinforcements affecting this hex are yet to enter + for (let p of reinf) { + let x = piece_hex(p) + if (x === REINFORCEMENTS || (x >= 1 && x <= 20)) + return true + } + } + return false } function is_river(a, b) { @@ -2906,6 +2931,22 @@ function set_delete(set, item) { // Map as plain sorted array of key/value pairs +function map_get(map, key, missing) { + let a = 0 + let b = (map.length >> 1) - 1 + while (a <= b) { + let m = (a + b) >> 1 + let x = map[m<<1] + if (key < x) + b = m - 1 + else if (key > x) + a = m + 1 + else + return map[(m<<1)+1] + } + return missing +} + function map_set(map, key, value) { let a = 0 let b = (map.length >> 1) - 1 -- cgit v1.2.3