API Reference¶
Factories¶
mudgym.envs.factory.make_env(observation='parsed', field_parsers=None, actions='text', render_mode=None, connection=None, connection_kwargs=None, tearoom_commands=None, world_ticker=None, *, persona=None, sex=None, persona_pool=None)
¶
Build a Gymnasium environment.
Reset and step expose info["persona"] and info["persona_sex"]. Non-None persona options override connection_kwargs. Configure existing connections on their provider. See make_parallel_env for per-player tuple shorthand.
world_ticker runs between the action and observation, defaulting to the connection's tick_for_step hook if present.
Source code in src/mudgym/envs/factory.py
mudgym.envs.factory.make_parallel_env(agents=2, *, observation='parsed', field_parsers=None, actions='text', render_mode=None, tearoom_commands=None, provider=None, world_ticker=None, personas=None, persona_pool=None)
¶
Create a PettingZoo environment whose players share one MUD world.
world_ticker runs after all actions and before any observations, defaulting to the provider's tick_for_step hook if present.
Source code in src/mudgym/envs/factory.py
Environments¶
mudgym.envs.env.MudEnv
¶
Bases: Env[dict[str, Any], str]
A Gymnasium environment for MUD2.
Source code in src/mudgym/envs/env.py
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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 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 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | |
bytes_to_observation(raw_bytes, *, sent_lines, response_complete)
¶
Turn a step's response payload into an observation and its renderable bytes.
Source code in src/mudgym/envs/env.py
clean_tearoom_exit(raw_bytes)
¶
Drop the tearoom setup through the exit narration.
Source code in src/mudgym/envs/env.py
update_points(raw_bytes, *, terminated=False)
¶
Update the tracked score from points events or permadeath.
Source code in src/mudgym/envs/env.py
reset(*, seed=None, options=None)
¶
Prepare, enter and collect the initial observation without advancing the world clock.
Source code in src/mudgym/envs/env.py
step(action)
¶
Send one action, then receive its completed observation.
world_ticker runs once after the action and before its observation, so a standalone env advances its own
world here. Vector and parallel coordinators drive act() and observe() themselves and own the joint
advancement, so their children are built without one.
Source code in src/mudgym/envs/env.py
validate_action(action)
¶
Validate an action without changing player or session state.
act(action)
¶
Send an action now, leaving its observation for a later observe call.
Source code in src/mudgym/envs/env.py
observe()
¶
Receive everything through this player's completed response.
This includes the earlier action, the observation-command responses, and anything caused by other players since that action was sent.
Source code in src/mudgym/envs/env.py
mudgym.envs.zoo.MudParallelEnv
¶
Bases: ParallelEnv[str, dict[str, Any], str]
Coordinates several named players acting together in one shared MUD world.
Source code in src/mudgym/envs/zoo.py
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 | |
Observation fields¶
mudgym.envs.fields.field.ObservationField
¶
Bases: ABC
A self-contained, pure (no side effects) observation field.
A field declares: - the command that produces its bytes, if any; - the observation-space keys it owns; - empty defaults for those keys; - an extractor from response chunks to values.
A chunk is the game's output bytes for a single game event delimited by a game prompt marker.
The abstract methods (full_space(), full_empty(), full_extract()) declare the parser's
full capability; space(), empty(), and extract() restrict that capability to
include_keys and are what consumers read.
Source code in src/mudgym/envs/fields/field.py
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 | |
__init__(include_keys=None)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_keys
|
Sequence[str] | None
|
restrict this field's observation contribution to the given space() keys. None (default) keeps every key. An empty sequence contributes no keys. |
None
|
Source code in src/mudgym/envs/fields/field.py
filter_keys(values)
¶
Restrict a full_space/full_empty/full_extract mapping to this field's include_keys.
Source code in src/mudgym/envs/fields/field.py
space()
¶
The observation-space slice this field contributes: full_space() restricted to include_keys.
empty()
¶
Default values for the contributed keys: full_empty() restricted to include_keys.
extract(chunks, **context)
¶
This field's observation contribution for a turn: full_extract() restricted to include_keys.
Source code in src/mudgym/envs/fields/field.py
full_space()
abstractmethod
¶
full_empty()
abstractmethod
¶
Default values for every parser key when nothing matches (dtypes/shapes match full_space()).
full_extract(chunks, **context)
abstractmethod
¶
Parse the turn's response chunks into every parser key. Pure: a function of its inputs alone.
context carries observer facts the env supplies each call, such as persona. A parser consumes what it
needs.
Source code in src/mudgym/envs/fields/field.py
matches(chunk)
¶
Whether chunk is a valid output of this field's command.
By default we return True. Subclasses can override this to make matching more robust, but should accept every output the command can produce, including when the persona is in the dark, asleep or blind.
Source code in src/mudgym/envs/fields/field.py
is_refusal(chunk)
¶
Whether chunk is a player-state refusal instead of this command's real output.
Source code in src/mudgym/envs/fields/field.py
lines(raw_bytes)
¶
The chunk's text lines: ANSI stripped, decoded, split on line breaks and stripped of whitespace.
find_last_line(regex, chunks)
¶
Return the last line matching regex across the response chunks (or None).
Source code in src/mudgym/envs/fields/field.py
mudgym.envs.fields.rawbytes.RawBytesField
¶
Bases: ObservationField
Returns the raw bytes from the game response, including ANSI escape codes, prompt markers and line breaks as a fixed-size uint8 numpy array.
Source code in src/mudgym/envs/fields/rawbytes.py
mudgym.envs.fields.fescore.FEScoreField
¶
Bases: ObservationField
Parsed FES line values
- vitals (8-dim) - stamina, max_stamina, effective_strength, strength, effective_dexterity, dexterity, magic, max_magic
- flags (4-dim) - blind, deaf, crippled, dumb
- reset_minutes (scalar)
- weather (text) - fair, raining, snowing, etc.
- weather_index (scalar) - index of the weather
Source code in src/mudgym/envs/fields/fescore.py
full_extract(chunks, **context)
¶
Parse the latest FES status line from the turn chunks, or the empty default if none is present.
Source code in src/mudgym/envs/fields/fescore.py
mudgym.envs.fields.fexits.FEXitsField
¶
Bases: ObservationField
FEX exit data field.
Provides known available exits as
- available_exits: MultiBinary vector over all directions.
- available_exit_names: Tuple of direction names.
Source code in src/mudgym/envs/fields/fexits.py
full_extract(chunks, **context)
¶
Parse the latest FEX exits line.
When no exits line is recognised (e.g. a dark room returns a blank exits response), default to all exits available so the agent can still attempt any direction.
Source code in src/mudgym/envs/fields/fexits.py
mudgym.envs.fields.feinventory.FEInventoryField
¶
Bases: ObservationField
Parses the fei command output, split by the inventory divider into portables (lying around) and
the player's own inventory.
Source code in src/mudgym/envs/fields/feinventory.py
full_extract(chunks, **context)
¶
Find the fei response chunk and split it on the divider into portables / inventory.
Source code in src/mudgym/envs/fields/feinventory.py
mudgym.envs.fields.superquicklook.SuperQuickLookField
¶
Bases: ObservationField
Parses room contents and inventory from the superquicklook command.
Missing views use index 0. Supplied room names must be recognised.
Source code in src/mudgym/envs/fields/superquicklook.py
full_extract(chunks, *, persona=None, **context)
¶
Parse the latest superquicklook room view, or the empty default if none is present.
Source code in src/mudgym/envs/fields/superquicklook.py
mudgym.envs.fields.mgcheats.MGCheatsField
¶
Bases: ObservationField
Reads the mgcheats block.
Missing data uses index 0. Supplied room values must be recognised.
Sample game response:
[mgcheats]room_id=mtrack1; room_name=beaten track near cliff; fighting=0; dark=0; glowing=0; asleep=0; gifted=0; here=[rain, cliff, road]; inventory=[][/mgcheats]
Source code in src/mudgym/envs/fields/mgcheats.py
parse(payload_bytes)
¶
Read the wire pairs without accepting duplicate keys.
Source code in src/mudgym/envs/fields/mgcheats.py
full_extract(chunks, **context)
¶
Parse the latest [mgcheats] block, or the empty default if none is present.
Source code in src/mudgym/envs/fields/mgcheats.py
Action wrappers¶
mudgym.envs.actions.discrete.DiscreteActionSpaceWrapper
¶
Bases: ActionWrapper
Sets the action space to a discrete categorical multiple choice space. Maps discrete actions (ints) to string commands for the underlying env.
We use command to refer to the text sent to the game and action as the
RL/gymnasium side concept.
Source code in src/mudgym/envs/actions/discrete.py
mudgym.envs.actions.discrete.DiscreteDirectionsWrapper
¶
Bases: DiscreteActionSpaceWrapper
Set the action space to include the movement directions.
Source code in src/mudgym/envs/actions/discrete.py
mudgym.envs.actions.discrete.ParallelDiscreteActionSpaceWrapper
¶
Bases: BaseParallelWrapper
Map every agent's discrete action before forwarding one parallel step.
Source code in src/mudgym/envs/actions/discrete.py
mudgym.envs.actions.discrete.ParallelDiscreteDirectionsWrapper
¶
Bases: ParallelDiscreteActionSpaceWrapper
Set every agent's action space to the movement directions.
Source code in src/mudgym/envs/actions/discrete.py
Connections¶
mudgym.connections.connection.MudConnection
¶
Bases: ABC
Connection interface for game sessions and recorded transcripts.
Reset prepares a session in the tearoom. Sending and reading are separate so several players can act before collecting their observations. Closing releases the connection's resources.
Source code in src/mudgym/connections/connection.py
mudgym.connections.wasm.wasmtime_provider.WasmtimeConnection
¶
Bases: MudConnection
One stable MudGym connection rebound to each fresh WASI session.
Source code in src/mudgym/connections/wasm/wasmtime_provider.py
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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
mudgym.connections.wasm.create_connection(*, persona=None, sex=None, **provider_options)
¶
Create a standalone connection that owns and closes its entire provider.
Source code in src/mudgym/connections/wasm/wasmtime_provider.py
mudgym.connections.provider.ConnectionProvider
¶
Bases: Protocol
Provide connections backed by shared resources.
The provider determines how players are arranged across worlds. Once create_connections returns, the caller owns the connections. If creation fails, the provider cleans up that call's resources. The owning environment closes the provider after its connections.
Source code in src/mudgym/connections/provider.py
mudgym.connections.wasm.WasmtimeProvider
¶
MudGym provider spreading stable connections over fresh isolated WASI worlds.
Source code in src/mudgym/connections/wasm/wasmtime_provider.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 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 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 | |
reset(*, seed=None)
¶
Replace every Store and Instance, then rebind the stable connections.
Source code in src/mudgym/connections/wasm/wasmtime_provider.py
advance_worlds(ticks)
¶
Advance every world once, reporting a world that ended structurally.
Healthy worlds still advance when a sibling ends. Terminal worlds are reported together as
WorldAdvancementFailed so the step hook can leave their final transitions for observation.