517 lines
17 KiB
TypeScript
517 lines
17 KiB
TypeScript
import { Pool } from "pg";
|
|
import { getPayload } from "payload";
|
|
import config from "@payload-config";
|
|
import "dotenv/config";
|
|
|
|
/**
|
|
* Idempotent seed for the Superstructure interior launch content.
|
|
*
|
|
* Authors the blueprint interior (three rooms per the plan's Launch Content
|
|
* section), plus the resources, labor class, technology, and asset the rooms
|
|
* reference:
|
|
*
|
|
* 1. Barracks: open at level 1; the L2 upgrade gates on Scrap materials, a
|
|
* labor class requirement, and an (unresearched) technology. Proves the
|
|
* upgrade path, tech gating, and labor gates.
|
|
* 2. Workshop: Fabricator station with two enabled resource recipes and one
|
|
* disabled asset-output recipe kept as the authoring template (asset credit
|
|
* stays behind the Game Rules `interior.assetOutputsEnabled` flag).
|
|
* 3. Rubble Annex: sealed at level 0 with an open-room job, starting junk
|
|
* level 3 with a loot table, and a broken doorway fixture that blocks its
|
|
* station until repaired. Proves open, cleanup, and repair.
|
|
*
|
|
* No floorplan image is uploaded: the UI falls back to the generated hotspot
|
|
* schematic, so the feature works before art lands.
|
|
*
|
|
* Idempotent: existing rows are matched by codeName / key / name and updated
|
|
* in place, so re-running after tuning edits converges instead of duplicating.
|
|
*/
|
|
|
|
const SCRAP_CODE_NAME = "res_scrap";
|
|
const ENERGY_CODE_NAME = "res_interior_energy";
|
|
const ALLOY_CODE_NAME = "res_interior_alloy";
|
|
const BLUEPRINT_CODE_NAME = "structure_superstructure_interior";
|
|
|
|
// Polymorphic relationship fields (recipe outputs, junk loot items) require the
|
|
// canonical `{ relationTo, value }` shape; plain ids fail validation on write.
|
|
const resRef = (id: number) => ({ relationTo: "resources" as const, value: id });
|
|
const assetRef = (id: number) => ({ relationTo: "assets" as const, value: id });
|
|
|
|
const alignResourcesSequence = async (pool: Pool) => {
|
|
const { rows } = await pool.query("SELECT MAX(id) AS max_id FROM resources");
|
|
const maxId = rows[0]?.max_id;
|
|
if (maxId != null) {
|
|
await pool.query("SELECT setval('resources_id_seq', $1, true)", [maxId]);
|
|
}
|
|
};
|
|
|
|
const ensureResource = async (
|
|
payload: Awaited<ReturnType<typeof getPayload>>,
|
|
pool: Pool,
|
|
args: { name: string; codeName: string; massPerUnit: number; stackSize: number },
|
|
): Promise<number> => {
|
|
const existing = await payload.find({
|
|
collection: "resources",
|
|
where: { codeName: { equals: args.codeName } },
|
|
limit: 1,
|
|
overrideAccess: true,
|
|
});
|
|
const found = existing.docs[0]?.id as number | undefined;
|
|
if (found) return found;
|
|
|
|
await alignResourcesSequence(pool);
|
|
const created = await payload.create({
|
|
collection: "resources",
|
|
data: {
|
|
name: args.name,
|
|
codeName: args.codeName,
|
|
approvalStatus: "approved",
|
|
type: "physical",
|
|
unitOfMeasure: "kg",
|
|
massPerUnit: args.massPerUnit,
|
|
stackSize: args.stackSize,
|
|
stackable: true,
|
|
gridWidth: 1,
|
|
gridHeight: 1,
|
|
rotatable: false,
|
|
baseValue: 0,
|
|
rarity: "common",
|
|
tradeable: true,
|
|
},
|
|
draft: false,
|
|
overrideAccess: true,
|
|
});
|
|
await alignResourcesSequence(pool);
|
|
payload.logger.info(`[seed-interior] created resource ${args.name}: ${created.id}`);
|
|
return created.id as number;
|
|
};
|
|
|
|
export const seedInteriorContent = async (): Promise<number> => {
|
|
const payload = await getPayload({ config });
|
|
const dbUri = process.env.DATABASE_URI;
|
|
if (!dbUri) throw new Error("[seed-interior] DATABASE_URI is not set");
|
|
const pool = new Pool({ connectionString: dbUri });
|
|
|
|
try {
|
|
// --- Supporting docs ---------------------------------------------------
|
|
|
|
const scrapId = await ensureResource(payload, pool, {
|
|
name: "Scrap",
|
|
codeName: SCRAP_CODE_NAME,
|
|
massPerUnit: 0.5,
|
|
stackSize: 1000,
|
|
});
|
|
const energyId = await ensureResource(payload, pool, {
|
|
name: "Energy Cells",
|
|
codeName: ENERGY_CODE_NAME,
|
|
massPerUnit: 0.2,
|
|
stackSize: 500,
|
|
});
|
|
const alloyId = await ensureResource(payload, pool, {
|
|
name: "Reclaimed Alloy",
|
|
codeName: ALLOY_CODE_NAME,
|
|
massPerUnit: 1.5,
|
|
stackSize: 200,
|
|
});
|
|
|
|
// Labor class the barracks/workshop upgrade gates reference. Key must
|
|
// match the requiredLaborClass strings below.
|
|
const engineerFound = await payload.find({
|
|
collection: "labor-classifications",
|
|
where: { key: { equals: "engineer" } },
|
|
limit: 1,
|
|
overrideAccess: true,
|
|
});
|
|
if (!engineerFound.docs[0]) {
|
|
await payload.create({
|
|
collection: "labor-classifications",
|
|
data: {
|
|
name: "Interior Engineer",
|
|
key: "engineer",
|
|
defaultUnitSalary: 20,
|
|
availableHeadcount: 8,
|
|
description: "Trades crew for Superstructure interior work (upgrades, repairs, stations).",
|
|
},
|
|
overrideAccess: true,
|
|
});
|
|
payload.logger.info("[seed-interior] created labor class engineer");
|
|
}
|
|
|
|
// Technology gate for the barracks L2 upgrade. Left unresearched on
|
|
// purpose so the gate is live; research it from the tech tree to open L2.
|
|
const techName = "Modular Habitation";
|
|
const techFound = await payload.find({
|
|
collection: "technologies",
|
|
where: { name: { equals: techName } },
|
|
limit: 1,
|
|
overrideAccess: true,
|
|
});
|
|
let techId = techFound.docs[0]?.id as number | undefined;
|
|
if (!techId) {
|
|
const techCreated = await payload.create({
|
|
collection: "technologies",
|
|
data: {
|
|
name: techName,
|
|
approvalStatus: "approved",
|
|
type: "upgrade",
|
|
summary:
|
|
"Prefab habitation modules; unlocks expanded berths in the Superstructure barracks.",
|
|
researchCosts: { minimumResearchDuration: 240 },
|
|
},
|
|
overrideAccess: true,
|
|
});
|
|
techId = techCreated.id as number;
|
|
payload.logger.info("[seed-interior] created technology 'Modular Habitation' (unresearched)");
|
|
}
|
|
|
|
// Asset produced by the disabled template recipe.
|
|
const assetName = "Field Repair Kit";
|
|
const kitFound = await payload.find({
|
|
collection: "assets",
|
|
where: { name: { equals: assetName } },
|
|
limit: 1,
|
|
overrideAccess: true,
|
|
});
|
|
let kitId = kitFound.docs[0]?.id as number | undefined;
|
|
if (!kitId) {
|
|
const kitCreated = await payload.create({
|
|
collection: "assets",
|
|
data: {
|
|
name: assetName,
|
|
approvalStatus: "approved",
|
|
className: "ptf_repair_kit",
|
|
assetType: "tool",
|
|
tradeable: true,
|
|
isLive: true,
|
|
marketTrading: { Rarity: "common", perceivedValue: { baseBuyPrice: 500, baseSellPrice: 400 } },
|
|
crafting: { craftable: false, craftingData: { craftingTimePerUnit: 0, batchSize: 1 } },
|
|
storageDimensions: { gridWidth: 1, gridHeight: 1 },
|
|
},
|
|
overrideAccess: true,
|
|
});
|
|
kitId = kitCreated.id as number;
|
|
payload.logger.info(`[seed-interior] created asset ${assetName}`);
|
|
}
|
|
|
|
// --- Blueprint -----------------------------------------------------------
|
|
|
|
const rooms = [
|
|
{
|
|
key: "barracks",
|
|
name: "Barracks",
|
|
roomType: "barracks",
|
|
description: {
|
|
root: {
|
|
type: "root",
|
|
children: [
|
|
{
|
|
type: "paragraph",
|
|
children: [
|
|
{
|
|
type: "text",
|
|
text: "Sleeping quarters for the garrison. Upgrades add berths and stores capacity.",
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
},
|
|
hotspot: { x: 8, y: 12, width: 26, height: 34 },
|
|
initialLevel: 1,
|
|
maxLevel: 3,
|
|
levels: [
|
|
{ level: 1, label: "Bunked", upgradeDurationMinutes: 60 },
|
|
{
|
|
level: 2,
|
|
label: "Expanded berths",
|
|
upgradeDurationMinutes: 720,
|
|
upgradeMaterials: [{ resource: scrapId, amount: 40 }],
|
|
requiredTech: [techId],
|
|
requiredLaborClass: "engineer",
|
|
requiredHeadcount: 1,
|
|
effects: [{ type: "storage_capacity" as const, value: 500 }],
|
|
},
|
|
{
|
|
level: 3,
|
|
label: "NCO wing",
|
|
upgradeDurationMinutes: 1440,
|
|
upgradeMaterials: [
|
|
{ resource: scrapId, amount: 60 },
|
|
{ resource: energyId, amount: 10 },
|
|
],
|
|
requiredLaborClass: "engineer",
|
|
requiredHeadcount: 1,
|
|
effects: [{ type: "labor_capacity" as const, value: 2 }],
|
|
},
|
|
],
|
|
decay: { conditionPerDay: 0.5, effectFloor: 20, junkMax: 3, initialJunkLevel: 1 },
|
|
junkLoot: [{ kind: "resource" as const, item: resRef(scrapId), amountMin: 3, amountMax: 12, weight: 1 }],
|
|
},
|
|
{
|
|
key: "workshop",
|
|
name: "Workshop",
|
|
roomType: "workshop",
|
|
description: {
|
|
root: {
|
|
type: "root",
|
|
children: [
|
|
{
|
|
type: "paragraph",
|
|
children: [
|
|
{
|
|
type: "text",
|
|
text: "The Fabricator presses Scrap and Energy Cells into reclaimed alloy.",
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
},
|
|
hotspot: { x: 42, y: 16, width: 22, height: 30 },
|
|
initialLevel: 1,
|
|
maxLevel: 2,
|
|
levels: [
|
|
{ level: 1, label: "Fitted out", upgradeDurationMinutes: 60 },
|
|
{
|
|
level: 2,
|
|
label: "Tuned line",
|
|
upgradeDurationMinutes: 1440,
|
|
upgradeMaterials: [
|
|
{ resource: scrapId, amount: 80 },
|
|
{ resource: energyId, amount: 20 },
|
|
],
|
|
requiredLaborClass: "engineer",
|
|
requiredHeadcount: 1,
|
|
effects: [{ type: "output_bonus" as const, value: 0.25 }],
|
|
},
|
|
],
|
|
station: {
|
|
name: "Fabricator",
|
|
enabled: true,
|
|
recipes: [
|
|
{
|
|
key: "alloy_cast",
|
|
name: "Alloy Casting",
|
|
enabled: true,
|
|
durationMinutes: 30,
|
|
requiredRoomLevel: 1,
|
|
inputs: [
|
|
{ resource: scrapId, amount: 8 },
|
|
{ resource: energyId, amount: 2 },
|
|
],
|
|
outputs: [{ kind: "resource" as const, item: resRef(alloyId), amount: 1 }],
|
|
},
|
|
{
|
|
key: "alloy_batch",
|
|
name: "Batch Smelting",
|
|
enabled: true,
|
|
durationMinutes: 240,
|
|
requiredRoomLevel: 2,
|
|
inputs: [
|
|
{ resource: scrapId, amount: 30 },
|
|
{ resource: energyId, amount: 8 },
|
|
],
|
|
outputs: [{ kind: "resource" as const, item: resRef(alloyId), amount: 4 }],
|
|
},
|
|
{
|
|
// Asset-output template. Stays disabled at launch: asset credit
|
|
// is gated behind Game Rules > interior assetOutputsEnabled.
|
|
key: "repair_kit_assembly",
|
|
name: "Field Repair Kit Assembly",
|
|
enabled: false,
|
|
durationMinutes: 120,
|
|
requiredRoomLevel: 1,
|
|
inputs: [
|
|
{ resource: scrapId, amount: 15 },
|
|
{ resource: energyId, amount: 1 },
|
|
],
|
|
outputs: [{ kind: "asset" as const, item: assetRef(kitId), amount: 1 }],
|
|
},
|
|
],
|
|
},
|
|
fixtures: [
|
|
{
|
|
key: "lathe",
|
|
name: "Lathe",
|
|
repairMaterials: [{ resource: scrapId, amount: 10 }],
|
|
repairDurationMinutes: 60,
|
|
},
|
|
],
|
|
decay: { conditionPerDay: 0.5, effectFloor: 25, junkMax: 4, initialJunkLevel: 0 },
|
|
junkLoot: [{ kind: "resource" as const, item: resRef(scrapId), amountMin: 2, amountMax: 8, weight: 1 }],
|
|
},
|
|
{
|
|
key: "rubble_annex",
|
|
name: "Rubble Annex",
|
|
roomType: "annex",
|
|
description: {
|
|
root: {
|
|
type: "root",
|
|
children: [
|
|
{
|
|
type: "paragraph",
|
|
children: [
|
|
{
|
|
type: "text",
|
|
text: "A collapsed side hall. Clear the doorway, clean out the rubble, and the salvage bench pays for itself.",
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
},
|
|
hotspot: { x: 70, y: 54, width: 22, height: 32 },
|
|
initialLevel: 0,
|
|
maxLevel: 2,
|
|
levels: [
|
|
{
|
|
level: 1,
|
|
label: "Opened up",
|
|
upgradeDurationMinutes: 180,
|
|
upgradeMaterials: [{ resource: scrapId, amount: 10 }],
|
|
},
|
|
{
|
|
level: 2,
|
|
label: "Reclaimed",
|
|
upgradeDurationMinutes: 720,
|
|
upgradeMaterials: [{ resource: scrapId, amount: 30 }],
|
|
},
|
|
],
|
|
station: {
|
|
name: "Salvage Bench",
|
|
enabled: true,
|
|
recipes: [
|
|
{
|
|
key: "strip_salvage",
|
|
name: "Strip Salvage",
|
|
enabled: true,
|
|
durationMinutes: 30,
|
|
requiredRoomLevel: 1,
|
|
inputs: [{ resource: energyId, amount: 1 }],
|
|
outputs: [{ kind: "resource" as const, item: resRef(scrapId), amount: 12 }],
|
|
},
|
|
],
|
|
},
|
|
fixtures: [
|
|
{
|
|
key: "doorway",
|
|
name: "Collapsed Doorway",
|
|
condition: 0,
|
|
brokenByDefault: true,
|
|
blocksStation: true,
|
|
repairMaterials: [{ resource: scrapId, amount: 15 }],
|
|
repairDurationMinutes: 90,
|
|
},
|
|
],
|
|
decay: { conditionPerDay: 1, effectFloor: 25, junkMax: 5, initialJunkLevel: 3 },
|
|
junkLoot: [
|
|
{ kind: "resource" as const, item: resRef(scrapId), amountMin: 5, amountMax: 20, weight: 6 },
|
|
{ kind: "resource" as const, item: resRef(energyId), amountMin: 1, amountMax: 3, weight: 2 },
|
|
],
|
|
},
|
|
];
|
|
|
|
let blueprint = await payload.find({
|
|
collection: "structures",
|
|
where: { codeName: { equals: BLUEPRINT_CODE_NAME } },
|
|
limit: 1,
|
|
overrideAccess: true,
|
|
});
|
|
|
|
let blueprintId: number;
|
|
if (blueprint.docs[0]) {
|
|
blueprintId = blueprint.docs[0].id as number;
|
|
} else {
|
|
const created = await payload.create({
|
|
collection: "structures",
|
|
data: {
|
|
name: "Superstructure",
|
|
codeName: BLUEPRINT_CODE_NAME,
|
|
approvalStatus: "approved",
|
|
category: "civilian",
|
|
description: {
|
|
root: {
|
|
type: "root",
|
|
version: 1,
|
|
direction: null,
|
|
format: "",
|
|
indent: 0,
|
|
children: [
|
|
{
|
|
type: "paragraph",
|
|
version: 1,
|
|
direction: null,
|
|
format: "",
|
|
indent: 0,
|
|
children: [
|
|
{
|
|
type: "text",
|
|
version: 1,
|
|
text: "The unit's main base structure. Its interior is the Superstructure Interior System.",
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
},
|
|
materials: [{ resource: scrapId, amount: 100 }],
|
|
constructionDurationMinutes: 1,
|
|
terrainType: "land",
|
|
maxHealth: 100,
|
|
},
|
|
draft: false,
|
|
overrideAccess: true,
|
|
});
|
|
blueprintId = created.id as number;
|
|
payload.logger.info(`[seed-interior] created Superstructure blueprint: ${blueprintId}`);
|
|
}
|
|
|
|
await payload.update({
|
|
collection: "structures",
|
|
id: blueprintId,
|
|
overrideAccess: true,
|
|
data: {
|
|
interiorEnabled: true,
|
|
interiorName: "The Superstructure",
|
|
interiorAccess: { mode: "anyone" },
|
|
rooms,
|
|
} as Record<string, unknown>,
|
|
});
|
|
|
|
// --- Instance ------------------------------------------------------------
|
|
|
|
const instances = await payload.find({
|
|
collection: "game-structures",
|
|
where: { type: { equals: blueprintId } },
|
|
limit: 5,
|
|
overrideAccess: true,
|
|
});
|
|
if (!instances.docs[0]) {
|
|
await payload.create({
|
|
collection: "game-structures",
|
|
data: {
|
|
name: "Superstructure",
|
|
type: blueprintId,
|
|
constructionStatus: "complete",
|
|
},
|
|
draft: false,
|
|
overrideAccess: true,
|
|
});
|
|
payload.logger.info("[seed-interior] created Superstructure game instance");
|
|
}
|
|
|
|
payload.logger.info(
|
|
`[seed-interior] done; blueprint ${blueprintId} (interior enabled, ${rooms.length} rooms)`,
|
|
);
|
|
return blueprintId;
|
|
} finally {
|
|
await pool.end();
|
|
}
|
|
};
|
|
|
|
// Standalone entry point (`bun src/tools/seed/seedInteriorContent.ts`).
|
|
// Skipped when imported by seedEverything, which awaits the export itself.
|
|
if ((import.meta as { main?: boolean }).main) {
|
|
await seedInteriorContent();
|
|
process.exit(0);
|
|
}
|