election-game/backend/packages/rogue/src/rival.ts

265 lines
14 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<RivalStyle, { name: string; text: string }> = {
balanced: { name: "後發制人", text: "前期比較弱,每週變強的幅度比較大" },
ground: { name: "組織戰", text: "佈樁的盾 ×1.4,常常跑基層" },
air: { name: "空戰", text: "電視、網路廣告效果 ×1.2" },
};
const STYLE_WEIGHTS: Record<RivalStyle, Record<RogueIntentKind, number>> = {
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 },
};
/**
* 對手規則(docs/25 第 3 節,參考《小丑牌》的 Boss 盲注):開局抽 1 條(逆風 5 起 2 條),整局有效、畫面上一直看得到。
* 對手的風格決定比較常抽到哪幾條。規則對所有候選人一樣,只是機制,不描述真人。
*/
export const RIVAL_RULES: Record<string, { name: string; text: string; weight: Record<RivalStyle, number> }> = {
pundit: { name: "名嘴加持", text: "對手的批評 ×1.5;你澄清、事實查核的時候,對手聲勢 −1", weight: { balanced: 1, ground: 0.6, air: 1.5 } },
elder: { name: "地方大老", text: "對手插旗的區,你要領先 12 個百分點(一般 8)才翻得過來", weight: { balanced: 1, ground: 1.5, air: 0.6 } },
airwar: { name: "大打空戰", text: "對手每週多一個電視或網路廣告", weight: { balanced: 1, ground: 0.5, air: 1.5 } },
stronghold: { name: "鐵票區", text: "對手的本命區每週自動有一座樁腳", weight: { balanced: 1, ground: 1.5, air: 0.8 } },
rumor: { name: "網路謠言", text: "每週末隨機一區你的支持者被拉走一點;那週打探過對手就沒事", weight: { balanced: 1, ground: 0.8, air: 1.3 } },
carpet: { name: "地毯式掃街", text: "對手的掃街會擴散到隔壁區兩成", weight: { balanced: 1, ground: 1.5, air: 0.5 } },
image: { name: "形象牌", text: "你的爆料、比較文宣效果只剩一半;電視辯論贏的回合多 50%", weight: { balanced: 1, ground: 1, air: 1 } },
};
/** 網路謠言:每週被拉走的票(以全市 100 萬人為基準) */
const RUMOR_BASE = 900;
export const hasRule = (s: RunState, id: string): boolean => (s.rivalRules ?? []).includes(id);
/** 開局抽對手規則(新手教學沒有) */
export function drawRivalRules(s: RunState): string[] {
if (s.options.tutorial) return [];
const n = asc(s, 5) ? 2 : 1;
const out: string[] = [];
for (let k = 0; k < n; k++) {
const id = pickWeighted(s, Object.keys(RIVAL_RULES).filter((x) => !out.includes(x)), (x) => RIVAL_RULES[x]!.weight[s.setup.rivalStyle]);
if (id) out.push(id);
}
return out;
}
/** 每週開始:鐵票區(對手本命區自動有樁腳) */
export function applyRivalRules(s: RunState): void {
if (!hasRule(s, "stronghold")) return;
for (const t of s.towns.filter((x) => x.home === "rival")) {
t.rivalShield += intentAmount(s, "stake", t);
s.log.push({ side: "rival", text: `鐵票區:${s.setup.rival.name}的本命區${t.name}樁腳到位`, town: t.code, votes: 0 });
}
}
/** 週末:網路謠言(那週打探過對手就沒事) */
export function rumorAtWeekend(s: RunState, probed: boolean): void {
if (!hasRule(s, "rumor")) return;
if (probed) {
s.log.push({ side: "you", text: "網路謠言:你這週打探過對手,謠言沒有擴散", town: null, votes: 0 });
return;
}
const t = pickWeighted(s, s.towns, (x) => x.you) ?? s.towns[0]!;
const lost = loseVotes(t, RUMOR_BASE * s.scale * t.reach, "you");
s.log.push({ side: "rival", text: `網路謠言在${t.name}流傳`, town: t.code, votes: -Math.round(lost) });
}
/** 每週最多幾個同一類的行動 */
const LIMITS: Partial<Record<RogueIntentKind, number>> = { 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<string>): 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;
const pundit = kind === "attack" && hasRule(s, "pundit") ? 1.5 : 1;
return round10(INTENT_BASE[kind] * rivalPower(s) * reach * eventMult(s, INTENT_TAGS[kind]) * styleMult(s, kind) * home * weak * pundit);
}
export function planIntents(s: RunState): Intent[] {
const weights = STYLE_WEIGHTS[s.setup.rivalStyle];
const count = new Map<RogueIntentKind, number>();
const used = new Set<string>();
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 });
}
// 大打空戰:每週多一個電視或網路廣告(這週還沒排的那一種)
if (hasRule(s, "airwar")) {
const kind = !count.get("tv") ? "tv" : !count.get("online") ? "online" : null;
if (kind) out.push({ id: `i${s.week}-air`, kind, town: null, amount: intentAmount(s, kind, null), 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 === "attack" && s.turnaround) {
const got = cityVotes(s, prevented * 0.5, "you");
s.log.push({ side: "you", text: "反將一軍:擋下的批評變成你的票", town: null, votes: Math.round(got) });
}
}
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 (blocked > 0 && has(s, "faction_broker")) {
const got = moveVotes(t, blocked * 0.5, "you");
if (got >= 1) s.log.push({ side: "you", text: `派系協調人・${t.name}`, town: t.code, votes: Math.round(got) });
}
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);
// 地毯式掃街:擴散到隔壁區兩成(隔壁區的樁腳照樣先擋)
if (i.kind === "canvass" && hasRule(s, "carpet")) {
for (const code of t.neighbors) {
const n = s.towns.find((x) => x.code === code);
if (!n) continue;
const spill = amount * 0.2;
const stop = Math.min(n.shield, spill);
n.shield -= stop;
const got = moveVotes(n, spill - stop, "rival");
if (got >= 1) s.log.push({ side: "rival", text: `${name}地毯式掃街擴散到${n.name}`, town: n.code, votes: -Math.round(got) });
}
}
}