diff options
author | Tor Andersson <tor@ccxvii.net> | 2023-11-12 03:04:36 +0100 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2023-11-18 00:46:17 +0100 |
commit | 8063f3b9ba28d439cad6c8317d3b58796d5a5ddb (patch) | |
tree | e7597d9119bf60c43a44e96268e7734a140be8e9 /rules.js | |
parent | e66e845ef1d691d2086afc7984ec5807706a6121 (diff) | |
download | crusader-rex-8063f3b9ba28d439cad6c8317d3b58796d5a5ddb.tar.gz |
Show road limits.
Use maps instead of objects for road_limit, last_used and main_road.
Diffstat (limited to 'rules.js')
-rw-r--r-- | rules.js | 109 |
1 files changed, 91 insertions, 18 deletions
@@ -199,6 +199,14 @@ function print_summary(text, skip_if_empty = false) { logi("nothing.") } +const id_player = [ null, FRANKS, SARACENS ] + +function player_id(p) { + if (p === FRANKS) return 1 + if (p === SARACENS) return 2 + return 0 +} + function enemy(p) { if (p === FRANKS) return SARACENS if (p === SARACENS) return FRANKS @@ -365,11 +373,11 @@ function road_id(a, b) { } function road_was_last_used_by_enemy(from, to) { - return game.last_used[road_id(from, to)] === enemy(game.active) + return map_get(game.last_used, road_id(from, to)) === player_id(enemy(game.active)) } function road_was_last_used_by_friendly(from, to) { - return game.last_used[road_id(from, to)] === game.active + return map_get(game.last_used, road_id(from, to)) === player_id(game.active) } function road_type(a, b) { @@ -377,11 +385,15 @@ function road_type(a, b) { } function road_limit(a, b) { - return game.road_limit[road_id(a,b)] || 0 + return map_get(game.road_limit, road_id(a,b), 0) +} + +function set_road_limit(a, b, n) { + map_set(game.road_limit, road_id(a,b), n) } function reset_road_limits() { - game.road_limit = {} + game.road_limit.length = 0 } function count_player(p, where) { @@ -749,7 +761,7 @@ function can_use_richards_sea_legs(who, to) { if (!game.attacker[to]) return true if (game.attacker[to] === FRANKS) - return (game.main_road[to] === "England") + return (get_main_road(to) === ENGLAND) } } return false @@ -1165,7 +1177,7 @@ function start_game_turn() { // Reset movement and attack tracking state reset_road_limits() - game.last_used = {} + game.last_used = [] game.attacker = {} set_clear(game.reserves1) set_clear(game.reserves2) @@ -1313,7 +1325,7 @@ function start_player_turn() { log("") log(".h2 " + game.active) reset_road_limits() - game.main_road = {} + game.main_road = [] let card = CARDS[game.active === FRANKS ? game.f_card : game.s_card] if (card.event) goto_event_card(card.event) @@ -1533,12 +1545,20 @@ function queue_attack(who, round) { } } +function get_main_road(to) { + return map_get(game.main_road, to, NOWHERE) +} + +function set_main_road(to, x) { + map_set(game.main_road, to, x) +} + function move_block(who, from, to) { game.location[who] = to - game.road_limit[road_id(from, to)] = road_limit(from, to) + 1 + set_road_limit(from, to, road_limit(from, to) + 1) game.distance ++ if (is_contested_field(to)) { - game.last_used[road_id(from, to)] = game.active + map_set(game.last_used, road_id(from, to), player_id(game.active)) // 6.56 Main Attack relief force by Player 2 arrives one round later than normal let relief_delay = 0 @@ -1548,21 +1568,21 @@ function move_block(who, from, to) { if (!game.attacker[to]) { game.attacker[to] = game.active - game.main_road[to] = from + set_main_road(to, from) return queue_attack(who, 1 + relief_delay) } else { // Attacker main attack or reinforcements if (game.attacker[to] === game.active) { - if (game.main_road[to] !== from) + if (get_main_road(to) !== from) return queue_attack(who, 2 + relief_delay) return queue_attack(who, 1 + relief_delay) } // Defender reinforcements - if (!game.main_road[to]) - game.main_road[to] = from + if (!get_main_road(to)) + set_main_road(to, from) - if (game.main_road[to] === from) { + if (get_main_road(to) === from) { return queue_attack(who, 2) } else { return queue_attack(who, 3) @@ -1936,7 +1956,7 @@ states.sea_move_to = { } else if (!is_friendly_port(to)) { // English Crusaders attack! game.attacker[to] = FRANKS - game.main_road[to] = "England" + set_main_road(to, ENGLAND) log(game.active + " sea moved:") logi("#" + from + " \u2192 #" + to + ATTACK_MARK + ".") @@ -2324,6 +2344,7 @@ states.regroup = { }, end_regroup: function () { clear_undo() + reset_road_limits() print_summary(game.active + " regrouped:", true) if (game.assassins) { delete game.assassins @@ -3833,11 +3854,11 @@ exports.setup = function (seed, scenario, options) { steps: [], attacker: {}, - road_limit: {}, - last_used: {}, + road_limit: [], + last_used: [], castle: [], - main_road: {}, + main_road: [], moved: [], reserves1: [], reserves2: [], @@ -3923,6 +3944,8 @@ exports.view = function(state, current) { steps: game.steps, moved: game.moved, sieges: make_siege_view(), + last_used: game.last_used, + road_limit: game.road_limit, battle: null, prompt: null, } @@ -3932,6 +3955,13 @@ exports.view = function(state, current) { if (game.winter_campaign && game.winter_campaign !== game.p1 && game.winter_campaign !== game.p2) view.winter_campaign = game.winter_campaign + if (game.main_road && game.main_road.length > 0) { + view.main_road = [] + for (let i = 0; i < game.main_road.length; i += 2) + if (game.main_road[i+1] !== ENGLAND) + set_add(view.main_road, road_id(game.main_road[i+0], game.main_road[i+1])) + } + states[game.state].prompt(view, current) if (states[game.state].show_battle) @@ -3960,6 +3990,15 @@ function array_insert(array, index, item) { return array } +function array_insert_pair(array, index, key, value) { + for (let i = array.length; i > index; i -= 2) { + array[i] = array[i-2] + array[i+1] = array[i-1] + } + array[index] = key + array[index+1] = value +} + function set_clear(set) { set.length = 0 } @@ -4028,6 +4067,40 @@ function set_toggle(set, item) { return array_insert(set, a, item) } +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 + 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 { + map[(m<<1)+1] = value + return + } + } + array_insert_pair(map, a<<1, key, value) +} + // Fast deep copy for objects without cycles function object_copy(original) { if (Array.isArray(original)) { |