@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
@@ -1,15 +1,18 @@
1
1
  // Where to play the cards warapp_submit_turn can play but the planner never does (PLAN-TEAMS part D2):
2
- // Diplomacy on the opponent pressing me hardest when two or more are, Sanctions on the leader, Airlift
2
+ // Diplomacy on the opponent pressing me hardest (#424: the only one in contact too, outside a 1v1; a card
3
+ // in force in its last turn chained first), Sanctions on the leader, Airlift
3
4
  // onto a border of mine or a teammate's that falls without it, and Gift as team support (a territory
4
5
  // that completes a teammate's bonus); and (G2) Spy, Reconnaissance and Surveillance only where the fog
5
6
  // hides what the plan needs. Advice only: nothing here changes the plan or its orders. Pure and
6
- // deterministic (ties on ascending id); no I/O.
7
+ // deterministic (ties on ascending id); no I/O. #434: with no border to save, the Airlift puts an idle or
8
+ // cut-off stack to work at the strike stack or the thinnest border.
7
9
  //
8
10
  // Rules (docs/STRATEGY.md, the wiki's card pages): a Diplomacy card stops attacks between its two
9
11
  // players from the turn after it is played; Sanctions cut the target's income from its next turn (a
10
12
  // negative percentage raises it); an Airlift lands after the deploys and before every attack, and armies
11
13
  // landing on a teammate's territory become the teammate's; a Gift moves a territory and its armies after
12
- // the airlifts and before the attacks. The card plays' encodings are UNVERIFIED live (V9).
14
+ // the airlifts and before the attacks. The card plays' encodings are UNVERIFIED live (V9) save Airlift's (#296)
15
+ // and Diplomacy's (#369: played live 2026-11-18 and read in force on later boards).
13
16
  import { bonusTerritories } from './bonuses.js';
14
17
  import { COMMANDER_STRENGTH } from './combat.js';
15
18
  import { reader } from './card-threats.js';
@@ -39,20 +42,26 @@ export function pressures(cb) {
39
42
  return [...by.values()].sort((a, b) => b.armies - a.armies || a.player - b.player);
40
43
  }
41
44
  /**
42
- * Diplomacy on the opponent pressing me hardest, when two or more opponents are in contact (with
43
- * one, peace with it is peace with everyone: the card is a truce for nothing). An opponent already at
44
- * peace with me by a Diplomacy card in force is passed over, and so is a truce partner whose truce
45
- * already covers every border it touches — unless the card adds protection: the truce is in notice
46
- * (it ends), or the partner presses a border the truce does not cover. Either way it is said.
47
- * `undefined` with fewer than two opponents in contact.
45
+ * Diplomacy on the opponent pressing me hardest. #424: the only opponent in contact too, when the game
46
+ * has others (`opponents`, still playing, teammates out): peace on that front frees its whole hold — in a
47
+ * 1v1 (or `opponents` unknown) peace with it is peace with everyone, so nothing is said. An opponent
48
+ * already at peace with me by a Diplomacy card in force is passed over, unless that card's last turn is
49
+ * this one (`lastTurn`): then it comes first, and the card chains the peace. A truce partner whose truce
50
+ * already covers every border it touches is passed over too — unless the card adds protection: the truce
51
+ * is in notice (it ends), or the partner presses a border the truce does not cover. Either way it is
52
+ * said. `undefined` with no opponent in contact, or one in a 1v1.
48
53
  */
49
54
  export function diplomacyAdvice(cb, opts = {}) {
50
55
  const all = pressures(cb);
51
- if (all.length < 2)
56
+ if (!all.length || (all.length === 1 && (opts.opponents ?? 1) < 2))
52
57
  return undefined;
58
+ const chains = (player) => opts.atPeace?.has(player) === true && opts.lastTurn?.has(player) === true;
59
+ // #424: a card in its last turn is chained first; otherwise the pressure order stands.
60
+ const ranked = [...all].sort((a, b) => Number(chains(b.player)) - Number(chains(a.player)));
53
61
  const skipped = [];
54
- for (const p of all) {
55
- if (opts.atPeace?.has(p.player)) {
62
+ for (const p of ranked) {
63
+ const chain = chains(p.player);
64
+ if (opts.atPeace?.has(p.player) && !chain) {
56
65
  skipped.push(`player ${p.player}: a Diplomacy card between you is already in force`);
57
66
  continue;
58
67
  }
@@ -72,15 +81,27 @@ export function diplomacyAdvice(cb, opts = {}) {
72
81
  continue;
73
82
  }
74
83
  }
75
- if (p.armies <= 0)
84
+ if (p.armies <= 0) {
85
+ if (chain)
86
+ continue;
76
87
  break;
88
+ }
77
89
  const takes = p.takes.length ? `; as the plan leaves them it can take ${p.takes.join(', ')}` : '';
90
+ const which = all.length === 1
91
+ ? 'the only opponent beside you'
92
+ : p === all[0]
93
+ ? `of the ${all.length} opponents beside you it presses hardest`
94
+ : `one of the ${all.length} opponents beside you`;
78
95
  return {
79
96
  player: p.player,
80
97
  pressure: p,
81
98
  ...(overTruce ? { overTruce } : {}),
82
- why: `Diplomacy with player ${p.player}: of the ${all.length} opponents beside you it presses hardest — your borders ${p.borders.join(', ')} ` +
83
- `keep ${p.armies} armies against it alone${takes}.${overTruce ? ` Played on a truce partner because ${overTruce}.` : ''}`,
99
+ ...(chain ? { chain: true } : {}),
100
+ why: `Diplomacy with player ${p.player}: ${which} — your borders ${p.borders.join(', ')} ` +
101
+ `keep ${p.armies} armies against it alone${takes}.${overTruce ? ` Played on a truce partner because ${overTruce}.` : ''}` +
102
+ (chain
103
+ ? ` Chain it: the Diplomacy card in force with player ${p.player} ends after this turn, and a new one starts the turn after it is played, so the peace runs on unbroken.`
104
+ : ''),
84
105
  skipped,
85
106
  };
86
107
  }
@@ -116,6 +137,8 @@ export function sanctionsAdvice(input) {
116
137
  ...(skipped.length ? { skipped } : {}),
117
138
  };
118
139
  }
140
+ /** #434: an idle or cut-off stack is worth an Airlift only with more than this many armies to spare. */
141
+ export const REDEPLOY_MIN_ARMIES = 3;
119
142
  /**
120
143
  * An Airlift onto a territory that falls this turn without it — mine as the plan leaves it, or (in a
121
144
  * team game) a teammate's with the deploys it has entered; never one the plan moves armies out of — from a territory of mine with armies to
@@ -188,7 +211,108 @@ export function airliftAdvice(cb) {
188
211
  (t.teammate !== undefined ? " The armies become your teammate's, and hold only if your teammate keeps them there: say so in team chat." : ''),
189
212
  };
190
213
  }
191
- return undefined;
214
+ return redeployAirlift(cb, r, spare);
215
+ }
216
+ /**
217
+ * #434: with no border to save, an Airlift that puts idle armies to work. Sources: a stack of mine the plan
218
+ * neither deploys on nor moves out of with only my land beside it (`idle`), or any stack outside my main
219
+ * holdings (the connected group of my territories with the most territories, then armies, then lowest id;
220
+ * `disconnected`) — each with more than {@link REDEPLOY_MIN_ARMIES} to spare over its hold. Target, in my main
221
+ * holdings: the plan's strike stack (the territory its attacks send the most from), else the border with the
222
+ * thinnest margin (the strongest single reach beside it less what the plan leaves there). An idle stack is
223
+ * flown only to a target two or more steps away through my land (a transfer brings it up otherwise). The most
224
+ * armies to spare first, then ascending id. `undefined` when none.
225
+ */
226
+ function redeployAirlift(cb, r, spare) {
227
+ const mine = r.ids.filter((t) => r.isMine(t));
228
+ if (!mine.length)
229
+ return undefined;
230
+ // My territories in groups connected through my land; distances through my land from a source.
231
+ const group = new Map();
232
+ const groups = [];
233
+ for (const t of mine) {
234
+ if (group.has(t))
235
+ continue;
236
+ const members = [];
237
+ const queue = [t];
238
+ group.set(t, groups.length);
239
+ while (queue.length) {
240
+ const x = queue.shift();
241
+ members.push(x);
242
+ for (const n of neighborsOf(cb.graph, x))
243
+ if (r.isMine(n) && !group.has(n)) {
244
+ group.set(n, groups.length);
245
+ queue.push(n);
246
+ }
247
+ }
248
+ groups.push(members.sort((a, b) => a - b));
249
+ }
250
+ const armiesIn = (ts) => ts.reduce((a, t) => a + r.armiesOf(t), 0);
251
+ const main = groups
252
+ .map((ts, i) => ({ i, ts }))
253
+ .sort((a, b) => b.ts.length - a.ts.length || armiesIn(b.ts) - armiesIn(a.ts) || a.ts[0] - b.ts[0])[0].i;
254
+ const inMain = (t) => group.get(t) === main;
255
+ const stepsThroughMine = (from, to) => {
256
+ const seen = new Map([[from, 0]]);
257
+ const queue = [from];
258
+ while (queue.length) {
259
+ const x = queue.shift();
260
+ if (x === to)
261
+ return seen.get(x);
262
+ for (const n of neighborsOf(cb.graph, x))
263
+ if (r.isMine(n) && !seen.has(n)) {
264
+ seen.set(n, seen.get(x) + 1);
265
+ queue.push(n);
266
+ }
267
+ }
268
+ return Infinity;
269
+ };
270
+ // The strike stack: my territory in the main holdings the plan's attacks send the most from.
271
+ const sent = new Map();
272
+ for (const m of cb.moves)
273
+ if (m.kind === 'attack' && r.isMine(m.from) && inMain(m.from))
274
+ sent.set(m.from, (sent.get(m.from) ?? 0) + m.armies);
275
+ const strike = [...sent].sort((a, b) => b[1] - a[1] || a[0] - b[0])[0]?.[0];
276
+ // The border with the thinnest margin (not one the plan withdraws from).
277
+ const border = mine
278
+ .filter((t) => inMain(t) && r.outOf(t) === 0 && r.state.get(t)?.armies !== null)
279
+ .flatMap((t) => {
280
+ const reach = r.reachOf(t);
281
+ return reach ? [{ t, reach, margin: r.after(t) - reach.reach }] : [];
282
+ })
283
+ .sort((a, b) => a.margin - b.margin || a.t - b.t)[0];
284
+ const target = strike !== undefined ? { id: strike, kind: 'strike' } : border ? { id: border.t, kind: 'border' } : undefined;
285
+ if (!target)
286
+ return undefined;
287
+ const sources = [...spare]
288
+ .flatMap(([s, can]) => {
289
+ if (s === target.id || can <= REDEPLOY_MIN_ARMIES)
290
+ return [];
291
+ if (!inMain(s))
292
+ return [{ s, can, source: 'disconnected' }];
293
+ const idle = r.outOf(s) === 0 && r.deployAt(s) === 0 && neighborsOf(cb.graph, s).every((n) => r.isMine(n));
294
+ return idle && stepsThroughMine(s, target.id) >= 2 ? [{ s, can, source: 'idle' }] : [];
295
+ })
296
+ .sort((a, b) => b.can - a.can || a.s - b.s);
297
+ const best = sources[0];
298
+ if (!best)
299
+ return undefined;
300
+ const commander = r.commanderOf(target.id, (q) => q === cb.me);
301
+ const from = best.source === 'idle'
302
+ ? `they sit idle behind your lines (nothing beside ${best.s} but your land, and the plan uses none of them)`
303
+ : `${best.s} is cut off from your main holdings, so its armies cannot walk to the front`;
304
+ const to = target.kind === 'strike'
305
+ ? `${target.id} is the strike stack (the plan's attacks send the most from it): the armies land before the attacks and stand behind the strike for next turn`
306
+ : `${target.id} is the border with the thinnest margin (player ${border.reach.player}'s ~${border.reach.reach} beside it against ${r.after(target.id)} of yours after the plan)`;
307
+ return {
308
+ from: best.s,
309
+ to: target.id,
310
+ armies: best.can,
311
+ ...(target.kind === 'border' ? { player: border.reach.player, reach: border.reach.reach } : {}),
312
+ commander,
313
+ redeploy: { source: best.source, target: target.kind },
314
+ why: `Airlift ${best.can} from ${best.s} to ${target.id}: ${from}, and ${to}.`,
315
+ };
192
316
  }
193
317
  /**
194
318
  * A Gift as team support: my members of a bonus a teammate is nearer to holding, when giving them
@@ -226,7 +350,11 @@ export function giftAdvice(cb, shared, gifts) {
226
350
  * on the owner shown beside the most hidden stacks next to me; Surveillance on the bonus I am completing
227
351
  * that is nearest done (then the most valuable, then ascending id), else the bonus worth the most with a
228
352
  * hidden border of mine in it; Reconnaissance on the hidden territory next to the most of mine (then the
229
- * most armies of mine beside it, then ascending id), else a hidden member of the bonus I am completing.
353
+ * most armies of mine beside it, then ascending id), else on the hidden territory behind the opponent in
354
+ * contact whose stacks tie down the most of my border armies ({@link pressures}: its estimate decides the
355
+ * most of the holds and concessions; #443) — a likely start (`likelyStarts`, the distribution mode's
356
+ * territories) first, then the one beside the most of its front, then ascending id — else a hidden member
357
+ * of the bonus I am completing.
230
358
  * `hold` says, for a card held with nothing to reveal, why it is kept. Pure; ties on ascending id.
231
359
  */
232
360
  export function intelAdvice(cb, opts) {
@@ -311,6 +439,7 @@ export function intelAdvice(cb, opts) {
311
439
  }
312
440
  if (opts.held.has('Reconnaissance')) {
313
441
  const x = blind[0];
442
+ const behind = x ? undefined : behindFront(cb, r, hidden, opts.likelyStarts);
314
443
  if (x) {
315
444
  advice.push({
316
445
  card: 'Reconnaissance',
@@ -318,6 +447,14 @@ export function intelAdvice(cb, opts) {
318
447
  why: `Reconnaissance on ${x.t}: it borders ${x.next} of your territories (${x.armies} armies) and the fog hides what stands there.`,
319
448
  });
320
449
  }
450
+ else if (behind) {
451
+ advice.push({
452
+ card: 'Reconnaissance',
453
+ target: behind.t,
454
+ why: `Reconnaissance on ${behind.t}: the fog hides it behind player ${behind.player}, whose stacks tie down ${behind.armies} of your border armies, ` +
455
+ `so its strength there is an estimate${behind.likely ? ' (a likely start)' : ''}.`,
456
+ });
457
+ }
321
458
  else if (building[0]) {
322
459
  const t = building[0].fogged[0];
323
460
  advice.push({ card: 'Reconnaissance', target: t, why: `Reconnaissance on ${t}: a member of bonus ${building[0].id}, which you are completing, hidden by the fog.` });
@@ -327,3 +464,26 @@ export function intelAdvice(cb, opts) {
327
464
  }
328
465
  return { advice, hold };
329
466
  }
467
+ /**
468
+ * #443: the hidden territory behind the opponent in contact whose estimate decides the most armies of my
469
+ * holds (the first by {@link pressures} with one): a hidden territory, not mine, beside one of its territories
470
+ * that border mine. A likely start first, then the one beside the most of that front, then ascending id.
471
+ */
472
+ function behindFront(cb, r, hidden, likely) {
473
+ for (const p of pressures(cb)) {
474
+ if (p.armies <= 0)
475
+ continue;
476
+ const front = new Set(p.borders.flatMap((b) => neighborsOf(cb.graph, b).filter((n) => r.ownerOf(n) === p.player)));
477
+ const seen = new Map();
478
+ for (const f of front)
479
+ for (const n of neighborsOf(cb.graph, f))
480
+ if (!r.isMine(n) && hidden(n))
481
+ seen.set(n, (seen.get(n) ?? 0) + 1);
482
+ const best = [...seen]
483
+ .map(([t, n]) => ({ t, n, likely: likely?.has(t) ?? false }))
484
+ .sort((a, b) => Number(b.likely) - Number(a.likely) || b.n - a.n || a.t - b.t)[0];
485
+ if (best)
486
+ return { t: best.t, player: p.player, armies: p.armies, likely: best.likely };
487
+ }
488
+ return undefined;
489
+ }
@@ -1,7 +1,9 @@
1
1
  // Cards the planner does not play, read against its plan (#163): my Bomb (where halving a stack
2
2
  // changes a hold, a capture or a commander kill, else hold it), an opponent's Bomb (borders that
3
3
  // hold only while unbombed), an opponent's Emergency Blockade (attacks that would die against the
4
- // wall) and my own (a conceded border where the wall kills more than I give up). Pure and
4
+ // wall) and my own (a conceded border where the wall pays: what it kills plus the bonuses and
5
+ // commander behind it the stack could reach only through it, against what I give up; #369), and my
6
+ // Blockade on the one gate into a held bonus the plan concedes (#434). Pure and
5
7
  // deterministic (ties on ascending territory id); no I/O.
6
8
  //
7
9
  // Rules (the wiki's Bomb_Card, Turn_phases, Emergency Blockade; docs/STRATEGY.md): a Bomb hits a
@@ -14,6 +16,8 @@
14
16
  import { armiesToTake, COMMANDER_STRENGTH, DEFAULT_DEFENSIVE_KILL_RATE, straightKills } from './combat.js';
15
17
  import { neighborsOf } from './graph.js';
16
18
  import { bonusTerritories } from './bonuses.js';
19
+ import { opensBehind } from './opens.js';
20
+ import { ownerShown } from './plan/shared.js';
17
21
  /** The common reads over the board and the plan (shared with card-support.ts). */
18
22
  export function reader(cb) {
19
23
  const state = new Map(cb.board.territories.map((t) => [t.territoryId, t]));
@@ -104,6 +108,7 @@ const KIND_RANK = { commander: 0, hold: 1, capture: 2 };
104
108
  export function bombAdvice(cb) {
105
109
  const r = reader(cb);
106
110
  const best = [];
111
+ const passed = new Set();
107
112
  for (const t of r.ids) {
108
113
  const p = r.ownerOf(t);
109
114
  if (!r.foe(p) || r.state.get(t)?.armies === null)
@@ -113,6 +118,11 @@ export function bombAdvice(cb) {
113
118
  const borders = neighborsOf(cb.graph, t).filter(r.isMine);
114
119
  if (!borders.length)
115
120
  continue;
121
+ // #452: a player at peace with me is never bombed — the capture or hold it would serve is never made.
122
+ if (p !== undefined && cb.peace?.has(p)) {
123
+ passed.add(p);
124
+ continue;
125
+ }
116
126
  const n = r.armiesOf(t) + cb.enemyDeploy;
117
127
  const left = Math.ceil(n / 2);
118
128
  const cmd = r.commanderOf(t, (q) => q === p) ? COMMANDER_STRENGTH : 0;
@@ -140,12 +150,18 @@ export function bombAdvice(cb) {
140
150
  }
141
151
  best.sort((a, b) => KIND_RANK[a.kind] - KIND_RANK[b.kind] || b.stake - a.stake || a.target - b.target);
142
152
  const top = best[0];
153
+ const peace = [...passed].sort((a, b) => a - b);
154
+ const passedOver = peace.length
155
+ ? ` ${peace.map((p) => `Player ${p}`).join(', ')} ${peace.length === 1 ? 'is' : 'are'} passed over: you are at peace with ${peace.length === 1 ? 'it' : 'them'} (a Diplomacy card in force or a binding truce), so no attack on ${peace.length === 1 ? 'it' : 'them'} is planned for a Bomb to serve.`
156
+ : '';
157
+ const withPassed = peace.length ? { passed: peace } : {};
143
158
  if (!top) {
144
159
  return {
145
- why: 'Hold the Bomb: halving no stack next to me changes a hold, a capture or a commander kill this turn, and while I hold it the opponent cannot safely stack.',
160
+ why: `Hold the Bomb: halving no stack next to me changes a hold, a capture or a commander kill this turn, and while I hold it the opponent cannot safely stack.${passedOver}`,
161
+ ...withPassed,
146
162
  };
147
163
  }
148
- return { target: top.target, kind: top.kind, why: `Bomb ${top.target}: ${top.why}.` };
164
+ return { target: top.target, kind: top.kind, why: `Bomb ${top.target}: ${top.why}.`, ...withPassed };
149
165
  }
150
166
  /**
151
167
  * My borders (after the plan) that hold against the opponent's reach unbombed but fall once a
@@ -196,27 +212,33 @@ export function wallThreats(cb, multiplier) {
196
212
  return out;
197
213
  }
198
214
  /**
199
- * Where my Emergency Blockade pays: a border the plan concedes, where the wall of my armies there
200
- * (this turn's deploy included) times the multiplier kills more of the stack that can reach it
201
- * than the armies I give up — never on my commander's territory (it would be lost with it),
202
- * never on one the plan moves armies out of (they would be lost with it: the wall comes before the
203
- * moves) and never on a member of a bonus I hold whole at the start of the turn (I need it). The best by
204
- * kills over armies given up, then ascending id; `undefined` when none pays.
215
+ * Where my Emergency Blockade pays (#163, #369): a border the plan concedes (it cannot hold it),
216
+ * walled with my armies there (this turn's deploy included) times the multiplier. Priced by the board:
217
+ * it gives up the territory and its armies, against what the wall kills of the stack that can reach
218
+ * it (if it attacks) plus what it protects — the income of the bonuses I hold behind it that the stack
219
+ * could reach next turn only through it (one turn of it), and my commander behind it, which always pays.
220
+ * Never when the opponent can go around: something of mine lies behind the wall but the stack reaches
221
+ * all of it as fast another way, so it need never attack the wall. Never on my commander's territory (it
222
+ * would be lost with it), never on one the plan moves armies out of (they would be lost with it: the
223
+ * wall comes before the moves) and never on a member of a bonus I hold whole at the start of the turn
224
+ * (the wall breaks it). The best shields my commander, then by kills plus income over armies given up,
225
+ * then ascending id; `undefined` when none pays.
205
226
  */
206
227
  export function wallOffer(cb, multiplier, conceded) {
207
228
  const r = reader(cb);
208
229
  const defence = cb.settings?.defensiveKillRate ?? DEFAULT_DEFENSIVE_KILL_RATE;
209
- const held = (id) => cb.graph.map.bonuses.some((bonus) => {
230
+ const heldBonuses = (id) => cb.graph.map.bonuses.filter((bonus) => {
210
231
  if (bonus.value <= 0)
211
232
  return false;
212
233
  const members = bonusTerritories(cb.graph, bonus.id);
213
234
  return members.includes(id) && members.every(r.isMine);
214
235
  });
236
+ const worth = (o) => o.kills + o.protects.income - o.givesUp;
215
237
  let best;
216
238
  for (const c of [...conceded].sort((a, b) => a - b)) {
217
239
  // A border the plan withdraws from is skipped: the wall turns it neutral before the moves, so
218
240
  // the armies the plan moves out of it would be lost with it.
219
- if (!r.isMine(c) || r.commanderOf(c, (q) => q === cb.me) || held(c) || r.outOf(c) > 0)
241
+ if (!r.isMine(c) || r.commanderOf(c, (q) => q === cb.me) || heldBonuses(c).length || r.outOf(c) > 0)
220
242
  continue;
221
243
  const reach = r.reachOf(c);
222
244
  if (!reach)
@@ -224,11 +246,101 @@ export function wallOffer(cb, multiplier, conceded) {
224
246
  const givesUp = r.armiesOf(c) + r.deployAt(c);
225
247
  const wall = wallOf(givesUp, multiplier);
226
248
  const kills = Math.min(reach.reach, straightKills(wall, defence));
227
- if (kills <= givesUp)
249
+ const shield = wallShield(cb, r, c, reach.player);
250
+ if (shield.goesAround)
251
+ continue;
252
+ // A wall the stack can take blocks nothing: it walks through and on.
253
+ const blocks = r.toTake(wall) > reach.reach;
254
+ const protects = blocks ? shield.protects : { territories: [], bonuses: [], income: 0, commander: false };
255
+ const offer = { territory: c, givesUp, wall, player: reach.player, reach: reach.reach, kills, protects };
256
+ if (!protects.commander && worth(offer) <= 0)
257
+ continue;
258
+ if (!best ||
259
+ (protects.commander && !best.protects.commander) ||
260
+ (protects.commander === best.protects.commander && worth(offer) > worth(best)))
261
+ best = offer;
262
+ }
263
+ return best;
264
+ }
265
+ /**
266
+ * Where my Blockade card pays (#434): a border the plan concedes that is the only way into held bonuses of
267
+ * mine (or my commander) behind it — every route from an enemy or the fog runs through it ({@link opensBehind},
268
+ * #291) — when the wall (my armies there after the plan's deploys and moves, the card running at the end of the
269
+ * turn, × the multiplier) is more than the strongest stack beside it can take. Never on a member of a bonus I
270
+ * hold whole (the wall gives the territory away and breaks the bonus), never on my commander's territory. The
271
+ * best shields my commander, then the most income, then gives up the fewest armies, then ascending id;
272
+ * `undefined` when none. The wall forms only if the territory is still mine at the end of the turn: it stops
273
+ * the attack from the next turn on.
274
+ */
275
+ export function blockadeOffer(cb, multiplier, conceded) {
276
+ const r = reader(cb);
277
+ const holdings = new Set(r.ids.filter(r.isMine));
278
+ const inHeldBonus = (id) => cb.graph.map.bonuses.some((bonus) => {
279
+ if (bonus.value <= 0)
280
+ return false;
281
+ const members = bonusTerritories(cb.graph, bonus.id);
282
+ return members.includes(id) && members.every(r.isMine);
283
+ });
284
+ // A source: an enemy's territory, or one whose owner the fog hides (anything may come from it).
285
+ const isSource = (x) => {
286
+ const shown = ownerShown(r.state.get(x));
287
+ if (!shown)
288
+ return true;
289
+ return shown.owner.kind === 'player' && r.foe(shown.owner.playerId);
290
+ };
291
+ let best;
292
+ for (const c of [...new Set(conceded)].sort((a, b) => a - b)) {
293
+ if (!r.isMine(c) || r.commanderOf(c, (q) => q === cb.me) || inHeldBonus(c))
294
+ continue;
295
+ const reach = r.reachOf(c);
296
+ const armies = r.after(c);
297
+ if (!reach || armies <= 0)
298
+ continue;
299
+ const wall = wallOf(armies, multiplier);
300
+ if (r.toTake(wall) <= reach.reach)
228
301
  continue;
229
- const offer = { territory: c, givesUp, wall, player: reach.player, reach: reach.reach, kills };
230
- if (!best || kills - givesUp > best.kills - best.givesUp)
302
+ const opens = opensBehind(cb.graph, holdings, isSource, c, new Set());
303
+ const commander = opens.ids.some((y) => r.commanderOf(y, (q) => q === cb.me));
304
+ if (opens.income <= 0 && !commander)
305
+ continue;
306
+ const offer = { territory: c, armies, wall, player: reach.player, reach: reach.reach, opens, commander };
307
+ if (!best ||
308
+ (commander && !best.commander) ||
309
+ (commander === best.commander &&
310
+ (opens.income > best.opens.income || (opens.income === best.opens.income && armies < best.armies))))
231
311
  best = offer;
232
312
  }
233
313
  return best;
234
314
  }
315
+ /**
316
+ * Behind the wall on `c` (#369): my territories next to `c` that `player`'s stacks beside `c` can reach
317
+ * next turn only through it. One is reachable another way when the player already borders it, or a
318
+ * territory other than `c` borders both it and one of those stacks (two steps either way).
319
+ * `goesAround`: something of mine lies next to `c`, and every piece of it is reachable another way.
320
+ */
321
+ function wallShield(cb, r, c, player) {
322
+ const stacks = neighborsOf(cb.graph, c).filter((n) => r.ownerOf(n) === player);
323
+ const behind = neighborsOf(cb.graph, c)
324
+ .filter((n) => n !== c && r.isMine(n))
325
+ .sort((a, b) => a - b);
326
+ const around = (y) => neighborsOf(cb.graph, y).some((x) => r.ownerOf(x) === player || (x !== c && stacks.some((s) => neighborsOf(cb.graph, s).includes(x))));
327
+ const territories = behind.filter((y) => !around(y));
328
+ const bonuses = new Map();
329
+ for (const bonus of cb.graph.map.bonuses) {
330
+ if (bonus.value <= 0)
331
+ continue;
332
+ const members = bonusTerritories(cb.graph, bonus.id);
333
+ if (members.every(r.isMine) && territories.some((y) => members.includes(y)))
334
+ bonuses.set(bonus.id, bonus.value);
335
+ }
336
+ const list = [...bonuses].sort((a, b) => a[0] - b[0]).map(([bonus, value]) => ({ bonus, value }));
337
+ return {
338
+ protects: {
339
+ territories,
340
+ bonuses: list,
341
+ income: list.reduce((a, b) => a + b.value, 0),
342
+ commander: territories.some((y) => r.commanderOf(y, (q) => q === cb.me)),
343
+ },
344
+ goesAround: behind.length > 0 && territories.length === 0,
345
+ };
346
+ }