mind_craft / app.py
broadfield-dev's picture
Update app.py
2c594a7 verified
raw
history blame
1.96 kB
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)