diff --git a/dungeonGenerator.js b/dungeonGenerator.js index 88d3f47..55f0548 100644 --- a/dungeonGenerator.js +++ b/dungeonGenerator.js @@ -1,11 +1,12 @@ import { callOllama } from "./ollamaClient.js"; -// Utility: strip markdown artifacts +// Utility: strip markdown artifacts and clean up extra whitespace function cleanText(str) { + if (!str) return ""; return str .replace(/^#+\s*/gm, "") - .replace(/\*\*(.*?)\*\*/g, "$1") - .replace(/[*_`]/g, "") + .replace(/\*\*(.*?)\*\*/g, "$1") // Removes bolding + .replace(/[*_`]/g, "") // Removes other markdown .replace(/\s+/g, " ") .trim(); } @@ -18,7 +19,8 @@ function parseList(raw) { } function parseObjects(raw, type = "rooms") { - return raw + let cleanedRaw = raw.replace(/Intermediate Rooms:/i, "").replace(/Climax Room:/i, "").trim(); + return cleanedRaw .split(/\n?\d+[).]\s+/) .map(entry => cleanText(entry)) .filter(Boolean) @@ -86,18 +88,18 @@ Output two sections labeled "Description:" and "Hooks & Rumors:". Use a numbered `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.`, +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(/Climax Room[:\n]/i); - const entranceRoom = parseObjects(entranceSection.replace(/Entrance Room[:\n]*/i, ""), "rooms")[0]; - const climaxRoom = parseObjects(climaxSection || "", "rooms")[0]; + 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 (Rooms, Encounters, NPCs, Treasures) + // 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}" @@ -108,15 +110,15 @@ 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.`, +- **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[:\n]|NPCs[:\n]|Treasures?[:\n]/i); - const intermediateRooms = parseObjects(intermediateRoomsSection.replace(/Intermediate Rooms[:\n]*/i, ""), "rooms"); + 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"); diff --git a/dungeonTemplate.js b/dungeonTemplate.js index 8a22620..2ea0f4b 100644 --- a/dungeonTemplate.js +++ b/dungeonTemplate.js @@ -204,7 +204,11 @@ export function dungeonTemplate(data) { `).join("")}