Wayfinding Summary and optimization within the client map

First of all about the client coordinate system:

 

Diamond is a unit grid used by the client, that is, the coordinates of the game Li Leida displayed. Isometric perspective of a client employed, using a diamond grid can simulate the effect of the plane of the 3D scene map. Red rectangle is the client and server utility grid coordinates.

 

寻路方法入口: bool StartFindPath(CPos start, CPos end, vector<Cvector2f>& path, int IgnoreSteps, int nRatio, bool bAnyDir, int nMaxStep)

 

(Related to knowledge and the A * pathfinding algorithm Dijkstra shortest time to achieve the following specific pathfinding explain here is not to do both of these in detail.)

 

start and end wayfinding start and end points, all pixels. path for the reference parameter, for storing the result pathfinding. IgnoreSteps to ignore the number of cells from the finish, that can stop a few cells from the finish. nMaxSteps pathfinding maximum number of steps is, for limiting the use of the A * algorithm timeout time pathfinding.

Pathfinding successful return true, the "line of inflection sets " (i.e., a straight line between adjacent points is no communication barriers, and any non-adjacent two points is not a straight line communication) is stored in the path. Otherwise it returns false.

 

StartFindPath pathfinding process:

  1. First () test whether the start and end points can be linearly communicated with LinePath:

    (1) LinePath need to ensure that the starting point in the scene; endpoint parameters directly returns the starting coordinate in outdoor scenes.

    (2) Then draw a straight line from the starting point and focus direction coordinates to the smallest unit (ie above said diamond lattice) traversal from the start to the end possessive determine whether there are high barriers (high barriers typically terrain, characters and monsters in general low barriers), the successful return true; otherwise it returns the first obstacle encountered grid.

  2. If a straight line find its way without success, then check the start and end points of the input parameters if not exceed scene.

  3. Enter the A * pathfinding algorithm FindPath ():

    (1) check to see if the same start and end points directly -1.

    (2) will be added to the open list in the starting point.

    (3) The operation of the loop is executed until the open list is empty or go to the end node  or exceeds the maximum number of steps pathfinding :

      I. find and remove a minimum total cost value of the node (total cost = G + H In the open list; G is spent from the start point to the node; H is estimated from that point to the end point of use where cost is .H "Manhattan distance" estimated cost from the node to the end point, the formula for the horizontal and vertical coordinates of difference values: H = abs (start.x-end.x) + abs (start.y-end.y)).

      . II in the direction of the adjacent eight processing node traversal, if the new node is not reachable through the open list stored directly added; otherwise, the process checks whether a new node to the node G is smaller, if it is smaller and it takes to update the node front; and re-adjust the order of the open list.

 

Achieve three important data structures of the A * algorithm process:

    (1) XPS_Node: structure of the storage location of the nodes, including node location, content cost G, H, and spent pre-node arriving in the node and the like.

    (2) XPS_Node ** m_pOpenList: array implemented in order to compare the total cost of doing weights with the smallest stack.

    (3) XPS_Node * m_aBacket: through open list for recording the added hash table XPS_Node; hash value is calculated by x, y-coordinate value; resolve hash collisions is the same hash value is stored in a list structure to XPS_Node on the same array subscript.

 

  4. If the A * pathfinding is not successful, then enter the following Dijkstra shortest path algorithm:

    (1) corresponding to the loading and pathlink.csv roadPoint.csv in the map data directory (both files are preset on the map coordinates of the vertices of each well and the distance between adjacent vertices), constructed Dijkstra algorithm requires FIG.

    (2) find the distance from the start and end nearest the apex and is capable of linear communication therewith. The two vertices as Dijkstra start and end in FIG.

    (3) N times the following loop:

      I. arrive to find the minimum cost node node, and mark the point when looking for the next ignore.

      II. For all nodes adjacent vertex node, a node checks takes to reach its neighboring node is less (cost [node] + link [node] [i] <cost [i]), if the update is it reaches its cost and its predecessor node set node.

  5. If Dijkstra pathfinding was unsuccessful (no csv file or can not find the start / end directly connected vertices, etc.), then carry out a wide range of A * pathfinding (This step is often time-consuming very serious players will be significant pathfinding delay).

  (Shown as a broken line can be a straight line as a starting point to find a vertex communication; Dijkstra to obtain a blue solid line path)

 

 

To find its way to optimize the code to do:

 

  1. Dijkstra algorithm to optimize the use of the heap:

The original implementation, each node steps in looking for minimum cost, once all nodes is obtained by traversing. Can take a minimum heap to reach the storage node, then each node to find the minimum cost by the complexity of O (N) down to O (logN). (I am here to achieve the minimum heap is the direct use of std priority queue priority_queue).

  2. When pathfinding using Dijkstra's algorithm, the first step is traversing all the vertices in the drawing, to find the apex can be in communication with the start, end position for the line. Because of the need linearly communicated this condition, so the start / end point of presence around the many obstacles to the grid when it is likely to fail , and into a wide range of A * pathfinding very time-consuming below. Accordingly to these step: when the linear communication fails, find using the start / end point of the nearest apex of a plurality of linear distance A * algorithm (time-out limit the number of steps in a smaller range), determined Dijkstra's start and end in FIG.

 

Here are the giant ** Valley map (or the specific name of the map cover on it: D) ​​on some test results:

starting point

end

The original time-consuming

Optimized

(79,42)

(570,1097)

151ms

26ms

(17,1104)

(538,13)

114ms

32ms

(246,47)

(625,688)

97ms

23ms

(577,808)

(295,584)

31ms

29ms

(36,105)

(419,910)

39ms

39ms

(625,373)

(6,203)

28ms

27ms

 

 

上述优化有些限制,就是需要寻路地图要要有相应的csv地图,并且有数量可观的结点,否则起不到较好的优化效果。而且第一步的Dijkstra算法的堆优化的实际作用并不大,毕竟数据量有限。

 

Guess you like

Origin www.cnblogs.com/geek1116/p/12122074.html