Skip to content

Commit 581fbe5

Browse files
committed
cp
1 parent ed22108 commit 581fbe5

File tree

5 files changed

+32
-17
lines changed

5 files changed

+32
-17
lines changed

packages/mettagrid/cpp/bindings/mettagrid_c.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -626,15 +626,8 @@ void MettaGrid::_step() {
626626
if (_inventory_regen_interval > 0 && current_step % _inventory_regen_interval == 0) {
627627
for (auto* agent : _agents) {
628628
if (!agent->inventory_regen_amounts.empty()) {
629-
// Look up regen amounts for agent's current vibe, fall back to "default" (vibe ID 0)
630-
auto vibe_it = agent->inventory_regen_amounts.find(agent->vibe);
631-
if (vibe_it == agent->inventory_regen_amounts.end()) {
632-
vibe_it = agent->inventory_regen_amounts.find(0); // "default" is vibe ID 0
633-
}
634-
if (vibe_it != agent->inventory_regen_amounts.end()) {
635-
for (const auto& [item, amount] : vibe_it->second) {
636-
agent->inventory.update(item, amount);
637-
}
629+
for (const auto& [item, amount] : agent->inventory_regen_amounts) {
630+
agent->inventory.update(item, amount);
638631
}
639632
}
640633
}

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

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

@@ -42,6 +43,8 @@ class Agent : public GridObject, public HasInventory, public Usable {
4243
std::unordered_map<ObservationType, std::unordered_map<InventoryItem, InventoryQuantity>> inventory_regen_amounts;
4344
// Damage configuration
4445
DamageConfig damage_config;
46+
// Inventory configuration (limits and modifiers)
47+
InventoryConfig inventory_config;
4548

4649
Agent(GridCoord r,
4750
GridCoord c,
@@ -51,7 +54,8 @@ class Agent : public GridObject, public HasInventory, public Usable {
5154

5255
void init(RewardType* reward_ptr);
5356

54-
void populate_initial_inventory(const std::unordered_map<InventoryItem, InventoryQuantity>& initial_inventory);
57+
void populate_initial_inventory(const std::unordered_map<InventoryItem, InventoryQuantity>& initial_inventory,
58+
const std::vector<InventoryItem>& inventory_deps = {});
5559

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

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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,14 +394,14 @@ def convert_to_cpp_game_config(game_config: GameConfig):
394394
initial_inventory_cpp[resource_id] = amount
395395

396396
# Create inventory config with limits from nested inventory config
397-
limits_list = []
397+
inventory_config = CppInventoryConfig()
398+
limit_defs = []
398399
for resource_limit in object_config.inventory.limits.values():
399400
resource_list = resource_limit.resources
400401
resource_ids = [resource_name_to_id[name] for name in resource_list if name in resource_name_to_id]
401402
if resource_ids:
402-
limits_list.append((resource_ids, resource_limit.limit))
403-
404-
inventory_config = CppInventoryConfig(limits_list)
403+
limit_defs.append(CppLimitDef(resources=resource_ids, base_limit=resource_limit.limit))
404+
inventory_config.limit_defs = limit_defs
405405

406406
# Get currency resource ID
407407
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
@@ -194,11 +194,11 @@ class ChangeVibeActionConfig(ActionConfig):
194194

195195
action_handler: str = Field(default="change_vibe")
196196
number_of_vibes: int = Field(default=0, ge=0, le=255)
197-
vibes: list[str] | None = Field(default=None)
197+
vibes: list[Vibe] | None = Field(default=None)
198198

199199
def _actions(self) -> list[Action]:
200200
if self.vibes is not None:
201-
return [self.ChangeVibe(Vibe(name=vibe)) for vibe in self.vibes]
201+
return [self.ChangeVibe(vibe) for vibe in self.vibes]
202202
return [self.ChangeVibe(vibe) for vibe in VIBES[: self.number_of_vibes]]
203203

204204
def ChangeVibe(self, vibe: Vibe) -> Action:
@@ -268,6 +268,17 @@ class AttackActionConfig(ActionConfig):
268268
default_factory=dict,
269269
description="Per-vibe armor bonus. Maps vibe name to bonus amount.",
270270
)
271+
loot: list[str] = Field(
272+
default_factory=list,
273+
description="Convenience field: resources to steal (merged into success.loot).",
274+
)
275+
276+
@model_validator(mode="after")
277+
def _merge_loot_into_success(self) -> "AttackActionConfig":
278+
"""Merge top-level loot into success.loot for convenience."""
279+
if self.loot:
280+
self.success.loot = list(set(self.success.loot + self.loot))
281+
return self
271282

272283
def _actions(self) -> list[Action]:
273284
# Attack only triggers via move, no standalone actions
@@ -305,6 +316,10 @@ class TransferActionConfig(ActionConfig):
305316
default_factory=list,
306317
description="List of vibe transfer configs specifying actor/target resource effects",
307318
)
319+
vibes: list[str] = Field(
320+
default_factory=list,
321+
description="Convenience field: vibes that can trigger transfer (used with vibe_transfers)",
322+
)
308323

309324
def _actions(self) -> list[Action]:
310325
# Transfer doesn't create standalone actions - it's triggered by move
@@ -653,7 +668,9 @@ class GameConfig(Config):
653668
@model_validator(mode="after")
654669
def _compute_feature_ids(self) -> "GameConfig":
655670
self.actions.change_vibe.number_of_vibes = self.actions.change_vibe.number_of_vibes or len(VIBES)
656-
self.vibe_names = [vibe.name for vibe in VIBES[: self.actions.change_vibe.number_of_vibes]]
671+
# Only set vibe_names from VIBES if not already provided by user
672+
if not self.vibe_names:
673+
self.vibe_names = [vibe.name for vibe in VIBES[: self.actions.change_vibe.number_of_vibes]]
657674
return self
658675

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

0 commit comments

Comments
 (0)