public void CalculatePath(LiMap.Cell firstCellWithTransition, bool hasUnknown) { Logger.instance.Assert(null != pathLastCell, "Don't set last cell. Please call SetupEnvironment"); this.hasUnknown = hasUnknown; if (null != transition && !transition.Cell.Pos.Equals(firstCellWithTransition.Pos)) { pathLastCell = transition.Cell; transition = transition.Next; } if (car.Durability < 1.0e-9 || car.Speed() < 5) { pathLastCell = currentDirLastCell(); transition = null; } beginDir = new TilePos(car.X, car.Y) - pathLastCell.Pos; HashSet <LiMap.Cell> visited = new HashSet <LiMap.Cell>(); int depth = Math.Min(3, (int)(car.Speed() / 10)); transition = mergePath(firstCellWithTransition, pathLastCell, transition, depth); Logger.instance.Assert(null != transition, "Can't find path."); path = createPathFromTransition(transition).ToArray(); Logger.instance.Assert(3 <= path.Length, "Can't find full path."); }
private CellTransition calculatePath(LiMap.Cell cell, Cell lastCell, HashSet <LiMap.Cell> visited, int depth) { if (visited.Contains(cell) || depth > Constant.PathMaxDepth) { return(null); } visited.Add(cell); Cell resultCell = new Cell(); resultCell.Pos = cell.Pos; resultCell.DirIn = cell.Pos - lastCell.Pos; CellTransition max = null; foreach (LiMap.Transition transition in cell.Transitions) { TileDir dir = transition.ToCell.Pos - cell.Pos; resultCell.DirOut = dir; CellTransition newTransition = calculatePath(transition.ToCell, resultCell, visited, depth + 1); if (null != newTransition) { newTransition.TransitionPriority = cellTransitionPriority(lastCell, resultCell, newTransition.Cell, transition.Weight); int checkDepth = transition.isCheckpoint ? 0 : 3; if (null == max || newTransition.Priority(checkDepth) > max.Priority(checkDepth)) { max = newTransition; } } } List <TileDir> dirOuts = new List <TileDir>(); foreach (TileDir dir in cell.Dirs) { if (dir != resultCell.DirIn.Negative()) { dirOuts.Add(dir); } } resultCell.DirOuts = dirOuts.ToArray(); resultCell.DirOut = null; if (0 != dirOuts.Count) { if (null != max) { resultCell.DirOut = max.Cell.Pos - cell.Pos; } } CellTransition result = new CellTransition(resultCell, max, cellPriority(resultCell)); visited.Remove(cell); return(result); }
private CellTransition mergePath(LiMap.Cell mapCell, Cell lastCell, CellTransition iter, int depth) { if (null != iter && depth > 0) { if (iter.Cell.Pos == mapCell.Pos && null != iter.Next) { foreach (LiMap.Transition transition in mapCell.Transitions) { if (iter.Next.Cell.Pos.Equals(transition.ToCell.Pos)) { iter.Next = mergePath(transition.ToCell, iter.Cell, iter.Next, depth - 1); return(iter); } } } } HashSet <LiMap.Cell> visited = new HashSet <LiMap.Cell>(); return(calculatePath(mapCell, lastCell, visited, 0)); }