Files
tidefront/packages/game-core/sim/bot.ts
T
db9979 6fe7a3bb0c Balance v1.24: Wertung nach Richtpreis, Kartell-Abwehr ×0,25, Kartell 0,40, Startrampe ab KZ 7, Kommandopunkte ab KZ 9
- Wertung: Ressourcen zum Börsen-Richtpreis (market.refPrice), Kampfpunkte gegen Kartell/NPC × scoring.npcDefenseFactor
  (Kartell-Überfall, Großflotte, Konvoi, Feld, Angriffe auf Großflotte/Belagerungsring); Punkte der laufenden Saison
  werden nicht gespeichert und gelten sofort rückwirkend
- Balance: raidStrengthShareOfDefense 0,40, Startrampe KZ 7, Rampenrabatt 0,10/0,3, Radarsatellit 5500/2200/4000,
  Nest 4 Beute ×1,13, Nest 6 ×1,3, Elektrolyse 6, Chipfabrik 45, Zerstörer gegen Korvette 1,6, Kampfpanzer
  300/60/50/30 und 2,5 KP, Vorhersage skillPerLevel 0,20, Kommandopunkte ab KZ 9 je 60
- Simulation sim/v124.ts (Kartell, Spätspiel-KP, Lagerwachstum, Vorhersage), ENTSCHEIDUNGEN-3 Abschnitt R
- Tests angepasst, neue Tests für Wertung
2026-11-27 14:19:41 +01:00

230 lines
10 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.
// Wirtschafts-Bot für die Balance-Simulation: baut gierig aus (billigster erlaubter Ausbau,
// Kraftwerk bei Strommangel) und steckt einen festen Anteil seiner Ausgaben in einen Verband.
// Der Verband folgt einer Doktrin (Anteile am Rüstungswert) und bleibt innerhalb der
// Kommandopunkte; Landungsschiffe werden nach Bedarf der Truppen gebaut.
import type { Balance, BaseState, Counts, GeoPoint, ResourceMap } from "../src/index.ts";
import {
commandPoints,
completeTraining,
completeUpgrade,
createBase,
fleetTotals,
maxAllowedLevel,
productionRates,
settle,
startTraining,
startUpgrade,
storageCapacity,
trainingCost,
upgradeCost,
} from "../src/index.ts";
const HOUR = 3600000;
const STEP = 10 * 60000;
/** Anteil der Ausgaben (zum Richtwert der Börse bewertet), den der Bot in Einheiten steckt. */
export const MILITARY_SHARE = 0.45;
/**
* Doktrin: Zielanteile am Rüstungswert je Einheitentyp und ab wann der Typ gebaut wird
* (Stufe des Baugebäudes). Landungsschiffe stehen nicht in der Liste, sie folgen dem Truppenbedarf.
*/
export type Doctrine = { id: string; share: number; fromLevel: number }[];
export const DOCTRINE: Doctrine = [
{ id: "korvette", share: 0.22, fromLevel: 1 },
{ id: "zerstoerer", share: 0.12, fromLevel: 3 },
{ id: "schuetzen", share: 0.3, fromLevel: 1 },
{ id: "pioniere", share: 0.1, fromLevel: 2 },
{ id: "kampfpanzer", share: 0.08, fromLevel: 4 },
{ id: "jagdbomber", share: 0.18, fromLevel: 1 },
];
/** Gegenprobe ab v1.2: U-Boote statt Zerstörer (gleicher Anteil, ebenfalls ab Werft 3). */
export const UBOOT_DOCTRINE: Doctrine = DOCTRINE.map((d) => (d.id === "zerstoerer" ? { ...d, id: "uboot" } : d));
/** Gegenprobe ab v1.2: schwere U-Boot-Rüstung (30 % U-Boote statt Zerstörern und Jagdbombern). */
export const UBOOT_HEAVY_DOCTRINE: Doctrine = [
...DOCTRINE.filter((d) => d.id !== "zerstoerer" && d.id !== "jagdbomber"),
{ id: "uboot", share: 0.3, fromLevel: 3 },
];
/** Gebäude, die der Bot ausbaut (Radar und Börse braucht er für Nester nicht). */
const BUILD_ORDER = [
"stahlwerk", "raffinerie", "versorgungshof", "chipfabrik", "kraftwerk", "lagerkomplex",
"kommandozentrale", "kaserne", "werft", "flugfeld", "hafenkommandantur", "tiefbunker",
];
/** Spätspiel-Bot (v1.24): Proviantüberschuss je Stunde, unter dem er zuerst den Versorgungshof ausbaut. */
const PROVIANT_RESERVE = 15;
/** Losgröße je Ausbildungsauftrag. */
const BATCH: Record<string, number> = { schuetzen: 10, pioniere: 5 };
export interface BotSnapshot {
day: number;
base: BaseState;
/** Ausgaben zum Richtwert: Gebäude und Einheiten. */
spentBuild: number;
spentMil: number;
}
export function value(cost: ResourceMap, balance: Balance): number {
let v = 0;
for (const [r, n] of Object.entries(cost)) v += n * (balance.market.refPrice[r] ?? 0);
return v;
}
function sum(m: Record<string, number>): number {
return Object.values(m).reduce((a, b) => a + b, 0);
}
/** Einheiten daheim plus in Ausbildung. */
function armyWithQueue(base: BaseState): Counts {
const all: Counts = { ...base.units };
for (const o of base.trainingQueue) if (o.kind === "unit") all[o.id] = (all[o.id] ?? 0) + o.count;
return all;
}
/** Mögliche Ausbildungsaufträge nach Doktrin, größtes Defizit zuerst. */
function nextOrders(base: BaseState, spentByType: Record<string, number>, balance: Balance, doctrine: Doctrine): { id: string; count: number }[] {
const army = armyWithQueue(base);
const totals = fleetTotals(army, balance);
// Erst Transportraum für vorhandene Truppen.
if (totals.troopSize > totals.troopSpace) return [{ id: "landungsschiff", count: 1 }];
const open = doctrine.filter((d) => (base.buildings[balance.units[d.id].builtIn ?? ""] ?? 0) >= d.fromLevel);
const shareSum = open.reduce((a, d) => a + d.share, 0);
const spent = open.reduce((a, d) => a + (spentByType[d.id] ?? 0), 0);
const ranked = open
.map((d) => ({ id: d.id, deficit: (d.share / shareSum) * (spent + 1) - (spentByType[d.id] ?? 0) }))
.sort((a, b) => b.deficit - a.deficit);
const out: { id: string; count: number }[] = [];
for (const { id } of ranked) {
const count = BATCH[id] ?? 1;
const def = balance.units[id];
// Truppen nur mit Transportraum, sonst zuerst ein Landungsschiff.
if (def.branch === "land" && totals.troopSize + (def.size ?? 0) * count > totals.troopSpace) {
out.push({ id: "landungsschiff", count: 1 });
} else out.push({ id, count });
}
return out;
}
/** Würde der Auftrag die Kommandopunkte oder die Proviantbilanz sprengen? */
function affordableArmy(base: BaseState, id: string, count: number, balance: Balance): boolean {
const army = armyWithQueue(base);
army[id] = (army[id] ?? 0) + count;
if (fleetTotals(army, balance).commandPointsUsed > commandPoints(base, balance) + 1e-9) return false;
const upkeep = (balance.units[id].upkeepPerHour ?? 0) * count;
return productionRates(base, balance).perHour.proviant - upkeep >= 0;
}
/**
* Lässt den Bot `days` Tage laufen und gibt Momentaufnahmen nach den Tagen in `checkpoints` zurück.
* militaryShare 0 ergibt den reinen Wirtschafts-Bot.
*/
export function runBot(
balance: Balance,
days: number,
checkpoints: number[],
militaryShare = MILITARY_SHARE,
doctrine: Doctrine = DOCTRINE,
/** Optional: greift je Schritt in den Zustand ein (z. B. Belohnungen des Auftragsbuchs, sim/guide.ts). */
onStep?: (base: BaseState, t: number) => BaseState,
/** Strom (v1.6): Lage des Hafens (Wetter), Startzeit und Kraftwerkstypen bei Strommangel (billigster zuerst). */
power: { site?: GeoPoint; startMs?: number; plants?: string[] } = {},
/**
* v1.24: Spätspiel-Spieler – sind die Kommandopunkte voll (weniger als ein Zerstörer frei), baut der Bot als Nächstes
* die Kommandozentrale (wie die KI-Spieler „mittel“/„stark“, sobald sie andere Gebäude deckelt); reicht der Proviant
* nicht für weiteren Unterhalt (unter PROVIANT_RESERVE je Stunde), zuerst den Versorgungshof.
*/
opts: { lateGame?: boolean } = {},
): BotSnapshot[] {
const t0 = power.startMs ?? 0;
let base: BaseState = createBase(balance, t0, power.site);
let spentBuild = 0;
let spentMil = 0;
const spentByType: Record<string, number> = {};
const out: BotSnapshot[] = [];
const marks = [...checkpoints].sort((a, b) => a - b);
let t = t0;
for (; t <= t0 + days * 24 * HOUR; t += STEP) {
if (base.buildQueue[0] && base.buildQueue[0].finishesAt <= t) {
const c = completeUpgrade(base, t, balance);
if (c.ok) base = c.base;
}
if (base.trainingQueue.some((o) => o.finishesAt <= t)) {
const c = completeTraining(base, t, balance);
if (c.ok) base = c.base;
}
base = settle(base, t, balance);
if (onStep) base = onStep(base, t);
const militaryFirst = spentMil <= militaryShare * (spentMil + spentBuild);
// Ist die Rüstung an der Reihe und fehlt Geld dafür, wird für sie gespart.
if (militaryFirst) {
if (train() !== "saving") build();
} else {
build();
train();
}
if (marks.length > 0 && t - t0 >= marks[0] * 24 * HOUR) {
out.push({ day: marks.shift()!, base: settle(base, t, balance), spentBuild, spentMil });
}
}
return out;
function allowed(id: string): boolean {
return (base.buildings[id] ?? 0) < maxAllowedLevel(base, id, balance);
}
function build(): void {
if (base.buildQueue.length > 0) return;
const rates = productionRates(base, balance);
const order = power.plants ? [...BUILD_ORDER.filter((id) => id !== "kraftwerk"), ...power.plants] : BUILD_ORDER;
let candidates = order.filter((id) => (base.buildings[id] ?? 0) < maxAllowedLevel(base, id, balance));
// Strommangel in Produktion oder Ausbildung (v1.6): erst ein Kraftwerk (bzw. die gewählten Quellen).
const plants = (power.plants ?? ["kraftwerk"]).filter((id) => (base.buildings[id] ?? 0) < maxAllowedLevel(base, id, balance));
const kz = "kommandozentrale";
const cpFree = commandPoints(base, balance) - fleetTotals(armyWithQueue(base), balance).commandPointsUsed;
const cpFull = opts.lateGame && militaryShare > 0 && cpFree < (balance.units.zerstoerer.commandPoints ?? 6);
if ((rates.powerFactor < 1 || rates.grid.served.training < 1) && plants.length > 0) candidates = plants;
else if (opts.lateGame && militaryShare > 0 && rates.perHour.proviant < PROVIANT_RESERVE && allowed("versorgungshof")) candidates = ["versorgungshof"];
else if (cpFull && allowed(kz)) candidates = [kz];
// Wer rüsten will, braucht zuerst Kaserne und Werft.
else if (militaryShare > 0) {
const missing = ["kaserne", "werft"].filter((id) => (base.buildings[id] ?? 0) === 0);
if (missing.length > 0) candidates = missing;
}
candidates.sort((a, b) => sum(upgradeCost(a, base.buildings[a] ?? 0, balance)) - sum(upgradeCost(b, base.buildings[b] ?? 0, balance)));
// Nur der billigste Ausbau; reicht es nicht, wird gespart.
const id = candidates[0];
if (id === undefined) return;
const cost = upgradeCost(id, base.buildings[id] ?? 0, balance);
const r = startUpgrade(base, id, t, balance);
if (r.ok) {
base = r.base;
spentBuild += value(cost, balance);
}
}
function train(): "trained" | "saving" | "idle" {
if (militaryShare <= 0) return "idle";
// Über dem Anteil nur, wenn das Lager überzulaufen droht.
const full = base.resources.stahl > 0.8 * storageCapacity(base, balance);
if (spentMil > militaryShare * (spentMil + spentBuild) && !full) return "idle";
for (const order of nextOrders(base, spentByType, balance, doctrine)) {
if (!affordableArmy(base, order.id, order.count, balance)) continue;
const r = startTraining(base, order.id, order.count, t, balance);
// Fehlt Geld für den dringendsten Auftrag, wird gespart; andere Hindernisse überspringen.
if (!r.ok && r.code === "insufficient_resources") return "saving";
if (!r.ok) continue;
base = r.base;
const v = value(trainingCost(order.id, order.count, balance), balance);
spentMil += v;
spentByType[order.id] = (spentByType[order.id] ?? 0) + v;
return "trained";
}
return "idle";
}
}