import type { RogueIntentKind } from "@election2026/protocol"; import { RIVAL_HOME_MULT } from "./archetypes.ts"; import { INTENT_BASE, INTENT_LABELS, INTENT_TAGS } from "./data.ts"; import { addRivalMomentum, addStatus, asc, cityVotes, eventMult, eventOf, has, isArch, loseVotes, moveVotes, spread } from "./mechanics.ts"; import { pickWeighted, round10, type Intent, type RivalStyle, type RunState, type TownState } from "./state.ts"; /** * 對手 AI(docs/21、22):每週一開始就把要做的事亮在地圖上(像《殺戮尖塔》的敵人意圖),週末才執行。 * 只看公開的資訊:各區支持度、你上週把牌打在哪裡。越接近投票日行動越多、越強。 * 對手是真實人物時,行動名稱是中性的(批評你的政見、到你的票倉拉票),不寫抹黑、爆料。 */ /** 對手整體強度(平衡報告調整用) */ export const RIVAL_POWER = 0.92; /** 每週變強多少 */ export const RIVAL_GROWTH = 0.035; /** 第 5 週預告、第 7 週執行的大型造勢 */ export const BIG_RALLY_WEEK = 7; export const BIG_RALLY_MULT = 2; /** 對手性格:開局亮出來,影響出招比例與被動能力 */ export const RIVAL_TRAITS: Record = { balanced: { name: "後發制人", text: "前期比較弱,每週變強的幅度比較大" }, ground: { name: "組織戰", text: "佈樁的盾 ×1.4,常常跑基層" }, air: { name: "空戰", text: "電視、網路廣告效果 ×1.2" }, }; const STYLE_WEIGHTS: Record> = { balanced: { canvass: 3, rally: 1.5, tv: 1.5, online: 1.2, attack: 1, smear: 0.8, stake: 1 }, ground: { canvass: 4, rally: 2, tv: 0.8, online: 0.6, attack: 0.8, smear: 1, stake: 1.5 }, air: { canvass: 2, rally: 1, tv: 2.5, online: 2, attack: 1.2, smear: 0.6, stake: 0.6 }, }; /** 每週最多幾個同一類的行動 */ const LIMITS: Partial> = { rally: 1, tv: 1, online: 1, attack: 1, smear: 1, stake: 1 }; export const intentsThisWeek = (s: RunState): number => (s.week <= 2 ? 2 : s.week < s.totalWeeks ? 3 : 4) + (asc(s, 6) && s.week >= 4 ? 1 : 0); export function rivalPower(s: RunState): number { const style = s.setup.rivalStyle; const growth = style === "balanced" ? 0.05 : RIVAL_GROWTH; const start = style === "balanced" ? 0.92 : 1; // 新手教學的老里長比較弱,照提示打就會贏 const tutorial = s.options.tutorial ? 0.4 : 1; return RIVAL_POWER * start * tutorial * s.scale * (1 + growth * (s.week - 1)) * (1 + 0.08 * s.rivalMomentum) * (eventOf(s).rival ?? 1) * (asc(s, 1) ? 1.12 : 1); } /** 對手性格帶來的倍率 */ const styleMult = (s: RunState, kind: RogueIntentKind): number => { const style = s.setup.rivalStyle; if (style === "ground" && kind === "stake") return 1.4; if (style === "air" && (kind === "tv" || kind === "online")) return 1.2; return 1; }; const closeness = (t: TownState): number => 1 - Math.abs(t.you - t.rival) / (t.you + t.rival || 1); function chooseTown(s: RunState, kind: RogueIntentKind, used: ReadonlySet): TownState { const open = s.towns.filter((t) => !used.has(t.code)); const pool = open.length ? open : s.towns; const focus = (t: TownState) => (s.lastFocus.includes(t.code) ? 1 : 0); const weight = (t: TownState): number => { if (kind === "smear") return t.you * (1 + focus(t)) * (t.flag === "you" ? 1.5 : 1); if (kind === "stake") return t.voters * (0.5 + closeness(t)) * (1 + 2 * focus(t)); return t.voters * (0.5 + closeness(t)) * (1 + 0.8 * focus(t)); }; return pickWeighted(s, pool, weight) ?? pool[0]!; } function intentAmount(s: RunState, kind: RogueIntentKind, town: TownState | null): number { // 打在某一區的行動和你的牌一樣,人多的區效果大 const reach = town && kind !== "stake" ? town.reach : 1; // 對手在自己的本命區比較強;媒體型被批評損失比較大(docs/25) const home = town?.home === "rival" ? RIVAL_HOME_MULT : 1; const weak = kind === "attack" && isArch(s, "media") ? 1.2 : 1; return round10(INTENT_BASE[kind] * rivalPower(s) * reach * eventMult(s, INTENT_TAGS[kind]) * styleMult(s, kind) * home * weak); } export function planIntents(s: RunState): Intent[] { const weights = STYLE_WEIGHTS[s.setup.rivalStyle]; const count = new Map(); const used = new Set(); const out: Intent[] = []; // 預告過的大型造勢這週執行 if (s.nextBig && s.week === BIG_RALLY_WEEK) { const t = s.towns.find((x) => x.code === s.nextBig!.town); out.push({ ...s.nextBig, id: `i${s.week}-big`, amount: round10(intentAmount(s, "rally", t ?? null) * BIG_RALLY_MULT) }); count.set("rally", 1); if (t) used.add(t.code); s.nextBig = null; } // 你上週攻下對手的本命區:對手這週少一個行動(至少留一個) const n = Math.max(1, intentsThisWeek(s) - (s.rivalStun ?? 0)); s.rivalStun = 0; for (let k = out.length; k < n; k++) { const kinds = (Object.keys(weights) as RogueIntentKind[]).filter((kind) => { if ((count.get(kind) ?? 0) >= (LIMITS[kind] ?? 9)) return false; // 第一週還沒什麼好批評的 if ((kind === "attack" || kind === "smear") && s.week < 2) return false; return true; }); const kind = pickWeighted(s, kinds, (x) => weights[x])!; count.set(kind, (count.get(kind) ?? 0) + 1); const city = kind === "tv" || kind === "online" || kind === "attack"; const town = city ? null : chooseTown(s, kind, used); if (town) used.add(town.code); out.push({ id: `i${s.week}-${k}`, kind, town: town?.code ?? null, amount: intentAmount(s, kind, town), cut: 1 }); } return out; } /** 第 5 週:預告第 7 週的大型造勢(選人多、選情接近的區) */ export function announceBigRally(s: RunState): void { if (s.week !== BIG_RALLY_WEEK - 2 || s.totalWeeks < BIG_RALLY_WEEK) return; const town = pickWeighted(s, s.towns, (t) => t.voters * closeness(t) ** 2) ?? s.towns[0]!; s.nextBig = { id: "big", kind: "rally", town: town.code, amount: round10(intentAmount(s, "rally", town) * BIG_RALLY_MULT * (1 + RIVAL_GROWTH * 2)), cut: 1, big: true }; } /** 對手的佈樁一宣布就生效:擋你這週在那區的牌 */ export function applyRivalShields(s: RunState): void { for (const i of s.intents) { if (i.kind !== "stake") continue; const t = s.towns.find((x) => x.code === i.town); if (t) t.rivalShield += i.amount; } } export const intentLabel = (s: RunState, i: Intent): string => { const town = s.towns.find((t) => t.code === i.town)?.name; return `${i.big ? "大型" : ""}${INTENT_LABELS[i.kind]}${town ? `・${town}` : "・全市"}`; }; /** 幕僚讓對手這個行動打折的倍率:批評遇到公關高手減半、你插旗的區有鐵票部隊減半 */ export function intentDamp(s: RunState, i: Intent): number { if (i.kind === "attack") return has(s, "spin_doctor") ? 0.5 : 1; const t = s.towns.find((x) => x.code === i.town); return t && t.flag === "you" && has(s, "iron_base") ? 0.5 : 1; } /** 週末:對手照意圖行動。你在那區的樁腳先擋;批評遇到「公關高手」減半,還會塞「記者追問」 */ export function executeIntent(s: RunState, i: Intent): void { const t = s.towns.find((x) => x.code === i.town) ?? null; const amount = i.amount * i.cut * intentDamp(s, i); const name = s.setup.rival.name; const label = intentLabel(s, i); if (i.kind === "stake") return; // 打對台、澄清、事實查核讓對手少拉的票:先列出來,攻守的效果才看得到 const prevented = i.amount * (1 - i.cut) * intentDamp(s, i); if (prevented >= 1) { const how = i.kind === "attack" ? (i.cut === 0 ? "批評被你完全擋下" : "批評被你擋掉一部分") : "被你打對台"; s.log.push({ side: "you", text: `${name}${label}${how}`, town: i.town, votes: 0, blocked: Math.round(prevented) }); } if (i.kind === "tv" || i.kind === "online") { const parts = spread(s.towns, amount, (x) => x.voters * (x.traits.includes(i.kind === "tv" ? "old" : "young") ? 1.5 : 1)); const net = s.towns.reduce((a, x, k) => a + moveVotes(x, parts[k]!, "rival"), 0); s.log.push({ side: "rival", text: `${name}${label}`, town: null, votes: -Math.round(net) }); return; } if (i.kind === "attack") { if (amount <= 0) { s.log.push({ side: "you", text: `${name}${label}:已經被你擋下`, town: null, votes: 0 }); return; } const lost = cityVotes(s, -amount); s.log.push({ side: "rival", text: `${name}${label}`, town: null, votes: Math.round(lost) }); if (addStatus(s, "reporter")) s.log.push({ side: "system", text: "記者追問塞進你的牌組", town: null, votes: 0 }); s.lastWeek.attacked = true; return; } if (!t) return; const blocked = Math.min(t.shield, amount); t.shield -= blocked; const rest = amount - blocked; if (blocked > 0) s.log.push({ side: "you", text: `樁腳擋下${name}在${t.name}的${INTENT_LABELS[i.kind]}`, town: t.code, votes: 0, blocked: Math.round(blocked) }); if (i.kind === "smear") { // 到你的票倉拉票:你的支持者被拉走一部分(變回還沒表態),對手拿到一半 const lost = loseVotes(t, rest, "you"); moveVotes(t, lost * 0.5, "rival"); s.log.push({ side: "rival", text: `${name}${label}`, town: t.code, votes: -Math.round(lost) }); return; } const net = moveVotes(t, rest, "rival"); s.log.push({ side: "rival", text: `${name}${label}`, town: t.code, votes: -Math.round(net) }); if (i.kind === "rally") addRivalMomentum(s, i.big ? 3 : 2); }