public virtual void InitializeFinder(NavNode from, Vector3 to, NavGrid grid) { g_Grid = grid; g_FromNode = from; g_TargetLocation = to; g_gVal = new Dictionary <NavNode, float>(); g_fVal = new Dictionary <NavNode, float>(); g_ClosedListA = new HashSet <NavNode>(); g_ClosedListH = new HashSet <NavNode>(); g_OpenList = new HashSet <NavNode>(); g_Parents = new Dictionary <NavNode, NavNode>(); g_Fringe = new SortedList <float, NavNode>(new NPCUtils.DuplicateKeyComparer <float>()); foreach (NavIncAStar s in g_Pathfinders) { s.g_Grid = g_Grid; s.g_gVal = g_gVal; s.g_fVal = g_fVal; s.g_FromNode = g_FromNode; s.g_TargetLocation = g_TargetLocation; s.g_Parents = g_Parents; s.g_ClosedListA = g_ClosedListA; s.g_ClosedListH = g_ClosedListH; s.g_OpenList = new HashSet <NavNode>(); s.g_Fringe = new SortedList <float, NavNode>(new NPCUtils.DuplicateKeyComparer <float>()); s.g_Pathfinders = g_Pathfinders; s.g_Fringe.Add(s.ComputeNodeHeuristic(s.g_FromNode), s.g_FromNode); s.g_OpenList.Add(s.g_FromNode); } }
private void rebakeNavGridIfDirty(NavGrid grid, Layer layer) { if (layer.navGridDirty) { // Regenerate the NavGrid. int size = layer.size; Node[,] nodes = grid.nodes; for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { Vector2 worldPoint = new Vector2(x + 0.5f, y + 0.5f); CellData data = layer.getCellState(x, y).data; nodes[x, y] = new Node( worldPoint, x, y, layer.depth, data.zMoveDirections, data.movementCost); } } grid.nodes = nodes; layer.navGridDirty = false; } }
/// <summary> /// Applied before InitializeGraph runs. /// </summary> internal static void Postfix(NavGrid __instance) { if (NavFences.AllFences.TryGetValue(__instance.id, out NavFences fences)) { fences.UpdateSerial(); } }
public NavGraph(int cell_count, NavGrid nav_grid) { grid = new HandleVector <NavGraphEdge> .Handle[nav_grid.maxLinksPerCell * cell_count]; for (int i = 0; i < grid.Length; i++) { grid[i] = HandleVector <NavGraphEdge> .InvalidHandle; } edges = new HandleVector <NavGraphEdge>(cell_count); for (int j = 0; j < cell_count; j++) { int num = j * nav_grid.maxLinksPerCell; NavGrid.Link link = nav_grid.Links[num]; while (link.link != NavGrid.InvalidHandle) { NavGraphEdge item = new NavGraphEdge { startNavType = link.startNavType, endNavType = link.endNavType, endCell = link.link, startCell = j }; HandleVector <NavGraphEdge> .Handle handle = edges.Add(item); grid[num] = handle; num++; link = nav_grid.Links[num]; } grid[num] = HandleVector <NavGraphEdge> .InvalidHandle; } }
/// <summary> /// Checks to see if the navigator can reciprocally go back and forth between a cell /// and another cell, checking only if they are immediately reachable. /// </summary> /// <param name="fromCell">The cell where the navigator is currently located.</param> /// <param name="toCell">The potential destination cell.</param> /// <param name="navGrid">The navigation grid to use for lookups.</param> /// <param name="startNavType">The navigation type to use.</param> /// <param name="abilities">The current navigator abilities.</param> /// <param name="newFlags">The flags available for this path.</param> /// <returns>true if navigation can be performed, both ways, or false otherwise.</returns> private static bool CanNavigateReciprocal(int fromCell, int toCell, NavGrid navGrid, NavType startNavType, PathFinderAbilities abilities, PathFlags flags) { var grid = PathFinder.PathGrid; bool ok = false; // Find a link from this cell to the target cell var link = FindLinkToCell(fromCell, toCell, navGrid, startNavType); if (link.link != InvalidHandle) { var endNavType = link.endNavType; var pp = new PathFinder.PotentialPath(toCell, endNavType, flags); int uwCost = grid.GetCell(toCell, endNavType, out _).underwaterCost; // Can navigate there, and has a link back? if (abilities.TraversePath(ref pp, fromCell, startNavType, link.cost, link. transitionId, uwCost) && (link = FindLinkToCell(toCell, fromCell, navGrid, endNavType)).link != InvalidHandle) { pp.cell = fromCell; pp.navType = startNavType; uwCost = grid.GetCell(fromCell, startNavType, out _).underwaterCost; ok = abilities.TraversePath(ref pp, toCell, endNavType, link.cost, link.transitionId, uwCost); } } return(ok); }
public void PlayIdle(Instance smi) { KAnimControllerBase component = smi.GetComponent <KAnimControllerBase>(); Navigator component2 = smi.GetComponent <Navigator>(); NavType nav_type = component2.CurrentNavType; Facing component3 = smi.GetComponent <Facing>(); if (component3.GetFacing()) { nav_type = NavGrid.MirrorNavType(nav_type); } if (smi.def.customIdleAnim != null) { HashedString pre_anim = HashedString.Invalid; HashedString hashedString = smi.def.customIdleAnim(smi, ref pre_anim); if (hashedString != HashedString.Invalid) { if (pre_anim != HashedString.Invalid) { component.Play(pre_anim, KAnim.PlayMode.Once, 1f, 0f); } component.Queue(hashedString, KAnim.PlayMode.Loop, 1f, 0f); return; } } HashedString idleAnim = component2.NavGrid.GetIdleAnim(nav_type); component.Play(idleAnim, KAnim.PlayMode.Loop, 1f, 0f); }
/// <summary> /// Checks to see if this cached path is still valid. If not, the cached parameters are /// updated assuming that pathing is recalculated. /// </summary> /// <param name="navGrid">The navigation grid to use.</param> /// <param name="newCell">The starting cell.</param> /// <param name="navType">The navigation type currently in use.</param> /// <param name="abilities">The path finder's current abilities.</param> /// <param name="newFlags">The path finder's current flags.</param> /// <returns>true if cached information can be used, or false otherwise.</returns> public bool CheckAndUpdate(NavGrid navGrid, int newCell, NavType navType, PathFinderAbilities abilities, PathFlags newFlags) { if (navGrid == null) { throw new ArgumentNullException("navGrid"); } if (abilities == null) { throw new ArgumentNullException("abilities"); } bool ok = false; if (NavFences.AllFences.TryGetValue(navGrid.id, out NavFences fences)) { long serial = fences.CurrentSerial; if (serial == globalSerial && flags == newFlags) { ok = (cell == newCell) || CanNavigateReciprocal(cell, newCell, navGrid, navType, abilities, newFlags); } else { // Guaranteed out of date flags = newFlags; globalSerial = serial; } cell = newCell; } return(ok); }
public void DryRunAlgorithm() { if (DryRunAlgo) { RaycastHit hit; List <Vector3> pathList = new List <Vector3>(); if (Physics.Raycast(new Ray(transform.position + (transform.up * 0.2f), -1 * transform.up), out hit)) { g_Grid = hit.collider.GetComponent <NavGrid>(); int succeed = 0; while (succeed < DryRunTests) { int x = Mathf.RoundToInt(UnityEngine.Random.Range(0, g_Grid.GridDimensions.x - 1)), y = Mathf.RoundToInt(UnityEngine.Random.Range(0, g_Grid.GridDimensions.y - 1)); g_FromNode = g_Grid.GetGridNode(x, y); if (g_FromNode != null) { NavNode to = null; do { x = Mathf.RoundToInt(UnityEngine.Random.Range(0, g_Grid.GridDimensions.x - 1)); y = Mathf.RoundToInt(UnityEngine.Random.Range(0, g_Grid.GridDimensions.x - 1)); to = g_Grid.GetGridNode(x, y); } while (to == null); g_FromNode.SetHighlightTile(true, Color.black, 1f); FindPath(g_FromNode.Position, to.Position); succeed++; } } } g_NPCController.Debug("Finished algorithm dry run"); } DryRunAlgo = false; g_FromNode = null; }
static void DrawLine(NavGrid grid, Vector2i start, Vector2i end) { Vector2 startPosition = grid.GetCasePosition(start); Vector2 endPosition = grid.GetCasePosition(end); Gizmos.color = grid.Color; Gizmos.DrawLine(startPosition, endPosition); }
public bool CanWalk(IMoveable <Node> moveable, Vector2Int from, Vector2Int to) { nodeCache.Clear(); NavGrid.GetLine(nodeCache, from, to); nodeCache.RemoveAt(0); nodeCache.RemoveAt(nodeCache.Count - 1); return(nodeCache.IsLineWalkable(moveable)); }
//Splits the NavGrid into SubGrids for display purposes //This is needed as drawing too many Handles at once slows down the editor public static SubGrid CreateSubGrids(NavGrid navGrid, int nodeRenderLimit) { SubGrid primarySubGrid = new SubGrid(navGrid, null, new Vector2Int(), navGrid.Width, navGrid.Height); GenerateSubGrids(primarySubGrid, navGrid, nodeRenderLimit); return(primarySubGrid); }
/// <summary> /// Applied after UpdateGraph runs. /// </summary> internal static void Postfix(NavGrid __instance, HashSet <int> dirty_nav_cells) { if (dirty_nav_cells.Count > 0 && NavFences.AllFences.TryGetValue(__instance.id, out NavFences fences)) { fences.UpdateSerial(dirty_nav_cells); } }
// Not the perfect place for this function, but did it for the cache. Might make a separate script for this. /// <summary> /// Used for objects to check if an object can see amother object /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <returns></returns> public bool CanSee(Vector2Int from, Vector2Int to) { nodeCache.Clear(); NavGrid.GetLine(nodeCache, from, to); nodeCache.RemoveAt(0); nodeCache.RemoveAt(nodeCache.Count - 1); return(!nodeCache.IsLineInterrupted()); }
void OnDrawGizmosSelected() { script = gameObject.GetComponentInParent <NavGrid> (); if (!script.drawAlways) { DrawGrid(); } }
NavPoint GetRandomNavPoint(NavGrid grid) { NavPoint point = null; do { point = grid.points[Random.Range(0, grid.points.Count)]; } while (notAllowedPoints.Contains(point)); return point; }
void MeasureMyRaycastWay() { NavGrid navgrid = Map.instance.navGrid; for (int i = 0; i < coords.Count; i += 2) { navgrid.LineCast(coords[i], coords[i + 1], coordsOpti); } }
Vector2i GetRandomPointOnNavgrid(NavGrid navgrid) { Vector2i point; point.x = RandomGenerator.instance.NextInt(0, navgrid.width); point.y = RandomGenerator.instance.NextInt(0, navgrid.height); return(point); }
public override void OnInspectorGUI() { DrawDefaultInspector(); NavGrid navGrid = target as NavGrid; if (GUILayout.Button("Bake NavGrid")) { navGrid.Bake(); } }
private static void DrawGizmo(NavGrid navGrid, GizmoType gizmoType) { foreach (NavTile t in navGrid.GetComponentsInChildren<NavTile>()) { Gizmos.color = new Color(0.5f, 0.75f, 1f, 0.5f); Gizmos.DrawCube(t.transform.position + Vector3.up * 0.001f, t.GetComponent<BoxCollider>().size); Gizmos.color = new Color(0.5f, 0.5f, 0.5f, 0.5f); Gizmos.DrawWireCube(t.transform.position, t.GetComponent<BoxCollider>().size); } }
public void MoveTo(Vector2Int targetPoint, NavGrid navgrid) { //Quit if a different NavGrid was clicked if (navgrid != locationData.navgrid) { return; } //Begin Pathfinding pathfinder.BeginPathFinding(locationData, targetPoint, ReceivePath); }
protected override void OnSpawn() { base.OnSpawn(); NavGrid navGrid = Pathfinding.Instance.GetNavGrid("MinionNavGrid"); NavGrid navGrid2 = navGrid; navGrid2.OnNavGridUpdateComplete = (Action <HashSet <int> >)Delegate.Combine(navGrid2.OnNavGridUpdateComplete, new Action <HashSet <int> >(OnNavGridUpdateComplete)); NavTable navTable = navGrid.NavTable; navTable.OnValidCellChanged = (Action <int, NavType>)Delegate.Combine(navTable.OnValidCellChanged, new Action <int, NavType>(OnValidNavCellChanged)); }
public override void OnInspectorGUI() { DrawDefaultInspector(); NavGrid myScript = (NavGrid)target; if (GUILayout.Button("Build Grid")) { myScript.CreateGrid(); } }
public NavNode(Vector3 position, Vector2 gridPos, bool walkable, float radius, NavGrid grid) { g_Radius = radius; g_Position = position; g_Walkable = walkable; g_NodeType = walkable ? NODE_TYPE.WALKABLE // hard to walk is also walkable at this point : NODE_TYPE.NONWALKABLE; g_NodeStatus = IsWalkable() ? NODE_STATUS.REGULAR : NODE_STATUS.BLOCKED; g_GridPosition = gridPos; g_Grid = grid; }
private void AutoAcquireLocationData() { locationData.navgrid = NavGrid.ClosestNavGrid(this.transform.position); if (locationData.navgrid == null) { return; } locationData.coordinates = locationData.navgrid.WorldPointToNode(this.transform.position); }
private void OnEnable() { navGrid = target as NavGrid; //Get properties widthProp = serializedObject.FindProperty("width"); heightProp = serializedObject.FindProperty("height"); offsetProp = serializedObject.FindProperty("positionOffset"); newWidth = widthProp.intValue; newHeight = heightProp.intValue; }
public SubGrid(NavGrid navGrid, SubGrid parent, Vector2Int originCoordinate, int width, int height) { this.navGrid = navGrid; this.parent = parent; this.originCoordinate = originCoordinate; this.width = width; this.height = height; subGrids = new List <SubGrid>(); }
public static void Run(NavGrid nav_grid, PathFinderAbilities abilities, PotentialPath potential_path, PathFinderQuery query, ref Path path) { Run(nav_grid, abilities, potential_path, query); if (query.GetResultCell() != InvalidCell) { BuildResultPath(query.GetResultCell(), query.GetResultNavType(), ref path); } else { path.Clear(); } }
public static Bitmap Render(NavGrid navgrid, Gradient gradient, int upscale = 1) { Bitmap result = new Bitmap(navgrid.Width, navgrid.Height); float maxPathCost = navgrid.MaxPathCost; Render(result, (x, y) => { NavGrid.Node node = navgrid[x, y]; return((node.PathCost <= maxPathCost) ? gradient(node.PathCost, maxPathCost) : Color.Black); }); return(Upscale(result, upscale)); }
static void DrawGizmoGraph(NavGrid grid) { for (int i = 0; i < grid.width; i++) { for (int j = 0; j < grid.height; j++) { if (grid[i][j] != null) { DrawConnections(grid, grid[i][j].Coord); } } } }
/// <summary> /// Returns true if there is a Wall between X pos and Y pos. /// </summary> public static bool AnyWallInBetween(Vector3 startPos, Vector3 endPos) { for (var i = 0; i < startPos.Distance(endPos); i += 5) { var point = NavGrid.WorldToCell(startPos.Extend(endPos, i)); if (point.IsWall() || point.IsBuilding()) { return(true); } } return(false); }
private bool CanReachTargetDirectly() { NavGrid navGrid = Map.instance.navGrid; Vector2i?characterCoord = navGrid.GetCoordAt(_character.position); Vector2i?targetCoord = navGrid.GetCoordAt(_target.position); if (characterCoord == null || targetCoord == null) { return(false); } return(navGrid.IsClearLine(characterCoord.Value, targetCoord.Value)); }