Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/mettagrid/cpp/include/mettagrid/objects/chest.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef PACKAGES_METTAGRID_CPP_INCLUDE_METTAGRID_OBJECTS_CHEST_HPP_
#define PACKAGES_METTAGRID_CPP_INCLUDE_METTAGRID_OBJECTS_CHEST_HPP_

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef PACKAGES_METTAGRID_CPP_INCLUDE_METTAGRID_OBJECTS_INVENTORY_HPP_
#define PACKAGES_METTAGRID_CPP_INCLUDE_METTAGRID_OBJECTS_INVENTORY_HPP_

#include <limits>
#include <string>
#include <unordered_map>
#include <vector>
Expand Down
2 changes: 1 addition & 1 deletion packages/mettagrid/cpp/src/mettagrid/objects/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ std::vector<PartialObservationToken> Agent::obs_features() const {
if (!this->obs_encoder) {
throw std::runtime_error("Observation encoder not set for agent");
}
const size_t num_tokens = this->inventory.get().size() + this->tag_ids.size() + 5;
const size_t num_tokens = this->inventory.get().size() * 2 + this->tag_ids.size() + 5;

std::vector<PartialObservationToken> features;
features.reserve(num_tokens);
Expand Down
8 changes: 5 additions & 3 deletions packages/mettagrid/docs/simulator_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,11 @@ obs = agent.observation
for token in obs.tokens:
if token.feature.name == "object_type":
print(f"Object at ({token.col()}, {token.row()}): {token.value}")
elif token.feature.name.startswith("inv:"):
resource = token.feature.name[4:] # Remove "inv:" prefix
print(f"Inventory {resource}: {token.value}")

# For inventory, use the agent.inventory property which handles the encoding
inventory = agent.inventory
for resource, amount in inventory.items():
print(f"Inventory {resource}: {amount}")
```

## Event Handling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,7 @@ def convert_to_cpp_game_config(mettagrid_config: dict | GameConfig):
if name in resource_name_to_id
}
limit_defs.append(
CppLimitDef(
resources=resource_ids, base_limit=min(resource_limit.limit, 255), modifiers=modifier_ids
)
CppLimitDef(resources=resource_ids, base_limit=resource_limit.limit, modifiers=modifier_ids)
)

inventory_config = CppInventoryConfig()
Expand Down
Loading