Files
mir_server/sdk/srvlib/include/AdvanceAstar.h

128 lines
3.9 KiB
C
Raw Normal View History

2025-01-09 17:45:40 +08:00
/*
* B*
*
*/
#pragma once
#include "../include/VirturalMap.h"
#include <_memchk.h>
//#include "CommonDef.h"
//#include <Windows.h>
//#include <tchar.h>
#include "_ast.h"
#include "List.h"
//点的坐标
struct SMALLCPOINT
{
unsigned short x; //
unsigned short y;
unsigned short dir;
//设置是否合法
inline bool isInvalid() { return x== 0xFF && y== 0xFF;}
//设置点非法
inline void SetInvalid() {x= 0xFF;y=0xFF;}
inline void MakePoint(int _x,int _y, int _dir = NULL) {x=(unsigned short)_x; y=(unsigned short)_y; dir=(unsigned short)_dir;}
inline SMALLCPOINT &operator =(const SMALLCPOINT &cval) {x=cval.x; y=cval.y; dir=cval.dir; return *this;}
inline bool operator ==(const SMALLCPOINT &cval) {return (x==cval.x && y==cval.y && dir==cval.dir);}
inline bool operator !=(const SMALLCPOINT &cval) {return !(this->operator==(cval));}
};
enum tagAstarResultCode
{
enSearchTooManyPoint =-1, //太多的寻路点
enSearchUnReach =-2, //目标掉不可达到
enSearchParamError=-3, //参数错误
enSearchCalError=-4, //内部计算错误
enSearchTooLong =-5, //距离过远
enSearchError =-6, //计算错误,进入了很大的循环
};
//优化过的A*寻路算法
class CAdvanceAstar
{
public:
CAdvanceAstar()
{
m_bHasInit =false;
};
const static int SIZE =64; //矩阵的一半大小
const static int MATRIX_ARRAY_LENGTH = 2*SIZE +1;
/*
* Comments:,64*64
* Param VirturalMap * pMap:
* Param int nMapWidth:
* Param int nMapHeight:
* Param int nStartPosX:x
* Param int nStartPosY:y
* Param int nEndPosX:x
* Param int nEndPosY:y
* Param int nLoopTime:
* Param int nParam :
* Param int nParam2 :
* @Return int:0<0 tagAstarResultCode中定义
*/
int Search(VirturalMap * pMap, int nMapWidth, int nMapHeight,int nStartPosX, int nStartPosY, int nEndPosX,int nEndPosY,int nLoopTime=1000,int nParam=0,int nParam2=0,int nParam3=0,int nParam4=0);
/*
* Comments:
* Param int x:x
* Param int y:y
* Param int& nParentX:x
* Param int& nParentY:y
* @Return bool:truefalse
*/
bool GetParent(int x, int y, int &nParentX,int & nParentY);
/*
* Comments:
* Param INT_PTR x:x
* Param INT_PTR y:y
* Param INT_PTR & nSonX:
* Param INT_PTR & nSonY:
* @Return bool:truefalse
*/
bool GetSon(int x, int y, int &nSonX,int & nSonY);
private:
//初始化寻路参数
void InitAstar();
private:
wylib::container::CBaseList<SMALLCPOINT> m_openList; //open列表
char m_visitedTable[MATRIX_ARRAY_LENGTH][MATRIX_ARRAY_LENGTH]; //状态的表
SMALLCPOINT m_parentTable[MATRIX_ARRAY_LENGTH][MATRIX_ARRAY_LENGTH]; //父亲节点的坐标
int m_nOffsetX; //坐标变化的误差
int m_nOffsetY; //坐标误差
char m_dirDistance_x[8]; //8个方向x
char m_dirDistance_y[8]; //8个方向y
char m_dir[24]; //8个方向为了加快方向的速度保存起来查找
char m_matrix_x[8]; //2方向查找矩阵
char m_matrix_y[8];
bool m_bHasInit; //是否初始化完毕了
int m_nDesPosX; //目标点的位置x
int m_nDesPosY; //目标点的位置y
};