Engine Library (pyAIAgent)
This library contains the core "engine" that powers the Lass agent. It handles low-level interaction with the emulator, ROM data parsing, and LLM communication.
Game State & RAM
pyAIAgent.game.state
Functions
get_active_battle_pokemon(sock)
Get current active player Pokemon in battle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sock
|
Any
|
Socket connection to mGBA |
required |
Source code in pyAIAgent/game/state.py
get_battle_state(sock)
Read battle-related memory.
Returns:
| Type | Description |
|---|---|
dict
|
dict with: |
dict
|
|
dict
|
|
dict
|
|
dict
|
|
Source code in pyAIAgent/game/state.py
get_battle_status_flags(sock)
Read detailed battle status flags (Phase 3). Player: 0xD062-D064 (3 bytes) (Wait, map says D062..D064) Enemy: 0xD067-D069 (3 bytes)
Status bits (approximate): Byte 0: Sleep (0-2), Poison (3), Burn (4), Freeze (5), Paralyze (6) Byte 1-2: Volatile statuses (Confusion, Leech Seed, etc)
Source code in pyAIAgent/game/state.py
get_dialog_text(sock)
Read the tile screen buffer to extract dialog text.
The dialog box in Pokemon Red occupies the bottom 4 rows of the screen. Screen buffer is at 0xC3A0-C507 (360 bytes for 20x18 tiles).
Returns the decoded dialog text, or None if no dialog detected.
Source code in pyAIAgent/game/state.py
get_enemy_pokemon(sock)
Get current enemy Pokemon in battle.
Source code in pyAIAgent/game/state.py
get_event_flags(sock)
Read key story event flags.
Source code in pyAIAgent/game/state.py
get_inventory(sock)
Read player inventory. 0xD31D: Item count 0xD31E + (i2): Item ID 0xD31E + (i2) + 1: Quantity
Source code in pyAIAgent/game/state.py
get_map_state(sock)
Read map-specific state (Phase 2). 0xD365: Last Map ID (for warp handling) 0xD887: Wild Encounter Rate
Source code in pyAIAgent/game/state.py
get_menu_state(sock)
Read extended menu-related memory for tracking menu interactions. OPTIMIZED: Uses batched reads to avoid socket timeouts.
Returns:
| Type | Description |
|---|---|
dict
|
dict with: |
dict
|
|
dict
|
|
dict
|
|
dict
|
|
dict
|
|
dict
|
|
dict
|
|
dict
|
|
dict
|
|
dict
|
|
Source code in pyAIAgent/game/state.py
get_money(sock)
Read player money (BCD encoded, 3 bytes at 0xD347).
Source code in pyAIAgent/game/state.py
get_name_entry_state(sock, menu_state=None)
Detect if player is on name entry screen and get cursor position. OPTIMIZED: Reuses menu_state data if provided, skips expensive tile buffer read.
The name entry screen displays a character grid where the player selects letters. Detection is done by checking menu item count patterns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sock
|
Any
|
Socket connection to mGBA |
required |
menu_state
|
Optional[Dict[str, Any]]
|
Optional pre-read menu state to avoid redundant reads |
None
|
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
dict with cursor info if on name entry, None otherwise |
Source code in pyAIAgent/game/state.py
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 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 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 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 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 | |
get_player_movement_state(sock)
Read player movement state for biking/surfing detection.
RAM Addresses: - D700: Bike speed (0 = not biking, >0 = biking) - C100-C10F: Player sprite data (slot 0, 16 bytes) - C101: Movement status (0=uninit, 1=ready, 2=delayed, 3=moving) - C102: Sprite image index (includes facing direction and animation) - C109: Facing direction (0=down, 4=up, 8=left, C=right)
Surfing detection: When surfing, the player's sprite image changes. The sprite image index at C102 will have distinctive values for surf sprite.
Returns:
| Type | Description |
|---|---|
dict
|
dict with: |
dict
|
|
dict
|
|
dict
|
|
dict
|
|
dict
|
|
dict
|
|
Source code in pyAIAgent/game/state.py
get_rom_path()
Get ROM path from environment variable or default, relative to roms folder
Source code in pyAIAgent/game/state.py
get_sprites(sock)
Read Sprite Table from RAM (0xC100-0xC1FF) to get NPC coordinates. There are 16 slots, each 16 bytes. Offset +0: Picture ID (0 if inactive/hidden) Offset +4: Y Coordinate (Grid Y + 4) Offset +6: X Coordinate (Grid X + 4)
Source code in pyAIAgent/game/state.py
get_stat_modifiers(sock)
Read battle stat modifiers (Phase 3). Player: 0xCD1A-CD20 (7 bytes? Atk, Def, Spd, Spc, Acc, Eva?) Enemy: 0xCD2E-CD34
Values: 7 = Neutral. >7 = Buffed. <7 = Debuffed.
Source code in pyAIAgent/game/state.py
get_text_state(sock)
Read text/dialog related memory flags.
Returns:
| Type | Description |
|---|---|
dict
|
dict with: |
dict
|
|
dict
|
|
dict
|
|
Source code in pyAIAgent/game/state.py
ROM Data & Decoding
pyAIAgent.game.data
Classes
MapLocation
Bases: IntEnum
Maps location IDs to their names
Source code in pyAIAgent/game/data.py
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 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 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 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 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 | |
Functions
decode_pokemon_text(raw_bytes)
Decode Pokemon Red/Blue text encoding to ASCII.
Pokemon Red uses custom tile indices for text: - 0x50 = String terminator - 0x7F = Space - 0x80-0x99 = A-Z (uppercase) - 0xA0-0xB9 = a-z (lowercase) - 0xF6-0xFF = Numbers 0-9 - Various special characters
Source code in pyAIAgent/game/data.py
get_item_map()
Returns a dictionary mapping internal Item IDs (hex) to Item Names.
Source code in pyAIAgent/game/data.py
get_location_name(value)
Return the enum name for a given int, or None if invalid.
get_move_map()
Returns a dictionary mapping internal Move IDs (int) to Move Names.
Source code in pyAIAgent/game/data.py
get_species_map()
Returns a dictionary mapping internal Pokémon Red/Blue IDs (hex) to a tuple containing: (Pokédex number, Name, Type1, Type2). Type2 is None if the Pokémon has only one type.
Source code in pyAIAgent/game/data.py
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
LLM Clients
pyAIAgent.llm.client
Socket Communication
pyAIAgent.utils.socket_utils
JSON Parsing
pyAIAgent.json_parser
Functions
parse_optional_fenced_json(text)
Parses JSON from text.
The JSON may be either
- Enclosed in triple-backtick fences
- Plain JSON without fences
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text string to parse |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The deserialized Python object. |