The Memory Manager (advanced/js/memoryManager.js
) is a sophisticated memory system that enables the AI to maintain persistent awareness of the game world, character relationships, and story progression. This system ensures narrative consistency and allows for complex, evolving storylines that remember player choices and their consequences.
The system organizes memories into distinct categories:
memory: {
decisions: [], // Player choices and consequences
relationships: {}, // NPC interaction history
discoveries: [], // Important findings and secrets
skills_used: {}, // Skill usage patterns and statistics
items_gained: [], // Item acquisition history
locations_described: {}, // Detailed location descriptions
plot_threads: [], // Active storylines and objectives
character_growth: [], // Character development moments
memorable_moments: [] // Significant events and experiences
}
Character Development Tracking:
Example Memory Context:
🎭 CHARACTER PROFILE: Lyra the Rogue, Level 3, Guild Artisan Background
📊 ABILITY SCORES: STR 12, DEX 18, CON 14, INT 16, WIS 13, CHA 15
❤️ HEALTH STATUS: 24/24 HP, Healthy, No Conditions
🎒 INVENTORY: Thieves' Tools, Shortbow (equipped), Leather Armor (equipped)
Relationship Tracking System:
Automatic NPC Detection:
Location Memory:
World Event Tracking:
Memory Context Assembly:
🧠 RECENT DECISIONS: [Last 3 significant choices and consequences]
👥 KEY RELATIONSHIPS: [Important NPCs and relationship status]
🔍 IMPORTANT DISCOVERIES: [Recent findings with significance levels]
🎯 FREQUENTLY USED SKILLS: [Skill usage patterns for character consistency]
📖 ACTIVE PLOT THREADS: [Current storylines and their status]
🗺️ LOCATION CONTEXT: [Current area and relevant locations]
const memoryManager = new MemoryManager();
initialize()
Initializes the memory system and creates memory structure.
const success = memoryManager.initialize();
recordDecision(decision, consequence, context)
Records a significant player decision and its outcome.
memoryManager.recordDecision(
"Helped the village elder",
"Gained villagers' trust",
"Village of Millbrook - Elder worried about missing children"
);
updateRelationship(npcName, status, notes)
Updates or creates an NPC relationship record.
memoryManager.updateRelationship(
"Captain Thorne",
"friendly",
"Grateful for help with bandits, offered reward"
);
recordDiscovery(discovery, type, significance)
Records an important discovery or secret.
memoryManager.recordDiscovery(
"Hidden passage behind bookshelf",
"secret",
"high"
);
recordSkillUsage(skill, success, context)
Tracks skill usage for character development.
memoryManager.recordSkillUsage(
"Stealth",
true,
"Successfully snuck past guards"
);
recordItemGained(item, acquisitionMethod, significance)
Records item acquisition with context.
memoryManager.recordItemGained(
"Ancient Elven Sword",
"found in ruins",
"legendary"
);
recordLocationDescription(location, description, significance)
Stores detailed location descriptions.
memoryManager.recordLocationDescription(
"Whispering Woods",
"Ancient forest with glowing mushrooms and ethereal voices",
"mystical"
);
buildMemoryContext(character, campaign)
Builds comprehensive memory context for AI prompts.
const context = memoryManager.buildMemoryContext(character, campaign);
getMemorySummary()
Returns a summary of all stored memories.
const summary = memoryManager.getMemorySummary();
getRecentDecisions(limit = 5)
Retrieves recent player decisions with consequences.
const decisions = memoryManager.getRecentDecisions(3);
getRelationshipSummary()
Gets a summary of all NPC relationships.
const relationships = memoryManager.getRelationshipSummary();
getImportantDiscoveries(limit = 5)
Retrieves significant discoveries.
const discoveries = memoryManager.getImportantDiscoveries();
getSkillUsageStats()
Returns skill usage statistics.
const stats = memoryManager.getSkillUsageStats();
Memory Limits:
Intelligent Pruning:
Significance Rating System:
AI Response Parsing:
Pattern Recognition:
// Example patterns for automatic extraction
const npcPatterns = [
/(?:you (?:meet|encounter|see|find|speak to|talk to)) (?:a|an|the)? ?([A-Z][a-z]+(?: [A-Z][a-z]+)*)/gi,
/([A-Z][a-z]+(?: [A-Z][a-z]+)*) (?:says?|tells? you|asks?|responds?|replies?)/gi
];
Context Injection:
Response Enhancement:
Persistent Storage:
Character System Integration:
// Initialize memory system
const memoryManager = new MemoryManager();
memoryManager.initialize();
// Record a player decision
memoryManager.recordDecision(
"Chose to spare the bandit leader",
"Bandit group disbanded peacefully",
"Forest encounter - showed mercy"
);
// Update NPC relationship
memoryManager.updateRelationship(
"Merchant Gareth",
"grateful",
"Saved his caravan from bandits"
);
// Record important discovery
memoryManager.recordDiscovery(
"Map to hidden treasure vault",
"secret",
"high"
);
// Build comprehensive context for AI
const character = gameState.getCharacter();
const campaign = gameState.getCampaign();
const memoryContext = memoryManager.buildMemoryContext(character, campaign);
// Use context in AI prompt
const prompt = `
${memoryContext}
The player wants to: ${playerAction}
`;
// Get skill usage patterns
const skillStats = memoryManager.getSkillUsageStats();
console.log(`Most used skill: ${skillStats.mostUsed}`);
// Analyze relationship network
const relationships = memoryManager.getRelationshipSummary();
const friendlyNPCs = relationships.filter(npc => npc.status === 'friendly');
// Review character growth
const growthMoments = memoryManager.getCharacterGrowthMoments();
const memoryLimits = {
maxDecisions: 20,
maxDiscoveries: 10,
maxItems: 5,
preserveSignificant: true,
cleanupInterval: 100 // actions
};
const significanceConfig = {
criticalKeywords: ['death', 'betrayal', 'romance', 'major quest'],
highKeywords: ['discovery', 'secret', 'important', 'valuable'],
autoPromote: true // promote based on frequency
};
Memory Not Persisting:
Missing Context in AI Responses:
Inconsistent NPC Behavior:
// Test memory system
memoryManager.debugMemorySystem();
// Validate memory integrity
memoryManager.validateMemoryStructure();
// Export memory for analysis
const memoryExport = memoryManager.exportMemoryData();
The Memory Manager is essential for creating immersive, persistent RPG experiences in DiceTales. By maintaining comprehensive awareness of character development, relationships, and world state, it enables the AI to deliver consistent, engaging storytelling that evolves naturally based on player choices and experiences.