@chrischall/warapp-mcp 0.0.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +4 -4
- package/.claude-plugin/plugin.json +2 -2
- package/README.md +211 -9
- package/dist/analysis/accept-surrender.js +56 -0
- package/dist/analysis/award.js +159 -0
- package/dist/analysis/bonuses.js +98 -0
- package/dist/analysis/breach.js +62 -0
- package/dist/analysis/calibrate.js +239 -0
- package/dist/analysis/card-support.js +329 -0
- package/dist/analysis/card-threats.js +234 -0
- package/dist/analysis/cards-in-effect.js +130 -0
- package/dist/analysis/chains.js +59 -0
- package/dist/analysis/chokepoints.js +140 -0
- package/dist/analysis/combat.js +231 -0
- package/dist/analysis/commander-safety.js +149 -0
- package/dist/analysis/commitments.js +338 -0
- package/dist/analysis/exchange.js +155 -0
- package/dist/analysis/finished-game.js +136 -0
- package/dist/analysis/game-lessons.js +217 -0
- package/dist/analysis/graph.js +82 -0
- package/dist/analysis/guard-off.js +54 -0
- package/dist/analysis/guess.js +351 -0
- package/dist/analysis/headroom.js +241 -0
- package/dist/analysis/hold-diff.js +64 -0
- package/dist/analysis/how-lost.js +130 -0
- package/dist/analysis/index.js +14 -0
- package/dist/analysis/local-deploy.js +179 -0
- package/dist/analysis/lookahead.js +108 -0
- package/dist/analysis/opponent-profile.js +552 -0
- package/dist/analysis/opponents.js +1253 -0
- package/dist/analysis/outlook.js +695 -0
- package/dist/analysis/overrides.js +60 -0
- package/dist/analysis/picks.js +1879 -0
- package/dist/analysis/plan/after-leftover.js +140 -0
- package/dist/analysis/plan/cap-spend.js +242 -0
- package/dist/analysis/plan/captures.js +1195 -0
- package/dist/analysis/plan/commander.js +236 -0
- package/dist/analysis/plan/completed.js +29 -0
- package/dist/analysis/plan/conceded-out.js +131 -0
- package/dist/analysis/plan/estimates.js +715 -0
- package/dist/analysis/plan/guard-off.js +232 -0
- package/dist/analysis/plan/holds.js +1631 -0
- package/dist/analysis/plan/leftover.js +760 -0
- package/dist/analysis/plan/move-order.js +319 -0
- package/dist/analysis/plan/orders.js +317 -0
- package/dist/analysis/plan/relations.js +702 -0
- package/dist/analysis/plan/report.js +672 -0
- package/dist/analysis/plan/shared.js +1405 -0
- package/dist/analysis/plan/stand-still.js +823 -0
- package/dist/analysis/plan/state.js +44 -0
- package/dist/analysis/plan/strike.js +1416 -0
- package/dist/analysis/plan/targets.js +651 -0
- package/dist/analysis/plan/team.js +299 -0
- package/dist/analysis/plan/threats.js +1001 -0
- package/dist/analysis/plan/transfers.js +328 -0
- package/dist/analysis/plan/util.js +5 -0
- package/dist/analysis/plan/vacated.js +285 -0
- package/dist/analysis/planner.js +2927 -0
- package/dist/analysis/player-record.js +144 -0
- package/dist/analysis/player.js +268 -0
- package/dist/analysis/profile.js +347 -0
- package/dist/analysis/relations.js +1030 -0
- package/dist/analysis/schedule.js +88 -0
- package/dist/analysis/since-last.js +50 -0
- package/dist/analysis/static-seats.js +71 -0
- package/dist/analysis/stress.js +584 -0
- package/dist/analysis/team-brief.js +151 -0
- package/dist/analysis/tripwire.js +644 -0
- package/dist/analysis/turn-sim.js +259 -0
- package/dist/analysis/whereabouts.js +1112 -0
- package/dist/board.js +105 -0
- package/dist/bundle.js +121082 -36148
- package/dist/calibration-source.js +75 -0
- package/dist/cards.js +435 -0
- package/dist/chat-cursor.js +135 -0
- package/dist/chat.js +265 -0
- package/dist/client.js +439 -0
- package/dist/config.js +62 -0
- package/dist/direct/link.js +102 -0
- package/dist/direct/session.js +267 -0
- package/dist/direct/transport.js +424 -0
- package/dist/finished-store.js +357 -0
- package/dist/guess-salt.js +65 -0
- package/dist/hosted.js +24 -0
- package/dist/index.js +33 -0
- package/dist/join-color.js +140 -0
- package/dist/lessons-store.js +356 -0
- package/dist/live.js +287 -0
- package/dist/map-details.js +127 -0
- package/dist/map-stats.js +160 -0
- package/dist/map-store.js +242 -0
- package/dist/move-order.js +141 -0
- package/dist/my-games.js +218 -0
- package/dist/normalize.js +443 -0
- package/dist/notifications.js +143 -0
- package/dist/open-games.js +537 -0
- package/dist/outlook-log.js +165 -0
- package/dist/path-lock.js +22 -0
- package/dist/pick-list.js +193 -0
- package/dist/player-record-store.js +231 -0
- package/dist/player-states.js +31 -0
- package/dist/profile-store.js +727 -0
- package/dist/protocol/bundle.js +375 -0
- package/dist/protocol/codec.js +556 -0
- package/dist/protocol/decoded.js +12 -0
- package/dist/protocol/interp.js +1904 -0
- package/dist/protocol/methods.js +615 -0
- package/dist/protocol/rpc.js +202 -0
- package/dist/push/listener.js +393 -0
- package/dist/push/packets.js +66 -0
- package/dist/push/socketio.js +333 -0
- package/dist/push/websocket.js +179 -0
- package/dist/redact.js +62 -0
- package/dist/relations-store.js +242 -0
- package/dist/replay.js +227 -0
- package/dist/scalars.js +28 -0
- package/dist/scout.js +346 -0
- package/dist/stats.js +291 -0
- package/dist/strategy/card-clock.js +185 -0
- package/dist/strategy/map-notes.js +128 -0
- package/dist/strategy/notes.js +2223 -0
- package/dist/surrender-pending.js +126 -0
- package/dist/team-cards.js +84 -0
- package/dist/team-orders.js +150 -0
- package/dist/teams.js +19 -0
- package/dist/tools/accept-surrender.js +311 -0
- package/dist/tools/award.js +206 -0
- package/dist/tools/begin-turn.js +162 -0
- package/dist/tools/board.js +77 -0
- package/dist/tools/card-advice.js +256 -0
- package/dist/tools/card-effects.js +49 -0
- package/dist/tools/card-plays.js +434 -0
- package/dist/tools/card-support-advice.js +140 -0
- package/dist/tools/chat-inbox.js +158 -0
- package/dist/tools/chat-read-mark.js +179 -0
- package/dist/tools/chat.js +525 -0
- package/dist/tools/check-plan.js +689 -0
- package/dist/tools/commander-check.js +103 -0
- package/dist/tools/commitments.js +216 -0
- package/dist/tools/common.js +143 -0
- package/dist/tools/diplomacy.js +342 -0
- package/dist/tools/end-of-game.js +384 -0
- package/dist/tools/game.js +151 -0
- package/dist/tools/guess-view.js +56 -0
- package/dist/tools/healthcheck.js +90 -0
- package/dist/tools/hints.js +34 -0
- package/dist/tools/how-lost.js +154 -0
- package/dist/tools/index.js +152 -0
- package/dist/tools/join-game.js +327 -0
- package/dist/tools/leave-game.js +181 -0
- package/dist/tools/live-context.js +339 -0
- package/dist/tools/live.js +404 -0
- package/dist/tools/map-details.js +88 -0
- package/dist/tools/map-stats.js +130 -0
- package/dist/tools/map.js +141 -0
- package/dist/tools/my-games.js +193 -0
- package/dist/tools/notifications.js +484 -0
- package/dist/tools/open-games.js +104 -0
- package/dist/tools/opponents.js +163 -0
- package/dist/tools/order-options.js +135 -0
- package/dist/tools/past-games.js +94 -0
- package/dist/tools/picks.js +305 -0
- package/dist/tools/profile-hook.js +156 -0
- package/dist/tools/profile-opponents.js +121 -0
- package/dist/tools/profile-reads.js +99 -0
- package/dist/tools/push.js +445 -0
- package/dist/tools/relations.js +611 -0
- package/dist/tools/resolve.js +45 -0
- package/dist/tools/route.js +36 -0
- package/dist/tools/scout.js +87 -0
- package/dist/tools/session.js +163 -0
- package/dist/tools/strategy.js +257 -0
- package/dist/tools/stress-opponents.js +83 -0
- package/dist/tools/surrender-pending.js +74 -0
- package/dist/tools/surrender.js +247 -0
- package/dist/tools/survival.js +189 -0
- package/dist/tools/team-brief.js +266 -0
- package/dist/tools/team-source.js +222 -0
- package/dist/tools/team.js +178 -0
- package/dist/tools/turn.js +2825 -0
- package/dist/tools/uncommit.js +160 -0
- package/dist/tools/where.js +300 -0
- package/dist/tools/write-kit.js +164 -0
- package/dist/transport-bridge.js +33 -0
- package/dist/trend.js +577 -0
- package/dist/version.js +4 -0
- package/manifest.json +194 -4
- package/mint.yaml +106 -14
- package/package.json +11 -6
- package/server.json +40 -3
- package/skills/warapp-api/SKILL.md +95 -4
- package/skills/warapp-api/references/scout.mjs +348 -0
- package/skills/warapp-api/scripts/warapp.sh +69 -8
- package/skills/warapp-mcp/SKILL.md +1096 -21
|
@@ -0,0 +1,584 @@
|
|
|
1
|
+
// A stress test for a proposed turn: the plan's orders are resolved (turn-sim.ts) against the
|
|
2
|
+
// replies an opponent could make with ONE deploy, and the ones that cost something are
|
|
3
|
+
// reported, worst first. Pure and deterministic; ties resolve on ascending ids.
|
|
4
|
+
//
|
|
5
|
+
// The planner judges each border and each capture by a rule. This judges the whole list of
|
|
6
|
+
// orders at once, by carrying the turn out: a border the plan's own transfer emptied, a capture
|
|
7
|
+
// that leaves two armies beside a stack, an attack that fails when its target is reinforced.
|
|
8
|
+
//
|
|
9
|
+
// Replies tried for each opponent (its deploy is the caller's estimate, all of it in one place):
|
|
10
|
+
// attack deploy on one stack next to a territory I hold (or will hold after my own
|
|
11
|
+
// orders) and attack it with everything that stack can move, its commander too;
|
|
12
|
+
// reinforce deploy on a territory my plan attacks and sit still;
|
|
13
|
+
// all-attack every stack with a target attacks at once, the deploy on the largest: once
|
|
14
|
+
// spread (each stack takes the most valuable target no other has taken) and once
|
|
15
|
+
// concentrated (each takes its most valuable target). Tried when two or more
|
|
16
|
+
// stacks have targets: it is what needs no deploy. With `targets` (#274, the
|
|
17
|
+
// commander check), also every stack beside a target converging on it, the
|
|
18
|
+
// deploy on each of them in turn, that stack attacking first.
|
|
19
|
+
// dodge (#180) a stack my orders attack moves out first, its deploy on it and its
|
|
20
|
+
// commander along, into each exit it can take: its own neighbour, or a neutral or
|
|
21
|
+
// a territory of mine its armies break (luck-free, on the board after my deploys).
|
|
22
|
+
// Run with it moving first only (moving second it is struck where it stands, which
|
|
23
|
+
// the other replies cover), so never when I am known to move first; an order of
|
|
24
|
+
// mine from a territory it took first does nothing (turn-sim.ts). A dodge into my
|
|
25
|
+
// territory is the `attack` reply moving first and is not run twice. `dodges`
|
|
26
|
+
// lists every one tried: what it took, the armies that got away, and what my
|
|
27
|
+
// strike then hit. A player my orders finish off unanswered (its commander killed,
|
|
28
|
+
// or, on a board with no fog, nothing left) that a reply keeps in the game is
|
|
29
|
+
// `eliminationsFailed`: the reply the end of a game turns on.
|
|
30
|
+
// joint (#180) two opponents strike one territory of mine (now, or after my orders)
|
|
31
|
+
// together, each from its largest stack beside it with its deploy: teammates
|
|
32
|
+
// always, any two opponents in a free-for-all only when I lead (`leader`, the
|
|
33
|
+
// caller's call: they gang up on the leader). Both relative orders are tried, with
|
|
34
|
+
// me first and with me last. `jointFalls` lists the territories that fall to some
|
|
35
|
+
// pair, with the armies they would need to hold (luck-free: one more than the two
|
|
36
|
+
// attacks together kill; armies alone, a commander of mine there not counted).
|
|
37
|
+
// chain (#180) under Multi-Attack, a stack's survivors carry on: after a capture they
|
|
38
|
+
// attack again, less the guard. For each stack (its deploy on it, its commander
|
|
39
|
+
// left behind) the chains through territories not its own or a teammate's (and
|
|
40
|
+
// not in the fog) are searched on the exact combat model, at most
|
|
41
|
+
// CHAIN_MAX_STEPS attacks deep and CHAIN_MAX_NODES attacks tried, on the board
|
|
42
|
+
// after my deploys; the one of two attacks or more that takes the most of mine
|
|
43
|
+
// (the income it breaks, then territories, then the shortest, then the first
|
|
44
|
+
// found in ascending ids) is tried.
|
|
45
|
+
// Each is run with me moving first and with the opponent moving first, unless the first mover
|
|
46
|
+
// is known; runs that end the same are reported once.
|
|
47
|
+
//
|
|
48
|
+
// Local Deployment (#180, `localDeployments`): every reply's deploy is bounded as the planner
|
|
49
|
+
// bounds it (#128, #293; local-deploy.ts `enemyDeployReaching`): the opponent's free income plus the
|
|
50
|
+
// pools of the bonuses it could hold (no member shown with another owner) that contain the territory
|
|
51
|
+
// deployed on; a deploy figure above that never lands on one territory, except with per-territory
|
|
52
|
+
// income (`perTerritoryIncome`), where what no bonus it could hold carries may land anywhere.
|
|
53
|
+
//
|
|
54
|
+
// One turn ahead (`exposedNextTurn`, computed by lookahead.ts, which the planner shares): a territory of mine that no enemy stack touches today can
|
|
55
|
+
// be a border tomorrow. For each stack, each territory beside it that it can take this turn (mine
|
|
56
|
+
// or neutral), and each territory of mine beyond that one, the two turns are carried out: the
|
|
57
|
+
// stack takes the one in between with its deploy, then attacks on with the survivors and its next
|
|
58
|
+
// deploy. A territory that falls is listed with the armies it would need to hold. I am taken to
|
|
59
|
+
// add nothing next turn: it shows what the orders leave exposed, not what will happen.
|
|
60
|
+
//
|
|
61
|
+
// Truces (B2 T6, `truces`): a partner stays among the opponents, so `safe` still means safe against a
|
|
62
|
+
// break. A reply in which a partner attacks a territory of mine its truce covers (any order of its, a
|
|
63
|
+
// chain's path or a dodge into it included) is marked `breaksTruce`, and `safeIfTruceHolds` is the
|
|
64
|
+
// verdict over the other replies alone (`null` when none is left to try). A reply that mixes a break
|
|
65
|
+
// with attacks elsewhere (every stack at once) is left out whole: its attacks elsewhere are each tried
|
|
66
|
+
// alone too.
|
|
67
|
+
//
|
|
68
|
+
// What this does NOT cover, so "safe" is only ever "safe against these": stacks hidden by fog,
|
|
69
|
+
// armies included (`blindBorders` lists my territories next to them), a deploy split over
|
|
70
|
+
// several stacks, two opponents together other than on one territory of mine (and two
|
|
71
|
+
// free-for-all opponents at all unless I lead), more than one chain per stack, cards, and an
|
|
72
|
+
// opponent that deploys more than the estimate. When no reply could be tried at all, `safe` is
|
|
73
|
+
// `null`: nothing is known.
|
|
74
|
+
import { bonusTerritories } from './bonuses.js';
|
|
75
|
+
import { breaksLuckFree, searchChains } from './chains.js';
|
|
76
|
+
import { COMMANDER_STRENGTH, DEFAULT_DEFENSIVE_KILL_RATE, DEFAULT_OFFENSIVE_KILL_RATE, straightKills } from './combat.js';
|
|
77
|
+
import { neighborsOf } from './graph.js';
|
|
78
|
+
import { couldHoldBonusPools, enemyDeployReaching, freeIncome } from './local-deploy.js';
|
|
79
|
+
import { twoStepReaches } from './lookahead.js';
|
|
80
|
+
import { DEFAULT_BASE_INCOME } from './player.js';
|
|
81
|
+
import { ownerShown } from './plan/shared.js';
|
|
82
|
+
import { simTerritories, simulateTurn, } from './turn-sim.js';
|
|
83
|
+
const DEFAULT_MAX_REPLIES = 8;
|
|
84
|
+
/** The chain search's caps (#180), shared with the planner (chains.ts, #199). */
|
|
85
|
+
export { CHAIN_MAX_NODES, CHAIN_MAX_STEPS } from './chains.js';
|
|
86
|
+
const byNum = (a, b) => a - b;
|
|
87
|
+
export function stressPlan(input) {
|
|
88
|
+
const { graph, board, me } = input;
|
|
89
|
+
const start = simTerritories(board);
|
|
90
|
+
const teams = input.teams;
|
|
91
|
+
const sameTeam = (a, b) => teams !== undefined && teams.get(a) !== undefined && teams.get(a) === teams.get(b);
|
|
92
|
+
const friendly = (owner) => owner === me || (typeof owner === 'number' && sameTeam(owner, me));
|
|
93
|
+
const off = input.settings?.offensiveKillRate ?? DEFAULT_OFFENSIVE_KILL_RATE;
|
|
94
|
+
const def = input.settings?.defensiveKillRate ?? DEFAULT_DEFENSIVE_KILL_RATE;
|
|
95
|
+
const guard = input.settings?.oneArmyStandsGuard === false ? 0 : 1;
|
|
96
|
+
const myDeploys = input.deploys.map((d) => ({ player: me, territoryId: d.territoryId, armies: d.armies }));
|
|
97
|
+
const myOrders = input.moves.map((m) => ({ ...m }));
|
|
98
|
+
const simulate = (sides, moveOrder) => simulateTurn({
|
|
99
|
+
graph,
|
|
100
|
+
board,
|
|
101
|
+
deploys: [...myDeploys, ...sides.flatMap((x) => (x.deploy > 0 ? [{ player: x.player, territoryId: x.deployTo, armies: x.deploy }] : []))],
|
|
102
|
+
orders: [{ player: me, orders: myOrders }, ...sides.map((x) => ({ player: x.player, orders: x.orders }))],
|
|
103
|
+
moveOrder,
|
|
104
|
+
...(input.settings ? { settings: input.settings } : {}),
|
|
105
|
+
...(teams ? { teams } : {}),
|
|
106
|
+
...(input.orderPriority ? { orderPriority: [me] } : {}),
|
|
107
|
+
});
|
|
108
|
+
const baseline = simulate([], [me]);
|
|
109
|
+
const mineAtStart = new Set([...start].filter(([, t]) => t.owner === me).map(([id]) => id));
|
|
110
|
+
const mineAfter = (r) => new Set([...r.territories].filter(([, t]) => t.owner === me).map(([id]) => id));
|
|
111
|
+
const baseMine = mineAfter(baseline);
|
|
112
|
+
const planned = [...baseMine].filter((id) => !mineAtStart.has(id)).sort(byNum);
|
|
113
|
+
const mineEither = new Set([...mineAtStart, ...baseMine]);
|
|
114
|
+
const bonusIds = [...graph.bonusById.keys()].sort(byNum);
|
|
115
|
+
const heldBonuses = (held) => bonusIds.filter((id) => {
|
|
116
|
+
const ts = bonusTerritories(graph, id);
|
|
117
|
+
return ts.length > 0 && ts.every((t) => held.has(t));
|
|
118
|
+
});
|
|
119
|
+
const baseBonuses = heldBonuses(baseMine);
|
|
120
|
+
const bonusValue = (id) => graph.bonusById.get(id)?.value ?? 0;
|
|
121
|
+
/** My bonuses (after my plan) that go with any of these territories. */
|
|
122
|
+
const bonusesWith = (ids) => baseBonuses.filter((b) => bonusTerritories(graph, b).some((t) => ids.includes(t)));
|
|
123
|
+
/** What a territory is worth to an attacker choosing a target: the bonuses of mine it breaks. */
|
|
124
|
+
const worth = (id) => bonusesWith([id]).reduce((n, b) => n + bonusValue(b), 0);
|
|
125
|
+
const armiesOf = (r, test) => [...r.territories.values()].reduce((n, t) => n + (test(t.owner) ? t.armies : 0), 0);
|
|
126
|
+
const holdsAny = (r, player) => [...r.territories.values()].some((t) => t.owner === player);
|
|
127
|
+
const blindBorders = [...mineEither].filter((id) => neighborsOf(graph, id).some((n) => start.get(n)?.owner === 'unknown')).sort(byNum);
|
|
128
|
+
// Local Deployment (#180, #293): each opponent's bonuses it could hold pay only into their own territories.
|
|
129
|
+
const onBoard = new Map(board.territories.map((t) => [t.territoryId, t]));
|
|
130
|
+
const shownOwner = (t) => {
|
|
131
|
+
const o = ownerShown(onBoard.get(t))?.owner;
|
|
132
|
+
return o === undefined ? undefined : o.kind === 'player' ? o.playerId : null;
|
|
133
|
+
};
|
|
134
|
+
const pools = input.localDeployments ? couldHoldBonusPools(graph, input.opponents.map((o) => o.id), shownOwner) : undefined;
|
|
135
|
+
const deployOn = (opp, on) => {
|
|
136
|
+
const all = Math.max(0, Math.floor(opp.deploy));
|
|
137
|
+
return pools ? enemyDeployReaching(all, freeIncome(opp.base ?? 0, DEFAULT_BASE_INCOME, opp.card ?? 0), pools.get(opp.id) ?? [], [on], input.perTerritoryIncome === true) : all;
|
|
138
|
+
};
|
|
139
|
+
/** Luck-free: `sent` armies (and a commander, when it goes along) take `t` as it stands. */
|
|
140
|
+
const breaks = (sent, withCommander, t) => breaksLuckFree(sent, withCommander, t, { offensiveKillRate: off, defensiveKillRate: def });
|
|
141
|
+
/** The board after my deploys: what dodges and chains are searched on. */
|
|
142
|
+
const afterMyDeploys = new Map([...start].map(([id, t]) => [id, { ...t }]));
|
|
143
|
+
for (const d of myDeploys) {
|
|
144
|
+
const c = afterMyDeploys.get(d.territoryId);
|
|
145
|
+
if (c && c.owner === me && d.armies > 0)
|
|
146
|
+
c.armies += d.armies;
|
|
147
|
+
}
|
|
148
|
+
const allOf = (s, deploy) => (start.get(s)?.armies ?? 0) + deploy;
|
|
149
|
+
const stacksOf = (id) => [...start]
|
|
150
|
+
.filter(([, t]) => t.owner === id)
|
|
151
|
+
.map(([t]) => t)
|
|
152
|
+
.sort(byNum);
|
|
153
|
+
/** The best chain out of `s` (see the header), or none of two attacks or more that takes a territory of mine. */
|
|
154
|
+
const chainFrom = (opp, s, dep) => {
|
|
155
|
+
const first = Math.max(0, allOf(s, dep) - guard);
|
|
156
|
+
if (first <= 0)
|
|
157
|
+
return undefined;
|
|
158
|
+
let best;
|
|
159
|
+
const hostile = (owner) => owner === 'neutral' || (typeof owner === 'number' && owner !== opp && !sameTeam(owner, opp));
|
|
160
|
+
searchChains({
|
|
161
|
+
graph,
|
|
162
|
+
territories: afterMyDeploys,
|
|
163
|
+
from: s,
|
|
164
|
+
sending: first,
|
|
165
|
+
guard,
|
|
166
|
+
offensiveKillRate: off,
|
|
167
|
+
defensiveKillRate: def,
|
|
168
|
+
hostile: (_id, t) => hostile(t.owner),
|
|
169
|
+
onAttack: ({ path: p, sends: sd, captured }) => {
|
|
170
|
+
if (!captured)
|
|
171
|
+
return;
|
|
172
|
+
const took = p.slice(1).filter((x) => mineEither.has(x));
|
|
173
|
+
if (p.length >= 3 && took.length) {
|
|
174
|
+
const income = bonusesWith(took).reduce((k, b) => k + Math.max(0, bonusValue(b)), 0);
|
|
175
|
+
const better = !best ||
|
|
176
|
+
income > best.income ||
|
|
177
|
+
(income === best.income && (took.length > best.mine || (took.length === best.mine && p.length < best.path.length)));
|
|
178
|
+
if (better)
|
|
179
|
+
best = { path: [...p], sends: [...sd], income, mine: took.length };
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
if (!best)
|
|
184
|
+
return undefined;
|
|
185
|
+
const b = best;
|
|
186
|
+
// The first order asks for everything (the resolver sends what can move); each next one the survivors less the guard.
|
|
187
|
+
const orders = b.path.slice(1).map((to, i) => ({ from: b.path[i], to, armies: i === 0 ? allOf(s, dep) : b.sends[i] }));
|
|
188
|
+
return { player: opp, kind: 'chain', deployTo: s, deploy: dep, orders, path: b.path };
|
|
189
|
+
};
|
|
190
|
+
// ---- the replies to try ----
|
|
191
|
+
const candidates = [];
|
|
192
|
+
const myTargets = new Set(input.moves.map((m) => m.to));
|
|
193
|
+
const opponents = [...input.opponents].sort((a, b) => a.id - b.id).filter((o) => o.id !== me && !friendly(o.id));
|
|
194
|
+
for (const opp of opponents) {
|
|
195
|
+
const stacks = stacksOf(opp.id);
|
|
196
|
+
/** For each stack, my territories (now, or after my plan) it touches. */
|
|
197
|
+
const reach = new Map();
|
|
198
|
+
for (const s of stacks) {
|
|
199
|
+
const ts = neighborsOf(graph, s).filter((n) => mineEither.has(n));
|
|
200
|
+
if (ts.length)
|
|
201
|
+
reach.set(s, ts);
|
|
202
|
+
}
|
|
203
|
+
const attacks = new Map();
|
|
204
|
+
for (const [s, ts] of reach) {
|
|
205
|
+
const dep = deployOn(opp, s);
|
|
206
|
+
for (const t of ts) {
|
|
207
|
+
const c = { player: opp.id, kind: 'attack', deployTo: s, deploy: dep, orders: [{ from: s, to: t, armies: allOf(s, dep), withCommander: true }] };
|
|
208
|
+
attacks.set(`${s}>${t}`, c);
|
|
209
|
+
candidates.push(c);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
for (const s of stacks) {
|
|
213
|
+
const dep = deployOn(opp, s);
|
|
214
|
+
if (myTargets.has(s) && dep > 0)
|
|
215
|
+
candidates.push({ player: opp.id, kind: 'reinforce', deployTo: s, deploy: dep, orders: [] });
|
|
216
|
+
}
|
|
217
|
+
if (reach.size >= 2) {
|
|
218
|
+
const bySize = [...reach.keys()].sort((a, b) => (start.get(b)?.armies ?? 0) - (start.get(a)?.armies ?? 0) || a - b);
|
|
219
|
+
const largest = bySize[0];
|
|
220
|
+
const dep = deployOn(opp, largest);
|
|
221
|
+
const ranked = (st) => [...reach.get(st)].sort((a, b) => worth(b) - worth(a) || a - b);
|
|
222
|
+
const ordersFor = (choice) => [...reach.keys()].map((st) => ({
|
|
223
|
+
from: st,
|
|
224
|
+
to: choice.get(st),
|
|
225
|
+
armies: allOf(st, st === largest ? dep : 0),
|
|
226
|
+
withCommander: true,
|
|
227
|
+
}));
|
|
228
|
+
// Spread: largest stack first, each taking its most valuable target no other stack has
|
|
229
|
+
// taken yet, so two stacks that reach the same two territories hit both.
|
|
230
|
+
const taken = new Set();
|
|
231
|
+
const spread = new Map();
|
|
232
|
+
for (const st of bySize) {
|
|
233
|
+
const ts = ranked(st);
|
|
234
|
+
const pick = ts.find((t) => !taken.has(t)) ?? ts[0];
|
|
235
|
+
taken.add(pick);
|
|
236
|
+
spread.set(st, pick);
|
|
237
|
+
}
|
|
238
|
+
// Concentrated: every stack on its most valuable target, so several fall on one.
|
|
239
|
+
const together = new Map([...reach.keys()].map((st) => [st, ranked(st)[0]]));
|
|
240
|
+
const variants = [ordersFor(spread), ordersFor(together)];
|
|
241
|
+
const same = JSON.stringify(variants[0]) === JSON.stringify(variants[1]);
|
|
242
|
+
for (const orders of same ? [variants[0]] : variants) {
|
|
243
|
+
candidates.push({ player: opp.id, kind: 'all-attack', deployTo: largest, deploy: dep, orders });
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
// #274 review 2: with `targets` (the commander check), every stack beside a target converges on
|
|
247
|
+
// it, the deploy on each in turn and that stack attacking first: the all-attack above puts the
|
|
248
|
+
// deploy on the largest stack, which may face another territory altogether.
|
|
249
|
+
for (const t of input.targets ? [...new Set(input.targets)].sort(byNum) : []) {
|
|
250
|
+
const beside = [...reach].filter(([, ts]) => ts.includes(t)).map(([st]) => st);
|
|
251
|
+
if (beside.length < 2)
|
|
252
|
+
continue;
|
|
253
|
+
const bySize = [...beside].sort((a, b) => (start.get(b)?.armies ?? 0) - (start.get(a)?.armies ?? 0) || a - b);
|
|
254
|
+
for (const d of bySize) {
|
|
255
|
+
const dep = deployOn(opp, d);
|
|
256
|
+
const orders = [d, ...bySize.filter((x) => x !== d)].map((st) => ({ from: st, to: t, armies: allOf(st, st === d ? dep : 0), withCommander: true }));
|
|
257
|
+
const key = JSON.stringify([d, orders]);
|
|
258
|
+
if (candidates.some((c) => c.kind === 'all-attack' && c.player === opp.id && JSON.stringify([c.deployTo, c.orders]) === key))
|
|
259
|
+
continue;
|
|
260
|
+
candidates.push({ player: opp.id, kind: 'all-attack', deployTo: d, deploy: dep, orders });
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
// Dodge (#180): a stack my orders attack moves out first, never when I am known to move first.
|
|
264
|
+
if (input.firstMover !== 'me') {
|
|
265
|
+
for (const s of stacks.filter((x) => myTargets.has(x))) {
|
|
266
|
+
const dep = deployOn(opp, s);
|
|
267
|
+
const withCommander = start.get(s)?.commander === opp.id;
|
|
268
|
+
for (const exit of [...neighborsOf(graph, s)].sort(byNum)) {
|
|
269
|
+
const t = afterMyDeploys.get(exit);
|
|
270
|
+
if (!t || t.owner === 'unknown')
|
|
271
|
+
continue;
|
|
272
|
+
const exitOwner = t.owner === opp.id ? 'own' : t.owner === 'neutral' ? 'neutral' : t.owner === me ? 'mine' : undefined;
|
|
273
|
+
if (exitOwner === undefined)
|
|
274
|
+
continue;
|
|
275
|
+
if (exitOwner !== 'own' && !breaks(Math.max(0, allOf(s, dep) - guard), withCommander, t))
|
|
276
|
+
continue;
|
|
277
|
+
const dodge = { stack: s, exit, exitOwner };
|
|
278
|
+
// Into my territory it is the attack reply, moving first: read the dodge from that run.
|
|
279
|
+
const same = attacks.get(`${s}>${exit}`);
|
|
280
|
+
if (same)
|
|
281
|
+
same.dodge = dodge;
|
|
282
|
+
else
|
|
283
|
+
candidates.push({ player: opp.id, kind: 'dodge', deployTo: s, deploy: dep, orders: [{ from: s, to: exit, armies: allOf(s, dep), withCommander: true }], dodge });
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
// Chains (#180): under Multi-Attack, survivors carry on.
|
|
288
|
+
if (input.settings?.multiAttack) {
|
|
289
|
+
for (const s of stacks) {
|
|
290
|
+
const c = chainFrom(opp.id, s, deployOn(opp, s));
|
|
291
|
+
if (c)
|
|
292
|
+
candidates.push(c);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
// Joint (#180): two opponents on one territory of mine.
|
|
297
|
+
const largestBeside = (id, t) => stacksOf(id)
|
|
298
|
+
.filter((s) => neighborsOf(graph, s).includes(t))
|
|
299
|
+
.sort((x, y) => (start.get(y)?.armies ?? 0) - (start.get(x)?.armies ?? 0) || x - y)[0];
|
|
300
|
+
for (let i = 0; i < opponents.length; i++) {
|
|
301
|
+
for (let j = i + 1; j < opponents.length; j++) {
|
|
302
|
+
const [a, b] = [opponents[i], opponents[j]];
|
|
303
|
+
if (!sameTeam(a.id, b.id) && !input.leader)
|
|
304
|
+
continue;
|
|
305
|
+
for (const t of [...mineEither].sort(byNum)) {
|
|
306
|
+
const [sa, sb] = [largestBeside(a.id, t), largestBeside(b.id, t)];
|
|
307
|
+
if (sa === undefined || sb === undefined)
|
|
308
|
+
continue;
|
|
309
|
+
const side = (o, s) => {
|
|
310
|
+
const dep = deployOn(o, s);
|
|
311
|
+
return { player: o.id, deployTo: s, deploy: dep, orders: [{ from: s, to: t, armies: allOf(s, dep), withCommander: true }] };
|
|
312
|
+
};
|
|
313
|
+
candidates.push({ ...side(a, sa), kind: 'joint', partner: side(b, sb) });
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
// #274: only the replies that strike the territories asked about.
|
|
318
|
+
if (input.targets) {
|
|
319
|
+
const wanted = new Set(input.targets);
|
|
320
|
+
const strikes = (x, path) => x.orders.some((o) => wanted.has(o.to)) || (path ?? []).some((t) => wanted.has(t));
|
|
321
|
+
const kept = candidates.filter((c) => strikes(c, c.path) || (c.partner !== undefined && strikes(c.partner)));
|
|
322
|
+
candidates.length = 0;
|
|
323
|
+
candidates.push(...kept);
|
|
324
|
+
}
|
|
325
|
+
// ---- B2 T6: which replies break a truce ----
|
|
326
|
+
const covers = new Map();
|
|
327
|
+
for (const t of input.truces ?? [])
|
|
328
|
+
covers.set(t.player, new Set([...(covers.get(t.player) ?? []), ...t.covered]));
|
|
329
|
+
const sideBreaks = (x, path) => {
|
|
330
|
+
const c = covers.get(x.player);
|
|
331
|
+
return c !== undefined && (x.orders.some((o) => c.has(o.to)) || (path ?? []).some((t) => c.has(t)));
|
|
332
|
+
};
|
|
333
|
+
const breaksTruce = (c) => sideBreaks(c, c.path) || (c.partner !== undefined && sideBreaks(c.partner));
|
|
334
|
+
// ---- run them ----
|
|
335
|
+
const assumptions = new Set(baseline.assumptions);
|
|
336
|
+
const baseCaptured = new Set(baseline.log.filter((e) => e.player === me && e.result === 'captured').map((e) => e.to));
|
|
337
|
+
const baseMyArmies = armiesOf(baseline, (o) => o === me);
|
|
338
|
+
/**
|
|
339
|
+
* Players my plan kills the commander of, or leaves with nothing — only on a board with no fog:
|
|
340
|
+
* with fog, a player may hold territories nobody sees.
|
|
341
|
+
*/
|
|
342
|
+
const noFog = [...start.values()].every((t) => t.owner !== 'unknown');
|
|
343
|
+
const finished = [...new Set([...start.values()].flatMap((t) => (typeof t.owner === 'number' && t.owner !== me ? [t.owner] : [])))]
|
|
344
|
+
.filter((p) => baseline.eliminated.includes(p) || (noFog && !holdsAny(baseline, p)))
|
|
345
|
+
.sort(byNum);
|
|
346
|
+
let runs = 0;
|
|
347
|
+
let harmfulCandidates = 0;
|
|
348
|
+
/** B2 T6: replies that break no truce, and those of them that cost something. */
|
|
349
|
+
let keptCandidates = 0;
|
|
350
|
+
let harmfulKept = 0;
|
|
351
|
+
const harmful = [];
|
|
352
|
+
const dodges = [];
|
|
353
|
+
const joints = new Map();
|
|
354
|
+
const judge = (c, r, run) => {
|
|
355
|
+
const after = mineAfter(r);
|
|
356
|
+
const lost = [...mineAtStart].filter((id) => !after.has(id)).sort(byNum);
|
|
357
|
+
const tookHere = new Set(r.log.filter((e) => e.player === me && e.result === 'captured').map((e) => e.to));
|
|
358
|
+
const commanderLost = r.eliminated.includes(me);
|
|
359
|
+
// With my commander gone everything is: which captures stuck no longer means anything.
|
|
360
|
+
const gone = commanderLost ? [] : planned.filter((id) => !after.has(id));
|
|
361
|
+
const retaken = gone.filter((id) => tookHere.has(id));
|
|
362
|
+
const capturesFailed = gone.filter((id) => !tookHere.has(id));
|
|
363
|
+
const held = new Set(heldBonuses(after));
|
|
364
|
+
const bonusesLost = baseBonuses.filter((id) => !held.has(id)).map((id) => ({ id, value: bonusValue(id) }));
|
|
365
|
+
const eliminationsFailed = commanderLost ? [] : finished.filter((p) => !r.eliminated.includes(p) && holdsAny(r, p));
|
|
366
|
+
if (!lost.length && !retaken.length && !capturesFailed.length && !bonusesLost.length && !commanderLost && !eliminationsFailed.length) {
|
|
367
|
+
return undefined;
|
|
368
|
+
}
|
|
369
|
+
const sides = [c, ...(c.partner ? [c.partner] : [])];
|
|
370
|
+
const theirs = (o) => sides.some((x) => x.player === o);
|
|
371
|
+
const sent = r.log.find((e) => e.player === c.player && e.index === 0)?.sent;
|
|
372
|
+
const single = c.kind === 'attack' || c.kind === 'dodge' || c.kind === 'joint' || c.kind === 'chain';
|
|
373
|
+
return {
|
|
374
|
+
opponent: c.player,
|
|
375
|
+
kind: c.kind,
|
|
376
|
+
deployTo: c.deployTo,
|
|
377
|
+
deploy: c.deploy,
|
|
378
|
+
...(single ? { from: c.orders[0].from, to: c.orders[0].to, armies: sent ?? 0 } : {}),
|
|
379
|
+
...(c.kind === 'all-attack' || c.kind === 'joint' ? { attacks: sides.flatMap((x) => x.orders.map((o) => ({ from: o.from, to: o.to }))) } : {}),
|
|
380
|
+
...(c.partner ? { partner: c.partner.player, partnerDeployTo: c.partner.deployTo, partnerDeploy: c.partner.deploy } : {}),
|
|
381
|
+
...(run.firstOpponent !== undefined ? { firstOpponent: run.firstOpponent } : {}),
|
|
382
|
+
...(c.path ? { path: c.path } : {}),
|
|
383
|
+
firstMover: run.firstMover,
|
|
384
|
+
lost,
|
|
385
|
+
capturesFailed,
|
|
386
|
+
retaken,
|
|
387
|
+
bonusesLost,
|
|
388
|
+
incomeLost: bonusesLost.reduce((n, b) => n + b.value, 0),
|
|
389
|
+
commanderLost,
|
|
390
|
+
eliminationsFailed,
|
|
391
|
+
myArmies: armiesOf(r, (o) => o === me) - baseMyArmies,
|
|
392
|
+
// Against what they would have with their deploys and nothing else happening.
|
|
393
|
+
theirArmies: armiesOf(r, theirs) - (armiesOf(baseline, theirs) + sides.reduce((n, x) => n + x.deploy, 0)),
|
|
394
|
+
...(breaksTruce(c) ? { breaksTruce: true } : {}),
|
|
395
|
+
};
|
|
396
|
+
};
|
|
397
|
+
/** Two outcomes are the same when they cost me the same things. */
|
|
398
|
+
const signature = (x) => JSON.stringify({ ...x, firstMover: undefined, firstOpponent: undefined });
|
|
399
|
+
const runsFor = (c) => {
|
|
400
|
+
const firsts = c.kind === 'dodge' ? ['opponent'] : input.firstMover !== undefined ? [input.firstMover] : ['me', 'opponent'];
|
|
401
|
+
if (!c.partner)
|
|
402
|
+
return firsts.map((f) => ({ firstMover: f, moveOrder: f === 'me' ? [me, c.player] : [c.player, me] }));
|
|
403
|
+
const pair = [c.player, c.partner.player];
|
|
404
|
+
return firsts.flatMap((f) => [pair, [...pair].reverse()].map((p) => ({ firstMover: f, firstOpponent: p[0], moveOrder: f === 'me' ? [me, ...p] : [...p, me] })));
|
|
405
|
+
};
|
|
406
|
+
for (const c of candidates) {
|
|
407
|
+
const sides = [c, ...(c.partner ? [c.partner] : [])];
|
|
408
|
+
const plan = runsFor(c);
|
|
409
|
+
const found = [];
|
|
410
|
+
let theyFirst;
|
|
411
|
+
let falls = false;
|
|
412
|
+
for (const run of plan) {
|
|
413
|
+
const r = simulate(sides, run.moveOrder);
|
|
414
|
+
runs++;
|
|
415
|
+
for (const a of r.assumptions)
|
|
416
|
+
assumptions.add(a);
|
|
417
|
+
const reply = judge(c, r, run);
|
|
418
|
+
if (reply)
|
|
419
|
+
found.push(reply);
|
|
420
|
+
if (run.firstMover === 'opponent')
|
|
421
|
+
theyFirst ??= { r, harmful: reply !== undefined };
|
|
422
|
+
if (c.kind === 'joint' && r.territories.get(c.orders[0].to)?.owner !== me)
|
|
423
|
+
falls = true;
|
|
424
|
+
}
|
|
425
|
+
if (found.length)
|
|
426
|
+
harmfulCandidates++;
|
|
427
|
+
if (!breaksTruce(c)) {
|
|
428
|
+
keptCandidates++;
|
|
429
|
+
if (found.length)
|
|
430
|
+
harmfulKept++;
|
|
431
|
+
}
|
|
432
|
+
// Outcomes that are the same are reported once: with no first mover named when every run
|
|
433
|
+
// ends that way, and with no first opponent when both of theirs do under one first mover.
|
|
434
|
+
const groups = new Map();
|
|
435
|
+
for (const f of found)
|
|
436
|
+
groups.set(signature(f), [...(groups.get(signature(f)) ?? []), f]);
|
|
437
|
+
for (const g of groups.values()) {
|
|
438
|
+
if (g.length === plan.length && plan.length > 1) {
|
|
439
|
+
const { firstMover: _f, firstOpponent: _o, ...same } = g[0];
|
|
440
|
+
harmful.push(same);
|
|
441
|
+
}
|
|
442
|
+
else if (g.length > 1 && c.partner && g.every((x) => x.firstMover === g[0].firstMover)) {
|
|
443
|
+
const { firstOpponent: _o, ...same } = g[0];
|
|
444
|
+
harmful.push(same);
|
|
445
|
+
}
|
|
446
|
+
else {
|
|
447
|
+
harmful.push(g[0]);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
if (c.dodge && theyFirst) {
|
|
451
|
+
const { stack, exit, exitOwner } = c.dodge;
|
|
452
|
+
const r = theyFirst.r;
|
|
453
|
+
const out = r.log.find((e) => e.player === c.player && e.index === 0);
|
|
454
|
+
const end = r.territories.get(exit);
|
|
455
|
+
const took = end?.owner === c.player && (out?.result === 'captured' || out?.result === 'transferred');
|
|
456
|
+
dodges.push({
|
|
457
|
+
opponent: c.player,
|
|
458
|
+
stack,
|
|
459
|
+
exit,
|
|
460
|
+
exitOwner,
|
|
461
|
+
deploy: c.deploy,
|
|
462
|
+
sent: out?.sent ?? 0,
|
|
463
|
+
took,
|
|
464
|
+
// What got away: a capture's survivors are all arrivals; a transfer into its own territory
|
|
465
|
+
// joins armies already there, so only what it sent counts (at most what stands there now).
|
|
466
|
+
escaped: !took ? 0 : out.result === 'transferred' ? Math.min(out.sent, end.armies) : end.armies,
|
|
467
|
+
commander: took && end.commander === c.player,
|
|
468
|
+
strikes: r.log.filter((e) => e.player === me && e.to === stack).map((e) => ({ from: e.from, result: e.result, sent: e.sent })),
|
|
469
|
+
harmful: theyFirst.harmful,
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
if (c.partner && falls) {
|
|
473
|
+
const t = c.orders[0].to;
|
|
474
|
+
const strength = (x) => {
|
|
475
|
+
const armies = Math.max(0, allOf(x.deployTo, x.deploy) - guard);
|
|
476
|
+
return { armies, power: armies + (start.get(x.deployTo)?.commander === x.player ? COMMANDER_STRENGTH : 0) };
|
|
477
|
+
};
|
|
478
|
+
const [pa, pb] = [strength(c), strength(c.partner)];
|
|
479
|
+
const bonusesLost = bonusesWith([t]).map((id) => ({ id, value: bonusValue(id) }));
|
|
480
|
+
const j = {
|
|
481
|
+
territory: t,
|
|
482
|
+
armies: baseline.territories.get(t)?.armies ?? 0,
|
|
483
|
+
opponents: [c.player, c.partner.player],
|
|
484
|
+
from: [c.deployTo, c.partner.deployTo],
|
|
485
|
+
attackers: [pa.armies, pb.armies],
|
|
486
|
+
needs: straightKills(pa.power, off) + straightKills(pb.power, off) + 1,
|
|
487
|
+
bonusesLost,
|
|
488
|
+
incomeLost: bonusesLost.reduce((n, b) => n + b.value, 0),
|
|
489
|
+
};
|
|
490
|
+
const known = joints.get(t);
|
|
491
|
+
if (!known || j.needs > known.needs)
|
|
492
|
+
joints.set(t, j);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
dodges.sort((a, b) => a.opponent - b.opponent || a.stack - b.stack || a.exit - b.exit);
|
|
496
|
+
const jointFalls = [...joints.values()].sort((a, b) => Math.max(0, b.incomeLost) - Math.max(0, a.incomeLost) || b.needs - b.armies - (a.needs - a.armies) || a.territory - b.territory);
|
|
497
|
+
// Worst first: my commander, an elimination undone, then income, territories held, captures undone, armies.
|
|
498
|
+
const order = harmful
|
|
499
|
+
.map((r, i) => ({ r, i }))
|
|
500
|
+
.sort((a, b) => Number(b.r.commanderLost) - Number(a.r.commanderLost) ||
|
|
501
|
+
b.r.eliminationsFailed.length - a.r.eliminationsFailed.length ||
|
|
502
|
+
// A negative bonus lost is no reason to rank a reply below one that takes nothing.
|
|
503
|
+
Math.max(0, b.r.incomeLost) - Math.max(0, a.r.incomeLost) ||
|
|
504
|
+
b.r.lost.length - a.r.lost.length ||
|
|
505
|
+
b.r.retaken.length + b.r.capturesFailed.length - (a.r.retaken.length + a.r.capturesFailed.length) ||
|
|
506
|
+
a.r.myArmies - a.r.theirArmies - (b.r.myArmies - b.r.theirArmies) ||
|
|
507
|
+
a.i - b.i)
|
|
508
|
+
.map((x) => x.r);
|
|
509
|
+
const tally = (pick) => {
|
|
510
|
+
const n = new Map();
|
|
511
|
+
for (const r of harmful)
|
|
512
|
+
if (!r.commanderLost)
|
|
513
|
+
for (const id of pick(r))
|
|
514
|
+
n.set(id, (n.get(id) ?? 0) + 1);
|
|
515
|
+
return [...n].sort((a, b) => a[0] - b[0]).map(([id, replies]) => ({ id, replies }));
|
|
516
|
+
};
|
|
517
|
+
const replies = order.slice(0, Math.max(0, input.maxReplies ?? DEFAULT_MAX_REPLIES));
|
|
518
|
+
// ---- one turn ahead (the shared look-ahead, lookahead.ts) ----
|
|
519
|
+
const exposed = new Map();
|
|
520
|
+
if (input.lookAhead !== false) {
|
|
521
|
+
const reaches = twoStepReaches({
|
|
522
|
+
graph,
|
|
523
|
+
board,
|
|
524
|
+
me,
|
|
525
|
+
opponents: input.opponents,
|
|
526
|
+
deploys: input.deploys,
|
|
527
|
+
moves: input.moves,
|
|
528
|
+
...(input.settings ? { settings: input.settings } : {}),
|
|
529
|
+
...(teams ? { teams } : {}),
|
|
530
|
+
...(input.firstMover !== undefined ? { firstMover: input.firstMover } : {}),
|
|
531
|
+
...(input.orderPriority ? { orderPriority: true } : {}),
|
|
532
|
+
});
|
|
533
|
+
for (const r of reaches) {
|
|
534
|
+
if (!r.falls)
|
|
535
|
+
continue;
|
|
536
|
+
const bonusesLost = bonusesWith([r.territory]).map((id) => ({ id, value: bonusValue(id) }));
|
|
537
|
+
const found = {
|
|
538
|
+
territory: r.territory,
|
|
539
|
+
armies: r.armies,
|
|
540
|
+
opponent: r.opponent,
|
|
541
|
+
from: r.from,
|
|
542
|
+
via: r.via,
|
|
543
|
+
attackers: r.attackers,
|
|
544
|
+
needs: r.needs,
|
|
545
|
+
bonusesLost,
|
|
546
|
+
incomeLost: bonusesLost.reduce((n, b) => n + b.value, 0),
|
|
547
|
+
};
|
|
548
|
+
const known = exposed.get(r.territory);
|
|
549
|
+
if (!known || found.attackers > known.attackers)
|
|
550
|
+
exposed.set(r.territory, found);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
const exposedNextTurn = [...exposed.values()].sort((a, b) => Math.max(0, b.incomeLost) - Math.max(0, a.incomeLost) || b.needs - b.armies - (a.needs - a.armies) || a.territory - b.territory);
|
|
554
|
+
return {
|
|
555
|
+
exact: baseline.exact,
|
|
556
|
+
assumptions: [...assumptions],
|
|
557
|
+
baseline: {
|
|
558
|
+
captured: [...baseCaptured].filter((id) => baseMine.has(id)).sort(byNum),
|
|
559
|
+
failed: baseline.log
|
|
560
|
+
.filter((e) => e.player === me && e.kind === 'attack' && e.result === 'failed')
|
|
561
|
+
.map((e) => e.to)
|
|
562
|
+
.sort(byNum),
|
|
563
|
+
heldBonuses: baseBonuses,
|
|
564
|
+
log: baseline.log.filter((e) => e.player === me),
|
|
565
|
+
},
|
|
566
|
+
considered: candidates.length,
|
|
567
|
+
runs,
|
|
568
|
+
harmful: harmfulCandidates,
|
|
569
|
+
replies,
|
|
570
|
+
...(replies[0] ? { worst: replies[0] } : {}),
|
|
571
|
+
atRisk: {
|
|
572
|
+
commander: harmful.filter((r) => r.commanderLost).length,
|
|
573
|
+
territories: tally((r) => r.lost),
|
|
574
|
+
captures: tally((r) => [...r.capturesFailed, ...r.retaken]),
|
|
575
|
+
bonuses: tally((r) => r.bonusesLost.map((b) => b.id)).map((x) => ({ id: x.id, value: bonusValue(x.id), replies: x.replies })),
|
|
576
|
+
},
|
|
577
|
+
dodges,
|
|
578
|
+
jointFalls,
|
|
579
|
+
blindBorders,
|
|
580
|
+
exposedNextTurn,
|
|
581
|
+
safe: candidates.length === 0 ? null : harmful.length === 0,
|
|
582
|
+
...(input.truces?.length ? { safeIfTruceHolds: keptCandidates === 0 ? null : harmfulKept === 0 } : {}),
|
|
583
|
+
};
|
|
584
|
+
}
|