@chrischall/warapp-mcp 0.6.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.
Files changed (111) hide show
  1. package/.claude-plugin/marketplace.json +2 -2
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/README.md +100 -7
  4. package/dist/analysis/accept-surrender.js +20 -2
  5. package/dist/analysis/calibrate.js +1 -0
  6. package/dist/analysis/card-support.js +187 -20
  7. package/dist/analysis/card-threats.js +68 -3
  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/coalition.js +55 -0
  13. package/dist/analysis/decision-replay.js +279 -0
  14. package/dist/analysis/decision-truth.js +146 -0
  15. package/dist/analysis/finished-game.js +62 -3
  16. package/dist/analysis/game-lessons.js +12 -6
  17. package/dist/analysis/local-deploy.js +14 -0
  18. package/dist/analysis/opponent-profile.js +46 -3
  19. package/dist/analysis/opponents.js +246 -19
  20. package/dist/analysis/outlook.js +323 -17
  21. package/dist/analysis/picks.js +270 -37
  22. package/dist/analysis/plan/after-leftover.js +28 -3
  23. package/dist/analysis/plan/captures.js +80 -16
  24. package/dist/analysis/plan/completed.js +8 -1
  25. package/dist/analysis/plan/conceded-out.js +16 -11
  26. package/dist/analysis/plan/deploy-estimates.js +513 -0
  27. package/dist/analysis/plan/doomed-out.js +135 -0
  28. package/dist/analysis/plan/estimates.js +73 -7
  29. package/dist/analysis/plan/free-capture.js +34 -0
  30. package/dist/analysis/plan/guard-off.js +2 -1
  31. package/dist/analysis/plan/holds.js +85 -313
  32. package/dist/analysis/plan/horizon.js +42 -0
  33. package/dist/analysis/plan/leftover.js +188 -10
  34. package/dist/analysis/plan/observe.js +896 -0
  35. package/dist/analysis/plan/orders.js +35 -1
  36. package/dist/analysis/plan/relations.js +115 -2
  37. package/dist/analysis/plan/report.js +10 -4
  38. package/dist/analysis/plan/shared.js +7 -1189
  39. package/dist/analysis/plan/stand-still.js +7 -5
  40. package/dist/analysis/plan/state.js +12 -0
  41. package/dist/analysis/plan/stockpile.js +104 -0
  42. package/dist/analysis/plan/strike-stack.js +467 -0
  43. package/dist/analysis/plan/strike.js +8 -441
  44. package/dist/analysis/plan/targets.js +374 -72
  45. package/dist/analysis/plan/threats.js +120 -9
  46. package/dist/analysis/plan/transfers.js +35 -1
  47. package/dist/analysis/plan/two-move.js +70 -0
  48. package/dist/analysis/plan/vacated.js +124 -2
  49. package/dist/analysis/plan/withdraw.js +316 -0
  50. package/dist/analysis/planner.js +356 -53
  51. package/dist/analysis/player.js +4 -0
  52. package/dist/analysis/relations.js +8 -2
  53. package/dist/analysis/since-last.js +18 -5
  54. package/dist/analysis/stress.js +1 -1
  55. package/dist/board.js +36 -2
  56. package/dist/build-id.js +106 -0
  57. package/dist/bundle.js +15863 -10242
  58. package/dist/chat-automation.js +22 -0
  59. package/dist/client.js +14 -0
  60. package/dist/decision-log.js +1093 -0
  61. package/dist/finished-store.js +5 -1
  62. package/dist/lessons-store.js +322 -21
  63. package/dist/my-games.js +2 -0
  64. package/dist/open-games.js +122 -18
  65. package/dist/outlook-log.js +42 -1
  66. package/dist/protocol/methods.js +4 -2
  67. package/dist/review-export.js +198 -0
  68. package/dist/state-bundle.js +62 -3
  69. package/dist/state-cli.js +5 -3
  70. package/dist/strategy/notes.js +56 -0
  71. package/dist/tools/accept-surrender.js +23 -9
  72. package/dist/tools/award.js +5 -1
  73. package/dist/tools/card-advice.js +102 -33
  74. package/dist/tools/card-support-advice.js +53 -8
  75. package/dist/tools/chat-advice.js +330 -0
  76. package/dist/tools/chat-inbox.js +15 -4
  77. package/dist/tools/chat.js +41 -11
  78. package/dist/tools/check-plan.js +16 -3
  79. package/dist/tools/commander-check.js +2 -1
  80. package/dist/tools/decision-capture.js +461 -0
  81. package/dist/tools/end-of-game.js +146 -18
  82. package/dist/tools/game-kind.js +92 -0
  83. package/dist/tools/how-lost.js +7 -2
  84. package/dist/tools/index.js +3 -0
  85. package/dist/tools/join-game.js +51 -9
  86. package/dist/tools/live-context.js +6 -0
  87. package/dist/tools/live.js +43 -6
  88. package/dist/tools/map-curves.js +73 -0
  89. package/dist/tools/notifications.js +4 -3
  90. package/dist/tools/open-games.js +2 -2
  91. package/dist/tools/opponents.js +2 -0
  92. package/dist/tools/past-games.js +74 -8
  93. package/dist/tools/picks.js +169 -57
  94. package/dist/tools/profile-hook.js +62 -3
  95. package/dist/tools/profile-opponents.js +2 -0
  96. package/dist/tools/push.js +2 -2
  97. package/dist/tools/relations.js +2 -0
  98. package/dist/tools/scout.js +16 -2
  99. package/dist/tools/state.js +6 -1
  100. package/dist/tools/strategy.js +3 -3
  101. package/dist/tools/stress-opponents.js +2 -0
  102. package/dist/tools/surrender-pending.js +10 -1
  103. package/dist/tools/surrender.js +18 -3
  104. package/dist/tools/turn.js +353 -19
  105. package/dist/tools/where.js +3 -1
  106. package/dist/version.js +1 -1
  107. package/manifest.json +23 -2
  108. package/mint.yaml +23 -0
  109. package/package.json +1 -1
  110. package/server.json +20 -2
  111. package/skills/warapp-mcp/SKILL.md +42 -8
@@ -4,7 +4,8 @@
4
4
  // each member of a bonus this turn's captures complete that falls to the enemy beside it (#265), to
5
5
  // the borders that fall without the armies withdrawn into them unless I am known to move first
6
6
  // (#215), to the borders short of their hold before the strike stack, then to the strike
7
- // stack, the short borders' comfortable margins (#218: the rest on the step's source when it
7
+ // stack, the territory with the most income at stake as a deterrent guard (#440), the short borders'
8
+ // comfortable margins (#218: the rest on the step's source when it
8
9
  // defends as well; #268: else spread over the borders facing one opponent for equal headroom
9
10
  // over its deploy), the step it saves toward, the expansion stack (#218: with no front in view,
10
11
  // the territory central to what I hold instead when the stack lies far from it; #288: with the
@@ -23,7 +24,14 @@ import { completedMembers } from './completed.js';
23
24
  import { COMFORT_MARGIN } from './shared.js';
24
25
  /** #218 (comment): hops the expansion stack may lie farther from my farthest border than the centre before the leftover stays central. */
25
26
  export const CENTRE_SLACK = 1;
27
+ /** #440: the least protected value of the territory a deterrent guard goes to. */
28
+ export const DETERRENT_MIN_VALUE = 5;
29
+ /** #440: how many times the most any border being topped up protects that territory must protect. */
30
+ export const DETERRENT_VALUE_RATIO = 2;
26
31
  import { knownFirst, lateArrivals, lateNet } from './holds.js';
32
+ import { isDoomedOut } from './doomed-out.js';
33
+ import { paybackLive } from './state.js';
34
+ import { stackFacing } from './strike.js';
27
35
  /**
28
36
  * The leftover deploy (rule 5). `stack` is the strike stack rule 0p.3c chose (plan/strike.ts),
29
37
  * `open` and `stays` planPass's views of a border; it returns the strike stack with the leftover
@@ -33,7 +41,7 @@ import { knownFirst, lateArrivals, lateNet } from './holds.js';
33
41
  export function leftoverDeploy(pc, ps, book, threats, hd, tgt, saveStep, stack, open, stays) {
34
42
  const { g, territories: state, label, bonusName, mine, mineSet, armiesOf, unitsAt, toTake, noSplit, bombOn } = pc;
35
43
  const { warnings, rationale, deployed, avail, locked, captured, moves, incoming } = ps;
36
- const { orderOf, deploy, undeploy, pools, roomAt, valueOf } = book;
44
+ const { orderOf, deploy, undeploy, pools, roomAt, valueOf, coverOf } = book;
37
45
  const { borders } = threats;
38
46
  const { conceded, kept, garrisoned, stood, threatened, stayAt } = hd;
39
47
  const { committing, byCloseness, bonusTarget, targets, repulsed } = tgt;
@@ -233,6 +241,7 @@ export function leftoverDeploy(pc, ps, book, threats, hd, tgt, saveStep, stack,
233
241
  };
234
242
  const revivable = threatened
235
243
  .filter((t) => conceded.has(t.id) &&
244
+ revivableHere(t.id) &&
236
245
  !stood.has(t.id) &&
237
246
  !moves.some((m) => m.from === t.id && m.reason !== 'withdraws from a conceded border'))
238
247
  .map((t) => {
@@ -440,7 +449,22 @@ export function leftoverDeploy(pc, ps, book, threats, hd, tgt, saveStep, stack,
440
449
  // stack, a border short of its margin), and then before the worse-order holds; otherwise the
441
450
  // worse-order holds go first, as before.
442
451
  // #240 review: judged as the plain plan judged it, so a repulsed attack's sure size revives nothing.
443
- const reviving = !stack && ps.budget > 0 && shortOf(comfortPool, new Map(), true).length > 0;
452
+ // #420: an opponent's most valuable border keeps its full deploy. When it was conceded while the borders
453
+ // its top-up sizes read comfortable (a 0 top-up leaves a guard-only stack no threat), no border is short
454
+ // and the revival would not run, so the leftover went to a stack nothing threatens: the full-deploy border
455
+ // is revived then too (alone), as it was when its other borders were sized for the full deploy.
456
+ const fullConceded = (() => {
457
+ const { primary, capAt } = threats.oneBorder();
458
+ const out = new Set();
459
+ for (const [player, t] of primary)
460
+ if (conceded.has(t) && [...capAt.values()].some((m) => m.has(player)))
461
+ out.add(t);
462
+ return out;
463
+ })();
464
+ const shortNow = shortOf(comfortPool, new Map(), true).length > 0;
465
+ const reviving = !stack && ps.budget > 0 && (shortNow || fullConceded.size > 0);
466
+ /** The conceded borders the revival may hold: every one when a border is short, else the #420 full-deploy ones. */
467
+ const revivableHere = (id) => shortNow || fullConceded.has(id);
444
468
  if (!reviving) {
445
469
  holdCompletedMembers();
446
470
  worseOrderHolds();
@@ -485,6 +509,43 @@ export function leftoverDeploy(pc, ps, book, threats, hd, tgt, saveStep, stack,
485
509
  const first = tr.turns[0];
486
510
  return `my income is flat (${tr.turns.map((t) => t.income).join(', ')} over turns ${first.turn}–${tr.turns[tr.turns.length - 1].turn}, slope ${tr.slope}/turn), so no comfort margin above the exact holds goes ahead of completing bonus ${bonusName(saving.bonusId)} (+${value}) from ${missing.some((x) => state.get(x)?.owner.kind === 'player') ? 'neutrals and weaker opponents' : 'neutrals'} (#303)`;
487
511
  })();
512
+ /** #218 (comment): the expansion stack's attacks land for sure without the leftover (under luck it raises their odds). */
513
+ const expansionSure = () => moves.every((m) => m.from !== expansion || m.kind !== 'attack' || (m.winChance ?? 1) >= 1);
514
+ /**
515
+ * #424: with no foe in view because a Diplomacy card in force makes the only one beside me no foe
516
+ * this turn, its border is still the front the card's end reopens: the leftover goes there rather
517
+ * than central or onto the expansion stack (live, it went to an ocean tile while the partner's
518
+ * stacks stood beside me). An open border of mine issuing no order beside a visible stack of an
519
+ * enforced partner, most at stake first, then the biggest partner stack beside it, then id. Only
520
+ * once the expansion stack's attacks land for sure without it, as the centre's rule.
521
+ */
522
+ const partnerFront = () => {
523
+ if (!noFront || !pc.truce.enforced.size || (expansion !== undefined && !expansionSure()))
524
+ return undefined;
525
+ const facing = (x) => {
526
+ let best;
527
+ for (const n of neighborsOf(g, x)) {
528
+ const st = state.get(n);
529
+ if (captured.has(n) || st?.owner.kind !== 'player' || !pc.truce.enforced.has(st.owner.playerId))
530
+ continue;
531
+ const armies = st.armies ?? 0;
532
+ if (!best || armies > best.armies || (armies === best.armies && st.owner.playerId < best.player))
533
+ best = { player: st.owner.playerId, armies };
534
+ }
535
+ return best;
536
+ };
537
+ const pick = [...mine]
538
+ .filter((x) => open(x) && !orderOf(x) && roomAt(x) > 0)
539
+ .flatMap((x) => {
540
+ const f = facing(x);
541
+ return f ? [{ id: x, ...f }] : [];
542
+ })
543
+ .sort((a, b) => valueOf(b.id) - valueOf(a.id) || b.armies - a.armies || a.id - b.id)[0];
544
+ if (!pick)
545
+ return undefined;
546
+ const until = pc.truce.given.find((r) => r.player === pick.player && r.enforced)?.enforcedUntil;
547
+ return { id: pick.id, player: pick.player, ...(until !== undefined ? { until } : {}) };
548
+ };
488
549
  const biggest = (ids) => [...ids].sort((a, b) => armiesOf(b) + (deployed.get(b) ?? 0) - (armiesOf(a) + (deployed.get(a) ?? 0)) || a - b)[0];
489
550
  const takenBack = reviving ? holdAfterAll() : undefined;
490
551
  if (reviving) {
@@ -640,15 +701,92 @@ export function leftoverDeploy(pc, ps, book, threats, hd, tgt, saveStep, stack,
640
701
  rationale.push(`Deploy ${d} of the leftover to ${label(b.id)} before the spread (#290): player ${b.player}'s stack beside it grew this turn (+${grewBy(b)}), so it is topped up toward its ${b.movable} movable armies plus its whole observed deploy (~${whole(b)}) (protects +${valueOf(b.id)}).`);
641
702
  }
642
703
  };
704
+ /**
705
+ * #440: before the leftover tops up a border that already holds, or one a recorded truce covers (T2),
706
+ * the territory with the most income at stake that carries a single-territory bonus of at least
707
+ * DETERRENT_MIN_VALUE gets a deterrent guard. Live (a large FFA), the +8
708
+ * territory that was the hub of nine pair bonuses ended on 3 while 15 armies topped up two low-value
709
+ * borders. That territory (open, issuing no order, not itself a border being topped up) must protect
710
+ * at least DETERRENT_MIN_VALUE and DETERRENT_VALUE_RATIO × the most any border being topped up
711
+ * protects, with an opponent within two steps; it gets armies until taking it needs more than
712
+ * COMFORT_MARGIN × a casual attack (the largest deploy estimate of those opponents, or its own
713
+ * threat when it is a border). The borders that would fall keep what makes them hold (a truce-covered
714
+ * one excepted: the truce covers it). Its armies then stay (`locked`: rule 6 moves none out), and so do
715
+ * they when it already needs more than that to take and gets no deploy.
716
+ */
717
+ const deterrentGuard = (bs) => {
718
+ const relaxed = (id) => ps.truce?.relaxed.has(id) ?? false;
719
+ const holdNeed = (b) => {
720
+ let d = 0;
721
+ while (d < ps.budget) {
722
+ const take = toTake(stays(b.id) + d + unitsAt(b.id));
723
+ if (take === null || take > b.threat)
724
+ break;
725
+ d++;
726
+ }
727
+ return d;
728
+ };
729
+ const reserve = bs.filter((b) => !relaxed(b.id)).reduce((sum, b) => sum + holdNeed(b), 0);
730
+ // A territory that carries a large single-territory bonus of its own (a city, a capital), not
731
+ // any member of a held bonus: the issue's case, and the golden corpus's churn stays small.
732
+ const carries = (x) => [...g.bonusById.values()].some((b) => b.value >= DETERRENT_MIN_VALUE && bonusTerritories(g, b.id).length === 1 && bonusTerritories(g, b.id)[0] === x);
733
+ const hub = [...mine]
734
+ .filter((x) => open(x) && !orderOf(x) && carries(x))
735
+ .sort((a, b) => valueOf(b) - valueOf(a) || a - b)[0];
736
+ if (hub === undefined || bs.some((b) => b.id === hub))
737
+ return;
738
+ const value = valueOf(hub);
739
+ const top = Math.max(0, ...bs.map((b) => valueOf(b.id)));
740
+ if (value < DETERRENT_MIN_VALUE || value < DETERRENT_VALUE_RATIO * top)
741
+ return;
742
+ const near = new Set([hub, ...neighborsOf(g, hub)].flatMap((x) => [x, ...neighborsOf(g, x)]));
743
+ const foes = [...new Set([...near].flatMap((x) => {
744
+ const st = state.get(x);
745
+ return !captured.has(x) && st?.owner.kind === 'player' && pc.foe(st.owner.playerId) ? [st.owner.playerId] : [];
746
+ }))].sort((a, b) => a - b);
747
+ if (!foes.length)
748
+ return;
749
+ const casual = Math.max(...foes.map((p) => pc.deployOf(p)), borders.get(hub)?.threat ?? 0);
750
+ const bar = COMFORT_MARGIN * casual;
751
+ const most = Math.min(roomAt(hub), ps.budget - reserve);
752
+ let d = 0;
753
+ while (d < most) {
754
+ const take = toTake(stays(hub) + d + unitsAt(hub));
755
+ if (take === null || take > bar)
756
+ break;
757
+ d++;
758
+ }
759
+ const against = `${COMFORT_MARGIN} × the ~${casual} ${borders.has(hub) && casual === borders.get(hub).threat ? 'threat beside it' : `deploy of player ${foes.length === 1 ? foes[0] : foes.join(' or ')} within two steps`}`;
760
+ if (d <= 0) {
761
+ // Already at its bar: it needs no deploy, but its armies still stay (rule 6 would otherwise
762
+ // walk them to the front and leave a stronger capital weaker than a topped-up one).
763
+ const at = stays(hub) + unitsAt(hub);
764
+ const take = toTake(at);
765
+ if (take !== null && take <= bar)
766
+ return;
767
+ locked.add(hub);
768
+ rationale.push(`${label(hub)} keeps its ${at} armies as a deterrent guard (#440): it has the most income at stake (protects +${value}, the borders being topped up at most +${top}), and they ${take === null ? 'cannot be taken' : `need ${take} to take`}, more than ${against}, so none move to the front.`);
769
+ return;
770
+ }
771
+ deploy(hub, d);
772
+ locked.add(hub);
773
+ keepFromMargin(hub);
774
+ const at = stays(hub) + unitsAt(hub);
775
+ rationale.push(`Deploy ${d} of the leftover to ${label(hub)} as a deterrent guard (#440): it has the most income at stake (protects +${value}, the borders being topped up at most +${top}), and its ${at} armies now need ${toTake(at)} to take, more than ${against}, before any border that already holds is topped up.`);
776
+ };
643
777
  /** #268: the rest spread by {@link headroomSpread}, already deployed: nothing is left for `where`. */
644
778
  let spreadRest;
645
779
  let where;
646
780
  let why;
781
+ /** #431: the move out of a doomed stack's border the leftover goes with (every territory of mine left). */
782
+ let ridesOut;
647
783
  let lock = false;
648
784
  /** #163: the leftover concentrates for one attack (strike, saving, expansion): no Bomb split. */
649
785
  let concentrating = false;
650
786
  /** #218 (comment): the leftover stays central: rule 6 keeps it there, not the armies already there. */
651
787
  let central = false;
788
+ /** #424: the Diplomacy partner's border the leftover keeps as the front. */
789
+ let peace;
652
790
  /** #226 review: the leftover defends `where` or saves there: the #226 margin leaves it. */
653
791
  let defends = false;
654
792
  /** #288: the attacks out of the expansion stack the leftover goes with (it has nothing else left to attack). */
@@ -659,14 +797,22 @@ export function leftoverDeploy(pc, ps, book, threats, hd, tgt, saveStep, stack,
659
797
  * #288: a source facing no threat whose every neighbour is mine or captured this turn has nothing
660
798
  * left to attack once the captures land, so a leftover there idles: it goes with its attacks whose
661
799
  * targets still have a step onward (most steps onward first, then id). Never under No-Split (an
662
- * order already carries the whole stack), nor with a combined capture or a softener.
800
+ * order already carries the whole stack), nor with a combined capture or a softener, nor onto a
801
+ * member a #422 payback completion may lose next turn.
663
802
  */
664
803
  const joinsOf = (src) => {
665
804
  if (noSplit || borders.has(src) || pc.myCommanders.has(src) || leftToAttack(src).length)
666
805
  return undefined;
667
806
  const onward = (t) => leftToAttack(t).filter((n) => n !== src).length;
668
807
  const out = moves
669
- .filter((m) => m.kind === 'attack' && m.from === src && captured.has(m.to) && !ps.combined.has(m.to) && state.get(m.to)?.owner.kind === 'neutral' && onward(m.to) > 0)
808
+ .filter((m) => m.kind === 'attack' &&
809
+ m.from === src &&
810
+ captured.has(m.to) &&
811
+ !ps.combined.has(m.to) &&
812
+ // #422: never onto a member a payback completion expects to lose next turn.
813
+ !paybackLive(ps, ps.paybackFalls, m.to) &&
814
+ state.get(m.to)?.owner.kind === 'neutral' &&
815
+ onward(m.to) > 0)
670
816
  .sort((a, b) => onward(b.to) - onward(a.to) || a.to - b.to);
671
817
  return out.length ? out : undefined;
672
818
  };
@@ -737,7 +883,7 @@ export function leftoverDeploy(pc, ps, book, threats, hd, tgt, saveStep, stack,
737
883
  lock = true;
738
884
  concentrating = true;
739
885
  where = stack.id;
740
- why = `one strike stack on the border facing player ${stack.player}, ${pc.siege?.target === stack.player ? 'the siege target' : 'the strongest opponent'} (~${stack.income} income, ${stack.source}), not spread`;
886
+ why = `one strike stack on the border facing player ${stack.player}, ${stackFacing(pc, stack)} (~${stack.income} income, ${stack.source}), not spread`;
741
887
  }
742
888
  else if (short[0] && flatSave && saving && open(saving.from) && roomAt(saving.from) > 0) {
743
889
  // (Under Local Deployment the leftover may not land there: then the margins stand.)
@@ -750,6 +896,7 @@ export function leftoverDeploy(pc, ps, book, threats, hd, tgt, saveStep, stack,
750
896
  }
751
897
  else if (short[0]) {
752
898
  defends = true;
899
+ deterrentGuard(short);
753
900
  // #218: the source of the step the leftover would save toward, when it is an open border:
754
901
  // once every short border has its comfortable margin, the rest goes there when it defends
755
902
  // as well as the border the rest would go to, and counts toward the step. A margin itself
@@ -803,6 +950,12 @@ export function leftoverDeploy(pc, ps, book, threats, hd, tgt, saveStep, stack,
803
950
  lock = true;
804
951
  concentrating = true;
805
952
  }
953
+ else if ((peace = partnerFront()) !== undefined) {
954
+ where = peace.id;
955
+ why = `the border facing player ${peace.player}, at peace by a Diplomacy card in force${peace.until !== undefined ? ` until turn ${peace.until}` : ''} — no other enemy is in view, and its border is the front again once the card ends (#424)`;
956
+ defends = true;
957
+ lock = true;
958
+ }
806
959
  else if (expansion !== undefined && open(expansion)) {
807
960
  // #218 (comment): with no enemy in view there is no front to go to, and a leftover riding
808
961
  // the top expansion walks deeper into one region every turn (live: 14 hops from the nearest
@@ -810,8 +963,7 @@ export function leftoverDeploy(pc, ps, book, threats, hd, tgt, saveStep, stack,
810
963
  // from my farthest border than the territory central to what I hold, and its attacks land
811
964
  // for sure without the leftover, the leftover stays there.
812
965
  // Only once its attacks land for sure: under luck the leftover there raises their odds.
813
- const sure = moves.every((m) => m.from !== expansion || m.kind !== 'attack' || (m.winChance ?? 1) >= 1);
814
- const c = noFront && sure ? centre() : undefined;
966
+ const c = noFront && expansionSure() ? centre() : undefined;
815
967
  if (c && c.id !== expansion && (c.ecc.get(expansion) ?? Infinity) > c.ecc.get(c.id) + CENTRE_SLACK) {
816
968
  where = c.id;
817
969
  why = `no enemy is in view, so there is no front to go to: it stays central to what I hold (at most ${c.ecc.get(c.id)} hops from each of my borders, where the stack working on the top expansion, ${label(expansion)}, is ${c.ecc.get(expansion)} from its farthest), ready for wherever a front opens`;
@@ -836,9 +988,15 @@ export function leftoverDeploy(pc, ps, book, threats, hd, tgt, saveStep, stack,
836
988
  why = 'my biggest stack';
837
989
  }
838
990
  else {
839
- where = biggest(mine);
991
+ // #431: never the border a doomed stack just left, while another territory of mine remains;
992
+ // when none does, the leftover leaves with that stack's move out (deploys land before moves).
993
+ const others = mine.filter((x) => !ps.doomedOut.has(x));
994
+ where = biggest(others.length ? others : mine);
840
995
  why = 'my biggest stack';
841
- warnings.push(`Every territory I hold is conceded; the leftover ${ps.budget} goes to ${label(where)} anyway.`);
996
+ ridesOut = ps.doomedOut.has(where) ? moves.find((m) => m.from === where && isDoomedOut(m)) : undefined;
997
+ warnings.push(ridesOut
998
+ ? `Every territory I hold is conceded; the leftover ${ps.budget} goes to ${label(where)} and leaves with its move out (#431).`
999
+ : `Every territory I hold is conceded; the leftover ${ps.budget} goes to ${label(where)} anyway.`);
842
1000
  }
843
1001
  }
844
1002
  let n = roomAt(where);
@@ -946,6 +1104,23 @@ export function leftoverDeploy(pc, ps, book, threats, hd, tgt, saveStep, stack,
946
1104
  else if (n > 0 && !spreadRest) {
947
1105
  rationale.push(`Deploy the leftover ${n} to ${label(where)}: ${why}.`);
948
1106
  deploy(where, n);
1107
+ // #431: the border its doomed stack left is lost for sure: the deploy goes with that stack.
1108
+ if (ridesOut) {
1109
+ const m = ridesOut;
1110
+ const add = Math.min(n, avail.get(where) - (pc.noSplit ? 0 : coverOf(where)));
1111
+ if (add > 0) {
1112
+ avail.set(where, avail.get(where) - add);
1113
+ m.armies += add;
1114
+ if (m.kind === 'transfer')
1115
+ incoming.set(m.to, (incoming.get(m.to) ?? 0) + add);
1116
+ else {
1117
+ if (ps.survivorsAt.has(m.to))
1118
+ ps.survivorsAt.set(m.to, ps.survivorsAt.get(m.to) + add);
1119
+ m.winChance = pc.chanceOf(m.armies, pc.defenceAt(m.to));
1120
+ }
1121
+ rationale.push(`The ${add} deployed on ${label(where)} leave with its move out to ${label(m.to)} (#431): ${m.armies} go.`);
1122
+ }
1123
+ }
949
1124
  // Keep a strike or expansion stack there: the transfer step must not walk it back as "idle
950
1125
  // interior" armies. Only when it got deploys (under Local Deployment it may get none).
951
1126
  if (lock)
@@ -980,6 +1155,9 @@ export function leftoverDeploy(pc, ps, book, threats, hd, tgt, saveStep, stack,
980
1155
  const m = [...p.territoryIds].sort((a, b) =>
981
1156
  // No-Split (#124): a member that issues an order would send the pool away with it.
982
1157
  Number(orderOf(a) !== undefined) - Number(orderOf(b) !== undefined) ||
1158
+ // #431: a border a doomed stack left (merged or attacked out of) is lost for sure: the pool
1159
+ // goes to any other member first, even an interior one.
1160
+ Number(ps.doomedOut.has(a)) - Number(ps.doomedOut.has(b)) ||
983
1161
  Number(borders.has(b)) - Number(borders.has(a)) ||
984
1162
  Number(lost(a)) - Number(lost(b)) ||
985
1163
  danger(b) - danger(a) ||