Skip to content

Commit

Permalink
添加A星寻路算法AStarFindPath接口
Browse files Browse the repository at this point in the history
  • Loading branch information
WallBreaker2 committed Feb 17, 2019
1 parent 0f578c3 commit f93f7f7
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 7 deletions.
Binary file modified doc/op接口说明.chm
Binary file not shown.
151 changes: 151 additions & 0 deletions op/AStar.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#pragma once

#include<list>
#include<Eigen/Dense>
#include <set>
#include <algorithm>
#include <list>
#include <vector>

using std::list;
using namespace Eigen;
using std::set;
using std::list;
using std::vector;
class AStar {
public:
struct Node {
int F, G;
Vector2i pos;
Node* parent;
//Node(int)
Node() :F(0), G(0), parent{ nullptr }{

}

/*Node& operator=(const Node& rhs) {
F = rhs.F;
G = rhs.G;
H = rhs.H;
pos = rhs.pos;
return *this;
}*/

};
struct Nodeless
{
bool operator()(const Node& lhs, const Node& rhs) const {
return lhs.pos[0] < rhs.pos[0] || (lhs.pos[0] == rhs.pos[0] && lhs.pos[1] < rhs.pos[1]);
}
};
struct Vec2less {
bool operator()(const Vector2i& lhs, const Vector2i& rhs) const {
return lhs[0] < rhs[0] || (lhs[0] == rhs[0] && lhs[1] < rhs[1]);
}
};
AStar() {
_start[0] = _start[1] = 0;
_end[0] = _end[1] = 0;
//_start < _end;

}
bool outside(Vector2i pos) {
return pos[0]<0 || pos[0]>_mapSize[0] || pos[1]<0 || pos[1]>_mapSize[1];
}
void set_map(int w,int h, const vector<Vector2i>& wall) {
_mapSize[0] = w; _mapSize[1] = h;
_wallset.clear();
for (auto&it : wall) {
_wallset.insert(it);
}
/* Vector2i tp;
for (int i = 0; i <= mapSize[0];++i) {
_wallset.insert({ i,0 });
_wallset.insert({ 0,i });
_wallset.insert({ mapSize[0],i });
_wallset.insert({ i,mapSize[1] });
}*/
}
void findpath(int beginX, int beginY,int endX,int endY, list<Vector2i>&path) {
_openset.clear();
_closedset.clear();
path.clear();
_start[0] = beginX; _start[1] = beginY;
_end[0] = endX; _end[1] = endY;
if (outside(_start) || outside(_end))
return;

Node curr_node;
curr_node.pos = _start;
_openset.insert(curr_node);
while (!_openset.empty()) {
auto S = std::min_element(_openset.begin(), _openset.end(), [](const Node& lhs, const Node& rhs) {return lhs.F < rhs.F; });
curr_node = *S;
_closedset.insert(curr_node);
_openset.erase(S);
if (curr_node.pos == _end) {//get it!
break;
}
//生成所有相邻节点
Node temp;
for (int i = 0; i < 8; ++i) {
temp.pos = curr_node.pos + _dir4[i];
temp.G = curr_node.G + 1;
int H = std::abs(temp.pos[0] - _end[0]) + std::abs(temp.pos[1] - _end[1]);
temp.F = temp.G + H;
if (_closedset.count(temp))
continue;
if (_wallset.count(temp.pos))
continue;
if (outside(temp.pos))
continue;
auto it = _openset.find(temp);
//如果节点不在开放节点
if (it == _openset.end()) {

temp.parent = (Node*)&(*_closedset.find(curr_node));
_openset.insert(temp);
}
else {
//更新开放节点
if (it->F > temp.F) {
auto ptr = (Node*)&(*it);
ptr->F = temp.F;
}
}
}
}
//获取路径
curr_node.pos = _end;
auto endit = _closedset.find(curr_node);
if (endit != _closedset.end()) {
auto pnode = (Node*)&(*endit);
while (pnode)
{
_pathset.insert(pnode->pos);
path.push_back(pnode->pos);
pnode = pnode->parent;
}
}



}

private:
//Eigen::
Vector2i _start, _end;
//地图大小
Vector2i _mapSize;
//开放节点
set<Node, Nodeless> _openset;
set<Node, Nodeless> _closedset;
//墙节点
set<Vector2i, Vec2less> _wallset;
//路径节点
set<Vector2i, Vec2less> _pathset;
//方向
Vector2i const _dir4[8] = { {0,1},{0,-1},{-1,0},{1,0},{1,1},{-1,1},{-1,-1},{1,-1} };
private:
};

43 changes: 41 additions & 2 deletions op/OpInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
#include "Cmder.h"
#include "Injecter.h"
#include "Tool.h"
#include "AStar.hpp"
// OpInterface

STDMETHODIMP OpInterface::Ver(BSTR* ret) {
#ifndef _WIN64
const char* ver = "0.2.1.3.x86";
const char* ver = "0.2.2.0.x86";
#else
static const wchar_t* ver = L"0.2.1.3.x64";
static const wchar_t* ver = L"0.2.2.0.x64";

#endif;
//Tool::setlog("address=%d,str=%s", ver, ver);
Expand Down Expand Up @@ -90,6 +91,44 @@ STDMETHODIMP OpInterface::InjectDll(BSTR process_name, BSTR dll_name, LONG* ret)
return S_OK;
}

STDMETHODIMP OpInterface::EnablePicCache(LONG enable, LONG* ret) {
_image_proc._enable_cache = enable;
*ret = 1;
return S_OK;
}

STDMETHODIMP OpInterface::AStarFindPath(LONG mapWidth, LONG mapHeight, BSTR disable_points, LONG beginX, LONG beginY, LONG endX, LONG endY, BSTR* path) {
AStar as;
vector<Vector2i>walls;
vector<wstring> vstr;
Vector2i tp;
split(disable_points, vstr, L"|");
for (auto&it : vstr) {
if (swscanf(it.c_str(), L"%d,%d", &tp[0], &tp[1]) != 2)
break;
walls.push_back(tp);
}
list<Vector2i> paths;

as.set_map( mapWidth, mapHeight , walls);
as.findpath(beginX,beginY , endX,endY , paths);
wstring pathstr;
wchar_t buf[20];
for (auto it = paths.rbegin(); it != paths.rend(); ++it) {
auto v = *it;
wsprintf(buf, L"%d,%d", v[0], v[1]);
pathstr += buf;
pathstr.push_back(L'|');
}
if (!pathstr.empty())
pathstr.pop_back();
CComBSTR newstr;
newstr.Append(pathstr.c_str());
newstr.CopyTo(path);
return S_OK;
}


STDMETHODIMP OpInterface::EnumWindow(LONG parent, BSTR title, BSTR class_name, LONG filter, BSTR* retstr)
{
// TODO: 在此添加实现代码
Expand Down
5 changes: 5 additions & 0 deletions op/OpInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ END_COM_MAP()
//Process
//inject dll
STDMETHOD(InjectDll)(BSTR process_name,BSTR dll_name, LONG* ret);
//设置是否开启或者关闭插件内部的图片缓存机制
STDMETHOD(EnablePicCache)(LONG enable, LONG* ret);
//---------------------algorithm-------------------------------
//A星算法
STDMETHOD(AStarFindPath)(LONG mapWidth,LONG mapHeight,BSTR disable_points,LONG beginX,LONG beginY, LONG endX,LONG endY,BSTR* path);
//--------------------windows api------------------------------
//根据指定条件,枚举系统中符合条件的窗口
STDMETHOD(EnumWindow)(LONG parent, BSTR title, BSTR class_name, LONG filter, BSTR* retstr);
Expand Down
10 changes: 6 additions & 4 deletions op/op.idl
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ interface IComponentRegistrar : IDispatch
]
interface IOpInterface : IDispatch
{
//base config 1-119
[id(1)] HRESULT Ver([out, retval] BSTR* ret);
[id(2)] HRESULT SetPath([in]BSTR path, [out, retval] LONG* ret);
[id(3)] HRESULT GetPath([out, retval] BSTR* path);
[id(4)] HRESULT GetBasePath([out, retval] BSTR* path);
[id(5)] HRESULT SetShowErrorMsg([in] LONG show_type, [out, retval] LONG* ret);

[id(10)] HRESULT Sleep([in] LONG millseconds, [out, retval] LONG* ret);

[id(31)] HRESULT InjectDll(BSTR process_name, BSTR dll_name, LONG* ret);
[id(6)] HRESULT Sleep([in] LONG millseconds, [out, retval] LONG* ret);
[id(7)] HRESULT InjectDll(BSTR process_name, BSTR dll_name, LONG* ret);
[id(8)] HRESULT EnablePicCache([in] LONG enable, [out, retval] LONG* ret);
//algorithm 20-49
[id(20)] HRESULT AStarFindPath([in] LONG mapWidth, [in] LONG mapHeight, [in] BSTR disable_points, [in] LONG beginX, [in] LONG beginY, [in] LONG endX, [in] LONG endY, [out, retval] BSTR* path);
//windows api 50-99
[id(58)] HRESULT EnumWindow([in] LONG parent, [in] BSTR title, [in] BSTR class_name, [in] LONG filter, [out, retval] BSTR* retstr);
[id(59)] HRESULT EnumWindowByProcess([in] BSTR process_name, [in] BSTR title, [in] BSTR class_name, [in] LONG filter, [out, retval] BSTR* retstring);
Expand Down
6 changes: 5 additions & 1 deletion op/op.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;_USRDLL;_CRT_NON_CONFORMING_SWPRINTFS;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;_USRDLL;_CRT_NON_CONFORMING_SWPRINTFS;_CRT_SECURE_NO_WARNINGS;_SILENCE_CXX17_ADAPTOR_TYPEDEFS_DEPRECATION_WARNING;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<LanguageStandard>stdcpp17</LanguageStandard>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
Expand Down Expand Up @@ -197,6 +197,9 @@
<RegisterOutput>true</RegisterOutput>
<IgnoreSpecificDefaultLibraries>MSVCRT</IgnoreSpecificDefaultLibraries>
</Link>
<PostBuildEvent>
<Command>copy $(SolutionDir)doc\*.chm $(SolutionDir)bin\x86 /y</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
Expand Down Expand Up @@ -296,6 +299,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="3rd_party\kiero\kiero.h" />
<ClInclude Include="AStar.hpp" />
<ClInclude Include="Bkbase.h" />
<ClInclude Include="bkdisplay.h" />
<ClInclude Include="Bkgdi.h" />
Expand Down
3 changes: 3 additions & 0 deletions op/op.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@
<ClInclude Include="include\Dict.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="AStar.hpp">
<Filter>Algorithm</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="op.rc">
Expand Down

0 comments on commit f93f7f7

Please sign in to comment.