mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-05-12 09:12:38 +02:00
update werewolf to meet WerewolfEnv
This commit is contained in:
parent
d692d9fb7b
commit
b568b8f4a0
18 changed files with 451 additions and 348 deletions
106
metagpt/environment/werewolf/const.py
Normal file
106
metagpt/environment/werewolf/const.py
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Desc :
|
||||
|
||||
from enum import Enum
|
||||
from metagpt.const import MESSAGE_ROUTE_TO_ALL
|
||||
|
||||
|
||||
class RoleType(Enum):
|
||||
VILLAGER = "Villager"
|
||||
WEREWOLF = "Werewolf"
|
||||
GUARD = "Guard"
|
||||
SEER = "Seer"
|
||||
WITCH = "Witch"
|
||||
MODERATOR = "Moderator"
|
||||
|
||||
|
||||
class RoleState(Enum):
|
||||
ALIVE = "alive" # the role is alive
|
||||
DEAD = "dead" # killed or poisoned
|
||||
KILLED = "killed" # killed by werewolf or voting
|
||||
POISONED = "poisoned" # killed by poison
|
||||
SAVED = "saved" # saved by antidote
|
||||
PROTECTED = "projected" # projected by guard
|
||||
|
||||
|
||||
class RoleActionRes(Enum):
|
||||
SAVE = "save"
|
||||
PASS = "pass" # ignore current action output
|
||||
|
||||
|
||||
# the ordered rules by the moderator to announce to everyone each step
|
||||
STEP_INSTRUCTIONS = {
|
||||
0: {
|
||||
"content": "It’s dark, everyone close your eyes. I will talk with you/your team secretly at night.",
|
||||
"send_to": {"Moderator"}, # for moderator to continue speaking
|
||||
"restricted_to": {},
|
||||
},
|
||||
1: {
|
||||
"content": "Guard, please open your eyes!",
|
||||
"send_to": {"Moderator"}, # for moderator to continue speaking
|
||||
"restricted_to": {},
|
||||
},
|
||||
2: {
|
||||
"content": """Guard, now tell me who you protect tonight?
|
||||
You only choose one from the following living options please: {living_players}.
|
||||
Or you can pass. For example: Protect ...""",
|
||||
"send_to": {"Guard"},
|
||||
"restricted_to": {"Moderator", "Guard"},
|
||||
},
|
||||
3: {"content": "Guard, close your eyes", "send_to": {"Moderator"}, "restricted_to": {}},
|
||||
4: {"content": "Werewolves, please open your eyes!", "send_to": {"Moderator"}, "restricted_to": {}},
|
||||
5: {
|
||||
"content": """Werewolves, I secretly tell you that {werewolf_players} are
|
||||
all of the 2 werewolves! Keep in mind you are teammates. The rest players are not werewolves.
|
||||
choose one from the following living options please:
|
||||
{living_players}. For example: Kill ...""",
|
||||
"send_to": {"Werewolf"},
|
||||
"restricted_to": {"Moderator", "Werewolf"},
|
||||
},
|
||||
6: {"content": "Werewolves, close your eyes", "send_to": {"Moderator"}, "restricted_to": {}},
|
||||
7: {"content": "Witch, please open your eyes!", "send_to": {"Moderator"}, "restricted_to": {}},
|
||||
8: {
|
||||
"content": """Witch, tonight {player_hunted} has been killed by the werewolves.
|
||||
You have a bottle of antidote, would you like to save him/her? If so, say "Save", else, say "Pass".""",
|
||||
"send_to": {"Witch"},
|
||||
"restricted_to": {"Moderator", "Witch"},
|
||||
}, # 要先判断女巫是否有解药,再去询问女巫是否使用解药救人
|
||||
9: {
|
||||
"content": """Witch, you also have a bottle of poison, would you like to use it to kill one of the living players?
|
||||
Choose one from the following living options: {living_players}.
|
||||
If so, say ONLY "Poison PlayerX", replace PlayerX with the actual player name, else, say "Pass".""",
|
||||
"send_to": {"Witch"},
|
||||
"restricted_to": {"Moderator", "Witch"},
|
||||
}, #
|
||||
10: {"content": "Witch, close your eyes", "send_to": {"Moderator"}, "restricted_to": {}},
|
||||
11: {"content": "Seer, please open your eyes!", "send_to": {"Moderator"}, "restricted_to": {}},
|
||||
12: {
|
||||
"content": """Seer, you can check one player's identity. Who are you going to verify its identity tonight?
|
||||
Choose only one from the following living options:{living_players}.""",
|
||||
"send_to": {"Seer"},
|
||||
"restricted_to": {"Moderator", "Seer"},
|
||||
},
|
||||
13: {"content": "Seer, close your eyes", "send_to": {"Moderator"}, "restricted_to": {}},
|
||||
# The 1-st daytime
|
||||
14: {
|
||||
"content": """It's daytime. Everyone woke up except those who had been killed.""",
|
||||
"send_to": {"Moderator"},
|
||||
"restricted_to": {},
|
||||
},
|
||||
15: {"content": "{player_current_dead} was killed last night!", "send_to": {"Moderator"}, "restricted_to": {}},
|
||||
16: {
|
||||
"content": """Living players: {living_players}, now freely talk about the current situation based on your observation and
|
||||
reflection with a few sentences. Decide whether to reveal your identity based on your reflection.""",
|
||||
"send_to": {MESSAGE_ROUTE_TO_ALL}, # send to all to speak in daytime
|
||||
"restricted_to": {},
|
||||
},
|
||||
17: {
|
||||
"content": """Now vote and tell me who you think is the werewolf. Don’t mention your role.
|
||||
You only choose one from the following living options please:
|
||||
{living_players}. Say ONLY: I vote to eliminate ...""",
|
||||
"send_to": {MESSAGE_ROUTE_TO_ALL},
|
||||
"restricted_to": {},
|
||||
},
|
||||
18: {"content": """{player_current_dead} was eliminated.""", "send_to": {"Moderator"}, "restricted_to": {}},
|
||||
}
|
||||
49
metagpt/environment/werewolf/env_space.py
Normal file
49
metagpt/environment/werewolf/env_space.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Desc : werewolf observation/action space and its action definition
|
||||
|
||||
from pydantic import ConfigDict, Field
|
||||
from gymnasium import spaces
|
||||
|
||||
from metagpt.environment.base_env_space import BaseEnvAction, BaseEnvActionType
|
||||
from metagpt.environment.werewolf.const import STEP_INSTRUCTIONS, RoleState
|
||||
|
||||
|
||||
class EnvActionType(BaseEnvActionType):
|
||||
NONE = 0 # no action to run, just get observation
|
||||
WOLF_KILL = 1 # wolf kill someone
|
||||
VOTE_KILL = 2 # vote kill someone
|
||||
WITCH_POISON = 3 # witch poison someone
|
||||
WITCH_SAVE = 4 # witch save someone
|
||||
GUARD_PROTECT = 5 # guard protect someone
|
||||
PROGRESS_STEP = 6 # step increment
|
||||
|
||||
|
||||
class EnvAction(BaseEnvAction):
|
||||
model_config = ConfigDict(arbitrary_types_allowed=True)
|
||||
|
||||
action_type: int = Field(default=EnvActionType.NONE, description="action type")
|
||||
player_name: str = Field(default="", description="the name of the player to do the action")
|
||||
target_player_name: str = Field(default="", description="the name of the player who take the action")
|
||||
|
||||
|
||||
def get_observation_space(player_num: int) -> spaces.Dict:
|
||||
space = spaces.Dict({
|
||||
"step_idx": spaces.Discrete(len(STEP_INSTRUCTIONS)),
|
||||
"player_states": spaces.MultiDiscrete([len(RoleState)] * player_num),
|
||||
"vote_counts": spaces.MultiDiscrete([player_num - 1] * player_num),
|
||||
|
||||
"player_current_dead": None, # TODO
|
||||
"winner": spaces.Text(16),
|
||||
"win_reason": spaces.Text(64)
|
||||
})
|
||||
return space
|
||||
|
||||
|
||||
def get_action_space() -> spaces.Dict:
|
||||
space = spaces.Dict({
|
||||
"action_type": spaces.Discrete(len(EnvActionType)),
|
||||
"player_name": spaces.Text(16), # the player to do the action
|
||||
"target_player_name": spaces.Text(16) # the target player who take the action
|
||||
})
|
||||
return space
|
||||
|
|
@ -6,7 +6,6 @@ from pydantic import Field
|
|||
|
||||
from metagpt.environment.base_env import Environment
|
||||
from metagpt.environment.werewolf.werewolf_ext_env import WerewolfExtEnv
|
||||
from metagpt.logs import logger
|
||||
from metagpt.schema import Message
|
||||
|
||||
|
||||
|
|
@ -15,13 +14,11 @@ class WerewolfEnv(Environment, WerewolfExtEnv):
|
|||
|
||||
def publish_message(self, message: Message, add_timestamp: bool = True):
|
||||
"""Post information to the current environment"""
|
||||
logger.debug(f"publish_message: {message.dump()}")
|
||||
if add_timestamp:
|
||||
# Because the content of the message may be repeated, for example, killing the same person in two nights
|
||||
# Therefore, a unique timestamp prefix needs to be added so that the same message will not be automatically deduplicated when added to the memory.
|
||||
message.content = f"{self.timestamp} | " + message.content
|
||||
self.memory.add(message)
|
||||
self.history += f"\n{message}"
|
||||
super().publish_message(message)
|
||||
|
||||
async def run(self, k=1):
|
||||
"""Process all Role runs by order"""
|
||||
|
|
|
|||
|
|
@ -4,98 +4,15 @@
|
|||
|
||||
import random
|
||||
from collections import Counter
|
||||
from enum import Enum
|
||||
from typing import Any, Callable, Optional
|
||||
|
||||
from pydantic import ConfigDict, Field
|
||||
|
||||
from metagpt.environment.base_env import ExtEnv, mark_as_readable, mark_as_writeable
|
||||
from metagpt.environment.base_env_space import BaseEnvAction, BaseEnvObsParams
|
||||
from metagpt.environment.base_env_space import BaseEnvObsParams
|
||||
from metagpt.environment.werewolf.env_space import EnvAction, EnvActionType
|
||||
from metagpt.logs import logger
|
||||
|
||||
|
||||
class RoleState(Enum):
|
||||
ALIVE = "alive" # the role is alive
|
||||
KILLED = "killed" # the role is killed by werewolf or voting
|
||||
POISONED = "poisoned" # the role is killed by posion
|
||||
SAVED = "saved" # the role is saved by antidote
|
||||
|
||||
|
||||
# the ordered rules by the moderator to announce to everyone each step
|
||||
STEP_INSTRUCTIONS = {
|
||||
0: {
|
||||
"content": "It’s dark, everyone close your eyes. I will talk with you/your team secretly at night.",
|
||||
"send_to": "Moderator", # for moderator to continuen speaking
|
||||
"restricted_to": "",
|
||||
},
|
||||
1: {
|
||||
"content": "Guard, please open your eyes!",
|
||||
"send_to": "Moderator", # for moderator to continuen speaking
|
||||
"restricted_to": "",
|
||||
},
|
||||
2: {
|
||||
"content": """Guard, now tell me who you protect tonight?
|
||||
You only choose one from the following living options please: {living_players}.
|
||||
Or you can pass. For example: Protect ...""",
|
||||
"send_to": "Guard",
|
||||
"restricted_to": "Moderator,Guard",
|
||||
},
|
||||
3: {"content": "Guard, close your eyes", "send_to": "Moderator", "restricted_to": ""},
|
||||
4: {"content": "Werewolves, please open your eyes!", "send_to": "Moderator", "restricted_to": ""},
|
||||
5: {
|
||||
"content": """Werewolves, I secretly tell you that {werewolf_players} are
|
||||
all of the 2 werewolves! Keep in mind you are teammates. The rest players are not werewolves.
|
||||
choose one from the following living options please:
|
||||
{living_players}. For example: Kill ...""",
|
||||
"send_to": "Werewolf",
|
||||
"restricted_to": "Moderator,Werewolf",
|
||||
},
|
||||
6: {"content": "Werewolves, close your eyes", "send_to": "Moderator", "restricted_to": ""},
|
||||
7: {"content": "Witch, please open your eyes!", "send_to": "Moderator", "restricted_to": ""},
|
||||
8: {
|
||||
"content": """Witch, tonight {player_hunted} has been killed by the werewolves.
|
||||
You have a bottle of antidote, would you like to save him/her? If so, say "Save", else, say "Pass".""",
|
||||
"send_to": "Witch",
|
||||
"restricted_to": "Moderator,Witch",
|
||||
}, # 要先判断女巫是否有解药,再去询问女巫是否使用解药救人
|
||||
9: {
|
||||
"content": """Witch, you also have a bottle of poison, would you like to use it to kill one of the living players?
|
||||
Choose one from the following living options: {living_players}.
|
||||
If so, say ONLY "Poison PlayerX", replace PlayerX with the actual player name, else, say "Pass".""",
|
||||
"send_to": "Witch",
|
||||
"restricted_to": "Moderator,Witch",
|
||||
}, #
|
||||
10: {"content": "Witch, close your eyes", "send_to": "Moderator", "restricted_to": ""},
|
||||
11: {"content": "Seer, please open your eyes!", "send_to": "Moderator", "restricted_to": ""},
|
||||
12: {
|
||||
"content": """Seer, you can check one player's identity. Who are you going to verify its identity tonight?
|
||||
Choose only one from the following living options:{living_players}.""",
|
||||
"send_to": "Seer",
|
||||
"restricted_to": "Moderator,Seer",
|
||||
},
|
||||
13: {"content": "Seer, close your eyes", "send_to": "Moderator", "restricted_to": ""},
|
||||
# The 1-st daytime
|
||||
14: {
|
||||
"content": """It's daytime. Everyone woke up except those who had been killed.""",
|
||||
"send_to": "Moderator",
|
||||
"restricted_to": "",
|
||||
},
|
||||
15: {"content": "{player_current_dead} was killed last night!", "send_to": "Moderator", "restricted_to": ""},
|
||||
16: {
|
||||
"content": """Living players: {living_players}, now freely talk about the current situation based on your observation and
|
||||
reflection with a few sentences. Decide whether to reveal your identity based on your reflection.""",
|
||||
"send_to": "", # send to all to speak in daytime
|
||||
"restricted_to": "",
|
||||
},
|
||||
17: {
|
||||
"content": """Now vote and tell me who you think is the werewolf. Don’t mention your role.
|
||||
You only choose one from the following living options please:
|
||||
{living_players}. Say ONLY: I vote to eliminate ...""",
|
||||
"send_to": "",
|
||||
"restricted_to": "",
|
||||
},
|
||||
18: {"content": """{player_current_dead} was eliminated.""", "send_to": "Moderator", "restricted_to": ""},
|
||||
}
|
||||
from metagpt.environment.werewolf.const import STEP_INSTRUCTIONS, RoleState, RoleType
|
||||
|
||||
|
||||
class WerewolfExtEnv(ExtEnv):
|
||||
|
|
@ -115,13 +32,13 @@ class WerewolfExtEnv(ExtEnv):
|
|||
special_role_players: list[str] = Field(default=[])
|
||||
winner: Optional[str] = Field(default=None)
|
||||
win_reason: Optional[str] = Field(default=None)
|
||||
witch_poison_left: int = Field(default=1)
|
||||
witch_antidote_left: int = Field(default=1)
|
||||
witch_poison_left: int = Field(default=1, description="should be 1 or 0")
|
||||
witch_antidote_left: int = Field(default=1, description="should be 1 or 0")
|
||||
|
||||
# game current round states, a round is from closing your eyes to the next time you close your eyes
|
||||
round_hunts: dict[str, str] = Field(default=dict(), description="nighttime wolf hunt result")
|
||||
round_votes: dict[str, str] = Field(
|
||||
default=dict(), description="daytime all players vote result, key=voteer, value=voted one"
|
||||
default=dict(), description="daytime all players vote result, key=voter, value=voted one"
|
||||
)
|
||||
player_hunted: Optional[str] = Field(default=None)
|
||||
player_protected: Optional[str] = Field(default=None)
|
||||
|
|
@ -140,8 +57,63 @@ class WerewolfExtEnv(ExtEnv):
|
|||
def observe(self, obs_params: Optional[BaseEnvObsParams] = None) -> Any:
|
||||
pass
|
||||
|
||||
def step(self, action: BaseEnvAction) -> tuple[dict[str, Any], float, bool, bool, dict[str, Any]]:
|
||||
pass
|
||||
def _get_obs(self):
|
||||
return {
|
||||
"game_setup": self.game_setup,
|
||||
"step_idx": self.step_idx,
|
||||
|
||||
"living_players": self.living_players,
|
||||
"werewolf_players": self.werewolf_players,
|
||||
"player_hunted": self.player_hunted,
|
||||
"player_current_dead": self.player_current_dead,
|
||||
"witch_poison_left": self.witch_poison_left,
|
||||
"witch_antidote_left": self.witch_antidote_left,
|
||||
"winner": self.winner,
|
||||
"win_reason": self.win_reason
|
||||
}
|
||||
|
||||
def step(self, action: EnvAction) -> tuple[dict[str, Any], float, bool, bool, dict[str, Any]]:
|
||||
action_type = action.action_type
|
||||
player_name = action.player_name
|
||||
target_player_name = action.target_player_name
|
||||
if action_type == EnvActionType.WOLF_KILL:
|
||||
self.wolf_kill_someone(wolf_name=player_name, player_name=target_player_name)
|
||||
elif action_type == EnvActionType.VOTE_KILL:
|
||||
self.vote_kill_someone(voter_name=player_name, player_name=target_player_name)
|
||||
elif action_type == EnvActionType.WITCH_POISON:
|
||||
self.witch_poison_someone(witch_name=player_name, player_name=target_player_name)
|
||||
elif action_type == EnvActionType.WITCH_SAVE:
|
||||
self.witch_save_someone(witch_name=player_name, player_name=target_player_name)
|
||||
elif action_type == EnvActionType.GUARD_PROTECT:
|
||||
self.guard_protect_someone(guard_name=player_name, player_name=target_player_name)
|
||||
elif action_type == EnvActionType.PROGRESS_STEP:
|
||||
self.progress_step()
|
||||
elif action_type == EnvActionType.NONE:
|
||||
pass
|
||||
else:
|
||||
raise ValueError(f"not supported action_type: {action_type}")
|
||||
|
||||
self.update_game_states()
|
||||
terminated = self._check_game_finish()
|
||||
obs = self._get_obs()
|
||||
return obs, 1.0, terminated, False, {}
|
||||
|
||||
def _check_game_finish(self) -> bool:
|
||||
"""return True if game finished else False"""
|
||||
# game's termination condition
|
||||
terminated = False
|
||||
living_werewolf = [p for p in self.werewolf_players if p in self.living_players]
|
||||
living_villagers = [p for p in self.villager_players if p in self.living_players]
|
||||
living_special_roles = [p for p in self.special_role_players if p in self.living_players]
|
||||
if not living_werewolf:
|
||||
self.winner = "good guys"
|
||||
self.win_reason = "werewolves all dead"
|
||||
terminated = True
|
||||
elif not living_villagers or not living_special_roles:
|
||||
self.winner = "werewolf"
|
||||
self.win_reason = "villagers all dead" if not living_villagers else "special roles all dead"
|
||||
terminated = True
|
||||
return terminated
|
||||
|
||||
@property
|
||||
def living_players(self) -> list[str]:
|
||||
|
|
@ -161,12 +133,12 @@ class WerewolfExtEnv(ExtEnv):
|
|||
|
||||
@property
|
||||
def werewolf_players(self) -> list[str]:
|
||||
player_names = self._role_type_players(role_type="Werewolf")
|
||||
player_names = self._role_type_players(role_type=RoleType.WEREWOLF.value)
|
||||
return player_names
|
||||
|
||||
@property
|
||||
def villager_players(self) -> list[str]:
|
||||
player_names = self._role_type_players(role_type="Villager")
|
||||
player_names = self._role_type_players(role_type=RoleType.VILLAGER.value)
|
||||
return player_names
|
||||
|
||||
def _init_players_state(self, players: list["Role"]):
|
||||
|
|
@ -193,14 +165,14 @@ class WerewolfExtEnv(ExtEnv):
|
|||
"""init players using different roles' num"""
|
||||
role_objs = []
|
||||
for role_obj in role_uniq_objs:
|
||||
if str(role_obj) == "Villager":
|
||||
if "Villager" in str(role_obj):
|
||||
role_objs.extend([role_obj] * num_villager)
|
||||
elif str(role_obj) == "Werewolf":
|
||||
elif "Werewolf" in str(role_obj):
|
||||
role_objs.extend([role_obj] * num_werewolf)
|
||||
else:
|
||||
role_objs.append(role_obj)
|
||||
if shuffle:
|
||||
random.shuffle(len(role_objs))
|
||||
random.shuffle(role_objs)
|
||||
if add_human:
|
||||
assigned_role_idx = random.randint(0, len(role_objs) - 1)
|
||||
assigned_role = role_objs[assigned_role_idx]
|
||||
|
|
@ -233,10 +205,12 @@ class WerewolfExtEnv(ExtEnv):
|
|||
roletype_state = self.players_state[player_name]
|
||||
self.players_state[player_name] = (roletype_state[0], state)
|
||||
|
||||
def _check_valid_role(self, player: "Role", role_type: str) -> bool:
|
||||
return True if role_type in str(player) else False
|
||||
def _check_valid_role(self, player_name: str, role_type: str) -> bool:
|
||||
roletype_state = self.players_state.get(player_name)
|
||||
return True if roletype_state and role_type in roletype_state[0] else False
|
||||
|
||||
def _check_player_continue(self, player_name: str, particular_step: int = -1) -> bool:
|
||||
"""to check if can do the operation to the player"""
|
||||
step_idx = self.step_idx % self.per_round_steps
|
||||
if particular_step > 0 and step_idx != particular_step: # step no
|
||||
# particular_step = 18, not daytime vote time, ignore
|
||||
|
|
@ -253,6 +227,10 @@ class WerewolfExtEnv(ExtEnv):
|
|||
self.step_idx += 1
|
||||
return instruction
|
||||
|
||||
@mark_as_writeable
|
||||
def progress_step(self):
|
||||
self.step_idx += 1
|
||||
|
||||
@mark_as_readable
|
||||
def get_players_state(self, player_names: list[str]) -> dict[str, RoleState]:
|
||||
players_state = {
|
||||
|
|
@ -263,14 +241,14 @@ class WerewolfExtEnv(ExtEnv):
|
|||
return players_state
|
||||
|
||||
@mark_as_writeable
|
||||
def vote_kill_someone(self, voteer: "Role", player_name: str = None):
|
||||
def vote_kill_someone(self, voter_name: str, player_name: str = None):
|
||||
"""player vote result at daytime
|
||||
player_name: if it's None, regard as abstaining from voting
|
||||
"""
|
||||
if not self._check_player_continue(voteer.name, particular_step=18): # 18=step no
|
||||
if not self._check_player_continue(voter_name, particular_step=18): # 18=step no
|
||||
return
|
||||
|
||||
self.round_votes[voteer.name] = player_name
|
||||
self.round_votes[voter_name] = player_name
|
||||
# check if all living players finish voting, then get the dead one
|
||||
if list(self.round_votes.keys()) == self.living_players:
|
||||
voted_all = list(self.round_votes.values()) # TODO in case of tie vote, check who was voted first
|
||||
|
|
@ -279,41 +257,55 @@ class WerewolfExtEnv(ExtEnv):
|
|||
self._update_players_state([self.player_current_dead])
|
||||
|
||||
@mark_as_writeable
|
||||
def wolf_kill_someone(self, wolf: "Role", player_name: str):
|
||||
if not self._check_valid_role(wolf, "Werewolf"):
|
||||
def wolf_kill_someone(self, wolf_name: str, player_name: str):
|
||||
if not self._check_valid_role(wolf_name, RoleType.WEREWOLF.value):
|
||||
return
|
||||
if not self._check_player_continue(wolf.name, particular_step=5): # 5=step no
|
||||
if not self._check_player_continue(wolf_name, particular_step=5): # 5=step no
|
||||
return
|
||||
|
||||
self.round_hunts[wolf.name] = player_name
|
||||
living_werewolf = [p for p in self.werewolf_players if p in self.living_players]
|
||||
self.round_hunts[wolf_name] = player_name
|
||||
# living_werewolf = [p for p in self.werewolf_players if p in self.living_players]
|
||||
# check if all living wolfs finish hunting, then get the hunted one
|
||||
if list(self.round_hunts.keys()) == living_werewolf:
|
||||
hunted_all = list(self.round_hunts.values())
|
||||
self.player_hunted = Counter(hunted_all).most_common()[0][0]
|
||||
# if list(self.round_hunts.keys()) == living_werewolf:
|
||||
# hunted_all = list(self.round_hunts.values())
|
||||
# self.player_hunted = Counter(hunted_all).most_common()[0][0]
|
||||
self.player_hunted = player_name
|
||||
|
||||
@mark_as_writeable
|
||||
def witch_poison_someone(self, witch: "Role", player_name: str = None):
|
||||
if not self._check_valid_role(witch, "Witch"):
|
||||
def _witch_poison_or_save_someone(self, witch_name: str, player_name: str = None,
|
||||
state: RoleState = RoleState.POISONED):
|
||||
if not self._check_valid_role(witch_name, RoleType.WITCH.value):
|
||||
return
|
||||
if not self._check_player_continue(player_name):
|
||||
return
|
||||
|
||||
self._update_players_state([player_name], RoleState.POISONED)
|
||||
self.player_poisoned = player_name
|
||||
assert state in [RoleState.POISONED, RoleState.SAVED]
|
||||
self._update_players_state([player_name], state)
|
||||
if state == RoleState.POISONED:
|
||||
self.player_poisoned = player_name
|
||||
self.witch_poison_left -= 1
|
||||
else:
|
||||
# self.player_protected = player_name
|
||||
self.is_hunted_player_saved = True
|
||||
self.witch_antidote_left -= 1
|
||||
|
||||
@mark_as_writeable
|
||||
def witch_save_someone(self, witch: "Role", player_name: str = None):
|
||||
if not self._check_valid_role(witch, "Witch"):
|
||||
def witch_poison_someone(self, witch_name: str, player_name: str = None):
|
||||
self._witch_poison_or_save_someone(witch_name, player_name, RoleState.POISONED)
|
||||
|
||||
@mark_as_writeable
|
||||
def witch_save_someone(self, witch_name: str, player_name: str = None):
|
||||
self._witch_poison_or_save_someone(witch_name, player_name, RoleState.SAVED)
|
||||
|
||||
@mark_as_writeable
|
||||
def guard_protect_someone(self, guard_name: str, player_name: str = None):
|
||||
if not self._check_valid_role(guard_name, RoleType.GUARD.value):
|
||||
return
|
||||
if not self._check_player_continue(player_name):
|
||||
return
|
||||
|
||||
self._update_players_state([player_name], RoleState.SAVED)
|
||||
self.player_protected = player_name
|
||||
|
||||
@mark_as_writeable
|
||||
def update_game_states(self, memories: list):
|
||||
def update_game_states(self):
|
||||
step_idx = self.step_idx % self.per_round_steps
|
||||
if step_idx not in [15, 18] or self.step_idx in self.eval_step_idx:
|
||||
return
|
||||
|
|
@ -335,16 +327,6 @@ class WerewolfExtEnv(ExtEnv):
|
|||
self.player_protected = None
|
||||
self.is_hunted_player_saved = False
|
||||
self.player_poisoned = None
|
||||
|
||||
# game's termination condition
|
||||
living_werewolf = [p for p in self.werewolf_players if p in self.living_players]
|
||||
living_villagers = [p for p in self.villager_players if p in self.living_players]
|
||||
living_special_roles = [p for p in self.special_role_players if p in self.living_players]
|
||||
if not living_werewolf:
|
||||
self.winner = "good guys"
|
||||
self.win_reason = "werewolves all dead"
|
||||
elif not living_villagers or not living_special_roles:
|
||||
self.winner = "werewolf"
|
||||
self.win_reason = "villagers all dead" if not living_villagers else "special roles all dead"
|
||||
if self.winner is not None:
|
||||
self._record_all_experiences() # TODO
|
||||
elif step_idx == 18:
|
||||
# updated use vote_kill_someone
|
||||
pass
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue