diff --git "a/index.html" "b/index.html" --- "a/index.html" +++ "b/index.html" @@ -1,1337 +1,349 @@ - - - - - - Gradio Choose Your Own Adventure - - - - - -
-

CHOOSE YOUR OWN ADVENTURE

-
-

An interactive text adventure game powered by Gradio-lite

-

Can you defeat the Evil Power Master and save the land?

-
- - - -import gradio as gr -import random -import json -# Make sure to import everything needed from the other files -from game_data import game_data, illustrations, enemies_data, items_data -from game_engine import GameState, create_svg_illustration - -def initialize_game(): - """Initialize a new game with default state""" - game_state = GameState() - current_page = 1 - page_data = game_data[str(current_page)] - - # Create the SVG illustration - svg_content = create_svg_illustration(page_data.get("illustration", "default")) - - # Build page content - content = f"

{page_data.get('title', 'Untitled Page')}

" - content += page_data.get("content", "

No content for this page.

") - - # Build options - options = [] - if "options" in page_data: - for opt in page_data["options"]: - options.append(opt["text"]) - if not options: # Fallback if no options defined - options = ["Restart"] - content += "

There are no actions defined here.

" - - - # Update game statistics display - stats = generate_stats_display(game_state) # Use the function here - - # Initialize inventory display - inventory = generate_inventory_display(game_state) # Use the function here - - # Set story path - story_path = "You are at the beginning of your adventure." - - return svg_content, content, gr.Dropdown(choices=options, label="What will you do?", value=None), game_state.to_json(), stats, inventory, story_path - def update_game(choice, game_state_json): """Update game based on player choice""" - if not choice: # Handle empty choice if dropdown value is cleared - return gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update() - - # Parse game state from JSON - game_state_dict = json.loads(game_state_json) - game_state = GameState.from_dict(game_state_dict) - - # Get current page data - current_page = game_state.current_page - # Ensure current_page is always a string for dictionary keys - current_page_str = str(current_page) - if current_page_str not in game_data: - # Handle invalid page number - maybe reset? - error_content = f"

Error: Reached invalid page number {current_page}. Resetting game.

" - svg_error = create_svg_illustration("default") - return svg_error, error_content, gr.Dropdown(choices=["Restart"], label="Error"), GameState().to_json(), generate_stats_display(GameState()), generate_inventory_display(GameState()), "Error - Resetting" - - page_data = game_data[current_page_str] - - # Find the selected option - selected_option = None - if "options" in page_data: - for i, opt in enumerate(page_data["options"]): - if opt["text"] == choice: + try: # Wrap major logic in try-except + if not choice: # Handle empty choice if dropdown value is cleared + # Return updates that don't change anything + return gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update() + + # Parse game state from JSON + game_state_dict = json.loads(game_state_json) + game_state = GameState.from_dict(game_state_dict) + + # Get current page data + current_page = game_state.current_page + current_page_str = str(current_page) + + if current_page_str not in game_data: + # Handle invalid page number + error_content = f"

Error: Reached invalid page number {current_page}. Resetting game.

" + svg_error = create_svg_illustration("default") + new_game_state = GameState() + return (svg_error, error_content, gr.Dropdown(choices=["Restart"], label="Error", value=None), new_game_state.to_json(), + generate_stats_display(new_game_state), generate_inventory_display(new_game_state), "Error - Resetting") + + page_data = game_data[current_page_str] + + # Find the selected option + selected_option = None + current_options_list = page_data.get("options", []) + for opt in current_options_list: + # Ensure 'text' key exists in option dictionary + if isinstance(opt, dict) and opt.get("text") == choice: selected_option = opt break - # Also check alternative option if it was displayed and chosen - alt_opt_data = page_data.get("alternativeOption") - if not selected_option and alt_opt_data and alt_opt_data["text"] == choice: - selected_option = alt_opt_data - - - if selected_option is None: - # Handle case where choice might be "Restart" or invalid - if choice == "Restart": - return initialize_game() - else: - # Stay on the same page if the choice wasn't found - options = [opt["text"] for opt in page_data.get("options", [])] - if not options: options = ["Restart"] + # Check alternative option if it was displayed and chosen + alt_opt_data = page_data.get("alternativeOption") + if not selected_option and isinstance(alt_opt_data, dict) and alt_opt_data.get("text") == choice: + # Check if the alternative option should have been available + # This logic needs refinement: check if primary options were filtered out *and* alt condition met + alt_show_if = alt_opt_data.get("showIf") + current_inventory = game_state.inventory + primary_options_available = False + if current_options_list: + for opt in current_options_list: + is_available = True + req_item = opt.get("requireItem") + if req_item and req_item not in current_inventory: + is_available = False + req_any = opt.get("requireAnyItem") + if is_available and req_any and not any(i in current_inventory for i in req_any): + is_available = False + if is_available: + primary_options_available = True + break # Found at least one available primary option + + # Select alternative only if no primary options were available AND showIf condition is met + if not primary_options_available and (not alt_show_if or any(item in current_inventory for item in alt_show_if)): + selected_option = alt_opt_data + + + if selected_option is None: + # Handle case where choice might be "Restart" or invalid + if choice == "Restart": + return initialize_game() + else: + # Stay on the same page if the choice wasn't found + options = [opt.get("text", "Invalid Option") for opt in current_options_list] + if alt_opt_data and alt_opt_data.get("text"): # Add alternative option text if it exists + # Check showIf condition for alternative option display + alt_show_if = alt_opt_data.get("showIf") + current_inventory = game_state.inventory + if not alt_show_if or any(item in current_inventory for item in alt_show_if): + # Check if primary options were filtered out + primary_options_available = False + if current_options_list: + # Re-check primary options availability + for opt in current_options_list: + is_available = True + req_item = opt.get("requireItem") + if req_item and req_item not in current_inventory: is_available = False + req_any = opt.get("requireAnyItem") + if is_available and req_any and not any(i in current_inventory for i in req_any): is_available = False + if is_available: + primary_options_available = True + break + if not primary_options_available: + options.append(alt_opt_data["text"]) + + if not options: options = ["Restart"] + + svg_content = create_svg_illustration(page_data.get("illustration", "default")) + content = f"

{page_data.get('title', 'Error')}

" + page_data.get("content", "") + f"

Debug: Choice '{choice}' not matched to any available option.

" + stats_display = generate_stats_display(game_state) + inventory_display = generate_inventory_display(game_state) + story_path_display = f"You remain on page {current_page}: {page_data.get('title', 'Error')}" + return svg_content, content, gr.Dropdown(choices=options, value=None), game_state.to_json(), stats_display, inventory_display, story_path_display + + # --- Option requirement checks --- + item_required = selected_option.get("requireItem") + any_item_required = selected_option.get("requireAnyItem") + current_inventory = game_state.inventory # Cache inventory + + # Check specific item requirement + if item_required and item_required not in current_inventory: + content = f"

{page_data['title']}

" + page_data["content"] + content += f"

You need the {item_required} for this option, but you don't have it.

" + options_texts = [opt.get("text") for opt in page_data.get("options", []) if opt.get("text")] + # Check if alt option should be added back + if alt_opt_data and alt_opt_data.get("text"): + alt_show_if = alt_opt_data.get("showIf") + if not alt_show_if or any(item in current_inventory for item in alt_show_if): + primary_available_check = any( + opt.get("text") and (not opt.get("requireItem") or opt.get("requireItem") in current_inventory) and \ + (not opt.get("requireAnyItem") or any(i in current_inventory for i in opt.get("requireAnyItem"))) + for opt in page_data.get("options", []) + ) + if not primary_available_check: options_texts.append(alt_opt_data["text"]) + + if not options_texts: options_texts = ["Restart"] svg_content = create_svg_illustration(page_data.get("illustration", "default")) - content = f"

{page_data.get('title', 'Error')}

" + page_data.get("content", "") + "

Invalid choice selected. Please try again.

" stats_display = generate_stats_display(game_state) inventory_display = generate_inventory_display(game_state) - story_path_display = f"You remain on page {current_page}: {page_data.get('title', 'Error')}" - return svg_content, content, gr.Dropdown(choices=options, value=None), game_state.to_json(), stats_display, inventory_display, story_path_display - - # Check if this option requires an item - item_required = selected_option.get("requireItem") - if item_required and item_required not in game_state.inventory: - content = f"

{page_data['title']}

" + page_data["content"] # Show original content - content += f"

You need the {item_required} for this option, but you don't have it.

" - options = [opt["text"] for opt in page_data["options"]] # Keep current options - svg_content = create_svg_illustration(page_data["illustration"]) - stats_display = generate_stats_display(game_state) - inventory_display = generate_inventory_display(game_state) - story_path_display = f"You remain on page {current_page}: {page_data['title']}" - return svg_content, content, gr.Dropdown(choices=options, value=None), game_state.to_json(), stats_display, inventory_display, story_path_display + story_path_display = f"You remain on page {current_page}: {page_data['title']}" + return svg_content, content, gr.Dropdown(choices=list(set(options_texts)), value=None), game_state.to_json(), stats_display, inventory_display, story_path_display # Use set to remove duplicates - # Check if option requires any item from a list - any_item_required = selected_option.get("requireAnyItem") - if any_item_required: - has_required = any(item in game_state.inventory for item in any_item_required) - if not has_required: + # Check 'any item' requirement + if any_item_required and not any(item in current_inventory for item in any_item_required): item_list = ", ".join(f"'{item}'" for item in any_item_required) - content = f"

{page_data['title']}

" + page_data["content"] # Show original content + content = f"

{page_data['title']}

" + page_data["content"] content += f"

You need one of the following items for this option: {item_list}, but you don't have any.

" - options = [opt["text"] for opt in page_data["options"]] # Keep current options - svg_content = create_svg_illustration(page_data["illustration"]) + options_texts = [opt.get("text") for opt in page_data.get("options", []) if opt.get("text")] + # Check if alt option should be added back (similar logic as above) + if alt_opt_data and alt_opt_data.get("text"): + alt_show_if = alt_opt_data.get("showIf") + if not alt_show_if or any(item in current_inventory for item in alt_show_if): + primary_available_check = any( + opt.get("text") and (not opt.get("requireItem") or opt.get("requireItem") in current_inventory) and \ + (not opt.get("requireAnyItem") or any(i in current_inventory for i in opt.get("requireAnyItem"))) + for opt in page_data.get("options", []) + ) + if not primary_available_check: options_texts.append(alt_opt_data["text"]) + + if not options_texts: options_texts = ["Restart"] + svg_content = create_svg_illustration(page_data.get("illustration", "default")) stats_display = generate_stats_display(game_state) inventory_display = generate_inventory_display(game_state) story_path_display = f"You remain on page {current_page}: {page_data['title']}" - return svg_content, content, gr.Dropdown(choices=options, value=None), game_state.to_json(), stats_display, inventory_display, story_path_display - - # Process special items to collect - item_to_add = selected_option.get("addItem") - if item_to_add and item_to_add not in game_state.inventory: - game_state.inventory.append(item_to_add) - # Optionally remove item if it's consumable (needs item_data lookup) - item_data = items_data.get(item_to_add, {}) - if item_data.get("type") == "consumable" and item_data.get("useOnAdd"): # e.g. a potion - if "hpRestore" in item_data: - game_state.current_hp = min(game_state.max_hp, game_state.current_hp + item_data["hpRestore"]) - # Don't add consumable to inventory if used immediately? Or remove after use? Let's keep it simple and add it. - - # Move to the next page - next_page = selected_option["next"] - game_state.current_page = next_page - next_page_str = str(next_page) # Use string for keys + return svg_content, content, gr.Dropdown(choices=list(set(options_texts)), value=None), game_state.to_json(), stats_display, inventory_display, story_path_display - if next_page_str not in game_data: - # Handle invalid destination page - error_content = f"

Error: Option leads to invalid page number {next_page}. Resetting game.

" - svg_error = create_svg_illustration("default") - return svg_error, error_content, gr.Dropdown(choices=["Restart"], label="Error"), GameState().to_json(), generate_stats_display(GameState()), generate_inventory_display(GameState()), "Error - Resetting" - - if next_page not in game_state.visited_pages: # Avoid duplicates if revisiting - game_state.visited_pages.append(next_page) - - # Update journey progress based on page transitions - game_state.journey_progress += 5 # Increment progress with each decision - if game_state.journey_progress > 100: - game_state.journey_progress = 100 - - battle_occurred = False - battle_message = "" # Initialize battle message - battle_result = True # Assume survival unless battle happens and player loses - - # Check for random battle (20% chance if page has randomBattle flag) - # Check the *current* page data before moving - if page_data.get("randomBattle", False) and random.random() < 0.2: - battle_result, battle_log = simulate_battle(game_state) - battle_occurred = True - if not battle_result: - # Player died in battle - content = "

Game Over

You have been defeated in battle!

" + battle_log - stats_display = generate_stats_display(game_state) # Show final stats - inventory_display = generate_inventory_display(game_state) # Show final inventory - return create_svg_illustration("game-over"), content, gr.Dropdown(choices=["Restart"], label="Game Over", value=None), game_state.to_json(), stats_display, inventory_display, "Game Over - You were defeated in battle." - else: - # Player survived battle, add battle log to the next page content - battle_message = f"
Battle Won!{battle_log}
" - # Get new page data (for the destination page 'next_page') - page_data = game_data[next_page_str] + # --- Process valid choice --- + item_to_add = selected_option.get("addItem") + if item_to_add and item_to_add not in game_state.inventory: + game_state.inventory.append(item_to_add) + item_data = items_data.get(item_to_add, {}) + if item_data.get("type") == "consumable" and item_data.get("useOnAdd"): + if "hpRestore" in item_data: + hp_before = game_state.current_hp + game_state.current_hp = min(game_state.max_hp, game_state.current_hp + item_data["hpRestore"]) + # Maybe add feedback about using the item? Add later if needed. + game_state.inventory.remove(item_to_add) # Remove consumable after use - # Process page stat effects - stat_increase = page_data.get("statIncrease") - if stat_increase: - stat = stat_increase["stat"] - amount = stat_increase["amount"] - if stat in game_state.stats: - game_state.stats[stat] += amount - - # Process HP loss - hp_loss = page_data.get("hpLoss") - if hp_loss: - game_state.current_hp -= hp_loss - if game_state.current_hp <= 0: - game_state.current_hp = 0 - content = "

Game Over

You have died from your wounds!

" - stats_display = generate_stats_display(game_state) # Show final stats - inventory_display = generate_inventory_display(game_state) # Show final inventory - return create_svg_illustration("game-over"), content, gr.Dropdown(choices=["Restart"], label="Game Over", value=None), game_state.to_json(), stats_display, inventory_display, "Game Over - You died from your wounds." - - challenge_log = "" - challenge_occurred = False - # Check if this is a challenge page - challenge = page_data.get("challenge") - if challenge: - challenge_occurred = True - success, roll, total = perform_challenge(game_state, challenge) - challenge_log = f"
" - challenge_log += f"Challenge: {challenge.get('title', 'Skill Check')}
" - challenge_log += f"Target Stat: {challenge['stat']}, Difficulty: {challenge['difficulty']}
" - challenge_log += f"You rolled a {roll} + ({game_state.stats.get(challenge['stat'], 0)} {challenge['stat']}) = {total}
" # Corrected stat display - - # Update story based on challenge result - if success: - challenge_log += "Success!
" - next_page = challenge["success"] - else: - # *** THIS IS THE CORRECTED LINE *** - challenge_log += "Failure!
" - next_page = challenge["failure"] - - # Update game state to reflect the page change from the challenge outcome - game_state.current_page = next_page - next_page_str = str(next_page) # Use string for keys + # Move to the next page (initial move before battle/challenge checks) + next_page = selected_option["next"] + next_page_str = str(next_page) if next_page_str not in game_data: - # Handle invalid page number from challenge outcome - error_content = f"

Error: Challenge outcome leads to invalid page number {next_page}. Resetting game.

" - svg_error = create_svg_illustration("default") - return svg_error, error_content, gr.Dropdown(choices=["Restart"], label="Error"), GameState().to_json(), generate_stats_display(GameState()), generate_inventory_display(GameState()), "Error - Resetting" - + error_content = f"

Error: Option leads to invalid page number {next_page}. Resetting game.

" + svg_error = create_svg_illustration("default") + new_game_state = GameState() + return (svg_error, error_content, gr.Dropdown(choices=["Restart"], label="Error", value=None), new_game_state.to_json(), + generate_stats_display(new_game_state), generate_inventory_display(new_game_state), "Error - Resetting") + + # --- Battle Check --- + battle_occurred = False + battle_message = "" + battle_result = True + + # Check random battle flag on the *current* page data (page_data before challenge potentially changes it) + if page_data.get("randomBattle", False) and random.random() < 0.2: + battle_result, battle_log = simulate_battle(game_state) + battle_occurred = True + if not battle_result: + content = "

Game Over

You have been defeated in battle!

" + battle_log + stats_display = generate_stats_display(game_state) + inventory_display = generate_inventory_display(game_state) + return create_svg_illustration("game-over"), content, gr.Dropdown(choices=["Restart"], label="Game Over", value=None), game_state.to_json(), stats_display, inventory_display, "Game Over - You were defeated in battle." + else: + battle_message = f"
Battle Won!{battle_log}
" + + # --- Set current page to the determined next page (before challenge check) --- + game_state.current_page = next_page if next_page not in game_state.visited_pages: game_state.visited_pages.append(next_page) - page_data = game_data[next_page_str] # Load data for the page determined by the challenge outcome - - # Create the SVG illustration for the final destination page - svg_content = create_svg_illustration(page_data.get("illustration", "default")) - - # Handle game over on the destination page - if page_data.get("gameOver", False): - content = f"

{page_data['title']}

" - content += page_data["content"] - if battle_occurred and battle_result: content += battle_message # Add battle log if survived - if challenge_occurred: content += challenge_log # Add challenge log - if "ending" in page_data: - content += f"
THE END
" - content += f"

{page_data['ending']}

" - - stats_display = generate_stats_display(game_state) # Show final stats - inventory_display = generate_inventory_display(game_state) # Show final inventory - return svg_content, content, gr.Dropdown(choices=["Restart"], label="Game Over", value=None), game_state.to_json(), stats_display, inventory_display, "Game Over - " + page_data.get('title', 'Untitled') - - # Build page content for the final destination page - content = f"

{page_data.get('title', 'Untitled Page')}

" - content += page_data.get("content", "

No content.

") - if battle_occurred and battle_result: content += battle_message # Add battle log if survived - if challenge_occurred: content += challenge_log # Add challenge log - - # Build options for the final destination page - options = [] - current_inventory = game_state.inventory # Cache inventory for checks - if "options" in page_data: # Check if options exist - for opt in page_data["options"]: - option_available = True - # Check if option requires an item - req_item = opt.get("requireItem") - if req_item and req_item not in current_inventory: - option_available = False - # Check if option requires any item from a list - req_any_item = opt.get("requireAnyItem") - if option_available and req_any_item: - if not any(item in current_inventory for item in req_any_item): - option_available = False - - if option_available: - options.append(opt["text"]) - - # Handle alternative option if necessary (only if no primary options are available/visible) - alt_opt_data = page_data.get("alternativeOption") - if alt_opt_data and not options: - alt_show_if = alt_opt_data.get("showIf") - # Show alternative if no condition or condition met - if not alt_show_if or any(item in current_inventory for item in alt_show_if): - options.append(alt_opt_data["text"]) - # Temporarily add the alternative option details to the current page's options - # This ensures it can be selected on the next turn if it was the only one shown - if "options" not in page_data: page_data["options"] = [] - # Avoid adding duplicate if somehow already there - if alt_opt_data not in page_data["options"]: - page_data["options"].append(alt_opt_data) - - - if not options: # If still no options (dead end maybe?) - options = ["Restart"] - content += "

There are no further actions you can take from here.

" - - # Update game progress display text - story_path = f"You are on page {next_page}: {page_data.get('title', 'Untitled')}" - if game_state.journey_progress >= 80: - story_path += " (Nearing the conclusion)" - elif game_state.journey_progress >= 50: - story_path += " (Middle of your journey)" - elif game_state.journey_progress >= 25: - story_path += " (Adventure beginning)" - - # Generate final displays - stats_display = generate_stats_display(game_state) - inventory_display = generate_inventory_display(game_state) - - # Ensure dropdown value is reset so change event fires even if same choice is valid again - return svg_content, content, gr.Dropdown(choices=options, label="What will you do?", value=None), game_state.to_json(), stats_display, inventory_display, story_path - - -def generate_stats_display(game_state): - """Generate HTML for displaying player stats""" - # Calculate HP percentage for the progress bar - hp_percent = (game_state.current_hp / game_state.max_hp) * 100 if game_state.max_hp > 0 else 0 - hp_color = "#4CAF50" # Green - if hp_percent < 30: - hp_color = "#F44336" # Red - elif hp_percent < 70: - hp_color = "#FFC107" # Yellow - - stats_html = f""" -
-
- Courage: {game_state.stats.get('courage', 'N/A')} -
-
- Wisdom: {game_state.stats.get('wisdom', 'N/A')} -
-
- Strength: {game_state.stats.get('strength', 'N/A')} -
-
- HP: {game_state.current_hp}/{game_state.max_hp} -
-
-
-
-
- """ - - # Add journey progress - stats_html += f""" -
-
- Journey Progress: - {game_state.journey_progress}% -
-
-
-
-
- """ - - return stats_html - -def generate_inventory_display(game_state): - """Generate HTML for displaying player inventory""" - if not game_state.inventory: - return "Your inventory is empty." - - inventory_html = "
" # Added margin-top - - for item in game_state.inventory: - item_data = items_data.get(item, {"type": "unknown", "description": "A mysterious item."}) - bg_color = "#e0e0e0" # Default grey - item_type = item_data.get("type", "unknown") - description = item_data.get('description', 'No description available.') - - if item_type == "weapon": - bg_color = "#ffcdd2" # Light red - elif item_type == "armor": - bg_color = "#c8e6c9" # Light green - elif item_type == "spell": - bg_color = "#bbdefb" # Light blue - elif item_type == "quest": - bg_color = "#fff9c4" # Light yellow - elif item_type == "consumable": - bg_color = "#d1c4e9" # Light purple - - inventory_html += f""" -
- {item} -
- """ - - inventory_html += "
" - return inventory_html - -def perform_challenge(game_state, challenge): - """Perform a skill challenge and determine success. Returns (success_bool, roll, total)""" - stat = challenge["stat"] - difficulty = challenge["difficulty"] - - # Roll dice (1-6) and add stat - roll = random.randint(1, 6) - player_stat_value = game_state.stats.get(stat, 0) # Default to 0 if stat doesn't exist - total = roll + player_stat_value - - # Determine if successful - success = total >= difficulty - - # Bonus for great success - if success and total >= difficulty + 3: - stat_increase = random.randint(1, 2) - if stat in game_state.stats: - game_state.stats[stat] = player_stat_value + stat_increase - - # Penalty for bad failure - if not success and total <= difficulty - 3: - stat_decrease = random.randint(1, 2) - if stat in game_state.stats: - game_state.stats[stat] = max(1, player_stat_value - stat_decrease) # Stat cannot go below 1 - - # Record challenge outcome - if success: - game_state.challenges_won += 1 - - return success, roll, total - -def simulate_battle(game_state): - """Simulate a battle with a random enemy. Returns (player_won_bool, battle_log_html)""" - battle_log = "
" - - # Select a random enemy type - enemy_types = list(enemies_data.keys()) - if not enemy_types: - return True, "

No enemies defined for battle!

" # Avoid error if no enemies exist - enemy_type = random.choice(enemy_types) - enemy = enemies_data[enemy_type].copy() # Copy data to modify HP - - battle_log += f"

A wild {enemy_type} appears!

" - # Simple battle simulation - player_hp = game_state.current_hp - enemy_hp = enemy["hp"] + # Update journey progress + game_state.journey_progress += 5 + if game_state.journey_progress > 100: game_state.journey_progress = 100 + + # Get data for the potentially final destination page (next_page_str) + page_data = game_data[next_page_str] # Now refers to the data of the page we are landing on + + # --- HP Loss / Stat Increase on Landing Page --- + stat_increase = page_data.get("statIncrease") + if stat_increase: + stat = stat_increase.get("stat") + amount = stat_increase.get("amount") + if stat and amount and stat in game_state.stats: + game_state.stats[stat] += amount + + hp_loss = page_data.get("hpLoss") + if hp_loss: + game_state.current_hp -= hp_loss + if game_state.current_hp <= 0: + game_state.current_hp = 0 + content = "

Game Over

You have died from your wounds!

" + stats_display = generate_stats_display(game_state) + inventory_display = generate_inventory_display(game_state) + return create_svg_illustration("game-over"), content, gr.Dropdown(choices=["Restart"], label="Game Over", value=None), game_state.to_json(), stats_display, inventory_display, "Game Over - You died from your wounds." + + + # --- Challenge Check on Landing Page --- + challenge_log = "" + challenge_occurred = False + challenge = page_data.get("challenge") + if challenge: + challenge_occurred = True + req_stat = challenge.get("stat") + req_difficulty = challenge.get("difficulty") + req_success_page = challenge.get("success") + req_failure_page = challenge.get("failure") + + if not all([req_stat, req_difficulty, req_success_page is not None, req_failure_page is not None]): # Check pages can be 0 + challenge_log = "
Challenge data incomplete.
" + challenge = None # Skip challenge processing + else: + success, roll, total = perform_challenge(game_state, challenge) + challenge_log = f"
" + challenge_log += f"Challenge: {challenge.get('title', 'Skill Check')}
" + challenge_log += f"Target Stat: {req_stat}, Difficulty: {req_difficulty}
" + stat_val_before_roll = total - roll # Stat value used in the roll + challenge_log += f"You rolled a {roll} + ({stat_val_before_roll} {req_stat}) = {total}
" + + if success: + challenge_log += "Success!
" + next_page = req_success_page + else: + # *** THIS IS THE CORRECTED LINE (around line 208 of app.py) *** + challenge_log += "Failure!
" + # ************************************************************** + next_page = req_failure_page + + # Update game state again based on challenge outcome + game_state.current_page = next_page + next_page_str = str(next_page) + + if next_page_str not in game_data: + error_content = f"

Error: Challenge outcome leads to invalid page {next_page}. Resetting.

" + svg_error = create_svg_illustration("default") + new_game_state = GameState() + return (svg_error, error_content, gr.Dropdown(choices=["Restart"], label="Error", value=None), new_game_state.to_json(), + generate_stats_display(new_game_state), generate_inventory_display(new_game_state), "Error - Resetting") + + if next_page not in game_state.visited_pages: + game_state.visited_pages.append(next_page) + # Load the *final* destination page data after challenge resolution + page_data = game_data[next_page_str] + + + # --- Prepare Final Output --- + final_page_title = page_data.get('title', 'Untitled') + svg_content = create_svg_illustration(page_data.get("illustration", "default")) + + # Handle game over on the final destination page + if page_data.get("gameOver", False): + content = f"

{final_page_title}

" + content += page_data.get("content", "") + if battle_occurred and battle_result: content += battle_message + if challenge_occurred: content += challenge_log + if "ending" in page_data: + content += f"
THE END
" + content += f"

{page_data['ending']}

" - player_base_attack = game_state.stats.get("strength", 1) - player_base_defense = 1 # Base defense - - player_attack = player_base_attack - player_defense = player_base_defense - - # Add weapon/armor bonuses if the player has relevant items - attack_bonuses = [] - defense_bonuses = [] - for item in game_state.inventory: - item_data = items_data.get(item, {}) - if "attackBonus" in item_data: - bonus = item_data["attackBonus"] - player_attack += bonus - attack_bonuses.append(f"{item} (+{bonus})") - if "defenseBonus" in item_data: - bonus = item_data["defenseBonus"] - player_defense += bonus - defense_bonuses.append(f"{item} (+{bonus})") - - if attack_bonuses: battle_log += f"

Your attack ({player_base_attack}) is boosted by: {', '.join(attack_bonuses)} = {player_attack} total.

" - if defense_bonuses: battle_log += f"

Your defense ({player_base_defense}) is boosted by: {', '.join(defense_bonuses)} = {player_defense} total.

" - - enemy_attack = enemy["attack"] - enemy_defense = enemy["defense"] - - battle_log += f"

Player HP: {player_hp}, Enemy HP: {enemy_hp}


" - - # Simple turn-based combat - turn = 1 - while player_hp > 0 and enemy_hp > 0 and turn < 20: # Add turn limit to prevent infinite loops - battle_log += f"

Turn {turn}:

" - # Player attacks - damage_to_enemy = max(1, player_attack - enemy_defense) - enemy_hp -= damage_to_enemy - battle_log += f"  You attack the {enemy_type} for {damage_to_enemy} damage. (Enemy HP: {max(0, enemy_hp)})
" - - if enemy_hp <= 0: - battle_log += f"

You defeated the {enemy_type}!

" - game_state.enemies_defeated += 1 # Increment defeated count - break - - # Enemy attacks - damage_to_player = max(1, enemy_attack - player_defense) - player_hp -= damage_to_player - battle_log += f"  The {enemy_type} attacks you for {damage_to_player} damage. (Your HP: {max(0, player_hp)})" - - if player_hp <= 0: - battle_log += f"

The {enemy_type} defeated you!

" - break - - battle_log += "
" - turn += 1 - - if turn >= 20 and player_hp > 0 and enemy_hp > 0: # Check if loop ended due to turn limit - battle_log += "

The battle drags on too long and ends inconclusively. The enemy retreats!

" - - # Update player HP after battle - game_state.current_hp = max(0, player_hp) # Ensure HP doesn't go below 0 - - # Return True if player won (or survived), False if player lost - player_won = game_state.current_hp > 0 - battle_log += "" - return player_won, battle_log - - -# Create Gradio interface -with gr.Blocks(theme=gr.themes.Soft()) as demo: - gr.Markdown("# Choose Your Own Adventure") - gr.Markdown("### Can you defeat the Evil Power Master?") - - with gr.Row(): - with gr.Column(scale=2): - illustration = gr.HTML(label="Scene") - content = gr.HTML(label="Story") - choice = gr.Dropdown(label="What will you do?", allow_custom_value=False) # Prevent custom values - - # Hidden field to store game state as JSON - game_state = gr.Textbox(label="Game State JSON", visible=False) # Changed label, still hidden - - with gr.Column(scale=1): - story_path = gr.Markdown(label="Your Path") - stats = gr.HTML(label="Character Stats") - inventory = gr.HTML(label="Inventory") - # map_btn = gr.Button("View Story Map") # Removed button for simplicity - - # Event listener for player choices - choice.change( - update_game, - inputs=[choice, game_state], - outputs=[illustration, content, choice, game_state, stats, inventory, story_path] - ) - - # Initialize game on load - demo.load( - initialize_game, - inputs=[], - outputs=[illustration, content, choice, game_state, stats, inventory, story_path] - ) - -# Launch the Gradio app -demo.launch() - - - - -import json -import random -# Import illustrations from game_data for use in create_svg_illustration -from game_data import illustrations - -class GameState: - """Class to manage game state""" - def __init__(self): - self.current_page = 1 - self.visited_pages = [1] - self.inventory = [] - self.max_hp = 30 - self.current_hp = 30 - self.stats = { - "courage": 7, - "wisdom": 5, - "strength": 6, - "luck": 4 # Luck isn't used currently, but kept it - } - self.status_effects = [] - self.enemies_defeated = 0 - self.challenges_won = 0 - self.current_path = "main" - self.journey_progress = 0 # Initialize progress - - def to_json(self): - """Convert game state to JSON string""" - # Ensure stats is part of the dict being dumped - state_dict = self.__dict__.copy() - return json.dumps(state_dict) - - @classmethod - def from_dict(cls, data): - """Create a GameState object from a dictionary""" - game_state = cls() - # Make sure default values are preserved if not in data - for key, value in game_state.__dict__.items(): - if key in data: - # Special handling for stats dictionary to avoid losing default keys - if key == 'stats' and isinstance(data[key], dict): - # Update default stats with loaded stats - default_stats = getattr(game_state, key) - default_stats.update(data[key]) - setattr(game_state, key, default_stats) - else: - setattr(game_state, key, data[key]) - return game_state - -def create_svg_illustration(illustration_key): - """Return SVG illustration based on the illustration key""" - # If illustration is not found, use a default illustration - svg_code = illustrations.get(illustration_key, illustrations.get("default", "No Image")) - - # Wrap the SVG in a container div with appropriate styling - # Ensure SVG scales correctly within the container - return f""" -
-
- {svg_code} -
-
- """ -
- - -# Game content data - contains all pages with branching storylines - -game_data = { - "1": { - "title": "The Beginning", - "content": """

The Evil Power Master has been terrorizing the land for years. With his dark magic, he has enslaved villages, summoned terrible monsters, and threatens to plunge the world into eternal darkness.

-

You, a skilled adventurer, have been called upon to stop him. You stand at the entrance to the last free city, Silverhold, ready to begin your quest.

-

The fate of the world rests on your shoulders. How will you prepare for your journey?

""", - "options": [ - { "text": "Visit the local weaponsmith for equipment", "next": 2 }, - { "text": "Seek wisdom at the ancient temple", "next": 3 }, - { "text": "Meet with the resistance leader for information", "next": 4 } - ], - "illustration": "city-gates" - }, - - "2": { - "title": "The Weaponsmith", - "content": """

The city's renowned weaponsmith, Gorn, welcomes you to his forge. Weapons of all kinds line the walls, from simple daggers to exotic blades that pulse with magic.

-

"I've been expecting you," Gorn says. "The Evil Power Master's fortress is treacherous. You'll need more than common steel to face what lies ahead."

-

He presents you with three weapons, each crafted with special materials and enchantments.

""", - "options": [ - { "text": "Take the Flaming Sword, powerful but dangerous", "next": 5, "addItem": "Flaming Sword" }, - { "text": "Choose the Whispering Bow, silent and precise", "next": 5, "addItem": "Whispering Bow" }, - { "text": "Select the Guardian Shield, defensive and protective", "next": 5, "addItem": "Guardian Shield" } - ], - "illustration": "weaponsmith" - }, - - "3": { - "title": "The Ancient Temple", - "content": """

The Temple of Eternal Light stands at the heart of Silverhold. Inside, the air is thick with incense, and soft chanting echoes through the chambers.

-

High Priestess Alara greets you with concern in her eyes. "The Evil Power Master grows stronger each day. Before you face him, you should prepare your mind and spirit."

-

She offers to teach you one of the temple's secret arts to aid your quest.

""", - "options": [ - { "text": "Learn the Healing Light technique", "next": 5, "addItem": "Healing Light Spell" }, - { "text": "Master the Shield of Faith protection", "next": 5, "addItem": "Shield of Faith Spell" }, - { "text": "Study the ancient Binding Runes", "next": 5, "addItem": "Binding Runes Scroll" } - ], - "illustration": "temple" - }, - - "4": { - "title": "The Resistance Leader", - "content": """

In a secluded tavern, you meet Lyra, leader of the resistance against the Evil Power Master. Battle-scarred but determined, she unfurls a map across the table.

-

"His fortress has three possible entry points," she explains, pointing to marked locations. "Each has its dangers and advantages. You must choose your path carefully."

-

She also offers you a special item from the resistance's limited supplies.

""", - "options": [ - { "text": "Take the map showing the secret tunnel entrance", "next": 5, "addItem": "Secret Tunnel Map" }, - { "text": "Accept the poison-tipped daggers for stealth", "next": 5, "addItem": "Poison Daggers" }, - { "text": "Choose the master key that unlocks ancient doors", "next": 5, "addItem": "Master Key" } - ], - "illustration": "resistance-meeting" - }, - - "5": { - "title": "The Journey Begins", - "content": """

Prepared as best you can be, you leave Silverhold behind. The path to the Evil Power Master's fortress takes you through the Shadowwood Forest, a place once beautiful but now corrupted by dark magic.

-

Strange sounds echo among the twisted trees, and the feeling of being watched raises the hair on your neck. The main road is the fastest route, but there are alternatives.

""", - "options": [ - { "text": "Take the main road through the forest", "next": 6 }, - { "text": "Follow the winding river path", "next": 7 }, - { "text": "Brave the ancient ruins shortcut", "next": 8 } - ], - "randomBattle": True, - "illustration": "shadowwood-forest" - }, - - "6": { - "title": "Ambush on the Road", - "content": """

The main road through Shadowwood is overgrown but still visible. As you make your way forward, the trees suddenly rustle with movement.

-

A group of the Evil Power Master's scouts emerges from hiding! They've been patrolling the road, looking for those who might oppose their master.

-

"Surrender now," their leader snarls, "or face the consequences!"

""", - "challenge": { - "title": "Escape the Ambush", - "description": "You need to either fight your way through or find an escape route.", - "stat": "courage", - "difficulty": 5, # Adjusted difficulty for demo - "success": 9, - "failure": 10 - }, - "illustration": "road-ambush" - }, - - "7": { - "title": "The Mist-Shrouded River", - "content": """

The river path is longer but less traveled. Thick mist clings to the water's surface, reducing visibility but potentially hiding you from enemies.

-

As you follow the winding shore, you notice strange glowing lights beneath the water's surface. Suddenly, the mist parts to reveal a mysterious figure standing on the water itself.

-

"Few choose this path," the figure says in a melodic voice. "To proceed, you must answer my riddle."

""", - "challenge": { - "title": "The River Spirit's Riddle", - "description": "Answer correctly to gain passage and a blessing. Answer wrongly and face the river's peril.", - "stat": "wisdom", - "difficulty": 6, # Adjusted difficulty for demo - "success": 11, - "failure": 12 - }, - "illustration": "river-spirit" - }, - - "8": { - "title": "The Forgotten Ruins", - "content": """

The ancient ruins rise from the forest floor like the bones of a forgotten civilization. Moss-covered stones and crumbling arches create a labyrinth of passages and dead ends.

-

This path is the shortest route to the fortress, but the ruins are said to be haunted by the spirits of those who built this place long ago.

-

As you enter the heart of the ruins, the temperature drops dramatically, and whispers echo through the stone corridors.

""", - "challenge": { - "title": "Navigate the Haunted Ruins", - "description": "Find the correct path through the ruins while avoiding the malevolent spirits.", - "stat": "wisdom", - "difficulty": 5, # Adjusted difficulty for demo - "success": 13, - "failure": 14 - }, - "illustration": "ancient-ruins" - }, - - "9": { - "title": "Breaking Through", - "content": """

With courage and quick thinking, you manage to fight your way through the ambush. Several scouts fall to your attacks, and the rest scatter into the forest.

-

Though you've escaped, the commotion may have alerted other enemies in the area. You need to move quickly before reinforcements arrive.

-

Ahead, the forest begins to thin, revealing the barren lands that surround the Evil Power Master's fortress.

""", - "options": [ - { "text": "Rush across the open ground to the fortress", "next": 15 }, - { "text": "Wait for nightfall before crossing", "next": 16 }, - { "text": "Search for underground passages", "next": 17 } - ], - "randomBattle": True, - "illustration": "forest-edge" - }, - - "10": { - "title": "Captured!", - "content": """

Despite your best efforts, the scouts overwhelm you. Disarmed and bound, you're dragged through the forest to a small outpost.

-

"The Master will be pleased," the leader laughs. "He always enjoys interrogating those who think they can stand against him."

-

As night falls, you're locked in a crude cell. All seems lost, but one guard looks at you with sympathy when the others aren't watching.

""", - "options": [ - { "text": "Try to escape during the night", "next": 18 }, - { "text": "Appeal to the sympathetic guard", "next": 19 }, - { "text": "Wait for transport to the fortress", "next": 20 } - ], - "hpLoss": 5, - "illustration": "prisoner-cell" - }, - - "11": { - "title": "The Spirit's Blessing", - "content": """

"Wisdom flows in you like the river itself," the spirit says, impressed by your answer. The mist swirls around you, and you feel a surge of energy.

-

"Take my blessing. The waters will aid you against the darkness." The spirit touches your forehead, and a cool sensation spreads through your body.

-

The mist parts, revealing a clear path forward and the distant silhouette of the Evil Power Master's fortress.

""", - "options": [ - { "text": "Continue to the barren plains", "next": 15 }, - { "text": "Ask the spirit for more information", "next": 16 }, - { "text": "Follow the underwater passage the spirit reveals", "next": 17 } - ], - "addItem": "Water Spirit's Blessing", - "statIncrease": { "stat": "wisdom", "amount": 2 }, - "illustration": "spirit-blessing" - }, - - "12": { - "title": "The Spirit's Wrath", - "content": """

"Incorrect," the spirit hisses, its melodic voice turning harsh. "The river does not suffer fools!"

-

The mist thickens around you, and the calm water suddenly churns. A powerful current grabs your legs, pulling you into the depths.

-

You struggle against the supernatural force, barely managing to break free and scramble onto the shore, soaked and battered.

""", - "options": [ - { "text": "Continue along the riverbank", "next": 15 }, - { "text": "Find another path through the forest", "next": 16 }, - { "text": "Rest and recover before moving on", "next": 17 } - ], - "hpLoss": 8, - "illustration": "river-danger" - }, - - "13": { - "title": "Ancient Allies", - "content": """

Your wisdom guides you through the ruins' labyrinth. As you navigate the crumbling corridors, the whispers change from threatening to curious.

-

In the central chamber, transparent figures materialize - the ancient spirits of this place. They were not malevolent but protective, testing those who pass through.

-

"You have shown respect and intelligence," their leader says. "We too oppose the darkness that has corrupted our land."

""", - "options": [ - { "text": "Accept their offer of a magical artifact", "next": 15, "addItem": "Ancient Amulet" }, - { "text": "Ask for knowledge about the fortress", "next": 16 }, - { "text": "Request they guide you through secret paths", "next": 17 } - ], - "statIncrease": { "stat": "wisdom", "amount": 1 }, # Reward for passing challenge implicitly - "illustration": "ancient-spirits" - }, - - "14": { - "title": "Lost in Time", - "content": """

The ruins become an impossible maze. Each turn leads to unfamiliar passages, and the whispers grow louder, disorienting you further.

-

Hours pass as you wander, and malevolent spirits tug at your life force. By the time you finally stumble out of the ruins, you're exhausted and drained.

-

Worse, you've emerged far from your intended path, and valuable time has been lost.

""", - "options": [ - { "text": "Rest to recover your strength", "next": 15 }, - { "text": "Push on despite your exhaustion", "next": 16 }, - { "text": "Find a local guide in a nearby village", "next": 17 } - ], - "hpLoss": 10, - "statIncrease": { "stat": "luck", "amount": -1 }, # Penalty for failing challenge implicitly - "illustration": "lost-ruins" - }, - - "15": { - "title": "The Looming Fortress", - "content": """

The Evil Power Master's fortress dominates the landscape - a massive structure of black stone with towers that seem to pierce the clouds.

-

Lightning crackles around its highest spires, and dark shapes patrol the battlements. The main gate is heavily guarded, but there must be other ways in.

-

As you survey the imposing structure, you consider your options for infiltration.

""", - "options": [ - { "text": "Approach the main gate (needs disguise)", "next": 21, "requireItem": "Guard Disguise" }, - { "text": "Scale the outer wall under cover of darkness", "next": 22 }, # Leads to Discovered! challenge - { "text": "Look for the secret tunnel (requires Secret Tunnel Map)", "next": 24, "requireItem": "Secret Tunnel Map" } - ], - # Alternative option shows if neither disguise nor map is present - "alternativeOption": { "text": "Search for another entrance (risky)", "next": 21, "showIf": ["Guard Disguise", "Secret Tunnel Map"] }, # Show if *missing* items - "illustration": "evil-fortress" - }, - - "16": { - "title": "Strategic Approach", - "content": """

You take time to observe the fortress from a distance. Patrols move in predictable patterns, and supply wagons come and go at regular intervals.

-

As night falls, different sections of the fortress light up, revealing potential weaknesses in security. Your careful observation provides valuable insights.

-

You identify several possible entry points, each with its own risks and advantages.

""", - "options": [ - { "text": "Infiltrate with an incoming supply wagon", "next": 21 }, - { "text": "Use magic to create a distraction (requires any spell)", "next": 22, "requireAnyItem": ["Healing Light Spell", "Shield of Faith Spell", "Binding Runes Scroll", "Water Spirit's Blessing"] }, # Leads to Discovered! challenge - { "text": "Bribe a guard to let you in (requires special item)", "next": 23, "requireAnyItem": ["Ancient Amulet", "Master Key"] } # Leads to Secret Resistance - ], - # If no required items for specific options, provide a fallback - "alternativeOption": { - "text": "Attempt to sneak in using shadows", - "next": 21, - "showIf": ["Healing Light Spell", "Shield of Faith Spell", "Binding Runes Scroll", "Water Spirit's Blessing", "Ancient Amulet", "Master Key"] # Show if missing required items - }, - "statIncrease": { "stat": "wisdom", "amount": 1 }, # Reward for observation - "illustration": "fortress-observation" - }, - - "17": { - "title": "The Hidden Way", - "content": """

Your search reveals an unexpected approach to the fortress - an ancient maintenance tunnel that runs beneath the barren plains, likely forgotten by the Evil Power Master.

-

The tunnel is old and could be unstable, but it would allow you to bypass most of the outer defenses entirely.

-

As you consider this option, you hear patrol horns in the distance - they're sweeping the area for intruders.

""", - "options": [ - { "text": "Enter the tunnel immediately", "next": 24 }, - { "text": "Hide and wait for the patrol to pass", "next": 21 }, - { "text": "Set a trap for the patrol", "next": 22 } # Leads to Discovered! challenge - ], - "illustration": "hidden-tunnel" - }, - - "18": { - "title": "Night Escape", - "content": """

You wait until the dead of night, when most guards are dozing at their posts. Using a loose stone you discovered in your cell, you work on the rusted lock.

-

With a satisfying click, the cell door unlocks. Moving silently through the shadows, you retrieve your confiscated equipment from a nearby storeroom.

-

Now you must leave the outpost without raising the alarm.

""", - "challenge": { - "title": "Stealthy Escape", - "description": "Slip past the sleeping guards without alerting them.", - "stat": "wisdom", - "difficulty": 6, - "success": 16, # Escaped successfully, back to observing - "failure": 10 # Recaptured! Back to cell (loop) - }, - "illustration": "night-escape" - }, - - "19": { - "title": "Unexpected Ally", - "content": """

When the guard passes your cell alone, you whisper, asking why they seemed sympathetic. The guard looks around nervously before responding.

-

"My village was enslaved by the Evil Power Master. My family serves him under threat of death, but many of us secretly hope for his downfall."

-

The guard offers to help you escape, but warns there will be a price for this betrayal if discovered.

""", - "options": [ - { "text": "Accept the guard's help (gain disguise, proceed)", "next": 16, "addItem": "Guard Disguise"}, # Gain disguise, proceed to strategic approach - { "text": "Decline - you don't want to put them at risk", "next": 18 }, # Try night escape instead - { "text": "Convince them to join your cause", "next": 20 } # They arrange transport? - ], - "illustration": "guard-ally" - }, - - "20": { - "title": "Journey to the Fortress", - "content": """

At dawn, you're bound and loaded onto a prison wagon with other captured travelers. The journey to the fortress is uncomfortable, but you use the time to observe and plan.

-

The other prisoners share what they know about the fortress. One mentions rumors of a resistance operating within the Evil Power Master's ranks.

-

As the wagon approaches the massive gates, you begin to formulate a plan.

""", - "options": [ - { "text": "Look for an opportunity to escape during transfer", "next": 22 }, # High risk, likely leads to discovered challenge - { "text": "Remain captive to get inside, then escape", "next": 21 }, # Get inside, then try to move - { "text": "Try to contact the internal resistance (needs clue/item)", "next": 23, "requireAnyItem": ["Guard Disguise", "Ancient Amulet"]} # Signal resistance - ], - "alternativeOption": { "text": "Remain captive with no plan (leads to capture)", "next": 28, "showIf": ["Guard Disguise", "Ancient Amulet"] }, # If no clue/item - "illustration": "prison-wagon" - }, - - "21": { - "title": "Infiltration", - "content": """

Using your skills and preparation, you manage to infiltrate the outer perimeter of the fortress. The black stone walls loom overhead, even more intimidating up close.

-

Guards patrol in pairs, and magical wards pulse with dark energy. You'll need to be extremely careful as you navigate deeper into enemy territory.

-

The sound of a bell signals a shift change, providing a momentary distraction.

""", - "options": [ - { "text": "Head towards the central tower", "next": 25 }, - { "text": "Search for the dungeons", "next": 26 }, - { "text": "Follow a group of mages", "next": 27 } - ], - "randomBattle": True, - "illustration": "fortress-interior" - }, - - "22": { - "title": "Discovered!", - "content": """

As you make your way through the fortress, an alarm suddenly sounds! Your presence has been detected, and you hear the heavy footsteps of guards approaching.

-

"Intruder in the south corridor!" a voice shouts. "Seal all exits!"

-

You have moments to react before you're surrounded.

""", - "challenge": { - "title": "Escape Detection", - "description": "You need to escape from the approaching guards before they trap you.", - "stat": "courage", - "difficulty": 7, - "success": 25, # Managed to evade, head to tower - "failure": 28 # Captured Again - }, - "illustration": "fortress-alarm" - }, - - "23": { - "title": "Secret Resistance", - "content": """

Through luck or skill (or maybe a bribe or disguise), you make contact with members of the secret resistance operating within the fortress. They're skeptical of you at first, but your actions have convinced them of your intentions.

-

"We've been working to undermine the Evil Power Master for years," their leader whispers. "Now with your help, we might finally have a chance to end his reign."

-

They share crucial information about the fortress layout and the Evil Power Master's weaknesses.

""", - "options": [ - { "text": "Join their planned uprising", "next": 29 }, - { "text": "Ask them to help you reach the Power Master directly", "next": 30 }, - { "text": "Share your knowledge with them", "next": 31 } - ], - "illustration": "secret-meeting" - }, - - "24": { - "title": "Underground Passage", - "content": """

The ancient tunnel is damp and narrow, forcing you to crouch as you make your way forward. Roots hang from the ceiling, and the air feels thick with age.

-

After what seems like hours of careful navigation, you notice light ahead. The tunnel opens into what appears to be a forgotten storeroom beneath the fortress.

-

Dust covers everything, but you can hear movement in the rooms above.

""", - "options": [ - { "text": "Explore the storeroom for useful items", "next": 32 }, - { "text": "Find a way up to the main fortress", "next": 33 }, - { "text": "Listen carefully to determine what's happening above", "next": 34 } - ], - "illustration": "underground-passage" - }, - - "25": { - "title": "The Central Tower", - "content": """

The central tower rises from the heart of the fortress like a black spear. As you get closer, you can feel powerful magic emanating from its peak.

-

Guards are more numerous here, and you spot several dark mages performing rituals in alcoves along the walls. Whatever the Evil Power Master is planning, it seems to be approaching its climax.

-

A massive door blocks the entrance to the tower, engraved with strange symbols.

""", - "options": [ - { "text": "Try to decipher the symbols (needs wisdom)", "next": 35 }, - { "text": "Look for another entrance", "next": 36 }, - { "text": "Use magic/key to open the door", "next": 37, "requireAnyItem": ["Healing Light Spell", "Shield of Faith Spell", "Binding Runes Scroll", "Water Spirit's Blessing", "Master Key"] } - ], - "alternativeOption": { "text": "Wait and observe the mages", "next": 36, "showIf": ["Healing Light Spell", "Shield of Faith Spell", "Binding Runes Scroll", "Water Spirit's Blessing", "Master Key"] }, - "illustration": "central-tower" - }, - - "26": { - "title": "The Dungeons", - "content": """

The dungeons are a maze of dark, damp corridors lined with cells. Moans and whispers echo off the stone walls, creating an eerie atmosphere of despair.

-

Many of the cells contain prisoners - ordinary people from villages conquered by the Evil Power Master, resistance fighters, and even a few magical creatures.

-

At the end of one corridor, you spot a heavily guarded cell that seems more important than the others.

""", - "options": [ - { "text": "Try to free some prisoners to cause a distraction", "next": 38 }, - { "text": "Investigate the heavily guarded cell", "next": 39 }, - { "text": "Look for a passage leading upward", "next": 40 } - ], - "illustration": "dungeon-corridors" - }, - - "27": { - "title": "The Ritual Chamber", - "content": """

Following the group of mages leads you to a large circular chamber. Hidden in the shadows, you observe as they prepare for what appears to be an important ritual.

-

In the center of the room hovers a dark crystal, pulsing with malevolent energy. The mages arrange themselves around it, beginning a chant in an ancient language.

-

"Soon the master's power will be unstoppable," one of them mutters. "The alignment is nearly perfect."

""", - "options": [ - { "text": "Disrupt the ritual", "next": 41 }, - { "text": "Quietly observe to learn more", "next": 42 }, - { "text": "Steal the crystal when they're distracted", "next": 43 } - ], - "illustration": "ritual-chamber" - }, - - "28": { - "title": "Captured Again", - "content": """

The guards overwhelm you, and this time, there's no easy escape. You're brought before a high-ranking officer, who smiles coldly.

-

"The Master will be most pleased," he says. "He so rarely gets to meet those foolish enough to challenge him directly."

-

You're escorted under heavy guard toward the central tower, where the Evil Power Master awaits.

""", - "options": [ - { "text": "Look for another chance to escape (low odds)", "next": 44 }, - { "text": "Prepare yourself mentally to face the Power Master", "next": 45 }, - { "text": "Try to gather information from the guards", "next": 46 } - ], - "illustration": "prisoner-escort" - }, - - "29": { - "title": "The Uprising", - "content": """

The resistance has planned carefully. When the signal is given, chaos erupts throughout the fortress. Guards find themselves fighting servants, and magical wards fail as saboteurs destroy key components.

-

In the confusion, you and a small team of resistance fighters make your way toward the central tower where the Evil Power Master resides.

-

"This is our only chance," the resistance leader whispers. "We must reach him before he realizes what's happening."

""", - "options": [ - { "text": "Lead the charge directly", "next": 47 }, - { "text": "Split up to create more distractions", "next": 48 }, # Send player to final battle - { "text": "Sneak ahead while the others draw attention", "next": 49 } - ], - "illustration": "fortress-uprising" - }, - - "30": { - "title": "The Final Approach", - "content": """

With the resistance's help, you navigate through secret passages unknown to most of the fortress inhabitants. These narrow corridors, built during the fortress's construction, lead directly to the upper levels.

-

"The Power Master's chambers are at the top of the central tower," your guide explains. "He rarely leaves them these days, too focused on some grand ritual. This may be our only chance to stop him."

-

The passage ends at a hidden door, beyond which lies the base of the central tower.

""", - "options": [ - { "text": "Enter the tower cautiously", "next": 50 }, - { "text": "Ask about the ritual the Power Master is performing", "next": 51 }, - { "text": "Request additional help for the final confrontation", "next": 52 } - ], - "illustration": "secret-passage" - }, - - "31": { - "title": "Knowledge Exchange", - "content": """

You share everything you've learned on your journey - about the river spirit, the ancient ruins, and any magical artifacts you've collected. In return, the resistance provides crucial information.

-

"The Evil Power Master draws his power from an ancient crystal," their sage explains. "It amplifies his natural abilities tenfold, but it's also a weakness. Destroy the crystal, and his power will be greatly diminished."

-

They also reveal that the Power Master is preparing for a cosmic alignment that will make him virtually unstoppable.

""", - "options": [ - { "text": "Ask how to locate the crystal", "next": 53 }, - { "text": "Find out when the alignment will occur", "next": 54 }, - { "text": "Learn if there are any weapons that can harm the crystal", "next": 55 } - ], - "statIncrease": { "stat": "wisdom", "amount": 1 }, # Reward for sharing info - "illustration": "knowledge-sharing" - }, - - # --- Placeholder Pages --- - "32": { "title": "Storeroom Search", "content": "

You search the dusty storeroom. Mostly old crates and broken tools, but you find a forgotten Healing Potion tucked away!

", "options": [{ "text": "Drink Potion and find way up", "next": 33, "addItem": "Healing Potion" }], "illustration": "underground-passage" }, # Add Potion - "33": { "title": "Ascending", "content": "

You find a loose grate leading to the fortress kitchens. Smells marginally better than the tunnel. You emerge cautiously.

", "options": [{ "text": "Sneak through the kitchens", "next": 21 }], "illustration": "fortress-interior" }, - "34": { "title": "Listening", "content": "

You hear the rhythmic marching of guards above, and the distant clang of metal from the armory. Sounds like standard patrols, nothing immediately alarming.

", "options": [{ "text": "Explore the storeroom", "next": 32 }, { "text": "Try to find a way up", "next": 33 }], "illustration": "underground-passage" }, - "35": { "title": "Deciphering Symbols", "content": "

The symbols pulse with dark energy. Your wisdom tells you they form a powerful ward, possibly tied to the Master's life force. It requires a specific magical resonance or immense force to bypass.

", "options": [{ "text": "Look for another entrance", "next": 36 }, { "text": "Use magic/key (if you have it)", "next": 37, "requireAnyItem": ["Healing Light Spell", "Shield of Faith Spell", "Binding Runes Scroll", "Water Spirit's Blessing", "Master Key", "Ancient Amulet"] }], "statIncrease": {"stat":"wisdom", "amount":1}, "illustration": "central-tower" }, - "36": { "title": "Searching for Entry", "content": "

You circle the base of the tower. High up, you spot a less guarded balcony. Scaling the sheer, dark stone might be possible for someone with exceptional strength or courage, but it's extremely dangerous.

", "options": [{ "text": "Attempt to climb (Strength/Courage Challenge)", "next": 22 }, {"text": "Return to the main door", "next": 25}], "illustration": "central-tower" }, # Leads to challenge - "37": { "title": "Magical Entry", "content": "

You focus your magical item (or the Master Key). The symbols flare violently, then fade as the massive door groans open! You step into the oppressive darkness of the tower base.

", "options": [{ "text": "Ascend the tower", "next": 48 }], "illustration": "final-confrontation" }, # Straight to the top - "38": { "title": "Causing Distraction", "content": "

Using your lockpicking skills (or the Master Key), you manage to unlock a few cells. The freed prisoners, though weak, immediately cause a commotion, shouting and banging, drawing several guards away from their posts.

", "options": [{ "text": "Investigate the guarded cell", "next": 39 }, {"text": "Look for an exit upwards", "next": 40}], "requireAnyItem": ["Master Key"], "illustration": "dungeon-corridors" }, # Added item requirement - "39": { "title": "Guarded Cell", "content": "

Inside the heavily barred cell is a frail figure clad in mage robes – likely a captured resistance member or someone who defied the Master. They look up weakly as you approach the bars.

", "options": [{ "text": "Attempt to free the mage (needs Master Key/Runes)", "next": 30, "requireAnyItem": ["Master Key", "Binding Runes Scroll"] }, {"text": "Leave them for now", "next": 40}], "illustration": "dungeon-corridors" }, # Specified required items - "40": { "title": "Finding Passage", "content": "

Behind a loose tapestry depicting the Master's grim visage, you find a hidden spiral staircase leading up, likely towards the main fortress levels above the dungeons.

", "options": [{ "text": "Take the stairs", "next": 21 }], "illustration": "fortress-interior" }, - "41": { "title": "Disrupting the Ritual", "content": "

You burst into the chamber! The mages are startled, their chant faltering. You launch an attack, disrupting their concentration entirely. The central crystal flares erratically, and raw magic arcs through the room.

", "options": [{ "text": "Fight the enraged mages!", "next": 22 }], "illustration": "ritual-chamber" }, # Likely leads to being discovered challenge - "42": { "title": "Observing the Ritual", "content": "

You watch intently as the mages chant in unison. The crystal seems to be drawing power from ley lines deep within the fortress, focusing it towards the tower's peak for the Master's final empowerment during the alignment.

", "options": [{ "text": "Try to disrupt it now", "next": 41 }, {"text": "Try to steal the crystal (very risky)", "next": 43}, {"text": "Leave and head to the tower", "next": 25}], "statIncrease": {"stat": "wisdom", "amount": 1}, "illustration": "ritual-chamber" }, - "43": { "title": "Stealing the Crystal", "content": "

As the mages are deep in their chant, you attempt to swiftly snatch the pulsing crystal. It burns with raw, cold energy! They immediately sense the disturbance and react with fury, turning to attack you.

", "options": [{ "text": "Fight back!", "next": 22 }], "illustration": "ritual-chamber" }, # Leads to being discovered challenge - "44": { "title": "Escape Attempt", "content": "

You scan your surroundings, looking for any weakness, any chance. But the guards are professionals, their grip like iron, their eyes vigilant. No escape seems possible right now.

", "options": [{ "text": "Focus and prepare mentally", "next": 45 }, {"text": "Try to glean info from guards", "next": 46}], "illustration": "prisoner-escort" }, - "45": { "title": "Mental Preparation", "content": "

You close your eyes briefly, focusing your mind. You recall your training, your purpose, the faces of those suffering under the Master's rule. You are ready. Let him come.

", "options": [{ "text": "Continue to the Master's chamber", "next": 48 }], "statIncrease": {"stat": "courage", "amount": 1}, "illustration": "final-confrontation" }, # Directly to final battle - "46": { "title": "Gathering Information", "content": "

You try to subtly listen to the guards' chatter. They mutter about the Master being 'nearly ready' and the 'alignment is minutes away'. One nervously mentions the crystal is 'unstable' at peak power. Time is critically short.

", "options": [{ "text": "Focus and prepare mentally", "next": 45 }, {"text": "Look desperately for escape again", "next": 44}], "statIncrease": {"stat": "wisdom", "amount": 1}, "illustration": "prisoner-escort" }, - "47": { "title": "Leading the Charge", "content": "

With a battle cry, you lead the resistance fighters forward, cutting through the surprised ranks of the Master's personal guard. Steel clashes against steel as you push relentlessly towards the main tower entrance.

", "options": [{ "text": "Breach the tower door", "next": 25 }], "illustration": "fortress-uprising" }, - # Page 48 is the final confrontation - "49": { "title": "Sneaking Ahead", "content": "

While the main uprising creates chaos as a diversion, you slip through the shadows, bypassing the main battle. You find a less-guarded servant's entrance to the central tower.

", "options": [{ "text": "Enter the tower stealthily", "next": 50 }], "illustration": "secret-passage" }, - "50": { "title": "Cautious Entry", "content": "

You ease open the hidden door and step into the base of the central tower. A grand staircase spirals upwards into oppressive darkness. The very air hums with palpable dark energy.

", "options": [{ "text": "Ascend the tower carefully", "next": 48 }], "illustration": "final-confrontation" }, # Head to final battle - "51": { "title": "Ritual Information", "content": "

Your resistance guide explains the Master is performing a ritual tied to a rare cosmic alignment to achieve immense power, possibly immortality. The dark crystal in his chamber is the focal point.

", "options": [{ "text": "Enter the tower now", "next": 50 }, {"text": "Ask for additional backup", "next": 52}], "illustration": "secret-passage" }, - "52": { "title": "Requesting Help", "content": "

The resistance leader nods grimly. 'Take Kaelen,' she says, gesturing to a scarred warrior armed with a blessed axe. 'May their strength aid yours.' Kaelen gives a curt nod.

", "options": [{ "text": "Accept Kaelen's help and enter", "next": 48 }], "addItem": "Resistance Ally", "illustration": "secret-passage" }, # Add ally and go to fight - "53": { "title": "Crystal Location", "content": "

The resistance sage confirms the crystal is kept in the Master's main audience chamber, at the very top of the central tower. It reputedly floats above his throne, radiating immense power.

", "options": [{ "text": "Prepare and head to the tower", "next": 48 }], "illustration": "knowledge-sharing" }, # Go to final battle - "54": { "title": "Alignment Timing", "content": "

'The alignment is upon us!' the sage gasps, pointing to strange lights in the sky visible even through the fortress windows. 'It may already be too late! You must hurry!'

", "options": [{ "text": "Race to the tower immediately!", "next": 48 }], "illustration": "knowledge-sharing" }, # Go to final battle - "55": { "title": "Crystal Weakness", "content": "

They mention ancient texts hinting that the crystal, forged in darkness, is vulnerable to sources of pure light or concentrated holy energy. Perhaps your Healing Light spell, if focused, or the Shield of Faith could disrupt it?

", "options": [{ "text": "Remember this and head to the tower", "next": 48 }], "illustration": "knowledge-sharing" }, # Go to final battle - - "56": { # Direct Confrontation Battle - "title": "Direct Confrontation", - "content": """

You charge at the Evil Power Master, drawing on all your courage and strength. He meets your attack with blasts of dark magic, the air between you crackling and distorting with raw energy.

-

The battle is fierce, pushing you to your absolute limits. Each blow you land seems partially absorbed by the dark energy surrounding him, while his counter-attacks grow increasingly powerful and difficult to evade.

-

As the fight rages, you realize that as long as the dark crystal pulses behind him, this might be an unwinnable war of attrition.

""", - "challenge": { - "title": "Battle with the Evil Power Master", - "description": "You must overcome his dark magic through sheer determination, skill, and perhaps exploiting a weakness.", - "stat": "strength", # Primary check is Strength - "difficulty": 9, # High difficulty - "success": 59, # Hero's Victory - "failure": 60 # Darkness Prevails - }, - "illustration": "power-battle" - }, - "57": { # Crystal Destruction Attempt - "title": "Crystal Destruction", - "content": """

Recognizing the crystal as the true source of his overwhelming power, you feint an attack and then lunge towards the malevolently pulsing gem floating above the throne. The Evil Power Master shouts in alarm and fury, desperately trying to intercept you.

-

"No! Stay away from that, you insignificant fool! You have no conception of the forces you're tampering with!"

-

As you get closer, the crystal's energy intensifies dramatically, throwing up a shimmering barrier of pure dark magic that you must break through to reach it.

""", - "challenge": { - "title": "Breaking the Crystal Barrier", - "description": "You must overcome the crystal's protective magic using wit, willpower, or perhaps a specific counter-energy.", - "stat": "wisdom", # Primary check is Wisdom - "difficulty": 8, # Slightly lower difficulty than direct fight - "success": 61, # The Crystal Shatters - "failure": 62 # A Desperate Gambit (leads to sacrifice ending) - }, - "illustration": "crystal-barrier" - }, - "58": { - "title": "Attempt to Reason", - "content": """

You hold up a hand, trying to speak to the Evil Power Master, appealing to any possible reason or shred of humanity that might remain within the darkness. He pauses, then throws back his head and laughs – a chilling, hollow sound.

-

"Humanity? Reason? Such fleeting, petty concerns! Power is the only truth, the only reason! And soon, I shall have ultimate power!" he snarls, launching a devastating bolt of dark energy directly at you.

-

Clearly, negotiation was never an option.

""", - "options": [ - { "text": "Attack the Evil Power Master directly", "next": 56 }, - { "text": "Try to destroy the crystal", "next": 57 } - ], - "illustration": "final-confrontation" - }, - "59": { - "title": "A Hero's Victory", - "content": """

Through sheer determination, skill, and perhaps exploiting a momentary weakness, you manage to overcome the Evil Power Master's formidable defenses. As he staggers backwards from your decisive blow, his concentration wavers, and his connection to the crystal weakens for a critical instant.

-

Seizing the opportunity, you strike the dark crystal with all your remaining might (or perhaps channel pure energy into it). It cracks visibly, then shatters in a blinding flash of light and concussive energy that throws you back.

-

The Evil Power Master screams a final, agonized cry as his borrowed power dissipates like smoke. "Impossible! I was... so close... to ultimate power!"

-

As the blinding light fades, you stand victorious amidst the crumbling chamber. The dark magic recedes from the land, and the people are free once more.

""", - "gameOver": True, - "ending": "You have defeated the Evil Power Master and destroyed the source of his terrible power. Songs will be sung of your bravery and skill for generations to come. The land slowly begins to heal from his corruption, and you are hailed as the Hero of Silverhold throughout the recovering realm.", - "illustration": "hero-victory" - }, - "60": { - "title": "Darkness Prevails", - "content": """

Despite your bravery and best efforts, the Evil Power Master's command over dark magic, amplified by the crystal, is simply too great. His relentless attacks overwhelm your defenses, shattering your weapon or shield, and bringing you crashing to your knees.

-

"Valiant, perhaps, but ultimately futile," he says, looking down at you with cold contempt. "You could have served me, you know. Gained power beyond your imagining. Now... you will simply serve as a warning." He raises a hand, dark energy coalescing.

-

As your vision fades into darkness, you see the crystal's evil glow intensifying, the alignment reaching its peak. You have failed. Darkness will cover the land, and the Master's reign is absolute.

""", - "gameOver": True, - "ending": "The Evil Power Master proved too powerful, and your heroic quest ends in tragic defeat. Darkness spreads unchallenged across the land as he completes his ritual and ascends to even greater, terrifying power. Hope fades, but perhaps, someday, another hero will rise to challenge his eternal reign.", - "illustration": "dark-victory" - }, - "61": { - "title": "The Crystal Shatters", - "content": """

Drawing on all your wisdom and inner willpower (or perhaps using a specific magical counter), you manage to push through the crystal's defensive barrier. Reaching out, you touch its vibrating surface, which burns with an unnatural cold against your skin.

-

The Evil Power Master shrieks in mingled fury and desperation, "Stop! Stop, you fool! You'll doom us all!"

-

With a final, concentrated effort, you channel your own life force or focused energy into the dark gem. Cracks spiderweb across its obsidian surface, spreading rapidly until it detonates with a deafening implosion of light and sound, sucking the dark energy from the room.

-

When your vision clears, the Evil Power Master lies broken and powerless on the floor, his power utterly broken along with the crystal. The oppressive atmosphere lifts, and peace can finally begin to return to the land.

""", - "gameOver": True, - "ending": "By cleverly targeting and destroying the source of the Evil Power Master's augmented strength, you have saved the realm from his tyranny. The fortress itself begins to shake and crumble as its dark magic foundations unravel. You escape just in time, leaving the ruin behind. Your name becomes a legend associated with wisdom and decisive action.", - "illustration": "crystal-destroyed" - }, - "62": { - "title": "A Desperate Gambit", - "content": """

The crystal's protective barrier proves too strong, violently repelling your attempts to break through. As you struggle against its crushing force, the Evil Power Master regains his composure and approaches, a cruel smile playing on his lips.

-

"Did you truly think it would be that easy?" he mocks, preparing a final, devastating spell. "This crystal has endured for millennia. It cannot be destroyed by a pathetic mortal like you."

-

Seeing no other option, in a final, desperate move, you gather all your remaining strength and hurl your most powerful weapon or focus your strongest spell directly at the unstable crystal.

-

To your surprise and the Evil Power Master's horror, this doesn't break the barrier but instead creates a catastrophic resonance effect within the overloaded gem. The crystal begins to vibrate violently, its contained energy spiraling wildly out of control.

-

"What have you done?!" the Master cries in terror as the crystal's energy erupts outwards, engulfing both of you, the chamber, and the entire tower top in blinding, destructive light.

""", - "gameOver": True, - "ending": "Your desperate, final attack destabilized the dark crystal, causing a catastrophic release of uncontrolled energy that utterly annihilated the Evil Power Master, his chamber, and tragically, yourself. Though you did not survive, your ultimate sacrifice shattered his power and saved the realm from imminent darkness. Bards will forever sing of your selfless heroism.", - "illustration": "heroic-sacrifice" - } -} - - -# --- Item Data --- -items_data = { - "Flaming Sword": {"type": "weapon", "description": "A sword wreathed in magical fire. Looks dangerous to friend and foe. +3 Attack.", "attackBonus": 3}, - "Whispering Bow": {"type": "weapon", "description": "A finely crafted bow enchanted for silence. Favored by assassins. +2 Attack.", "attackBonus": 2}, - "Guardian Shield": {"type": "armor", "description": "A sturdy shield blessed with protective magic. Glows faintly. +2 Defense.", "defenseBonus": 2}, - "Healing Light Spell": {"type": "spell", "description": "A basic clerical spell that can mend minor wounds."}, - "Shield of Faith Spell": {"type": "spell", "description": "A protective spell that creates a temporary shimmering barrier."}, - "Binding Runes Scroll": {"type": "spell", "description": "A scroll containing complex runes to temporarily immobilize an enemy."}, - "Secret Tunnel Map": {"type": "quest", "description": "A crudely drawn map marking a hidden tunnel entrance near the fortress's north wall."}, - "Poison Daggers": {"type": "weapon", "description": "A pair of wicked-looking daggers coated with a fast-acting paralytic poison. +1 Attack.", "attackBonus": 1}, - "Master Key": {"type": "quest", "description": "A heavy iron key with intricate wards. Said to unlock any non-magical lock."}, - "Water Spirit's Blessing": {"type": "spell", "description": "A lingering blessing from the river spirit, enhancing clarity and insight."}, - "Ancient Amulet": {"type": "quest", "description": "A heavy stone amulet radiating ancient protective magic. Seems to resist dark energy. +1 Defense.", "defenseBonus": 1}, - "Guard Disguise": {"type": "quest", "description": "A slightly ill-fitting uniform worn by the fortress guards."}, - "Healing Potion": {"type": "consumable", "description": "A vial containing a sparkling red liquid. Restores 10 HP.", "hpRestore": 10, "useOnAdd": True}, # Added HP restore value and use flag - "Resistance Ally": {"type": "quest", "description": "Kaelen, a grim resistance fighter armed with a blessed axe, fights alongside you."}, # Added name -} - -# --- Enemy Data --- -enemies_data = { - "Scout": {"hp": 15, "attack": 4, "defense": 1}, - "Dark Mage": {"hp": 20, "attack": 6, "defense": 2}, - "Fortress Guard": {"hp": 25, "attack": 5, "defense": 3}, - "Shadow Hound": {"hp": 18, "attack": 5, "defense": 2}, - "Animated Armor": {"hp": 30, "attack": 4, "defense": 4}, -} - - -# --- SVG templates for illustrations --- -# Helper function for placeholder SVGs -def create_placeholder_svg(text="Placeholder", color="#cccccc"): - # Basic SVG with a rectangle and centered text - return f""" - - {text} - Sorry, your browser does not support inline SVG. - """ - -illustrations = { - "default": create_placeholder_svg("Default Scene", "#aaaaaa"), - "game-over": create_placeholder_svg("Game Over", "#ffaaaa"), - "city-gates": """ - - - - - - - - - - - - - - - - - - - - - - """, - "weaponsmith": """ - - - - - - - - - - - - - - - - - - - - - - - - - """, - # --- Add other placeholders --- - "temple": create_placeholder_svg("Ancient Temple", "#fffde7"), - "resistance-meeting": create_placeholder_svg("Resistance Meeting", "#efebe9"), - "shadowwood-forest": create_placeholder_svg("Shadowwood Forest", "#38502e"), - "road-ambush": create_placeholder_svg("Road Ambush", "#795548"), - "river-spirit": create_placeholder_svg("River Spirit", "#b3e5fc"), - "ancient-ruins": create_placeholder_svg("Ancient Ruins", "#bdbdbd"), - "forest-edge": create_placeholder_svg("Forest Edge", "#a5d6a7"), - "prisoner-cell": create_placeholder_svg("Prisoner Cell", "#616161"), - "spirit-blessing": create_placeholder_svg("Spirit Blessing", "#e1f5fe"), - "river-danger": create_placeholder_svg("River Danger", "#42a5f5"), - "ancient-spirits": create_placeholder_svg("Ancient Spirits", "#e0e0e0"), - "lost-ruins": create_placeholder_svg("Lost in Ruins", "#9e9e9e"), - "evil-fortress": create_placeholder_svg("Evil Fortress", "#212121"), - "fortress-observation": create_placeholder_svg("Observing Fortress", "#424242"), - "hidden-tunnel": create_placeholder_svg("Hidden Tunnel", "#757575"), - "night-escape": create_placeholder_svg("Night Escape", "#37474f"), - "guard-ally": create_placeholder_svg("Guard Ally", "#bcaaa4"), - "prison-wagon": create_placeholder_svg("Prison Wagon", "#8d6e63"), - "fortress-interior": create_placeholder_svg("Fortress Interior", "#616161"), - "fortress-alarm": create_placeholder_svg("Fortress Alarm", "#f44336"), - "secret-meeting": create_placeholder_svg("Secret Meeting", "#4e342e"), - "underground-passage": create_placeholder_svg("Underground Passage", "#6d4c41"), - "central-tower": create_placeholder_svg("Central Tower", "#3e2723"), - "dungeon-corridors": create_placeholder_svg("Dungeon Corridors", "#5d4037"), - "ritual-chamber": create_placeholder_svg("Ritual Chamber", "#4a148c"), - "prisoner-escort": create_placeholder_svg("Prisoner Escort", "#757575"), - "fortress-uprising": create_placeholder_svg("Fortress Uprising", "#ff7043"), - "secret-passage": create_placeholder_svg("Secret Passage", "#a1887f"), - "knowledge-sharing": create_placeholder_svg("Knowledge Sharing", "#fff176"), - "final-confrontation": create_placeholder_svg("Final Confrontation", "#d32f2f"), - "power-battle": create_placeholder_svg("Power Battle", "#ef5350"), - "crystal-barrier": create_placeholder_svg("Crystal Barrier", "#ab47bc"), - "hero-victory": create_placeholder_svg("Hero's Victory", "#ffee58"), - "dark-victory": create_placeholder_svg("Darkness Prevails", "#263238"), - "crystal-destroyed": create_placeholder_svg("Crystal Destroyed", "#81d4fa"), - "heroic-sacrifice": create_placeholder_svg("Heroic Sacrifice", "#ffcc80"), + stats_display = generate_stats_display(game_state) + inventory_display = generate_inventory_display(game_state) + return svg_content, content, gr.Dropdown(choices=["Restart"], label="Game Over", value=None), game_state.to_json(), stats_display, inventory_display, "Game Over - " + final_page_title + + # Build content for the final destination page (if not game over) + content = f"

{final_page_title}

" + content += page_data.get("content", "

No content.

") + if battle_occurred and battle_result: content += battle_message + if challenge_occurred: content += challenge_log + + # Build options for the final destination page + options_texts = [] + current_inventory = game_state.inventory # Cache inventory again + final_page_options = page_data.get("options", []) + if final_page_options: + for opt in final_page_options: + option_available = True + if isinstance(opt, dict): # Ensure opt is a dictionary + req_item = opt.get("requireItem") + if req_item and req_item not in current_inventory: + option_available = False + req_any_item = opt.get("requireAnyItem") + if option_available and req_any_item: + if not any(item in current_inventory for item in req_any_item): + option_available = False + if option_available and opt.get("text"): + options_texts.append(opt["text"]) + # else: skip invalid option format + + # Handle alternative option if necessary (only if no primary options were available/visible) + alt_opt_data = page_data.get("alternativeOption") + if isinstance(alt_opt_data, dict) and alt_opt_data.get("text") and not options_texts: # Check if text exists and no primary options shown + alt_show_if = alt_opt_data.get("showIf") + if not alt_show_if or any(item in current_inventory for item in alt_show_if): + options_texts.append(alt_opt_data["text"]) + + + if not options_texts: # If still no options (dead end?) + options_texts = ["Restart"] + content += "

There are no further actions you can take from here.

" + + # Update story path display text + story_path = f"You are on page {next_page}: {final_page_title}" + if game_state.journey_progress >= 80: story_path += " (Nearing the conclusion)" + elif game_state.journey_progress >= 50: story_path += " (Middle of your journey)" + elif game_state.journey_progress >= 25: story_path += " (Adventure beginning)" + + # Generate final displays + stats_display = generate_stats_display(game_state) + inventory_display = generate_inventory_display(game_state) -} -
+ # Return final state + return (svg_content, content, gr.Dropdown(choices=list(set(options_texts)), label="What will you do?", value=None), + game_state.to_json(), stats_display, inventory_display, story_path) - - - - \ No newline at end of file + except Exception as e: + # Generic error handling for the entire update function + print(f"Error during update_game: {e}") + import traceback + traceback.print_exc() # Print detailed traceback to console (if possible in Gradio Lite) + error_content = f"

A critical error occurred: {e}. Resetting game state.

" + svg_error = create_svg_illustration("default") + new_game_state = GameState() # Reset state + return (svg_error, error_content, gr.Dropdown(choices=["Restart"], label="Critical Error", value=None), new_game_state.to_json(), + generate_stats_display(new_game_state), generate_inventory_display(new_game_state), "Critical Error - Reset") + \ No newline at end of file