Files
Dune/game/bot-battle-choices.ts
T
Quintine 28ba1618ba Implement Homeworld mechanics through Caladan reinforcement checkpoint
Connect typed Homeworld custody, transport, battle, income, revival and Caladan reinforcement flows with private multiplayer recovery and controls. Preserve the optimized test workflow and keep unfinished rules modes gated. Verified with 3682 offline tests, typecheck, lint, production build and 40 HTTP tests; existing saved games remain unchanged.
2026-09-10 08:12:26 +10:00

59 lines
2.0 KiB
TypeScript

import type { Action, GameView } from './engine';
import { fighterCount } from './advisors';
import { presenceByLocation } from './force-presence';
import {
gameDistance,
gameTerritories,
MOBILE_LOCATION,
splitLocation,
} from './board';
/** Public fighter locations and storm order determine which battle may be chosen. */
export function botBattleChoices(g: GameView): Action[] {
const me = g.players.find((p) => p.id === g.me)!;
if (g.active !== me.id) return [];
if (Array.isArray(g.battleChoices))
return g.battleChoices
.filter((battle) => battle.attacker === me.id)
.map((battle) => ({
type: 'chooseBattle',
territory: battle.territory,
target: battle.defender,
}));
// Legacy component fixtures may predate the authoritative combat frontier.
const ownLocations = Object.keys(presenceByLocation(me));
return gameTerritories(g)
.filter((t) => t.type !== 'polar' && fighterCount(me, t.id) > 0)
.flatMap((t) =>
g.players
.filter(
(p) =>
p.id !== me.id &&
p.id !== me.ally &&
fighterCount(p, t.id) > 0 &&
g.order.indexOf(p.id) >= g.order.indexOf(me.id) &&
ownLocations
.filter((key) => splitLocation(key).territory === t.id)
.some((from) =>
Object.keys(presenceByLocation(p))
.filter((key) => splitLocation(key).territory === t.id)
.some(
(to) =>
gameDistance(
g,
from,
to,
(key) =>
splitLocation(
key === MOBILE_LOCATION
? (g.mobileStronghold?.location ?? key)
: key,
).sector === g.storm,
) === 0,
),
),
)
.map((p) => ({ type: 'chooseBattle', territory: t.id, target: p.id })),
);
}