@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,328 @@
|
|
|
1
|
+
// Rule 6, the transfers (#121, #124, #125, #128, #172, #179; #133): once every deploy, attack and
|
|
2
|
+
// withdrawal is planned, idle armies move one step toward the front — the enemy front first, else
|
|
3
|
+
// the nearest territory not mine (a teammate's is no front, C3) — never into or through a conceded border, and keeping what a
|
|
4
|
+
// territory beside a pending capture, a stack two steps away or a finished-off player's exit needs.
|
|
5
|
+
// First (#125) idle armies facing no visible enemy stage one step toward the source of the step
|
|
6
|
+
// the leftover deploy saved toward. planPass runs it where it ran before: after the efficient
|
|
7
|
+
// sizes (S2), before Order Priority for a strike (rule 1s).
|
|
8
|
+
// #257: before any of that, an idle interior stack beside a conceded border moves into it when that
|
|
9
|
+
// transfer (plus what the plan leaves there) makes the border hold; the border is then held, not
|
|
10
|
+
// conceded — the conceded border the plan attacks from first, as #121's revival from the leftover.
|
|
11
|
+
// #260: when none revives and the enemy front is reached only through a conceded border the plan
|
|
12
|
+
// attacks an enemy from (#272: or more nearly so than any other way), an idle stack still heads for
|
|
13
|
+
// it — into that border — when the nearest territory not mine is no nearer; never into another conceded border or one a withdrawal leaves,
|
|
14
|
+
// and never from a stack beside a visible enemy at the start of the turn (interior only once a
|
|
15
|
+
// strike lands).
|
|
16
|
+
// When those steps together make the border hold, it is held after all, never left reported conceded.
|
|
17
|
+
import { bfsDistances, neighborsOf } from '../graph.js';
|
|
18
|
+
import { teammateAt } from './state.js';
|
|
19
|
+
import { capStepRoom, truceKeep } from './relations.js';
|
|
20
|
+
/** The transfers (rule 6), with staging toward `staging` (#125) when the leftover saved toward a step. */
|
|
21
|
+
export function transfers(pc, ps, book, threats, hd, caps, staging) {
|
|
22
|
+
const { g, me, s, foe, territories: state, label, bonusName, mine, mineSet, unitsAt, toTake, noSplit, myCommanders } = pc;
|
|
23
|
+
const { warnings, rationale, avail, locked, coverKeep, centralKeep, captured, moves } = ps;
|
|
24
|
+
const { guardAt, coverOf, orderOf, warnSplit } = book;
|
|
25
|
+
const { holdArmies, borderAt, ownerOf, borders } = threats;
|
|
26
|
+
const { arrived, conceded, threatened, stayAt, twoStepCite, twoStepHold, twoStepKeep } = hd;
|
|
27
|
+
const { strikes, worstCase } = caps;
|
|
28
|
+
/**
|
|
29
|
+
* #257: a transfer from `from` to `to`, added to one already planned between them (a territory
|
|
30
|
+
* issues each move once; only the largest move into a border lands before the opponent's first).
|
|
31
|
+
*/
|
|
32
|
+
const transfer = (from, to, armies, reason) => {
|
|
33
|
+
const same = moves.find((m) => m.kind === 'transfer' && m.from === from && m.to === to);
|
|
34
|
+
if (same)
|
|
35
|
+
same.armies += armies;
|
|
36
|
+
else
|
|
37
|
+
moves.push({ from, to, armies, kind: 'transfer', reason });
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* What an idle stack on `t` keeps (rule 6's keeps): #128's hold while an attack on a player beside
|
|
41
|
+
* it may fail, #172's two-step hold, an exit's cover (#179), the leftover kept central (#218) and a
|
|
42
|
+
* truce's hold (B2).
|
|
43
|
+
*/
|
|
44
|
+
const keepOf = (t) => {
|
|
45
|
+
// #128: interior only if this turn's attacks on players beside it land. Until they do, it
|
|
46
|
+
// keeps what holds against the threat there as if they failed (its neighbours' owners'
|
|
47
|
+
// stacks and deploy as they stand): a +12 bonus was left on 1 beside a capture.
|
|
48
|
+
const pending = neighborsOf(g, t).filter((x) => captured.has(x) && state.get(x)?.owner.kind === 'player');
|
|
49
|
+
const ifFail = pending.length ? borderAt(t, () => false) : null;
|
|
50
|
+
const keep128 = ifFail ? Math.max(0, holdArmies(ifFail.threat) - unitsAt(t)) : 0;
|
|
51
|
+
// #172: or it is interior now, but a stack two steps away can take the territory in between
|
|
52
|
+
// this turn and attack it next. The larger hold binds (never the two added together).
|
|
53
|
+
const far = twoStepHold(t);
|
|
54
|
+
const twoStep = far !== null && far.stay > keep128 ? far : null;
|
|
55
|
+
const truceHold = truceKeep(pc, threats, t);
|
|
56
|
+
const keep = Math.max(keep128, twoStep?.stay ?? 0, coverKeep.has(t) ? coverKeep.get(t) + guardAt(t) : 0,
|
|
57
|
+
// #218 (comment): the leftover kept central stays; the armies already there may still go.
|
|
58
|
+
centralKeep.has(t) ? centralKeep.get(t) + guardAt(t) : 0,
|
|
59
|
+
// B2 (T4): beside a truce's covered territory, what holds it there.
|
|
60
|
+
truceHold);
|
|
61
|
+
return { pending, ifFail, twoStep, truceHold, keep };
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* #257: an idle interior stack (no enemy beside it, not my commander's, issuing no order under
|
|
65
|
+
* No-Split) moves into a conceded border beside it when that transfer, with what the plan leaves
|
|
66
|
+
* on the border (its deploys included), makes it hold against the threat it was conceded on: the
|
|
67
|
+
* border is then held. Only a conceded border no withdrawal leaves (its armies would be gone);
|
|
68
|
+
* one whose garrison stays (#59) may be. The conceded border the plan attacks from goes first, then
|
|
69
|
+
* worth per army needed, fewest needed, id; from the stack beside it with the most to send (as
|
|
70
|
+
* rule 6 would move it: its keeps stay) it sends what makes it hold (No-Split: the whole stack),
|
|
71
|
+
* so one stack may hold several; rule 6 then moves the rest, a held border being the front.
|
|
72
|
+
*/
|
|
73
|
+
function reviveConceded(isEnemy) {
|
|
74
|
+
const attacksFrom = (c) => moves
|
|
75
|
+
.filter((m) => m.from === c && m.kind === 'attack')
|
|
76
|
+
.map((m) => m.to)
|
|
77
|
+
.sort((a, b) => a - b);
|
|
78
|
+
const need = (c, threat) => {
|
|
79
|
+
let k = 0;
|
|
80
|
+
while ((toTake(stayAt(c) + (arrived.get(c) ?? 0) + k + unitsAt(c)) ?? Infinity) <= threat) {
|
|
81
|
+
k++;
|
|
82
|
+
if (k > 10_000)
|
|
83
|
+
return Infinity;
|
|
84
|
+
}
|
|
85
|
+
return k;
|
|
86
|
+
};
|
|
87
|
+
const candidates = threatened
|
|
88
|
+
.filter((t) => conceded.has(t.id) && mineSet.has(t.id) && borders.has(t.id))
|
|
89
|
+
.filter((t) => !moves.some((m) => m.from === t.id && m.kind === 'transfer'))
|
|
90
|
+
.map((t) => ({ t, threat: borders.get(t.id).threat, k: need(t.id, borders.get(t.id).threat), from: attacksFrom(t.id) }))
|
|
91
|
+
.filter((c) => c.k > 0 && Number.isFinite(c.k))
|
|
92
|
+
.sort((a, b) => Number(b.from.length > 0) - Number(a.from.length > 0) ||
|
|
93
|
+
b.t.worth / b.k - a.t.worth / a.k ||
|
|
94
|
+
a.k - b.k ||
|
|
95
|
+
a.t.id - b.t.id);
|
|
96
|
+
for (const c of candidates) {
|
|
97
|
+
const id = c.t.id;
|
|
98
|
+
// What each idle stack beside it can send, as rule 6 would move it.
|
|
99
|
+
const sources = neighborsOf(g, id)
|
|
100
|
+
.filter((t) => mineSet.has(t) && !conceded.has(t) && !locked.has(t) && !myCommanders.has(t) && !orderOf(t))
|
|
101
|
+
.filter((t) => !neighborsOf(g, t).some((x) => isEnemy(x)))
|
|
102
|
+
.map((t) => {
|
|
103
|
+
const n = avail.get(t);
|
|
104
|
+
const part = Math.min(n, Math.max(0, stayAt(t) - keepOf(t).keep), capStepRoom(pc, ps, t, id));
|
|
105
|
+
return { t, n, part: noSplit && part < n ? 0 : part };
|
|
106
|
+
})
|
|
107
|
+
.filter((x) => x.part >= c.k)
|
|
108
|
+
.sort((a, b) => b.part - a.part || a.t - b.t);
|
|
109
|
+
const src = sources[0];
|
|
110
|
+
if (!src)
|
|
111
|
+
continue;
|
|
112
|
+
// What makes it hold (No-Split: the whole stack); the rest stays idle for the next border and
|
|
113
|
+
// rule 6, which now passes through this one.
|
|
114
|
+
const k = noSplit ? src.part : c.k;
|
|
115
|
+
avail.set(src.t, src.n - k);
|
|
116
|
+
const reason = `holds ${label(id)} after all (not conceded)`;
|
|
117
|
+
transfer(src.t, id, k, reason);
|
|
118
|
+
arrived.set(id, (arrived.get(id) ?? 0) + k);
|
|
119
|
+
conceded.delete(id);
|
|
120
|
+
locked.add(id);
|
|
121
|
+
const conceding = `${label(id)} cannot be held `;
|
|
122
|
+
for (let i = warnings.length - 1; i >= 0; i--)
|
|
123
|
+
if (warnings[i].startsWith(conceding) && warnings[i].endsWith(': conceded'))
|
|
124
|
+
warnings.splice(i, 1);
|
|
125
|
+
const total = stayAt(id) + (arrived.get(id) ?? 0) + unitsAt(id);
|
|
126
|
+
rationale.push(`Hold ${label(id)} after all (not conceded): transfer ${k} idle armies from ${label(src.t)} into it (${total} armies need ${toTake(total)} to take, above the ~${c.threat} threat; protects +${c.t.value})${c.from.length ? `; the border the plan attacks ${c.from.map(label).join(', ')} from` : ''}. They land only once the transfer runs, so the border holds only if it runs before the attack on it.`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (s.idleTransfers ?? true) {
|
|
130
|
+
const isEnemy = (id) => {
|
|
131
|
+
const st = state.get(id);
|
|
132
|
+
return st?.owner.kind === 'player' && foe(st.owner.playerId) && !pc.truce.noFront(id);
|
|
133
|
+
};
|
|
134
|
+
reviveConceded(isEnemy);
|
|
135
|
+
// #121: a capture counts as held only when it lands for sure — a neutral, or an attack on a
|
|
136
|
+
// player sized for its owner's whole deploy (finishing off, fog pressure, a commander strike).
|
|
137
|
+
// An exact-sized attack on a player fails if the owner reinforces first, and a garrison moved
|
|
138
|
+
// off beside it as "idle interior" would leave its border on the guard.
|
|
139
|
+
const lands = (t) => state.get(t)?.owner.kind !== 'player' || worstCase.has(t) || strikes.has(t);
|
|
140
|
+
const held = new Set([...mine, ...[...captured].filter(lands)]);
|
|
141
|
+
// A conceded border is not "the front": the plan gives it up, so armies never flow into it and
|
|
142
|
+
// distances are not measured through it; idle armies concentrate on a front that holds.
|
|
143
|
+
const passable = (id) => held.has(id) && !conceded.has(id);
|
|
144
|
+
// B2 (T4): a truce's covered territory is no front (isEnemy, above).
|
|
145
|
+
// Enemy front first (#121): idle armies head for the territories a visible enemy holds; only
|
|
146
|
+
// with no enemy reachable do they head for the nearest territory not mine (neutral or fog).
|
|
147
|
+
// PLAN-TEAMS C3: a teammate's territory is no front either; nothing there is to be taken.
|
|
148
|
+
const ids = [...g.territoryById.keys()];
|
|
149
|
+
const enemyFront = ids.filter((id) => !held.has(id) && isEnemy(id));
|
|
150
|
+
const anyFront = ids.filter((id) => !held.has(id) && !pc.truce.noFront(id) && !teammateAt(pc, id));
|
|
151
|
+
const distEnemy = bfsDistances(g, enemyFront, { passable });
|
|
152
|
+
const distAny = bfsDistances(g, anyFront, { passable });
|
|
153
|
+
// #260: when the enemy front is reached only through a conceded border the plan attacks an
|
|
154
|
+
// enemy from (no revival above made it hold), idle armies still head for it — into that border —
|
|
155
|
+
// rather than to a neutral edge no nearer. Never into any other conceded border (the plan gives
|
|
156
|
+
// it up), nor one a withdrawal leaves.
|
|
157
|
+
const attacksFrom = (x) => moves
|
|
158
|
+
.filter((m) => m.from === x && m.kind === 'attack' && isEnemy(m.to))
|
|
159
|
+
.map((m) => m.to)
|
|
160
|
+
.sort((a, b) => a - b);
|
|
161
|
+
const withdrawn = new Set(moves.filter((m) => m.kind === 'transfer' && conceded.has(m.from)).map((m) => m.from));
|
|
162
|
+
const viaOk = (id) => !conceded.has(id) || (!withdrawn.has(id) && attacksFrom(id).length > 0);
|
|
163
|
+
const distEnemyVia = bfsDistances(g, enemyFront, { passable: (id) => held.has(id) && viaOk(id) });
|
|
164
|
+
// #125: staging. Idle armies on a territory facing no visible enemy — a border beside neutrals
|
|
165
|
+
// included — walk one step toward the source of the capture the leftover saved toward, when
|
|
166
|
+
// it is no farther than the enemy front; once there they help take it.
|
|
167
|
+
if (staging) {
|
|
168
|
+
const st = staging;
|
|
169
|
+
const distStage = bfsDistances(g, [st.from], { passable });
|
|
170
|
+
for (const t of mine) {
|
|
171
|
+
const n = avail.get(t);
|
|
172
|
+
if (n <= 0 || locked.has(t) || t === st.from || conceded.has(t) || myCommanders.has(t))
|
|
173
|
+
continue;
|
|
174
|
+
if (neighborsOf(g, t).some((x) => isEnemy(x) || x === st.t))
|
|
175
|
+
continue;
|
|
176
|
+
if (moves.some((m) => m.from === t && m.kind === 'attack'))
|
|
177
|
+
continue;
|
|
178
|
+
const d = distStage.get(t);
|
|
179
|
+
if (d === undefined || d < 1 || d > (distEnemy.get(t) ?? Infinity))
|
|
180
|
+
continue;
|
|
181
|
+
// B2 (T5): never onto a truce's capped border past its cap, nor back along a transfer.
|
|
182
|
+
const step = neighborsOf(g, t)
|
|
183
|
+
.filter((x) => mineSet.has(x) && !conceded.has(x) && (distStage.get(x) ?? Infinity) < d && capStepRoom(pc, ps, t, x) > 0)
|
|
184
|
+
.sort((a, b) => distStage.get(a) - distStage.get(b) || a - b)[0];
|
|
185
|
+
if (step === undefined)
|
|
186
|
+
continue;
|
|
187
|
+
// #172: what a stack two steps away needs it to keep stays (the step below says so).
|
|
188
|
+
const far = twoStepHold(t);
|
|
189
|
+
// #179: nor what covers a finished-off player's exit. B2 (T4): nor what holds it beside a truce's covered territory.
|
|
190
|
+
const truceStay = truceKeep(pc, threats, t) - guardAt(t);
|
|
191
|
+
const room = capStepRoom(pc, ps, t, step);
|
|
192
|
+
const k = Math.min(n - Math.max(twoStepKeep(t), coverOf(t), centralKeep.get(t) ?? 0, truceStay), room);
|
|
193
|
+
// No-Split (#124): the whole stack or nothing.
|
|
194
|
+
if (k <= 0 || (noSplit && k < n))
|
|
195
|
+
continue;
|
|
196
|
+
if (orderOf(t)) {
|
|
197
|
+
warnSplit(t, `its ${k} idle armies do not stage toward ${label(st.t)}`);
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
avail.set(t, n - k);
|
|
201
|
+
const reason = `stages toward ${label(st.t)}, the next step of bonus ${bonusName(st.bonusId)}`;
|
|
202
|
+
transfer(t, step, k, reason);
|
|
203
|
+
arrived.set(step, (arrived.get(step) ?? 0) + k);
|
|
204
|
+
rationale.push(`Move ${k} from ${label(t)} to ${label(step)}: ${reason} (${st.cost} to take from ${label(st.from)}, not affordable this turn)${k < n && k === room
|
|
205
|
+
? `; the rest stay: a truce caps the armies on ${label(step)} at what this leaves there`
|
|
206
|
+
: k < n && far
|
|
207
|
+
? twoStepCite(t, far, (c) => `; the rest stay: ${c}`)
|
|
208
|
+
: k < n && truceStay > 0
|
|
209
|
+
? '; the rest stay: they hold it beside a truce\'s covered territory'
|
|
210
|
+
: ''}.`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
// #260 (review): the via steps into each conceded border, so that one that several steps make
|
|
214
|
+
// hold together (#257 tries one stack at a time) is held after all, not reported conceded.
|
|
215
|
+
const viaInto = new Map();
|
|
216
|
+
for (const t of mine) {
|
|
217
|
+
// Only an interior stack: one beside a visible enemy at the start of the turn (interior only
|
|
218
|
+
// once this turn's strike lands) keeps its armies for that strike and its own border, as
|
|
219
|
+
// #257's revival keeps them.
|
|
220
|
+
// #272: or when the via route is strictly nearer than the plain one — live, the plain route
|
|
221
|
+
// reached the front through a neutral captured this turn, one step longer, and the stack went
|
|
222
|
+
// there instead of into the border the plan attacks from.
|
|
223
|
+
const via = distEnemyVia.has(t) &&
|
|
224
|
+
distEnemyVia.get(t) < (distEnemy.get(t) ?? Infinity) &&
|
|
225
|
+
distEnemyVia.get(t) <= (distAny.get(t) ?? Infinity) &&
|
|
226
|
+
!neighborsOf(g, t).some((x) => isEnemy(x));
|
|
227
|
+
const dist = via ? distEnemyVia : distEnemy.has(t) ? distEnemy : distAny;
|
|
228
|
+
const n = avail.get(t);
|
|
229
|
+
if (n <= 0 || locked.has(t))
|
|
230
|
+
continue;
|
|
231
|
+
// My commander's territory next to an enemy at the start of the turn keeps its armies,
|
|
232
|
+
// even when this turn's captures make it interior: a capture can still fail. So does one
|
|
233
|
+
// next to a border the plan concedes (#121): the enemy takes it next, and then faces it.
|
|
234
|
+
if (myCommanders.has(t) &&
|
|
235
|
+
neighborsOf(g, t).some((x) => (ownerOf(x) ?? me) !== me || conceded.has(x)))
|
|
236
|
+
continue;
|
|
237
|
+
const d = dist.get(t);
|
|
238
|
+
if (d === undefined || d <= 1)
|
|
239
|
+
continue;
|
|
240
|
+
// Only into a territory held at the start of the turn: a planned capture is
|
|
241
|
+
// not mine yet, and a "transfer" into it would execute as an attack.
|
|
242
|
+
// B2 (T5): never onto a truce's capped border past its cap, nor back along a transfer.
|
|
243
|
+
const step = neighborsOf(g, t)
|
|
244
|
+
.filter((x) => mineSet.has(x) &&
|
|
245
|
+
(via ? viaOk(x) : !conceded.has(x)) &&
|
|
246
|
+
(dist.get(x) ?? Infinity) < d &&
|
|
247
|
+
capStepRoom(pc, ps, t, x) > 0)
|
|
248
|
+
.sort((a, b) => dist.get(a) - dist.get(b) || a - b)[0];
|
|
249
|
+
if (step === undefined)
|
|
250
|
+
continue;
|
|
251
|
+
const { pending, ifFail, twoStep, truceHold, keep } = keepOf(t);
|
|
252
|
+
// No-Split (#124): a transfer sends the whole stack, so one that must keep some sends none.
|
|
253
|
+
const room = capStepRoom(pc, ps, t, step);
|
|
254
|
+
const part = Math.min(n, Math.max(0, stayAt(t) - keep), room);
|
|
255
|
+
const k = noSplit && part < n ? 0 : part;
|
|
256
|
+
if (k <= 0) {
|
|
257
|
+
if (twoStep)
|
|
258
|
+
rationale.push(twoStepCite(t, twoStep, (c) => `Keep the ${n} idle armies on ${label(t)}: ${c}.`, `Keep the ${n} idle armies on ${label(t)}.`));
|
|
259
|
+
else if (pending.length)
|
|
260
|
+
rationale.push(`Keep the ${n} idle armies on ${label(t)}: it is interior only once ${pending.map(label).join(', ')} falls, and until then they hold it against ~${ifFail.threat}.`);
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
if (orderOf(t)) {
|
|
264
|
+
warnSplit(t, `its ${k} idle armies are not transferred to ${label(step)}`);
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
avail.set(t, n - k);
|
|
268
|
+
const reason = 'moves idle interior armies toward the front';
|
|
269
|
+
transfer(t, step, k, reason);
|
|
270
|
+
arrived.set(step, (arrived.get(step) ?? 0) + k);
|
|
271
|
+
// #260: say why a step goes toward a front past a conceded border, and what it heads for.
|
|
272
|
+
let front = '';
|
|
273
|
+
if (via) {
|
|
274
|
+
const targets = attacksFrom(step);
|
|
275
|
+
const beside = neighborsOf(g, step).filter((x) => !held.has(x) && isEnemy(x)).sort((a, b) => a - b);
|
|
276
|
+
// An attack target that lands for sure is held, so it is not the front beside the step.
|
|
277
|
+
const landing = targets.filter((x) => !beside.includes(x));
|
|
278
|
+
front = ` (the enemy front${beside.length ? `, ${beside.map(label).join(', ')},` : ''} is reached ${distEnemy.has(t) ? `soonest (${distEnemyVia.get(t)} steps against ${distEnemy.get(t)} any other way)` : 'only'} through a border the plan concedes, and no farther than the nearest territory not mine${conceded.has(step)
|
|
279
|
+
? `; ${label(step)} is the border the plan attacks ${targets.map(label).join(', ')} from${landing.length ? ` (${landing.map(label).join(', ')}, a capture that lands this turn, not the front)` : ''}; they are lost with it if it falls`
|
|
280
|
+
: ''})`;
|
|
281
|
+
}
|
|
282
|
+
if (via && conceded.has(step)) {
|
|
283
|
+
const v = viaInto.get(step) ?? { from: [], lines: [] };
|
|
284
|
+
v.from.push(t);
|
|
285
|
+
v.lines.push({ i: rationale.length, front });
|
|
286
|
+
viaInto.set(step, v);
|
|
287
|
+
}
|
|
288
|
+
rationale.push(`Transfer ${k} from ${label(t)} to ${label(step)}: ${reason}${front}${k < n && k === room
|
|
289
|
+
? ` (the rest stay: a truce caps the armies on ${label(step)} at what this leaves there)`
|
|
290
|
+
: k < n && twoStep
|
|
291
|
+
? twoStepCite(t, twoStep, (c) => ` (the rest stay: ${c})`)
|
|
292
|
+
: k < n && !ifFail && centralKeep.has(t)
|
|
293
|
+
? ` (the ${centralKeep.get(t)} of the leftover kept central stay)`
|
|
294
|
+
: k < n && !ifFail && truceHold > 0
|
|
295
|
+
? ` (${truceHold} stay: they hold it beside a truce's covered territory)`
|
|
296
|
+
: k < n
|
|
297
|
+
? ` (${stayAt(t)} stay: it is interior only once ${pending.map(label).join(', ')} falls, and until then they hold it against ~${ifFail.threat})`
|
|
298
|
+
: ''}.`);
|
|
299
|
+
}
|
|
300
|
+
// #260 (review): what the via steps landed may make a conceded border hold — two idle stacks
|
|
301
|
+
// each too small to revive it alone (#257) — and then it is held, not conceded: the plan says
|
|
302
|
+
// so as #257 does, and its "cannot be held" warning goes. One they leave short stays conceded.
|
|
303
|
+
for (const [id, v] of [...viaInto].sort((a, b) => a[0] - b[0])) {
|
|
304
|
+
const threat = borders.get(id)?.threat;
|
|
305
|
+
if (threat === undefined)
|
|
306
|
+
continue;
|
|
307
|
+
const total = stayAt(id) + (arrived.get(id) ?? 0) + unitsAt(id);
|
|
308
|
+
if ((toTake(total) ?? Infinity) <= threat)
|
|
309
|
+
continue;
|
|
310
|
+
conceded.delete(id);
|
|
311
|
+
locked.add(id);
|
|
312
|
+
const conceding = `${label(id)} cannot be held `;
|
|
313
|
+
for (let i = warnings.length - 1; i >= 0; i--)
|
|
314
|
+
if (warnings[i].startsWith(conceding) && warnings[i].endsWith(': conceded'))
|
|
315
|
+
warnings.splice(i, 1);
|
|
316
|
+
const sent = moves.filter((m) => m.kind === 'transfer' && m.to === id && v.from.includes(m.from)).reduce((n, m) => n + m.armies, 0);
|
|
317
|
+
for (const { i, front } of v.lines)
|
|
318
|
+
rationale[i] = rationale[i].replace(front, ` (into ${label(id)}, which these transfers make hold: it is held after all)`);
|
|
319
|
+
const value = threatened.find((x) => x.id === id)?.value ?? 0;
|
|
320
|
+
const from = attacksFrom(id);
|
|
321
|
+
rationale.push(`Hold ${label(id)} after all (not conceded): the ${sent} idle armies transferred into it from ${v.from
|
|
322
|
+
.slice()
|
|
323
|
+
.sort((a, b) => a - b)
|
|
324
|
+
.map(label)
|
|
325
|
+
.join(', ')} make it hold together (${total} armies need ${toTake(total)} to take, above the ~${threat} threat; protects +${value})${from.length ? `; the border the plan attacks ${from.map(label).join(', ')} from` : ''}. They land only once the transfers run, so the border holds only if they run before the attack on it.`);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// Two one-line helpers every planner module shares (#133): an ascending numeric sort and a percentage.
|
|
2
|
+
/** Ascending numeric order, for `Array.prototype.sort`. */
|
|
3
|
+
export const byNum = (a, b) => a - b;
|
|
4
|
+
/** `p` (0..1) as a percentage with one decimal, e.g. `12.5%`. */
|
|
5
|
+
export const pct = (p) => `${Math.round(p * 1000) / 10}%`;
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
// #281: land a player who is out left behind (a static seat, #270), and what taking it denies; and the
|
|
2
|
+
// owner's extension, empty enemy territory with the guard off. The judgements are pure; pass 3's
|
|
3
|
+
// target bookkeeping and capture (`addEmptyEnemyTargets`, `tryEmptyEnemy`) change the plan state.
|
|
4
|
+
//
|
|
5
|
+
// planTurn plans a static seat's territories as neutrals; `BoardCtx.vacated` keeps which ones they
|
|
6
|
+
// were. Such a territory beside a live opponent is one that opponent takes next turn, for next to
|
|
7
|
+
// nothing, when its stack there can (nobody defends it but its static armies), so the border it would add comes
|
|
8
|
+
// either way: taking it first moves the front one step out, onto land the opponent would otherwise
|
|
9
|
+
// gain. Rule 4d's frontier budget therefore never refuses a cheap one on its hold cost alone, and
|
|
10
|
+
// pass 3 ranks it with the denial credited — a credit that can put it ahead of every plain capture
|
|
11
|
+
// (its score may fall to 0 or below, where a plain capture's never goes below one army). Everything
|
|
12
|
+
// else is as any neutral: the capture spends only the armies above what holds its source (#121), and
|
|
13
|
+
// is sized at its cost (the fewest that take it).
|
|
14
|
+
//
|
|
15
|
+
// The owner's extension (#281, 2026-11-16): with One Army Must Stand Guard off, a live opponent's own
|
|
16
|
+
// territory beside mine standing at 0 (no special unit) that breaks none of its bonuses (that is
|
|
17
|
+
// #165's one-army break) and was not mine on the previous board (#276's retake) is cheap expansion
|
|
18
|
+
// too (`emptyEnemiesOf`). Pass 3 takes it, sized for its owner's deploy there (#120) when a source
|
|
19
|
+
// can pay that, else at its bare cost with #120's warning, ranked as a capture of that size; rule 4d
|
|
20
|
+
// does not undo it when the border it adds faces only enemy territories a border of mine beside it
|
|
21
|
+
// already faces (`sameFront`: the front moves one step, no new exposure). A border to a new stack is
|
|
22
|
+
// judged by rule 4d as before. Never a truce's covered territory, one my attacks failed on (#240) or
|
|
23
|
+
// one lowering income; the capture spends what stands above its source's hold, as any.
|
|
24
|
+
import { neighborsOf } from '../graph.js';
|
|
25
|
+
/** The end of every empty enemy capture's reason (#281, the owner's extension): tests and sweeps match on it. */
|
|
26
|
+
export const CHEAP_EXPANSION = 'cheap expansion (#281)';
|
|
27
|
+
/** A vacated territory costing at most this many armies to take is cheap enough to deny (#281). */
|
|
28
|
+
export const VACATED_MAX_COST = 2;
|
|
29
|
+
/** The vacated territories that are denials (#281), by territory: {@link denialOf} over them, computed once. */
|
|
30
|
+
export function denialsOf(pc) {
|
|
31
|
+
const out = new Map();
|
|
32
|
+
for (const t of [...pc.vacated.keys()].sort((a, b) => a - b)) {
|
|
33
|
+
const d = denialOf(pc, t);
|
|
34
|
+
if (d)
|
|
35
|
+
out.set(t, d);
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* What capturing `t` denies (#281), or `null`: `t` must be vacated by a static seat (still a neutral
|
|
41
|
+
* on this board), cost me at most {@link VACATED_MAX_COST}, and border a visible live opponent's stack
|
|
42
|
+
* that can take it with the armies it has (a teammate never counts).
|
|
43
|
+
*/
|
|
44
|
+
export function denialOf(pc, t) {
|
|
45
|
+
if (!pc.vacated.has(t) || pc.territories.get(t)?.owner.kind !== 'neutral')
|
|
46
|
+
return null;
|
|
47
|
+
const cost = pc.costAt(t);
|
|
48
|
+
if (cost === null || cost > VACATED_MAX_COST)
|
|
49
|
+
return null;
|
|
50
|
+
let best = null;
|
|
51
|
+
for (const n of [...neighborsOf(pc.g, t)].sort((a, b) => a - b)) {
|
|
52
|
+
const o = pc.territories.get(n);
|
|
53
|
+
if (o?.owner.kind !== 'player' || !pc.foe(o.owner.playerId) || pc.allies.has(o.owner.playerId))
|
|
54
|
+
continue;
|
|
55
|
+
if (o.armies === null || o.armies - pc.guard < cost)
|
|
56
|
+
continue;
|
|
57
|
+
const player = o.owner.playerId;
|
|
58
|
+
const holds = (x) => {
|
|
59
|
+
const s = pc.territories.get(x);
|
|
60
|
+
return s?.owner.kind === 'player' && s.owner.playerId === player;
|
|
61
|
+
};
|
|
62
|
+
let progress = 0;
|
|
63
|
+
const completes = [];
|
|
64
|
+
for (const id of pc.bonusesWith.get(t) ?? []) {
|
|
65
|
+
const value = pc.g.bonusById.get(id)?.value ?? 0;
|
|
66
|
+
const members = pc.bonusTs.get(id) ?? [];
|
|
67
|
+
if (value <= 0 || !members.length)
|
|
68
|
+
continue;
|
|
69
|
+
const others = members.filter((x) => x !== t);
|
|
70
|
+
const theirs = others.filter(holds).length;
|
|
71
|
+
if (theirs === others.length) {
|
|
72
|
+
progress += value;
|
|
73
|
+
completes.push(id);
|
|
74
|
+
}
|
|
75
|
+
else if (theirs > 0)
|
|
76
|
+
progress += value / members.length;
|
|
77
|
+
}
|
|
78
|
+
progress = Math.round(progress * 100) / 100;
|
|
79
|
+
if (!best || progress > best.progress)
|
|
80
|
+
best = { player, cost, progress, completes };
|
|
81
|
+
}
|
|
82
|
+
return best;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* A denial's credit in a score, in armies: the territory the opponent would gain (1) plus its income
|
|
86
|
+
* over the payback horizon.
|
|
87
|
+
*/
|
|
88
|
+
export function denialCredit(pc, d) {
|
|
89
|
+
return 1 + pc.paybackTurns * d.progress;
|
|
90
|
+
}
|
|
91
|
+
/** The rationale line for a vacated territory taken though it adds a border facing an enemy (#281). */
|
|
92
|
+
export function denialLine(pc, t, d) {
|
|
93
|
+
const by = `player ${pc.vacated.get(t)}`;
|
|
94
|
+
const gives = d.completes.length
|
|
95
|
+
? `, completing bonus ${d.completes.map((id) => `${pc.bonusName(id)} (+${pc.g.bonusById.get(id).value})`).join(', ')} for it`
|
|
96
|
+
: d.progress > 0
|
|
97
|
+
? `, toward its bonuses (~+${d.progress} a turn)`
|
|
98
|
+
: '';
|
|
99
|
+
const takes = d.cost <= 1 ? 'free' : `for ${d.cost}`;
|
|
100
|
+
return `Take ${pc.label(t)} though it faces player ${d.player}: ${by} left it (out, no AI), it costs ${d.cost}, and player ${d.player} would take it ${takes} next turn${gives} — the border comes either way (#281).`;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* #281 (owner's extension): the empty enemy territories pass 3 may take, by territory. Only with One
|
|
104
|
+
* Army Must Stand Guard off, not under No-Split, under a guarded goal (`expand` never attacks a
|
|
105
|
+
* player; `complete-bonus` makes no plain expansion): a live opponent's territory (never a
|
|
106
|
+
* teammate's) beside mine, in view at 0 with no special unit, whose owner holds no positive bonus it
|
|
107
|
+
* is in (#165 breaks those), not mine on `previousBoard` (#276 retakes those), not already a target
|
|
108
|
+
* (a counter-attack), never a truce's covered territory, one my attacks failed on (#240) or one whose
|
|
109
|
+
* capture lowers income. Pure.
|
|
110
|
+
*/
|
|
111
|
+
export function emptyEnemiesOf(pc, previousBoard, reinforcementAt, skip) {
|
|
112
|
+
const { g, territories: state, mineSet, foe, allies, guard, noSplit, guarded, unitsAt, uncounted, costAt, me } = pc;
|
|
113
|
+
const out = new Map();
|
|
114
|
+
if (guard || noSplit || !guarded)
|
|
115
|
+
return out;
|
|
116
|
+
const was = new Map(previousBoard?.territories.map((t) => [t.territoryId, t.owner]));
|
|
117
|
+
const enemyOf = (id) => {
|
|
118
|
+
const o = state.get(id)?.owner;
|
|
119
|
+
return o?.kind === 'player' && foe(o.playerId) && !allies.has(o.playerId) ? o.playerId : undefined;
|
|
120
|
+
};
|
|
121
|
+
/** A neighbour a stack may stand on: an enemy's, or one whose owner or armies the board hides. */
|
|
122
|
+
const exposure = (id) => {
|
|
123
|
+
if (mineSet.has(id))
|
|
124
|
+
return false;
|
|
125
|
+
const st = state.get(id);
|
|
126
|
+
if (!st || st.owner.kind === 'unknown' || st.armies === null)
|
|
127
|
+
return true;
|
|
128
|
+
return enemyOf(id) !== undefined;
|
|
129
|
+
};
|
|
130
|
+
for (const t of [...state.keys()].sort((a, b) => a - b)) {
|
|
131
|
+
const st = state.get(t);
|
|
132
|
+
const player = enemyOf(t);
|
|
133
|
+
if (player === undefined || st.armies !== 0 || st.specialUnits?.length || unitsAt(t) > 0 || uncounted(t))
|
|
134
|
+
continue;
|
|
135
|
+
const near = neighborsOf(g, t);
|
|
136
|
+
if (!near.some((n) => mineSet.has(n)) || skip(t))
|
|
137
|
+
continue;
|
|
138
|
+
const before = was.get(t);
|
|
139
|
+
if (before?.kind === 'player' && before.playerId === me)
|
|
140
|
+
continue;
|
|
141
|
+
const breaks = [...g.bonusById.values()].some((b) => b.value > 0 &&
|
|
142
|
+
b.territoryIds.includes(t) &&
|
|
143
|
+
b.territoryIds.every((x) => {
|
|
144
|
+
const o = state.get(x)?.owner;
|
|
145
|
+
return o?.kind === 'player' && o.playerId === player;
|
|
146
|
+
}));
|
|
147
|
+
if (breaks)
|
|
148
|
+
continue;
|
|
149
|
+
const bare = costAt(t);
|
|
150
|
+
if (bare === null)
|
|
151
|
+
continue;
|
|
152
|
+
const extra = reinforcementAt(t);
|
|
153
|
+
const sureCost = extra > 0 ? costAt(t, extra) : null;
|
|
154
|
+
const sure = sureCost !== null && sureCost > bare ? sureCost : null;
|
|
155
|
+
const faces = near.filter(exposure).sort((a, b) => a - b);
|
|
156
|
+
const already = new Set(near.filter((n) => mineSet.has(n)).flatMap((n) => neighborsOf(g, n).filter((x) => x !== t && exposure(x))));
|
|
157
|
+
out.set(t, { player, extra, sure, bare, sameFront: faces.every((x) => already.has(x)), faces });
|
|
158
|
+
}
|
|
159
|
+
return out;
|
|
160
|
+
}
|
|
161
|
+
/** The rationale line for an empty enemy territory rule 4d let through on the same front (#281). */
|
|
162
|
+
export function emptyEnemyLine(pc, t, e) {
|
|
163
|
+
const faces = e.faces.map(pc.label).join(', ');
|
|
164
|
+
return `Take ${pc.label(t)} though it adds a border facing an enemy: One Army Must Stand Guard is off and player ${e.player} left it empty, and the border it adds faces only ${faces}, which my borders beside it already face — the front moves one step, no new exposure (#281).`;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Pass 3's order (and rule 5b's), computed once per target: capture cost plus the frontier it adds. A
|
|
168
|
+
* denial (#281) pays no hold need there — its border comes either way — and is credited with
|
|
169
|
+
* {@link denialCredit}, so it can outrank every plain capture. An empty enemy territory (#281's
|
|
170
|
+
* extension) costs the size it is first tried at (its sure size, else its bare one), plus the
|
|
171
|
+
* frontier without its own hold need when its border is on the same front, else the whole frontier.
|
|
172
|
+
*/
|
|
173
|
+
export function plainOrderCosts(pc, tgt, ids) {
|
|
174
|
+
const out = new Map();
|
|
175
|
+
for (const t of ids) {
|
|
176
|
+
const d = tgt.denials.get(t);
|
|
177
|
+
const e = tgt.emptyEnemies.get(t);
|
|
178
|
+
if (d)
|
|
179
|
+
out.set(t, tgt.landScoreCost(t) - denialCredit(pc, d));
|
|
180
|
+
else if (e) {
|
|
181
|
+
const size = e.sure ?? e.bare;
|
|
182
|
+
const frontier = tgt.frontierDelta(pc.mineSet, [t], (x) => (x === t ? size : pc.armiesOf(x)), !e.sameFront).armies;
|
|
183
|
+
out.set(t, tgt.scoreCost(size, frontier));
|
|
184
|
+
}
|
|
185
|
+
else
|
|
186
|
+
out.set(t, tgt.targetScoreCost(t));
|
|
187
|
+
}
|
|
188
|
+
return out;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* #281 (the owner's extension): make the empty enemy territories pass 3 may take targets, before it
|
|
192
|
+
* ranks them (with their frontier cost alone, as every target has). Returns their ids.
|
|
193
|
+
*/
|
|
194
|
+
export function addEmptyEnemyTargets(pc, ps, tgt) {
|
|
195
|
+
const added = [];
|
|
196
|
+
for (const [t, e] of tgt.emptyEnemies) {
|
|
197
|
+
if (ps.captured.has(t) || tgt.targets.has(t))
|
|
198
|
+
continue;
|
|
199
|
+
tgt.targets.set(t, { id: t, cost: e.bare, enemy: true, sources: neighborsOf(pc.g, t).filter((x) => pc.mineSet.has(x)) });
|
|
200
|
+
tgt.edgeCost.set(t, tgt.frontierDelta(pc.mineSet, [t], (x) => (x === t ? tgt.survivorsOf(t) : pc.armiesOf(x))).armies);
|
|
201
|
+
added.push(t);
|
|
202
|
+
}
|
|
203
|
+
return added;
|
|
204
|
+
}
|
|
205
|
+
/** #281: after pass 3, an empty enemy territory it did not take is no target again (rule 4e and the rest size their own). */
|
|
206
|
+
export function dropEmptyEnemyTargets(ps, tgt, added) {
|
|
207
|
+
for (const t of added)
|
|
208
|
+
if (!ps.captured.has(t)) {
|
|
209
|
+
tgt.targets.delete(t);
|
|
210
|
+
tgt.edgeCost.delete(t);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* #281 (the owner's extension): pass 3's capture of an empty enemy territory. Sized first for what its
|
|
215
|
+
* owner may deploy there before it lands (#120: no "assumes" warning, a worst case); only when no source
|
|
216
|
+
* can pay that (a refusal — R3, a trade, rule 4d — stands) is it taken at its bare cost, with #120's
|
|
217
|
+
* warning. What the #276 second line still lacks is kept out of this turn's deploy while it is tried,
|
|
218
|
+
* so the capture is never paid from a held bonus's garrison.
|
|
219
|
+
*/
|
|
220
|
+
export function tryEmptyEnemy(pc, ps, book, tgt, caps, t) {
|
|
221
|
+
const e = tgt.emptyEnemies.get(t);
|
|
222
|
+
const sources0 = tgt.targets.get(t).sources;
|
|
223
|
+
const tg = notFromTransit(pc, tgt.targets.get(t));
|
|
224
|
+
if (!tg.sources.length)
|
|
225
|
+
return false;
|
|
226
|
+
let owed = 0;
|
|
227
|
+
for (const [id, x] of book.secondLine)
|
|
228
|
+
owed += Math.max(0, x.size - (book.secondLineKept.get(id) ?? 0));
|
|
229
|
+
const kept = Math.min(owed, Math.max(0, ps.budget));
|
|
230
|
+
ps.budget -= kept;
|
|
231
|
+
tgt.targets.set(t, tg);
|
|
232
|
+
try {
|
|
233
|
+
const reason = `player ${e.player}'s territory stands empty with One Army Must Stand Guard off: ${CHEAP_EXPANSION}`;
|
|
234
|
+
if (e.sure !== null) {
|
|
235
|
+
tgt.targets.set(t, { ...tg, cost: e.sure });
|
|
236
|
+
caps.worstCase.set(t, e.extra);
|
|
237
|
+
const ok = caps.tryExpand(t, `${reason}, sized to land even if player ${e.player} deploys its ~${e.extra} there first (#120)`);
|
|
238
|
+
tgt.targets.set(t, ok ? { ...tg, cost: e.sure } : tg);
|
|
239
|
+
if (ok)
|
|
240
|
+
return true;
|
|
241
|
+
caps.worstCase.delete(t);
|
|
242
|
+
if (ps.refusal !== undefined || caps.stopped.some((x) => x.t === t))
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
return caps.tryExpand(t, reason);
|
|
246
|
+
}
|
|
247
|
+
finally {
|
|
248
|
+
ps.budget += kept;
|
|
249
|
+
const now = tgt.targets.get(t);
|
|
250
|
+
tgt.targets.set(t, { ...now, sources: sources0 });
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* #281 × #282: the territory my commander walks from this turn, or `undefined`. Everything standing
|
|
255
|
+
* there is its transit hold (`commanderTransit` sizes the hold on the whole stack until the walk runs),
|
|
256
|
+
* so no #281 capture — a denial or an empty enemy territory — is ever sourced from it. My commander's
|
|
257
|
+
* territory when it stays needs no such rule: `keepOf` keeps its worst-case hold (rule 1w) from every capture.
|
|
258
|
+
*/
|
|
259
|
+
export function commanderTransitFrom(pc) {
|
|
260
|
+
return pc.commanderAt?.retreat?.from;
|
|
261
|
+
}
|
|
262
|
+
/** `tg` without the territory my commander walks from among its sources (#281 × #282). */
|
|
263
|
+
export function notFromTransit(pc, tg) {
|
|
264
|
+
const from = commanderTransitFrom(pc);
|
|
265
|
+
return from === undefined || !tg.sources.includes(from) ? tg : { ...tg, sources: tg.sources.filter((x) => x !== from) };
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* #281 × #282: try a denial's capture with the territory my commander walks from left out of its
|
|
269
|
+
* sources, its sources restored after (the capture itself keeps no reference to the list).
|
|
270
|
+
*/
|
|
271
|
+
export function withoutTransit(pc, tgt, t, attempt) {
|
|
272
|
+
const tg = tgt.targets.get(t);
|
|
273
|
+
const narrowed = notFromTransit(pc, tg);
|
|
274
|
+
if (narrowed === tg)
|
|
275
|
+
return attempt();
|
|
276
|
+
if (!narrowed.sources.length)
|
|
277
|
+
return false;
|
|
278
|
+
tgt.targets.set(t, narrowed);
|
|
279
|
+
try {
|
|
280
|
+
return attempt();
|
|
281
|
+
}
|
|
282
|
+
finally {
|
|
283
|
+
tgt.targets.set(t, { ...tgt.targets.get(t), sources: tg.sources });
|
|
284
|
+
}
|
|
285
|
+
}
|