1414
1515#include " actions/action_handler.hpp"
1616#include " actions/attack.hpp"
17- #include " actions/build.hpp"
18- #include " actions/build_config.hpp"
1917#include " actions/change_vibe.hpp"
2018#include " actions/move.hpp"
2119#include " actions/move_config.hpp"
2220#include " actions/noop.hpp"
23- #include " actions/transfer.hpp"
2421#include " config/observation_features.hpp"
2522#include " core/grid.hpp"
2623#include " core/types.hpp"
@@ -85,15 +82,10 @@ MettaGrid::MettaGrid(const GameConfig& game_config, const py::list map, unsigned
8582
8683 _action_success.resize (num_agents);
8784
88- init_action_handlers ();
85+ init_action_handlers (_game_config );
8986
9087 _init_grid (_game_config, map);
9188
92- // Set runtime context for Build handler (needs obs_encoder and agents count)
93- if (_build_handler) {
94- _build_handler->set_runtime_context (¤t_step, _obs_encoder.get (), num_agents);
95- }
96-
9789 // Pre-compute goal_obs tokens for each agent
9890 if (_global_obs_config.goal_obs ) {
9991 _agent_goal_obs_tokens.resize (_agents.size ());
@@ -259,11 +251,11 @@ void MettaGrid::_init_buffers(unsigned int num_agents) {
259251 _compute_observations (executed_actions);
260252}
261253
262- void MettaGrid::init_action_handlers () {
254+ void MettaGrid::init_action_handlers (const GameConfig& game_config ) {
263255 _max_action_priority = 0 ;
264256
265257 // Noop
266- auto noop = std::make_unique<Noop>(*_game_config .actions .at (" noop" ));
258+ auto noop = std::make_unique<Noop>(*game_config .actions .at (" noop" ));
267259 noop->init (_grid.get (), &_rng);
268260 if (noop->priority > _max_action_priority) _max_action_priority = noop->priority ;
269261 for (const auto & action : noop->actions ()) {
@@ -272,72 +264,39 @@ void MettaGrid::init_action_handlers() {
272264 _action_handler_impl.push_back (std::move (noop));
273265
274266 // Move
275- auto move_config = std::static_pointer_cast<const MoveActionConfig>(_game_config .actions .at (" move" ));
276- auto move = std::make_unique<Move>(*move_config, &_game_config );
267+ auto move_config = std::static_pointer_cast<const MoveActionConfig>(game_config .actions .at (" move" ));
268+ auto move = std::make_unique<Move>(*move_config, &game_config );
277269 move->init (_grid.get (), &_rng);
278270 if (move->priority > _max_action_priority) _max_action_priority = move->priority ;
279271 for (const auto & action : move->actions ()) {
280272 _action_handlers.push_back (action);
281273 }
282- // Capture the raw pointer to pass to other handlers
283- Move* move_ptr = move.get ();
284274 _action_handler_impl.push_back (std::move (move));
285275
286276 // Attack
287- auto attack_config = std::static_pointer_cast<const AttackActionConfig>(_game_config.actions .at (" attack" ));
288- auto attack = std::make_unique<Attack>(*attack_config, &_game_config);
289- attack->init (_grid.get (), &_rng);
290- if (attack->priority > _max_action_priority) _max_action_priority = attack->priority ;
291- for (const auto & action : attack->actions ()) {
292- _action_handlers.push_back (action);
293- }
294-
295- // Transfer
296- auto transfer_config = std::static_pointer_cast<const TransferActionConfig>(_game_config.actions .at (" transfer" ));
297- auto transfer = std::make_unique<Transfer>(*transfer_config, &_game_config);
298- transfer->init (_grid.get (), &_rng);
299- if (transfer->priority > _max_action_priority) _max_action_priority = transfer->priority ;
300- for (const auto & action : transfer->actions ()) {
301- _action_handlers.push_back (action);
302- }
303-
304- // Build (creates objects at previous location after successful move)
305- _build_handler = nullptr ;
306- std::unique_ptr<Build> build;
307- if (_game_config.actions .find (" build" ) != _game_config.actions .end ()) {
308- auto build_config = std::static_pointer_cast<const BuildActionConfig>(_game_config.actions .at (" build" ));
309- build = std::make_unique<Build>(*build_config, &_game_config, _stats.get ());
310- build->init (_grid.get (), &_rng);
311- if (build->priority > _max_action_priority) _max_action_priority = build->priority ;
312- // Build doesn't create standalone actions - it's triggered by move
313- _build_handler = build.get ();
314- }
315-
316- // Register vibe-triggered action handlers with Move
317- std::unordered_map<std::string, ActionHandler*> handlers;
318- handlers[" attack" ] = attack.get ();
319- handlers[" transfer" ] = transfer.get ();
320- if (build) {
321- handlers[" build" ] = build.get ();
322- }
323- move_ptr->set_action_handlers (handlers);
324-
325- _action_handler_impl.push_back (std::move (attack));
326- _action_handler_impl.push_back (std::move (transfer));
327- if (build) {
328- _action_handler_impl.push_back (std::move (build));
277+ if (game_config.actions .find (" attack" ) != game_config.actions .end ()) {
278+ auto attack_config = std::static_pointer_cast<const AttackActionConfig>(game_config.actions .at (" attack" ));
279+ auto attack = std::make_unique<Attack>(*attack_config, &game_config);
280+ attack->init (_grid.get (), &_rng);
281+ if (attack->priority > _max_action_priority) _max_action_priority = attack->priority ;
282+ for (const auto & action : attack->actions ()) {
283+ _action_handlers.push_back (action);
284+ }
285+ _action_handler_impl.push_back (std::move (attack));
329286 }
330287
331288 // ChangeVibe
332- auto change_vibe_config =
333- std::static_pointer_cast<const ChangeVibeActionConfig>(_game_config.actions .at (" change_vibe" ));
334- auto change_vibe = std::make_unique<ChangeVibe>(*change_vibe_config, &_game_config);
335- change_vibe->init (_grid.get (), &_rng);
336- if (change_vibe->priority > _max_action_priority) _max_action_priority = change_vibe->priority ;
337- for (const auto & action : change_vibe->actions ()) {
338- _action_handlers.push_back (action);
289+ if (game_config.actions .find (" change_vibe" ) != game_config.actions .end ()) {
290+ auto change_vibe_config =
291+ std::static_pointer_cast<const ChangeVibeActionConfig>(game_config.actions .at (" change_vibe" ));
292+ auto change_vibe = std::make_unique<ChangeVibe>(*change_vibe_config, &game_config);
293+ change_vibe->init (_grid.get (), &_rng);
294+ if (change_vibe->priority > _max_action_priority) _max_action_priority = change_vibe->priority ;
295+ for (const auto & action : change_vibe->actions ()) {
296+ _action_handlers.push_back (action);
297+ }
298+ _action_handler_impl.push_back (std::move (change_vibe));
339299 }
340- _action_handler_impl.push_back (std::move (change_vibe));
341300}
342301
343302void MettaGrid::add_agent (Agent* agent) {
@@ -1091,10 +1050,6 @@ PYBIND11_MODULE(mettagrid_c, m) {
10911050 bind_chest_config (m);
10921051 bind_action_config (m);
10931052 bind_attack_action_config (m);
1094- bind_vibe_transfer_effect (m);
1095- bind_transfer_action_config (m);
1096- bind_vibe_build_effect (m);
1097- bind_build_action_config (m);
10981053 bind_change_vibe_action_config (m);
10991054 bind_move_action_config (m);
11001055 bind_global_obs_config (m);
0 commit comments