Spaces:
Sleeping
Sleeping
Deploy Myco from CI
Browse files- ui/renderers.py +423 -571
ui/renderers.py
CHANGED
|
@@ -1,592 +1,444 @@
|
|
| 1 |
-
"""
|
| 2 |
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 7 |
)
|
| 8 |
-
from game.state import
|
| 9 |
from models.mushroom import Mushroom
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
}
|
| 14 |
-
|
| 15 |
-
# ── Inline CSS + keyframes injected once into the scene ──────────────────────
|
| 16 |
-
SCENE_STYLES = """
|
| 17 |
-
<style>
|
| 18 |
-
/* ── Base scene ── */
|
| 19 |
-
.forest-scene {
|
| 20 |
-
position: relative; isolation: isolate;
|
| 21 |
-
overflow: hidden; min-height: 20rem;
|
| 22 |
-
padding: 0.75rem; border-radius: 28px;
|
| 23 |
-
background:
|
| 24 |
-
radial-gradient(circle at 50% 72%, rgba(255,239,173,.26), transparent 8rem),
|
| 25 |
-
radial-gradient(circle at 18% 24%, rgba(134,225,153,.22), transparent 10rem),
|
| 26 |
-
linear-gradient(180deg,#123626 0%,#245c3c 54%,#523b20 100%);
|
| 27 |
-
border: 1px solid rgba(225,255,209,.25);
|
| 28 |
-
box-shadow: 0 18px 48px rgba(8,30,20,.26);
|
| 29 |
-
color: #f9ffe9;
|
| 30 |
-
}
|
| 31 |
-
|
| 32 |
-
/* ── HUD bar ── */
|
| 33 |
-
.scene-hud {
|
| 34 |
-
position: relative; z-index: 3;
|
| 35 |
-
display: flex; flex-wrap: wrap; gap: .4rem;
|
| 36 |
-
padding: .5rem .75rem; border-radius: 999px;
|
| 37 |
-
background: rgba(13,38,28,.72); backdrop-filter: blur(6px);
|
| 38 |
-
font-size: .8rem; font-weight: 800;
|
| 39 |
-
}
|
| 40 |
-
|
| 41 |
-
/* ── Stage ── */
|
| 42 |
-
.scene-stage {
|
| 43 |
-
position: relative; height: 13rem;
|
| 44 |
-
margin: .6rem 0;
|
| 45 |
-
background:
|
| 46 |
-
linear-gradient(transparent 62%, rgba(106,79,33,.55) 63%),
|
| 47 |
-
radial-gradient(ellipse at bottom, rgba(190,255,180,.2), transparent 60%);
|
| 48 |
-
border-radius: 22px; overflow: hidden;
|
| 49 |
-
}
|
| 50 |
-
.scene-stage span { position: absolute; line-height:1; }
|
| 51 |
-
|
| 52 |
-
/* ── Trees / grass ── */
|
| 53 |
-
.tree-left { left:6%; bottom:18%; font-size:5rem; filter:drop-shadow(0 8px 12px rgba(0,0,0,.28)); }
|
| 54 |
-
.tree-right { right:7%; bottom:20%; font-size:4.75rem; filter:drop-shadow(0 8px 12px rgba(0,0,0,.28)); }
|
| 55 |
-
.grass-one { left:26%; bottom:14%; font-size:2.4rem; }
|
| 56 |
-
.grass-two { right:28%;bottom:16%; font-size:2.2rem; }
|
| 57 |
-
|
| 58 |
-
/* ── Sparkles ── */
|
| 59 |
-
.sparkle-one { left:34%; top:18%; font-size:1.8rem; animation:twinkle 1.8s ease-in-out infinite; }
|
| 60 |
-
.sparkle-two { right:33%; top:24%; font-size:1.6rem; animation:twinkle 2.2s ease-in-out infinite reverse; }
|
| 61 |
-
@keyframes twinkle {
|
| 62 |
-
0%,100%{opacity:.3;transform:scale(.85);}
|
| 63 |
-
50%{opacity:1;transform:scale(1.18);}
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
/* ── Myco player — position grid ── */
|
| 67 |
-
.myco-player {
|
| 68 |
-
font-size:4rem; z-index:2;
|
| 69 |
-
transition: left .32s cubic-bezier(.34,1.56,.64,1), bottom .32s cubic-bezier(.34,1.56,.64,1);
|
| 70 |
-
animation: myco-bob 2.4s ease-in-out infinite;
|
| 71 |
-
filter:drop-shadow(0 6px 14px rgba(255,92,92,.28));
|
| 72 |
-
}
|
| 73 |
-
@keyframes myco-bob {
|
| 74 |
-
0%,100%{transform:translateY(0);}
|
| 75 |
-
50%{transform:translateY(-.45rem);}
|
| 76 |
-
}
|
| 77 |
-
.pos-0-0 .myco-player{left:14%;bottom:62%;}
|
| 78 |
-
.pos-1-0 .myco-player{left:41%;bottom:62%;}
|
| 79 |
-
.pos-2-0 .myco-player{left:67%;bottom:62%;}
|
| 80 |
-
.pos-0-1 .myco-player{left:14%;bottom:36%;}
|
| 81 |
-
.pos-1-1 .myco-player{left:41%;bottom:36%;}
|
| 82 |
-
.pos-2-1 .myco-player{left:67%;bottom:36%;}
|
| 83 |
-
.pos-0-2 .myco-player{left:14%;bottom:13%;}
|
| 84 |
-
.pos-1-2 .myco-player{left:41%;bottom:13%;}
|
| 85 |
-
.pos-2-2 .myco-player{left:67%;bottom:13%;}
|
| 86 |
-
|
| 87 |
-
/* ── Mushroom sprite ── */
|
| 88 |
-
.mushroom-sprite {
|
| 89 |
-
right:33%; bottom:22%; font-size:3.6rem; z-index:2;
|
| 90 |
-
filter:drop-shadow(0 6px 14px rgba(0,0,0,.3));
|
| 91 |
-
}
|
| 92 |
-
|
| 93 |
-
/* ── STATE: waiting (no mushroom) ── */
|
| 94 |
-
.state-waiting .mushroom-sprite { opacity:0; }
|
| 95 |
-
|
| 96 |
-
/* ── STATE: discovered — mushroom pops up, Myco runs toward it ── */
|
| 97 |
-
.state-discovered .mushroom-sprite {
|
| 98 |
-
animation: shroom-popup 0.7s cubic-bezier(.34,1.56,.64,1) both;
|
| 99 |
-
}
|
| 100 |
-
.state-discovered .myco-player {
|
| 101 |
-
animation: myco-bob 2.4s ease-in-out infinite,
|
| 102 |
-
myco-run-in 0.6s cubic-bezier(.34,1.56,.64,1) both;
|
| 103 |
-
}
|
| 104 |
-
@keyframes shroom-popup {
|
| 105 |
-
0% { opacity:0; transform:translateY(3rem) scale(.2); }
|
| 106 |
-
60% { opacity:1; transform:translateY(-.4rem) scale(1.18); }
|
| 107 |
-
100% { opacity:1; transform:translateY(0) scale(1); }
|
| 108 |
-
}
|
| 109 |
-
@keyframes myco-run-in {
|
| 110 |
-
0% { transform:translateX(-3rem) scaleX(-1); }
|
| 111 |
-
100% { transform:translateX(0) scaleX(1); }
|
| 112 |
-
}
|
| 113 |
-
|
| 114 |
-
/* ── STATE: picked — mushroom flies up and disappears ── */
|
| 115 |
-
.state-picked .mushroom-sprite {
|
| 116 |
-
animation: shroom-catch 0.55s cubic-bezier(.34,1.56,.64,1) both;
|
| 117 |
-
}
|
| 118 |
-
.state-picked .myco-player {
|
| 119 |
-
animation: myco-bob 2.4s ease-in-out infinite,
|
| 120 |
-
myco-catch-jump 0.55s ease both;
|
| 121 |
-
}
|
| 122 |
-
@keyframes shroom-catch {
|
| 123 |
-
0% { opacity:1; transform:translateY(0) scale(1); }
|
| 124 |
-
40% { opacity:1; transform:translateY(-2.5rem) scale(1.3) rotate(15deg); }
|
| 125 |
-
100% { opacity:0; transform:translateY(-5rem) scale(.2) rotate(40deg); }
|
| 126 |
-
}
|
| 127 |
-
@keyframes myco-catch-jump {
|
| 128 |
-
0%,100%{ transform:translateY(0); }
|
| 129 |
-
40% { transform:translateY(-1.4rem) scale(1.1); }
|
| 130 |
-
}
|
| 131 |
-
|
| 132 |
-
/* ── STATE: danger (poison, not yet picked) ── */
|
| 133 |
-
.state-danger .mushroom-sprite {
|
| 134 |
-
animation: shroom-popup 0.7s cubic-bezier(.34,1.56,.64,1) both,
|
| 135 |
-
poison-pulse 1.2s ease-in-out infinite 0.7s;
|
| 136 |
-
filter: drop-shadow(0 0 18px rgba(255,62,91,.9));
|
| 137 |
-
}
|
| 138 |
-
.state-danger .myco-player {
|
| 139 |
-
animation: myco-bob 2.4s ease-in-out infinite,
|
| 140 |
-
myco-recoil 0.5s ease both;
|
| 141 |
-
}
|
| 142 |
-
@keyframes poison-pulse {
|
| 143 |
-
0%,100%{ filter:drop-shadow(0 0 10px rgba(255,62,91,.6)); }
|
| 144 |
-
50% { filter:drop-shadow(0 0 28px rgba(255,62,91,1)); }
|
| 145 |
-
}
|
| 146 |
-
@keyframes myco-recoil {
|
| 147 |
-
0% { transform:translateX(0); }
|
| 148 |
-
30% { transform:translateX(-1.2rem); }
|
| 149 |
-
60% { transform:translateX(.4rem); }
|
| 150 |
-
100%{ transform:translateX(0); }
|
| 151 |
-
}
|
| 152 |
-
|
| 153 |
-
/* ── STATE: game over ── */
|
| 154 |
-
.state-game-over .forest-scene,
|
| 155 |
-
.state-game-over {
|
| 156 |
-
animation: screen-shake .45s ease both;
|
| 157 |
-
background:
|
| 158 |
-
radial-gradient(circle at 50% 72%, rgba(255,62,91,.35), transparent 8rem),
|
| 159 |
-
linear-gradient(180deg,#3b0a0a 0%,#6b1a1a 54%,#2a0a0a 100%) !important;
|
| 160 |
-
}
|
| 161 |
-
.state-game-over .mushroom-sprite {
|
| 162 |
-
animation: shroom-explode .6s ease both;
|
| 163 |
-
filter: drop-shadow(0 0 24px rgba(255,62,91,1));
|
| 164 |
-
}
|
| 165 |
-
.state-game-over .myco-player {
|
| 166 |
-
animation: myco-fall .6s ease both;
|
| 167 |
-
}
|
| 168 |
-
@keyframes screen-shake {
|
| 169 |
-
0%,100%{ transform:translateX(0); }
|
| 170 |
-
20% { transform:translateX(-8px); }
|
| 171 |
-
40% { transform:translateX(8px); }
|
| 172 |
-
60% { transform:translateX(-5px); }
|
| 173 |
-
80% { transform:translateX(5px); }
|
| 174 |
-
}
|
| 175 |
-
@keyframes shroom-explode {
|
| 176 |
-
0% { transform:scale(1) rotate(0); opacity:1; }
|
| 177 |
-
50% { transform:scale(1.8) rotate(20deg); opacity:.8; }
|
| 178 |
-
100%{ transform:scale(0) rotate(90deg); opacity:0; }
|
| 179 |
-
}
|
| 180 |
-
@keyframes myco-fall {
|
| 181 |
-
0% { transform:rotate(0) translateY(0); opacity:1; }
|
| 182 |
-
100%{ transform:rotate(-90deg) translateY(2rem); opacity:.4; }
|
| 183 |
-
}
|
| 184 |
-
|
| 185 |
-
/* ── Rarity glows ── */
|
| 186 |
-
.rarity-rare .mushroom-sprite { filter:drop-shadow(0 0 18px rgba(138,220,255,.9)); }
|
| 187 |
-
.rarity-legendary .mushroom-sprite{ filter:drop-shadow(0 0 28px rgba(255,215,0,1)); animation:legendary-pulse 1s ease-in-out infinite 0.7s; }
|
| 188 |
-
@keyframes legendary-pulse {
|
| 189 |
-
0%,100%{ filter:drop-shadow(0 0 18px rgba(255,215,0,.7)); transform:scale(1); }
|
| 190 |
-
50% { filter:drop-shadow(0 0 36px rgba(255,215,0,1)); transform:scale(1.1); }
|
| 191 |
-
}
|
| 192 |
-
|
| 193 |
-
/* ── Mini map grid ── */
|
| 194 |
-
.forest-map {
|
| 195 |
-
display:grid; grid-template-columns:repeat(3,1fr);
|
| 196 |
-
gap:.3rem; margin:.4rem auto .6rem;
|
| 197 |
-
max-width:9rem; padding:.4rem;
|
| 198 |
-
border:1px solid rgba(255,255,255,.18); border-radius:14px;
|
| 199 |
-
background:rgba(13,38,28,.58);
|
| 200 |
-
}
|
| 201 |
-
.map-tile {
|
| 202 |
-
display:grid; min-height:1.7rem; place-items:center;
|
| 203 |
-
border-radius:8px; background:rgba(255,255,255,.1); font-weight:900;
|
| 204 |
-
}
|
| 205 |
-
.map-tile.active { background:rgba(255,224,138,.92); color:#3d2c00!important; }
|
| 206 |
-
|
| 207 |
-
/* ── Caption ── */
|
| 208 |
-
.scene-caption {
|
| 209 |
-
display:flex; justify-content:space-between; gap:.6rem;
|
| 210 |
-
padding:.55rem .8rem; border-radius:14px;
|
| 211 |
-
background:rgba(13,38,28,.68); backdrop-filter:blur(6px);
|
| 212 |
-
align-items:center;
|
| 213 |
-
}
|
| 214 |
-
.scene-caption span { color:#d9f4d4; }
|
| 215 |
-
|
| 216 |
-
/* ── Score burst ── */
|
| 217 |
-
.score-burst {
|
| 218 |
-
margin:.65rem auto 0; max-width:520px;
|
| 219 |
-
padding:.5rem .85rem; border-radius:999px;
|
| 220 |
-
background:rgba(255,224,112,.18); border:1px solid rgba(255,244,190,.36);
|
| 221 |
-
color:#fff7d6; font-weight:900; text-align:center;
|
| 222 |
-
animation:score-pop .56s ease both;
|
| 223 |
-
}
|
| 224 |
-
@keyframes score-pop {
|
| 225 |
-
0%{transform:scale(.92);opacity:0;}
|
| 226 |
-
45%{transform:scale(1.06);opacity:1;}
|
| 227 |
-
100%{transform:scale(1);opacity:1;}
|
| 228 |
-
}
|
| 229 |
-
.state-game-over .score-burst {
|
| 230 |
-
background:rgba(130,0,30,.45); border-color:rgba(255,120,140,.55);
|
| 231 |
-
}
|
| 232 |
-
|
| 233 |
-
/* ── Story moment ── */
|
| 234 |
-
.story-moment {
|
| 235 |
-
margin:.75rem auto 0; max-width:760px;
|
| 236 |
-
padding:.8rem 1rem; border:1px solid rgba(255,234,166,.3);
|
| 237 |
-
border-radius:18px; background:rgba(25,18,45,.72);
|
| 238 |
-
color:#fff7d6; box-shadow:0 0 24px rgba(165,111,255,.22);
|
| 239 |
-
}
|
| 240 |
-
.story-moment strong,.story-moment span { display:block; }
|
| 241 |
-
.story-moment span { margin-top:.2rem; color:#f6e7b2; }
|
| 242 |
-
|
| 243 |
-
/* ── Suppress Gradio white flash ── */
|
| 244 |
-
.generating .forest-scene, .pending .forest-scene { opacity:.88!important; }
|
| 245 |
-
.progress-level,.progress-level-inner { display:none!important; }
|
| 246 |
-
div.gradio-html { background:transparent!important; }
|
| 247 |
-
.wrap.default.pending,.wrap.default.generating { background:transparent!important; }
|
| 248 |
-
</style>
|
| 249 |
-
"""
|
| 250 |
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
<div class="myco-hero">
|
| 254 |
-
<div class="myco-mascot">🍄</div>
|
| 255 |
-
<div>
|
| 256 |
-
<p class="eyebrow">Tiny AI forest game</p>
|
| 257 |
-
<h1>Myco</h1>
|
| 258 |
-
<p>Follow an AI mushroom companion through a glowing forest. Every discovery starts with Myco's intuition.</p>
|
| 259 |
-
</div>
|
| 260 |
-
</div>
|
| 261 |
-
"""
|
| 262 |
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
<span class="home-star star-one">✨</span>
|
| 269 |
-
<span class="home-star star-two">✦</span>
|
| 270 |
-
<span class="home-star star-three">✧</span>
|
| 271 |
-
</div>
|
| 272 |
-
<div class="home-forest-floor">
|
| 273 |
-
<span class="home-tree left">🌲</span>
|
| 274 |
-
<span class="home-grass grass-left">🌿</span>
|
| 275 |
-
<span class="home-myco" title="Myco">🍄</span>
|
| 276 |
-
<span class="home-brown-mushroom" title="Brown mushroom">🍄🟫</span>
|
| 277 |
-
<span class="home-grass grass-right">🍃</span>
|
| 278 |
-
<span class="home-tree right">🌳</span>
|
| 279 |
-
<span class="home-moon">🌖</span>
|
| 280 |
-
</div>
|
| 281 |
-
<p><strong>The Milky Way is awake.</strong> Myco hears a brown mushroom whispering your first clue.</p>
|
| 282 |
-
<span class="home-whisper">"Tap Play. Let's find what the forest is hiding."</span>
|
| 283 |
-
</div>
|
| 284 |
"""
|
| 285 |
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
"""
|
| 299 |
|
| 300 |
-
def forest_scene(
|
| 301 |
-
current: MushroomState | None,
|
| 302 |
-
collection: CollectionState | None,
|
| 303 |
-
position: tuple[int, int] | None = None,
|
| 304 |
-
) -> str:
|
| 305 |
-
count = len(_safe_collection(collection))
|
| 306 |
-
area = current_area(count)
|
| 307 |
-
pos_x, pos_y = _safe_position(position)
|
| 308 |
-
pos_cls = f"pos-{pos_x}-{pos_y}"
|
| 309 |
|
|
|
|
| 310 |
if current is None:
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
<
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
if position is None:
|
| 390 |
-
return
|
| 391 |
x, y = position
|
| 392 |
return (max(0, min(2, int(x))), max(0, min(2, int(y))))
|
| 393 |
|
| 394 |
-
def _movement_tiles(active_x, active_y):
|
| 395 |
-
tiles = []
|
| 396 |
-
for y in range(3):
|
| 397 |
-
for x in range(3):
|
| 398 |
-
active = " active" if (x, y) == (active_x, active_y) else ""
|
| 399 |
-
icon = "🍄" if active else "·"
|
| 400 |
-
tiles.append(f'<span class="map-tile{active}">{icon}</span>')
|
| 401 |
-
return "".join(tiles)
|
| 402 |
-
|
| 403 |
-
def _scene_state_class(current):
|
| 404 |
-
if current.get("game_over") == "Yes":
|
| 405 |
-
return "state-game-over"
|
| 406 |
-
if current.get("picked") == "Yes":
|
| 407 |
-
return "state-picked"
|
| 408 |
-
if current.get("poison") == "Yes":
|
| 409 |
-
return "state-danger"
|
| 410 |
-
return "state-discovered"
|
| 411 |
-
|
| 412 |
-
def game_status_markdown(
|
| 413 |
-
current: MushroomState | None,
|
| 414 |
-
collection: CollectionState | None,
|
| 415 |
-
position: tuple[int, int] | None = None,
|
| 416 |
-
) -> str:
|
| 417 |
-
count = len(_safe_collection(collection))
|
| 418 |
-
pos_x, pos_y = _safe_position(position)
|
| 419 |
-
active_mystery = next_mystery(count)
|
| 420 |
-
mystery_line = _mystery_goal_line(active_mystery, count)
|
| 421 |
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
)
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
)
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
discovery, objective, score_line, bloom_line,
|
| 460 |
-
event, clue_line, thread_line, mystery_line, ai_job,
|
| 461 |
-
))
|
| 462 |
-
|
| 463 |
-
def _mystery_bloom_meter(count):
|
| 464 |
-
filled = max(0, min(5, count))
|
| 465 |
-
petals = "".join("🌕" if i < filled else "🌑" for i in range(5))
|
| 466 |
-
if filled >= 5:
|
| 467 |
-
return f"{petals} Impossible Bloom visible"
|
| 468 |
-
return f"{petals} {5-filled} clue-whispers remain"
|
| 469 |
-
|
| 470 |
-
def mushroom_card(
|
| 471 |
-
mushroom: Mushroom | None,
|
| 472 |
-
clue: str | None = None,
|
| 473 |
-
secret: str | None = None,
|
| 474 |
-
state: MushroomState | None = None,
|
| 475 |
-
) -> str:
|
| 476 |
-
if mushroom is None:
|
| 477 |
-
return "### 🌲 Forest Clearing\nClick **Search Clearing** — watch Myco run toward the mushroom!"
|
| 478 |
-
s = state or {}
|
| 479 |
-
poison = mushroom.name in POISONOUS_MUSHROOM_NAMES
|
| 480 |
-
poison_warning = "\n> ⚠️ **Myco senses danger — do NOT pick this without studying first!**" if poison else ""
|
| 481 |
-
return "\n".join((
|
| 482 |
-
f"### 🍄🟫 {mushroom.name}",
|
| 483 |
-
f"**{mushroom.rarity} discovery** · found near **{mushroom.habitat}**",
|
| 484 |
-
f"> {mushroom.lore}",
|
| 485 |
-
poison_warning,
|
| 486 |
-
"",
|
| 487 |
-
f"* **Edible:** {mushroom.edible}",
|
| 488 |
-
f"* **Magic:** {mushroom.magic}",
|
| 489 |
-
f"* **Danger:** {mushroom.danger}",
|
| 490 |
-
f"* **Value:** {_mushroom_value_label(mushroom.rarity)}",
|
| 491 |
-
f"* **Poison risk:** {_poison_risk_label(mushroom.name)}",
|
| 492 |
-
f"* **Event:** {s.get('event_emoji','✨')} {s.get('event_title','Strange Omen')}",
|
| 493 |
-
f"* **Mystery:** {s.get('mystery_title','The Wrong Memory')}",
|
| 494 |
-
f"* **Clue:** {clue or 'Study with Myco to reveal the clue.'}",
|
| 495 |
-
f"* **Secret:** {secret or 'Collect it to see how the MycoDex reacts.'}",
|
| 496 |
-
))
|
| 497 |
-
|
| 498 |
-
def _mushroom_value_label(rarity):
|
| 499 |
-
if rarity == "Legendary": return "100 spores"
|
| 500 |
-
if rarity == "Rare": return "35 spores + special discovery"
|
| 501 |
-
return "10 spores"
|
| 502 |
-
|
| 503 |
-
def _poison_risk_label(name):
|
| 504 |
-
return "⚠️ POISONOUS — Game Over if picked!" if name in POISONOUS_MUSHROOM_NAMES else "No obvious poison"
|
| 505 |
-
|
| 506 |
-
def world_map_markdown(collection: CollectionState | None) -> str:
|
| 507 |
-
count = len(_safe_collection(collection))
|
| 508 |
-
cards = []
|
| 509 |
-
for area in AREA_UNLOCKS:
|
| 510 |
-
unlocked = count >= area.required_discoveries
|
| 511 |
-
state = "unlocked" if unlocked else "locked"
|
| 512 |
-
badge = "✅ Unlocked" if unlocked else f"🔒 {area.required_discoveries - count} away"
|
| 513 |
-
cards.append("\n".join((
|
| 514 |
-
f'<div class="map-card {state}">',
|
| 515 |
-
' <div class="map-card-top">',
|
| 516 |
-
f' <strong>{escape(area.name)}</strong><span>{badge}</span>',
|
| 517 |
-
' </div>',
|
| 518 |
-
f' <p>{escape(area.description)}</p>',
|
| 519 |
-
'</div>',
|
| 520 |
-
)))
|
| 521 |
-
secrets = tuple(_secret_badge(t.name, count >= t.required_discoveries) for t in revealed_mysteries(count))
|
| 522 |
-
if not secrets:
|
| 523 |
-
secrets = (_secret_badge("First Spore", False),)
|
| 524 |
-
return "\n".join((
|
| 525 |
-
'<div class="world-map-panel">',
|
| 526 |
-
' <div class="world-map-header"><p class="eyebrow">World Map</p>',
|
| 527 |
-
' <h2>Forest Atlas</h2>',
|
| 528 |
-
f' <p>{count} discoveries logged.</p></div>',
|
| 529 |
-
f' <div class="achievement-row">{"".join(secrets)}</div>',
|
| 530 |
-
f' <div class="map-grid">{"".join(cards)}</div>',
|
| 531 |
-
'</div>',
|
| 532 |
-
))
|
| 533 |
-
|
| 534 |
-
def _secret_badge(label, unlocked):
|
| 535 |
-
state = "earned" if unlocked else "pending"
|
| 536 |
-
icon = "🔎" if unlocked else "◇"
|
| 537 |
-
return f'<span class="achievement {state}">{icon} {escape(label)}</span>'
|
| 538 |
-
|
| 539 |
-
def dex_markdown(collection: CollectionState | None) -> str:
|
| 540 |
-
current_collection = _safe_collection(collection)
|
| 541 |
-
if not current_collection:
|
| 542 |
-
return EMPTY_DEX
|
| 543 |
-
rows = ["### 📖 MycoDex", ""]
|
| 544 |
-
for entry in current_collection:
|
| 545 |
-
rows.append(
|
| 546 |
-
f"* 🍄 **{entry['name']}** — {entry['rarity']} — {entry['habitat']} "
|
| 547 |
-
f"— clue: {entry.get('clue','not studied yet')}"
|
| 548 |
)
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
|
|
|
|
|
|
| 592 |
|
|
|
|
| 1 |
+
"""Gradio game UI for Myco — LLM-powered forest adventure."""
|
| 2 |
|
| 3 |
+
from typing import Any, cast
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
from game.engine import (
|
| 7 |
+
collect_current, discover_mushroom, eat_current,
|
| 8 |
+
follow_whisper, hf_companion_status, myco_reply,
|
| 9 |
+
pick_current, study_current,
|
| 10 |
)
|
| 11 |
+
from game.state import welcome_history
|
| 12 |
from models.mushroom import Mushroom
|
| 13 |
+
from ui.renderers import (
|
| 14 |
+
dex_markdown,
|
| 15 |
+
game_status_markdown, hero_markdown, home_forest_markdown,
|
| 16 |
+
mushroom_card, play_hook_markdown, progress_markdown,
|
| 17 |
+
world_map_markdown,
|
| 18 |
+
)
|
| 19 |
|
| 20 |
+
EMPTY_COLLECTION = None
|
| 21 |
+
START_POSITION = (1, 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
HOME_MARKDOWN = """
|
| 24 |
+
## Welcome to the Myco forest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
Myco is a tiny AI-powered forest game. Open **🎮 Play** to search for mushrooms,
|
| 27 |
+
visit **📖 MycoDex** to review your collection, and use **🧭 Guide** for help.
|
| 28 |
+
|
| 29 |
+
**Goal:** solve one mystery — why does Myco remember a forest that vanished before it was born?
|
| 30 |
+
Follow clues until the Impossible Mushroom blooms inside the MycoDex.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
"""
|
| 32 |
|
| 33 |
+
GUIDE_MARKDOWN = """
|
| 34 |
+
## How to play
|
| 35 |
+
|
| 36 |
+
1. Use **⬆️⬇️⬅️➡️** or **WASD** keys to walk through the forest grid.
|
| 37 |
+
2. Moving automatically triggers a new clearing encounter! Myco will immediately report what it senses.
|
| 38 |
+
3. Choose your tactical action:
|
| 39 |
+
- **🔍 Study (Q)** — Myco examines the mushroom and updates its magic field.
|
| 40 |
+
- **🍄 Pick (E)** — grab it as a game item. Poisonous picks = Game Over!
|
| 41 |
+
- **📖 Collect (C)** — add it to the MycoDex for safe keeping and score.
|
| 42 |
+
- **🌌 Follow Whisper** — reveal mystery fragments. Risky on unstudied mushrooms!
|
| 43 |
+
- **🍽️ Eat?** — Myco will stop you every time (for good reason).
|
| 44 |
+
4. **Ask Myco** anything in the chat — it knows the forest, the mystery, and the mushrooms.
|
| 45 |
+
|
| 46 |
+
**Controls:** W/A/S/D or Arrows = Move · Q = Study · E = Pick · C = Collect
|
| 47 |
+
|
| 48 |
+
**Scoring:** Common = 10 spores · Rare = 35 · Legendary = 100 · Poison = −25 + Game Over
|
| 49 |
"""
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
def mushroom_card_from_state(current):
|
| 53 |
if current is None:
|
| 54 |
+
return mushroom_card(None)
|
| 55 |
+
return mushroom_card(
|
| 56 |
+
Mushroom.from_dict(current),
|
| 57 |
+
current.get("clue"),
|
| 58 |
+
current.get("secret"),
|
| 59 |
+
current,
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def forest_scene(current, collection, position):
|
| 64 |
+
"""Renders an interactive, visually responsive CSS map representing the real game layout."""
|
| 65 |
+
px, py = _safe_pos(position)
|
| 66 |
+
|
| 67 |
+
html = """
|
| 68 |
+
<div style="
|
| 69 |
+
display: flex;
|
| 70 |
+
flex-direction: column;
|
| 71 |
+
align-items: center;
|
| 72 |
+
justify-content: center;
|
| 73 |
+
background: #111122;
|
| 74 |
+
padding: 20px;
|
| 75 |
+
border-radius: 12px;
|
| 76 |
+
border: 2px solid #2e2e4f;
|
| 77 |
+
margin-bottom: 15px;
|
| 78 |
+
">
|
| 79 |
+
<div style="color: #8a8aaf; font-weight: bold; margin-bottom: 12px; font-size: 13px; letter-spacing: 2px; font-family: monospace;">
|
| 80 |
+
🌲 MYCO FOREST VIEWPORT (3 × 3) 🌲
|
| 81 |
+
</div>
|
| 82 |
+
<div style="
|
| 83 |
+
display: grid;
|
| 84 |
+
grid-template-columns: repeat(3, 85px);
|
| 85 |
+
grid-template-rows: repeat(3, 85px);
|
| 86 |
+
gap: 10px;
|
| 87 |
+
">
|
| 88 |
+
"""
|
| 89 |
+
|
| 90 |
+
for y in range(3):
|
| 91 |
+
for x in range(3):
|
| 92 |
+
is_player = (x == px and y == py)
|
| 93 |
+
|
| 94 |
+
tile_style = """
|
| 95 |
+
width: 85px;
|
| 96 |
+
height: 85px;
|
| 97 |
+
background: #1b263b;
|
| 98 |
+
border-radius: 8px;
|
| 99 |
+
display: flex;
|
| 100 |
+
align-items: center;
|
| 101 |
+
justify-content: center;
|
| 102 |
+
font-size: 32px;
|
| 103 |
+
position: relative;
|
| 104 |
+
box-shadow: inset 0 0 12px rgba(0,0,0,0.6);
|
| 105 |
+
"""
|
| 106 |
+
|
| 107 |
+
if is_player:
|
| 108 |
+
tile_style += "border: 3px solid #00b4d8; background: #22333b; box-shadow: 0 0 18px rgba(0,180,216,0.4);"
|
| 109 |
+
cell_content = "🧙♂️"
|
| 110 |
+
else:
|
| 111 |
+
cell_content = "🌲"
|
| 112 |
+
|
| 113 |
+
coord_label = f'<span style="position: absolute; bottom: 4px; right: 6px; font-size: 9px; color: #415a77; font-family: monospace;">{x},{y}</span>'
|
| 114 |
+
html += f'<div style="{tile_style}">{cell_content}{coord_label}</div>'
|
| 115 |
+
|
| 116 |
+
html += f"""
|
| 117 |
+
</div>
|
| 118 |
+
<div style="margin-top: 14px; color: #00b4d8; font-size: 12px; font-family: monospace; letter-spacing: 1px;">
|
| 119 |
+
Coordinates: [X: {px}, Y: {py}]
|
| 120 |
+
</div>
|
| 121 |
+
</div>
|
| 122 |
+
"""
|
| 123 |
+
return html
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def search_forest(collection, position):
|
| 127 |
+
mushroom, current, history = discover_mushroom(collection)
|
| 128 |
+
pos = _safe_pos(position)
|
| 129 |
+
return (
|
| 130 |
+
forest_scene(current, collection, pos),
|
| 131 |
+
game_status_markdown(current, collection, pos),
|
| 132 |
+
mushroom_card_from_state(current),
|
| 133 |
+
current,
|
| 134 |
+
history,
|
| 135 |
+
dex_markdown(collection),
|
| 136 |
+
progress_markdown(collection),
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def ask_companion(message, history, current, collection, position):
|
| 141 |
+
return myco_reply(message, history, current, collection, position)
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def collect_discovery(current, collection, history, position):
|
| 145 |
+
updated_coll, updated_hist = collect_current(current, collection, history)
|
| 146 |
+
pos = _safe_pos(position)
|
| 147 |
+
return (
|
| 148 |
+
updated_coll,
|
| 149 |
+
forest_scene(current, updated_coll, pos),
|
| 150 |
+
game_status_markdown(current, updated_coll, pos),
|
| 151 |
+
dex_markdown(updated_coll),
|
| 152 |
+
world_map_markdown(updated_coll),
|
| 153 |
+
updated_hist,
|
| 154 |
+
progress_markdown(updated_coll),
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
def pick_discovery(current, collection, history, position):
|
| 159 |
+
updated_coll, updated_current, updated_hist = pick_current(current, collection, history)
|
| 160 |
+
pos = _safe_pos(position)
|
| 161 |
+
return (
|
| 162 |
+
updated_coll,
|
| 163 |
+
updated_current,
|
| 164 |
+
forest_scene(updated_current, updated_coll, pos),
|
| 165 |
+
game_status_markdown(updated_current, updated_coll, pos),
|
| 166 |
+
mushroom_card_from_state(updated_current),
|
| 167 |
+
dex_markdown(updated_coll),
|
| 168 |
+
world_map_markdown(updated_coll),
|
| 169 |
+
updated_hist,
|
| 170 |
+
progress_markdown(updated_coll),
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def follow_story_whisper(current, collection, history, position):
|
| 175 |
+
updated_current, updated_hist = follow_whisper(current, collection, history)
|
| 176 |
+
pos = _safe_pos(position)
|
| 177 |
+
return (
|
| 178 |
+
updated_current,
|
| 179 |
+
forest_scene(updated_current, collection, pos),
|
| 180 |
+
game_status_markdown(updated_current, collection, pos),
|
| 181 |
+
mushroom_card_from_state(updated_current),
|
| 182 |
+
updated_hist,
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
def study_discovery(current, history, collection, position):
|
| 187 |
+
updated_current, updated_hist = study_current(current, history)
|
| 188 |
+
pos = _safe_pos(position)
|
| 189 |
+
return (
|
| 190 |
+
updated_current,
|
| 191 |
+
forest_scene(updated_current, collection, pos),
|
| 192 |
+
game_status_markdown(updated_current, collection, pos),
|
| 193 |
+
mushroom_card_from_state(updated_current),
|
| 194 |
+
updated_hist,
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
def move_player(position, direction, collection):
|
| 199 |
+
"""Calculates movement coordinates and instantly triggers a continuous gameplay search event loop."""
|
| 200 |
+
x, y = _safe_pos(position)
|
| 201 |
+
if direction == "north": y -= 1
|
| 202 |
+
elif direction == "south": y += 1
|
| 203 |
+
elif direction == "west": x -= 1
|
| 204 |
+
elif direction == "east": x += 1
|
| 205 |
+
pos = _safe_pos((x, y))
|
| 206 |
+
|
| 207 |
+
# Real Game Loop Mechanics: Moving handles exploration encounters automatically
|
| 208 |
+
mushroom, current, history = discover_mushroom(collection)
|
| 209 |
+
return (
|
| 210 |
+
pos,
|
| 211 |
+
forest_scene(current, collection, pos),
|
| 212 |
+
game_status_markdown(current, collection, pos),
|
| 213 |
+
mushroom_card_from_state(current),
|
| 214 |
+
current,
|
| 215 |
+
history,
|
| 216 |
+
dex_markdown(collection),
|
| 217 |
+
progress_markdown(collection),
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
def block_unsafe_eating(current, history):
|
| 222 |
+
return eat_current(current, history)
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
def open_play_tab():
|
| 226 |
+
return gr.update(selected="play")
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
def _safe_pos(position):
|
| 230 |
if position is None:
|
| 231 |
+
return START_POSITION
|
| 232 |
x, y = position
|
| 233 |
return (max(0, min(2, int(x))), max(0, min(2, int(y))))
|
| 234 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
+
def build_app():
|
| 237 |
+
with gr.Blocks(title="Myco — AI Forest Game", theme=gr.themes.Soft()) as demo:
|
| 238 |
+
gr.HTML("""
|
| 239 |
+
<style>
|
| 240 |
+
.gradio-container { background: #1a1a2e !important; }
|
| 241 |
+
.gr-html, iframe, .html-container {
|
| 242 |
+
transition: none !important;
|
| 243 |
+
animation: none !important;
|
| 244 |
+
}
|
| 245 |
+
/* Completely freeze component flickering layout shifts */
|
| 246 |
+
* {
|
| 247 |
+
transition: none !important;
|
| 248 |
+
}
|
| 249 |
+
.pending, .generating {
|
| 250 |
+
opacity: 1.0 !important;
|
| 251 |
+
background: transparent !important;
|
| 252 |
+
animation: none !important;
|
| 253 |
+
border: none !important;
|
| 254 |
+
box-shadow: none !important;
|
| 255 |
+
}
|
| 256 |
+
/* Completely clear default overlay panels */
|
| 257 |
+
.progress-level, .progress-level-inner {
|
| 258 |
+
display: none !important;
|
| 259 |
+
}
|
| 260 |
+
</style>
|
| 261 |
+
|
| 262 |
+
<script>
|
| 263 |
+
document.addEventListener('keydown', function(event) {
|
| 264 |
+
// Keep keyboard hotkeys completely quiet if typing a query in the chatbox input
|
| 265 |
+
if (event.target.nodeName === 'INPUT' || event.target.nodeName === 'TEXTAREA') return;
|
| 266 |
+
|
| 267 |
+
const keyMap = {
|
| 268 |
+
'w': 'btn-north', 'W': 'btn-north', 'ArrowUp': 'btn-north',
|
| 269 |
+
'a': 'btn-west', 'A': 'btn-west', 'ArrowLeft': 'btn-west',
|
| 270 |
+
's': 'btn-south', 'S': 'btn-south', 'ArrowDown': 'btn-south',
|
| 271 |
+
'd': 'btn-east', 'D': 'btn-east', 'ArrowRight': 'btn-east',
|
| 272 |
+
'e': 'btn-pick', 'E': 'btn-pick',
|
| 273 |
+
'q': 'btn-study', 'Q': 'btn-study',
|
| 274 |
+
'c': 'btn-collect', 'C': 'btn-collect'
|
| 275 |
+
};
|
| 276 |
+
|
| 277 |
+
const targetId = keyMap[event.key];
|
| 278 |
+
if (targetId) {
|
| 279 |
+
const btn = document.getElementById(targetId);
|
| 280 |
+
if (btn) btn.click();
|
| 281 |
+
}
|
| 282 |
+
});
|
| 283 |
+
</script>
|
| 284 |
+
""")
|
| 285 |
+
|
| 286 |
+
# ── State ──────────────────────────────────────────────────────────
|
| 287 |
+
current_mushroom = gr.State(None)
|
| 288 |
+
collection = gr.State([])
|
| 289 |
+
player_position = gr.State(START_POSITION)
|
| 290 |
+
|
| 291 |
+
# ── Hero header removed here ───────────────────────────────────────
|
| 292 |
+
|
| 293 |
+
with gr.Tabs(selected="home") as tabs:
|
| 294 |
+
|
| 295 |
+
# ── HOME ───────────────────────────────────────────────────────
|
| 296 |
+
with gr.Tab("🏠 Home", id="home"):
|
| 297 |
+
gr.Markdown(HOME_MARKDOWN)
|
| 298 |
+
gr.HTML(home_forest_markdown())
|
| 299 |
+
start_play_button = gr.Button(
|
| 300 |
+
"🍄 Enter the Forest", variant="primary", size="lg"
|
| 301 |
+
)
|
| 302 |
+
gr.Markdown(f"*{hf_companion_status()}*")
|
| 303 |
+
|
| 304 |
+
# ── PLAY ───────────────────────────────────────────────────────
|
| 305 |
+
with gr.Tab("🎮 Play", id="play"):
|
| 306 |
+
# TOP: Discovery card + action buttons + chat
|
| 307 |
+
with gr.Row(equal_height=True):
|
| 308 |
+
with gr.Column(scale=2):
|
| 309 |
+
gr.Markdown("### 🍄 Current Discovery")
|
| 310 |
+
discovery = gr.Markdown(mushroom_card(None))
|
| 311 |
+
with gr.Row():
|
| 312 |
+
study_button = gr.Button("🔍 Study", variant="secondary", elem_id="btn-study")
|
| 313 |
+
collect_button = gr.Button("📖 Collect", variant="secondary", elem_id="btn-collect")
|
| 314 |
+
with gr.Row():
|
| 315 |
+
whisper_button = gr.Button("🌌 Follow Whisper", variant="secondary", elem_id="btn-whisper")
|
| 316 |
+
eat_button = gr.Button("🍽️ Eat?", variant="stop", elem_id="btn-eat")
|
| 317 |
+
|
| 318 |
+
with gr.Column(scale=3):
|
| 319 |
+
gr.Markdown("### 💬 Myco Chat")
|
| 320 |
+
chat = gr.Chatbot(
|
| 321 |
+
value=cast(Any, welcome_history()),
|
| 322 |
+
label="Myco",
|
| 323 |
+
height=400,
|
| 324 |
+
show_label=False,
|
| 325 |
+
)
|
| 326 |
+
with gr.Row():
|
| 327 |
+
prompt = gr.Textbox(
|
| 328 |
+
placeholder="Ask Myco anything about the forest...",
|
| 329 |
+
show_label=False,
|
| 330 |
+
scale=4,
|
| 331 |
+
lines=1,
|
| 332 |
+
)
|
| 333 |
+
ask_button = gr.Button("Ask 🍄", variant="primary", scale=1)
|
| 334 |
+
|
| 335 |
+
# MIDDLE: Quest log
|
| 336 |
+
status = gr.Markdown(
|
| 337 |
+
game_status_markdown(None, EMPTY_COLLECTION, START_POSITION)
|
| 338 |
+
)
|
| 339 |
+
|
| 340 |
+
# BOTTOM: Forest Map + Controls
|
| 341 |
+
with gr.Row():
|
| 342 |
+
with gr.Column(scale=2):
|
| 343 |
+
scene = gr.HTML(
|
| 344 |
+
forest_scene(None, EMPTY_COLLECTION, START_POSITION),
|
| 345 |
+
elem_id="forest-scene",
|
| 346 |
+
)
|
| 347 |
+
with gr.Column(scale=1):
|
| 348 |
+
search_button = gr.Button("🌿 Inspect Current Spot", variant="secondary")
|
| 349 |
+
gr.Markdown("**Move Navigator (WASD / Arrows)**")
|
| 350 |
+
with gr.Row():
|
| 351 |
+
gr.HTML("<div></div>")
|
| 352 |
+
north_button = gr.Button("⬆️", size="sm", elem_id="btn-north")
|
| 353 |
+
gr.HTML("<div></div>")
|
| 354 |
+
with gr.Row():
|
| 355 |
+
west_button = gr.Button("⬅️", size="sm", elem_id="btn-west")
|
| 356 |
+
pick_button = gr.Button("🍄 Pick", variant="primary", size="sm", elem_id="btn-pick")
|
| 357 |
+
east_button = gr.Button("➡️", size="sm", elem_id="btn-east")
|
| 358 |
+
with gr.Row():
|
| 359 |
+
gr.HTML("<div></div>")
|
| 360 |
+
south_button = gr.Button("⬇️", size="sm", elem_id="btn-south")
|
| 361 |
+
gr.HTML("<div></div>")
|
| 362 |
+
|
| 363 |
+
# ── MYCODEX ────────────────────────────────────────────────────
|
| 364 |
+
with gr.Tab("📖 MycoDex"):
|
| 365 |
+
with gr.Row():
|
| 366 |
+
progress = gr.Markdown(progress_markdown(EMPTY_COLLECTION))
|
| 367 |
+
dex = gr.Markdown(dex_markdown(EMPTY_COLLECTION))
|
| 368 |
+
|
| 369 |
+
# ── WORLD MAP ──────────────────────────────────────────────────
|
| 370 |
+
with gr.Tab("🗺️ World Map"):
|
| 371 |
+
world_map = gr.HTML(world_map_markdown(EMPTY_COLLECTION))
|
| 372 |
+
|
| 373 |
+
# ── GUIDE ──────────────────────────────────────────────────────
|
| 374 |
+
with gr.Tab("🧭 Guide"):
|
| 375 |
+
gr.Markdown(GUIDE_MARKDOWN)
|
| 376 |
+
|
| 377 |
+
# ── Event wiring ───────────────────────────────────────────────────
|
| 378 |
+
start_play_button.click(open_play_tab, outputs=[tabs], show_progress="hidden")
|
| 379 |
+
|
| 380 |
+
search_button.click(
|
| 381 |
+
search_forest,
|
| 382 |
+
inputs=[collection, player_position],
|
| 383 |
+
outputs=[scene, status, discovery, current_mushroom, chat, dex, progress],
|
| 384 |
+
show_progress="hidden",
|
| 385 |
)
|
| 386 |
+
|
| 387 |
+
ask_button.click(
|
| 388 |
+
ask_companion,
|
| 389 |
+
inputs=[prompt, chat, current_mushroom, collection, player_position],
|
| 390 |
+
outputs=[prompt, chat],
|
| 391 |
+
show_progress="hidden",
|
| 392 |
)
|
| 393 |
+
prompt.submit(
|
| 394 |
+
ask_companion,
|
| 395 |
+
inputs=[prompt, chat, current_mushroom, collection, player_position],
|
| 396 |
+
outputs=[prompt, chat],
|
| 397 |
+
show_progress="hidden",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 398 |
)
|
| 399 |
+
|
| 400 |
+
for direction, button in [
|
| 401 |
+
("north", north_button), ("south", south_button),
|
| 402 |
+
("west", west_button), ("east", east_button),
|
| 403 |
+
]:
|
| 404 |
+
button.click(
|
| 405 |
+
move_player,
|
| 406 |
+
inputs=[player_position, gr.State(direction), collection],
|
| 407 |
+
outputs=[player_position, scene, status, discovery, current_mushroom, chat, dex, progress],
|
| 408 |
+
show_progress="hidden",
|
| 409 |
+
)
|
| 410 |
+
|
| 411 |
+
study_button.click(
|
| 412 |
+
study_discovery,
|
| 413 |
+
inputs=[current_mushroom, chat, collection, player_position],
|
| 414 |
+
outputs=[current_mushroom, scene, status, discovery, chat],
|
| 415 |
+
show_progress="hidden",
|
| 416 |
+
)
|
| 417 |
+
collect_button.click(
|
| 418 |
+
collect_discovery,
|
| 419 |
+
inputs=[current_mushroom, collection, chat, player_position],
|
| 420 |
+
outputs=[collection, scene, status, dex, world_map, chat, progress],
|
| 421 |
+
show_progress="hidden",
|
| 422 |
+
)
|
| 423 |
+
pick_button.click(
|
| 424 |
+
pick_discovery,
|
| 425 |
+
inputs=[current_mushroom, collection, chat, player_position],
|
| 426 |
+
outputs=[collection, current_mushroom, scene, status, discovery, dex, world_map, chat, progress],
|
| 427 |
+
show_progress="hidden",
|
| 428 |
+
)
|
| 429 |
+
whisper_button.click(
|
| 430 |
+
follow_story_whisper,
|
| 431 |
+
inputs=[current_mushroom, collection, chat, player_position],
|
| 432 |
+
outputs=[current_mushroom, scene, status, discovery, chat],
|
| 433 |
+
show_progress="hidden",
|
| 434 |
+
)
|
| 435 |
+
eat_button.click(
|
| 436 |
+
block_unsafe_eating,
|
| 437 |
+
inputs=[current_mushroom, chat],
|
| 438 |
+
outputs=[chat],
|
| 439 |
+
show_progress="hidden",
|
| 440 |
+
)
|
| 441 |
+
|
| 442 |
+
demo.queue()
|
| 443 |
+
return demo
|
| 444 |
|