summaryrefslogtreecommitdiff
path: root/rules.js
diff options
context:
space:
mode:
Diffstat (limited to 'rules.js')
-rw-r--r--rules.js147
1 files changed, 79 insertions, 68 deletions
diff --git a/rules.js b/rules.js
index 6df7316..54647b8 100644
--- a/rules.js
+++ b/rules.js
@@ -12,7 +12,7 @@ var states = {}
var game = null
var view = null
-let { hex_exists, hex_road, side_road, side_limit, hex_name, units, regions } = require("./data")
+let { all_hexes, hex_exists, hex_road, side_road, side_limit, hex_name, units, regions } = require("./data")
function debug_hexes3(n, list) {
console.log("--", n, "--")
@@ -105,10 +105,6 @@ const region_libya_and_sidi_omar_and_sollum = regions["Libya"].concat(regions["S
const region_egypt_and_tobruk = regions["Egypt"].concat(regions["Tobruk"])
const region_libya_except_tobruk = regions["Libya"].filter(r => r !== TOBRUK)
-const region_all = []
-for (let x = first_hex; x <= last_hex; ++x)
- region_all.push(x)
-
function calc_distance(a, b) {
let ax = a % hexw, ay = (a / hexw)|0, az = -ax - ay
let bx = b % hexw, by = (b / hexw)|0, bz = -bx - by
@@ -159,7 +155,7 @@ function find_unit(name) {
}
function is_map_hex(x) {
- return next >= first_hex && next <= last_hex && hex_exists[next] === 1
+ return x >= first_hex && x <= last_hex && hex_exists[x] === 1
}
function is_hex_or_adjacent_to(x, where) {
@@ -442,16 +438,13 @@ function release_hex_control(a) {
// no longer a battle hex: release hexsides if possible
set_delete(game.axis_hexes, a)
set_delete(game.allied_hexes, a)
- for (let s = 0; s < 6; ++s) {
- let b = a + hexnext[s]
- if (b >= first_hex && b <= last_hex && hex_exists[b]) {
- if (!is_battle_hex(b)) {
- let side = to_side_id(a, b)
- set_delete(game.axis_sides, side)
- set_delete(game.allied_sides, side)
- }
+ for_each_adjacent_hex(a, b => {
+ if (!is_battle_hex(b)) {
+ let side = to_side_id(a, b)
+ set_delete(game.axis_sides, side)
+ set_delete(game.allied_sides, side)
}
- }
+ })
}
function is_new_battle_hex(a) {
@@ -468,28 +461,24 @@ function claim_hex_control_for_defender(a) {
else
set_add(game.axis_hexes, a)
- for (let s = 0; s < 6; ++s) {
- let b = a + hexnext[s]
- if (b >= first_hex && b <= last_hex && hex_exists[b]) {
- let side = to_side_id(a, b)
- if (side_limit[side] > 0) {
- if (game.active === AXIS) {
- if (!set_has(game.axis_sides, side))
- set_add(game.allied_sides, side)
- } else {
- if (!set_has(game.allied_sides, side))
- set_add(game.axis_sides, side)
- }
+ for_each_adjacent_hex(a, b => {
+ let side = to_side_id(a, b)
+ if (side_limit[side] > 0) {
+ if (game.active === AXIS) {
+ if (!set_has(game.axis_sides, side))
+ set_add(game.allied_sides, side)
+ } else {
+ if (!set_has(game.allied_sides, side))
+ set_add(game.axis_sides, side)
}
}
- }
+ })
}
function claim_stuff() {
- for (let x = first_hex; x <= last_hex; ++x)
- if (hex_exists[x])
- if (is_new_battle_hex(x))
- claim_hex_control_for_defender(x)
+ for (let x of all_hexes)
+ if (is_new_battle_hex(x))
+ claim_hex_control_for_defender(x)
}
// === SUPPLY NETWORK ===
@@ -554,7 +543,7 @@ function trace_supply_highway(here, d) {
let has_supply = false
- supply_visited[here] = true
+ supply_visited[here] = 1
for (let s = 0; s < 6; ++s) {
let next = here + hexnext[s]
@@ -586,7 +575,7 @@ function trace_supply_highway(here, d) {
}
}
- supply_visited[here] = false
+ supply_visited[here] = 0
if (has_supply) {
supply_net[here] = 1
@@ -607,7 +596,7 @@ function trace_supply_chain(here, d, n, range) {
let has_supply = false
- supply_visited[here] = true
+ supply_visited[here] = 1
for (let s = 0; s < 6; ++s) {
let next = here + hexnext[s]
@@ -659,7 +648,7 @@ function trace_supply_chain(here, d, n, range) {
}
}
- supply_visited[here] = false
+ supply_visited[here] = 0
if (has_supply) {
supply_net[here] = 1
@@ -670,11 +659,17 @@ function trace_supply_chain(here, d, n, range) {
return has_supply
}
+var supply_visited = new Array(hexcount)
+var supply_src = new Array(hexcount)
+
function trace_supply_network(start) {
- supply_visited = new Array(hexcount).fill(false)
- supply_net = new Array(hexcount).fill(0)
- supply_line = new Array(sidecount).fill(0)
- supply_src = new Array(hexcount).fill(0)
+ supply_net = new Array(hexcount)
+ supply_line = new Array(sidecount)
+
+ supply_visited.fill(0)
+ supply_src.fill(0)
+ supply_net.fill(0)
+ supply_line.fill(0)
supply_src[start] = 1
supply_net[start] = 1
@@ -885,24 +880,29 @@ function pick_path(to, road, speed) {
return next_road
}
-function pay_movement_cost(to, this_road, speed) {
-}
-
-function neighbor_has_friendly_unit(here) {
+function adjacent_hex_has_friendly_unit(here) {
for (let s = 0; s < 6; ++s) {
let next = here + hexnext[s]
- if (next >= first_hex && next <= last_hex)
+ if (is_map_hex(next))
if (has_friendly_unit(next))
return true
}
return false
}
+function for_each_adjacent_hex(here, fn) {
+ for (let s = 0; s < 6; ++s) {
+ let next = here + hexnext[s]
+ if (is_map_hex(next))
+ fn(next)
+ }
+}
+
function for_each_hex_and_adjacent_hex(here, fn) {
fn(here)
for (let s = 0; s < 6; ++s) {
let next = here + hexnext[s]
- if (next >= first_hex && next <= last_hex && hex_exists[next])
+ if (is_map_hex(next))
fn(next)
}
}
@@ -980,8 +980,14 @@ function goto_supply_check() {
goto_turn_option()
}
+function clear_all_unit_moved() {
+ for (let u = 0; u < units.length; ++u)
+ clear_unit_moved(u)
+}
+
function goto_turn_option() {
game.state = 'turn_option'
+ clear_all_unit_moved()
}
states.turn_option = {
@@ -1100,7 +1106,7 @@ states.regroup_move_command_point = {
gen_rommel_move()
for (let x = first_hex; x <= last_hex; ++x) {
if (!is_enemy_hex(x)) {
- if (has_friendly_unit(x) || neighbor_has_friendly_unit(x))
+ if (has_friendly_unit(x) || adjacent_hex_has_friendly_unit(x))
gen_action_hex(x)
}
}
@@ -1273,8 +1279,6 @@ function apply_move(move, who, from, to) {
set_unit_moved(who)
set_unit_hex(who, to)
- pay_movement_cost(to, game.move_road, speed)
-
if (is_battle_hex(to)) {
let side = to_side_id(to, path_from[road][to])
@@ -1345,13 +1349,13 @@ states.move_to = {
search_move(from, 0, 4)
if (from === game.from1 && !game.to1)
- for (let to = first_hex; to <= last_hex; ++to)
- if (to != from && hex_exists[to] && can_move_group_1(who, from, to))
+ for (let to of all_hexes)
+ if (to != from && can_move_group_1(who, from, to))
gen_action_hex(to)
if (from === game.from2 && !game.to2)
- for (let to = first_hex; to <= last_hex; ++to)
- if (to != from && hex_exists[to] && can_move_group_2(who, from, to))
+ for (let to of all_hexes)
+ if (to != from && can_move_group_2(who, from, to))
gen_action_hex(to)
if (can_move_regroup_1(who, from, game.to1))
@@ -1422,13 +1426,13 @@ states.group_move_to = {
search_move(from, game.move_used, game.move_road)
if (game.move_from === game.from1 && !game.to1)
- for (let to = first_hex; to <= last_hex; ++to)
- if (to != from && hex_exists[to] && can_move_group_1(who, game.move_from, to))
+ for (let to of all_hexes)
+ if (to != from && can_move_group_1(who, game.move_from, to))
gen_action_hex(to)
if (game.move_from === game.from2 && !game.to2)
- for (let to = first_hex; to <= last_hex; ++to)
- if (to != from && hex_exists[to] && can_move_group_2(who, game.move_from, to))
+ for (let to of all_hexes)
+ if (to != from && can_move_group_2(who, game.move_from, to))
gen_action_hex(to)
gen_action_unit(who)
@@ -1474,8 +1478,8 @@ function stop_move(who) {
// === REFUSE BATTLE ===
function gen_withdraw_group_move(who, from) {
- for (let to = first_hex; to <= last_hex; ++to)
- if (to != from && hex_exists[to] && can_move_to(to, 4, unit_speed(who)))
+ for (let to of all_hexes)
+ if (to != from && can_move_to(to, 4, unit_speed(who)))
gen_action_hex(to)
}
@@ -1512,17 +1516,22 @@ states.refuse_battle_who = {
inactive: "refuse battle (withdraw group move: who)",
prompt() {
view.prompt = `Withdraw: Select unit to move.`
+ let done = true
for_each_friendly_unit_in_hex(game.from1, u => {
gen_action_unit(u)
+ done = false
})
+ if (done)
+ gen_action('end_retreat')
},
unit(u) {
push_undo()
game.selected = [ u ]
game.state = 'refuse_battle_to'
},
- next() {
+ end_retreat() {
clear_undo()
+ release_hex_control(game.from1)
game.state = 'refuse_battle'
}
}
@@ -1545,10 +1554,6 @@ states.refuse_battle_to = {
game.selected = []
game.state = 'refuse_battle_who'
},
- next() {
- clear_undo()
- game.state = 'refuse_battle'
- }
}
// ==== COMBAT PHASE ===
@@ -1563,10 +1568,9 @@ states.select_active_battles = {
inactive: "combat phase (select active battles)",
prompt() {
view.prompt = `Select active battles.`
- for (let x = first_hex; x <= last_hex; ++x)
- if (hex_exists[x])
- if (!set_has(game.active_battles, x) && is_battle_hex(x))
- gen_action_hex(x)
+ for (let x of all_hexes)
+ if (!set_has(game.active_battles, x) && is_battle_hex(x))
+ gen_action_hex(x)
gen_action('next')
},
hex(x) {
@@ -1999,6 +2003,13 @@ states.pursuit_hits = {
},
}
+function end_pursuit_fire() {
+ game.from1 = game.pursuit
+ game.pursuit = 0
+ set_passive_player()
+ game.state = 'refuse_battle_who'
+}
+
// === DEPLOYMENT ===
states.free_deployment = {