Files
scrollsmith/dungeonGenerator.js

147 lines
8.1 KiB
JavaScript

import { callOllama } from "./ollamaClient.js";
// Utility: strip markdown artifacts and clean up extra whitespace
function cleanText(str) {
if (!str) return "";
return str
.replace(/^#+\s*/gm, "")
.replace(/\*\*(.*?)\*\*/g, "$1") // Removes bolding
.replace(/[*_`]/g, "") // Removes other markdown
.replace(/\s+/g, " ")
.trim();
}
function parseList(raw) {
return raw
.split(/\n?\d+[).]\s+/)
.map(line => cleanText(line))
.filter(Boolean);
}
function parseObjects(raw, type = "rooms") {
let cleanedRaw = raw.replace(/Intermediate Rooms:/i, "").replace(/Climax Room:/i, "").trim();
return cleanedRaw
.split(/\n?\d+[).]\s+/)
.map(entry => cleanText(entry))
.filter(Boolean)
.map(entry => {
const [name, ...descParts] = entry.split(/[-–—:]/);
const desc = descParts.join(" ").trim();
if (type === "rooms") return { name: name.trim(), description: desc };
if (type === "encounters") return { name: name.trim(), details: desc };
if (type === "npcs") return { name: name.trim(), trait: desc };
return entry;
});
}
export async function generateDungeon() {
// Step 1: Titles
const generatedTitlesRaw = await callOllama(
`Generate 50 short, punchy dungeon titles (max 5 words each), numbered as a plain text list.
Each title should come from a different style or theme. Make the set varied and evocative. For example:
- OSR / classic tabletop: gritty, mysterious, old-school
- Mörk Borg: dark, apocalyptic, foreboding
- Pulpy fantasy: adventurous, dramatic, larger-than-life
- Mildly sci-fi: alien, technological, strange
- Weird fantasy: uncanny, surreal, unsettling
- Whimsical: fun, quirky, playful
Avoid repeating materials or adjectives. Absolutely do not use the words "Obsidian" or "Clockwork" in any title. Do not include explanations, markdown, or preambles. Do not include the style or theme in parenthesis. Only the 50 numbered titles.`,
undefined, 5, "Step 1: Titles"
);
const generatedTitles = parseList(generatedTitlesRaw);
console.log("Generated Titles:", generatedTitles);
const title = generatedTitles[Math.floor(Math.random() * generatedTitles.length)];
console.log("Selected title:", title);
// Step 2: Core Concepts
const coreConceptsRaw = await callOllama(
`For a dungeon titled "${title}", generate three core concepts: a central conflict, a primary faction, and a major environmental hazard or dynamic element.
Output as a numbered list with bolded headings. Plain text only. Absolutely do not use the word "Obsidian" or "obsidian" anywhere in the output.
Example:
1. **Central Conflict:** The dungeon's power source is failing, causing reality to warp.
2. **Primary Faction:** A group of rival cultists trying to seize control of the power source.
3. **Dynamic Element:** Zones of temporal distortion that cause random, brief time shifts.`,
undefined, 5, "Step 2: Core Concepts"
);
const coreConcepts = coreConceptsRaw;
console.log("Core Concepts:", coreConcepts);
// Step 3: Flavor Text & Hooks
const flavorHooksRaw = await callOllama(
`Based on the title "${title}" and these core concepts:
${coreConcepts}
Write a single evocative paragraph describing the location. Maximum 2 sentences. Maximum 100 words. Then, generate 3 short adventure hooks or rumors.
The hooks should reference the central conflict, faction, and dynamic element.
Output two sections labeled "Description:" and "Hooks & Rumors:". Use a numbered list for the hooks. Plain text only. Absolutely do not use the word "Obsidian" or "obsidian" anywhere in the output.`,
undefined, 5, "Step 3: Flavor & Hooks"
);
const [flavorSection, hooksSection] = flavorHooksRaw.split(/Hooks & Rumors[:\n]/i);
const flavor = cleanText(flavorSection.replace(/Description[:\n]*/i, ""));
const hooksRumors = parseList(hooksSection || "");
console.log("Flavor Text:", flavor);
console.log("Hooks & Rumors:", hooksRumors);
// Step 4: Key Rooms
const keyRoomsRaw = await callOllama(
`Based on the title "${title}", description "${flavor}", and these core concepts:
${coreConcepts}
Generate two key rooms that define the dungeon's narrative arc.
1. **Entrance Room:** Give it a name and a description that sets the tone and introduces the environmental hazard.
2. **Climax Room:** Give it a name and a description that includes the primary faction and the central conflict.
Output as two numbered items, plain text only. Do not use bolded headings. Do not include any intro or other text. Only the numbered list. Absolutely do not use the word "Obsidian" or "obsidian" anywhere in the output.`,
undefined, 5, "Step 4: Key Rooms"
);
const [entranceSection, climaxSection] = keyRoomsRaw.split(/\n?2[).] /); // Split on "2. " to separate the two rooms
const entranceRoom = parseObjects(entranceSection, "rooms")[0];
const climaxRoom = parseObjects(`1. ${climaxSection}`, "rooms")[0]; // Prepend "1. " to make parsing consistent
console.log("Entrance Room:", entranceRoom);
console.log("Climax Room:", climaxRoom);
// Step 5: Main Content (Locations, Encounters, NPCs, Treasures)
const mainContentRaw = await callOllama(
`Based on the following dungeon elements and the need for narrative flow:
Title: "${title}"
Description: "${flavor}"
Core Concepts:
${coreConcepts}
Entrance Room: ${JSON.stringify(entranceRoom)}
Climax Room: ${JSON.stringify(climaxRoom)}
Generate the rest of the dungeon's content to fill the space between the entrance and the climax.
- **Strictly 3 Locations:** Each with a name and a short description (max 20 words). The description must be a single sentence. It should contain an environmental feature, a puzzle, or an element that connects to the core concepts or the final room.
- **Strictly 4 Encounters:** Name and details. At least two encounters must be directly tied to the primary faction.
- **Strictly 3 NPCs:** Proper name and a trait. One NPC should be a member of the primary faction, one should be a potential ally, and one should be a rival.
- **Strictly 3 Treasures:** Name and a description that includes a danger or side-effect. Each treasure should be thematically tied to a specific encounter or room.
Output as four separate numbered lists. Label the lists as "Locations:", "Encounters:", "NPCs:", and "Treasures:". Do not use any bolding, preambles, or extra text. Absolutely do not use the word "Obsidian" or "obsidian" anywhere in the output.`,
undefined, 5, "Step 5: Main Content"
);
const [intermediateRoomsSection, encountersSection, npcsSection, treasureSection] = mainContentRaw.split(/Encounters:|NPCs:|Treasures?:/i);
const intermediateRooms = parseObjects(intermediateRoomsSection.replace(/Locations:/i, ""), "rooms");
const rooms = [entranceRoom, ...intermediateRooms, climaxRoom];
const encounters = parseObjects(encountersSection || "", "encounters");
const npcs = parseObjects(npcsSection || "", "npcs");
const treasure = parseList(treasureSection || "");
console.log("Rooms:", rooms);
console.log("Encounters:", encounters);
console.log("NPCs:", npcs);
console.log("Treasure:", treasure);
// Step 6: Player Choices and Consequences
const plotResolutionsRaw = await callOllama(
`Based on all of the following elements, suggest 3 possible, non-conflicting story climaxes or plot resolutions for adventurers exploring this location. Each resolution must provide a meaningful choice with a tangible consequence, directly related to the Central Conflict, the Primary Faction, or the NPCs.
Dungeon Elements:
${JSON.stringify({ title, flavor, hooksRumors, rooms, encounters, treasure, npcs, coreConcepts }, null, 2)}
Start each item with phrases like "The adventurers could" or "The PCs might". Deepen the narrative texture and allow for roleplay and tactical creativity. Keep each item short (max 2 sentences). Output as a numbered list, plain text only. Absolutely do not use the word "Obsidian" or "obsidian" anywhere in the output.`,
undefined, 5, "Step 6: Plot Resolutions"
);
const plotResolutions = parseList(plotResolutionsRaw);
console.log("Plot Resolutions:", plotResolutions);
console.log("\nDungeon generation complete!");
return { title, flavor, map: "map.png", hooksRumors, rooms, encounters, treasure, npcs, plotResolutions };
}