@chrischall/warapp-mcp 0.0.0 → 0.2.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 +211 -9
- package/dist/analysis/accept-surrender.js +56 -0
- package/dist/analysis/award.js +159 -0
- package/dist/analysis/bonuses.js +98 -0
- package/dist/analysis/breach.js +62 -0
- package/dist/analysis/calibrate.js +239 -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/finished-game.js +136 -0
- package/dist/analysis/game-lessons.js +217 -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 +179 -0
- package/dist/analysis/lookahead.js +108 -0
- package/dist/analysis/opponent-profile.js +552 -0
- package/dist/analysis/opponents.js +1253 -0
- package/dist/analysis/outlook.js +695 -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 +1405 -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 +1001 -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 +2927 -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 +584 -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 +1112 -0
- package/dist/board.js +105 -0
- package/dist/bundle.js +121082 -36148
- package/dist/calibration-source.js +75 -0
- package/dist/cards.js +435 -0
- package/dist/chat-cursor.js +135 -0
- package/dist/chat.js +265 -0
- package/dist/client.js +439 -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/finished-store.js +357 -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/lessons-store.js +356 -0
- package/dist/live.js +287 -0
- package/dist/map-details.js +127 -0
- package/dist/map-stats.js +160 -0
- package/dist/map-store.js +242 -0
- package/dist/move-order.js +141 -0
- package/dist/my-games.js +218 -0
- package/dist/normalize.js +443 -0
- package/dist/notifications.js +143 -0
- package/dist/open-games.js +537 -0
- package/dist/outlook-log.js +165 -0
- package/dist/path-lock.js +22 -0
- package/dist/pick-list.js +193 -0
- package/dist/player-record-store.js +231 -0
- package/dist/player-states.js +31 -0
- package/dist/profile-store.js +727 -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 +615 -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 +227 -0
- package/dist/scalars.js +28 -0
- package/dist/scout.js +346 -0
- package/dist/stats.js +291 -0
- package/dist/strategy/card-clock.js +185 -0
- package/dist/strategy/map-notes.js +128 -0
- package/dist/strategy/notes.js +2223 -0
- package/dist/surrender-pending.js +126 -0
- package/dist/team-cards.js +84 -0
- package/dist/team-orders.js +150 -0
- package/dist/teams.js +19 -0
- package/dist/tools/accept-surrender.js +311 -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 +158 -0
- package/dist/tools/chat-read-mark.js +179 -0
- package/dist/tools/chat.js +525 -0
- package/dist/tools/check-plan.js +689 -0
- package/dist/tools/commander-check.js +103 -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/end-of-game.js +384 -0
- package/dist/tools/game.js +151 -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 +152 -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-details.js +88 -0
- package/dist/tools/map-stats.js +130 -0
- package/dist/tools/map.js +141 -0
- package/dist/tools/my-games.js +193 -0
- package/dist/tools/notifications.js +484 -0
- package/dist/tools/open-games.js +104 -0
- package/dist/tools/opponents.js +163 -0
- package/dist/tools/order-options.js +135 -0
- package/dist/tools/past-games.js +94 -0
- package/dist/tools/picks.js +305 -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 +445 -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 +257 -0
- package/dist/tools/stress-opponents.js +83 -0
- package/dist/tools/surrender-pending.js +74 -0
- package/dist/tools/surrender.js +247 -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 +178 -0
- package/dist/tools/turn.js +2825 -0
- package/dist/tools/uncommit.js +160 -0
- package/dist/tools/where.js +300 -0
- package/dist/tools/write-kit.js +164 -0
- package/dist/transport-bridge.js +33 -0
- package/dist/trend.js +577 -0
- package/dist/version.js +4 -0
- package/manifest.json +194 -4
- package/mint.yaml +106 -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 +1096 -21
|
@@ -0,0 +1,702 @@
|
|
|
1
|
+
// B2 (docs/PLAN-TEAMS.md section 8): the planner honours a truce. The rule logic lives here; the
|
|
2
|
+
// phase modules only call it.
|
|
3
|
+
//
|
|
4
|
+
// A truce (`PlanInput.relations`) is honoured only in a free-for-all, while `active` or in `notice`
|
|
5
|
+
// (until its `endsTurn`), with a player who is a foe (not me, not a teammate) — except T1, which
|
|
6
|
+
// holds wherever the agreement binds, a team or two-player game included (D3: never a surprise
|
|
7
|
+
// break; `warapp_submit_turn` refuses such an order in any format):
|
|
8
|
+
// T1 a covered territory of the partner is never a target, in any pass (`noTarget`: the targets,
|
|
9
|
+
// tryCapture, the strikes, the lead strikes, the guard-off breaks and rule 4e's enemy fallback);
|
|
10
|
+
// T2 a covered border of mine that faces only the partner is sized for the partner's visible
|
|
11
|
+
// stacks next to it attacking with no deploy and no trend (`forceAt`; its separate-attack
|
|
12
|
+
// rule still applies), and the report never says it is sized for that deploy (`relaxedFor`);
|
|
13
|
+
// T3 never relaxed: my commander's territory, a border protecting more than
|
|
14
|
+
// TRUCE_RELAX_VALUE × my income, any border while the trust is `watch`;
|
|
15
|
+
// T4 the partner is no front: not for the no-second-front rule (rule 0p.2, a partner still
|
|
16
|
+
// playing: `partnerFronts`), and idle armies do not walk toward its covered territories (rule 6);
|
|
17
|
+
// T5 a border cap: my orders leave at most the cap on a covered border territory — no transfer or
|
|
18
|
+
// withdrawal of mine fills it past the cap (`capRoom`), and the excess is deployed one step
|
|
19
|
+
// behind, an incoming transfer trimmed, or the standing armies moved one step behind toward
|
|
20
|
+
// another front, said so when I already stand above it; a warning when what the cap leaves
|
|
21
|
+
// falls short against another opponent there, or of T3's full hold under my commander;
|
|
22
|
+
// T6 what a break would cost (`truceBreakLosses`, after the plan is finished): for each binding
|
|
23
|
+
// truce, the partner's whole deploy estimate on any territory of its beside a covered territory
|
|
24
|
+
// of mine (covered or not: T2 relaxes against all of them) and an attack on it with everything
|
|
25
|
+
// there, carried out against this plan's
|
|
26
|
+
// own deploys and moves on the turn simulator (turn-sim.ts), both move orders unless the first
|
|
27
|
+
// mover is known; the costliest is reported (`lossIfBroken`);
|
|
28
|
+
// T7 never a surprise break: a notice binds until its `endsTurn`; the plan advises `give-notice`
|
|
29
|
+
// when the trust turned `watch` for the endgame or for outgrowing;
|
|
30
|
+
// T8 broken by them (`state: 'broken'`): an ordinary opponent at once, the plan naming the term,
|
|
31
|
+
// the turn and the evidence, and saying to record `broken-by-them` before submitting;
|
|
32
|
+
// T9 enforced (a Diplomacy card the decoder is sure of, PLAN-TEAMS D3): that player is no foe for
|
|
33
|
+
// the turn — its threat is 0 and no attack on it is planned — in any format, since the game
|
|
34
|
+
// enforces it; when the card is gone by next turn (`enforcedUntil`), the player still counts
|
|
35
|
+
// for next turn's reasoning (`lookAheadFoes`): the two-step look-ahead (#172), and (G2) the
|
|
36
|
+
// frontier's hold cost (rule 4d) and a completion's defensibility (R3), via `nextTurnAt`.
|
|
37
|
+
// With no relations every function here is a no-op, so every plan is byte-identical to one made
|
|
38
|
+
// without them. Pure and deterministic (ties on ascending id).
|
|
39
|
+
import { bonusTerritories } from '../bonuses.js';
|
|
40
|
+
import { bfsDistances, neighborsOf } from '../graph.js';
|
|
41
|
+
import { simTerritories, simulateTurn } from '../turn-sim.js';
|
|
42
|
+
import { CONCEDE_FACTOR, } from './shared.js';
|
|
43
|
+
import { byNum } from './util.js';
|
|
44
|
+
/** T3: a covered border protecting more than this share of my income keeps its full hold. */
|
|
45
|
+
export const TRUCE_RELAX_VALUE = 0.25;
|
|
46
|
+
/** No relations: every check false, nothing to report. */
|
|
47
|
+
const NONE = {
|
|
48
|
+
given: [],
|
|
49
|
+
status: [],
|
|
50
|
+
binds: [],
|
|
51
|
+
partners: new Set(),
|
|
52
|
+
enforced: new Set(),
|
|
53
|
+
lookAheadFoes: new Set(),
|
|
54
|
+
noTarget: () => false,
|
|
55
|
+
noFront: () => false,
|
|
56
|
+
format: 'ffa',
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Read the truces of `input` (the estimates phase, before the foes are fixed): which bind this turn
|
|
60
|
+
* and the territories none of my passes may attack. `notFoe` says who is me or a teammate.
|
|
61
|
+
*/
|
|
62
|
+
export function truceOf(input, o) {
|
|
63
|
+
const given = input.relations ?? [];
|
|
64
|
+
if (!given.length)
|
|
65
|
+
return NONE;
|
|
66
|
+
const ffa = o.format === 'ffa';
|
|
67
|
+
const status = [];
|
|
68
|
+
const binds = [];
|
|
69
|
+
/** Whether the agreement itself binds this turn, whatever the format (T1 keeps to it everywhere). */
|
|
70
|
+
const kept = [];
|
|
71
|
+
/** T9: whether each relation's card is in effect this turn (a game rule, so in any format). */
|
|
72
|
+
const enforcedNow = given.map((r) => r.enforced === true && (r.enforcedUntil === undefined || o.turnNow === null || o.turnNow < r.enforcedUntil));
|
|
73
|
+
given.forEach((r, i) => {
|
|
74
|
+
let st;
|
|
75
|
+
if (o.notFoe(r.player))
|
|
76
|
+
st = 'not-a-foe';
|
|
77
|
+
else if (r.state === 'broken')
|
|
78
|
+
st = 'broken';
|
|
79
|
+
// D3: a relation that is only a card ends with it.
|
|
80
|
+
else if (r.card && !enforcedNow[i])
|
|
81
|
+
st = 'over';
|
|
82
|
+
else if (r.state === 'notice' && r.endsTurn !== undefined && o.turnNow !== null && o.turnNow >= r.endsTurn)
|
|
83
|
+
st = 'over';
|
|
84
|
+
else
|
|
85
|
+
st = r.state;
|
|
86
|
+
kept.push(st === 'active' || st === 'notice');
|
|
87
|
+
if (!ffa && !r.card && !(enforcedNow[i] && kept[i]))
|
|
88
|
+
st = 'ignored';
|
|
89
|
+
status.push(st);
|
|
90
|
+
binds.push(st === 'active' || st === 'notice');
|
|
91
|
+
});
|
|
92
|
+
const owner = new Map();
|
|
93
|
+
for (const t of input.board.territories)
|
|
94
|
+
if (t.owner.kind === 'player')
|
|
95
|
+
owner.set(t.territoryId, t.owner.playerId);
|
|
96
|
+
const partners = new Set();
|
|
97
|
+
const enforced = new Set();
|
|
98
|
+
const lookAheadFoes = new Set();
|
|
99
|
+
/** Per partner whose agreement binds (in any format), the territories of theirs covered. */
|
|
100
|
+
const theirs = new Map();
|
|
101
|
+
given.forEach((r, i) => {
|
|
102
|
+
if (!kept[i])
|
|
103
|
+
return;
|
|
104
|
+
// T2–T5 and T9 only in a free-for-all; T1 (never a surprise break, D3) wherever the agreement binds.
|
|
105
|
+
if (binds[i]) {
|
|
106
|
+
partners.add(r.player);
|
|
107
|
+
if (enforcedNow[i]) {
|
|
108
|
+
enforced.add(r.player);
|
|
109
|
+
if (r.enforcedUntil !== undefined && o.turnNow !== null && r.enforcedUntil <= o.turnNow + 1)
|
|
110
|
+
lookAheadFoes.add(r.player);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const set = theirs.get(r.player) ?? new Set();
|
|
114
|
+
for (const t of r.coveredTheirs)
|
|
115
|
+
set.add(t);
|
|
116
|
+
theirs.set(r.player, set);
|
|
117
|
+
});
|
|
118
|
+
const noTarget = (t) => {
|
|
119
|
+
const p = owner.get(t);
|
|
120
|
+
if (p === undefined)
|
|
121
|
+
return false;
|
|
122
|
+
const set = theirs.get(p);
|
|
123
|
+
if (!set)
|
|
124
|
+
return false;
|
|
125
|
+
return enforced.has(p) || set.has(t);
|
|
126
|
+
};
|
|
127
|
+
const noFront = (t) => {
|
|
128
|
+
const p = owner.get(t);
|
|
129
|
+
return p !== undefined && partners.has(p) && noTarget(t);
|
|
130
|
+
};
|
|
131
|
+
// A player another binding relation enforces for longer is no look-ahead foe.
|
|
132
|
+
given.forEach((r, i) => {
|
|
133
|
+
if (binds[i] && enforcedNow[i] && (r.enforcedUntil === undefined || (o.turnNow !== null && r.enforcedUntil > o.turnNow + 1)))
|
|
134
|
+
lookAheadFoes.delete(r.player);
|
|
135
|
+
});
|
|
136
|
+
return { given, status, binds, partners, enforced, lookAheadFoes, noTarget, noFront, format: o.format };
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* T4 for rule 0p.2: the binding partners the game profile counts among my opponents — on the
|
|
140
|
+
* game's player list and still `Playing`, or, with no list, holding a territory in sight — so a
|
|
141
|
+
* truce with a player who is out takes no front away. 0 with no binding truce.
|
|
142
|
+
*/
|
|
143
|
+
export function partnerFronts(pc) {
|
|
144
|
+
const { truce, game, board, g } = pc;
|
|
145
|
+
if (!truce.partners.size)
|
|
146
|
+
return 0;
|
|
147
|
+
const listed = game.players ?? [];
|
|
148
|
+
const playing = listed.length
|
|
149
|
+
? (p) => listed.some((x) => x.id === p && (x.state ?? 'Playing') === 'Playing')
|
|
150
|
+
: (p) => board.territories.some((t) => g.territoryById.has(t.territoryId) && t.owner.kind === 'player' && t.owner.playerId === p && t.armies !== null);
|
|
151
|
+
return [...truce.partners].filter(playing).length;
|
|
152
|
+
}
|
|
153
|
+
/** The foe players with a stack (visible, or its owner shown) next to my `t`, ascending. */
|
|
154
|
+
function foesNext(pc, t) {
|
|
155
|
+
const out = new Set();
|
|
156
|
+
for (const n of neighborsOf(pc.g, t)) {
|
|
157
|
+
const st = pc.territories.get(n);
|
|
158
|
+
if (st?.owner.kind === 'player' && pc.foe(st.owner.playerId))
|
|
159
|
+
out.add(st.owner.playerId);
|
|
160
|
+
}
|
|
161
|
+
return [...out].sort(byNum);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* T2 and T3, once the deploy book knows what each border protects (`valueOf`, my holdings at the
|
|
165
|
+
* start of the turn): a covered border of mine facing only its partner is relaxed unless my
|
|
166
|
+
* commander stands there, it protects more than {@link TRUCE_RELAX_VALUE} × my income, or the trust
|
|
167
|
+
* is `watch`. `undefined` with no binding truce (the threats phase then reads nothing).
|
|
168
|
+
*/
|
|
169
|
+
export function trucePass(pc, valueOf) {
|
|
170
|
+
const { truce, mineSet, myCommanders, income } = pc;
|
|
171
|
+
if (!truce.partners.size)
|
|
172
|
+
return undefined;
|
|
173
|
+
const relaxed = new Map();
|
|
174
|
+
const fullHold = new Map();
|
|
175
|
+
truce.given.forEach((r, i) => {
|
|
176
|
+
if (!truce.binds[i] || truce.enforced.has(r.player))
|
|
177
|
+
return;
|
|
178
|
+
for (const t of [...r.coveredMine].sort(byNum)) {
|
|
179
|
+
if (!mineSet.has(t) || relaxed.has(t) || fullHold.has(t))
|
|
180
|
+
continue;
|
|
181
|
+
const foes = foesNext(pc, t);
|
|
182
|
+
if (foes.length !== 1 || foes[0] !== r.player)
|
|
183
|
+
continue;
|
|
184
|
+
const why = myCommanders.has(t) ? 'commander' : r.trust === 'watch' ? 'watch' : valueOf(t) > TRUCE_RELAX_VALUE * income ? 'value' : undefined;
|
|
185
|
+
if (why)
|
|
186
|
+
fullHold.set(t, { player: r.player, why });
|
|
187
|
+
else
|
|
188
|
+
relaxed.set(t, r.player);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
return { relaxed, fullHold, capMoved: new Map() };
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* T2 in the report: my `t` is relaxed for player `p` this pass — sized for its visible stacks there
|
|
195
|
+
* with no deploy, so no line may call it sized for (or a gamble on) that player's deploy. Always
|
|
196
|
+
* false with no binding truce.
|
|
197
|
+
*/
|
|
198
|
+
export function relaxedFor(ps, t, p) {
|
|
199
|
+
return ps.truce?.relaxed.get(t) === p;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* T4's other half: a territory of mine next to a truce's covered territory is no interior, though
|
|
203
|
+
* the partner is no front — it keeps what holds it against the threat there (T2's truce hold, or
|
|
204
|
+
* T3's full one), so rule 6 never walks its garrison away. 0 beside no covered territory.
|
|
205
|
+
*/
|
|
206
|
+
export function truceKeep(pc, threats, t) {
|
|
207
|
+
if (!pc.truce.partners.size || !neighborsOf(pc.g, t).some((n) => pc.truce.noTarget(n)))
|
|
208
|
+
return 0;
|
|
209
|
+
const b = threats.borderAt(t, () => false);
|
|
210
|
+
return b ? Math.max(0, threats.holdArmies(b.threat) - pc.unitsAt(t)) : 0;
|
|
211
|
+
}
|
|
212
|
+
/** My covered territories next to a covered territory of the partner of `r` (the covered border). */
|
|
213
|
+
function coveredBorder(pc, r) {
|
|
214
|
+
const theirs = new Set(r.coveredTheirs);
|
|
215
|
+
return [...r.coveredMine]
|
|
216
|
+
.filter((t) => pc.mineSet.has(t))
|
|
217
|
+
.filter((t) => neighborsOf(pc.g, t).some((n) => {
|
|
218
|
+
const st = pc.territories.get(n);
|
|
219
|
+
return theirs.has(n) && st?.owner.kind === 'player' && st.owner.playerId === r.player;
|
|
220
|
+
}))
|
|
221
|
+
.sort(byNum);
|
|
222
|
+
}
|
|
223
|
+
const capsMemo = new WeakMap();
|
|
224
|
+
function capsOf(pc) {
|
|
225
|
+
const hit = capsMemo.get(pc);
|
|
226
|
+
if (hit)
|
|
227
|
+
return hit;
|
|
228
|
+
const { truce } = pc;
|
|
229
|
+
const capped = new Map();
|
|
230
|
+
const borderOf = new Map();
|
|
231
|
+
truce.given.forEach((r, i) => {
|
|
232
|
+
if (!truce.binds[i] || r.borderCap === undefined || truce.enforced.has(r.player))
|
|
233
|
+
return;
|
|
234
|
+
const ts = coveredBorder(pc, r);
|
|
235
|
+
borderOf.set(i, ts);
|
|
236
|
+
for (const t of ts)
|
|
237
|
+
capped.set(t, Math.min(capped.get(t) ?? Infinity, Math.max(0, Math.floor(r.borderCap))));
|
|
238
|
+
});
|
|
239
|
+
const caps = { borderOf, capped };
|
|
240
|
+
capsMemo.set(pc, caps);
|
|
241
|
+
return caps;
|
|
242
|
+
}
|
|
243
|
+
/** What the orders planned so far (`ps.moves`, plus `pending` not yet in it) leave on my `t`. */
|
|
244
|
+
function endsAt(pc, ps, t, pending = []) {
|
|
245
|
+
let n = pc.armiesOf(t) + (ps.deployed.get(t) ?? 0);
|
|
246
|
+
for (const m of [...ps.moves, ...pending])
|
|
247
|
+
n += (m.kind === 'transfer' && m.to === t ? m.armies : 0) - (m.from === t ? m.armies : 0);
|
|
248
|
+
return n;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* T5 for the moves that fill a border (rule 1b's withdrawals, #125's staging, rule 6's transfers):
|
|
252
|
+
* how many more armies my orders may move onto `t` without leaving more than a truce's border cap
|
|
253
|
+
* there, counting this turn's deploys and the orders planned so far (`pending`: those not yet in
|
|
254
|
+
* `ps.moves`). Infinity on a territory no cap binds — always, with no relations.
|
|
255
|
+
*/
|
|
256
|
+
export function capRoom(pc, ps, t, pending = []) {
|
|
257
|
+
if (!pc.truce.partners.size)
|
|
258
|
+
return Infinity;
|
|
259
|
+
const cap = capsOf(pc).capped.get(t);
|
|
260
|
+
if (cap === undefined)
|
|
261
|
+
return Infinity;
|
|
262
|
+
return Math.max(0, cap - endsAt(pc, ps, t, pending));
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* T5 for one fill move from my `from` onto my `to`: {@link capRoom} on `to`, and 0 when a transfer
|
|
266
|
+
* from `to` into `from` is already planned — a cap that closes the nearest step turns idle armies
|
|
267
|
+
* another way, and they must not walk back along a move of this turn. Infinity with no binding truce.
|
|
268
|
+
*/
|
|
269
|
+
export function capStepRoom(pc, ps, from, to, pending = []) {
|
|
270
|
+
if (!pc.truce.partners.size)
|
|
271
|
+
return Infinity;
|
|
272
|
+
if ([...ps.moves, ...pending].some((m) => m.kind === 'transfer' && m.from === to && m.to === from))
|
|
273
|
+
return 0;
|
|
274
|
+
return capRoom(pc, ps, to, pending);
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* T5, after the transfers: what my orders leave on a covered border territory is at most the cap.
|
|
278
|
+
* The excess is deployed one step behind — to a territory of mine on no capped border, nearest
|
|
279
|
+
* another front (a foe's territory no truce covers, else any territory not mine and not covered) —
|
|
280
|
+
* then taken off a transfer of mine into it (the latest first; No-Split: a whole one only), then the
|
|
281
|
+
* standing armies move one step behind, and the plan says so; one already above the cap at the
|
|
282
|
+
* start is said to move back. What cannot go (No-Split, no step, Local Deployment's room, armies that
|
|
283
|
+
* cannot move again) is a warning that names it — this plan's own deploy for a hold, when that is
|
|
284
|
+
* what exceeds the cap; the plan never exceeds a term silently: it says to deploy less there, or to
|
|
285
|
+
* give notice and ask the owner first. A capped border that also faces another opponent is not
|
|
286
|
+
* "toward another front": when what the cap leaves there no longer holds it against that opponent
|
|
287
|
+
* (and it held before), a warning names the opponent and the hold — the cap's cost (section 4). So
|
|
288
|
+
* does one where my commander stands, when what the cap leaves no longer meets T3's full hold against
|
|
289
|
+
* the partner itself.
|
|
290
|
+
*/
|
|
291
|
+
export function applyBorderCaps(pc, ps, book, hd, tp, threats) {
|
|
292
|
+
const { truce, g, mineSet, label, armiesOf, noSplit, unitsAt, territories: state } = pc;
|
|
293
|
+
if (!tp)
|
|
294
|
+
return;
|
|
295
|
+
const { deployed, avail, moves, warnings, rationale, incoming } = ps;
|
|
296
|
+
const { capped, borderOf } = capsOf(pc);
|
|
297
|
+
if (!capped.size)
|
|
298
|
+
return;
|
|
299
|
+
// Another front: a foe's territory no truce covers, else any territory not mine no truce covers.
|
|
300
|
+
const ids = [...g.territoryById.keys()];
|
|
301
|
+
const open = (id) => !mineSet.has(id) && !truce.noTarget(id);
|
|
302
|
+
const foeFront = ids.filter((id) => {
|
|
303
|
+
const st = state.get(id);
|
|
304
|
+
return open(id) && st?.owner.kind === 'player' && pc.foe(st.owner.playerId);
|
|
305
|
+
});
|
|
306
|
+
const passable = (id) => mineSet.has(id) && !hd.conceded.has(id);
|
|
307
|
+
const distFoe = bfsDistances(g, foeFront, { passable });
|
|
308
|
+
const distAny = bfsDistances(g, ids.filter(open), { passable });
|
|
309
|
+
truce.given.forEach((r, i) => {
|
|
310
|
+
for (const t of borderOf.get(i) ?? []) {
|
|
311
|
+
const cap = capped.get(t);
|
|
312
|
+
const before = endsAt(pc, ps, t);
|
|
313
|
+
let excess = before - cap;
|
|
314
|
+
if (excess <= 0)
|
|
315
|
+
continue;
|
|
316
|
+
// Another opponent beside it, with every truce partner's stacks left out.
|
|
317
|
+
const facesOther = foesNext(pc, t).some((p) => !truce.partners.has(p));
|
|
318
|
+
const already = armiesOf(t) > cap;
|
|
319
|
+
const dist = [...neighborsOf(g, t)].some((x) => distFoe.has(x)) ? distFoe : distAny;
|
|
320
|
+
const step = neighborsOf(g, t)
|
|
321
|
+
.filter((x) => mineSet.has(x) && !capped.has(x) && !hd.conceded.has(x))
|
|
322
|
+
.sort((a, b) => (dist.get(a) ?? Infinity) - (dist.get(b) ?? Infinity) || a - b)[0];
|
|
323
|
+
const moved = tp.capMoved.get(i) ?? [];
|
|
324
|
+
const said = [];
|
|
325
|
+
const trimmed = [];
|
|
326
|
+
/** Local Deployment: deploys the step's pool had no room for, put back on `t`. */
|
|
327
|
+
let noRoom;
|
|
328
|
+
if (step !== undefined) {
|
|
329
|
+
// This turn's deploys there land one step behind instead.
|
|
330
|
+
const off = Math.min(excess, deployed.get(t) ?? 0, avail.get(t));
|
|
331
|
+
if (off > 0) {
|
|
332
|
+
// Taken back first, so the step's room counts them (Local Deployment: what fits there).
|
|
333
|
+
book.undeploy(t, off);
|
|
334
|
+
const shift = Math.min(off, book.roomAt(step));
|
|
335
|
+
if (shift > 0)
|
|
336
|
+
book.deploy(step, shift);
|
|
337
|
+
if (shift < off)
|
|
338
|
+
book.deploy(t, off - shift);
|
|
339
|
+
if (shift < off)
|
|
340
|
+
noRoom = { shift, off };
|
|
341
|
+
if (shift > 0)
|
|
342
|
+
said.push(`deploy ${shift} on ${label(step)} instead`);
|
|
343
|
+
excess -= shift;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
// A transfer of mine into it carries fewer (the latest first; No-Split: a whole one only): the
|
|
347
|
+
// armies stay where they stood rather than arrive and move back. Its own line is retold.
|
|
348
|
+
for (let j = moves.length - 1; j >= 0 && excess > 0; j--) {
|
|
349
|
+
const m = moves[j];
|
|
350
|
+
if (m.kind !== 'transfer' || m.to !== t)
|
|
351
|
+
continue;
|
|
352
|
+
const k = Math.min(excess, m.armies);
|
|
353
|
+
if (noSplit && k < m.armies)
|
|
354
|
+
continue;
|
|
355
|
+
retell(pc, ps, m, m.armies - k);
|
|
356
|
+
m.armies -= k;
|
|
357
|
+
if (!m.armies)
|
|
358
|
+
moves.splice(j, 1);
|
|
359
|
+
avail.set(m.from, (avail.get(m.from) ?? 0) + k);
|
|
360
|
+
if (hd.arrived.has(t))
|
|
361
|
+
hd.arrived.set(t, Math.max(0, hd.arrived.get(t) - k));
|
|
362
|
+
if (m.reason === WITHDRAWS && incoming.has(t))
|
|
363
|
+
incoming.set(t, Math.max(0, incoming.get(t) - k));
|
|
364
|
+
excess -= k;
|
|
365
|
+
trimmed.push(`move ${k} fewer from ${label(m.from)} into it`);
|
|
366
|
+
}
|
|
367
|
+
if (step !== undefined) {
|
|
368
|
+
// Then the armies standing there, unless No-Split would send the whole stack.
|
|
369
|
+
const k = Math.min(excess, avail.get(t));
|
|
370
|
+
const busy = moves.some((m) => m.from === t);
|
|
371
|
+
if (k > 0 && !(noSplit && (k < avail.get(t) || busy))) {
|
|
372
|
+
avail.set(t, avail.get(t) - k);
|
|
373
|
+
// One order per pair: a transfer already planned there carries them too.
|
|
374
|
+
const same = moves.find((m) => m.from === t && m.to === step);
|
|
375
|
+
if (same)
|
|
376
|
+
same.armies += k;
|
|
377
|
+
else
|
|
378
|
+
moves.push({
|
|
379
|
+
from: t,
|
|
380
|
+
to: step,
|
|
381
|
+
armies: k,
|
|
382
|
+
kind: 'transfer',
|
|
383
|
+
reason: `keeps the truce's border cap of ${cap} with player ${r.player} on ${label(t)}`,
|
|
384
|
+
});
|
|
385
|
+
hd.arrived.set(step, (hd.arrived.get(step) ?? 0) + k);
|
|
386
|
+
moved.push({ from: t, to: step, armies: k });
|
|
387
|
+
excess -= k;
|
|
388
|
+
said.push(`move ${k} back to ${label(step)}`);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
if (moved.length)
|
|
392
|
+
tp.capMoved.set(i, moved);
|
|
393
|
+
if (said.length || trimmed.length)
|
|
394
|
+
rationale.push(`Truce with player ${r.player} (border cap ${cap}): ${already ? `${label(t)} already stands above the cap with ${armiesOf(t)}, so` : `my orders would leave more than ${cap} on ${label(t)}, so`} ${[...trimmed, ...said].join(' and ')}${said.length && !facesOther ? ', toward another front' : ''} (T5).`);
|
|
395
|
+
if (excess > 0) {
|
|
396
|
+
const dep = deployed.get(t) ?? 0;
|
|
397
|
+
const why = step === undefined && dep > 0
|
|
398
|
+
? `: this plan's own deploy of ${dep} there (for its hold, or the leftover)${armiesOf(t) > cap ? `, on top of the ${armiesOf(t)} already standing there,` : ''} is what exceeds it, and no territory of mine one step behind can take it, so these orders break a term of the truce — keep it by deploying less there, or give notice (notice-by-me) and ask the owner before exceeding the cap; never break it silently.`
|
|
399
|
+
: step === undefined
|
|
400
|
+
? ' (no territory of mine one step behind to move them to)'
|
|
401
|
+
: noSplit
|
|
402
|
+
? ' (No-Split: an order sends the whole stack)'
|
|
403
|
+
: noRoom
|
|
404
|
+
? ` (Local Deployment: the pools had room for only ${noRoom.shift} of the ${noRoom.off} deployed there on ${label(step)}, and the armies there cannot move again this turn)`
|
|
405
|
+
: ' (the armies there cannot move again this turn)';
|
|
406
|
+
warnings.push(why.endsWith('.')
|
|
407
|
+
? `Truce with player ${r.player}: my orders leave ${excess} above the agreed border cap of ${cap} on ${label(t)}${why}`
|
|
408
|
+
: `Truce with player ${r.player}: my orders leave ${excess} above the agreed border cap of ${cap} on ${label(t)}${why}: that breaks a term of the truce.`);
|
|
409
|
+
}
|
|
410
|
+
if (facesOther) {
|
|
411
|
+
// The cap's cost: what it leaves no longer holds the border against another opponent.
|
|
412
|
+
const f = threats.forceAt(t, (n) => {
|
|
413
|
+
const st = state.get(n);
|
|
414
|
+
return st?.owner.kind === 'player' && truce.partners.has(st.owner.playerId);
|
|
415
|
+
}, { trend: true, allPlayers: true });
|
|
416
|
+
const need = f && !truce.partners.has(f.player) ? Math.max(0, threats.holdArmies(f.threat) - unitsAt(t)) : 0;
|
|
417
|
+
const left = endsAt(pc, ps, t);
|
|
418
|
+
if (f && need > 0 && left < need && before >= need)
|
|
419
|
+
warnings.push(`Truce with player ${r.player} (border cap ${cap}): ${label(t)} also faces player ${f.player}, and the ${left} the cap leaves there do not hold it — its hold needs ${need} against player ${f.player}'s ~${f.threat}. Keep the term, or give notice (notice-by-me) and ask the owner before exceeding the cap; never break it silently.`);
|
|
420
|
+
}
|
|
421
|
+
if (pc.myCommanders.has(t)) {
|
|
422
|
+
// T3 keeps my commander's full hold against the partner; the cap may still thin it below that.
|
|
423
|
+
const f = threats.forceAt(t, (n) => {
|
|
424
|
+
const st = state.get(n);
|
|
425
|
+
return !(st?.owner.kind === 'player' && st.owner.playerId === r.player);
|
|
426
|
+
}, { trend: true, allPlayers: true, full: true });
|
|
427
|
+
const need = f ? Math.max(0, threats.holdArmies(f.threat) - unitsAt(t)) : 0;
|
|
428
|
+
const left = endsAt(pc, ps, t);
|
|
429
|
+
if (f && need > 0 && left < need && before >= need)
|
|
430
|
+
warnings.push(`Truce with player ${r.player} (border cap ${cap}): my commander stands on ${label(t)}, and the ${left} the cap leaves there do not hold it against player ${r.player}'s full attack — its hold needs ${need} against ~${f.threat}. Keep the term, or give notice (notice-by-me) and ask the owner before exceeding the cap; never break it silently.`);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
/** Rule 1b's withdrawal reason (holds.ts), which the line retold below is keyed on. */
|
|
436
|
+
const WITHDRAWS = 'withdraws from a conceded border';
|
|
437
|
+
/**
|
|
438
|
+
* T5: a transfer the cap trims now carries `armies`: its own rationale line says so, or goes with
|
|
439
|
+
* it (and so does a dropped withdrawal's Order Priority warning) — the lines rule 1b, #125's staging
|
|
440
|
+
* and rule 6 wrote for it, as stand-still.ts's reclaim finds them.
|
|
441
|
+
*/
|
|
442
|
+
function retell(pc, ps, m, armies) {
|
|
443
|
+
const { label } = pc;
|
|
444
|
+
const { rationale, warnings } = ps;
|
|
445
|
+
const verb = m.reason === WITHDRAWS ? 'Withdraw' : m.reason === 'moves idle interior armies toward the front' ? 'Transfer' : 'Move';
|
|
446
|
+
const head = `${verb} ${m.armies} from ${label(m.from)} to ${label(m.to)}:`;
|
|
447
|
+
const r = rationale.findIndex((l) => l.startsWith(head));
|
|
448
|
+
if (r >= 0) {
|
|
449
|
+
if (armies)
|
|
450
|
+
rationale[r] = `${verb} ${armies} from ${label(m.from)} to ${label(m.to)}:${rationale[r].slice(head.length)}`;
|
|
451
|
+
else
|
|
452
|
+
rationale.splice(r, 1);
|
|
453
|
+
}
|
|
454
|
+
if (!armies && m.reason === WITHDRAWS) {
|
|
455
|
+
const w = warnings.findIndex((l) => l.startsWith(`The withdrawal from ${label(m.from)} only executes`));
|
|
456
|
+
if (w >= 0)
|
|
457
|
+
warnings.splice(w, 1);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
/** The trust reasons that make the plan advise giving notice (T7). */
|
|
461
|
+
const NOTICE_REASONS = ['endgame', 'outgrowing'];
|
|
462
|
+
/**
|
|
463
|
+
* T2's armies freed on `relaxed` (one relation's): per border, the full hold's shortfall less the
|
|
464
|
+
* truce hold's — only where the plan without the truce would have funded the full hold. A border
|
|
465
|
+
* this plan concedes against the smaller threat is conceded against the full one too, and one whose
|
|
466
|
+
* full hold rule 1a would refuse (more than {@link CONCEDE_FACTOR} × its worth, or more than my
|
|
467
|
+
* income) gets nothing there either way: neither frees an army. Each border's figure is capped at
|
|
468
|
+
* what the plan's orders really leave it short of the full hold (`ps`'s deploys and moves), so armies
|
|
469
|
+
* that end there anyway — the leftover, a #172 two-step keep, a transfer in — are not counted as
|
|
470
|
+
* freed. At most my income in all.
|
|
471
|
+
*/
|
|
472
|
+
export function armiesFreed(pc, ps, threats, relaxed, hold) {
|
|
473
|
+
const { armiesOf, unitsAt, income, paybackTurns, profile } = pc;
|
|
474
|
+
const { forceAt, holdArmies } = threats;
|
|
475
|
+
/** What a hold of `t` needs beyond the armies there against `threat`. */
|
|
476
|
+
const short = (t, threat) => Math.max(0, holdArmies(threat) - armiesOf(t) - unitsAt(t));
|
|
477
|
+
let freed = 0;
|
|
478
|
+
for (const t of relaxed) {
|
|
479
|
+
if (hold.conceded.has(t))
|
|
480
|
+
continue;
|
|
481
|
+
const full = forceAt(t, () => false, { trend: true, allPlayers: true, full: true });
|
|
482
|
+
const now = forceAt(t, () => false, { trend: true, allPlayers: true });
|
|
483
|
+
if (!full || !now)
|
|
484
|
+
continue;
|
|
485
|
+
const fullShort = short(t, full.threat);
|
|
486
|
+
const worth = hold.valueOf(t) + armiesOf(t) / paybackTurns;
|
|
487
|
+
// Rule 1a's economic test, as holdBasics' `uneconomic` reads it (#122: not in the opening when
|
|
488
|
+
// the hold costs no more than the armies already there).
|
|
489
|
+
const uneconomic = fullShort > CONCEDE_FACTOR * worth && !(profile.phase === 'opening' && fullShort <= armiesOf(t));
|
|
490
|
+
if (fullShort > income || uneconomic)
|
|
491
|
+
continue;
|
|
492
|
+
// What the plan's orders really leave short of the full hold on `t`.
|
|
493
|
+
const leftShort = Math.max(0, holdArmies(full.threat) - endsAt(pc, ps, t) - unitsAt(t));
|
|
494
|
+
freed += Math.min(Math.max(0, fullShort - short(t, now.threat)), leftShort);
|
|
495
|
+
}
|
|
496
|
+
return Math.min(freed, income);
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* The report (T2's armies freed, T3's full holds, T5's moves, T7's advice, T8's breaks), and the
|
|
500
|
+
* warnings and rationale that say what the truces did. Run before the plan is finished — also for a
|
|
501
|
+
* plan holding no territory (`tp` undefined, nothing relaxed); with no relations it returns nothing
|
|
502
|
+
* and says nothing. `freedOn` is {@link armiesFreed} for one relation's relaxed borders.
|
|
503
|
+
*/
|
|
504
|
+
/** T8: `warapp_submit_turn` reads only the ledger, so a break the plan acts on must be recorded first. */
|
|
505
|
+
const RECORD_BROKEN = (r) => `Record it with warapp_record_agreement (${r.agreement ? `agreement_id ${r.agreement}, ` : ''}status broken-by-them) before submitting: until then warapp_submit_turn refuses an order on its covered territories.`;
|
|
506
|
+
export function reportTruce(pc, ps, tp, freedOn) {
|
|
507
|
+
const { truce, label, turnNow } = pc;
|
|
508
|
+
if (!truce.given.length)
|
|
509
|
+
return undefined;
|
|
510
|
+
const { warnings, rationale } = ps;
|
|
511
|
+
if (truce.status.some((s) => s === 'ignored'))
|
|
512
|
+
warnings.push(`Truces ignored (B2): ${truce.format === 'teams' ? 'this is a team game' : 'only two players are in this game'}, and the planner honours a truce only in a free-for-all; every player is planned as an ordinary opponent, except that the covered territories of an active or in-notice agreement are still never a target (T1: the plan never orders a surprise break).`);
|
|
513
|
+
const advice = [];
|
|
514
|
+
const relations = truce.given.map((r, i) => {
|
|
515
|
+
const st = truce.status[i];
|
|
516
|
+
const flags = [...(r.watch ?? [])];
|
|
517
|
+
const name = r.card ? `Diplomacy card with player ${r.player}` : `Truce with player ${r.player}${r.agreement ? ` (agreement ${r.agreement})` : ''}`;
|
|
518
|
+
const mineRelaxed = tp ? [...tp.relaxed].filter(([t, p]) => p === r.player && r.coveredMine.includes(t)).map(([t]) => t).sort(byNum) : [];
|
|
519
|
+
const fullHold = tp
|
|
520
|
+
? [...tp.fullHold]
|
|
521
|
+
.filter(([t, f]) => f.player === r.player && r.coveredMine.includes(t))
|
|
522
|
+
.map(([territory, f]) => ({ territory, why: f.why }))
|
|
523
|
+
.sort((a, b) => a.territory - b.territory)
|
|
524
|
+
: [];
|
|
525
|
+
const freed = mineRelaxed.length ? freedOn(mineRelaxed) : 0;
|
|
526
|
+
if (st === 'broken' && r.breach)
|
|
527
|
+
warnings.push(`${name} is broken by them: ${r.breach.term} on turn ${r.breach.turn}${r.breach.territories.length ? ` (${r.breach.territories.map(label).join(', ')})` : ''} — ${r.breach.evidence}. Player ${r.player} is planned as an ordinary opponent from this turn (T8). ${RECORD_BROKEN(r)}`);
|
|
528
|
+
else if (st === 'broken')
|
|
529
|
+
warnings.push(`${name} is broken by them: player ${r.player} is planned as an ordinary opponent from this turn (T8). ${RECORD_BROKEN(r)}`);
|
|
530
|
+
else if (st === 'over' && r.card)
|
|
531
|
+
rationale.push(`${name}: it is gone by this turn, so player ${r.player} is an ordinary opponent (T9).`);
|
|
532
|
+
else if (st === 'over')
|
|
533
|
+
rationale.push(`${name}: its notice ran out on turn ${r.endsTurn}, so player ${r.player} is an ordinary opponent this turn (T7).`);
|
|
534
|
+
else if (st === 'not-a-foe')
|
|
535
|
+
warnings.push(`${name}: player ${r.player} is me or a teammate, so the truce changes nothing.`);
|
|
536
|
+
else if (st === 'active' || st === 'notice') {
|
|
537
|
+
const enforced = truce.enforced.has(r.player);
|
|
538
|
+
const notice = st === 'notice' && r.endsTurn !== undefined ? ` (in notice${r.noticeBy ? ` given by ${r.noticeBy === 'me' ? 'me' : 'them'}` : ''}: binding until turn ${r.endsTurn})` : '';
|
|
539
|
+
rationale.push(enforced
|
|
540
|
+
? `${name}${notice}: a Diplomacy card is in effect, so player ${r.player}'s attacks on me do nothing this turn — its threat is 0 and no attack on it is planned (T9)` +
|
|
541
|
+
(truce.lookAheadFoes.has(r.player)
|
|
542
|
+
? `; the card is gone by turn ${r.enforcedUntil}, so its stacks still count for next turn's look-ahead.`
|
|
543
|
+
: r.enforcedUntil !== undefined
|
|
544
|
+
? `; the card lasts until turn ${r.enforcedUntil}.`
|
|
545
|
+
: '.')
|
|
546
|
+
: `${name}${notice}: none of its ${r.coveredTheirs.length} covered ${r.coveredTheirs.length === 1 ? 'territory is' : 'territories are'} a target this turn (T1), and player ${r.player} is no front for the no-second-front rule or the idle armies (T4).`);
|
|
547
|
+
if (mineRelaxed.length)
|
|
548
|
+
rationale.push(`${name}: ${mineRelaxed.map(label).join(', ')} ${mineRelaxed.length === 1 ? 'faces' : 'face'} only player ${r.player}, so ${mineRelaxed.length === 1 ? 'it is' : 'they are'} held against its visible stacks there attacking with no deploy (T2) — a break needs a visible build-up first; ${freed} ${freed === 1 ? 'army' : 'armies'} freed this turn.`);
|
|
549
|
+
const why = { commander: 'my commander stands there', value: `it protects more than ${TRUCE_RELAX_VALUE} × my income`, watch: `the trust is watch (${flags.join(', ') || 'a flag'})` };
|
|
550
|
+
for (const f of fullHold)
|
|
551
|
+
rationale.push(`${name}: ${label(f.territory)} keeps its full hold against player ${r.player}'s deploy: ${why[f.why]} (T3).`);
|
|
552
|
+
if (r.trust === 'watch' && flags.some((x) => NOTICE_REASONS.includes(x)) && !(st === 'notice' && r.noticeBy === 'me')) {
|
|
553
|
+
const reason = flags.includes('endgame') ? 'the endgame: no other opponent is still a match for either of us, so the partner no longer needs me' : 'player ' + r.player + ' is outgrowing me';
|
|
554
|
+
advice.push({ kind: 'give-notice', player: r.player, ...(r.agreement ? { agreement: r.agreement } : {}), why: reason });
|
|
555
|
+
warnings.push(`${name}: give notice (T7) — ${reason}. The plan never breaks it by surprise: it stays binding until its notice runs${turnNow !== null ? ` (counted from the turn notice is given, after turn ${turnNow})` : ''}.`);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
return {
|
|
559
|
+
player: r.player,
|
|
560
|
+
...(r.agreement ? { agreement: r.agreement } : {}),
|
|
561
|
+
state: st,
|
|
562
|
+
honoured: truce.binds[i],
|
|
563
|
+
enforced: truce.binds[i] && truce.enforced.has(r.player),
|
|
564
|
+
...(r.enforcedUntil !== undefined ? { enforcedUntil: r.enforcedUntil } : {}),
|
|
565
|
+
...(r.card ? { card: r.card } : {}),
|
|
566
|
+
trust: r.trust,
|
|
567
|
+
flags,
|
|
568
|
+
...(r.endsTurn !== undefined ? { endsTurn: r.endsTurn } : {}),
|
|
569
|
+
relaxed: mineRelaxed,
|
|
570
|
+
fullHold,
|
|
571
|
+
armiesFreed: freed,
|
|
572
|
+
...(tp?.capMoved.get(i)?.length ? { capMoved: tp.capMoved.get(i) } : {}),
|
|
573
|
+
...(r.breach ? { breach: r.breach } : {}),
|
|
574
|
+
// T6: filled in once the plan's orders are final (truceBreakLosses).
|
|
575
|
+
lossIfBroken: null,
|
|
576
|
+
};
|
|
577
|
+
});
|
|
578
|
+
return { relations, advice };
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* T6, once the plan's orders are final: for each binding truce that is not enforced, what a break
|
|
582
|
+
* would cost this turn. The partner lands its whole deploy estimate (`deployOf`) on any territory of
|
|
583
|
+
* its in view beside a covered territory of mine — covered by the truce or not, since T2 relaxes that
|
|
584
|
+
* border against every stack of the partner next to it — and attacks it with everything there (its
|
|
585
|
+
* commander too), against this plan's deploys and moves; each such pair is carried out on the turn
|
|
586
|
+
* simulator, with me moving first and with the partner first unless the first mover is known (and
|
|
587
|
+
* verified), and the costliest is kept: my commander, then the income lost, the territories, the armies,
|
|
588
|
+
* then ascending ids. Per relation index (parallel to `truce.given`): `null` where nothing binds or no
|
|
589
|
+
* such pair exists. Only the partner moves besides me: every other player is left out of the break.
|
|
590
|
+
*/
|
|
591
|
+
export function truceBreakLosses(pc, plan) {
|
|
592
|
+
const { truce, g, board, me, s, opts, deployOf } = pc;
|
|
593
|
+
if (!truce.given.length)
|
|
594
|
+
return [];
|
|
595
|
+
const start = simTerritories(board);
|
|
596
|
+
const settings = {
|
|
597
|
+
...(s.offensiveKillRate !== undefined ? { offensiveKillRate: s.offensiveKillRate } : {}),
|
|
598
|
+
...(s.defensiveKillRate !== undefined ? { defensiveKillRate: s.defensiveKillRate } : {}),
|
|
599
|
+
...(s.luckModifier !== undefined ? { luckModifier: s.luckModifier } : {}),
|
|
600
|
+
...(s.roundingMode !== undefined ? { roundingMode: s.roundingMode } : {}),
|
|
601
|
+
...(s.oneArmyStandsGuard !== undefined ? { oneArmyStandsGuard: s.oneArmyStandsGuard } : {}),
|
|
602
|
+
...(s.multiAttack !== undefined ? { multiAttack: s.multiAttack } : {}),
|
|
603
|
+
};
|
|
604
|
+
const myDeploys = plan.deploys.filter((d) => d.armies > 0).map((d) => ({ player: me, territoryId: d.territoryId, armies: d.armies }));
|
|
605
|
+
const myOrders = plan.moves.map((m) => ({ from: m.from, to: m.to, armies: m.armies }));
|
|
606
|
+
const run = (partner, from, to, deploy, order) => simulateTurn({
|
|
607
|
+
graph: g,
|
|
608
|
+
board,
|
|
609
|
+
deploys: [...myDeploys, ...(deploy > 0 ? [{ player: partner, territoryId: from, armies: deploy }] : [])],
|
|
610
|
+
orders: [
|
|
611
|
+
{ player: me, orders: myOrders },
|
|
612
|
+
{ player: partner, orders: [{ from, to, armies: (start.get(from)?.armies ?? 0) + deploy, withCommander: true }] },
|
|
613
|
+
],
|
|
614
|
+
moveOrder: order,
|
|
615
|
+
settings,
|
|
616
|
+
});
|
|
617
|
+
const baseline = simulateTurn({ graph: g, board, deploys: myDeploys, orders: [{ player: me, orders: myOrders }], moveOrder: [me], settings });
|
|
618
|
+
const mineAfter = (r) => new Set([...r.territories].filter(([, t]) => t.owner === me).map(([id]) => id));
|
|
619
|
+
const armiesAfter = (r) => [...r.territories.values()].reduce((n, t) => n + (t.owner === me ? t.armies : 0), 0);
|
|
620
|
+
const bonusIds = [...g.bonusById.keys()].sort(byNum);
|
|
621
|
+
const heldBonuses = (held) => bonusIds.filter((id) => {
|
|
622
|
+
const ts = bonusTerritories(g, id);
|
|
623
|
+
return ts.length > 0 && ts.every((t) => held.has(t));
|
|
624
|
+
});
|
|
625
|
+
const baseMine = mineAfter(baseline);
|
|
626
|
+
const baseBonuses = heldBonuses(baseMine);
|
|
627
|
+
const baseArmies = armiesAfter(baseline);
|
|
628
|
+
const known = opts.movesFirst !== undefined && !opts.movesFirstUnverified;
|
|
629
|
+
const worse = (a, b) => Number(a.commanderLost) - Number(b.commanderLost) ||
|
|
630
|
+
Math.max(0, a.bonusValue) - Math.max(0, b.bonusValue) ||
|
|
631
|
+
a.territories.length - b.territories.length ||
|
|
632
|
+
a.armies - b.armies;
|
|
633
|
+
return truce.given.map((r, i) => {
|
|
634
|
+
if (!truce.binds[i] || truce.enforced.has(r.player))
|
|
635
|
+
return null;
|
|
636
|
+
// A break is an attack on my covered territory wherever it comes from: T2 relaxes such a border
|
|
637
|
+
// against every stack of the partner next to it, so every one of them is tried, covered or not.
|
|
638
|
+
const mineCovered = new Set(r.coveredMine.filter((t) => start.get(t)?.owner === me));
|
|
639
|
+
const theirs = [...new Set([...mineCovered].flatMap((t) => [...neighborsOf(g, t)]))]
|
|
640
|
+
.filter((t) => start.get(t)?.owner === r.player)
|
|
641
|
+
.sort(byNum);
|
|
642
|
+
const deploy = Math.max(0, Math.floor(deployOf(r.player)));
|
|
643
|
+
let best = null;
|
|
644
|
+
for (const from of theirs) {
|
|
645
|
+
for (const to of [...neighborsOf(g, from)].filter((n) => mineCovered.has(n)).sort(byNum)) {
|
|
646
|
+
const orders = known
|
|
647
|
+
? [{ who: opts.movesFirst ? 'me' : 'partner', order: opts.movesFirst ? [me, r.player] : [r.player, me] }]
|
|
648
|
+
: [
|
|
649
|
+
{ who: 'me', order: [me, r.player] },
|
|
650
|
+
{ who: 'partner', order: [r.player, me] },
|
|
651
|
+
];
|
|
652
|
+
const outcomes = orders.map(({ who, order }) => {
|
|
653
|
+
const res = run(r.player, from, to, deploy, order);
|
|
654
|
+
const after = mineAfter(res);
|
|
655
|
+
const held = new Set(heldBonuses(after));
|
|
656
|
+
const bonuses = baseBonuses.filter((b) => !held.has(b)).map((id) => ({ id, value: g.bonusById.get(id)?.value ?? 0 }));
|
|
657
|
+
const loss = {
|
|
658
|
+
from,
|
|
659
|
+
to,
|
|
660
|
+
deploy,
|
|
661
|
+
attackers: res.log.find((e) => e.player === r.player && e.index === 0)?.sent ?? 0,
|
|
662
|
+
territories: [...baseMine].filter((t) => !after.has(t)).sort(byNum),
|
|
663
|
+
armies: Math.max(0, baseArmies - armiesAfter(res)),
|
|
664
|
+
bonuses,
|
|
665
|
+
bonusValue: bonuses.reduce((n, b) => n + b.value, 0),
|
|
666
|
+
commanderLost: res.eliminated.includes(me),
|
|
667
|
+
exact: res.exact,
|
|
668
|
+
};
|
|
669
|
+
return { who, loss };
|
|
670
|
+
});
|
|
671
|
+
const top = outcomes.reduce((a, b) => (worse(b.loss, a.loss) > 0 ? b : a));
|
|
672
|
+
const same = outcomes.every((o) => JSON.stringify(o.loss) === JSON.stringify(top.loss));
|
|
673
|
+
const loss = !known && !same ? { ...top.loss, firstMover: top.who } : top.loss;
|
|
674
|
+
if (!best || worse(loss, best) > 0)
|
|
675
|
+
best = loss;
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
return best;
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* T6's report: each binding truce's {@link truceBreakLosses} on its view, and a rationale line that
|
|
683
|
+
* says what a break would take. Nothing with no relations.
|
|
684
|
+
*/
|
|
685
|
+
export function reportBreakLosses(pc, views, plan) {
|
|
686
|
+
const losses = truceBreakLosses(pc, plan);
|
|
687
|
+
const { label } = pc;
|
|
688
|
+
views.forEach((v, i) => {
|
|
689
|
+
const loss = losses[i] ?? null;
|
|
690
|
+
v.lossIfBroken = loss;
|
|
691
|
+
if (!loss)
|
|
692
|
+
return;
|
|
693
|
+
const name = `Truce with player ${v.player}${v.agreement ? ` (agreement ${v.agreement})` : ''}`;
|
|
694
|
+
const lost = loss.commanderLost
|
|
695
|
+
? 'my commander, and with it everything'
|
|
696
|
+
: loss.territories.length
|
|
697
|
+
? `${loss.territories.map(label).join(', ')}, ${loss.armies} ${loss.armies === 1 ? 'army' : 'armies'} and +${loss.bonusValue} income`
|
|
698
|
+
: `no territory (${loss.armies} ${loss.armies === 1 ? 'army' : 'armies'})`;
|
|
699
|
+
const when = loss.firstMover ? ` (if ${loss.firstMover === 'me' ? 'I move' : 'it moves'} first)` : '';
|
|
700
|
+
plan.rationale.push(`${name}: if it broke this turn — its ~${loss.deploy} deploy on ${label(loss.from)} and an attack on ${label(loss.to)} with ${loss.attackers} — I would lose ${lost}${when} (T6).`);
|
|
701
|
+
});
|
|
702
|
+
}
|