private void GeneratePaths() { CellPath path = new CellPath(startCell, start_v3); if (startCell == endCell) { potentialPaths.Add(path); return; } #if UNITY_EDITOR float totalDist = Debuger_K.doDebug ? Vector3.Distance(start_v3, end_v3) : 0f; #endif path.h = EuclideanDistance(start_v3); excluded.Clear(); excluded.Add(startCell); foreach (var connection in startCell.connections) { CellPath newPath = new CellPath(path, connection); newPath.g = connection.Cost(properties, ignoreCrouchCost); if (connection is CellContentPointedConnection) { newPath.h = EuclideanDistance((connection as CellContentPointedConnection).exitPoint); } else { newPath.h = EuclideanDistance(connection.connection); } AddCellNode(newPath); } int limit = 0; while (true) { limit++; if (limit > 1500) { Debug.Log("limit > 1500"); break; } CellPath current = TakeCellNode(); if (current == null) { break; } Cell currentCell = current.last; if (currentCell == endCell) { potentialPaths.Add(current); #if UNITY_EDITOR if (Debuger_K.doDebug) { float lerped = Mathf.InverseLerp(0, totalDist, Vector3.Distance(end_v3, currentCell.centerV3)); Debuger_K.AddPath(current.path[current.path.Count - 2].centerV3 + Vector3.up, current.path[current.path.Count - 1].centerV3 + Vector3.up, new Color(lerped, 1 - lerped, 0, 1f)); } #endif if (potentialPaths.Count >= maxPaths) { break; } else { continue; } } if (excluded.Contains(currentCell)) { continue; } else { excluded.Add(currentCell); } #if UNITY_EDITOR if (Debuger_K.doDebug) { float lerped = Mathf.InverseLerp(0, totalDist, Vector3.Distance(end_v3, currentCell.centerV3)); Debuger_K.AddPath(current.path[current.path.Count - 2].centerV3 + (Vector3.up * 0.3f), current.path[current.path.Count - 1].centerV3 + (Vector3.up * 0.3f), new Color(lerped, 1 - lerped, 0, 1f)); } #endif foreach (var connection in currentCell.connections) { Cell newCell = connection.connection; if (current.Contains(newCell) == false) { CellPath newPath = new CellPath(current, connection); #if UNITY_EDITOR if (Debuger_K.debugPath) { Debuger_K.AddLabel(SomeMath.MidPoint(current.last.centerV3, newCell.centerV3), connection.Cost(properties, ignoreCrouchCost), DebugGroup.path); } #endif newPath.g = current.g + connection.Cost(properties, ignoreCrouchCost); if (connection is CellContentPointedConnection) { newPath.h = EuclideanDistance((connection as CellContentPointedConnection).exitPoint); //Debuger3.AddLabel((connection as CellPointedConnection).exitPoint, newPath.h); } else { newPath.h = EuclideanDistance(connection.connection); } AddCellNode(newPath); } } } }
//just use Dijkstra to find nearest cover private List <NodeCoverPoint> SearchCover() { List <NodeCoverPoint> result = new List <NodeCoverPoint>(); CellPath path = new CellPath(startCell, start_v3); if (startCell.covers != null) { result.AddRange(startCell.covers); } excluded.Add(startCell); foreach (var connection in startCell.connections) { CellPath newPath = new CellPath(path, connection); newPath.g = connection.Cost(start_v3, properties, ignoreCrouchCost); AddCellNode(newPath); } while (true) { CellPath current = TakeCellNode(); if (current == null || current.g > maxMoveCost) { break; } Cell currentCell = current.last; if (excluded.Contains(currentCell)) { continue; } else { excluded.Add(currentCell); } #if UNITY_EDITOR if (Debuger_K.debugPath) { Debuger_K.AddPath(current.path[current.path.Count - 2].centerV3, current.path[current.path.Count - 1].centerV3, Color.green); } #endif if (currentCell.covers != null) { result.AddRange(currentCell.covers); } foreach (var connection in currentCell.connections) { Cell newCell = connection.connection; if (current.Contains(newCell) == false) { CellPath newPath = new CellPath(current, connection); newPath.g = current.g + connection.Cost(properties, ignoreCrouchCost); AddCellNode(newPath); } } } return(result); }