summaryrefslogtreecommitdiff
path: root/rules.js
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2022-07-27 17:41:25 +0200
committerTor Andersson <tor@ccxvii.net>2022-11-17 13:11:26 +0100
commit330d6dfb5382f4b3b338d88b8a08d4f776f5ee36 (patch)
treeffa52c4af79a97770fa8a9f4e0c4e2637ce5a8c2 /rules.js
parent803a82364b56a60b3e8cd2cb5f84eb27eb290255 (diff)
downloadrommel-in-the-desert-330d6dfb5382f4b3b338d88b8a08d4f776f5ee36.tar.gz
Select hex sides when engaging.
Diffstat (limited to 'rules.js')
-rw-r--r--rules.js103
1 files changed, 87 insertions, 16 deletions
diff --git a/rules.js b/rules.js
index d397644..5283d9a 100644
--- a/rules.js
+++ b/rules.js
@@ -1199,10 +1199,10 @@ function search_move_bfs(from, cost, start, road, max_cost, retreat, sline, sdis
if (presence_invalid)
update_presence()
if (is_axis_player()) {
- path_enemy = presence_axis
+ path_enemy = presence_allied
friendly_sides = game.axis_sides
} else {
- path_enemy = presence_allied
+ path_enemy = presence_axis
friendly_sides = game.allied_sides
}
@@ -1767,12 +1767,12 @@ function goto_move() {
states.move = {
inactive: "move",
prompt() {
- view.prompt = `Move: Select unit to move.`
-
let rommel1 = (game.rommel === 1) ? 1 : 0
let rommel2 = (game.rommel === 2) ? 1 : 0
if (game.selected < 0) {
+ view.prompt = `Move: Select unit to move.`
+
// Select Group Move 1
if (!game.to1 && game.from1) {
@@ -1840,6 +1840,7 @@ states.move = {
else
gen_action('end_move')
} else {
+ view.prompt = `Move: Select hex to move to.`
// Deselect
gen_action_unit(game.selected)
@@ -1849,6 +1850,7 @@ states.move = {
search_withdraw(game.selected, (rommel1 | rommel2))
else
search_move(unit_hex(game.selected), unit_speed(game.selected) + (rommel1 | rommel2))
+
gen_move()
}
},
@@ -1943,10 +1945,6 @@ function apply_move(to) {
push_undo()
- if (has_enemy_unit(to)) {
- // TODO: pick hex-side manually when engaging
- }
-
search_move(from, speed + (rommel1 | rommel2))
if (!game.to1 && game.from1 === from)
@@ -1966,19 +1964,92 @@ function apply_move(to) {
return move_unit(who, to, speed + rommel2)
}
+// to check usable alternate paths to enter destination hex
+function can_move_via(via, to, speed, road) {
+ let cost = path_cost[road][via]
+ let side = to_side_id(via, to)
+ let max_side = side_limit[side]
+
+ console.log("can_move_via", via, to, speed, road, "=", cost)
+
+ // too far
+ if (cost + 1 > speed + road)
+ return false
+
+ // can't cross this hexside
+ if (max_side === 0)
+ return false
+
+ // must stay on road for current bonus
+ if (side_road[side] < road)
+ return false
+
+ // must stop on enemies
+ if (has_enemy_unit(via))
+ return false
+
+ // may not exceed hexside limit
+ if (has_enemy_unit(to))
+ if ((game.side_limit[side] | 0) >= max_side)
+ return false
+
+ return true
+}
+
function move_unit(who, to, speed) {
let from = unit_hex(who)
let road = pick_path(to, speed)
- set_unit_moved(who)
- set_unit_hex(who, to)
+ if (has_enemy_unit(to)) {
+ game.engage_via = []
+ for_each_adjacent_hex(to, via => {
+ if (
+ (hex_road[from] >= 4 && can_move_via(via, to, speed, 4)) ||
+ (hex_road[from] >= 2 && can_move_via(via, to, speed, 2)) ||
+ (hex_road[from] >= 1 && can_move_via(via, to, speed, 1)) ||
+ (hex_road[from] >= 0 && can_move_via(via, to, speed, 0))
+ )
+ game.engage_via.push(via)
+ })
+ console.log("ENGAGE", to, "FROM", game.engage_via)
+ if (game.engage_via.length === 1) {
+ engage_via(who, game.engage_via[0], to)
+ } else {
+ game.engage_who = who
+ game.engage_to = to
+ game.state = 'engage'
+ return
+ }
+ } else {
+ log(`>from #${from} to #${to}`)
+ set_unit_moved(who)
+ set_unit_hex(who, to)
+ }
+}
+
+states.engage = {
+ prompt() {
+ view.prompt = `Move: Select which hex side to cross.`
+ view.selected = game.engage_who
+ for (let x of game.engage_via)
+ gen_action_hex(x)
+ },
+ hex(via) {
+ engage_via(game.engage_who, via, game.engage_to)
+ delete game.engage_via
+ delete game.engage_who
+ delete game.engage_to
+ game.state = 'move'
+ }
+}
- log(`>from #${from} to #${to}`)
+function engage_via(who, via, to) {
+ let from = unit_hex(who)
- // TODO: pick hex-side manually
- if (is_battle_hex(to)) {
- let side = to_side_id(to, path_from[road][to])
+ set_unit_moved(who)
+ set_unit_hex(who, to)
+ let side = to_side_id(via, to)
if (game.side_limit[side])
game.side_limit[side] = 2
else
@@ -1989,8 +2060,8 @@ function move_unit(who, to, speed) {
claim_hex_control_for_defender(to)
set_add(game.active_battles, to)
}
- return true
- }
+
+ log(`>from #${from} via #${via} to #${to}`)
}
// === RETREAT ===