import { callOllama } from "./ollamaClient.js"; // Utility: strip markdown artifacts function cleanText(str) { return str .replace(/^#+\s*/gm, "") .replace(/\*\*(.*?)\*\*/g, "$1") .replace(/[*_`]/g, "") .replace(/\s+/g, " ") .trim(); } function parseList(raw) { return raw .split(/\n?\d+[).]\s+/) .map(line => cleanText(line)) .filter(Boolean); } function parseObjects(raw, type = "rooms") { return raw .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. **The Entrance/Start Room:** The first room the adventurers will enter. Give it a name and a description that sets the tone and introduces the environmental hazard. 2. **The Climax/Final Room:** The final room where the central conflict will be resolved. Give it a name and a description that includes the primary faction and the central conflict. Output two numbered lists, labeled "Entrance Room:" and "Climax Room:". Plain text only. Absolutely do not use the word "Obsidian" or "obsidian" anywhere in the output.`, undefined, 5, "Step 4: Key Rooms" ); const [entranceSection, climaxSection] = keyRoomsRaw.split(/Climax Room[:\n]/i); const entranceRoom = parseObjects(entranceSection.replace(/Entrance Room[:\n]*/i, ""), "rooms")[0]; const climaxRoom = parseObjects(climaxSection || "", "rooms")[0]; console.log("Entrance Room:", entranceRoom); console.log("Climax Room:", climaxRoom); // Step 5: Main Content (Rooms, 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. - **3 Intermediate Rooms:** Name and a description. Each description must be a maximum of 50 words and contain an environmental feature, a puzzle, or an element that connects to the core concepts or the final room. - **4 Encounters:** Name and details. At least two encounters must be directly tied to the primary faction. - **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. - **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, labeled "Intermediate Rooms:", "Encounters:", "NPCs:", and "Treasures:". Plain text only. 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[:\n]|NPCs[:\n]|Treasures?[:\n]/i); const intermediateRooms = parseObjects(intermediateRoomsSection.replace(/Intermediate Rooms[:\n]*/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 }; }