File size: 5,677 Bytes
fdcbf65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from game.dungeon import Dungeon
from game.player import Player
from game.npc import NPC
from game.combat import combat, adjust_difficulty
from game.items import Item, use_item
from assets.ascii_art import display_dungeon
from helper import (
    get_game_state,
    run_action,
    start_game,
    is_safe,
    detect_inventory_changes,
    update_inventory,
)

import pdb
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def main_loop(message, history, game_state):
    """Main game loop that processes commands and returns responses"""

    try:
        print(f"\nProcessing command: {message}")  # Debug print

        # Get AI response
        output = run_action(message, history, game_state)
        logger.info(f"Generated response: {output}")
        print(f"\nGenerated output: {output}")  # Debug print

        # Safety check
        safe = is_safe(output)
        print(f"\nSafety Check Result: {'SAFE' if safe else 'UNSAFE'}")
        logger.info(f"Safety check result: {'SAFE' if safe else 'UNSAFE'}")

        if not safe:
            logging.warning("Unsafe output detected")
            logger.warning("Unsafe content detected - blocking response")
            print("Unsafe content detected - Response blocked")
            return "This response was blocked for safety reasons.", history

        # Update history with safe response
        if not history:
            history = []
        history.append((message, output))

        return output, history

    except Exception as e:
        logger.error(f"Error in main_loop: {str(e)}")
        return "Error processing command", history


def main():
    """Initialize game and start interface"""
    try:
        logger.info("Starting main function")
        print("\nInitializing game...")
        game_state = get_game_state(
            inventory={
                "cloth pants": 1,
                "cloth shirt": 1,
                "goggles": 1,
                "leather bound journal": 1,
                "gold": 5,
            }
        )
        logger.debug(f"Initialized game state: {game_state}")

        # Create and add game objects
        dungeon = Dungeon(10, 10)
        player = Player("Hero")

        # Update game state with objects
        game_state["dungeon"] = dungeon
        game_state["player"] = player

        # logger.info(f"Game state in main(): {game_state}")

        # Start game interface
        print("Starting game interface...")
        start_game(main_loop, game_state, True)

    except Exception as e:
        logger.error(f"Error in main: {str(e)}")
        raise


if __name__ == "__main__":
    main()


# def main_loop(message, history):
#     logging.info(f"main_loop called with message: {message}")

#     # Initialize history if None
#     history = history or []

#     # Get AI response
#     output = run_action(message, history, game_state)

#     # Safety check
#     safe = is_safe(output)
#     if not safe:
#         logging.error("Unsafe output detected")
#         return "Invalid Output"

#     # Format the output nicely
#     output_lines = [output]

#     # Handle movement and exploration
#     if message.lower().startswith(("go", "move", "walk")):
#         direction = message.split()[1]
#         game_state["player"].move(direction, game_state["dungeon"])
#         room_desc = game_state["dungeon"].get_room_description(
#             game_state["dungeon"].current_room, game_state
#         )
#         output_lines.append(f"\n{room_desc}")

#     # Handle NPC interactions
#     elif message.lower().startswith("talk"):
#         npc_name = message.split()[2]
#         for npc in game_state["dungeon"].npcs:
#             if npc.name.lower() == npc_name.lower():
#                 dialogue = game_state["player"].interact(npc, game_state)
#                 output_lines += f"\n{dialogue}"

#     # Handle item interactions and inventory
#     elif message.lower().startswith(("take", "pick up")):
#         item_name = " ".join(message.split()[1:])
#         for item in game_state["dungeon"].items:
#             if item.name.lower() == item_name.lower():
#                 game_state["player"].inventory.append(item)
#                 game_state["dungeon"].items.remove(item)
#                 output += f"\nYou picked up {item.name}"
#                 item_desc = game_state["player"].examine(item, game_state)
#                 output_lines += f"\n{item_desc}"

#     # Format final output
#     final_output = "\n".join(output_lines)
#     history.append((message, final_output))
#     logging.info(f"main_loop output: {final_output}")

#     return final_output, history


# def main():
#     logging.info("Starting main function")

#     try:
#         # Initialize game state with error handling
#         global game_state
#         game_state = get_game_state(
#             inventory={
#                 "cloth pants": 1,
#                 "cloth shirt": 1,
#                 "goggles": 1,
#                 "leather bound journal": 1,
#                 "gold": 5,
#             }
#         )

#         # Verify game state initialization
#         if not game_state:
#             raise ValueError("Failed to initialize game state")

#         # Create dungeon and populate with AI-generated content
#         dungeon = Dungeon(10, 10)
#         game_state["dungeon"] = dungeon

#         # Create player and add to game state
#         player = Player("Hero")
#         game_state["player"] = player

#         # Start game interface
#         start_game(main_loop, True)

#     except Exception as e:
#         logging.error(f"Error in main: {str(e)}")
#         raise