-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_xecutor.py
59 lines (50 loc) · 1.91 KB
/
env_xecutor.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
import numpy as np
import shutil
import os
from emulator import Key
from emulator import Emulator
from env_default import default_action
from env_default import default_next_frame
from env_default import default_reset
########################################################################
class XecutorEnv():
def __init__(self):
self.action_space = 18
self.lives = 3
self.score = 0
self.emu = Emulator("./roms/xecutor.z80")
self.latest_frame = None
self.next_state = None
self.viewport = (70,-74,62,-122)
self.skip_frames = 1
def reset(self, skip=0):
self.lives = 3
self.score = 0
default_reset(self.emu, skip)
self.latest_frame, self.next_state = default_next_frame(self.emu, self.viewport, self.skip_frames, filter_image=False)
return self.next_state
def render(self, renderer):
renderer.render(self.next_state, self.latest_frame)
def step(self, action):
emu = self.emu
default_action(emu, action, (Key.W, Key.S, Key.X, Key.C, Key.Q))
self.latest_frame, self.next_state = default_next_frame(emu, self.viewport, self.skip_frames, filter_image=False)
reward = self.UpdateReward();
terminal = self.UpdateLivesAndRewindIfPlayerDied();
return (self.next_state, reward, terminal)
def UpdateLivesAndRewindIfPlayerDied(self):
emu = self.emu
new_lives = emu.GetByte(0xE470) - 0x1E
old_lives = self.lives;
self.lives = new_lives;
if self.lives == 0: # loss of 1 life - terminal state
return True;
return False;
def UpdateReward(self):
new_score = self.ReadScore()
if new_score == 0: return 0
reward = new_score - self.score
self.score = new_score
return reward
def ReadScore(self):
return 10 * (self.emu.GetByte(0xE013) + (self.emu.GetByte(0xE014) * 256))