Files
omnia-langchain/game_loop.py

112 lines
3.3 KiB
Python

import logging
from entities import Player
from interaction import ask_entity
from time_utils import WorldClock
from world_architect import WorldState
logger = logging.getLogger(__name__)
def _build_name_lookup(entities):
name_lookup = {}
for entity_key, entity in entities.items():
name_lookup[entity_key.lower()] = entity_key
name_lookup[entity.name.lower()] = entity_key
return name_lookup
def start_game(
entities,
player_id=None,
world_time=None,
location="Unknown",
world_state=None,
):
player = None
if player_id:
player = entities.get(player_id)
if player is None:
raise ValueError(f"Player entity '{player_id}' not found in scenario.")
else:
player = Player(
name="Player",
traits=["Curious"],
stats={},
voice_sample="Voice: 'Direct and concise.'",
entity_id="player",
)
available_entities = {
entity_id: entity
for entity_id, entity in entities.items()
if entity_id != player_id
}
world_clock = WorldClock.from_time_str(world_time)
# Initialize world state if not provided
if world_state is None:
world_state = WorldState()
world_state.world_clock = world_clock
for entity_id, entity in entities.items():
world_state.entities[entity_id] = {
"name": entity.name,
"location": location,
"health": 100,
"status": "calm",
"mood": entity.current_mood,
}
world_state.locations[location.lower().replace(" ", "_")] = {
"name": location,
"description": f"The {location}",
"occupants": len(entities),
"visibility": "clear",
}
current_entity = None
name_lookup = _build_name_lookup(available_entities)
entity_names = "/".join(
[entity.name for entity in available_entities.values()] + ["Exit"]
)
logger.info("--- WORLD INITIALIZED ---")
logger.info("World initialized with %s active entities.", len(available_entities))
logger.info("Current location: %s", location)
logger.info(
"World time: %s (%s)", world_clock.get_time_str(), world_clock.get_vibe()
)
while True:
target_name = (
input(f"\nWho do you want to talk to? ({entity_names}): ").lower().strip()
)
if target_name in ["exit", "quit"]:
if current_entity:
current_entity.reflect_and_summarize(world_clock, location, world_state)
break
target_key = name_lookup.get(target_name)
if target_key is None:
logger.warning("Target not found.")
continue
new_entity = available_entities[target_key]
if current_entity and current_entity != new_entity:
logger.info(
"You leave %s and approach %s.", current_entity.name, new_entity.name
)
current_entity.reflect_and_summarize(world_clock, location, world_state)
current_entity = new_entity
user_msg = input(f"You to {current_entity.name}: ")
ask_entity(
current_entity,
player,
user_msg,
world_clock,
location,
world_state=world_state,
)