Download app.py from LonginAI/moje-hra: direct link, hf CLI and curl.
- Browser
- Download file 22.6 kB
-
https://huggingface.co/spaces/LonginAI/moje-hra/resolve/main/app.py
- Command line
-
hf download hf://spaces/LonginAI/moje-hra/app.py
-
curl -L -o app.py https://huggingface.co/spaces/LonginAI/moje-hra/resolve/main/app.py
22.6 kB
| import gradio as gr | |
| import numpy as np | |
| import random | |
| import json | |
| from dataclasses import dataclass, asdict | |
| from typing import List, Dict, Any, Optional | |
| # Abstract base class for map objects | |
| class MapObject: | |
| map_level: int | |
| typ: str # General type (e.g., terrain, resource, creature) | |
| druh: str # Specific kind (e.g., forest, oak_tree, wolf_pack) | |
| value: int # Quantity or intensity | |
| metadata: Dict[str, Any] # Additional information | |
| # Define terrain types with their characteristics | |
| TERRAIN_TYPES = { | |
| "ocean": {"color": (0, 0, 139), "spawnable": False}, | |
| "sea": {"color": (0, 0, 205), "spawnable": False}, | |
| "beach": {"color": (238, 203, 173), "spawnable": False}, | |
| "plains": {"color": (34, 139, 34), "spawnable": True}, | |
| "forest": {"color": (0, 100, 0), "spawnable": True}, | |
| "jungle": {"color": (0, 80, 0), "spawnable": True}, | |
| "hills": {"color": (139, 137, 137), "spawnable": True}, | |
| "mountains": {"color": (105, 105, 105), "spawnable": True}, | |
| "desert": {"color": (238, 203, 173), "spawnable": True}, | |
| "swamp": {"color": (143, 188, 143), "spawnable": True}, | |
| "tundra": {"color": (220, 220, 220), "spawnable": True}, | |
| "lake": {"color": (65, 105, 225), "spawnable": False}, | |
| "river": {"color": (30, 144, 255), "spawnable": False}, | |
| "stone_field": {"color": (169, 169, 169), "spawnable": True}, | |
| "cave": {"color": (40, 40, 40), "spawnable": True} | |
| } | |
| # Object types that can spawn on different terrains | |
| OBJECT_TYPES = { | |
| "tree": {"terrains": ["forest", "jungle"], "value_range": (1, 10)}, | |
| "giant_tree": {"terrains": ["jungle"], "value_range": (1, 3)}, | |
| "stone": {"terrains": ["hills", "mountains", "stone_field"], "value_range": (1, 8)}, | |
| "sand": {"terrains": ["desert", "beach"], "value_range": (1, 5)}, | |
| "ore": {"terrains": ["hills", "mountains"], "value_range": (1, 5)}, | |
| "fertile_soil": {"terrains": ["plains", "swamp"], "value_range": (1, 10)}, | |
| "fish": {"terrains": ["ocean", "sea", "lake", "river"], "value_range": (1, 20)}, | |
| "underground_water": {"terrains": ["plains", "swamp", "hills"], "value_range": (1, 3)}, | |
| "lava": {"terrains": ["mountains", "cave"], "value_range": (1, 3)}, | |
| "coal": {"terrains": ["mountains", "hills"], "value_range": (1, 4)}, | |
| "mercury": {"terrains": ["cave"], "value_range": (1, 2)}, | |
| "crystals": {"terrains": ["mountains", "cave"], "value_range": (1, 3)}, | |
| "mana_spring": {"terrains": ["forest", "jungle", "swamp", "cave"], "value_range": (1, 2)}, | |
| "rubber": {"terrains": ["jungle"], "value_range": (1, 5)}, | |
| "wildlife": {"terrains": ["forest", "jungle", "plains", "swamp", "tundra"], "value_range": (1, 8)}, | |
| "herbs": {"terrains": ["forest", "jungle", "plains", "swamp"], "value_range": (1, 6)}, | |
| "fruit": {"terrains": ["forest", "jungle"], "value_range": (1, 8)}, | |
| "honey": {"terrains": ["forest"], "value_range": (1, 4)}, | |
| "grass": {"terrains": ["plains", "swamp"], "value_range": (1, 10)}, | |
| "clay": {"terrains": ["swamp", "beach"], "value_range": (1, 6)}, | |
| "ruins": {"terrains": ["plains", "desert", "hills"], "value_range": (1, 3)}, | |
| "altar": {"terrains": ["forest", "mountains", "cave"], "value_range": (1, 1)}, | |
| "shaman": {"terrains": ["forest", "jungle", "swamp", "cave"], "value_range": (1, 2)}, | |
| "horse": {"terrains": ["plains", "hills"], "value_range": (1, 5)}, | |
| "cattle": {"terrains": ["plains"], "value_range": (1, 8)}, | |
| "wolf_pack": {"terrains": ["forest", "tundra", "hills"], "value_range": (1, 4)}, | |
| "magic_node": {"terrains": ["forest", "jungle", "mountains", "cave"], "value_range": (1, 2)}, | |
| "ancient_temple": {"terrains": ["jungle", "mountains", "desert"], "value_range": (1, 1)}, | |
| "crystal_cave": {"terrains": ["mountains", "hills"], "value_range": (1, 2)}, | |
| "mushroom_grove": {"terrains": ["forest", "swamp"], "value_range": (1, 6)}, | |
| "berry_bush": {"terrains": ["forest", "tundra"], "value_range": (1, 8)}, | |
| "hot_spring": {"terrains": ["hills", "mountains"], "value_range": (1, 3)}, | |
| "fossil_bed": {"terrains": ["desert", "stone_field"], "value_range": (1, 4)}, | |
| "gem_vein": {"terrains": ["mountains", "cave"], "value_range": (1, 3)}, | |
| "ancient_ruins": {"terrains": ["desert", "plains", "mountains"], "value_range": (1, 2)}, | |
| "waterfall": {"terrains": ["mountains", "hills"], "value_range": (1, 2)}, | |
| "fishing_village": {"terrains": ["river", "lake", "beach"], "value_range": (1, 1)}, | |
| "abandoned_mine": {"terrains": ["hills", "mountains"], "value_range": (1, 1)}, | |
| "druid_circle": {"terrains": ["forest", "plains"], "value_range": (1, 1)}, | |
| "mermaid_lagoon": {"terrains": ["lake", "river"], "value_range": (1, 1)}, | |
| "ghost_town": {"terrains": ["desert", "plains"], "value_range": (1, 1)}, | |
| "fire_geyser": {"terrains": ["mountains"], "value_range": (1, 2)}, | |
| "ice_cave": {"terrains": ["mountains", "tundra"], "value_range": (1, 1)}, | |
| "wishing_well": {"terrains": ["plains", "forest", "hills"], "value_range": (1, 1)}, | |
| "sacred_grove": {"terrains": ["forest", "jungle"], "value_range": (1, 1)} | |
| } | |
| # Generate realistic terrain map | |
| def generate_terrain_map(width=12, height=12): | |
| # Initialize map with random terrains | |
| terrain_map = np.random.choice(list(TERRAIN_TYPES.keys()), size=(height, width)) | |
| # Create more realistic clusters | |
| for _ in range(20): # Number of smoothing iterations | |
| for y in range(height): | |
| for x in range(width): | |
| # Check neighbors | |
| neighbors = [] | |
| for dy in [-1, 0, 1]: | |
| for dx in [-1, 0, 1]: | |
| if 0 <= y+dy < height and 0 <= x+dx < width: | |
| neighbors.append(terrain_map[y+dy, x+dx]) | |
| # Most common neighbor becomes the new terrain | |
| if neighbors: | |
| terrain_map[y, x] = max(set(neighbors), key=neighbors.count) | |
| # Add realistic features | |
| # Ensure ocean borders | |
| for x in range(width): | |
| terrain_map[0, x] = random.choice(["ocean", "sea", "beach"]) | |
| terrain_map[height-1, x] = random.choice(["ocean", "sea", "beach"]) | |
| for y in range(height): | |
| terrain_map[y, 0] = random.choice(["ocean", "sea", "beach"]) | |
| terrain_map[y, width-1] = random.choice(["ocean", "sea", "beach"]) | |
| # Place rivers | |
| for _ in range(3): | |
| start_edge = random.choice(["top", "bottom", "left", "right"]) | |
| end_edge = random.choice(["top", "bottom", "left", "right"]) | |
| if start_edge == "top": | |
| start_x, start_y = random.randint(0, width-1), 0 | |
| elif start_edge == "bottom": | |
| start_x, start_y = random.randint(0, width-1), height-1 | |
| elif start_edge == "left": | |
| start_x, start_y = 0, random.randint(0, height-1) | |
| else: # right | |
| start_x, start_y = width-1, random.randint(0, height-1) | |
| if end_edge == "top": | |
| end_x, end_y = random.randint(0, width-1), 0 | |
| elif end_edge == "bottom": | |
| end_x, end_y = random.randint(0, width-1), height-1 | |
| elif end_edge == "left": | |
| end_x, end_y = 0, random.randint(0, height-1) | |
| else: # right | |
| end_x, end_y = width-1, random.randint(0, height-1) | |
| # Simple river path (in a real implementation, you'd use pathfinding) | |
| x, y = start_x, start_y | |
| while x != end_x or y != end_y: | |
| terrain_map[y, x] = "river" | |
| # Move toward destination | |
| if x < end_x: | |
| x += 1 | |
| elif x > end_x: | |
| x -= 1 | |
| if y < end_y: | |
| y += 1 | |
| elif y > end_y: | |
| y -= 1 | |
| # Place lakes in inland areas | |
| for _ in range(5): | |
| x, y = random.randint(2, width-3), random.randint(2, height-3) | |
| if terrain_map[y, x] not in ["ocean", "sea", "river"]: | |
| terrain_map[y, x] = "lake" | |
| return terrain_map | |
| # Generate objects for a specific terrain | |
| def generate_objects(terrain_type: str, width: int = 24, height: int = 24) -> List[MapObject]: | |
| objects = [] | |
| # Only spawn objects on spawnable terrains | |
| if not TERRAIN_TYPES[terrain_type]["spawnable"]: | |
| return objects | |
| # Determine possible object types for this terrain | |
| possible_objects = [obj for obj, data in OBJECT_TYPES.items() if terrain_type in data["terrains"]] | |
| # Generate random objects | |
| num_objects = random.randint(5, 20) | |
| for _ in range(num_objects): | |
| obj_type = random.choice(possible_objects) | |
| obj_druh = obj_type | |
| obj_value = random.randint(*OBJECT_TYPES[obj_type]["value_range"]) | |
| x = random.randint(0, width-1) | |
| y = random.randint(0, height-1) | |
| # Add some metadata for fun | |
| metadata = {"x": x, "y": y} | |
| if obj_type == "tree": | |
| metadata["height"] = random.randint(5, 20) | |
| elif obj_type == "wildlife": | |
| animals = ["deer", "boar", "rabbit", "bear", "fox"] | |
| metadata["species"] = random.choice(animals) | |
| obj = MapObject( | |
| map_level=2, | |
| typ=obj_type, | |
| druh=obj_druh, | |
| value=obj_value, | |
| metadata=metadata | |
| ) | |
| objects.append(obj) | |
| return objects | |
| # Generate detailed terrain for a specific tile | |
| def generate_detailed_terrain(terrain_type: str, width: int = 24, height: int = 24): | |
| # Generate a map with more detailed features | |
| detailed_map = np.random.choice(list(TERRAIN_TYPES.keys()), size=(height, width)) | |
| # Create clusters based on the parent terrain type | |
| for _ in range(10): | |
| for y in range(height): | |
| for x in range(width): | |
| # Check neighbors | |
| neighbors = [] | |
| for dy in [-1, 0, 1]: | |
| for dx in [-1, 0, 1]: | |
| if 0 <= y+dy < height and 0 <= x+dx < width: | |
| neighbors.append(detailed_map[y+dy, x+dx]) | |
| # Most common neighbor becomes the new terrain | |
| if neighbors: | |
| detailed_map[y, x] = max(set(neighbors), key=neighbors.count) | |
| # Refine based on parent terrain | |
| for y in range(height): | |
| for x in range(width): | |
| # Higher probability of keeping the parent terrain type | |
| if random.random() > 0.7: | |
| detailed_map[y, x] = terrain_type | |
| return detailed_map | |
| # Generate sub-tile details (48x48) | |
| def generate_subtile_details(terrain_type: str, width: int = 48, height: int = 48): | |
| # Generate even more detailed features | |
| subtile_map = np.random.choice(list(TERRAIN_TYPES.keys()), size=(height, width)) | |
| # Create even more clusters for detailed view | |
| for _ in range(5): | |
| for y in range(height): | |
| for x in range(width): | |
| # Check neighbors | |
| neighbors = [] | |
| for dy in [-1, 0, 1]: | |
| for dx in [-1, 0, 1]: | |
| if 0 <= y+dy < height and 0 <= x+dx < width: | |
| neighbors.append(subtile_map[y+dy, x+dx]) | |
| # Most common neighbor becomes the new terrain | |
| if neighbors: | |
| subtile_map[y, x] = max(set(neighbors), key=neighbors.count) | |
| # Refine based on parent terrain | |
| for y in range(height): | |
| for x in range(width): | |
| # Higher probability of keeping the parent terrain type | |
| if random.random() > 0.6: | |
| subtile_map[y, x] = terrain_type | |
| return subtile_map | |
| # Convert terrain map to image | |
| def terrain_map_to_image(terrain_map, tile_size=24): | |
| height, width = terrain_map.shape | |
| img = np.zeros((height * tile_size, width * tile_size, 3), dtype=np.uint8) | |
| for y in range(height): | |
| for x in range(width): | |
| terrain = terrain_map[y, x] | |
| color = TERRAIN_TYPES[terrain]["color"] | |
| img[y*tile_size:(y+1)*tile_size, x*tile_size:(x+1)*tile_size] = color | |
| return img | |
| # Convert detailed map to image with grid | |
| def detailed_map_to_image(terrain_map, tile_size=24, show_grid=False): | |
| height, width = terrain_map.shape | |
| img = np.zeros((height * tile_size, width * tile_size, 3), dtype=np.uint8) | |
| for y in range(height): | |
| for x in range(width): | |
| terrain = terrain_map[y, x] | |
| color = TERRAIN_TYPES[terrain]["color"] | |
| img[y*tile_size:(y+1)*tile_size, x*tile_size:(x+1)*tile_size] = color | |
| if show_grid: | |
| # Draw grid lines | |
| for x in range(0, width * tile_size, tile_size): | |
| img[:, x:x+1] = [0, 0, 0] | |
| for y in range(0, height * tile_size, tile_size): | |
| img[y:y+1, :] = [0, 0, 0] | |
| return img | |
| # Convert subtile map to image with 3x3 grid sections | |
| def subtile_map_to_image(terrain_map, tile_size=16, show_sections=False): | |
| height, width = terrain_map.shape | |
| img = np.zeros((height * tile_size, width * tile_size, 3), dtype=np.uint8) | |
| for y in range(height): | |
| for x in range(width): | |
| terrain = terrain_map[y, x] | |
| color = TERRAIN_TYPES[terrain]["color"] | |
| img[y*tile_size:(y+1)*tile_size, x*tile_size:(x+1)*tile_size] = color | |
| # Draw main grid (24x24 sections) | |
| if show_sections: | |
| for x in range(0, width * tile_size, tile_size * 3): | |
| img[:, x:x+1] = [255, 255, 255] | |
| for y in range(0, height * tile_size, tile_size * 3): | |
| img[y:y+1, :] = [255, 255, 255] | |
| return img | |
| # Generate the initial world map | |
| world_map = generate_terrain_map(12, 12) | |
| current_map_level = 1 | |
| current_tile_x = 0 | |
| current_tile_y = 0 | |
| selected_tile_info = {"terrain": "plains", "objects": []} | |
| # Create Gradio interface | |
| with gr.Blocks(title="Terrain Map Explorer") as demo: | |
| gr.Markdown("# Terrain Map Explorer") | |
| gr.Markdown("Explore a procedurally generated terrain map with three levels of detail.") | |
| # State variables | |
| map_level_state = gr.State(value=1) | |
| tile_x_state = gr.State(value=0) | |
| tile_y_state = gr.State(value=0) | |
| terrain_map_state = gr.State(value=world_map) | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| # Image display | |
| map_image = gr.Image(label="Map View", interactive=True, height=400) | |
| # Navigation buttons | |
| with gr.Row(): | |
| up_button = gr.Button("↑ Up", elem_id="up_button") | |
| enter_button = gr.Button("Enter →", elem_id="enter_button") | |
| # Arrow buttons for border navigation | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| left_arrow = gr.Button("←", elem_id="left_arrow", visible=False) | |
| with gr.Column(scale=1): | |
| pass # Spacer | |
| with gr.Column(scale=1): | |
| right_arrow = gr.Button("→", elem_id="right_arrow", visible=False) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| top_arrow = gr.Button("↑", elem_id="top_arrow", visible=False) | |
| with gr.Column(scale=1): | |
| pass # Spacer | |
| with gr.Column(scale=1): | |
| bottom_arrow = gr.Button("↓", elem_id="bottom_arrow", visible=False) | |
| with gr.Column(scale=1): | |
| # Inspector panel | |
| inspector = gr.JSON(label="Tile Inspector") | |
| # Function to update the map display based on current state | |
| def update_map(map_level, tile_x, tile_y, terrain_map): | |
| global selected_tile_info | |
| if map_level == 1: | |
| # Show the whole world map | |
| img = terrain_map_to_image(terrain_map, 24) | |
| selected_tile_info = {"map_level": 1, "terrain": "overview"} | |
| return img, selected_tile_info, gr.Button(visible=False), gr.Button(visible=False), gr.Button(visible=False), gr.Button(visible=False) | |
| elif map_level == 2: | |
| # Show detailed terrain of a selected tile | |
| terrain_type = terrain_map[tile_y, tile_x] | |
| detailed_map = generate_detailed_terrain(terrain_type, 12, 12) | |
| objects = generate_objects(terrain_type, 12, 12) | |
| # Store information for inspector | |
| selected_tile_info = { | |
| "map_level": 2, | |
| "terrain_type": terrain_type, | |
| "coordinates": (tile_x, tile_y), | |
| "objects": [asdict(obj) for obj in objects] | |
| } | |
| img = detailed_map_to_image(detailed_map, 24, True) | |
| # Show navigation arrows based on position | |
| show_left = tile_x > 0 | |
| show_right = tile_x < 11 | |
| show_top = tile_y > 0 | |
| show_bottom = tile_y < 11 | |
| return img, selected_tile_info, gr.Button(visible=show_left), gr.Button(visible=show_right), gr.Button(visible=show_top), gr.Button(visible=show_bottom) | |
| elif map_level == 3: | |
| # Show subtile details (3x3 sections) | |
| terrain_type = terrain_map[tile_y, tile_x] | |
| subtile_map = generate_subtile_details(terrain_type, 48, 48) | |
| # Generate objects for this detailed view | |
| objects = generate_objects(terrain_type, 48, 48) | |
| # Store information for inspector | |
| selected_tile_info = { | |
| "map_level": 3, | |
| "terrain_type": terrain_type, | |
| "coordinates": (tile_x, tile_y), | |
| "objects": [asdict(obj) for obj in objects] | |
| } | |
| img = subtile_map_to_image(subtile_map, 16, True) | |
| return img, selected_tile_info, gr.Button(visible=False), gr.Button(visible=False), gr.Button(visible=False), gr.Button(visible=False) | |
| # Function to handle map clicks | |
| def handle_map_click(evt: gr.SelectData, map_level, tile_x, tile_y, terrain_map): | |
| if map_level == 1: | |
| # Select a tile on the world map | |
| x, y = evt.index | |
| return map_level, x, y, terrain_map | |
| elif map_level == 2: | |
| # Select a tile on the detailed map | |
| x, y = evt.index | |
| # Divide the detailed map into 3x3 sections | |
| section_x = x // 3 | |
| section_y = y // 3 | |
| return map_level, section_x, section_y, terrain_map | |
| elif map_level == 3: | |
| # Select a subtile | |
| x, y = evt.index | |
| # Divide into 3x3 sections for display | |
| section_x = x // 16 # 48/3 = 16 pixels per section | |
| section_y = y // 16 | |
| return map_level, section_x, section_y, terrain_map | |
| return map_level, tile_x, tile_y, terrain_map | |
| # Function to handle Enter button click | |
| def enter_tile(map_level, tile_x, tile_y, terrain_map): | |
| if map_level < 3: | |
| map_level += 1 | |
| return map_level, tile_x, tile_y, terrain_map | |
| # Function to handle Up button click | |
| def go_up(map_level, tile_x, tile_y, terrain_map): | |
| if map_level > 1: | |
| map_level -= 1 | |
| return map_level, tile_x, tile_y, terrain_map | |
| # Navigation arrow functions | |
| def go_left(map_level, tile_x, tile_y, terrain_map): | |
| if tile_x > 0: | |
| tile_x -= 1 | |
| return map_level, tile_x, tile_y, terrain_map | |
| def go_right(map_level, tile_x, tile_y, terrain_map): | |
| if tile_x < 11: | |
| tile_x += 1 | |
| return map_level, tile_x, tile_y, terrain_map | |
| def go_top(map_level, tile_x, tile_y, terrain_map): | |
| if tile_y > 0: | |
| tile_y -= 1 | |
| return map_level, tile_x, tile_y, terrain_map | |
| def go_bottom(map_level, tile_x, tile_y, terrain_map): | |
| if tile_y < 11: | |
| tile_y += 1 | |
| return map_level, tile_x, tile_y, terrain_map | |
| # Initial map update | |
| initial_img, initial_info, initial_left, initial_right, initial_top, initial_bottom = update_map( | |
| map_level_state.value, tile_x_state.value, tile_y_state.value, terrain_map_state.value | |
| ) | |
| map_image.value = initial_img | |
| inspector.value = initial_info | |
| # Event handlers | |
| map_image.select( | |
| handle_map_click, | |
| inputs=[map_level_state, tile_x_state, tile_y_state, terrain_map_state], | |
| outputs=[map_level_state, tile_x_state, tile_y_state, terrain_map_state] | |
| ) | |
| enter_button.click( | |
| enter_tile, | |
| inputs=[map_level_state, tile_x_state, tile_y_state, terrain_map_state], | |
| outputs=[map_level_state, tile_x_state, tile_y_state, terrain_map_state] | |
| ) | |
| up_button.click( | |
| go_up, | |
| inputs=[map_level_state, tile_x_state, tile_y_state, terrain_map_state], | |
| outputs=[map_level_state, tile_x_state, tile_y_state, terrain_map_state] | |
| ) | |
| left_arrow.click( | |
| go_left, | |
| inputs=[map_level_state, tile_x_state, tile_y_state, terrain_map_state], | |
| outputs=[map_level_state, tile_x_state, tile_y_state, terrain_map_state] | |
| ) | |
| right_arrow.click( | |
| go_right, | |
| inputs=[map_level_state, tile_x_state, tile_y_state, terrain_map_state], | |
| outputs=[map_level_state, tile_x_state, tile_y_state, terrain_map_state] | |
| ) | |
| top_arrow.click( | |
| go_top, | |
| inputs=[map_level_state, tile_x_state, tile_y_state, terrain_map_state], | |
| outputs=[map_level_state, tile_x_state, tile_y_state, terrain_map_state] | |
| ) | |
| bottom_arrow.click( | |
| go_bottom, | |
| inputs=[map_level_state, tile_x_state, tile_y_state, terrain_map_state], | |
| outputs=[map_level_state, tile_x_state, tile_y_state, terrain_map_state] | |
| ) | |
| # Update map when state changes | |
| map_level_state.change( | |
| update_map, | |
| inputs=[map_level_state, tile_x_state, tile_y_state, terrain_map_state], | |
| outputs=[map_image, inspector, left_arrow, right_arrow, top_arrow, bottom_arrow] | |
| ) | |
| tile_x_state.change( | |
| update_map, | |
| inputs=[map_level_state, tile_x_state, tile_y_state, terrain_map_state], | |
| outputs=[map_image, inspector, left_arrow, right_arrow, top_arrow, bottom_arrow] | |
| ) | |
| tile_y_state.change( | |
| update_map, | |
| inputs=[map_level_state, tile_x_state, tile_y_state, terrain_map_state], | |
| outputs=[map_image, inspector, left_arrow, right_arrow, top_arrow, bottom_arrow] | |
| ) | |
| # Launch the interface | |
| if __name__ == "__main__": | |
| demo.launch() |