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)
¶
Build one Gymnasium environment.
Source code in src/mudgym/envs/factory.py
mudgym.envs.factory.make_vector_env(envs, *, observation='parsed', field_parsers=None, actions='text', render_mode=None, tearoom_commands=None, provider=None)
¶
Create a Gymnasium vector env. It need not know how worlds are arranged.
The provider decides where the connections lead. Reset is deliberately all-or-nothing for now, and both reset and step finish the shared action/setup work before collecting observations. A supplied provider becomes the resulting environment's responsibility and is closed with it.
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)
¶
Create a PettingZoo environment whose players share one MUD world.
The registry supplies a one-world default. If a caller passes a provider we trust that it honours the same promise. The resulting environment owns that provider, and action wrappers sit around the joint environment rather than around each player.
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
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 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 | |
persona
property
¶
The persona name of the current player. Set during session reset.
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, sent_lines)
¶
Remove the tearoom setup and return the exit fes score.
Source code in src/mudgym/envs/env.py
step(action)
¶
Send one action, then receive its marker-framed observation.
Vector and parallel coordinators call act() on every child before calling observe()
on any child, so each returned observation follows the complete joint action batch.
Source code in src/mudgym/envs/env.py
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 up to this player's end-of-turn marker.
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.vector.MudVectorEnv
¶
Bases: VectorEnv
Source code in src/mudgym/envs/vector.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 | |
batch_observations(observations)
¶
Put child observations into the vector observation space.
Source code in src/mudgym/envs/vector.py
batch_infos(infos)
¶
Use Gymnasium's mask convention to combine child info dictionaries.
Source code in src/mudgym/envs/vector.py
child_seeds(seed)
¶
Turn the vector seed into one Gym-side seed per child.
Source code in src/mudgym/envs/vector.py
step(actions)
¶
Send every child action before receiving any child observation; never autoreset.
Source code in src/mudgym/envs/vector.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
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 | |
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 keeps none, useful for using as an end of step marker. |
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.
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 (currently persona, the
observing persona's bare name); a parser names what it consumes and ignores the rest.
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 just return True, but subclasses can override this to make matching more robust, in which case this method should return True for every output the command can really produce, including edge cases (eg, when dark, asleep, blind, etc)
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.
decode(raw_bytes)
¶
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
- points (scalar)
- 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
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 | |
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. Pure: reads the step bytes and returns its own keys only.
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.
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)
¶
Parse a single mgcheats payload (semicolon-separated key=value pairs) into a dict of values.
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
¶
Base class for managing connections to MUD2 game instances.
The state machine handles the fiddly lifecycle. This class wraps it so we can swap in different transports.
Lifecycle:
- reset() gets us to TEA_SIPPED, ready for an episode to begin. The env's reset then
exits the tearoom to start that episode.
- send_line() and read_response() split sending from receiving so several players can
see each other's actions.
- close() terminates the child process. Use reset() instead when the connection will be
reused for another episode.
Source code in src/mudgym/connections/connection.py
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 | |
spawn()
¶
The method that does the actual connecting by spawning and returning our child process.
Source code in src/mudgym/connections/connection.py
reset()
¶
Resets the connection to be ready to start a new episode (TEA_SIPPED state).
This tells us the MudConnection is ready but the MudEnv has its own reset() steps afterwards that does
episode related things that don't make sense here, like issuing commands to set up the initial environment state
(eg, score), running observation commands, and taking the northwards step outside of the tearoom.
I can imagine a situation with multiple MudConnections waiting on each other after reset() to be ready so it
seemed negligent to leave agents hanging around outside of the sanctity of the Tearoom where they might get
attacked by mobiles or something.
Source code in src/mudgym/connections/connection.py
send_line(line)
¶
Send a line without waiting for its response.
Source code in src/mudgym/connections/connection.py
read_response(end_of_turn_marker)
¶
Read the response up to the marker for lines already sent through send_line.
The connection remembers which lines were actually sent, which lets the state machine find their echoes without asking the caller to reconstruct the wire history afterwards.
Source code in src/mudgym/connections/connection.py
invalidate()
¶
Throw away a desynchronised transport so the next reset has to spawn a fresh one.
Source code in src/mudgym/connections/connection.py
mudgym.connections.docker_run.DockerRunConnection
¶
Bases: MudConnection
Docker run connection.
Spins up a new container via docker run for each connection.
Source code in src/mudgym/connections/docker_run.py
cleanup_container()
¶
Remove any existing container with our name to avoid conflicts.
This handles the case where a previous container wasn't properly cleaned up, e.g., if the process was killed abruptly or marimo re-ran a cell.
Source code in src/mudgym/connections/docker_run.py
spawn()
¶
Spawn the Docker container, ensuring any stale container is cleaned up first.
mudgym.connections.docker_exec.DockerExecConnection
¶
Bases: MudConnection
MUD2 connection that execs into an existing Docker container.
Typically for using a single container running with multiple game slots, or when connecting multiple clients to the same game slot.
Source code in src/mudgym/connections/docker_exec.py
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 | |
find_container_id(name, start_if_missing=True)
¶
Find container ID by name.
Source code in src/mudgym/connections/docker_exec.py
start_container()
¶
Start a new container and return the container ID.
Source code in src/mudgym/connections/docker_exec.py
close()
¶
Close the exec session, and remove the container if this connection started it.
We only tear down the container we own (one we started because none was running). A pre-existing, shared container is left alone so other clients exec'd into it survive.
Source code in src/mudgym/connections/docker_exec.py
mudgym.connections.provider.ConnectionProvider
¶
Bases: Protocol
Provides batches of connections backed by some shared set of resources.
The provider decides what those connections are connected to. That might be one world per connection, several players sharing a world, or something else entirely - the vector env doesn't need to know.
Once create_connections returns, the caller owns the connections. If it fails before returning, the
provider cleans up whatever that call managed to create. The provider itself stays around until the owning
environment closes, as it may also own containers or other resources the connections depend on.
Source code in src/mudgym/connections/provider.py
mudgym.connections.provider.DockerExecProvider
¶
Bases: ConnectionProvider
Provides one fixed batch of connections backed by Docker worlds.
worlds describes the topology, while the count passed to create_connections is simply how many connections the caller needs. If worlds is omitted we use one world per connection. Docker needs the whole count up front to size its containers, so this particular provider only supplies one batch.
Source code in src/mudgym/connections/provider.py
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 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 | |
prepare_shared_container(slots)
¶
Start a new container with multiple game slots.
Source code in src/mudgym/connections/provider.py
create_connections(count)
¶
Start enough Docker worlds and create this provider's one connection batch.
Source code in src/mudgym/connections/provider.py
reset(*, seed=None)
¶
close()
¶
Close the shared infrastructure, but not the connections owned by environments.