private int _OpenGrids_compfunc(GridST g1, GridST g2) { bool flag = g1.F < g2.F; int result; if (flag) { result = -1; } else { bool flag2 = g1.F == g2.F; if (flag2) { bool flag3 = g1.H < g2.H; if (flag3) { result = -1; return(result); } } result = 1; } return(result); }
private int _OpenGrids_compfunc(GridST g1, GridST g2) { if (g1.F < g2.F) { return(-1); } else if (g1.F == g2.F) { if (g1.H < g2.H) { return(-1); } } return(1); }
public List <GridST> findPath(Vec2 start, Vec2 end) { bool flag = !this.IsWalkAble((float)((int)end.x), (float)((int)end.y)); List <GridST> result; if (flag) { end = this._getWalkabelEnd(start, end); bool flag2 = end == null; if (flag2) { GameTools.PrintNotice("end point not walkable!"); result = null; return(result); } } bool flag3 = start.x == end.x && start.y == end.y; if (flag3) { result = new List <GridST>(); } else { int num = (int)((long)this._curMapConf["param"]["width"]._int / 32L); int num2 = (int)((long)this._curMapConf["param"]["height"]._int / 32L); List <GridST> list = new List <GridST>(); Dictionary <uint, GridST> dictionary = new Dictionary <uint, GridST>(); Dictionary <int, int> dictionary2 = new Dictionary <int, int>(); list.Add(new GridST { x = (float)((int)start.x), y = (float)((int)start.y), parent = null, G = 0, H = 0, F = 0, isCorner = false, tile_idx = 0u }); GridST gridST = null; while (list.Count > 0) { GridST gridST2 = ArrayUtil.pop_priority_queue <GridST>(list, new ArrayUtil.CompareFunction <GridST>(this._OpenGrids_compfunc)); uint key = (uint)((int)gridST2.y << 9 | ((int)gridST2.x & 511)); dictionary2[(int)key] = 1; for (int i = 0; i < 3; i++) { int num3 = (int)(gridST2.y + (float)i - 1f); bool flag4 = num3 < 0 || num3 >= num2; if (!flag4) { uint num4 = (uint)((uint)num3 << 9); for (int j = 0; j < 3; j++) { int num5 = (int)(gridST2.x + (float)j - 1f); bool flag5 = num5 < 0 || num5 >= num; if (!flag5) { uint num6 = num4 | (uint)(num5 & 511); bool flag6 = dictionary2.ContainsKey((int)num6); if (!flag6) { int key2 = (int)num6; bool flag7 = this._CollideInfoEx[key2] == 1u || (this._BlockZoneInfo.ContainsKey((int)num6) && this._BlockZoneInfo[(int)num6] > 0u); if (flag7) { dictionary2[(int)num6] = 1; } else { int num7 = gridST2.G + 1; bool isCorner = false; bool flag8 = gridST2.x != (float)num5 && gridST2.y != (float)num3; if (flag8) { num7 = gridST2.G + 2; isCorner = true; } int num8 = (int)(Math.Abs((float)num3 - end.y) + Math.Abs((float)num5 - end.x)); int num9 = num7 + num8; bool flag9 = dictionary.ContainsKey(num6); if (flag9) { GridST gridST3 = dictionary[num6]; bool flag10 = num9 < gridST3.F || (num9 == gridST3.F && num8 < gridST3.H); if (flag10) { gridST3.F = num9; gridST3.H = num8; gridST3.G = num7; gridST3.parent = gridST2; gridST3.isCorner = isCorner; gridST3.tile_idx = num6; } } else { dictionary[num6] = new GridST { x = (float)num5, y = (float)num3, parent = gridST2, G = num7, H = num8, F = num9, isCorner = isCorner, tile_idx = num6 }; ArrayUtil.push_priority_queue <GridST>(list, dictionary[num6], new ArrayUtil.CompareFunction <GridST>(this._OpenGrids_compfunc)); bool flag11 = (float)num5 == end.x && (float)num3 == end.y; if (flag11) { gridST = dictionary[num6]; break; } } } } } } bool flag12 = gridST != null; if (flag12) { break; } } } bool flag13 = gridST != null; if (flag13) { break; } } bool flag14 = gridST == null; if (flag14) { result = null; } else { List <GridST> list2 = new List <GridST>(); while (gridST != null) { list2.Add(new GridST { x = gridST.x, y = gridST.y, parent = gridST.parent, G = gridST.G, H = gridST.H, F = gridST.F, isCorner = gridST.isCorner, tile_idx = gridST.tile_idx }); gridST = gridST.parent; } result = list2; } } return(result); }
public List <GridST> findPath(Vec2 start, Vec2 end) { if (!this.IsWalkAble((int)end.x, (int)end.y)) { //如果目标点不能行走,以此取目标点和出发点连线上的点,改目标点为最接近目标点的可行走点,开始寻路 end = this._getWalkabelEnd(start, end); if (end == null) { GameTools.PrintNotice("end point not walkable!"); return(null); } } if (start.x == end.x && start.y == end.y) { return(new List <GridST>()); } int map_width = (int)(_curMapConf["param"]["width"]._int / GameConstant.GEZI); int map_height = (int)(_curMapConf["param"]["height"]._int / GameConstant.GEZI); List <GridST> Open_Grids = new List <GridST>(); Dictionary <uint, GridST> Open_Grids_map = new Dictionary <uint, GridST>(); Dictionary <int, int> Grids_hex_mark = new Dictionary <int, int>(); GridST st = new GridST(); st.x = (int)start.x; st.y = (int)start.y; st.parent = null; st.G = 0; st.H = 0; st.F = 0; st.isCorner = false; st.tile_idx = 0; Open_Grids.Add(st); GridST end_tile = null; int i = 0; int j = 0; while (Open_Grids.Count > 0) { GridST cur_tile = ArrayUtil.pop_priority_queue(Open_Grids, _OpenGrids_compfunc); uint tile_idx = (uint)(((int)(cur_tile.y)) << 9 | ((int)(cur_tile.x)) & 0x000001ff); Grids_hex_mark[(int)tile_idx] = 1; // 标记为已走 //debug.dbginfo("cur node x["+cur_tile.x+"] y["+cur_tile.y+"]"); for (i = 0; i < 3; ++i) { int edge_tile_y = (int)(cur_tile.y + i - 1); if (edge_tile_y < 0 || edge_tile_y >= map_height) // 坐标不合法 { continue; } uint edge_tile_idx_y = (uint)(edge_tile_y << 9); for (j = 0; j < 3; ++j) { int edge_tile_x = (int)(cur_tile.x + j - 1); if (edge_tile_x < 0 || edge_tile_x >= map_width) // 坐标不合法 { continue; } uint edge_tile_idx = (uint)(edge_tile_idx_y | (uint)(edge_tile_x & 0x000001ff)); if (Grids_hex_mark.ContainsKey((int)edge_tile_idx)) // 不能走,或已判断过 { continue; } int index = (int)edge_tile_idx; //if( index > _CollideInfoEx.Count -1 ) //{ // //GameTools.PrintError( "CollideInfoEx info err!" ); // continue; //} if ((_CollideInfoEx[index] == 1) || (this._BlockZoneInfo.ContainsKey((int)edge_tile_idx) && this._BlockZoneInfo[(int)edge_tile_idx] > 0)) // 格子不可行走 { Grids_hex_mark[(int)edge_tile_idx] = 1; // 标记为 已判断过,不能走 continue; } // 计算 F, G, H int G = cur_tile.G + 1; // 上下左右 +1 bool isCorner = false; if (cur_tile.x != edge_tile_x && cur_tile.y != edge_tile_y) { G = cur_tile.G + 2; // 边角 +2 isCorner = true; } int H = (int)(Math.Abs(edge_tile_y - end.y) + Math.Abs(edge_tile_x - end.x)); int F = G + H; if (Open_Grids_map.ContainsKey(edge_tile_idx)) { // 已在开放格子列表中 //dbg_str += "founded!"; GridST edge_tile = Open_Grids_map[edge_tile_idx]; if (F < edge_tile.F || (F == edge_tile.F && H < edge_tile.H)) { // 新路径更佳 edge_tile.F = F; edge_tile.H = H; edge_tile.G = G; edge_tile.parent = cur_tile; edge_tile.isCorner = isCorner; edge_tile.tile_idx = edge_tile_idx; } } else { // 新格子 GridST stt = new GridST(); stt.x = edge_tile_x; stt.y = edge_tile_y; stt.parent = cur_tile; stt.G = G; stt.H = H; stt.F = F; stt.isCorner = isCorner; stt.tile_idx = edge_tile_idx; Open_Grids_map[edge_tile_idx] = stt; ArrayUtil.push_priority_queue(Open_Grids, Open_Grids_map[edge_tile_idx], _OpenGrids_compfunc); if (edge_tile_x == end.x && edge_tile_y == end.y) { end_tile = Open_Grids_map[edge_tile_idx]; break; } } } if (end_tile != null) { // 找到了 break; } } if (end_tile != null) { // 找到了 break; } } if (end_tile == null) { return(null); } List <GridST> path_ary = new List <GridST>(); for (; end_tile != null;) { GridST ast = new GridST(); ast.x = end_tile.x; ast.y = end_tile.y; ast.parent = end_tile.parent; ast.G = end_tile.G; ast.H = end_tile.H; ast.F = end_tile.F; ast.isCorner = end_tile.isCorner; ast.tile_idx = end_tile.tile_idx; path_ary.Add(ast); end_tile = end_tile.parent; } return(path_ary); }