Skip to content

Commit 2ffd88e

Browse files
committed
cp
1 parent 13f0211 commit 2ffd88e

File tree

6 files changed

+32
-29
lines changed

6 files changed

+32
-29
lines changed

packages/mettagrid/cpp/include/mettagrid/objects/chest.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef PACKAGES_METTAGRID_CPP_INCLUDE_METTAGRID_OBJECTS_CHEST_HPP_
22
#define PACKAGES_METTAGRID_CPP_INCLUDE_METTAGRID_OBJECTS_CHEST_HPP_
33

4+
#include <algorithm>
45
#include <set>
56
#include <unordered_map>
67
#include <vector>
@@ -121,7 +122,8 @@ class Chest : public GridObject, public Usable, public HasInventory {
121122
throw std::runtime_error("Observation encoder not set for chest");
122123
}
123124
std::vector<PartialObservationToken> features;
124-
features.reserve(1 + this->inventory.get().size() + this->tag_ids.size() + (this->vibe != 0 ? 1 : 0));
125+
// Up to 2 tokens per inventory item (low byte + optional high byte)
126+
features.reserve(1 + this->inventory.get().size() * 2 + this->tag_ids.size() + (this->vibe != 0 ? 1 : 0));
125127

126128
if (this->vibe != 0) features.push_back({ObservationFeature::Vibe, static_cast<ObservationType>(this->vibe)});
127129

packages/mettagrid/cpp/include/mettagrid/objects/inventory.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef PACKAGES_METTAGRID_CPP_INCLUDE_METTAGRID_OBJECTS_INVENTORY_HPP_
22
#define PACKAGES_METTAGRID_CPP_INCLUDE_METTAGRID_OBJECTS_INVENTORY_HPP_
33

4+
#include <limits>
45
#include <string>
56
#include <unordered_map>
67
#include <vector>

packages/mettagrid/cpp/src/mettagrid/objects/agent.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ std::vector<PartialObservationToken> Agent::obs_features() const {
209209
if (!this->obs_encoder) {
210210
throw std::runtime_error("Observation encoder not set for agent");
211211
}
212-
const size_t num_tokens = this->inventory.get().size() + this->tag_ids.size() + 5;
212+
const size_t num_tokens = this->inventory.get().size() * 2 + this->tag_ids.size() + 5;
213213

214214
std::vector<PartialObservationToken> features;
215215
features.reserve(num_tokens);

packages/mettagrid/docs/observations.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -103,30 +103,30 @@ The feature set follows this general structure (exact IDs depend on configuratio
103103
depend on your game configuration (number of resources, whether protocol details are enabled, etc.), so always use
104104
`IdMap` to get the exact feature IDs for your configuration.
105105

106-
| Feature Name | Description | Objects with this Feature | Notes |
107-
| --------------------------------- | ------------------------------------------------------------------------------- | ------------------------- | ------------------------------------------------------------------------------- |
108-
| `agent:group` | Agent's group/team identifier | agents | |
109-
| `agent:frozen` | Whether agent is frozen or not. Frozen agents cannot act | agents | |
110-
| `episode_completion_pct` | Portion of the episode completed, from 0 (start) to 255 (end). Not a percentage | self | |
111-
| `last_action` | Last action taken by the agent | self | |
112-
| `last_reward` | Last reward received by the agent | self | |
113-
| `vibe` | Agent's current vibe | any object | Values can be found in `vibes.VIBES` |
114-
| `agent:compass` | Compass direction toward assembler | self | |
115-
| `tag` | Tags associated with an object (e.g., "wall", "oxygen_extractor", "blue") | any object | Values can be found in `IdMap.tag_names()`. Multiple tags emit multiple tokens. |
116-
| `cooldown_remaining` | Remaining cooldown time for objects | assembler, extractors | Value capped at 255 |
117-
| `clipped` | Whether an assembler is clipped or not | extractors | |
118-
| `remaining_uses` | Remaining uses for objects with use limits | extractors | Value capped at 255. Only emitted if `max_uses > 0` |
119-
| `inv:{resource_name}` | Base inventory amount (amount % token_value_max) | agents, chests | One feature per resource. See [Inventory Encoding](#inventory-encoding) below. |
120-
| `inv:{resource_name}:p1` | Power 1 component ((amount / B) % B) | agents, chests | Only emitted if amount >= B. See [Inventory Encoding](#inventory-encoding). |
121-
| `inv:{resource_name}:p2` | Power 2 component ((amount / B²) % B) | agents, chests | Only emitted if amount >= B². See [Inventory Encoding](#inventory-encoding). |
122-
| `protocol_input:{resource_name}` | Required input resource amount for current protocol | assembler, extractors | One feature per resource |
123-
| `protocol_output:{resource_name}` | Output resource amount for current protocol | assembler, extractors | One feature per resource |
106+
| Feature Name | Description | Objects with this Feature | Notes |
107+
| ------------------------ | ------------------------------------------------------------------------------- | ------------------------- | ------------------------------------------------------------------------------- |
108+
| `agent:group` | Agent's group/team identifier | agents | |
109+
| `agent:frozen` | Whether agent is frozen or not. Frozen agents cannot act | agents | |
110+
| `episode_completion_pct` | Portion of the episode completed, from 0 (start) to 255 (end). Not a percentage | self | |
111+
| `last_action` | Last action taken by the agent | self | |
112+
| `last_reward` | Last reward received by the agent | self | |
113+
| `vibe` | Agent's current vibe | any object | Values can be found in `vibes.VIBES` |
114+
| `agent:compass` | Compass direction toward assembler | self | |
115+
| `tag` | Tags associated with an object (e.g., "wall", "oxygen_extractor", "blue") | any object | Values can be found in `IdMap.tag_names()`. Multiple tags emit multiple tokens. |
116+
| `cooldown_remaining` | Remaining cooldown time for objects | assembler, extractors | Value capped at 255 |
117+
| `clipped` | Whether an assembler is clipped or not | extractors | |
118+
| `remaining_uses` | Remaining uses for objects with use limits | extractors | Value capped at 255. Only emitted if `max_uses > 0` |
119+
| `inv:{resource_name}` | Base inventory amount (amount % token_value_max) | agents, chests | One feature per resource. See [Inventory Encoding](#inventory-encoding) below. |
120+
| `inv:{resource_name}:p1` | Power 1 component ((amount / B) % B) | agents, chests | Only emitted if amount >= B. See [Inventory Encoding](#inventory-encoding). |
121+
| `inv:{resource_name}:p2` | Power 2 component ((amount / B²) % B) | agents, chests | Only emitted if amount >= B². See [Inventory Encoding](#inventory-encoding). |
122+
| `protocol_input:{resource_name}` | Required input resource amount for current protocol | assembler, extractors | One feature per resource |
123+
| `protocol_output:{resource_name}` | Output resource amount for current protocol | assembler, extractors | One feature per resource |
124124

125125
### Inventory Encoding
126126

127-
Inventory values are encoded using a multi-token scheme with a configurable base (`ObsConfig.token_value_max`, default
128-
255). This allows representing large amounts while keeping individual token values bounded. The number of tokens is
129-
dynamically computed based on the maximum inventory value (uint16_t max = 65535).
127+
Inventory values are encoded using a multi-token scheme with a configurable base
128+
(`ObsConfig.token_value_max`, default 255). This allows representing large amounts while keeping individual token values
129+
bounded. The number of tokens is dynamically computed based on the maximum inventory value (uint16_t max = 65535).
130130

131131
- **`inv:{resource}`**: Base value = `amount % B` (always emitted if amount > 0)
132132
- **`inv:{resource}:p1`**: Power 1 = `(amount / B) % B` (only emitted if amount >= B)

packages/mettagrid/docs/simulator_api.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,11 @@ obs = agent.observation
265265
for token in obs.tokens:
266266
if token.feature.name == "object_type":
267267
print(f"Object at ({token.col()}, {token.row()}): {token.value}")
268-
elif token.feature.name.startswith("inv:"):
269-
resource = token.feature.name[4:] # Remove "inv:" prefix
270-
print(f"Inventory {resource}: {token.value}")
268+
269+
# For inventory, use the agent.inventory property which handles the encoding
270+
inventory = agent.inventory
271+
for resource, amount in inventory.items():
272+
print(f"Inventory {resource}: {amount}")
271273
```
272274

273275
## Event Handling

packages/mettagrid/python/src/mettagrid/config/mettagrid_c_config.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,7 @@ def convert_to_cpp_game_config(mettagrid_config: dict | GameConfig):
348348
if name in resource_name_to_id
349349
}
350350
limit_defs.append(
351-
CppLimitDef(
352-
resources=resource_ids, base_limit=min(resource_limit.limit, 255), modifiers=modifier_ids
353-
)
351+
CppLimitDef(resources=resource_ids, base_limit=resource_limit.limit, modifiers=modifier_ids)
354352
)
355353

356354
inventory_config = CppInventoryConfig()

0 commit comments

Comments
 (0)