Skip to content

Commit

Permalink
add scene identification
Browse files Browse the repository at this point in the history
  • Loading branch information
AcademicDog committed Oct 8, 2019
1 parent 19f9188 commit d19829a
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## v1.0.0.1001
#### New features:
* 新增了场景识别,目前涉及的场景仅有4个,即0-其他; 1-庭院; 2-探索界面; 3-章节界面; 4-探索内。因此场景识别仅针对探索有效。进一步测试后将会逐渐推广。

## v1.0.0.0930
#### New features:
* 新增了御魂本地双开的初始代码,目前还在测试中
Expand Down
24 changes: 8 additions & 16 deletions explore/explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import tools.utilities as ut

import configparser
import logging
import random
import time

Expand Down Expand Up @@ -152,6 +153,10 @@ def fight_moster(self, mood1, mood2):
self.log.writeinfo('已进入战斗')
time.sleep(1)

# 等待式神准备
self.yys.wait_game_color(((1024,524),(1044, 544)), (138,198,233), 30)
logging.info('式神准备完成')

# 检查狗粮经验
self.check_exp_full()

Expand Down Expand Up @@ -179,16 +184,8 @@ def start(self):
mood1 = ut.Mood(2)
mood2 = ut.Mood(3)
while self.run:
# 点击挑战按钮
if self.yys.wait_game_img('img\\TAN-SUO.png', 3, False):
self.click_until('探索按钮', 'img\\YING-BING.png',
*TansuoPos.tansuo_btn, mood1.get1mood()/1000)
else:
self.click_until('最后章节', 'img\\TAN-SUO.png',
*TansuoPos.last_chapter, mood1.get1mood()/1000)
time.sleep(0.5)
self.click_until('探索按钮', 'img\\YING-BING.png',
*TansuoPos.tansuo_btn, mood1.get1mood()/1000)
# 进入探索内
self.switch_to_scene(4)

# 开始打怪
i = 0
Expand All @@ -204,11 +201,6 @@ def start(self):
i += 1

# 退出探索
if self.yys.find_game_img('img\\YING-BING.png'):
while self.run:
self.yys.mouse_click_bg(*TansuoPos.quit_btn)
if self.yys.wait_game_img('img\\QUE-REN.png', 3, False):
break
self.yys.mouse_click_bg(*TansuoPos.confirm_btn)
self.switch_to_scene(3)
self.log.writeinfo('结束本轮探索')
time.sleep(0.5)
100 changes: 98 additions & 2 deletions gameLib/fighter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from gameLib.game_ctl import GameControl
from tools.logsystem import WriteLog
from tools.game_pos import TansuoPos

import configparser
import logging
import random
import time
import win32gui

Expand Down Expand Up @@ -58,20 +61,23 @@ def click_monster(self):
# 点击怪物
pass

def click_until(self, tag, img_path, pos, pos_end=None, step_time=0.5):
def click_until(self, tag, img_path, pos, pos_end=None, step_time=0.5, appear=True):
'''
在某一时间段内,后台点击鼠标,直到出现某一图片出现
在某一时间段内,后台点击鼠标,直到出现某一图片出现或消失
:param tag: 按键名
:param img_path: 图片路径
:param pos: (x,y) 鼠标单击的坐标
:param pos_end=None: (x,y) 若pos_end不为空,则鼠标单击以pos为左上角坐标pos_end为右下角坐标的区域内的随机位置
:step_time=0.5: 查询间隔
:appear: 图片出现或消失:Ture-出现;False-消失
:return: 成功返回True, 失败退出游戏
'''
# 在指定时间内反复监测画面并点击
start_time = time.time()
while time.time()-start_time <= self.max_op_time and self.run:
result = self.yys.find_game_img(img_path)
if not appear:
result = not result
if result:
self.log.writeinfo(self.name + '点击 ' + tag + ' 成功')
return True
Expand All @@ -96,3 +102,93 @@ def deactivate(self):
self.log.writewarning(self.name + '手动停止脚本')
self.run = False
self.yys.run = False

def slide_x_scene(self, distance):
'''
水平滑动场景
:return: 成功返回True; 失败返回False
'''
x0 = random.randint(distance + 10, 1126)
x1 = x0 - distance
y0 = random.randint(436, 486)
y1 = random.randint(436, 486)
self.yys.mouse_drag_bg((x0, y0), (x1, y1))
logging.info(self.name + '水平滑动界面')

def get_scene(self):
'''
识别当前场景
:return: 返回场景名称:1-庭院; 2-探索界面; 3-章节界面; 4-探索内
'''
# 拒绝悬赏
self.yys.rejectbounty()

# 分别识别庭院、探索、章节页、探索内
maxVal, maxLoc = self.yys.find_multi_img(
'img/JIA-CHENG.png', 'img/JUE-XING.png', 'img/TAN-SUO.png', 'img/YING-BING.png')

scene_cof = max(maxVal)
if scene_cof > 0.97:
scene = maxVal.index(scene_cof)
return scene + 1
else:
return 0

def switch_to_scene(self, scene):
'''
切换场景
:param scene: 需要切换到的场景:1-庭院; 2-探索界面; 3-章节界面; 4-探索内
:return: 切换成功返回True;切换失败直接退出
'''
scene_now = self.get_scene()
logging.info(self.name + '目前场景:' + str(scene_now))
if scene_now == scene:
return True
if scene_now == 1:
# 庭院中
if scene == 2 or scene == 3 or scene == 4:
# 先将界面划到最右边
self.slide_x_scene(800)
time.sleep(2)
self.slide_x_scene(800)

# 点击探索灯笼进入探索界面
self.click_until('探索灯笼', 'img/JUE-XING.png', *
TansuoPos.tansuo_denglong, 2)

# 递归
self.switch_to_scene(scene)

if scene_now == 2:
# 探索界面
if scene == 3 or scene == 4:
# 点击最后章节
self.click_until('最后章节', 'img/TAN-SUO.png',
*TansuoPos.last_chapter, 2)

# 递归
self.switch_to_scene(scene)

if scene_now == 3:
# 章节界面
if scene == 4:
# 点击探索按钮
self.click_until('探索按钮', 'img/YING-BING.png',
*TansuoPos.tansuo_btn, 2)

# 递归
self.switch_to_scene(scene)

if scene_now == 4:
# 探索内
if scene == 3:
# 点击退出探索
self.click_until('退出按钮', 'img\\QUE-REN.png',
*TansuoPos.quit_btn, 2)

# 点击确认
self.click_until('确认按钮', 'img\\QUE-REN.png',
*TansuoPos.confirm_btn, 2, False)

# 递归
self.switch_to_scene(scene)
39 changes: 39 additions & 0 deletions gameLib/game_ctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,45 @@ def find_img(self, img_template_path, part=0, pos1=None, pos2=None, gray=0):
except:
return 0, 0

def find_multi_img(self, *img_template_path, part=0, pos1=None, pos2=None, gray=0):
"""
查找多张图片
:param img_template_path: 欲查找的图片路径列表
:param part=0: 是否全屏查找,1为否,其他为是
:param pos1=None: 欲查找范围的左上角坐标
:param pos2=None: 欲查找范围的右下角坐标
:param gray=0: 是否彩色查找,0:查找彩色图片,1:查找黑白图片
:return: (maxVal,maxLoc) maxVal为相关性列表,越接近1越好,maxLoc为得到的坐标列表
"""
# 窗口截图
if part == 1:
img_src = self.window_part_shot(pos1, pos2, None, gray)
else:
img_src = self.window_full_shot(None, gray)

# 返回值列表
maxVal_list = []
maxLoc_list = []
for item in img_template_path:
# 读入文件
if gray == 0:
img_template = cv2.imread(item, cv2.IMREAD_COLOR)
else:
img_template = cv2.imread(item, cv2.IMREAD_GRAYSCALE)

# 开始识别
try:
res = cv2.matchTemplate(
img_src, img_template, cv2.TM_CCOEFF_NORMED)
minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(res)
maxVal_list.append(maxVal)
maxLoc_list.append(maxLoc)
except:
maxVal_list.append(0)
maxLoc_list.append(0)
# 返回列表
return maxVal_list, maxLoc_list

def activate_window(self):
user32 = ctypes.WinDLL('user32.dll')
user32.SwitchToThisWindow(self.hwnd, True)
Expand Down
Binary file added img/JUE-XING.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tools/game_pos.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class CommonPos():
class TansuoPos():
last_chapter = (934, 493), (1108, 572) # 列表最后一章
tansuo_btn=(787,458),(890,500) #探索按钮
tansuo_denglong = (424, 118), (462, 158) # 探索灯笼
ready_btn = (1000, 460), (1069, 513) # 准备按钮
fight_quit=GamePos((1055,462),(1121,518)) #退出战斗
quit_btn = (32, 45), (58, 64) # 退出副本
Expand Down

0 comments on commit d19829a

Please sign in to comment.