broadfield-dev commited on
Commit
662b3b3
·
verified ·
1 Parent(s): 0f2d63b

Create app.py

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