| 225 | } |
| 226 | |
| 227 | void FindMeleeTarget() |
| 228 | { |
| 229 | bool visited[MAXDUNX][MAXDUNY] = { { 0 } }; |
| 230 | int maxSteps = 25; // Max steps for FindPath is 25 |
| 231 | int rotations; |
| 232 | bool canTalk; |
| 233 | |
| 234 | struct SearchNode { |
| 235 | int x, y; |
| 236 | int steps; |
| 237 | }; |
| 238 | std::list<SearchNode> queue; |
| 239 | |
| 240 | { |
| 241 | const int start_x = plr[myplr]._px; |
| 242 | const int start_y = plr[myplr]._py; |
| 243 | visited[start_x][start_y] = true; |
| 244 | queue.push_back({ start_x, start_y, 0 }); |
| 245 | } |
| 246 | |
| 247 | while (!queue.empty()) { |
| 248 | SearchNode node = queue.front(); |
| 249 | queue.pop_front(); |
| 250 | |
| 251 | for (int i = 0; i < 8; i++) { |
| 252 | const int dx = node.x + pathxdir[i]; |
| 253 | const int dy = node.y + pathydir[i]; |
| 254 | |
| 255 | if (visited[dx][dy]) |
| 256 | continue; // already visisted |
| 257 | |
| 258 | if (node.steps > maxSteps) { |
| 259 | visited[dx][dy] = true; |
| 260 | continue; |
| 261 | } |
| 262 | |
| 263 | if (!PosOkPlayer(myplr, dx, dy)) { |
| 264 | visited[dx][dy] = true; |
| 265 | |
| 266 | if (dMonster[dx][dy] != 0) { |
| 267 | const int mi = dMonster[dx][dy] > 0 ? dMonster[dx][dy] - 1 : -(dMonster[dx][dy] + 1); |
| 268 | if (CanTargetMonster(mi)) { |
| 269 | const bool newCanTalk = CanTalkToMonst(mi); |
| 270 | if (pcursmonst != -1 && !canTalk && newCanTalk) |
| 271 | continue; |
| 272 | const int newRotations = GetRotaryDistance(dx, dy); |
| 273 | if (pcursmonst != -1 && canTalk == newCanTalk && rotations < newRotations) |
| 274 | continue; |
| 275 | rotations = newRotations; |
| 276 | canTalk = newCanTalk; |
| 277 | pcursmonst = mi; |
| 278 | if (!canTalk) |
| 279 | maxSteps = node.steps; // Monsters found, cap search to current steps |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | continue; |
| 284 | } |
no test coverage detected