cortico-world-genesis 2.1.4 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/README.md +17 -2
- package/describe.json +1 -1
- package/dist/webui.js +93 -3
- package/package.json +1 -1
- package/src/ENV_PROMPT.md +7 -0
- package/src/flora.ts +4 -1
- package/src/game.ts +132 -4
- package/src/index.ts +3 -0
- package/src/info.ts +10 -0
- package/src/panel-types.ts +17 -1
- package/src/profile.ts +117 -0
- package/src/site.ts +3 -1
- package/src/store.ts +21 -3
- package/src/webui/client.ts +108 -3
- package/src/world.ts +164 -24
- package/src/world.ts.recovered +0 -3636
package/src/game.ts
CHANGED
|
@@ -118,6 +118,7 @@ import {
|
|
|
118
118
|
RESOURCE_LABELS,
|
|
119
119
|
SPECIES,
|
|
120
120
|
seedId,
|
|
121
|
+
type ClimateSample,
|
|
121
122
|
type Genes,
|
|
122
123
|
type PlantRecord,
|
|
123
124
|
type ResourceId,
|
|
@@ -284,6 +285,7 @@ export interface GameState {
|
|
|
284
285
|
/** 上次结算「疤往下游扩散」的时刻(母星时)。 */
|
|
285
286
|
lastScarHomeHours: number;
|
|
286
287
|
lastIndustryHomeHours?: number;
|
|
288
|
+
lastIceHomeHours?: number;
|
|
287
289
|
/** 交易终端上次自动交易的那一天(母星日序号),保证一天只跑一次。 */
|
|
288
290
|
lastAutoTradeDay: number;
|
|
289
291
|
/** 上次结算机械臂的时刻(母星时)。 */
|
|
@@ -527,6 +529,18 @@ export interface GameEvent {
|
|
|
527
529
|
text: string;
|
|
528
530
|
}
|
|
529
531
|
|
|
532
|
+
/** 【再创世】带走的档案(见 profile.ts):人回休眠舱,其余原样带过来。 */
|
|
533
|
+
export interface CarryOver {
|
|
534
|
+
credits: number;
|
|
535
|
+
tungsten: number;
|
|
536
|
+
energyKj: number;
|
|
537
|
+
inventory: Record<string, number>;
|
|
538
|
+
upgrades: Upgrades;
|
|
539
|
+
seedBags: SeedBag[];
|
|
540
|
+
people: number;
|
|
541
|
+
samples: number;
|
|
542
|
+
}
|
|
543
|
+
|
|
530
544
|
export interface StartOptions {
|
|
531
545
|
/** 恒星名(开局时定,存档沿用)。 */
|
|
532
546
|
starName: string;
|
|
@@ -536,6 +550,8 @@ export interface StartOptions {
|
|
|
536
550
|
timeScale?: number;
|
|
537
551
|
/** 创世种子:只用来决定初始应急种子包的基因(同一个世界永远一样)。 */
|
|
538
552
|
seed?: number | string;
|
|
553
|
+
/** 【再创世】带来的档案;不传就是全新一局。 */
|
|
554
|
+
carry?: CarryOver;
|
|
539
555
|
}
|
|
540
556
|
|
|
541
557
|
/**
|
|
@@ -621,7 +637,7 @@ export function startGame(system: StarSystem, opts: StartOptions, nowMs: number)
|
|
|
621
637
|
const cal = calendarFor(home, opts.homeName);
|
|
622
638
|
const scale = opts.timeScale && opts.timeScale > 0 ? opts.timeScale : 1;
|
|
623
639
|
const spawn = spawnOnLand(home);
|
|
624
|
-
|
|
640
|
+
const fresh: GameState = {
|
|
625
641
|
calendar: cal,
|
|
626
642
|
homePlanetIndex,
|
|
627
643
|
epochRealMs: nowMs,
|
|
@@ -644,13 +660,14 @@ export function startGame(system: StarSystem, opts: StartOptions, nowMs: number)
|
|
|
644
660
|
scar: {},
|
|
645
661
|
lastScarHomeHours: 0,
|
|
646
662
|
lastIndustryHomeHours: 0,
|
|
663
|
+
lastIceHomeHours: 0,
|
|
647
664
|
lastAutoTradeDay: -1,
|
|
648
665
|
lastArmsHomeHours: 0,
|
|
649
666
|
lastCycleWarnDay: -1,
|
|
650
667
|
lastGeneratorRealMs: nowMs - GENERATOR_COOLDOWN_MS,
|
|
651
668
|
activeMs: nowMs,
|
|
652
669
|
credits: 0,
|
|
653
|
-
inventory: { biomass: 0 },
|
|
670
|
+
inventory: { biomass: 0, deuterium: 0 },
|
|
654
671
|
stores: { [SHIP_POD_ID]: { ...POD_STOCK } },
|
|
655
672
|
sites: [],
|
|
656
673
|
plants: {},
|
|
@@ -684,6 +701,34 @@ export function startGame(system: StarSystem, opts: StartOptions, nowMs: number)
|
|
|
684
701
|
birthHours: 0,
|
|
685
702
|
lastLaborHomeHours: 0,
|
|
686
703
|
};
|
|
704
|
+
return opts.carry ? applyCarry(fresh, opts.carry) : fresh;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
function applyCarry(state: GameState, carry: CarryOver): GameState {
|
|
708
|
+
const bags = carry.seedBags.length > 0
|
|
709
|
+
? carry.seedBags.map((bag) => {
|
|
710
|
+
const copy = { ...bag };
|
|
711
|
+
delete copy.at;
|
|
712
|
+
return copy;
|
|
713
|
+
})
|
|
714
|
+
: state.seedBags;
|
|
715
|
+
return {
|
|
716
|
+
...state,
|
|
717
|
+
energyKj: carry.energyKj,
|
|
718
|
+
credits: carry.credits,
|
|
719
|
+
tungsten: carry.tungsten,
|
|
720
|
+
inventory: {
|
|
721
|
+
...carry.inventory,
|
|
722
|
+
biomass: carry.inventory.biomass ?? state.inventory.biomass,
|
|
723
|
+
deuterium: carry.inventory.deuterium ?? 0,
|
|
724
|
+
},
|
|
725
|
+
upgrades: { ...state.upgrades, ...carry.upgrades },
|
|
726
|
+
seedBags: bags,
|
|
727
|
+
samples: carry.samples,
|
|
728
|
+
sleepers: carry.people,
|
|
729
|
+
awakened: false,
|
|
730
|
+
birthHours: 0,
|
|
731
|
+
};
|
|
687
732
|
}
|
|
688
733
|
|
|
689
734
|
/** 抵达新天体后命名。行星由 Agent 起名;卫星按「<行星简称>卫<发现序号>」自动生成。 */
|
|
@@ -775,7 +820,8 @@ export function tick(system: StarSystem, state: GameState, nowMs: number): { sta
|
|
|
775
820
|
const traded = advanceTerminals(system, scarred, nowMs);
|
|
776
821
|
const terrafied = advanceTerra(system, traded, nowMs);
|
|
777
822
|
const industrious = advanceIndustry(system, terrafied.state, nowMs);
|
|
778
|
-
const
|
|
823
|
+
const frozen = advanceIce(system, industrious.state, nowMs);
|
|
824
|
+
const grown = advanceFlora(system, frozen.state, nowMs);
|
|
779
825
|
const lived = advanceLife(system, grown.state, nowMs);
|
|
780
826
|
const labored = advanceLabor(system, lived.state, nowMs);
|
|
781
827
|
const built = settleBuilds(system, labored.state, nowMs);
|
|
@@ -1948,6 +1994,7 @@ export function foundSite(system: StarSystem, state: GameState, latDeg: number,
|
|
|
1948
1994
|
kind: sample.wetness >= 0.5 ? 'water' : 'land',
|
|
1949
1995
|
foundedHomeHours: nowPlanetHours(state, nowMs),
|
|
1950
1996
|
nutrients: endowmentFor(surface, latDeg, lonDeg),
|
|
1997
|
+
...(sample.biome === 1 ? { waterL: 0 } : {}),
|
|
1951
1998
|
};
|
|
1952
1999
|
return {
|
|
1953
2000
|
state: {
|
|
@@ -2385,6 +2432,18 @@ export const CONVERTER_RECIPES: Record<string, { gas: string; liquid: string; la
|
|
|
2385
2432
|
export const CANNED_AMMONIA_N = 0.1;
|
|
2386
2433
|
export const CANNED_RETURN_FRACTION = 0.9;
|
|
2387
2434
|
|
|
2435
|
+
// 冰盖:站点自带一口 100 L 的冰窖,靠融雪慢慢攒水;水同时给作物当湿润度。
|
|
2436
|
+
export const ICE_CELLAR_L = 100;
|
|
2437
|
+
export const ICE_MELT_L_PER_HOUR = 0.2;
|
|
2438
|
+
export const ICE_IRRIGATION_L_PER_PLANT_HOUR = 0.02;
|
|
2439
|
+
export const ICE_WETNESS_BONUS_MAX = 0.5;
|
|
2440
|
+
export const ICE_WIND_MIN_KJ_PER_HOUR = 400;
|
|
2441
|
+
export const ICE_WIND_MAX_KJ_PER_HOUR = 900;
|
|
2442
|
+
|
|
2443
|
+
// 创世时的「死局」门槛:可落脚陆地太少就不让迫降。
|
|
2444
|
+
export const LANDING_MIN_CELLS = 12;
|
|
2445
|
+
export const LANDING_MIN_WARM_CELLS = 3;
|
|
2446
|
+
|
|
2388
2447
|
|
|
2389
2448
|
/**
|
|
2390
2449
|
* 【种植盆】:一只通电的小暖箱。不占地、不产堆场,只同时种 3 粒种子。
|
|
@@ -4720,7 +4779,10 @@ export function advanceFlora(system: StarSystem, state: GameState, nowMs: number
|
|
|
4720
4779
|
plants[siteKey] = list;
|
|
4721
4780
|
continue;
|
|
4722
4781
|
}
|
|
4723
|
-
const climate =
|
|
4782
|
+
const climate = withIceWater(
|
|
4783
|
+
site,
|
|
4784
|
+
climateAt(planet.surface, site.latDeg, site.lonDeg, nowHours, yearHomeHoursOf(state), planet.obliquityDeg),
|
|
4785
|
+
);
|
|
4724
4786
|
// 群系改变器:改造过的格子气候被拉向温和值(超级温室,任何环境都能工作)。
|
|
4725
4787
|
if (planet.surface.kind === 'terrestrial' && state.terra) {
|
|
4726
4788
|
const terra = terraClimate(planet.surface, state.terra, site.planetIndex, site.cellI, site.cellJ, climate);
|
|
@@ -5936,6 +5998,8 @@ export function deserializeGame(system: StarSystem, raw: unknown): GameState {
|
|
|
5936
5998
|
if (!Number.isFinite(lastCycleWarnDay)) throw new Error('game.json 的环路提醒日期不合法。');
|
|
5937
5999
|
const lastIndustryHomeHours = Number((g as { lastIndustryHomeHours?: unknown }).lastIndustryHomeHours ?? 0);
|
|
5938
6000
|
if (!Number.isFinite(lastIndustryHomeHours)) throw new Error('game.json 的工业结算时刻不合法。');
|
|
6001
|
+
const lastIceHomeHours = Number((g as { lastIceHomeHours?: unknown }).lastIceHomeHours ?? 0);
|
|
6002
|
+
if (!Number.isFinite(lastIceHomeHours)) throw new Error('game.json 的冰窖结算时刻不合法。');
|
|
5939
6003
|
// 星图是后加的:旧档没有就按「一张没有」处理。
|
|
5940
6004
|
const charts = (g as { charts?: unknown }).charts ?? [];
|
|
5941
6005
|
if (!Array.isArray(charts) || charts.some((v) => typeof v !== 'string')) {
|
|
@@ -5947,6 +6011,7 @@ export function deserializeGame(system: StarSystem, raw: unknown): GameState {
|
|
|
5947
6011
|
lastArmsHomeHours,
|
|
5948
6012
|
lastCycleWarnDay,
|
|
5949
6013
|
lastIndustryHomeHours,
|
|
6014
|
+
lastIceHomeHours,
|
|
5950
6015
|
upgrades: { ...(g.upgrades as Upgrades), telescope, sparkPlug, battery, subagent, blueprints, spectrometer },
|
|
5951
6016
|
tungsten,
|
|
5952
6017
|
grids,
|
|
@@ -6280,6 +6345,11 @@ export function windRateKjPerHour(system: StarSystem, state: GameState, structur
|
|
|
6280
6345
|
const cell = cellIndexFor(site.latDeg, site.lonDeg);
|
|
6281
6346
|
const biome = surface.biome[cell.i]?.[cell.j] ?? 0;
|
|
6282
6347
|
const elevation = surface.elevationM[cell.i]?.[cell.j] ?? 0;
|
|
6348
|
+
if (biome === 1) {
|
|
6349
|
+
// 冰盖上的下降风:贴地、常年不停,随纬度变强(南极那种)。
|
|
6350
|
+
const strength = Math.min(1, Math.abs(site.latDeg) / 90);
|
|
6351
|
+
return Math.round(ICE_WIND_MIN_KJ_PER_HOUR + (ICE_WIND_MAX_KJ_PER_HOUR - ICE_WIND_MIN_KJ_PER_HOUR) * strength);
|
|
6352
|
+
}
|
|
6283
6353
|
return biome >= 12 && biome <= 16
|
|
6284
6354
|
? WIND_STORM_KJ_PER_HOUR
|
|
6285
6355
|
: elevation >= WIND_HIGHLAND_MIN_M ? WIND_HIGHLAND_KJ_PER_HOUR : 0;
|
|
@@ -6439,3 +6509,61 @@ export function useCanned(
|
|
|
6439
6509
|
text: '把 ' + n + ' 罐液氨施进了这段地:氮到 ' + (nextNutrients.n * 100).toFixed(0) + '%,收回 ' + returned + ' 个空钢瓶。',
|
|
6440
6510
|
};
|
|
6441
6511
|
}
|
|
6512
|
+
|
|
6513
|
+
/** 冰窖里的水同时当灌溉水:水量越多,站点气候越湿(最多 +0.5)。 */
|
|
6514
|
+
export function withIceWater(site: Site, climate: ClimateSample): ClimateSample {
|
|
6515
|
+
if (!site.waterL) return climate;
|
|
6516
|
+
const bonus = ICE_WETNESS_BONUS_MAX * Math.min(1, site.waterL / ICE_CELLAR_L);
|
|
6517
|
+
return { ...climate, wetness: Math.min(1, climate.wetness + bonus) };
|
|
6518
|
+
}
|
|
6519
|
+
|
|
6520
|
+
/** 冰盖站点:融雪进水窖(0.2 L/母星时),每株作物每小时喝 0.02 L。 */
|
|
6521
|
+
export function advanceIce(system: StarSystem, state: GameState, nowMs: number): { state: GameState; events: GameEvent[] } {
|
|
6522
|
+
const nowHours = nowPlanetHours(state, nowMs);
|
|
6523
|
+
const last = state.lastIceHomeHours ?? nowHours;
|
|
6524
|
+
const delta = nowHours - last;
|
|
6525
|
+
if (!(delta > 0)) return { state, events: [] };
|
|
6526
|
+
let changed = false;
|
|
6527
|
+
const sites = state.sites.map((site) => {
|
|
6528
|
+
// 冰盖站点的 kind 仍按湿度算(可能是 water),所以这里只看格子是不是冰。
|
|
6529
|
+
const planet = system.planets[site.planetIndex - 1];
|
|
6530
|
+
if (!planet || planet.surface.kind !== "terrestrial") return site;
|
|
6531
|
+
const cell = cellIndexFor(site.latDeg, site.lonDeg);
|
|
6532
|
+
if ((planet.surface.biome[cell.i]?.[cell.j] ?? 0) !== 1) return site;
|
|
6533
|
+
const crops = state.plants[site.key]?.length ?? 0;
|
|
6534
|
+
const melt = ICE_MELT_L_PER_HOUR * delta;
|
|
6535
|
+
const drink = ICE_IRRIGATION_L_PER_PLANT_HOUR * delta * crops;
|
|
6536
|
+
const after = Math.max(0, Math.min(ICE_CELLAR_L, (site.waterL ?? 0) + melt - drink));
|
|
6537
|
+
changed = true;
|
|
6538
|
+
return { ...site, waterL: Math.round(after * 1000) / 1000 };
|
|
6539
|
+
});
|
|
6540
|
+
if (!changed) return { state: { ...state, lastIceHomeHours: nowHours }, events: [] };
|
|
6541
|
+
return { state: { ...state, sites, lastIceHomeHours: nowHours }, events: [] };
|
|
6542
|
+
}
|
|
6543
|
+
|
|
6544
|
+
/** 创世时的死局判定:母星可落脚陆地太少,就别让子代理迫降了。 */
|
|
6545
|
+
export function landingRefusal(system: StarSystem): string | null {
|
|
6546
|
+
const home = mostHabitable(system);
|
|
6547
|
+
const surface = home.surface;
|
|
6548
|
+
if (surface.kind !== "terrestrial") {
|
|
6549
|
+
return `子代理拒绝在该星系迫降:母星没有可供站点立足的地面。换一颗种子试试。`;
|
|
6550
|
+
}
|
|
6551
|
+
let land = 0;
|
|
6552
|
+
let warm = 0;
|
|
6553
|
+
let total = 0;
|
|
6554
|
+
for (let i = 0; i < surface.elevationM.length; i++) {
|
|
6555
|
+
const row = surface.elevationM[i];
|
|
6556
|
+
for (let j = 0; j < row.length; j++) {
|
|
6557
|
+
total += 1;
|
|
6558
|
+
const elev = row[j];
|
|
6559
|
+
const biome = surface.biome[i]?.[j] ?? 0;
|
|
6560
|
+
if (elev >= 0 && biome !== 0) {
|
|
6561
|
+
land += 1;
|
|
6562
|
+
if (biome >= 2 && biome <= 9) warm += 1;
|
|
6563
|
+
}
|
|
6564
|
+
}
|
|
6565
|
+
}
|
|
6566
|
+
if (land >= LANDING_MIN_CELLS && warm >= LANDING_MIN_WARM_CELLS) return null;
|
|
6567
|
+
const pct = total > 0 ? ((land / total) * 100).toFixed(1) : "0.0";
|
|
6568
|
+
return `子代理拒绝在该星系迫降:母星可用陆地只有 ${land} 格(约 ${pct}%),剩下的全是水或冰面——没有能扎第一个站点的地方。换一颗种子,或者把恒星质量的滑块往中间挪一点。`;
|
|
6569
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -204,3 +204,6 @@ export {
|
|
|
204
204
|
export { HABITAT_COMFORT_C, HABITAT_CLIMATE_PULL_C, habitatInteriorC, habitatComfortIssue } from './game.ts';
|
|
205
205
|
export { ordersForDay, GENE_LABELS, type Order } from './orders.ts';
|
|
206
206
|
export { PROTOCOL_TASKS, CHAPTER_TITLES, protocolStatus, protocolDone, protocolComplete } from './protocol.ts';
|
|
207
|
+
|
|
208
|
+
export { ProfileStore, PROFILE_SCHEMA, FUEL_RESOURCE, RECREATE_ENERGY_KJ, RECREATE_FUEL_KG, RECREATE_SAMPLES, consumeRecreate, exportProfile, recreateBlockers } from './profile.ts';
|
|
209
|
+
export type { Profile, ProfileStoreOptions } from './profile.ts';
|
package/src/info.ts
CHANGED
|
@@ -23,6 +23,7 @@ import { COUNTER_COST_KJ, MONITOR_COST_KJ, SAIL_COST_KJ, SAIL_KJ_PER_HOME_HOUR,
|
|
|
23
23
|
import { RESOURCE_LABELS, SPECIES } from './flora.ts';
|
|
24
24
|
import { LABOR_YIELD_BONUS, LABOR_YIELD_BONUS_MAX, RESIDENTS_PER_LABOR } from './life.ts';
|
|
25
25
|
import { SELL_GOODS } from './trade.ts';
|
|
26
|
+
import { REALISM_CORE_COST_KJ, REALISM_CORE_UPKEEP_KJ_PER_HOUR } from './life.ts';
|
|
26
27
|
|
|
27
28
|
export type InfoKind = 'building' | 'resource' | 'crop' | 'upgrade' | 'mechanic';
|
|
28
29
|
|
|
@@ -304,6 +305,15 @@ export const INFO_ENTRIES: InfoEntry[] = (() => {
|
|
|
304
305
|
about: '装在温室上,把温室的耐冷下限再往下撑 33℃。',
|
|
305
306
|
usage: '先有温室,再 `genesis_install { id: "heater", site: "<站点>" }`。',
|
|
306
307
|
},
|
|
308
|
+
{
|
|
309
|
+
id: 'realism',
|
|
310
|
+
label: '拟真核心',
|
|
311
|
+
aliases: ['拟真', 'realism', '气候模拟'],
|
|
312
|
+
facts: [`造价:${kj(REALISM_CORE_COST_KJ)} + 0 份钨`, `维护:${REALISM_CORE_UPKEEP_KJ_PER_HOUR} kJ/母星时`],
|
|
313
|
+
about: '装在温室上:温室不再把里面夹成一个恒温箱,而是跟着外面的季节、昼夜缓慢变化——更像一块地,也更难。',
|
|
314
|
+
usage: '先有温室,再 `genesis_install { id: "realism", site: "<站点>" }`。',
|
|
315
|
+
pitfalls: '装了之后作物会遇到与外面接近的冷热,挑错物种就会应激;想稳产就选耐性宽的。',
|
|
316
|
+
},
|
|
307
317
|
];
|
|
308
318
|
for (const device of devices) {
|
|
309
319
|
entries.push({
|
package/src/panel-types.ts
CHANGED
|
@@ -89,6 +89,10 @@ export interface GenesisPanelGreenhouse {
|
|
|
89
89
|
ambientC: number | null;
|
|
90
90
|
/** 当前温差(℃)。 */
|
|
91
91
|
deltaC: number;
|
|
92
|
+
/** 室内温度(℃);断电或超出能力时为 null。 */
|
|
93
|
+
interiorC: number | null;
|
|
94
|
+
/** 是否通电(断电时温室不工作)。 */
|
|
95
|
+
powered: boolean;
|
|
92
96
|
realismCores: number;
|
|
93
97
|
/** 冷库数量(影响保鲜)。 */
|
|
94
98
|
coldStores: number;
|
|
@@ -101,7 +105,17 @@ export interface GenesisPanelFlora {
|
|
|
101
105
|
shopOpen: boolean;
|
|
102
106
|
shopWindow: string;
|
|
103
107
|
shopLevel: number;
|
|
104
|
-
sites: {
|
|
108
|
+
sites: {
|
|
109
|
+
key: string;
|
|
110
|
+
kind: string;
|
|
111
|
+
plants: number;
|
|
112
|
+
ripe: number;
|
|
113
|
+
latDeg: number;
|
|
114
|
+
lonDeg: number;
|
|
115
|
+
/** 这一格的湿度与肥力(0…1,取当前季节的气候);气态行星或没有地表时为 null。 */
|
|
116
|
+
wetness?: number | null;
|
|
117
|
+
fertility?: number | null;
|
|
118
|
+
}[];
|
|
105
119
|
/** 每株/每群的当前状态(移栽时挑得到具体哪一株)。 */
|
|
106
120
|
crops?: { site: string; id: string; species: string; name: string; stage: string; progress: number; ripe: boolean; health: number; dead: boolean }[];
|
|
107
121
|
inventory: { id: string; name: string; amount: number }[];
|
|
@@ -224,6 +238,8 @@ export interface GenesisProtocolPanel {
|
|
|
224
238
|
hoursLeft: number;
|
|
225
239
|
} | null;
|
|
226
240
|
/** 人口:常住 / 容量 / 舱里还剩多少人 / 每 20 人一个「人手」。 */
|
|
241
|
+
/** 【再创世】:带着档案去下一张地图。blockers 为空才点得动。 */
|
|
242
|
+
recreate: { blockers: string[]; ready: boolean; seed: string; starMass: number };
|
|
227
243
|
population?: {
|
|
228
244
|
population: number;
|
|
229
245
|
capacity: number;
|
package/src/profile.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { populationOf, sleepersOf, type GameState, type SeedBag, type Upgrades } from './game.ts';
|
|
4
|
+
|
|
5
|
+
/** 点火燃料的资源 id(`flora.ts` 的 RESOURCE_LABELS 里叫「氘氚燃料」)。 */
|
|
6
|
+
export const FUEL_RESOURCE = 'deuterium';
|
|
7
|
+
|
|
8
|
+
/** 档案:能跨地图带走的那一小块。其余(站点、建筑、作物、疤)都长在地图上。 */
|
|
9
|
+
export interface Profile {
|
|
10
|
+
schema: 1;
|
|
11
|
+
/** 已经过了几颗星(第一次是 1)。 */
|
|
12
|
+
runs: number;
|
|
13
|
+
/** 上一颗星的名字,星卡里留一行。 */
|
|
14
|
+
lastStar?: string;
|
|
15
|
+
credits: number;
|
|
16
|
+
tungsten: number;
|
|
17
|
+
energyKj: number;
|
|
18
|
+
inventory: Record<string, number>;
|
|
19
|
+
upgrades: Upgrades;
|
|
20
|
+
seedBags: SeedBag[];
|
|
21
|
+
/** 所有人类(醒着的也算)——再创世时全体回休眠舱。 */
|
|
22
|
+
people: number;
|
|
23
|
+
samples: number;
|
|
24
|
+
updatedAt: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const PROFILE_SCHEMA = 1;
|
|
28
|
+
/** 【再创世】的门槛:一百万 kJ、一百公斤氘氚燃料、一份异星样本、机动核心修好。 */
|
|
29
|
+
export const RECREATE_ENERGY_KJ = 1_000_000;
|
|
30
|
+
export const RECREATE_FUEL_KG = 100;
|
|
31
|
+
export const RECREATE_SAMPLES = 1;
|
|
32
|
+
|
|
33
|
+
/** 从一局游戏里轧出档案。 */
|
|
34
|
+
export function exportProfile(
|
|
35
|
+
state: GameState,
|
|
36
|
+
meta: { runs?: number; lastStar?: string; nowMs?: number } = {},
|
|
37
|
+
): Profile {
|
|
38
|
+
return {
|
|
39
|
+
schema: PROFILE_SCHEMA,
|
|
40
|
+
runs: meta.runs ?? 1,
|
|
41
|
+
...(meta.lastStar ? { lastStar: meta.lastStar } : {}),
|
|
42
|
+
credits: state.credits,
|
|
43
|
+
tungsten: state.tungsten,
|
|
44
|
+
energyKj: state.energyKj,
|
|
45
|
+
inventory: { ...state.inventory, [FUEL_RESOURCE]: state.inventory[FUEL_RESOURCE] ?? 0 },
|
|
46
|
+
upgrades: { ...state.upgrades },
|
|
47
|
+
seedBags: state.seedBags.map((bag) => ({ ...bag, ...(bag.at ? {} : {}) })),
|
|
48
|
+
people: populationOf(state) + sleepersOf(state),
|
|
49
|
+
samples: state.samples,
|
|
50
|
+
updatedAt: new Date(meta.nowMs ?? Date.now()).toISOString(),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 门槛检查:返回空数组表示可以点火。 */
|
|
55
|
+
export function recreateBlockers(state: GameState): string[] {
|
|
56
|
+
const blockers: string[] = [];
|
|
57
|
+
if (state.energyKj < RECREATE_ENERGY_KJ) {
|
|
58
|
+
blockers.push(`能量不够:要 ${RECREATE_ENERGY_KJ.toLocaleString('en-US')} kJ,现在 ${Math.round(state.energyKj).toLocaleString('en-US')} kJ。`);
|
|
59
|
+
}
|
|
60
|
+
const fuel = state.inventory[FUEL_RESOURCE] ?? 0;
|
|
61
|
+
if (fuel < RECREATE_FUEL_KG) {
|
|
62
|
+
blockers.push(`氘氚燃料不够:要 ${RECREATE_FUEL_KG} kg,现在 ${fuel} kg。`);
|
|
63
|
+
}
|
|
64
|
+
if (state.samples < RECREATE_SAMPLES) {
|
|
65
|
+
blockers.push(`异星生命样本不够:要 ${RECREATE_SAMPLES} 份,现在 ${state.samples} 份。`);
|
|
66
|
+
}
|
|
67
|
+
if (state.upgrades.mobilityCore !== 'ready') {
|
|
68
|
+
blockers.push('机动核心还没修好:先修它,不然飞船起不来。');
|
|
69
|
+
}
|
|
70
|
+
if (state.transit) blockers.push('移动途中不能点火。');
|
|
71
|
+
return blockers;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** 扣掉点火成本(能量、燃料、样本)。 */
|
|
75
|
+
export function consumeRecreate(state: GameState): GameState {
|
|
76
|
+
const blockers = recreateBlockers(state);
|
|
77
|
+
if (blockers.length > 0) throw new Error(blockers.join('\n'));
|
|
78
|
+
return {
|
|
79
|
+
...state,
|
|
80
|
+
energyKj: state.energyKj - RECREATE_ENERGY_KJ,
|
|
81
|
+
inventory: { ...state.inventory, [FUEL_RESOURCE]: (state.inventory[FUEL_RESOURCE] ?? 0) - RECREATE_FUEL_KG },
|
|
82
|
+
samples: state.samples - RECREATE_SAMPLES,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface ProfileStoreOptions {
|
|
87
|
+
dataDir: string;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** `<dataDir>/genesis/profile.json`:档案单独一份,和地图的 runs/ 分开。 */
|
|
91
|
+
export class ProfileStore {
|
|
92
|
+
readonly file: string;
|
|
93
|
+
|
|
94
|
+
constructor(options: ProfileStoreOptions) {
|
|
95
|
+
this.file = join(options.dataDir, 'genesis', 'profile.json');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async read(): Promise<Profile | null> {
|
|
99
|
+
let raw: string;
|
|
100
|
+
try {
|
|
101
|
+
raw = await readFile(this.file, 'utf8');
|
|
102
|
+
} catch (error) {
|
|
103
|
+
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return null;
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
const parsed = JSON.parse(raw) as Profile;
|
|
107
|
+
if (parsed.schema !== PROFILE_SCHEMA) {
|
|
108
|
+
throw new Error(`profile.json 的 schema 是 ${String(parsed.schema)},这一版只认识 ${PROFILE_SCHEMA}。`);
|
|
109
|
+
}
|
|
110
|
+
return parsed;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async write(profile: Profile): Promise<void> {
|
|
114
|
+
await mkdir(dirname(this.file), { recursive: true });
|
|
115
|
+
await writeFile(this.file, `${JSON.stringify(profile, null, 2)}\n`, 'utf8');
|
|
116
|
+
}
|
|
117
|
+
}
|
package/src/site.ts
CHANGED
|
@@ -24,6 +24,8 @@ export interface Site {
|
|
|
24
24
|
foundedHomeHours: number;
|
|
25
25
|
/** 四维养分库存;收获会扣,草木灰/堆肥会补。 */
|
|
26
26
|
nutrients: NutrientStock;
|
|
27
|
+
/** 冰盖站点自带冰窖里的水(L,0…100):融雪攒、灌溉喝,同时给作物加湿润度。 */
|
|
28
|
+
waterL?: number;
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
/**
|
|
@@ -177,7 +179,7 @@ export function climateAt(
|
|
|
177
179
|
export function siteRefusal(surface: TerrestrialSurface, latDeg: number, lonDeg: number, existing: Site[]): string | null {
|
|
178
180
|
const sample = sampleCell(surface, latDeg, lonDeg);
|
|
179
181
|
if (!sample.land) return '这里是水面(海洋),站点要建在陆地上。';
|
|
180
|
-
|
|
182
|
+
// 冰盖可以建站(2.1.7):自带 100 L 冰窖、下降风发电,苦但能活。
|
|
181
183
|
const sameCell = existing.filter((site) => site.cellI === sample.cellI && site.cellJ === sample.cellJ);
|
|
182
184
|
if (sameCell.length >= MAX_SITES_PER_CELL) {
|
|
183
185
|
return `这一格已经有 ${MAX_SITES_PER_CELL} 个站点了(上限 ${MAX_SITES_PER_CELL} 个)。`;
|
package/src/store.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mkdir, readFile, unlink, writeFile } from 'node:fs/promises';
|
|
1
|
+
import { mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises';
|
|
2
2
|
import { dirname, join } from 'node:path';
|
|
3
3
|
import type { GenesisSpec, StarSystem } from './model.ts';
|
|
4
4
|
import { assertSave } from './schema.ts';
|
|
@@ -70,6 +70,24 @@ export class GenesisStore {
|
|
|
70
70
|
return { system, created: true, path: this.file };
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
+
/**
|
|
74
|
+
* 把当前这一局归档到 `genesis/runs/<run>/`({system,game}.json 一起搬)。
|
|
75
|
+
* 【再创世】时调用:旧地图从此只读、不再可进。
|
|
76
|
+
*/
|
|
77
|
+
async archiveRun(run: number, gameFile: string): Promise<string> {
|
|
78
|
+
const dir = join(dirname(this.file), 'runs', String(run));
|
|
79
|
+
await mkdir(dir, { recursive: true });
|
|
80
|
+
for (const src of [this.file, gameFile]) {
|
|
81
|
+
const name = src.slice(src.lastIndexOf('\\') + 1).split('/').pop()!;
|
|
82
|
+
try {
|
|
83
|
+
await rename(src, join(dir, name));
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return dir;
|
|
89
|
+
}
|
|
90
|
+
|
|
73
91
|
/** 删档重开。返回是否真的删掉了文件。 */
|
|
74
92
|
async delete(): Promise<boolean> {
|
|
75
93
|
try {
|
|
@@ -81,5 +99,5 @@ export class GenesisStore {
|
|
|
81
99
|
}
|
|
82
100
|
}
|
|
83
101
|
}
|
|
84
|
-
|
|
85
|
-
|
|
102
|
+
|
|
103
|
+
|