@chrischall/warapp-mcp 0.5.0 → 0.7.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.
Files changed (125) hide show
  1. package/.claude-plugin/marketplace.json +3 -3
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/README.md +133 -8
  4. package/dist/analysis/accept-surrender.js +20 -2
  5. package/dist/analysis/calibrate.js +1 -0
  6. package/dist/analysis/card-support.js +177 -17
  7. package/dist/analysis/card-threats.js +126 -14
  8. package/dist/analysis/chat-advice.js +182 -0
  9. package/dist/analysis/chat-blocks.js +106 -0
  10. package/dist/analysis/chat-intent.js +559 -0
  11. package/dist/analysis/chat-read.js +159 -0
  12. package/dist/analysis/decision-replay.js +279 -0
  13. package/dist/analysis/decision-truth.js +146 -0
  14. package/dist/analysis/finished-game.js +62 -3
  15. package/dist/analysis/game-lessons.js +15 -10
  16. package/dist/analysis/hold-diff.js +3 -1
  17. package/dist/analysis/income-trend.js +38 -0
  18. package/dist/analysis/intel.js +75 -0
  19. package/dist/analysis/opens.js +64 -0
  20. package/dist/analysis/opponent-profile.js +121 -10
  21. package/dist/analysis/opponents.js +246 -20
  22. package/dist/analysis/outlook.js +360 -15
  23. package/dist/analysis/picks.js +91 -12
  24. package/dist/analysis/plan/after-leftover.js +4 -1
  25. package/dist/analysis/plan/bonus-attempts.js +105 -0
  26. package/dist/analysis/plan/capture-instead.js +116 -0
  27. package/dist/analysis/plan/captures.js +49 -7
  28. package/dist/analysis/plan/conceded-out.js +13 -2
  29. package/dist/analysis/plan/estimates.js +203 -30
  30. package/dist/analysis/plan/guard-off.js +2 -1
  31. package/dist/analysis/plan/holds.js +110 -9
  32. package/dist/analysis/plan/jabs.js +113 -0
  33. package/dist/analysis/plan/leftover.js +393 -8
  34. package/dist/analysis/plan/orders.js +76 -4
  35. package/dist/analysis/plan/relations.js +135 -2
  36. package/dist/analysis/plan/report.js +69 -3
  37. package/dist/analysis/plan/second-sizing.js +133 -0
  38. package/dist/analysis/plan/shared.js +279 -27
  39. package/dist/analysis/plan/siege.js +303 -0
  40. package/dist/analysis/plan/stand-still.js +9 -5
  41. package/dist/analysis/plan/state.js +8 -0
  42. package/dist/analysis/plan/stockpile.js +104 -0
  43. package/dist/analysis/plan/strike.js +39 -7
  44. package/dist/analysis/plan/targets.js +256 -110
  45. package/dist/analysis/plan/threats.js +148 -10
  46. package/dist/analysis/plan/transfers.js +57 -6
  47. package/dist/analysis/plan/two-move.js +70 -0
  48. package/dist/analysis/plan/vacated.js +124 -2
  49. package/dist/analysis/planner.js +527 -49
  50. package/dist/analysis/profile.js +13 -2
  51. package/dist/analysis/since-last.js +18 -5
  52. package/dist/analysis/stress.js +8 -2
  53. package/dist/board.js +36 -2
  54. package/dist/bonus-attempt-store.js +55 -0
  55. package/dist/build-id.js +106 -0
  56. package/dist/bundle.js +54138 -41059
  57. package/dist/cards.js +7 -5
  58. package/dist/chat-automation.js +22 -0
  59. package/dist/chat.js +35 -1
  60. package/dist/client.js +14 -0
  61. package/dist/commerce.js +111 -0
  62. package/dist/decision-log.js +1093 -0
  63. package/dist/finished-store.js +5 -1
  64. package/dist/index.js +9 -1
  65. package/dist/lessons-store.js +322 -21
  66. package/dist/live.js +43 -0
  67. package/dist/move-order.js +64 -3
  68. package/dist/normalize.js +17 -0
  69. package/dist/open-games.js +36 -16
  70. package/dist/outlook-log.js +42 -1
  71. package/dist/profile-store.js +12 -2
  72. package/dist/protocol/methods.js +62 -5
  73. package/dist/protocol/rpc.js +8 -0
  74. package/dist/relations-store.js +38 -4
  75. package/dist/review-export.js +198 -0
  76. package/dist/siege-store.js +49 -0
  77. package/dist/state-bundle.js +584 -0
  78. package/dist/state-cli.js +71 -0
  79. package/dist/strategy/notes.js +56 -0
  80. package/dist/team-cards.js +25 -0
  81. package/dist/tools/accept-surrender.js +23 -9
  82. package/dist/tools/award.js +20 -2
  83. package/dist/tools/card-advice.js +93 -28
  84. package/dist/tools/card-plays.js +2 -2
  85. package/dist/tools/card-support-advice.js +37 -7
  86. package/dist/tools/chat-advice.js +330 -0
  87. package/dist/tools/chat-inbox.js +19 -4
  88. package/dist/tools/chat.js +106 -17
  89. package/dist/tools/check-plan.js +68 -12
  90. package/dist/tools/commerce-deploy.js +33 -0
  91. package/dist/tools/common.js +31 -0
  92. package/dist/tools/decision-capture.js +459 -0
  93. package/dist/tools/end-of-game.js +151 -21
  94. package/dist/tools/game-kind.js +92 -0
  95. package/dist/tools/how-lost.js +7 -2
  96. package/dist/tools/index.js +10 -0
  97. package/dist/tools/join-game.js +10 -4
  98. package/dist/tools/live-context.js +6 -0
  99. package/dist/tools/live.js +10 -1
  100. package/dist/tools/map-curves.js +73 -0
  101. package/dist/tools/notifications.js +4 -3
  102. package/dist/tools/opponents.js +2 -0
  103. package/dist/tools/past-games.js +74 -8
  104. package/dist/tools/picks.js +61 -10
  105. package/dist/tools/profile-hook.js +62 -3
  106. package/dist/tools/profile-opponents.js +4 -0
  107. package/dist/tools/push.js +2 -2
  108. package/dist/tools/relations.js +139 -0
  109. package/dist/tools/scout.js +16 -2
  110. package/dist/tools/state.js +232 -0
  111. package/dist/tools/strategy.js +3 -3
  112. package/dist/tools/stress-opponents.js +7 -2
  113. package/dist/tools/surrender-pending.js +10 -1
  114. package/dist/tools/surrender.js +18 -3
  115. package/dist/tools/survival.js +11 -3
  116. package/dist/tools/team-brief.js +2 -2
  117. package/dist/tools/team.js +12 -3
  118. package/dist/tools/turn.js +643 -76
  119. package/dist/tools/where.js +3 -1
  120. package/dist/version.js +1 -1
  121. package/manifest.json +32 -3
  122. package/mint.yaml +37 -4
  123. package/package.json +4 -4
  124. package/server.json +20 -2
  125. package/skills/warapp-mcp/SKILL.md +73 -17
@@ -0,0 +1,303 @@
1
+ import { bonusTerritories } from '../bonuses.js';
2
+ import { neighborsOf } from '../graph.js';
3
+ import { captureCost, ownerShown, TRACKED_MIN_TURNS } from './shared.js';
4
+ /** A target's break is scored this many times a 1v1 break, so breaks outrank completions and advances. */
5
+ export const SIEGE_BREAK_WEIGHT = 3;
6
+ /** The assault is released once the target's income falls to this share of its baseline. */
7
+ export const SIEGE_INCOME_DROP = 0.6;
8
+ /** … or my income reaches this multiple of the target's. */
9
+ export const SIEGE_INCOME_LEAD = 1.8;
10
+ /**
11
+ * The enemy bonuses I could break: every territory visible and held by one enemy, or (#72) inferred
12
+ * behind the fog: its track (over at least TRACKED_MIN_TURNS observed turns) lists it in every
13
+ * combination of bonuses that fits the opponent's income gap (all of them listed), or in its single
14
+ * best fit (the first listed, with fewer bonuses than the next), and on this board every member is
15
+ * that opponent's or hidden. Such a break is "probable".
16
+ */
17
+ export function breakableBonuses(g, state, foe, tracks) {
18
+ const breakable = [];
19
+ for (const b of [...g.map.bonuses].sort((x, y) => x.id - y.id)) {
20
+ const ts = bonusTerritories(g, b.id);
21
+ if (b.value <= 0 || !ts.length)
22
+ continue;
23
+ const owners = new Set();
24
+ let visible = true;
25
+ for (const t of ts) {
26
+ const st = state.get(t);
27
+ if (st?.owner.kind !== 'player' || !foe(st.owner.playerId) || st.armies === null)
28
+ visible = false;
29
+ else
30
+ owners.add(st.owner.playerId);
31
+ }
32
+ if (visible && owners.size === 1) {
33
+ breakable.push({ bonusId: b.id, value: b.value, player: [...owners][0] });
34
+ continue;
35
+ }
36
+ // Inferred (#72): one opponent's visible members, the rest hidden, and its track fits it.
37
+ if (owners.size !== 1)
38
+ continue;
39
+ const player = [...owners][0];
40
+ const hiddenOrTheirs = ts.every((t) => {
41
+ const st = ownerShown(state.get(t));
42
+ return st === undefined || (st.owner.kind === 'player' && st.owner.playerId === player);
43
+ });
44
+ if (!hiddenOrTheirs)
45
+ continue;
46
+ const tr = tracks?.find((x) => x.playerId === player);
47
+ const fits = tr && tr.income !== null && tr.turns.length >= TRACKED_MIN_TURNS ? tr.inferred?.candidates : undefined;
48
+ if (!fits?.length)
49
+ continue;
50
+ // "Every" only when the tracker listed every fit (no "and N more"). The best fit must be
51
+ // strictly smaller than the next: equal-sized fits are ordered only by bonus id.
52
+ const more = tr.inferred.more;
53
+ const inEvery = more === 0 && fits.every((c) => c.bonusIds.includes(b.id));
54
+ const single = fits.length === 1 && more === 0;
55
+ const inBest = fits[0].bonusIds.includes(b.id) && (single || (fits[1] !== undefined && fits[0].bonusIds.length < fits[1].bonusIds.length));
56
+ if (!inEvery && !inBest)
57
+ continue;
58
+ breakable.push({
59
+ bonusId: b.id,
60
+ value: b.value,
61
+ player,
62
+ probable: inEvery ? 'in every combination of bonuses that fits its income gap' : 'in the best fit of its income gap',
63
+ });
64
+ }
65
+ return breakable;
66
+ }
67
+ /** A siege whose newest kept turn is more than this many turns old is over: a new one starts. */
68
+ export const SIEGE_MEMORY_GAP = 3;
69
+ /** Turns a siege's memory keeps (the newest). */
70
+ export const MAX_SIEGE_TURNS = 100;
71
+ /**
72
+ * The turns of the siege on `target` before `turn` that `memory` keeps, oldest first: none when the
73
+ * memory is another target's, the turn is unknown, or its newest turn before this one is more than
74
+ * {@link SIEGE_MEMORY_GAP} turns back (that siege lapsed).
75
+ */
76
+ export function priorSiegeTurns(memory, target, turn) {
77
+ if (!memory || memory.target !== target || turn === undefined)
78
+ return [];
79
+ const before = memory.turns.filter((t) => t.turn < turn).sort((a, b) => a.turn - b.turn);
80
+ const newest = before[before.length - 1];
81
+ return newest && turn - newest.turn <= SIEGE_MEMORY_GAP ? before : [];
82
+ }
83
+ /** The memory to keep after planning `turn`: this turn's record after the earlier turns of the same siege. */
84
+ export function rememberSiegeTurn(memory, turn, r) {
85
+ const record = { turn, opponentIncome: r.opponentIncome, myIncome: r.myIncome, breaks: r.brokenThisTurn.map((b) => b.bonusId) };
86
+ return { target: r.target, turns: [...priorSiegeTurns(memory, r.target, turn), record].slice(-MAX_SIEGE_TURNS) };
87
+ }
88
+ export const SIEGE_NOTE = "The baseline is the larger of the most the tracker saw the target deploy over its window of recent boards and its income on the siege's first turn (kept per game by warapp_plan_turn while the siege runs; a gap of more than 3 turns starts a new one). A bonus counts as broken on a turn when that turn's plan took one of its members, as planned, not as the board later showed.";
89
+ const round2 = (v) => Math.round(v * 100) / 100;
90
+ /**
91
+ * The siege of this turn, or a reason it cannot run. `target`: the caller's (`siegeTarget`), else
92
+ * the strongest opponent in contact (by income; ties to the lower id).
93
+ */
94
+ export function siegeOf(o) {
95
+ const { g, state, mineAtStart, foe } = o;
96
+ const guard = o.settings.oneArmyStandsGuard === false ? 0 : 1;
97
+ const ownerOf = (t) => {
98
+ const st = state.get(t);
99
+ return st?.owner.kind === 'player' ? st.owner.playerId : undefined;
100
+ };
101
+ const inContact = new Set();
102
+ for (const x of mineAtStart)
103
+ for (const n of neighborsOf(g, x)) {
104
+ const p = ownerOf(n);
105
+ if (p !== undefined && foe(p))
106
+ inContact.add(p);
107
+ }
108
+ let target = o.target;
109
+ if (target !== undefined) {
110
+ if (!foe(target))
111
+ return { warning: `siege_target ${target} is not an opponent of mine still on the board (me, a teammate or a player at peace): no siege this turn, the plan is balanced.` };
112
+ if (![...state.values()].some((st) => st.owner.kind === 'player' && st.owner.playerId === target))
113
+ return { warning: `siege_target ${target} holds no territory in view: no siege this turn, the plan is balanced.` };
114
+ }
115
+ else {
116
+ target = [...inContact].sort((a, b) => o.incomeOfPlayer(b) - o.incomeOfPlayer(a) || a - b)[0];
117
+ if (target === undefined)
118
+ return { warning: 'goal siege: no opponent borders my territories, so there is no one to besiege: the plan is balanced.' };
119
+ }
120
+ const p = target;
121
+ const opponentIncome = Math.max(0, o.incomeOfPlayer(p));
122
+ const tr = o.tracks?.find((t) => t.playerId === p);
123
+ const observed = (tr?.turns ?? []).filter((t) => !tr?.incomeTurns.length || tr.incomeTurns.includes(t.from)).map((t) => t.observed);
124
+ const earlier = priorSiegeTurns(o.memory, p, o.turn);
125
+ const tracked = Math.max(0, ...observed);
126
+ const start = earlier[0]?.opponentIncome ?? 0;
127
+ const baseline = Math.max(opponentIncome, tracked, start);
128
+ const baselineFrom = baseline === opponentIncome ? 'now' : start > tracked ? 'siege-start' : 'tracked';
129
+ const fallenTo = baseline > 0 ? round2(opponentIncome / baseline) : null;
130
+ const ratio = opponentIncome > 0 ? round2(o.myIncome / opponentIncome) : null;
131
+ const breakable = breakableBonuses(g, state, foe, o.tracks).filter((b) => b.player === p);
132
+ const breakMembers = new Set(breakable.flatMap((b) => bonusTerritories(g, b.bonusId)));
133
+ const armiesAt = (t) => state.get(t)?.armies ?? null;
134
+ /** The cheapest capture of a visible member next to mine, at its defenders as they stand (`null`: none in reach). */
135
+ const cheapest = (bonusId) => {
136
+ const costs = bonusTerritories(g, bonusId).flatMap((t) => {
137
+ const a = armiesAt(t);
138
+ if (a === null || ownerOf(t) !== p || !neighborsOf(g, t).some((n) => mineAtStart.has(n)))
139
+ return [];
140
+ const c = captureCost(a, o.settings);
141
+ return c === null ? [] : [c];
142
+ });
143
+ return costs.length ? Math.min(...costs) : null;
144
+ };
145
+ const breaks = breakable
146
+ .map((b) => ({ bonusId: b.bonusId, value: b.value, cost: cheapest(b.bonusId), ...(b.probable ? { probable: true } : {}) }))
147
+ .sort((a, b) => (a.cost ?? Infinity) / a.value - (b.cost ?? Infinity) / b.value || a.bonusId - b.bonusId);
148
+ // The core strike: one stack of mine takes a member of a bonus the target holds in full (every
149
+ // member in view, so the break is sure) against its defenders plus the target's whole deploy.
150
+ let core;
151
+ for (const b of breakable.filter((x) => !x.probable).sort((x, y) => y.value - x.value || x.bonusId - y.bonusId)) {
152
+ for (const t of [...bonusTerritories(g, b.bonusId)].sort((x, y) => x - y)) {
153
+ const a = armiesAt(t);
154
+ if (a === null)
155
+ continue;
156
+ const need = captureCost(a + o.deployOf(p), o.settings);
157
+ if (need === null)
158
+ continue;
159
+ for (const x of neighborsOf(g, t).filter((n) => mineAtStart.has(n)).sort((m, n) => m - n)) {
160
+ const send = Math.max(0, (armiesAt(x) ?? 0) - guard);
161
+ if (send >= need && (!core || send - need > core.send - core.need))
162
+ core = { from: x, to: t, bonusId: b.bonusId, send, need };
163
+ }
164
+ }
165
+ if (core)
166
+ break;
167
+ }
168
+ const fell = (observed.length > 0 || earlier.length > 0) && baseline > 0 && opponentIncome <= SIEGE_INCOME_DROP * baseline;
169
+ const lead = opponentIncome === 0 ? o.myIncome > 0 : o.myIncome >= SIEGE_INCOME_LEAD * opponentIncome;
170
+ const trigger = fell ? 'income-fell' : lead ? 'income-lead' : core ? 'core-strike' : null;
171
+ return {
172
+ siege: {
173
+ target: p,
174
+ opponentIncome,
175
+ baseline,
176
+ baselineTurns: observed.length,
177
+ baselineFrom,
178
+ myIncome: o.myIncome,
179
+ ratio,
180
+ fallenTo,
181
+ breaks,
182
+ ready: trigger !== null,
183
+ trigger,
184
+ ...(core ? { core } : {}),
185
+ breakMembers,
186
+ rivals: [...inContact].filter((x) => x !== p).sort((a, b) => a - b).map((x) => ({ player: x, income: Math.max(0, o.incomeOfPlayer(x)) })),
187
+ ...(o.turn !== undefined ? { turn: o.turn } : {}),
188
+ earlier,
189
+ },
190
+ };
191
+ }
192
+ // ---- The profile's siege (#382 item 2) ----
193
+ /** A sure break fits a siege when it costs at most this many turns of the income it takes away. */
194
+ export const SIEGE_FIT_TURNS = 2;
195
+ /** … one turn only when the target retook that bonus on this many earlier siege turns or more. */
196
+ export const SIEGE_RETAKEN_TURNS = 2;
197
+ /** In a free-for-all the target must earn this multiple of every other opponent in contact with me. */
198
+ export const SIEGE_FFA_DOMINANCE = 1.5;
199
+ /**
200
+ * Whether the profile picks the siege (no explicit goal; #382). It fits when, against the siege's target:
201
+ * - the format is a 1v1 or a free-for-all (a team game focuses its fire by `teams-focus`), in contact
202
+ * (phase `contact` or `late`);
203
+ * - I am not ahead (ahead, the assault and the 1v1 pressure rules play; an unknown standing is no fit);
204
+ * - in a free-for-all the target is the leader on strength (not me) and earns at least
205
+ * {@link SIEGE_FFA_DOMINANCE} × every other opponent in contact with me (one war, not two fronts);
206
+ * - a sure break (every member in view, a member next to mine) costs at most {@link SIEGE_FIT_TURNS} turns of
207
+ * its bonus — one when the target retook it on {@link SIEGE_RETAKEN_TURNS} earlier siege turns — and at most
208
+ * my income, so one turn's deploys can pay for a break every turn (a siege that cannot afford its breaks only
209
+ * holds the assault back: corpus case 921, where it idled a 23-army stack beside a sure capture).
210
+ * Once the assault trigger fires the siege hands back (`handBack`): the profile's own goal plays the assault.
211
+ */
212
+ export function siegeFit(s, o) {
213
+ if (o.format === 'teams' || (o.phase !== 'contact' && o.phase !== 'late'))
214
+ return { fits: false };
215
+ if (o.standing !== 'even' && o.standing !== 'behind')
216
+ return { fits: false };
217
+ if (o.format === 'ffa' && (o.leader !== s.target || s.rivals.some((r) => r.income * SIEGE_FFA_DOMINANCE > s.opponentIncome)))
218
+ return { fits: false };
219
+ const retakes = (b) => s.earlier.filter((t) => t.breaks.includes(b)).length;
220
+ const fits = s.breaks.flatMap((b) => {
221
+ if (b.probable || b.cost === null)
222
+ return [];
223
+ const retaken = retakes(b.bonusId) >= SIEGE_RETAKEN_TURNS;
224
+ const turns = retaken ? 1 : SIEGE_FIT_TURNS;
225
+ return b.cost <= turns * b.value && b.cost <= s.myIncome ? [{ bonusId: b.bonusId, value: b.value, cost: b.cost, turns, retaken }] : [];
226
+ });
227
+ if (!fits.length)
228
+ return { fits: false };
229
+ if (s.ready)
230
+ return { fits: false, handBack: s.trigger };
231
+ return { fits: true, fit: fits[0] };
232
+ }
233
+ /** The line that says why the profile chose the siege. */
234
+ export function siegeFitLine(s, bonusName) {
235
+ const f = s.auto;
236
+ if (!f)
237
+ return undefined;
238
+ return `The profile chose the siege on player ${s.target} (#382): in contact, not ahead, ${s.rivals.length ? `every other opponent beside me earning at most 1/${SIEGE_FFA_DOMINANCE} of its income, ` : ''}and ${bonusName(f.bonusId)} (+${f.value}) for ${f.cost} breaks within ${f.turns} turn${f.turns === 1 ? '' : 's'} of its income${f.retaken ? ` (counted one turn: retaken after ${SIEGE_RETAKEN_TURNS} earlier breaks)` : ''}. Breaking it costs less than it takes away, so attrition beats an assault until the trigger fires; an explicit goal overrides it.`;
239
+ }
240
+ /** The rationale lines a siege adds (first in the plan's rationale). */
241
+ export function siegeLines(s, label, bonusName) {
242
+ const ratio = s.ratio === null ? 'it earns nothing' : `my ${s.myIncome} is ${s.ratio}× its income`;
243
+ const fell = s.baselineFrom === 'siege-start'
244
+ ? `its income ~${s.opponentIncome} against a baseline of ~${s.baseline} (${s.fallenTo === null ? '—' : `${Math.round(s.fallenTo * 100)}%`}, its income on turn ${s.earlier[0].turn}, when the siege began)`
245
+ : s.baselineTurns > 0
246
+ ? `its income ~${s.opponentIncome} against a baseline of ~${s.baseline} (${s.fallenTo === null ? '—' : `${Math.round(s.fallenTo * 100)}%`}, the most seen over ${s.baselineTurns} tracked turn${s.baselineTurns === 1 ? '' : 's'})`
247
+ : `its income ~${s.opponentIncome} (no tracked turns yet, so no baseline to fall from)`;
248
+ const so = s.earlier.length ? `; siege turn ${s.earlier.length + 1} (since turn ${s.earlier[0].turn}): its income ${[...s.earlier.map((t) => t.opponentIncome), s.opponentIncome].join(' → ')}` : '';
249
+ const head = `Siege on player ${s.target} (#382): ${fell}; ${ratio}${so}.`;
250
+ const breaks = s.breaks.length
251
+ ? `Its bonuses to break, cheapest per income lost first: ${s.breaks
252
+ .map((b) => `${bonusName(b.bonusId)} (+${b.value}${b.cost === null ? ', no member in reach' : `, ${b.cost} to take a member as it stands`}${b.probable ? ', probable' : ''})`)
253
+ .join('; ')}. Each break is sized for its defenders plus the deploy player ${s.target} may put there first where the armies allow (#120).`
254
+ : `No bonus of player ${s.target} is breakable in view this turn.`;
255
+ const state = !s.ready
256
+ ? `Assault held: player ${s.target}'s income has not fallen to ${Math.round(SIEGE_INCOME_DROP * 100)}% of its baseline, mine is not ${SIEGE_INCOME_LEAD}× its, and no stack of mine can take a member of its held bonuses against its whole deploy; only its bonus members are attacked, and the spare deploys build one strike stack facing it.`
257
+ : s.trigger === 'income-fell'
258
+ ? `Assault released: player ${s.target}'s income fell to ${Math.round((s.fallenTo ?? 0) * 100)}% of its baseline (at most ${Math.round(SIEGE_INCOME_DROP * 100)}%); the strike stack strikes.`
259
+ : s.trigger === 'income-lead'
260
+ ? `Assault released: my income is ${s.ratio ?? '∞'}× player ${s.target}'s (at least ${SIEGE_INCOME_LEAD}×); the strike stack strikes.`
261
+ : `Assault released: ${label(s.core.from)} can send ${s.core.send} to take ${label(s.core.to)} of ${bonusName(s.core.bonusId)} (${s.core.need} take it even after player ${s.target}'s whole deploy there); the strike stack strikes.`;
262
+ return [head, breaks, state];
263
+ }
264
+ /** The report of a siege once the plan's captures are known. */
265
+ export function siegeReport(s, g, captured, bonusName) {
266
+ const brokenThisTurn = s.breaks
267
+ .filter((b) => bonusTerritories(g, b.bonusId).some((t) => captured.has(t)))
268
+ .map((b) => ({ bonusId: b.bonusId, value: b.value }));
269
+ let memory;
270
+ if (s.turn !== undefined) {
271
+ const turns = [...s.earlier.map((t) => ({ turn: t.turn, breaks: t.breaks, income: t.opponentIncome })), { turn: s.turn, breaks: brokenThisTurn.map((b) => b.bonusId), income: s.opponentIncome }];
272
+ const broken = new Map();
273
+ for (const t of turns)
274
+ for (const b of t.breaks)
275
+ broken.set(b, [...(broken.get(b) ?? []), t.turn]);
276
+ const list = [...broken].sort(([a], [b]) => a - b).map(([bonusId, ts]) => ({ bonusId, turns: ts }));
277
+ memory = {
278
+ startedTurn: turns[0].turn,
279
+ turns: turns.length,
280
+ startIncome: turns[0].income,
281
+ opponentIncomeTrend: turns.map((t) => ({ turn: t.turn, income: t.income })),
282
+ broken: list,
283
+ rebroken: list.filter((b) => b.turns.length > 1).map((b) => b.bonusId),
284
+ };
285
+ }
286
+ return {
287
+ target: s.target,
288
+ opponentIncome: s.opponentIncome,
289
+ baseline: s.baseline,
290
+ baselineTurns: s.baselineTurns,
291
+ baselineFrom: s.baselineFrom,
292
+ myIncome: s.myIncome,
293
+ ratio: s.ratio,
294
+ fallenTo: s.fallenTo,
295
+ ...(memory ? { memory } : {}),
296
+ ...(s.auto ? { auto: siegeFitLine(s, bonusName ?? ((b) => `bonus ${b}`)) } : {}),
297
+ brokenThisTurn,
298
+ breaks: s.breaks,
299
+ ready: s.ready,
300
+ trigger: s.trigger,
301
+ note: SIEGE_NOTE,
302
+ };
303
+ }
@@ -330,7 +330,7 @@ export function neverStandStill(pc, ps, book, threats, hd, tgt, caps) {
330
330
  const { growthOf, facing } = threats;
331
331
  const { conceded } = hd;
332
332
  const { targets, bonusTarget, targetScoreCost, toFinish, plans, finishCost, sizeOf } = tgt;
333
- const { stopped, outgrows } = caps;
333
+ const { stopped, outgrows, gateStop } = caps;
334
334
  /**
335
335
  * #125: the step rule 4e ranked first and could not afford this turn (its size, nothing else):
336
336
  * the leftover saves toward it (rule 5) and idle stacks stage toward it (rule 6).
@@ -376,6 +376,9 @@ export function neverStandStill(pc, ps, book, threats, hd, tgt, caps) {
376
376
  // B2 (T1): never a truce's covered territory.
377
377
  if (pc.truce.noTarget(t))
378
378
  continue;
379
+ // #298: nor a missing member of a bonus dropped after repeated failed attempts.
380
+ if (tgt.droppedMembers.has(t))
381
+ continue;
379
382
  // An enemy commander is rule 1s's, sized for its owner's full deploy: never a fallback.
380
383
  if (unitsAt(t) > 0)
381
384
  continue;
@@ -428,7 +431,7 @@ export function neverStandStill(pc, ps, book, threats, hd, tgt, caps) {
428
431
  ]
429
432
  : []),
430
433
  ].join('; ');
431
- const reasonOf = ({ id, f }) => `cheapest expansion toward bonus ${bonusName(id)} (+${f.value}, ${toFinish(f.cost, f.fogged, f.fogEach)}, ${f.cost === fewest ? 'the fewest of any bonus in reach' : 'the fewest of any bonus with a capture affordable this turn'})`;
434
+ const reasonOf = ({ id, f }) => `cheapest expansion toward bonus ${bonusName(id)} (+${f.value}, ${toFinish(f.cost, f)}, ${f.cost === fewest ? 'the fewest of any bonus in reach' : 'the fewest of any bonus with a capture affordable this turn'})`;
432
435
  // ---- #107, in contact: enemy captures ----
433
436
  /** Rule 0p.2's setting: an FFA with two or more opponents left. Applied here with or without adapt. */
434
437
  const ffaFronts = profile.format === 'ffa' && profile.opponents >= 2 && !leadLever;
@@ -500,7 +503,7 @@ export function neverStandStill(pc, ps, book, threats, hd, tgt, caps) {
500
503
  if (e.bonus.breaks)
501
504
  return `cheapest capture: breaks bonus ${bonusName(e.bonus.id)} (+${v}) of player ${e.player}`;
502
505
  const f = finishCost.get(e.bonus.id);
503
- return `cheapest capture: advances bonus ${bonusName(e.bonus.id)} (+${v}, ${toFinish(f.cost, f.fogged, f.fogEach)}) from player ${e.player}`;
506
+ return `cheapest capture: advances bonus ${bonusName(e.bonus.id)} (+${v}, ${toFinish(f.cost, f)}) from player ${e.player}`;
504
507
  };
505
508
  // #240 review: what fits is judged on a repulsed territory's sure size.
506
509
  const shortOf = (x) => Math.max(0, sizeOf(x.t, x.cost) - x.reach);
@@ -612,8 +615,9 @@ export function neverStandStill(pc, ps, book, threats, hd, tgt, caps) {
612
615
  failed.set(st.t, { kind: 'hold', ...short });
613
616
  continue;
614
617
  }
615
- // #114: in contact rule 4d is a cost (ranked above), not a veto.
616
- const stop = contact ? null : outgrows(st.t);
618
+ // #114: in contact rule 4d is a cost (ranked above), not a veto — except the 1v1 gate budget (#297):
619
+ // a gate the next income cannot hold is lost next turn, armies and all.
620
+ const stop = contact ? gateStop(st.t) : outgrows(st.t);
617
621
  if (stop === null) {
618
622
  taken = pick;
619
623
  break;
@@ -1,3 +1,11 @@
1
+ // What one planning pass (`planPass` in planner.ts) works from and what it builds (#133).
2
+ //
3
+ // `PlanCtx` is read-only: the inputs and everything derived from them before the first order is
4
+ // planned (the estimates phase, plan/estimates.ts) — the graph, the board, the settings, the kill
5
+ // rates and combat model, the opponents' deploy estimates, the game profile and the goal it picks.
6
+ // `PlanState` is what the phases share and change: the warnings and rationale, and the orders the
7
+ // plan has made so far. A phase moved out of planner.ts takes `(ctx, state)` explicitly instead of
8
+ // closing over planPass's locals; planPass still names each field as the local it was.
1
9
  /** PLAN-TEAMS C3: a territory a teammate visibly holds (never one in a no-team game). */
2
10
  export function teammateAt(pc, id) {
3
11
  if (!pc.allies.size)
@@ -0,0 +1,104 @@
1
+ import { bonusTerritories } from '../bonuses.js';
2
+ import { neighborsOf } from '../graph.js';
3
+ /** #421: armies of mine in view, in turns of my income, that make a stockpile on a flat income. */
4
+ export const STOCKPILE_TURNS = 4;
5
+ /** #421: the visible-army lead over one opponent beside me that turns a stockpile into a strike stack facing it. */
6
+ export const STOCKPILE_LEAD = 2;
7
+ /** #421 (owner decision, 2026-11-20): the share of a flat income held out of captures for the strike stack. */
8
+ export const STRIKE_SHARE = 0.25;
9
+ /** My armies in view and each other player's (special units counted), visible territories only. */
10
+ export function armiesInView(a) {
11
+ const by = new Map();
12
+ let my = 0;
13
+ for (const [id, st] of a.territories) {
14
+ if (st.owner.kind !== 'player' || st.armies === null)
15
+ continue;
16
+ if (a.mineSet.has(id))
17
+ my += st.armies + a.unitsAt(id);
18
+ else
19
+ by.set(st.owner.playerId, (by.get(st.owner.playerId) ?? 0) + st.armies + a.unitsAt(id));
20
+ }
21
+ return { my, by };
22
+ }
23
+ /**
24
+ * #421: the pile — adapting (not `defend`, not No-Split), my income flat and my armies in view at least
25
+ * {@link STOCKPILE_TURNS} turns of it. `undefined` otherwise.
26
+ */
27
+ export function pileOf(a) {
28
+ if (!a.auto || a.goal === 'defend' || a.noSplit || !a.incomeTrend?.flat || a.income <= 0)
29
+ return undefined;
30
+ const { my, by } = armiesInView(a);
31
+ if (my < STOCKPILE_TURNS * a.income)
32
+ return undefined;
33
+ return { my, by, turns: Math.round((my / a.income) * 10) / 10 };
34
+ }
35
+ /**
36
+ * #421 (#450): outside a 1v1 and a siege, the pile builds the strike stack facing the opponent in
37
+ * `facing` whose armies in view mine outnumber {@link STOCKPILE_LEAD}× or more (the fewest first, then
38
+ * id). Visible armies only, never an income estimate.
39
+ */
40
+ export function stockpileOf(a) {
41
+ if (a.siege || a.format === '1v1')
42
+ return undefined;
43
+ const pile = pileOf(a);
44
+ if (!pile)
45
+ return undefined;
46
+ const { my, by, turns } = pile;
47
+ const p = [...a.facing]
48
+ .filter((q) => a.foe(q) && my >= STOCKPILE_LEAD * (by.get(q) ?? 0))
49
+ .sort((x, y) => (by.get(x) ?? 0) - (by.get(y) ?? 0) || x - y)[0];
50
+ if (p === undefined)
51
+ return undefined;
52
+ const ratio = by.get(p) ? `${(Math.round((my / by.get(p)) * 10) / 10).toFixed(1)}×` : 'unbounded';
53
+ return {
54
+ p,
55
+ why: `the opponent whose armies in view mine outnumber ${ratio} (#421)`,
56
+ line: `${my} armies of mine stand on the board, ${turns} turns of my flat ${a.income} income, ${ratio} player ${p}'s in view`,
57
+ };
58
+ }
59
+ /**
60
+ * #421 (owner decision, 2026-11-20): in contact on a flat income — where rule 0p.3c builds a strike stack
61
+ * (a 1v1 adapting, a siege, or the stockpile above) — {@link STRIKE_SHARE} of my income, rounded down, is
62
+ * held out of captures for the stack. Holds still take their exact need first; the leftover deploy then
63
+ * gives the stack what the captures left, ahead of comfort margins. 0 otherwise.
64
+ */
65
+ export function strikeShareOf(a) {
66
+ if (!a.incomeTrend?.flat || a.income <= 0)
67
+ return 0;
68
+ const stack = (a.siege !== undefined && a.contact.has(a.siege)) ||
69
+ (a.auto && a.format === '1v1' && a.goal !== 'defend' && a.contact.size > 0) ||
70
+ stockpileOf({ ...a, siege: a.siege !== undefined, facing: a.contact }) !== undefined;
71
+ return stack ? Math.floor(STRIKE_SHARE * a.income) : 0;
72
+ }
73
+ /**
74
+ * #421 (owner decision, 2026-11-20): the contested bonuses an all-in completion could take on the pile — a
75
+ * positive bonus I hold a member of, whose every missing member is in view (armies shown) beside a territory of
76
+ * mine, at least one of them an opponent's, none a teammate's. By id.
77
+ */
78
+ export function contestedBonuses(g, territories, mineSet, foe) {
79
+ return g.map.bonuses
80
+ .filter((b) => {
81
+ const ts = bonusTerritories(g, b.id);
82
+ if (b.value <= 0 || !ts.some((t) => mineSet.has(t)))
83
+ return false;
84
+ const missing = ts.filter((t) => !mineSet.has(t));
85
+ if (!missing.length)
86
+ return false;
87
+ let opponent = false;
88
+ for (const t of missing) {
89
+ const st = territories.get(t);
90
+ if (!st || st.armies === null || st.owner.kind === 'unknown' || st.owner.kind === 'available')
91
+ return false;
92
+ if (!neighborsOf(g, t).some((n) => mineSet.has(n)))
93
+ return false;
94
+ if (st.owner.kind === 'player') {
95
+ if (!foe(st.owner.playerId))
96
+ return false;
97
+ opponent = true;
98
+ }
99
+ }
100
+ return opponent;
101
+ })
102
+ .map((b) => b.id)
103
+ .sort((a, b) => a - b);
104
+ }
@@ -11,6 +11,12 @@ import { guardOffExposure } from '../guard-off.js';
11
11
  import { captureCost, CONCEDE_FACTOR, guaranteedFallsReaching, MIN_ENEMY_DEPLOY, SECOND_FRONT_LEAD, visibleState, } from './shared.js';
12
12
  import { commanderStaysLine, commanderTransit } from './commander.js';
13
13
  import { byNum, pct } from './util.js';
14
+ import { STRIKE_SHARE, stockpileOf } from './stockpile.js';
15
+ export { STOCKPILE_LEAD, STOCKPILE_TURNS } from './stockpile.js';
16
+ /** Who a strike stack faces, for the rationale: the siege target, a stockpile's (#421) or the strongest opponent. */
17
+ export function stackFacing(pc, s) {
18
+ return pc.siege?.target === s.player ? 'the siege target' : (s.facing ?? 'the strongest opponent');
19
+ }
14
20
  /**
15
21
  * The strikes that run before triage, in the order planPass ran them: rule 1e's finishing off is
16
22
  * defined, the commander strikes (rule 1s) are made, then finishing off and pressure (rule 1e),
@@ -893,7 +899,7 @@ export function guardOffBreaks(pc, ps, book, hd, tgt) {
893
899
  }
894
900
  }
895
901
  }
896
- /** Rule 0p.3c (#72, #120, #201): the 1v1 strike stack's border, chosen before the leftover deploy. */
902
+ /** Rule 0p.3c (#72, #120, #201, #421): the strike stack's border (a 1v1, a siege or a stockpile), chosen before the leftover deploy. */
897
903
  export function chooseStrikeStack(pc, ps, book, threats, open, stays) {
898
904
  const { g, income, profile, auto, goal, estimates, deployOf, incomeOfPlayer, paybackTurns, territories: state, label, mine, armiesOf, unitsAt, defenceAt, completedBy, inFocus, guard, rates, costAt, noSplit, } = pc;
899
905
  const { rationale, captured, combined, staged } = ps;
@@ -902,7 +908,7 @@ export function chooseStrikeStack(pc, ps, book, threats, open, stays) {
902
908
  // Concentrate (#72, adapting in a 1v1 facing the opponent): the spare deploys go to one strike stack
903
909
  // on the border facing the strongest opponent (the highest income estimate, tracked when its
904
910
  // track is trusted), the stack that keeps the most there, rather than to comfort margins.
905
- // A 1v1 rule: in an FFA or a team game the comfort margins keep the leftover (several fronts).
911
+ // A 1v1 rule: in an FFA or a team game the comfort margins keep the leftover (several fronts), unless a stockpile (#421, below).
906
912
  // Chosen even when holds and captures leave no leftover (#102): the stack is still spent.
907
913
  let stack;
908
914
  /**
@@ -921,10 +927,26 @@ export function chooseStrikeStack(pc, ps, book, threats, open, stays) {
921
927
  };
922
928
  /** The stack on `x` a strike could send: what stays, plus a small expansion's armies (No-Split). */
923
929
  const stackAt = (x) => stays(x) + (smallExpansion(x)?.armies ?? 0);
924
- if (auto && profile.format === '1v1' && goal !== 'defend' && facing.size) {
925
- const player = [...facing]
926
- .map((p) => ({ p, e: estimates.get(p) }))
927
- .sort((a, b) => (b.e?.estimate ?? 0) - (a.e?.estimate ?? 0) || a.p - b.p)[0];
930
+ // #382: a siege builds the stack facing its target in any format (its attacks held until the trigger:
931
+ // the target's other territories are no targets then, so the stack only grows or breaks).
932
+ const siege = pc.siege && facing.has(pc.siege.target) ? pc.siege : undefined;
933
+ // #421: outside a 1v1, a stockpile on a flat income — my armies in view at least STOCKPILE_TURNS
934
+ // turns of income while it has not grown — builds the stack facing the opponent beside me whose
935
+ // armies in view mine outnumber STOCKPILE_LEAD× or more (the fewest first, then id). Visible armies
936
+ // only, never an income estimate: the pile is spent where it dominates. Not under No-Split (an order
937
+ // carries the whole stack), and never on my commander's territory.
938
+ const stockpile = stockpileOf({
939
+ territories: state, mineSet: pc.mineSet, unitsAt, foe: pc.foe, income, ...(pc.incomeTrend ? { incomeTrend: pc.incomeTrend } : {}),
940
+ auto, goal, noSplit, format: profile.format, siege: siege !== undefined, facing,
941
+ });
942
+ if ((auto && profile.format === '1v1' && goal !== 'defend' && facing.size) || siege || stockpile) {
943
+ const player = siege
944
+ ? { p: siege.target, e: estimates.get(siege.target) }
945
+ : stockpile
946
+ ? { p: stockpile.p, e: estimates.get(stockpile.p) }
947
+ : [...facing]
948
+ .map((p) => ({ p, e: estimates.get(p) }))
949
+ .sort((a, b) => (b.e?.estimate ?? 0) - (a.e?.estimate ?? 0) || a.p - b.p)[0];
928
950
  /** The opponent's territories next to `x` a strike could aim at. */
929
951
  const aims = (x) => neighborsOf(g, x).filter((n) => {
930
952
  const st = state.get(n);
@@ -934,6 +956,8 @@ export function chooseStrikeStack(pc, ps, book, threats, open, stays) {
934
956
  .filter((x) => open(x) &&
935
957
  // No-Split: a territory whose one order is not a small expansion has nothing left to strike with.
936
958
  (!orderOf(x) || smallExpansion(x) !== undefined) &&
959
+ // #421: a stockpile's stack never stands on my commander's territory.
960
+ !(stockpile && pc.myCommanders.has(x)) &&
937
961
  aims(x).length > 0)
938
962
  .sort((a, b) => stackAt(b) - stackAt(a) || a - b);
939
963
  /**
@@ -1009,14 +1033,22 @@ export function chooseStrikeStack(pc, ps, book, threats, open, stays) {
1009
1033
  ? `, rather than ${label(most)}, which holds more armies but ${able.some((a) => a.x === most) ? `has less to spare after its best strike` : 'can take nothing next to it so sized'}`
1010
1034
  : ''}.`);
1011
1035
  }
1036
+ if (id !== undefined && stockpile)
1037
+ rationale.push(`Stockpile (#421): ${stockpile.line}, so the spare deploys build one strike stack on ${label(id)} facing it, spent on what it can take every turn, rather than comfort margins.`);
1012
1038
  if (id !== undefined)
1013
1039
  stack = {
1014
1040
  id,
1015
1041
  player: player.p,
1016
1042
  income: player.e?.estimate ?? MIN_ENEMY_DEPLOY,
1017
1043
  source: player.e?.source === 'tracked' ? 'tracked' : player.e?.source === 'observed' ? 'observed' : 'estimated',
1044
+ ...(stockpile ? { facing: stockpile.why } : {}),
1018
1045
  };
1019
1046
  }
1047
+ // #421 (owner decision, 2026-11-20): the share the estimates phase held out of captures.
1048
+ if (pc.strikeShare > 0)
1049
+ rationale.push(stack
1050
+ ? `Strike share (#421): ${pc.strikeShare} of my flat ${income} income (${STRIKE_SHARE * 100}%) was held out of captures for the strike stack on ${label(stack.id)}, ahead of comfort margins; holds took their exact need first.`
1051
+ : `Strike share (#421): ${pc.strikeShare} of my flat ${income} income (${STRIKE_SHARE * 100}%) was held out of captures for a strike stack, but no border could carry one this turn: it goes to the leftover as usual.`);
1020
1052
  return { stack, smallExpansion };
1021
1053
  }
1022
1054
  /** #102: spend the strike stack on the enemy territories next to it, once the leftover deploy is in. */
@@ -1306,7 +1338,7 @@ export function spendStrikeStack(pc, ps, input, book, threats, tgt, cap, strikeS
1306
1338
  why: `not made from the strike stack ${label(S)} — ${refused.get(c.id) ?? 'not worth it'}${effect ? `: ${effect}` : ''}`,
1307
1339
  });
1308
1340
  }
1309
- const builds = `the spare deploys build one strike stack facing player ${strikeStack.player}, the strongest opponent (~${strikeStack.income} income, ${strikeStack.source})`;
1341
+ const builds = `the spare deploys build one strike stack facing player ${strikeStack.player}, ${stackFacing(pc, strikeStack)} (~${strikeStack.income} income, ${strikeStack.source})`;
1310
1342
  if (!taken.length) {
1311
1343
  const held = candidates.length
1312
1344
  ? candidates.map((c) => `${label(c.id)}: ${refused.get(c.id) ?? 'not worth it'}`).join('; ')