@@ -78,51 +78,39 @@ class Game:
78
78
a_gps : GPS
79
79
80
80
a_speedometer : Speedometer
81
-
82
81
a_lap_progress : LapProgress
83
-
84
82
a_lap_time : LapTime
85
-
86
83
a_revolutions_per_minute : RevolutionsPerMinute
87
-
88
84
a_wrong_way : WrongWay
89
85
90
86
a_is_recording : bool
91
87
92
88
a_user23 = ctypes .windll .user32
93
-
94
89
a_list_bitmap : []
95
-
96
90
a_controls : Controls
97
-
98
91
a_font_settings : FontSettings
99
-
100
92
a_speed : int
101
-
102
93
a_car_distance_offset : float
103
-
104
94
a_car_direction_offset : int
105
-
106
95
a_race_initialised : bool
107
-
108
96
a_cycles_passed : int
109
-
110
97
a_cuda_device : None
111
98
112
99
a_gps_img_rcg_strategy : AGpsImageRecognitionStrategy
113
-
114
100
a_gps_strategy_enum : GPSStrategyEnum
115
-
116
101
a_cheat_engine : CheatEngine
117
-
118
102
a_image_manipulation : ImageManipulation
119
103
120
104
a_dictionary_menus : dict [str , str ]
121
-
122
105
a_enabled_game_api_values : EnabledGameApiValues
123
-
124
106
a_car_state : CarState
125
107
108
+ a_speed_with_visualiser : int
109
+ a_speed_without_visualiser : int
110
+
111
+ a_hwnd : None
112
+ a_visualise : bool
113
+
126
114
def __init__ (self ) -> None :
127
115
self .a_image_manipulation = ImageManipulation ()
128
116
self .a_image_manipulation .load_comparable_images ()
@@ -132,7 +120,7 @@ def __init__(self) -> None:
132
120
self .a_cycles_passed = 0
133
121
self .a_cheat_engine = CheatEngine ()
134
122
self .a_list_bitmap = []
135
- self .a_speed = 3
123
+ self .a_speed = 6
136
124
self .a_controls = Controls ()
137
125
self .a_font_settings = FontSettings (
138
126
par_font = cv2 .FONT_HERSHEY_SIMPLEX ,
@@ -146,6 +134,7 @@ def __init__(self) -> None:
146
134
'standing_menu' : 'standings_menu' ,
147
135
'attention_restart' : 'attention_restart'
148
136
}
137
+ self .a_visualise = True
149
138
150
139
cuda .printCudaDeviceInfo (0 )
151
140
@@ -177,7 +166,10 @@ def init_game_memory_objects(self) -> None:
177
166
self .a_wrong_way .construct ()
178
167
179
168
def initialize_game (self , par_game_inputs : GameInputs ,
180
- par_enabled_game_api_values : EnabledGameApiValues ) -> None :
169
+ par_enabled_game_api_values : EnabledGameApiValues ,
170
+ par_max_speed_with_visualiser : int ,
171
+ par_max_speed_without_visualiser : int
172
+ ) -> None :
181
173
"""
182
174
Initializes the game by starting the game, waiting for it to start, creating and
183
175
initializing required game objects,and setting the game speed and cheat engine.
@@ -187,12 +179,17 @@ def initialize_game(self, par_game_inputs: GameInputs,
187
179
game inputs.
188
180
par_enabled_game_api_values (EnabledGameApiValues): an instance of EnabledGameApiValues
189
181
class containing the enabled game api values.
182
+ par_max_speed_with_visualiser (int): max speed multiplier with visualiser enabled
183
+ par_max_speed_without_visualiser (int): max speed multiplier without visualiser enabled
184
+
190
185
191
186
Returns:
192
187
None
193
188
"""
194
189
self .a_game_state = GameStateStarting ()
195
190
self .a_enabled_game_api_values = par_enabled_game_api_values
191
+ self .a_speed_with_visualiser = par_max_speed_with_visualiser
192
+ self .a_speed_without_visualiser = par_max_speed_without_visualiser
196
193
self .start_game ()
197
194
# self.start_cheat_engine()
198
195
@@ -237,19 +234,43 @@ class containing the enabled game api values.
237
234
238
235
self .a_cycles_passed = 0
239
236
237
+ par_game_inputs .game_initialization_inputs .put ((
238
+ self .a_race_initialised
239
+ ))
240
+
241
+ agent_settings : tuple [bool , bool ] = par_game_inputs .agent_settings_to_game .get ()
242
+
243
+ self .a_visualise = agent_settings [0 ]
244
+
245
+ # If Not Realtime set speed based on enabled visualiser
246
+ if not agent_settings [1 ]:
247
+ if self .a_visualise :
248
+ self .a_speed = self .a_speed_with_visualiser
249
+ else :
250
+ self .a_speed = self .a_speed_without_visualiser
251
+ # If Realtime set speed to 1
252
+ else :
253
+ self .a_speed = 1
254
+
240
255
par_game_inputs .game_initialization_inputs .put ((
241
256
self .a_race_initialised ,
242
257
self .a_speed
243
258
))
244
259
260
+ self .a_cheat_engine .reconfigure_speed (self .a_speed )
261
+
245
262
self .a_car_state = self .create_empty_car_state ()
246
263
247
264
# pylint: disable=too-many-locals
248
265
# pylint: disable=too-many-branches
249
266
# pylint: disable=too-many-statements
267
+ # pylint: disable=too-many-arguments
250
268
def main_loop (self , par_game_inputs : GameInputs ,
251
269
par_results_path : str ,
252
- par_enabled_game_api_values : EnabledGameApiValues ):
270
+ par_enabled_game_api_values : EnabledGameApiValues ,
271
+ par_max_speed_with_visualiser : int ,
272
+ par_max_speed_without_visualiser : int
273
+ ):
253
274
"""
254
275
Main Loop That Controls All The Game Logic
255
276
@@ -259,12 +280,17 @@ def main_loop(self, par_game_inputs: GameInputs,
259
280
par_results_path (str): Path of the folder containing results including graph images
260
281
par_enabled_game_api_values (EnabledGameApiValues): an instance of EnabledGameApiValues
261
282
class containing the enabled game api values.
283
+ par_max_speed_with_visualiser (int): max speed multiplier with visualiser enabled
284
+ par_max_speed_without_visualiser (int): max speed multiplier without visualiser enabled
262
285
263
286
Returns:
264
287
None: This method doesn't return anything.
265
288
"""
266
289
267
- self .initialize_game (par_game_inputs , par_enabled_game_api_values )
290
+ self .initialize_game (par_game_inputs ,
291
+ par_enabled_game_api_values ,
292
+ par_max_speed_with_visualiser ,
293
+ par_max_speed_without_visualiser )
268
294
269
295
tmp_start_time = time .time ()
270
296
tmp_speed_constant = 1 / self .a_speed
@@ -276,7 +302,7 @@ class containing the enabled game api values.
276
302
try :
277
303
tmp_wrong_way_value = self .a_wrong_way .return_is_wrong_way ()
278
304
# pylint: disable=broad-except
279
- except Exception as exception :
305
+ except pymem . exception . MemoryReadError as exception :
280
306
Printer .print_info (f"Waiting for pointers to initialize { exception } " , "GAME" )
281
307
time .sleep (1 )
282
308
@@ -287,10 +313,12 @@ class containing the enabled game api values.
287
313
288
314
self .a_car_state .reset_car_state ()
289
315
290
- # Check for quit key -> !! WARNING - Without this all the windows will be BLANK GREY !!!
291
- if cv2 .waitKey (1 ) == ord ('q' ):
292
- cv2 .destroyAllWindows ()
293
- break
316
+ if self .a_visualise :
317
+ # Check for quit key -> !! WARNING - Without this all the windows will be
318
+ # BLANK GREY !!!
319
+ if cv2 .waitKey (1 ) == ord ('q' ):
320
+ cv2 .destroyAllWindows ()
321
+ break
294
322
295
323
# Check for record key
296
324
if keyboard .is_pressed ('r' ):
@@ -340,11 +368,12 @@ class containing the enabled game api values.
340
368
self .a_car_distance_offset = tmp_car_offset_distance
341
369
self .a_car_direction_offset = tmp_car_offset_direction
342
370
343
- if tmp_contour is not None :
371
+ if tmp_contour is not None and self . a_visualise :
344
372
cv2 .drawContours (self .a_screenshot , [tmp_contour ], - 1 , (255 , 0 , 255 ), - 1 )
345
373
346
374
backup_screenshot : ndarray = self .a_screenshot
347
- self .show_graph (par_image_path = par_results_path + 'scatter_plot.png' )
375
+ if self .a_visualise :
376
+ self .show_graph (par_image_path = par_results_path + 'scatter_plot.png' )
348
377
349
378
tmp_frame_counter += tmp_speed_constant
350
379
@@ -359,7 +388,8 @@ class containing the enabled game api values.
359
388
tmp_needs_restart : bool = par_game_inputs .game_restart_inputs .get ()
360
389
if tmp_needs_restart :
361
390
self .a_game_state = GameStateRestarting ()
362
- self .update_state_on_screen (self .a_screenshot )
391
+ if self .a_visualise :
392
+ self .update_state_on_screen (self .a_screenshot )
363
393
364
394
par_game_inputs .game_restart_inputs .put (tmp_needs_restart )
365
395
self .reset_game_race (0.7 / float (self .a_speed ), 0.01 / float (self .a_speed ))
@@ -387,16 +417,16 @@ class containing the enabled game api values.
387
417
)
388
418
389
419
par_game_inputs .agent_inputs_state .put (self .a_car_state , )
420
+ if self .a_visualise :
421
+ self .show_texts_on_image (par_image = backup_screenshot ,
422
+ par_font_color = (159 , 43 , 104 ),
423
+ par_car_state = self .a_car_state
424
+ )
390
425
391
- self .show_texts_on_image (par_image = backup_screenshot ,
392
- par_font_color = (159 , 43 , 104 ),
393
- par_car_state = self .a_car_state
394
- )
395
-
396
- self .show_state_on_image (par_image = backup_screenshot ,
397
- par_game_state = self .a_game_state )
426
+ self .show_state_on_image (par_image = backup_screenshot ,
427
+ par_game_state = self .a_game_state )
398
428
399
- cv2 .imshow ('Main Vision' , backup_screenshot )
429
+ cv2 .imshow ('Main Vision' , backup_screenshot )
400
430
401
431
def is_race_initialised (self ) -> bool :
402
432
"""
@@ -633,9 +663,11 @@ def window_capture(self) -> Tuple[np.ndarray, int, int]:
633
663
- int: Width of the captured image
634
664
- int: Height of the captured image
635
665
"""
636
- # Find the game window
666
+ # Find the game window4
637
667
hwnd = win32gui .FindWindow (None , self .api_settings ['game_title_name' ])
638
668
669
+ # win32gui.SendMessage(hwnd, win32con.WM_ACTIVATE, win32con.WA_CLICKACTIVE, hwnd)
670
+
639
671
# Get the window device context
640
672
w_dc = win32gui .GetWindowDC (hwnd )
641
673
@@ -831,7 +863,8 @@ def is_in_correct_restart_state(self, par_screen_image: ndarray) -> RestartState
831
863
832
864
return RestartStateEnum .UNKNOWN_STATE
833
865
834
- def reset_game_race (self , par_sleep_time_delay : float , par_sleep_time_key_press : float ) -> None :
866
+ def reset_game_race (self , par_sleep_time_delay : float ,
867
+ par_sleep_time_key_press : float ) -> None :
835
868
"""
836
869
Reset the game race to the initial state.
837
870
@@ -872,7 +905,8 @@ def reset_game_race(self, par_sleep_time_delay: float, par_sleep_time_key_press:
872
905
# Press Enter - Restarts The Race
873
906
# Then Prompt Will Appear - We Move To The OK Button
874
907
for key_to_press in keys_to_press :
875
- self .a_controls .press_and_release_key (key_to_press , par_sleep_time_key_press , True )
908
+ self .a_controls .press_and_release_key (key_to_press ,
909
+ par_sleep_time_key_press , True )
876
910
time .sleep (par_sleep_time_delay )
877
911
878
912
tmp_restart_state = self .is_in_correct_restart_state (self .window_capture ()[0 ])
@@ -887,12 +921,14 @@ def reset_game_race(self, par_sleep_time_delay: float, par_sleep_time_key_press:
887
921
# If the prompt with Attention (Do you Really Want to Restart) appears press Enter
888
922
time .sleep (par_sleep_time_delay )
889
923
890
- self .a_controls .press_and_release_key (self .a_controls .ENTER , par_sleep_time_key_press , True )
924
+ self .a_controls .press_and_release_key (self .a_controls .ENTER ,
925
+ par_sleep_time_key_press , True )
891
926
time .sleep (par_sleep_time_delay )
892
927
893
928
time .sleep (1 * self .a_speed )
894
929
895
- def init_game_race (self , par_sleep_time_delay : float , par_sleep_time_key_press : float ):
930
+ def init_game_race (self ,
931
+ par_sleep_time_delay : float , par_sleep_time_key_press : float ):
896
932
"""
897
933
Initializes a race in the game by navigating through the game's menu system.
898
934
0 commit comments