Skip to content

Commit

Permalink
feat: Support Chat Action
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhoues committed Feb 8, 2024
1 parent 6b50f76 commit 14c5c48
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
3 changes: 3 additions & 0 deletions minerl/env/_multiagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ def _process_action(self, actor_name, action_in) -> str:
action_str = []
for h in bottom_env_spec.actionables:
if h.to_string() in action_in:
# NOTE Modified: Support Chat Action
if h.to_string() == "chat" and action_in[h.to_string()] == "":
continue
action_str.append(h.to_hero(action_in[h.to_string()]))

return "\n".join(action_str)
Expand Down
11 changes: 11 additions & 0 deletions minerl/env/_singleagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ def step(self, single_agent_action: Dict[str, Any]) -> Tuple[

return obs[aname], rew[aname], done, info[aname]

# NOTE Modified: Support Chat Action
def execute_cmd(self, command: str) -> Tuple[
Dict[str, Any], float, bool, Dict[str, Any]]:

single_agent_action = self.noop_action()

assert "chat" in single_agent_action, "Please modify the agent's action space to support chat actions."
single_agent_action["chat"] = command

return self.step(single_agent_action = single_agent_action)

def render(self, mode='human'):
return super().render(mode)[self.task.agent_names[0]]

Expand Down
3 changes: 2 additions & 1 deletion minerl/herobraine/env_specs/human_controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ def create_actionables(self) -> List[TranslationHandler]:
Simple envs have some basic keyboard control functionality, but
not all.
"""
# NOTE Modified: Support Chat Action
return [
H.KeybasedCommandAction(v, v) for v in mc.KEYMAP.values()
] + [H.CameraAction()]
] + [H.CameraAction()] + [H.ChatAction()]

def create_monitors(self) -> List[TranslationHandler]:
return [H.IsGuiOpen(), H.ObservationFromCurrentLocation()]
Expand Down
3 changes: 2 additions & 1 deletion minerl/herobraine/hero/handlers/agent/actions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Copyright (c) 2020 All Rights Reserved
# Author: William H. Guss, Brandon Houghton


# NOTE Modified: Support Chat Action
from .camera import *
from .chat import *
from .mousewheel import *
from .craft import *
from .equip import *
Expand Down
42 changes: 42 additions & 0 deletions minerl/herobraine/hero/handlers/agent/actions/chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# NOTE Modified: Support Chat Action

from minerl.herobraine.hero.handlers.agent.action import Action
import minerl.herobraine.hero.spaces as spaces

# TODO add more command support (things like what commands are allowed) from Malmo
# TODO ensure other agents can send chats, not just first agent (again, check Malmo)
class ChatAction(Action):
"""
Handler which lets agents send Minecraft chat messages
Note: this may currently be limited to the
first agent sending messages (check Malmo for this)
This can be used to execute MINECRAFT COMMANDS !!!
Example usage:
.. code-block:: python
ChatAction()
To summon a creeper, use this action dictionary:
.. code-block:: json
{"chat": "/summon creeper"}
"""

def to_string(self):
return 'chat'

def xml_template(self) -> str:
return str("<ChatCommands> </ChatCommands>")

def __init__(self):
self._command = 'chat'
super().__init__(self.command, spaces.Text([1]))

def from_universal(self, x):
return []
7 changes: 5 additions & 2 deletions minerl/human_play_interface/human_play_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

KEYBOARD_TO_MINERL_ACTION = {v: k for k, v in MINERL_ACTION_TO_KEYBOARD.items()}

# NOTE Modified: Support Chat Action
IGNORED_ACTIONS = {"chat"}

# Camera actions are in degrees, while mouse movement is in pixels
# Multiply mouse speed by some arbitrary multiplier
Expand Down Expand Up @@ -113,15 +115,16 @@ def _on_mouse_drag(self, x, y, dx, dy, button, modifier):
self.last_mouse_delta[0] -= dy * MOUSE_MULTIPLIER
self.last_mouse_delta[1] += dx * MOUSE_MULTIPLIER

# NOTE Modified: Support Chat Action
def _validate_minerl_env(self, minerl_env):
"""Make sure we have a valid MineRL environment. Raises if not."""
# Make sure action has right items
remaining_buttons = set(MINERL_ACTION_TO_KEYBOARD.keys())
remaining_buttons = set(MINERL_ACTION_TO_KEYBOARD.keys()).union(IGNORED_ACTIONS)
remaining_buttons.add("camera")
for action_name, action_space in minerl_env.action_space.spaces.items():
if action_name not in remaining_buttons:
raise RuntimeError(f"Invalid MineRL action space: action {action_name} is not supported.")
elif (not isinstance(action_space, spaces.Discrete) or action_space.n != 2) and action_name != "camera":
elif not action_name in IGNORED_ACTIONS and (not isinstance(action_space, spaces.Discrete) or action_space.n != 2) and action_name != "camera":
raise RuntimeError(f"Invalid MineRL action space: action {action_name} had space {action_space}. Only Discrete(2) is supported.")
remaining_buttons.remove(action_name)
if len(remaining_buttons) > 0:
Expand Down

0 comments on commit 14c5c48

Please sign in to comment.