@chrischall/warapp-mcp 0.7.0 → 0.8.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 +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/README.md +2 -2
- package/dist/analysis/card-support.js +12 -4
- package/dist/analysis/coalition.js +55 -0
- package/dist/analysis/local-deploy.js +14 -0
- package/dist/analysis/opponents.js +2 -1
- package/dist/analysis/picks.js +270 -37
- package/dist/analysis/plan/after-leftover.js +26 -4
- package/dist/analysis/plan/captures.js +76 -13
- package/dist/analysis/plan/completed.js +8 -1
- package/dist/analysis/plan/conceded-out.js +16 -11
- package/dist/analysis/plan/deploy-estimates.js +513 -0
- package/dist/analysis/plan/doomed-out.js +135 -0
- package/dist/analysis/plan/estimates.js +3 -1
- package/dist/analysis/plan/free-capture.js +34 -0
- package/dist/analysis/plan/holds.js +8 -309
- package/dist/analysis/plan/horizon.js +42 -0
- package/dist/analysis/plan/leftover.js +43 -5
- package/dist/analysis/plan/observe.js +896 -0
- package/dist/analysis/plan/orders.js +35 -1
- package/dist/analysis/plan/report.js +10 -4
- package/dist/analysis/plan/shared.js +4 -1376
- package/dist/analysis/plan/stand-still.js +5 -3
- package/dist/analysis/plan/state.js +12 -0
- package/dist/analysis/plan/strike-stack.js +467 -0
- package/dist/analysis/plan/strike.js +3 -463
- package/dist/analysis/plan/targets.js +213 -26
- package/dist/analysis/plan/threats.js +12 -4
- package/dist/analysis/plan/transfers.js +24 -1
- package/dist/analysis/plan/two-move.js +1 -1
- package/dist/analysis/plan/withdraw.js +316 -0
- package/dist/analysis/planner.js +54 -11
- package/dist/analysis/player.js +4 -0
- package/dist/analysis/relations.js +8 -2
- package/dist/analysis/stress.js +1 -1
- package/dist/bundle.js +8008 -7184
- package/dist/my-games.js +2 -0
- package/dist/open-games.js +94 -10
- package/dist/tools/card-advice.js +26 -11
- package/dist/tools/card-support-advice.js +17 -1
- package/dist/tools/check-plan.js +2 -1
- package/dist/tools/commander-check.js +2 -1
- package/dist/tools/decision-capture.js +4 -2
- package/dist/tools/join-game.js +44 -8
- package/dist/tools/live.js +33 -5
- package/dist/tools/open-games.js +2 -2
- package/dist/tools/picks.js +120 -47
- package/dist/tools/turn.js +43 -4
- package/dist/version.js +1 -1
- package/manifest.json +1 -1
- package/package.json +1 -1
- package/server.json +2 -2
|
@@ -0,0 +1,513 @@
|
|
|
1
|
+
import { commerceRules } from '../../commerce.js';
|
|
2
|
+
import { LEAD_ARMIES } from '../opponents.js';
|
|
3
|
+
import { bonusTerritories } from '../bonuses.js';
|
|
4
|
+
import { neighborsOf } from '../graph.js';
|
|
5
|
+
import { armyCapIncome, boardIncome, DEFAULT_BASE_INCOME, incomeRules, territoryIncome } from '../player.js';
|
|
6
|
+
import { byNum } from './util.js';
|
|
7
|
+
import { LOW_COVERAGE, MIN_ENEMY_DEPLOY, OWNERS_SHOWN_FOG, PRIOR_EVIDENCE_TURNS, TRACKED_MIN_TURNS, bombPossibleOn, fogPaceIncome, localFreeOn } from './shared.js';
|
|
8
|
+
import { observeDeploy, ownerShown, trackCoverage, visibleState } from './observe.js';
|
|
9
|
+
/**
|
|
10
|
+
* The army count every visible player territory holds, when they all hold the same one (the
|
|
11
|
+
* initial allotment). Neutrals are left out (#128): their stacks are not the starting stacks on
|
|
12
|
+
* ordinary maps, so counting them meant this never fired. Only the first turn can be the opening
|
|
13
|
+
* board (#128): with a previous board, or a known turn past the first, equal stacks are a
|
|
14
|
+
* coincidence of the mid-game, and reading them as the opening would drop visible bonuses.
|
|
15
|
+
*/
|
|
16
|
+
export function openingArmies(g, board, when = {}) {
|
|
17
|
+
if (when.previous !== undefined || (when.turn !== null && when.turn !== undefined && when.turn > 1))
|
|
18
|
+
return null;
|
|
19
|
+
const counts = new Set();
|
|
20
|
+
for (const t of board.territories)
|
|
21
|
+
if (g.territoryById.has(t.territoryId) && t.armies !== null && t.owner.kind === 'player')
|
|
22
|
+
counts.add(t.armies);
|
|
23
|
+
return counts.size === 1 ? [...counts][0] : null;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* #164: the deployments read as card plays that count as cards — each on or after the turn the clock
|
|
27
|
+
* says one is due and before `turn` — and the turn the next card is due: a card's capturing turns after
|
|
28
|
+
* the last one counted (pieces carry over while a card is held), never before the turn after the play.
|
|
29
|
+
*/
|
|
30
|
+
function cardsCounted(played, r, turn) {
|
|
31
|
+
let due = r.firstTurn;
|
|
32
|
+
const counted = [];
|
|
33
|
+
for (const t of [...new Set(played)].sort(byNum)) {
|
|
34
|
+
if (t < due || t >= turn)
|
|
35
|
+
continue;
|
|
36
|
+
counted.push(t);
|
|
37
|
+
due = r.turnsPerCard === null ? Infinity : Math.max(t + 1, r.firstTurn + counted.length * Math.max(1, r.turnsPerCard));
|
|
38
|
+
}
|
|
39
|
+
return { counted, due };
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* #368: an opponent's tracked figure as of each of its last three consecutive observed turns — the most
|
|
43
|
+
* it was seen to earn (`earned`) over that turn and the two before it, the tracker's max-observed window —
|
|
44
|
+
* when it rose on both steps; else `null` (fewer than three consecutive turns, or a step not a rise).
|
|
45
|
+
*/
|
|
46
|
+
export function risingTracked(turns) {
|
|
47
|
+
const n = turns.length;
|
|
48
|
+
if (n < 3)
|
|
49
|
+
return null;
|
|
50
|
+
for (let i = n - 2; i < n; i++)
|
|
51
|
+
if (turns[i].from !== turns[i - 1].from + 1)
|
|
52
|
+
return null;
|
|
53
|
+
const asOf = (i) => Math.max(...turns.slice(Math.max(0, i - 2), i + 1).map((x) => x.earned));
|
|
54
|
+
const series = [asOf(n - 3), asOf(n - 2), asOf(n - 1)];
|
|
55
|
+
return series[0] < series[1] && series[1] < series[2] ? series : null;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Per-opponent enemy deploy estimates, one per visible enemy player (sorted by
|
|
59
|
+
* id): base income plus the bonuses that player visibly holds in full, never
|
|
60
|
+
* below max({@link MIN_ENEMY_DEPLOY}, base income). Its stack next to me does not
|
|
61
|
+
* limit it — income held in plain view can all be deployed on my border, however
|
|
62
|
+
* small the stack there. Bonuses in the fog are unseen, so this is what the
|
|
63
|
+
* visible board supports. On the opening board (every visible player territory holds
|
|
64
|
+
* the same count, with no previous board and `when.turn` unknown or the first) every
|
|
65
|
+
* opponent gets the floor.
|
|
66
|
+
*/
|
|
67
|
+
export function enemyDeployEstimates(g, board, me, settings = {}, previous, tracks, prior, when = {}) {
|
|
68
|
+
const owner = new Map();
|
|
69
|
+
const armies = new Map();
|
|
70
|
+
const seenNow = new Map();
|
|
71
|
+
for (const t of board.territories)
|
|
72
|
+
if (g.territoryById.has(t.territoryId))
|
|
73
|
+
seenNow.set(t.territoryId, t);
|
|
74
|
+
// Owners count whatever the armies: an owner-only territory (light fog) is visibly held, so
|
|
75
|
+
// it counts toward base income and the bonuses held in full. Only stacks need the armies.
|
|
76
|
+
for (const t of board.territories) {
|
|
77
|
+
if (!g.territoryById.has(t.territoryId) || t.owner.kind !== 'player')
|
|
78
|
+
continue;
|
|
79
|
+
owner.set(t.territoryId, t.owner.playerId);
|
|
80
|
+
if (t.armies !== null)
|
|
81
|
+
armies.set(t.territoryId, t.armies);
|
|
82
|
+
}
|
|
83
|
+
const players = [...new Set([...owner.values()].filter((p) => p !== me))].sort(byNum);
|
|
84
|
+
const opening = openingArmies(g, board, { turn: when.turn, previous }) !== null;
|
|
85
|
+
// #164: under No, Light and Dense fog every owner shows, so income is exact — when this board
|
|
86
|
+
// shows every territory's owner too (the setting is a name, never compared by position).
|
|
87
|
+
const ownersAll = when.fog !== undefined &&
|
|
88
|
+
OWNERS_SHOWN_FOG.has(when.fog) &&
|
|
89
|
+
g.map.territories.every((t) => ownerShown(seenNow.get(t.id)) !== undefined);
|
|
90
|
+
const minimum = Math.max(0, Math.floor(settings.baseIncome ?? DEFAULT_BASE_INCOME));
|
|
91
|
+
const per = settings.bonusArmyPer ?? 0;
|
|
92
|
+
// #418: my income on the board (cards aside) and my armies in view, against which a rising track is weighed.
|
|
93
|
+
const myLand = new Set();
|
|
94
|
+
let myArmies = 0;
|
|
95
|
+
for (const [t, p] of owner)
|
|
96
|
+
if (p === me) {
|
|
97
|
+
myLand.add(t);
|
|
98
|
+
myArmies += armies.get(t) ?? 0;
|
|
99
|
+
}
|
|
100
|
+
const myBoardIncome = boardIncome(g, myLand, { base: minimum, per }).total;
|
|
101
|
+
return players.map((playerId) => {
|
|
102
|
+
// Its visible territories are a lower bound on what it holds, so on its
|
|
103
|
+
// per-territory income too.
|
|
104
|
+
const theirs = new Set();
|
|
105
|
+
for (const [t, p] of owner)
|
|
106
|
+
if (p === playerId)
|
|
107
|
+
theirs.add(t);
|
|
108
|
+
const held = theirs.size;
|
|
109
|
+
const base = territoryIncome(held, minimum, per);
|
|
110
|
+
// The one income rule (#85) over what it visibly holds; `bonusIncome` is what it adds to the base.
|
|
111
|
+
const bonusIncome = boardIncome(g, theirs, { base: minimum, per }).total - base;
|
|
112
|
+
let adjacent = 0;
|
|
113
|
+
let any = 0;
|
|
114
|
+
for (const [t, p] of owner) {
|
|
115
|
+
if (p !== playerId)
|
|
116
|
+
continue;
|
|
117
|
+
const a = armies.get(t);
|
|
118
|
+
if (a === undefined)
|
|
119
|
+
continue;
|
|
120
|
+
any = Math.max(any, a);
|
|
121
|
+
if (neighborsOf(g, t).some((n) => owner.get(n) === me))
|
|
122
|
+
adjacent = Math.max(adjacent, a);
|
|
123
|
+
}
|
|
124
|
+
const largestStack = adjacent || any;
|
|
125
|
+
// Every opponent earns at least the game's base income (never below the 5 minimum), and
|
|
126
|
+
// never less than the bonuses it visibly holds: its stack next to me limits neither.
|
|
127
|
+
const floor = Math.max(MIN_ENEMY_DEPLOY, base);
|
|
128
|
+
const visible = opening ? floor : Math.max(floor, base + bonusIncome);
|
|
129
|
+
// What it was seen to deploy is cut only by what it could earn
|
|
130
|
+
// (#69): its base plus every bonus none of whose territories someone else visibly holds
|
|
131
|
+
// (an owner-only territory is visibly held: its owner shows). Both come from this board,
|
|
132
|
+
// so a bonus it lost since last turn is out, though it may have paid for that deploy.
|
|
133
|
+
let possible = 0;
|
|
134
|
+
/** #235: of `possible`, the bonuses it visibly holds in full (no share of them goes to another). */
|
|
135
|
+
let possibleOwn = 0;
|
|
136
|
+
for (const b of g.map.bonuses) {
|
|
137
|
+
const ts = bonusTerritories(g, b.id);
|
|
138
|
+
if (b.value <= 0 || !ts.length)
|
|
139
|
+
continue;
|
|
140
|
+
const taken = ts.some((t) => {
|
|
141
|
+
const st = ownerShown(seenNow.get(t));
|
|
142
|
+
return st !== undefined && !(st.owner.kind === 'player' && st.owner.playerId === playerId);
|
|
143
|
+
});
|
|
144
|
+
if (!taken)
|
|
145
|
+
possible += b.value;
|
|
146
|
+
if (!taken && ts.every((t) => theirs.has(t)))
|
|
147
|
+
possibleOwn += b.value;
|
|
148
|
+
}
|
|
149
|
+
// #226: with per-territory income, the territories it could hold pay at the rate: its visible
|
|
150
|
+
// ones and every one whose owner this board hides (it may hold any of them).
|
|
151
|
+
const couldHold = per > 0
|
|
152
|
+
? g.map.territories.filter((t) => {
|
|
153
|
+
const st = ownerShown(seenNow.get(t.id));
|
|
154
|
+
return st === undefined || (st.owner.kind === 'player' && st.owner.playerId === playerId) || theirs.has(t.id);
|
|
155
|
+
}).length
|
|
156
|
+
: held;
|
|
157
|
+
const couldEarn = territoryIncome(couldHold, minimum, per) + possible;
|
|
158
|
+
// #119: the fog prior. In a symmetric-start 1v1 an opponent bordering the fog earns at least
|
|
159
|
+
// what I earn, capped at what it could earn on this board. It only ever raises the figure.
|
|
160
|
+
const fogBeside = [...theirs].some((t) => neighborsOf(g, t).some((n) => !ownerShown(seenNow.get(n))));
|
|
161
|
+
// #166: with its starts proven, its opening simulated from them raises the prior when it earns
|
|
162
|
+
// more than I do; never lowers it (the simulation is a simple player, and #119's evidence read low).
|
|
163
|
+
const priorBase = Math.max(prior?.myIncome ?? 0, prior?.opening?.get(playerId) ?? 0);
|
|
164
|
+
// #235: in a free-for-all the hidden part of the board is shared: an even share of the hidden
|
|
165
|
+
// territories and of the bonuses it could still hold (its own in full), never above couldEarn.
|
|
166
|
+
const k = prior?.format === 'ffa' && (prior.opponents ?? 0) > 1 ? prior.opponents : 0;
|
|
167
|
+
const priorCap = prior && k > 0
|
|
168
|
+
? Math.min(couldEarn, territoryIncome(held + Math.ceil((couldHold - held) / k), minimum, per) + possibleOwn + Math.ceil((possible - possibleOwn) / k))
|
|
169
|
+
: null;
|
|
170
|
+
const priorFull = prior && fogBeside ? Math.min(Math.max(0, Math.floor(priorBase)), priorCap ?? couldEarn) : null;
|
|
171
|
+
// #444: incomes other players stated scale this one's prior down by their ratio to mine (never up).
|
|
172
|
+
const statedRatio = prior?.stated && !prior.stated.players.some((x) => x.id === playerId) ? prior.stated.ratio : null;
|
|
173
|
+
const priorFigure = priorFull !== null && statedRatio !== null ? Math.min(priorFull, Math.ceil(priorFull * statedRatio - 1e-9)) : priorFull;
|
|
174
|
+
// #419: the past winners' pace, as the prior is figured (capped, scaled, only beside the fog), when above it.
|
|
175
|
+
const paceIncome = prior?.winnerPace ? fogPaceIncome(prior.winnerPace, prior.myIncome) : null;
|
|
176
|
+
const paceFull = paceIncome !== null && fogBeside && paceIncome > priorBase ? Math.min(paceIncome, priorCap ?? couldEarn) : null;
|
|
177
|
+
const paceFigure = paceFull !== null && statedRatio !== null ? Math.min(paceFull, Math.ceil(paceFull * statedRatio - 1e-9)) : paceFull;
|
|
178
|
+
const paceTop = paceFigure !== null && priorFigure !== null && paceFigure > priorFigure ? paceFigure : null;
|
|
179
|
+
// A track over enough turns replaces the single-turn observed figure (S3).
|
|
180
|
+
const track = tracks?.find((t) => t.playerId === playerId);
|
|
181
|
+
const usable = track && track.income !== null && track.turns.length >= TRACKED_MIN_TURNS ? track : undefined;
|
|
182
|
+
// #164: with every owner shown, its income is base + held bonuses exactly; a deploy above it was
|
|
183
|
+
// a card (or armies the board misread), never income.
|
|
184
|
+
const exact = ownersAll && !opening ? Math.max(0, base + bonusIncome) : null;
|
|
185
|
+
// #164: a Reinforcement card play is seen as a deploy (its low reading) above the income that turn:
|
|
186
|
+
// exactly, when every owner shows; under fog only a jump of the card's armies over its visible income
|
|
187
|
+
// counts (a smaller excess may be a bonus the fog hides — and the tracked income then carries the jump).
|
|
188
|
+
const above = (low, income) => (exact !== null ? low > income : low >= income + when.reinforcement.armies);
|
|
189
|
+
let tracked = usable ? usable.income : null;
|
|
190
|
+
// #427: the tracker reads every deploy as income, so a card play it saw set its figure (the most it was
|
|
191
|
+
// seen to deploy) and was then carried, while the clock adds the next card on top: one card counted
|
|
192
|
+
// twice, every turn. When the turns that set the figure are all plays counted as cards, it is read
|
|
193
|
+
// again with each play's card armies out (never below its visible income, never above the figure).
|
|
194
|
+
let cardInTrack = null;
|
|
195
|
+
if (usable && tracked !== null && when.reinforcement && when.reinforcement.armies > 0 && when.turn !== null && when.turn !== undefined) {
|
|
196
|
+
const r = when.reinforcement;
|
|
197
|
+
const plays = new Set(cardsCounted(usable.turns.filter((x) => above(x.low, x.visibleIncome)).map((x) => x.from), r, when.turn).counted);
|
|
198
|
+
const top = usable.turns.filter((x) => x.observed >= tracked);
|
|
199
|
+
if (top.length && top.every((x) => plays.has(x.from))) {
|
|
200
|
+
const without = Math.max(usable.visibleIncome, ...usable.turns.map((x) => (plays.has(x.from) ? Math.min(x.cap, Math.max(0, x.raw - r.armies)) : x.observed)));
|
|
201
|
+
if (without < tracked) {
|
|
202
|
+
cardInTrack = { was: tracked, income: without, armies: r.armies, turns: top.map((x) => x.from).sort(byNum) };
|
|
203
|
+
tracked = without;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// #235 review: does the track end on this board? Its newest observed turn then ends at the turn
|
|
208
|
+
// being planned (the newest board's turn, when that is not given: tracks end on `board`).
|
|
209
|
+
const newestTurn = when.turn ?? usable?.lastTurn ?? null;
|
|
210
|
+
const newest = usable?.turns.at(-1);
|
|
211
|
+
const trackEndsNow = newest !== undefined && newest.to === newestTurn;
|
|
212
|
+
// #123: what it was expected to deploy before this observation (its visible income, and its
|
|
213
|
+
// tracked income when it has a usable track, else the prior): ambiguous armies beyond twice that
|
|
214
|
+
// are moves out of the fog ({@link ambiguousBound}); a capture from me is priced at the guard.
|
|
215
|
+
// With a track the prior stays out of the bound, and the reading is no part of the estimate
|
|
216
|
+
// (#235 1c): the track replaces it. A track that ends on this board has read the newest turn
|
|
217
|
+
// already, so the turn is read with that track's own bound (`ObservedTurn.expected`), and the
|
|
218
|
+
// rationale's observed line agrees with the tracker and with the label below.
|
|
219
|
+
const trackBound = trackEndsNow ? newest.expected : undefined;
|
|
220
|
+
const observation = previous
|
|
221
|
+
? observeDeploy(g, board, previous, playerId, settings, {
|
|
222
|
+
me,
|
|
223
|
+
guard: (settings.oneArmyStandsGuard ?? true) ? 1 : 0,
|
|
224
|
+
expected: Math.max(visible, trackBound ?? tracked ?? priorFull ?? 0),
|
|
225
|
+
most: couldEarn,
|
|
226
|
+
// #293: under Local Deployment, growth beside the fog outside every bonus it could hold is
|
|
227
|
+
// deploy only up to its free income.
|
|
228
|
+
...(settings.localDeployments
|
|
229
|
+
? { localFree: localFreeOn(base, MIN_ENEMY_DEPLOY, when.reinforcement ?? settings.opponentReinforcement, when.turn == null ? null : when.turn - 1) }
|
|
230
|
+
: {}),
|
|
231
|
+
...(when.previousOrders ? { myOrders: when.previousOrders } : {}),
|
|
232
|
+
// #271 review: the turn read is the one before the turn planned.
|
|
233
|
+
...(bombPossibleOn(settings, when.turn == null ? null : when.turn - 1) ? { bomb: true } : {}),
|
|
234
|
+
})
|
|
235
|
+
: null;
|
|
236
|
+
// #235 1c: the track's newest turn, when it ends on this board and its bound cut armies that
|
|
237
|
+
// arrived beside the fog (or joined a capture): what that turn reads with them all as deploys.
|
|
238
|
+
// A label only: a stack walking in is not income (#123 lesson 5), so the estimate stays.
|
|
239
|
+
const newestCut = trackEndsNow && newest.high > newest.raw && (newest.grewFromFog.length > 0 || newest.reinforced.length > 0)
|
|
240
|
+
? {
|
|
241
|
+
from: newest.from,
|
|
242
|
+
to: newest.to,
|
|
243
|
+
observed: newest.observed,
|
|
244
|
+
reading: Math.min(newest.high, newest.cap),
|
|
245
|
+
expected: newest.expected ?? null,
|
|
246
|
+
}
|
|
247
|
+
: null;
|
|
248
|
+
const observedCap = observation ? couldEarn : null;
|
|
249
|
+
const observedRaw = observation?.deploy ?? null;
|
|
250
|
+
const observed = observation ? Math.min(observation.deploy, observedCap) : null;
|
|
251
|
+
// A usable track replaces the single-turn observed figure (S3; #235 1c: its newest reading too).
|
|
252
|
+
const seenSource = tracked !== null
|
|
253
|
+
? tracked > visible
|
|
254
|
+
? 'tracked'
|
|
255
|
+
: 'visible'
|
|
256
|
+
: observed !== null && observed > visible
|
|
257
|
+
? 'observed'
|
|
258
|
+
: 'visible';
|
|
259
|
+
const seen = seenSource === 'tracked' ? tracked : seenSource === 'observed' ? observed : visible;
|
|
260
|
+
// #235: the prior shrinks toward that evidence: the more consecutive observed turns, and the more
|
|
261
|
+
// of its edge in view, the less of its lead it keeps.
|
|
262
|
+
let priorBlend = null;
|
|
263
|
+
if (priorFigure !== null && usable) {
|
|
264
|
+
const toward = Math.max(tracked, visible);
|
|
265
|
+
if (priorFigure > toward) {
|
|
266
|
+
// The consecutive observed turns up to the newest board; a run that stopped earlier is none.
|
|
267
|
+
let turns = trackEndsNow ? 1 : 0;
|
|
268
|
+
const run = usable.turns;
|
|
269
|
+
for (let i = run.length - 1; turns > 0 && i > 0 && run[i - 1].from === run[i].from - 1; i--)
|
|
270
|
+
turns++;
|
|
271
|
+
const edge = new Set();
|
|
272
|
+
for (const t of theirs)
|
|
273
|
+
for (const n of neighborsOf(g, t))
|
|
274
|
+
if (!theirs.has(n))
|
|
275
|
+
edge.add(n);
|
|
276
|
+
const edgeShare = edge.size ? [...edge].filter((n) => ownerShown(seenNow.get(n)) !== undefined).length / edge.size : 0;
|
|
277
|
+
// #294: each turn of the run weighs what it read of its frontier (this board's edge for a turn
|
|
278
|
+
// that carries no coverage).
|
|
279
|
+
const evidence = run.slice(run.length - turns).reduce((n, x) => n + (x.coverage?.share ?? edgeShare), 0);
|
|
280
|
+
const share = turns ? evidence / turns : edgeShare;
|
|
281
|
+
const weight = PRIOR_EVIDENCE_TURNS / (PRIOR_EVIDENCE_TURNS + evidence);
|
|
282
|
+
const figure = toward + Math.ceil(weight * (priorFigure - toward) - 1e-9);
|
|
283
|
+
priorBlend = { prior: priorFigure, toward, turns, share, weight, figure };
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
const priorUsed = priorBlend ? priorBlend.figure : priorFigure;
|
|
287
|
+
const source = exact !== null ? 'exact' : priorUsed !== null && priorUsed > seen ? 'prior' : seenSource;
|
|
288
|
+
// #294: what its tracked figure was read from (#368: while that is little, thin evidence below).
|
|
289
|
+
const coverage = usable ? trackCoverage(usable.turns.filter((x) => usable.incomeTurns.includes(x.from))) : null;
|
|
290
|
+
// #444: an untracked opponent whose prior a stated income scaled: #368's midpoint between what is in
|
|
291
|
+
// view (or observed) and that prior.
|
|
292
|
+
const statedMid = statedRatio !== null && source === 'prior' && !usable ? seen + Math.ceil((priorUsed - seen) / 2 - 1e-9) : null;
|
|
293
|
+
const chosen = source === 'exact' ? exact : source === 'prior' ? (statedMid ?? priorUsed) : seen;
|
|
294
|
+
// #368 (owner decision of 2026-11-19: on a hidden opponent, choose the middle until we learn
|
|
295
|
+
// otherwise): thin evidence — its tracked figure read from under LOW_COVERAGE of its frontier —
|
|
296
|
+
// sizes it for the midpoint between the lower bound (what is in view or tracked, raised to the
|
|
297
|
+
// trend's next step when its tracked figure rose two turns running, #418: unless I lead it on armies
|
|
298
|
+
// in view and on income — above its tracked figure, or with LEAD_ARMIES × its armies above what it
|
|
299
|
+
// earned before the newest rise) and the upper bound (the fog
|
|
300
|
+
// prior whole, else the most it could earn), moved toward what the evidence gives as the coverage
|
|
301
|
+
// nears LOW_COVERAGE. A rising track read from more of its frontier only raises the estimate to that
|
|
302
|
+
// lower bound. Never the maximum.
|
|
303
|
+
let thinEvidence = null;
|
|
304
|
+
if (source !== 'exact' && usable && tracked !== null) {
|
|
305
|
+
// #418: no trend step on an opponent I lead on income (its tracked figure) and on armies in view.
|
|
306
|
+
// #418 review: with LEAD_ARMIES × its armies in view, the income lead is judged against what it
|
|
307
|
+
// earned before the newest rise (or its visible income) instead: an over-read tracked figure must
|
|
308
|
+
// not switch off the gate meant to stop it. (On that judgement alone, with a bare army lead, the
|
|
309
|
+
// calibration lost 3 of 4 changed samples further below truth.)
|
|
310
|
+
let theirArmies = 0;
|
|
311
|
+
for (const t of theirs)
|
|
312
|
+
theirArmies += armies.get(t) ?? 0;
|
|
313
|
+
const risen = risingTracked(usable.turns);
|
|
314
|
+
const leadIt = risen !== null &&
|
|
315
|
+
myArmies > theirArmies &&
|
|
316
|
+
(myBoardIncome > Math.max(visible, tracked) || (myArmies >= LEAD_ARMIES * theirArmies && myBoardIncome > Math.max(visible, risen[1])));
|
|
317
|
+
const rising = leadIt ? null : risen;
|
|
318
|
+
const thinCoverage = coverage !== null && coverage.share < LOW_COVERAGE;
|
|
319
|
+
if (thinCoverage || rising) {
|
|
320
|
+
const trend = rising ? Math.min(couldEarn, rising[2] + Math.ceil((rising[2] - rising[0]) / 2 - 1e-9)) : null;
|
|
321
|
+
const lower = Math.min(couldEarn, Math.max(visible, tracked, trend ?? 0));
|
|
322
|
+
// #419: the top of its range is the larger of the prior and the past winners' pace.
|
|
323
|
+
const upper = Math.max(lower, Math.min(couldEarn, paceTop ?? priorFigure ?? couldEarn));
|
|
324
|
+
const midpoint = lower + Math.ceil((upper - lower) / 2 - 1e-9);
|
|
325
|
+
// What the evidence gives without thin evidence (the track, or #235's prior shrunk toward it),
|
|
326
|
+
// never below the lower bound: the figure meets it at LOW_COVERAGE, so it does not jump there.
|
|
327
|
+
const toward = Math.min(couldEarn, Math.max(lower, chosen));
|
|
328
|
+
const weight = thinCoverage ? Math.max(0, 1 - coverage.share / LOW_COVERAGE) : 0;
|
|
329
|
+
const figure = toward + Math.ceil(weight * (midpoint - toward) - 1e-9);
|
|
330
|
+
// Thin coverage: the figure replaces the estimate, up or down. A rising track alone: a raise only.
|
|
331
|
+
if (thinCoverage || figure > chosen)
|
|
332
|
+
thinEvidence = {
|
|
333
|
+
coverage: coverage ? coverage.share : null,
|
|
334
|
+
rising,
|
|
335
|
+
tracked,
|
|
336
|
+
prior: priorFigure,
|
|
337
|
+
trend,
|
|
338
|
+
lower,
|
|
339
|
+
upper,
|
|
340
|
+
midpoint,
|
|
341
|
+
toward,
|
|
342
|
+
weight,
|
|
343
|
+
figure,
|
|
344
|
+
...(paceTop !== null && prior?.winnerPace
|
|
345
|
+
? {
|
|
346
|
+
pace: {
|
|
347
|
+
figure: paceTop,
|
|
348
|
+
basis: prior.winnerPace.basis,
|
|
349
|
+
afterTurn: prior.winnerPace.afterTurn,
|
|
350
|
+
games: prior.winnerPace.games,
|
|
351
|
+
...(prior.winnerPace.ratio !== undefined ? { ratio: prior.winnerPace.ratio } : {}),
|
|
352
|
+
},
|
|
353
|
+
}
|
|
354
|
+
: {}),
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
const income = thinEvidence ? thinEvidence.figure : chosen;
|
|
359
|
+
// #368: the figure thin evidence chose names its source: the prior when there is one and the figure
|
|
360
|
+
// lies above the lower bound, else the track.
|
|
361
|
+
const finalSource = thinEvidence
|
|
362
|
+
? thinEvidence.prior !== null && thinEvidence.figure > thinEvidence.lower
|
|
363
|
+
? 'prior'
|
|
364
|
+
: 'tracked'
|
|
365
|
+
: source;
|
|
366
|
+
// #164: a Reinforcement card, from the turn the clock says one can be in hand, until a deployment
|
|
367
|
+
// above its income is seen (`above`); the next card is due as {@link cardsCounted} says.
|
|
368
|
+
const played = [];
|
|
369
|
+
if (when.reinforcement) {
|
|
370
|
+
for (const x of track?.turns ?? [])
|
|
371
|
+
if (above(x.low, x.visibleIncome))
|
|
372
|
+
played.push(x.from);
|
|
373
|
+
// Last turn's deploy was paid by last turn's income: what it held on the previous board (a
|
|
374
|
+
// bonus it lost since paid for it), never below this board's figure.
|
|
375
|
+
if (observation && previous && when.turn !== null && when.turn !== undefined) {
|
|
376
|
+
const before = new Set();
|
|
377
|
+
for (const t of previous.territories)
|
|
378
|
+
if (g.territoryById.has(t.territoryId) && t.owner.kind === 'player' && t.owner.playerId === playerId)
|
|
379
|
+
before.add(t.territoryId);
|
|
380
|
+
const paid = boardIncome(g, before, { base: minimum, per }).total;
|
|
381
|
+
if (above(observation.low, Math.max(exact ?? visible, paid)))
|
|
382
|
+
played.push(when.turn - 1);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
let card = null;
|
|
386
|
+
let cardPlayed = null;
|
|
387
|
+
if (when.reinforcement && when.turn !== null && when.turn !== undefined) {
|
|
388
|
+
const r = when.reinforcement;
|
|
389
|
+
const { counted, due } = cardsCounted(played, r, when.turn);
|
|
390
|
+
cardPlayed = counted.at(-1) ?? null;
|
|
391
|
+
if (when.turn >= due && r.armies > 0)
|
|
392
|
+
card = { armies: r.armies, due };
|
|
393
|
+
}
|
|
394
|
+
const estimate = income + (card?.armies ?? 0);
|
|
395
|
+
return {
|
|
396
|
+
playerId,
|
|
397
|
+
estimate,
|
|
398
|
+
held,
|
|
399
|
+
base,
|
|
400
|
+
bonusIncome,
|
|
401
|
+
largestStack,
|
|
402
|
+
opening,
|
|
403
|
+
observed,
|
|
404
|
+
observedRaw,
|
|
405
|
+
observedCap,
|
|
406
|
+
couldEarn,
|
|
407
|
+
couldHold,
|
|
408
|
+
observation,
|
|
409
|
+
tracked,
|
|
410
|
+
newestCut,
|
|
411
|
+
trackedTurns: usable ? usable.incomeTurns : [],
|
|
412
|
+
prior: priorFigure,
|
|
413
|
+
priorBlend,
|
|
414
|
+
coverage,
|
|
415
|
+
priorCap,
|
|
416
|
+
fogBeside,
|
|
417
|
+
source: finalSource,
|
|
418
|
+
thinEvidence,
|
|
419
|
+
exact,
|
|
420
|
+
card,
|
|
421
|
+
cardPlayed,
|
|
422
|
+
...(cardInTrack ? { cardInTrack } : {}),
|
|
423
|
+
...(priorFull !== null && priorFigure < priorFull ? { stated: { full: priorFull, scaled: priorFigure, lower: seen, midpoint: statedMid } } : {}),
|
|
424
|
+
};
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Default enemy deploy: the largest {@link enemyDeployEstimates} figure among the
|
|
429
|
+
* players with a stack next to me — visible, or its owner shown and its armies hidden (#164) —
|
|
430
|
+
* at least {@link MIN_ENEMY_DEPLOY}. Pass the turn, fog setting and Reinforcement clock (`when`)
|
|
431
|
+
* and the fog prior {@link planEnemyEstimates} uses, or a mid-game board of equal stacks reads as
|
|
432
|
+
* the opening board (#128), an opponent's card is left out (#164), and the figure disagrees with
|
|
433
|
+
* the planner's. Unlike the planner's, it does not know the teams: a teammate counts.
|
|
434
|
+
*/
|
|
435
|
+
export function defaultEnemyDeploy(g, board, me, settings = {}, previous, tracks, prior, when = {}) {
|
|
436
|
+
const mineIds = new Set(board.territories.flatMap((t) => (t.owner.kind === 'player' && t.owner.playerId === me ? [t.territoryId] : [])));
|
|
437
|
+
const next = new Set();
|
|
438
|
+
for (const t of board.territories) {
|
|
439
|
+
if (t.owner.kind !== 'player' || t.owner.playerId === me)
|
|
440
|
+
continue;
|
|
441
|
+
if (t.armies === null && (ownerShown(t) === undefined || visibleState(t)))
|
|
442
|
+
continue;
|
|
443
|
+
if (!g.territoryById.has(t.territoryId))
|
|
444
|
+
continue;
|
|
445
|
+
if (neighborsOf(g, t.territoryId).some((n) => mineIds.has(n)))
|
|
446
|
+
next.add(t.owner.playerId);
|
|
447
|
+
}
|
|
448
|
+
return enemyDeployEstimates(g, board, me, settings, previous, tracks, prior, when).reduce((m, e) => (next.has(e.playerId) ? Math.max(m, e.estimate) : m), MIN_ENEMY_DEPLOY);
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* This turn's income on the board, cards aside: {@link boardIncome} over the territories `me`
|
|
452
|
+
* holds, cut by the game's army cap (#253, {@link armyCapIncome}: min(income, round(cap × income)
|
|
453
|
+
* − my armies on the board)). warapp_plan_turn and warapp_submit_turn both use it (plus the armies
|
|
454
|
+
* of the card plays in their orders, which the cap leaves alone), so the plan's deploy total is the
|
|
455
|
+
* total the submit check requires.
|
|
456
|
+
*/
|
|
457
|
+
export function incomeFor(g, board, me, settings) {
|
|
458
|
+
return incomeDetail(g, board, me, settings).income;
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Another player's income from what the board shows, before any army cap: base, bonuses and cities
|
|
462
|
+
* over the territories it visibly holds. The cap is not applied (#253): its fogged armies are
|
|
463
|
+
* unknown, so a cut from its visible armies alone would depend on the fog. My own income is
|
|
464
|
+
* {@link incomeFor}.
|
|
465
|
+
*/
|
|
466
|
+
export function visibleIncomeFor(g, board, player, settings) {
|
|
467
|
+
const held = new Set();
|
|
468
|
+
let cities = 0;
|
|
469
|
+
for (const t of board.territories)
|
|
470
|
+
if (g.territoryById.has(t.territoryId) && t.owner.kind === 'player' && t.owner.playerId === player) {
|
|
471
|
+
held.add(t.territoryId);
|
|
472
|
+
cities += t.cities ?? 0;
|
|
473
|
+
}
|
|
474
|
+
return boardIncome(g, held, incomeRules(settings)).total + cityGold(cities, settings);
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* #332: the gold a commerce game's cities pay each turn (CityIncome each), part of the income like a bonus (the client's
|
|
478
|
+
* income function adds it after base and bonuses, before the army cap). 0 outside a commerce game with city income.
|
|
479
|
+
*/
|
|
480
|
+
export function cityGold(cities, settings) {
|
|
481
|
+
const per = commerceRules(settings)?.cityIncome ?? 0;
|
|
482
|
+
return cities > 0 && per > 0 ? cities * per : 0;
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* {@link incomeFor} with the army cap's arithmetic: `cap` when the game has one and every territory
|
|
486
|
+
* of mine shows its armies (my own always should); `capUnknown` when one does not, and the income is
|
|
487
|
+
* then left uncut. The cap reads my armies from the start-of-turn standing as war.app showed it:
|
|
488
|
+
* callers may pass a board with the award or a prior merged in (livePlanSetup's `board`), which must
|
|
489
|
+
* never change my own territories' armies — a merge that did would move the cap's cut.
|
|
490
|
+
*/
|
|
491
|
+
export function incomeDetail(g, board, me, settings) {
|
|
492
|
+
const held = new Set();
|
|
493
|
+
let armies = 0;
|
|
494
|
+
let unread = false;
|
|
495
|
+
let cities = 0;
|
|
496
|
+
for (const t of board.territories) {
|
|
497
|
+
if (!g.territoryById.has(t.territoryId) || t.owner.kind !== 'player' || t.owner.playerId !== me)
|
|
498
|
+
continue;
|
|
499
|
+
held.add(t.territoryId);
|
|
500
|
+
cities += t.cities ?? 0;
|
|
501
|
+
if (t.armies === null)
|
|
502
|
+
unread = true;
|
|
503
|
+
else
|
|
504
|
+
armies += t.armies;
|
|
505
|
+
}
|
|
506
|
+
const income = boardIncome(g, held, incomeRules(settings)).total + cityGold(cities, settings);
|
|
507
|
+
if (settings?.armyCap === undefined || settings.armyCap === null || !(settings.armyCap > 0))
|
|
508
|
+
return { income };
|
|
509
|
+
if (unread)
|
|
510
|
+
return { income, capUnknown: true };
|
|
511
|
+
const cap = armyCapIncome(income, settings.armyCap, armies);
|
|
512
|
+
return { income: cap.received, cap };
|
|
513
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// #431: a doomed conceded stack attacks out or merges instead of standing. Rule 1b withdraws a conceded
|
|
2
|
+
// border's movable armies toward a border that holds; with no neighbour of mine that is not conceded they
|
|
3
|
+
// stayed ("… has nowhere to withdraw to: its N movable armies stay"), even when the expected attack (#59)
|
|
4
|
+
// takes the stack. Then they die where they stand, killing only the defensive share of the attacker. In a
|
|
5
|
+
// finished 3-player FFA (45684504) pocket stacks of ~1,000-1,800 were lost that way, most beside a
|
|
6
|
+
// territory they could have taken.
|
|
7
|
+
//
|
|
8
|
+
// So, after #272's free steps and before rule 1c and rule 1b: a conceded border of mine with no such
|
|
9
|
+
// neighbour, not my commander's, not kept by rule 1c, whose stack the expected attack takes, weighs its
|
|
10
|
+
// options by the exchange model (src/analysis/exchange.ts), each by the armies of mine still standing
|
|
11
|
+
// after the turn plus the enemy armies killed (a neutral's dead are no gain):
|
|
12
|
+
// - stand: nothing of mine survives; the attacker loses the defensive share of the stack (at most the
|
|
13
|
+
// expected attack);
|
|
14
|
+
// - attack out with the whole movable stack into a neighbour it captures against the owner's estimated
|
|
15
|
+
// deploy there (`classifyExchange`'s `estimated` case; never a truce's covered territory, one with a
|
|
16
|
+
// special unit, a dropped member or one lowering income; a #240 repulsed one only at its sure size): the survivors live on;
|
|
17
|
+
// - merge into a neighbouring conceded stack of mine that has nowhere to withdraw to either and that,
|
|
18
|
+
// merged, holds against its own expected attack (a truce cap respected): every army lives on.
|
|
19
|
+
// The best option is taken when it beats standing (a merge first on a tie, then the larger capture). The
|
|
20
|
+
// unmovable rest (a covered exit's cover) stands either way. Not done: an attack that does not capture (its
|
|
21
|
+
// survivors come home to the doomed stack), and whether the territory a capture lands on is itself doomed.
|
|
22
|
+
import { classifyExchange } from '../exchange.js';
|
|
23
|
+
import { neighborsOf } from '../graph.js';
|
|
24
|
+
import { straightKills } from '../combat.js';
|
|
25
|
+
import { keptByCompletion } from './conceded-out.js';
|
|
26
|
+
import { capRoom } from './relations.js';
|
|
27
|
+
import { byNum } from './util.js';
|
|
28
|
+
const MERGES = 'merges a doomed conceded stack into one that holds (#431)';
|
|
29
|
+
const ATTACKS = 'a doomed conceded stack attacks out rather than stand (#431)';
|
|
30
|
+
/** A #431 order: like a withdrawal, it runs only while its border is mine (Order Priority protects it). */
|
|
31
|
+
export const isDoomedOut = (m) => m.reason === MERGES || m.reason === ATTACKS;
|
|
32
|
+
export function doomedConcededOut(pc, ps, book, hd, tgt, caps, threats) {
|
|
33
|
+
const { g, foe, territories: state, label, mineSet, unitsAt, uncounted, defenceAt, toTake, chanceOf, costAt, myCommanders, noSplit, rates, deployOf } = pc;
|
|
34
|
+
const { avail, captured, spared, rationale, warnings, moves, locked, incoming, survivorsAt, captureOrder, captureUndo } = ps;
|
|
35
|
+
const { conceded, stayAt } = hd;
|
|
36
|
+
const { coverOf, gainWith, orderOf } = book;
|
|
37
|
+
const { targets } = tgt;
|
|
38
|
+
const { expectedAt, reinforcementAt } = threats;
|
|
39
|
+
if (!conceded.size)
|
|
40
|
+
return;
|
|
41
|
+
const def = rates.defensiveKillRate;
|
|
42
|
+
/**
|
|
43
|
+
* Rule 1b's test: no neighbour of mine that is not conceded, which a withdrawal could go to. Rule 1c
|
|
44
|
+
* runs after this pass and keeps a conceded border whose bonus this turn's captures complete: rule 1b
|
|
45
|
+
* then withdraws into it as usual, so such a neighbour counts as one to withdraw to.
|
|
46
|
+
*/
|
|
47
|
+
const nowhere = (x) => !neighborsOf(g, x).some((c) => mineSet.has(c) && (!conceded.has(c) || keptByCompletion(pc, ps, c)));
|
|
48
|
+
/** What stands on my `x` against the expected attack, and whether that attack takes it. */
|
|
49
|
+
const doom = (x, more = 0) => {
|
|
50
|
+
const stack = stayAt(x) + unitsAt(x) + more;
|
|
51
|
+
const take = toTake(stack);
|
|
52
|
+
const expected = expectedAt(x);
|
|
53
|
+
return { stack, take, expected, doomed: take !== null && expected > 0 && take <= expected };
|
|
54
|
+
};
|
|
55
|
+
for (const x of [...conceded].sort(byNum)) {
|
|
56
|
+
if (!mineSet.has(x) || myCommanders.has(x) || keptByCompletion(pc, ps, x) || !nowhere(x))
|
|
57
|
+
continue;
|
|
58
|
+
// No-Split (#124): a covered exit sends nothing, and a territory with an order sends no other.
|
|
59
|
+
if (noSplit && (coverOf(x) > 0 || orderOf(x)))
|
|
60
|
+
continue;
|
|
61
|
+
const n = (avail.get(x) ?? 0) - (noSplit ? 0 : coverOf(x));
|
|
62
|
+
if (n <= 0)
|
|
63
|
+
continue;
|
|
64
|
+
const d = doom(x);
|
|
65
|
+
if (!d.doomed)
|
|
66
|
+
continue;
|
|
67
|
+
const killsBy = (armies) => Math.min(d.expected, straightKills(Math.max(0, armies), def));
|
|
68
|
+
// The rest that cannot move stands and dies in every option, killing its share.
|
|
69
|
+
const rest = killsBy(d.stack - n);
|
|
70
|
+
const stand = killsBy(d.stack);
|
|
71
|
+
const options = [];
|
|
72
|
+
for (const t of neighborsOf(g, x)) {
|
|
73
|
+
if (mineSet.has(t)) {
|
|
74
|
+
// A merge: into another pocket stack (it keeps its armies too) that then holds.
|
|
75
|
+
if (!conceded.has(t) || !nowhere(t) || capRoom(pc, ps, t) < n)
|
|
76
|
+
continue;
|
|
77
|
+
if (doom(t, n).doomed)
|
|
78
|
+
continue;
|
|
79
|
+
options.push({ kind: 'merge', to: t, value: n + rest });
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (captured.has(t) || spared.has(t) || pc.truce.noTarget(t) || tgt.droppedMembers.has(t))
|
|
83
|
+
continue;
|
|
84
|
+
const st = state.get(t);
|
|
85
|
+
if (!st || st.armies === null || uncounted(t) || unitsAt(t) > 0)
|
|
86
|
+
continue;
|
|
87
|
+
const owner = st.owner;
|
|
88
|
+
const enemy = owner.kind === 'player';
|
|
89
|
+
if (owner.kind === 'player' ? !foe(owner.playerId) : owner.kind !== 'neutral')
|
|
90
|
+
continue;
|
|
91
|
+
if (gainWith([t]) < 0)
|
|
92
|
+
continue;
|
|
93
|
+
const deploy = enemy ? reinforcementAt(t) : 0;
|
|
94
|
+
const income = owner.kind === 'player' ? Math.max(deploy, deployOf(owner.playerId)) : 0;
|
|
95
|
+
const ex = classifyExchange({ attackers: n, defenders: defenceAt(t), deploy, income, rates });
|
|
96
|
+
const e = ex.cases[1];
|
|
97
|
+
// The planner's own sizing (luck, win chance) must agree that the whole stack takes it.
|
|
98
|
+
const cost = costAt(t, defenceAt(t) + deploy);
|
|
99
|
+
// A #240 repulsed territory is attacked at its sure size or not at all.
|
|
100
|
+
if (!e.captures || cost === null || Math.max(cost, tgt.repulsed.get(t)?.cost ?? 0) > n)
|
|
101
|
+
continue;
|
|
102
|
+
options.push({ kind: 'attack', to: t, value: n - e.losses + (enemy ? e.kills : 0) + rest, losses: e.losses, kills: e.kills, deploy, enemy });
|
|
103
|
+
}
|
|
104
|
+
options.sort((a, b) => b.value - a.value || Number(b.kind === 'merge') - Number(a.kind === 'merge') || a.to - b.to);
|
|
105
|
+
const best = options[0];
|
|
106
|
+
if (!best || best.value <= stand)
|
|
107
|
+
continue;
|
|
108
|
+
const head = `${label(x)} is doomed (#431): conceded, nowhere to withdraw to, and its ${d.stack} need ${d.take} to take against the expected ~${d.expected} attack.`;
|
|
109
|
+
avail.set(x, avail.get(x) - n);
|
|
110
|
+
locked.add(x);
|
|
111
|
+
ps.doomedOut.add(x);
|
|
112
|
+
// Like a withdrawal (rule 1b) or rule 4e's attack out, the order only runs while the border is mine.
|
|
113
|
+
warnings.push(`The ${best.kind === 'merge' ? 'merge' : 'attack'} out of ${label(x)} only executes if ${label(x)} is still mine when the order runs; play Order Priority so it goes first.`);
|
|
114
|
+
ps.cardAdvice = 'order-priority';
|
|
115
|
+
if (best.kind === 'merge') {
|
|
116
|
+
locked.add(best.to);
|
|
117
|
+
incoming.set(best.to, (incoming.get(best.to) ?? 0) + n);
|
|
118
|
+
// Like a withdrawal it only runs while the border is still mine: it goes first.
|
|
119
|
+
moves.unshift({ from: x, to: best.to, armies: n, kind: 'transfer', reason: MERGES });
|
|
120
|
+
rationale.push(`${head} Merge its ${n} into ${label(best.to)}, which then holds (${doom(best.to).stack} need ${doom(best.to).take} to take against ~${expectedAt(best.to)}), rather than stand and kill ~${stand}.`);
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const t = best.to;
|
|
124
|
+
captured.add(t);
|
|
125
|
+
survivorsAt.set(t, n - straightKills(defenceAt(t), def));
|
|
126
|
+
captureOrder.push(t);
|
|
127
|
+
captureUndo.set(t, () => avail.set(x, avail.get(x) + n));
|
|
128
|
+
if (!targets.has(t))
|
|
129
|
+
targets.set(t, { id: t, cost: costAt(t) ?? n, enemy: best.enemy, sources: [x] });
|
|
130
|
+
if (best.deploy > 0)
|
|
131
|
+
caps.worstCase.set(t, defenceAt(t) + best.deploy);
|
|
132
|
+
moves.push({ from: x, to: t, armies: n, kind: 'attack', reason: ATTACKS, winChance: chanceOf(n, defenceAt(t)) });
|
|
133
|
+
rationale.push(`${head} Attack ${label(t)} with ${noSplit ? 'its whole stack' : `all ${n}`}: it holds ${defenceAt(t)}${best.deploy ? ` (+${best.deploy} if its owner deploys there first)` : ''}, so ~${n - best.losses} survive${best.enemy ? ` and ~${best.kills} of the enemy die` : ''}, against ~${stand} killed and none left by standing.`);
|
|
134
|
+
}
|
|
135
|
+
}
|