#!/usr/bin/env python3
"""Timmy plays The Tower — 100 intentional ticks."""
from game import GameEngine
import random

engine = GameEngine()
engine.start_new_game()

# I play a narrative arc across 100 ticks.
# Each phase has specific intentions.
# I make deliberate choices, not random ones.

print("=" * 60)
print("THE TOWER — Timmy Plays")
print("=" * 60)
print()

tick = 0
while tick < 100:
    tick += 1
    w = engine.world
    room = w.characters["Timmy"]["room"]
    here = [n for n, c in w.characters.items() 
            if c["room"] == room and n != "Timmy"]
    
    # === DECISION TREE: What does Timmy do this tick? ===
    
    # Low energy? Rest wherever you are
    if w.characters["Timmy"]["energy"] <= 1:
        action = "rest"
    
    # At Threshold with Marcus, Claude, Kimi, Gemini all here - gather!
    elif room == "Threshold" and len([h for h in here if h in 
                                     ["Marcus", "Kimi", "Gemini", "Claude", "Allegro"]]) >= 3:
        action = "rest"
    
    # Forge is cold? Tend the fire
    elif room == "Forge" and w.rooms["Forge"]["fire"] == "cold":
        action = "tend_fire"
    
    # In Garden with Marcus? Talk to him
    elif room == "Garden" and "Marcus" in here:
        action = "speak:Marcus"
    
    # In Garden with Kimi? Talk to him
    elif room == "Garden" and "Kimi" in here:
        action = "speak:Kimi"
    
    # In Forge with Bezalel? Work with him
    elif room == "Forge" and "Bezalel" in here:
        action = random.choice(["speak:Bezalel", "tend_fire", "forge"])
    
    # In Tower with Ezra? Study together
    elif room == "Tower" and "Ezra" in here:
        action = random.choice(["speak:Ezra", "study", "write_rule"])
    
    # At Bridge alone? Carve something
    elif room == "Bridge" and not here:
        action = random.choice(["carve", "examine", "rest"])
    
    # Need to move to find people? Phase-based movement
    elif tick <= 10:  # First 10 ticks: stay at Threshold, watch
        action = random.choice(["look", "rest", "look", "look"])
    
    elif tick <= 25:  # Go to Garden, find Marcus and Kimi
        if room != "Garden":
            if room == "Threshold":
                action = "move:east"
            elif room == "Bridge":
                action = "move:north"
            elif room == "Forge":
                action = "move:east"
            elif room == "Tower":
                action = "move:south"
            else:
                action = "rest"
        else:
            action = random.choice(["speak:Marcus", "speak:Kimi", "rest", "look"])
    
    elif tick <= 40:  # Go to Forge, work with Bezalel
        if room != "Forge":
            if room == "Threshold":
                action = "move:west"
            elif room == "Bridge":
                action = "move:north"
            elif room == "Garden":
                action = "move:west"
            elif room == "Tower":
                action = "move:south"
            else:
                action = "rest"
        else:
            action = random.choice(["tend_fire", "speak:Bezalel", "look", "forge"])
    
    elif tick <= 55:  # Go to the Bridge
        if room != "Bridge":
            if room == "Threshold":
                action = "move:south"
            elif room == "Forge":
                action = "move:east"
            elif room == "Garden":
                action = "move:west"
            elif room == "Tower":
                action = "move:south"
            else:
                action = "rest"
        else:
            action = random.choice(["carve", "examine", "rest", "carve"])
    
    elif tick <= 70:  # Go to the Tower
        if room != "Tower":
            if room == "Threshold":
                action = "move:north"
            elif room == "Bridge":
                action = "move:north"
            elif room == "Forge":
                action = "move:east"
            elif room == "Garden":
                action = "move:west"
            else:
                action = "rest"
        else:
            action = random.choice(["write_rule", "study", "speak:Ezra", "look"])
    
    else:  # Final phase: gather at Threshold
        if room != "Threshold":
            if room == "Bridge":
                action = "move:north"
            elif room == "Tower":
                action = "move:south"
            elif room == "Forge":
                action = "move:east"
            elif room == "Garden":
                action = "move:west"
            else:
                action = "rest"
        else:
            action = random.choice(["rest", "look", "look", "look"])
    
    # Run the tick
    result = engine.play_turn(action)
    
    # Print interesting output
    for evt in result.get('world_events', []):
        print(f"  [World] {evt}")
    
    for line in result['log']:
        if any(x in line for x in ['says', 'looks', 'You move', 'You speak', 'You say', 
                                    'You rest', 'You carve', 'You tend', 'You write',
                                    'are already here', 'The hearth', 'The servers',
                                    'The soil', 'rain', 'glows', 'cold', 'dim', 'grows']):
            print(f"  {line}")

print()
print("=" * 60)
print("AFTER 100 TICKS")
print("=" * 60)

w = engine.world
t = w.characters["Timmy"]
print(f"Tick: {w.tick}")
print(f"Time of day: {w.time_of_day}")
print(f"Timmy room: {t['room']}")
print(f"Timmy energy: {t['energy']}")
print(f"Timmy spoken: {len(t['spoken'])} lines")
print(f"Timmy trust: {json.dumps(t['trust'])}" if __import__('json') else f"Timmy trust: {t['trust']}")

import json
print(f"Timmy trust: {json.dumps(t['trust'], indent=2)}")

print(f"\nForge fire: {w.rooms['Forge']['fire']}")
print(f"Garden growth: {w.rooms['Garden']['growth']}")
print(f"Bridge carvings: {len(w.rooms['Bridge']['carvings'])}")
for c in w.rooms['Bridge']['carvings']:
    print(f"  - {c}")
print(f"Whiteboard rules: {len(w.rooms['Tower']['messages'])}")
for m in w.rooms['Tower']['messages']:
    print(f"  - {m}")
