-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmx4_env.py
146 lines (118 loc) · 3.66 KB
/
mmx4_env.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import numpy as np
import gym
from gym import spaces
from stable_baselines3.common.env_checker import check_env
from server import Server
from emulator_grid import start_emulator
class Mmx4Env(gym.Env):
ARROW_TO_BUTTON = {
0: None,
1: "left",
2: "right",
}
ACTION_TO_BUTTON = {
1: "cross",
2: "square",
3: "circle",
}
def __init__(
self,
boss,
bizhawk_path,
ini_path,
port=6969,
time=600,
image_size=(84, 84),
allow_circle=True,
enjoy=False,
):
self.observation_space = spaces.Box(
low=0,
high=255,
shape=(1,) + image_size,
dtype=np.uint8,
)
if allow_circle:
self.action_space = spaces.MultiDiscrete([3, 2, 2, 2])
else:
self.action_space = spaces.MultiDiscrete([3, 2, 2])
self.server = Server(port=port, img_size=image_size)
self._process = start_emulator(
boss=boss,
port=port,
bizhawk_path=bizhawk_path,
ini_path=ini_path,
)
self.server.accept_connection()
self.max_steps = 60 // 6 * time
self.frame = 0
self.first_load = True
self.enjoy = enjoy
self.boss = boss
def _get_obs(self):
return self._screen_matrix
def _get_info(self):
return {
"player_hp": self.player_hp,
"boss_hp": self.boss_hp,
}
def reset(self, seed=None, options=None):
self.frame = 0
self.server.load_state(self.boss, self.first_load)
if self.first_load:
self.first_load = False
(
self._screen_matrix,
self.player_hp,
self.boss_hp,
) = self.server.get_msg()
observation = self._get_obs()
info = self._get_info()
return observation, info
def step(self, action):
past_player_hp = self.player_hp
past_boss_hp = self.boss_hp
self.server.send_msg(self._actions_to_buttons(action))
(
self._screen_matrix,
self.player_hp,
self.boss_hp,
) = self.server.get_msg()
self.frame += 1
self.boss_dmg = past_boss_hp - self.boss_hp
self.player_dmg = past_player_hp - self.player_hp
observation = self._get_obs()
reward = self.boss_dmg - self.player_dmg
if self.enjoy:
if self.player_dmg or self.boss_dmg:
print(f"Player HP: {self.player_hp}; Boss HP: {self.boss_hp}")
# until any of them dies
terminated = not (self.player_hp and self.boss_hp)
if not self.boss_hp:
for _ in range(100):
self.server.send_msg(["nothing"])
self.server.get_msg()
else:
# until player takes damage or kills boss
terminated = bool(self.player_dmg) or not self.boss_hp
truncated = self.frame >= self.max_steps
info = self._get_info()
return observation, reward, terminated, truncated, info
def close(self):
self._process.terminate()
def _actions_to_buttons(self, action):
buttons = [
self.ACTION_TO_BUTTON[i]
for i, act in enumerate(action[1:], start=1)
if act
]
arrow = self.ARROW_TO_BUTTON[action[0]]
if arrow is not None:
buttons.append(arrow)
if not buttons:
buttons = ["nothing"]
return buttons
if __name__ == "__main__":
env = Mmx4Env(boss=0, port=6969)
check_env(env)
print("Environment passed the check")