324 lines
19 KiB
JavaScript
324 lines
19 KiB
JavaScript
import { callOllama } from "./ollamaClient.js";
|
|
import { cleanText } from "./textUtils.js";
|
|
import {
|
|
parseList,
|
|
parseObjects,
|
|
parseMainContentSections,
|
|
parseRandomEventsRaw,
|
|
splitCombinedEncounters,
|
|
} from "./parsing.js";
|
|
import {
|
|
deduplicateRoomsByName,
|
|
padNpcsToMinimum,
|
|
buildEncountersList,
|
|
mergeRandomEventsWithFallbacks,
|
|
limitIntermediateRooms,
|
|
fixRoomPlaceholderName,
|
|
} from "./dungeonBuild.js";
|
|
import { validateAndFixContent } from "./contentFixes.js";
|
|
|
|
export async function generateDungeon() {
|
|
// Step 1: Titles
|
|
const generatedTitles = 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
|
|
|
|
CRITICAL: Ensure all spelling is correct. Double-check all words before outputting.
|
|
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. Do not include the theme in the name of the title (no parenthesis).`,
|
|
undefined, 5, "Step 1: Titles"
|
|
);
|
|
console.log("Generated Titles:", generatedTitles);
|
|
const titlesList = parseList(generatedTitles);
|
|
const title = titlesList[Math.floor(Math.random() * titlesList.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 50-60 words. Then, generate 4-5 short adventure hooks or rumors.
|
|
The hooks should reference the central conflict, faction, and dynamic element. Hooks should suggest different approaches (stealth, diplomacy, force, exploration) and create anticipation.
|
|
|
|
EXAMPLE OF GOOD HOOK:
|
|
"A merchant's cart was found abandoned near the entrance, its cargo of rare herbs scattered. The merchant's journal mentions strange lights in the depths and a warning about 'the watchers'."
|
|
|
|
CRITICAL: Hooks must be concise to fit in a single column on a one-page dungeon layout. Each hook must be 25-30 words maximum. Be specific with details, not vague.
|
|
CRITICAL: Ensure all spelling is correct. Double-check all words, especially proper nouns and technical terms.
|
|
Output two sections labeled "Description:" and "Hooks & Rumors:". Use a numbered list for the hooks. Plain text only. Do not use em-dashes (—) anywhere in the output. 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 rawFlavor = cleanText(flavorSection.replace(/Description[:\n]*/i, ""));
|
|
const flavorWords = rawFlavor.split(/\s+/);
|
|
const flavor = flavorWords.length > 60 ? flavorWords.slice(0, 60).join(' ') + '...' : rawFlavor;
|
|
const hooksRumors = parseList(hooksSection || "").map(h => h.replace(/^[^:]+:\s*/, '').trim());
|
|
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.
|
|
CRITICAL: These rooms need rich environmental and tactical details with multiple interaction possibilities.
|
|
|
|
EXAMPLE OF GOOD ROOM DESCRIPTION:
|
|
"Chamber of Echoes: Flickering torchlight casts dancing shadows across moss-covered walls. A constant dripping echoes from stalactites overhead, and the air smells of damp earth and ozone. Three stone pillars provide cover, while a raised dais in the center offers high ground. A rusted lever on the west wall controls a hidden portcullis. The floor is slick with moisture, making movement difficult."
|
|
|
|
1. Entrance Room: Give it a name (max 5 words) and a description (25-35 words) that MUST include:
|
|
- Immediate observable features and environmental details (lighting, sounds, smells, textures, temperature, visibility)
|
|
- Interactable elements that players can use (levers, objects, portals, mechanisms, environmental hazards)
|
|
- Tactical considerations (cover, elevation, movement restrictions, line of sight)
|
|
- Sets the tone and introduces the environmental hazard/dynamic element
|
|
|
|
2. Climax Room: Give it a name (max 5 words) and a description (25-35 words) that MUST include:
|
|
- Connection to the primary faction and the central conflict
|
|
- Rich environmental and tactical details
|
|
- Multiple approach options or solutions
|
|
- Tactical considerations and environmental factors that affect gameplay
|
|
|
|
EXACT FORMAT REQUIRED - each room on its own numbered line:
|
|
1. Room Name: Description text here.
|
|
2. Room Name: Description text here.
|
|
|
|
CRITICAL: Ensure all spelling is correct. Double-check all words before outputting.
|
|
CRITICAL: Be specific and concrete. Avoid vague words like "some", "various", "several" without details.
|
|
Output ONLY the two numbered items, one per line. Use colons (:) to separate room names from descriptions, not em-dashes. Do not use em-dashes (—) anywhere. Do not combine items. Do not use bolded headings. Do not include any intro or other text. 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[).] /);
|
|
const entranceRoom = parseObjects(entranceSection, "rooms")[0];
|
|
const climaxRoom = parseObjects(`1. ${climaxSection}`, "rooms")[0];
|
|
|
|
if (entranceRoom) fixRoomPlaceholderName(entranceRoom);
|
|
if (climaxRoom) fixRoomPlaceholderName(climaxRoom);
|
|
|
|
console.log("Entrance Room:", entranceRoom);
|
|
console.log("Climax Room:", climaxRoom);
|
|
|
|
// Step 5: Main Content (Locations, Encounters, NPCs, Treasures, Random Events)
|
|
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. CRITICAL: All content must fit on a single one-page dungeon layout with three columns. Keep descriptions rich and evocative with tactical/environmental details.
|
|
|
|
- **Strictly 3 Locations (EXACTLY 3, no more, no less):** Each with a name (max 6 words) and a description (25-35 words). Each room MUST include:
|
|
- Rich environmental features that affect gameplay (lighting, sounds, smells, textures, temperature, visibility)
|
|
- Interactable elements that players can use (levers, objects, portals, mechanisms, environmental hazards)
|
|
- Multiple approaches or solutions to challenges in the room
|
|
- Tactical considerations (cover, elevation, movement restrictions, line of sight)
|
|
- Hidden aspects discoverable through interaction or investigation
|
|
Format as "Name: description" using colons, NOT em-dashes.
|
|
|
|
EXAMPLE LOCATION:
|
|
"Whispering Gallery: Dim phosphorescent fungi line the walls, casting an eerie green glow. The air hums with a low-frequency vibration that makes conversation difficult. Two collapsed pillars create natural cover, while a narrow ledge 10 feet up offers a sniper position. A hidden pressure plate near the entrance triggers a portcullis trap."
|
|
|
|
- **Strictly 6 Encounters:** Numbered 1-6 (for d6 rolling). Name (max 6 words) and details (2 sentences MAX, approximately 25-40 words). Each encounter MUST:
|
|
- Start with the room/location name followed by a colon, then the details (e.g., "Location Name: Details text")
|
|
- The location name must match one of the actual room names from this dungeon
|
|
- Include environmental hazards/opportunities (cover, elevation, traps, interactable objects, terrain features)
|
|
- Include tactical considerations (positioning, line of sight, escape routes, bottlenecks, high ground)
|
|
- Offer multiple resolution options (combat, negotiation, stealth, puzzle-solving, environmental manipulation, timing-based solutions)
|
|
- Include consequences and outcomes tied to player choices
|
|
- Integrate with the environmental dynamic element from core concepts
|
|
- At least two encounters must be directly tied to the primary faction
|
|
Format as "Name: Location Name: details" using colons, NOT em-dashes. CRITICAL: Always start encounter details with the location name and a colon.
|
|
|
|
EXAMPLE ENCOUNTER:
|
|
"Guardian Golem: Chamber of Echoes: The golem activates when the lever is pulled, blocking the exit. It's vulnerable to water damage from the dripping stalactites. Players can use the pillars for cover or try to disable it by breaking the rune on its back. If defeated peacefully, it reveals a hidden passage."
|
|
|
|
- **Strictly 4-5 NPCs:** Proper name (max 4 words) and a description (50-65 words). Each NPC MUST include:
|
|
- Clear motivation or goal
|
|
- Relationship to primary faction
|
|
- How they can help or hinder the party
|
|
- Quirks or memorable traits
|
|
- Multiple interaction possibilities (negotiation, intimidation, help, betrayal)
|
|
- One NPC should be a key figure tied to the central conflict
|
|
- One should be a member of the primary faction, one should be a potential ally, one should be a rival
|
|
Format as "Name: description" using colons, NOT em-dashes.
|
|
|
|
EXAMPLE NPC:
|
|
"Kaelen the Warden: A former guard who was left behind when the faction retreated. He knows the secret passages but demands the party help him escape. He's paranoid and checks over his shoulder constantly. Can be bribed with food or convinced through shared stories of betrayal. Will turn on the party if he thinks they're working with the faction."
|
|
|
|
- **Strictly 4-5 Treasures:** Name (max 5 words) and a description (30-40 words). Each treasure MUST:
|
|
- Include a clear danger or side-effect
|
|
- Be connected to a specific encounter, NPC, or room
|
|
- Have story significance beyond just value
|
|
- Have potential for creative use beyond obvious purpose
|
|
- Some should be cursed, have activation requirements, or serve dual purposes
|
|
Format as "Name — Description" using em-dash.
|
|
|
|
EXAMPLE TREASURE:
|
|
"Whispering Blade — This dagger amplifies the wielder's voice to a deafening roar when drawn. Found in the Guardian Golem's chamber, it was used to command the construct. The blade is cursed: each use permanently reduces the wielder's hearing. Can be used to shatter glass or stun enemies, but the curse cannot be removed."
|
|
|
|
- **Strictly 1 Random Events Table:** A d6 table (EXACTLY 6 entries, no more, no less) with random events/wandering encounters. Each entry MUST:
|
|
- Have a short, evocative event name (max 4 words)
|
|
- Provide interesting complications or opportunities (not just combat)
|
|
- Tie to the core concepts and dynamic element
|
|
- Add replayability and surprise
|
|
- Description should be 15-20 words maximum
|
|
- Be UNIQUE and DIFFERENT from each other (no duplicates or generic placeholders)
|
|
- Be SPECIFIC to this dungeon's theme, conflict, and dynamic element
|
|
Format as numbered 1-6 list under "Random Events:" label. Each event must be formatted as "Event Name: Description text" using colons, NOT em-dashes.
|
|
|
|
CRITICAL: Each item must be on its own numbered line. DO NOT combine multiple items into a single numbered entry.
|
|
|
|
EXACT FORMAT REQUIRED (DO NOT use placeholder names like "Location Name", "NPC Name", or "Treasure Name" - use actual creative names):
|
|
Locations:
|
|
1. Actual Room Name: Description text.
|
|
2. Actual Room Name: Description text.
|
|
3. Actual Room Name: Description text.
|
|
|
|
Encounters:
|
|
1. Actual Encounter Name: Actual Room Name: Details text.
|
|
2. Actual Encounter Name: Actual Room Name: Details text.
|
|
3. Actual Encounter Name: Actual Room Name: Details text.
|
|
4. Actual Encounter Name: Actual Room Name: Details text.
|
|
5. Actual Encounter Name: Actual Room Name: Details text.
|
|
6. Actual Encounter Name: Actual Room Name: Details text.
|
|
|
|
NPCs:
|
|
1. Actual Character Name: Description text.
|
|
2. Actual Character Name: Description text.
|
|
3. Actual Character Name: Description text.
|
|
4. Actual Character Name: Description text.
|
|
|
|
Treasures:
|
|
1. Actual Item Name — Description text.
|
|
2. Actual Item Name — Description text.
|
|
3. Actual Item Name — Description text.
|
|
4. Actual Item Name — Description text.
|
|
|
|
Random Events:
|
|
1. Event Name: Event description.
|
|
2. Event Name: Event description.
|
|
3. Event Name: Event description.
|
|
4. Event Name: Event description.
|
|
5. Event Name: Event description.
|
|
6. Event Name: Event description.
|
|
|
|
CRITICAL: Every name must be unique and creative. Never use generic placeholders like "Location Name", "NPC Name", "Encounter Name", or "Treasure Name". Use actual descriptive names that fit the dungeon's theme.
|
|
|
|
CRITICAL: Ensure all spelling is correct. Double-check all words, especially proper nouns, character names, and location names. Verify consistency of names across all sections.
|
|
|
|
CRITICAL: Location name matching - When writing encounters, the location name in the encounter details MUST exactly match one of the room names you've created (Entrance Room, Climax Room, or one of the 3 Locations). Double-check that every encounter location matches an actual room name.
|
|
|
|
CRITICAL: Avoid vague language - Do not use words like "some", "various", "several", "many", "few", "things", "stuff", "items", or "objects" without specific details. Be concrete and specific in all descriptions.
|
|
|
|
CRITICAL: All names required - Every room, encounter, NPC, and treasure MUST have a name. Do not leave names blank or use placeholders. If you cannot think of a name, create one based on the dungeon's theme.
|
|
|
|
CRITICAL: You MUST output exactly five separate sections with these exact labels on their own lines:
|
|
"Locations:"
|
|
"Encounters:"
|
|
"NPCs:"
|
|
"Treasures:"
|
|
"Random Events:"
|
|
|
|
Each section must start with its label on its own line, followed by numbered items. Do NOT combine sections. Do NOT embed encounters in location descriptions. Each item must be on its own numbered line. Do not use any bolding, preambles, or extra text. Do not use em-dashes (—) in encounters or NPCs, only use colons for those sections. Absolutely do not use the word "Obsidian" or "obsidian" anywhere in the output.`,
|
|
undefined, 5, "Step 5: Main Content"
|
|
);
|
|
const { intermediateRoomsSection, encountersSection, npcsSection, treasureSection, randomEventsSection } =
|
|
parseMainContentSections(mainContentRaw);
|
|
|
|
const intermediateRooms = parseObjects(intermediateRoomsSection.replace(/Locations:/i, ""), "rooms");
|
|
const limitedIntermediateRooms = limitIntermediateRooms(intermediateRooms, 3);
|
|
|
|
const allRooms = [entranceRoom, ...limitedIntermediateRooms, climaxRoom].filter(Boolean);
|
|
const rooms = deduplicateRoomsByName(allRooms);
|
|
const parsedEncounters = splitCombinedEncounters(parseObjects(encountersSection || "", "encounters"));
|
|
const parsedNpcs = parseObjects(npcsSection || "", "npcs");
|
|
const treasure = parseObjects(treasureSection || "", "treasure");
|
|
|
|
const npcs = padNpcsToMinimum(parsedNpcs, coreConcepts, 4);
|
|
const encounters = buildEncountersList(parsedEncounters, rooms, coreConcepts);
|
|
|
|
const randomEventsFilteredMapped = parseRandomEventsRaw(randomEventsSection || "");
|
|
const randomEvents = mergeRandomEventsWithFallbacks(randomEventsFilteredMapped, coreConcepts, 6);
|
|
|
|
[[encounters, 6, 'encounters'], [npcs, 4, 'NPCs'], [treasure, 4, 'treasures'], [randomEvents, 6, 'random events']]
|
|
.filter(([arr, expected]) => arr.length < expected && arr.length > 0)
|
|
.forEach(([arr, expected, name]) => console.warn(`Expected at least ${expected} ${name} but got ${arr.length}`));
|
|
console.log("Rooms:", rooms);
|
|
console.log("Encounters:", encounters);
|
|
console.log("NPCs:", npcs);
|
|
console.log("Treasure:", treasure);
|
|
console.log("Random Events:", randomEvents);
|
|
|
|
// Step 6: Player Choices and Consequences
|
|
const factionName = coreConcepts.match(/Primary Faction[:\s]+([^.]+)/i)?.[1]?.trim() || 'the primary faction';
|
|
const npcNamesList = npcs.map(n => n.name).join(", ");
|
|
|
|
const plotResolutionsRaw = await callOllama(
|
|
`Based on all of the following elements, suggest 4-5 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)}
|
|
|
|
CRITICAL: This content must fit in a single column on a one-page dungeon layout. Keep descriptions meaningful but concise.
|
|
Start each item with phrases like "The adventurers could" or "The adventurers might". Do not use "PCs" or "player characters" - always use "adventurers" instead.
|
|
|
|
EXAMPLE PLOT RESOLUTION:
|
|
"The adventurers could ally with the primary faction, gaining access to their resources but becoming enemies of the rival group. This choice unlocks new areas but closes off diplomatic solutions with other NPCs."
|
|
|
|
IMPORTANT: When referencing NPCs, use these exact names with correct spelling: ${npcNamesList}. When referencing the faction, use: ${factionName}. Ensure all names are spelled consistently and correctly.
|
|
CRITICAL: Double-check all spelling before outputting. Verify all proper nouns match exactly as provided above.
|
|
|
|
Each resolution MUST:
|
|
- Offer meaningful choice with clear consequences
|
|
- Integrate NPCs, faction dynamics, and player actions
|
|
- Include failure states or unexpected outcomes as options
|
|
- Reflect different approaches players might take
|
|
Keep each item to 1-2 sentences MAX (approximately 15-25 words). Be extremely concise. Output as a numbered list, plain text only. Do not use em-dashes (—) anywhere in the output. 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);
|
|
|
|
// Step 7: Validation and Content Fixing
|
|
console.log("\n[Validation] Running content validation and fixes...");
|
|
const dungeonData = {
|
|
title,
|
|
flavor,
|
|
map: "map.png",
|
|
hooksRumors,
|
|
rooms,
|
|
encounters,
|
|
treasure,
|
|
npcs,
|
|
plotResolutions,
|
|
randomEvents,
|
|
coreConcepts
|
|
};
|
|
|
|
const validatedData = validateAndFixContent(dungeonData);
|
|
|
|
console.log("\nDungeon generation complete!");
|
|
return validatedData;
|
|
}
|