-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnq_environment_baselines.py
275 lines (243 loc) · 12.3 KB
/
nq_environment_baselines.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import time
import random
import gym
from gym import spaces
from key_pressor.key_pressor_creator import *
from nq_screen_extractor import *
from tensorflow import keras
screenshot_upper_bound = 90000
jewel_reward_point = 0.00000
kill_reward_point = 0.0000
reward_for_being_alive = 0.001
class NQEnv(gym.Env):
metadata = {'render.modes': ['console']}
def __init__(self):
super(NQEnv, self).__init__()
self.action_space = spaces.Discrete(5)
self.observation_space = spaces.Box(low=0, high=255, shape=(130, 130, 1), dtype=np.uint8)
screenshot, stage_enum = self.take_screenshot_save_to_selfstate()
self._episode_ended = False
self._stage = stage_enum
self._raw_screenshot = screenshot
self._infinite_loop_safe_guard = 0
self._first_game_stage_not_finished = True
self._game_over_penalty_is_given = False
self.key_pressor = create_key_pressor()
def reset(self): #reset this python app's state for a new game
print("RESET")
screenshot, stage_enum = self.take_screenshot_save_to_selfstate()
self._stage = stage_enum
self._raw_screenshot = screenshot
self._episode_ended = False
self._infinite_loop_safe_guard = 0
self._first_game_stage_not_finished = True
self._game_over_penalty_is_given = False
return self._state
def step(self, action):
if self._episode_ended:
return self.reset()
actions = ['left_arrow', 'right_arrow', 'up_arrow', 'down_arrow', 'nothing']
try:
str(actions[action])
print("action: " + str(actions[action]) + " which stage: " + str(self._stage))
except:
print("only integer scalar array error: " + str(action))
self.take_screenshot_save_to_selfstate()
return self._state, 0.0, False, {}
if (self._stage == GameStage.game_over or self._stage == GameStage.died) and is_back_button_selected(self._raw_screenshot):
time.sleep(0.5)
self.press_spacebar()
time.sleep(0.5)
screenshot, stage_enum = self.take_screenshot_save_to_selfstate()
self._episode_ended = True
if not self._game_over_penalty_is_given:
tf.keras.preprocessing.image.save_img("current_2" + str(random.randint(0, screenshot_upper_bound)) + ".png", self._raw_screenshot, file_format='png')
tf.keras.preprocessing.image.save_img("next_stage_2" + str(random.randint(0, screenshot_upper_bound)) + ".png", screenshot, file_format='png')
self._stage = stage_enum
self._raw_screenshot = screenshot
print("game_over_penalty_ was not _given, applying penalty") #it's game start page
return self._state, -2.0, True, {}
else:
self._stage = stage_enum
self._raw_screenshot = screenshot
return self._state, 0.0, True, {}
if self._stage == GameStage.game_over or self._stage == GameStage.died:
time.sleep(0.5)
self.press_key(2)
time.sleep(0.5)
screenshot, stage_enum = self.take_screenshot_save_to_selfstate()
if stage_enum == GameStage.in_progress:
tf.keras.preprocessing.image.save_img("current" + str(random.randint(0, screenshot_upper_bound)) + ".png", self._raw_screenshot, file_format='png')
tf.keras.preprocessing.image.save_img("next_stage" + str(random.randint(0, screenshot_upper_bound)) + ".png", screenshot, file_format='png')
print("*** CNN game over recognition error ***, stage: " + str(self._stage) + " next stage: in_progress")
self._stage = stage_enum
self._raw_screenshot = screenshot
return self._state, 0.0, False, {}
if self._stage == GameStage.starting_page:
time.sleep(0.1)
self.press_spacebar()
time.sleep(0.1)
screenshot, stage_enum = self.take_screenshot_save_to_selfstate()
self._stage = stage_enum
self._raw_screenshot = screenshot
return self._state, 0.0, False, {}
if self._stage == GameStage.character_upgrade:
time.sleep(0.1)
self.press_key(0)
time.sleep(0.1)
self.press_spacebar()
screenshot, stage_enum = self.take_screenshot_save_to_selfstate()
self._stage = stage_enum
self._raw_screenshot = screenshot
return self._state, 0.0, False, {}
if self._stage == GameStage.interval:
time.sleep(0.1)
self.press_key(3)
time.sleep(0.1)
self.press_spacebar()
time.sleep(0.5)
screenshot, stage_enum = self.take_screenshot_save_to_selfstate()
# if stage_enum == GameStage.in_progress and not self._first_game_stage_not_finished:
# time.sleep(1.5)
self._stage = stage_enum
self._raw_screenshot = screenshot
return self._state, 0.0, False, {}
if self._stage == GameStage.interval_upgrade:
# self.press_key(action)
time.sleep(0.1)
self.press_key(0)
time.sleep(0.1)
self.press_spacebar()
time.sleep(0.1)
self._infinite_loop_safe_guard = self._infinite_loop_safe_guard + 1
screenshot, stage_enum = self.take_screenshot_save_to_selfstate()
self._stage = stage_enum
self._raw_screenshot = screenshot
if self._infinite_loop_safe_guard > 60:
print("looping in Interval stage for more than 60 times")
self._episode_ended = True
return self._state, -0.01, True, {}
if stage_enum == GameStage.interval_upgrade:
return self._state, -(self._infinite_loop_safe_guard * 0.0008), False, {}
if stage_enum == GameStage.interval:
return self._state, 0.0, False, {}
if stage_enum == GameStage.interval_sorry:
return self._state, -0.001, False, {}
else:
print("mostly likely, game over, a penalty is given, inside "
"interval_upgrade: next stage: " + str(stage_enum))
return self._state, -2.0, False, {}
if self._stage == GameStage.interval_sorry:
self.press_spacebar()
time.sleep(0.1)
screenshot, stage_enum = self.take_screenshot_save_to_selfstate()
self._stage = stage_enum
self._raw_screenshot = screenshot
return self._state, 0.0, False, {}
if self._stage == GameStage.game_over_sorry:
self.press_spacebar()
time.sleep(0.1)
screenshot, stage_enum = self.take_screenshot_save_to_selfstate()
self._stage = stage_enum
self._raw_screenshot = screenshot
return self._state, 0.0, False, {}
if self._stage == GameStage.store_page:
self.press_key(3) # select back button
time.sleep(0.1)
self.press_spacebar()
time.sleep(0.1)
screenshot, stage_enum = self.take_screenshot_save_to_selfstate()
self._stage = stage_enum
self._raw_screenshot = screenshot
self._episode_ended = True
if self._game_over_penalty_is_given:
return self._state, 0.0, True, {}
else:
return self._state, -2.0, True, {}
if self._stage == GameStage.main_page:
self.press_spacebar()
time.sleep(0.1)
screenshot, stage_enum = self.take_screenshot_save_to_selfstate()
self._stage = stage_enum
self._raw_screenshot = screenshot
return self._state, 0.0, False, {}
if self._stage == GameStage.paused_game_while_in_progress:
self.press_spacebar()
time.sleep(0.15)
screenshot, stage_enum = self.take_screenshot_save_to_selfstate()
if stage_enum == GameStage.died or stage_enum == GameStage.game_over:
self._game_over_penalty_is_given = True
# print("game over penalty is given, next stage: " + str(stage_enum))
self._stage = stage_enum
self._raw_screenshot = screenshot
return self._state, -1.0, False, {}
self._stage = stage_enum
self._raw_screenshot = screenshot
return self._state, 0.0, False, {}
#######################################
self.press_key(action)
next_screenshot, next_stage_enum = self.take_screenshot_save_to_selfstate()
# print(" next stage: " + str(next_stage_enum))
if self._stage == GameStage.in_progress and next_stage_enum == GameStage.paused_game_while_in_progress:
self._stage = next_stage_enum
self._raw_screenshot = next_screenshot
return self._state, -5.0, False, {}
if self._stage == GameStage.in_progress and next_stage_enum == GameStage.in_progress:
if self._infinite_loop_safe_guard != 0:
self._infinite_loop_safe_guard = 0
reward = self.calculate_reward_game_in_progress(self._raw_screenshot, self._stage, next_screenshot, next_stage_enum)
self._stage = next_stage_enum
self._raw_screenshot = next_screenshot
return self._state, reward, False, {}
if self._stage == GameStage.in_progress \
and (next_stage_enum == GameStage.game_over
or next_stage_enum == GameStage.interval_upgrade
or next_stage_enum == GameStage.died
or next_stage_enum == GameStage.starting_page):
if next_stage_enum == GameStage.game_over:
print("game over without death scene")
tf.keras.preprocessing.image.save_img("current_" + str(random.randint(0, screenshot_upper_bound)) + ".png", self._raw_screenshot, file_format='png')
tf.keras.preprocessing.image.save_img("next_stage_" + str(random.randint(0, screenshot_upper_bound)) + ".png", next_screenshot, file_format='png')
self._stage = next_stage_enum
self._raw_screenshot = next_screenshot
# print("game over penalty is given, next stage: " + str(next_stage_enum))
self._game_over_penalty_is_given = True
return self._state, -2.0, False, {}
if self._stage == GameStage.in_progress and next_stage_enum == GameStage.interval:
print("**finished stage")
self._stage = next_stage_enum
self._raw_screenshot = next_screenshot
self._first_game_stage_not_finished = False
return self._state, 0.2, False, {}
if self._stage == GameStage.in_progress:
self._stage = next_stage_enum
self._raw_screenshot = next_screenshot
print("unexpected route - in progress => " + next_stage_enum.name)
self._game_over_penalty_is_given = True
return self._state, -2.0, False, {}
self._stage = next_stage_enum
self._raw_screenshot = next_screenshot
print("unexpected route ****** which stage in next turn: " + next_stage_enum.name)
return self._state, 0.0, False, {}
def take_screenshot_save_to_selfstate(self):
i = capture_window()
stage_enum = which_stage(i)
img_resized = tf.image.resize(i, (130, 130))
img_resized = tf.image.rgb_to_grayscale(img_resized)
img_resized = keras.preprocessing.image.img_to_array(img_resized)
img_resized_255 = img_resized * 255
img_resized_255 = img_resized_255.astype(np.uint8)
self._state = img_resized_255
return i, stage_enum
def calculate_reward_game_in_progress(self, screenshot, stage_enum, screenshot_after_action, next_stage_enum):
return reward_for_being_alive
def press_spacebar(self):
self.key_pressor.press_key(self.key_pressor.spacebar)
time.sleep(0.1)
def press_key(self, action):
keys_to_press = [[self.key_pressor.leftarrow], [self.key_pressor.rightarrow], [self.key_pressor.uparrow], [self.key_pressor.downarrow]]
if action != 4:
for key in keys_to_press[action]:
self.key_pressor.press_key(key)
else:
time.sleep(0.01)