summaryrefslogtreecommitdiff
path: root/rules.js
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2022-07-24 16:59:45 +0200
committerTor Andersson <tor@ccxvii.net>2022-11-17 13:11:26 +0100
commitf05d21d531e0685918a506c6274e8821be8b2366 (patch)
tree0a705a412d8173287bee9cf4c8d987319943959a /rules.js
parent9d272f13ac290e331c3f167f85d695228cee56ce (diff)
downloadrommel-in-the-desert-f05d21d531e0685918a506c6274e8821be8b2366.tar.gz
show supply lines menu
Diffstat (limited to 'rules.js')
-rw-r--r--rules.js382
1 files changed, 180 insertions, 202 deletions
diff --git a/rules.js b/rules.js
index 64c62b5..f740ab4 100644
--- a/rules.js
+++ b/rules.js
@@ -231,6 +231,19 @@ function is_hex_or_adjacent_to(x, where) {
// === UNIT STATE ===
+function apply_select(u) {
+ if (game.selected === u)
+ game.selected = -1
+ else
+ game.selected = u
+}
+
+function pop_selected() {
+ let u = game.selected
+ game.selected = -1
+ return u
+}
+
const UNIT_DISRUPTED_SHIFT = 0
const UNIT_DISRUPTED_MASK = 1 << UNIT_DISRUPTED_SHIFT
@@ -592,6 +605,13 @@ function for_each_undisrupted_friendly_unit_in_hex(x, fn) {
fn(u)
}
+function for_each_undisrupted_friendly_unit_in_hex_or_adjacent(x, fn) {
+ // TODO: first/last_enemy_unit
+ for (let u = 0; u < units.length; ++u)
+ if (is_friendly_unit(u) && !is_unit_disrupted(u) && is_hex_or_adjacent_to(unit_hex(u), x))
+ fn(u)
+}
+
function for_each_enemy_unit_in_hex(x, fn) {
// TODO: first/last_enemy_unit
for (let u = 0; u < units.length; ++u)
@@ -1478,100 +1498,12 @@ function goto_move() {
game.state = 'move_who'
}
-function slowest_speed_of_selected_units() {
- let r = 4
- for (let u of game.selected) {
- let s = unit_speed(u)
- if (s < r)
- r = s
- }
- return r
-}
-function gen_move_who() {
+function gen_move(search_fn) {
let rommel1 = (game.rommel === 1) ? 1 : 0
let rommel2 = (game.rommel === 2) ? 1 : 0
-
- // Deselect
- for (let u of game.selected)
- gen_action_unit(u)
-
- // Select Group Move 1
- if (!game.to1 && game.from1) {
- if (game.selected.length === 0 || unit_hex(game.selected[0]) === game.from1) {
- if (!is_battle_hex(game.from1)) {
- for_each_friendly_unit(u => {
- if (!is_unit_disrupted(u) && !is_unit_moved(u) && !set_has(game.selected, u))
- if (unit_hex(u) === game.from1)
- gen_action_unit(u)
- })
- }
- }
- }
-
- // Select Group Move 2
- if (!game.to2 && game.from2) {
- if (game.selected.length === 0 || unit_hex(game.selected[0]) === game.from2) {
- if (!is_battle_hex(game.from2)) {
- for_each_friendly_unit(u => {
- if (!is_unit_disrupted(u) && !is_unit_moved(u) && !set_has(game.selected, u))
- if (unit_hex(u) === game.from2)
- gen_action_unit(u)
- })
- }
- }
- }
-
- function overlap(x, f, t) {
- return t ? is_hex_or_adjacent_to(x, f) : x === f
- }
-
- // Select Regroup Move 1
- if (game.to1) {
- // Multi-select if it does not engage and is an unambigously regroup move 1.
- if (game.selected.length === 0 || (!has_enemy_unit(game.to1) && overlap(unit_hex(game.selected[0]), game.from1, game.to1) && !overlap(unit_hex(game.selected[0]), game.from2, game.to2))) {
- search_move(game.to1, 4 + rommel1)
- for_each_friendly_unit(u => {
- if (!is_unit_disrupted(u) && !is_unit_moved(u) && !set_has(game.selected, u)) {
- let x = unit_hex(u)
- if (is_hex_or_adjacent_to(x, game.from1))
- if (game.selected.length === 0 || !overlap(x, game.from2, game.to2))
- if (can_move_to(game.to1, unit_speed(u) + rommel1))
- if (!is_battle_hex(x))
- gen_action_unit(u)
- }
- })
- }
- }
-
- // Select Regroup Move 2
- if (game.to2) {
- // Multi-select if it does not engage and is unambigously regroup move 2.
- if (game.selected.length === 0 || (!has_enemy_unit(game.to2) && !overlap(unit_hex(game.selected[0]), game.from1, game.to1) && overlap(unit_hex(game.selected[0]), game.from2, game.to2))) {
- search_move(game.to2, 4 + rommel2)
- for_each_friendly_unit(u => {
- if (!is_unit_disrupted(u) && !is_unit_moved(u) && !set_has(game.selected, u)) {
- let x = unit_hex(u)
- if (is_hex_or_adjacent_to(x, game.from2))
- if (game.selected.length === 0 || !overlap(x, game.from1, game.to1))
- if (can_move_to(game.to2, unit_speed(u) + rommel2))
- if (!is_battle_hex(x))
- gen_action_unit(u)
- }
- })
- }
- }
-}
-
-function gen_move_to(search_fn) {
- let rommel1 = (game.rommel === 1) ? 1 : 0
- let rommel2 = (game.rommel === 2) ? 1 : 0
- let speed = slowest_speed_of_selected_units()
-
- // NOTE: During normal regroup moves, we check that we can reach
- // the destination before allowing multi-select.
- // During retreats, we only move from one hex at a time.
- let from = unit_hex(game.selected[0])
+ let speed = unit_speed(game.selected)
+ let from = unit_hex(game.selected)
search_fn(from, max(speed + rommel1, speed + rommel2))
@@ -1579,9 +1511,7 @@ function gen_move_to(search_fn) {
if (from === game.from1) {
for (let to of all_hexes)
if (to != from && can_move_to(to, speed + rommel1))
- // no multi-unit engagement moves
- if (game.selected.length === 1 || !has_enemy_unit(to))
- gen_action_hex(to)
+ gen_action_hex(to)
}
}
@@ -1589,9 +1519,7 @@ function gen_move_to(search_fn) {
if (from === game.from2) {
for (let to of all_hexes)
if (to != from && can_move_to(to, speed + rommel2))
- // no multi-unit engagement moves
- if (game.selected.length === 1 || !has_enemy_unit(to))
- gen_action_hex(to)
+ gen_action_hex(to)
}
}
@@ -1611,11 +1539,9 @@ function gen_move_to(search_fn) {
function apply_move(to) {
let rommel1 = (game.rommel === 1) ? 1 : 0
let rommel2 = (game.rommel === 2) ? 1 : 0
- let speed = slowest_speed_of_selected_units()
- let from = unit_hex(game.selected[0])
-
- let list = game.selected
- game.selected = []
+ let who = pop_selected()
+ let from = unit_hex(who)
+ let speed = unit_speed(who)
push_undo()
@@ -1625,28 +1551,28 @@ function apply_move(to) {
search_move(from, max(speed + rommel1, speed + rommel2))
- for (let who of list) {
- if (!game.to1 && game.from1) {
- if (from === game.from1)
+ if (!game.to1 && game.from1) {
+ if (from === game.from1)
+ if (can_move_to(to, speed + rommel1))
move_unit(rommel1, who, from, to)
- }
+ }
- if (!game.to2 && game.from2) {
- if (from === game.from2)
+ if (!game.to2 && game.from2) {
+ if (from === game.from2)
+ if (can_move_to(to, speed + rommel2))
move_unit(rommel2, who, from, to)
- }
+ }
- if (game.to1) {
- if (is_hex_or_adjacent_to(from, game.from1))
- if (can_move_to(game.to1, speed + rommel1))
- move_unit(rommel1, who, from, to)
- }
+ if (game.to1) {
+ if (is_hex_or_adjacent_to(from, game.from1))
+ if (can_move_to(game.to1, speed + rommel1))
+ move_unit(rommel1, who, from, to)
+ }
- if (game.to2) {
- if (is_hex_or_adjacent_to(from, game.from2))
- if (can_move_to(game.to2, speed + rommel2))
- move_unit(rommel2, who, from, to)
- }
+ if (game.to2) {
+ if (is_hex_or_adjacent_to(from, game.from2))
+ if (can_move_to(game.to2, speed + rommel2))
+ move_unit(rommel2, who, from, to)
}
}
@@ -1681,39 +1607,88 @@ states.move_who = {
prompt() {
view.prompt = `Move: Select unit to move.`
- gen_move_who()
+ let rommel1 = (game.rommel === 1) ? 1 : 0
+ let rommel2 = (game.rommel === 2) ? 1 : 0
- if (game.selected.length > 0) {
+ if (game.selected < 0) {
+
+ // Select Group Move 1
+ if (!game.to1 && game.from1) {
+ if (!is_battle_hex(game.from1)) {
+ for_each_undisrupted_friendly_unit_in_hex(game.from1, u => {
+ if (!is_unit_moved(u))
+ gen_action_unit(u)
+ })
+ }
+ }
+
+ // Select Group Move 2
+ if (!game.to2 && game.from2) {
+ if (!is_battle_hex(game.from2)) {
+ for_each_undisrupted_friendly_unit_in_hex(game.from2, u => {
+ if (!is_unit_moved(u))
+ gen_action_unit(u)
+ })
+ }
+ }
+
+ // Select Regroup Move 1
+ if (game.to1) {
+ search_move(game.to1, 4 + rommel1)
+ for_each_undisrupted_friendly_unit_in_hex_or_adjacent(game.from1, u => {
+ if (!is_unit_moved(u) && can_move_to(game.to1, unit_speed(u) + rommel1))
+ if (!is_battle_hex(unit_hex(u)))
+ gen_action_unit(u)
+ })
+ }
+
+ // Select Regroup Move 2
+ if (game.to1) {
+ search_move(game.to2, 4 + rommel1)
+ for_each_undisrupted_friendly_unit_in_hex_or_adjacent(game.from2, u => {
+ if (!is_unit_moved(u) && can_move_to(game.to2, unit_speed(u) + rommel2))
+ if (!is_battle_hex(unit_hex(u)))
+ gen_action_unit(u)
+ })
+ }
+
+ // Retreat
+ let has_retreat_hex = false
+ if (!game.to1 && game.from1 && is_battle_hex(game.from1))
+ has_retreat_hex = true
+ if (!game.to2 && game.from2 && is_battle_hex(game.from2))
+ has_retreat_hex = true
+ if (game.to1 && !has_enemy_unit(game.to1)) {
+ for_each_hex_and_adjacent_hex(game.from1, x => {
+ if (has_undisrupted_friendly_unit(x) && is_battle_hex(x))
+ has_retreat_hex = true
+ })
+ }
+ if (game.to2 && !has_enemy_unit(game.to2)) {
+ for_each_hex_and_adjacent_hex(game.from2, x => {
+ if (has_undisrupted_friendly_unit(x) && is_battle_hex(x))
+ has_retreat_hex = true
+ })
+ }
+ if (has_retreat_hex)
+ gen_action('retreat')
+
+ gen_action('end_move')
+ } else {
+
+ // Deselect
+ gen_action_unit(game.selected)
+
+ // Move
if (game.turn_option === 'pass')
- gen_move_to(search_withdraw_normal)
+ gen_move(search_withdraw)
else
- gen_move_to(search_move)
- }
+ gen_move(search_move)
- let has_retreat_hex = false
- if (!game.to1 && game.from1 && is_battle_hex(game.from1))
- has_retreat_hex = true
- if (!game.to2 && game.from2 && is_battle_hex(game.from2))
- has_retreat_hex = true
- if (game.to1 && !has_enemy_unit(game.to1)) {
- for_each_hex_and_adjacent_hex(game.from1, x => {
- if (has_undisrupted_friendly_unit(x) && is_battle_hex(x))
- has_retreat_hex = true
- })
}
- if (game.to2 && !has_enemy_unit(game.to2)) {
- for_each_hex_and_adjacent_hex(game.from2, x => {
- if (has_undisrupted_friendly_unit(x) && is_battle_hex(x))
- has_retreat_hex = true
- })
- }
- if (has_retreat_hex)
- gen_action('retreat')
-
- gen_action('end_move')
},
unit(who) {
- set_toggle(game.selected, who)
+ apply_select(who)
},
hex(to) {
apply_move(to)
@@ -1871,9 +1846,9 @@ states.retreat_who = {
if (game.selected.length > 0) {
if (game.turn_option === 'pass')
- gen_move_to(search_withdraw_retreat)
+ gen_move(search_withdraw_retreat)
else
- gen_move_to(search_move_retreat)
+ gen_move(search_move_retreat)
}
},
unit(who) {
@@ -1933,20 +1908,20 @@ states.refuse_battle = {
}
}
-states.refuse_battle_who = {
+states.refuse_battle_withdraw = {
inactive: "refuse battle (withdraw group move)",
prompt() {
view.prompt = `Withdraw: Select unit to move.`
- let done = true
- for_each_undisrupted_friendly_unit_in_hex(game.from1, u => {
- gen_action_unit(u)
- done = false
- })
- if (done)
- gen_action('end_retreat')
-
- if (game.selected.length > 0) {
- let speed = slowest_speed_of_selected_units()
+ if (game.selected < 0) {
+ let done = true
+ for_each_undisrupted_friendly_unit_in_hex(game.from1, u => {
+ gen_action_unit(u)
+ done = false
+ })
+ if (done)
+ gen_action('end_retreat')
+ } else {
+ let speed = unit_speed(game.selected)
search_withdraw_retreat(game.from1, speed)
for (let to of all_hexes)
if (to != game.from1 && can_move_to(to, speed))
@@ -1954,17 +1929,14 @@ states.refuse_battle_who = {
}
},
unit(who) {
- set_toggle(game.selected, who)
+ apply_select(who)
},
hex(to) {
- let list = game.selected
- game.selected = []
+ let who = pop_selected()
push_undo()
- for (let who of list) {
- log(`>to #${to}`)
- set_unit_hex(who, to)
- set_unit_disrupted(who)
- }
+ log(`>to #${to}`)
+ set_unit_hex(who, to)
+ set_unit_disrupted(who)
},
end_retreat() {
clear_undo()
@@ -2453,7 +2425,7 @@ function end_pursuit_fire() {
if (game.retreat) {
game.state = 'retreat_who'
} else {
- game.state = 'refuse_battle_who'
+ game.state = 'refuse_battle_withdraw'
}
}
@@ -2464,52 +2436,39 @@ states.free_deployment = {
prompt() {
let scenario = SCENARIOS[game.scenario]
let deploy = hexdeploy + scenario.start
+ let axis = (game.active === AXIS)
let done = true
- if (game.active === AXIS)
- view.prompt = "Axis Free Deployment"
- else
- view.prompt = "Allied Free Deployment"
+ view.prompt = axis ? "Axis Free Deployment" : "Allied Free Deployment"
- // TODO: first/last_axis_unit
- for (let u = 0; u < units.length; ++u) {
- if (is_friendly_unit(u)) {
- if (unit_hex(u) === deploy) {
- done = false
- gen_action_unit(u)
+ if (game.selected < 0) {
+ for_each_friendly_unit_in_hex(deploy, u => {
+ gen_action_unit(u)
+ done = false
+ })
+ } else {
+ for (let x of (axis ? scenario.axis_deployment : scenario.allied_deployment)) {
+ if (!is_enemy_hex(x)) {
+ let limit = scenario.deployment_limit[x] | 0
+ if (!limit || count_friendly_units_in_hex(x) < limit)
+ gen_action_hex(x)
}
}
- }
-
- if (game.selected.length > 0) {
- let list
- if (game.active === AXIS)
- list = scenario.axis_deployment
- else
- list = scenario.allied_deployment
- // TODO: scenario.deployment_limit
- for (let i = 0; i < list.length; ++i)
- if (!is_enemy_hex(list[i]))
- gen_action_hex(list[i])
+ gen_action_unit(game.selected)
+ done = false
}
if (done)
gen_action_next()
},
unit(u) {
- if (set_has(game.selected, u))
- set_delete(game.selected, u)
- else
- set_add(game.selected, u)
+ apply_select(u)
},
- hex(x) {
+ hex(to) {
+ let who = pop_selected()
push_undo()
- log(`Deployed ${game.selected.length} at #${x}.`)
- for (let i = 0; i < game.selected.length; ++i) {
- let u = game.selected[i]
- set_unit_hex(u, x)
- }
- set_clear(game.selected)
+ log(`Deployed at #${to}.`)
+ set_unit_hex(who, to)
},
next() {
clear_undo()
@@ -2593,6 +2552,7 @@ const SCENARIOS = {
allied_deployment: region_egypt,
axis_initial_supply: 6,
allied_initial_supply: 3,
+ deployment_limit: {},
special: {
no_rommel_bonus: true,
only_one_die_for_buildup: true,
@@ -2606,6 +2566,7 @@ const SCENARIOS = {
allied_deployment: region_egypt_and_libya,
axis_initial_supply: 6,
allied_initial_supply: 6,
+ deployment_limit: {},
},
"Crusader": {
year: 1941,
@@ -2656,6 +2617,7 @@ const SCENARIOS = {
allied_deployment: regions["East Line"],
axis_initial_supply: 10,
allied_initial_supply: 12,
+ deployment_limit: {},
special: {
gazala_pre_build: true,
}
@@ -2668,6 +2630,7 @@ const SCENARIOS = {
allied_deployment: regions["Egypt"],
axis_initial_supply: 8,
allied_initial_supply: 8,
+ deployment_limit: {},
},
"1941-42": {
year: 1941,
@@ -2677,6 +2640,7 @@ const SCENARIOS = {
allied_deployment: region_egypt_and_libya,
axis_initial_supply: 6,
allied_initial_supply: 6,
+ deployment_limit: {},
},
}
@@ -2979,7 +2943,7 @@ function setup(name) {
log_h2("Axis Deployment")
game.active = 'Axis'
game.state = 'free_deployment'
- game.selected = []
+ game.selected = -1
}
// === PUBLIC FUNCTIONS ===
@@ -2997,7 +2961,7 @@ exports.setup = function (seed, scenario, options) {
state: null,
phasing: AXIS,
active: AXIS,
- selected: null,
+ selected: -1,
scenario: scenario,
month: 0,
@@ -3098,6 +3062,20 @@ exports.view = function(state, current) {
return common_view(current)
}
+exports.query = function (state, current, q) {
+ if (q === 'supply') {
+ game = state
+ return {
+ axis_supply: game.axis_supply,
+ axis_supply_line: game.axis_supply_line,
+ allied_supply: game.allied_supply,
+ allied_supply_line: game.allied_supply_line,
+ }
+ }
+ return null
+}
+
+
function gen_action_next() {
gen_action('next')
}