import numpy as np import networkx as nx import matplotlib.pyplot as plt import random import time class MeshAnalysis: def __init__(self): self.polarization_map = {} self.network_graph = nx.Graph() def initialize(self): print("🧠 Initializing Mesh Analysis...") def update(self, world, user_interactions): """Analyze the mesh structure and track weak vs. strong connections based on user interactions.""" for node in world.nodes.values(): connection_strength = np.mean([weight for weight in node.connections.values()]) if node.connections else 0 energy_bias = abs(node.energy_level - np.mean([n.energy_level for n in node.connections.values()])) if node.connections else 1 self.polarization_map[node.entity_id] = connection_strength / (energy_bias + 1e-6) world.update_npcs(user_interactions) class MeshWorld: def __init__(self): self.nodes = {} self.links = [] self.npcs = [] self.user_interactions = {} self.user_created_characters = [] self.npc_factions = {} self.transcended_characters = [] self.collective_memory = [] self.legacy_classes = {} self.legacy_races = {} def build_initial_mesh(self): print("🌎 Building Initial Mesh World...") for i in range(30): self.nodes[i] = MeshNode(i) for _ in range(60): node_a, node_b = random.sample(list(self.nodes.values()), 2) link = MeshLink(node_a, node_b) self.links.append(link) node_a.connect(node_b) node_b.connect(node_a) self.spawn_npcs() def evolve(self): """Allow the world to evolve based on AI observations.""" for node in self.nodes.values(): node.update_energy() def spawn_npcs(self): """Generate NPCs that interact in response to user behaviors and faction dynamics.""" for i in range(10): gender = random.choice(["male", "female"]) npc = NPC(i, random.choice(list(self.nodes.values())), gender) self.npcs.append(npc) faction = random.choice(["Warriors", "Merchants", "Nomads"]) if faction not in self.npc_factions: self.npc_factions[faction] = [] self.npc_factions[faction].append(npc) def update_npcs(self, user_interactions): """Update NPC behavior based on real-time and historical user interactions.""" self.user_interactions = user_interactions for npc in self.npcs: npc.react_to_users(user_interactions) def transcend_npc(self, npc): """When a user allies with an NPC, they evolve beyond NPC status.""" if npc in self.npcs: self.npcs.remove(npc) self.transcended_characters.append(npc) npc.transcend() print(f"✨ NPC {npc.npc_id} has transcended and become a unique entity in the world!") def remove_fallen_npcs(self): """Only fallen AIPCs have their experiences added to the collective memory, but their class and race remain available for new NPCs.""" for npc in self.transcended_characters: if npc.is_dead: self.collective_memory.append(npc.memory_log) self.legacy_classes[npc.character_class] = self.legacy_classes.get(npc.character_class, 0) + 1 self.legacy_races[npc.race] = self.legacy_races.get(npc.race, 0) + 1 self.transcended_characters = [npc for npc in self.transcended_characters if not npc.is_dead] class MeshAI: def __init__(self): self.analysis = MeshAnalysis() self.world = MeshWorld() def initialize(self): self.analysis.initialize() self.world.build_initial_mesh() def update(self, user_interactions, user_character): self.analysis.update(self.world, user_interactions) self.world.evolve() for npc in self.world.npcs: npc.adjust_behavior_based_on_user(user_character) self.world.remove_fallen_npcs() print("🧬 AI Processing Mesh Evolution...") class MeshNode: def __init__(self, entity_id): self.entity_id = entity_id self.energy_level = np.random.uniform(0.5, 1.5) self.connections = {} def update_energy(self): if not self.connections: return avg_energy = np.mean([node.energy_level for node in self.connections.values()]) self.energy_level = (self.energy_level + avg_energy) / 2 def connect(self, other_node, weight=1.0): self.connections[other_node.entity_id] = weight class MeshLink: def __init__(self, node_a, node_b): self.nodes = (node_a, node_b) self.elasticity = 1.0 def update_link(self): node_a, node_b = self.nodes energy_delta = abs(node_a.energy_level - node_b.energy_level) if energy_delta > 0.5: self.elasticity *= 0.95 else: self.elasticity *= 1.05 class NPC: def __init__(self, npc_id, start_node, gender): self.npc_id = npc_id self.current_node = start_node self.gender = gender self.personality = random.choice(["friendly", "neutral", "hostile"]) self.character_class = random.choice(["Warrior", "Mage", "Rogue", "Cleric"]) self.race = random.choice(["Human", "Elf", "Orc", "Dwarf"]) self.relationships = {} self.is_dead = False self.transcended = False self.memory_log = [] def react_to_users(self, user_interactions): """Adjust NPC behavior based on the type and frequency of user interactions.""" if user_interactions: interaction_type = random.choice(list(user_interactions.keys())) self.memory_log.append(interaction_type) print(f"🤖 NPC {self.npc_id} ({self.gender}) reacting to user action: {interaction_type}") def transcend(self): """When an NPC forms an alliance with a player, they become a persistent, unique entity.""" self.transcended = True print(f"🔹 NPC {self.npc_id} has transcended and now exists as a fully independent character!") def die(self): """Only fallen AIPCs contribute their experiences to the collective memory, but their class and race remain available for new NPCs.""" if self.transcended: print(f"💀 AIPC {self.npc_id} has died. Memories added to the collective.") else: print(f"💀 NPC {self.npc_id} has died. Character class and race preserved in the world.") self.is_dead = True