@chrischall/warapp-mcp 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +4 -4
- package/.claude-plugin/plugin.json +2 -2
- package/README.md +199 -8
- package/dist/analysis/award.js +159 -0
- package/dist/analysis/bonuses.js +98 -0
- package/dist/analysis/breach.js +62 -0
- package/dist/analysis/card-support.js +329 -0
- package/dist/analysis/card-threats.js +234 -0
- package/dist/analysis/cards-in-effect.js +130 -0
- package/dist/analysis/chains.js +59 -0
- package/dist/analysis/chokepoints.js +140 -0
- package/dist/analysis/combat.js +231 -0
- package/dist/analysis/commander-safety.js +149 -0
- package/dist/analysis/commitments.js +338 -0
- package/dist/analysis/exchange.js +155 -0
- package/dist/analysis/graph.js +82 -0
- package/dist/analysis/guard-off.js +54 -0
- package/dist/analysis/guess.js +351 -0
- package/dist/analysis/headroom.js +241 -0
- package/dist/analysis/hold-diff.js +64 -0
- package/dist/analysis/how-lost.js +130 -0
- package/dist/analysis/index.js +14 -0
- package/dist/analysis/local-deploy.js +159 -0
- package/dist/analysis/lookahead.js +108 -0
- package/dist/analysis/opponent-profile.js +551 -0
- package/dist/analysis/opponents.js +1245 -0
- package/dist/analysis/outlook.js +633 -0
- package/dist/analysis/overrides.js +60 -0
- package/dist/analysis/picks.js +1879 -0
- package/dist/analysis/plan/after-leftover.js +140 -0
- package/dist/analysis/plan/cap-spend.js +242 -0
- package/dist/analysis/plan/captures.js +1195 -0
- package/dist/analysis/plan/commander.js +236 -0
- package/dist/analysis/plan/completed.js +29 -0
- package/dist/analysis/plan/conceded-out.js +131 -0
- package/dist/analysis/plan/estimates.js +715 -0
- package/dist/analysis/plan/guard-off.js +232 -0
- package/dist/analysis/plan/holds.js +1631 -0
- package/dist/analysis/plan/leftover.js +760 -0
- package/dist/analysis/plan/move-order.js +319 -0
- package/dist/analysis/plan/orders.js +317 -0
- package/dist/analysis/plan/relations.js +702 -0
- package/dist/analysis/plan/report.js +672 -0
- package/dist/analysis/plan/shared.js +1218 -0
- package/dist/analysis/plan/stand-still.js +823 -0
- package/dist/analysis/plan/state.js +44 -0
- package/dist/analysis/plan/strike.js +1416 -0
- package/dist/analysis/plan/targets.js +651 -0
- package/dist/analysis/plan/team.js +299 -0
- package/dist/analysis/plan/threats.js +927 -0
- package/dist/analysis/plan/transfers.js +328 -0
- package/dist/analysis/plan/util.js +5 -0
- package/dist/analysis/plan/vacated.js +285 -0
- package/dist/analysis/planner.js +2922 -0
- package/dist/analysis/player-record.js +144 -0
- package/dist/analysis/player.js +268 -0
- package/dist/analysis/profile.js +347 -0
- package/dist/analysis/relations.js +1030 -0
- package/dist/analysis/schedule.js +88 -0
- package/dist/analysis/since-last.js +50 -0
- package/dist/analysis/static-seats.js +71 -0
- package/dist/analysis/stress.js +577 -0
- package/dist/analysis/team-brief.js +151 -0
- package/dist/analysis/tripwire.js +644 -0
- package/dist/analysis/turn-sim.js +259 -0
- package/dist/analysis/whereabouts.js +1109 -0
- package/dist/board.js +105 -0
- package/dist/bundle.js +117959 -36172
- package/dist/cards.js +435 -0
- package/dist/chat-cursor.js +135 -0
- package/dist/chat.js +192 -0
- package/dist/client.js +361 -0
- package/dist/config.js +62 -0
- package/dist/direct/link.js +102 -0
- package/dist/direct/session.js +267 -0
- package/dist/direct/transport.js +424 -0
- package/dist/guess-salt.js +65 -0
- package/dist/hosted.js +24 -0
- package/dist/index.js +33 -0
- package/dist/join-color.js +140 -0
- package/dist/live.js +278 -0
- package/dist/map-stats.js +152 -0
- package/dist/map-store.js +204 -0
- package/dist/move-order.js +141 -0
- package/dist/my-games.js +180 -0
- package/dist/normalize.js +411 -0
- package/dist/open-games.js +537 -0
- package/dist/outlook-log.js +138 -0
- package/dist/path-lock.js +22 -0
- package/dist/pick-list.js +193 -0
- package/dist/player-record-store.js +229 -0
- package/dist/player-states.js +31 -0
- package/dist/profile-store.js +677 -0
- package/dist/protocol/bundle.js +375 -0
- package/dist/protocol/codec.js +556 -0
- package/dist/protocol/decoded.js +12 -0
- package/dist/protocol/interp.js +1904 -0
- package/dist/protocol/methods.js +531 -0
- package/dist/protocol/rpc.js +202 -0
- package/dist/push/listener.js +393 -0
- package/dist/push/packets.js +66 -0
- package/dist/push/socketio.js +333 -0
- package/dist/push/websocket.js +179 -0
- package/dist/redact.js +62 -0
- package/dist/relations-store.js +242 -0
- package/dist/replay.js +215 -0
- package/dist/scalars.js +28 -0
- package/dist/scout.js +346 -0
- package/dist/stats.js +269 -0
- package/dist/strategy/card-clock.js +185 -0
- package/dist/strategy/map-notes.js +128 -0
- package/dist/strategy/notes.js +2168 -0
- package/dist/team-cards.js +84 -0
- package/dist/team-orders.js +150 -0
- package/dist/teams.js +19 -0
- package/dist/tools/award.js +206 -0
- package/dist/tools/begin-turn.js +162 -0
- package/dist/tools/board.js +77 -0
- package/dist/tools/card-advice.js +256 -0
- package/dist/tools/card-effects.js +49 -0
- package/dist/tools/card-plays.js +434 -0
- package/dist/tools/card-support-advice.js +140 -0
- package/dist/tools/chat-inbox.js +146 -0
- package/dist/tools/chat.js +483 -0
- package/dist/tools/check-plan.js +688 -0
- package/dist/tools/commander-check.js +102 -0
- package/dist/tools/commitments.js +216 -0
- package/dist/tools/common.js +143 -0
- package/dist/tools/diplomacy.js +342 -0
- package/dist/tools/game.js +126 -0
- package/dist/tools/guess-view.js +56 -0
- package/dist/tools/healthcheck.js +90 -0
- package/dist/tools/hints.js +34 -0
- package/dist/tools/how-lost.js +154 -0
- package/dist/tools/index.js +142 -0
- package/dist/tools/join-game.js +327 -0
- package/dist/tools/leave-game.js +181 -0
- package/dist/tools/live-context.js +339 -0
- package/dist/tools/live.js +404 -0
- package/dist/tools/map-stats.js +117 -0
- package/dist/tools/map.js +133 -0
- package/dist/tools/my-games.js +123 -0
- package/dist/tools/open-games.js +104 -0
- package/dist/tools/opponents.js +151 -0
- package/dist/tools/order-options.js +135 -0
- package/dist/tools/picks.js +292 -0
- package/dist/tools/profile-hook.js +156 -0
- package/dist/tools/profile-opponents.js +121 -0
- package/dist/tools/profile-reads.js +99 -0
- package/dist/tools/push.js +385 -0
- package/dist/tools/relations.js +611 -0
- package/dist/tools/resolve.js +45 -0
- package/dist/tools/route.js +36 -0
- package/dist/tools/scout.js +87 -0
- package/dist/tools/session.js +163 -0
- package/dist/tools/strategy.js +200 -0
- package/dist/tools/stress-opponents.js +83 -0
- package/dist/tools/surrender.js +240 -0
- package/dist/tools/survival.js +189 -0
- package/dist/tools/team-brief.js +266 -0
- package/dist/tools/team-source.js +222 -0
- package/dist/tools/team.js +182 -0
- package/dist/tools/turn.js +2761 -0
- package/dist/tools/uncommit.js +160 -0
- package/dist/tools/where.js +275 -0
- package/dist/tools/write-kit.js +158 -0
- package/dist/transport-bridge.js +33 -0
- package/dist/trend.js +452 -0
- package/dist/version.js +4 -0
- package/manifest.json +174 -4
- package/mint.yaml +101 -14
- package/package.json +11 -6
- package/server.json +40 -3
- package/skills/warapp-api/SKILL.md +95 -4
- package/skills/warapp-api/references/scout.mjs +348 -0
- package/skills/warapp-api/scripts/warapp.sh +69 -8
- package/skills/warapp-mcp/SKILL.md +969 -21
|
@@ -0,0 +1,672 @@
|
|
|
1
|
+
// The report of a planning pass (#133): once every order is planned, what the plan expects (the
|
|
2
|
+
// captures, the bonuses completed and their joint chance, #126; the income at risk, #109), the
|
|
3
|
+
// frontier budget (rule 4d, #71), the two-step holds still cited (#172), the absorptions claimed
|
|
4
|
+
// (#201), the gambled and shared borders (#69, #167, #201), the offence report (#72), the warnings
|
|
5
|
+
// that follow the deploys, the "Not attacking" line (#167), and the plan itself: the offence list,
|
|
6
|
+
// #172's final check of each territory in between, the retaliation warning (#201), the exchanges
|
|
7
|
+
// (#167) and the turns to eliminate each opponent (#179). planPass runs them where they ran before:
|
|
8
|
+
// the expected outcome and the offence report after Order Priority for a strike, the plan last,
|
|
9
|
+
// after the move order (plan/move-order.ts).
|
|
10
|
+
import { bonusTerritories } from '../bonuses.js';
|
|
11
|
+
import { classifyExchange } from '../exchange.js';
|
|
12
|
+
import { neighborsOf } from '../graph.js';
|
|
13
|
+
import { bonusesAtRisk, captureCost, CITE_OPEN, CITE_RE, ETA_HORIZON, NOT_ATTACKING_REASONS, SECOND_FRONT_LEAD, SLOT_NAMES, } from './shared.js';
|
|
14
|
+
import { knownFirst, lateArrivals, lateNet, withdrawnInto } from './holds.js';
|
|
15
|
+
import { relaxedFor } from './relations.js';
|
|
16
|
+
import { byNum, pct } from './util.js';
|
|
17
|
+
/** #212: why a neighbour of mine could not attack a lead step, as the skip line says it. */
|
|
18
|
+
const BLOCKED = {
|
|
19
|
+
commander: 'holds my commander: nothing leaves it (rule 1k)',
|
|
20
|
+
order: 'already issues an order',
|
|
21
|
+
locked: 'keeps its armies where they are this turn',
|
|
22
|
+
};
|
|
23
|
+
/** The expected outcome, the frontier, the two-step holds and the gambled and shared borders. */
|
|
24
|
+
export function reportExpected(pc, ps, book, threats, hd, tgt, caps) {
|
|
25
|
+
const { g, foe, deployOf, territories: state, label, bonusName, mine, mineSet, incomeAfter, unitsAt, completedBy, guard, toTake, noSplit, } = pc;
|
|
26
|
+
const { warnings, rationale, absorbLines, captured, moves, survivorsAt } = ps;
|
|
27
|
+
const { heldNow } = book;
|
|
28
|
+
const { borders } = threats;
|
|
29
|
+
const { conceded, heldAgainst, keptIncome, stayAt, twoStepBound, twoStepCited, twoStepHold, twoStepNote } = hd;
|
|
30
|
+
const { frontierOf, startHold } = tgt;
|
|
31
|
+
const { notCompleted, stopped, stoppedLine, denied } = caps;
|
|
32
|
+
// ---- expected ----
|
|
33
|
+
const after = heldNow();
|
|
34
|
+
const completed = completedBy(after);
|
|
35
|
+
// #126: a completion lands only if every capture it needs does — its chance is their product.
|
|
36
|
+
for (const id of completed) {
|
|
37
|
+
const taken = bonusTerritories(g, id).filter((x) => captured.has(x));
|
|
38
|
+
// Each member's capture is its last attack (a combined capture's softeners fail by design).
|
|
39
|
+
const chances = taken.map((x) => moves.filter((m) => m.kind === 'attack' && m.to === x).at(-1)?.winChance ?? 1);
|
|
40
|
+
const joint = chances.reduce((a, b) => a * b, 1);
|
|
41
|
+
if (joint < 1)
|
|
42
|
+
rationale.push(`Bonus ${bonusName(id)} (+${g.bonusById.get(id).value}) completes with a ${pct(joint)} chance: the product of its ${taken.length} capture${taken.length === 1 ? "'s" : "s'"} win chances this turn.`);
|
|
43
|
+
}
|
|
44
|
+
// A bonus refused in one pass but completed by a later one is not reported as refused.
|
|
45
|
+
for (const id of [...notCompleted.keys()].sort(byNum)) {
|
|
46
|
+
if (completed.includes(id))
|
|
47
|
+
continue;
|
|
48
|
+
warnings.push(notCompleted.get(id).warning);
|
|
49
|
+
rationale.push(notCompleted.get(id).why);
|
|
50
|
+
}
|
|
51
|
+
// Issue #109: the income the plan's concessions put at risk. `nextIncome` counts every
|
|
52
|
+
// territory the plan keeps (conceded borders included); a second figure drops the conceded
|
|
53
|
+
// borders, by the same income rule, and names the bonuses they break.
|
|
54
|
+
const nextIncome = incomeAfter(after);
|
|
55
|
+
const atRisk = bonusesAtRisk(g, after, conceded);
|
|
56
|
+
const ifFall = incomeAfter(new Set([...after].filter((t) => !conceded.has(t))));
|
|
57
|
+
const reportRisk = atRisk.length > 0 || ifFall !== nextIncome;
|
|
58
|
+
if (ifFall < nextIncome) {
|
|
59
|
+
const what = atRisk.length
|
|
60
|
+
? `bonus ${atRisk.map((b) => `${bonusName(b.bonusId)} (+${b.value})`).join(', ')}`
|
|
61
|
+
: [...conceded].sort(byNum).map(label).join(', ');
|
|
62
|
+
warnings.push(`This plan concedes ${what}: next income ${nextIncome} if they hold, ${ifFall} if the conceded borders fall.`);
|
|
63
|
+
}
|
|
64
|
+
const nextIncomeText = ifFall < nextIncome ? `${nextIncome}, ${ifFall} if the conceded borders fall` : `${nextIncome}`;
|
|
65
|
+
// Rule 4d's frontier budget (#71): the frontier this plan leaves against the next income.
|
|
66
|
+
const frontier = {
|
|
67
|
+
...frontierOf(after, (x) => (mineSet.has(x) ? stayAt(x) : (survivorsAt.get(x) ?? 0))),
|
|
68
|
+
income: keptIncome(after),
|
|
69
|
+
};
|
|
70
|
+
const startCost = startHold();
|
|
71
|
+
const outgrown = frontier.holdCost > frontier.income && frontier.holdCost > startCost;
|
|
72
|
+
// A capture stopped by one pass but taken by a later one (the next income grew in between) is
|
|
73
|
+
// in the orders: nothing may still say it was not taken.
|
|
74
|
+
const notTaken = stopped.filter((x) => !captured.has(x.t));
|
|
75
|
+
const takenAfterAll = stopped.filter((x) => captured.has(x.t)).map((x) => stoppedLine(x.t));
|
|
76
|
+
if (takenAfterAll.length) {
|
|
77
|
+
const kept = rationale.filter((r) => !takenAfterAll.some((line) => r.startsWith(line)));
|
|
78
|
+
rationale.splice(0, rationale.length, ...kept);
|
|
79
|
+
}
|
|
80
|
+
// #281: each vacated territory taken though it faces an enemy, said once.
|
|
81
|
+
for (const t of [...denied.keys()].sort(byNum))
|
|
82
|
+
if (captured.has(t))
|
|
83
|
+
rationale.push(denied.get(t));
|
|
84
|
+
if (outgrown || notTaken.length) {
|
|
85
|
+
const stop = notTaken.length
|
|
86
|
+
? ` Expansion that adds enemy borders without completing a bonus is stopped: ${notTaken.map((x) => `${label(x.t)} (the frontier would need ${x.holdCost}, next income ${x.income})`).join(', ')} not taken.`
|
|
87
|
+
: '';
|
|
88
|
+
warnings.push(outgrown
|
|
89
|
+
? `Frontier grows faster than income: holding its ${frontier.enemyBorders} enemy-facing borders next turn needs ${frontier.holdCost} at the expected deploy, more than ${frontier.income < nextIncome
|
|
90
|
+
? `the ${frontier.income} of next income the plan counts on (${nextIncome} if every border holds: the bonuses it concedes or still contests are left out)`
|
|
91
|
+
: `the next income of ${frontier.income}`} (${startCost} at the start of the turn).${stop}`
|
|
92
|
+
: `Frontier grows faster than income.${stop}`);
|
|
93
|
+
}
|
|
94
|
+
// #172: each two-step hold an order cited, or that shrank or ruled out an order (a pressure
|
|
95
|
+
// attack's size, a softener, a finisher, a top-up), is stated once, with the stay the plan ends
|
|
96
|
+
// on (an order's own note would read the stay mid-plan) — if it still applies at the end. One
|
|
97
|
+
// that no longer does (a later capture took the territory in between, or the stay fell so far
|
|
98
|
+
// that holding is hopeless) is not stated, and the orders' citations of it are dropped.
|
|
99
|
+
const stillHeld = new Set();
|
|
100
|
+
for (const t of [...new Set([...twoStepBound, ...twoStepCited.keys()])].sort(byNum)) {
|
|
101
|
+
// The stay the plan ends on, rule 6's arrivals included, decides whether the hold still applies.
|
|
102
|
+
const h = twoStepHold(t, undefined, true);
|
|
103
|
+
if (!h)
|
|
104
|
+
continue;
|
|
105
|
+
stillHeld.add(t);
|
|
106
|
+
rationale.push(`Hold ${label(t)} against a stack two steps away (${twoStepNote(t, h)}).`);
|
|
107
|
+
}
|
|
108
|
+
for (let i = rationale.length - 1; i >= 0; i--) {
|
|
109
|
+
const line = rationale[i];
|
|
110
|
+
if (!line.includes(CITE_OPEN))
|
|
111
|
+
continue;
|
|
112
|
+
const text = line.replace(CITE_RE, (_m, t, clause, fallback) => stillHeld.has(Number(t)) ? clause : fallback);
|
|
113
|
+
if (text.trim())
|
|
114
|
+
rationale[i] = text;
|
|
115
|
+
else
|
|
116
|
+
rationale.splice(i, 1);
|
|
117
|
+
}
|
|
118
|
+
// #201: an absorption is claimed only where an attack on that player is made.
|
|
119
|
+
for (const [p, line] of absorbLines) {
|
|
120
|
+
const owned = (t) => {
|
|
121
|
+
const o = state.get(t)?.owner;
|
|
122
|
+
return o?.kind === 'player' && o.playerId === p;
|
|
123
|
+
};
|
|
124
|
+
if (moves.some((m) => m.kind === 'attack' && owned(m.to)))
|
|
125
|
+
continue;
|
|
126
|
+
const i = rationale.indexOf(line);
|
|
127
|
+
if (i >= 0)
|
|
128
|
+
rationale.splice(i, 1);
|
|
129
|
+
}
|
|
130
|
+
if (frontier.enemyBorders)
|
|
131
|
+
rationale.push(`Frontier after this plan: ${frontier.borders} borders, ${frontier.enemyBorders} facing an enemy, needing ${frontier.holdCost} next turn to hold at the expected deploy (next income ${nextIncomeText}); every capture's score paid for the borders it adds.`);
|
|
132
|
+
// #201, behind: the borders sized for the expected attack, per player — what that gambles.
|
|
133
|
+
{
|
|
134
|
+
// Read from `borders`, the sizing the plan used (re-judged after captures), never from a
|
|
135
|
+
// hypothetical re-judge: the expected attack is the border's `threat`, the worst case its `worst`.
|
|
136
|
+
const gambled = new Map();
|
|
137
|
+
for (const b of borders.values())
|
|
138
|
+
if (b.worst !== undefined)
|
|
139
|
+
gambled.set(b.id, { player: b.player, worst: b.worst, expected: b.threat, growth: b.growth });
|
|
140
|
+
const byPlayer = new Map();
|
|
141
|
+
for (const [id, x] of gambled)
|
|
142
|
+
if (mineSet.has(id) && !conceded.has(id) && !relaxedFor(ps, id, x.player))
|
|
143
|
+
byPlayer.set(x.player, [...(byPlayer.get(x.player) ?? []), id]);
|
|
144
|
+
for (const [p, ids] of [...byPlayer].sort((a, b) => a[0] - b[0])) {
|
|
145
|
+
// Gambled: holds against the expected attack as the plan leaves it, not against the worst case.
|
|
146
|
+
// One short of even the expected attack is no gamble (its threat warning says so).
|
|
147
|
+
const takeAt = (id) => toTake(stayAt(id) + unitsAt(id)) ?? Infinity;
|
|
148
|
+
const gamble = ids.sort(byNum).filter((id) => takeAt(id) > gambled.get(id).expected && takeAt(id) <= gambled.get(id).worst);
|
|
149
|
+
const allHold = ids.every((id) => takeAt(id) > gambled.get(id).worst);
|
|
150
|
+
if (!gamble.length && !allHold)
|
|
151
|
+
continue;
|
|
152
|
+
warnings.push(`Behind (#201): the borders facing player ${p} are sized for its expected attack (its movable armies next to each plus its whole ~${deployOf(p)} deploy, #59), not the worst case its growth per turn projects, and the armies saved go to the captures. ${gamble.length
|
|
153
|
+
? `Gambled: ${gamble
|
|
154
|
+
.map((id) => {
|
|
155
|
+
const x = gambled.get(id);
|
|
156
|
+
return `${label(id)} (it holds against the expected ~${x.expected}, not the worst-case ~${x.worst} at its growth of ~${x.growth} a turn)`;
|
|
157
|
+
})
|
|
158
|
+
.join(', ')} — each falls if player ${p} keeps growing at that rate and attacks it.`
|
|
159
|
+
: 'Each still holds against the worst case as the plan leaves it.'}`);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
// Issue #69, second point: several borders facing one opponent are each sized for its whole
|
|
163
|
+
// deploy. That is each border's worst case — the opponent picks where to land after I
|
|
164
|
+
// commit — so a sure hold of all of them needs every one sized for it; no army is counted
|
|
165
|
+
// twice on one border (movable + max(deploy, growth)).
|
|
166
|
+
for (const [p, ids] of [...heldAgainst].sort((a, b) => a[0] - b[0])) {
|
|
167
|
+
// B2 (T2): a border relaxed for that player is sized for no deploy of its: not this line's.
|
|
168
|
+
const held = [...ids].filter((id) => !conceded.has(id) && !relaxedFor(ps, id, p)).sort(byNum);
|
|
169
|
+
if (held.length < 2)
|
|
170
|
+
continue;
|
|
171
|
+
rationale.push(`Each of the ${held.length} borders held against player ${p} (${held.map(label).join(', ')}) is sized for its full ~${deployOf(p)} deploy: it chooses where to land after seeing my orders, so any one of them can face all of it, and a share each would leave whichever it picks short. Each border's threat counts its stacks next to that border once plus one deploy, never the deploy twice.`);
|
|
172
|
+
}
|
|
173
|
+
// #167: cover odds. One deploy lands on one territory: against a player that faces two or more of
|
|
174
|
+
// my kept borders, those that hold against its stacks alone but not with its whole deploy on them
|
|
175
|
+
// are a gamble on where it lands — say which, and the chance each survives if it picks at random.
|
|
176
|
+
// A split deploy takes every border whose shortfall it funds alongside another's: those get 0%.
|
|
177
|
+
{
|
|
178
|
+
const facingBy = new Map();
|
|
179
|
+
for (const x of mine) {
|
|
180
|
+
if (conceded.has(x))
|
|
181
|
+
continue;
|
|
182
|
+
const by = new Map();
|
|
183
|
+
for (const n of neighborsOf(g, x)) {
|
|
184
|
+
const st = state.get(n);
|
|
185
|
+
if (captured.has(n) || st?.owner.kind !== 'player' || !foe(st.owner.playerId) || st.armies === null)
|
|
186
|
+
continue;
|
|
187
|
+
by.set(st.owner.playerId, (by.get(st.owner.playerId) ?? 0) + Math.max(0, st.armies - guard) + unitsAt(n));
|
|
188
|
+
}
|
|
189
|
+
// B2 (T2): a border relaxed for q is sized for no deploy of its, so it is no gamble on where that lands.
|
|
190
|
+
for (const [q, movable] of by)
|
|
191
|
+
if (!relaxedFor(ps, x, q))
|
|
192
|
+
facingBy.set(q, [...(facingBy.get(q) ?? []), { x, movable }]);
|
|
193
|
+
}
|
|
194
|
+
for (const [q, list] of [...facingBy].sort((a, b) => a[0] - b[0])) {
|
|
195
|
+
if (list.length < 2)
|
|
196
|
+
continue;
|
|
197
|
+
const d = deployOf(q);
|
|
198
|
+
const short = list.flatMap(({ x, movable }) => {
|
|
199
|
+
const take = toTake(stayAt(x) + unitsAt(x));
|
|
200
|
+
return take !== null && take > movable && take <= movable + d ? [{ x, gap: take - movable }] : [];
|
|
201
|
+
});
|
|
202
|
+
if (!short.length)
|
|
203
|
+
continue;
|
|
204
|
+
const n = list.length;
|
|
205
|
+
// A deploy can be split: a border whose shortfall the deploy funds together with another
|
|
206
|
+
// border's is no coin flip — the split takes both. How many it takes at once: the smallest
|
|
207
|
+
// shortfalls first, while the deploy covers them.
|
|
208
|
+
const splits = short.filter(({ x, gap }) => short.some((o) => o.x !== x && gap + o.gap <= d));
|
|
209
|
+
const gaps = short.map(({ gap }) => gap).sort((a, b) => a - b);
|
|
210
|
+
let k = 0;
|
|
211
|
+
for (let left = d; k < gaps.length && gaps[k] <= left; k++)
|
|
212
|
+
left -= gaps[k];
|
|
213
|
+
const flips = short.filter((s) => !splits.includes(s));
|
|
214
|
+
const parts = [];
|
|
215
|
+
// #201: the split takes k at once. Each of those holds with chance 0% only when there are
|
|
216
|
+
// k of them or fewer; with more, it picks k of them and each holds with (m − k) ÷ m.
|
|
217
|
+
if (splits.length) {
|
|
218
|
+
const m = splits.length;
|
|
219
|
+
const named = `(${splits.map(({ x, gap }) => `${label(x)} short ${gap}`).join(', ')})`;
|
|
220
|
+
parts.push(m <= k
|
|
221
|
+
? `its ~${d} deploy, split, can take ${k} of the ${n} borders facing it at once ${named}: each of those holds with chance 0%.`
|
|
222
|
+
: `its ~${d} deploy, split, can take ${k} of the ${n} borders facing it at once ${named}: if it picks ${k} of those ${m} at random, each holds with chance ${Math.round(((m - k) / m) * 100)}%, and all of them with 0%.`);
|
|
223
|
+
}
|
|
224
|
+
if (flips.length)
|
|
225
|
+
parts.push(`${splits.length ? 'Besides, its' : 'its'} one ~${d} deploy can land on one of the ${n} borders facing it; ${flips.length} hold${flips.length === 1 ? 's' : ''} against its stacks alone but not with that deploy on ${flips.length === 1 ? 'it' : 'them'} (${flips.map(({ x }) => label(x)).join(', ')}) — a gamble on where it lands: if it picks among the ${n} at random, ${flips.length === 1
|
|
226
|
+
? `it holds with chance ${Math.round(((n - 1) / n) * 100)}%`
|
|
227
|
+
: `each holds with chance ${Math.round(((n - 1) / n) * 100)}%${splits.length ? '' : `, and all of them with ${Math.round(((n - flips.length) / n) * 100)}%`}`}.`);
|
|
228
|
+
rationale.push(`Cover odds against player ${q} (#167): ${parts.join(' ')}`);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
if (noSplit && moves.length && guard)
|
|
232
|
+
warnings.push('No-Split is on: each order sends its territory\'s whole stack less the one army that stands guard; whether war.app leaves that army behind under No-Split is unverified (a live turn with the guard off left every source at 0).');
|
|
233
|
+
return { completed, nextIncome, atRisk, ifFall, reportRisk, frontier };
|
|
234
|
+
}
|
|
235
|
+
/** The offence report (#72), the warnings that follow the deploys, and the "Not attacking" line (#167). */
|
|
236
|
+
export function reportOffence(pc, ps, book, threats, hd, tgt, holdLocked, leadSpareFor) {
|
|
237
|
+
const { g, foe, profile, auto, goal, territories: state, label, mine, mineSet, unitsAt, uncounted, uncountedKind, defenceAt, costAt, toTake, noSplit, leadLever, ffaLeader, leadText, myCommanders, } = pc;
|
|
238
|
+
const { warnings, rationale, deployed, avail, spared, unabsorbed, captured, moves } = ps;
|
|
239
|
+
const { gainWith, orderOf, pools, coverOf } = book;
|
|
240
|
+
// As the deploy book reads it (Local Deployment).
|
|
241
|
+
const local = pools !== null && pools.bonuses.length > 0;
|
|
242
|
+
const { reinforcementAt, forceAt, borders, borderAt } = threats;
|
|
243
|
+
const { conceded, stood, keepFor, stayAt, threatWarnings, twoStepKeep } = hd;
|
|
244
|
+
const { offenceSkip, badTrades, leadTry } = tgt;
|
|
245
|
+
// ---- offence report (#72) ----
|
|
246
|
+
for (const [t, { lost, killed }] of [...badTrades].sort((a, b) => a[0] - b[0]))
|
|
247
|
+
if (!captured.has(t))
|
|
248
|
+
rationale.push(`Not attacking ${label(t)}: the combined capture loses ${lost} armies for the ${killed} it kills, a trade below even while behind on armies.`);
|
|
249
|
+
/** What the final orders send at `t` (a top-up of idle armies can raise a capture's size). */
|
|
250
|
+
const sentAt = (t) => moves.reduce((n, m) => (m.kind === 'attack' && m.to === t ? n + m.armies : n), 0);
|
|
251
|
+
/**
|
|
252
|
+
* #215: a border of mine that holds only with the armies withdrawn into it, which land after the
|
|
253
|
+
* opponent's first attack when it moves first (unless I am known to move first, {@link lateArrivals}):
|
|
254
|
+
* the extra deploy that would hold it whoever moves first, the threat and the player; `null` when
|
|
255
|
+
* it holds either way, falls either way, or is conceded. `atStart` is the threat warned of before
|
|
256
|
+
* the deploys (its worst case, #201), judged once this turn's captures are done (#212).
|
|
257
|
+
*/
|
|
258
|
+
const firstOnlyAt = (id, atStart) => {
|
|
259
|
+
if (!mineSet.has(id) || conceded.has(id))
|
|
260
|
+
return null;
|
|
261
|
+
// Withdrawn into it (rule 1b, counted in what stays there) and, #215 review, idle armies
|
|
262
|
+
// transferred in by rule 6 (never counted in a hold): both my own orders, late in the worse order.
|
|
263
|
+
const known = knownFirst(pc);
|
|
264
|
+
// Known to move first, a withdrawal keeps #127's reading (it is scheduled as my first order).
|
|
265
|
+
if (known && withdrawnInto(ps, id) > 0)
|
|
266
|
+
return null;
|
|
267
|
+
const late = lateArrivals(pc, ps, id);
|
|
268
|
+
const into = moves.filter((m) => m.kind === 'transfer' && m.to === id);
|
|
269
|
+
const idle = Math.max(0, into.reduce((n, m) => n + m.armies, 0) - withdrawnInto(ps, id));
|
|
270
|
+
const after = borderAt(id, (x) => captured.has(x));
|
|
271
|
+
if (late + idle <= 0 || !after)
|
|
272
|
+
return null;
|
|
273
|
+
const threat = Math.min(atStart, after.worst ?? after.threat);
|
|
274
|
+
const base = stayAt(id) - late + unitsAt(id);
|
|
275
|
+
// #215 review: when the opponent moves first, its own orders have not run either (lateNet).
|
|
276
|
+
const own = stayAt(id) - lateNet(pc, ps, id) + unitsAt(id);
|
|
277
|
+
// #215 review: moving first, only my first order runs before the opponent's first (the zigzag),
|
|
278
|
+
// so with idle transfers in, only one move into it can land in time: the largest. (Withdrawals
|
|
279
|
+
// alone keep #127's reading — the withdrawal is scheduled as my first order.)
|
|
280
|
+
const first = idle > 0 ? [...into].sort((a, b) => b.armies - a.armies || a.from - b.from)[0] : null;
|
|
281
|
+
const inTime = first ? first.armies : late;
|
|
282
|
+
if ((toTake(base + inTime) ?? Infinity) <= threat || (toTake(own) ?? Infinity) > threat)
|
|
283
|
+
return null;
|
|
284
|
+
let d = 0;
|
|
285
|
+
while ((toTake(own + d) ?? Infinity) <= threat)
|
|
286
|
+
d++;
|
|
287
|
+
// Known to move first, the one transfer that can land in time is named even when it is alone.
|
|
288
|
+
return { late, idle, first: into.length > 1 || known ? first : null, known, own: own - unitsAt(id), d, threat, player: after.player };
|
|
289
|
+
};
|
|
290
|
+
// A border warned about before the deploys may have been deployed to since: say what the
|
|
291
|
+
// plan leaves there, so the warning does not contradict the hold, floor or spread lines.
|
|
292
|
+
for (let i = 0; i < warnings.length; i++) {
|
|
293
|
+
const hit = [...threatWarnings].find(([, w]) => w.text === warnings[i]);
|
|
294
|
+
if (!hit)
|
|
295
|
+
continue;
|
|
296
|
+
const [id, { threat: atStart }] = hit;
|
|
297
|
+
const n = deployed.get(id) ?? 0;
|
|
298
|
+
if (n <= 0)
|
|
299
|
+
continue;
|
|
300
|
+
const have = stayAt(id);
|
|
301
|
+
const take = toTake(have + unitsAt(id));
|
|
302
|
+
// #212: judged against the threat once this turn's captures are done (a captured stack beside
|
|
303
|
+
// it cannot attack), as the hold and lead-step lines size it — else a border the plan just
|
|
304
|
+
// sized is called short against a stack it takes this turn.
|
|
305
|
+
const after = borderAt(id, (x) => captured.has(x));
|
|
306
|
+
const threat = after ? Math.min(atStart, after.worst ?? after.threat) : 0;
|
|
307
|
+
// #215: one that holds only with the armies withdrawn into it says so (the warning below).
|
|
308
|
+
const firstOnly = firstOnlyAt(id, atStart)?.known === false;
|
|
309
|
+
// The opponents' territories beside it that this turn's attacks take.
|
|
310
|
+
const takenNear = neighborsOf(g, id)
|
|
311
|
+
.filter((x) => captured.has(x) && state.get(x)?.owner.kind === 'player')
|
|
312
|
+
.sort(byNum);
|
|
313
|
+
const lower = threat < atStart && takenNear.length > 0;
|
|
314
|
+
const once = lower ? `once ${takenNear.map(label).join(' and ')} ${takenNear.length === 1 ? 'is' : 'are'} taken` : '';
|
|
315
|
+
warnings[i] += ` Before this turn's deploys: the plan deploys ${n} there, leaving ${have} armies (${take ?? 'none can'} to take${!after
|
|
316
|
+
? `; nothing can reach it${once ? ` ${once}` : ''}: it holds`
|
|
317
|
+
: take !== null && take > threat
|
|
318
|
+
? `, more than the ${threat} that can ${lower ? `still reach it ${once}` : 'reach it'}: it holds${firstOnly ? ' only if I move first' : ''}`
|
|
319
|
+
: `; ${lower ? `even ${once}, ` : ''}the ${threat} that can reach it still could`}).`;
|
|
320
|
+
}
|
|
321
|
+
// #215: every border that holds only if I move first, and what would hold it either way (the
|
|
322
|
+
// leftover sizes such a hold when it can, plan/leftover.ts: one named here it could not afford).
|
|
323
|
+
for (const id of [...borders.keys()].sort(byNum)) {
|
|
324
|
+
const b = borders.get(id);
|
|
325
|
+
const f = firstOnlyAt(id, b.worst ?? b.threat);
|
|
326
|
+
if (!f)
|
|
327
|
+
continue;
|
|
328
|
+
const ownTake = toTake(f.own + unitsAt(id));
|
|
329
|
+
if (f.known && f.first) {
|
|
330
|
+
// #215 review: known to move first, only my first order lands before the opponent's first.
|
|
331
|
+
warnings.push(`${label(id)} holds only if my first order is the ${f.first.armies} from ${label(f.first.from)}: ${f.idle} of its armies are idle armies transferred into it this turn, and I move first, so only my first order runs before player ${f.player}'s first attack (~${f.threat}). Its own ${f.own} need ${ownTake} to take; ${f.d} more deployed there would hold it with no transfer in.`);
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
// #215 review: why the leftover made no deploy there (plan/leftover.ts records it); idle
|
|
335
|
+
// transfers come after it (rule 6), so no deploy was sized for them.
|
|
336
|
+
const skip = f.idle > 0 ? undefined : ps.firstOnlySkip.get(id);
|
|
337
|
+
const fix = skip === 'order'
|
|
338
|
+
? // #215 review: its own count includes the stack its order sends, which still stands when
|
|
339
|
+
// the opponent moves first; deploys there would stand too, then leave with that order.
|
|
340
|
+
`${f.d} more deployed there would hold it whoever moves first (until its order runs: No-Split sends every army there with it, deploys included); the plan sizes no deploy for that on a territory that issues an order`
|
|
341
|
+
: `${f.d} more deployed there would hold it whoever moves first${skip === 'budget'
|
|
342
|
+
? ', more than the plan had left to deploy there'
|
|
343
|
+
: skip === 'room'
|
|
344
|
+
? ', more than Local Deployment lets the plan deploy there'
|
|
345
|
+
: ''}`;
|
|
346
|
+
const moved = f.idle <= 0
|
|
347
|
+
? `${f.late} of its armies are withdrawn into it`
|
|
348
|
+
: f.late <= 0
|
|
349
|
+
? `${f.idle} of its armies are idle armies transferred into it`
|
|
350
|
+
: `${f.late + f.idle} of its armies are withdrawn or transferred into it`;
|
|
351
|
+
// #215 review: a first move only the award of picks says I have is the wiki's rule, unverified.
|
|
352
|
+
// An opponent's Order Priority would put its first order before my first (#215 review).
|
|
353
|
+
const award = pc.opts.movesFirst === true && pc.opts.movesFirstUnverified
|
|
354
|
+
? " (the award of picks says I move first: the wiki's rule, not verified live)"
|
|
355
|
+
: pc.opts.movesFirst === true && pc.opts.opponentsMayHoldOrderPriority
|
|
356
|
+
? ' (I move first, but an opponent may hold an Order Priority card, which puts its first order before mine)'
|
|
357
|
+
: '';
|
|
358
|
+
warnings.push(`${label(id)} holds only if I move first: ${moved} this turn, and ${pc.opts.movesFirst === false ? `player ${f.player} moves first, so its attack` : `if player ${f.player} moves first its attack`} (~${f.threat}) lands before them${award}${f.first ? `; moving first, only my first order runs before its first, so it holds only if that is the ${f.first.armies} from ${label(f.first.from)}` : ''}. Its own ${f.own} need ${ownTake} to take; ${fix}.`);
|
|
359
|
+
}
|
|
360
|
+
// #235: the leader's borders sized for the strongest single opponent, when the plan keeps them.
|
|
361
|
+
// A held bonus's members share one line (#235 review): it is said once.
|
|
362
|
+
const soloSaid = new Set();
|
|
363
|
+
for (const [id, line] of [...ps.soloHeld].sort((a, b) => a[0] - b[0]))
|
|
364
|
+
if (!conceded.has(id) && mineSet.has(id) && !soloSaid.has(line)) {
|
|
365
|
+
soloSaid.add(line);
|
|
366
|
+
rationale.push(line);
|
|
367
|
+
}
|
|
368
|
+
// #201: the FFA leader's borders held against two or more opponents attacking together.
|
|
369
|
+
if (ffaLeader)
|
|
370
|
+
for (const id of [...borders.keys()].sort(byNum)) {
|
|
371
|
+
if (conceded.has(id) || !mineSet.has(id) || ps.soloHeld.has(id))
|
|
372
|
+
continue;
|
|
373
|
+
const f = forceAt(id, (n) => captured.has(n), { trend: true, allPlayers: true });
|
|
374
|
+
if (!f?.split || f.split.players.length < 2)
|
|
375
|
+
continue;
|
|
376
|
+
rationale.push(`I lead this free-for-all (#201): ${label(id)} is held against players ${f.split.players.join(' and ')} attacking together (${f.split.attacks.join(' then ')}, up to ${f.split.takes} armies), not each alone: the others may combine against the leader.`);
|
|
377
|
+
}
|
|
378
|
+
// #128: a border that holds against each opponent alone but not against two of them attacking
|
|
379
|
+
// it in the same turn (FFA players do not coordinate, so this is a warning, not a hold).
|
|
380
|
+
for (const id of [...borders.keys()].sort(byNum)) {
|
|
381
|
+
if (conceded.has(id) || !mineSet.has(id))
|
|
382
|
+
continue;
|
|
383
|
+
const alone = borderAt(id, (n) => captured.has(n));
|
|
384
|
+
const pile = forceAt(id, (n) => captured.has(n), { trend: true, allPlayers: true, pileOn: true });
|
|
385
|
+
if (!alone || !pile?.split || pile.split.players.length < 2)
|
|
386
|
+
continue;
|
|
387
|
+
const have = stayAt(id) + unitsAt(id);
|
|
388
|
+
const take = toTake(have);
|
|
389
|
+
if (take === null || take <= alone.threat || have > pile.split.takes)
|
|
390
|
+
continue;
|
|
391
|
+
warnings.push(`${label(id)} holds against each opponent alone, but players ${pile.split.players.join(' and ')} attacking it in the same turn (${pile.split.attacks.join(' then ')}) could take up to ${pile.split.takes} armies, and ${have} stay there.`);
|
|
392
|
+
}
|
|
393
|
+
// ---- #167: every enemy territory next to mine left alone names why ----
|
|
394
|
+
// One rationale line lists each visible enemy territory next to a territory I held at the start
|
|
395
|
+
// that no order attacks, with the first reason that applies: a special unit there, a reason the
|
|
396
|
+
// offence report already gave (rule 0p.2's second front among them), a capture that lowers income,
|
|
397
|
+
// its size against the most any neighbour of mine can spare once it holds its border (#215: a conceded
|
|
398
|
+
// one, its garrison not standing, offers what it can still send once its orders leave), or that it is
|
|
399
|
+
// no target of the plan's (rule 2: a player's territory is attacked to break a bonus, counter-attack,
|
|
400
|
+
// finish a player off, strike, or as rule 4e's fallback; adapting in an FFA, the lead strikes).
|
|
401
|
+
{
|
|
402
|
+
/** #215: a conceded border whose garrison does not stand (rule 1b): it keeps nothing back (my commander's never is: rule 1k). */
|
|
403
|
+
const givenUp = (x) => conceded.has(x) && !stood.has(x);
|
|
404
|
+
const spareNow = (x, n) => {
|
|
405
|
+
if (!mineSet.has(x) || myCommanders.has(x) || orderOf(x))
|
|
406
|
+
return 0;
|
|
407
|
+
// #215: a conceded border (its garrison not standing) holds nothing: what it can still send
|
|
408
|
+
// once its orders leave (an attack out, rule 1b's withdrawal) is free — every army on it but
|
|
409
|
+
// what the guard-off cover keeps there (coverOf), so it is below what stays on it.
|
|
410
|
+
if (givenUp(x))
|
|
411
|
+
return Math.max(0, avail.get(x) - coverOf(x));
|
|
412
|
+
if (holdLocked.has(x))
|
|
413
|
+
return 0;
|
|
414
|
+
const gone = (m) => m === n;
|
|
415
|
+
return Math.max(0, avail.get(x) - Math.max(keepFor(x, gone), twoStepKeep(x, gone)));
|
|
416
|
+
};
|
|
417
|
+
const attacked = new Set(moves.filter((m) => m.kind === 'attack').map((m) => m.to));
|
|
418
|
+
const left = [...new Set(mine.flatMap((x) => neighborsOf(g, x)))].sort(byNum).filter((n) => {
|
|
419
|
+
const st = state.get(n);
|
|
420
|
+
return !mineSet.has(n) && !attacked.has(n) && st?.owner.kind === 'player' && foe(st.owner.playerId) && st.armies !== null;
|
|
421
|
+
});
|
|
422
|
+
const reasonFor = (n) => {
|
|
423
|
+
const st = state.get(n);
|
|
424
|
+
const p = st.owner.playerId;
|
|
425
|
+
if (uncounted(n))
|
|
426
|
+
return `it holds ${uncountedKind(n)}, whose strength the board does not carry`;
|
|
427
|
+
// B2 (T1): the truce covers it.
|
|
428
|
+
if (pc.truce.noTarget(n))
|
|
429
|
+
return `my truce with player ${p} covers it (T1)`;
|
|
430
|
+
if (unitsAt(n) > 0)
|
|
431
|
+
return `an enemy commander stands there: only a commander strike (rule 1s) attacks it, sized for its owner's whole deploy`;
|
|
432
|
+
if (spared.has(n) && unabsorbed.has(p))
|
|
433
|
+
return `absorbing player ${p} would not bring my income to the FFA leader's: the armies are better spent against the leader (#201)`;
|
|
434
|
+
if (spared.has(n))
|
|
435
|
+
return `player ${p} is a buffer in this FFA: finishing it off costs more frontier than it pays (#179)`;
|
|
436
|
+
const skip = offenceSkip.get(n);
|
|
437
|
+
if (skip)
|
|
438
|
+
return skip;
|
|
439
|
+
if (gainWith([n]) < 0)
|
|
440
|
+
return 'taking it would lower my income (it completes a negative bonus)';
|
|
441
|
+
const need = costAt(n);
|
|
442
|
+
const extra = reinforcementAt(n);
|
|
443
|
+
const sure = extra > 0 ? costAt(n, defenceAt(n) + extra) : need;
|
|
444
|
+
// #203: why its bonus's step failed, as measured when that pass tried it (later deploys,
|
|
445
|
+
// counted in the spares below, could not fund it then). #212: said whichever line follows,
|
|
446
|
+
// so a step tried and refused never reads as standing armies alone.
|
|
447
|
+
const tried = leadTry.get(n);
|
|
448
|
+
const deployText = tried === undefined
|
|
449
|
+
? ''
|
|
450
|
+
: tried.kind === 'r3'
|
|
451
|
+
? `; its bonus's step was refused: what survives the attack would not hold it next turn (R3)`
|
|
452
|
+
: tried.kind === 'trade'
|
|
453
|
+
? `; its bonus's step was refused: a trade below even (~${tried.lost} lost for ${tried.killed} killed)`
|
|
454
|
+
: tried.kind === 'frontier'
|
|
455
|
+
? `; its bonus's step was stopped by rule 4d's frontier budget: it adds a border facing an enemy`
|
|
456
|
+
: tried.from === undefined
|
|
457
|
+
? `; when its bonus's step was tried, no neighbour of mine was free to attack it${tried.blocked?.length
|
|
458
|
+
? ` (${tried.blocked
|
|
459
|
+
.map(({ from, why }) => `${label(from)} ${BLOCKED[why]}`)
|
|
460
|
+
.join('; ')})`
|
|
461
|
+
: ''}`
|
|
462
|
+
: tried.hold !== undefined
|
|
463
|
+
? `; when its bonus's step was tried, ${label(tried.from)} holds its border, and with the step gone its hold needed ${tried.hold} more than it had; this turn's deploy could add at most ${tried.reach} there${local ? ' (Local Deployment)' : ''}: ${Math.max(0, tried.reach - tried.hold)} left for the attack`
|
|
464
|
+
: `; when its bonus's step was tried, ${label(tried.from)} could spare ${tried.spare} for it and ${tried.reach === 0
|
|
465
|
+
? 'none of this turn\'s deploy could still reach it'
|
|
466
|
+
: `this turn's deploy could add at most ${tried.reach} there${local ? ' (Local Deployment)' : ''}: ${tried.spare + tried.reach} in all`}`;
|
|
467
|
+
const best = neighborsOf(g, n)
|
|
468
|
+
.filter((x) => mineSet.has(x))
|
|
469
|
+
.map((x) => ({ x, spare: spareNow(x, n) }))
|
|
470
|
+
.sort((a, b) => b.spare - a.spare || a.x - b.x)[0];
|
|
471
|
+
if (need === null)
|
|
472
|
+
return 'no attack can take it at these kill rates';
|
|
473
|
+
if (best.spare < need)
|
|
474
|
+
return `taking it needs ${need}${sure !== null && sure > need ? ` (${sure} if player ${p} deploys its ~${extra} there first)` : ''}, and ${givenUp(best.x)
|
|
475
|
+
? `the most a neighbour of mine has left for it is ${best.spare} (${label(best.x)}, conceded: ${orderOf(best.x)
|
|
476
|
+
? `No-Split sends its whole stack on its other order`
|
|
477
|
+
: 'what it can still send once its other orders leave'})`
|
|
478
|
+
: `the most a neighbour of mine can spare once it holds its border is ${best.spare}${best.spare > 0 ? ` (${label(best.x)})` : ''}`}${deployText}`;
|
|
479
|
+
if (goal !== 'balanced') {
|
|
480
|
+
// Rule 4e's fallback (and a lead) can still take a player's territory under another goal.
|
|
481
|
+
const others = [...attacked].filter((t) => t !== n && state.get(t)?.owner.kind === 'player').sort(byNum);
|
|
482
|
+
return others.length
|
|
483
|
+
? `under goal ${goal} the plan attacks a player's territory only where a rule calls for it (here ${others.map(label).join(', ')}), and none calls for this one`
|
|
484
|
+
: `goal ${goal} attacks no player's territory${goal === 'complete-bonus' ? ' outside the bonus it completes' : ''}`;
|
|
485
|
+
}
|
|
486
|
+
const ffaLead = auto && profile.format === 'ffa' && profile.opponents >= 2;
|
|
487
|
+
if (ffaLead && leadLever && !noSplit && sure !== null && sure > need) {
|
|
488
|
+
// The lead strikes ran: this one was not sized for sure (defenders plus its owner's deploy).
|
|
489
|
+
const lead = neighborsOf(g, n)
|
|
490
|
+
.filter((x) => mineSet.has(x))
|
|
491
|
+
.map((x) => ({ x, spare: leadSpareFor(x, n) }))
|
|
492
|
+
.sort((a, b) => b.spare - a.spare || a.x - b.x)[0];
|
|
493
|
+
if (lead.spare < sure)
|
|
494
|
+
return `a lead strike (my army lead is ${leadText()}, over ${SECOND_FRONT_LEAD}×) needs ${sure} — sized for its ${defenceAt(n)} defenders plus player ${p}'s ~${extra} deploy — and the most a neighbour of mine can spare for it is ${lead.spare}${lead.spare > 0 ? ` (${label(lead.x)})` : ''}${deployText}`;
|
|
495
|
+
}
|
|
496
|
+
return `${label(best.x)} could spare ${best.spare} for it, but it breaks no bonus and is no counter-attack, finishing blow or strike — the only reasons the plan attacks a player's territory (rule 2), besides rule 4e's fallback when nothing else is captured${!ffaLead
|
|
497
|
+
? ''
|
|
498
|
+
: !leadLever
|
|
499
|
+
? ` (a lead strike takes one only with an army lead over ${SECOND_FRONT_LEAD}×; mine is ${leadText()})`
|
|
500
|
+
: noSplit
|
|
501
|
+
? ` (no lead strike under No-Split: an order carries the whole stack)`
|
|
502
|
+
: ''}`;
|
|
503
|
+
};
|
|
504
|
+
/** #253: why rule 4h, the army cap's spend of idle armies, did not take it either. */
|
|
505
|
+
const capNote = (n) => {
|
|
506
|
+
const why = ps.capRefused.get(n);
|
|
507
|
+
return why ? `; the army cap's spend of idle armies (rule 4h) tried it: it ${why}` : '';
|
|
508
|
+
};
|
|
509
|
+
// Capped (#130 trimmed the response before): the first NOT_ATTACKING_REASONS by id with their
|
|
510
|
+
// reasons, the rest named.
|
|
511
|
+
const told = left.slice(0, NOT_ATTACKING_REASONS);
|
|
512
|
+
const rest = left.slice(NOT_ATTACKING_REASONS);
|
|
513
|
+
if (left.length)
|
|
514
|
+
rationale.push(`Not attacking ${left.length === 1 ? 'the enemy territory' : `${left.length} enemy territories`} next to mine: ${told
|
|
515
|
+
.map((n) => `${label(n)} (player ${state.get(n).owner.playerId}, ${state.get(n).armies} armies): ${reasonFor(n)}${capNote(n)}`)
|
|
516
|
+
.join('; ')}${rest.length ? `; and ${rest.length} more: ${rest.map(label).join(', ')}` : ''}.`);
|
|
517
|
+
}
|
|
518
|
+
return { sentAt };
|
|
519
|
+
}
|
|
520
|
+
/** The plan: the offence list, #172's final check, the retaliation warning, the exchanges and the ETAs. */
|
|
521
|
+
export function finishPlan(pc, ps, pass, threats, hd, tgt, expected, sentAt, slotOf, facesEnemy, exits) {
|
|
522
|
+
const { g, s, income, foe, profile, goal, enemyDeploy, deployOf, territories: state, label, mine, armiesOf, unitsAt, defenceAt, guard, rates, toTake, } = pc;
|
|
523
|
+
const { warnings, rationale, deployed, captured, combined, moves } = ps;
|
|
524
|
+
const { reinforcementAt, borders, ownedBy, ownerOf, fogEdge } = threats;
|
|
525
|
+
const { conceded, stayAt } = hd;
|
|
526
|
+
const { offence, offenceSkip } = tgt;
|
|
527
|
+
const { completed, nextIncome, atRisk, ifFall, reportRisk, frontier } = expected;
|
|
528
|
+
const offenceList = [...offence.values()].map(({ bonusId, ...o }) => {
|
|
529
|
+
// A strike attack's cost is what the final orders send (a top-up can raise it).
|
|
530
|
+
// The strike stack's own entry is final as built.
|
|
531
|
+
if (o.kind === 'strike')
|
|
532
|
+
return o;
|
|
533
|
+
if (o.kind === 'strike-attack')
|
|
534
|
+
return o.taken && captured.has(o.target) ? { ...o, cost: sentAt(o.target) || o.cost } : o;
|
|
535
|
+
// A bonus broken this turn is a break made, whichever member fell and whatever path took it.
|
|
536
|
+
const via = captured.has(o.target)
|
|
537
|
+
? o.target
|
|
538
|
+
: bonusId !== undefined
|
|
539
|
+
? bonusTerritories(g, bonusId).filter((x) => captured.has(x)).sort(byNum)[0]
|
|
540
|
+
: undefined;
|
|
541
|
+
if (via !== undefined)
|
|
542
|
+
return {
|
|
543
|
+
...o,
|
|
544
|
+
target: via,
|
|
545
|
+
cost: sentAt(via) || o.cost,
|
|
546
|
+
taken: true,
|
|
547
|
+
why: via === o.target ? o.why : `already broken by the capture of ${label(via)}: ${o.why}`,
|
|
548
|
+
};
|
|
549
|
+
return {
|
|
550
|
+
...o,
|
|
551
|
+
taken: false,
|
|
552
|
+
why: `not made — ${offenceSkip.get(o.target) ?? 'other uses of the income scored higher per army, or it could not be afforded'}: ${o.why}`,
|
|
553
|
+
};
|
|
554
|
+
});
|
|
555
|
+
// #172: how each territory in between that a two-step hold judged ends — kept (not conceded)
|
|
556
|
+
// and holding its own border with what the final orders leave there (its armies, deploys and
|
|
557
|
+
// every transfer in, less every order out) — for settledPass to check the pass against.
|
|
558
|
+
for (const v of pass.viaSeen.keys()) {
|
|
559
|
+
const b = borders.get(v);
|
|
560
|
+
const ends = moves.reduce((n, m) => n - (m.from === v ? m.armies : 0) + (m.kind === 'transfer' && m.to === v ? m.armies : 0), armiesOf(v) + (deployed.get(v) ?? 0));
|
|
561
|
+
pass.viaFinal.set(v, !conceded.has(v) && b !== undefined && (toTake(ends + unitsAt(v)) ?? Infinity) > b.threat);
|
|
562
|
+
}
|
|
563
|
+
// ---- #201: a break without an elimination invites retaliation (FFA) ----
|
|
564
|
+
// In an FFA a player whose bonus I break and leave standing aims what it has left at me; in a
|
|
565
|
+
// 1v1 it does anyway, so no warning there.
|
|
566
|
+
if (profile.format === 'ffa' && profile.opponents >= 2) {
|
|
567
|
+
for (const [p, ts] of [...ownedBy].sort((a, b) => a[0] - b[0])) {
|
|
568
|
+
if (!foe(p))
|
|
569
|
+
continue;
|
|
570
|
+
const broken = g.map.bonuses
|
|
571
|
+
.filter((b) => b.value > 0)
|
|
572
|
+
.filter((b) => {
|
|
573
|
+
const members = bonusTerritories(g, b.id);
|
|
574
|
+
return members.length > 0 && members.every((t) => ownerOf(t) === p) && members.some((t) => captured.has(t));
|
|
575
|
+
})
|
|
576
|
+
.sort((a, b) => a.id - b.id);
|
|
577
|
+
if (!broken.length)
|
|
578
|
+
continue;
|
|
579
|
+
const left = ts.filter((t) => !captured.has(t));
|
|
580
|
+
if (!left.length && !fogEdge(p).length)
|
|
581
|
+
continue;
|
|
582
|
+
const armies = left.reduce((n, t) => n + armiesOf(t), 0);
|
|
583
|
+
const value = broken.reduce((n, b) => n + b.value, 0);
|
|
584
|
+
warnings.push(`Breaking player ${p}'s bonus${broken.length === 1 ? '' : 'es'} ${broken.map((b) => `${b.name} (+${b.value})`).join(', ')} without eliminating it invites retaliation (#201): it keeps ${left.length} territor${left.length === 1 ? 'y' : 'ies'} in view (${armies} armies)${fogEdge(p).length ? ' and more in the fog' : ''} and a ~${deployOf(p)} deploy, and loses +${value} a turn to me — in a free-for-all what it has left is likeliest aimed at whoever broke it.`);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
// ---- #167: the exchange of each attack on a player ----
|
|
588
|
+
const exchanges = moves.flatMap((m) => {
|
|
589
|
+
const st = state.get(m.to);
|
|
590
|
+
if (m.kind !== 'attack' || st?.owner.kind !== 'player' || !foe(st.owner.playerId) || st.armies === null)
|
|
591
|
+
return [];
|
|
592
|
+
const p = st.owner.playerId;
|
|
593
|
+
const deploy = reinforcementAt(m.to);
|
|
594
|
+
// Its stack can hit my source: what stays there against its movable armies plus its deploy.
|
|
595
|
+
const take = toTake(stayAt(m.from) + unitsAt(m.from));
|
|
596
|
+
const threatensSource = take !== null && take <= Math.max(0, st.armies - guard) + unitsAt(m.to) + deployOf(p);
|
|
597
|
+
const x = classifyExchange({
|
|
598
|
+
attackers: m.armies,
|
|
599
|
+
defenders: defenceAt(m.to),
|
|
600
|
+
deploy,
|
|
601
|
+
income: Math.max(deploy, deployOf(p)),
|
|
602
|
+
threatensSource,
|
|
603
|
+
rates,
|
|
604
|
+
});
|
|
605
|
+
return [{ from: m.from, to: m.to, armies: m.armies, player: p, ...x }];
|
|
606
|
+
});
|
|
607
|
+
if (exchanges.length)
|
|
608
|
+
rationale.push(`Exchange of attacks on players (#167): ${exchanges
|
|
609
|
+
.map((e) => {
|
|
610
|
+
const [now, est, whole] = e.cases;
|
|
611
|
+
const at = (c) => `${c.captures ? 'takes' : `${c.net >= 0 ? '+' : ''}${c.net}`} against ${c.defenders}`;
|
|
612
|
+
return `${label(e.to)} from ${label(e.from)} with ${e.armies}: ${e.label} (${at(now)}, ${at(est)} after the estimated deploy, ${at(whole)} after the whole income)`;
|
|
613
|
+
})
|
|
614
|
+
.join('; ')}.`);
|
|
615
|
+
// ---- #179: turns to eliminate each visible opponent ----
|
|
616
|
+
const eliminateEta = {};
|
|
617
|
+
{
|
|
618
|
+
const dist = new Map(mine.map((t) => [t, 0]));
|
|
619
|
+
const queue = [...mine];
|
|
620
|
+
for (let i = 0; i < queue.length; i++) {
|
|
621
|
+
const x = queue[i];
|
|
622
|
+
for (const n of neighborsOf(g, x))
|
|
623
|
+
if (!dist.has(n)) {
|
|
624
|
+
dist.set(n, dist.get(x) + 1);
|
|
625
|
+
queue.push(n);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
const movable = mine.reduce((n, x) => n + Math.max(0, armiesOf(x) - guard), 0);
|
|
629
|
+
for (const [p, ts] of [...ownedBy].sort((a, b) => a[0] - b[0])) {
|
|
630
|
+
const distance = Math.max(...ts.map((t) => dist.get(t) ?? Infinity));
|
|
631
|
+
const base = ts.reduce((n, t) => n + (captureCost(defenceAt(t), s) ?? Infinity), 0);
|
|
632
|
+
if (!Number.isFinite(distance) || !Number.isFinite(base))
|
|
633
|
+
continue;
|
|
634
|
+
const need = (k) => base + (k * deployOf(p) > 0 ? (captureCost(k * deployOf(p), s) ?? Infinity) : 0);
|
|
635
|
+
let turns = null;
|
|
636
|
+
for (let k = Math.max(1, distance); k <= ETA_HORIZON && turns === null; k++)
|
|
637
|
+
if (need(k) <= movable + income * k)
|
|
638
|
+
turns = k;
|
|
639
|
+
const armies = need(turns ?? Math.max(1, distance));
|
|
640
|
+
eliminateEta[p] = { turns, atLeast: fogEdge(p).length > 0, distance, armies: Number.isFinite(armies) ? armies : null };
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
return {
|
|
644
|
+
goal,
|
|
645
|
+
profile,
|
|
646
|
+
// Deploys interleave like moves, and the order they land in shows the opponent my income and
|
|
647
|
+
// the move order: next to a visible enemy first, in the back after (ascending id within each).
|
|
648
|
+
deploys: [...deployed]
|
|
649
|
+
.filter(([, n]) => n > 0)
|
|
650
|
+
.sort((a, b) => Number(facesEnemy(b[0])) - Number(facesEnemy(a[0])) || a[0] - b[0])
|
|
651
|
+
.map(([territoryId, armies]) => ({ territoryId, armies })),
|
|
652
|
+
moves,
|
|
653
|
+
expected: {
|
|
654
|
+
captures: [...captured].sort(byNum),
|
|
655
|
+
bonusesCompleted: completed,
|
|
656
|
+
nextIncome,
|
|
657
|
+
...(reportRisk ? { nextIncomeIfConcededFall: ifFall, bonusesAtRisk: atRisk } : {}),
|
|
658
|
+
},
|
|
659
|
+
warnings,
|
|
660
|
+
rationale,
|
|
661
|
+
enemyDeploy,
|
|
662
|
+
conceded: [...conceded].sort(byNum),
|
|
663
|
+
cardAdvice: ps.cardAdvice,
|
|
664
|
+
multiSourceCaptures: [...combined].sort(byNum),
|
|
665
|
+
frontier,
|
|
666
|
+
offence: offenceList,
|
|
667
|
+
moveSlots: moves.map((m) => SLOT_NAMES[slotOf(m)]),
|
|
668
|
+
...(exchanges.length ? { exchanges } : {}),
|
|
669
|
+
...(Object.keys(eliminateEta).length ? { eta: { eliminate: eliminateEta } } : {}),
|
|
670
|
+
...(exits.length ? { exits } : {}),
|
|
671
|
+
};
|
|
672
|
+
}
|