@chrischall/warapp-mcp 0.0.0-stage → 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 +34 -0
- package/.claude-plugin/plugin.json +21 -0
- package/.mcp.json +11 -0
- package/LICENSE +21 -0
- package/README.md +264 -2
- 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 +143479 -0
- 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 +247 -0
- package/mint.yaml +140 -0
- package/package.json +68 -4
- package/server.json +72 -0
- package/skills/warapp-api/SKILL.md +226 -0
- package/skills/warapp-api/references/scout.mjs +348 -0
- package/skills/warapp-api/scripts/warapp.sh +228 -0
- package/skills/warapp-mcp/SKILL.md +1011 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
// Attack arithmetic under straight rounding and no luck.
|
|
2
|
+
export const DEFAULT_OFFENSIVE_KILL_RATE = 60;
|
|
3
|
+
export const DEFAULT_DEFENSIVE_KILL_RATE = 70;
|
|
4
|
+
/**
|
|
5
|
+
* Fewest attacking armies that take a territory held by `defenders` armies,
|
|
6
|
+
* assuming straight rounding and 0% luck: enough to kill every defender
|
|
7
|
+
* (`round(a·off) ≥ d`) with at least one survivor to move in
|
|
8
|
+
* (`a − round(d·def) ≥ 1`). `null` when attackers cannot kill at all.
|
|
9
|
+
*/
|
|
10
|
+
export function armiesToTake(defenders, rates = {}) {
|
|
11
|
+
// Both terms are the luck-free share `expectedKills` rounded half up, solved for exactly.
|
|
12
|
+
const off = rates.offensiveKillRate ?? DEFAULT_OFFENSIVE_KILL_RATE;
|
|
13
|
+
const def = rates.defensiveKillRate ?? DEFAULT_DEFENSIVE_KILL_RATE;
|
|
14
|
+
if (defenders <= 0)
|
|
15
|
+
return 1;
|
|
16
|
+
if (off <= 0)
|
|
17
|
+
return null;
|
|
18
|
+
// round(a·off/100) ≥ d ⇔ a·off ≥ 100d − 50 (integer-exact for integer rates).
|
|
19
|
+
const toKill = Math.ceil((100 * defenders - 50) / off);
|
|
20
|
+
const lost = straightKills(defenders, def);
|
|
21
|
+
return Math.max(toKill, lost + 1);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The luck-free kill share of `n` armies at `ratePct`%: `n·rate/100`, computed in that order
|
|
25
|
+
* so a share that is exactly .5 stays .5 (`n * (rate / 100)` reads 45 at 70% as
|
|
26
|
+
* 31.499999999999996 and rounds it down). Every kill count — {@link armiesToTake},
|
|
27
|
+
* {@link winChance}, {@link guaranteedKills} — starts from this, so they always agree.
|
|
28
|
+
*/
|
|
29
|
+
export function expectedKills(n, ratePct) {
|
|
30
|
+
return (n * ratePct) / 100;
|
|
31
|
+
}
|
|
32
|
+
/** {@link expectedKills} rounded half up: the kills of a luck-free straight-rounded round. */
|
|
33
|
+
export function straightKills(n, ratePct) {
|
|
34
|
+
return Math.round(expectedKills(n, ratePct));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A commander fights as this many armies, attacking and defending (the client bundle's
|
|
38
|
+
* `Commander`: attack and defence power `a + 7`). It is never partially damaged — it dies
|
|
39
|
+
* only to 7 damage left over after the armies with it are all killed (armies take damage
|
|
40
|
+
* first; its combat order is last) — so a stack of `d` armies plus a commander falls to
|
|
41
|
+
* exactly the attack that takes `d + 7` armies.
|
|
42
|
+
*/
|
|
43
|
+
export const COMMANDER_STRENGTH = 7;
|
|
44
|
+
/**
|
|
45
|
+
* The armies the special units on a territory fight as: {@link COMMANDER_STRENGTH} per
|
|
46
|
+
* commander. Bosses and custom units are not counted (their strength is per-unit data the
|
|
47
|
+
* board does not carry).
|
|
48
|
+
*/
|
|
49
|
+
export function specialUnitStrength(units) {
|
|
50
|
+
return (units ?? []).reduce((n, u) => n + (u.kind === 'commander' ? COMMANDER_STRENGTH : 0), 0);
|
|
51
|
+
}
|
|
52
|
+
/** A luck modifier as a fraction in [0, 0.95] (a value above 1 is read as a percent). */
|
|
53
|
+
export function luckFraction(v) {
|
|
54
|
+
if (!v || v <= 0)
|
|
55
|
+
return 0;
|
|
56
|
+
const f = v > 1 ? v / 100 : v;
|
|
57
|
+
return Math.min(f, 0.95);
|
|
58
|
+
}
|
|
59
|
+
const weighted = (rounding) => rounding === 'WeightedRandom';
|
|
60
|
+
/** ln(k!) for k = 0..n, memoised. */
|
|
61
|
+
const logFact = [0];
|
|
62
|
+
function lnFact(k) {
|
|
63
|
+
for (let i = logFact.length; i <= k; i++)
|
|
64
|
+
logFact.push(logFact[i - 1] + Math.log(i));
|
|
65
|
+
return logFact[k];
|
|
66
|
+
}
|
|
67
|
+
function binomial(n, k, p) {
|
|
68
|
+
if (p <= 0)
|
|
69
|
+
return k === 0 ? 1 : 0;
|
|
70
|
+
if (p >= 1)
|
|
71
|
+
return k === n ? 1 : 0;
|
|
72
|
+
return Math.exp(lnFact(n) - lnFact(k) - lnFact(n - k) + k * Math.log(p) + (n - k) * Math.log(1 - p));
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The probability that `n` armies at `ratePct`% kill at least `k` (`atLeast`) or at most `k`
|
|
76
|
+
* (`!atLeast`). Summed over the binomial outcomes as 1 − (the failing mass), so a certain
|
|
77
|
+
* outcome is exactly 1 and an impossible one exactly 0.
|
|
78
|
+
*/
|
|
79
|
+
function killProbability(n, ratePct, luck, rounding, k, atLeast) {
|
|
80
|
+
const r = ratePct / 100;
|
|
81
|
+
const f = expectedKills(n, ratePct);
|
|
82
|
+
/** P(the kills meet the bound | the expected-plus-random share is x). */
|
|
83
|
+
const meets = (x) => {
|
|
84
|
+
if (weighted(rounding)) {
|
|
85
|
+
const lo = Math.floor(x);
|
|
86
|
+
const frac = x - lo;
|
|
87
|
+
// biome-ignore lint/complexity/useMathMinMax: a NaN fraction must give 0, which Math.max would not.
|
|
88
|
+
const pUp = frac > 0 ? frac : 0;
|
|
89
|
+
// kills = lo + 1 with pUp, else lo.
|
|
90
|
+
const okLo = atLeast ? lo >= k : lo <= k;
|
|
91
|
+
const okHi = atLeast ? lo + 1 >= k : lo + 1 <= k;
|
|
92
|
+
return (okLo ? 1 - pUp : 0) + (okHi ? pUp : 0);
|
|
93
|
+
}
|
|
94
|
+
const kills = Math.round(x);
|
|
95
|
+
return (atLeast ? kills >= k : kills <= k) ? 1 : 0;
|
|
96
|
+
};
|
|
97
|
+
if (luck === 0)
|
|
98
|
+
return meets(f);
|
|
99
|
+
let fail = 0;
|
|
100
|
+
let pass = 0;
|
|
101
|
+
for (let g = 0; g <= n; g++) {
|
|
102
|
+
const m = meets(f + (g - f) * luck);
|
|
103
|
+
const p = binomial(n, g, r);
|
|
104
|
+
if (m < 1)
|
|
105
|
+
fail += p * (1 - m);
|
|
106
|
+
if (m > 0)
|
|
107
|
+
pass += p * m;
|
|
108
|
+
}
|
|
109
|
+
if (fail === 0)
|
|
110
|
+
return 1;
|
|
111
|
+
if (pass === 0)
|
|
112
|
+
return 0;
|
|
113
|
+
return Math.min(1, Math.max(0, 1 - fail));
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The exact probability that `attackers` armies capture a territory held by `defenders`:
|
|
117
|
+
* every defender dies and at least one attacker survives. Summed over the binomial
|
|
118
|
+
* distributions — no simulation, no randomness. Exactly 1 when the capture is certain
|
|
119
|
+
* (0% luck under straight rounding is always 0 or 1).
|
|
120
|
+
*/
|
|
121
|
+
export function winChance(attackers, defenders, settings = {}) {
|
|
122
|
+
if (attackers <= 0)
|
|
123
|
+
return 0;
|
|
124
|
+
if (defenders <= 0)
|
|
125
|
+
return 1;
|
|
126
|
+
const off = settings.offensiveKillRate ?? DEFAULT_OFFENSIVE_KILL_RATE;
|
|
127
|
+
const def = settings.defensiveKillRate ?? DEFAULT_DEFENSIVE_KILL_RATE;
|
|
128
|
+
if (off <= 0)
|
|
129
|
+
return 0;
|
|
130
|
+
const luck = luckFraction(settings.luck);
|
|
131
|
+
const kill = killProbability(attackers, off, luck, settings.rounding, defenders, true);
|
|
132
|
+
if (kill === 0)
|
|
133
|
+
return 0;
|
|
134
|
+
const survive = killProbability(defenders, def, luck, settings.rounding, attackers - 1, false);
|
|
135
|
+
return kill * survive;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* The kills `n` armies at `ratePct`% make whatever the dice: the expected share
|
|
139
|
+
* `(1 − luck)·n·rate` alone, rounded half up (straight) or down (weighted-random, which only
|
|
140
|
+
* guarantees the floor).
|
|
141
|
+
*/
|
|
142
|
+
export function guaranteedKills(n, ratePct, luck = 0, rounding = 'StraightRound') {
|
|
143
|
+
if (n <= 0 || ratePct <= 0)
|
|
144
|
+
return 0;
|
|
145
|
+
const x = (1 - luckFraction(luck)) * expectedKills(n, ratePct);
|
|
146
|
+
return weighted(rounding) ? Math.floor(x) : Math.round(x);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* The smallest attack larger than `n` that guarantees one more kill at `ratePct`%;
|
|
150
|
+
* `null` when no size ever kills (a zero rate).
|
|
151
|
+
*/
|
|
152
|
+
export function nextBreakpoint(n, ratePct, luck = 0, rounding = 'StraightRound') {
|
|
153
|
+
if (ratePct <= 0 || luckFraction(luck) >= 1)
|
|
154
|
+
return null;
|
|
155
|
+
const now = guaranteedKills(Math.max(0, n), ratePct, luck, rounding);
|
|
156
|
+
let m = Math.max(0, n) + 1;
|
|
157
|
+
while (guaranteedKills(m, ratePct, luck, rounding) <= now)
|
|
158
|
+
m++;
|
|
159
|
+
return m;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* The efficient attack sizes up to `upTo`: each size that guarantees one more kill than the
|
|
163
|
+
* size below it (e.g. 1, 3, 5, 6, 8, 10, 11, 13 at 60%). Any size between two of them kills
|
|
164
|
+
* no more than the smaller one.
|
|
165
|
+
*/
|
|
166
|
+
export function killBreakpoints(ratePct, upTo, luck = 0, rounding = 'StraightRound') {
|
|
167
|
+
const out = [];
|
|
168
|
+
for (let m = 1; m <= upTo; m++)
|
|
169
|
+
if (guaranteedKills(m, ratePct, luck, rounding) > guaranteedKills(m - 1, ratePct, luck, rounding))
|
|
170
|
+
out.push(m);
|
|
171
|
+
return out;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* #271: the fewest attackers that can have killed `kills` defenders, at the most every attacker may
|
|
175
|
+
* kill: the offensive rate plus luck's share of the rest (luck L lets each army kill with
|
|
176
|
+
* probability r on top of the (1 − L)·r it always kills, so at most r + L·(1 − r) of a defender
|
|
177
|
+
* each). Straight rounding rounds the share half up; weighted-random rounding may round any
|
|
178
|
+
* fraction up. A lower bound on an attack seen only by what it killed; `null` when attackers
|
|
179
|
+
* cannot kill at all.
|
|
180
|
+
*/
|
|
181
|
+
export function fewestAttackersToKill(kills, rates = {}, luck = 0, rounding = 'StraightRound') {
|
|
182
|
+
if (kills <= 0)
|
|
183
|
+
return 0;
|
|
184
|
+
const off = rates.offensiveKillRate ?? DEFAULT_OFFENSIVE_KILL_RATE;
|
|
185
|
+
const best = off + luckFraction(luck) * (100 - off);
|
|
186
|
+
if (best <= 0)
|
|
187
|
+
return null;
|
|
188
|
+
// round(a·best/100) ≥ k ⇔ a·best ≥ 100k − 50; weighted: a·best/100 > k − 1.
|
|
189
|
+
return weighted(rounding) ? Math.floor((100 * (kills - 1)) / best) + 1 : Math.ceil((100 * kills - 50) / best);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* #271: the fewest attackers that can have taken a territory held by `defenders`: enough to kill
|
|
193
|
+
* every defender ({@link fewestAttackersToKill}) and one more than the defenders surely kill
|
|
194
|
+
* ({@link guaranteedKills}), so one survives to move in. {@link armiesToTake} when luck is off under
|
|
195
|
+
* straight rounding; `null` when attackers cannot kill at all.
|
|
196
|
+
*/
|
|
197
|
+
export function fewestAttackersToTake(defenders, rates = {}, luck = 0, rounding = 'StraightRound') {
|
|
198
|
+
if (defenders <= 0)
|
|
199
|
+
return 1;
|
|
200
|
+
const kill = fewestAttackersToKill(defenders, rates, luck, rounding);
|
|
201
|
+
if (kill === null)
|
|
202
|
+
return null;
|
|
203
|
+
return Math.max(kill, guaranteedKills(defenders, rates.defensiveKillRate ?? DEFAULT_DEFENSIVE_KILL_RATE, luck, rounding) + 1);
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* The fewest attackers whose {@link winChance} reaches `minChance` against `defenders`;
|
|
207
|
+
* `null` when attackers cannot kill at all.
|
|
208
|
+
*/
|
|
209
|
+
export function armiesForChance(defenders, minChance, settings = {}) {
|
|
210
|
+
if (defenders <= 0)
|
|
211
|
+
return 1;
|
|
212
|
+
if ((settings.offensiveKillRate ?? DEFAULT_OFFENSIVE_KILL_RATE) <= 0)
|
|
213
|
+
return null;
|
|
214
|
+
const ok = (n) => winChance(n, defenders, settings) >= minChance;
|
|
215
|
+
// winChance is monotone in the attackers: double, then bisect.
|
|
216
|
+
let hi = Math.max(1, defenders);
|
|
217
|
+
while (!ok(hi)) {
|
|
218
|
+
hi *= 2;
|
|
219
|
+
if (hi > 1_000_000)
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
let lo = 0;
|
|
223
|
+
while (hi - lo > 1) {
|
|
224
|
+
const mid = (lo + hi) >> 1;
|
|
225
|
+
if (ok(mid))
|
|
226
|
+
hi = mid;
|
|
227
|
+
else
|
|
228
|
+
lo = mid;
|
|
229
|
+
}
|
|
230
|
+
return hi;
|
|
231
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// Is my commander takeable after my orders? (#274) Losing the commander ends the game, so this
|
|
2
|
+
// asks the stress test (stress.ts) one question only: of the single-deploy replies it tries, which
|
|
3
|
+
// take the territory my commander stands on once my orders have run, and how many armies would
|
|
4
|
+
// that territory need to survive every one of them. Pure and deterministic.
|
|
5
|
+
//
|
|
6
|
+
// territory where my commander stands after my orders run unanswered (my orders may take it along);
|
|
7
|
+
// armies the armies of mine there then (the commander not counted);
|
|
8
|
+
// distance moves from it to the nearest territory of an opponent or in the fog (null: none on the map);
|
|
9
|
+
// killers replies tried that kill it, each at the deploys given (the caller sizes them);
|
|
10
|
+
// needs the armies it needs there to survive every reply tried (luck-free under luck, as the
|
|
11
|
+
// stress test resolves battles): found by adding armies on the commander's starting
|
|
12
|
+
// territory (carried along when my orders move the commander from there) until no reply
|
|
13
|
+
// kills it. `armies` when none does; null past {@link SEARCH_CAP};
|
|
14
|
+
// killedBy the smallest deploy, made by every opponent at once (a per-opponent deploy, not a total),
|
|
15
|
+
// at which some reply tried kills it, with the opponent whose reply it is and, when that
|
|
16
|
+
// reply is two opponents striking together (#180, when I lead a free-for-all), its partner
|
|
17
|
+
// (null when none up to {@link SEARCH_CAP} does: no stack of theirs is in reach this turn;
|
|
18
|
+
// also null with `killedBy: false`, which skips that search: warapp_submit_turn never reads it).
|
|
19
|
+
//
|
|
20
|
+
// My Airlifts (`airlifts`, phase 43) run after every deploy and before every move: they are applied
|
|
21
|
+
// to the board with my deploys before anything else (armies off `from` and onto `to`, the commander
|
|
22
|
+
// with them when it goes along), so an Airlift out of, onto or of the commander is counted.
|
|
23
|
+
//
|
|
24
|
+
// Only the replies that strike the commander's territory are tried (`targets`), and never the
|
|
25
|
+
// look-ahead, so it is cheap enough to run inside warapp_submit_turn. What the stress test does not
|
|
26
|
+
// try (fog, a split deploy, cards, …) this does not either.
|
|
27
|
+
import { bfsDistances } from './graph.js';
|
|
28
|
+
import { stressPlan } from './stress.js';
|
|
29
|
+
import { simulateTurn } from './turn-sim.js';
|
|
30
|
+
/** Armies, or a deploy, past which the searches stop. */
|
|
31
|
+
export const SEARCH_CAP = 1_000_000;
|
|
32
|
+
/** The territory my commander stands on, if it is on the board. */
|
|
33
|
+
export function myCommanderTerritory(board, me) {
|
|
34
|
+
return board.territories.find((t) => t.owner.kind === 'player' && t.owner.playerId === me && t.specialUnits?.some((u) => u.kind === 'commander' && u.playerId === me))
|
|
35
|
+
?.territoryId;
|
|
36
|
+
}
|
|
37
|
+
/** The board once my deploys and then my Airlifts have run (the commander moved with its Airlift). */
|
|
38
|
+
function airlifted(board, me, deploys, airlifts) {
|
|
39
|
+
const territories = board.territories.map((t) => ({ ...t, ...(t.specialUnits ? { specialUnits: [...t.specialUnits] } : {}) }));
|
|
40
|
+
const at = new Map(territories.map((t) => [t.territoryId, t]));
|
|
41
|
+
const add = (id, n) => {
|
|
42
|
+
const t = at.get(id);
|
|
43
|
+
if (t)
|
|
44
|
+
t.armies = Math.max(0, (t.armies ?? 0) + n);
|
|
45
|
+
};
|
|
46
|
+
for (const d of deploys)
|
|
47
|
+
add(d.territoryId, d.armies);
|
|
48
|
+
for (const a of airlifts) {
|
|
49
|
+
add(a.from, -a.armies);
|
|
50
|
+
add(a.to, a.armies);
|
|
51
|
+
if (!a.commander)
|
|
52
|
+
continue;
|
|
53
|
+
const src = at.get(a.from);
|
|
54
|
+
const dst = at.get(a.to);
|
|
55
|
+
const unit = src?.specialUnits?.find((u) => u.kind === 'commander' && u.playerId === me);
|
|
56
|
+
if (!src || !dst || !unit)
|
|
57
|
+
continue;
|
|
58
|
+
src.specialUnits = src.specialUnits.filter((u) => u !== unit);
|
|
59
|
+
if (!src.specialUnits.length)
|
|
60
|
+
delete src.specialUnits;
|
|
61
|
+
dst.specialUnits = [...(dst.specialUnits ?? []), unit];
|
|
62
|
+
}
|
|
63
|
+
return { ...board, territories };
|
|
64
|
+
}
|
|
65
|
+
/** The smallest n in [lo, SEARCH_CAP] with `ok(n)`, for `ok` false then true; null when `ok(SEARCH_CAP)` is false. */
|
|
66
|
+
function smallest(lo, ok) {
|
|
67
|
+
if (ok(lo))
|
|
68
|
+
return lo;
|
|
69
|
+
let bad = lo;
|
|
70
|
+
let hi = Math.max(1, lo * 2);
|
|
71
|
+
while (!ok(hi)) {
|
|
72
|
+
bad = hi;
|
|
73
|
+
if (hi >= SEARCH_CAP)
|
|
74
|
+
return null;
|
|
75
|
+
hi = Math.min(SEARCH_CAP, hi * 2);
|
|
76
|
+
}
|
|
77
|
+
while (hi - bad > 1) {
|
|
78
|
+
const mid = Math.floor((bad + hi) / 2);
|
|
79
|
+
if (ok(mid))
|
|
80
|
+
hi = mid;
|
|
81
|
+
else
|
|
82
|
+
bad = mid;
|
|
83
|
+
}
|
|
84
|
+
return hi;
|
|
85
|
+
}
|
|
86
|
+
export function commanderSafety(options) {
|
|
87
|
+
const { killedBy: searchKilledBy = true, airlifts, ...given } = options;
|
|
88
|
+
const start = myCommanderTerritory(given.board, given.me);
|
|
89
|
+
if (start === undefined)
|
|
90
|
+
return undefined;
|
|
91
|
+
// My deploys and Airlifts applied first: everything below runs on the board the moves start from.
|
|
92
|
+
const input = airlifts?.length ? { ...given, board: airlifted(given.board, given.me, given.deploys, airlifts), deploys: [] } : given;
|
|
93
|
+
const { graph, board, me } = input;
|
|
94
|
+
// Where it stands when the moves begin (its Airlift's destination, when one takes it).
|
|
95
|
+
const from = myCommanderTerritory(board, me) ?? start;
|
|
96
|
+
// Where my orders leave it (they may take it along), with nobody answering.
|
|
97
|
+
const after = simulateTurn({
|
|
98
|
+
graph,
|
|
99
|
+
board,
|
|
100
|
+
deploys: input.deploys.map((d) => ({ player: me, territoryId: d.territoryId, armies: d.armies })),
|
|
101
|
+
orders: [{ player: me, orders: input.moves.map((m) => ({ ...m })) }],
|
|
102
|
+
moveOrder: [me],
|
|
103
|
+
...(input.settings ? { settings: input.settings } : {}),
|
|
104
|
+
...(input.teams ? { teams: input.teams } : {}),
|
|
105
|
+
...(input.orderPriority ? { orderPriority: [me] } : {}),
|
|
106
|
+
}).territories;
|
|
107
|
+
const ended = [...after].find(([, t]) => t.commander === me)?.[0];
|
|
108
|
+
const lostUnanswered = ended === undefined;
|
|
109
|
+
const territory = ended ?? from;
|
|
110
|
+
const armies = lostUnanswered ? 0 : (after.get(territory)?.armies ?? 0);
|
|
111
|
+
const at = [...new Set([from, territory])];
|
|
112
|
+
const run = (over) => stressPlan({ ...input, ...over, targets: at, lookAhead: false });
|
|
113
|
+
const kills = (r) => r.atRisk.commander;
|
|
114
|
+
const report = run({});
|
|
115
|
+
const killers = kills(report);
|
|
116
|
+
// The armies that hold: more on the starting territory, carried along by the move that takes the commander.
|
|
117
|
+
const carrier = territory === from ? -1 : input.moves.findIndex((m) => m.from === from && m.withCommander === true);
|
|
118
|
+
const withExtra = (extra) => ({
|
|
119
|
+
deploys: [...input.deploys, { territoryId: from, armies: extra }],
|
|
120
|
+
moves: input.moves.map((m, i) => (i === carrier ? { ...m, armies: m.armies + extra } : m)),
|
|
121
|
+
});
|
|
122
|
+
const extra = lostUnanswered ? null : killers === 0 ? 0 : smallest(1, (n) => kills(run(withExtra(n))) === 0);
|
|
123
|
+
// The smallest deploy, the same for every opponent, that kills it.
|
|
124
|
+
const killing = (d) => run({ opponents: input.opponents.map((o) => ({ ...o, deploy: d })) });
|
|
125
|
+
const killedAt = searchKilledBy && input.opponents.length && !lostUnanswered ? smallest(0, (d) => kills(killing(d)) > 0) : null;
|
|
126
|
+
const killer = killedAt === null ? undefined : killing(killedAt).replies.find((r) => r.commanderLost);
|
|
127
|
+
// Moves to the nearest territory of an opponent (a teammate's is not one) or in the fog.
|
|
128
|
+
const myTeam = input.teams?.get(me);
|
|
129
|
+
const hostile = board.territories.filter((t) => t.owner.kind === 'unknown' ||
|
|
130
|
+
t.armies === null ||
|
|
131
|
+
(t.owner.kind === 'player' && t.owner.playerId !== me && (myTeam === undefined || input.teams?.get(t.owner.playerId) !== myTeam)));
|
|
132
|
+
const dist = bfsDistances(graph, [territory]);
|
|
133
|
+
const reach = hostile.map((t) => dist.get(t.territoryId)).filter((d) => d !== undefined);
|
|
134
|
+
const worst = report.replies.find((r) => r.commanderLost);
|
|
135
|
+
return {
|
|
136
|
+
territory,
|
|
137
|
+
from: start,
|
|
138
|
+
armies,
|
|
139
|
+
distance: reach.length ? Math.min(...reach) : null,
|
|
140
|
+
killers,
|
|
141
|
+
...(worst ? { worst } : {}),
|
|
142
|
+
needs: extra === null ? null : armies + extra,
|
|
143
|
+
killedBy: killedAt === null || !killer
|
|
144
|
+
? null
|
|
145
|
+
: { deploy: killedAt, opponent: killer.opponent, ...(killer.kind === 'joint' && killer.partner !== undefined ? { partner: killer.partner } : {}) },
|
|
146
|
+
lostUnanswered,
|
|
147
|
+
exact: report.exact,
|
|
148
|
+
};
|
|
149
|
+
}
|