按键精灵,魔兽世界自动寻路实现脚本及思路
注:仅供个人学习和娱乐
软件
umi截图插件,excel,按键精灵,神梦dll
神梦dll:https://files.cnblogs.com/files/wxp100/%E7%A5%9E%E6%A2%A6dll.zip?t=1721007482&download=true
umi:https://github.com/hiroi-sora/Umi-OCR
思路
先移动一段距离通过计算斜率,做方向对比,换算成角度,判断最终转向
脚本
Function 对接umiocr(图片路径)
Dim URL,JsonData,json,username,password,base64
URL = "http://192.168.1.40:1224/api/ocr"
Import "SmHTTP.dll"
Set SmHTTP = CreateObject("SMWH.SmHTTP")
base64=Plugin.SMWH.Base64_File(图片路径)
JsonData = SmHTTP.JsonData( _
"base64",base64 , _
"Content-Type", "application/json" _
)
json = SmHTTP.HTTP_POST(URL, JsonData)
//TracePrint json
If SmHTTP.GetStatus() = 200 Then
If SmHTTP.GetJSON(json, "code")=100 Then
Dim 文本左上x, 文本左上y, 文本右上x, 文本右上y, 识别的文本
文本左上x=SmHTTP.GetJSON(json, "data[0]['box'][0][0]")
文本左上y=SmHTTP.GetJSON(json, "data[0]['box'][0][1]")
文本右上x=SmHTTP.GetJSON(json, "data[0]['box'][2][0]")
文本右上y=SmHTTP.GetJSON(json, "data[0]['box'][2][1]")
识别的文本 = SmHTTP.GetJSON(json, "data[0]['text']")
//对接umiocr="识别的文本:" & 识别的文本 & ",文本范围:" & 文本左上x & "," & 文本左上y & "," & 文本右上x & "," & 文本右上y
对接umiocr= 识别的文本
Else
对接umiocr=-1
End If
Else
对接umiocr=-1
End If
End Function
Function 判断方向(x1,y1,x2,y2)
Dim pi
pi = 3.14159265359
If x2 <> x1 Then
斜率 = (y2 - y1) / (x2 - x1)
End If
a = Atn(斜率) / pi * 180
//TracePrint x1&" "&y1&" "&x2&" "&y2
If y2 - y1 < 0 Then
If x2 - x1 > 0 Then
// 此时目的地在角色的东北方
a = a + 270
ElseIf x2 - x1 < 0 Then
// 此时目的地在角色的西北方
a = a + 90
ElseIf x2 - x1 = 0 Then
// 此时目的地在角色的上方
a = 180
End If
ElseIf y2 - y1 > 0 Then
If x2 - x1 > 0 Then
// 此时目的地在角色的东南方
a = a + 270
ElseIf x2 - x1 < 0 Then
// 此时目的地在角色的西南方
a = a + 90
ElseIf x2 - x1 = 0 Then
// 此时目的地在角色的下方
a = 0
End If
ElseIf y2 - y1 = 0 Then
If x2 - x1 > 0 Then
// 此时目的地在角色的右方
a = 270
ElseIf x2 - x1 < 0 Then
// 此时目的地在角色的左方
a= 90
End If
End If
判断方向=a
End Function
//转一圈2秒,转一度需要2000/360
旋转一度用时 =2000/360
Sub 调整人物角度(目标方向, 移动方向)
角度 = abs(目标方向 - 移动方向)
If 角度 > 10 Then
If 目标方向>移动方向 Then
KeyDown "Right", 1
Delay 角度 * 旋转一度用时
KeyUp "Right", 1
Else
KeyDown "left", 1
Delay 角度 * 旋转一度用时
KeyUp "left", 1
End If
End If
End sub
//获取所有目的坐标数据
Call Plugin.Office.OpenXls("C:\Users\wxp\Desktop\test1.xlsx")
Dim line
line=1
Do
data = Plugin.Office.ReadXls(1, line, 1)
If data <>"" Then
line = line + 1
Else
Exit Do
End If
Loop
ReDim array_x(line-2), array_y(line-2)
For i=0 to line-2
array_x(i) = Plugin.Office.ReadXls(1, i+1, 1)
array_y(i) = Plugin.Office.ReadXls(1, i+1, 2)
Next
Call Plugin.Office.CloseXls()
//移动坐标x_x,m_y 初始坐标i_x,i_y 目标坐标t_x,t_y
Dim m_x, m_y, i_x, i_y, t_x, t_y, count
count = 0
t_x=array_x(count)
t_y=array_y(count)
//获取初始坐标
Call Plugin.Pic.PrintScreen(9, 27, 108, 53, "D:\按键精灵\temp\jt.bmp")
坐标 = 对接umiocr("D:\按键精灵\temp\jt.bmp")
i_x=left(坐标,4)
i_y=right(坐标,4)
//初始距离
Dim d
//判断移动后距离
d=Sqr((i_x-t_x)*(i_x-t_x)+(i_y-t_y)*(i_y-t_y))
Do
If (d > 0.25) Then
dim 目标方向
目标方向 = 判断方向(t_x,t_y,i_x,i_y)
KeyDown "up", 1
Delay 1200
KeyUp "up", 1
Call Plugin.Pic.PrintScreen(9, 27, 108, 53, "D:\按键精灵\temp\jt.bmp")
坐标 = 对接umiocr("D:\按键精灵\temp\jt.bmp")
m_x = left(坐标, 4)
m_y = right(坐标, 4)
d=Sqr((m_x-t_x)*(m_x-t_x)+(m_y-t_y)*(m_y-t_y))
dim 移动方向
移动方向 = 判断方向(m_x,m_y,i_x,i_y)
//TracePrint "目标方向:" & 目标方向 & "移动方向:" & 移动方向
call 调整人物角度 (目标方向,移动方向)
i_x = m_x
i_y = m_y
ElseIf (d < 0.25)
TracePrint "距离:"&d&"目标移动到位"
If count < line-2 Then
count=count+1
t_x = array_x(count)
t_y = array_y(count)
i_x = m_x
i_y = m_y
d=Sqr((i_x-t_x)*(i_x-t_x)+(i_y-t_y)*(i_y-t_y))
TracePrint "移动坐标次数:" & count
Else
Exit Do
End If
End If
loop
ps:
坐标截图:“D:\按键精灵\temp\jt.bmp”
坐标移动位置记录:“C:\Users\wxp\Desktop\test1.xlsx“