Skip to content

🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming

init environment

better6299071a4281452
diff --git a/metagpt/const.py b/metagpt/const.py
index 0ae425a47..e9297aab1 100644
--- a/metagpt/const.py
+++ b/metagpt/const.py
@@ -126,3 +126,6 @@ IGNORED_MESSAGE_ID = "0"
GENERALIZATION = "Generalize"
COMPOSITION = "Composite"
AGGREGATION = "Aggregate"
+
+# Others
+ADB_EXEC_FAIL = "FAILED"
diff --git a/metagpt/environment/__init__.py b/metagpt/environment/__init__.py
new file mode 100644
index 000000000..2bcf8efd0
--- /dev/null
+++ b/metagpt/environment/__init__.py
@@ -0,0 +1,3 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc :
diff --git a/metagpt/environment/android_env/__init__.py b/metagpt/environment/android_env/__init__.py
new file mode 100644
index 000000000..2bcf8efd0
--- /dev/null
+++ b/metagpt/environment/android_env/__init__.py
@@ -0,0 +1,3 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc :
diff --git a/metagpt/environment/android_env/android_env.py b/metagpt/environment/android_env/android_env.py
new file mode 100644
index 000000000..c6058aa4a
--- /dev/null
+++ b/metagpt/environment/android_env/android_env.py
@@ -0,0 +1,9 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc : MG Android Env
+
+from metagpt.env.android_env.android_ext_env import AndroidExtEnv
+
+
+class AndroidEnv(AndroidExtEnv):
+ pass
diff --git a/metagpt/environment/android_env/android_ext_env.py b/metagpt/environment/android_env/android_ext_env.py
new file mode 100644
index 000000000..d7a63b5e7
--- /dev/null
+++ b/metagpt/environment/android_env/android_ext_env.py
@@ -0,0 +1,150 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc : The Android external environment to integrate with Android apps
+
+import subprocess
+from pathlib import Path
+from typing import Optional
+
+from pydantic import Field
+
+from metagpt.const import ADB_EXEC_FAIL
+from metagpt.env.base_env import ExtEnv, mark_as_readable, mark_as_writeable
+
+
+class AndroidExtEnv(ExtEnv):
+ device_id: Optional[str] = Field(default=None)
+ screenshot_dir: Optional[Path] = Field(default=None)
+ xml_dir: Optional[Path] = Field(default=None)
+ width: int = Field(default=720, description="device screen width")
+ height: int = Field(default=1080, description="device screen height")
+
+ @property
+ def adb_prefix_si(self):
+ """adb cmd prefix with `device_id` and `shell input`"""
+ return f"adb -s {self.device_id} shell input "
+
+ @property
+ def adb_prefix_shell(self):
+ """adb cmd prefix with `device_id` and `shell`"""
+ return f"adb -s {self.device_id} shell "
+
+ @property
+ def adb_prefix(self):
+ """adb cmd prefix with `device_id`"""
+ return f"adb -s {self.device_id} "
+
+ def execute_adb_with_cmd(self, adb_cmd: str) -> str:
+ res = subprocess.run(adb_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
+ exec_res = ADB_EXEC_FAIL
+ if not res.returncode:
+ exec_res = res.stdout.strip()
+ return exec_res
+
+ @property
+ def device_shape(self) -> tuple[int, int]:
+ adb_cmd = f"{self.adb_prefix_shell} wm size"
+ shape = (0, 0)
+ shape_res = self.execute_adb_with_cmd(adb_cmd)
+ if shape_res != ADB_EXEC_FAIL:
+ shape = map(int, shape_res.split(": ")[1].split("x"))
+ return shape
+
+ def list_devices(self):
+ adb_cmd = "adb devices"
+ res = self.execute_adb_with_cmd(adb_cmd)
+ devices = []
+ if res != ADB_EXEC_FAIL:
+ devices = res.split("\n")[1:]
+ devices = [device.split()[0] for device in devices]
+ return devices
+
+ @mark_as_readable
+ def get_screenshot(self, ss_name: str, local_save_dir: Path) -> str:
+ """
+ ss_name: screenshot file name
+ local_save_dir: local dir to store image from virtual machine
+ """
+ assert self.screenshot_dir
+ ss_remote_path = str(Path(self.screenshot_dir.joinpath(f"{ss_name}.png")))
+ ss_cmd = f"{self.adb_prefix_shell} screencap -p {ss_remote_path}"
+ ss_res = self.execute_adb_with_cmd(ss_cmd)
+
+ res = ADB_EXEC_FAIL
+ if ss_res != ADB_EXEC_FAIL:
+ ss_local_path = str(Path(local_save_dir.joinpath(f"{ss_name}.png")))
+ pull_cmd = f"{self.adb_prefix} pull {ss_remote_path} {ss_local_path}"
+ pull_res = self.execute_adb_with_cmd(pull_cmd)
+ if pull_res != ADB_EXEC_FAIL:
+ res = ss_local_path
+ return res
+
+ @mark_as_readable
+ def get_xml(self, xml_name: str, local_save_dir: Path) -> str:
+ xml_remote_path = str(Path(self.xml_dir).joinpath(f"{xml_name}.xml"))
+ dump_cmd = f"{self.adb_prefix_shell} uiautomator dump {xml_remote_path}"
+ xml_res = self.execute_adb_with_cmd(dump_cmd)
+
+ res = ADB_EXEC_FAIL
+ if xml_res != ADB_EXEC_FAIL:
+ xml_local_path = str(Path(local_save_dir.joinpath(f"{xml_name}.xml")))
+ pull_cmd = f"{self.adb_prefix} pull {xml_remote_path} {xml_local_path}"
+ pull_res = self.execute_adb_with_cmd(pull_cmd)
+ if pull_res != ADB_EXEC_FAIL:
+ res = xml_local_path
+ return res
+
+ @mark_as_writeable
+ def system_back(self) -> str:
+ adb_cmd = f"{self.adb_prefix_si} keyevent KEYCODE_BACK"
+ back_res = self.execute_adb_with_cmd(adb_cmd)
+ return back_res
+
+ @mark_as_writeable
+ def system_tap(self, x: int, y: int) -> str:
+ adb_cmd = f"{self.adb_prefix_si} tap {x} {y}"
+ tap_res = self.execute_adb_with_cmd(adb_cmd)
+ return tap_res
+
+ @mark_as_writeable
+ def user_input(self, input_txt: str) -> str:
+ input_txt = input_txt.replace(" ", "%s").replace("'", "")
+ adb_cmd = f"{self.adb_prefix_si} text {input_txt}"
+ input_res = self.execute_adb_with_cmd(adb_cmd)
+ return input_res
+
+ @mark_as_writeable
+ def user_longpress(self, x: int, y: int, duration: int = 500) -> str:
+ adb_cmd = f"{self.adb_prefix_si} swipe {x} {y} {x} {y} {duration}"
+ press_res = self.execute_adb_with_cmd(adb_cmd)
+ return press_res
+
+ @mark_as_writeable
+ def user_swipe(self, x: int, y: int, dir: str = "up", dist: str = "medium", if_quick: bool = False) -> str:
+ dist_unit = int(self.width / 10)
+ if dist == "long":
+ dist_unit *= 3
+ elif dist == "medium":
+ dist_unit *= 2
+
+ if dir == "up":
+ offset = 0, -2 * dist_unit
+ elif dir == "down":
+ offset = 0, 2 * dist_unit
+ elif dir == "left":
+ offset = -1 * dist_unit, 0
+ elif dir == "right":
+ offset = dist_unit, 0
+ else:
+ return ADB_EXEC_FAIL
+
+ duration = 100 if quick else 400
+ adb_cmd = f"{self.adb_prefix_si} swipe {x} {y} {x + offset[0]} {y + offset[1]} {duration}"
+ swipe_res = self.execute_adb_with_cmd(adb_cmd)
+ return swipe_res
+
+ @mark_as_writeable
+ def user_swipe_to(self, start: tuple[int, int], end: tuple[int, int], duration: int = 400):
+ adb_cmd = f"{self.adb_prefix_si} swipe {start[0]} {start[1]} {end[0]} {end[1]} {duration}"
+ swipe_res = self.execute_adb_with_cmd(adb_cmd)
+ return swipe_res
diff --git a/metagpt/environment/api/__init__.py b/metagpt/environment/api/__init__.py
new file mode 100644
index 000000000..2bcf8efd0
--- /dev/null
+++ b/metagpt/environment/api/__init__.py
@@ -0,0 +1,3 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc :
diff --git a/metagpt/environment/api/env_api.py b/metagpt/environment/api/env_api.py
new file mode 100644
index 000000000..bb7a75243
--- /dev/null
+++ b/metagpt/environment/api/env_api.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc : the environment api store
+
+from typing import Callable
+
+from pydantic import BaseModel, Field
+
+
+class EnvAPIAbstract(BaseModel):
+ """api/interface summary description"""
+
+ api_name: str = Field(default="", description="the api function name or id")
+ args: set = Field(default={}, description="the api function `args` params")
+ kwargs: dict = Field(default=dict(), description="the api function `kwargs` params")
+
+
+class EnvAPIRegistry(BaseModel):
+ """the registry to store environment w&r api/interface"""
+
+ registry: dict[str, Callable] = Field(default=dict(), include=False)
+
+ def get(self, api_name: str):
+ return self.registry.get(api_name)
+
+
+class WriteAPIRegistry(EnvAPIRegistry):
+ """just as a new class name"""
+
+ pass
+
+
+class ReadAPIRegistry(EnvAPIRegistry):
+ """just as a new class name"""
+
+ pass
diff --git a/metagpt/environment/api/read_api.py b/metagpt/environment/api/read_api.py
new file mode 100644
index 000000000..7b0076ce7
--- /dev/null
+++ b/metagpt/environment/api/read_api.py
@@ -0,0 +1,3 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc : the readable api/interface abstraction to integrate with environment
diff --git a/metagpt/environment/api/write_api.py b/metagpt/environment/api/write_api.py
new file mode 100644
index 000000000..de6665592
--- /dev/null
+++ b/metagpt/environment/api/write_api.py
@@ -0,0 +1,3 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc : the writable api/interface abstraction to integrate with environment
diff --git a/metagpt/environment/base_env.py b/metagpt/environment/base_env.py
new file mode 100644
index 000000000..4c25ae044
--- /dev/null
+++ b/metagpt/environment/base_env.py
@@ -0,0 +1,73 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc : base env of executing environment
+
+from enum import Enum
+from typing import Union
+
+from pydantic import BaseModel, ConfigDict, Field
+
+from metagpt.env.api.env_api import EnvAPIAbstract, ReadAPIRegistry, WriteAPIRegistry
+from metagpt.schema import Message
+
+
+class EnvType(Enum):
+ ANDROID = "Android"
+ GYM = "Gym"
+ WEREWOLF = "Werewolf"
+ MINCRAFT = "Minsraft"
+ STANFORDTOWN = "StanfordTown"
+
+
+def mark_as_readable(func):
+ """mark functionn as a readable one in ExtEnv, it observes something from ExtEnv"""
+
+ def wrapper(self: ExtEnv, *args, **kwargs):
+ api_name = str(func) # TODO
+ self.read_api_registry[api_name] = func
+ return func(self, *args, **kwargs)
+
+ return wrapper
+
+
+def mark_as_writeable(func):
+ """mark functionn as a writeable one in ExtEnv, it do something to ExtEnv"""
+
+ def wrapper(self: ExtEnv, *args, **kwargs):
+ api_name = str(func) # TODO
+ self.write_api_registry[api_name] = func
+ return func(self, *args, **kwargs)
+
+ return wrapper
+
+
+class ExtEnv(BaseModel):
+ """External Env to intergate actual game environment"""
+
+ write_api_registry: WriteAPIRegistry = Field(default_factory=WriteAPIRegistry, include=False)
+ read_api_registry: ReadAPIRegistry = Field(default_factory=ReadAPIRegistry, include=False)
+
+
+class Env(ExtEnv):
+ """Env to intergate with MetaGPT"""
+
+ model_config = ConfigDict(arbitrary_types_allowed=True)
+
+ def observe(self, env_action: Union[str, EnvAPIAbstract]):
+ api_name = env_action.api_name if isinstance(env_action, EnvAPIAbstract) else env_action
+ read_api = self.read_api_registry.get(api_name)
+ res = read_api(*env_action.args, **env_action.kwargs)
+ return res
+
+ def step(self, env_action: Union[str, Message, EnvAPIAbstract, list[EnvAPIAbstract]]):
+ res = None
+ if isinstance(env_action, Message):
+ self.publish_message(env_action)
+ elif isinstance(env_action, EnvAPIAbstract):
+ write_api = self.write_api_registry.get(env_action.api_name)
+ res = write_api(*env_action.args, **env_action.kwargs)
+
+ return res
+
+ def publish_message(self, message: "Message"):
+ pass
diff --git a/metagpt/environment/gym_env/__init__.py b/metagpt/environment/gym_env/__init__.py
new file mode 100644
index 000000000..2bcf8efd0
--- /dev/null
+++ b/metagpt/environment/gym_env/__init__.py
@@ -0,0 +1,3 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc :
diff --git a/metagpt/environment/gym_env/gym_env.py b/metagpt/environment/gym_env/gym_env.py
new file mode 100644
index 000000000..2bcf8efd0
--- /dev/null
+++ b/metagpt/environment/gym_env/gym_env.py
@@ -0,0 +1,3 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc :
diff --git a/metagpt/environment/mincraft_env/__init__.py b/metagpt/environment/mincraft_env/__init__.py
new file mode 100644
index 000000000..2bcf8efd0
--- /dev/null
+++ b/metagpt/environment/mincraft_env/__init__.py
@@ -0,0 +1,3 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc :
diff --git a/metagpt/environment/mincraft_env/mincraft_env.py b/metagpt/environment/mincraft_env/mincraft_env.py
new file mode 100644
index 000000000..2bcf8efd0
--- /dev/null
+++ b/metagpt/environment/mincraft_env/mincraft_env.py
@@ -0,0 +1,3 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc :
diff --git a/metagpt/environment/mincraft_env/mincraft_ext_env.py b/metagpt/environment/mincraft_env/mincraft_ext_env.py
new file mode 100644
index 000000000..2bcf8efd0
--- /dev/null
+++ b/metagpt/environment/mincraft_env/mincraft_ext_env.py
@@ -0,0 +1,3 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc :
diff --git a/metagpt/environment/software_env/__init__.py b/metagpt/environment/software_env/__init__.py
new file mode 100644
index 000000000..2bcf8efd0
--- /dev/null
+++ b/metagpt/environment/software_env/__init__.py
@@ -0,0 +1,3 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc :
diff --git a/metagpt/environment/software_env/software_env.py b/metagpt/environment/software_env/software_env.py
new file mode 100644
index 000000000..53c832071
--- /dev/null
+++ b/metagpt/environment/software_env/software_env.py
@@ -0,0 +1,9 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc : MG Software Env
+
+from metagpt.env.base_env import Env
+
+
+class SoftwareEnv(Env):
+ pass
diff --git a/metagpt/environment/stanford_town_env/__init__.py b/metagpt/environment/stanford_town_env/__init__.py
new file mode 100644
index 000000000..2bcf8efd0
--- /dev/null
+++ b/metagpt/environment/stanford_town_env/__init__.py
@@ -0,0 +1,3 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc :
diff --git a/metagpt/environment/stanford_town_env/stanford_town_env.py b/metagpt/environment/stanford_town_env/stanford_town_env.py
new file mode 100644
index 000000000..41489732d
--- /dev/null
+++ b/metagpt/environment/stanford_town_env/stanford_town_env.py
@@ -0,0 +1,9 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc : MG StanfordTown Env
+
+from metagpt.env.stanford_town_env.stanford_town_ext_env import StanfordTownExtEnv
+
+
+class StanfordTownEnv(StanfordTownExtEnv):
+ pass
diff --git a/metagpt/environment/stanford_town_env/stanford_town_ext_env.py b/metagpt/environment/stanford_town_env/stanford_town_ext_env.py
new file mode 100644
index 000000000..04e4f07e0
--- /dev/null
+++ b/metagpt/environment/stanford_town_env/stanford_town_ext_env.py
@@ -0,0 +1,373 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc : The StanfordTown external environment to interate with the web interface
+
+from pathlib import Path
+from typing import Optional, Tuple
+
+from pydantic import Field, model_validator
+
+from metagpt.env.base_env import ExtEnv, mark_as_readable, mark_as_writeable
+from metagpt.utils.common import read_json_file
+
+
+class StanfordTownExtEnv(ExtEnv):
+ maze_asset_path: Optional[Path] = Field(default=None, description="the path to store maze assets")
+ maze_width: int = Field(default=140)
+ meze_height: int = Field(default=100)
+ sq_tile_size: int = Field(default=32, description="the pixel height/width of a tile")
+ special_constraint: str = Field(
+ default="", description="a string description of any relevant special constraints " "the world might have"
+ )
+ titles: list[list[dict]] = Field(default=[])
+ address_tiles: dict[set] = Field(default={})
+ collision_maze: list[list] = Field(default=[])
+
+ @model_validator(mode="before")
+ @classmethod
+ def _init_maze(cls, values: dict):
+ maze_asset_path = values.get("maze_asset_path")
+ assert maze_asset_path
+ maze_asset_path = Path(maze_asset_path)
+
+ maze_matrix_path = maze_asset_path.joinpath("matrix")
+ meta_info = read_json_file(maze_matrix_path.joinpath("maze_meta_info.json"))
+
+ values["maze_width"] = int(meta_info["maze_width"])
+ values["meze_height"] = int(meta_info["meze_height"])
+ values["sq_tile_size"] = int(meta_info["sq_tile_size"])
+ values["special_constraint"] = meta_info["special_constraint"]
+
+ # READING IN SPECIAL BLOCKS
+ # Special blocks are those that are colored in the Tiled map.
+ # Here is an example row for the arena block file:
+ # e.g, "25331, Double Studio, Studio, Bedroom 2, Painting"
+
+ blocks_folder = maze_matrix_path.joinpath("special_blocks")
+
+ _wb = blocks_folder.joinpath("world_blocks.csv")
+ wb_rows = read_csv_to_list(_wb, header=False)
+ wb = wb_rows[0][-1]
+
+ _sb = blocks_folder.joinpath("sector_blocks.csv")
+ sb_rows = read_csv_to_list(_sb, header=False)
+ sb_dict = dict()
+ for i in sb_rows:
+ sb_dict[i[0]] = i[-1]
+
+ _ab = blocks_folder.joinpath("arena_blocks.csv")
+ ab_rows = read_csv_to_list(_ab, header=False)
+ ab_dict = dict()
+ for i in ab_rows:
+ ab_dict[i[0]] = i[-1]
+
+ _gob = blocks_folder.joinpath("game_object_blocks.csv")
+ gob_rows = read_csv_to_list(_gob, header=False)
+ gob_dict = dict()
+ for i in gob_rows:
+ gob_dict[i[0]] = i[-1]
+
+ _slb = blocks_folder.joinpath("spawning_location_blocks.csv")
+ slb_rows = read_csv_to_list(_slb, header=False)
+ slb_dict = dict()
+ for i in slb_rows:
+ slb_dict[i[0]] = i[-1]
+
+ # [SECTION 3] Reading in the matrices
+ # This is your typical two dimensional matrices. It's made up of 0s and
+ # the number that represents the color block from the blocks folder.
+ maze_folder = maze_matrix_path.joinpath("maze")
+
+ _cm = maze_folder.joinpath("collision_maze.csv")
+ collision_maze_raw = read_csv_to_list(_cm, header=False)[0]
+ _sm = maze_folder.joinpath("sector_maze.csv")
+ sector_maze_raw = read_csv_to_list(_sm, header=False)[0]
+ _am = maze_folder.joinpath("arena_maze.csv")
+ arena_maze_raw = read_csv_to_list(_am, header=False)[0]
+ _gom = maze_folder.joinpath("game_object_maze.csv")
+ game_object_maze_raw = read_csv_to_list(_gom, header=False)[0]
+ _slm = maze_folder.joinpath("spawning_location_maze.csv")
+ spawning_location_maze_raw = read_csv_to_list(_slm, header=False)[0]
+
+ # Loading the maze. The mazes are taken directly from the json exports of
+ # Tiled maps. They should be in csv format.
+ # Importantly, they are "not" in a 2-d matrix format -- they are single
+ # row matrices with the length of width x height of the maze. So we need
+ # to convert here.
+ # example format: [['0', '0', ... '25309', '0',...], ['0',...]...]
+ # 25309 is the collision bar number right now.
+ collision_maze = []
+ sector_maze = []
+ arena_maze = []
+ game_object_maze = []
+ spawning_location_maze = []
+ for i in range(0, len(collision_maze_raw), meta_info["maze_width"]):
+ tw = meta_info["maze_width"]
+ collision_maze += [collision_maze_raw[i : i + tw]]
+ sector_maze += [sector_maze_raw[i : i + tw]]
+ arena_maze += [arena_maze_raw[i : i + tw]]
+ game_object_maze += [game_object_maze_raw[i : i + tw]]
+ spawning_location_maze += [spawning_location_maze_raw[i : i + tw]]
+ values["collision_maze"] = collision_maze
+
+ tiles = []
+ for i in range(maze_height):
+ row = []
+ for j in range(maze_width):
+ tile_details = dict()
+ tile_details["world"] = wb
+
+ tile_details["sector"] = ""
+ if sector_maze[i][j] in sb_dict:
+ tile_details["sector"] = sb_dict[sector_maze[i][j]]
+
+ tile_details["arena"] = ""
+ if arena_maze[i][j] in ab_dict:
+ tile_details["arena"] = ab_dict[arena_maze[i][j]]
+
+ tile_details["game_object"] = ""
+ if game_object_maze[i][j] in gob_dict:
+ tile_details["game_object"] = gob_dict[game_object_maze[i][j]]
+
+ tile_details["spawning_location"] = ""
+ if spawning_location_maze[i][j] in slb_dict:
+ tile_details["spawning_location"] = slb_dict[spawning_location_maze[i][j]]
+
+ tile_details["collision"] = False
+ if collision_maze[i][j] != "0":
+ tile_details["collision"] = True
+
+ tile_details["events"] = set()
+
+ row += [tile_details]
+ tiles += [row]
+ values["tiles"] = tiles
+
+ # Each game object occupies an event in the tile. We are setting up the
+ # default event value here.
+ for i in range(maze_height):
+ for j in range(maze_width):
+ if tiles[i][j]["game_object"]:
+ object_name = ":".join(
+ [tiles[i][j]["world"], tiles[i][j]["sector"], tiles[i][j]["arena"], tiles[i][j]["game_object"]]
+ )
+ go_event = (object_name, None, None, None)
+ tiles[i][j]["events"].add(go_event)
+
+ # Reverse tile access.
+ # <address_tiles> -- given a string address, we return a set of all
+ # tile coordinates belonging to that address (this is opposite of
+ # tiles that give you the string address given a coordinate). This is
+ # an optimization component for finding paths for the personas' movement.
+ # address_tiles['<spawn_loc>bedroom-2-a'] == {(58, 9)}
+ # address_tiles['double studio:recreation:pool table']
+ # == {(29, 14), (31, 11), (30, 14), (32, 11), ...},
+ address_tiles = dict()
+ for i in range(maze_height):
+ for j in range(maze_width):
+ addresses = []
+ if tiles[i][j]["sector"]:
+ add = f'{tiles[i][j]["world"]}:'
+ add += f'{tiles[i][j]["sector"]}'
+ addresses += [add]
+ if tiles[i][j]["arena"]:
+ add = f'{tiles[i][j]["world"]}:'
+ add += f'{tiles[i][j]["sector"]}:'
+ add += f'{tiles[i][j]["arena"]}'
+ addresses += [add]
+ if tiles[i][j]["game_object"]:
+ add = f'{tiles[i][j]["world"]}:'
+ add += f'{tiles[i][j]["sector"]}:'
+ add += f'{tiles[i][j]["arena"]}:'
+ add += f'{tiles[i][j]["game_object"]}'
+ addresses += [add]
+ if tiles[i][j]["spawning_location"]:
+ add = f'<spawn_loc>{tiles[i][j]["spawning_location"]}'
+ addresses += [add]
+
+ for add in addresses:
+ if add in address_tiles:
+ address_tiles[add].add((j, i))
+ else:
+ address_tiles[add] = set([(j, i)])
+ values["address_tiles"] = address_tiles
+ return values
+
+ def turn_coordinate_to_tile(self, px_coordinate: tuple[int, int]) -> tuple[int, int]:
+ """
+ Turns a pixel coordinate to a tile coordinate.
+ """
+ x = math.ceil(px_coordinate[0] / self.sq_tile_size)
+ y = math.ceil(px_coordinate[1] / self.sq_tile_size)
+ return (x, y)
+
+ @mark_as_readable
+ def get_collision_maze(self) -> list:
+ return self.collision_maze
+
+ @mark_as_readable
+ def get_address_tiles(self) -> dict:
+ return self.address_tiles
+
+ @mark_as_readable
+ def access_tile(self, tile: tuple[int, int]) -> dict:
+ """
+ Returns the tiles details dictionary that is stored in self.tiles of the
+ designated x, y location.
+
+ INPUT
+ tile: The tile coordinate of our interest in (x, y) form.
+ OUTPUT
+ The tile detail dictionary for the designated tile.
+ EXAMPLE OUTPUT
+ Given (58, 9),
+ self.tiles[9][58] = {'world': 'double studio',
+ 'sector': 'double studio', 'arena': 'bedroom 2',
+ 'game_object': 'bed', 'spawning_location': 'bedroom-2-a',
+ 'collision': False,
+ 'events': {('double studio:double studio:bedroom 2:bed',
+ None, None)}}
+ """
+ x = tile[0]
+ y = tile[1]
+ return self.tiles[y][x]
+
+ @mark_as_readable
+ def get_tile_path(self, tile: tuple[int, int], level: str) -> str:
+ """
+ Get the tile string address given its coordinate. You designate the level
+ by giving it a string level description.
+
+ INPUT:
+ tile: The tile coordinate of our interest in (x, y) form.
+ level: world, sector, arena, or game object
+ OUTPUT
+ The string address for the tile.
+ EXAMPLE OUTPUT
+ Given tile=(58, 9), and level=arena,
+ "double studio:double studio:bedroom 2"
+ """
+ x = tile[0]
+ y = tile[1]
+ tile = self.tiles[y][x]
+
+ path = f"{tile['world']}"
+ if level == "world":
+ return path
+ else:
+ path += f":{tile['sector']}"
+
+ if level == "sector":
+ return path
+ else:
+ path += f":{tile['arena']}"
+
+ if level == "arena":
+ return path
+ else:
+ path += f":{tile['game_object']}"
+
+ return path
+
+ @mark_as_readable
+ def get_nearby_tiles(self, tile: tuple[int, int], vision_r: int) -> list[tuple[int, int]]:
+ """
+ Given the current tile and vision_r, return a list of tiles that are
+ within the radius. Note that this implementation looks at a square
+ boundary when determining what is within the radius.
+ i.e., for vision_r, returns x's.
+ x x x x x
+ x x x x x
+ x x P x x
+ x x x x x
+ x x x x x
+
+ INPUT:
+ tile: The tile coordinate of our interest in (x, y) form.
+ vision_r: The radius of the persona's vision.
+ OUTPUT:
+ nearby_tiles: a list of tiles that are within the radius.
+ """
+ left_end = 0
+ if tile[0] - vision_r > left_end:
+ left_end = tile[0] - vision_r
+
+ right_end = self.maze_width - 1
+ if tile[0] + vision_r + 1 < right_end:
+ right_end = tile[0] + vision_r + 1
+
+ bottom_end = self.maze_height - 1
+ if tile[1] + vision_r + 1 < bottom_end:
+ bottom_end = tile[1] + vision_r + 1
+
+ top_end = 0
+ if tile[1] - vision_r > top_end:
+ top_end = tile[1] - vision_r
+
+ nearby_tiles = []
+ for i in range(left_end, right_end):
+ for j in range(top_end, bottom_end):
+ nearby_tiles += [(i, j)]
+ return nearby_tiles
+
+ @mark_as_writeable
+ def add_tiles_event(self, pt_y: int, pt_x: int, event: Tuple[str, str, str, str]):
+ self.tiles[pt_y][pt_x]["events"].add(event)
+
+ @mark_as_writeable
+ def add_event_from_tile(self, curr_event: tuple[str], tile: tuple[int, int]) -> None:
+ """
+ Add an event triple to a tile.
+
+ INPUT:
+ curr_event: Current event triple.
+ e.g., ('double studio:double studio:bedroom 2:bed', None,
+ None)
+ tile: The tile coordinate of our interest in (x, y) form.
+ OUPUT:
+ None
+ """
+ self.tiles[tile[1]][tile[0]]["events"].add(curr_event)
+
+ @mark_as_writeable
+ def remove_event_from_tile(self, curr_event: tuple[str], tile: tuple[int, int]) -> None:
+ """dswaq
+ Remove an event triple from a tile.
+
+ INPUT:
+ curr_event: Current event triple.
+ e.g., ('double studio:double studio:bedroom 2:bed', None,
+ None)
+ tile: The tile coordinate of our interest in (x, y) form.
+ OUPUT:
+ None
+ """
+ curr_tile_ev_cp = self.tiles[tile[1]][tile[0]]["events"].copy()
+ for event in curr_tile_ev_cp:
+ if event == curr_event:
+ self.tiles[tile[1]][tile[0]]["events"].remove(event)
+
+ @mark_as_writeable
+ def turn_event_from_tile_idle(self, curr_event: tuple[str], tile: tuple[int, int]) -> None:
+ curr_tile_ev_cp = self.tiles[tile[1]][tile[0]]["events"].copy()
+ for event in curr_tile_ev_cp:
+ if event == curr_event:
+ self.tiles[tile[1]][tile[0]]["events"].remove(event)
+ new_event = (event[0], None, None, None)
+ self.tiles[tile[1]][tile[0]]["events"].add(new_event)
+
+ @mark_as_writeable
+ def remove_subject_events_from_tile(self, subject: str, tile: tuple[int, int]) -> None:
+ """
+ Remove an event triple that has the input subject from a tile.
+
+ INPUT:
+ subject: "Isabella Rodriguez"
+ tile: The tile coordinate of our interest in (x, y) form.
+ OUPUT:
+ None
+ """
+ curr_tile_ev_cp = self.tiles[tile[1]][tile[0]]["events"].copy()
+ for event in curr_tile_ev_cp:
+ if event[0] == subject:
+ self.tiles[tile[1]][tile[0]]["events"].remove(event)
diff --git a/metagpt/environment/werewolf_env/__init__.py b/metagpt/environment/werewolf_env/__init__.py
new file mode 100644
index 000000000..2bcf8efd0
--- /dev/null
+++ b/metagpt/environment/werewolf_env/__init__.py
@@ -0,0 +1,3 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc :
diff --git a/metagpt/environment/werewolf_env/werewolf_env.py b/metagpt/environment/werewolf_env/werewolf_env.py
new file mode 100644
index 000000000..29e9f9b81
--- /dev/null
+++ b/metagpt/environment/werewolf_env/werewolf_env.py
@@ -0,0 +1,9 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc : MG Werewolf Env
+
+from metagpt.env.werewolf_env.werewolf_ext_env import WerewolfExtEnv
+
+
+class WerewolfEnv(WerewolfExtEnv):
+ pass
diff --git a/metagpt/environment/werewolf_env/werewolf_ext_env.py b/metagpt/environment/werewolf_env/werewolf_ext_env.py
new file mode 100644
index 000000000..8543ce246
--- /dev/null
+++ b/metagpt/environment/werewolf_env/werewolf_ext_env.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Desc : The werewolf game external environment to integrate with
+
+from enum import Enum
+
+from pydantic import Field
+
+from metagpt.env.base_env import ExtEnv, mark_as_readable, mark_as_writeable
+
+
+class RoleState(Enum):
+ ALIVE = "alive"
+ KILLED = "killed"
+ POISONED = "poisoned"
+ SAVED = "saved"
+
+
+class WerewolfExtEnv(ExtEnv):
+ roles_state: dict[str, RoleState] = Field(default=dict(), description="the role's current state")
+
+ @mark_as_readable
+ def get_roles_status(self):
+ pass
+
+ @mark_as_writeable
+ def wolf_kill_someone(self, role_name: str):
+ pass
+
+ @mark_as_writeable
+ def witch_poison_someone(self, role_name: str = None):
+ if not role_name:
+ return
+
+ @mark_as_writeable
+ def witch_save_someone(self, role_name: str = None):
+ if not role_name:
+ return