Skip to content

Commit

Permalink
Merge branch 'dev' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
pringwong committed Apr 3, 2024
2 parents aba2e95 + c03fc5d commit 68d723f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ CivRealm is an interactive environment for the open-source strategy game [Freeci
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-gap: 20px;
align-items: center;
align-items: stretch;
}
.grid > article {
border: 1px solid #ccc;
Expand Down
6 changes: 5 additions & 1 deletion src/civrealm/configs/default_settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ batch_size_run: 5
epoch_num: 1
# The port used by the first environment; other parallel environments use ports following this port.
port_start: 6300
# The username of minitask
minitask_username: minitask

debug:
# Records the game state and available actions at every step.
Expand Down Expand Up @@ -90,7 +92,9 @@ debug:
# load_game: "testcontroller_T200_2023-07-31-01_51"
# The name of the saved game to be loaded.
load_game: ""

# whether to aitoggle and take the player
take_player:

# whether to use randomly generated seeds for running games. if True, the following random seeds (mapseed, gameseed, agentseed) are ignored
randomly_generate_seeds: True
# The seed for generating a map. The same seed leads to the same map.
Expand Down
15 changes: 11 additions & 4 deletions src/civrealm/envs/freeciv_minitask_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class MinitaskType(ExtendedEnum):
MT_BATTLE_ATTACK_CITY = "battle_attack_city"
MT_BATTLE_DEFEND_CITY = "battle_defend_city"
MT_DIPLOMACY_TRADE_TECH = "diplomacy_trade_tech"

MT_FULL_GAME_50_TURN = "fullgame_50_turn_objectives"

@unique
class MinitaskGameStatus(ExtendedEnum):
Expand All @@ -67,16 +67,17 @@ class MinitaskDifficulty(ExtendedEnum):
MD_HARD = 'hard'


DEFAULT_TASK = "minitask"
MAX_ID = 999
BATTLE_MINITASK_LIST = [
_minitask for _minitask in MinitaskType.list() if _minitask.startswith("battle")]
FULLGAME_MINITASK_LIST = [
_minitask for _minitask in MinitaskType.list() if _minitask.startswith("fullgame")]


class FreecivMinitaskEnv(FreecivBaseEnv):
""" CivRealm environment for mini-game. """

def __init__(self, username: str = DEFAULT_TASK, client_port: int = fc_args['client_port']):
def __init__(self, username: str = fc_args["minitask_username"], client_port: int = fc_args['client_port']):
super().__init__(username=username, client_port=client_port, is_minitask=True)
fc_args['username'] = username
self.filename = None
Expand All @@ -99,6 +100,10 @@ def get_minitask(name, minitask_pattern, max_id):
if isinstance(minitask_type, list):
minitask_type = random.choice(minitask_type)

if minitask_type in FULLGAME_MINITASK_LIST and fc_args['username'] == "minitask":
minitask_level = "normal"
minitask_id = 0

if minitask_type not in MinitaskType.list():
raise ValueError(
f"Not supported type as {minitask_pattern}. The suppported list is {MinitaskType.list()}!")
Expand Down Expand Up @@ -161,7 +166,9 @@ def set_minitask(self, seed, minitask_pattern, max_id):
fc_args['username'], minitask_pattern, max_id)
self.filename = minitask
self.task_type = re.match(
r"{}_T\d+_task_([a-z]+)_.*".format(fc_args['username']), minitask)[1]
r"{}_T\d+_task_([0-9a-z_]+)_level".format(fc_args['username']), minitask)[1]
if self.task_type in FULLGAME_MINITASK_LIST and fc_args['username'] == "minitask":
self.civ_controller.set_parameter('debug.take_player', "AI*1")
self.civ_controller.set_parameter('debug.load_game', minitask)
return

Expand Down
3 changes: 3 additions & 0 deletions src/civrealm/freeciv/civ_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,9 @@ def load_game(self, save_name):
self.ws_client.send_message(f"/set victories {fc_args['victories']}")
self.ws_client.send_message(f"/set endvictory {fc_args['endvictory']}")
self.ws_client.send_message(f"/set advisor {fc_args['advisor']}")
if fc_args['debug.take_player']:
self.clstate.aitoggle_player(fc_args['debug.take_player'])
self.clstate.take_player(fc_args['debug.take_player'])

requests.post(
f"http://{self.host}:{fc_web_args['port']}/gamesetting?openchatbox={fc_args['openchatbox']}")
Expand Down

0 comments on commit 68d723f

Please sign in to comment.