/
pydqxhelper.py
359 lines (340 loc) · 17.2 KB
/
pydqxhelper.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# -*- coding: UTF-8 -*-
#
import os
import sys
import cv2
import numpy as np
import win32gui
from PIL import ImageGrab
from time import sleep
from pywinauto.SendKeysCtypes import SendKeys # SendInput対応
class DQXHelper():
""" メインクラス """
""" 初期化 """
def __init__(self, debug=False):
# パターンマッチテーブル
# (画像, (元画像検索範囲ymin, ymax, xmin, xmax), (ターゲット座標y, x), 自信度)
self.tbl = {}
self.tbl['Attack1'] = (self.img('attack.png'), (250, 360, 0, 150), (330, 57), 0.5) # こうげき
self.tbl['Attack2'] = (self.img('attack.png'), (250, 360, 0, 150), (319, 50), 0.5) # こうげき
self.tbl['Hissatsu'] = (self.img('hissatsu.png'), (250, 360, 0, 150), (300, 48), 0.4) # ひっさつ
self.tbl['Field'] = (self.img('field.png'), (300, 480, 0, 240), (319, 29), 0.2) # 小マップ
self.tbl['Menu'] = (self.img('menu.png'), (400, 480, 500, 640), (422, 568), 0.3) # HP/MP
self.tbl['Gold'] = (self.img('gold.png'), ( 0, 100, 500, 640), ( 66, 594), 0.5) # G
self.tbl['LongMenu'] = (self.img('longmenu.png'), (400, 480, 0, 640), (468, 42), 0.2) # 画面下部バー
self.tbl['Chest'] = (self.img('chest.png'), ( 0, 100, 0, 150), ( 63, 46), 0.5) # たからばこ
self.tbl['Person'] = (self.img('person.png'), ( 0, 100, 200, 400), ( 63, 243), 0.5) # 対人メニュー
self.tbl['YesNo'] = (self.img('yesno.png'), (300, 400, 400, 640), (325, 499), 0.5) # はい・いいえ
self.tbl['SlotK'] = (self.img('slotk.png'), (400, 500, 0, 150), (418, 55), 0.5) # スロット(かけきん)
self.tbl['SlotM1'] = (self.img('slotm.png'), (350, 450, 0, 640), (395, 50), 0.2) # スロット(メニュー1)
self.tbl['SlotM2'] = (self.img('slotm.png'), (350, 450, 0, 640), (395, 100), 0.2) # スロット(メニュー2)
self.tbl['SlotM3'] = (self.img('slotm.png'), (350, 450, 0, 640), (395, 66), 0.2) # スロット(メニュー3)
self.debug = debug
# GUI指定オプション
self.gui_b_loop = True
self.gui_b_field = False
self.gui_b_battle_ini = False
self.gui_b_battle_end = False
self.gui_field_command = ''
self.gui_battle_command = ''
""" テンプレート読み込み """
def img(self, f):
return cv2.imread(os.path.join('template', f), cv2.CV_LOAD_IMAGE_GRAYSCALE)
""" スクリーンショットのキャプチャ """
def capture(self):
hwnd = win32gui.GetForegroundWindow()
bbox = win32gui.GetWindowRect(hwnd)
img_capt = ImageGrab.grab(bbox)
if img_capt:
return np.array(img_capt)[:, :, ::-1] # RGB->BGR
return None
""" グレースケール化、平滑化、二値化 """
def transform(self, img_orig):
img_gray = cv2.cvtColor(img_orig, cv2.COLOR_BGR2GRAY)
img_blur = cv2.GaussianBlur(img_gray, (1, 1), 0)
img_proc = cv2.adaptiveThreshold(img_blur, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
#img_proc = cv2.threshold(img_blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
if self.debug:
tmpdir = 'c:\\temp\\'
cv2.imwrite(tmpdir + 'orig.png', img_orig)
cv2.imwrite(tmpdir + 'gray.png', img_gray)
cv2.imwrite(tmpdir + 'blur.png', img_blur)
cv2.imwrite(tmpdir + 'proc.png', img_proc)
cv2.imwrite(tmpdir + 'attack1.png', img_proc[329:347, 56:130])
cv2.imwrite(tmpdir + 'attack2.png', img_proc[319:337, 50:121])
cv2.imwrite(tmpdir + 'hissatsu.png', img_proc[300:322, 48:125])
cv2.imwrite(tmpdir + 'field.png', img_proc[319:475, 29:200])
cv2.imwrite(tmpdir + 'menu.png', img_proc[422:460, 568:583])
cv2.imwrite(tmpdir + 'gold.png', img_proc[66:85, 594:608])
cv2.imwrite(tmpdir + 'longmenu.png', img_proc[468:480, 42:600])
cv2.imwrite(tmpdir + 'chest.png', img_proc[63:83, 46:143])
cv2.imwrite(tmpdir + 'person.png', img_proc[63:84, 243:318])
cv2.imwrite(tmpdir + 'yesno.png', img_proc[325:371, 499:560])
cv2.imwrite(tmpdir + 'slot1.png', img_proc[418:435, 55:110])
cv2.imwrite(tmpdir + 'slot2.png', img_proc[395:403, 50:490]) # 上下位置は3パターンある
print 'Saved',
return img_proc
""" パターンマッチング
TM_CCOEFF_NORMEDはmaxLoc、SQDIFFはminLocがベストマッチ """
def find_match(self, img_proc, entry_name):
# マッチテーブル読み込み
entry = self.tbl[entry_name]
img_part = entry[0]
(ymin, ymax, xmin, xmax) = entry[1]
ytgt = entry[2][0] - ymin
xtgt = entry[2][1] - xmin
confid = entry[3]
img_all = img_proc[ymin:ymax, xmin:xmax]
# サイズチェック
if img_all.shape[0] < img_part.shape[0] or img_all.shape[1] < img_part.shape[1]:
# キャプチャ失敗?
print 'ERROR: Bad Capture.'
return 0
# マッチング
result = cv2.matchTemplate(img_part, img_all, cv2.TM_CCOEFF_NORMED)
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(result)
(x, y) = maxLoc
#(y, x) = np.unravel_index(result.argmax(), result.shape)
if self.debug:
print '/ Confid:', round(maxVal, 3), '/ Actual:', (y, x), '/ Target:', (ytgt, xtgt)
# 評価
if maxVal > confid and abs(y - ytgt) < 10 and abs(x - xtgt) < 10:
# 自信度が指定値以上、かつx/yが指定値と10ピクセル以上ずれていない
return round(maxVal, 1)
else:
return 0
""" デバッグモード """
def debug_mode(self, img_proc):
names = self.tbl.keys()
names.sort()
for name in names:
if self.debug:
print '/ Name:', name,
confid = self.find_match(img_proc, name)
if confid > 0:
print '{0}({1})'.format(name, confid),
print ''
""" コマンド実行 """
def exec_command(self, name):
if name == 'mantan':
# 「まんたん」(フィールド)
SendKeys('{F11}')
SendKeys('{LEFT}{UP}', 0.1)
SendKeys('{ENTER}{ENTER}', 0.3)
SendKeys('{ESC}{ESC}{ESC}', 0.1)
print 'Command(mantan)'
elif name == 'hissatsu':
# 「ひっさつ」(戦闘)
SendKeys('{UP}')
SendKeys('{ENTER}{ENTER}{ENTER}', 0.3)
print 'Command(hissatsu)'
elif name == 'attack':
# 「こうげき」 (戦闘)
SendKeys('{ENTER}{ENTER}', 0.3)
print 'Command(attack)'
elif name == 'spell':
# 「じゅもん」(戦闘)
SendKeys('{DOWN}')
SendKeys('{ENTER}{ENTER}{ENTER}', 0.3)
print 'Command(spell)'
elif name == 'tokugi':
# 「とくぎ」(戦闘)
SendKeys('{DOWN}{DOWN}', 0.1)
SendKeys('{ENTER}{ENTER}{ENTER}', 0.3)
print 'Command(tokugi)'
""" フィールドモード """
def field_mode(self, bcommand):
print 'Field Mode', bcommand
b_battle_ini = True
b_battle_end = False
count5 = 5
while self.gui_b_loop:
# ゲーム画面がアクティブ
if win32gui.GetWindowText(win32gui.GetForegroundWindow()).decode('SJIS').startswith(u'ドラゴンクエスト'):
# 5回毎にカメラリセット
count5 = count5 - 1
if count5 == 0:
SendKeys('{F12}')
count5 = 5
# 画像処理
img_orig = self.capture()
if img_orig is None:
print 'ERROR: Capture Failed.'
sleep(0.5)
continue
img_proc = self.transform(img_orig)
# 分岐
if self.find_match(img_proc, 'Person'):
# 対人メニューが表示されている場合、最優先でキャンセル
SendKeys('{ESC}')
print 'Person(ESC)'
else:
# フィールド、戦闘中、その他の判断
b_match_field = self.find_match(img_proc, 'Field')
b_match_menu = self.find_match(img_proc, 'Menu')
if b_match_field:
# フィールド上(左下小マップ表示あり)
b_match_chest = self.find_match(img_proc, 'Chest')
if self.gui_b_battle_end and b_battle_end and not b_match_chest and not b_match_menu:
# 戦闘終了時まんたん(GUIから)
# 宝箱なし、HP/MPメニューなし、行動可能
print 'BattleEnd ->',
self.exec_command('mantan')
b_battle_end = False
elif self.gui_b_field:
# 禁止されてなければ分岐(GUIから)
print 'Field ->',
if b_match_chest:
# 宝箱を開いている場合、ゲット
SendKeys('{ENTER}')
print 'GetItem(ENTER)'
elif b_match_menu:
# HP/MPが表示されている場合、キャンセル
SendKeys('{ESC}')
print 'Menu(ESC)'
elif b_battle_end:
# 戦闘終了直後(かつまんたん未実施)の場合、誤動作を防ぐため待機
print 'BattleEnd(Sleep)'
sleep(5)
b_battle_end = False
elif self.gui_field_command:
# フィールド割り込みコマンド(GUIから)
print'Interrupt',
self.exec_command(self.gui_field_command)
self.gui_field_command = ''
else:
# ENTERで何かActivateしてみる
SendKeys('{ENTER}')
print 'Else(ENTER)'
else:
# 禁止されてるから何もしない
print 'x',
elif not b_match_field and b_match_menu:
# 戦闘中(左下小マップなし、HP/MP表示あり)
print 'Battle ->',
if self.find_match(img_proc, 'Hissatsu'):
# 「ひっさつ」
self.exec_command('hissatsu')
elif self.find_match(img_proc, 'Attack1'):
# 通常戦闘状態(こうげき~どうぐメニュー表示)
if self.gui_battle_command:
# 戦闘割り込みコマンド(GUIから)
print'Interrupt',
self.exec_command(self.gui_battle_command)
self.gui_battle_command = ''
elif self.gui_b_battle_ini and b_battle_ini:
# 初回行動時とくぎ(GUIから)
print 'BattleInit ->',
self.exec_command('tokugi')
b_battle_ini = False
else:
# 「こうげき」「じゅもん」「とくぎ」
self.exec_command(bcommand)
elif self.find_match(img_proc, 'Attack2'):
# 「こうげき」選択済み
# イレギュラー状態になるとここに来る可能性あり
SendKeys('{ENTER}')
print 'Attack2(ENTER)'
else:
# コマンド未表示なので何もしない
print 'x'
# 戦闘中なので、戦闘完了後行動フラグをたてる
b_battle_end = True
elif not b_match_field and not b_match_menu:
# その他(左下小マップ表示なし、HP/MP表示なし)
# 戦闘開始前後や、立て看板表示など
print 'Others ->',
if self.find_match(img_proc, 'LongMenu'):
# 画面下部メッセージがある場合、キャンセル
SendKeys('{ESC}')
print 'LongMenu(ESC)'
else:
# することがないので何もしない
print 'x'
# 戦闘開始前後なので、戦闘初回行動フラグをたてる
b_battle_ini = True
else:
# 未対応なので何もしない
print '?',
else:
# ゲーム画面が非アクティブなので何もしない
print '.',
# アイドル時間
sleep(0.5)
""" スロットモード """
def slot_mode(self):
print 'Slot Mode'
while self.gui_b_loop:
# ゲーム画面がアクティブ
if win32gui.GetWindowText(win32gui.GetForegroundWindow()).decode('SJIS').startswith(u'ドラゴンクエスト'):
# 画像処理
img_orig = self.capture()
if img_orig is None:
print 'ERROR: Capture Failed.'
sleep(0.5)
continue
img_proc = self.transform(img_orig)
# 分岐
if self.find_match(img_proc, 'SlotK'):
SendKeys('{ENTER}')
print 'SlotK(ENTER)'
elif self.find_match(img_proc, 'SlotM1') or self.find_match(img_proc, 'SlotM2') or self.find_match(img_proc, 'SlotM3'):
SendKeys('{ENTER}')
print 'SlotM(ENTER)'
else:
# することがないので何もしない
print 'x',
else:
# ゲーム画面が非アクティブなので何もしない
print '.',
# アイドル時間
sleep(0.5)
""" ループ停止・再開 """
def set_gui_b_loop(self, val):
self.gui_b_loop = val
""" フィールド有効フラグ """
def set_gui_b_field(self, val):
self.gui_b_field = val
""" 戦闘開始時とくぎ有効フラグ """
def set_gui_b_battle_ini(self, val):
self.gui_b_battle_ini = val
""" 戦闘終了時まんたん有効フラグ """
def set_gui_b_battle_end(self, val):
self.gui_b_battle_end = val
""" フィールド割り込みコマンド """
def set_gui_field_command(self, val):
self.gui_field_command = val
""" 戦闘割り込みコマンド """
def set_gui_battle_command(self, val):
self.gui_battle_command = val
""" メイン """
def run(self, args):
if not args:
print 'Invalid option.'
elif args[0] in ['attack', 'spell', 'tokugi']:
# 「こうげき」「じゅもん」「とくぎ」でフィールドモード
self.field_mode(args[0])
elif args[0] == 'slot':
# スロットゲームモード
self.slot_mode()
elif args[0] == 'debug':
# debugサブディレクトリ以下すべて読み込み
print 'DEBUG: Reading all files'
for filename in [f for f in os.listdir('debug') if f.endswith('.png')]:
print 'DEBUG:', filename,
img_orig = cv2.imread(os.path.join('debug', filename))
img_proc = self.transform(img_orig)
self.debug_mode(img_proc)
elif args[0].endswith('.png'):
# 指定ファイル読み込み(+デバッグファイル出力)
self.debug = True
print 'DEBUG:', sys.argv[1]
img_orig = cv2.imread(sys.argv[1])
img_proc = self.transform(img_orig)
print ''
self.debug_mode(img_proc)
# 実行
if __name__ == '__main__':
dqx = DQXHelper()
dqx.run(sys.argv[1:])