@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,82 @@
|
|
|
1
|
+
// Territory adjacency graph and breadth-first search over a GameMap.
|
|
2
|
+
//
|
|
3
|
+
// Pure and deterministic: adjacency lists are symmetrized, de-duplicated and
|
|
4
|
+
// sorted ascending, so every search visits neighbours in the same order and
|
|
5
|
+
// ties always resolve toward the lower territory id.
|
|
6
|
+
const EMPTY = Object.freeze([]);
|
|
7
|
+
/**
|
|
8
|
+
* Build the graph. A link is kept when either side declares it (maps are not
|
|
9
|
+
* always symmetric); self-links and links to unknown territories are dropped.
|
|
10
|
+
*/
|
|
11
|
+
export function buildGraph(map) {
|
|
12
|
+
const territoryById = new Map(map.territories.map((t) => [t.id, t]));
|
|
13
|
+
const sets = new Map();
|
|
14
|
+
for (const t of map.territories)
|
|
15
|
+
sets.set(t.id, new Set());
|
|
16
|
+
for (const t of map.territories) {
|
|
17
|
+
for (const n of t.connectedTo) {
|
|
18
|
+
if (n === t.id || !territoryById.has(n))
|
|
19
|
+
continue;
|
|
20
|
+
sets.get(t.id).add(n);
|
|
21
|
+
sets.get(n).add(t.id);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const neighbors = new Map();
|
|
25
|
+
for (const [id, s] of sets)
|
|
26
|
+
neighbors.set(id, [...s].sort((a, b) => a - b));
|
|
27
|
+
return { map, territoryById, bonusById: new Map(map.bonuses.map((b) => [b.id, b])), neighbors };
|
|
28
|
+
}
|
|
29
|
+
export function neighborsOf(g, id) {
|
|
30
|
+
return g.neighbors.get(id) ?? EMPTY;
|
|
31
|
+
}
|
|
32
|
+
/** Hop distance from the nearest source to every reachable territory. Unknown sources are ignored. */
|
|
33
|
+
export function bfsDistances(g, sources, opts = {}) {
|
|
34
|
+
const dist = new Map();
|
|
35
|
+
const queue = [];
|
|
36
|
+
for (const s of [...sources].sort((a, b) => a - b)) {
|
|
37
|
+
if (g.territoryById.has(s) && !dist.has(s)) {
|
|
38
|
+
dist.set(s, 0);
|
|
39
|
+
queue.push(s);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
for (let head = 0; head < queue.length; head++) {
|
|
43
|
+
const v = queue[head];
|
|
44
|
+
const d = dist.get(v) + 1;
|
|
45
|
+
for (const n of neighborsOf(g, v)) {
|
|
46
|
+
if (dist.has(n) || (opts.passable && !opts.passable(n)))
|
|
47
|
+
continue;
|
|
48
|
+
dist.set(n, d);
|
|
49
|
+
queue.push(n);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return dist;
|
|
53
|
+
}
|
|
54
|
+
/** Shortest path (inclusive of both ends), or `null` when either end is unknown or unreachable. */
|
|
55
|
+
export function shortestPath(g, from, to) {
|
|
56
|
+
if (!g.territoryById.has(from) || !g.territoryById.has(to))
|
|
57
|
+
return null;
|
|
58
|
+
const prev = new Map([[from, from]]);
|
|
59
|
+
const queue = [from];
|
|
60
|
+
for (let head = 0; head < queue.length && !prev.has(to); head++) {
|
|
61
|
+
const v = queue[head];
|
|
62
|
+
for (const n of neighborsOf(g, v)) {
|
|
63
|
+
if (prev.has(n))
|
|
64
|
+
continue;
|
|
65
|
+
prev.set(n, v);
|
|
66
|
+
queue.push(n);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (!prev.has(to))
|
|
70
|
+
return null;
|
|
71
|
+
const path = [to];
|
|
72
|
+
for (let v = to; v !== from;) {
|
|
73
|
+
v = prev.get(v);
|
|
74
|
+
path.push(v);
|
|
75
|
+
}
|
|
76
|
+
return path.reverse();
|
|
77
|
+
}
|
|
78
|
+
/** Hops between two territories, or `null` when unknown or unreachable. */
|
|
79
|
+
export function distance(g, from, to) {
|
|
80
|
+
const p = shortestPath(g, from, to);
|
|
81
|
+
return p ? p.length - 1 : null;
|
|
82
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { neighborsOf } from './graph.js';
|
|
2
|
+
export function guardOffExposure(args) {
|
|
3
|
+
const { graph, board, me } = args;
|
|
4
|
+
const friends = args.friends ?? new Set();
|
|
5
|
+
const byId = new Map(board.territories.map((t) => [t.territoryId, t]));
|
|
6
|
+
const ownerOf = (id) => {
|
|
7
|
+
const o = byId.get(id)?.owner;
|
|
8
|
+
return o?.kind === 'player' ? o.playerId : undefined;
|
|
9
|
+
};
|
|
10
|
+
const isMine = (id) => ownerOf(id) === me;
|
|
11
|
+
const isEnemy = (id) => {
|
|
12
|
+
const p = ownerOf(id);
|
|
13
|
+
return p !== undefined && p !== me && !friends.has(p);
|
|
14
|
+
};
|
|
15
|
+
const hidden = (id) => {
|
|
16
|
+
const t = byId.get(id);
|
|
17
|
+
return !t || t.owner.kind === 'unknown' || t.armies === null;
|
|
18
|
+
};
|
|
19
|
+
const left = new Map();
|
|
20
|
+
for (const t of board.territories)
|
|
21
|
+
if (isMine(t.territoryId))
|
|
22
|
+
left.set(t.territoryId, t.armies ?? 0);
|
|
23
|
+
for (const d of args.deploys)
|
|
24
|
+
if (left.has(d.territoryId))
|
|
25
|
+
left.set(d.territoryId, left.get(d.territoryId) + d.armies);
|
|
26
|
+
for (const m of args.moves) {
|
|
27
|
+
if (left.has(m.from))
|
|
28
|
+
left.set(m.from, Math.max(0, left.get(m.from) - m.armies));
|
|
29
|
+
if (left.has(m.to))
|
|
30
|
+
left.set(m.to, left.get(m.to) + m.armies);
|
|
31
|
+
}
|
|
32
|
+
const hasMyCommander = (id) => byId.get(id)?.specialUnits?.some((u) => u.kind === 'commander' && u.playerId === me) ?? false;
|
|
33
|
+
const emptied = [...left.entries()]
|
|
34
|
+
.filter(([id, n]) => n === 0 && !hasMyCommander(id) && neighborsOf(graph, id).some((x) => isEnemy(x) || hidden(x)))
|
|
35
|
+
.map(([id]) => id)
|
|
36
|
+
.sort((a, b) => a - b);
|
|
37
|
+
const attacked = new Set(args.moves.map((m) => m.to));
|
|
38
|
+
const breaks = [];
|
|
39
|
+
for (const t of [...board.territories].sort((a, b) => a.territoryId - b.territoryId)) {
|
|
40
|
+
const id = t.territoryId;
|
|
41
|
+
const owner = ownerOf(id);
|
|
42
|
+
if (owner === undefined || !isEnemy(id) || t.armies !== 0 || t.specialUnits?.length || attacked.has(id))
|
|
43
|
+
continue;
|
|
44
|
+
const from = neighborsOf(graph, id).filter(isMine).sort((a, b) => a - b)[0];
|
|
45
|
+
if (from === undefined)
|
|
46
|
+
continue;
|
|
47
|
+
const held = [...graph.bonusById.values()]
|
|
48
|
+
.filter((b) => b.value > 0 && b.territoryIds.includes(id) && b.territoryIds.every((x) => ownerOf(x) === owner))
|
|
49
|
+
.sort((a, b) => b.value - a.value || a.id - b.id)[0];
|
|
50
|
+
if (held)
|
|
51
|
+
breaks.push({ territoryId: id, from, playerId: owner, bonusId: held.id, value: held.value });
|
|
52
|
+
}
|
|
53
|
+
return { emptied, breaks };
|
|
54
|
+
}
|
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
// The border guess: a randomized deploy split for when no fixed split holds.
|
|
2
|
+
//
|
|
3
|
+
// One opponent touches two or more of my borders. The deploys the plan spends on those borders
|
|
4
|
+
// cannot hold all of them against "its stacks + its whole deploy", so the opponent picks which
|
|
5
|
+
// border to hit: a fixed (predictable) split loses the border it always leaves short. A split
|
|
6
|
+
// drawn at random, weighted by what each border is worth, cuts the expected loss (the security
|
|
7
|
+
// game of docs/STRATEGY.md "Guessing at a border").
|
|
8
|
+
//
|
|
9
|
+
// The model: the opponent attacks every border with the stacks beside it, and lands its whole
|
|
10
|
+
// deploy next to one border (its choice). I fund each border at one of its threshold levels: what
|
|
11
|
+
// planned moves out of it need, what holds the stacks alone, what holds the stacks plus the
|
|
12
|
+
// deploy (anything between two levels is wasted under exact combat). A border that falls costs
|
|
13
|
+
// its worth: 1, plus the value of every bonus I hold in full that contains it, plus 100 when my
|
|
14
|
+
// commander stands there. The best fixed split is the maximin; the mixed value is the exact
|
|
15
|
+
// value of the matrix game (a small simplex over the allocations × targets matrix).
|
|
16
|
+
//
|
|
17
|
+
// Only the deploys the plan puts on these borders are redistributed (`budget`): expansion and
|
|
18
|
+
// every other order of the plan are untouched. #274: the territory my commander stands on, and any a
|
|
19
|
+
// commander move of the plan walks it to, keep at least the plan's deploy in every allocation: the
|
|
20
|
+
// planner sizes that hold at my income and every stack within two moves, more than this model's one
|
|
21
|
+
// deploy, and warapp_submit_turn refuses orders that leave the commander takeable. Pure and deterministic (ties on ascending id),
|
|
22
|
+
// no I/O; the sample is drawn from a caller's seed with mulberry32.
|
|
23
|
+
// No guess against an opponent whose estimate the #275 tripwire marked untrusted (`untrusted`): the
|
|
24
|
+
// plan sized its held-bonus borders above the estimate, and a split drawn from the estimate would undo it.
|
|
25
|
+
import { bonusTerritories } from './bonuses.js';
|
|
26
|
+
import { armiesToTake, DEFAULT_DEFENSIVE_KILL_RATE, DEFAULT_OFFENSIVE_KILL_RATE, luckFraction, specialUnitStrength } from './combat.js';
|
|
27
|
+
import { neighborsOf } from './graph.js';
|
|
28
|
+
import { alliesOf } from './planner.js';
|
|
29
|
+
/** A guess is emitted only when the mix beats the best fixed split by this share of the worth at stake. */
|
|
30
|
+
export const MIN_GUESS_GAIN = 0.05;
|
|
31
|
+
/** A border with my commander on it is worth this much more (losing it can lose the game). */
|
|
32
|
+
const COMMANDER_WORTH = 100;
|
|
33
|
+
/** At most this many borders (the allocations grow as 3^borders). */
|
|
34
|
+
const MAX_GUESS_BORDERS = 7;
|
|
35
|
+
const byNum = (a, b) => a - b;
|
|
36
|
+
const skip = (reason, message) => ({ kind: 'skipped', reason, message });
|
|
37
|
+
const round = (x) => Math.round(x * 1e6) / 1e6;
|
|
38
|
+
/** A 32-bit seed from a game id and a turn (FNV-1a): re-planning a turn draws the same sample. */
|
|
39
|
+
export function guessSeed(gameId, turn, salt = '') {
|
|
40
|
+
// The salt is a local secret (src/guess-salt.ts): without it, anyone who reads this public code
|
|
41
|
+
// could reproduce the draw from the game id and turn.
|
|
42
|
+
const text = `warapp-guess:${salt}:${gameId}:${turn ?? 'none'}`;
|
|
43
|
+
let h = 0x811c9dc5;
|
|
44
|
+
for (let i = 0; i < text.length; i++)
|
|
45
|
+
h = Math.imul(h ^ text.charCodeAt(i), 0x01000193);
|
|
46
|
+
return h >>> 0;
|
|
47
|
+
}
|
|
48
|
+
/** mulberry32: a small deterministic PRNG, one draw in [0, 1). */
|
|
49
|
+
function mulberry32(seed) {
|
|
50
|
+
let a = seed >>> 0;
|
|
51
|
+
return () => {
|
|
52
|
+
a = (a + 0x6d2b79f5) >>> 0;
|
|
53
|
+
let t = a;
|
|
54
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
55
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
56
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The exact value of a zero-sum matrix game where the row player minimises `loss[a][j]`:
|
|
61
|
+
* its optimal mix `x`, the column player's optimal mix `q` and the value. Solved as the LP
|
|
62
|
+
* max Σw s.t. (C − loss)·w ≤ 1, w ≥ 0 (C makes every payoff positive) by a dense tableau simplex
|
|
63
|
+
* with Bland's rule; the row mix is read from the slacks' reduced costs.
|
|
64
|
+
*/
|
|
65
|
+
export function solveLossGame(loss) {
|
|
66
|
+
const n = loss.length;
|
|
67
|
+
const k = loss[0]?.length ?? 0;
|
|
68
|
+
if (n === 0 || k === 0)
|
|
69
|
+
return { value: 0, x: [], q: [] };
|
|
70
|
+
const C = Math.max(...loss.flat()) + 1;
|
|
71
|
+
const width = k + n + 1;
|
|
72
|
+
const rhs = k + n;
|
|
73
|
+
const T = [];
|
|
74
|
+
for (let a = 0; a < n; a++) {
|
|
75
|
+
const row = new Float64Array(width);
|
|
76
|
+
for (let j = 0; j < k; j++)
|
|
77
|
+
row[j] = C - loss[a][j];
|
|
78
|
+
row[k + a] = 1;
|
|
79
|
+
row[rhs] = 1;
|
|
80
|
+
T.push(row);
|
|
81
|
+
}
|
|
82
|
+
const obj = new Float64Array(width);
|
|
83
|
+
for (let j = 0; j < k; j++)
|
|
84
|
+
obj[j] = -1;
|
|
85
|
+
const basis = Array.from({ length: n }, (_, a) => k + a);
|
|
86
|
+
const EPS = 1e-12;
|
|
87
|
+
for (let iter = 0; iter < 10_000; iter++) {
|
|
88
|
+
let e = -1;
|
|
89
|
+
for (let c = 0; c < rhs; c++)
|
|
90
|
+
if (obj[c] < -EPS) {
|
|
91
|
+
e = c;
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
if (e < 0)
|
|
95
|
+
break;
|
|
96
|
+
let r = -1;
|
|
97
|
+
let best = Infinity;
|
|
98
|
+
for (let i = 0; i < n; i++) {
|
|
99
|
+
const v = T[i][e];
|
|
100
|
+
if (v <= EPS)
|
|
101
|
+
continue;
|
|
102
|
+
const ratio = T[i][rhs] / v;
|
|
103
|
+
if (ratio < best - EPS || (Math.abs(ratio - best) <= EPS && basis[i] < basis[r])) {
|
|
104
|
+
best = ratio;
|
|
105
|
+
r = i;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (r < 0)
|
|
109
|
+
break; // unbounded: impossible with positive payoffs
|
|
110
|
+
const pr = T[r];
|
|
111
|
+
const pv = pr[e];
|
|
112
|
+
for (let c = 0; c < width; c++)
|
|
113
|
+
pr[c] = pr[c] / pv;
|
|
114
|
+
for (let i = 0; i < n; i++) {
|
|
115
|
+
if (i === r)
|
|
116
|
+
continue;
|
|
117
|
+
const row = T[i];
|
|
118
|
+
const f = row[e];
|
|
119
|
+
if (f !== 0)
|
|
120
|
+
for (let c = 0; c < width; c++)
|
|
121
|
+
row[c] = row[c] - f * pr[c];
|
|
122
|
+
}
|
|
123
|
+
const f = obj[e];
|
|
124
|
+
for (let c = 0; c < width; c++)
|
|
125
|
+
obj[c] = obj[c] - f * pr[c];
|
|
126
|
+
basis[r] = e;
|
|
127
|
+
}
|
|
128
|
+
const V = obj[rhs];
|
|
129
|
+
const vB = 1 / V;
|
|
130
|
+
const w = new Array(k).fill(0);
|
|
131
|
+
basis.forEach((b, i) => {
|
|
132
|
+
if (b < k)
|
|
133
|
+
w[b] = T[i][rhs];
|
|
134
|
+
});
|
|
135
|
+
const clean = (xs) => {
|
|
136
|
+
const ys = xs.map((v) => (v > 1e-9 ? v : 0));
|
|
137
|
+
const s = ys.reduce((a, b) => a + b, 0);
|
|
138
|
+
return ys.map((v) => v / s);
|
|
139
|
+
};
|
|
140
|
+
const x = clean(Array.from({ length: n }, (_, a) => obj[k + a] * vB));
|
|
141
|
+
const q = clean(w.map((v) => v * vB));
|
|
142
|
+
// The value of the cleaned mix against the best reply (never below the LP's).
|
|
143
|
+
const value = Math.max(...Array.from({ length: k }, (_, j) => x.reduce((acc, p, a) => acc + p * loss[a][j], 0)));
|
|
144
|
+
return { value, x, q };
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* The border guess for a planned turn, or why there is none. See the file header for the model.
|
|
148
|
+
*/
|
|
149
|
+
export function borderGuess(input) {
|
|
150
|
+
const { graph: g, board, me, plan } = input;
|
|
151
|
+
const s = input.settings ?? {};
|
|
152
|
+
if (luckFraction(s.luckModifier) > 0 || s.roundingMode === 'WeightedRandom')
|
|
153
|
+
return skip('luck', 'Battles have luck or weighted rounding here, so hold levels are not exact.');
|
|
154
|
+
if (s.localDeployments === true)
|
|
155
|
+
return skip('local-deployment', 'Local Deployment is on: deploys cannot move freely between these borders.');
|
|
156
|
+
const rates = { offensiveKillRate: s.offensiveKillRate ?? DEFAULT_OFFENSIVE_KILL_RATE, defensiveKillRate: s.defensiveKillRate ?? DEFAULT_DEFENSIVE_KILL_RATE };
|
|
157
|
+
const guard = (s.oneArmyStandsGuard ?? true) ? 1 : 0;
|
|
158
|
+
const allies = alliesOf(me, input.players);
|
|
159
|
+
const state = new Map();
|
|
160
|
+
for (const t of board.territories)
|
|
161
|
+
if (g.territoryById.has(t.territoryId))
|
|
162
|
+
state.set(t.territoryId, t);
|
|
163
|
+
const ownerOf = (id) => {
|
|
164
|
+
const t = state.get(id);
|
|
165
|
+
return t?.owner.kind === 'player' ? t.owner.playerId : null;
|
|
166
|
+
};
|
|
167
|
+
const foe = (p) => p !== null && p !== me && !allies.has(p);
|
|
168
|
+
/** A foe's territory with its armies shown. */
|
|
169
|
+
const foeStack = (id) => foe(ownerOf(id)) && state.get(id).armies !== null;
|
|
170
|
+
const mine = [...state.values()].filter((t) => ownerOf(t.territoryId) === me).map((t) => t.territoryId).sort(byNum);
|
|
171
|
+
const mineSet = new Set(mine);
|
|
172
|
+
const opponents = new Set();
|
|
173
|
+
for (const t of mine)
|
|
174
|
+
for (const n of neighborsOf(g, t))
|
|
175
|
+
if (foeStack(n))
|
|
176
|
+
opponents.add(ownerOf(n));
|
|
177
|
+
if (opponents.size === 0)
|
|
178
|
+
return skip('no-contact', 'No opponent stack is in sight next to my territories.');
|
|
179
|
+
if (opponents.size > 1)
|
|
180
|
+
return skip('several-opponents', `${opponents.size} opponents touch my borders: the guess models one opponent with one deploy.`);
|
|
181
|
+
const opp = [...opponents][0];
|
|
182
|
+
if (input.enemyDeploy === undefined && input.untrusted?.includes(opp))
|
|
183
|
+
return skip('untrusted', `Player ${opp}'s deploy estimate is untrusted (#275): the plan sized its borders above it, and a guess drawn from the estimate would undo that.`);
|
|
184
|
+
const D = Math.max(0, Math.floor(input.enemyDeploy ?? input.estimates?.find((e) => e.playerId === opp)?.estimate ?? plan.enemyDeploy));
|
|
185
|
+
const ids = mine.filter((t) => neighborsOf(g, t).some((n) => foeStack(n) && ownerOf(n) === opp));
|
|
186
|
+
if (ids.length < 2)
|
|
187
|
+
return skip('one-border', 'The opponent touches only one of my territories: there is nothing to guess between.');
|
|
188
|
+
if (ids.length > MAX_GUESS_BORDERS)
|
|
189
|
+
return skip('too-many-borders', `The opponent touches ${ids.length} of my territories (at most ${MAX_GUESS_BORDERS} are modelled).`);
|
|
190
|
+
const held = g.map.bonuses.filter((b) => {
|
|
191
|
+
if (b.value <= 0)
|
|
192
|
+
return false;
|
|
193
|
+
const ts = bonusTerritories(g, b.id);
|
|
194
|
+
return ts.length > 0 && ts.every((t) => mineSet.has(t));
|
|
195
|
+
});
|
|
196
|
+
const planDeploy = new Map();
|
|
197
|
+
for (const d of plan.deploys)
|
|
198
|
+
planDeploy.set(d.territoryId, (planDeploy.get(d.territoryId) ?? 0) + d.armies);
|
|
199
|
+
if (s.noSplit === true && plan.moves.some((m) => ids.includes(m.from)))
|
|
200
|
+
return skip('no-split', 'No-Split is on and the plan moves a whole stack out of one of these borders.');
|
|
201
|
+
/** Least defenders (armies + unit strength) that `attack` armies cannot take. */
|
|
202
|
+
const need = (attack) => {
|
|
203
|
+
let d = 0;
|
|
204
|
+
while ((armiesToTake(d, rates) ?? Infinity) <= attack)
|
|
205
|
+
d++;
|
|
206
|
+
return d;
|
|
207
|
+
};
|
|
208
|
+
/** #274: where my commander stands or walks to this turn. */
|
|
209
|
+
const commanderAt = new Set([
|
|
210
|
+
...mine.filter((id) => (state.get(id).specialUnits ?? []).some((u) => u.kind === 'commander' && u.playerId === me)),
|
|
211
|
+
...plan.moves.filter((m) => m.commander).map((m) => m.to),
|
|
212
|
+
]);
|
|
213
|
+
const borders = ids.map((id) => {
|
|
214
|
+
const t = state.get(id);
|
|
215
|
+
const armies = t.armies ?? 0;
|
|
216
|
+
const myUnits = (t.specialUnits ?? []).filter((u) => u.playerId === me);
|
|
217
|
+
const strength = specialUnitStrength(myUnits);
|
|
218
|
+
const stack = neighborsOf(g, id)
|
|
219
|
+
.filter((n) => foeStack(n) && ownerOf(n) === opp)
|
|
220
|
+
.reduce((a, n) => {
|
|
221
|
+
const nt = state.get(n);
|
|
222
|
+
const units = (nt.specialUnits ?? []).filter((u) => u.playerId === opp);
|
|
223
|
+
return a + Math.max(0, (nt.armies ?? 0) - guard) + specialUnitStrength(units);
|
|
224
|
+
}, 0);
|
|
225
|
+
const worth = 1 +
|
|
226
|
+
held.filter((b) => bonusTerritories(g, b.id).includes(id)).reduce((a, b) => a + b.value, 0) +
|
|
227
|
+
(myUnits.some((u) => u.kind === 'commander') ? COMMANDER_WORTH : 0);
|
|
228
|
+
// The least deploy that keeps every planned move out of it valid (each move counting the
|
|
229
|
+
// armies moved in earlier in the list, as warapp_submit_turn checks it).
|
|
230
|
+
let running = armies;
|
|
231
|
+
let minDeploy = 0;
|
|
232
|
+
let movedOut = 0;
|
|
233
|
+
for (const m of plan.moves) {
|
|
234
|
+
if (m.to === id)
|
|
235
|
+
running += m.armies;
|
|
236
|
+
if (m.from === id) {
|
|
237
|
+
running -= m.armies;
|
|
238
|
+
movedOut += m.armies;
|
|
239
|
+
minDeploy = Math.max(minDeploy, guard - running);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return {
|
|
243
|
+
id,
|
|
244
|
+
worth,
|
|
245
|
+
armies,
|
|
246
|
+
movedOut,
|
|
247
|
+
base: armies - movedOut,
|
|
248
|
+
minDeploy,
|
|
249
|
+
holdAlone: Math.max(0, need(stack) - strength),
|
|
250
|
+
holdFull: Math.max(0, need(stack + D) - strength),
|
|
251
|
+
planDeploy: planDeploy.get(id) ?? 0,
|
|
252
|
+
...(commanderAt.has(id) ? { pinned: true } : {}),
|
|
253
|
+
};
|
|
254
|
+
});
|
|
255
|
+
const k = borders.length;
|
|
256
|
+
const budget = borders.reduce((a, b) => a + b.planDeploy, 0);
|
|
257
|
+
const richest = borders.reduce((best, b, i) => (b.worth > borders[best].worth ? i : best), 0);
|
|
258
|
+
// Allocations: each border at one of its distinct levels within the budget, the rest on the richest;
|
|
259
|
+
// a pinned border (#274) at the plan's deploy only.
|
|
260
|
+
const levels = borders.map((b) => b.pinned
|
|
261
|
+
? [Math.max(b.minDeploy, b.planDeploy)]
|
|
262
|
+
: [...new Set([b.minDeploy, Math.max(b.minDeploy, b.holdAlone - b.base), Math.max(b.minDeploy, b.holdFull - b.base)])].sort(byNum));
|
|
263
|
+
const seen = new Set();
|
|
264
|
+
const allocs = [];
|
|
265
|
+
const rec = (i, acc, spent) => {
|
|
266
|
+
if (i === k) {
|
|
267
|
+
const a = [...acc];
|
|
268
|
+
a[richest] = a[richest] + (budget - spent);
|
|
269
|
+
const key = a.join(',');
|
|
270
|
+
if (!seen.has(key)) {
|
|
271
|
+
seen.add(key);
|
|
272
|
+
allocs.push(a);
|
|
273
|
+
}
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
for (const c of levels[i])
|
|
277
|
+
if (spent + c <= budget)
|
|
278
|
+
rec(i + 1, [...acc, c], spent + c);
|
|
279
|
+
};
|
|
280
|
+
rec(0, [], 0);
|
|
281
|
+
const lossOf = (alloc, j) => borders.reduce((a, b, i) => a + (b.base + alloc[i] < (i === j ? b.holdFull : b.holdAlone) ? b.worth : 0), 0);
|
|
282
|
+
const worst = (alloc) => Math.max(...borders.map((_, j) => lossOf(alloc, j)));
|
|
283
|
+
const planAlloc = borders.map((b) => b.planDeploy);
|
|
284
|
+
const planLoss = worst(planAlloc);
|
|
285
|
+
if (allocs.length === 0)
|
|
286
|
+
allocs.push(planAlloc);
|
|
287
|
+
const M = allocs.map((a) => borders.map((_, j) => lossOf(a, j)));
|
|
288
|
+
const fixedLoss = Math.min(...M.map((row) => Math.max(...row)));
|
|
289
|
+
if (fixedLoss === 0)
|
|
290
|
+
return skip('fixed-split-holds', 'A fixed split of the deploys on these borders holds every one of them: no need to guess.');
|
|
291
|
+
// Drop dominated allocations (another loses no more against every target); the first of equals stays.
|
|
292
|
+
const keep = [];
|
|
293
|
+
for (let a = 0; a < M.length; a++) {
|
|
294
|
+
const dominated = M.some((row, b) => b !== a && row.every((v, j) => v <= M[a][j]) && (row.some((v, j) => v < M[a][j]) || b < a));
|
|
295
|
+
if (!dominated)
|
|
296
|
+
keep.push(a);
|
|
297
|
+
}
|
|
298
|
+
const { value, x, q } = solveLossGame(keep.map((a) => M[a]));
|
|
299
|
+
const worthAtStake = borders.reduce((a, b) => a + b.worth, 0);
|
|
300
|
+
if (fixedLoss - value < MIN_GUESS_GAIN * worthAtStake)
|
|
301
|
+
return skip('small-gain', `Mixing would cut the expected loss only from ${fixedLoss} to ${round(value)} of ${worthAtStake} at stake: the fixed split is as good.`);
|
|
302
|
+
const mix = keep.map((a, i) => ({ allocation: allocs[a], probability: round(x[i]) })).filter((m) => m.probability > 0);
|
|
303
|
+
const draw = mulberry32(input.seed)();
|
|
304
|
+
let acc = 0;
|
|
305
|
+
let pick = mix[mix.length - 1];
|
|
306
|
+
const totalP = mix.reduce((a, m) => a + m.probability, 0);
|
|
307
|
+
for (const m of mix) {
|
|
308
|
+
acc += m.probability / totalP;
|
|
309
|
+
if (draw < acc) {
|
|
310
|
+
pick = m;
|
|
311
|
+
break;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
const allocation = pick.allocation;
|
|
315
|
+
const prob = (test, i) => round(mix.reduce((a, m) => a + (test(m.allocation, i) ? m.probability : 0), 0) / totalP);
|
|
316
|
+
const onBorders = new Set(ids);
|
|
317
|
+
const deploys = new Map();
|
|
318
|
+
for (const d of plan.deploys)
|
|
319
|
+
if (!onBorders.has(d.territoryId))
|
|
320
|
+
deploys.set(d.territoryId, (deploys.get(d.territoryId) ?? 0) + d.armies);
|
|
321
|
+
borders.forEach((b, i) => {
|
|
322
|
+
if (allocation[i] > 0)
|
|
323
|
+
deploys.set(b.id, allocation[i]);
|
|
324
|
+
});
|
|
325
|
+
return {
|
|
326
|
+
kind: 'guess',
|
|
327
|
+
opponent: opp,
|
|
328
|
+
enemyDeploy: D,
|
|
329
|
+
budget,
|
|
330
|
+
borders: borders.map((b, i) => ({
|
|
331
|
+
id: b.id,
|
|
332
|
+
worth: b.worth,
|
|
333
|
+
armies: b.armies,
|
|
334
|
+
movedOut: b.movedOut,
|
|
335
|
+
holdAlone: b.holdAlone,
|
|
336
|
+
holdFull: b.holdFull,
|
|
337
|
+
planDeploy: b.planDeploy,
|
|
338
|
+
guessDeploy: allocation[i],
|
|
339
|
+
holdProbability: prob((a, j) => b.base + a[j] >= b.holdFull, i),
|
|
340
|
+
holdAloneProbability: prob((a, j) => b.base + a[j] >= b.holdAlone, i),
|
|
341
|
+
})),
|
|
342
|
+
worthAtStake,
|
|
343
|
+
fixedLoss,
|
|
344
|
+
mixedLoss: round(value),
|
|
345
|
+
planLoss,
|
|
346
|
+
targets: borders.map((b, j) => ({ id: b.id, probability: round(q[j] ?? 0) })),
|
|
347
|
+
mix,
|
|
348
|
+
allocation,
|
|
349
|
+
deploys: [...deploys].sort((a, b) => a[0] - b[0]).map(([territoryId, armies]) => ({ territoryId, armies })),
|
|
350
|
+
};
|
|
351
|
+
}
|