I encountered a need for Automatic route searching in the project, so I decided to start learning about astar. I didn't go into details about astar, but I had to say that I had barely met the requirement. Here I will share with you, mutual progress,
Astar has a formula f (x) = g (x) + h (x). It is easy to figure out this formula. f (x) is the total value from the current location to the next location, g (x) indicates the actual price. This indicates that the price is fixed. h (x) indicates the estimated value. That is to say, the price from the next position to the end is unknown, so it is called the valuation value. As shown in, the Black Grid indicates the current position, the green grid indicates the location that the next step may arrive, that is, the top, bottom, left, and right directions, and the red grid indicates the end point, brown indicates an obstacle. Now we need to move from a Black Grid to a red grid, so the next step of the Black Grid must be one of the green grids, and the black grid to the green grid are located in the same way, therefore, we can clearly understand that the actual cost is 1 (the cost of moving one step), that is, g (x). There is a long distance between the green grid and the red grid, and there are obstacles in the middle, therefore, the cost is unknown, that is, h (x). Therefore, the total cost is f (x) = g (x) + h (x ), we can see that there are four green grids around us. It is better to take that step. So we need to obtain the f (x) values of these four grids and then sort them, select the cell with the smallest f (x) value, that is, the cell with the lowest total cost. In this way, continue until the end is reached or there is no green grid on the map.
Next let's take a look at this tool class. g (x) and H (x) are suitable for selection. Generally, the Manhattan algorithm is used, that is, the sum of the distance between two points in the X and Y directions,
-- Filename: pathutil. lua -- Author: BZX -- Date: 2014-07-01 -- Purpose: pathfinding module ("pathutil", package. seeall) Local _ map_data -- map data local _ open_list -- open node local _ open_map -- open node, to improve performance, add local _ close_map -- close the node local _ deleget -- proxy local _ dest_point -- target point local _ start_point -- start point local _ path -- find path -- [[deleget = {G = function (point1, point2) -- add your code -- returns the actual cost of point1 to point2. End H = func Tion (point1, point2) -- add your code -- returns the estimated cost of point1 to point2. End getvalue = function (J, I) -- returns row I of the map, data 1 in column J is an obstacle, and 0 is not an obstacle. End width -- map width height -- map height} --] function findpath (deleget, start_point, dest_point) _ deleget = deleget _ dest_point = dest_point _ start_point = start_point Init () while not table. isempty (_ open_list) Do Local cur_point = _ open_list [1] table. remove (_ open_list, 1) _ open_ma P [cursor] = nil if isequal (cur_point, dest_point) then return makepath (cur_point) else _ close_map [cursor] = cur_point local next_points = getnextpoints (cur_point) for I = 1, # next_points do local next_point = next_points [I] If _ open_map [next_point.key] = nil and _ close_map [next_point.key] = nil and isobstacle (next_point) = false then _ open_map [next_point.key] = next_point table. in SERT (_ open_list, next_point) end table. sort (_ open_list, comparef) end return nilendfunction Init () _ open_list = {} _ open_map = {} _ close_map = {} _ Path = {} _ map_data = {} for I = 1, _ deleget. height do _ map_data [I] ={} for j = 1, _ deleget. width do local value = _ deleget. getvalue (J, I) _ map_data [I] [J] = value end _ open_map [getkey (_ start_point)] = _ start_point table. insert (_ open_list ,_ Start_point) endfunction createpoint (x, y) local point = {["X"] = x, ["Y"] = Y, ["last"] = nil, ["g_value"] = 0, ["h_value"] = 0, ["f_value"] = 0} Point ["key"] = getkey (point) return pointend -- get the next point that can be moved -- @ Param point function getnextpoints (point) Local next_points ={} for I = 1, # _ deleget. directions do local offset = _ deleget. directions [I] local next_point = createpoint (point. X + offset [1], point. Y + offset [2]) next_point ["last"] = point if next_point.x> = 1 and next_point.x <= _ deleget. width and next_point.y> = 1 and next_point.y <= _ deleget. height then next_point ["g_value"] = _ deleget. G (point, next_point) next_point ["h_value"] = _ deleget. H (point, _ dest_point) -- math. ABS (next_points.x-_ dest_point.x) + math. ABS (next_points.y-_ dest_point.y) next_point ["f_value"] = next_point.g _ Value + next_point.h_value table. insert (next_points, next_point) end return next_pointsend -- get path -- @ Param end_point target point function makepath (end_point) _ path ={} local point = end_point while point. last ~ = Nil do table. insert (_ path, createpoint (point. x, point. y) point = point. last end local start_point = point table. insert (_ path, start_point) return _ pathend -- two point price comparator function comparef (point1, point2) return point1.f _ value <point2.f _ valueend -- whether it is an obstacle function isobstacle (point) local value = _ map_data [point. y] [point. x] If value = 1 then return true end return falseend -- whether the two vertices are the same vertex function isequal (point1, point2) return point1.key = point2.keyend -- Obtain the keyfunction getkey (point) local key = string of the vertex Based on the vertex. format ("% d, % d", point. x, point. y) return keyend
The following is the usage of the tool class pathutil
Local deleget = {} deleget. G = function (point1, point2) return math. ABS (point1.x-point2.x) + math. ABS (point1.y-point2.y) end deleget. H = deleget. g deleget. getvalue = function (J, I) Local index = findtreasureutil. getindex (J, I) Local map_info = _ map_info.map [Index] If map_info.display = 0 and map_info.eid ~ = 1 then return 0 end return 1 end deleget. directions = {-1, 0}, {0,-1}, {0, 1}, {1, 0} -- left, top, bottom, right deleget. width = _ Cols deleget. height = _ rows local dest_row, dest_col = findtreasureutil. getmapposition (TAG) Local dest_point = pathutil. createpoint (dest_col, dest_row) Local start_row, start_col = findtreasureutil. getmapposition (_ player_index) Local start_point = pathutil. createpoint (start_col, start_row) _ Path = pathutil. findpath (deleget, start_point, dest_point)
_ Path is the path we have found. The starting point is the last element and the ending point is the first element. Because the map in the project is relatively simple, I did not go into it too deeply, there are still a lot of online information about astar, which is usually too simple for me. I hope you will give me more advice when I first come to contact Alibaba Cloud.
Additional advertisement: Micro-shops selling beauty products if you need to learn more can add my sister QQ: 937893128 various great gods to your wife, girlfriend, small three, small four, small five ...... there are also many of your friends to buy