Spaces:
Sleeping
Sleeping
File size: 1,963 Bytes
662b3b3 2c594a7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import numpy as np
import random
from typing import Dict, List
import uvicorn
app = FastAPI()
# Enable CORS for browser access
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# Mock InstantMesh (replace with real model later)
def generate_mesh(chunk_x: int, chunk_z: int) -> List[List[float]]:
chunk_size = 5
mesh = np.zeros((chunk_size, chunk_size))
prompt = "rocky hill" if random.random() > 0.5 else "flat plains"
if "hill" in prompt:
mesh[2, 2] = random.randint(2, 5)
for i in range(chunk_size):
for j in range(chunk_size):
dist = np.sqrt((i - 2)**2 + (j - 2)**2)
mesh[i, j] = max(0, 5 - dist)
else:
mesh = np.ones((chunk_size, chunk_size)) * random.uniform(0, 1)
return mesh.tolist()
# Game state
class Game:
def __init__(self):
self.player_pos = [0, 0] # [x, z]
self.world: Dict[tuple, List[List[float]]] = {}
self.chunk_size = 5
def get_chunk_coords(self, x: int, z: int) -> tuple:
return (x // self.chunk_size, z // self.chunk_size)
def generate_chunk(self, chunk_x: int, chunk_z: int):
key = (chunk_x, chunk_z)
if key not in self.world:
print(f"Generating chunk at {key}")
self.world[key] = generate_mesh(chunk_x, chunk_z)
return self.world[key]
game = Game()
@app.get("/move")
async def move(dx: int = 0, dz: int = 0):
game.player_pos[0] += dx
game.player_pos[1] += dz
chunk_x, chunk_z = game.get_chunk_coords(game.player_pos[0], game.player_pos[1])
chunk = game.generate_chunk(chunk_x, chunk_z)
return {
"player_pos": game.player_pos,
"chunk": chunk,
"chunk_coords": [chunk_x, chunk_z]
}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860) |