Skip to content

Commit fc76f37

Browse files
committed
cp
1 parent c9ce550 commit fc76f37

File tree

6 files changed

+34
-25
lines changed

6 files changed

+34
-25
lines changed

packages/mettagrid/cpp/bindings/mettagrid_c.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -578,15 +578,8 @@ void MettaGrid::_step() {
578578
if (_inventory_regen_interval > 0 && current_step % _inventory_regen_interval == 0) {
579579
for (auto* agent : _agents) {
580580
if (!agent->inventory_regen_amounts.empty()) {
581-
// Look up regen amounts for agent's current vibe, fall back to "default" (vibe ID 0)
582-
auto vibe_it = agent->inventory_regen_amounts.find(agent->vibe);
583-
if (vibe_it == agent->inventory_regen_amounts.end()) {
584-
vibe_it = agent->inventory_regen_amounts.find(0); // "default" is vibe ID 0
585-
}
586-
if (vibe_it != agent->inventory_regen_amounts.end()) {
587-
for (const auto& [item, amount] : vibe_it->second) {
588-
agent->inventory.update(item, amount);
589-
}
581+
for (const auto& [item, amount] : agent->inventory_regen_amounts) {
582+
agent->inventory.update(item, amount);
590583
}
591584
}
592585
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "objects/agent_config.hpp"
1313
#include "objects/constants.hpp"
1414
#include "objects/has_inventory.hpp"
15+
#include "objects/inventory_config.hpp"
1516
#include "objects/usable.hpp"
1617
#include "systems/stats_tracker.hpp"
1718

@@ -40,6 +41,8 @@ class Agent : public GridObject, public HasInventory, public Usable {
4041
std::unordered_map<InventoryItem, InventoryQuantity> inventory_regen_amounts;
4142
// Damage configuration
4243
DamageConfig damage_config;
44+
// Inventory configuration (limits and modifiers)
45+
InventoryConfig inventory_config;
4346

4447
Agent(GridCoord r,
4548
GridCoord c,
@@ -49,7 +52,8 @@ class Agent : public GridObject, public HasInventory, public Usable {
4952

5053
void init(RewardType* reward_ptr);
5154

52-
void populate_initial_inventory(const std::unordered_map<InventoryItem, InventoryQuantity>& initial_inventory);
55+
void populate_initial_inventory(const std::unordered_map<InventoryItem, InventoryQuantity>& initial_inventory,
56+
const std::vector<InventoryItem>& inventory_deps = {});
5357

5458
void set_inventory(const std::unordered_map<InventoryItem, InventoryQuantity>& inventory);
5559

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ void Agent::populate_initial_inventory(const std::unordered_map<InventoryItem, I
9090

9191
// Then add remaining items
9292
for (const auto& [item, amount] : initial_inventory) {
93+
if (added.count(item)) continue; // Skip items already added
9394
this->inventory.update(item, amount, /*ignore_limits=*/true);
9495
}
9596
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ def convert_to_cpp_game_config(game_config: GameConfig):
166166
regen_amounts_flat = {}
167167
for resource_name, amount in inv_config.get("regen_amounts", {}).items():
168168
regen_amounts_flat[resource_name_to_id[resource_name]] = amount
169-
# Wrap in nested map with vibe 0 as default
170-
inventory_regen_amounts = {0: regen_amounts_flat} if regen_amounts_flat else {}
169+
# Pass flat map directly (C++ expects dict[int, int])
170+
inventory_regen_amounts = regen_amounts_flat
171171

172172
diversity_tracked_resources = [
173173
resource_name_to_id[resource_name]
@@ -388,14 +388,14 @@ def convert_to_cpp_game_config(game_config: GameConfig):
388388
initial_inventory_cpp[resource_id] = amount
389389

390390
# Create inventory config with limits from nested inventory config
391-
limits_list = []
391+
inventory_config = CppInventoryConfig()
392+
limit_defs = []
392393
for resource_limit in object_config.inventory.limits.values():
393394
resource_list = resource_limit.resources
394395
resource_ids = [resource_name_to_id[name] for name in resource_list if name in resource_name_to_id]
395396
if resource_ids:
396-
limits_list.append((resource_ids, resource_limit.limit))
397-
398-
inventory_config = CppInventoryConfig(limits_list)
397+
limit_defs.append(CppLimitDef(resources=resource_ids, base_limit=resource_limit.limit))
398+
inventory_config.limit_defs = limit_defs
399399

400400
# Get currency resource ID
401401
currency_resource_id = resource_name_to_id.get(object_config.currency_resource, 0)

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ class ChangeVibeActionConfig(ActionConfig):
189189

190190
action_handler: str = Field(default="change_vibe")
191191
number_of_vibes: int = Field(default=0, ge=0, le=255)
192-
vibes: list[str] | None = Field(default=None)
192+
vibes: list[Vibe] | None = Field(default=None)
193193

194194
def _actions(self) -> list[Action]:
195195
if self.vibes is not None:
196-
return [self.ChangeVibe(Vibe(name=vibe)) for vibe in self.vibes]
196+
return [self.ChangeVibe(vibe) for vibe in self.vibes]
197197
return [self.ChangeVibe(vibe) for vibe in VIBES[: self.number_of_vibes]]
198198

199199
def ChangeVibe(self, vibe: Vibe) -> Action:
@@ -263,6 +263,17 @@ class AttackActionConfig(ActionConfig):
263263
default_factory=dict,
264264
description="Per-vibe armor bonus. Maps vibe name to bonus amount.",
265265
)
266+
loot: list[str] = Field(
267+
default_factory=list,
268+
description="Convenience field: resources to steal (merged into success.loot).",
269+
)
270+
271+
@model_validator(mode="after")
272+
def _merge_loot_into_success(self) -> "AttackActionConfig":
273+
"""Merge top-level loot into success.loot for convenience."""
274+
if self.loot:
275+
self.success.loot = list(set(self.success.loot + self.loot))
276+
return self
266277

267278
def _actions(self) -> list[Action]:
268279
# Attack only triggers via move, no standalone actions
@@ -300,6 +311,10 @@ class TransferActionConfig(ActionConfig):
300311
default_factory=list,
301312
description="List of vibe transfer configs specifying actor/target resource effects",
302313
)
314+
vibes: list[str] = Field(
315+
default_factory=list,
316+
description="Convenience field: vibes that can trigger transfer (used with vibe_transfers)",
317+
)
303318

304319
def _actions(self) -> list[Action]:
305320
# Transfer doesn't create standalone actions - it's triggered by move
@@ -649,7 +664,9 @@ class GameConfig(Config):
649664
@model_validator(mode="after")
650665
def _compute_feature_ids(self) -> "GameConfig":
651666
self.actions.change_vibe.number_of_vibes = self.actions.change_vibe.number_of_vibes or len(VIBES)
652-
self.vibe_names = [vibe.name for vibe in VIBES[: self.actions.change_vibe.number_of_vibes]]
667+
# Only set vibe_names from VIBES if not already provided by user
668+
if not self.vibe_names:
669+
self.vibe_names = [vibe.name for vibe in VIBES[: self.actions.change_vibe.number_of_vibes]]
653670
return self
654671

655672
def id_map(self) -> "IdMap":

recipes/experiment/cog_arena.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,6 @@ def make_env(num_agents: int = 10) -> MettaGridConfig:
248248
"shield": 10,
249249
"gear": 4,
250250
},
251-
limits={
252-
"battery": ResourceLimitsConfig(limit=2000, resources=["battery"]),
253-
"weapon": ResourceLimitsConfig(limit=2000, resources=["weapon"]),
254-
"shield": ResourceLimitsConfig(limit=2000, resources=["shield"]),
255-
"gear": ResourceLimitsConfig(limit=2000, resources=["gear"]),
256-
},
257251
),
258252
currency_resource="heart",
259253
),

0 commit comments

Comments
 (0)