-
Notifications
You must be signed in to change notification settings - Fork 0
/
character_creation_handler.py
107 lines (78 loc) · 3.98 KB
/
character_creation_handler.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
import json
import numpy as np
from PIL import Image
from config import CONFIG
from constants import convert_jp_str_to_int
from image_processing import get_count_by_equality, convert_to_black_and_white, extract_characters
from overlay import OverlayWindow
from poorcr import PoorCR
from screenshot import get_window_by_title, get_window_image
def detect_character_creation_player(img_ss_rgb: np.ndarray) -> bool:
return get_count_by_equality(*img_ss_rgb, 33, 56, 60, 123, 255, 197, 247) == 483
def detect_character_creation_shiori(img_ss_rgb: np.ndarray) -> bool:
return get_count_by_equality(*img_ss_rgb, 33, 56, 60, 123, 255, 197, 247) == 371
class CharacterCreationHandler:
def __init__(self, window_id: int):
self.window_id = window_id
self.pcr = PoorCR(padding_x=4)
self.has_saved_once_player = False
self.has_saved_once_shiori = False
def handle(self, img_ss_rgb: np.ndarray, img_ss: Image):
if detect_character_creation_player(img_ss_rgb):
self.handle_character_creation_player(img_ss)
if detect_character_creation_shiori(img_ss_rgb):
self.handle_character_creation_shiori(img_ss)
def handle_character_creation_player(self, img_ss: Image):
configs = {}
for field, (x, y, w, h) in [
("jp_surname", (103, 64, 103 + 45, 64 + 13)),
("jp_name", (159, 64, 159 + 45, 64 + 13)),
("jp_nickname", (103, 96, 103 + 77, 96 + 13)),
("birth_month", (103, 127, 103 + 29, 127 + 13)),
("birth_day", (151, 127, 151 + 29, 127 + 13)),
("blood_type", (103, 159, 103 + 29, 159 + 13))
]:
img_field = img_ss.crop((x, y, w, h))
img_field = convert_to_black_and_white(img_field, (222, 239, 239))
configs[field] = self.pcr.detect(img_field)
configs["birth_month"] = convert_jp_str_to_int(configs["birth_month"])
configs["birth_day"] = convert_jp_str_to_int(configs["birth_day"])
if not self.has_saved_once_player:
for entry in CONFIG["save"]["player"].keys():
if entry in configs.keys():
CONFIG["save"]["player"][entry] = configs[entry]
CONFIG.save()
print(f"Saved player config (Player character info).")
self.has_saved_once_player = True
def handle_character_creation_shiori(self, img_ss: Image):
configs = {}
for field, (x, y, w, h) in [
("birth_month", (103, 95, 103 + 29, 95 + 13)),
("birth_day", (151, 95, 151 + 29, 95 + 13)),
("blood_type", (103, 127, 103 + 29, 127 + 13))
]:
img_field = img_ss.crop((x, y, w, h))
img_field = convert_to_black_and_white(img_field, (222, 239, 239))
configs[field] = self.pcr.detect(img_field)
configs["birth_month"] = convert_jp_str_to_int(configs["birth_month"])
configs["birth_day"] = convert_jp_str_to_int(configs["birth_day"])
if not self.has_saved_once_shiori:
for entry in CONFIG["save"]["shiori"].keys():
if entry in configs.keys():
CONFIG["save"]["shiori"][entry] = configs[entry]
CONFIG.save()
print(f"Saved player config (Shiori info).")
self.has_saved_once_shiori = True
if __name__ == "__main__":
window_id = get_window_by_title("Tokimeki Memorial")
OverlayWindow.create_master()
overlay = OverlayWindow(window_id)
cch = CharacterCreationHandler(window_id)
img_ss = get_window_image(window_id,
offset_x=(overlay.letterbox_offset[0], overlay.letterbox_offset[1]),
offset_y=(overlay.letterbox_offset[2], overlay.letterbox_offset[3]))
img_ss = img_ss.resize((img_ss.size[0] // overlay.game_scaling,
img_ss.size[1] // overlay.game_scaling),
Image.NEAREST)
img_ss_rgb = np.array(img_ss.convert('RGB')).T
cch.handle(img_ss_rgb, img_ss)