diff --git a/dungeonGenerator.js b/dungeonGenerator.js index a720f59..5116f98 100644 --- a/dungeonGenerator.js +++ b/dungeonGenerator.js @@ -47,6 +47,18 @@ function parseObjects(raw, type = "rooms") { return null; } // For other types, use original logic + if (type === "treasure") { + const parts = entry.split(/[—]/); + if (parts.length >= 2) { + const cleanName = parts[0].trim(); + if (cleanName.toLowerCase().includes('treasure name') || cleanName.toLowerCase().includes('actual ')) { + return null; + } + let desc = parts.slice(1).join(' ').trim(); + desc = desc.replace(/^description\s*:?\s*/i, '').trim(); + return { name: cleanName, description: desc }; + } + } const [name, ...descParts] = entry.split(/[-–—:]/); const cleanName = name.trim(); // Skip placeholder names @@ -56,7 +68,8 @@ function parseObjects(raw, type = "rooms") { cleanName.toLowerCase().includes('actual ')) { return null; } - const desc = descParts.join(" ").trim(); + let desc = descParts.join(" ").trim(); + if (type === "npcs") desc = desc.replace(/^description\s*:?\s*/i, '').trim(); const obj = { name: cleanName }; if (type === "rooms") return { ...obj, description: desc }; if (type === "npcs") return { ...obj, trait: desc }; @@ -67,6 +80,18 @@ function parseObjects(raw, type = "rooms") { } const parseEncounterText = (text, idx) => { + // Handle "Encounter N Name Room Name Details" format + const encounterMatch = text.match(/Encounter\s+(\d+)\s+(.+?)\s+(?:Room Name|Location)\s+(.+?)\s+Details\s+(.+)/i); + if (encounterMatch) { + const [, , name, location, details] = encounterMatch; + return { name: name.trim(), details: `${location.trim()}: ${details.trim()}` }; + } + // Handle "Encounter N Name: Location: Details" format + const colonFormat = text.match(/Encounter\s+\d+\s+(.+?):\s*(.+?):\s*(.+)/i); + if (colonFormat) { + const [, name, location, details] = colonFormat; + return { name: name.trim(), details: `${location.trim()}: ${details.trim()}` }; + } const match = text.match(/^(\d+)\s+(.+?)(?::\s*(.+))?$/); if (match) { const [, , name, details] = match; @@ -75,7 +100,7 @@ const parseEncounterText = (text, idx) => { const colonSplit = text.split(/[:]/); if (colonSplit.length > 1) { return { - name: colonSplit[0].replace(/^\d+\s+/, "").trim(), + name: colonSplit[0].replace(/^\d+\s+|Encounter\s+\d+\s+/i, "").trim(), details: colonSplit.slice(1).join(":").trim() }; } @@ -86,17 +111,18 @@ const parseEncounterText = (text, idx) => { details: text.replace(/^\d+\s+[A-Z][a-z]+(?:\s+[A-Z][a-z]+)*\s*/, "").trim() }; } - return { name: `Encounter ${idx + 1}`, details: text.replace(/^\d+\s+/, "").trim() }; + return { name: `Encounter ${idx + 1}`, details: text.replace(/^\d+\s+|Encounter\s+\d+\s+/i, "").trim() }; }; const splitCombinedEncounters = (encounters) => { - const shouldSplit = encounters.length === 1 && (encounters[0].name === "1" || encounters[0].details.match(/\d+\s+[A-Z]/)); + if (encounters.length === 0) return []; + const shouldSplit = encounters.length === 1 && (encounters[0].name === "1" || encounters[0].details?.match(/\d+\s+[A-Z]/) || encounters[0].details?.includes('Encounter')); if (!shouldSplit) return encounters; console.warn("Encounters appear combined, attempting to split..."); const combinedText = encounters[0].details || ""; - const split = combinedText.split(/(?=\d+\s+[A-Z][a-z])/).filter(Boolean); + const split = combinedText.split(/(?=Encounter\s+\d+|\d+\s+[A-Z][a-z])/i).filter(Boolean); return (split.length > 1 || (split.length === 1 && combinedText.length > 100)) - ? split.map(parseEncounterText).filter(e => e?.name && e?.details?.length > 10) + ? split.map((text, idx) => parseEncounterText(text, idx)).filter(e => e?.name && e?.details?.length > 10) : encounters; }; @@ -458,7 +484,7 @@ function validateAndFixContent(dungeonData) { 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. + `Generate 50 different and unique short, punchy dungeon titles (2-4 words maximum, single evocative name, no colons), 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 @@ -474,7 +500,14 @@ Avoid repeating materials or adjectives. Absolutely do not use the words "Obsidi ); const generatedTitles = parseList(generatedTitlesRaw); console.log("Generated Titles:", generatedTitles); - const title = generatedTitles[Math.floor(Math.random() * generatedTitles.length)]; + let title = generatedTitles[Math.floor(Math.random() * generatedTitles.length)]; + // Remove colons, dashes, and other separators, but keep the full title + title = title.split(/[:—–-]/)[0].trim(); + // Only limit to 4 words if it's clearly too long (more than 6 words) + const titleWords = title.split(/\s+/); + if (titleWords.length > 6) { + title = titleWords.slice(0, 4).join(' '); + } console.log("Selected title:", title); // Step 2: Core Concepts @@ -494,7 +527,7 @@ Example: 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 4-5 short adventure hooks or rumors. +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: @@ -506,8 +539,11 @@ Output two sections labeled "Description:" and "Hooks & Rumors:". Use a numbered 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 || ""); + let flavor = cleanText(flavorSection.replace(/Description[:\n]*/i, "")); + const words = flavor.split(/\s+/); + if (words.length > 60) flavor = words.slice(0, 60).join(' ') + '...'; + let hooksRumors = parseList(hooksSection || ""); + hooksRumors = hooksRumors.map(h => h.replace(/^[^:]+:\s*/, '').trim()); console.log("Flavor Text:", flavor); console.log("Hooks & Rumors:", hooksRumors); @@ -632,12 +668,14 @@ EXAMPLE NPC: 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) with random events/wandering encounters. Each entry MUST: +- **Strictly 1 Random Events Table:** A d6 table (EXACTLY 6 entries, no more, no less) with random events/wandering encounters. Each entry MUST: - Provide interesting complications or opportunities (not just combat) - Tie to the core concepts and dynamic element - Add replayability and surprise - Be 15-20 words maximum - Format as numbered 1-6 list under "Random Events:" label. + - 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 a complete, interesting sentence describing what happens. CRITICAL: Each item must be on its own numbered line. DO NOT combine multiple items into a single numbered entry. @@ -678,10 +716,57 @@ Random Events: 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. -Output as five separate numbered lists with these exact labels: "Locations:", "Encounters:", "NPCs:", "Treasures:", and "Random Events:". Each item must be on its own line starting with a number. Do not combine items. 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.`, +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] = mainContentRaw.split(/Encounters:|NPCs:|Treasures?:|Random Events:/i); + let [intermediateRoomsSection, encountersSection, npcsSection, treasureSection, randomEventsSection] = mainContentRaw.split(/Encounters:|NPCs:|Treasures?:|Random Events:/i); + + // Ensure random events section is properly extracted (handle case where label might be missing) + if (!randomEventsSection && mainContentRaw.toLowerCase().includes('random')) { + const randomMatch = mainContentRaw.match(/Random Events?[:\s]*\n?([^]*?)(?=Locations?:|Encounters?:|NPCs?:|Treasures?:|$)/i); + if (randomMatch) { + randomEventsSection = randomMatch[1]; + } + } + + // Ensure NPCs section is properly extracted + if (!npcsSection && mainContentRaw.toLowerCase().includes('npc')) { + const npcMatch = mainContentRaw.match(/NPCs?[:\s]*\n?([^]*?)(?=Treasures?:|Random Events?:|Locations?:|Encounters?:|$)/i); + if (npcMatch) { + npcsSection = npcMatch[1]; + } + } + + // If sections are missing, try to extract from combined output + if (!encountersSection && intermediateRoomsSection.includes('Encounter')) { + const encounterMatches = intermediateRoomsSection.match(/Encounter\s+\d+[^]*?(?=Encounter\s+\d+|NPCs?:|Treasures?:|Random Events?:|Location \d+|$)/gi); + if (encounterMatches && encounterMatches.length > 0) { + encountersSection = encounterMatches.map((m, i) => { + // Convert "Encounter N Name Room Name Location Details" to "N. Name: Location: Details" + const match = m.match(/Encounter\s+(\d+)\s+(.+?)\s+(?:Room Name|Location)\s+(.+?)\s+Details\s+(.+)/i); + if (match) { + const [, num, name, location, details] = match; + return `${num}. ${name.trim()}: ${location.trim()}: ${details.trim().substring(0, 200)}`; + } + // Try format without "Room Name" + const simpleMatch = m.match(/Encounter\s+(\d+)\s+(.+?)\s+([A-Z][^:]+?)\s+Details\s+(.+)/i); + if (simpleMatch) { + const [, num, name, location, details] = simpleMatch; + return `${num}. ${name.trim()}: ${location.trim()}: ${details.trim().substring(0, 200)}`; + } + return `${i + 1}. ${m.trim()}`; + }).join('\n'); + intermediateRoomsSection = intermediateRoomsSection.replace(/Encounter\s+\d+[^]*?(?=Encounter\s+\d+|NPCs?:|Treasures?:|Random Events?:|Location \d+|$)/gi, ''); + } + } + const intermediateRooms = parseObjects(intermediateRoomsSection.replace(/Locations:/i, ""), "rooms"); // Limit to exactly 3 intermediate rooms to ensure total of 5 rooms (entrance + 3 intermediate + climax) const limitedIntermediateRooms = intermediateRooms.slice(0, 3); @@ -689,10 +774,104 @@ Output as five separate numbered lists with these exact labels: "Locations:", "E console.warn(`Expected exactly 3 intermediate locations but got ${intermediateRooms.length}, limiting to first 3`); } const rooms = [entranceRoom, ...limitedIntermediateRooms, climaxRoom]; - const encounters = splitCombinedEncounters(parseObjects(encountersSection || "", "encounters")); - const npcs = splitCombinedNPCs(parseObjects(npcsSection || "", "npcs")); - const treasure = splitCombinedTreasures(parseObjects(treasureSection || "", "treasure")); - const randomEvents = parseList(randomEventsSection || ""); + let encounters = splitCombinedEncounters(parseObjects(encountersSection || "", "encounters")); + let npcs = parseObjects(npcsSection || "", "npcs"); + const treasure = parseObjects(treasureSection || "", "treasure"); + + // Pad NPCs to at least 4 if needed (only if we have some NPCs already) + if (npcs.length > 0 && npcs.length < 4) { + const factionName = coreConcepts.match(/Primary Faction[:\s]+([^.]+)/i)?.[1]?.trim() || 'the primary faction'; + while (npcs.length < 4) { + npcs.push({ + name: `NPC ${npcs.length + 1}`, + trait: `A member of ${factionName.toLowerCase()} with unknown motives.` + }); + } + } + + // Pad encounters to exactly 6 (only pad if we have at least 1 real encounter) + if (encounters.length > 0 && encounters.length < 6) { + const dynamicElement = coreConcepts.match(/Dynamic Element[:\s]+([^.]+)/i)?.[1]?.trim() || 'strange occurrences'; + const conflict = coreConcepts.match(/Central Conflict[:\s]+([^.]+)/i)?.[1]?.trim() || 'a threat'; + while (encounters.length < 6) { + const roomIndex = encounters.length % rooms.length; + const roomName = rooms[roomIndex]?.name || 'Unknown Location'; + // Use more descriptive fallback names based on room and theme + const fallbackNames = [ + `${roomName} Guardian`, + `${roomName} Threat`, + `${roomName} Challenge`, + `${dynamicElement.split(' ')[0]} Manifestation`, + `${conflict.split(' ')[0]} Encounter`, + `${roomName} Hazard` + ]; + encounters.push({ + name: fallbackNames[encounters.length % fallbackNames.length], + details: `An encounter related to ${dynamicElement.toLowerCase()} occurs here.` + }); + } + } else if (encounters.length === 0) { + // If no encounters at all, create 6 basic ones + const dynamicElement = coreConcepts.match(/Dynamic Element[:\s]+([^.]+)/i)?.[1]?.trim() || 'strange occurrences'; + for (let i = 0; i < 6; i++) { + const roomIndex = i % rooms.length; + const roomName = rooms[roomIndex]?.name || 'Unknown Location'; + encounters.push({ + name: `${roomName} Encounter`, + details: `An encounter related to ${dynamicElement.toLowerCase()} occurs here.` + }); + } + } + let randomEvents = parseList(randomEventsSection || ""); + // Filter out placeholder events and strip any title prefixes + randomEvents = randomEvents + .filter(e => + e && + e.toLowerCase() !== 'a random event occurs' && + e.toLowerCase() !== 'a random event occurs.' && + !e.toLowerCase().includes('placeholder') && + e.length > 10 + ) + .map(e => { + // Strip title prefixes like "Event 1:", "Random Event:", "Guardian Golem:", "Location Name:", etc. + // Remove numbered prefixes, random event labels, and location/encounter name prefixes + let cleaned = e.replace(/^(Event\s+\d+[:\s]+|Random\s+Event[:\s]+|Random\s+Events?[:\s]+)/i, '').trim(); + // Remove location/encounter name prefixes (format: "Name: Location: description" or "Name: description") + // If it starts with a capitalized phrase followed by colon, it might be a title + const titleMatch = cleaned.match(/^([A-Z][^:]{3,40}):\s*(.+)$/); + if (titleMatch) { + const potentialTitle = titleMatch[1]; + const rest = titleMatch[2]; + // If the rest also has a colon, the first part is likely a title to remove + if (rest.includes(':')) { + cleaned = rest; + } else { + // Check if it looks like a location name (common patterns) + if (potentialTitle.match(/\b(Chamber|Room|Tower|Hall|Temple|Shrine|Crypt|Tomb|Vault|Gate|Bridge|Path|Corridor|Labyrinth|Dungeon|Cavern|Cave|Grotto|Sanctum|Shrine|Altar|Pit|Well|Pool|Fountain|Garden|Courtyard|Plaza|Square|Arena|Colosseum|Theater|Library|Laboratory|Workshop|Forge|Smithy|Barracks|Armory|Treasury|Storehouse|Warehouse|Cellar|Basement|Attic|Loft|Garret|Turret|Spire|Keep|Fortress|Castle|Palace|Manor|Mansion|Estate|Villa|Cottage|Hut|Shack|Tent|Pavilion|Gazebo|Pergola|Arbor|Bower|Grotto|Grot|Den|Lair|Nest|Burrow|Hollow|Recess|Niche|Alcove|Cubby|Cubbyhole|Closet|Wardrobe|Cupboard|Cabinet|Drawer|Shelf|Rack|Peg|Hook|Nail|Screw|Bolt|Rivet|Pin|Clip|Clamp|Brace|Bracket|Support|Prop|Pillar|Column|Post|Pole|Rod|Stick|Staff|Wand|Scepter|Mace|Club|Hammer|Axe|Sword|Dagger|Knife|Blade|Edge|Point|Tip|End|Top|Bottom|Side|Front|Back|Left|Right|Center|Middle|Core|Heart|Soul|Spirit|Mind|Body|Head|Neck|Chest|Torso|Waist|Hip|Thigh|Knee|Leg|Foot|Toe|Heel|Ankle|Shin|Calf|Thigh|Buttock|Backside|Rear|Behind|Aft|Stern|Bow|Port|Starboard|Fore|Aft|Midship|Amidship|Amidships|Amidship|Amidships)\b/i)) { + cleaned = rest; + } + } + } + return cleaned.trim(); + }); + randomEvents = randomEvents.slice(0, 6); + + // Generate context-aware fallbacks if needed (only if we have some events already) + if (randomEvents.length > 0 && randomEvents.length < 6) { + const dynamicElement = coreConcepts.match(/Dynamic Element[:\s]+([^.]+)/i)?.[1]?.trim() || 'strange occurrences'; + const conflict = coreConcepts.match(/Central Conflict[:\s]+([^.]+)/i)?.[1]?.trim() || 'a mysterious threat'; + const fallbackEvents = [ + `The ${dynamicElement.toLowerCase()} causes unexpected changes in the environment.`, + `A sign of ${conflict.toLowerCase()} appears, requiring immediate attention.`, + `The dungeon shifts, revealing a previously hidden passage or danger.`, + `An NPC from the primary faction appears with urgent information.`, + `The power source fluctuates, creating temporary hazards or opportunities.`, + `Echoes of past events manifest, providing clues or complications.` + ]; + while (randomEvents.length < 6) { + randomEvents.push(fallbackEvents[randomEvents.length % fallbackEvents.length]); + } + } [[encounters, 6, 'encounters'], [npcs, 4, 'NPCs'], [treasure, 4, 'treasures'], [randomEvents, 6, 'random events']] .filter(([arr, expected]) => arr.length < expected && arr.length > 0) diff --git a/dungeonTemplate.js b/dungeonTemplate.js index 4ff8d85..73811e1 100644 --- a/dungeonTemplate.js +++ b/dungeonTemplate.js @@ -63,8 +63,8 @@ export function dungeonTemplate(data) { padding: 0; font-family: ${bodyFont}; color: #1a1a1a; - font-size: 0.75em; - line-height: 1.4em; + font-size: 0.7em; + line-height: 1.35em; } .content-page { height: 100vh; @@ -91,7 +91,7 @@ export function dungeonTemplate(data) { font-family: ${quoteFont}; margin: 0.3em 0 0.6em; font-size: 0.85em; - line-height: 1.4em; + line-height: 1.35em; } .columns { display: grid; @@ -102,7 +102,7 @@ export function dungeonTemplate(data) { .col { display: flex; flex-direction: column; - gap: 0.3em; + gap: 0.25em; overflow-wrap: break-word; word-break: normal; hyphens: auto; @@ -110,7 +110,7 @@ export function dungeonTemplate(data) { .section-block { break-inside: avoid; page-break-inside: avoid; - margin-bottom: 0.4em; + margin-bottom: 0.3em; } h2 { font-family: ${headingFont}; @@ -129,29 +129,29 @@ export function dungeonTemplate(data) { page-break-inside: avoid; } .room h3 { - margin: 0.15em 0 0.08em; + margin: 0.1em 0 0.05em; font-size: 0.95em; font-weight: bold; color: #1a1a1a; } .room p { - margin: 0 0 0.35em; - font-size: 0.9em; + margin: 0 0 0.25em; + font-size: 0.85em; font-weight: normal; - line-height: 1.35em; + line-height: 1.3em; } .encounter, .npc, .treasure, .plot-resolution { margin: 0 0 0.35em; break-inside: avoid; page-break-inside: avoid; - font-size: 0.9em; - line-height: 1.35em; + font-size: 0.85em; + line-height: 1.3em; } .random-events { margin: 0.2em 0; break-inside: avoid; page-break-inside: avoid; - font-size: 0.9em; + font-size: 0.85em; } .random-events table { margin-top: 0.15em; @@ -228,8 +228,8 @@ export function dungeonTemplate(data) { } li { margin: 0.08em 0; - font-size: 0.9em; - line-height: 1.35em; + font-size: 0.85em; + line-height: 1.3em; } @@ -268,12 +268,30 @@ export function dungeonTemplate(data) { ${data.rooms && data.rooms.length > 0 ? `
${escapeHtml(room.description)}
+${escapeHtml(desc)}