Skip to content

Commit 90ef5ac

Browse files
committed
Add android support
And refactory
1 parent 11555c4 commit 90ef5ac

28 files changed

+1089
-967
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ dmypy.json
131131
# Visual Studio
132132
.vs
133133

134+
# Visual Studio Code
135+
.vscode
136+
134137
# swy-bot
135138
saved
136139
constant.py

README.md

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
# 食物语自动挂机脚本
33

44
## 简介
5-
食物语自动挂机脚本, 主要用于客潮~~和活动小游戏~~(太难做砍掉了绝对不是因为我懒啊啊啊啊啊啊)
5+
食物语自动挂机脚本, 主要用于客潮和活动小游戏(目前仅包括千人千面自动消除)
66

7-
现已加入千人千面自动挂机, 但由于某些原因仅提供算法, 不提供配置, 请在task.py中自行修改配置
7+
由于某些原因小游戏自动挂机仅提供算法, 不提供配置, 请在tasks/minigame.py中自行修改配置
8+
9+
目前已支持Android系统原生运行脚本, 但有一些性能问题, 详情请见android分支, 若想体验可到releases中下载
810

911
现已加入食物语线性规划做菜计算器, 但处于早期测试阶段, 如遇bug请提交issue
1012

@@ -17,24 +19,31 @@
1719
## 食用方式
1820
详见程序内指引
1921

20-
**注意:** 若使用ADB(Scrcpy模式), 请确保系统环境变量中有ADB, 并且ADB已连接至手机
22+
**注意:** 若使用ADB和Scrcpy模式, 请确保系统环境变量中有ADB, 并且ADB已连接至手机
2123

2224
---
2325
## 食材
24-
- Python3
26+
必需依赖:
27+
- Python 3.8+
28+
- numpy
2529
- opencv-python
26-
- PyAV
27-
- pywin32(仅Windows上需要)
28-
- PyAutoGUI(仅Windows以外的系统需要)
29-
- pure-python-adb
30-
- PuLP
31-
- pyinstaller(如需要打包)
3230

33-
详见requirements.txt
31+
平台特定(现已引入动态加载模式, 未安装库仅会禁用对应模式, 脚本仍可正常运行):
32+
- Windows原生模式:
33+
- pywin32
34+
- linux和mac原生模式:
35+
- PyAutoGUI
36+
- 任意系统ADB模式:
37+
- pure-python-adb
38+
- 任意系统Scrcpy模式:
39+
- av
40+
41+
线性规划做菜计算器:
42+
- PuLP
3443

3544
---
3645
## 菜谱&改良
37-
本项目可在Windows, Linux和MacOS上运行, 但目前仅在Windows上测试过
46+
本项目可在Windows, Linux,MacOS和Android系统上运行, Android系统需要专用的用于加载脚本的APP才能运行, 详见android分支
3847

3948
另外本挂机脚本实际上提供了一个框架, 经过简单修改应该也能用于其他游戏, 甚至是用于训练人工智能玩游戏(逃
4049

@@ -44,19 +53,32 @@
4453
- libs/ 脚本所需的库, 目前只有Scrcpy的服务端
4554
- data/ 存储游戏数据和挂机所需资源的目录
4655
- saved/ 截图会保存在这个目录
47-
- main.py 主文件
48-
- player.py 负责与游戏交互(通过原生窗口/ADB/Scrcpy)
49-
- task.py 执行挂机任务
56+
- algorithms/ 算法
57+
- detect.py 图像识别相关算法
58+
- matching.py 自动连连看算法(原作者TheThreeDog)
59+
- players/ 模式
60+
- player.py 模式基类
61+
- native.py 通过原生窗口与游戏交互
62+
- adb.py 通过ADB与游戏交互
63+
- scrcpy.py 通过Scrcpy与游戏交互
64+
- debug.py 调试用
65+
- tasks/ 挂机任务
66+
- task.py 挂机任务基类
67+
- kechao.py 客潮挂机任务
68+
- minigame.py 小游戏挂机任务
5069
- platforms/ 工具模块的平台相关实现
70+
- console.py 具有命令行的系统上的实现
5171
- windows.py Windows系统相关实现
5272
- linux.py Linux系统相关实现
73+
- android.py Android系统相关实现
74+
- main.py 主模块
75+
- profit.py 线性规划做菜计算器(可单独运行)
76+
- swy.py 食物语游戏数据(单独运行为输出游戏数据)
77+
- scrcpy.py Python实现的Scrcpy客户端
5378
- utils.py 工具模块
54-
- swy.py 负责加载游戏数据
55-
- profit.py 线性规划做菜计算器
56-
- matching.py 自动连连看算法(原作者TheThreeDog)
5779

5880
### 自动挂机
59-
详见player.py和task.py
81+
详见players和tasks目录
6082

6183
### 线性规划做菜计算器
6284
线性规划算法详见profit.py中的注释
@@ -68,15 +90,15 @@
6890

6991
另外烹饪时间出现小数的话只有入没有舍, 是ceil
7092

71-
### 自动连连看
93+
### 自动小游戏
7294
由于某些原因, 本项目仅提供用于学习的算法, 不包含配置, 如需使用请自行配置
7395

7496
---
7597
## 如何烹饪
7698
1. 拿走菜谱(克隆仓库到本地)
7799
1. 准备好食材(安装第三方库)
78100
1. 开始做菜(运行main.py)
79-
1. 装盘(如需构建, 请使用pyinstaller)
101+
1. 装盘(目前仍无法打包运行, 请考虑使用Android原生版本)
80102
1. 随心所欲地修改菜谱(请随意修改代码)
81103

82104
---
@@ -96,6 +118,7 @@
96118
2021/9/13 加入自动连连看算法, 初步支持Linux
97119
2021/9/14 已加入对Linux和MacOS的支持, 但尚未测试, 更新版本号至V1.6
98120
2021/9/17 已加入千人千面自动挂机功能, 更新版本号至V1.7
121+
2023/2/2 代码重写, 用协程替代状态机, 实现动态加载模式, 移植到Android系统, 由于更新内容较多, 更新版本号为V2.0
99122
</pre>
100123
</details>
101124

algorithms/detect.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import numpy
2+
import cv2
3+
4+
sift = cv2.SIFT_create()
5+
6+
def siftcompute(image, draw=False):
7+
# 转灰度图
8+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
9+
# 计算关键点和描述符
10+
# key_points: 关键点信息, 包括位置, 尺度, 方向信息
11+
# descriptors: 关键点描述符, 每个关键点对应128个梯度信息的特征向量
12+
kp, des = sift.detectAndCompute(gray, None)
13+
if draw:
14+
cv2.drawKeypoints(image, kp, image, (0, 255, 0),
15+
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
16+
return des
17+
18+
def siftsimilarity(img1, img2, threshold=0.7):
19+
# 计算两张图片的特征点
20+
des1 = siftcompute(img1)
21+
des2 = siftcompute(img2)
22+
# 构建 FLANN
23+
FLANN_INDEX_KDTREE = 1
24+
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
25+
search_params = dict(checks=50)
26+
flann = cv2.FlannBasedMatcher(index_params, search_params)
27+
# 比较两张图片的特征点
28+
matches = flann.knnMatch(des1, des2, k=2)
29+
if len(matches) == 0:
30+
return 0.0
31+
# 计算匹配点数并计算相似度
32+
good = [m for (m, n) in matches if m.distance < n.distance * threshold]
33+
return len(good) / len(matches)
34+
35+
def isimagesame(img1, img2):
36+
# return siftsimilarity(img1, img2, 0.7) > 0.8
37+
result = cv2.matchTemplate(img1, img2, cv2.TM_CCOEFF_NORMED)
38+
location = numpy.where(result >= 0.5)
39+
for pt in zip(*location[::-1]):
40+
return True
41+
return False
42+
43+
def findtemplate(image, template, threshold=0.75, outline=False):
44+
theight, twidth = template.shape[:2]
45+
result = cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED)
46+
# result = cv2.normalize(result, None, 0, 1, cv2.NORM_MINMAX)
47+
location = numpy.where(result >= threshold)
48+
lx, ly = 0, 0
49+
points = []
50+
for pt in zip(*location[::-1]):
51+
x, y = pt[0] + int(twidth / 2), pt[1] + int(theight / 2)
52+
# 去掉重复点
53+
if x - lx < twidth or y - ly < theight:
54+
continue
55+
points.append((x, y))
56+
lx, ly = x, y
57+
if outline:
58+
cv2.rectangle(image, pt, (pt[0] + twidth, pt[1] + theight), (0, 255, 0), 1)
59+
return points
60+
61+
def findcircle(image, r, outline=False):
62+
gimg = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
63+
gimg = cv2.medianBlur(gimg, 5)
64+
result = cv2.HoughCircles(gimg, cv2.HOUGH_GRADIENT, 1, int(r / 2), None, 100, 40, r - 10, r + 10)
65+
points = []
66+
if result is not None:
67+
result = numpy.uint16(numpy.around(result))
68+
for p in result[0, :]:
69+
points.append((p[0], p[1]))
70+
if outline:
71+
cv2.circle(image, (p[0], p[1]), p[2], (0, 255, 0), 1)
72+
return points

matching.py renamed to algorithms/matching.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# coding=utf-8
2-
31
'''
42
Copyright 2018 TheThreeDog
53

0 commit comments

Comments
 (0)