-
Notifications
You must be signed in to change notification settings - Fork 1
/
PathFinder.cpp
238 lines (191 loc) · 4.71 KB
/
PathFinder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "DevilPK.h"
POINT ptPath[200];
BOOL PathToVector(INT nVectorType)
{
POINT ptStart, ptEnd = {0};
//ptPath = {0};
LPVECTOR lpTarget = NULL;
INT nCount = 0;
DWORD dwLevelX, dwLevelY;
CMatrix<WORD, WORD> GMap;
CPathFinder PathFinder;
CMap g_collisionMap;
if(v_TeleportQueue.IsLocked || !v_TeleportQueue.IsEmpty())
return FALSE;
for(INT i = 0; i < v_CurrentVectors.GetSize(); i++)
{
if(v_CurrentVectors[i]->nType == nVectorType)
{
lpTarget = v_CurrentVectors[i];
break;
}
}
if(!lpTarget)
return FALSE;
v_GlobalMap->CreateCollisionMap();
v_GlobalMap->ExportCollisionMap(GMap);
dwLevelX = GetUnitLevel(Myself)->dwPosX * 5;
dwLevelY = GetUnitLevel(Myself)->dwPosY * 5;
ptStart.x = Myself->pPath->xPos - dwLevelX;
ptStart.y = Myself->pPath->yPos - dwLevelY;
if(!GMap.IsValidIndex(ptStart.x, ptStart.y))
{
return FALSE;
}
ptEnd.x = (lpTarget->ptPos.x / 32) - dwLevelX;
ptEnd.y = (lpTarget->ptPos.y / 32) - dwLevelY;
if(!GMap.IsValidIndex(ptEnd.x, ptEnd.y))
{
return FALSE;
}
PathFinder.SetPathingMap(GMap.GetData(), GMap.GetCX(), GMap.GetCY());
nCount = PathFinder.CreateBestPath(ptStart, ptEnd, ptPath, 200);
if(nCount == 0)
{
return FALSE;
}
for(INT i = 1; i < nCount; i++)
{
ptPath[i].x += dwLevelX;
ptPath[i].y += dwLevelY;
v_TeleportQueue.Add(ptPath[i]);
}
v_CurrentVector = lpTarget;
CreateThread(0,0,TeleportThread,0,0,0);
return TRUE;
}
CPathFinder::CPathFinder()
{
m_pMap = NULL;
m_nSizeX = 0;
m_nSizeY = 0;
memset(&m_ptStart, 0, sizeof(POINT));
memset(&m_ptEnd, 0, sizeof(POINT));
}
CPathFinder::~CPathFinder()
{
}
BOOL CPathFinder::CreateBestPath(POINT ptStart, POINT ptEnd, LPPOINT lpBuffer, DWORD dwMaxCount)
{
POINT ptPos;
INT nFlag, nIterations = 0;
DWORD dwFound;
if(lpBuffer == NULL || dwMaxCount == 0 || m_nSizeX <= 0 || m_nSizeY <= 0 || m_pMap == NULL)
return 0;
if(ptStart.x > m_nSizeX || ptStart.y > m_nSizeY || ptEnd.x > m_nSizeX || ptEnd.y > m_nSizeY)
return 0;
memset(lpBuffer, 0, sizeof(POINT) * dwMaxCount);
m_ptStart = ptStart;
m_ptEnd = ptEnd;
CreateDistanceTable();
lpBuffer[0] = ptStart;
dwFound = 1;
ptPos = ptStart;
nFlag = GetNextMove(ptPos);
while(nFlag != PATH_FAILED && dwFound < dwMaxCount)
{
if(nIterations > 5000)
return 0;
if(nFlag == PATH_REACHED)
{
lpBuffer[dwFound] = ptEnd;
dwFound++;
return dwFound;
}
int nRedundancy = CheckRedundancy(lpBuffer, dwFound, ptPos);
if(nRedundancy == -1)
{
lpBuffer[dwFound] = ptPos;
dwFound++;
}
else
{
dwFound = nRedundancy + 1;
lpBuffer[dwFound] = ptPos;
}
nFlag = GetNextMove(ptPos);
nIterations++;
}
return FALSE;
}
VOID CPathFinder::SetPathingMap(LPWORD *pMap, INT nSizeX, INT nSizeY)
{
m_pMap = pMap;
m_nSizeX = nSizeX;
m_nSizeY = nSizeY;
}
PATHRETURN CPathFinder::GetNextMove(POINT& pos, int nAdjust)
{
if(CalculateDistance(m_ptEnd, pos) <= TELEPORT_RANGE)
{
pos = m_ptEnd;
return PATH_REACHED;
}
if (!IsValidIndex(pos.x, pos.y))
return PATH_FAILED;
BlockLocation(pos, nAdjust);
POINT p, best;
int value = RANGE_INVALID;
for (p.x = pos.x - TELEPORT_RANGE; p.x <= pos.x + TELEPORT_RANGE; p.x++)
{
for (p.y = pos.y - TELEPORT_RANGE; p.y <= pos.y + TELEPORT_RANGE; p.y++)
{
if (!IsValidIndex(p.x, p.y))
continue;
if (m_pMap[p.x][p.y] < value && CalculateDistance(p, pos) <= TELEPORT_RANGE)
{
value = m_pMap[p.x][p.y];
best = p;
}
}
}
if (value >= RANGE_INVALID)
return PATH_FAILED;
pos = best;
BlockLocation(pos, nAdjust);
return PATH_CONTINUE;
}
BOOL CPathFinder::CreateDistanceTable()
{
if(m_pMap == NULL)
return FALSE;
for (int x = 0; x < m_nSizeX; x++)
{
for (int y = 0; y < m_nSizeY; y++)
{
if ((m_pMap[x][y] % 2) == 0)
m_pMap[x][y] = (SHORT)CalculateDistance(x, y, m_ptEnd.x, m_ptEnd.y);
else
m_pMap[x][y] = RANGE_INVALID;
}
}
m_pMap[m_ptEnd.x][m_ptEnd.y] = 1;
return TRUE;
}
BOOL CPathFinder::CheckRedundancy(LPPOINT lpPath, INT nCount, POINT ptPos)
{
if(lpPath == NULL || nCount == 0)
return -1;
for(INT i = 0; i < nCount; i++)
{
if(CalculateDistance(ptPos, lpPath[i]) <= (TELEPORT_RANGE / 3))
return i;
}
return -1;
}
BOOL CPathFinder::IsValidIndex(INT nX, INT nY)
{
return nX >= 0 && nX < m_nSizeX && nY >= 0 && nY < m_nSizeY;
}
VOID CPathFinder::BlockLocation(POINT pos, INT nRange)
{
nRange = max(nRange, 1);
for(INT i = pos.x - nRange; i < pos.x + nRange; i++)
{
for(INT j = pos.y - nRange; j < pos.y + nRange; j++)
{
if (IsValidIndex(i, j))
m_pMap[i][j] = RANGE_INVALID;
}
}
}