using UnityEngine; using System.Collections.Generic; using System.Linq; /// /// Tracks which rooms have been visited by agents of a specific character type /// Shared among all agents of the same type to create a collective memory /// public class AIRoomMemory { private string characterType; private HashSet visitedRoomIds = new(); private Dictionary roomLastVisitTime = new(); public AIRoomMemory(string characterType) { this.characterType = characterType; } /// /// Marks a room as visited /// public void VisitRoom(int roomId) { if (!visitedRoomIds.Contains(roomId)) { visitedRoomIds.Add(roomId); } roomLastVisitTime[roomId] = Time.time; } /// /// Checks if a room has been visited /// public bool HasVisited(int roomId) { return visitedRoomIds.Contains(roomId); } /// /// Checks if a room has NOT been visited (convenience method) /// public bool IsRoomUnvisited(int roomId) { return !visitedRoomIds.Contains(roomId); } /// /// Gets all visited room IDs /// public HashSet GetVisitedRooms() { return new HashSet(visitedRoomIds); } /// /// Gets unvisited rooms from a list of options /// public List FilterUnvisitedRooms(List rooms) { return rooms.Where(r => !HasVisited(r.Id)).ToList(); } /// /// Gets the character type this memory is for /// public string CharacterType => characterType; /// /// Gets the number of visited rooms /// public int VisitedCount => visitedRoomIds.Count; } /// /// Global manager for all agent room memories /// Allows different character types to have separate memory systems /// public class AIRoomMemoryManager : MonoBehaviour { private static AIRoomMemoryManager instance; private Dictionary memoryByCharacterType = new(); void Awake() { if (instance == null) { instance = this; DontDestroyOnLoad(gameObject); } else if (instance != this) { Destroy(gameObject); } } /// /// Gets or creates a room memory for a character type /// public static AIRoomMemory GetMemory(string characterType) { if (instance == null) { GameObject go = new GameObject("AIRoomMemoryManager"); instance = go.AddComponent(); } if (!instance.memoryByCharacterType.ContainsKey(characterType)) { instance.memoryByCharacterType[characterType] = new AIRoomMemory(characterType); } return instance.memoryByCharacterType[characterType]; } /// /// Clears all memory for a character type (useful for new maze) /// public static void ClearMemory(string characterType) { if (instance != null && instance.memoryByCharacterType.ContainsKey(characterType)) { instance.memoryByCharacterType.Remove(characterType); } } /// /// Clears all memories /// public static void ClearAllMemories() { if (instance != null) { instance.memoryByCharacterType.Clear(); } } }