diff --git a/apps/web/src/screens/Base.tsx b/apps/web/src/screens/Base.tsx index ef53731..afe7828 100644 --- a/apps/web/src/screens/Base.tsx +++ b/apps/web/src/screens/Base.tsx @@ -26,7 +26,7 @@ import { IntelPanel, SabotageBanner, sabotagedUntil } from "../components/Intel. import { BlockadeBanner } from "../components/Naval.tsx"; // See: Blockade („blockiert bis …“) import { buildingLive, harborWeather } from "../lib/live-art.ts"; // Bewegte Gebäude import { CatchUpLine, CreditCost, buildCostSplit, useBuildCost } from "../components/CatchUp.tsx"; // Aufholhilfe: Baugutschrift -import type { Balance as CoreBalance } from "@tidefront/game-core"; +import { upgradeCost, type Balance as CoreBalance } from "@tidefront/game-core"; const invalidateBase = () => [trpc.base.pathFilter().queryKey]; @@ -136,11 +136,10 @@ function affordable(cost: Record | null, res: Record (res[r] ?? 0) >= v); } -/** Kosten des Ausbaus von Stufe L auf L+1 (wie im Regelkern: cost × costFactor^L, aufgerundet). */ +/** Kosten des Ausbaus von Stufe L auf L+1 – derselbe Regelkern wie der Server (inkl. stufenabhängiger Kostenmischung). */ function levelCost(balance: Balance, id: string, level: number): Record { - const out: Record = {}; - for (const [r, v] of Object.entries(balance.buildings[id]?.cost ?? {})) out[r] = Math.ceil(v * balance.building.costFactor ** level); - return out; + if (!balance.buildings[id]) return {}; + return upgradeCost(id, level, balance as unknown as CoreBalance); } function BuildYard({ base, now, onResearch }: { base: BaseData; now: number; onResearch: () => void }) { diff --git a/docs/REGELN.md b/docs/REGELN.md index b7165bc..09e2464 100644 --- a/docs/REGELN.md +++ b/docs/REGELN.md @@ -32,6 +32,10 @@ Vier Ressourcen: `stahl`, `treibstoff`, `elektronik`, `proviant`. Strom ist eine ## Gebäude - Kosten für den Ausbau von Stufe L auf L+1: `cost * costFactor^L`, je Ressource aufgerundet. + Kostenmischung (v1.29, `building.costShift`): vor dem Aufrunden wird der Anteil `min(max, perLevel × L)` des Stahls + (0,03 je Stufe, höchstens 35 %) wertgleich zum Börsen-Richtpreis auf die Zielmischung `to` umgelegt (Elektronik 75 %, + Proviant 25 %; Kaserne und Versorgungshof je 50 % über `costShiftTo`). Stufe 0 bleibt unverändert, höhere Stufen + verlagern mehr; Gesamtwert und Bauzeiten bleiben gleich. - Bauzeit: `seconds * timeFactor^L`, höchstens `maxBuildSeconds`. - Höchststufe `maxLevel`. Kein Gebäude außer der Kommandozentrale darf höher sein als `Stufe Kommandozentrale + levelCapAboveCommand`. diff --git a/packages/balance/v1/balance.json b/packages/balance/v1/balance.json index 660e852..d842b37 100644 --- a/packages/balance/v1/balance.json +++ b/packages/balance/v1/balance.json @@ -1,5 +1,5 @@ { - "version": "v1.28", + "version": "v1.29", "resources": ["stahl", "treibstoff", "elektronik", "proviant"], "start": { "stahl": 2000, "treibstoff": 1000, "elektronik": 500, "proviant": 1000 }, "baseTricklePerHour": { "stahl": 20, "treibstoff": 10, "elektronik": 5, "proviant": 15 }, @@ -10,14 +10,15 @@ "maxBuildSeconds": 432000, "levelCapAboveCommand": 3, "productionGrowth": 1.15, - "queueSlots": 3 + "queueSlots": 3, + "costShift": { "perLevel": 0.03, "max": 0.35, "to": { "elektronik": 0.75, "proviant": 0.25 } } }, "buildings": { "kommandozentrale": { "cost": { "stahl": 400, "treibstoff": 100, "elektronik": 100 }, "seconds": 120, "commandPointsBase": 60, "commandPointsPerLevel": 30, "commandPointsLateFromLevel": 9, "commandPointsLatePerLevel": 60 }, "stahlwerk": { "cost": { "stahl": 120, "treibstoff": 30, "elektronik": 0 }, "seconds": 20, "produces": "stahl", "perHour": 120, "powerPerLevel": 10 }, "raffinerie": { "cost": { "stahl": 150, "treibstoff": 20, "elektronik": 10 }, "seconds": 25, "produces": "treibstoff", "perHour": 60, "powerPerLevel": 12 }, "chipfabrik": { "cost": { "stahl": 200, "treibstoff": 60, "elektronik": 20 }, "seconds": 40, "produces": "elektronik", "perHour": 45, "powerPerLevel": 15 }, - "versorgungshof": { "cost": { "stahl": 100, "treibstoff": 20, "elektronik": 0 }, "seconds": 20, "produces": "proviant", "perHour": 80, "powerPerLevel": 6 }, + "versorgungshof": { "cost": { "stahl": 100, "treibstoff": 20, "elektronik": 0 }, "costShiftTo": { "proviant": 0.5, "elektronik": 0.5 }, "seconds": 20, "produces": "proviant", "perHour": 80, "powerPerLevel": 6 }, "kraftwerk": { "cost": { "stahl": 140, "treibstoff": 40, "elektronik": 10 }, "seconds": 25, "powerSupplyPerLevel": 60, "fuelPerHourPerLevel": 3 }, "windpark": { "cost": { "stahl": 180, "treibstoff": 10, "elektronik": 40 }, "seconds": 30, "windPowerPerLevel": 90, "stormCutoff": 0.95 }, "solarfeld": { "cost": { "stahl": 120, "treibstoff": 0, "elektronik": 70 }, "seconds": 30, "solarPowerPerLevel": 180, "cloudLoss": 0.7 }, @@ -27,7 +28,7 @@ "lagerkomplex": { "cost": { "stahl": 250, "treibstoff": 0, "elektronik": 0 }, "seconds": 30, "capacityBase": 5000, "capacityGrowth": 1.5 }, "tiefbunker": { "cost": { "stahl": 300, "treibstoff": 0, "elektronik": 50 }, "seconds": 45, "protectedBase": 300, "protectedGrowth": 1.4 }, "werft": { "cost": { "stahl": 400, "treibstoff": 100, "elektronik": 50 }, "seconds": 90, "buildSpeedPerLevel": 0.1, "powerPerLevel": 3, "powerRank": 3 }, - "kaserne": { "cost": { "stahl": 200, "treibstoff": 20, "elektronik": 10 }, "seconds": 45, "buildSpeedPerLevel": 0.1, "powerPerLevel": 3, "powerRank": 3 }, + "kaserne": { "cost": { "stahl": 200, "treibstoff": 20, "elektronik": 10 }, "costShiftTo": { "proviant": 0.5, "elektronik": 0.5 }, "seconds": 45, "buildSpeedPerLevel": 0.1, "powerPerLevel": 3, "powerRank": 3 }, "flugfeld": { "cost": { "stahl": 350, "treibstoff": 120, "elektronik": 120 }, "seconds": 90, "buildSpeedPerLevel": 0.1, "slotsPerLevel": 6, "powerPerLevel": 3, "powerRank": 3 }, "radarstation": { "cost": { "stahl": 200, "treibstoff": 40, "elektronik": 150 }, "seconds": 60, "warningMinutesBase": 10, "warningMinutesPerLevel": 5, "powerPerLevel": 5, "powerRank": 1 }, "hafenkommandantur": { "cost": { "stahl": 300, "treibstoff": 80, "elektronik": 60 }, "seconds": 60, "missionSlotsBase": 1, "missionSlotsPerTwoLevels": 1, "marketFromLevel": 3, "creditLimitPerLevel": 5000 }, diff --git a/packages/engine/test/social.test.ts b/packages/engine/test/social.test.ts index 4a1bae4..71b20cc 100644 --- a/packages/engine/test/social.test.ts +++ b/packages/engine/test/social.test.ts @@ -133,8 +133,9 @@ test("Rangliste: Punkte je Kategorie nach Formel", async () => { const rows = await computeStandings(db, season.startsAt.getTime(), Date.now()); const a = rows.find((r) => r.playerId === A.playerId)!; const b = rows.find((r) => r.playerId === B.playerId)!; - // Stahlwerk: 120 + 30 × 1,8 = 174, Stufe 2: 180 + 45 × 1,8 = 261. - assert.equal(buildingValue({ stahlwerk: 2 }), 174 + 261); + // Stahlwerk: 120 + 30 × 1,8 = 174, Stufe 2: 180 + 45 × 1,8 = 261 – ab v1.29 mit Kostenmischung (3 % des Stahls zu + // Elektronik/Proviant, je Ressource aufgerundet): 175 + 45 × 1,8 + 2 × 3 + 3 × 0,6 = 263,8. + assert.ok(Math.abs(buildingValue({ stahlwerk: 2 }) - (174 + 263.8)) < 1e-9); // A: Ausbau 435, Militär 2 Korvetten 1460, Kampf 1530 + 1400, Beute 1990 + 900. B: Bunkerlinie 460, Kampf 420 + 125. assert.deepEqual({ ausbau: a.ausbau, militaer: a.militaer, kampf: a.kampf, beute: a.beute, total: a.total }, { ausbau: 4, militaer: 14, kampf: 29, beute: 28, total: 75 }); diff --git a/packages/game-core/ENTSCHEIDUNGEN-5.md b/packages/game-core/ENTSCHEIDUNGEN-5.md index 2f12957..e9afefe 100644 --- a/packages/game-core/ENTSCHEIDUNGEN-5.md +++ b/packages/game-core/ENTSCHEIDUNGEN-5.md @@ -88,3 +88,64 @@ Titel/Ruhmeshalle nur Menschen; 5a Tag 36 Ankündigung, Tag 38 12:00 + 72 h; 6a 11. **Verwaltung:** „Saisonfinale vorziehen“ (`admin.startFinale`, Engine `startFinaleNow`) kündigt sofort an; ein beendetes Finale der Saison wird ersetzt (Ergebnis gelöscht), ein laufendes nicht. 12. **Wertung:** eine abgegrenzte Stelle in `scoring.ts` (`armada` in `reportScore`) mit `scoring.armadaFactor`; sie ersetzt nur den NPC-Faktor. Armada-Kämpfe sind keine Spielerkämpfe, die PvP-Faktoren bleiben dort 1. +## Baukosten umverteilen: stufenabhängige Kostenmischung (Balance v1.29) + +**Anlass:** Wirtschaftsprüfung 0.24.2 – Stahl ist die ganze Saison einziger Engpass (≈ 1 % ungenutzt), Treibstoff 57 %, +Elektronik 66 %, Proviant 43 % bleiben liegen, weil Bauen fast nur Stahl kostet. Nutzerentscheid: Baukosten umverteilen. + +1. **Mechanik statt Werte-Hack:** Das Kostenmodell kannte je Stufe nur `costFactor`. Neu `building.costShift` + (`perLevel`, `max`, `to`) in `upgradeCost`: Beim Ausbau L → L+1 wird der Anteil `min(max, perLevel × L)` des Stahls + vor dem Aufrunden wertgleich zum Börsen-Richtpreis (`market.refPrice`) auf die Zielmischung umgelegt; optional je + Gebäude `costShiftTo`. Stufe 0 bleibt gleich, höhere Stufen verlagern mehr; Gesamtwert gleich (Aufrunden je + Ressource: ≤ 5,4 Richtwert je Stufe mehr), Bauzeiten unverändert. Ohne `costShift` gilt die alte Formel. +2. **Werte:** `perLevel` 0,03, `max` 0,35 (ab Stufe 12), `to` Elektronik 75 % / Proviant 25 %; Kaserne und + Versorgungshof `costShiftTo` Proviant 50 % / Elektronik 50 %. **Kein Treibstoff:** Treibstoff war im ersten Versuch + (40 % Treibstoff) beim aktiven Spieler sofort Engpass (0 % ungenutzt, ausgefallene Nester) – er wird für Fahrten + gebraucht. Verlagert (alle Gebäude, Werte `mix`-Rechnung): bis Stufe 6 −11 % Stahl, bis 10 −21,5 %, bis 12 −27 %, + bis 15 −33 %. Beispiel Kommandozentrale Stufe 9 → 10: vorher 15 378 St / 3 845 Tr / 3 845 El, jetzt 11 226 St / + 3 845 Tr / 4 883 El / 1 730 Pr. +3. **Startphase:** Stufen 0–2 verlagern 0–6 %, Startbestände bleiben (Kommandozentrale Tag 3 unverändert: passiv 1, + aktiv 3). Einheiten-, Unterhalts- und Forschungskosten unverändert. +4. **Oberfläche/Server:** alle Kosten kommen aus `upgradeCost` (Views, Bots, Wertung, Aufholhilfe); der Bauhof + (`Base.tsx` levelCost) rechnete die Formel selbst nach und nutzt jetzt `upgradeCost` aus dem Regelkern. + Gebäudekarten zeigen Proviant automatisch (Kosten-Komponente ist generisch). Handbuch nennt keine Gebäudekosten. +5. **Aufholhilfe:** `splitBuildCost` teilt jede Ressource anteilig, also auch Proviant – Test in `cost-shift.test.ts`. +6. **Bots:** keine Änderung an `bots-econ.ts` nötig. Die Engine-Bots gewichten Erzeuger ohnehin nach Knappheit; + `bots-sim.test.ts` (8 Tage) grün mit vier Startzeiten; Gebäudestufen Tag 8 Händler/Festung/Freibeuter 68/65/65 + statt 65/58/61, Punkte 884/1166/1804 statt 679/839/1647. +7. **Wertung:** Ausbaupunkte = Kostenwert zum Richtpreis, durch die Wertgleichheit praktisch unverändert + (`social.test.ts`: Stahlwerk 1→2 261 → 263,8 durch Aufrunden). + +**Rechnung** (`econ.ts` der Wirtschaftsprüfung, Spätspiel-Bot 45 % Rüstung, 42 Tage; „ref“ = Standard-Bot, +„spät“ = Spätspiel-Ausbau; ungenutzt = Endbestand Tag 42 / gesamter Zufluss inkl. Start, Grundrate, Extern; +Engpass = Rohstoff mit der niedrigsten mittleren Lagerfüllung der Woche): + +| Lauf | ungenutzt St / Tr / El / Pr vorher | **nachher** | KZ Tag 42 | Ausbauwert 42 Tage | +|---|---|---|---|---| +| passiv ref | 1 / 57 / 66 / 43 % | **0 / 47 / 35 / 22 %** | 8 → 9 | 503 k → 708 k | +| aktiv ref | 1 / 32 / 60 / 69 % | **0 / 5 / 5 / 28 %** | 12 → 13 | 2,42 M → 3,77 M | +| passiv spät | 13 / 41 / 50 / 13 % | **12 / 41 / 16 / 0 %** | 12 → 13 | 319 k → 434 k | +| aktiv spät | 28 / 11 / 57 / 48 % | **25 / 6 / 21 / 33 %** | 16 → 17 | 1,65 M → 2,18 M | + +| Engpass je Woche (2. knappster) | W1 | W2 | W3 | W4 | W5 | W6 | +|---|---|---|---|---|---|---| +| aktiv ref vorher | St (El 20 %) | St (El 17 %) | St (El 15 %) | St (El 15 %) | St (Tr 12 %) | St (Tr 10 %) | +| **aktiv ref nachher** | St (El 16 %) | St (El 8 %) | St (El 4 %) | St (El 2 %) | St (El 1,5 %) | Tr (St 1,1 %) | +| passiv ref vorher | St (El 15 %) | St (El 23 %) | St (El 22 %) | St (El 22 %) | St (El 21 %) | St (El 22 %) | +| **passiv ref nachher** | St (El 14 %) | St (El 18 %) | St (El 16 %) | St (El 12 %) | St (El 10 %) | St (El 9 %) | +| aktiv spät vorher | St | St | St | Tr | Tr | Tr | +| **aktiv spät nachher** | St | St | El | Tr | Tr | Tr | + +- **Ziel erreicht beim aktiven Spieler:** alle vier Rohstoffe knapp (5/5/28 % statt 32/60/69 %), Stahl und + Elektronik binden gemeinsam, ab Woche 6 auch Treibstoff. +- **Passiver Spieler (keine Belohnungen, keine Fahrten):** Elektronik 66 → 35 %, Proviant 43 → 22 %; Stahl bleibt + Engpass, weil die Rüstung (45 % der Ausgaben, Einheitenkosten unverändert, stahllastig) ihn bindet. Treibstoff + 57 → 47 %: Treibstoff ist der Fahrtrohstoff; mehr Treibstoff in Baukosten machte den aktiven Spieler knapp + (s. o.). Bewusst so gelassen. +- **Treibstoff aktiv:** Der Rechen-Bot spart nie für Fahrten; an Tag 36–42 fiel 1 Nest aus (vorher 0), „spät“ + 6 statt 2 Ausfälle. Ein Spieler hält Fahrtreibstoff zurück; im Auge behalten (Live-Daten), ggf. Raffinerie. +- **Schnelleres Wachstum:** Rohstoffe, die vorher lagen, werden verbaut – Ausbauwert +40–55 %, das ist bei Faktor + 1,5 je Stufe etwa **eine Stufe** am Saisonende (KZ 12 → 13). Nest-Zielfenster (`npm run sim`, „Erster Prüftag mit + ≥ 70 %“) **unverändert**: Stufe 1 Tag 1, 2 Tag 3, 3 Tag 7, 4 Tag 14, 5 Tag 21. Nur der Bot mit 30 % Rüstung + erreicht Stufe 4/5 an Tag 14/18 statt 18/21; die 42-Tage-Kette erreicht Nest 4 an Tag 11 statt 12, Nest 5 an Tag + 19 statt 20; gedachtes Nest 6 ×1,3–1,5 an Tag 42 100 % statt 84/63/37 % (Tag 28/35 gleich). diff --git a/packages/game-core/src/buildings.ts b/packages/game-core/src/buildings.ts index 8111fda..fbfb636 100644 --- a/packages/game-core/src/buildings.ts +++ b/packages/game-core/src/buildings.ts @@ -13,13 +13,27 @@ export function missingRequirement(base: BaseState, buildingId: string, balance: return null; } -/** Kosten für den Ausbau von Stufe `level` auf `level + 1`, je Ressource aufgerundet. */ +/** + * Kosten für den Ausbau von Stufe `level` auf `level + 1`, je Ressource aufgerundet: cost × costFactor^level, dann + * (v1.29, building.costShift) der Anteil min(max, perLevel × level) des Stahls wertgleich zum Richtpreis auf die + * Zielmischung umgelegt. + */ export function upgradeCost(buildingId: string, level: number, balance: Balance): ResourceMap { const def = balance.buildings[buildingId]; - const cost: ResourceMap = {}; - for (const r of balance.resources) { - cost[r] = Math.ceil((def.cost[r] ?? 0) * balance.building.costFactor ** level); + const raw: ResourceMap = {}; + for (const r of balance.resources) raw[r] = (def.cost[r] ?? 0) * balance.building.costFactor ** level; + const shift = balance.building.costShift; + const share = shift ? Math.min(shift.max, shift.perLevel * level) : 0; + const to = def.costShiftTo ?? shift?.to ?? {}; + const weight = Object.values(to).reduce((a, w) => a + w, 0); + const price = balance.market.refPrice; + if (share > 0 && weight > 0 && (raw.stahl ?? 0) > 0) { + const moved = raw.stahl * share; + raw.stahl -= moved; + for (const [r, w] of Object.entries(to)) raw[r] = (raw[r] ?? 0) + (moved * (price.stahl ?? 1) * w) / weight / (price[r] ?? 1); } + const cost: ResourceMap = {}; + for (const r of balance.resources) cost[r] = Math.ceil(raw[r] ?? 0); return cost; } diff --git a/packages/game-core/src/types.ts b/packages/game-core/src/types.ts index b87d0ea..c556cba 100644 --- a/packages/game-core/src/types.ts +++ b/packages/game-core/src/types.ts @@ -25,8 +25,21 @@ export interface AttackValues { uboot?: number; } +/** + * Stufenabhängige Kostenmischung (v1.29): Beim Ausbau von Stufe L auf L+1 wird der Anteil min(max, perLevel × L) der + * Stahlkosten zum Börsen-Richtpreis (market.refPrice) wertgleich auf andere Rohstoffe umgelegt, gewichtet nach `to` + * (bzw. BuildingDef.costShiftTo). Höhere Stufen verlagern also mehr; der Gesamtwert bleibt (bis auf Rundung) gleich. + */ +export interface CostShift { + perLevel: number; + max: number; + to: ResourceMap; +} + export interface BuildingDef { cost: ResourceMap; + /** Kostenverschiebung (v1.29): Zielmischung des von Stahl verlagerten Werts; ohne Angabe building.costShift.to. */ + costShiftTo?: ResourceMap; seconds: number; produces?: string; perHour?: number; @@ -148,6 +161,8 @@ export interface Balance { productionGrowth: number; /** Höchstzahl der Aufträge in der Bauschlange eines Stützpunkts. */ queueSlots: number; + /** Stufenabhängige Kostenmischung (v1.29), siehe upgradeCost. */ + costShift?: CostShift; }; buildings: Record; units: Record; diff --git a/packages/game-core/test/buildings.test.ts b/packages/game-core/test/buildings.test.ts index 008e071..ce6e2b2 100644 --- a/packages/game-core/test/buildings.test.ts +++ b/packages/game-core/test/buildings.test.ts @@ -15,7 +15,8 @@ import { HOUR, T0, approx, loadBalance, makeBase } from "./helpers.ts"; const balance = loadBalance(); -test("Kostenformel für mehrere Stufen, aufgerundet", () => { +test("Kostenformel für mehrere Stufen, aufgerundet (ohne Kostenverschiebung, die prüft cost-shift.test.ts)", () => { + const balance = { ...loadBalance(), building: { ...loadBalance().building, costShift: undefined } }; assert.deepEqual(upgradeCost("stahlwerk", 0, balance), { stahl: 120, treibstoff: 30, elektronik: 0, proviant: 0 }); assert.deepEqual(upgradeCost("stahlwerk", 1, balance), { stahl: 180, treibstoff: 45, elektronik: 0, proviant: 0 }); assert.deepEqual(upgradeCost("stahlwerk", 2, balance), { stahl: 270, treibstoff: 68, elektronik: 0, proviant: 0 }); diff --git a/packages/game-core/test/cost-shift.test.ts b/packages/game-core/test/cost-shift.test.ts new file mode 100644 index 0000000..ab2c663 --- /dev/null +++ b/packages/game-core/test/cost-shift.test.ts @@ -0,0 +1,91 @@ +// Stufenabhängige Kostenmischung (building.costShift, v1.29): höhere Stufen verlagern einen wachsenden Anteil des Stahls +// wertgleich zum Börsen-Richtpreis auf andere Rohstoffe. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { emptyCatchUp, refValue, splitBuildCost, startUpgrade, upgradeCost, type Balance, type ResourceMap } from "../src/index.ts"; +import { T0, loadBalance, makeBase } from "./helpers.ts"; + +const real = loadBalance(); +/** Feste Testwerte, unabhängig von den aktuellen Balance-Zahlen. */ +const balance: Balance = { + ...real, + building: { ...real.building, costFactor: 1.5, costShift: { perLevel: 0.03, max: 0.3, to: { elektronik: 0.75, proviant: 0.25 } } }, + market: { ...real.market, refPrice: { stahl: 1, treibstoff: 1.8, elektronik: 3, proviant: 0.6 } }, + buildings: { + ...real.buildings, + testwerk: { cost: { stahl: 1000, treibstoff: 100, elektronik: 0 }, seconds: 10 }, + testkaserne: { cost: { stahl: 1000 }, costShiftTo: { proviant: 1 }, seconds: 10 }, + }, +}; +const value = (c: ResourceMap) => Object.entries(c).reduce((a, [r, n]) => a + n * (balance.market.refPrice[r] ?? 0), 0); + +test("Stufe 0 bleibt unverändert, ohne costShift gilt die alte Formel", () => { + assert.deepEqual(upgradeCost("testwerk", 0, balance), { stahl: 1000, treibstoff: 100, elektronik: 0, proviant: 0 }); + const plain = { ...balance, building: { ...balance.building, costShift: undefined } }; + assert.deepEqual(upgradeCost("testwerk", 4, plain), { stahl: Math.ceil(1000 * 1.5 ** 4), treibstoff: Math.ceil(100 * 1.5 ** 4), elektronik: 0, proviant: 0 }); +}); + +test("Stufe 2: 6 % des Stahls wandern wertgleich nach der Zielmischung", () => { + // 1000 × 1,5² = 2250 Stahl, davon 6 % = 135 → 2115 Stahl; 135 × 0,75 / 3 = 33,75 Elektronik, 135 × 0,25 / 0,6 = 56,25 Proviant. + assert.deepEqual(upgradeCost("testwerk", 2, balance), { stahl: 2115, treibstoff: 225, elektronik: 34, proviant: 57 }); +}); + +test("Der Anteil wächst je Stufe und ist bei max gedeckelt", () => { + const share = (l: number) => 1 - upgradeCost("testwerk", l, balance).stahl / (1000 * 1.5 ** l); + assert.ok(share(4) > share(2) && share(8) > share(4)); + assert.ok(Math.abs(share(10) - 0.3) < 1e-3); + assert.ok(Math.abs(share(15) - 0.3) < 1e-3); +}); + +test("Der Gesamtwert zum Richtpreis bleibt bis auf Rundung gleich", () => { + for (const l of [1, 5, 9, 14]) { + const raw = (1000 + 100 * 1.8) * 1.5 ** l; + const v = value(upgradeCost("testwerk", l, balance)); + assert.ok(v >= raw - 1e-6 && v - raw <= 1 + 1.8 + 3 + 0.6, `Stufe ${l}: ${v} vs ${raw}`); + } +}); + +test("costShiftTo eines Gebäudes ersetzt die allgemeine Zielmischung", () => { + const c = upgradeCost("testkaserne", 5, balance); + // 1000 × 1,5⁵ = 7593,75; 15 % = 1139,0625 Stahl → 1898,4375 Proviant + assert.deepEqual(c, { stahl: Math.ceil(7593.75 - 1139.0625), treibstoff: 0, elektronik: 0, proviant: Math.ceil(1139.0625 / 0.6) }); +}); + +test("Echte Balance: Stahl der Gebäude bis Stufe 12 sinkt um rund ein Viertel, Gesamtwert gleich", () => { + let steelOld = 0, steelNew = 0, valueOld = 0, valueNew = 0; + const plain = { ...real, building: { ...real.building, costShift: undefined } }; + const price = real.market.refPrice; + for (const id of Object.keys(real.buildings)) { + if (real.buildings[id].requires) continue; + for (let l = 0; l < 12; l++) { + const a = upgradeCost(id, l, plain), b = upgradeCost(id, l, real); + steelOld += a.stahl; steelNew += b.stahl; + for (const r of real.resources) { valueOld += a[r] * price[r]; valueNew += b[r] * price[r]; } + } + } + const moved = 1 - steelNew / steelOld; + assert.ok(moved > 0.18 && moved < 0.32, `verlagert ${moved}`); + assert.ok(Math.abs(valueNew / valueOld - 1) < 0.002, `Wert ${valueNew / valueOld}`); +}); + +test("Aufholhilfe: Baugutschrift deckt auch beim verlagerten Mix denselben Anteil jeder Ressource (inkl. Proviant)", () => { + const level = 8; + const cost = upgradeCost("kommandozentrale", level, real); + assert.ok(cost.proviant > 0 && cost.elektronik > 0, "hohe Stufe kostet Proviant und mehr Elektronik"); + const total = refValue(cost, real); + const half = splitBuildCost(cost, total / 2, real); + for (const r of real.resources) { + assert.ok(Math.abs((half.fromCredit[r] ?? 0) - cost[r] / 2) <= 1, `${r}: ${half.fromCredit[r]} vs ${cost[r] / 2}`); + assert.equal((half.fromCredit[r] ?? 0) + half.fromStock[r], cost[r]); + } + // Ganz aus der Gutschrift: das Lager (ohne Proviant) bleibt unberührt. + const base = makeBase(real, { + buildings: { kommandozentrale: level }, + resources: { stahl: 0, treibstoff: 0, elektronik: 0, proviant: 0 }, + catchUp: { ...emptyCatchUp(T0), credit: total + 10 }, + }); + const up = startUpgrade(base, "kommandozentrale", T0, real); + assert.ok(up.ok, JSON.stringify(up)); + assert.ok(Math.abs(up.base.catchUp!.credit - 10) < 1e-6); + assert.deepEqual(up.order.credit, Object.fromEntries(Object.entries(cost).filter(([, v]) => v > 0))); +});